1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049370503705137052370533705437055370563705737058370593706037061370623706337064370653706637067370683706937070370713707237073370743707537076370773707837079370803708137082370833708437085370863708737088370893709037091370923709337094370953709637097370983709937100371013710237103371043710537106371073710837109371103711137112371133711437115371163711737118371193712037121371223712337124371253712637127371283712937130371313713237133371343713537136371373713837139371403714137142371433714437145371463714737148371493715037151371523715337154371553715637157371583715937160371613716237163371643716537166371673716837169371703717137172371733717437175371763717737178371793718037181371823718337184371853718637187371883718937190371913719237193371943719537196371973719837199372003720137202372033720437205372063720737208372093721037211372123721337214372153721637217372183721937220372213722237223372243722537226372273722837229372303723137232372333723437235372363723737238372393724037241372423724337244372453724637247372483724937250372513725237253372543725537256372573725837259372603726137262372633726437265372663726737268372693727037271372723727337274372753727637277372783727937280372813728237283372843728537286372873728837289372903729137292372933729437295372963729737298372993730037301373023730337304373053730637307373083730937310373113731237313373143731537316373173731837319373203732137322373233732437325373263732737328373293733037331373323733337334373353733637337373383733937340373413734237343373443734537346373473734837349373503735137352373533735437355373563735737358373593736037361373623736337364373653736637367373683736937370373713737237373373743737537376373773737837379373803738137382373833738437385373863738737388373893739037391373923739337394373953739637397373983739937400374013740237403374043740537406374073740837409374103741137412374133741437415374163741737418374193742037421374223742337424374253742637427374283742937430374313743237433374343743537436374373743837439374403744137442374433744437445374463744737448374493745037451374523745337454374553745637457374583745937460374613746237463374643746537466374673746837469374703747137472374733747437475374763747737478374793748037481374823748337484374853748637487374883748937490374913749237493374943749537496374973749837499375003750137502375033750437505375063750737508375093751037511375123751337514375153751637517375183751937520375213752237523375243752537526375273752837529375303753137532375333753437535375363753737538375393754037541375423754337544375453754637547375483754937550375513755237553375543755537556375573755837559375603756137562375633756437565375663756737568375693757037571375723757337574375753757637577375783757937580375813758237583375843758537586375873758837589375903759137592375933759437595375963759737598375993760037601376023760337604376053760637607376083760937610376113761237613376143761537616376173761837619376203762137622376233762437625376263762737628376293763037631376323763337634376353763637637376383763937640376413764237643376443764537646376473764837649376503765137652376533765437655376563765737658376593766037661376623766337664376653766637667376683766937670376713767237673376743767537676376773767837679376803768137682376833768437685376863768737688376893769037691376923769337694376953769637697376983769937700377013770237703377043770537706377073770837709377103771137712377133771437715377163771737718377193772037721377223772337724377253772637727377283772937730377313773237733377343773537736377373773837739377403774137742377433774437745377463774737748377493775037751377523775337754377553775637757377583775937760377613776237763377643776537766377673776837769377703777137772377733777437775377763777737778377793778037781377823778337784377853778637787377883778937790377913779237793377943779537796377973779837799378003780137802378033780437805378063780737808378093781037811378123781337814378153781637817378183781937820378213782237823378243782537826378273782837829378303783137832378333783437835378363783737838378393784037841378423784337844378453784637847378483784937850378513785237853378543785537856378573785837859378603786137862378633786437865378663786737868378693787037871378723787337874378753787637877378783787937880378813788237883378843788537886378873788837889378903789137892378933789437895378963789737898378993790037901379023790337904379053790637907379083790937910379113791237913379143791537916379173791837919379203792137922379233792437925379263792737928379293793037931379323793337934379353793637937379383793937940379413794237943379443794537946379473794837949379503795137952379533795437955379563795737958379593796037961379623796337964379653796637967379683796937970379713797237973379743797537976379773797837979379803798137982379833798437985379863798737988379893799037991379923799337994379953799637997379983799938000380013800238003380043800538006380073800838009380103801138012380133801438015380163801738018380193802038021380223802338024380253802638027380283802938030380313803238033380343803538036380373803838039380403804138042380433804438045380463804738048380493805038051380523805338054380553805638057380583805938060380613806238063380643806538066380673806838069380703807138072380733807438075380763807738078380793808038081380823808338084380853808638087380883808938090380913809238093380943809538096380973809838099381003810138102381033810438105381063810738108381093811038111381123811338114381153811638117381183811938120381213812238123381243812538126381273812838129381303813138132381333813438135381363813738138381393814038141381423814338144381453814638147381483814938150381513815238153381543815538156381573815838159381603816138162381633816438165381663816738168381693817038171381723817338174381753817638177381783817938180381813818238183381843818538186381873818838189381903819138192381933819438195381963819738198381993820038201382023820338204382053820638207382083820938210382113821238213382143821538216382173821838219382203822138222382233822438225382263822738228382293823038231382323823338234382353823638237382383823938240382413824238243382443824538246382473824838249382503825138252382533825438255382563825738258382593826038261382623826338264382653826638267382683826938270382713827238273382743827538276382773827838279382803828138282382833828438285382863828738288382893829038291382923829338294382953829638297382983829938300383013830238303383043830538306383073830838309383103831138312383133831438315383163831738318383193832038321383223832338324383253832638327383283832938330383313833238333383343833538336383373833838339383403834138342383433834438345383463834738348383493835038351383523835338354383553835638357383583835938360383613836238363383643836538366383673836838369383703837138372383733837438375383763837738378383793838038381383823838338384383853838638387383883838938390383913839238393383943839538396383973839838399384003840138402384033840438405384063840738408384093841038411384123841338414384153841638417384183841938420384213842238423384243842538426384273842838429384303843138432384333843438435384363843738438384393844038441384423844338444384453844638447384483844938450384513845238453384543845538456384573845838459384603846138462384633846438465384663846738468384693847038471384723847338474384753847638477384783847938480384813848238483384843848538486384873848838489384903849138492384933849438495384963849738498384993850038501385023850338504385053850638507385083850938510385113851238513385143851538516385173851838519385203852138522385233852438525385263852738528385293853038531385323853338534385353853638537385383853938540385413854238543385443854538546385473854838549385503855138552385533855438555385563855738558385593856038561385623856338564385653856638567385683856938570385713857238573385743857538576385773857838579385803858138582385833858438585385863858738588385893859038591385923859338594385953859638597385983859938600386013860238603386043860538606386073860838609386103861138612386133861438615386163861738618386193862038621386223862338624386253862638627386283862938630386313863238633386343863538636386373863838639386403864138642386433864438645386463864738648386493865038651386523865338654386553865638657386583865938660386613866238663386643866538666386673866838669386703867138672386733867438675386763867738678386793868038681386823868338684386853868638687386883868938690386913869238693386943869538696386973869838699387003870138702387033870438705387063870738708387093871038711387123871338714387153871638717387183871938720387213872238723387243872538726387273872838729387303873138732387333873438735387363873738738387393874038741387423874338744387453874638747387483874938750387513875238753387543875538756387573875838759387603876138762387633876438765387663876738768387693877038771387723877338774387753877638777387783877938780387813878238783387843878538786387873878838789387903879138792387933879438795387963879738798387993880038801388023880338804388053880638807388083880938810388113881238813388143881538816388173881838819388203882138822388233882438825388263882738828388293883038831388323883338834388353883638837388383883938840388413884238843388443884538846388473884838849388503885138852388533885438855388563885738858388593886038861388623886338864388653886638867388683886938870388713887238873388743887538876388773887838879388803888138882388833888438885388863888738888388893889038891388923889338894388953889638897388983889938900389013890238903389043890538906389073890838909389103891138912389133891438915389163891738918389193892038921389223892338924389253892638927389283892938930389313893238933389343893538936389373893838939389403894138942389433894438945389463894738948389493895038951389523895338954389553895638957389583895938960389613896238963389643896538966389673896838969389703897138972389733897438975389763897738978389793898038981389823898338984389853898638987389883898938990389913899238993389943899538996389973899838999390003900139002390033900439005390063900739008390093901039011390123901339014390153901639017390183901939020390213902239023390243902539026390273902839029390303903139032390333903439035390363903739038390393904039041390423904339044390453904639047390483904939050390513905239053390543905539056390573905839059390603906139062390633906439065390663906739068390693907039071390723907339074390753907639077390783907939080390813908239083390843908539086390873908839089390903909139092390933909439095390963909739098390993910039101391023910339104391053910639107391083910939110391113911239113391143911539116391173911839119391203912139122391233912439125391263912739128391293913039131391323913339134391353913639137391383913939140391413914239143391443914539146391473914839149391503915139152391533915439155391563915739158391593916039161391623916339164391653916639167391683916939170391713917239173391743917539176391773917839179391803918139182391833918439185391863918739188391893919039191391923919339194391953919639197391983919939200392013920239203392043920539206392073920839209392103921139212392133921439215392163921739218392193922039221392223922339224392253922639227392283922939230392313923239233392343923539236392373923839239392403924139242392433924439245392463924739248392493925039251392523925339254392553925639257392583925939260392613926239263392643926539266392673926839269392703927139272392733927439275392763927739278392793928039281392823928339284392853928639287392883928939290392913929239293392943929539296392973929839299393003930139302393033930439305393063930739308393093931039311393123931339314393153931639317393183931939320393213932239323393243932539326393273932839329393303933139332393333933439335393363933739338393393934039341393423934339344393453934639347393483934939350393513935239353393543935539356393573935839359393603936139362393633936439365393663936739368393693937039371393723937339374393753937639377393783937939380393813938239383393843938539386393873938839389393903939139392393933939439395393963939739398393993940039401394023940339404394053940639407394083940939410394113941239413394143941539416394173941839419394203942139422394233942439425394263942739428394293943039431394323943339434394353943639437394383943939440394413944239443394443944539446394473944839449394503945139452394533945439455394563945739458394593946039461394623946339464394653946639467394683946939470394713947239473394743947539476394773947839479394803948139482394833948439485394863948739488394893949039491394923949339494394953949639497394983949939500395013950239503395043950539506395073950839509395103951139512395133951439515395163951739518395193952039521395223952339524395253952639527395283952939530395313953239533395343953539536395373953839539395403954139542395433954439545395463954739548395493955039551395523955339554395553955639557395583955939560395613956239563395643956539566395673956839569395703957139572395733957439575395763957739578395793958039581395823958339584395853958639587395883958939590395913959239593395943959539596395973959839599396003960139602396033960439605396063960739608396093961039611396123961339614396153961639617396183961939620396213962239623396243962539626396273962839629396303963139632396333963439635396363963739638396393964039641396423964339644396453964639647396483964939650396513965239653396543965539656396573965839659396603966139662396633966439665396663966739668396693967039671396723967339674396753967639677396783967939680396813968239683396843968539686396873968839689396903969139692396933969439695396963969739698396993970039701397023970339704397053970639707397083970939710397113971239713397143971539716397173971839719397203972139722397233972439725397263972739728397293973039731397323973339734397353973639737397383973939740397413974239743397443974539746397473974839749397503975139752397533975439755397563975739758397593976039761397623976339764397653976639767397683976939770397713977239773397743977539776397773977839779397803978139782397833978439785397863978739788397893979039791397923979339794397953979639797397983979939800398013980239803398043980539806398073980839809398103981139812398133981439815398163981739818398193982039821398223982339824398253982639827398283982939830398313983239833398343983539836398373983839839398403984139842398433984439845398463984739848398493985039851398523985339854398553985639857398583985939860398613986239863398643986539866398673986839869398703987139872398733987439875398763987739878398793988039881398823988339884398853988639887398883988939890398913989239893398943989539896398973989839899399003990139902399033990439905399063990739908399093991039911399123991339914399153991639917399183991939920399213992239923399243992539926399273992839929399303993139932399333993439935399363993739938399393994039941399423994339944399453994639947399483994939950399513995239953399543995539956399573995839959399603996139962399633996439965399663996739968399693997039971399723997339974399753997639977399783997939980399813998239983399843998539986399873998839989399903999139992399933999439995399963999739998399994000040001400024000340004400054000640007400084000940010400114001240013400144001540016400174001840019400204002140022400234002440025400264002740028400294003040031400324003340034400354003640037400384003940040400414004240043400444004540046400474004840049400504005140052400534005440055400564005740058400594006040061400624006340064400654006640067400684006940070400714007240073400744007540076400774007840079400804008140082400834008440085400864008740088400894009040091400924009340094400954009640097400984009940100401014010240103401044010540106401074010840109401104011140112401134011440115401164011740118401194012040121401224012340124401254012640127401284012940130401314013240133401344013540136401374013840139401404014140142401434014440145401464014740148401494015040151401524015340154401554015640157401584015940160401614016240163401644016540166401674016840169401704017140172401734017440175401764017740178401794018040181401824018340184401854018640187401884018940190401914019240193401944019540196401974019840199402004020140202402034020440205402064020740208402094021040211402124021340214402154021640217402184021940220402214022240223402244022540226402274022840229402304023140232402334023440235402364023740238402394024040241402424024340244402454024640247402484024940250402514025240253402544025540256402574025840259402604026140262402634026440265402664026740268402694027040271402724027340274402754027640277402784027940280402814028240283402844028540286402874028840289402904029140292402934029440295402964029740298402994030040301403024030340304403054030640307403084030940310403114031240313403144031540316403174031840319403204032140322403234032440325403264032740328403294033040331403324033340334403354033640337403384033940340403414034240343403444034540346403474034840349403504035140352403534035440355403564035740358403594036040361403624036340364403654036640367403684036940370403714037240373403744037540376403774037840379403804038140382403834038440385403864038740388403894039040391403924039340394403954039640397403984039940400404014040240403404044040540406404074040840409404104041140412404134041440415404164041740418404194042040421404224042340424404254042640427404284042940430404314043240433404344043540436404374043840439404404044140442404434044440445404464044740448404494045040451404524045340454404554045640457404584045940460404614046240463404644046540466404674046840469404704047140472404734047440475404764047740478404794048040481404824048340484404854048640487404884048940490404914049240493404944049540496404974049840499405004050140502405034050440505405064050740508405094051040511405124051340514405154051640517405184051940520405214052240523405244052540526405274052840529405304053140532405334053440535405364053740538405394054040541405424054340544405454054640547405484054940550405514055240553405544055540556405574055840559405604056140562405634056440565405664056740568405694057040571405724057340574405754057640577405784057940580405814058240583405844058540586405874058840589405904059140592405934059440595405964059740598405994060040601406024060340604406054060640607406084060940610406114061240613406144061540616406174061840619406204062140622406234062440625406264062740628406294063040631406324063340634406354063640637406384063940640406414064240643406444064540646406474064840649406504065140652406534065440655406564065740658406594066040661406624066340664406654066640667406684066940670406714067240673406744067540676406774067840679406804068140682406834068440685406864068740688406894069040691406924069340694406954069640697406984069940700407014070240703407044070540706407074070840709407104071140712407134071440715407164071740718407194072040721407224072340724407254072640727407284072940730407314073240733407344073540736407374073840739407404074140742407434074440745407464074740748407494075040751407524075340754407554075640757407584075940760407614076240763407644076540766407674076840769407704077140772407734077440775407764077740778407794078040781407824078340784407854078640787407884078940790407914079240793407944079540796407974079840799408004080140802408034080440805408064080740808408094081040811408124081340814408154081640817408184081940820408214082240823408244082540826408274082840829408304083140832408334083440835408364083740838408394084040841408424084340844408454084640847408484084940850408514085240853408544085540856408574085840859408604086140862408634086440865408664086740868408694087040871408724087340874408754087640877408784087940880408814088240883408844088540886408874088840889408904089140892408934089440895408964089740898408994090040901409024090340904409054090640907409084090940910409114091240913409144091540916409174091840919409204092140922409234092440925409264092740928409294093040931409324093340934409354093640937409384093940940409414094240943409444094540946409474094840949409504095140952409534095440955409564095740958409594096040961409624096340964409654096640967409684096940970409714097240973409744097540976409774097840979409804098140982409834098440985409864098740988409894099040991409924099340994409954099640997409984099941000410014100241003410044100541006410074100841009410104101141012410134101441015410164101741018410194102041021410224102341024410254102641027410284102941030410314103241033410344103541036410374103841039410404104141042410434104441045410464104741048410494105041051410524105341054410554105641057410584105941060410614106241063410644106541066410674106841069410704107141072410734107441075410764107741078410794108041081410824108341084410854108641087410884108941090410914109241093410944109541096410974109841099411004110141102411034110441105411064110741108411094111041111411124111341114411154111641117411184111941120411214112241123411244112541126411274112841129411304113141132411334113441135411364113741138411394114041141411424114341144411454114641147411484114941150411514115241153411544115541156411574115841159411604116141162411634116441165411664116741168411694117041171411724117341174411754117641177411784117941180411814118241183411844118541186411874118841189411904119141192411934119441195411964119741198411994120041201412024120341204412054120641207412084120941210412114121241213412144121541216412174121841219412204122141222412234122441225412264122741228412294123041231412324123341234412354123641237412384123941240412414124241243412444124541246412474124841249412504125141252412534125441255412564125741258412594126041261412624126341264412654126641267412684126941270412714127241273412744127541276412774127841279412804128141282412834128441285412864128741288412894129041291412924129341294412954129641297412984129941300413014130241303413044130541306413074130841309413104131141312413134131441315413164131741318413194132041321413224132341324413254132641327413284132941330413314133241333413344133541336413374133841339413404134141342413434134441345413464134741348413494135041351413524135341354413554135641357413584135941360413614136241363413644136541366413674136841369413704137141372413734137441375413764137741378413794138041381413824138341384413854138641387413884138941390413914139241393413944139541396413974139841399414004140141402414034140441405414064140741408414094141041411414124141341414414154141641417414184141941420414214142241423414244142541426414274142841429414304143141432414334143441435414364143741438414394144041441414424144341444414454144641447414484144941450414514145241453414544145541456414574145841459414604146141462414634146441465414664146741468414694147041471414724147341474414754147641477414784147941480414814148241483414844148541486414874148841489414904149141492414934149441495414964149741498414994150041501415024150341504415054150641507415084150941510415114151241513415144151541516415174151841519415204152141522415234152441525415264152741528415294153041531415324153341534415354153641537415384153941540415414154241543415444154541546415474154841549415504155141552415534155441555415564155741558415594156041561415624156341564415654156641567415684156941570415714157241573415744157541576415774157841579415804158141582415834158441585415864158741588415894159041591415924159341594415954159641597415984159941600416014160241603416044160541606416074160841609416104161141612416134161441615416164161741618416194162041621416224162341624416254162641627416284162941630416314163241633416344163541636416374163841639416404164141642416434164441645416464164741648416494165041651416524165341654416554165641657416584165941660416614166241663416644166541666416674166841669416704167141672416734167441675416764167741678416794168041681416824168341684416854168641687416884168941690416914169241693416944169541696416974169841699417004170141702417034170441705417064170741708417094171041711417124171341714417154171641717417184171941720417214172241723417244172541726417274172841729417304173141732417334173441735417364173741738417394174041741417424174341744417454174641747417484174941750417514175241753417544175541756417574175841759417604176141762417634176441765417664176741768417694177041771417724177341774417754177641777417784177941780417814178241783417844178541786417874178841789417904179141792417934179441795417964179741798417994180041801418024180341804418054180641807418084180941810418114181241813418144181541816418174181841819418204182141822418234182441825418264182741828418294183041831418324183341834418354183641837418384183941840418414184241843418444184541846418474184841849418504185141852418534185441855418564185741858418594186041861418624186341864418654186641867418684186941870418714187241873418744187541876418774187841879418804188141882418834188441885418864188741888418894189041891418924189341894418954189641897418984189941900419014190241903419044190541906419074190841909419104191141912419134191441915419164191741918419194192041921419224192341924419254192641927419284192941930419314193241933419344193541936419374193841939419404194141942419434194441945419464194741948419494195041951419524195341954419554195641957419584195941960419614196241963419644196541966419674196841969419704197141972419734197441975419764197741978419794198041981419824198341984419854198641987419884198941990419914199241993419944199541996419974199841999420004200142002420034200442005420064200742008420094201042011420124201342014420154201642017420184201942020420214202242023420244202542026420274202842029420304203142032420334203442035420364203742038420394204042041420424204342044420454204642047420484204942050420514205242053420544205542056420574205842059420604206142062420634206442065420664206742068420694207042071420724207342074420754207642077420784207942080420814208242083420844208542086420874208842089420904209142092420934209442095420964209742098420994210042101421024210342104421054210642107421084210942110421114211242113421144211542116421174211842119421204212142122421234212442125421264212742128421294213042131421324213342134421354213642137421384213942140421414214242143421444214542146421474214842149421504215142152421534215442155421564215742158421594216042161421624216342164421654216642167421684216942170421714217242173421744217542176421774217842179421804218142182421834218442185421864218742188421894219042191421924219342194421954219642197421984219942200422014220242203422044220542206422074220842209422104221142212422134221442215422164221742218422194222042221422224222342224422254222642227422284222942230422314223242233422344223542236422374223842239422404224142242422434224442245422464224742248422494225042251422524225342254422554225642257422584225942260422614226242263422644226542266422674226842269422704227142272422734227442275422764227742278422794228042281422824228342284422854228642287422884228942290422914229242293422944229542296422974229842299423004230142302423034230442305423064230742308423094231042311423124231342314423154231642317423184231942320423214232242323423244232542326423274232842329423304233142332423334233442335423364233742338423394234042341423424234342344423454234642347423484234942350423514235242353423544235542356423574235842359423604236142362423634236442365423664236742368423694237042371423724237342374423754237642377423784237942380423814238242383423844238542386423874238842389423904239142392423934239442395423964239742398423994240042401424024240342404424054240642407424084240942410424114241242413424144241542416424174241842419424204242142422424234242442425424264242742428424294243042431424324243342434424354243642437424384243942440424414244242443424444244542446424474244842449424504245142452424534245442455424564245742458424594246042461424624246342464424654246642467424684246942470424714247242473424744247542476424774247842479424804248142482424834248442485424864248742488424894249042491424924249342494424954249642497424984249942500425014250242503425044250542506425074250842509425104251142512425134251442515425164251742518425194252042521425224252342524425254252642527425284252942530425314253242533425344253542536425374253842539425404254142542425434254442545425464254742548425494255042551425524255342554425554255642557425584255942560425614256242563425644256542566425674256842569425704257142572425734257442575425764257742578425794258042581425824258342584425854258642587425884258942590425914259242593425944259542596425974259842599426004260142602426034260442605426064260742608426094261042611426124261342614426154261642617426184261942620426214262242623426244262542626426274262842629426304263142632426334263442635426364263742638426394264042641426424264342644426454264642647426484264942650426514265242653426544265542656426574265842659426604266142662426634266442665426664266742668426694267042671426724267342674426754267642677426784267942680426814268242683426844268542686426874268842689426904269142692426934269442695426964269742698426994270042701427024270342704427054270642707427084270942710427114271242713427144271542716427174271842719427204272142722427234272442725427264272742728427294273042731427324273342734427354273642737427384273942740427414274242743427444274542746427474274842749427504275142752427534275442755427564275742758427594276042761427624276342764427654276642767427684276942770427714277242773427744277542776427774277842779427804278142782427834278442785427864278742788427894279042791427924279342794427954279642797427984279942800428014280242803428044280542806428074280842809428104281142812428134281442815428164281742818428194282042821428224282342824428254282642827428284282942830428314283242833428344283542836428374283842839428404284142842428434284442845428464284742848428494285042851428524285342854428554285642857428584285942860428614286242863428644286542866428674286842869428704287142872428734287442875428764287742878428794288042881428824288342884428854288642887428884288942890428914289242893428944289542896428974289842899429004290142902429034290442905429064290742908429094291042911429124291342914429154291642917429184291942920429214292242923429244292542926429274292842929429304293142932429334293442935429364293742938429394294042941429424294342944429454294642947429484294942950429514295242953429544295542956429574295842959429604296142962429634296442965429664296742968429694297042971429724297342974429754297642977429784297942980429814298242983429844298542986429874298842989429904299142992429934299442995429964299742998429994300043001430024300343004430054300643007430084300943010430114301243013430144301543016430174301843019430204302143022430234302443025430264302743028430294303043031430324303343034430354303643037430384303943040430414304243043430444304543046430474304843049430504305143052430534305443055430564305743058430594306043061430624306343064430654306643067430684306943070430714307243073430744307543076430774307843079430804308143082430834308443085430864308743088430894309043091430924309343094430954309643097430984309943100431014310243103431044310543106431074310843109431104311143112431134311443115431164311743118431194312043121431224312343124431254312643127431284312943130431314313243133431344313543136431374313843139431404314143142431434314443145431464314743148431494315043151431524315343154431554315643157431584315943160431614316243163431644316543166431674316843169431704317143172431734317443175431764317743178431794318043181431824318343184431854318643187431884318943190431914319243193431944319543196431974319843199432004320143202432034320443205432064320743208432094321043211432124321343214432154321643217432184321943220432214322243223432244322543226432274322843229432304323143232432334323443235432364323743238432394324043241432424324343244432454324643247432484324943250432514325243253432544325543256432574325843259432604326143262432634326443265432664326743268432694327043271432724327343274432754327643277432784327943280432814328243283432844328543286432874328843289432904329143292432934329443295432964329743298432994330043301433024330343304433054330643307433084330943310433114331243313433144331543316433174331843319433204332143322433234332443325433264332743328433294333043331433324333343334433354333643337433384333943340433414334243343433444334543346433474334843349433504335143352433534335443355433564335743358433594336043361433624336343364433654336643367433684336943370433714337243373433744337543376433774337843379433804338143382433834338443385433864338743388433894339043391433924339343394433954339643397433984339943400434014340243403434044340543406434074340843409434104341143412434134341443415434164341743418434194342043421434224342343424434254342643427434284342943430434314343243433434344343543436434374343843439434404344143442434434344443445434464344743448434494345043451434524345343454434554345643457434584345943460434614346243463434644346543466434674346843469434704347143472434734347443475434764347743478434794348043481434824348343484434854348643487434884348943490434914349243493434944349543496434974349843499435004350143502435034350443505435064350743508435094351043511435124351343514435154351643517435184351943520435214352243523435244352543526435274352843529435304353143532435334353443535435364353743538435394354043541435424354343544435454354643547435484354943550435514355243553435544355543556435574355843559435604356143562435634356443565435664356743568435694357043571435724357343574435754357643577435784357943580435814358243583435844358543586435874358843589435904359143592435934359443595435964359743598435994360043601436024360343604436054360643607436084360943610436114361243613436144361543616436174361843619436204362143622436234362443625436264362743628436294363043631436324363343634436354363643637436384363943640436414364243643436444364543646436474364843649436504365143652436534365443655436564365743658436594366043661436624366343664436654366643667436684366943670436714367243673436744367543676436774367843679436804368143682436834368443685436864368743688436894369043691436924369343694436954369643697436984369943700437014370243703437044370543706437074370843709437104371143712437134371443715437164371743718437194372043721437224372343724437254372643727437284372943730437314373243733437344373543736437374373843739437404374143742437434374443745437464374743748437494375043751437524375343754437554375643757437584375943760437614376243763437644376543766437674376843769437704377143772437734377443775437764377743778437794378043781437824378343784437854378643787437884378943790437914379243793437944379543796437974379843799438004380143802438034380443805438064380743808438094381043811438124381343814438154381643817438184381943820438214382243823438244382543826438274382843829438304383143832438334383443835438364383743838438394384043841438424384343844438454384643847438484384943850438514385243853438544385543856438574385843859438604386143862438634386443865438664386743868438694387043871438724387343874438754387643877438784387943880438814388243883438844388543886438874388843889438904389143892438934389443895438964389743898438994390043901439024390343904439054390643907439084390943910439114391243913439144391543916439174391843919439204392143922439234392443925439264392743928439294393043931439324393343934439354393643937439384393943940439414394243943439444394543946439474394843949439504395143952439534395443955439564395743958439594396043961439624396343964439654396643967439684396943970439714397243973439744397543976439774397843979439804398143982439834398443985439864398743988439894399043991439924399343994439954399643997439984399944000440014400244003440044400544006440074400844009440104401144012440134401444015440164401744018440194402044021440224402344024440254402644027440284402944030440314403244033440344403544036440374403844039440404404144042440434404444045440464404744048440494405044051440524405344054440554405644057440584405944060440614406244063440644406544066440674406844069440704407144072440734407444075440764407744078440794408044081440824408344084440854408644087440884408944090440914409244093440944409544096440974409844099441004410144102441034410444105441064410744108441094411044111441124411344114441154411644117441184411944120441214412244123441244412544126441274412844129441304413144132441334413444135441364413744138441394414044141441424414344144441454414644147441484414944150441514415244153441544415544156441574415844159441604416144162441634416444165441664416744168441694417044171441724417344174441754417644177441784417944180441814418244183441844418544186441874418844189441904419144192441934419444195441964419744198441994420044201442024420344204442054420644207442084420944210442114421244213442144421544216442174421844219442204422144222442234422444225442264422744228442294423044231442324423344234442354423644237442384423944240442414424244243442444424544246442474424844249442504425144252442534425444255442564425744258442594426044261442624426344264442654426644267442684426944270442714427244273442744427544276442774427844279442804428144282442834428444285442864428744288442894429044291442924429344294442954429644297442984429944300443014430244303443044430544306443074430844309443104431144312443134431444315443164431744318443194432044321443224432344324443254432644327443284432944330443314433244333443344433544336443374433844339443404434144342443434434444345443464434744348443494435044351443524435344354443554435644357443584435944360443614436244363443644436544366443674436844369443704437144372443734437444375443764437744378443794438044381443824438344384443854438644387443884438944390443914439244393443944439544396443974439844399444004440144402444034440444405444064440744408444094441044411444124441344414444154441644417444184441944420444214442244423444244442544426444274442844429444304443144432444334443444435444364443744438444394444044441444424444344444444454444644447444484444944450444514445244453444544445544456444574445844459444604446144462444634446444465444664446744468444694447044471444724447344474444754447644477444784447944480444814448244483444844448544486444874448844489444904449144492444934449444495444964449744498444994450044501445024450344504445054450644507445084450944510445114451244513445144451544516445174451844519445204452144522445234452444525445264452744528445294453044531445324453344534445354453644537445384453944540445414454244543445444454544546445474454844549445504455144552445534455444555445564455744558445594456044561445624456344564445654456644567445684456944570445714457244573445744457544576445774457844579445804458144582445834458444585445864458744588445894459044591445924459344594445954459644597445984459944600446014460244603446044460544606446074460844609446104461144612446134461444615446164461744618446194462044621446224462344624446254462644627446284462944630446314463244633446344463544636446374463844639446404464144642446434464444645446464464744648446494465044651446524465344654446554465644657446584465944660446614466244663446644466544666446674466844669446704467144672446734467444675446764467744678446794468044681446824468344684446854468644687446884468944690446914469244693446944469544696446974469844699447004470144702447034470444705447064470744708447094471044711447124471344714447154471644717447184471944720447214472244723447244472544726447274472844729447304473144732447334473444735447364473744738447394474044741447424474344744447454474644747447484474944750447514475244753447544475544756447574475844759447604476144762447634476444765447664476744768447694477044771447724477344774447754477644777447784477944780447814478244783447844478544786447874478844789447904479144792447934479444795447964479744798447994480044801448024480344804448054480644807448084480944810448114481244813448144481544816448174481844819448204482144822448234482444825448264482744828448294483044831448324483344834448354483644837448384483944840448414484244843448444484544846448474484844849448504485144852448534485444855448564485744858448594486044861448624486344864448654486644867448684486944870448714487244873448744487544876448774487844879448804488144882448834488444885448864488744888448894489044891448924489344894448954489644897448984489944900449014490244903449044490544906449074490844909449104491144912449134491444915449164491744918449194492044921449224492344924449254492644927449284492944930449314493244933449344493544936449374493844939449404494144942449434494444945449464494744948449494495044951449524495344954449554495644957449584495944960449614496244963449644496544966449674496844969449704497144972449734497444975449764497744978449794498044981449824498344984449854498644987449884498944990449914499244993449944499544996449974499844999450004500145002450034500445005450064500745008450094501045011450124501345014450154501645017450184501945020450214502245023450244502545026450274502845029450304503145032450334503445035450364503745038450394504045041450424504345044450454504645047450484504945050450514505245053450544505545056450574505845059450604506145062450634506445065450664506745068450694507045071450724507345074450754507645077450784507945080450814508245083450844508545086450874508845089450904509145092450934509445095450964509745098450994510045101451024510345104451054510645107451084510945110451114511245113451144511545116451174511845119451204512145122451234512445125451264512745128451294513045131451324513345134451354513645137451384513945140451414514245143451444514545146451474514845149451504515145152451534515445155451564515745158451594516045161451624516345164451654516645167451684516945170451714517245173451744517545176451774517845179451804518145182451834518445185451864518745188451894519045191451924519345194451954519645197451984519945200452014520245203452044520545206452074520845209452104521145212452134521445215452164521745218452194522045221452224522345224452254522645227452284522945230452314523245233452344523545236452374523845239452404524145242452434524445245452464524745248452494525045251452524525345254452554525645257452584525945260452614526245263452644526545266452674526845269452704527145272452734527445275452764527745278452794528045281452824528345284452854528645287452884528945290452914529245293452944529545296452974529845299453004530145302453034530445305453064530745308453094531045311453124531345314453154531645317453184531945320453214532245323453244532545326453274532845329453304533145332453334533445335453364533745338453394534045341453424534345344453454534645347453484534945350453514535245353453544535545356453574535845359453604536145362453634536445365453664536745368453694537045371453724537345374453754537645377453784537945380453814538245383453844538545386453874538845389453904539145392453934539445395453964539745398453994540045401454024540345404454054540645407454084540945410454114541245413454144541545416454174541845419454204542145422454234542445425454264542745428454294543045431454324543345434454354543645437454384543945440454414544245443454444544545446454474544845449454504545145452454534545445455454564545745458454594546045461454624546345464454654546645467454684546945470454714547245473454744547545476454774547845479454804548145482454834548445485454864548745488454894549045491454924549345494454954549645497454984549945500455014550245503455044550545506455074550845509455104551145512455134551445515455164551745518455194552045521455224552345524455254552645527455284552945530455314553245533455344553545536455374553845539455404554145542455434554445545455464554745548455494555045551455524555345554455554555645557455584555945560455614556245563455644556545566455674556845569455704557145572455734557445575455764557745578455794558045581455824558345584455854558645587455884558945590455914559245593455944559545596455974559845599456004560145602456034560445605456064560745608456094561045611456124561345614456154561645617456184561945620456214562245623456244562545626456274562845629456304563145632456334563445635456364563745638456394564045641456424564345644456454564645647456484564945650456514565245653456544565545656456574565845659456604566145662456634566445665456664566745668456694567045671456724567345674456754567645677456784567945680456814568245683456844568545686456874568845689456904569145692456934569445695456964569745698456994570045701457024570345704457054570645707457084570945710457114571245713457144571545716457174571845719457204572145722457234572445725457264572745728457294573045731457324573345734457354573645737457384573945740457414574245743457444574545746457474574845749457504575145752457534575445755457564575745758457594576045761457624576345764457654576645767457684576945770457714577245773457744577545776457774577845779457804578145782457834578445785457864578745788457894579045791457924579345794457954579645797457984579945800458014580245803458044580545806458074580845809458104581145812458134581445815458164581745818458194582045821458224582345824458254582645827458284582945830458314583245833458344583545836458374583845839458404584145842458434584445845458464584745848458494585045851458524585345854458554585645857458584585945860458614586245863458644586545866458674586845869458704587145872458734587445875458764587745878458794588045881458824588345884458854588645887458884588945890458914589245893458944589545896458974589845899459004590145902459034590445905459064590745908459094591045911459124591345914459154591645917459184591945920459214592245923459244592545926459274592845929459304593145932459334593445935459364593745938459394594045941459424594345944459454594645947459484594945950459514595245953459544595545956459574595845959459604596145962459634596445965459664596745968459694597045971459724597345974459754597645977459784597945980459814598245983459844598545986459874598845989459904599145992459934599445995459964599745998459994600046001460024600346004460054600646007460084600946010460114601246013460144601546016460174601846019460204602146022460234602446025460264602746028460294603046031460324603346034460354603646037460384603946040460414604246043460444604546046460474604846049460504605146052460534605446055460564605746058460594606046061460624606346064460654606646067460684606946070460714607246073460744607546076460774607846079460804608146082460834608446085460864608746088460894609046091460924609346094460954609646097460984609946100461014610246103461044610546106461074610846109461104611146112461134611446115461164611746118461194612046121461224612346124461254612646127461284612946130461314613246133461344613546136461374613846139461404614146142461434614446145461464614746148461494615046151461524615346154461554615646157461584615946160461614616246163461644616546166461674616846169461704617146172461734617446175461764617746178461794618046181461824618346184461854618646187461884618946190461914619246193461944619546196461974619846199462004620146202462034620446205462064620746208462094621046211462124621346214462154621646217462184621946220462214622246223462244622546226462274622846229462304623146232462334623446235462364623746238462394624046241462424624346244462454624646247462484624946250462514625246253462544625546256462574625846259462604626146262462634626446265462664626746268462694627046271462724627346274462754627646277462784627946280462814628246283462844628546286462874628846289462904629146292462934629446295462964629746298462994630046301463024630346304463054630646307463084630946310463114631246313463144631546316463174631846319463204632146322463234632446325463264632746328463294633046331463324633346334463354633646337463384633946340463414634246343463444634546346463474634846349463504635146352463534635446355463564635746358463594636046361463624636346364463654636646367463684636946370463714637246373463744637546376463774637846379463804638146382463834638446385463864638746388463894639046391463924639346394463954639646397463984639946400464014640246403464044640546406464074640846409464104641146412464134641446415464164641746418464194642046421464224642346424464254642646427464284642946430464314643246433464344643546436464374643846439464404644146442464434644446445464464644746448464494645046451464524645346454464554645646457464584645946460464614646246463464644646546466464674646846469464704647146472464734647446475464764647746478464794648046481464824648346484464854648646487464884648946490464914649246493464944649546496464974649846499465004650146502465034650446505465064650746508465094651046511465124651346514465154651646517465184651946520465214652246523465244652546526465274652846529465304653146532465334653446535465364653746538465394654046541465424654346544465454654646547465484654946550465514655246553465544655546556465574655846559465604656146562465634656446565465664656746568465694657046571465724657346574465754657646577465784657946580465814658246583465844658546586465874658846589465904659146592465934659446595465964659746598465994660046601466024660346604466054660646607466084660946610466114661246613466144661546616466174661846619466204662146622466234662446625466264662746628466294663046631466324663346634466354663646637466384663946640466414664246643466444664546646466474664846649466504665146652466534665446655466564665746658466594666046661466624666346664466654666646667466684666946670466714667246673466744667546676466774667846679466804668146682466834668446685466864668746688466894669046691466924669346694466954669646697466984669946700467014670246703467044670546706467074670846709467104671146712467134671446715467164671746718467194672046721467224672346724467254672646727467284672946730467314673246733467344673546736467374673846739467404674146742467434674446745467464674746748467494675046751467524675346754467554675646757467584675946760467614676246763467644676546766467674676846769467704677146772467734677446775467764677746778467794678046781467824678346784467854678646787467884678946790467914679246793467944679546796467974679846799468004680146802468034680446805468064680746808468094681046811468124681346814468154681646817468184681946820468214682246823468244682546826468274682846829468304683146832468334683446835468364683746838468394684046841468424684346844468454684646847468484684946850468514685246853468544685546856468574685846859468604686146862468634686446865468664686746868468694687046871468724687346874468754687646877468784687946880468814688246883468844688546886468874688846889468904689146892468934689446895468964689746898468994690046901469024690346904469054690646907469084690946910469114691246913469144691546916469174691846919469204692146922469234692446925469264692746928469294693046931469324693346934469354693646937469384693946940469414694246943469444694546946469474694846949469504695146952469534695446955469564695746958469594696046961469624696346964469654696646967469684696946970469714697246973469744697546976469774697846979469804698146982469834698446985469864698746988469894699046991469924699346994469954699646997469984699947000470014700247003470044700547006470074700847009470104701147012470134701447015470164701747018470194702047021470224702347024470254702647027470284702947030470314703247033470344703547036470374703847039470404704147042470434704447045470464704747048470494705047051470524705347054470554705647057470584705947060470614706247063470644706547066470674706847069470704707147072470734707447075470764707747078470794708047081470824708347084470854708647087470884708947090470914709247093470944709547096470974709847099471004710147102471034710447105471064710747108471094711047111471124711347114471154711647117471184711947120471214712247123471244712547126471274712847129471304713147132471334713447135471364713747138471394714047141471424714347144471454714647147471484714947150471514715247153471544715547156471574715847159471604716147162471634716447165471664716747168471694717047171471724717347174471754717647177471784717947180471814718247183471844718547186471874718847189471904719147192471934719447195471964719747198471994720047201472024720347204472054720647207472084720947210472114721247213472144721547216472174721847219472204722147222472234722447225472264722747228472294723047231472324723347234472354723647237472384723947240472414724247243472444724547246472474724847249472504725147252472534725447255472564725747258472594726047261472624726347264472654726647267472684726947270472714727247273472744727547276472774727847279472804728147282472834728447285472864728747288472894729047291472924729347294472954729647297472984729947300473014730247303473044730547306473074730847309473104731147312473134731447315473164731747318473194732047321473224732347324473254732647327473284732947330473314733247333473344733547336473374733847339473404734147342473434734447345473464734747348473494735047351473524735347354473554735647357473584735947360473614736247363473644736547366473674736847369473704737147372473734737447375473764737747378473794738047381473824738347384473854738647387473884738947390473914739247393473944739547396473974739847399474004740147402474034740447405474064740747408474094741047411474124741347414474154741647417474184741947420474214742247423474244742547426474274742847429474304743147432474334743447435474364743747438474394744047441474424744347444474454744647447474484744947450474514745247453474544745547456474574745847459474604746147462474634746447465474664746747468474694747047471474724747347474474754747647477474784747947480474814748247483474844748547486474874748847489474904749147492474934749447495474964749747498474994750047501475024750347504475054750647507475084750947510475114751247513475144751547516475174751847519475204752147522475234752447525475264752747528475294753047531475324753347534475354753647537475384753947540475414754247543475444754547546475474754847549475504755147552475534755447555475564755747558475594756047561475624756347564475654756647567475684756947570475714757247573475744757547576475774757847579475804758147582475834758447585475864758747588475894759047591475924759347594475954759647597475984759947600476014760247603476044760547606476074760847609476104761147612476134761447615476164761747618476194762047621476224762347624476254762647627476284762947630476314763247633476344763547636476374763847639476404764147642476434764447645476464764747648476494765047651476524765347654476554765647657476584765947660476614766247663476644766547666476674766847669476704767147672476734767447675476764767747678476794768047681476824768347684476854768647687476884768947690476914769247693476944769547696476974769847699477004770147702477034770447705477064770747708477094771047711477124771347714477154771647717477184771947720477214772247723477244772547726477274772847729477304773147732477334773447735477364773747738477394774047741477424774347744477454774647747477484774947750477514775247753477544775547756477574775847759477604776147762477634776447765477664776747768477694777047771477724777347774477754777647777477784777947780477814778247783477844778547786477874778847789477904779147792477934779447795477964779747798477994780047801478024780347804478054780647807478084780947810478114781247813478144781547816478174781847819478204782147822478234782447825478264782747828478294783047831478324783347834478354783647837478384783947840478414784247843478444784547846478474784847849478504785147852478534785447855478564785747858478594786047861478624786347864478654786647867478684786947870478714787247873478744787547876478774787847879478804788147882478834788447885478864788747888478894789047891478924789347894478954789647897478984789947900479014790247903479044790547906479074790847909479104791147912479134791447915479164791747918479194792047921479224792347924479254792647927479284792947930479314793247933479344793547936479374793847939479404794147942479434794447945479464794747948479494795047951479524795347954479554795647957479584795947960479614796247963479644796547966479674796847969479704797147972479734797447975479764797747978479794798047981479824798347984479854798647987479884798947990479914799247993479944799547996479974799847999480004800148002480034800448005480064800748008480094801048011480124801348014480154801648017480184801948020480214802248023480244802548026480274802848029480304803148032480334803448035480364803748038480394804048041480424804348044480454804648047480484804948050480514805248053480544805548056480574805848059480604806148062480634806448065480664806748068480694807048071480724807348074480754807648077480784807948080480814808248083480844808548086480874808848089480904809148092480934809448095480964809748098480994810048101481024810348104481054810648107481084810948110481114811248113481144811548116481174811848119481204812148122481234812448125481264812748128481294813048131481324813348134481354813648137481384813948140481414814248143481444814548146481474814848149481504815148152481534815448155481564815748158481594816048161481624816348164481654816648167481684816948170481714817248173481744817548176481774817848179481804818148182481834818448185481864818748188481894819048191481924819348194481954819648197481984819948200482014820248203482044820548206482074820848209482104821148212482134821448215482164821748218482194822048221482224822348224482254822648227482284822948230482314823248233482344823548236482374823848239482404824148242482434824448245482464824748248482494825048251482524825348254482554825648257482584825948260482614826248263482644826548266482674826848269482704827148272482734827448275482764827748278482794828048281482824828348284482854828648287482884828948290482914829248293482944829548296482974829848299483004830148302483034830448305483064830748308483094831048311483124831348314483154831648317483184831948320483214832248323483244832548326483274832848329483304833148332483334833448335483364833748338483394834048341483424834348344483454834648347483484834948350483514835248353483544835548356483574835848359483604836148362483634836448365483664836748368483694837048371483724837348374483754837648377483784837948380483814838248383483844838548386483874838848389483904839148392483934839448395483964839748398483994840048401484024840348404484054840648407484084840948410484114841248413484144841548416484174841848419484204842148422484234842448425484264842748428484294843048431484324843348434484354843648437484384843948440484414844248443484444844548446484474844848449484504845148452484534845448455484564845748458484594846048461484624846348464484654846648467484684846948470484714847248473484744847548476484774847848479484804848148482484834848448485484864848748488484894849048491484924849348494484954849648497484984849948500485014850248503485044850548506485074850848509485104851148512485134851448515485164851748518485194852048521485224852348524485254852648527485284852948530485314853248533485344853548536485374853848539485404854148542485434854448545485464854748548485494855048551485524855348554485554855648557485584855948560485614856248563485644856548566485674856848569485704857148572485734857448575485764857748578485794858048581485824858348584485854858648587485884858948590485914859248593485944859548596485974859848599486004860148602486034860448605486064860748608486094861048611486124861348614486154861648617486184861948620486214862248623486244862548626486274862848629486304863148632486334863448635486364863748638486394864048641486424864348644486454864648647486484864948650486514865248653486544865548656486574865848659486604866148662486634866448665486664866748668486694867048671486724867348674486754867648677486784867948680486814868248683486844868548686486874868848689486904869148692486934869448695486964869748698486994870048701487024870348704487054870648707487084870948710487114871248713487144871548716487174871848719487204872148722487234872448725487264872748728487294873048731487324873348734487354873648737487384873948740487414874248743487444874548746487474874848749487504875148752487534875448755487564875748758487594876048761487624876348764487654876648767487684876948770487714877248773487744877548776487774877848779487804878148782487834878448785487864878748788487894879048791487924879348794487954879648797487984879948800488014880248803488044880548806488074880848809488104881148812488134881448815488164881748818488194882048821488224882348824488254882648827488284882948830488314883248833488344883548836488374883848839488404884148842488434884448845488464884748848488494885048851488524885348854488554885648857488584885948860488614886248863488644886548866488674886848869488704887148872488734887448875488764887748878488794888048881488824888348884488854888648887488884888948890488914889248893488944889548896488974889848899489004890148902489034890448905489064890748908489094891048911489124891348914489154891648917489184891948920489214892248923489244892548926489274892848929489304893148932489334893448935489364893748938489394894048941489424894348944489454894648947489484894948950489514895248953489544895548956489574895848959489604896148962489634896448965489664896748968489694897048971489724897348974489754897648977489784897948980489814898248983489844898548986489874898848989489904899148992489934899448995489964899748998489994900049001490024900349004490054900649007490084900949010490114901249013490144901549016490174901849019490204902149022490234902449025490264902749028490294903049031490324903349034490354903649037490384903949040490414904249043490444904549046490474904849049490504905149052490534905449055490564905749058490594906049061490624906349064490654906649067490684906949070490714907249073490744907549076490774907849079490804908149082490834908449085490864908749088490894909049091490924909349094490954909649097490984909949100491014910249103491044910549106491074910849109491104911149112491134911449115491164911749118491194912049121491224912349124491254912649127491284912949130491314913249133491344913549136491374913849139491404914149142491434914449145491464914749148491494915049151491524915349154491554915649157491584915949160491614916249163491644916549166491674916849169491704917149172491734917449175491764917749178491794918049181491824918349184491854918649187491884918949190491914919249193491944919549196491974919849199492004920149202492034920449205492064920749208492094921049211492124921349214492154921649217492184921949220492214922249223492244922549226492274922849229492304923149232492334923449235492364923749238492394924049241492424924349244492454924649247492484924949250492514925249253492544925549256492574925849259492604926149262492634926449265492664926749268492694927049271492724927349274492754927649277492784927949280492814928249283492844928549286492874928849289492904929149292492934929449295492964929749298492994930049301493024930349304493054930649307493084930949310493114931249313493144931549316493174931849319493204932149322493234932449325493264932749328493294933049331493324933349334493354933649337493384933949340493414934249343493444934549346493474934849349493504935149352493534935449355493564935749358493594936049361493624936349364493654936649367493684936949370493714937249373493744937549376493774937849379493804938149382493834938449385493864938749388493894939049391493924939349394493954939649397493984939949400494014940249403494044940549406494074940849409494104941149412494134941449415494164941749418494194942049421494224942349424494254942649427494284942949430494314943249433494344943549436494374943849439494404944149442494434944449445494464944749448494494945049451494524945349454494554945649457494584945949460494614946249463494644946549466494674946849469494704947149472494734947449475494764947749478494794948049481494824948349484494854948649487494884948949490494914949249493494944949549496494974949849499495004950149502495034950449505495064950749508495094951049511495124951349514495154951649517495184951949520495214952249523495244952549526495274952849529495304953149532495334953449535495364953749538495394954049541495424954349544495454954649547495484954949550495514955249553495544955549556495574955849559495604956149562495634956449565495664956749568495694957049571495724957349574495754957649577495784957949580495814958249583495844958549586495874958849589495904959149592495934959449595495964959749598495994960049601496024960349604496054960649607496084960949610496114961249613496144961549616496174961849619496204962149622496234962449625496264962749628496294963049631496324963349634496354963649637496384963949640496414964249643496444964549646496474964849649496504965149652496534965449655496564965749658496594966049661496624966349664496654966649667496684966949670496714967249673496744967549676496774967849679496804968149682496834968449685496864968749688496894969049691496924969349694496954969649697496984969949700497014970249703497044970549706497074970849709497104971149712497134971449715497164971749718497194972049721497224972349724497254972649727497284972949730497314973249733497344973549736497374973849739497404974149742497434974449745497464974749748497494975049751497524975349754497554975649757497584975949760497614976249763497644976549766497674976849769497704977149772497734977449775497764977749778497794978049781497824978349784497854978649787497884978949790497914979249793497944979549796497974979849799498004980149802498034980449805498064980749808498094981049811498124981349814498154981649817498184981949820498214982249823498244982549826498274982849829498304983149832498334983449835498364983749838498394984049841498424984349844498454984649847498484984949850498514985249853498544985549856498574985849859498604986149862498634986449865498664986749868498694987049871498724987349874498754987649877498784987949880498814988249883498844988549886498874988849889498904989149892498934989449895498964989749898498994990049901499024990349904499054990649907499084990949910499114991249913499144991549916499174991849919499204992149922499234992449925499264992749928499294993049931499324993349934499354993649937499384993949940499414994249943499444994549946499474994849949499504995149952499534995449955499564995749958499594996049961499624996349964499654996649967499684996949970499714997249973499744997549976499774997849979499804998149982499834998449985499864998749988499894999049991499924999349994499954999649997499984999950000500015000250003500045000550006500075000850009500105001150012500135001450015500165001750018500195002050021500225002350024500255002650027500285002950030500315003250033500345003550036500375003850039500405004150042500435004450045500465004750048500495005050051500525005350054500555005650057500585005950060500615006250063500645006550066500675006850069500705007150072500735007450075500765007750078500795008050081500825008350084500855008650087500885008950090500915009250093500945009550096500975009850099501005010150102501035010450105501065010750108501095011050111501125011350114501155011650117501185011950120501215012250123501245012550126501275012850129501305013150132501335013450135501365013750138501395014050141501425014350144501455014650147501485014950150501515015250153501545015550156501575015850159501605016150162501635016450165501665016750168501695017050171501725017350174501755017650177501785017950180501815018250183501845018550186501875018850189501905019150192501935019450195501965019750198501995020050201502025020350204502055020650207502085020950210502115021250213502145021550216502175021850219502205022150222502235022450225502265022750228502295023050231502325023350234502355023650237502385023950240502415024250243502445024550246502475024850249502505025150252502535025450255502565025750258502595026050261502625026350264502655026650267502685026950270502715027250273502745027550276502775027850279502805028150282502835028450285502865028750288502895029050291502925029350294502955029650297502985029950300503015030250303503045030550306503075030850309503105031150312503135031450315503165031750318503195032050321503225032350324503255032650327503285032950330503315033250333503345033550336503375033850339503405034150342503435034450345503465034750348503495035050351503525035350354503555035650357503585035950360503615036250363503645036550366503675036850369503705037150372503735037450375503765037750378503795038050381503825038350384503855038650387503885038950390503915039250393503945039550396503975039850399504005040150402504035040450405504065040750408504095041050411504125041350414504155041650417504185041950420504215042250423504245042550426504275042850429504305043150432504335043450435504365043750438504395044050441504425044350444504455044650447504485044950450504515045250453504545045550456504575045850459504605046150462504635046450465504665046750468504695047050471504725047350474504755047650477504785047950480504815048250483504845048550486504875048850489504905049150492504935049450495504965049750498504995050050501505025050350504505055050650507505085050950510505115051250513505145051550516505175051850519505205052150522505235052450525505265052750528505295053050531505325053350534505355053650537505385053950540505415054250543505445054550546505475054850549505505055150552505535055450555505565055750558505595056050561505625056350564505655056650567505685056950570505715057250573505745057550576505775057850579505805058150582505835058450585505865058750588505895059050591505925059350594505955059650597505985059950600506015060250603506045060550606506075060850609506105061150612506135061450615506165061750618506195062050621506225062350624506255062650627506285062950630506315063250633506345063550636506375063850639506405064150642506435064450645506465064750648506495065050651506525065350654506555065650657506585065950660506615066250663506645066550666506675066850669506705067150672506735067450675506765067750678506795068050681506825068350684506855068650687506885068950690506915069250693506945069550696506975069850699507005070150702507035070450705507065070750708507095071050711507125071350714507155071650717507185071950720507215072250723507245072550726507275072850729507305073150732507335073450735507365073750738507395074050741507425074350744507455074650747507485074950750507515075250753507545075550756507575075850759507605076150762507635076450765507665076750768507695077050771507725077350774507755077650777507785077950780507815078250783507845078550786507875078850789507905079150792507935079450795507965079750798507995080050801508025080350804508055080650807508085080950810508115081250813508145081550816508175081850819508205082150822508235082450825508265082750828508295083050831508325083350834508355083650837508385083950840508415084250843508445084550846508475084850849508505085150852508535085450855508565085750858508595086050861508625086350864508655086650867508685086950870508715087250873508745087550876508775087850879508805088150882508835088450885508865088750888508895089050891508925089350894508955089650897508985089950900509015090250903509045090550906509075090850909509105091150912509135091450915509165091750918509195092050921509225092350924509255092650927509285092950930509315093250933509345093550936509375093850939509405094150942509435094450945509465094750948509495095050951509525095350954509555095650957509585095950960509615096250963509645096550966509675096850969509705097150972509735097450975509765097750978509795098050981509825098350984509855098650987509885098950990509915099250993509945099550996509975099850999510005100151002510035100451005510065100751008510095101051011510125101351014510155101651017510185101951020510215102251023510245102551026510275102851029510305103151032510335103451035510365103751038510395104051041510425104351044510455104651047510485104951050510515105251053510545105551056510575105851059510605106151062510635106451065510665106751068510695107051071510725107351074510755107651077510785107951080510815108251083510845108551086510875108851089510905109151092510935109451095510965109751098510995110051101511025110351104511055110651107511085110951110511115111251113511145111551116511175111851119511205112151122511235112451125511265112751128511295113051131511325113351134511355113651137511385113951140511415114251143511445114551146511475114851149511505115151152511535115451155511565115751158511595116051161511625116351164511655116651167511685116951170511715117251173511745117551176511775117851179511805118151182511835118451185511865118751188511895119051191511925119351194511955119651197511985119951200512015120251203512045120551206512075120851209512105121151212512135121451215512165121751218512195122051221512225122351224512255122651227512285122951230512315123251233512345123551236512375123851239512405124151242512435124451245512465124751248512495125051251512525125351254512555125651257512585125951260512615126251263512645126551266512675126851269512705127151272512735127451275512765127751278512795128051281512825128351284512855128651287512885128951290512915129251293512945129551296512975129851299513005130151302513035130451305513065130751308513095131051311513125131351314513155131651317513185131951320513215132251323513245132551326513275132851329513305133151332513335133451335513365133751338513395134051341513425134351344513455134651347513485134951350513515135251353513545135551356513575135851359513605136151362513635136451365513665136751368513695137051371513725137351374513755137651377513785137951380513815138251383513845138551386513875138851389513905139151392513935139451395513965139751398513995140051401514025140351404514055140651407514085140951410514115141251413514145141551416514175141851419514205142151422514235142451425514265142751428514295143051431514325143351434514355143651437514385143951440514415144251443514445144551446514475144851449514505145151452514535145451455514565145751458514595146051461514625146351464514655146651467514685146951470514715147251473514745147551476514775147851479514805148151482514835148451485514865148751488514895149051491514925149351494514955149651497514985149951500515015150251503515045150551506515075150851509515105151151512515135151451515515165151751518515195152051521515225152351524515255152651527515285152951530515315153251533515345153551536515375153851539515405154151542515435154451545515465154751548515495155051551515525155351554515555155651557515585155951560515615156251563515645156551566515675156851569515705157151572515735157451575515765157751578515795158051581515825158351584515855158651587515885158951590515915159251593515945159551596515975159851599516005160151602516035160451605516065160751608516095161051611516125161351614516155161651617516185161951620516215162251623516245162551626516275162851629516305163151632516335163451635516365163751638516395164051641516425164351644516455164651647516485164951650516515165251653516545165551656516575165851659516605166151662516635166451665516665166751668516695167051671516725167351674516755167651677516785167951680516815168251683516845168551686516875168851689516905169151692516935169451695516965169751698516995170051701517025170351704517055170651707517085170951710517115171251713517145171551716517175171851719517205172151722517235172451725517265172751728517295173051731517325173351734517355173651737517385173951740517415174251743517445174551746517475174851749517505175151752517535175451755517565175751758517595176051761517625176351764517655176651767517685176951770517715177251773517745177551776517775177851779517805178151782517835178451785517865178751788517895179051791517925179351794517955179651797517985179951800518015180251803518045180551806518075180851809518105181151812518135181451815518165181751818518195182051821518225182351824518255182651827518285182951830518315183251833518345183551836518375183851839518405184151842518435184451845518465184751848518495185051851518525185351854518555185651857518585185951860518615186251863518645186551866518675186851869518705187151872518735187451875518765187751878518795188051881518825188351884518855188651887518885188951890518915189251893518945189551896518975189851899519005190151902519035190451905519065190751908519095191051911519125191351914519155191651917519185191951920519215192251923519245192551926519275192851929519305193151932519335193451935519365193751938519395194051941519425194351944519455194651947519485194951950519515195251953519545195551956519575195851959519605196151962519635196451965519665196751968519695197051971519725197351974519755197651977519785197951980519815198251983519845198551986519875198851989519905199151992519935199451995519965199751998519995200052001520025200352004520055200652007520085200952010520115201252013520145201552016520175201852019520205202152022520235202452025520265202752028520295203052031520325203352034520355203652037520385203952040520415204252043520445204552046520475204852049520505205152052520535205452055520565205752058520595206052061520625206352064520655206652067520685206952070520715207252073520745207552076520775207852079520805208152082520835208452085520865208752088520895209052091520925209352094520955209652097520985209952100521015210252103521045210552106521075210852109521105211152112521135211452115521165211752118521195212052121521225212352124521255212652127521285212952130521315213252133521345213552136521375213852139521405214152142521435214452145521465214752148521495215052151521525215352154521555215652157521585215952160521615216252163521645216552166521675216852169521705217152172521735217452175521765217752178521795218052181521825218352184521855218652187521885218952190521915219252193521945219552196521975219852199522005220152202522035220452205522065220752208522095221052211522125221352214522155221652217522185221952220522215222252223522245222552226522275222852229522305223152232522335223452235522365223752238522395224052241522425224352244522455224652247522485224952250522515225252253522545225552256522575225852259522605226152262522635226452265522665226752268522695227052271522725227352274522755227652277522785227952280522815228252283522845228552286522875228852289522905229152292522935229452295522965229752298522995230052301523025230352304523055230652307523085230952310523115231252313523145231552316523175231852319523205232152322523235232452325523265232752328523295233052331523325233352334523355233652337523385233952340523415234252343523445234552346523475234852349523505235152352523535235452355523565235752358523595236052361523625236352364523655236652367523685236952370523715237252373523745237552376523775237852379523805238152382523835238452385523865238752388523895239052391523925239352394523955239652397523985239952400524015240252403524045240552406524075240852409524105241152412524135241452415524165241752418524195242052421524225242352424524255242652427524285242952430524315243252433524345243552436524375243852439524405244152442524435244452445524465244752448524495245052451524525245352454524555245652457524585245952460524615246252463524645246552466524675246852469524705247152472524735247452475524765247752478524795248052481524825248352484524855248652487524885248952490524915249252493524945249552496524975249852499525005250152502525035250452505525065250752508525095251052511525125251352514525155251652517525185251952520525215252252523525245252552526525275252852529525305253152532525335253452535525365253752538525395254052541525425254352544525455254652547525485254952550525515255252553525545255552556525575255852559525605256152562525635256452565525665256752568525695257052571525725257352574525755257652577525785257952580525815258252583525845258552586525875258852589525905259152592525935259452595525965259752598525995260052601526025260352604526055260652607526085260952610526115261252613526145261552616526175261852619526205262152622526235262452625526265262752628526295263052631526325263352634526355263652637526385263952640526415264252643526445264552646526475264852649526505265152652526535265452655526565265752658526595266052661526625266352664526655266652667526685266952670526715267252673526745267552676526775267852679526805268152682526835268452685526865268752688526895269052691526925269352694526955269652697526985269952700527015270252703527045270552706527075270852709527105271152712527135271452715527165271752718527195272052721527225272352724527255272652727527285272952730527315273252733527345273552736527375273852739527405274152742527435274452745527465274752748527495275052751527525275352754527555275652757527585275952760527615276252763527645276552766527675276852769527705277152772527735277452775527765277752778527795278052781527825278352784527855278652787527885278952790527915279252793527945279552796527975279852799528005280152802528035280452805528065280752808528095281052811528125281352814528155281652817528185281952820528215282252823528245282552826528275282852829528305283152832528335283452835528365283752838528395284052841528425284352844528455284652847528485284952850528515285252853528545285552856528575285852859528605286152862528635286452865528665286752868528695287052871528725287352874528755287652877528785287952880528815288252883528845288552886528875288852889528905289152892528935289452895528965289752898528995290052901529025290352904529055290652907529085290952910529115291252913529145291552916529175291852919529205292152922529235292452925529265292752928529295293052931529325293352934529355293652937529385293952940529415294252943529445294552946529475294852949529505295152952529535295452955529565295752958529595296052961529625296352964529655296652967529685296952970529715297252973529745297552976529775297852979529805298152982529835298452985529865298752988529895299052991529925299352994529955299652997529985299953000530015300253003530045300553006530075300853009530105301153012530135301453015530165301753018530195302053021530225302353024530255302653027530285302953030530315303253033530345303553036530375303853039530405304153042530435304453045530465304753048530495305053051530525305353054530555305653057530585305953060530615306253063530645306553066530675306853069530705307153072530735307453075530765307753078530795308053081530825308353084530855308653087530885308953090530915309253093530945309553096530975309853099531005310153102531035310453105531065310753108531095311053111531125311353114531155311653117531185311953120531215312253123531245312553126531275312853129531305313153132531335313453135531365313753138531395314053141531425314353144531455314653147531485314953150531515315253153531545315553156531575315853159531605316153162531635316453165531665316753168531695317053171531725317353174531755317653177531785317953180531815318253183531845318553186531875318853189531905319153192531935319453195531965319753198531995320053201532025320353204532055320653207532085320953210532115321253213532145321553216532175321853219532205322153222532235322453225532265322753228532295323053231532325323353234532355323653237532385323953240532415324253243532445324553246532475324853249532505325153252532535325453255532565325753258532595326053261532625326353264532655326653267532685326953270532715327253273532745327553276532775327853279532805328153282532835328453285532865328753288532895329053291532925329353294532955329653297532985329953300533015330253303533045330553306533075330853309533105331153312533135331453315533165331753318533195332053321533225332353324533255332653327533285332953330533315333253333533345333553336533375333853339533405334153342533435334453345533465334753348533495335053351533525335353354533555335653357533585335953360533615336253363533645336553366533675336853369533705337153372533735337453375533765337753378533795338053381533825338353384533855338653387533885338953390533915339253393533945339553396533975339853399534005340153402534035340453405534065340753408534095341053411534125341353414534155341653417534185341953420534215342253423534245342553426534275342853429534305343153432534335343453435534365343753438534395344053441534425344353444534455344653447534485344953450534515345253453534545345553456534575345853459534605346153462534635346453465534665346753468534695347053471534725347353474534755347653477534785347953480534815348253483534845348553486534875348853489534905349153492534935349453495534965349753498534995350053501535025350353504535055350653507535085350953510535115351253513535145351553516535175351853519535205352153522535235352453525535265352753528535295353053531535325353353534535355353653537535385353953540535415354253543535445354553546535475354853549535505355153552535535355453555535565355753558535595356053561535625356353564535655356653567535685356953570535715357253573535745357553576535775357853579535805358153582535835358453585535865358753588535895359053591535925359353594535955359653597535985359953600536015360253603536045360553606536075360853609536105361153612536135361453615536165361753618536195362053621536225362353624536255362653627536285362953630536315363253633536345363553636536375363853639536405364153642536435364453645536465364753648536495365053651536525365353654536555365653657536585365953660536615366253663536645366553666536675366853669536705367153672536735367453675536765367753678536795368053681536825368353684536855368653687536885368953690536915369253693536945369553696536975369853699537005370153702537035370453705537065370753708537095371053711537125371353714537155371653717537185371953720537215372253723537245372553726537275372853729537305373153732537335373453735537365373753738537395374053741537425374353744537455374653747537485374953750537515375253753537545375553756537575375853759537605376153762537635376453765537665376753768537695377053771537725377353774537755377653777537785377953780537815378253783537845378553786537875378853789537905379153792537935379453795537965379753798537995380053801538025380353804538055380653807538085380953810538115381253813538145381553816538175381853819538205382153822538235382453825538265382753828538295383053831538325383353834538355383653837538385383953840538415384253843538445384553846538475384853849538505385153852538535385453855538565385753858538595386053861538625386353864538655386653867538685386953870538715387253873538745387553876538775387853879538805388153882538835388453885538865388753888538895389053891538925389353894538955389653897538985389953900539015390253903539045390553906539075390853909539105391153912539135391453915539165391753918539195392053921539225392353924539255392653927539285392953930539315393253933539345393553936539375393853939539405394153942539435394453945539465394753948539495395053951539525395353954539555395653957539585395953960539615396253963539645396553966539675396853969539705397153972539735397453975539765397753978539795398053981539825398353984539855398653987539885398953990539915399253993539945399553996539975399853999540005400154002540035400454005540065400754008540095401054011540125401354014540155401654017540185401954020540215402254023540245402554026540275402854029540305403154032540335403454035540365403754038540395404054041540425404354044540455404654047540485404954050540515405254053540545405554056540575405854059540605406154062540635406454065540665406754068540695407054071540725407354074540755407654077540785407954080540815408254083540845408554086540875408854089540905409154092540935409454095540965409754098540995410054101541025410354104541055410654107541085410954110541115411254113541145411554116541175411854119541205412154122541235412454125541265412754128541295413054131541325413354134541355413654137541385413954140541415414254143541445414554146541475414854149541505415154152541535415454155541565415754158541595416054161541625416354164541655416654167541685416954170541715417254173541745417554176541775417854179541805418154182541835418454185541865418754188541895419054191541925419354194541955419654197541985419954200542015420254203542045420554206542075420854209542105421154212542135421454215542165421754218542195422054221542225422354224542255422654227542285422954230542315423254233542345423554236542375423854239542405424154242542435424454245542465424754248542495425054251542525425354254542555425654257542585425954260542615426254263542645426554266542675426854269542705427154272542735427454275542765427754278542795428054281542825428354284542855428654287542885428954290542915429254293542945429554296542975429854299543005430154302543035430454305543065430754308543095431054311543125431354314543155431654317543185431954320543215432254323543245432554326543275432854329543305433154332543335433454335543365433754338543395434054341543425434354344543455434654347543485434954350543515435254353543545435554356543575435854359543605436154362543635436454365543665436754368543695437054371543725437354374543755437654377543785437954380543815438254383543845438554386543875438854389543905439154392543935439454395543965439754398543995440054401544025440354404544055440654407544085440954410544115441254413544145441554416544175441854419544205442154422544235442454425544265442754428544295443054431544325443354434544355443654437544385443954440544415444254443544445444554446544475444854449544505445154452544535445454455544565445754458544595446054461544625446354464544655446654467544685446954470544715447254473544745447554476544775447854479544805448154482544835448454485544865448754488544895449054491544925449354494544955449654497544985449954500545015450254503545045450554506545075450854509545105451154512545135451454515545165451754518545195452054521545225452354524545255452654527545285452954530545315453254533545345453554536545375453854539545405454154542545435454454545545465454754548545495455054551545525455354554545555455654557545585455954560545615456254563545645456554566545675456854569545705457154572545735457454575545765457754578545795458054581545825458354584545855458654587545885458954590545915459254593545945459554596545975459854599546005460154602546035460454605546065460754608546095461054611546125461354614546155461654617546185461954620546215462254623546245462554626546275462854629546305463154632546335463454635546365463754638546395464054641546425464354644546455464654647546485464954650546515465254653546545465554656546575465854659546605466154662546635466454665546665466754668546695467054671546725467354674546755467654677546785467954680546815468254683546845468554686546875468854689546905469154692546935469454695546965469754698546995470054701547025470354704547055470654707547085470954710547115471254713547145471554716547175471854719547205472154722547235472454725547265472754728547295473054731547325473354734547355473654737547385473954740547415474254743547445474554746547475474854749547505475154752547535475454755547565475754758547595476054761547625476354764547655476654767547685476954770547715477254773547745477554776547775477854779547805478154782547835478454785547865478754788547895479054791547925479354794547955479654797547985479954800548015480254803548045480554806548075480854809548105481154812548135481454815548165481754818548195482054821548225482354824548255482654827548285482954830548315483254833548345483554836548375483854839548405484154842548435484454845548465484754848548495485054851548525485354854548555485654857548585485954860548615486254863548645486554866548675486854869548705487154872548735487454875548765487754878548795488054881548825488354884548855488654887548885488954890548915489254893548945489554896548975489854899549005490154902549035490454905549065490754908549095491054911549125491354914549155491654917549185491954920549215492254923549245492554926549275492854929549305493154932549335493454935549365493754938549395494054941549425494354944549455494654947549485494954950549515495254953549545495554956549575495854959549605496154962549635496454965549665496754968549695497054971549725497354974549755497654977549785497954980549815498254983549845498554986549875498854989549905499154992549935499454995549965499754998549995500055001550025500355004550055500655007550085500955010550115501255013550145501555016550175501855019550205502155022550235502455025550265502755028550295503055031550325503355034550355503655037550385503955040550415504255043550445504555046550475504855049550505505155052550535505455055550565505755058550595506055061550625506355064550655506655067550685506955070550715507255073550745507555076550775507855079550805508155082550835508455085550865508755088550895509055091550925509355094550955509655097550985509955100551015510255103551045510555106551075510855109551105511155112551135511455115551165511755118551195512055121551225512355124551255512655127551285512955130551315513255133551345513555136551375513855139551405514155142551435514455145551465514755148551495515055151551525515355154551555515655157551585515955160551615516255163551645516555166551675516855169551705517155172551735517455175551765517755178551795518055181551825518355184551855518655187551885518955190551915519255193551945519555196551975519855199552005520155202552035520455205552065520755208552095521055211552125521355214552155521655217552185521955220552215522255223552245522555226552275522855229552305523155232552335523455235552365523755238552395524055241552425524355244552455524655247552485524955250552515525255253552545525555256552575525855259552605526155262552635526455265552665526755268552695527055271552725527355274552755527655277552785527955280552815528255283552845528555286552875528855289552905529155292552935529455295552965529755298552995530055301553025530355304553055530655307553085530955310553115531255313553145531555316553175531855319553205532155322553235532455325553265532755328553295533055331553325533355334553355533655337553385533955340553415534255343553445534555346553475534855349553505535155352553535535455355553565535755358553595536055361553625536355364553655536655367553685536955370553715537255373553745537555376553775537855379553805538155382553835538455385553865538755388553895539055391553925539355394553955539655397553985539955400554015540255403554045540555406554075540855409554105541155412554135541455415554165541755418554195542055421554225542355424554255542655427554285542955430554315543255433554345543555436554375543855439554405544155442554435544455445554465544755448554495545055451554525545355454554555545655457554585545955460554615546255463554645546555466554675546855469554705547155472554735547455475554765547755478554795548055481554825548355484554855548655487554885548955490554915549255493554945549555496554975549855499555005550155502555035550455505555065550755508555095551055511555125551355514555155551655517555185551955520555215552255523555245552555526555275552855529555305553155532555335553455535555365553755538555395554055541555425554355544555455554655547555485554955550555515555255553555545555555556555575555855559555605556155562555635556455565555665556755568555695557055571555725557355574555755557655577555785557955580555815558255583555845558555586555875558855589555905559155592555935559455595555965559755598555995560055601556025560355604556055560655607556085560955610556115561255613556145561555616556175561855619556205562155622556235562455625556265562755628556295563055631556325563355634556355563655637556385563955640556415564255643556445564555646556475564855649556505565155652556535565455655556565565755658556595566055661556625566355664556655566655667556685566955670556715567255673556745567555676556775567855679556805568155682556835568455685556865568755688556895569055691556925569355694556955569655697556985569955700557015570255703557045570555706557075570855709557105571155712557135571455715557165571755718557195572055721557225572355724557255572655727557285572955730557315573255733557345573555736557375573855739557405574155742557435574455745557465574755748557495575055751557525575355754557555575655757557585575955760557615576255763557645576555766557675576855769557705577155772557735577455775557765577755778557795578055781557825578355784557855578655787557885578955790557915579255793557945579555796557975579855799558005580155802558035580455805558065580755808558095581055811558125581355814558155581655817558185581955820558215582255823558245582555826558275582855829558305583155832558335583455835558365583755838558395584055841558425584355844558455584655847558485584955850558515585255853558545585555856558575585855859558605586155862558635586455865558665586755868558695587055871558725587355874558755587655877558785587955880558815588255883558845588555886558875588855889558905589155892558935589455895558965589755898558995590055901559025590355904559055590655907559085590955910559115591255913559145591555916559175591855919559205592155922559235592455925559265592755928559295593055931559325593355934559355593655937559385593955940559415594255943559445594555946559475594855949559505595155952559535595455955559565595755958559595596055961559625596355964559655596655967559685596955970559715597255973559745597555976559775597855979559805598155982559835598455985559865598755988559895599055991559925599355994559955599655997559985599956000560015600256003560045600556006560075600856009560105601156012560135601456015560165601756018560195602056021560225602356024560255602656027560285602956030560315603256033560345603556036560375603856039560405604156042560435604456045560465604756048560495605056051560525605356054560555605656057560585605956060560615606256063560645606556066560675606856069560705607156072560735607456075560765607756078560795608056081560825608356084560855608656087560885608956090560915609256093560945609556096560975609856099561005610156102561035610456105561065610756108561095611056111561125611356114561155611656117561185611956120561215612256123561245612556126561275612856129561305613156132561335613456135561365613756138561395614056141561425614356144561455614656147561485614956150561515615256153561545615556156561575615856159561605616156162561635616456165561665616756168561695617056171561725617356174561755617656177561785617956180561815618256183561845618556186561875618856189561905619156192561935619456195561965619756198561995620056201562025620356204562055620656207562085620956210562115621256213562145621556216562175621856219562205622156222562235622456225562265622756228562295623056231562325623356234562355623656237562385623956240562415624256243562445624556246562475624856249562505625156252562535625456255562565625756258562595626056261562625626356264562655626656267562685626956270562715627256273562745627556276562775627856279562805628156282562835628456285562865628756288562895629056291562925629356294562955629656297562985629956300563015630256303563045630556306563075630856309563105631156312563135631456315563165631756318563195632056321563225632356324563255632656327563285632956330563315633256333563345633556336563375633856339563405634156342563435634456345563465634756348563495635056351563525635356354563555635656357563585635956360563615636256363563645636556366563675636856369563705637156372563735637456375563765637756378563795638056381563825638356384563855638656387563885638956390563915639256393563945639556396563975639856399564005640156402564035640456405564065640756408564095641056411564125641356414564155641656417564185641956420564215642256423564245642556426564275642856429564305643156432564335643456435564365643756438564395644056441564425644356444564455644656447564485644956450564515645256453564545645556456564575645856459564605646156462564635646456465564665646756468564695647056471564725647356474564755647656477564785647956480564815648256483564845648556486564875648856489564905649156492564935649456495564965649756498564995650056501565025650356504565055650656507565085650956510565115651256513565145651556516565175651856519565205652156522565235652456525565265652756528565295653056531565325653356534565355653656537565385653956540565415654256543565445654556546565475654856549565505655156552565535655456555565565655756558565595656056561565625656356564565655656656567565685656956570565715657256573565745657556576565775657856579565805658156582565835658456585565865658756588565895659056591565925659356594565955659656597565985659956600566015660256603566045660556606566075660856609566105661156612566135661456615566165661756618566195662056621566225662356624566255662656627566285662956630566315663256633566345663556636566375663856639566405664156642566435664456645566465664756648566495665056651566525665356654566555665656657566585665956660566615666256663566645666556666566675666856669566705667156672566735667456675566765667756678566795668056681566825668356684566855668656687566885668956690566915669256693566945669556696566975669856699567005670156702567035670456705567065670756708567095671056711567125671356714567155671656717567185671956720567215672256723567245672556726567275672856729567305673156732567335673456735567365673756738567395674056741567425674356744567455674656747567485674956750567515675256753567545675556756567575675856759567605676156762567635676456765567665676756768567695677056771567725677356774567755677656777567785677956780567815678256783567845678556786567875678856789567905679156792567935679456795567965679756798567995680056801568025680356804568055680656807568085680956810568115681256813568145681556816568175681856819568205682156822568235682456825568265682756828568295683056831568325683356834568355683656837568385683956840568415684256843568445684556846568475684856849568505685156852568535685456855568565685756858568595686056861568625686356864568655686656867568685686956870568715687256873568745687556876568775687856879568805688156882568835688456885568865688756888568895689056891568925689356894568955689656897568985689956900569015690256903569045690556906569075690856909569105691156912569135691456915569165691756918569195692056921569225692356924569255692656927569285692956930569315693256933569345693556936569375693856939569405694156942569435694456945569465694756948569495695056951569525695356954569555695656957569585695956960569615696256963569645696556966569675696856969569705697156972569735697456975569765697756978569795698056981569825698356984569855698656987569885698956990569915699256993569945699556996569975699856999570005700157002570035700457005570065700757008570095701057011570125701357014570155701657017570185701957020570215702257023570245702557026570275702857029570305703157032570335703457035570365703757038570395704057041570425704357044570455704657047570485704957050570515705257053570545705557056570575705857059570605706157062570635706457065570665706757068570695707057071570725707357074570755707657077570785707957080570815708257083570845708557086570875708857089570905709157092570935709457095570965709757098570995710057101571025710357104571055710657107571085710957110571115711257113571145711557116571175711857119571205712157122571235712457125571265712757128571295713057131571325713357134571355713657137571385713957140571415714257143571445714557146571475714857149571505715157152571535715457155571565715757158571595716057161571625716357164571655716657167571685716957170571715717257173571745717557176571775717857179571805718157182571835718457185571865718757188571895719057191571925719357194571955719657197571985719957200572015720257203572045720557206572075720857209572105721157212572135721457215572165721757218572195722057221572225722357224572255722657227572285722957230572315723257233572345723557236572375723857239572405724157242572435724457245572465724757248572495725057251572525725357254572555725657257572585725957260572615726257263572645726557266572675726857269572705727157272572735727457275572765727757278572795728057281572825728357284572855728657287572885728957290572915729257293572945729557296572975729857299573005730157302573035730457305573065730757308573095731057311573125731357314573155731657317573185731957320573215732257323573245732557326573275732857329573305733157332573335733457335573365733757338573395734057341573425734357344573455734657347573485734957350573515735257353573545735557356573575735857359573605736157362573635736457365573665736757368573695737057371573725737357374573755737657377573785737957380573815738257383573845738557386573875738857389573905739157392573935739457395573965739757398573995740057401574025740357404574055740657407574085740957410574115741257413574145741557416574175741857419574205742157422574235742457425574265742757428574295743057431574325743357434574355743657437574385743957440574415744257443574445744557446574475744857449574505745157452574535745457455574565745757458574595746057461574625746357464574655746657467574685746957470574715747257473574745747557476574775747857479574805748157482574835748457485574865748757488574895749057491574925749357494574955749657497574985749957500575015750257503575045750557506575075750857509575105751157512575135751457515575165751757518575195752057521575225752357524575255752657527575285752957530575315753257533575345753557536575375753857539575405754157542575435754457545575465754757548575495755057551575525755357554575555755657557575585755957560575615756257563575645756557566575675756857569575705757157572575735757457575575765757757578575795758057581575825758357584575855758657587575885758957590575915759257593575945759557596575975759857599576005760157602576035760457605576065760757608576095761057611576125761357614576155761657617576185761957620576215762257623576245762557626576275762857629576305763157632576335763457635576365763757638576395764057641576425764357644576455764657647576485764957650576515765257653576545765557656576575765857659576605766157662576635766457665576665766757668576695767057671576725767357674576755767657677576785767957680576815768257683576845768557686576875768857689576905769157692576935769457695576965769757698576995770057701577025770357704577055770657707577085770957710577115771257713577145771557716577175771857719577205772157722577235772457725577265772757728577295773057731577325773357734577355773657737577385773957740577415774257743577445774557746577475774857749577505775157752577535775457755577565775757758577595776057761577625776357764577655776657767577685776957770577715777257773577745777557776577775777857779577805778157782577835778457785577865778757788577895779057791577925779357794577955779657797577985779957800578015780257803578045780557806578075780857809578105781157812578135781457815578165781757818578195782057821578225782357824578255782657827578285782957830578315783257833578345783557836578375783857839578405784157842578435784457845578465784757848578495785057851578525785357854578555785657857578585785957860578615786257863578645786557866578675786857869578705787157872578735787457875578765787757878578795788057881578825788357884578855788657887578885788957890578915789257893578945789557896578975789857899579005790157902579035790457905579065790757908579095791057911579125791357914579155791657917579185791957920579215792257923579245792557926579275792857929579305793157932579335793457935579365793757938579395794057941579425794357944579455794657947579485794957950579515795257953579545795557956579575795857959579605796157962579635796457965579665796757968579695797057971579725797357974579755797657977579785797957980579815798257983579845798557986579875798857989579905799157992579935799457995579965799757998579995800058001580025800358004580055800658007580085800958010580115801258013580145801558016580175801858019580205802158022580235802458025580265802758028580295803058031580325803358034580355803658037580385803958040580415804258043580445804558046580475804858049580505805158052580535805458055580565805758058580595806058061580625806358064580655806658067580685806958070580715807258073580745807558076580775807858079580805808158082580835808458085580865808758088580895809058091580925809358094580955809658097580985809958100581015810258103581045810558106581075810858109581105811158112581135811458115581165811758118581195812058121581225812358124581255812658127581285812958130581315813258133581345813558136581375813858139581405814158142581435814458145581465814758148581495815058151581525815358154581555815658157581585815958160581615816258163581645816558166581675816858169581705817158172581735817458175581765817758178581795818058181581825818358184581855818658187581885818958190581915819258193581945819558196581975819858199582005820158202582035820458205582065820758208582095821058211582125821358214582155821658217582185821958220582215822258223582245822558226582275822858229582305823158232582335823458235582365823758238582395824058241582425824358244582455824658247582485824958250582515825258253582545825558256582575825858259582605826158262582635826458265582665826758268582695827058271582725827358274582755827658277582785827958280582815828258283582845828558286582875828858289582905829158292582935829458295582965829758298582995830058301583025830358304583055830658307583085830958310583115831258313583145831558316583175831858319583205832158322583235832458325583265832758328583295833058331583325833358334583355833658337583385833958340583415834258343583445834558346583475834858349583505835158352583535835458355583565835758358583595836058361583625836358364583655836658367583685836958370583715837258373583745837558376583775837858379583805838158382583835838458385583865838758388583895839058391583925839358394583955839658397583985839958400584015840258403584045840558406584075840858409584105841158412584135841458415584165841758418584195842058421584225842358424584255842658427584285842958430584315843258433584345843558436584375843858439584405844158442584435844458445584465844758448584495845058451584525845358454584555845658457584585845958460584615846258463584645846558466584675846858469584705847158472584735847458475584765847758478584795848058481584825848358484584855848658487584885848958490584915849258493584945849558496584975849858499585005850158502585035850458505585065850758508585095851058511585125851358514585155851658517585185851958520585215852258523585245852558526585275852858529585305853158532585335853458535585365853758538585395854058541585425854358544585455854658547585485854958550585515855258553585545855558556585575855858559585605856158562585635856458565585665856758568585695857058571585725857358574585755857658577585785857958580585815858258583585845858558586585875858858589585905859158592585935859458595585965859758598585995860058601586025860358604586055860658607586085860958610586115861258613586145861558616586175861858619586205862158622586235862458625586265862758628586295863058631586325863358634586355863658637586385863958640586415864258643586445864558646586475864858649586505865158652586535865458655586565865758658586595866058661586625866358664586655866658667586685866958670586715867258673586745867558676586775867858679586805868158682586835868458685586865868758688586895869058691586925869358694586955869658697586985869958700587015870258703587045870558706587075870858709587105871158712587135871458715587165871758718587195872058721587225872358724587255872658727587285872958730587315873258733587345873558736587375873858739587405874158742587435874458745587465874758748587495875058751587525875358754587555875658757587585875958760587615876258763587645876558766587675876858769587705877158772587735877458775587765877758778587795878058781587825878358784587855878658787587885878958790587915879258793587945879558796587975879858799588005880158802588035880458805588065880758808588095881058811588125881358814588155881658817588185881958820588215882258823588245882558826588275882858829588305883158832588335883458835588365883758838588395884058841588425884358844588455884658847588485884958850588515885258853588545885558856588575885858859588605886158862588635886458865588665886758868588695887058871588725887358874588755887658877588785887958880588815888258883588845888558886588875888858889588905889158892588935889458895588965889758898588995890058901589025890358904589055890658907589085890958910589115891258913589145891558916589175891858919589205892158922589235892458925589265892758928589295893058931589325893358934589355893658937589385893958940589415894258943589445894558946589475894858949589505895158952589535895458955589565895758958589595896058961589625896358964589655896658967589685896958970589715897258973589745897558976589775897858979589805898158982589835898458985589865898758988589895899058991589925899358994589955899658997589985899959000590015900259003590045900559006590075900859009590105901159012590135901459015590165901759018590195902059021590225902359024590255902659027590285902959030590315903259033590345903559036590375903859039590405904159042590435904459045590465904759048590495905059051590525905359054590555905659057590585905959060590615906259063590645906559066590675906859069590705907159072590735907459075590765907759078590795908059081590825908359084590855908659087590885908959090590915909259093590945909559096590975909859099591005910159102591035910459105591065910759108591095911059111591125911359114591155911659117591185911959120591215912259123591245912559126591275912859129591305913159132591335913459135591365913759138591395914059141591425914359144591455914659147591485914959150591515915259153591545915559156591575915859159591605916159162591635916459165591665916759168591695917059171591725917359174591755917659177591785917959180591815918259183591845918559186591875918859189591905919159192591935919459195591965919759198591995920059201592025920359204592055920659207592085920959210592115921259213592145921559216592175921859219592205922159222592235922459225592265922759228592295923059231592325923359234592355923659237592385923959240592415924259243592445924559246592475924859249592505925159252592535925459255592565925759258592595926059261592625926359264592655926659267592685926959270592715927259273592745927559276592775927859279592805928159282592835928459285592865928759288592895929059291592925929359294592955929659297592985929959300593015930259303593045930559306593075930859309593105931159312593135931459315593165931759318593195932059321593225932359324593255932659327593285932959330593315933259333593345933559336593375933859339593405934159342593435934459345593465934759348593495935059351593525935359354593555935659357593585935959360593615936259363593645936559366593675936859369593705937159372593735937459375593765937759378593795938059381593825938359384593855938659387593885938959390593915939259393593945939559396593975939859399594005940159402594035940459405594065940759408594095941059411594125941359414594155941659417594185941959420594215942259423594245942559426594275942859429594305943159432594335943459435594365943759438594395944059441594425944359444594455944659447594485944959450594515945259453594545945559456594575945859459594605946159462594635946459465594665946759468594695947059471594725947359474594755947659477594785947959480594815948259483594845948559486594875948859489594905949159492594935949459495594965949759498594995950059501595025950359504595055950659507595085950959510595115951259513595145951559516595175951859519595205952159522595235952459525595265952759528595295953059531595325953359534595355953659537595385953959540595415954259543595445954559546595475954859549595505955159552595535955459555595565955759558595595956059561595625956359564595655956659567595685956959570595715957259573595745957559576595775957859579595805958159582595835958459585595865958759588595895959059591595925959359594595955959659597595985959959600596015960259603596045960559606596075960859609596105961159612596135961459615596165961759618596195962059621596225962359624596255962659627596285962959630596315963259633596345963559636596375963859639596405964159642596435964459645596465964759648596495965059651596525965359654596555965659657596585965959660596615966259663596645966559666596675966859669596705967159672596735967459675596765967759678596795968059681596825968359684596855968659687596885968959690596915969259693596945969559696596975969859699597005970159702597035970459705597065970759708597095971059711597125971359714597155971659717597185971959720597215972259723597245972559726597275972859729597305973159732597335973459735597365973759738597395974059741597425974359744597455974659747597485974959750597515975259753597545975559756597575975859759597605976159762597635976459765597665976759768597695977059771597725977359774597755977659777597785977959780597815978259783597845978559786597875978859789597905979159792597935979459795597965979759798597995980059801598025980359804598055980659807598085980959810598115981259813598145981559816598175981859819598205982159822598235982459825598265982759828598295983059831598325983359834598355983659837598385983959840598415984259843598445984559846598475984859849598505985159852598535985459855598565985759858598595986059861598625986359864598655986659867598685986959870598715987259873598745987559876598775987859879598805988159882598835988459885598865988759888598895989059891598925989359894598955989659897598985989959900599015990259903599045990559906599075990859909599105991159912599135991459915599165991759918599195992059921599225992359924599255992659927599285992959930599315993259933599345993559936599375993859939599405994159942599435994459945599465994759948599495995059951599525995359954599555995659957599585995959960599615996259963599645996559966599675996859969599705997159972599735997459975599765997759978599795998059981599825998359984599855998659987599885998959990599915999259993599945999559996599975999859999600006000160002600036000460005600066000760008600096001060011600126001360014600156001660017600186001960020600216002260023600246002560026600276002860029600306003160032600336003460035600366003760038600396004060041600426004360044600456004660047600486004960050600516005260053600546005560056600576005860059600606006160062600636006460065600666006760068600696007060071600726007360074600756007660077600786007960080600816008260083600846008560086600876008860089600906009160092600936009460095600966009760098600996010060101601026010360104601056010660107601086010960110601116011260113601146011560116601176011860119601206012160122601236012460125601266012760128601296013060131601326013360134601356013660137601386013960140601416014260143601446014560146601476014860149601506015160152601536015460155601566015760158601596016060161601626016360164601656016660167601686016960170601716017260173601746017560176601776017860179601806018160182601836018460185601866018760188601896019060191601926019360194601956019660197601986019960200602016020260203602046020560206602076020860209602106021160212602136021460215602166021760218602196022060221602226022360224602256022660227602286022960230602316023260233602346023560236602376023860239602406024160242602436024460245602466024760248602496025060251602526025360254602556025660257602586025960260602616026260263602646026560266602676026860269602706027160272602736027460275602766027760278602796028060281602826028360284602856028660287602886028960290602916029260293602946029560296602976029860299603006030160302603036030460305603066030760308603096031060311603126031360314603156031660317603186031960320603216032260323603246032560326603276032860329603306033160332603336033460335603366033760338603396034060341603426034360344603456034660347603486034960350603516035260353603546035560356603576035860359603606036160362603636036460365603666036760368603696037060371603726037360374603756037660377603786037960380603816038260383603846038560386603876038860389603906039160392603936039460395603966039760398603996040060401604026040360404604056040660407604086040960410604116041260413604146041560416604176041860419604206042160422604236042460425604266042760428604296043060431604326043360434604356043660437604386043960440604416044260443604446044560446604476044860449604506045160452604536045460455604566045760458604596046060461604626046360464604656046660467604686046960470604716047260473604746047560476604776047860479604806048160482604836048460485604866048760488604896049060491604926049360494604956049660497604986049960500605016050260503605046050560506605076050860509605106051160512605136051460515605166051760518605196052060521605226052360524605256052660527605286052960530605316053260533605346053560536605376053860539605406054160542605436054460545605466054760548605496055060551605526055360554605556055660557605586055960560605616056260563605646056560566605676056860569605706057160572605736057460575605766057760578605796058060581605826058360584605856058660587605886058960590605916059260593605946059560596605976059860599606006060160602606036060460605606066060760608606096061060611606126061360614606156061660617606186061960620606216062260623606246062560626606276062860629606306063160632606336063460635606366063760638606396064060641606426064360644606456064660647606486064960650606516065260653606546065560656606576065860659606606066160662606636066460665606666066760668606696067060671606726067360674606756067660677606786067960680606816068260683606846068560686606876068860689606906069160692606936069460695606966069760698606996070060701607026070360704607056070660707607086070960710607116071260713607146071560716607176071860719607206072160722607236072460725607266072760728607296073060731607326073360734607356073660737607386073960740607416074260743607446074560746607476074860749607506075160752607536075460755607566075760758607596076060761607626076360764607656076660767607686076960770607716077260773607746077560776607776077860779607806078160782607836078460785607866078760788607896079060791607926079360794607956079660797607986079960800608016080260803608046080560806608076080860809608106081160812608136081460815608166081760818608196082060821608226082360824608256082660827608286082960830608316083260833608346083560836608376083860839608406084160842608436084460845608466084760848608496085060851608526085360854608556085660857608586085960860608616086260863608646086560866608676086860869608706087160872608736087460875608766087760878608796088060881608826088360884608856088660887608886088960890608916089260893608946089560896608976089860899609006090160902609036090460905609066090760908609096091060911609126091360914609156091660917609186091960920609216092260923609246092560926609276092860929609306093160932609336093460935609366093760938609396094060941609426094360944609456094660947609486094960950609516095260953609546095560956609576095860959609606096160962609636096460965609666096760968609696097060971609726097360974609756097660977609786097960980609816098260983609846098560986609876098860989609906099160992609936099460995609966099760998609996100061001610026100361004610056100661007610086100961010610116101261013610146101561016610176101861019610206102161022610236102461025610266102761028610296103061031610326103361034610356103661037610386103961040610416104261043610446104561046610476104861049610506105161052610536105461055610566105761058610596106061061610626106361064610656106661067610686106961070610716107261073610746107561076610776107861079610806108161082610836108461085610866108761088610896109061091610926109361094610956109661097610986109961100611016110261103611046110561106611076110861109611106111161112611136111461115611166111761118611196112061121611226112361124611256112661127611286112961130611316113261133611346113561136611376113861139611406114161142611436114461145611466114761148611496115061151611526115361154611556115661157611586115961160611616116261163611646116561166611676116861169611706117161172611736117461175611766117761178611796118061181611826118361184611856118661187611886118961190611916119261193611946119561196611976119861199612006120161202612036120461205612066120761208612096121061211612126121361214612156121661217612186121961220612216122261223612246122561226612276122861229612306123161232612336123461235612366123761238612396124061241612426124361244612456124661247612486124961250612516125261253612546125561256612576125861259612606126161262612636126461265612666126761268612696127061271612726127361274612756127661277612786127961280612816128261283612846128561286612876128861289612906129161292612936129461295612966129761298612996130061301613026130361304613056130661307613086130961310613116131261313613146131561316613176131861319613206132161322613236132461325613266132761328613296133061331613326133361334613356133661337613386133961340613416134261343613446134561346613476134861349613506135161352613536135461355613566135761358613596136061361613626136361364613656136661367613686136961370613716137261373613746137561376613776137861379613806138161382613836138461385613866138761388613896139061391613926139361394613956139661397613986139961400614016140261403614046140561406614076140861409614106141161412 |
- /* api.c API unit tests
- *
- * Copyright (C) 2006-2023 wolfSSL Inc.
- *
- * This file is part of wolfSSL.
- *
- * wolfSSL is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * wolfSSL is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
- */
- /* For AES-CBC, input lengths can optionally be validated to be a
- * multiple of the block size, by defining WOLFSSL_AES_CBC_LENGTH_CHECKS,
- * also available via the configure option --enable-aescbc-length-checks.
- */
- /*----------------------------------------------------------------------------*
- | Includes
- *----------------------------------------------------------------------------*/
- #ifdef HAVE_CONFIG_H
- #include <config.h>
- #endif
- #include <wolfssl/wolfcrypt/settings.h>
- #undef TEST_OPENSSL_COEXIST /* can't use this option with this example */
- #ifndef FOURK_BUF
- #define FOURK_BUF 4096
- #endif
- #ifndef TWOK_BUF
- #define TWOK_BUF 2048
- #endif
- #ifndef ONEK_BUF
- #define ONEK_BUF 1024
- #endif
- #if defined(WOLFSSL_STATIC_MEMORY)
- #include <wolfssl/wolfcrypt/memory.h>
- #endif /* WOLFSSL_STATIC_MEMORY */
- #ifndef HEAP_HINT
- #define HEAP_HINT NULL
- #endif /* WOLFSSL_STAIC_MEMORY */
- #ifdef WOLFSSL_ASNC_CRYPT
- #include <wolfssl/wolfcrypt/async.h>
- #endif
- #ifdef HAVE_ECC
- #include <wolfssl/wolfcrypt/ecc.h> /* wc_ecc_fp_free */
- #ifndef ECC_ASN963_MAX_BUF_SZ
- #define ECC_ASN963_MAX_BUF_SZ 133
- #endif
- #ifndef ECC_PRIV_KEY_BUF
- #define ECC_PRIV_KEY_BUF 66 /* For non user defined curves. */
- #endif
- /* ecc key sizes: 14, 16, 20, 24, 28, 30, 32, 40, 48, 64 */
- /* logic to choose right key ECC size */
- #if (defined(HAVE_ECC112) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 112
- #define KEY14 14
- #else
- #define KEY14 32
- #endif
- #if (defined(HAVE_ECC128) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 128
- #define KEY16 16
- #else
- #define KEY16 32
- #endif
- #if (defined(HAVE_ECC160) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 160
- #define KEY20 20
- #else
- #define KEY20 32
- #endif
- #if (defined(HAVE_ECC192) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 192
- #define KEY24 24
- #else
- #define KEY24 32
- #endif
- #if defined(HAVE_ECC224) || defined(HAVE_ALL_CURVES)
- #define KEY28 28
- #else
- #define KEY28 32
- #endif
- #if defined(HAVE_ECC239) || defined(HAVE_ALL_CURVES)
- #define KEY30 30
- #else
- #define KEY30 32
- #endif
- #define KEY32 32
- #if defined(HAVE_ECC320) || defined(HAVE_ALL_CURVES)
- #define KEY40 40
- #else
- #define KEY40 32
- #endif
- #if defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)
- #define KEY48 48
- #else
- #define KEY48 32
- #endif
- #if defined(HAVE_ECC512) || defined(HAVE_ALL_CURVES)
- #define KEY64 64
- #else
- #define KEY64 32
- #endif
- #if !defined(HAVE_COMP_KEY)
- #if !defined(NOCOMP)
- #define NOCOMP 0
- #endif
- #else
- #if !defined(COMP)
- #define COMP 1
- #endif
- #endif
- #if !defined(DER_SZ)
- #define DER_SZ(ks) ((ks) * 2 + 1)
- #endif
- #endif
- #ifndef NO_ASN
- #include <wolfssl/wolfcrypt/asn_public.h>
- #endif
- #include <wolfssl/error-ssl.h>
- #include <stdlib.h>
- #include <wolfssl/ssl.h> /* compatibility layer */
- #include <wolfssl/test.h>
- #include <tests/unit.h>
- #include "examples/server/server.h"
- /* for testing compatibility layer callbacks */
- #ifndef NO_MD5
- #include <wolfssl/wolfcrypt/md5.h>
- #endif
- #ifndef NO_SHA
- #include <wolfssl/wolfcrypt/sha.h>
- #endif
- #ifndef NO_SHA256
- #include <wolfssl/wolfcrypt/sha256.h>
- #endif
- #ifdef WOLFSSL_SHA512
- #include <wolfssl/wolfcrypt/sha512.h>
- #endif
- #ifdef WOLFSSL_SHA384
- #include <wolfssl/wolfcrypt/sha512.h>
- #endif
- #ifdef WOLFSSL_SHA3
- #include <wolfssl/wolfcrypt/sha3.h>
- #ifndef HEAP_HINT
- #define HEAP_HINT NULL
- #endif
- #endif
- #ifndef NO_AES
- #include <wolfssl/wolfcrypt/aes.h>
- #ifdef HAVE_AES_DECRYPT
- #include <wolfssl/wolfcrypt/wc_encrypt.h>
- #endif
- #endif
- #ifdef WOLFSSL_RIPEMD
- #include <wolfssl/wolfcrypt/ripemd.h>
- #endif
- #ifndef NO_DES3
- #include <wolfssl/wolfcrypt/des3.h>
- #include <wolfssl/wolfcrypt/wc_encrypt.h>
- #endif
- #ifdef WC_RC2
- #include <wolfssl/wolfcrypt/rc2.h>
- #endif
- #ifndef NO_HMAC
- #include <wolfssl/wolfcrypt/hmac.h>
- #endif
- #ifdef HAVE_CHACHA
- #include <wolfssl/wolfcrypt/chacha.h>
- #endif
- #ifdef HAVE_POLY1305
- #include <wolfssl/wolfcrypt/poly1305.h>
- #endif
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- #include <wolfssl/wolfcrypt/chacha20_poly1305.h>
- #endif
- #ifdef HAVE_CAMELLIA
- #include <wolfssl/wolfcrypt/camellia.h>
- #endif
- #ifndef NO_RC4
- #include <wolfssl/wolfcrypt/arc4.h>
- #endif
- #ifdef HAVE_BLAKE2
- #include <wolfssl/wolfcrypt/blake2.h>
- #endif
- #include <wolfssl/wolfcrypt/hash.h>
- #ifndef NO_RSA
- #include <wolfssl/wolfcrypt/rsa.h>
- #define FOURK_BUF 4096
- #define GEN_BUF 294
- #ifndef USER_CRYPTO_ERROR
- #define USER_CRYPTO_ERROR (-101) /* error returned by IPP lib. */
- #endif
- #endif
- #ifndef NO_SIG_WRAPPER
- #include <wolfssl/wolfcrypt/signature.h>
- #endif
- #ifdef HAVE_AESCCM
- #include <wolfssl/wolfcrypt/aes.h>
- #endif
- #ifdef HAVE_PKCS7
- #include <wolfssl/wolfcrypt/pkcs7.h>
- #include <wolfssl/wolfcrypt/asn.h>
- #ifdef HAVE_LIBZ
- #include <wolfssl/wolfcrypt/compress.h>
- #endif
- #endif
- #ifdef WOLFSSL_SMALL_CERT_VERIFY
- #include <wolfssl/wolfcrypt/asn.h>
- #endif
- #ifndef NO_DSA
- #include <wolfssl/wolfcrypt/dsa.h>
- #ifndef ONEK_BUF
- #define ONEK_BUF 1024
- #endif
- #ifndef TWOK_BUF
- #define TWOK_BUF 2048
- #endif
- #ifndef FOURK_BUF
- #define FOURK_BUF 4096
- #endif
- #ifndef DSA_SIG_SIZE
- #define DSA_SIG_SIZE 40
- #endif
- #ifndef MAX_DSA_PARAM_SIZE
- #define MAX_DSA_PARAM_SIZE 256
- #endif
- #endif
- #ifdef WOLFSSL_CMAC
- #include <wolfssl/wolfcrypt/cmac.h>
- #endif
- #ifdef HAVE_ED25519
- #include <wolfssl/wolfcrypt/ed25519.h>
- #endif
- #ifdef HAVE_CURVE25519
- #include <wolfssl/wolfcrypt/curve25519.h>
- #endif
- #ifdef HAVE_ED448
- #include <wolfssl/wolfcrypt/ed448.h>
- #endif
- #ifdef HAVE_CURVE448
- #include <wolfssl/wolfcrypt/curve448.h>
- #endif
- #ifdef HAVE_PKCS12
- #include <wolfssl/wolfcrypt/pkcs12.h>
- #endif
- #include <wolfssl/wolfcrypt/logging.h>
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || defined(OPENSSL_ALL))
- #include <wolfssl/openssl/ssl.h>
- #ifndef NO_ASN
- /* for ASN_COMMON_NAME DN_tags enum */
- #include <wolfssl/wolfcrypt/asn.h>
- #endif
- #ifdef HAVE_OCSP
- #include <wolfssl/openssl/ocsp.h>
- #endif
- #endif
- #ifdef OPENSSL_EXTRA
- #include <wolfssl/openssl/cmac.h>
- #include <wolfssl/openssl/x509v3.h>
- #include <wolfssl/openssl/asn1.h>
- #include <wolfssl/openssl/crypto.h>
- #include <wolfssl/openssl/pkcs12.h>
- #include <wolfssl/openssl/evp.h>
- #include <wolfssl/openssl/dh.h>
- #include <wolfssl/openssl/bn.h>
- #include <wolfssl/openssl/buffer.h>
- #include <wolfssl/openssl/pem.h>
- #include <wolfssl/openssl/ec.h>
- #include <wolfssl/openssl/engine.h>
- #include <wolfssl/openssl/hmac.h>
- #include <wolfssl/openssl/objects.h>
- #include <wolfssl/openssl/rand.h>
- #include <wolfssl/openssl/modes.h>
- #include <wolfssl/openssl/fips_rand.h>
- #include <wolfssl/openssl/kdf.h>
- #ifdef OPENSSL_ALL
- #include <wolfssl/openssl/txt_db.h>
- #include <wolfssl/openssl/lhash.h>
- #endif
- #ifndef NO_AES
- #include <wolfssl/openssl/aes.h>
- #endif
- #ifndef NO_DES3
- #include <wolfssl/openssl/des.h>
- #endif
- #ifdef HAVE_ECC
- #include <wolfssl/openssl/ecdsa.h>
- #endif
- #ifdef HAVE_PKCS7
- #include <wolfssl/openssl/pkcs7.h>
- #endif
- #ifdef HAVE_ED25519
- #include <wolfssl/openssl/ed25519.h>
- #endif
- #ifdef HAVE_ED448
- #include <wolfssl/openssl/ed448.h>
- #endif
- #endif /* OPENSSL_EXTRA */
- #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
- && !defined(NO_SHA256) && !defined(RC_NO_RNG)
- #include <wolfssl/wolfcrypt/srp.h>
- #endif
- #if (defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)) || \
- defined(HAVE_SESSION_TICKET) || (defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)) || \
- defined(WOLFSSL_TEST_STATIC_BUILD) || defined(WOLFSSL_DTLS) || \
- defined(HAVE_ECH)
- /* for testing SSL_get_peer_cert_chain, or SESSION_TICKET_HINT_DEFAULT,
- * for setting authKeyIdSrc in WOLFSSL_X509, or testing DTLS sequence
- * number tracking */
- #include "wolfssl/internal.h"
- #endif
- /* force enable test buffers */
- #ifndef USE_CERT_BUFFERS_2048
- #define USE_CERT_BUFFERS_2048
- #endif
- #ifndef USE_CERT_BUFFERS_256
- #define USE_CERT_BUFFERS_256
- #endif
- #include <wolfssl/certs_test.h>
- typedef struct testVector {
- const char* input;
- const char* output;
- size_t inLen;
- size_t outLen;
- } testVector;
- #if defined(HAVE_PKCS7)
- typedef struct {
- const byte* content;
- word32 contentSz;
- int contentOID;
- int encryptOID;
- int keyWrapOID;
- int keyAgreeOID;
- byte* cert;
- size_t certSz;
- byte* privateKey;
- word32 privateKeySz;
- } pkcs7EnvelopedVector;
- #ifndef NO_PKCS7_ENCRYPTED_DATA
- typedef struct {
- const byte* content;
- word32 contentSz;
- int contentOID;
- int encryptOID;
- byte* encryptionKey;
- word32 encryptionKeySz;
- } pkcs7EncryptedVector;
- #endif
- #endif /* HAVE_PKCS7 */
- /*----------------------------------------------------------------------------*
- | Constants
- *----------------------------------------------------------------------------*/
- /* Test result constants and macros. */
- /* Test succeeded. */
- #define TEST_SUCCESS (1)
- /* Test failed. */
- #define TEST_FAIL (0)
- /* Test skipped - not run. */
- #define TEST_SKIPPED (-7777)
- /* Returns the result based on whether check is true.
- *
- * @param [in] check Condition for success.
- * @return When condition is true: TEST_SUCCESS.
- * @return When condition is false: TEST_FAIL.
- */
- #ifdef DEBUG_WOLFSSL_VERBOSE
- #define XSTRINGIFY(s) STRINGIFY(s)
- #define STRINGIFY(s) #s
- #define TEST_RES_CHECK(check) ({ \
- int _ret = (check) ? TEST_SUCCESS : TEST_FAIL; \
- if (_ret == TEST_FAIL) { \
- fprintf(stderr, " check \"%s\" at %d ", \
- XSTRINGIFY(check), __LINE__); \
- } \
- _ret; })
- #else
- #define TEST_RES_CHECK(check) \
- ((check) ? TEST_SUCCESS : TEST_FAIL)
- #endif /* DEBUG_WOLFSSL_VERBOSE */
- #define TEST_STRING "Everyone gets Friday off."
- #define TEST_STRING_SZ 25
- #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
- #define TEST_RSA_BITS 1024
- #else
- #define TEST_RSA_BITS 2048
- #endif
- #define TEST_RSA_BYTES (TEST_RSA_BITS/8)
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- (!defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT))
- static const char* bogusFile =
- #ifdef _WIN32
- "NUL"
- #else
- "/dev/null"
- #endif
- ;
- #endif /* !NO_FILESYSTEM && !NO_CERTS && (!NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT) */
- enum {
- TESTING_RSA = 1,
- TESTING_ECC = 2
- };
- #ifdef WOLFSSL_QNX_CAAM
- #include <wolfssl/wolfcrypt/port/caam/wolfcaam.h>
- static int testDevId = WOLFSSL_CAAM_DEVID;
- #else
- static int testDevId = INVALID_DEVID;
- #endif
- /*----------------------------------------------------------------------------*
- | Setup
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_Init(void)
- {
- int result;
- result = wolfSSL_Init();
- result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
- return result;
- }
- static int test_wolfSSL_Cleanup(void)
- {
- int result;
- result = wolfSSL_Cleanup();
- result = TEST_RES_CHECK(result == WOLFSSL_SUCCESS);
- return result;
- }
- /* Initialize the wolfCrypt state.
- * POST: 0 success.
- */
- static int test_wolfCrypt_Init(void)
- {
- int result;
- result = wolfCrypt_Init();
- result = TEST_RES_CHECK(result == 0);
- return result;
- } /* END test_wolfCrypt_Init */
- static int test_wolfCrypt_Cleanup(void)
- {
- int result;
- result = wolfCrypt_Cleanup();
- result = TEST_RES_CHECK(result == 0);
- return result;
- }
- /*----------------------------------------------------------------------------*
- | Platform dependent function test
- *----------------------------------------------------------------------------*/
- static int test_fileAccess(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
- const char *fname[] = {
- svrCertFile, svrKeyFile, caCertFile,
- eccCertFile, eccKeyFile, eccRsaCertFile,
- cliCertFile, cliCertDerFile, cliKeyFile,
- dhParamFile,
- cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
- cliEdCertFile, cliEdKeyFile, caEdCertFile,
- NULL
- };
- const char derfile[] = "./certs/server-cert.der";
- XFILE f;
- size_t sz;
- byte *buff;
- int i;
- AssertTrue(XFOPEN("badfilename", "rb") == XBADFILE);
- for (i=0; fname[i] != NULL ; i++) {
- AssertTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
- XFCLOSE(f);
- }
- AssertTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
- AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
- sz = (size_t) XFTELL(f);
- XREWIND(f);
- AssertTrue(sz == sizeof_server_cert_der_2048);
- AssertTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL) ;
- AssertTrue(XFREAD(buff, 1, sz, f) == sz);
- XMEMCMP(server_cert_der_2048, buff, sz);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*----------------------------------------------------------------------------*
- | Method Allocators
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_Method_Allocators(void)
- {
- #define TEST_METHOD_ALLOCATOR(allocator, condition) \
- do { \
- WOLFSSL_METHOD *method; \
- condition(method = allocator()); \
- XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
- } while(0)
- #define TEST_VALID_METHOD_ALLOCATOR(a) \
- TEST_METHOD_ALLOCATOR(a, AssertNotNull)
- #define TEST_INVALID_METHOD_ALLOCATOR(a) \
- TEST_METHOD_ALLOCATOR(a, AssertNull)
- #ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_SSLV3
- #ifndef NO_WOLFSSL_SERVER
- TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
- #endif
- #endif
- #ifdef WOLFSSL_ALLOW_TLSV10
- #ifndef NO_WOLFSSL_SERVER
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
- #endif
- #endif
- #ifndef NO_WOLFSSL_SERVER
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
- #endif
- #endif /* !NO_OLD_TLS */
- #ifndef WOLFSSL_NO_TLS12
- #ifndef NO_WOLFSSL_SERVER
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
- #endif
- #endif /* !WOLFSSL_NO_TLS12 */
- #ifdef WOLFSSL_TLS13
- #ifndef NO_WOLFSSL_SERVER
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
- #endif
- #endif /* WOLFSSL_TLS13 */
- #ifndef NO_WOLFSSL_SERVER
- TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
- #endif
- #ifdef WOLFSSL_DTLS
- #ifndef NO_OLD_TLS
- #ifndef NO_WOLFSSL_SERVER
- TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
- #endif
- #endif
- #ifndef WOLFSSL_NO_TLS12
- #ifndef NO_WOLFSSL_SERVER
- TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
- #endif
- #endif
- #endif /* WOLFSSL_DTLS */
- #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
- /* Stubs */
- #ifndef NO_WOLFSSL_SERVER
- TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
- #endif
- #endif
- /* Test Either Method (client or server) */
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
- #ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_TLSV10
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
- #endif
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
- #endif /* !NO_OLD_TLS */
- #ifndef WOLFSSL_NO_TLS12
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
- #endif /* !WOLFSSL_NO_TLS12 */
- #ifdef WOLFSSL_TLS13
- TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
- #endif /* WOLFSSL_TLS13 */
- #ifdef WOLFSSL_DTLS
- TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
- #ifndef NO_OLD_TLS
- TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
- #endif /* !NO_OLD_TLS */
- #ifndef WOLFSSL_NO_TLS12
- TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
- #endif /* !WOLFSSL_NO_TLS12 */
- #endif /* WOLFSSL_DTLS */
- #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
- return TEST_SUCCESS;
- }
- /*----------------------------------------------------------------------------*
- | Context
- *----------------------------------------------------------------------------*/
- #ifndef NO_WOLFSSL_SERVER
- static int test_wolfSSL_CTX_new(void)
- {
- WOLFSSL_CTX *ctx;
- WOLFSSL_METHOD* method;
- AssertNull(ctx = wolfSSL_CTX_new(NULL));
- AssertNotNull(method = wolfSSLv23_server_method());
- AssertNotNull(ctx = wolfSSL_CTX_new(method));
- wolfSSL_CTX_free(ctx);
- return TEST_RES_CHECK(1);
- }
- #endif
- #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
- (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
- static int test_for_double_Free(void)
- {
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- int skipTest = 0;
- const char* testCertFile;
- const char* testKeyFile;
- char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
- ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
- "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
- "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
- "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
- "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
- "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
- "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
- "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
- "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
- "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
- ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
- "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
- ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
- "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
- "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
- "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
- "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
- "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
- "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
- "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
- "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
- "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
- "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
- "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
- "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
- "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
- "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
- "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
- "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
- /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
- #ifdef OPENSSL_EXTRA
- char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
- "!SRP:!kRSA:!aNULL:!eNULL";
- #endif
- #ifndef NO_RSA
- testCertFile = svrCertFile;
- testKeyFile = svrKeyFile;
- #elif defined(HAVE_ECC)
- testCertFile = eccCertFile;
- testKeyFile = eccKeyFile;
- #else
- skipTest = 1;
- #endif
- if (skipTest != 1) {
- #ifndef NO_WOLFSSL_SERVER
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #endif
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- /* First test freeing SSL, then CTX */
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- #ifndef NO_WOLFSSL_CLIENT
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #endif
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- /* Next test freeing CTX then SSL */
- wolfSSL_CTX_free(ctx);
- wolfSSL_free(ssl);
- #ifndef NO_WOLFSSL_SERVER
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #endif
- /* Test setting ciphers at ctx level */
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
- defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
- /* only update TLSv13 suites */
- AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
- #endif
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
- !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
- defined(WOLFSSL_AES_128) && !defined(NO_RSA)
- /* only update pre-TLSv13 suites */
- AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-GCM-SHA256"));
- #endif
- #ifdef OPENSSL_EXTRA
- AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
- #endif
- AssertNotNull(ssl = wolfSSL_new(ctx));
- wolfSSL_CTX_free(ctx);
- wolfSSL_free(ssl);
- #ifndef NO_WOLFSSL_CLIENT
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #endif
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile, WOLFSSL_FILETYPE_PEM));
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- /* test setting ciphers at SSL level */
- AssertTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
- defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
- /* only update TLSv13 suites */
- AssertTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
- #endif
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
- !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
- defined(WOLFSSL_AES_128) && !defined(NO_RSA)
- /* only update pre-TLSv13 suites */
- AssertTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
- #endif
- wolfSSL_CTX_free(ctx);
- wolfSSL_free(ssl);
- }
- return TEST_RES_CHECK(1);
- }
- #endif
- static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
- (!defined(NO_RSA) || defined(HAVE_ECC))
- const char* testCertFile;
- const char* testKeyFile;
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- const byte cipherList[] =
- {
- /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
- /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
- /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
- /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
- /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
- /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
- /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
- /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
- /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
- /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
- /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
- /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
- /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
- /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
- /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
- /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
- /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
- /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
- /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
- /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
- /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
- /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
- /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
- /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
- /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
- /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
- /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
- /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
- /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
- /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
- /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
- /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
- /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
- /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
- /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
- /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
- /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
- /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
- /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
- /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
- /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
- /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
- /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
- /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
- /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
- /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
- /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
- /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
- /* SHA256 */
- /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
- /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
- /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
- /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
- /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
- /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
- /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
- /* SHA384 */
- /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
- /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
- /* AES-GCM */
- /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
- /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
- /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
- /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
- /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
- /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
- /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
- /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
- /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
- /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
- /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
- /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
- /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
- /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
- /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
- /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
- /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
- /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
- /* AES-CCM, first byte is 0xC0 but isn't ECC,
- * also, in some of the other AES-CCM suites
- * there will be second byte number conflicts
- * with non-ECC AES-GCM */
- /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
- /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
- /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
- /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
- /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
- /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
- /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
- /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
- /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
- /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
- /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
- /* Camellia */
- /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
- /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
- /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
- /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
- /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
- /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
- /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
- /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
- /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
- /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
- /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
- /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
- /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
- /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
- /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
- /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
- /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
- /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
- /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
- /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
- /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
- /* TLS v1.3 cipher suites */
- /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
- /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
- /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
- /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
- /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
- /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
- /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
- /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
- };
- #ifndef NO_RSA
- testCertFile = svrCertFile;
- testKeyFile = svrKeyFile;
- #elif defined(HAVE_ECC)
- testCertFile = eccCertFile;
- testKeyFile = eccKeyFile;
- #endif
- #ifndef NO_WOLFSSL_SERVER
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #endif
- AssertTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
- sizeof(cipherList)));
- wolfSSL_CTX_free(ctx);
- #ifndef NO_WOLFSSL_SERVER
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #endif
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
- WOLFSSL_FILETYPE_PEM));
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- AssertTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
- sizeof(cipherList)));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
- (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
- return res;
- }
- static int test_wolfSSL_CTX_use_certificate_file(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX *ctx;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- /* invalid context */
- AssertFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- /* invalid cert file */
- AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
- WOLFSSL_FILETYPE_PEM));
- /* invalid cert type */
- AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
- #ifdef NO_RSA
- /* rsa needed */
- AssertFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,WOLFSSL_FILETYPE_PEM));
- #else
- /* success */
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- #endif
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
- static int test_wolfSSL_CTX_use_certificate_ASN1(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
- WOLFSSL_CTX* ctx;
- int ret;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
- server_cert_der_2048);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
- #endif
- return res;
- }
- #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
- /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
- * context using buffer.
- * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
- * --enable-testcert flag.
- */
- static int test_wolfSSL_CTX_use_certificate_buffer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
- !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX* ctx;
- int ret;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- ret = wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
- sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
- #endif
- return res;
- } /*END test_wolfSSL_CTX_use_certificate_buffer*/
- static int test_wolfSSL_CTX_use_PrivateKey_file(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX *ctx;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- /* invalid context */
- AssertFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- /* invalid key file */
- AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
- WOLFSSL_FILETYPE_PEM));
- /* invalid key type */
- AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
- /* success */
- #ifdef NO_RSA
- /* rsa needed */
- AssertFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- #else
- /* success */
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- #endif
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /* test both file and buffer versions along with unloading trusted peer certs */
- static int test_wolfSSL_CTX_trust_peer_cert(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
- WOLFSSL_CTX *ctx;
- WOLFSSL* ssl;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertNotNull(ssl = wolfSSL_new(ctx));
- #if !defined(NO_FILESYSTEM)
- /* invalid file */
- AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- AssertIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* success */
- AssertIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- /* unload cert */
- AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
- /* invalid file */
- AssertIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- AssertIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- AssertIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* success */
- AssertIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_LOCAL_X509_STORE
- /* unload cert */
- AssertIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
- #endif
- #endif
- /* Test of loading certs from buffers */
- /* invalid buffer */
- AssertIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* success */
- #ifdef USE_CERT_BUFFERS_1024
- AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
- sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- #endif
- #ifdef USE_CERT_BUFFERS_2048
- AssertIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
- sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- #endif
- /* unload cert */
- AssertIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_load_verify_locations(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX *ctx;
- #ifndef NO_RSA
- WOLFSSL_CERT_MANAGER* cm;
- #ifdef PERSIST_CERT_CACHE
- int cacheSz;
- #endif
- #endif
- #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
- const char* load_certs_path = "./certs/external";
- const char* load_no_certs_path = "./examples";
- const char* load_expired_path = "./certs/test/expired";
- #endif
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- /* invalid arguments */
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL), WOLFSSL_FAILURE);
- /* invalid ca file */
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
- WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
- #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
- (defined(WOLFSSL_QT) && \
- !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
- /* invalid path */
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
- WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
- #endif
- /* load ca cert */
- #ifdef NO_RSA
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
- WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
- #else /* Skip the following test without RSA certs. */
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
- #ifdef PERSIST_CERT_CACHE
- /* Get cert cache size */
- cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx);
- #endif
- /* Test unloading CA's */
- AssertIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
- #ifdef PERSIST_CERT_CACHE
- /* Verify no certs (result is less than cacheSz) */
- AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
- #endif
- /* load ca cert again */
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL), WOLFSSL_SUCCESS);
- /* Test getting CERT_MANAGER */
- AssertNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
- /* Test unloading CA's using CM */
- AssertIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
- #ifdef PERSIST_CERT_CACHE
- /* Verify no certs (result is less than cacheSz) */
- AssertIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
- #endif
- #endif
- #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
- /* Test loading CA certificates using a path */
- #ifdef NO_RSA
- /* failure here okay since certs in external directory are RSA */
- AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
- WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
- WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
- #endif
- /* Test loading path with no files */
- AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_no_certs_path,
- WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
- /* Test loading expired CA certificates */
- #ifdef NO_RSA
- AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
- WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
- WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_expired_path,
- WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
- WOLFSSL_SUCCESS);
- #endif
- /* Test loading CA certificates and ignoring all errors */
- #ifdef NO_RSA
- AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
- WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
- #else
- AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
- WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
- #endif
- #endif
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_load_system_CA_certs(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
- (!defined(NO_RSA) || defined(HAVE_ECC))
- WOLFSSL_CTX* ctx;
- byte dirValid = 0;
- int ret = 0;
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- if (ctx == NULL) {
- fprintf(stderr, "wolfSSL_CTX_new failed.\n");
- ret = -1;
- }
- if (ret == 0) {
- #if defined(USE_WINDOWS_API) || defined(__APPLE__)
- dirValid = 1;
- #else
- word32 numDirs;
- const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
- if (caDirs == NULL || numDirs == 0) {
- fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
- ret = -1;
- }
- else {
- ReadDirCtx dirCtx;
- word32 i;
- for (i = 0; i < numDirs; ++i) {
- if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
- /* Directory isn't empty. */
- dirValid = 1;
- wc_ReadDirClose(&dirCtx);
- break;
- }
- }
- }
- #endif
- }
- /*
- * If the directory isn't empty, we should be able to load CA
- * certs from it. On Windows/Mac, we assume the CA cert stores are
- * usable.
- */
- if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
- WOLFSSL_SUCCESS) {
- fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
- ret = -1;
- }
- #ifdef OPENSSL_EXTRA
- if (ret == 0 &&
- wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
- fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
- ret = -1;
- }
- #endif /* OPENSSL_EXTRA */
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(ret == 0);
- #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
- return res;
- }
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
- static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz, int file_type)
- {
- int ret;
- WOLFSSL_CERT_MANAGER* cm;
- cm = wolfSSL_CertManagerNew();
- if (cm == NULL) {
- fprintf(stderr, "test_cm_load_ca failed\n");
- return -1;
- }
- ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
- wolfSSL_CertManagerFree(cm);
- return ret;
- }
- static int test_cm_load_ca_file(const char* ca_cert_file)
- {
- int ret = 0;
- byte* cert_buf = NULL;
- size_t cert_sz = 0;
- #if defined(WOLFSSL_PEM_TO_DER)
- DerBuffer* pDer = NULL;
- #endif
- ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
- if (ret == 0) {
- /* normal test */
- ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
- if (ret == WOLFSSL_SUCCESS) {
- /* test including null terminator in length */
- byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
- if (tmp == NULL) {
- ret = MEMORY_E;
- }
- else {
- cert_buf = tmp;
- cert_buf[cert_sz] = '\0';
- ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
- WOLFSSL_FILETYPE_PEM);
- }
- }
- #if defined(WOLFSSL_PEM_TO_DER)
- if (ret == WOLFSSL_SUCCESS) {
- /* test loading DER */
- ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
- if (ret == 0 && pDer != NULL) {
- ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
- WOLFSSL_FILETYPE_ASN1);
- wc_FreeDer(&pDer);
- }
- }
- #endif
- }
- free(cert_buf);
- return ret;
- }
- static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
- int file_type, word32 flags)
- {
- int ret;
- WOLFSSL_CERT_MANAGER* cm;
- cm = wolfSSL_CertManagerNew();
- if (cm == NULL) {
- fprintf(stderr, "test_cm_load_ca failed\n");
- return -1;
- }
- ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
- 0, flags);
- wolfSSL_CertManagerFree(cm);
- return ret;
- }
- static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
- {
- int ret = 0;
- byte* cert_buf = NULL;
- size_t cert_sz = 0;
- #if defined(WOLFSSL_PEM_TO_DER)
- DerBuffer* pDer = NULL;
- #endif
- ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
- if (ret == 0) {
- /* normal test */
- ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
- WOLFSSL_FILETYPE_PEM, flags);
- if (ret == WOLFSSL_SUCCESS) {
- /* test including null terminator in length */
- byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
- if (tmp == NULL) {
- ret = MEMORY_E;
- }
- else {
- cert_buf = tmp;
- cert_buf[cert_sz] = '\0';
- ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
- WOLFSSL_FILETYPE_PEM, flags);
- }
- }
- #if defined(WOLFSSL_PEM_TO_DER)
- if (ret == WOLFSSL_SUCCESS) {
- /* test loading DER */
- ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
- if (ret == 0 && pDer != NULL) {
- ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
- WOLFSSL_FILETYPE_ASN1, flags);
- wc_FreeDer(&pDer);
- }
- }
- #endif
- }
- free(cert_buf);
- return ret;
- }
- #endif /* !NO_FILESYSTEM && !NO_CERTS */
- static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_OCSP) && !defined(NO_RSA)
- /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
- defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
- defined(HAVE_LIGHTY)
- WOLFSSL_CERT_MANAGER* cm = NULL;
- /* Raw OCSP response bytes captured using the following setup:
- * - Run responder with
- * openssl ocsp -port 9999 -ndays 9999
- * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
- * -rsigner certs/ocsp/ocsp-responder-cert.pem
- * -rkey certs/ocsp/ocsp-responder-key.pem
- * -CA certs/ocsp/intermediate1-ca-cert.pem
- * - Run client with
- * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
- * -issuer certs/ocsp/intermediate1-ca-cert.pem
- * -cert certs/ocsp/server1-cert.pem
- * -CAfile certs/ocsp/root-ca-cert.pem -noverify
- * - Copy raw response from Wireshark.
- */
- byte response[] = {
- 0x30, 0x82, 0x07, 0x40, 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, 0x82, 0x07, 0x35, 0x06,
- 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, 0x07, 0x26, 0x30, 0x82,
- 0x07, 0x22, 0x30, 0x82, 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09,
- 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
- 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10,
- 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65,
- 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53,
- 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67,
- 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04,
- 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20,
- 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a,
- 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77,
- 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x31,
- 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0x30, 0x64, 0x30, 0x62, 0x30,
- 0x3a, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x71, 0x4d,
- 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18,
- 0xda, 0x04, 0x04, 0x14, 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2,
- 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, 0x00, 0x18, 0x0f, 0x32,
- 0x30, 0x32, 0x31, 0x30, 0x35, 0x30, 0x33, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30, 0x5a, 0xa0, 0x11,
- 0x18, 0x0f, 0x32, 0x30, 0x34, 0x38, 0x30, 0x39, 0x31, 0x37, 0x32, 0x31, 0x34, 0x37, 0x31, 0x30,
- 0x5a, 0xa1, 0x23, 0x30, 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x30,
- 0x01, 0x02, 0x04, 0x12, 0x04, 0x10, 0x38, 0x31, 0x60, 0x99, 0xc8, 0x05, 0x09, 0x68, 0x1c, 0x33,
- 0x49, 0xea, 0x45, 0x26, 0x2f, 0x6d, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
- 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x4d, 0x58, 0xcc, 0x69, 0x42, 0xe2,
- 0x9e, 0x64, 0xf6, 0x57, 0xce, 0xcb, 0x5f, 0x14, 0xaf, 0x08, 0x6c, 0xc1, 0x52, 0x7a, 0x40, 0x0a,
- 0xfd, 0xb6, 0xce, 0xbb, 0x40, 0xf4, 0xb9, 0xa5, 0x88, 0xc7, 0xf3, 0x42, 0x9f, 0xa9, 0x94, 0xbe,
- 0x6e, 0x7e, 0x09, 0x30, 0x9d, 0x0e, 0x10, 0x6f, 0x9c, 0xd9, 0x4c, 0x71, 0x81, 0x41, 0x64, 0x95,
- 0xf5, 0x85, 0x77, 0x94, 0x81, 0x61, 0x88, 0xc8, 0x0b, 0x50, 0xbb, 0x37, 0xc8, 0x86, 0x76, 0xd8,
- 0xa2, 0xed, 0x66, 0x34, 0xfb, 0xe4, 0xe7, 0x09, 0x8c, 0xf5, 0xb5, 0x85, 0xd0, 0x4b, 0xb5, 0xe6,
- 0x23, 0x62, 0xc3, 0xd0, 0xef, 0xf7, 0x42, 0x89, 0x02, 0x80, 0x64, 0xc9, 0xed, 0xdd, 0x7c, 0x8f,
- 0x0d, 0xe7, 0x43, 0x9b, 0x88, 0x1f, 0xb0, 0xfd, 0x24, 0x01, 0xc7, 0x55, 0xc3, 0x73, 0x12, 0x84,
- 0x09, 0x7c, 0x57, 0xa8, 0x5d, 0xab, 0x75, 0x29, 0x5c, 0x36, 0x97, 0x64, 0x40, 0x0b, 0x55, 0x34,
- 0x0a, 0x5d, 0xb1, 0x1b, 0x61, 0x1b, 0xdc, 0xe5, 0x89, 0xdd, 0x92, 0x62, 0x57, 0xa7, 0x52, 0xb4,
- 0x38, 0x9a, 0x48, 0xc8, 0x3a, 0x14, 0xde, 0x69, 0x42, 0xe9, 0x37, 0xa4, 0xe7, 0x2d, 0x00, 0xa7,
- 0x0b, 0x29, 0x18, 0xd5, 0xce, 0xd9, 0x0d, 0xdd, 0xfe, 0xae, 0x86, 0xb3, 0x32, 0x1c, 0xc9, 0x33,
- 0xb0, 0x2b, 0xb7, 0x3c, 0x0d, 0x43, 0xd8, 0x6c, 0xf2, 0xb7, 0xcd, 0x7b, 0xd5, 0x7d, 0xf0, 0xde,
- 0x34, 0x9f, 0x6d, 0x83, 0xb9, 0xd5, 0xed, 0xe3, 0xda, 0x96, 0x40, 0x9e, 0xd6, 0xa6, 0xfd, 0x70,
- 0x80, 0x70, 0x87, 0x61, 0x0f, 0xc5, 0x9f, 0x75, 0xfe, 0x11, 0x78, 0x34, 0xc9, 0x42, 0x16, 0x73,
- 0x46, 0x7b, 0x05, 0x53, 0x28, 0x43, 0xbe, 0xee, 0x88, 0x67, 0x1d, 0xcc, 0x74, 0xa7, 0xb6, 0x58,
- 0x7b, 0x29, 0x68, 0x40, 0xcf, 0xce, 0x7b, 0x19, 0x33, 0x68, 0xa0, 0x82, 0x04, 0xc6, 0x30, 0x82,
- 0x04, 0xc2, 0x30, 0x82, 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, 0x02, 0x01, 0x02, 0x02,
- 0x01, 0x04, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
- 0x00, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
- 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68,
- 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c,
- 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
- 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03,
- 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67,
- 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53,
- 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09,
- 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40,
- 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x32,
- 0x31, 0x30, 0x32, 0x31, 0x30, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x17, 0x0d, 0x32, 0x33,
- 0x31, 0x31, 0x30, 0x37, 0x31, 0x39, 0x34, 0x39, 0x35, 0x34, 0x5a, 0x30, 0x81, 0x9e, 0x31, 0x0b,
- 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06,
- 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e,
- 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74,
- 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c,
- 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45,
- 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x03,
- 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, 0x43, 0x53,
- 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, 0x1d, 0x06,
- 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f,
- 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x82, 0x01, 0x22,
- 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
- 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, 0x00, 0xb8, 0xba, 0x23,
- 0xb4, 0xf6, 0xc3, 0x7b, 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, 0x1e, 0x63, 0xb9, 0x85,
- 0x23, 0x34, 0x50, 0x6d, 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, 0x5c, 0x2d, 0xf7, 0x63,
- 0x88, 0xd1, 0x07, 0x7a, 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, 0x22, 0xb4, 0x94, 0x41,
- 0x38, 0xe2, 0x9d, 0x74, 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, 0xca, 0x3f, 0x46, 0x2b,
- 0xfe, 0xe5, 0x5a, 0x3f, 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, 0xc3, 0xee, 0x42, 0xf8,
- 0x8d, 0xeb, 0x92, 0x95, 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, 0x16, 0x80, 0x90, 0xce,
- 0x24, 0x35, 0x21, 0xc4, 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, 0x0a, 0x5a, 0x4f, 0x4a,
- 0x73, 0x31, 0x50, 0xee, 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, 0x87, 0xb1, 0x99, 0xe2,
- 0x10, 0xa7, 0x06, 0x72, 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, 0x76, 0xf8, 0xe0, 0x4a,
- 0xec, 0xbc, 0x93, 0xf4, 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, 0xb4, 0x90, 0x30, 0xbb,
- 0x17, 0xb0, 0xfe, 0x97, 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, 0x19, 0x12, 0x3c, 0xab,
- 0x82, 0x71, 0x78, 0xff, 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, 0x8c, 0x27, 0xac, 0x11,
- 0xb8, 0xd8, 0x43, 0x49, 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, 0x24, 0x87, 0x17, 0x3b,
- 0xd8, 0x04, 0x65, 0x6c, 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, 0x73, 0x68, 0x26, 0x14,
- 0x87, 0x95, 0xc3, 0x5f, 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, 0x0a, 0x8b, 0x98, 0xf3,
- 0xe3, 0xff, 0x4e, 0x44, 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, 0x39, 0x02, 0x03, 0x01,
- 0x00, 0x01, 0xa3, 0x82, 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, 0x06, 0x03, 0x55, 0x1d,
- 0x13, 0x04, 0x02, 0x30, 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14,
- 0x32, 0x67, 0xe1, 0xb1, 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, 0x40, 0x50, 0xb5, 0x46,
- 0x56, 0xb8, 0x30, 0x36, 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x81, 0xbc, 0x30,
- 0x81, 0xb9, 0x80, 0x14, 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, 0x47, 0xa5, 0x38, 0xd7,
- 0xb0, 0x04, 0x82, 0x3a, 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, 0x81, 0x9a, 0x30, 0x81,
- 0x97, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13,
- 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67,
- 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65,
- 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07,
- 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b,
- 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
- 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20,
- 0x72, 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
- 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
- 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, 0x13, 0x06, 0x03, 0x55,
- 0x1d, 0x25, 0x04, 0x0c, 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x09,
- 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x03,
- 0x82, 0x01, 0x01, 0x00, 0x07, 0xca, 0xa6, 0xa1, 0x9f, 0xbf, 0xaf, 0x92, 0x41, 0x35, 0x66, 0x51,
- 0xac, 0xbc, 0x2c, 0xec, 0xe7, 0x8d, 0x65, 0x7e, 0xe9, 0x40, 0xfe, 0x5a, 0xab, 0x8a, 0x1d, 0x3d,
- 0x13, 0xdb, 0xb4, 0x43, 0x2c, 0x9a, 0x36, 0x98, 0x21, 0xa5, 0xe8, 0xca, 0xa9, 0x4d, 0xfc, 0xe3,
- 0xf7, 0x45, 0x88, 0xcd, 0x33, 0xbf, 0x8a, 0x62, 0x10, 0x2f, 0xb2, 0xb7, 0x04, 0xef, 0x26, 0x43,
- 0x51, 0x1d, 0x43, 0x62, 0x7d, 0x1e, 0x50, 0xc8, 0xd5, 0x98, 0x94, 0x71, 0x8f, 0x3b, 0x23, 0x26,
- 0xf1, 0x71, 0x8e, 0x1e, 0x3d, 0x3f, 0x21, 0xfd, 0xb7, 0x2d, 0x65, 0xe4, 0x07, 0x65, 0xac, 0x3c,
- 0xfc, 0xc0, 0x47, 0xa9, 0x32, 0xf6, 0xda, 0x26, 0x93, 0x10, 0xb2, 0xd1, 0x6d, 0xc8, 0x81, 0x31,
- 0x7c, 0xb0, 0x6b, 0xc5, 0x22, 0x8d, 0xb3, 0xfa, 0xbe, 0x82, 0xea, 0x41, 0x42, 0xc4, 0xc0, 0xef,
- 0xe3, 0x84, 0x0f, 0x6f, 0x9a, 0x03, 0x63, 0xb3, 0x30, 0xe0, 0x31, 0x81, 0x2a, 0x16, 0xb3, 0x47,
- 0xd9, 0x5b, 0x38, 0x93, 0x07, 0xd0, 0x6e, 0x79, 0x52, 0x2c, 0xe5, 0x50, 0x84, 0x79, 0x10, 0xe7,
- 0xf6, 0x31, 0x7a, 0x3e, 0x48, 0xa2, 0x38, 0x21, 0x90, 0x7a, 0xf2, 0x5f, 0x48, 0xa4, 0x46, 0x93,
- 0x87, 0xdd, 0x5c, 0x83, 0x64, 0xea, 0xb5, 0x99, 0xa2, 0xe9, 0x01, 0x40, 0xfe, 0xf0, 0x48, 0x66,
- 0x4f, 0x96, 0xf7, 0x83, 0x52, 0xf8, 0x6d, 0xf8, 0x5f, 0xed, 0x0c, 0xbb, 0xbe, 0xd0, 0x69, 0x10,
- 0x4b, 0x99, 0x8f, 0xf8, 0x61, 0x53, 0x9d, 0x12, 0xca, 0x86, 0xaa, 0xb1, 0x80, 0xb4, 0xa6, 0xc1,
- 0xcb, 0xb7, 0x48, 0xf7, 0x9f, 0x55, 0xb4, 0x6e, 0xab, 0xd3, 0xa1, 0xaa, 0x4b, 0xa7, 0x21, 0x6e,
- 0x16, 0x7f, 0xad, 0xbb, 0xea, 0x0f, 0x41, 0x80, 0x9b, 0x7f, 0xd6, 0x46, 0xa2, 0xc0, 0x61, 0x72,
- 0x59, 0x59, 0xa0, 0x07
- };
- OcspEntry entry[1];
- CertStatus status[1];
- OcspRequest* request;
- byte serial[] = {0x05};
- byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
- byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
- XMEMSET(entry, 0, sizeof(OcspEntry));
- XMEMSET(status, 0, sizeof(CertStatus));
- AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
- request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
- DYNAMIC_TYPE_OCSP_REQUEST);
- AssertNotNull(request->serial);
- request->serialSz = sizeof(serial);
- XMEMCPY(request->serial, serial, sizeof(serial));
- XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
- XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
- AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
- AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CertManagerLoadCA(cm,
- "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
- /* Response should be valid. */
- AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
- sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
- /* Flip a byte in the request serial number, response should be invalid
- * now. */
- request->serial[0] ^= request->serial[0];
- AssertIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, response,
- sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
- wolfSSL_OCSP_REQUEST_free(request);
- wolfSSL_CertManagerFree(cm);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
- * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
- #endif /* HAVE_OCSP */
- return res;
- }
- static int test_wolfSSL_CheckOCSPResponse(void)
- {
- int result = TEST_SKIPPED;
- #if defined(HAVE_OCSP) && !defined(NO_RSA) && defined(OPENSSL_ALL)
- const char* responseFile = "./certs/ocsp/test-response.der";
- const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
- const char* responseNoInternFile = "./certs/ocsp/test-response-nointern.der";
- const char* caFile = "./certs/ocsp/root-ca-cert.pem";
- OcspResponse* res = NULL;
- byte data[4096];
- const unsigned char* pt;
- int dataSz;
- XFILE f;
- WOLFSSL_OCSP_BASICRESP* bs;
- WOLFSSL_X509_STORE* st;
- WOLFSSL_X509* issuer;
- f = XFOPEN(responseFile, "rb");
- AssertTrue(f != XBADFILE);
- dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
- AssertIntGT(dataSz, 0);
- XFCLOSE(f);
- pt = data;
- res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
- AssertNotNull(res);
- issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
- AssertNotNull(issuer);
- st = wolfSSL_X509_STORE_new();
- AssertNotNull(st);
- AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
- bs = wolfSSL_OCSP_response_get1_basic(res);
- AssertNotNull(bs);
- AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
- wolfSSL_OCSP_BASICRESP_free(bs);
- wolfSSL_OCSP_RESPONSE_free(res);
- wolfSSL_X509_STORE_free(st);
- wolfSSL_X509_free(issuer);
- /* check loading a response with optional certs */
- f = XFOPEN(responseNoInternFile, "rb");
- AssertTrue(f != XBADFILE);
- dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
- AssertIntGT(dataSz, 0);
- XFCLOSE(f);
- pt = data;
- res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
- AssertNotNull(res);
- wolfSSL_OCSP_RESPONSE_free(res);
- /* check loading a response with multiple certs */
- {
- WOLFSSL_CERT_MANAGER* cm = NULL;
- OcspEntry *entry;
- CertStatus* status;
- OcspRequest* request;
- byte serial1[] = {0x01};
- byte serial[] = {0x02};
- byte issuerHash[] = {
- 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
- 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
- 0x37, 0x15, 0x8A, 0x88
- };
- byte issuerKeyHash[] = {
- 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
- 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
- 0x7E, 0x72, 0x15, 0x21
- };
- entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
- DYNAMIC_TYPE_OPENSSL);
- AssertNotNull(entry);
- status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
- DYNAMIC_TYPE_OPENSSL);
- AssertNotNull(status);
- XMEMSET(entry, 0, sizeof(OcspEntry));
- XMEMSET(status, 0, sizeof(CertStatus));
- AssertNotNull(request = wolfSSL_OCSP_REQUEST_new());
- request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
- DYNAMIC_TYPE_OCSP_REQUEST);
- AssertNotNull(request->serial);
- request->serialSz = sizeof(serial);
- XMEMCPY(request->serial, serial, sizeof(serial));
- XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
- XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
- AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
- AssertIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
- WOLFSSL_SUCCESS);
- f = XFOPEN(responseMultiFile, "rb");
- AssertTrue(f != XBADFILE);
- dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
- AssertIntGT(dataSz, 0);
- XFCLOSE(f);
- AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
- dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
- dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
- AssertNotNull(entry->status);
- XMEMCPY(request->serial, serial1, sizeof(serial1));
- AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
- dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
- /* store both status's in the entry to check that "next" is not
- * overwritten */
- status->next = entry->status;
- entry->status = status;
- XMEMCPY(request->serial, serial, sizeof(serial));
- AssertIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
- dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
- AssertNotNull(entry->status->next);
- /* compare the status found */
- AssertIntEQ(status->serialSz, entry->status->serialSz);
- AssertIntEQ(XMEMCMP(status->serial, entry->status->serial,
- status->serialSz), 0);
- wolfSSL_OCSP_CERTID_free(entry);
- wolfSSL_OCSP_REQUEST_free(request);
- wolfSSL_CertManagerFree(cm);
- }
- #if defined(WC_RSA_PSS)
- {
- const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
- /* check loading a response with RSA-PSS signature */
- f = XFOPEN(responsePssFile, "rb");
- AssertTrue(f != XBADFILE);
- dataSz = (word32)XFREAD(data, 1, sizeof(data), f);
- AssertIntGT(dataSz, 0);
- XFCLOSE(f);
- pt = data;
- res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz);
- AssertNotNull(res);
- /* try to verify the response */
- issuer = wolfSSL_X509_load_certificate_file(caFile, SSL_FILETYPE_PEM);
- AssertNotNull(issuer);
- st = wolfSSL_X509_STORE_new();
- AssertNotNull(st);
- AssertIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
- bs = wolfSSL_OCSP_response_get1_basic(res);
- AssertNotNull(bs);
- AssertIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
- wolfSSL_OCSP_BASICRESP_free(bs);
- wolfSSL_OCSP_RESPONSE_free(res);
- wolfSSL_X509_STORE_free(st);
- wolfSSL_X509_free(issuer);
- }
- #endif
- result = TEST_RES_CHECK(1);
- #endif /* HAVE_OCSP */
- return result;
- }
- static int test_wolfSSL_CertManagerLoadCABuffer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
- const char* ca_cert = "./certs/ca-cert.pem";
- const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
- int ret;
- ret = test_cm_load_ca_file(ca_cert);
- #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
- AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
- #elif defined(NO_RSA)
- AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
- #else
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- #endif
- ret = test_cm_load_ca_file(ca_expired_cert);
- #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
- AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
- res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
- #elif defined(NO_RSA)
- AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
- res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
- #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
- !defined(OPENSSL_COMPATIBLE_DEFAULTS)
- AssertIntEQ(ret, ASN_AFTER_DATE_E);
- res = TEST_RES_CHECK(ret == ASN_AFTER_DATE_E);
- #else
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
- #endif
- #endif
- return res;
- }
- static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
- const char* ca_cert = "./certs/ca-cert.pem";
- const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
- int ret;
- ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE);
- #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
- AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
- #elif defined(NO_RSA)
- AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
- #else
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- #endif
- ret = test_cm_load_ca_file_ex(ca_expired_cert,
- WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY);
- #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
- AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
- res = TEST_RES_CHECK(ret == WOLFSSL_FATAL_ERROR);
- #elif defined(NO_RSA)
- AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
- res = TEST_RES_CHECK(ret == ASN_UNKNOWN_OID_E);
- #else
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
- #endif
- #endif
- return res;
- }
- static int test_wolfSSL_CertManagerGetCerts(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
- defined(WOLFSSL_SIGNER_DER_CERT)
- WOLFSSL_CERT_MANAGER* cm = NULL;
- WOLFSSL_STACK* sk = NULL;
- X509* x509 = NULL;
- X509* cert1 = NULL;
- FILE* file1 = NULL;
- #ifdef DEBUG_WOLFSSL_VERBOSE
- WOLFSSL_BIO* bio = NULL;
- #endif
- int i = 0;
- int ret = 0;
- const byte* der;
- int derSz = 0;
- AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
- AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
- fclose(file1);
- AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
- AssertNull(sk = wolfSSL_CertManagerGetCerts(cm));
- AssertNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
- ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz, WOLFSSL_FILETYPE_ASN1);
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
- * and full OpenSSL compatibility */
- AssertIntEQ(ret, ASN_SELF_SIGNED_E);
- #else
- AssertIntEQ(ret, ASN_NO_SIGNER_E);
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
- "./certs/ca-cert.pem", NULL));
- AssertNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
- for (i = 0; i < sk_X509_num(sk); i++) {
- x509 = sk_X509_value(sk, i);
- AssertIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
- #ifdef DEBUG_WOLFSSL_VERBOSE
- bio = BIO_new(wolfSSL_BIO_s_file());
- if (bio != NULL) {
- BIO_set_fp(bio, stderr, BIO_NOCLOSE);
- X509_print(bio, x509);
- BIO_free(bio);
- }
- #endif /* DEBUG_WOLFSSL_VERBOSE */
- }
- wolfSSL_X509_free(cert1);
- sk_X509_pop_free(sk, NULL);
- wolfSSL_CertManagerFree(cm);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
- defined(WOLFSSL_SIGNER_DER_CERT) */
- return res;
- }
- static int test_wolfSSL_CertManagerSetVerify(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
- int ret = 0;
- WOLFSSL_CERT_MANAGER* cm;
- int tmp = myVerifyAction;
- const char* ca_cert = "./certs/ca-cert.pem";
- const char* expiredCert = "./certs/test/expired/expired-cert.pem";
- cm = wolfSSL_CertManagerNew();
- AssertNotNull(cm);
- wolfSSL_CertManagerSetVerify(cm, myVerify);
- ret = wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL);
- #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
- AssertIntEQ(ret, -1);
- #else
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- #endif
- /* Use the test CB that always accepts certs */
- myVerifyAction = VERIFY_OVERRIDE_ERROR;
- ret = wolfSSL_CertManagerVerify(cm, expiredCert, WOLFSSL_FILETYPE_PEM);
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ALWAYS_VERIFY_CB
- {
- const char* verifyCert = "./certs/server-cert.pem";
- /* Use the test CB that always fails certs */
- myVerifyAction = VERIFY_FORCE_FAIL;
- ret = wolfSSL_CertManagerVerify(cm, verifyCert, WOLFSSL_FILETYPE_PEM);
- AssertIntEQ(ret, VERIFY_CERT_ERROR);
- }
- #endif
- wolfSSL_CertManagerFree(cm);
- myVerifyAction = tmp;
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(DEBUG_UNIT_TEST_CERTS)
- /* Used when debugging name constraint tests. Not static to allow use in
- * multiple locations with complex define guards. */
- void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
- {
- BIO* out = BIO_new_file(fileName, "wb");
- if (out != NULL) {
- PEM_write_bio_X509(out, x509);
- BIO_free(out);
- }
- }
- void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
- {
- BIO* out = BIO_new_file(fileName, "wb");
- if (out != NULL) {
- BIO_write(out, der, derSz);
- BIO_free(out);
- }
- }
- #else
- #define DEBUG_WRITE_CERT_X509(x509, fileName)
- #define DEBUG_WRITE_DER(der, derSz, fileName)
- #endif
- static int test_wolfSSL_CertManagerNameConstraint(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
- defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
- !defined(NO_SHA256)
- WOLFSSL_CERT_MANAGER* cm;
- WOLFSSL_EVP_PKEY *priv;
- WOLFSSL_X509_NAME* name;
- const char* ca_cert = "./certs/test/cert-ext-nc.der";
- const char* server_cert = "./certs/test/server-goodcn.pem";
- int i = 0;
- static const byte extNameConsOid[] = {85, 29, 30};
- RsaKey key;
- WC_RNG rng;
- byte *der;
- int derSz;
- word32 idx = 0;
- byte *pt;
- WOLFSSL_X509 *x509, *ca;
- wc_InitRng(&rng);
- /* load in CA private key for signing */
- AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
- AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
- sizeof_server_key_der_2048), 0);
- /* get ca certificate then alter it */
- AssertNotNull(der =
- (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
- WOLFSSL_FILETYPE_ASN1));
- AssertNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
- XMEMCPY(der, pt, derSz);
- /* find the name constraint extension and alter it */
- pt = der;
- for (i = 0; i < derSz - 3; i++) {
- if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
- pt += 3;
- break;
- }
- pt++;
- }
- AssertIntNE(i, derSz - 3); /* did not find OID if this case is hit */
- /* go to the length value and set it to 0 */
- while (i < derSz && *pt != 0x81) {
- pt++;
- i++;
- }
- AssertIntNE(i, derSz); /* did not place to alter */
- pt++;
- *pt = 0x00;
- /* resign the altered certificate */
- AssertIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
- FOURK_BUF, &key, NULL, &rng)), 0);
- AssertNotNull(cm = wolfSSL_CertManagerNew());
- AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
- wolfSSL_CertManagerFree(cm);
- XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_X509_free(x509);
- wc_FreeRsaKey(&key);
- wc_FreeRng(&rng);
- /* add email alt name to satisfy constraint */
- pt = (byte*)server_key_der_2048;
- AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
- (const unsigned char**)&pt, sizeof_server_key_der_2048));
- AssertNotNull(cm = wolfSSL_CertManagerNew());
- AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
- WOLFSSL_FILETYPE_ASN1));
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
- DEBUG_WRITE_DER(der, derSz, "ca.der");
- AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* Good cert test with proper alt email name */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
- (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_X509_free(x509);
- /* Cert with bad alt name list */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
- (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
- wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- wolfSSL_CertManagerFree(cm);
- wolfSSL_X509_free(x509);
- wolfSSL_X509_free(ca);
- wolfSSL_EVP_PKEY_free(priv);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CertManagerNameConstraint2(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
- defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
- const char* ca_cert = "./certs/test/cert-ext-ndir.der";
- const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
- const char* server_cert = "./certs/server-cert.pem";
- WOLFSSL_CERT_MANAGER* cm;
- WOLFSSL_X509 *x509, *ca;
- const unsigned char *der;
- const unsigned char *pt;
- WOLFSSL_EVP_PKEY *priv;
- WOLFSSL_X509_NAME* name;
- int derSz;
- /* C=US*/
- char altName[] = {
- 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
- 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
- };
- /* C=ID */
- char altNameFail[] = {
- 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
- 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
- };
- /* C=US ST=California*/
- char altNameExc[] = {
- 0x30, 0x22,
- 0x31, 0x0B,
- 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
- 0x31, 0x13,
- 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
- 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
- };
- /* load in CA private key for signing */
- pt = ca_key_der_2048;
- AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
- sizeof_ca_key_der_2048));
- AssertNotNull(cm = wolfSSL_CertManagerNew());
- AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
- WOLFSSL_FILETYPE_ASN1));
- AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
- #else
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- #endif
- AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* add in matching DIR alt name and resign */
- wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
- #else
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- #endif
- AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_X509_free(x509);
- /* check verify fail */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- /* add in miss matching DIR alt name and resign */
- wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
- ASN_DIR_TYPE);
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
- #else
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- #endif
- AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
- #ifndef WOLFSSL_NO_ASN_STRICT
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- #else
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- #endif
- /* check that it still fails if one bad altname and one good altname is in
- * the certificate */
- wolfSSL_X509_free(x509);
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
- wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
- ASN_DIR_TYPE);
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
- #else
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- #endif
- AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
- #ifndef WOLFSSL_NO_ASN_STRICT
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- #else
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- #endif
- /* check it fails with switching position of bad altname */
- wolfSSL_X509_free(x509);
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
- ASN_DIR_TYPE);
- wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
- #else
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- #endif
- AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
- #ifndef WOLFSSL_NO_ASN_STRICT
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- #else
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- #endif
- wolfSSL_CertManagerFree(cm);
- wolfSSL_X509_free(x509);
- wolfSSL_X509_free(ca);
- /* now test with excluded name constraint */
- AssertNotNull(cm = wolfSSL_CertManagerNew());
- AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
- WOLFSSL_FILETYPE_ASN1));
- AssertNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
- ASN_DIR_TYPE);
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
- #else
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- #endif
- AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
- #ifndef WOLFSSL_NO_ASN_STRICT
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- #else
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- #endif
- wolfSSL_CertManagerFree(cm);
- wolfSSL_X509_free(x509);
- wolfSSL_X509_free(ca);
- wolfSSL_EVP_PKEY_free(priv);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CertManagerNameConstraint3(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
- defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
- !defined(NO_SHA256)
- WOLFSSL_CERT_MANAGER* cm;
- WOLFSSL_EVP_PKEY *priv;
- WOLFSSL_X509_NAME* name;
- const char* ca_cert = "./certs/test/cert-ext-mnc.der";
- const char* server_cert = "./certs/test/server-goodcn.pem";
- byte *der;
- int derSz;
- byte *pt;
- WOLFSSL_X509 *x509, *ca;
- pt = (byte*)server_key_der_2048;
- AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
- (const unsigned char**)&pt, sizeof_server_key_der_2048));
- AssertNotNull(cm = wolfSSL_CertManagerNew());
- AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
- WOLFSSL_FILETYPE_ASN1));
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
- DEBUG_WRITE_DER(der, derSz, "ca.der");
- AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* check satisfying .wolfssl.com constraint passes */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
- (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_X509_free(x509);
- /* check satisfying .random.com constraint passes */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
- (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_X509_free(x509);
- /* check fail case when neither constraint is matched */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
- (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- wolfSSL_CertManagerFree(cm);
- wolfSSL_X509_free(x509);
- wolfSSL_X509_free(ca);
- wolfSSL_EVP_PKEY_free(priv);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CertManagerNameConstraint4(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
- defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
- !defined(NO_SHA256)
- WOLFSSL_CERT_MANAGER* cm;
- WOLFSSL_EVP_PKEY *priv;
- WOLFSSL_X509_NAME* name;
- const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
- const char* server_cert = "./certs/test/server-goodcn.pem";
- byte *der;
- int derSz;
- byte *pt;
- WOLFSSL_X509 *x509, *ca;
- pt = (byte*)server_key_der_2048;
- AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
- (const unsigned char**)&pt, sizeof_server_key_der_2048));
- AssertNotNull(cm = wolfSSL_CertManagerNew());
- AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
- WOLFSSL_FILETYPE_ASN1));
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
- DEBUG_WRITE_DER(der, derSz, "ca.der");
- AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* check satisfying wolfssl.com constraint passes */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_X509_free(x509);
- /* check satisfying example.com constraint passes */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_X509_free(x509);
- /* check satisfying wolfssl.com constraint passes with list of DNS's */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
- wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
- wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_X509_free(x509);
- /* check fail when one DNS in the list is bad */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
- wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
- wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- wolfSSL_X509_free(x509);
- /* check fail case when neither constraint is matched */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"common", 6, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- wolfSSL_CertManagerFree(cm);
- wolfSSL_X509_free(x509);
- wolfSSL_X509_free(ca);
- wolfSSL_EVP_PKEY_free(priv);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CertManagerNameConstraint5(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
- defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
- !defined(NO_SHA256)
- WOLFSSL_CERT_MANAGER* cm;
- WOLFSSL_EVP_PKEY *priv;
- WOLFSSL_X509_NAME* name;
- const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
- const char* server_cert = "./certs/test/server-goodcn.pem";
- byte *der;
- int derSz;
- byte *pt;
- WOLFSSL_X509 *x509, *ca;
- pt = (byte*)server_key_der_2048;
- AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
- (const unsigned char**)&pt, sizeof_server_key_der_2048));
- AssertNotNull(cm = wolfSSL_CertManagerNew());
- AssertNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
- WOLFSSL_FILETYPE_ASN1));
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
- DEBUG_WRITE_DER(der, derSz, "ca.der");
- AssertIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* check satisfying wolfssl.com constraint passes */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"example", 7, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
- wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_X509_free(x509);
- /* fail with DNS check because of common name */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
- wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- wolfSSL_X509_free(x509);
- /* fail on permitted DNS name constraint */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
- wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
- wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- wolfSSL_X509_free(x509);
- /* fail on permitted email name constraint */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
- wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
- wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
- wolfSSL_X509_free(x509);
- /* success with empty email name */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- X509_NAME_free(name);
- wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
- AssertIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
- DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
- AssertNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_X509_free(x509);
- wolfSSL_CertManagerFree(cm);
- wolfSSL_X509_free(ca);
- wolfSSL_EVP_PKEY_free(priv);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_FPKI(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- XFILE f;
- const char* fpkiCert = "./certs/fpki-cert.der";
- DecodedCert cert;
- byte buf[4096];
- byte* uuid;
- byte* fascn;
- word32 fascnSz;
- word32 uuidSz;
- int bytes;
- f = XFOPEN(fpkiCert, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- wc_InitDecodedCert(&cert, buf, bytes, NULL);
- AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
- AssertIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
- fascn = (byte*)XMALLOC(fascnSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- AssertNotNull(fascn);
- AssertIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
- XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- AssertIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
- uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- AssertNotNull(uuid);
- AssertIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
- XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- wc_FreeDecodedCert(&cert);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /* use RID in confuncture with other names to test parsing of unknown other
- * names */
- static int test_wolfSSL_OtherName(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- XFILE f;
- const char* ridCert = "./certs/rid-cert.der";
- DecodedCert cert;
- byte buf[4096];
- int bytes;
- f = XFOPEN(ridCert, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- wc_InitDecodedCert(&cert, buf, bytes, NULL);
- AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
- wc_FreeDecodedCert(&cert);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CertRsaPss(void)
- {
- int res = TEST_SKIPPED;
- /* FIPS v2 and below don't support long salts. */
- #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
- (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
- (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
- (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
- XFILE f;
- const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
- const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
- #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
- RSA_MAX_SIZE >= 3072
- const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
- #endif
- #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
- const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
- #endif
- DecodedCert cert;
- byte buf[4096];
- int bytes;
- WOLFSSL_CERT_MANAGER* cm;
- cm = wolfSSL_CertManagerNew();
- AssertNotNull(cm);
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
- #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
- #endif
- f = XFOPEN(rsaPssSha256Cert, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- wc_InitDecodedCert(&cert, buf, bytes, NULL);
- AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
- wc_FreeDecodedCert(&cert);
- #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
- RSA_MAX_SIZE >= 3072
- f = XFOPEN(rsaPssSha384Cert, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- wc_InitDecodedCert(&cert, buf, bytes, NULL);
- AssertIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
- wc_FreeDecodedCert(&cert);
- #endif
- wolfSSL_CertManagerFree(cm);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CertManagerCRL(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
- !defined(NO_RSA)
- const char* ca_cert = "./certs/ca-cert.pem";
- const char* crl1 = "./certs/crl/crl.pem";
- const char* crl2 = "./certs/crl/crl2.pem";
- WOLFSSL_CERT_MANAGER* cm = NULL;
- AssertNotNull(cm = wolfSSL_CertManagerNew());
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
- wolfSSL_CertManagerFreeCRL(cm);
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
- wolfSSL_CertManagerFree(cm);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_load_verify_locations_ex(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX* ctx;
- const char* ca_cert = "./certs/ca-cert.pem";
- const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- /* test good CA */
- AssertTrue(WOLFSSL_SUCCESS ==
- wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
- WOLFSSL_LOAD_FLAG_NONE));
- /* test expired CA */
- #ifndef OPENSSL_COMPATIBLE_DEFAULTS
- AssertIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
- WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
- WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
- #endif
- AssertIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
- WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- defined(USE_CERT_BUFFERS_2048)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX* ctx;
- const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
- byte ca_expired_cert[TWOK_BUF];
- word32 sizeof_ca_expired_cert;
- XFILE fp;
- #ifndef NO_WOLFSSL_CLIENT
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- #endif
- AssertNotNull(ctx);
- /* test good CA */
- AssertTrue(WOLFSSL_SUCCESS ==
- wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
- sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
- WOLFSSL_LOAD_FLAG_NONE));
- /* load expired CA */
- XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
- fp = XFOPEN(ca_expired_cert_file, "rb");
- AssertTrue(fp != XBADFILE);
- sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
- sizeof(ca_expired_cert), fp);
- XFCLOSE(fp);
- /* test expired CA failure */
- #ifndef OPENSSL_COMPATIBLE_DEFAULTS
- AssertIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
- sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
- WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
- sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
- WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
- #endif
- /* test expired CA success */
- AssertIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
- sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
- WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
- WOLFSSL_CTX* ctx;
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #endif
- AssertTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
- ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
- WOLFSSL_FILETYPE_ASN1));
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_add1_chain_cert(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
- defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- const char *certChain[] = {
- "./certs/intermediate/client-int-cert.pem",
- "./certs/intermediate/ca-int2-cert.pem",
- "./certs/intermediate/ca-int-cert.pem",
- "./certs/ca-cert.pem",
- NULL
- };
- const char** cert;
- WOLFSSL_X509* x509;
- WOLF_STACK_OF(X509)* chain = NULL;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertNotNull(ssl = wolfSSL_new(ctx));
- for (cert = certChain; *cert != NULL; cert++) {
- x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- AssertIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
- X509_free(x509);
- }
- for (cert = certChain; *cert != NULL; cert++) {
- x509 = wolfSSL_X509_load_certificate_file(*cert, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- AssertIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
- X509_free(x509);
- }
- AssertIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
- AssertIntEQ(sk_X509_num(chain), 3);
- AssertIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
- AssertIntEQ(sk_X509_num(chain), 3);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
- const char* server_chain_der = "./certs/server-cert-chain.der";
- const char* client_single_pem = "./certs/client-cert.pem";
- WOLFSSL_CTX* ctx;
- int ret = 0;
- (void)server_chain_der;
- (void)client_single_pem;
- (void)ctx;
- #ifndef NO_WOLFSSL_CLIENT
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #endif
- AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
- server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
- client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_SetTmpDH_file(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
- WOLFSSL_CTX *ctx;
- (void)ctx;
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #endif
- /* invalid context */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
- dhParamFile, WOLFSSL_FILETYPE_PEM));
- /* invalid dhParamFile file */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
- NULL, WOLFSSL_FILETYPE_PEM));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
- bogusFile, WOLFSSL_FILETYPE_PEM));
- /* success */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
- WOLFSSL_FILETYPE_PEM));
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_DH) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
- WOLFSSL_CTX *ctx;
- (void)ctx;
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #endif
- /* invalid context */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- /* invalid dhParamFile file */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
- 0, WOLFSSL_FILETYPE_ASN1));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dsa_key_der_2048,
- sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- /* success */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_DH) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
- WOLFSSL_CTX *ctx;
- (void)ctx;
- #ifndef NO_WOLFSSL_CLIENT
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
- AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
- AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_der_load_verify_locations(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
- WOLFSSL_CTX* ctx = NULL;
- const char* derCert = "./certs/server-cert.der";
- const char* nullPath = NULL;
- const char* invalidPath = "./certs/this-cert-does-not-exist.der";
- const char* emptyPath = "";
- /* der load Case 1 ctx NULL */
- AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #endif
- /* Case 2 filePath NULL */
- AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
- /* Case 3 invalid format */
- AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
- /* Case 4 filePath not valid */
- AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
- /* Case 5 filePath empty */
- AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
- #ifndef NO_RSA
- /* Case 6 success case */
- AssertIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- #endif
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_enable_disable(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_CERTS
- WOLFSSL_CTX* ctx = NULL;
- #ifdef HAVE_CRL
- AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
- #endif
- #ifdef HAVE_OCSP
- AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
- #endif
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
- defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
- AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifdef HAVE_EXTENDED_MASTER
- AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
- #endif
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #ifdef HAVE_EXTENDED_MASTER
- AssertIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
- #endif
- #elif !defined(NO_WOLFSSL_SERVER)
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #else
- return TEST_SUCCESS;
- #endif
- #ifdef HAVE_CRL
- AssertIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
- #endif
- #ifdef HAVE_OCSP
- AssertIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
- WOLFSSL_SUCCESS);
- #endif
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
- defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
- AssertIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
- #endif
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* NO_CERTS */
- return res;
- }
- static int test_wolfSSL_CTX_ticket_API(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX* ctx = NULL;
- void *userCtx = (void*)"this is my ctx";
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
- AssertTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
- wolfSSL_CTX_free(ctx);
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
- AssertNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
- return res;
- }
- static int test_wolfSSL_set_minmax_proto_version(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_EXTRA
- WOLFSSL_CTX *ctx;
- WOLFSSL *ssl;
- int ret;
- (void)ret;
- (void)ssl;
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
- AssertIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
- AssertIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*----------------------------------------------------------------------------*
- | SSL
- *----------------------------------------------------------------------------*/
- static int test_server_wolfSSL_new(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX *ctx;
- WOLFSSL_CTX *ctx_nocert;
- WOLFSSL *ssl;
- AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- /* invalid context */
- AssertNull(ssl = wolfSSL_new(NULL));
- #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && !defined(OPENSSL_EXTRA)
- AssertNull(ssl = wolfSSL_new(ctx_nocert));
- #endif
- /* success */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- wolfSSL_CTX_free(ctx_nocert);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_client_wolfSSL_new(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX *ctx;
- WOLFSSL_CTX *ctx_nocert;
- WOLFSSL *ssl;
- AssertNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- /* invalid context */
- AssertNull(ssl = wolfSSL_new(NULL));
- /* success */
- AssertNotNull(ssl = wolfSSL_new(ctx_nocert));
- wolfSSL_free(ssl);
- /* success */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- wolfSSL_CTX_free(ctx_nocert);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SetTmpDH_file(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
- !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX *ctx;
- WOLFSSL *ssl;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #ifndef NO_RSA
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- #elif defined(HAVE_ECC)
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
- WOLFSSL_FILETYPE_PEM));
- #elif defined(HAVE_ED25519)
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
- WOLFSSL_FILETYPE_PEM));
- #elif defined(HAVE_ED448)
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
- WOLFSSL_FILETYPE_PEM));
- #endif
- AssertNotNull(ssl = wolfSSL_new(ctx));
- /* invalid ssl */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
- dhParamFile, WOLFSSL_FILETYPE_PEM));
- /* invalid dhParamFile file */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
- NULL, WOLFSSL_FILETYPE_PEM));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
- bogusFile, WOLFSSL_FILETYPE_PEM));
- /* success */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
- WOLFSSL_FILETYPE_PEM));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SetTmpDH_buffer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX *ctx;
- WOLFSSL *ssl;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
- sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
- sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertNotNull(ssl = wolfSSL_new(ctx));
- /* invalid ssl */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- /* invalid dhParamFile file */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
- 0, WOLFSSL_FILETYPE_ASN1));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
- sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- /* success */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX *ctx, *ctx2;
- WOLFSSL *ssl, *ssl2;
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
- sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
- sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx2);
- AssertTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
- sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
- sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
- ssl2 = wolfSSL_new(ctx2);
- AssertNotNull(ssl2);
- AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
- AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
- AssertIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
- sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
- wolfSSL_free(ssl2);
- wolfSSL_CTX_free(ctx2);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
- * allowed.
- * POST: return 1 on success.
- */
- static int test_wolfSSL_SetMinVersion(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_WOLFSSL_CLIENT
- int failFlag = WOLFSSL_SUCCESS;
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- int itr;
- #ifndef NO_OLD_TLS
- const int versions[] = {
- #ifdef WOLFSSL_ALLOW_TLSV10
- WOLFSSL_TLSV1,
- #endif
- WOLFSSL_TLSV1_1,
- WOLFSSL_TLSV1_2};
- #elif !defined(WOLFSSL_NO_TLS12)
- const int versions[] = { WOLFSSL_TLSV1_2 };
- #else
- const int versions[] = { WOLFSSL_TLSV1_3 };
- #endif
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- ssl = wolfSSL_new(ctx);
- for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
- if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
- failFlag = WOLFSSL_FAILURE;
- }
- }
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
- #endif
- return res;
- } /* END test_wolfSSL_SetMinVersion */
- /*----------------------------------------------------------------------------*
- | EC
- *----------------------------------------------------------------------------*/
- /* Test function for EC_POINT_new, EC_POINT_mul, EC_POINT_free,
- EC_GROUP_new_by_curve_name, EC_GROUP_order_bits
- */
- #ifdef OPENSSL_EXTRA
- static int test_wolfSSL_EC(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(WOLFSSL_SP_MATH) && \
- (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
- #if defined(HAVE_ECC)
- BN_CTX *ctx;
- EC_GROUP *group;
- EC_GROUP *group2;
- EC_POINT *Gxy, *new_point, *set_point;
- BIGNUM *k = NULL, *Gx = NULL, *Gy = NULL, *Gz = NULL;
- BIGNUM *X, *Y;
- BIGNUM *set_point_bn;
- char* hexStr;
- int group_bits;
- const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD17AF381913FF7A96314EA47055EA0FD0";
- /* NISTP256R1 Gx/Gy */
- const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
- const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
- #ifndef HAVE_SELFTEST
- EC_POINT *tmp;
- size_t bin_len;
- unsigned char* buf = NULL;
- const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C2964FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5";
- const unsigned char binUncompG[] = {
- 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
- 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
- 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
- 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
- 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
- 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
- };
- #ifdef HAVE_COMP_KEY
- const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296";
- const unsigned char binCompG[] = {
- 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
- 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
- 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
- };
- #endif
- #endif
- AssertNotNull(ctx = BN_CTX_new());
- AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
- AssertNotNull(group2 = EC_GROUP_dup(group));
- AssertIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
- AssertNotNull(Gxy = EC_POINT_new(group));
- AssertNotNull(new_point = EC_POINT_new(group));
- AssertNotNull(set_point = EC_POINT_new(group));
- AssertNotNull(X = BN_new());
- AssertNotNull(Y = BN_new());
- AssertNotNull(set_point_bn = BN_new());
- /* load test values */
- AssertIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
- AssertIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
- AssertIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
- AssertIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
- /* populate coordinates for input point */
- Gxy->X = Gx;
- Gxy->Y = Gy;
- Gxy->Z = Gz;
- #ifndef HAVE_SELFTEST
- /* perform point multiplication */
- AssertIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(BN_is_zero(new_point->X), 0);
- AssertIntEQ(BN_is_zero(new_point->Y), 0);
- AssertIntEQ(BN_is_zero(new_point->Z), 0);
- AssertIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(BN_is_zero(new_point->X), 0);
- AssertIntEQ(BN_is_zero(new_point->Y), 0);
- AssertIntEQ(BN_is_zero(new_point->Z), 0);
- AssertIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(BN_is_zero(new_point->X), 0);
- AssertIntEQ(BN_is_zero(new_point->Y), 0);
- AssertIntEQ(BN_is_zero(new_point->Z), 0);
- #else
- AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy, ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(BN_is_zero(new_point->X), 0);
- AssertIntEQ(BN_is_zero(new_point->Y), 0);
- AssertIntEQ(BN_is_zero(new_point->Z), 0);
- #endif
- /* check if point X coordinate is zero */
- AssertIntEQ(BN_is_zero(new_point->X), 0);
- #ifdef USE_ECC_B_PARAM
- AssertIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
- #endif /* USE_ECC_B_PARAM */
- /* Force non-affine coordinates */
- AssertIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
- (WOLFSSL_BIGNUM*)BN_value_one()), 1);
- new_point->inSet = 0;
- /* extract the coordinates from point */
- AssertIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y, ctx), WOLFSSL_SUCCESS);
- /* check if point X coordinate is zero */
- AssertIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
- /* set the same X and Y points in another object */
- AssertIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y, ctx), WOLFSSL_SUCCESS);
- /* compare points as they should be the same */
- AssertIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
- /* Test copying */
- AssertIntEQ(EC_POINT_copy(new_point, set_point), 1);
- /* Test inverting */
- AssertIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
- AssertPtrEq(EC_POINT_point2bn(group, set_point, POINT_CONVERSION_UNCOMPRESSED,
- set_point_bn, ctx), set_point_bn);
- /* check bn2hex */
- hexStr = BN_bn2hex(k);
- AssertStrEQ(hexStr, kTest);
- #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
- BN_print_fp(stderr, k);
- fprintf(stderr, "\n");
- #endif
- XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
- hexStr = BN_bn2hex(Gx);
- AssertStrEQ(hexStr, kGx);
- #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
- BN_print_fp(stderr, Gx);
- fprintf(stderr, "\n");
- #endif
- XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
- hexStr = BN_bn2hex(Gy);
- AssertStrEQ(hexStr, kGy);
- #if !defined(NO_FILESYSTEM) && defined(XFPRINTF)
- BN_print_fp(stderr, Gy);
- fprintf(stderr, "\n");
- #endif
- XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
- #ifndef HAVE_SELFTEST
- hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
- AssertStrEQ(hexStr, uncompG);
- XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
- #ifdef HAVE_COMP_KEY
- hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
- AssertStrEQ(hexStr, compG);
- XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
- #endif
- bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx);
- AssertIntEQ(bin_len, sizeof(binUncompG));
- AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
- AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, buf,
- bin_len, ctx), bin_len);
- AssertIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
- XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
- #ifdef HAVE_COMP_KEY
- bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL, 0, ctx);
- AssertIntEQ(bin_len, sizeof(binCompG));
- AssertNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL, DYNAMIC_TYPE_ECC));
- AssertIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
- bin_len, ctx), bin_len);
- AssertIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
- XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
- #endif
- AssertNotNull(tmp = EC_POINT_new(group));
- AssertIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG), ctx), 1);
- AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
- EC_POINT_free(tmp);
- #ifdef HAVE_COMP_KEY
- AssertNotNull(tmp = EC_POINT_new(group));
- AssertIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx), 1);
- AssertIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
- EC_POINT_free(tmp);
- #endif
- #endif
- /* test BN_mod_add */
- AssertIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
- (WOLFSSL_BIGNUM*)BN_value_one(),
- (WOLFSSL_BIGNUM*)BN_value_one(), NULL), 1);
- AssertIntEQ(BN_is_zero(new_point->Z), 1);
- /* cleanup */
- BN_free(X);
- BN_free(Y);
- BN_free(k);
- BN_free(set_point_bn);
- EC_POINT_free(new_point);
- EC_POINT_free(set_point);
- EC_POINT_free(Gxy);
- EC_GROUP_free(group);
- EC_GROUP_free(group2);
- BN_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ECC */
- #endif /* OPENSSL_EXTRA && !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
- return res;
- }
- #endif /* OPENSSL_EXTRA */
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
- EC_GROUP *group;
- BIO* bio;
- AssertNotNull(bio = BIO_new(BIO_s_file()));
- AssertIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
- AssertNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
- AssertIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
- EC_GROUP_free(group);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ECC */
- return res;
- }
- #endif /* !NO_BIO */
- # if defined(OPENSSL_EXTRA)
- static int test_wolfSSL_ECDSA_SIG(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_ECC
- WOLFSSL_ECDSA_SIG* sig = NULL;
- WOLFSSL_ECDSA_SIG* sig2 = NULL;
- const unsigned char* cp;
- unsigned char* p;
- unsigned char outSig[8];
- unsigned char sigData[8] =
- { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
- sig = wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData));
- AssertNull(sig);
- cp = sigData;
- AssertNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
- AssertIntEQ((cp == sigData + 8), 1);
- cp = sigData;
- AssertNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
- AssertNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
- AssertIntEQ((sig == sig2), 1);
- cp = outSig;
- p = outSig;
- AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
- AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
- AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
- AssertIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
- AssertIntEQ((p == outSig + 8), 1);
- AssertIntEQ(XMEMCMP(sigData, outSig, 8), 0);
- wolfSSL_ECDSA_SIG_free(sig);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ECC */
- return res;
- }
- static int test_EC_i2d(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(HAVE_FIPS)
- EC_KEY *key;
- EC_KEY *copy;
- int len;
- unsigned char *buf = NULL;
- const unsigned char *tmp = NULL;
- AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
- AssertIntEQ(EC_KEY_generate_key(key), 1);
- AssertIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
- AssertIntEQ(i2d_EC_PUBKEY(key, &buf), len);
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- buf = NULL;
- AssertIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
- AssertIntEQ(i2d_ECPrivateKey(key, &buf), len);
- tmp = buf;
- AssertNotNull(d2i_ECPrivateKey(©, &tmp, len));
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- buf = NULL;
- AssertIntGT((len = i2o_ECPublicKey(key, &buf)), 0);
- tmp = buf;
- AssertNotNull(o2i_ECPublicKey(©, &tmp, len));
- AssertIntEQ(EC_KEY_check_key(key), 1);
- XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
- EC_KEY_free(key);
- EC_KEY_free(copy);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ECC */
- return res;
- }
- static int test_ECDSA_size_sign(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
- EC_KEY *key;
- int id;
- byte hash[WC_MAX_DIGEST_SIZE];
- byte sig[ECC_MAX_SIG_SIZE];
- unsigned int sigSz = sizeof(sig);
- XMEMSET(hash, 123, sizeof(hash));
- id = wc_ecc_get_curve_id_from_name("SECP256R1");
- AssertIntEQ(id, ECC_SECP256R1);
- AssertNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
- AssertIntEQ(EC_KEY_generate_key(key), 1);
- AssertIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
- AssertIntGE(ECDSA_size(key), sigSz);
- AssertIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
- EC_KEY_free(key);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
- return res;
- }
- static int test_ED25519(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
- defined(WOLFSSL_KEY_GEN)
- byte priv[ED25519_PRV_KEY_SIZE];
- unsigned int privSz = (unsigned int)sizeof(priv);
- byte pub[ED25519_PUB_KEY_SIZE];
- unsigned int pubSz = (unsigned int)sizeof(pub);
- #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
- const char* msg = TEST_STRING;
- unsigned int msglen = (unsigned int)TEST_STRING_SZ;
- byte sig[ED25519_SIG_SIZE];
- unsigned int sigSz = (unsigned int)sizeof(sig);
- #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
- AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
- AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
- #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
- AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
- &sigSz), WOLFSSL_SUCCESS);
- AssertIntEQ(sigSz, ED25519_SIG_SIZE);
- #ifdef HAVE_ED25519_VERIFY
- AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
- sigSz), WOLFSSL_SUCCESS);
- #endif /* HAVE_ED25519_VERIFY */
- #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
- return res;
- }
- static int test_ED448(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
- defined(WOLFSSL_KEY_GEN)
- byte priv[ED448_PRV_KEY_SIZE];
- unsigned int privSz = (unsigned int)sizeof(priv);
- byte pub[ED448_PUB_KEY_SIZE];
- unsigned int pubSz = (unsigned int)sizeof(pub);
- #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
- const char* msg = TEST_STRING;
- unsigned int msglen = (unsigned int)TEST_STRING_SZ;
- byte sig[ED448_SIG_SIZE];
- unsigned int sigSz = (unsigned int)sizeof(sig);
- #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
- AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
- AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
- #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
- AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
- &sigSz), WOLFSSL_SUCCESS);
- AssertIntEQ(sigSz, ED448_SIG_SIZE);
- #ifdef HAVE_ED448_VERIFY
- AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
- sigSz), WOLFSSL_SUCCESS);
- #endif /* HAVE_ED448_VERIFY */
- #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
- return res;
- }
- #endif /* OPENSSL_EXTRA */
- #include <wolfssl/openssl/pem.h>
- /*----------------------------------------------------------------------------*
- | EVP
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_EVP_PKEY_print_public(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
- WOLFSSL_BIO* rbio = NULL;
- WOLFSSL_BIO* wbio = NULL;
- WOLFSSL_EVP_PKEY* pkey = NULL;
- char line[256] = { 0 };
- char line1[256] = { 0 };
- int i;
- /* test error cases */
- AssertIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
- /*
- * test RSA public key print
- * in this test, pass '3' for indent
- */
- #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
- rbio = BIO_new_mem_buf( client_keypub_der_1024,
- sizeof_client_keypub_der_1024);
- AssertNotNull(rbio);
- wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
- AssertNotNull(pkey);
- wbio = BIO_new(BIO_s_mem());
- AssertNotNull(wbio);
- AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, " RSA Public-Key: (1024 bit)\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, " Modulus:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* skip to the end of modulus element*/
- for (i = 0; i < 8 ;i++) {
- BIO_gets(wbio, line, sizeof(line));
- }
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, " Exponent: 65537 (0x010001)\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* should reach EOF */
- AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- BIO_free(rbio);
- BIO_free(wbio);
- rbio = NULL;
- wbio = NULL;
- #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
- /*
- * test DSA public key print
- */
- #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
- rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
- sizeof_dsa_pub_key_der_2048);
- AssertNotNull(rbio);
- wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
- AssertNotNull(pkey);
- wbio = BIO_new(BIO_s_mem());
- AssertNotNull(wbio);
- AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "DSA Public-Key: (2048 bit)\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "pub:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1,
- " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* skip to the end of pub element*/
- for (i = 0; i < 17 ;i++) {
- BIO_gets(wbio, line, sizeof(line));
- }
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "P:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* skip to the end of P element*/
- for (i = 0; i < 18 ;i++) {
- BIO_gets(wbio, line, sizeof(line));
- }
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "Q:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* skip to the end of Q element*/
- for (i = 0; i < 3 ;i++) {
- BIO_gets(wbio, line, sizeof(line));
- }
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "G:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* skip to the end of G element*/
- for (i = 0; i < 18 ;i++) {
- BIO_gets(wbio, line, sizeof(line));
- }
- /* should reach EOF */
- AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- BIO_free(rbio);
- BIO_free(wbio);
- rbio = NULL;
- wbio = NULL;
- #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
- /*
- * test ECC public key print
- */
- #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
- rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
- sizeof_ecc_clikeypub_der_256);
- AssertNotNull(rbio);
- wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
- AssertNotNull(pkey);
- wbio = BIO_new(BIO_s_mem());
- AssertNotNull(wbio);
- AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "Public-Key: (256 bit)\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "pub:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1,
- " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* skip to the end of pub element*/
- for (i = 0; i < 4 ;i++) {
- BIO_gets(wbio, line, sizeof(line));
- }
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "ASN1 OID: prime256v1\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "NIST CURVE: P-256\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* should reach EOF */
- AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- BIO_free(rbio);
- BIO_free(wbio);
- rbio = NULL;
- wbio = NULL;
- #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
- /*
- * test DH public key print
- */
- #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
- rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
- sizeof_dh_pub_key_der_2048);
- AssertNotNull(rbio);
- wolfSSL_d2i_PUBKEY_bio(rbio, &pkey);
- AssertNotNull(pkey);
- wbio = BIO_new(BIO_s_mem());
- AssertNotNull(wbio);
- AssertIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "DH Public-Key: (2048 bit)\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "public-key:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1,
- " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* skip to the end of public-key element*/
- for (i = 0; i < 17 ;i++) {
- BIO_gets(wbio, line, sizeof(line));
- }
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "prime:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1,
- " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* skip to the end of prime element*/
- for (i = 0; i < 17 ;i++) {
- BIO_gets(wbio, line, sizeof(line));
- }
- BIO_gets(wbio, line, sizeof(line));
- strcpy(line1, "generator: 2 (0x02)\n");
- AssertIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
- /* should reach EOF */
- AssertIntLE(BIO_gets(wbio, line, sizeof(line)) ,0);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- BIO_free(rbio);
- BIO_free(wbio);
- rbio = NULL;
- wbio = NULL;
- #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
- /* to prevent "unused variable" warning */
- (void)pkey;
- (void)wbio;
- (void)rbio;
- (void)line;
- (void)line1;
- (void)i;
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- /* Test functions for base64 encode/decode */
- static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && \
- ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
- EVP_ENCODE_CTX* ctx = NULL;
- AssertNotNull( ctx = EVP_ENCODE_CTX_new());
- AssertIntEQ( ctx->remaining,0);
- AssertIntEQ( ctx->data[0],0);
- AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
- EVP_ENCODE_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
- return res;
- }
- static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && \
- ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
- EVP_ENCODE_CTX* ctx = NULL;
- AssertNotNull( ctx = EVP_ENCODE_CTX_new());
- EVP_ENCODE_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
- return res;
- }
- static int test_wolfSSL_EVP_EncodeInit(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
- EVP_ENCODE_CTX* ctx = NULL;
- AssertNotNull( ctx = EVP_ENCODE_CTX_new());
- AssertIntEQ( ctx->remaining,0);
- AssertIntEQ( ctx->data[0],0);
- AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
- /* make ctx dirty */
- ctx->remaining = 10;
- XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
- EVP_EncodeInit(ctx);
- AssertIntEQ( ctx->remaining,0);
- AssertIntEQ( ctx->data[0],0);
- AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
- EVP_ENCODE_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
- return res;
- }
- static int test_wolfSSL_EVP_EncodeUpdate(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
- int outl;
- int total;
- const unsigned char plain0[] = {"Th"};
- const unsigned char plain1[] = {"This is a base64 encodeing test."};
- const unsigned char plain2[] = {"This is additional data."};
- const unsigned char enc0[] = {"VGg=\n"};
- /* expected encoded result for the first output 64 chars plus trailing LF*/
- const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
- const unsigned char enc2[] =
- {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
- unsigned char encOutBuff[300];
- EVP_ENCODE_CTX* ctx = NULL;
- AssertNotNull( ctx = EVP_ENCODE_CTX_new());
- EVP_EncodeInit(ctx);
- /* illegal parameter test */
- AssertIntEQ(
- EVP_EncodeUpdate(
- NULL, /* pass NULL as ctx */
- encOutBuff,
- &outl,
- plain1,
- sizeof(plain1)-1),
- 0 /* expected result code 0: fail */
- );
- AssertIntEQ(
- EVP_EncodeUpdate(
- ctx,
- NULL, /* pass NULL as out buff */
- &outl,
- plain1,
- sizeof(plain1)-1),
- 0 /* expected result code 0: fail */
- );
- AssertIntEQ(
- EVP_EncodeUpdate(
- ctx,
- encOutBuff,
- NULL, /* pass NULL as outl */
- plain1,
- sizeof(plain1)-1),
- 0 /* expected result code 0: fail */
- );
- AssertIntEQ(
- EVP_EncodeUpdate(
- ctx,
- encOutBuff,
- &outl,
- NULL, /* pass NULL as in */
- sizeof(plain1)-1),
- 0 /* expected result code 0: fail */
- );
- AssertIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
- /* meaningless parameter test */
- AssertIntEQ(
- EVP_EncodeUpdate(
- ctx,
- encOutBuff,
- &outl,
- plain1,
- 0), /* pass zero input */
- 1 /* expected result code 1: success */
- );
- /* very small data encoding test */
- EVP_EncodeInit(ctx);
- AssertIntEQ(
- EVP_EncodeUpdate(
- ctx,
- encOutBuff,
- &outl,
- plain0,
- sizeof(plain0)-1),
- 1 /* expected result code 1: success */
- );
- AssertIntEQ(outl,0);
- EVP_EncodeFinal(
- ctx,
- encOutBuff + outl,
- &outl);
- AssertIntEQ( outl, sizeof(enc0)-1);
- AssertIntEQ(
- XSTRNCMP(
- (const char*)encOutBuff,
- (const char*)enc0,sizeof(enc0) ),
- 0);
- XMEMSET( encOutBuff,0, sizeof(encOutBuff));
- AssertIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
- sizeof(enc0)-1);
- AssertIntEQ(
- XSTRNCMP(
- (const char*)encOutBuff,
- (const char*)enc0,sizeof(enc0) ),
- 0);
- /* pass small size( < 48bytes ) input, then make sure they are not
- * encoded and just stored in ctx
- */
- EVP_EncodeInit(ctx);
- total = 0;
- outl = 0;
- XMEMSET( encOutBuff,0, sizeof(encOutBuff));
- AssertIntEQ(
- EVP_EncodeUpdate(
- ctx,
- encOutBuff, /* buffer for output */
- &outl, /* size of output */
- plain1, /* input */
- sizeof(plain1)-1), /* size of input */
- 1); /* expected result code 1:success */
- total += outl;
- AssertIntEQ(outl, 0); /* no output expected */
- AssertIntEQ(ctx->remaining, sizeof(plain1) -1);
- AssertTrue(
- XSTRNCMP((const char*)(ctx->data),
- (const char*)plain1,
- ctx->remaining) ==0 );
- AssertTrue(encOutBuff[0] == 0);
- /* call wolfSSL_EVP_EncodeUpdate again to make it encode
- * the stored data and the new input together
- */
- AssertIntEQ(
- EVP_EncodeUpdate(
- ctx,
- encOutBuff + outl, /* buffer for output */
- &outl, /* size of output */
- plain2, /* additional input */
- sizeof(plain2) -1), /* size of additional input */
- 1); /* expected result code 1:success */
- total += outl;
- AssertIntNE(outl, 0); /* some output is expected this time*/
- AssertIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
- AssertIntEQ(
- XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
- /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
- EVP_EncodeFinal(
- ctx,
- encOutBuff + outl,
- &outl);
- total += outl;
- AssertIntNE(total,0);
- AssertIntNE(outl,0);
- AssertIntEQ(XSTRNCMP(
- (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
- /* test with illeagal parameters */
- outl = 1;
- EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
- AssertIntEQ(outl, 0);
- outl = 1;
- EVP_EncodeFinal(ctx, NULL, &outl);
- AssertIntEQ(outl, 0);
- EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
- EVP_EncodeFinal(NULL, NULL, NULL);
- EVP_ENCODE_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
- return res;
- }
- static int test_wolfSSL_EVP_EncodeFinal(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
- /* tests for wolfSSL_EVP_EncodeFinal are included in
- * test_wolfSSL_EVP_EncodeUpdate
- */
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
- return res;
- }
- static int test_wolfSSL_EVP_DecodeInit(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
- EVP_ENCODE_CTX* ctx = NULL;
- AssertNotNull( ctx = EVP_ENCODE_CTX_new());
- AssertIntEQ( ctx->remaining,0);
- AssertIntEQ( ctx->data[0],0);
- AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
- /* make ctx dirty */
- ctx->remaining = 10;
- XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
- EVP_DecodeInit(ctx);
- AssertIntEQ( ctx->remaining,0);
- AssertIntEQ( ctx->data[0],0);
- AssertIntEQ( ctx->data[sizeof(ctx->data) -1],0);
- EVP_ENCODE_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
- return res;
- }
- static int test_wolfSSL_EVP_DecodeUpdate(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
- int outl;
- unsigned char decOutBuff[300];
- EVP_ENCODE_CTX* ctx;
- static const unsigned char enc1[] =
- {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
- /* const unsigned char plain1[] =
- {"This is a base64 decoding test."} */
- ctx = EVP_ENCODE_CTX_new();
- EVP_DecodeInit(ctx);
- /* illegal parameter tests */
- /* pass NULL as ctx */
- AssertIntEQ(
- EVP_DecodeUpdate(
- NULL, /* pass NULL as ctx */
- decOutBuff,
- &outl,
- enc1,
- sizeof(enc1)-1),
- -1 /* expected result code -1: fail */
- );
- AssertIntEQ( outl, 0);
- /* pass NULL as output */
- AssertIntEQ(
- EVP_DecodeUpdate(
- ctx,
- NULL, /* pass NULL as out buff */
- &outl,
- enc1,
- sizeof(enc1)-1),
- -1 /* expected result code -1: fail */
- );
- AssertIntEQ( outl, 0);
- /* pass NULL as outl */
- AssertIntEQ(
- EVP_DecodeUpdate(
- ctx,
- decOutBuff,
- NULL, /* pass NULL as outl */
- enc1,
- sizeof(enc1)-1),
- -1 /* expected result code -1: fail */
- );
- /* pass NULL as input */
- AssertIntEQ(
- EVP_DecodeUpdate(
- ctx,
- decOutBuff,
- &outl,
- NULL, /* pass NULL as in */
- sizeof(enc1)-1),
- -1 /* expected result code -1: fail */
- );
- AssertIntEQ( outl, 0);
- AssertIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
- /* pass zero length input */
- AssertIntEQ(
- EVP_DecodeUpdate(
- ctx,
- decOutBuff,
- &outl,
- enc1,
- 0), /* pass zero as input len */
- 1 /* expected result code 1: success */
- );
- /* decode correct base64 string */
- {
- static const unsigned char enc2[] =
- {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
- static const unsigned char plain2[] =
- {"This is a base64 decoding test."};
- EVP_EncodeInit(ctx);
- AssertIntEQ(
- EVP_DecodeUpdate(
- ctx,
- decOutBuff,
- &outl,
- enc2,
- sizeof(enc2)-1),
- 0 /* expected result code 0: success */
- );
- AssertIntEQ(outl,sizeof(plain2) -1);
- AssertIntEQ(
- EVP_DecodeFinal(
- ctx,
- decOutBuff + outl,
- &outl),
- 1 /* expected result code 1: success */
- );
- AssertIntEQ(outl, 0); /* expected DecodeFinal outout no data */
- AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
- sizeof(plain2) -1 ),0);
- AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
- sizeof(plain2)-1);
- AssertIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
- sizeof(plain2) -1 ),0);
- }
- /* decode correct base64 string which does not have '\n' in its last*/
- {
- static const unsigned char enc3[] =
- {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
- static const unsigned char plain3[] =
- {"This is a base64 decoding test."}; /* 31 chars */
- EVP_EncodeInit(ctx);
- AssertIntEQ(
- EVP_DecodeUpdate(
- ctx,
- decOutBuff,
- &outl,
- enc3,
- sizeof(enc3)-1),
- 0 /* expected result code 0: success */
- );
- AssertIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
- AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
- sizeof(plain3) -1 ),0);
- AssertIntEQ(
- EVP_DecodeFinal(
- ctx,
- decOutBuff + outl,
- &outl),
- 1 /* expected result code 1: success */
- );
- AssertIntEQ(outl,0 );
- AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
- sizeof(plain3)-1);
- AssertIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
- sizeof(plain3) -1 ),0);
- }
- /* decode string which has a padding char ('=') in the illegal position*/
- {
- static const unsigned char enc4[] =
- {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
- EVP_EncodeInit(ctx);
- AssertIntEQ(
- EVP_DecodeUpdate(
- ctx,
- decOutBuff,
- &outl,
- enc4,
- sizeof(enc4)-1),
- -1 /* expected result code -1: error */
- );
- AssertIntEQ(outl,0);
- AssertIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
- }
- /* small data decode test */
- {
- static const unsigned char enc00[] = {"VG"};
- static const unsigned char enc01[] = {"g=\n"};
- static const unsigned char plain4[] = {"Th"};
- EVP_EncodeInit(ctx);
- AssertIntEQ(
- EVP_DecodeUpdate(
- ctx,
- decOutBuff,
- &outl,
- enc00,
- sizeof(enc00)-1),
- 1 /* expected result code 1: success */
- );
- AssertIntEQ(outl,0);
- AssertIntEQ(
- EVP_DecodeUpdate(
- ctx,
- decOutBuff + outl,
- &outl,
- enc01,
- sizeof(enc01)-1),
- 0 /* expected result code 0: success */
- );
- AssertIntEQ(outl,sizeof(plain4)-1);
- /* test with illegal parameters */
- AssertIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
- AssertIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
- AssertIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
- AssertIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
- EVP_DecodeFinal(
- ctx,
- decOutBuff + outl,
- &outl);
- AssertIntEQ( outl, 0);
- AssertIntEQ(
- XSTRNCMP(
- (const char*)decOutBuff,
- (const char*)plain4,sizeof(plain4)-1 ),
- 0);
- }
- EVP_ENCODE_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
- return res;
- }
- static int test_wolfSSL_EVP_DecodeFinal(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
- /* tests for wolfSSL_EVP_DecodeFinal are included in
- * test_wolfSSL_EVP_DecodeUpdate
- */
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
- return res;
- }
- /* Test function for wolfSSL_EVP_get_cipherbynid.
- */
- #ifdef OPENSSL_EXTRA
- static int test_wolfSSL_EVP_get_cipherbynid(void)
- {
- #ifndef NO_AES
- const WOLFSSL_EVP_CIPHER* c;
- c = wolfSSL_EVP_get_cipherbynid(419);
- #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
- defined(WOLFSSL_AES_128)
- AssertNotNull(c);
- AssertNotNull(strcmp("EVP_AES_128_CBC", c));
- #else
- AssertNull(c);
- #endif
- c = wolfSSL_EVP_get_cipherbynid(423);
- #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
- defined(WOLFSSL_AES_192)
- AssertNotNull(c);
- AssertNotNull(strcmp("EVP_AES_192_CBC", c));
- #else
- AssertNull(c);
- #endif
- c = wolfSSL_EVP_get_cipherbynid(427);
- #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
- defined(WOLFSSL_AES_256)
- AssertNotNull(c);
- AssertNotNull(strcmp("EVP_AES_256_CBC", c));
- #else
- AssertNull(c);
- #endif
- c = wolfSSL_EVP_get_cipherbynid(904);
- #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
- AssertNotNull(c);
- AssertNotNull(strcmp("EVP_AES_128_CTR", c));
- #else
- AssertNull(c);
- #endif
- c = wolfSSL_EVP_get_cipherbynid(905);
- #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
- AssertNotNull(c);
- AssertNotNull(strcmp("EVP_AES_192_CTR", c));
- #else
- AssertNull(c);
- #endif
- c = wolfSSL_EVP_get_cipherbynid(906);
- #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
- AssertNotNull(c);
- AssertNotNull(strcmp("EVP_AES_256_CTR", c));
- #else
- AssertNull(c);
- #endif
- c = wolfSSL_EVP_get_cipherbynid(418);
- #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
- AssertNotNull(c);
- AssertNotNull(strcmp("EVP_AES_128_ECB", c));
- #else
- AssertNull(c);
- #endif
- c = wolfSSL_EVP_get_cipherbynid(422);
- #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
- AssertNotNull(c);
- AssertNotNull(strcmp("EVP_AES_192_ECB", c));
- #else
- AssertNull(c);
- #endif
- c = wolfSSL_EVP_get_cipherbynid(426);
- #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
- AssertNotNull(c);
- AssertNotNull(strcmp("EVP_AES_256_ECB", c));
- #else
- AssertNull(c);
- #endif
- #endif /* !NO_AES */
- #ifndef NO_DES3
- AssertNotNull(strcmp("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
- #ifdef WOLFSSL_DES_ECB
- AssertNotNull(strcmp("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
- #endif
- AssertNotNull(strcmp("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
- #ifdef WOLFSSL_DES_ECB
- AssertNotNull(strcmp("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
- #endif
- #endif /* !NO_DES3 */
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- AssertNotNull(strcmp("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
- #endif
- /* test for nid is out of range */
- AssertNull(wolfSSL_EVP_get_cipherbynid(1));
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_CIPHER_CTX(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- const EVP_CIPHER *init = EVP_aes_128_cbc();
- const EVP_CIPHER *test;
- byte key[AES_BLOCK_SIZE] = {0};
- byte iv[AES_BLOCK_SIZE] = {0};
- AssertNotNull(ctx);
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- test = EVP_CIPHER_CTX_cipher(ctx);
- AssertTrue(init == test);
- AssertIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
- AssertIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
- EVP_CIPHER_CTX_free(ctx);
- /* test EVP_CIPHER_CTX_cleanup with NULL */
- AssertIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
- return res;
- }
- #endif /* OPENSSL_EXTRA */
- /*----------------------------------------------------------------------------*
- | IO
- *----------------------------------------------------------------------------*/
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_RSA) && !defined(SINGLE_THREADED) && \
- !defined(NO_WOLFSSL_SERVER) && !defined(NO_WOLFSSL_CLIENT)
- #define HAVE_IO_TESTS_DEPENDENCIES
- #endif
- /* helper functions */
- #ifdef HAVE_IO_TESTS_DEPENDENCIES
- #ifdef WOLFSSL_SESSION_EXPORT
- #ifdef WOLFSSL_DTLS
- /* set up function for sending session information */
- static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
- {
- WOLFSSL_CTX* ctx = NULL;
- WOLFSSL* ssl = NULL;
- AssertNotNull(inSsl);
- AssertNotNull(buf);
- AssertIntNE(0, sz);
- /* Set ctx to DTLS 1.2 */
- ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
- AssertNotNull(ctx);
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- (void)userCtx;
- return 0;
- }
- #endif
- /* returns negative value on fail and positive (including 0) on success */
- static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
- {
- int ret, err, loop_count, count, timeout = 10;
- char msg[] = "I hear you fa shizzle!";
- char input[1024];
- loop_count = ((func_args*)args)->argc;
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_accept(ssl);
- err = wolfSSL_get_error(ssl, 0);
- if (err == WOLFSSL_ERROR_WANT_READ ||
- err == WOLFSSL_ERROR_WANT_WRITE) {
- int select_ret;
- err = WC_PENDING_E;
- select_ret = tcp_select(*sockfd, timeout);
- if (select_ret == TEST_TIMEOUT) {
- return WOLFSSL_FATAL_ERROR;
- }
- }
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- return ret;
- }
- for (count = 0; count < loop_count; count++) {
- int select_ret;
- select_ret = tcp_select(*sockfd, timeout);
- if (select_ret == TEST_TIMEOUT) {
- ret = WOLFSSL_FATAL_ERROR;
- break;
- }
- do {
- ret = wolfSSL_read(ssl, input, sizeof(input)-1);
- if (ret > 0) {
- input[ret] = '\0';
- fprintf(stderr, "Client message: %s\n", input);
- }
- } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
- do {
- if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
- return WOLFSSL_FATAL_ERROR;
- }
- err = wolfSSL_get_error(ssl, ret);
- } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
- }
- return ret;
- }
- #endif /* WOLFSSL_SESSION_EXPORT */
- /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
- #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
- defined(HAVE_AES_CBC)
- typedef struct openssl_key_ctx {
- byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
- byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
- byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
- byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
- } openssl_key_ctx;
- static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
- static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
- static WC_INLINE int OpenSSLTicketInit(void)
- {
- int ret = wc_InitRng(&myOpenSSLKey_rng);
- if (ret != 0) return ret;
- ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
- sizeof(myOpenSSLKey_ctx.name));
- if (ret != 0) return ret;
- ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
- sizeof(myOpenSSLKey_ctx.key));
- if (ret != 0) return ret;
- ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
- sizeof(myOpenSSLKey_ctx.hmacKey));
- if (ret != 0) return ret;
- ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
- sizeof(myOpenSSLKey_ctx.iv));
- if (ret != 0) return ret;
- return 0;
- }
- static WC_INLINE int myTicketEncCbOpenSSL(WOLFSSL* ssl,
- byte name[WOLFSSL_TICKET_NAME_SZ],
- byte iv[WOLFSSL_TICKET_IV_SZ],
- WOLFSSL_EVP_CIPHER_CTX *ectx,
- WOLFSSL_HMAC_CTX *hctx, int enc) {
- (void)ssl;
- if (enc) {
- XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
- XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
- }
- else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
- sizeof(myOpenSSLKey_ctx.name)) != 0 ||
- XMEMCMP(iv, myOpenSSLKey_ctx.iv,
- sizeof(myOpenSSLKey_ctx.iv)) != 0) {
- return 0;
- }
- HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
- if (enc)
- EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
- else
- EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
- return 1;
- }
- static WC_INLINE void OpenSSLTicketCleanup(void)
- {
- wc_FreeRng(&myOpenSSLKey_rng);
- }
- #endif
- #ifdef WOLFSSL_HAVE_TLS_UNIQUE
- #ifdef WC_SHA512_DIGEST_SIZE
- #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
- #else
- #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
- #endif
- byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
- byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
- byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
- byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
- #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
- static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
- {
- SOCKET_T sockfd = 0;
- SOCKET_T clientfd = 0;
- word16 port;
- callback_functions* cbf;
- WOLFSSL_CTX* ctx = 0;
- WOLFSSL* ssl = 0;
- func_args* opts = (func_args*)args;
- char msg[] = "I hear you fa shizzle!";
- char input[1024];
- int idx;
- int ret, err = 0;
- int sharedCtx = 0;
- int doUdp = 0;
- SOCKADDR_IN_T cliAddr;
- socklen_t cliLen;
- #ifdef WOLFSSL_HAVE_TLS_UNIQUE
- size_t msg_len = 0;
- #endif
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- opts->return_code = TEST_FAIL;
- cbf = opts->callbacks;
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (cbf != NULL && cbf->ctx) {
- ctx = cbf->ctx;
- sharedCtx = 1;
- }
- else
- #endif
- {
- WOLFSSL_METHOD* method = NULL;
- if (cbf != NULL && cbf->method != NULL) {
- method = cbf->method();
- }
- else {
- method = wolfSSLv23_server_method();
- }
- ctx = wolfSSL_CTX_new(method);
- }
- if (ctx == NULL) {
- goto done;
- }
- if (cbf == NULL || !cbf->ticNoInit) {
- #if defined(HAVE_SESSION_TICKET) && \
- ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
- #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
- OpenSSLTicketInit();
- wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
- #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
- TicketInit();
- wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
- #endif
- #endif
- }
- #if defined(USE_WINDOWS_API)
- port = opts->signal->port;
- #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
- !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
- /* Let tcp_listen assign port */
- port = 0;
- #else
- /* Use default port */
- port = wolfSSLPort;
- #endif
- if (cbf != NULL)
- doUdp = cbf->doUdp;
- /* do it here to detect failure */
- tcp_accept(
- &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
- if (doUdp) {
- cliLen = sizeof(cliAddr);
- idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
- (struct sockaddr*)&cliAddr, &cliLen);
- AssertIntGT(idx, 0);
- }
- else {
- CloseSocket(sockfd);
- }
- wolfSSL_CTX_set_verify(ctx,
- WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- #endif
- if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
- != WOLFSSL_SUCCESS) {
- /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
- goto done;
- }
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #else
- if (wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #endif
- /*err_sys("can't load server cert chain file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #else
- if (wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #endif
- /*err_sys("can't load server key file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- /* call ctx setup callback */
- if (cbf != NULL && cbf->ctx_ready != NULL) {
- cbf->ctx_ready(ctx);
- }
- ssl = wolfSSL_new(ctx);
- if (ssl == NULL) {
- goto done;
- }
- if (doUdp) {
- err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
- if (err != WOLFSSL_SUCCESS)
- goto done;
- }
- #ifdef WOLFSSL_SESSION_EXPORT
- /* only add in more complex nonblocking case with session export tests */
- if (args && opts->argc > 0) {
- /* set as nonblock and time out for waiting on read/write */
- tcp_set_nonblocking(&clientfd);
- wolfSSL_dtls_set_using_nonblock(ssl, 1);
- }
- #endif
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #else
- if (wolfSSL_use_certificate_file(ssl, svrCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #endif
- /*err_sys("can't load server cert chain file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #else
- if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #endif
- /*err_sys("can't load server key file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
- /*err_sys("SSL_set_fd failed");*/
- goto done;
- }
- #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
- wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
- #elif !defined(NO_DH)
- SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
- #endif
- /* call ssl setup callback */
- if (cbf != NULL && cbf->ssl_ready != NULL) {
- cbf->ssl_ready(ssl);
- }
- #ifdef WOLFSSL_SESSION_EXPORT
- /* only add in more complex nonblocking case with session export tests */
- if (opts->argc > 0) {
- ret = nonblocking_accept_read(args, ssl, &clientfd);
- if (ret >= 0) {
- opts->return_code = TEST_SUCCESS;
- }
- #ifdef WOLFSSL_TIRTOS
- Task_yield();
- #endif
- goto done;
- }
- #endif
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_accept(ssl);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- /*err_sys("SSL_accept failed");*/
- goto done;
- }
- #ifdef WOLFSSL_HAVE_TLS_UNIQUE
- XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
- msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
- AssertIntGE(msg_len, 0);
- XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
- msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
- AssertIntGE(msg_len, 0);
- #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
- idx = wolfSSL_read(ssl, input, sizeof(input)-1);
- if (idx > 0) {
- input[idx] = '\0';
- fprintf(stderr, "Client message: %s\n", input);
- }
- if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
- /*err_sys("SSL_write failed");*/
- #ifdef WOLFSSL_TIRTOS
- return;
- #else
- return 0;
- #endif
- }
- if (cbf != NULL && cbf->on_result != NULL)
- cbf->on_result(ssl);
- #ifdef WOLFSSL_TIRTOS
- Task_yield();
- #endif
- opts->return_code = TEST_SUCCESS;
- done:
- if (cbf != NULL)
- cbf->last_err = err;
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl);
- if (!sharedCtx)
- wolfSSL_CTX_free(ctx);
- CloseSocket(clientfd);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
- && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- if (cbf == NULL || !cbf->ticNoInit) {
- #if defined(HAVE_SESSION_TICKET) && \
- ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
- #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
- OpenSSLTicketCleanup();
- #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
- TicketCleanup();
- #endif
- #endif
- }
- #ifndef WOLFSSL_TIRTOS
- return 0;
- #endif
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
- static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
- {
- SOCKET_T sockfd = 0;
- SOCKET_T clientfd = 0;
- word16 port;
- callback_functions* cbf;
- WOLFSSL_CTX* ctx = 0;
- WOLFSSL* ssl = 0;
- char msg[] = "I hear you fa shizzle!";
- char input[1024];
- int idx;
- int ret, err = 0;
- int sharedCtx = 0;
- int loop_count = ((func_args*)args)->argc;
- int count = 0;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- ((func_args*)args)->return_code = TEST_FAIL;
- cbf = ((func_args*)args)->callbacks;
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (cbf != NULL && cbf->ctx) {
- ctx = cbf->ctx;
- sharedCtx = 1;
- }
- else
- #endif
- {
- WOLFSSL_METHOD* method = NULL;
- if (cbf != NULL && cbf->method != NULL) {
- method = cbf->method();
- }
- else {
- method = wolfSSLv23_server_method();
- }
- ctx = wolfSSL_CTX_new(method);
- }
- #if defined(USE_WINDOWS_API)
- port = ((func_args*)args)->signal->port;
- #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
- !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
- /* Let tcp_listen assign port */
- port = 0;
- #else
- /* Use default port */
- port = wolfSSLPort;
- #endif
- wolfSSL_CTX_set_verify(ctx,
- WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- #endif
- if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
- != WOLFSSL_SUCCESS) {
- /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
- goto done;
- }
- if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load server cert chain file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load server key file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- /* call ctx setup callback */
- if (cbf != NULL && cbf->ctx_ready != NULL) {
- cbf->ctx_ready(ctx);
- }
- while (count != loop_count) {
- ssl = wolfSSL_new(ctx);
- if (ssl == NULL) {
- goto done;
- }
- if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load server cert chain file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load server key file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
- wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
- #elif !defined(NO_DH)
- SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
- #endif
- /* call ssl setup callback */
- if (cbf != NULL && cbf->ssl_ready != NULL) {
- cbf->ssl_ready(ssl);
- }
- /* do it here to detect failure */
- tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
- CloseSocket(sockfd);
- if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
- /*err_sys("SSL_set_fd failed");*/
- goto done;
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_accept(ssl);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- /*err_sys("SSL_accept failed");*/
- goto done;
- }
- idx = wolfSSL_read(ssl, input, sizeof(input)-1);
- if (idx > 0) {
- input[idx] = '\0';
- fprintf(stderr, "Client message: %s\n", input);
- }
- if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
- /*err_sys("SSL_write failed");*/
- #ifdef WOLFSSL_TIRTOS
- return;
- #else
- return 0;
- #endif
- }
- /* free ssl for this connection */
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl); ssl = NULL;
- CloseSocket(clientfd);
- count++;
- }
- #ifdef WOLFSSL_TIRTOS
- Task_yield();
- #endif
- ((func_args*)args)->return_code = TEST_SUCCESS;
- done:
- if (ssl != NULL) {
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl);
- }
- if (!sharedCtx)
- wolfSSL_CTX_free(ctx);
- CloseSocket(clientfd);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
- && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- #ifndef WOLFSSL_TIRTOS
- return 0;
- #endif
- }
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
- typedef int (*cbType)(WOLFSSL_CTX *ctx, WOLFSSL *ssl);
- static int test_client_nofail(void* args, cbType cb)
- {
- #if !defined(NO_WOLFSSL_CLIENT)
- SOCKET_T sockfd = 0;
- callback_functions* cbf;
- WOLFSSL_CTX* ctx = 0;
- WOLFSSL* ssl = 0;
- WOLFSSL_CIPHER* cipher;
- char msg[64] = "hello wolfssl!";
- char reply[1024];
- int input;
- int msgSz = (int)XSTRLEN(msg);
- int ret, err = 0;
- int cipherSuite;
- int sharedCtx = 0;
- int doUdp = 0;
- const char* cipherName1, *cipherName2;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- ((func_args*)args)->return_code = TEST_FAIL;
- cbf = ((func_args*)args)->callbacks;
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (cbf != NULL && cbf->ctx) {
- ctx = cbf->ctx;
- sharedCtx = cbf->isSharedCtx;
- }
- else
- #endif
- {
- WOLFSSL_METHOD* method = NULL;
- if (cbf != NULL && cbf->method != NULL) {
- method = cbf->method();
- }
- else {
- method = wolfSSLv23_client_method();
- }
- ctx = wolfSSL_CTX_new(method);
- }
- if (cbf != NULL)
- doUdp = cbf->doUdp;
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- #endif
- /* Do connect here so server detects failures */
- tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
- doUdp, 0, NULL);
- /* Connect the socket so that we don't have to set the peer later on */
- if (doUdp)
- udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
- if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
- {
- /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
- goto done;
- }
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #else
- if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #endif
- /*err_sys("can't load client cert file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #else
- if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #endif
- /*err_sys("can't load client key file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- /* call ctx setup callback */
- if (cbf != NULL && cbf->ctx_ready != NULL) {
- cbf->ctx_ready(ctx);
- }
- ssl = wolfSSL_new(ctx);
- if (ssl == NULL) {
- goto done;
- }
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #else
- if (wolfSSL_use_certificate_file(ssl, cliCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #endif
- /*err_sys("can't load client cert file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #else
- if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- #endif
- /*err_sys("can't load client key file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- if (!doUdp) {
- if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
- /*err_sys("SSL_set_fd failed");*/
- goto done;
- }
- }
- else {
- #ifdef WOLFSSL_DTLS
- if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
- /*err_sys("SSL_set_fd failed");*/
- goto done;
- }
- #else
- goto done;
- #endif
- }
- /* call ssl setup callback */
- if (cbf != NULL && cbf->ssl_ready != NULL) {
- cbf->ssl_ready(ssl);
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_connect(ssl);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- /*err_sys("SSL_connect failed");*/
- goto done;
- }
- /* test the various get cipher methods */
- /* Internal cipher suite names */
- cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
- cipherName1 = wolfSSL_get_cipher_name(ssl);
- cipherName2 = wolfSSL_get_cipher_name_from_suite(
- (cipherSuite >> 8), cipherSuite & 0xFF);
- AssertStrEQ(cipherName1, cipherName2);
- /* IANA Cipher Suites Names */
- /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
- then it's the internal cipher suite name */
- cipher = wolfSSL_get_current_cipher(ssl);
- cipherName1 = wolfSSL_CIPHER_get_name(cipher);
- cipherName2 = wolfSSL_get_cipher(ssl);
- AssertStrEQ(cipherName1, cipherName2);
- #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
- !defined(WOLFSSL_QT)
- cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
- (cipherSuite >> 8), cipherSuite & 0xFF);
- AssertStrEQ(cipherName1, cipherName2);
- #endif
- if (cb != NULL)
- (cb)(ctx, ssl);
- if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
- /*err_sys("SSL_write failed");*/
- goto done;
- }
- input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
- if (input > 0) {
- reply[input] = '\0';
- fprintf(stderr, "Server response: %s\n", reply);
- }
- if (cbf != NULL && cbf->on_result != NULL)
- cbf->on_result(ssl);
- ((func_args*)args)->return_code = TEST_SUCCESS;
- done:
- if (cbf != NULL)
- cbf->last_err = err;
- wolfSSL_free(ssl);
- if (!sharedCtx)
- wolfSSL_CTX_free(ctx);
- CloseSocket(sockfd);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
- && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- #else
- (void)args;
- (void)cb;
- #endif /* !NO_WOLFSSL_CLIENT */
- return 0;
- }
- void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
- callback_functions* server_cb)
- {
- func_args client_args;
- func_args server_args;
- tcp_ready ready;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- server_args.callbacks = server_cb;
- client_args.signal = &ready;
- client_args.callbacks = client_cb;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- client_cb->return_code = client_args.return_code;
- server_cb->return_code = server_args.return_code;
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
- !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT)
- static void test_client_reuse_WOLFSSLobj(void* args, void *cb, void* server_args)
- {
- SOCKET_T sockfd = 0;
- callback_functions* cbf;
- WOLFSSL_CTX* ctx = 0;
- WOLFSSL* ssl = 0;
- WOLFSSL_SESSION* session = NULL;
- char msg[64] = "hello wolfssl!";
- char reply[1024];
- int input;
- int msgSz = (int)XSTRLEN(msg);
- int ret, err = 0;
- int sharedCtx = 0;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- ((func_args*)args)->return_code = TEST_FAIL;
- cbf = ((func_args*)args)->callbacks;
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
- if (cbf != NULL && cbf->ctx) {
- ctx = cbf->ctx;
- sharedCtx = 1;
- }
- else
- #endif
- {
- WOLFSSL_METHOD* method = NULL;
- if (cbf != NULL && cbf->method != NULL) {
- method = cbf->method();
- }
- else {
- method = wolfSSLv23_client_method();
- }
- ctx = wolfSSL_CTX_new(method);
- }
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- #endif
- /* Do connect here so server detects failures */
- tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
- 0, 0, NULL);
- if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
- {
- /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
- goto done;
- }
- if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load client cert file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load client key file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- /* call ctx setup callback */
- if (cbf != NULL && cbf->ctx_ready != NULL) {
- cbf->ctx_ready(ctx);
- }
- ssl = wolfSSL_new(ctx);
- if (ssl == NULL) {
- goto done;
- }
- /* keep handshakre resources for re-using WOLFSSL obj */
- wolfSSL_KeepArrays(ssl);
- if (wolfSSL_KeepHandshakeResources(ssl)) {
- /* err_sys("SSL_KeepHandshakeResources failed"); */
- goto done;
- }
- if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load client cert file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load client key file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
- /*err_sys("SSL_set_fd failed");*/
- goto done;
- }
- /* call ssl setup callback */
- if (cbf != NULL && cbf->ssl_ready != NULL) {
- cbf->ssl_ready(ssl);
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_connect(ssl);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- /*err_sys("SSL_connect failed");*/
- goto done;
- }
- /* Build first session */
- if (cb != NULL)
- ((cbType)cb)(ctx, ssl);
- if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
- /*err_sys("SSL_write failed");*/
- goto done;
- }
- input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
- if (input > 0) {
- reply[input] = '\0';
- fprintf(stderr, "Server response: %s\n", reply);
- }
- /* Session Resumption by re-using WOLFSSL object */
- wolfSSL_set_quiet_shutdown(ssl, 1);
- if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
- /* err_sys ("SSL shutdown failed"); */
- goto done;
- }
- session = wolfSSL_get1_session(ssl);
- if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
- /* err_sys ("SSL_clear failed"); */
- goto done;
- }
- wolfSSL_set_session(ssl, session);
- wolfSSL_SESSION_free(session);
- session = NULL;
- /* close socket once */
- CloseSocket(sockfd);
- sockfd = 0;
- /* wait until server ready */
- wait_tcp_ready((func_args*)server_args);
- fprintf(stderr, "session resumption\n");
- /* Do re-connect */
- tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
- 0, 0, NULL);
- if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
- /*err_sys("SSL_set_fd failed");*/
- goto done;
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_connect(ssl);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- /*err_sys("SSL_connect failed");*/
- goto done;
- }
- /* Build first session */
- if (cb != NULL)
- ((cbType)cb)(ctx, ssl);
- if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
- /*err_sys("SSL_write failed");*/
- goto done;
- }
- input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
- if (input > 0) {
- reply[input] = '\0';
- fprintf(stderr, "Server response: %s\n", reply);
- }
- ((func_args*)args)->return_code = TEST_SUCCESS;
- done:
- wolfSSL_free(ssl);
- if (!sharedCtx)
- wolfSSL_CTX_free(ctx);
- CloseSocket(sockfd);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- return;
- }
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
- !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
- static int test_client_verifyDepth(void* args)
- {
- #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
- SOCKET_T sockfd = 0;
- callback_functions* cbf;
- WOLFSSL_CTX* ctx = 0;
- WOLFSSL* ssl = 0;
- char msg[64] = "hello wolfssl!";
- char reply[1024];
- int input;
- int msgSz = (int)XSTRLEN(msg);
- int ret, err = 0;
- int verify_depth = ((func_args*)args)->argc;
- ((func_args*)args)->return_code = TEST_FAIL;
- cbf = ((func_args*)args)->callbacks;
- {
- WOLFSSL_METHOD* method = NULL;
- if (cbf != NULL && cbf->method != NULL) {
- method = cbf->method();
- }
- else {
- method = wolfSSLv23_client_method();
- }
- ctx = wolfSSL_CTX_new(method);
- }
- /* Do connect here so server detects failures */
- tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
- 0, 0, NULL);
- if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0)
- != WOLFSSL_SUCCESS)
- {
- /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
- goto done;
- }
- if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load client cert file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- /*err_sys("can't load client key file, "
- "Please run from wolfSSL home dir");*/
- goto done;
- }
- SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
- /* set verify depth */
- if (verify_depth == 0) {
- myVerifyAction = VERIFY_OVERRIDE_ERROR;
- SSL_CTX_set_verify_depth(ctx, verify_depth);
- }
- else if (verify_depth == -1) {
- myVerifyAction = VERIFY_USE_PREVERFIY;
- SSL_CTX_set_verify_depth(ctx, 0);
- }
- else if (verify_depth > 0) {
- myVerifyAction = VERIFY_USE_PREVERFIY;
- SSL_CTX_set_verify_depth(ctx, verify_depth);
- }
- ssl = wolfSSL_new(ctx);
- if (ssl == NULL) {
- goto done;
- }
- if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
- /*err_sys("SSL_set_fd failed");*/
- goto done;
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_connect(ssl);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- goto done;
- }
- if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
- goto done;
- }
- input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
- if (input > 0) {
- reply[input] = '\0';
- fprintf(stderr, "Server response: %s\n", reply);
- }
- ((func_args*)args)->return_code = TEST_SUCCESS;
- done:
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(sockfd);
- #else
- (void)args;
- #endif /* defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT) */
- return 0;
- }
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
- defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
- defined(HAVE_ALPN) && defined(HAVE_SNI) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
- #define HAVE_ALPN_PROTOS_SUPPORT
- #endif
- /* Generic TLS client / server with callbacks for API unit tests
- * Used by SNI / ALPN / crypto callback helper functions */
- #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
- defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
- #define ENABLE_TLS_CALLBACK_TEST
- #endif
- #if defined(ENABLE_TLS_CALLBACK_TEST) || \
- (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
- /* TLS server for API unit testing - generic */
- static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
- {
- callback_functions* callbacks = ((func_args*)args)->callbacks;
- WOLFSSL_CTX* ctx = NULL;
- WOLFSSL* ssl = NULL;
- SOCKET_T sfd = 0;
- SOCKET_T cfd = 0;
- word16 port;
- char msg[] = "I hear you fa shizzle!";
- int len = (int) XSTRLEN(msg);
- char input[1024];
- int idx;
- int ret, err = 0;
- ((func_args*)args)->return_code = TEST_FAIL;
- #ifdef WOLFSSL_STATIC_MEMORY
- if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
- callbacks->memSz > 0) {
- ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
- callbacks->mem, callbacks->memSz, 0, 1);
- if (ret != WOLFSSL_SUCCESS) {
- fprintf(stderr, "CTX static new failed %d\n", ret);
- return 0;
- }
- }
- #else
- if (ctx == NULL) {
- ctx = wolfSSL_CTX_new(callbacks->method());
- }
- if (ctx == NULL) {
- fprintf(stderr, "CTX new failed\n");
- return 0;
- }
- #endif
- /* set defaults */
- if (callbacks->caPemFile == NULL)
- callbacks->caPemFile = cliCertFile;
- if (callbacks->certPemFile == NULL)
- callbacks->certPemFile = svrCertFile;
- if (callbacks->keyPemFile == NULL)
- callbacks->keyPemFile = svrKeyFile;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
- #if defined(USE_WINDOWS_API)
- port = ((func_args*)args)->signal->port;
- #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
- !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
- /* Let tcp_listen assign port */
- port = 0;
- #else
- /* Use default port */
- port = wolfSSLPort;
- #endif
- wolfSSL_CTX_set_verify(ctx,
- WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- #endif
- #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
- if (callbacks->method == wolfDTLSv1_2_server_method) {
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_dtls_set_export(ctx, test_export));
- }
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
- WOLFSSL_FILETYPE_PEM));
- if (callbacks->ctx_ready)
- callbacks->ctx_ready(ctx);
- ssl = wolfSSL_new(ctx);
- if (ssl == NULL) {
- fprintf(stderr, "SSL new failed\n");
- wolfSSL_CTX_free(ctx);
- return 0;
- }
- if (wolfSSL_dtls(ssl)) {
- SOCKADDR_IN_T cliAddr;
- socklen_t cliLen;
- cliLen = sizeof(cliAddr);
- tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
- idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
- (struct sockaddr*)&cliAddr, &cliLen);
- AssertIntGT(idx, 0);
- wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
- }
- else {
- tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
- CloseSocket(sfd);
- }
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
- if (callbacks->loadToSSL) {
- wolfSSL_SetDevId(ssl, callbacks->devId);
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
- WOLFSSL_FILETYPE_PEM));
- }
- #ifdef NO_PSK
- #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
- wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
- #elif !defined(NO_DH)
- SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
- #endif
- #endif
- if (callbacks->ssl_ready)
- callbacks->ssl_ready(ssl);
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_accept(ssl);
- err = wolfSSL_get_error(ssl, ret);
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "accept error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- /*err_sys("SSL_accept failed");*/
- }
- else {
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- idx = wolfSSL_read(ssl, input, sizeof(input)-1);
- err = wolfSSL_get_error(ssl, idx);
- } while (err == WC_PENDING_E);
- if (idx > 0) {
- input[idx] = 0;
- fprintf(stderr, "Client message: %s\n", input);
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_write(ssl, msg, len);
- err = wolfSSL_get_error(ssl, ret);
- } while (err == WC_PENDING_E);
- AssertIntEQ(len, ret);
- #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
- defined(WOLFSSL_DTLS)
- if (wolfSSL_dtls(ssl)) {
- byte* import;
- word32 sz;
- wolfSSL_dtls_export(ssl, NULL, &sz);
- import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- AssertNotNull(import);
- idx = wolfSSL_dtls_export(ssl, import, &sz);
- AssertIntGE(idx, 0);
- AssertIntGE(wolfSSL_dtls_import(ssl, import, idx), 0);
- XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- #endif
- #ifdef WOLFSSL_TIRTOS
- Task_yield();
- #endif
- ((func_args*)args)->return_code = TEST_SUCCESS;
- }
- if (callbacks->on_result)
- callbacks->on_result(ssl);
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(cfd);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
- && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- #ifndef WOLFSSL_TIRTOS
- return 0;
- #endif
- }
- /* TLS Client for API unit testing - generic */
- static void run_wolfssl_client(void* args)
- {
- callback_functions* callbacks = ((func_args*)args)->callbacks;
- WOLFSSL_CTX* ctx = NULL;
- WOLFSSL* ssl = NULL;
- SOCKET_T sfd = 0;
- char msg[] = "hello wolfssl server!";
- int len = (int) XSTRLEN(msg);
- char input[1024];
- int ret, err = 0;
- ((func_args*)args)->return_code = TEST_FAIL;
- /* set defaults */
- if (callbacks->caPemFile == NULL)
- callbacks->caPemFile = caCertFile;
- if (callbacks->certPemFile == NULL)
- callbacks->certPemFile = cliCertFile;
- if (callbacks->keyPemFile == NULL)
- callbacks->keyPemFile = cliKeyFile;
- #ifdef WOLFSSL_STATIC_MEMORY
- if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
- callbacks->memSz > 0) {
- ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
- callbacks->mem, callbacks->memSz, 0, 1);
- if (ret != WOLFSSL_SUCCESS) {
- fprintf(stderr, "CTX static new failed %d\n", ret);
- return;
- }
- }
- #else
- if (ctx == NULL) {
- ctx = wolfSSL_CTX_new(callbacks->method());
- }
- if (ctx == NULL) {
- fprintf(stderr, "CTX new failed\n");
- return;
- }
- #endif
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- if (!callbacks->loadToSSL) {
- wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
- }
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0));
- if (!callbacks->loadToSSL) {
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
- WOLFSSL_FILETYPE_PEM));
- }
- if (callbacks->ctx_ready)
- callbacks->ctx_ready(ctx);
- ssl = wolfSSL_new(ctx);
- if (wolfSSL_dtls(ssl)) {
- tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
- 1, 0, ssl);
- }
- else {
- tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
- 0, 0, ssl);
- }
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
- if (callbacks->loadToSSL) {
- wolfSSL_SetDevId(ssl, callbacks->devId);
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
- WOLFSSL_FILETYPE_PEM));
- }
- if (callbacks->ssl_ready)
- callbacks->ssl_ready(ssl);
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_connect(ssl);
- err = wolfSSL_get_error(ssl, ret);
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- /*err_sys("SSL_connect failed");*/
- }
- else {
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_write(ssl, msg, len);
- err = wolfSSL_get_error(ssl, ret);
- } while (err == WC_PENDING_E);
- AssertIntEQ(len, ret);
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_read(ssl, input, sizeof(input)-1);
- err = wolfSSL_get_error(ssl, ret);
- } while (err == WC_PENDING_E);
- if (ret > 0) {
- input[ret] = '\0'; /* null term */
- fprintf(stderr, "Server response: %s\n", input);
- }
- ((func_args*)args)->return_code = TEST_SUCCESS;
- }
- if (callbacks->on_result)
- callbacks->on_result(ssl);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(sfd);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- }
- #endif /* ENABLE_TLS_CALLBACK_TEST */
- static int test_wolfSSL_read_write(void)
- {
- /* The unit testing for read and write shall happen simultaneously, since
- * one can't do anything with one without the other. (Except for a failure
- * test case.) This function will call all the others that will set up,
- * execute, and report their test findings.
- *
- * Set up the success case first. This function will become the template
- * for the other tests. This should eventually be renamed
- *
- * The success case isn't interesting, how can this fail?
- * - Do not give the client context a CA certificate. The connect should
- * fail. Do not need server for this?
- * - Using NULL for the ssl object on server. Do not need client for this.
- * - Using NULL for the ssl object on client. Do not need server for this.
- * - Good ssl objects for client and server. Client write() without server
- * read().
- * - Good ssl objects for client and server. Server write() without client
- * read().
- * - Forgetting the password callback?
- */
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_reuse_WOLFSSLobj(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
- !defined(WOLFSSL_TLS13)
- /* The unit test for session resumption by re-using WOLFSSL object.
- * WOLFSSL object is not cleared after first session. It re-use the obeject
- * for second connection.
- */
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- /* the var is used for loop number */
- server_args.argc = 2;
- start_thread(test_server_loop, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
- return res;
- }
- static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && !defined(NO_WOLFSSL_CLIENT)
- /* This unit test is to check set verify Depth */
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- callback_functions client_cbf;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&client_cbf, 0, sizeof(callback_functions));
- #ifdef WOLFSSL_TLS13
- client_cbf.method = wolfTLSv1_3_client_method;
- #endif /* WOLFSSL_TLS13 */
- client_args.callbacks = &client_cbf;
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- /* the var is used for loop number */
- server_args.argc = 1;
- /* test case 1 verify depth is equal to peer chain */
- {
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- /* the var is used for verify depth */
- client_args.argc = 2;
- test_client_verifyDepth(&client_args);
- join_thread(serverThread);
- AssertIntEQ(client_args.return_code, TEST_SUCCESS);
- AssertIntEQ(server_args.return_code, TEST_SUCCESS);
- }
- /* test case 2
- * verify depth is zero, number of peer's chain is 2.
- * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
- * callback.
- */
- /* the var is used for verify depth 0 and VERIFY_OVERRIDE_ERROR */
- {
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- client_args.argc = 0;
- test_client_verifyDepth(&client_args);
- join_thread(serverThread);
- AssertIntEQ(client_args.return_code, TEST_SUCCESS);
- AssertIntEQ(server_args.return_code, TEST_SUCCESS);
- }
- /* test case 3
- * verify depth is zero, number of peer's chain is 2
- * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
- * therefore, handshake becomes failure.
- */
- /* the var is used for verify depth 0 and VERIFY_USE_PREVERFIY */
- {
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- client_args.argc = -1;
- test_client_verifyDepth(&client_args);
- join_thread(serverThread);
- AssertIntEQ(client_args.return_code, TEST_SUCCESS);
- AssertIntEQ(server_args.return_code, TEST_SUCCESS);
- }
- FreeTcpReady(&ready);
- res = TEST_RES_CHECK(1);
- #else
- (void)test_client_verifyDepth;
- #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
- return res;
- }
- static int test_wolfSSL_CTX_set_cipher_list(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
- && !defined(NO_SHA256)
- WOLFSSL_CTX* ctx;
- WOLFSSL_CTX* ctxClient;
- WOLFSSL* sslClient;
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- callback_functions client_cb;
- callback_functions server_cb;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- XMEMSET(&client_cb, 0, sizeof(callback_functions));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- AssertNotNull((ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method())));
- AssertTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
- AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE-RSA-AES128-SHA256"));
- client_cb.ctx = ctxClient;
- server_cb.ctx = ctx;
- /* we are responsible for free'ing WOLFSSL_CTX */
- server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
- server_args.signal = &ready;
- server_args.callbacks = &server_cb;
- client_args.signal = &ready;
- client_args.callbacks = &client_cb;
- client_args.return_code = TEST_FAIL;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- wolfSSL_CTX_free(client_cb.ctx);
- wolfSSL_CTX_free(server_cb.ctx);
- AssertIntEQ(server_args.return_code, TEST_SUCCESS);
- FreeTcpReady(&ready);
- /* check with cipher string that has '+' */
- AssertNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
- AssertTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
- AssertNotNull((sslClient = wolfSSL_new(ctxClient)));
- /* check for the existance of an ECDHE ECDSA cipher suite */
- {
- int i = 0;
- int found = 0;
- const char* suite;
- WOLF_STACK_OF(WOLFSSL_CIPHER)* sk;
- WOLFSSL_CIPHER* current;
- AssertNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
- do {
- current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
- if (current) {
- suite = wolfSSL_CIPHER_get_name(current);
- if (suite && XSTRSTR(suite, "ECDSA")) {
- found = 1;
- break;
- }
- }
- } while (current);
- AssertIntEQ(found, 1);
- }
- wolfSSL_free(sslClient);
- wolfSSL_CTX_free(ctxClient);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_client_get_finished(void* args, cbType cb)
- {
- #if defined(WOLFSSL_HAVE_TLS_UNIQUE) && !defined(NO_WOLFSSL_CLIENT)
- SOCKET_T sockfd = 0;
- callback_functions* cbf;
- WOLFSSL_CTX* ctx = 0;
- WOLFSSL* ssl = 0;
- char msg[64] = "hello wolfssl!";
- char reply[1024];
- int msgSz = (int)XSTRLEN(msg);
- int ret, err = 0;
- WOLFSSL_METHOD* method = NULL;
- size_t msg_len = 0;
- (void) args;
- (void) cb;
- ((func_args*)args)->return_code = TEST_FAIL;
- cbf = ((func_args*)args)->callbacks;
- if (cbf != NULL && cbf->method != NULL) {
- method = cbf->method();
- }
- else {
- method = wolfSSLv23_client_method();
- }
- ctx = wolfSSL_CTX_new(method);
- /* Do connect here so server detects failures */
- tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
- 0, 0, NULL);
- if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
- {
- /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
- goto done;
- }
- if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- goto done;
- }
- if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
- goto done;
- }
- /* call ctx setup callback */
- if (cbf != NULL && cbf->ctx_ready != NULL) {
- cbf->ctx_ready(ctx);
- }
- ssl = wolfSSL_new(ctx);
- if (ssl == NULL) {
- goto done;
- }
- if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
- goto done;
- }
- /* call ssl setup callback */
- if (cbf != NULL && cbf->ssl_ready != NULL) {
- cbf->ssl_ready(ssl);
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_connect(ssl);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- goto done;
- }
- /* get_finished test */
- /* 1. get own sent message */
- XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
- msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
- AssertIntGE(msg_len, 0);
- /* 2. get peer message */
- XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
- msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
- AssertIntGE(msg_len, 0);
- if (cb != NULL)
- (cb)(ctx, ssl);
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_write(ssl, msg, msgSz);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- if (ret != msgSz) {
- /*err_sys("SSL_write failed");*/
- goto done;
- }
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_read(ssl, reply, sizeof(reply)-1);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- if (ret > 0) {
- reply[ret] = '\0';
- fprintf(stderr, "Server response: %s\n", reply);
- }
- ((func_args*)args)->return_code = TEST_SUCCESS;
- done:
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(sockfd);
- #else
- (void)args;
- (void)cb;
- #endif /* WOLFSSL_HAVE_TLS_UNIQUE && !NO_WOLFSSL_CLIENT */
- return 0;
- }
- static int test_wolfSSL_get_finished(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_HAVE_TLS_UNIQUE)
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_get_finished(&client_args, NULL);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- /* test received msg vs sent msg */
- AssertIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
- AssertIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
- FreeTcpReady(&ready);
- res = TEST_RES_CHECK(1);
- #else
- (void)test_client_get_finished;
- #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
- return res;
- }
- #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
- !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
- !defined(NO_SESSION_CACHE)
- /* Sessions to restore/store */
- static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
- static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
- static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
- static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
- {
- /* Don't store sessions. Lookup is still enabled. */
- AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
- WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
- #ifdef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
- WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
- WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
- #endif
- /* Require both peers to provide certs */
- wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
- }
- static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
- {
- WOLFSSL_SESSION** sess;
- if (wolfSSL_is_server(ssl))
- sess = &test_wolfSSL_CTX_add_session_server_sess;
- else
- sess = &test_wolfSSL_CTX_add_session_client_sess;
- if (*sess == NULL) {
- #ifdef NO_SESSION_CACHE_REF
- AssertNotNull(*sess = wolfSSL_get1_session(ssl));
- #else
- /* Test for backwards compatibility */
- if (wolfSSL_is_server(ssl)) {
- AssertNotNull(*sess = wolfSSL_get1_session(ssl));
- }
- else {
- AssertNotNull(*sess = wolfSSL_get_session(ssl));
- }
- #endif
- /* Now save the session in the internal store to make it available
- * for lookup. For TLS 1.3, we can't save the session without
- * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
- * session from cache. */
- if (wolfSSL_is_server(ssl)
- #ifndef WOLFSSL_TICKET_HAVE_ID
- && wolfSSL_version(ssl) != TLS1_3_VERSION
- #endif
- )
- AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
- *sess), WOLFSSL_SUCCESS);
- }
- else {
- /* If we have a session retrieved then remaining connections should be
- * resuming on that session */
- AssertIntEQ(wolfSSL_session_reused(ssl), 1);
- }
- /* Save CTX to be able to decrypt tickets */
- if (wolfSSL_is_server(ssl) &&
- test_wolfSSL_CTX_add_session_server_ctx == NULL) {
- AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
- = wolfSSL_get_SSL_CTX(ssl));
- AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
- WOLFSSL_SUCCESS);
- }
- #ifdef SESSION_CERTS
- #ifndef WOLFSSL_TICKET_HAVE_ID
- if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
- wolfSSL_session_reused(ssl))
- #endif
- {
- /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
- * for all connections. TLS 1.3 only has tickets so if we don't
- * include the session id in the ticket then the certificates
- * will not be available on resumption. */
- WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
- AssertNotNull(peer);
- wolfSSL_X509_free(peer);
- AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
- #ifdef OPENSSL_EXTRA
- AssertNotNull(SSL_SESSION_get0_peer(*sess));
- #endif
- }
- #endif /* SESSION_CERTS */
- }
- static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
- {
- /* Set the session to reuse for the client */
- AssertIntEQ(wolfSSL_set_session(ssl,
- test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
- }
- #endif
- static int test_wolfSSL_CTX_add_session(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
- !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
- !defined(NO_SESSION_CACHE)
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- callback_functions client_cb;
- callback_functions server_cb;
- method_provider methods[][2] = {
- #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
- !defined(NO_DES3))
- /* Without AES there are almost no ciphersuites available. This leads
- * to no ciphersuites being available and an error. */
- { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
- #endif
- #ifndef WOLFSSL_NO_TLS12
- { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
- #endif
- /* Needs the default ticket callback since it is tied to the
- * connection context and this makes it easy to carry over the ticket
- * crypto context between connections */
- #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
- defined(HAVE_SESSION_TICKET)
- { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
- #endif
- };
- const size_t methodsLen = sizeof(methods)/sizeof(*methods);
- size_t i, j;
- for (i = 0; i < methodsLen; i++) {
- /* First run creates a connection while the second+ run will attempt
- * to resume the connection. The trick is that the internal cache
- * is turned off. wolfSSL_CTX_add_session should put the session in
- * the cache anyway. */
- test_wolfSSL_CTX_add_session_client_sess = NULL;
- test_wolfSSL_CTX_add_session_server_sess = NULL;
- test_wolfSSL_CTX_add_session_server_ctx = NULL;
- for (j = 0; j < 5; j++) {
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&client_cb, 0, sizeof(callback_functions));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- client_cb.method = methods[i][0];
- server_cb.method = methods[i][1];
- server_args.signal = &ready;
- server_args.callbacks = &server_cb;
- client_args.signal = &ready;
- client_args.callbacks = &client_cb;
- if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
- server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
- server_cb.isSharedCtx = 1;
- }
- server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
- client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
- if (j != 0)
- client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
- server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
- client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
- server_cb.ticNoInit = 1; /* Use default builtin */
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- }
- wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
- wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
- wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
- /* canned export of a session using older version 3 */
- static unsigned char version_3[] = {
- 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
- 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
- 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
- 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
- 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
- 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
- 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
- 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
- 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
- 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
- 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
- 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
- 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
- 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
- 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
- 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
- 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
- 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
- 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
- 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
- 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
- 0x2E, 0x31, 0xED, 0x4F
- };
- #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
- static int test_wolfSSL_dtls_export(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- callback_functions server_cbf;
- callback_functions client_cbf;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- /* set using dtls */
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&server_cbf, 0, sizeof(callback_functions));
- XMEMSET(&client_cbf, 0, sizeof(callback_functions));
- server_cbf.method = wolfDTLSv1_2_server_method;
- client_cbf.method = wolfDTLSv1_2_client_method;
- server_args.callbacks = &server_cbf;
- client_args.callbacks = &client_cbf;
- server_args.signal = &ready;
- client_args.signal = &ready;
- start_thread(run_wolfssl_server, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- run_wolfssl_client(&client_args);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- {
- SOCKET_T sockfd = 0;
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- char msg[64] = "hello wolfssl!";
- char reply[1024];
- int msgSz = (int)XSTRLEN(msg);
- byte *session, *window;
- unsigned int sessionSz, windowSz;
- #ifndef TEST_IPV6
- struct sockaddr_in peerAddr;
- #else
- struct sockaddr_in6 peerAddr;
- #endif /* TEST_IPV6 */
- int i;
- /* Set ctx to DTLS 1.2 */
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
- AssertNotNull(ssl = wolfSSL_new(ctx));
- /* test importing version 3 */
- AssertIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
- /* test importing bad length and bad version */
- version_3[2] += 1;
- AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
- version_3[2] -= 1; version_3[1] = 0XA0;
- AssertIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- /* check storing client state after connection and storing window only */
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- /* set using dtls */
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&server_cbf, 0, sizeof(callback_functions));
- server_cbf.method = wolfDTLSv1_2_server_method;
- server_cbf.doUdp = 1;
- server_args.callbacks = &server_cbf;
- server_args.argc = 3; /* set loop_count to 3 */
- server_args.signal = &ready;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- /* create and connect with client */
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
- /* store server information connected too */
- XMEMSET(&peerAddr, 0, sizeof(peerAddr));
- #ifndef TEST_IPV6
- peerAddr.sin_family = AF_INET;
- AssertIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
- peerAddr.sin_port = XHTONS(server_args.signal->port);
- #else
- peerAddr.sin6_family = AF_INET6;
- AssertIntEQ(
- XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
- peerAddr.sin6_port = XHTONS(server_args.signal->port);
- #endif
- AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
- session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- AssertIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
- AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
- AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
- AssertIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
- window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
- wolfSSL_free(ssl);
- for (i = 1; i < server_args.argc; i++) {
- /* restore state */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
- AssertIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
- AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
- AssertIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
- AssertIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
- wolfSSL_free(ssl);
- }
- XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_CTX_free(ctx);
- fprintf(stderr, "done and waiting for server\n");
- join_thread(serverThread);
- AssertIntEQ(server_args.return_code, TEST_SUCCESS);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
- #ifdef WOLFSSL_TLS13
- static const byte canned_client_tls13_session[] = {
- 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
- 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
- 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
- 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
- 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
- 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
- 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
- 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
- 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
- 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
- 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
- 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
- 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
- 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
- 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
- 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
- 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
- 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
- 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
- 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
- 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
- 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
- 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
- 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
- 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
- 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
- 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
- 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
- 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x03
- };
- static const byte canned_server_tls13_session[] = {
- 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
- 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
- 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
- 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
- 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
- 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
- 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
- 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
- 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
- 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
- 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
- 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
- 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
- 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
- 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
- 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
- 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
- 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
- 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
- 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
- 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
- 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
- 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
- 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
- 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
- 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
- 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
- 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
- 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x04
- };
- #endif /* WOLFSSL_TLS13 */
- static const byte canned_client_session[] = {
- 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
- 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
- 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
- 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
- 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
- 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
- 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
- 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
- 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
- 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
- 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
- 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
- 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
- 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
- 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
- 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
- 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
- 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
- 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
- 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
- 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
- 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
- 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
- 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
- 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
- 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
- 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
- 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
- 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
- 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x03
- };
- static const byte canned_server_session[] = {
- 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
- 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
- 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
- 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
- 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
- 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
- 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
- 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
- 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
- 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
- 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
- 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
- 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
- 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
- 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
- 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
- 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
- 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
- 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
- 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
- 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
- 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
- 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
- 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
- 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
- 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
- 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
- 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x04
- };
- static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
- {
- SOCKET_T sockfd = 0;
- SOCKET_T clientfd = 0;
- word16 port;
- callback_functions* cbf;
- WOLFSSL_CTX* ctx = 0;
- WOLFSSL* ssl = 0;
- char msg[] = "I hear you fa shizzle!";
- char input[1024];
- int idx;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- ((func_args*)args)->return_code = TEST_FAIL;
- cbf = ((func_args*)args)->callbacks;
- {
- WOLFSSL_METHOD* method = NULL;
- if (cbf != NULL && cbf->method != NULL) {
- method = cbf->method();
- }
- else {
- method = wolfTLSv1_2_server_method();
- }
- ctx = wolfSSL_CTX_new(method);
- }
- if (ctx == NULL) {
- goto done;
- }
- wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
- #if defined(USE_WINDOWS_API)
- port = ((func_args*)args)->signal->port;
- #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
- !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
- /* Let tcp_listen assign port */
- port = 0;
- #else
- /* Use default port */
- port = wolfSSLPort;
- #endif
- /* do it here to detect failure */
- tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
- CloseSocket(sockfd);
- /* call ctx setup callback */
- if (cbf != NULL && cbf->ctx_ready != NULL) {
- cbf->ctx_ready(ctx);
- }
- ssl = wolfSSL_new(ctx);
- if (ssl == NULL) {
- goto done;
- }
- wolfSSL_set_fd(ssl, clientfd);
- /* call ssl setup callback */
- if (cbf != NULL && cbf->ssl_ready != NULL) {
- cbf->ssl_ready(ssl);
- }
- idx = wolfSSL_read(ssl, input, sizeof(input)-1);
- if (idx > 0) {
- input[idx] = '\0';
- fprintf(stderr, "Client message export/import: %s\n", input);
- }
- else {
- fprintf(stderr, "ret = %d error = %d\n", idx,
- wolfSSL_get_error(ssl, idx));
- goto done;
- }
- if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
- /*err_sys("SSL_write failed");*/
- #ifdef WOLFSSL_TIRTOS
- return;
- #else
- return 0;
- #endif
- }
- #ifdef WOLFSSL_TIRTOS
- Task_yield();
- #endif
- ((func_args*)args)->return_code = TEST_SUCCESS;
- done:
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(clientfd);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
- && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- #if defined(HAVE_SESSION_TICKET) && \
- ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
- #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
- OpenSSLTicketCleanup();
- #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
- TicketCleanup();
- #endif
- #endif
- #ifndef WOLFSSL_TIRTOS
- return 0;
- #endif
- }
- static void load_tls12_canned_server(WOLFSSL* ssl)
- {
- int clientfd = wolfSSL_get_fd(ssl);
- AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
- sizeof(canned_server_session)), sizeof(canned_server_session));
- wolfSSL_set_fd(ssl, clientfd);
- }
- #ifdef WOLFSSL_TLS13
- static void load_tls13_canned_server(WOLFSSL* ssl)
- {
- int clientfd = wolfSSL_get_fd(ssl);
- AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
- sizeof(canned_server_tls13_session)),
- sizeof(canned_server_tls13_session));
- wolfSSL_set_fd(ssl, clientfd);
- }
- #endif
- /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
- static int test_wolfSSL_tls_export_run(int v)
- {
- SOCKET_T sockfd = 0;
- WOLFSSL_CTX* ctx = 0;
- WOLFSSL* ssl = 0;
- char msg[64] = "hello wolfssl!";
- char reply[1024];
- word32 replySz;
- int msgSz = (int)XSTRLEN(msg);
- const byte* clientSession = NULL;
- int clientSessionSz = 0;
- tcp_ready ready;
- func_args server_args;
- THREAD_TYPE serverThread;
- callback_functions server_cbf;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&server_cbf, 0, sizeof(callback_functions));
- switch (v) {
- case WOLFSSL_TLSV1_2:
- server_cbf.method = wolfTLSv1_2_server_method;
- server_cbf.ssl_ready = load_tls12_canned_server;
- /* setup the client side */
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
- wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
- clientSession = canned_client_session;
- clientSessionSz = sizeof(canned_client_session);
- break;
- #ifdef WOLFSSL_TLS13
- case WOLFSSL_TLSV1_3:
- server_cbf.method = wolfTLSv1_3_server_method;
- server_cbf.ssl_ready = load_tls13_canned_server;
- /* setup the client side */
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
- clientSession = canned_client_tls13_session;
- clientSessionSz = sizeof(canned_client_tls13_session);
- break;
- #endif
- }
- server_args.callbacks = &server_cbf;
- server_args.signal = &ready;
- start_thread(tls_export_server, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- AssertNotNull(ssl = wolfSSL_new(ctx));
- tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
- AssertIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
- clientSessionSz);
- replySz = sizeof(reply);
- AssertIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
- #if !defined(NO_PSK) && defined(HAVE_ANON)
- /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
- AssertIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
- #endif
- wolfSSL_set_fd(ssl, sockfd);
- AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
- AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(sockfd);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
- && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- join_thread(serverThread);
- AssertIntEQ(server_args.return_code, TEST_SUCCESS);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- return TEST_RES_CHECK(1);
- }
- #endif
- static int test_wolfSSL_tls_export(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
- test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
- #ifdef WOLFSSL_TLS13
- test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*----------------------------------------------------------------------------*
- | TLS extensions tests
- *----------------------------------------------------------------------------*/
- #ifdef ENABLE_TLS_CALLBACK_TEST
- /* Connection test runner - generic */
- static void test_wolfSSL_client_server(callback_functions* client_callbacks,
- callback_functions* server_callbacks)
- {
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- client_args.callbacks = client_callbacks;
- server_args.callbacks = server_callbacks;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- /* RUN Server side */
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- start_thread(run_wolfssl_server, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- /* RUN Client side */
- run_wolfssl_client(&client_args);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- client_callbacks->return_code = client_args.return_code;
- server_callbacks->return_code = server_args.return_code;
- }
- #endif /* ENABLE_TLS_CALLBACK_TEST */
- #ifdef HAVE_SNI
- static int test_wolfSSL_UseSNI_params(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- WOLFSSL *ssl = wolfSSL_new(ctx);
- AssertNotNull(ctx);
- AssertNotNull(ssl);
- /* invalid [ctx|ssl] */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
- /* invalid type */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
- /* invalid data */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
- /* success case */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT */
- return res;
- }
- /* BEGIN of connection tests callbacks */
- static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
- {
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
- }
- static void use_SNI_at_ssl(WOLFSSL* ssl)
- {
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
- }
- static void different_SNI_at_ssl(WOLFSSL* ssl)
- {
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
- }
- static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
- {
- use_SNI_at_ssl(ssl);
- wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
- WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
- }
- static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
- {
- use_SNI_at_ssl(ssl);
- wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
- WOLFSSL_SNI_ANSWER_ON_MISMATCH);
- }
- static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
- {
- use_SNI_at_ctx(ctx);
- wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
- WOLFSSL_SNI_ABORT_ON_ABSENCE);
- }
- static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
- {
- use_SNI_at_ssl(ssl);
- wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
- WOLFSSL_SNI_ABORT_ON_ABSENCE);
- }
- static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
- {
- use_SNI_at_ctx(ctx);
- wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
- WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
- }
- static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
- {
- AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
- }
- static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
- {
- AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
- }
- static void verify_SNI_no_matching(WOLFSSL* ssl)
- {
- byte type = WOLFSSL_SNI_HOST_NAME;
- void* request = (void*) &type; /* to be overwritten */
- AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
- AssertNotNull(request);
- AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
- AssertNull(request);
- }
- static void verify_SNI_real_matching(WOLFSSL* ssl)
- {
- byte type = WOLFSSL_SNI_HOST_NAME;
- void* request = NULL;
- AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
- AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
- AssertNotNull(request);
- AssertStrEQ("www.wolfssl.com", (char*)request);
- }
- static void verify_SNI_fake_matching(WOLFSSL* ssl)
- {
- byte type = WOLFSSL_SNI_HOST_NAME;
- void* request = NULL;
- AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
- AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
- AssertNotNull(request);
- AssertStrEQ("ww2.wolfssl.com", (char*)request);
- }
- static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
- {
- AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
- }
- /* END of connection tests callbacks */
- static int test_wolfSSL_UseSNI_connection(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- callback_functions client_cb;
- callback_functions server_cb;
- size_t i;
- struct {
- method_provider client_meth;
- method_provider server_meth;
- } methods[] = {
- #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
- {wolfSSLv23_client_method, wolfSSLv23_server_method},
- #endif
- #ifndef WOLFSSL_NO_TLS12
- {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method},
- #endif
- #ifdef WOLFSSL_TLS13
- {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method},
- #endif
- };
- for (i = 0; i < (sizeof(methods)/sizeof(*methods)); i++) {
- XMEMSET(&client_cb, 0, sizeof(callback_functions));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- client_cb.method = methods[i].client_meth;
- server_cb.method = methods[i].server_meth;
- client_cb.devId = testDevId;
- server_cb.devId = testDevId;
- /* success case at ctx */
- printf("success case at ctx\n");
- client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
- server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* success case at ssl */
- printf("success case at ssl\n");
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* default mismatch behavior */
- printf("default mismatch behavior\n");
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* continue on mismatch */
- printf("continue on mismatch\n");
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* fake answer on mismatch */
- printf("fake answer on mismatch\n");
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* sni abort - success */
- printf("sni abort - success\n");
- client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
- server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* sni abort - abort when absent (ctx) */
- printf("sni abort - abort when absent (ctx)\n");
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
- server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* sni abort - abort when absent (ssl) */
- printf("sni abort - abort when absent (ssl)\n");
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* sni abort - success when overwritten */
- printf("sni abort - success when overwritten\n");
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
- 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;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* sni abort - success when allowing mismatches */
- printf("sni abort - success when allowing mismatches\n");
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
- server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- }
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
- return res;
- }
- static int test_wolfSSL_SNI_GetFromBuffer(void)
- {
- byte buff[] = { /* www.paypal.com */
- 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
- 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
- 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
- 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
- 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
- 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
- 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
- 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
- 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
- };
- byte buff2[] = { /* api.textmate.org */
- 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
- 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
- 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
- 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
- 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
- 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
- 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
- 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
- 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
- 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
- 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
- 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
- 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
- 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
- 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
- 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
- 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
- };
- byte buff3[] = { /* no sni extension */
- 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
- 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
- 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
- 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
- 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
- 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
- 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
- };
- byte buff4[] = { /* last extension has zero size */
- 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
- 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
- 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
- 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
- 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
- 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
- 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
- 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
- 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
- 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
- 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
- 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
- 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
- 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
- 0x12, 0x00, 0x00
- };
- byte buff5[] = { /* SSL v2.0 client hello */
- 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
- /* dummy bytes bellow, just to pass size check */
- 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
- 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
- 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
- };
- byte result[32] = {0};
- word32 length = 32;
- AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
- 0, result, &length));
- AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
- 0, result, &length));
- AssertIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
- 1, result, &length));
- AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
- 0, result, &length));
- buff[0] = 0x16;
- AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
- 0, result, &length));
- buff[1] = 0x03;
- AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
- sizeof(buff), 0, result, &length));
- buff[2] = 0x03;
- AssertIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
- sizeof(buff), 0, result, &length));
- buff[4] = 0x64;
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
- 0, result, &length));
- result[length] = 0;
- AssertStrEQ("www.paypal.com", (const char*) result);
- length = 32;
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
- 0, result, &length));
- result[length] = 0;
- AssertStrEQ("api.textmate.org", (const char*) result);
- /* SSL v2.0 tests */
- AssertIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
- sizeof(buff5), 0, result, &length));
- buff5[2] = 0x02;
- AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
- sizeof(buff5), 0, result, &length));
- buff5[2] = 0x01; buff5[6] = 0x08;
- AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
- sizeof(buff5), 0, result, &length));
- buff5[6] = 0x09; buff5[8] = 0x01;
- AssertIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
- sizeof(buff5), 0, result, &length));
- return TEST_RES_CHECK(1);
- }
- #endif /* HAVE_SNI */
- #endif /* HAVE_IO_TESTS_DEPENDENCIES */
- static int test_wolfSSL_UseTrustedCA(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
- && !defined(NO_RSA)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX *ctx;
- WOLFSSL *ssl;
- byte id[20];
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- #else
- AssertNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
- #endif
- AssertNotNull((ssl = wolfSSL_new(ctx)));
- XMEMSET(id, 0, sizeof(id));
- /* error cases */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
- WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
- WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
- WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
- #ifdef NO_SHA
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
- WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
- #endif
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
- WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
- /* success cases */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
- WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
- #ifndef NO_SHA
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
- WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
- WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* HAVE_TRUSTED_CA */
- return res;
- }
- static int test_wolfSSL_UseMaxFragment(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- #ifndef NO_WOLFSSL_SERVER
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- #else
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- #endif
- WOLFSSL *ssl;
- #ifdef OPENSSL_EXTRA
- int (*UseMaxFragment)(SSL *s, uint8_t mode);
- int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
- #else
- int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
- int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- #endif
- AssertNotNull(ctx);
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- #ifdef OPENSSL_EXTRA
- CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
- UseMaxFragment = SSL_set_tlsext_max_fragment_length;
- #else
- UseMaxFragment = wolfSSL_UseMaxFragment;
- CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
- #endif
- /* error cases */
- AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
- AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
- AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
- AssertIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
- AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
- AssertIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
- /* success case */
- #ifdef OPENSSL_EXTRA
- AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
- #else
- AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
- AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
- AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
- AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
- #ifdef OPENSSL_EXTRA
- AssertIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
- AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
- #else
- AssertIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
- AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
- AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
- AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
- AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
- #ifdef OPENSSL_EXTRA
- AssertIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
- #else
- AssertIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
- #endif
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return res;
- }
- static int test_wolfSSL_UseTruncatedHMAC(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- #ifndef NO_WOLFSSL_SERVER
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- #else
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- #endif
- WOLFSSL *ssl;
- AssertNotNull(ctx);
- #ifndef NO_WOLFSSL_SERVER
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- #endif
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- /* error cases */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
- /* success case */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return res;
- }
- static int test_wolfSSL_UseSupportedCurve(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- WOLFSSL *ssl = wolfSSL_new(ctx);
- AssertNotNull(ctx);
- AssertNotNull(ssl);
- /* error cases */
- AssertIntNE(WOLFSSL_SUCCESS,
- wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
- AssertIntNE(WOLFSSL_SUCCESS,
- wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
- /* success case */
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
- {
- AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
- }
- static void use_ALPN_all(WOLFSSL* ssl)
- {
- /* http/1.1,spdy/1,spdy/2,spdy/3 */
- char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
- WOLFSSL_ALPN_FAILED_ON_MISMATCH));
- }
- static void use_ALPN_all_continue(WOLFSSL* ssl)
- {
- /* http/1.1,spdy/1,spdy/2,spdy/3 */
- char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
- WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
- }
- static void use_ALPN_one(WOLFSSL* ssl)
- {
- /* spdy/2 */
- char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
- WOLFSSL_ALPN_FAILED_ON_MISMATCH));
- }
- static void use_ALPN_unknown(WOLFSSL* ssl)
- {
- /* http/2.0 */
- char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
- WOLFSSL_ALPN_FAILED_ON_MISMATCH));
- }
- static void use_ALPN_unknown_continue(WOLFSSL* ssl)
- {
- /* http/2.0 */
- char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
- WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
- }
- static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
- {
- /* spdy/3 */
- char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
- char *proto = NULL;
- word16 protoSz = 0;
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
- /* check value */
- AssertIntNE(1, sizeof(nego_proto) == protoSz);
- if (proto) {
- AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
- }
- }
- static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
- {
- char *proto = NULL;
- word16 protoSz = 0;
- AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
- wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
- /* check value */
- AssertIntEQ(1, (0 == protoSz));
- AssertIntEQ(1, (NULL == proto));
- }
- static void verify_ALPN_matching_http1(WOLFSSL* ssl)
- {
- /* http/1.1 */
- char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
- char *proto;
- word16 protoSz = 0;
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
- /* check value */
- AssertIntEQ(1, sizeof(nego_proto) == protoSz);
- AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
- }
- static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
- {
- /* spdy/2 */
- char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
- char *proto;
- word16 protoSz = 0;
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
- /* check value */
- AssertIntEQ(1, sizeof(nego_proto) == protoSz);
- AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
- }
- static void verify_ALPN_client_list(WOLFSSL* ssl)
- {
- /* http/1.1,spdy/1,spdy/2,spdy/3 */
- char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
- 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
- char *clist = NULL;
- word16 clistSz = 0;
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
- &clistSz));
- /* check value */
- AssertIntEQ(1, sizeof(alpn_list) == clistSz);
- AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
- }
- static int test_wolfSSL_UseALPN_connection(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- callback_functions client_cb;
- callback_functions server_cb;
- XMEMSET(&client_cb, 0, sizeof(callback_functions));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- client_cb.method = wolfSSLv23_client_method;
- server_cb.method = wolfSSLv23_server_method;
- client_cb.devId = testDevId;
- server_cb.devId = testDevId;
- /* success case same list */
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* success case only one for server */
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* success case only one for client */
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* success case none for client */
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* success case mismatch behavior but option 'continue' set */
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* success case read protocol send by client */
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* mismatch behavior with same list
- * the first and only this one must be taken */
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* default mismatch behavior */
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
- return res;
- }
- static int test_wolfSSL_UseALPN_params(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_WOLFSSL_CLIENT
- /* "http/1.1" */
- char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
- /* "spdy/1" */
- char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
- /* "spdy/2" */
- char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
- /* "spdy/3" */
- char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
- char buff[256];
- word32 idx;
- WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- WOLFSSL *ssl = wolfSSL_new(ctx);
- AssertNotNull(ctx);
- AssertNotNull(ssl);
- /* error cases */
- AssertIntNE(WOLFSSL_SUCCESS,
- wolfSSL_UseALPN(NULL, http1, sizeof(http1),
- WOLFSSL_ALPN_FAILED_ON_MISMATCH));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
- WOLFSSL_ALPN_FAILED_ON_MISMATCH));
- /* success case */
- /* http1 only */
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_UseALPN(ssl, http1, sizeof(http1),
- WOLFSSL_ALPN_FAILED_ON_MISMATCH));
- /* http1, spdy1 */
- XMEMCPY(buff, http1, sizeof(http1));
- idx = sizeof(http1);
- buff[idx++] = ',';
- XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
- idx += sizeof(spdy1);
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
- WOLFSSL_ALPN_FAILED_ON_MISMATCH));
- /* http1, spdy2, spdy1 */
- XMEMCPY(buff, http1, sizeof(http1));
- idx = sizeof(http1);
- buff[idx++] = ',';
- XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
- idx += sizeof(spdy2);
- buff[idx++] = ',';
- XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
- idx += sizeof(spdy1);
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
- WOLFSSL_ALPN_FAILED_ON_MISMATCH));
- /* spdy3, http1, spdy2, spdy1 */
- XMEMCPY(buff, spdy3, sizeof(spdy3));
- idx = sizeof(spdy3);
- buff[idx++] = ',';
- XMEMCPY(buff+idx, http1, sizeof(http1));
- idx += sizeof(http1);
- buff[idx++] = ',';
- XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
- idx += sizeof(spdy2);
- buff[idx++] = ',';
- XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
- idx += sizeof(spdy1);
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
- WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif /* HAVE_ALPN */
- #ifdef HAVE_ALPN_PROTOS_SUPPORT
- static void CTX_set_alpn_protos(SSL_CTX *ctx)
- {
- unsigned char p[] = {
- 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
- 6, 's', 'p', 'd', 'y', '/', '2',
- 6, 's', 'p', 'd', 'y', '/', '1',
- };
- unsigned char p_len = sizeof(p);
- int ret;
- ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
- #ifdef WOLFSSL_ERROR_CODE_OPENSSL
- AssertIntEQ(ret, 0);
- #else
- AssertIntEQ(ret, SSL_SUCCESS);
- #endif
- }
- static void set_alpn_protos(SSL* ssl)
- {
- unsigned char p[] = {
- 6, 's', 'p', 'd', 'y', '/', '3',
- 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
- 6, 's', 'p', 'd', 'y', '/', '2',
- 6, 's', 'p', 'd', 'y', '/', '1',
- };
- unsigned char p_len = sizeof(p);
- int ret;
- ret = SSL_set_alpn_protos(ssl, p, p_len);
- #ifdef WOLFSSL_ERROR_CODE_OPENSSL
- AssertIntEQ(ret, 0);
- #else
- AssertIntEQ(ret, SSL_SUCCESS);
- #endif
- }
- static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
- {
- /* "spdy/3" */
- char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
- const unsigned char *proto;
- unsigned int protoSz = 0;
- SSL_get0_alpn_selected(ssl, &proto, &protoSz);
- /* check value */
- AssertIntEQ(1, sizeof(nego_proto) == protoSz);
- AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
- }
- static void verify_alpn_matching_http1(WOLFSSL* ssl)
- {
- /* "http/1.1" */
- char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
- const unsigned char *proto;
- unsigned int protoSz = 0;
- SSL_get0_alpn_selected(ssl, &proto, &protoSz);
- /* check value */
- AssertIntEQ(1, sizeof(nego_proto) == protoSz);
- AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
- }
- static int test_wolfSSL_set_alpn_protos(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- callback_functions client_cb;
- callback_functions server_cb;
- XMEMSET(&client_cb, 0, sizeof(callback_functions));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- client_cb.method = wolfSSLv23_client_method;
- server_cb.method = wolfSSLv23_server_method;
- client_cb.devId = testDevId;
- server_cb.devId = testDevId;
- /* use CTX_alpn_protos */
- client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
- server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- /* use set_alpn_protos */
- client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
- server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
- test_wolfSSL_client_server(&client_cb, &server_cb);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
- return res;
- }
- #endif /* HAVE_ALPN_PROTOS_SUPPORT */
- static int test_wolfSSL_DisableExtendedMasterSecret(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- WOLFSSL *ssl = wolfSSL_new(ctx);
- AssertNotNull(ctx);
- AssertNotNull(ssl);
- /* error cases */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
- /* success cases */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- WOLFSSL *ssl = wolfSSL_new(ctx);
- AssertNotNull(ctx);
- AssertNotNull(ssl);
- /* error cases */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
- /* success cases */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
- (!defined(NO_RSA) || defined(HAVE_ECC))
- /* Called when writing. */
- static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
- {
- (void)ssl;
- (void)buf;
- (void)sz;
- (void)ctx;
- /* Force error return from wolfSSL_accept_TLSv13(). */
- return WANT_WRITE;
- }
- /* Called when reading. */
- static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
- {
- WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
- int len = (int)msg->length;
- (void)ssl;
- (void)sz;
- /* Pass back as much of message as will fit in buffer. */
- if (len > sz)
- len = sz;
- XMEMCPY(buf, msg->buffer, len);
- /* Move over returned data. */
- msg->buffer += len;
- msg->length -= len;
- /* Amount actually copied. */
- return len;
- }
- #endif
- /* Test the detection of duplicate known TLS extensions.
- * Specifically in a ClientHello.
- */
- static int test_tls_ext_duplicate(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
- !defined(NO_FILESYSTEM)
- const unsigned char clientHelloDupTlsExt[] = {
- 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
- 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
- 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
- 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
- 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
- 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
- 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
- 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
- 0x00, 0x9e, 0x01, 0x00,
- /* Extensions - duplicate signature algorithms. */
- 0x00, 0x19, 0x00, 0x0d,
- 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
- 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
- /* Supported Versions extension for TLS 1.3. */
- 0x00, 0x2b,
- 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
- };
- WOLFSSL_BUFFER_INFO msg;
- const char* testCertFile;
- const char* testKeyFile;
- WOLFSSL_CTX *ctx;
- WOLFSSL *ssl;
- #ifndef NO_RSA
- testCertFile = svrCertFile;
- testKeyFile = svrKeyFile;
- #elif defined(HAVE_ECC)
- testCertFile = eccCertFile;
- testKeyFile = eccKeyFile;
- #endif
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
- WOLFSSL_FILETYPE_PEM));
- /* Read from 'msg'. */
- wolfSSL_SetIORecv(ctx, BufferInfoRecv);
- /* No where to send to - dummy sender. */
- wolfSSL_SetIOSend(ctx, DummySend);
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- msg.buffer = (unsigned char*)clientHelloDupTlsExt;
- msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
- wolfSSL_SetIOReadCtx(ssl, &msg);
- AssertIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*----------------------------------------------------------------------------*
- | X509 Tests
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_X509_NAME_get_entry(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_RSA)
- #if defined(OPENSSL_ALL) || \
- (defined(OPENSSL_EXTRA) && \
- (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
- /* use openssl like name to test mapping */
- X509_NAME_ENTRY* ne;
- X509_NAME* name;
- X509* x509;
- #ifndef NO_FILESYSTEM
- ASN1_STRING* asn;
- char* subCN = NULL;
- #endif
- int idx;
- ASN1_OBJECT *object = NULL;
- #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
- defined(WOLFSSL_NGINX)
- #ifndef NO_BIO
- BIO* bio;
- #endif
- #endif
- #ifndef NO_FILESYSTEM
- x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- name = X509_get_subject_name(x509);
- idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
- AssertIntGE(idx, 0);
- ne = X509_NAME_get_entry(name, idx);
- AssertNotNull(ne);
- asn = X509_NAME_ENTRY_get_data(ne);
- AssertNotNull(asn);
- subCN = (char*)ASN1_STRING_data(asn);
- AssertNotNull(subCN);
- wolfSSL_FreeX509(x509);
- #endif
- x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- name = X509_get_subject_name(x509);
- idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
- AssertIntGE(idx, 0);
- #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
- defined(WOLFSSL_NGINX)
- #ifndef NO_BIO
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(bio, name, 4,
- (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
- (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
- BIO_free(bio);
- #endif
- #endif
- ne = X509_NAME_get_entry(name, idx);
- AssertNotNull(ne);
- AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
- wolfSSL_FreeX509(x509);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
- #endif /* !NO_CERTS && !NO_RSA */
- return res;
- }
- /* Testing functions dealing with PKCS12 parsing out X509 certs */
- static int test_wolfSSL_PKCS12(void)
- {
- int res = TEST_SKIPPED;
- /* .p12 file is encrypted with DES3 */
- #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
- * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
- * Password Key
- */
- #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
- !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
- !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
- byte buf[6000];
- char file[] = "./certs/test-servercert.p12";
- char order[] = "./certs/ecc-rsa-server.p12";
- #ifdef WC_RC2
- char rc2p12[] = "./certs/test-servercert-rc2.p12";
- #endif
- char pass[] = "a password";
- const char goodPsw[] = "wolfSSL test";
- const char badPsw[] = "bad";
- #ifdef HAVE_ECC
- WOLFSSL_X509_NAME* subject;
- WOLFSSL_X509 *x509;
- #endif
- XFILE f;
- int bytes, ret, goodPswLen, badPswLen;
- WOLFSSL_BIO *bio;
- WOLFSSL_EVP_PKEY *pkey;
- WC_PKCS12 *pkcs12;
- WC_PKCS12 *pkcs12_2;
- WOLFSSL_X509 *cert;
- WOLFSSL_X509 *tmp;
- WOLF_STACK_OF(WOLFSSL_X509) *ca;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
- || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
- WOLFSSL_CTX *ctx;
- WOLFSSL *ssl;
- WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
- #endif
- f = XFOPEN(file, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- goodPswLen = (int)XSTRLEN(goodPsw);
- badPswLen = (int)XSTRLEN(badPsw);
- bio = BIO_new_mem_buf((void*)buf, bytes);
- AssertNotNull(bio);
- pkcs12 = d2i_PKCS12_bio(bio, NULL);
- AssertNotNull(pkcs12);
- PKCS12_free(pkcs12);
- AssertIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
- d2i_PKCS12_bio(bio, &pkcs12);
- AssertNotNull(pkcs12);
- BIO_free(bio);
- /* check verify MAC directly */
- ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen);
- AssertIntEQ(ret, 1);
- /* check verify MAC fail case directly */
- ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen);
- AssertIntEQ(ret, 0);
- /* check verify MAC fail case */
- ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
- AssertIntEQ(ret, 0);
- AssertNull(pkey);
- AssertNull(cert);
- /* check parse with no extra certs kept */
- ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
- AssertIntEQ(ret, 1);
- AssertNotNull(pkey);
- AssertNotNull(cert);
- wolfSSL_EVP_PKEY_free(pkey);
- wolfSSL_X509_free(cert);
- /* check parse with extra certs kept */
- ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
- AssertIntEQ(ret, 1);
- AssertNotNull(pkey);
- AssertNotNull(cert);
- AssertNotNull(ca);
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
- || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
- /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #endif
- /* Copy stack structure */
- AssertNotNull(tmp_ca = X509_chain_up_ref(ca));
- AssertIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
- /* CTX now owns the tmp_ca stack structure */
- tmp_ca = NULL;
- AssertIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
- AssertNotNull(tmp_ca);
- AssertIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
- /* Check that the main cert is also set */
- AssertNotNull(SSL_CTX_get0_certificate(ctx));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertNotNull(SSL_get_certificate(ssl));
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- #endif
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- /* should be 2 other certs on stack */
- tmp = sk_X509_pop(ca);
- AssertNotNull(tmp);
- X509_free(tmp);
- tmp = sk_X509_pop(ca);
- AssertNotNull(tmp);
- X509_free(tmp);
- AssertNull(sk_X509_pop(ca));
- EVP_PKEY_free(pkey);
- X509_free(cert);
- sk_X509_pop_free(ca, X509_free);
- /* check PKCS12_create */
- AssertNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
- AssertIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
- SSL_SUCCESS);
- AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
- -1, -1, 100, -1, 0)));
- EVP_PKEY_free(pkey);
- X509_free(cert);
- sk_X509_pop_free(ca, NULL);
- AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
- SSL_SUCCESS);
- PKCS12_free(pkcs12_2);
- AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
- NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
- NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
- 2000, 1, 0)));
- EVP_PKEY_free(pkey);
- X509_free(cert);
- sk_X509_pop_free(ca, NULL);
- /* convert to DER then back and parse */
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
- PKCS12_free(pkcs12_2);
- AssertNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
- BIO_free(bio);
- AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
- SSL_SUCCESS);
- /* should be 2 other certs on stack */
- tmp = sk_X509_pop(ca);
- AssertNotNull(tmp);
- X509_free(tmp);
- tmp = sk_X509_pop(ca);
- AssertNotNull(tmp);
- X509_free(tmp);
- AssertNull(sk_X509_pop(ca));
- #ifndef NO_RC4
- PKCS12_free(pkcs12_2);
- AssertNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
- NID_pbe_WithSHA1And128BitRC4,
- NID_pbe_WithSHA1And128BitRC4,
- 2000, 1, 0)));
- EVP_PKEY_free(pkey);
- X509_free(cert);
- sk_X509_pop_free(ca, NULL);
- AssertIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
- SSL_SUCCESS);
- #endif /* NO_RC4 */
- EVP_PKEY_free(pkey);
- X509_free(cert);
- PKCS12_free(pkcs12);
- PKCS12_free(pkcs12_2);
- sk_X509_pop_free(ca, NULL);
- #ifdef HAVE_ECC
- /* test order of parsing */
- f = XFOPEN(order, "rb");
- AssertTrue(f != XBADFILE);
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
- AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
- AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
- WOLFSSL_SUCCESS);
- /* check use of pkey after parse */
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
- || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #endif
- AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
- SSL_CTX_free(ctx);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- AssertNotNull(pkey);
- AssertNotNull(cert);
- AssertNotNull(ca);
- /* compare subject lines of certificates */
- AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
- SSL_FILETYPE_PEM));
- AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
- (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
- X509_free(x509);
- /* test expected fail case */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
- SSL_FILETYPE_PEM));
- AssertIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
- (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
- X509_free(x509);
- X509_free(cert);
- /* get subject line from ca stack */
- AssertNotNull(cert = sk_X509_pop(ca));
- AssertNotNull(subject = wolfSSL_X509_get_subject_name(cert));
- /* compare subject from certificate in ca to expected */
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
- SSL_FILETYPE_PEM));
- AssertIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
- (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
- EVP_PKEY_free(pkey);
- X509_free(x509);
- X509_free(cert);
- BIO_free(bio);
- PKCS12_free(pkcs12);
- sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
- /* test order of parsing */
- f = XFOPEN(file, "rb");
- AssertTrue(f != XBADFILE);
- AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
- XFCLOSE(f);
- /* check verify MAC fail case */
- ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
- AssertIntEQ(ret, 0);
- AssertNull(pkey);
- AssertNull(cert);
- /* check parse with no extra certs kept */
- ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
- AssertIntEQ(ret, 1);
- AssertNotNull(pkey);
- AssertNotNull(cert);
- wolfSSL_EVP_PKEY_free(pkey);
- wolfSSL_X509_free(cert);
- /* check parse with extra certs kept */
- ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
- AssertIntEQ(ret, 1);
- AssertNotNull(pkey);
- AssertNotNull(cert);
- AssertNotNull(ca);
- wolfSSL_EVP_PKEY_free(pkey);
- wolfSSL_X509_free(cert);
- sk_X509_pop_free(ca, NULL);
- PKCS12_free(pkcs12);
- #endif /* HAVE_ECC */
- #ifdef WC_RC2
- /* test PKCS#12 with RC2 encryption */
- f = XFOPEN(rc2p12, "rb");
- AssertTrue(f != XBADFILE);
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- AssertNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
- AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
- /* check verify MAC fail case */
- ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL);
- AssertIntEQ(ret, 0);
- AssertNull(pkey);
- AssertNull(cert);
- /* check parse iwth not extra certs kept */
- ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL);
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- AssertNotNull(pkey);
- AssertNotNull(cert);
- /* check parse with extra certs kept */
- ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca);
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- AssertNotNull(pkey);
- AssertNotNull(cert);
- AssertNotNull(ca);
- wolfSSL_EVP_PKEY_free(pkey);
- wolfSSL_X509_free(cert);
- sk_X509_pop_free(ca, NULL);
- BIO_free(bio);
- PKCS12_free(pkcs12);
- #endif /* WC_RC2 */
- /* Test i2d_PKCS12_bio */
- f = XFOPEN(file, "rb");
- AssertTrue((f != XBADFILE));
- AssertNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
- XFCLOSE(f);
- bio = BIO_new(BIO_s_mem());
- AssertNotNull(bio);
- ret = i2d_PKCS12_bio(bio, pkcs12);
- AssertIntEQ(ret, 1);
- ret = i2d_PKCS12_bio(NULL, pkcs12);
- AssertIntEQ(ret, 0);
- ret = i2d_PKCS12_bio(bio, NULL);
- AssertIntEQ(ret, 0);
- PKCS12_free(pkcs12);
- BIO_free(bio);
- (void)order;
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- #endif /* HAVE_FIPS */
- return res;
- }
- #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
- defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
- (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
- #define TEST_PKCS8_ENC
- #endif
- #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
- && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
- /* used to keep track if FailTestCallback was called */
- static int failTestCallbackCalled = 0;
- static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
- {
- (void)passwd;
- (void)sz;
- (void)rw;
- (void)userdata;
- /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
- failTestCallbackCalled = 1;
- return -1;
- }
- #endif
- static int test_wolfSSL_no_password_cb(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
- && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
- WOLFSSL_CTX* ctx;
- byte buff[FOURK_BUF];
- const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
- const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
- XFILE f;
- int bytes;
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
- #endif
- wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
- AssertTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- AssertIntLE(bytes, sizeof(buff));
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- AssertTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- AssertIntLE(bytes, sizeof(buff));
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- if (failTestCallbackCalled != 0) {
- Fail(("Password callback should not be called by default"),
- ("Password callback was called without attempting "
- "to first decipher private key without password."));
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #ifdef TEST_PKCS8_ENC
- /* for PKCS8 test case */
- static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
- {
- int flag = 0;
- (void)rw;
- if (userdata != NULL) {
- flag = *((int*)userdata); /* user set data */
- }
- switch (flag) {
- case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
- * can be anything the user wishes to be passed to the callback
- * associated with the WOLFSSL_CTX */
- XSTRNCPY(passwd, "yassl123", sz);
- return 8;
- default:
- return BAD_FUNC_ARG;
- }
- }
- #endif /* TEST_PKCS8_ENC */
- /* Testing functions dealing with PKCS8 */
- static int test_wolfSSL_PKCS8(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- byte buff[FOURK_BUF];
- byte der[FOURK_BUF];
- #ifndef NO_RSA
- const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
- const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
- #endif
- const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
- #ifdef HAVE_ECC
- const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
- #endif
- XFILE f;
- int bytes;
- WOLFSSL_CTX* ctx;
- #if defined(HAVE_ECC) && !defined(NO_CODING)
- int ret;
- ecc_key key;
- word32 x = 0;
- #endif
- #ifdef TEST_PKCS8_ENC
- #if !defined(NO_RSA) && !defined(NO_SHA)
- const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
- const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
- #endif
- #if defined(HAVE_ECC) && !defined(NO_SHA)
- const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
- const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
- #endif
- int flag;
- #endif
- (void)der;
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
- #endif
- #else
- #ifndef WOLFSSL_NO_TLS12
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
- #endif
- #endif
- #ifdef TEST_PKCS8_ENC
- wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
- wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
- flag = 1; /* used by password callback as return code */
- #if !defined(NO_RSA) && !defined(NO_SHA)
- /* test loading PEM PKCS8 encrypted file */
- f = XFOPEN(serverKeyPkcs8EncPemFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- /* this next case should fail because of password callback return code */
- flag = 0; /* used by password callback as return code */
- AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
- AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
- "yassl123"), 0);
- /* test that error value is returned with a bad password */
- AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
- "bad"), 0);
- /* test loading PEM PKCS8 encrypted file */
- f = XFOPEN(serverKeyPkcs8EncDerFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- flag = 1; /* used by password callback as return code */
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* this next case should fail because of password callback return code */
- flag = 0; /* used by password callback as return code */
- AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- #endif /* !NO_RSA && !NO_SHA */
- #if defined(HAVE_ECC) && !defined(NO_SHA)
- /* test loading PEM PKCS8 encrypted ECC Key file */
- f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- flag = 1; /* used by password callback as return code */
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- /* this next case should fail because of password callback return code */
- flag = 0; /* used by password callback as return code */
- AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
- AssertIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
- "yassl123"), 0);
- /* test that error value is returned with a bad password */
- AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
- "bad"), 0);
- /* test loading DER PKCS8 encrypted ECC Key file */
- f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- flag = 1; /* used by password callback as return code */
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* this next case should fail because of password callback return code */
- flag = 0; /* used by password callback as return code */
- AssertIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* leave flag as "okay" */
- flag = 1;
- #endif /* HAVE_ECC && !NO_SHA */
- #endif /* TEST_PKCS8_ENC */
- #ifndef NO_RSA
- /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
- f = XFOPEN(serverKeyPkcs8DerFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- /* test loading PEM PKCS8 private key file (not encrypted) */
- f = XFOPEN(serverKeyPkcs8PemFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- #endif /* !NO_RSA */
- /* Test PKCS8 PEM ECC key no crypt */
- f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- #ifdef HAVE_ECC
- /* Test PKCS8 PEM ECC key no crypt */
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- #ifndef NO_CODING
- /* decrypt PKCS8 PEM to key in DER format */
- AssertIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
- (word32)sizeof(der), NULL)), 0);
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
- wc_ecc_free(&key);
- }
- AssertIntEQ(ret, 0);
- #endif
- /* Test PKCS8 DER ECC key no crypt */
- f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- /* Test using a PKCS8 ECC PEM */
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- #else
- /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
- AssertIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
- (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
- #endif /* HAVE_ECC */
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
- return res;
- }
- static int test_wolfSSL_PKCS8_ED25519(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
- defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
- defined(HAVE_ED25519_KEY_IMPORT)
- const byte encPrivKey[] = \
- "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
- "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
- "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
- "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
- "2L9W4v7swXkV+X+a1ww=\n"
- "-----END ENCRYPTED PRIVATE KEY-----\n";
- const char password[] = "abcdefghijklmnopqrstuvwxyz";
- byte der[FOURK_BUF];
- WOLFSSL_CTX* ctx;
- int bytes;
- XMEMSET(der, 0, sizeof(der));
- AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
- (word32)sizeof(der), password)), 0);
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return res;
- }
- static int test_wolfSSL_PKCS8_ED448(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
- defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
- defined(HAVE_ED448_KEY_IMPORT)
- const byte encPrivKey[] = \
- "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
- "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
- "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
- "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
- "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
- "-----END ENCRYPTED PRIVATE KEY-----\n";
- const char password[] = "abcdefghijklmnopqrstuvwxyz";
- byte der[FOURK_BUF];
- WOLFSSL_CTX* ctx;
- int bytes;
- XMEMSET(der, 0, sizeof(der));
- AssertIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
- (word32)sizeof(der), password)), 0);
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return res;
- }
- /* Testing functions dealing with PKCS5 */
- static int test_wolfSSL_PKCS5(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
- #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
- const char* passwd = "myfipsPa$$W0rd";
- #else
- const char *passwd = "pass1234";
- #endif
- const unsigned char *salt = (unsigned char *)"salt1234";
- unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- int ret = 0;
- AssertNotNull(out);
- ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
- (int)XSTRLEN((const char *) salt), 10,
- WC_SHA_DIGEST_SIZE,out);
- AssertIntEQ(ret, SSL_SUCCESS);
- #ifdef WOLFSSL_SHA512
- ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
- (int)XSTRLEN((const char *) salt), 10,
- wolfSSL_EVP_sha512(), WC_SHA_DIGEST_SIZE, out);
- AssertIntEQ(ret, SSL_SUCCESS);
- #endif
- XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
- return res;
- }
- /* test parsing URI from certificate */
- static int test_wolfSSL_URI(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
- && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
- defined(OPENSSL_EXTRA))
- WOLFSSL_X509* x509;
- const char uri[] = "./certs/client-uri-cert.pem";
- const char badUri[] = "./certs/client-relative-uri.pem";
- x509 = wolfSSL_X509_load_certificate_file(uri, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- wolfSSL_FreeX509(x509);
- x509 = wolfSSL_X509_load_certificate_file(badUri, WOLFSSL_FILETYPE_PEM);
- #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
- && !defined(WOLFSSL_FPKI)
- AssertNull(x509);
- #else
- AssertNotNull(x509);
- wolfSSL_FreeX509(x509);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_TBS(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
- && defined(OPENSSL_EXTRA)
- WOLFSSL_X509* x509;
- const unsigned char* tbs;
- int tbsSz;
- AssertNotNull(x509 =
- wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
- AssertNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
- AssertNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
- AssertNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
- AssertIntEQ(tbsSz, 1003);
- wolfSSL_FreeX509(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_verify(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
- && defined(OPENSSL_EXTRA)
- WOLFSSL_X509* ca;
- WOLFSSL_X509* serv;
- WOLFSSL_EVP_PKEY* pkey;
- unsigned char buf[2048];
- const unsigned char* pt = NULL;
- int bufSz;
- AssertNotNull(ca =
- wolfSSL_X509_load_certificate_file(caCertFile, WOLFSSL_FILETYPE_PEM));
- AssertIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(bufSz, 294);
- bufSz = 2048;
- AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
- AssertNotNull(serv =
- wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM));
- /* success case */
- pt = buf;
- AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
- AssertIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
- AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
- wolfSSL_EVP_PKEY_free(pkey);
- /* fail case */
- bufSz = 2048;
- AssertIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
- WOLFSSL_SUCCESS);
- pt = buf;
- AssertNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
- AssertIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
- AssertIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
- wolfSSL_EVP_PKEY_free(pkey);
- wolfSSL_FreeX509(ca);
- wolfSSL_FreeX509(serv);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
- defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
- /* create certificate with version 2 */
- static void test_set_x509_badversion(WOLFSSL_CTX* ctx)
- {
- WOLFSSL_X509 *x509, *x509v2;
- WOLFSSL_EVP_PKEY *priv, *pub;
- unsigned char *der = NULL, *key = NULL, *pt;
- char *header, *name;
- int derSz;
- long keySz;
- XFILE fp;
- WOLFSSL_ASN1_TIME *notBefore, *notAfter;
- time_t t;
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- WOLFSSL_FILETYPE_PEM));
- fp = XFOPEN(cliKeyFile, "rb");
- AssertIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
- WOLFSSL_SUCCESS);
- XFCLOSE(fp);
- pt = key;
- AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
- (const unsigned char**)&pt, keySz));
- /* create the version 2 certificate */
- AssertNotNull(x509v2 = X509_new());
- AssertIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_subject_name(x509v2,
- wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
- wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
- AssertNotNull(pub = wolfSSL_X509_get_pubkey(x509));
- AssertIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
- t = time(NULL);
- AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
- AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
- AssertTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
- AssertTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
- AssertIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
- derSz = wolfSSL_i2d_X509(x509v2, &der);
- AssertIntGT(derSz, 0);
- AssertIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
- WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
- XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL); /* TODO: Replace with API call */
- XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_X509_free(x509);
- wolfSSL_X509_free(x509v2);
- wolfSSL_EVP_PKEY_free(priv);
- wolfSSL_EVP_PKEY_free(pub);
- wolfSSL_ASN1_TIME_free(notBefore);
- wolfSSL_ASN1_TIME_free(notAfter);
- }
- /* override certificate version error */
- static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
- {
- #ifndef OPENSSL_COMPATIBLE_DEFAULTS
- AssertIntEQ(store->error, ASN_VERSION_E);
- #else
- AssertIntEQ(store->error, 0);
- #endif
- AssertIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
- (void)preverify;
- return 1;
- }
- /* set verify callback that will override bad certificate version */
- static void test_set_override_x509(WOLFSSL_CTX* ctx)
- {
- wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
- }
- #endif
- static int test_wolfSSL_X509_TLS_version(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && !defined(NO_AES) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
- defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
- tcp_ready ready;
- func_args server_args;
- func_args client_args;
- THREAD_TYPE serverThread;
- callback_functions func_cb_client;
- callback_functions func_cb_server;
- /* test server rejects a client certificate that is not version 3 */
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
- XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- server_args.return_code = TEST_FAIL;
- client_args.return_code = TEST_FAIL;
- func_cb_client.ctx_ready = &test_set_x509_badversion;
- #ifndef WOLFSSL_NO_TLS12
- func_cb_client.method = wolfTLSv1_2_client_method;
- #else
- func_cb_client.method = wolfTLSv1_3_client_method;
- #endif
- client_args.callbacks = &func_cb_client;
- #ifndef WOLFSSL_NO_TLS12
- func_cb_server.method = wolfTLSv1_2_server_method;
- #else
- func_cb_server.method = wolfTLSv1_3_server_method;
- #endif
- server_args.callbacks = &func_cb_server;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- #ifndef OPENSSL_COMPATIBLE_DEFAULTS
- AssertIntEQ(client_args.return_code, TEST_FAIL);
- AssertIntEQ(server_args.return_code, TEST_FAIL);
- #else
- AssertIntEQ(client_args.return_code, TEST_SUCCESS);
- AssertIntEQ(server_args.return_code, TEST_SUCCESS);
- #endif
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- /* Now re run but override the bad X509 version */
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
- XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- server_args.return_code = TEST_FAIL;
- client_args.return_code = TEST_FAIL;
- func_cb_client.ctx_ready = &test_set_x509_badversion;
- func_cb_server.ctx_ready = &test_set_override_x509;
- #ifndef WOLFSSL_NO_TLS12
- func_cb_client.method = wolfTLSv1_2_client_method;
- #else
- func_cb_client.method = wolfTLSv1_3_client_method;
- #endif
- client_args.callbacks = &func_cb_client;
- #ifndef WOLFSSL_NO_TLS12
- func_cb_server.method = wolfTLSv1_2_server_method;
- #else
- func_cb_server.method = wolfTLSv1_3_server_method;
- #endif
- server_args.callbacks = &func_cb_server;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- AssertIntEQ(client_args.return_code, TEST_SUCCESS);
- AssertIntEQ(server_args.return_code, TEST_SUCCESS);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
- * version allowed.
- * POST: 1 on success.
- */
- static int test_wolfSSL_CTX_SetMinVersion(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_WOLFSSL_CLIENT
- int failFlag = WOLFSSL_SUCCESS;
- WOLFSSL_CTX* ctx;
- int itr;
- #ifndef NO_OLD_TLS
- const int versions[] = {
- #ifdef WOLFSSL_ALLOW_TLSV10
- WOLFSSL_TLSV1,
- #endif
- WOLFSSL_TLSV1_1,
- WOLFSSL_TLSV1_2 };
- #elif !defined(WOLFSSL_NO_TLS12)
- const int versions[] = { WOLFSSL_TLSV1_2 };
- #elif defined(WOLFSSL_TLS13)
- const int versions[] = { WOLFSSL_TLSV1_3 };
- #else
- const int versions[0];
- #endif
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
- if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
- != WOLFSSL_SUCCESS) {
- failFlag = WOLFSSL_FAILURE;
- }
- }
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
- #endif
- return res;
- } /* END test_wolfSSL_CTX_SetMinVersion */
- /*----------------------------------------------------------------------------*
- | OCSP Stapling
- *----------------------------------------------------------------------------*/
- /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
- * need to contact the CA, lowering the cost of cert revocation checking.
- * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
- * POST: 1 returned for success.
- */
- static int test_wolfSSL_UseOCSPStapling(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
- !defined(NO_WOLFSSL_CLIENT)
- int ret;
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
- #else
- ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
- #endif
- #else
- #ifndef WOLFSSL_NO_TLS12
- ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
- #else
- ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
- #endif
- #endif
- ssl = wolfSSL_new(ctx);
- ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
- WOLFSSL_CSR2_OCSP_USE_NONCE);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
- #endif
- return res;
- } /*END test_wolfSSL_UseOCSPStapling */
- /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
- * stapling eliminates the need to contact the CA and lowers cert revocation
- * check.
- * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
- */
- static int test_wolfSSL_UseOCSPStaplingV2(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
- !defined(NO_WOLFSSL_CLIENT)
- int ret;
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
- #else
- ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
- #endif
- #else
- #ifndef WOLFSSL_NO_TLS12
- ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
- #else
- ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
- #endif
- #endif
- ssl = wolfSSL_new(ctx);
- ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
- WOLFSSL_CSR2_OCSP_USE_NONCE );
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
- #endif
- return res;
- } /*END test_wolfSSL_UseOCSPStaplingV2*/
- /*----------------------------------------------------------------------------*
- | Multicast Tests
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_mcast(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
- (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- int result;
- byte preMasterSecret[512];
- byte clientRandom[32];
- byte serverRandom[32];
- byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
- byte buf[256];
- word16 newId;
- ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method());
- AssertNotNull(ctx);
- result = wolfSSL_CTX_mcast_set_member_id(ctx, 0);
- AssertIntEQ(result, WOLFSSL_SUCCESS);
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
- XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
- XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
- result = wolfSSL_set_secret(ssl, 23,
- preMasterSecret, sizeof(preMasterSecret),
- clientRandom, serverRandom, suite);
- AssertIntEQ(result, WOLFSSL_SUCCESS);
- result = wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf));
- AssertIntLE(result, 0);
- AssertIntLE(newId, 100);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
- * WOLFSSL_SNIFFER) */
- return res;
- }
- /*----------------------------------------------------------------------------*
- | Wolfcrypt
- *----------------------------------------------------------------------------*/
- /*
- * Unit test for the wc_InitBlake2b()
- */
- static int test_wc_InitBlake2b(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_BLAKE2
- Blake2b blake;
- int ret = 0;
- /* Test good arg. */
- ret = wc_InitBlake2b(&blake, 64);
- if (ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!ret) {
- ret = wc_InitBlake2b(NULL, 64);
- if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- if (!ret) {
- ret = wc_InitBlake2b(NULL, 128);
- if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- if (!ret) {
- ret = wc_InitBlake2b(&blake, 128);
- if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- if (!ret) {
- ret = wc_InitBlake2b(NULL, 0);
- if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- if (!ret) {
- ret = wc_InitBlake2b(&blake, 0);
- if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_InitBlake2b*/
- /*
- * Unit test for the wc_InitBlake2b_WithKey()
- */
- static int test_wc_InitBlake2b_WithKey(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_BLAKE2
- Blake2b blake;
- word32 digestSz = BLAKE2B_KEYBYTES;
- byte key[BLAKE2B_KEYBYTES];
- word32 keylen = BLAKE2B_KEYBYTES;
- int ret = 0;
- XMEMSET(key, 0, sizeof(key));
- /* Test good arg. */
- ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen);
- if (ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_InitBlake2b_WithKey(&blake, digestSz, key, 256);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen);
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END wc_InitBlake2b_WithKey*/
- /*
- * Unit test for the wc_InitBlake2s_WithKey()
- */
- static int test_wc_InitBlake2s_WithKey(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_BLAKE2S
- Blake2s blake;
- word32 digestSz = BLAKE2S_KEYBYTES;
- byte *key = (byte*)"01234567890123456789012345678901";
- word32 keylen = BLAKE2S_KEYBYTES;
- int ret = 0;
- /* Test good arg. */
- ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen);
- if (ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_InitBlake2s_WithKey(&blake, digestSz, key, 256);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen);
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END wc_InitBlake2s_WithKey*/
- /*
- * Unit test for the wc_InitMd5()
- */
- static int test_wc_InitMd5(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_MD5
- wc_Md5 md5;
- int ret;
- int flag = 0;
- /* Test good arg. */
- ret = wc_InitMd5(&md5);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!flag) {
- ret = wc_InitMd5(NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Md5Free(&md5);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_InitMd5 */
- /*
- * Testing wc_UpdateMd5()
- */
- static int test_wc_Md5Update(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_MD5
- wc_Md5 md5;
- byte hash[WC_MD5_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- int flag = 0;
- ret = wc_InitMd5(&md5);
- if (ret != 0) {
- flag = ret;
- }
- /* Input */
- if (!flag) {
- a.input = "a";
- a.inLen = XSTRLEN(a.input);
- ret = wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Md5Final(&md5, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update input. */
- if (!flag) {
- a.input = "abc";
- a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
- "\x72";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- ret = wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Md5Final(&md5, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /*Pass in bad values. */
- if (!flag) {
- b.input = NULL;
- b.inLen = 0;
- ret = wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- c.input = NULL;
- c.inLen = WC_MD5_DIGEST_SIZE;
- ret = wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Md5Free(&md5);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Md5Update() */
- /*
- * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
- */
- static int test_wc_Md5Final(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_MD5
- /* Instantiate */
- wc_Md5 md5;
- byte* hash_test[3];
- byte hash1[WC_MD5_DIGEST_SIZE];
- byte hash2[2*WC_MD5_DIGEST_SIZE];
- byte hash3[5*WC_MD5_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /* Initialize */
- ret = wc_InitMd5(&md5);
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test)/sizeof(byte*);
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_Md5Final(&md5, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_Md5Final(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Md5Final(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Md5Final(&md5, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Md5Free(&md5);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- }
- /*
- * Unit test for the wc_InitSha()
- */
- static int test_wc_InitSha(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SHA
- wc_Sha sha;
- int ret;
- int flag = 0;
- /* Test good arg. */
- ret = wc_InitSha(&sha);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!flag) {
- ret = wc_InitSha(NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_ShaFree(&sha);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_InitSha */
- /*
- * Tesing wc_ShaUpdate()
- */
- static int test_wc_ShaUpdate(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SHA
- wc_Sha sha;
- byte hash[WC_SHA_DIGEST_SIZE];
- testVector a, b, c;
- int flag = 0;
- int ret;
- ret = wc_InitSha(&sha);
- if (ret != 0) {
- flag = ret;
- }
- /* Input. */
- if (!flag) {
- a.input = "a";
- a.inLen = XSTRLEN(a.input);
- ret = wc_ShaUpdate(&sha, NULL, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_ShaUpdate(&sha, (byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_ShaFinal(&sha, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update input. */
- if (!flag) {
- a.input = "abc";
- a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
- "\x6C\x9C\xD0\xD8\x9D";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- ret = wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_ShaFinal(&sha, hash);
- if (ret !=0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Try passing in bad values. */
- if (!flag) {
- b.input = NULL;
- b.inLen = 0;
- ret = wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- c.input = NULL;
- c.inLen = WC_SHA_DIGEST_SIZE;
- ret = wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_ShaFree(&sha);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_ShaUpdate() */
- /*
- * Unit test on wc_ShaFinal
- */
- static int test_wc_ShaFinal(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SHA
- wc_Sha sha;
- byte* hash_test[3];
- byte hash1[WC_SHA_DIGEST_SIZE];
- byte hash2[2*WC_SHA_DIGEST_SIZE];
- byte hash3[5*WC_SHA_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /*Initialize*/
- ret = wc_InitSha(&sha);
- if (ret) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test)/sizeof(byte*);
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_ShaFinal(&sha, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_ShaFinal(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_ShaFinal(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_ShaFinal(&sha, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_ShaFree(&sha);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_ShaFinal */
- /*
- * Unit test for wc_InitSha256()
- */
- static int test_wc_InitSha256(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SHA256
- wc_Sha256 sha256;
- int ret;
- int flag = 0;
- /* Test good arg. */
- ret = wc_InitSha256(&sha256);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!flag) {
- ret = wc_InitSha256(NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha256Free(&sha256);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_InitSha256 */
- /*
- * Unit test for wc_Sha256Update()
- */
- static int test_wc_Sha256Update(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SHA256
- wc_Sha256 sha256;
- byte hash[WC_SHA256_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- int flag = 0;
- ret = wc_InitSha256(&sha256);
- if (ret != 0) {
- flag = ret;
- }
- /* Input. */
- if (!flag) {
- a.input = "a";
- a.inLen = XSTRLEN(a.input);
- ret = wc_Sha256Update(&sha256, NULL, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha256Update(&sha256, (byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Sha256Final(&sha256, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update input. */
- if (!flag) {
- a.input = "abc";
- a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
- "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
- "\x15\xAD";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- ret = wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Sha256Final(&sha256, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Try passing in bad values */
- if (!flag) {
- b.input = NULL;
- b.inLen = 0;
- ret = wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- c.input = NULL;
- c.inLen = WC_SHA256_DIGEST_SIZE;
- ret = wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha256Free(&sha256);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha256Update */
- /*
- * Unit test function for wc_Sha256Final()
- */
- static int test_wc_Sha256Final(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SHA256
- wc_Sha256 sha256;
- byte* hash_test[3];
- byte hash1[WC_SHA256_DIGEST_SIZE];
- byte hash2[2*WC_SHA256_DIGEST_SIZE];
- byte hash3[5*WC_SHA256_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /* Initialize */
- ret = wc_InitSha256(&sha256);
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test) / sizeof(byte*);
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_Sha256Final(&sha256, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag ) {
- ret = wc_Sha256Final(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha256Final(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha256Final(&sha256, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha256Free(&sha256);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha256Final */
- /*
- * Unit test function for wc_Sha256FinalRaw()
- */
- static int test_wc_Sha256FinalRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
- !defined(WOLFSSL_NO_HASH_RAW)
- wc_Sha256 sha256;
- byte* hash_test[3];
- byte hash1[WC_SHA256_DIGEST_SIZE];
- byte hash2[2*WC_SHA256_DIGEST_SIZE];
- byte hash3[5*WC_SHA256_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /* Initialize */
- ret = wc_InitSha256(&sha256);
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test) / sizeof(byte*);
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_Sha256FinalRaw(&sha256, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_Sha256FinalRaw(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha256FinalRaw(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha256FinalRaw(&sha256, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha256Free(&sha256);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha256FinalRaw */
- /*
- * Unit test function for wc_Sha256GetFlags()
- */
- static int test_wc_Sha256GetFlags(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha256 sha256;
- word32 flags = 0;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha256(&sha256);
- if (flag == 0) {
- flag = wc_Sha256GetFlags(&sha256, &flags);
- }
- if (flag == 0) {
- if (flags & WC_HASH_FLAG_ISCOPY) {
- flag = 0;
- }
- }
- wc_Sha256Free(&sha256);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha256GetFlags */
- /*
- * Unit test function for wc_Sha256Free()
- */
- static int test_wc_Sha256Free(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SHA256
- wc_Sha256Free(NULL);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_Sha256Free */
- /*
- * Unit test function for wc_Sha256GetHash()
- */
- static int test_wc_Sha256GetHash(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SHA256
- wc_Sha256 sha256;
- byte hash1[WC_SHA256_DIGEST_SIZE];
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha256(&sha256);
- if (flag == 0) {
- flag = wc_Sha256GetHash(&sha256, hash1);
- }
- /*test bad arguments*/
- if (flag == 0) {
- flag = wc_Sha256GetHash(NULL, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha256GetHash(NULL, hash1);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha256GetHash(&sha256, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- wc_Sha256Free(&sha256);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha256GetHash */
- /*
- * Unit test function for wc_Sha256Copy()
- */
- static int test_wc_Sha256Copy(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SHA256
- wc_Sha256 sha256;
- wc_Sha256 temp;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha256(&sha256);
- if (flag == 0) {
- flag = wc_InitSha256(&temp);
- }
- if (flag == 0) {
- flag = wc_Sha256Copy(&sha256, &temp);
- }
- /*test bad arguments*/
- if (flag == 0) {
- flag = wc_Sha256Copy(NULL, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha256Copy(NULL, &temp);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha256Copy(&sha256, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- wc_Sha256Free(&sha256);
- wc_Sha256Free(&temp);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha256Copy */
- /*
- * Testing wc_InitSha512()
- */
- static int test_wc_InitSha512(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA512
- wc_Sha512 sha512;
- int ret;
- int flag = 0;
- /* Test good arg. */
- ret = wc_InitSha512(&sha512);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!flag) {
- ret = wc_InitSha512(NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha512Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_InitSha512 */
- /*
- * wc_Sha512Update() test.
- */
- static int test_wc_Sha512Update(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA512
- wc_Sha512 sha512;
- byte hash[WC_SHA512_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- int flag = 0;
- ret = wc_InitSha512(&sha512);
- if (ret != 0) {
- flag = ret;
- }
- /* Input. */
- if (!flag) {
- a.input = "a";
- a.inLen = XSTRLEN(a.input);
- ret = wc_Sha512Update(&sha512, NULL, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha512Update(&sha512,(byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha512Final(&sha512, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update input. */
- if (!flag) {
- a.input = "abc";
- a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
- "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
- "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
- "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
- "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- ret = wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Sha512Final(&sha512, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Try passing in bad values */
- if (!flag) {
- b.input = NULL;
- b.inLen = 0;
- ret = wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- c.input = NULL;
- c.inLen = WC_SHA512_DIGEST_SIZE;
- ret = wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha512Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha512Update */
- #ifdef WOLFSSL_SHA512
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
- (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
- /* Perfoms test for
- * - wc_Sha512Final/wc_Sha512FinalRaw
- * - wc_Sha512_224Final/wc_Sha512_224Final
- * - wc_Sha512_256Final/wc_Sha512_256Final
- * parameter:
- * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
- * WC_HASH_TYPE_SHA512_256
- * - isRaw: if is non-zero, xxxFinalRaw function will be tested
- *return 0 on success
- */
- static int test_Sha512_Family_Final(int type, int isRaw)
- {
- wc_Sha512 sha512;
- byte* hash_test[3];
- byte hash1[WC_SHA512_DIGEST_SIZE];
- byte hash2[2*WC_SHA512_DIGEST_SIZE];
- byte hash3[5*WC_SHA512_DIGEST_SIZE];
- int times, i, ret;
- int(*initFp)(wc_Sha512*);
- int(*finalFp)(wc_Sha512*, byte*);
- void(*freeFp)(wc_Sha512*);
- if (type == WC_HASH_TYPE_SHA512) {
- initFp = wc_InitSha512;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
- !defined(WOLFSSL_NO_HASH_RAW)
- finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
- #else
- finalFp = (isRaw)? NULL : wc_Sha512Final;
- #endif
- freeFp = wc_Sha512Free;
- }
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if !defined(WOLFSSL_NOSHA512_224)
- else if (type == WC_HASH_TYPE_SHA512_224) {
- initFp = wc_InitSha512_224;
- #if !defined(WOLFSSL_NO_HASH_RAW)
- finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
- #else
- finalFp = (isRaw)? NULL : wc_Sha512_224Final;
- #endif
- freeFp = wc_Sha512_224Free;
- }
- #endif
- #if !defined(WOLFSSL_NOSHA512_256)
- else if (type == WC_HASH_TYPE_SHA512_256) {
- initFp = wc_InitSha512_256;
- #if !defined(WOLFSSL_NO_HASH_RAW)
- finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
- #else
- finalFp = (isRaw)? NULL : wc_Sha512_256Final;
- #endif
- freeFp = wc_Sha512_256Free;
- }
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- else
- return TEST_FAIL;
- /* Initialize */
- ret = initFp(&sha512);
- if (!ret) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test) / sizeof(byte *);
- /* Good test args. */
- for (i = 0; i < times && ret == 0; i++) {
- ret = finalFp(&sha512, hash_test[i]);
- }
- /* Test bad args. */
- if (!ret) {
- if (finalFp(NULL, NULL) != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!ret) {
- if (finalFp(NULL, hash1) != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!ret) {
- if (finalFp(&sha512, NULL) != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- freeFp(&sha512);
- return ret;
- }
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
- (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
- #endif /* WOLFSSL_SHA512 */
- /*
- * Unit test function for wc_Sha512Final()
- */
- static int test_wc_Sha512Final(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA512
- wc_Sha512 sha512;
- byte* hash_test[3];
- byte hash1[WC_SHA512_DIGEST_SIZE];
- byte hash2[2*WC_SHA512_DIGEST_SIZE];
- byte hash3[5*WC_SHA512_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /* Initialize */
- ret = wc_InitSha512(&sha512);
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test) / sizeof(byte *);
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_Sha512Final(&sha512, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_Sha512Final(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha512Final(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha512Final(&sha512, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha512Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha512Final */
- /*
- * Unit test function for wc_Sha512GetFlags()
- */
- static int test_wc_Sha512GetFlags(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha512 sha512;
- word32 flags = 0;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha512(&sha512);
- if (flag == 0) {
- flag = wc_Sha512GetFlags(&sha512, &flags);
- }
- if (flag == 0) {
- if (flags & WC_HASH_FLAG_ISCOPY) {
- flag = 0;
- }
- }
- wc_Sha512Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha512GetFlags */
- /*
- * Unit test function for wc_Sha512FinalRaw()
- */
- static int test_wc_Sha512FinalRaw(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
- !defined(WOLFSSL_NO_HASH_RAW)
- wc_Sha512 sha512;
- byte* hash_test[3];
- byte hash1[WC_SHA512_DIGEST_SIZE];
- byte hash2[2*WC_SHA512_DIGEST_SIZE];
- byte hash3[5*WC_SHA512_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /* Initialize */
- ret = wc_InitSha512(&sha512);
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test) / sizeof(byte*);
- /* Good test args. */
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_Sha512FinalRaw(&sha512, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag ) {
- ret = wc_Sha512FinalRaw(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha512FinalRaw(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha512FinalRaw(&sha512, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha512Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha512FinalRaw */
- /*
- * Unit test function for wc_Sha512Free()
- */
- static int test_wc_Sha512Free(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA512
- wc_Sha512Free(NULL);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_Sha512Free */
- #ifdef WOLFSSL_SHA512
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
- (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
- static int test_Sha512_Family_GetHash(int type )
- {
- int flag = 0;
- int(*initFp)(wc_Sha512*);
- int(*ghashFp)(wc_Sha512*, byte*);
- wc_Sha512 sha512;
- byte hash1[WC_SHA512_DIGEST_SIZE];
- if (type == WC_HASH_TYPE_SHA512) {
- initFp = wc_InitSha512;
- ghashFp = wc_Sha512GetHash;
- }
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if !defined(WOLFSSL_NOSHA512_224)
- else if (type == WC_HASH_TYPE_SHA512_224) {
- initFp = wc_InitSha512_224;
- ghashFp = wc_Sha512_224GetHash;
- }
- #endif
- #if !defined(WOLFSSL_NOSHA512_256)
- else if (type == WC_HASH_TYPE_SHA512_256) {
- initFp = wc_InitSha512_256;
- ghashFp = wc_Sha512_256GetHash;
- }
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- else {
- initFp = NULL;
- ghashFp = NULL;
- }
- if (initFp == NULL || ghashFp == NULL)
- return TEST_FAIL;
- if (!flag) {
- flag = initFp(&sha512);
- }
- if (!flag) {
- flag = ghashFp(&sha512, hash1);
- }
- /*test bad arguments*/
- if (!flag) {
- if (ghashFp(NULL, NULL) != BAD_FUNC_ARG )
- flag = WOLFSSL_FATAL_ERROR;
- }
- if (!flag) {
- if (ghashFp(NULL, hash1) != BAD_FUNC_ARG )
- flag = WOLFSSL_FATAL_ERROR;
- }
- if (!flag) {
- if (ghashFp(&sha512, NULL) != BAD_FUNC_ARG )
- flag = WOLFSSL_FATAL_ERROR;
- }
- wc_Sha512Free(&sha512);
- return flag;
- }
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
- (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
- #endif /* WOLFSSL_SHA512 */
- /*
- * Unit test function for wc_Sha512GetHash()
- */
- static int test_wc_Sha512GetHash(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA512
- wc_Sha512 sha512;
- byte hash1[WC_SHA512_DIGEST_SIZE];
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha512(&sha512);
- if (flag == 0) {
- flag = wc_Sha512GetHash(&sha512, hash1);
- }
- /*test bad arguments*/
- if (flag == 0) {
- flag = wc_Sha512GetHash(NULL, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha512GetHash(NULL, hash1);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha512GetHash(&sha512, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- wc_Sha512Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha512GetHash */
- /*
- * Unit test function for wc_Sha512Copy()
- */
- static int test_wc_Sha512Copy(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA512
- wc_Sha512 sha512;
- wc_Sha512 temp;
- int flag;
- /* Initialize */
- flag = wc_InitSha512(&sha512);
- if (flag == 0) {
- flag = wc_InitSha512(&temp);
- }
- if (flag == 0) {
- flag = wc_Sha512Copy(&sha512, &temp);
- }
- /*test bad arguments*/
- if (flag == 0) {
- flag = wc_Sha512Copy(NULL, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha512Copy(NULL, &temp);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha512Copy(&sha512, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- wc_Sha512Free(&sha512);
- wc_Sha512Free(&temp);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha512Copy */
- static int test_wc_InitSha512_224(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- wc_Sha512 sha512;
- int ret;
- int flag = 0;
- /* Test good arg. */
- ret = wc_InitSha512_224(&sha512);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!flag) {
- ret = wc_InitSha512_224(NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha512_224Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_224Update(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- wc_Sha512 sha512;
- byte hash[WC_SHA512_DIGEST_SIZE];
- testVector a, c;
- int ret;
- int flag = 0;
- ret = wc_InitSha512_224(&sha512);
- if (ret != 0) {
- flag = ret;
- }
- /* Input. */
- if (!flag) {
- a.input = "a";
- a.inLen = XSTRLEN(a.input);
- ret = wc_Sha512_224Update(&sha512, NULL, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha512_224Update(&sha512,(byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha512_224Final(&sha512, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update input. */
- if (!flag) {
- a.input = "abc";
- a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
- "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- ret = wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Sha512_224Final(&sha512, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- c.input = NULL;
- c.inLen = WC_SHA512_224_DIGEST_SIZE;
- ret = wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha512_224Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_224Final(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
- res = TEST_RES_CHECK(ret == 0);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_224GetFlags(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha512 sha512, copy;
- word32 flags = 0;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha512_224(&sha512);
- if (!flag) {
- flag = wc_InitSha512_224(©);
- }
- if (!flag) {
- flag = wc_Sha512_224Copy(&sha512, ©);
- }
- if (!flag) {
- flag = wc_Sha512_224GetFlags(©, &flags);
- }
- if (!flag) {
- if (flags & WC_HASH_FLAG_ISCOPY)
- flag = 0;
- else
- flag = WOLFSSL_FATAL_ERROR;
- }
- wc_Sha512_224Free(©);
- wc_Sha512_224Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_224FinalRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
- defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
- !defined(WOLFSSL_NO_HASH_RAW)
- int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }
- static int test_wc_Sha512_224Free(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- wc_Sha512_224Free(NULL);
- res = TEST_RES_CHECK(1);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_224GetHash(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_224Copy(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- wc_Sha512 sha512;
- wc_Sha512 temp;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha512_224(&sha512);
- if (flag == 0) {
- flag = wc_InitSha512_224(&temp);
- }
- if (flag == 0) {
- flag = wc_Sha512_224Copy(&sha512, &temp);
- }
- /*test bad arguments*/
- if (flag == 0) {
- if (wc_Sha512_224Copy(NULL, NULL) != BAD_FUNC_ARG)
- flag = WOLFSSL_FATAL_ERROR;
- }
- if (flag == 0) {
- if (wc_Sha512_224Copy(NULL, &temp) != BAD_FUNC_ARG)
- flag = WOLFSSL_FATAL_ERROR;
- }
- if (flag == 0) {
- if (wc_Sha512_224Copy(&sha512, NULL) != BAD_FUNC_ARG)
- flag = WOLFSSL_FATAL_ERROR;
- }
- wc_Sha512_224Free(&sha512);
- wc_Sha512_224Free(&temp);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_InitSha512_256(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- wc_Sha512 sha512;
- int ret;
- int flag = 0;
- /* Test good arg. */
- ret = wc_InitSha512_256(&sha512);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!flag) {
- ret = wc_InitSha512_256(NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha512_256Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_256Update(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- wc_Sha512 sha512;
- byte hash[WC_SHA512_DIGEST_SIZE];
- testVector a, c;
- int ret;
- int flag = 0;
- ret = wc_InitSha512_256(&sha512);
- if (ret != 0) {
- flag = ret;
- }
- /* Input. */
- if (!flag) {
- a.input = "a";
- a.inLen = XSTRLEN(a.input);
- ret = wc_Sha512_256Update(&sha512, NULL, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha512_256Update(&sha512,(byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha512_256Final(&sha512, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update input. */
- if (!flag) {
- a.input = "abc";
- a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
- "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
- "\x07\xe7\xaf\x23";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- ret = wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Sha512_256Final(&sha512, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- c.input = NULL;
- c.inLen = WC_SHA512_256_DIGEST_SIZE;
- ret = wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha512_256Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_256Final(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
- res = TEST_RES_CHECK(ret == 0);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_256GetFlags(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha512 sha512, copy;
- word32 flags = 0;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha512_256(&sha512);
- if (!flag ) {
- flag = wc_InitSha512_256(©);
- }
- if (!flag ) {
- flag = wc_Sha512_256Copy(&sha512, ©);
- }
- if (!flag ) {
- flag = wc_Sha512_256GetFlags(©, &flags);
- }
- if (!flag) {
- if (flags & WC_HASH_FLAG_ISCOPY)
- flag = 0;
- else
- flag = WOLFSSL_FATAL_ERROR;
- }
- wc_Sha512_256Free(&sha512);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_256FinalRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
- defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
- !defined(WOLFSSL_NO_HASH_RAW)
- int ret = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }
- static int test_wc_Sha512_256Free(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- wc_Sha512_256Free(NULL);
- res = TEST_RES_CHECK(1);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_256GetHash(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- int ret = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- static int test_wc_Sha512_256Copy(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- wc_Sha512 sha512;
- wc_Sha512 temp;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha512_256(&sha512);
- if (flag == 0) {
- flag = wc_InitSha512_256(&temp);
- }
- if (flag == 0) {
- flag = wc_Sha512_256Copy(&sha512, &temp);
- }
- /*test bad arguments*/
- if (flag == 0) {
- if (wc_Sha512_256Copy(NULL, NULL) != BAD_FUNC_ARG)
- flag = WOLFSSL_FATAL_ERROR;
- }
- if (flag == 0) {
- if (wc_Sha512_256Copy(NULL, &temp) != BAD_FUNC_ARG)
- flag = WOLFSSL_FATAL_ERROR;
- }
- if (flag == 0) {
- if (wc_Sha512_256Copy(&sha512, NULL) != BAD_FUNC_ARG)
- flag = WOLFSSL_FATAL_ERROR;
- }
- wc_Sha512_256Free(&sha512);
- wc_Sha512_256Free(&temp);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- /*
- * Testing wc_InitSha384()
- */
- static int test_wc_InitSha384(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA384
- wc_Sha384 sha384;
- int ret;
- int flag = 0;
- /* Test good arg. */
- ret = wc_InitSha384(&sha384);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!flag) {
- ret = wc_InitSha384(NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha384Free(&sha384);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_InitSha384 */
- /*
- * test wc_Sha384Update()
- */
- static int test_wc_Sha384Update(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA384
- wc_Sha384 sha384;
- byte hash[WC_SHA384_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- int flag = 0;
- ret = wc_InitSha384(&sha384);
- if (ret != 0) {
- flag = ret;
- }
- /* Input */
- if (!flag) {
- a.input = "a";
- a.inLen = XSTRLEN(a.input);
- ret = wc_Sha384Update(&sha384, NULL, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha384Update(&sha384, (byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Sha384Final(&sha384, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update input. */
- if (!flag) {
- a.input = "abc";
- a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
- "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
- "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
- "\xc8\x25\xa7";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- ret = wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Sha384Final(&sha384, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass in bad values. */
- if (!flag) {
- b.input = NULL;
- b.inLen = 0;
- ret = wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- c.input = NULL;
- c.inLen = WC_SHA384_DIGEST_SIZE;
- ret = wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha384Free(&sha384);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha384Update */
- /*
- * Unit test function for wc_Sha384Final();
- */
- static int test_wc_Sha384Final(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA384
- wc_Sha384 sha384;
- byte* hash_test[3];
- byte hash1[WC_SHA384_DIGEST_SIZE];
- byte hash2[2*WC_SHA384_DIGEST_SIZE];
- byte hash3[5*WC_SHA384_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /* Initialize */
- ret = wc_InitSha384(&sha384);
- if (ret) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test) / sizeof(byte*);
- /* Good test args. */
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_Sha384Final(&sha384, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_Sha384Final(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha384Final(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha384Final(&sha384, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha384Free(&sha384);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha384Final */
- /*
- * Unit test function for wc_Sha384GetFlags()
- */
- static int test_wc_Sha384GetFlags(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha384 sha384;
- word32 flags = 0;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha384(&sha384);
- if (flag == 0) {
- flag = wc_Sha384GetFlags(&sha384, &flags);
- }
- if (flag == 0) {
- if (flags & WC_HASH_FLAG_ISCOPY) {
- flag = 0;
- }
- }
- wc_Sha384Free(&sha384);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha384GetFlags */
- /*
- * Unit test function for wc_Sha384FinalRaw()
- */
- static int test_wc_Sha384FinalRaw(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
- !defined(WOLFSSL_NO_HASH_RAW)
- wc_Sha384 sha384;
- byte* hash_test[3];
- byte hash1[WC_SHA384_DIGEST_SIZE];
- byte hash2[2*WC_SHA384_DIGEST_SIZE];
- byte hash3[5*WC_SHA384_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /* Initialize */
- ret = wc_InitSha384(&sha384);
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test) / sizeof(byte*);
- /* Good test args. */
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_Sha384FinalRaw(&sha384, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag ) {
- ret = wc_Sha384FinalRaw(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha384FinalRaw(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha384FinalRaw(&sha384, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha384Free(&sha384);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha384FinalRaw */
- /*
- * Unit test function for wc_Sha384Free()
- */
- static int test_wc_Sha384Free(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA384
- wc_Sha384Free(NULL);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_Sha384Free */
- /*
- * Unit test function for wc_Sha384GetHash()
- */
- static int test_wc_Sha384GetHash(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA384
- wc_Sha384 sha384;
- byte hash1[WC_SHA384_DIGEST_SIZE];
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha384(&sha384);
- if (flag == 0) {
- flag = wc_Sha384GetHash(&sha384, hash1);
- }
- /*test bad arguments*/
- if (flag == 0) {
- flag = wc_Sha384GetHash(NULL, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha384GetHash(NULL, hash1);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha384GetHash(&sha384, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- wc_Sha384Free(&sha384);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha384GetHash */
- /*
- * Unit test function for wc_Sha384Copy()
- */
- static int test_wc_Sha384Copy(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA384
- wc_Sha384 sha384;
- wc_Sha384 temp;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha384(&sha384);
- if (flag == 0) {
- flag = wc_InitSha384(&temp);
- }
- if (flag == 0) {
- flag = wc_Sha384Copy(&sha384, &temp);
- }
- /*test bad arguments*/
- if (flag == 0) {
- flag = wc_Sha384Copy(NULL, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha384Copy(NULL, &temp);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha384Copy(&sha384, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- wc_Sha384Free(&sha384);
- wc_Sha384Free(&temp);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha384Copy */
- /*
- * Testing wc_InitSha224();
- */
- static int test_wc_InitSha224(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA224
- wc_Sha224 sha224;
- int ret;
- int flag = 0;
- /* Test good arg. */
- ret = wc_InitSha224(&sha224);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!flag) {
- ret = wc_InitSha224(NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha224Free(&sha224);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_InitSha224 */
- /*
- * Unit test on wc_Sha224Update
- */
- static int test_wc_Sha224Update(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA224
- wc_Sha224 sha224;
- byte hash[WC_SHA224_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- int flag = 0;
- ret = wc_InitSha224(&sha224);
- if (ret != 0) {
- flag = ret;
- }
- /* Input. */
- if (!flag) {
- a.input = "a";
- a.inLen = XSTRLEN(a.input);
- ret = wc_Sha224Update(&sha224, NULL, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha224Update(&sha224, (byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Sha224Final(&sha224, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update input. */
- if (!flag) {
- a.input = "abc";
- a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
- "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- ret = wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_Sha224Final(&sha224, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass in bad values. */
- if (!flag) {
- b.input = NULL;
- b.inLen = 0;
- ret = wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- c.input = NULL;
- c.inLen = WC_SHA224_DIGEST_SIZE;
- ret = wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha224Free(&sha224);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha224Update */
- /*
- * Unit test for wc_Sha224Final();
- */
- static int test_wc_Sha224Final(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA224
- wc_Sha224 sha224;
- byte* hash_test[3];
- byte hash1[WC_SHA224_DIGEST_SIZE];
- byte hash2[2*WC_SHA224_DIGEST_SIZE];
- byte hash3[5*WC_SHA224_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /* Initialize */
- ret = wc_InitSha224(&sha224);
- if (ret) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test) / sizeof(byte*);
- /* Good test args. */
- /* Testing oversized buffers. */
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_Sha224Final(&sha224, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_Sha224Final(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha224Final(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_Sha224Final(&sha224, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha224Free(&sha224);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha224Final */
- /*
- * Unit test function for wc_Sha224SetFlags()
- */
- static int test_wc_Sha224SetFlags(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha224 sha224;
- word32 flags = 0;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha224(&sha224);
- if (flag == 0) {
- flag = wc_Sha224SetFlags(&sha224, flags);
- }
- if (flag == 0) {
- if (flags & WC_HASH_FLAG_ISCOPY) {
- flag = 0;
- }
- }
- wc_Sha224Free(&sha224);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha224SetFlags */
- /*
- * Unit test function for wc_Sha224GetFlags()
- */
- static int test_wc_Sha224GetFlags(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha224 sha224;
- word32 flags = 0;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha224(&sha224);
- if (flag == 0) {
- flag = wc_Sha224GetFlags(&sha224, &flags);
- }
- if (flag == 0) {
- if (flags & WC_HASH_FLAG_ISCOPY) {
- flag = 0;
- }
- }
- wc_Sha224Free(&sha224);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha224GetFlags */
- /*
- * Unit test function for wc_Sha224Free()
- */
- static int test_wc_Sha224Free(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA224
- wc_Sha224Free(NULL);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_Sha224Free */
- /*
- * Unit test function for wc_Sha224GetHash()
- */
- static int test_wc_Sha224GetHash(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA224
- wc_Sha224 sha224;
- byte hash1[WC_SHA224_DIGEST_SIZE];
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha224(&sha224);
- if (flag == 0) {
- flag = wc_Sha224GetHash(&sha224, hash1);
- }
- /*test bad arguments*/
- if (flag == 0) {
- flag = wc_Sha224GetHash(NULL, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha224GetHash(NULL, hash1);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha224GetHash(&sha224, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- wc_Sha224Free(&sha224);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha224GetHash */
- /*
- * Unit test function for wc_Sha224Copy()
- */
- static int test_wc_Sha224Copy(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHA224
- wc_Sha224 sha224;
- wc_Sha224 temp;
- int flag = 0;
- /* Initialize */
- flag = wc_InitSha224(&sha224);
- if (flag == 0) {
- flag = wc_InitSha224(&temp);
- }
- if (flag == 0) {
- flag = wc_Sha224Copy(&sha224, &temp);
- }
- /*test bad arguments*/
- if (flag == 0) {
- flag = wc_Sha224Copy(NULL, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha224Copy(NULL, &temp);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- if (flag == 0) {
- flag = wc_Sha224Copy(&sha224, NULL);
- if (flag == BAD_FUNC_ARG) {
- flag = 0;
- }
- }
- wc_Sha224Free(&sha224);
- wc_Sha224Free(&temp);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha224Copy */
- /*
- * Testing wc_InitRipeMd()
- */
- static int test_wc_InitRipeMd(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_RIPEMD
- RipeMd ripemd;
- int ret;
- int flag = 0;
- /* Test good arg. */
- ret = wc_InitRipeMd(&ripemd);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad arg. */
- if (!flag) {
- ret = wc_InitRipeMd(NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_InitRipeMd */
- /*
- * Testing wc_RipeMdUpdate()
- */
- static int test_wc_RipeMdUpdate(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_RIPEMD
- RipeMd ripemd;
- byte hash[RIPEMD_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- int flag = 0;
- ret = wc_InitRipeMd(&ripemd);
- if (ret != 0) {
- flag = ret;
- }
- /* Input */
- if (!flag) {
- a.input = "a";
- a.inLen = XSTRLEN(a.input);
- ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_RipeMdFinal(&ripemd, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update input. */
- if (!flag) {
- a.input = "abc";
- a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
- "\xb0\x87\xf1\x5a\x0b\xfc";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- ret = wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_RipeMdFinal(&ripemd, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass in bad values. */
- if (!flag) {
- b.input = NULL;
- b.inLen = 0;
- ret = wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- c.input = NULL;
- c.inLen = RIPEMD_DIGEST_SIZE;
- ret = wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_RipeMdUdpate */
- /*
- * Unit test function for wc_RipeMdFinal()
- */
- static int test_wc_RipeMdFinal(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_RIPEMD
- RipeMd ripemd;
- byte* hash_test[3];
- byte hash1[RIPEMD_DIGEST_SIZE];
- byte hash2[2*RIPEMD_DIGEST_SIZE];
- byte hash3[5*RIPEMD_DIGEST_SIZE];
- int times, i, ret;
- int flag = 0;
- /* Initialize */
- ret = wc_InitRipeMd(&ripemd);
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- hash_test[0] = hash1;
- hash_test[1] = hash2;
- hash_test[2] = hash3;
- }
- times = sizeof(hash_test) / sizeof(byte*);
- /* Testing oversized buffers. */
- for (i = 0; i < times; i++) {
- if (!flag) {
- ret = wc_RipeMdFinal(&ripemd, hash_test[i]);
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_RipeMdFinal(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_RipeMdFinal(NULL, hash1);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_RipeMdFinal(&ripemd, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_RipeMdFinal */
- /*
- * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
- * wc_InitSha3_512
- */
- static int test_wc_InitSha3(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3)
- wc_Sha3 sha3;
- int ret = 0;
- (void)sha3;
- #if !defined(WOLFSSL_NOSHA3_224)
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitSha3_224(NULL, HEAP_HINT, testDevId);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha3_224_Free(&sha3);
- #endif /* NOSHA3_224 */
- #if !defined(WOLFSSL_NOSHA3_256)
- if (ret == 0) {
- ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitSha3_256(NULL, HEAP_HINT, testDevId);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha3_256_Free(&sha3);
- } /* END sha3_256 */
- #endif /* NOSHA3_256 */
- #if !defined(WOLFSSL_NOSHA3_384)
- if (ret == 0) {
- ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitSha3_384(NULL, HEAP_HINT, testDevId);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha3_384_Free(&sha3);
- } /* END sha3_384 */
- #endif /* NOSHA3_384 */
- #if !defined(WOLFSSL_NOSHA3_512)
- if (ret == 0) {
- ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitSha3_512(NULL, HEAP_HINT, testDevId);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha3_512_Free(&sha3);
- } /* END sha3_512 */
- #endif /* NOSHA3_512 */
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_InitSha3 */
- /*
- * Testing wc_Sha3_Update()
- */
- static int testing_wc_Sha3_Update(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
- !defined(WOLFSSL_AFALG_XILINX)
- wc_Sha3 sha3;
- byte msg[] = "Everybody's working for the weekend.";
- byte msg2[] = "Everybody gets Friday off.";
- byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
- "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
- "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
- "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
- "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
- word32 msglen = sizeof(msg) - 1;
- word32 msg2len = sizeof(msg2);
- word32 msgCmplen = sizeof(msgCmp);
- int ret = 0;
- #if !defined(WOLFSSL_NOSHA3_224)
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_Sha3_224_Update(&sha3, msg, msglen);
- if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
- if (ret == 0 && XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass bad args. */
- if (ret == 0) {
- ret = wc_Sha3_224_Update(NULL, msg2, msg2len);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_224_Update(&sha3, NULL, 5);
- }
- if (ret == BAD_FUNC_ARG) {
- wc_Sha3_224_Free(&sha3);
- if (wc_InitSha3_224(&sha3, HEAP_HINT, testDevId)) {
- return TEST_FAIL;
- }
- ret = wc_Sha3_224_Update(&sha3, NULL, 0);
- if (ret == 0) {
- ret = wc_Sha3_224_Update(&sha3, msg2, msg2len);
- }
- if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- wc_Sha3_224_Free(&sha3);
- #endif /* SHA3_224 */
- #if !defined(WOLFSSL_NOSHA3_256)
- if (ret == 0) {
- ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_Sha3_256_Update(&sha3, msg, msglen);
- if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
- if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass bad args. */
- if (ret == 0) {
- ret = wc_Sha3_256_Update(NULL, msg2, msg2len);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_256_Update(&sha3, NULL, 5);
- }
- if (ret == BAD_FUNC_ARG) {
- wc_Sha3_256_Free(&sha3);
- if (wc_InitSha3_256(&sha3, HEAP_HINT, testDevId)) {
- return TEST_FAIL;
- }
- ret = wc_Sha3_256_Update(&sha3, NULL, 0);
- if (ret == 0) {
- ret = wc_Sha3_256_Update(&sha3, msg2, msg2len);
- }
- if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- wc_Sha3_256_Free(&sha3);
- }
- #endif /* SHA3_256 */
- #if !defined(WOLFSSL_NOSHA3_384)
- if (ret == 0) {
- ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_Sha3_384_Update(&sha3, msg, msglen);
- if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
- if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass bad args. */
- if (ret == 0) {
- ret = wc_Sha3_384_Update(NULL, msg2, msg2len);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_384_Update(&sha3, NULL, 5);
- }
- if (ret == BAD_FUNC_ARG) {
- wc_Sha3_384_Free(&sha3);
- if (wc_InitSha3_384(&sha3, HEAP_HINT, testDevId)) {
- return TEST_FAIL;
- }
- ret = wc_Sha3_384_Update(&sha3, NULL, 0);
- if (ret == 0) {
- ret = wc_Sha3_384_Update(&sha3, msg2, msg2len);
- }
- if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- wc_Sha3_384_Free(&sha3);
- }
- #endif /* SHA3_384 */
- #if !defined(WOLFSSL_NOSHA3_512)
- if (ret == 0) {
- ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_Sha3_512_Update(&sha3, msg, msglen);
- if (XMEMCMP(msg, sha3.t, msglen) || sha3.i != msglen) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
- if (XMEMCMP(sha3.t, msgCmp, msgCmplen) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass bad args. */
- if (ret == 0) {
- ret = wc_Sha3_512_Update(NULL, msg2, msg2len);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_512_Update(&sha3, NULL, 5);
- }
- if (ret == BAD_FUNC_ARG) {
- wc_Sha3_512_Free(&sha3);
- if (wc_InitSha3_512(&sha3, HEAP_HINT, testDevId)) {
- return TEST_FAIL;
- }
- ret = wc_Sha3_512_Update(&sha3, NULL, 0);
- if (ret == 0) {
- ret = wc_Sha3_512_Update(&sha3, msg2, msg2len);
- }
- if (ret == 0 && XMEMCMP(msg2, sha3.t, msg2len) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- wc_Sha3_512_Free(&sha3);
- }
- #endif /* SHA3_512 */
- res = TEST_RES_CHECK(ret == 0);
- #endif /* WOLFSSL_SHA3 */
- return res;
- } /* END testing_wc_Sha3_Update */
- /*
- * Testing wc_Sha3_224_Final()
- */
- static int test_wc_Sha3_224_Final(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- wc_Sha3 sha3;
- const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
- "nopnopq";
- const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
- "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
- "\x64\xea\xd0\xfc\xce\x33";
- byte hash[WC_SHA3_224_DIGEST_SIZE];
- byte hashRet[WC_SHA3_224_DIGEST_SIZE];
- int ret = 0;
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
- if (ret == 0) {
- ret = wc_Sha3_224_Final(&sha3, hash);
- if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Sha3_224_Final(NULL, hash);
- if (ret == 0) {
- ret = wc_Sha3_224_Final(&sha3, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha3_224_Free(&sha3);
- if (ret == 0) {
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashRet, 0, sizeof(hashRet));
- ret= wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
- if (ret == 0) {
- ret = wc_Sha3_224_GetHash(&sha3, hashRet);
- }
- if (ret == 0) {
- ret = wc_Sha3_224_Final(&sha3, hash);
- if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- /* Test bad args. */
- ret = wc_Sha3_224_GetHash(NULL, hashRet);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_224_GetHash(&sha3, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- wc_Sha3_224_Free(&sha3);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Sha3_224_Final */
- /*
- * Testing wc_Sha3_256_Final()
- */
- static int test_wc_Sha3_256_Final(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- wc_Sha3 sha3;
- const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
- "nopnopq";
- const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
- "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
- "\xdd\x97\x49\x6d\x33\x76";
- byte hash[WC_SHA3_256_DIGEST_SIZE];
- byte hashRet[WC_SHA3_256_DIGEST_SIZE];
- int ret = 0;
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
- if (ret == 0) {
- ret = wc_Sha3_256_Final(&sha3, hash);
- if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Sha3_256_Final(NULL, hash);
- if (ret == 0) {
- ret = wc_Sha3_256_Final(&sha3, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha3_256_Free(&sha3);
- if (ret == 0) {
- ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashRet, 0, sizeof(hashRet));
- ret= wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
- if (ret == 0) {
- ret = wc_Sha3_256_GetHash(&sha3, hashRet);
- }
- if (ret == 0) {
- ret = wc_Sha3_256_Final(&sha3, hash);
- if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- /* Test bad args. */
- ret = wc_Sha3_256_GetHash(NULL, hashRet);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_256_GetHash(&sha3, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- wc_Sha3_256_Free(&sha3);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Sha3_256_Final */
- /*
- * Testing wc_Sha3_384_Final()
- */
- static int test_wc_Sha3_384_Final(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- wc_Sha3 sha3;
- const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
- "nopnopq";
- const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
- "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
- "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
- "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
- byte hash[WC_SHA3_384_DIGEST_SIZE];
- byte hashRet[WC_SHA3_384_DIGEST_SIZE];
- int ret = 0;
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
- if (ret == 0) {
- ret = wc_Sha3_384_Final(&sha3, hash);
- if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Sha3_384_Final(NULL, hash);
- if (ret == 0) {
- ret = wc_Sha3_384_Final(&sha3, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha3_384_Free(&sha3);
- if (ret == 0) {
- ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashRet, 0, sizeof(hashRet));
- ret= wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
- if (ret == 0) {
- ret = wc_Sha3_384_GetHash(&sha3, hashRet);
- }
- if (ret == 0) {
- ret = wc_Sha3_384_Final(&sha3, hash);
- if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- /* Test bad args. */
- ret = wc_Sha3_384_GetHash(NULL, hashRet);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_384_GetHash(&sha3, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- wc_Sha3_384_Free(&sha3);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Sha3_384_Final */
- /*
- * Testing wc_Sha3_512_Final()
- */
- static int test_wc_Sha3_512_Final(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
- !defined(WOLFSSL_NOSHA3_384)
- wc_Sha3 sha3;
- const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
- "nopnopq";
- const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
- "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
- "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
- "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
- "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
- byte hash[WC_SHA3_512_DIGEST_SIZE];
- byte hashRet[WC_SHA3_512_DIGEST_SIZE];
- int ret = 0;
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
- if (ret == 0) {
- ret = wc_Sha3_512_Final(&sha3, hash);
- if (ret == 0 && XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Sha3_512_Final(NULL, hash);
- if (ret == 0) {
- ret = wc_Sha3_384_Final(&sha3, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Sha3_512_Free(&sha3);
- if (ret == 0) {
- ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashRet, 0, sizeof(hashRet));
- ret= wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg));
- if (ret == 0) {
- ret = wc_Sha3_512_GetHash(&sha3, hashRet);
- }
- if (ret == 0) {
- ret = wc_Sha3_512_Final(&sha3, hash);
- if (ret == 0 && XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- /* Test bad args. */
- ret = wc_Sha3_512_GetHash(NULL, hashRet);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_512_GetHash(&sha3, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- wc_Sha3_512_Free(&sha3);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Sha3_512_Final */
- /*
- * Testing wc_Sha3_224_Copy()
- */
- static int test_wc_Sha3_224_Copy(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
- wc_Sha3 sha3, sha3Cpy;
- const char* msg = TEST_STRING;
- word32 msglen = (word32)TEST_STRING_SZ;
- byte hash[WC_SHA3_224_DIGEST_SIZE];
- byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
- int ret = 0;
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Sha3_224_Free(&sha3);
- return TEST_FAIL;
- }
- ret = wc_Sha3_224_Update(&sha3, (byte*)msg, msglen);
- if (ret == 0) {
- ret = wc_Sha3_224_Copy(&sha3Cpy, &sha3);
- if (ret == 0) {
- ret = wc_Sha3_224_Final(&sha3, hash);
- if (ret == 0) {
- ret = wc_Sha3_224_Final(&sha3Cpy, hashCpy);
- }
- }
- if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Sha3_224_Copy(NULL, &sha3);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_224_Copy(&sha3Cpy, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Sha3_224_Copy */
- /*
- * Testing wc_Sha3_256_Copy()
- */
- static int test_wc_Sha3_256_Copy(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- wc_Sha3 sha3, sha3Cpy;
- const char* msg = TEST_STRING;
- word32 msglen = (word32)TEST_STRING_SZ;
- byte hash[WC_SHA3_256_DIGEST_SIZE];
- byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
- int ret = 0;
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Sha3_256_Free(&sha3);
- return TEST_FAIL;
- }
- ret = wc_Sha3_256_Update(&sha3, (byte*)msg, msglen);
- if (ret == 0) {
- ret = wc_Sha3_256_Copy(&sha3Cpy, &sha3);
- if (ret == 0) {
- ret = wc_Sha3_256_Final(&sha3, hash);
- if (ret == 0) {
- ret = wc_Sha3_256_Final(&sha3Cpy, hashCpy);
- }
- }
- if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Sha3_256_Copy(NULL, &sha3);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_256_Copy(&sha3Cpy, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Sha3_256_Copy */
- /*
- * Testing wc_Sha3_384_Copy()
- */
- static int test_wc_Sha3_384_Copy(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
- wc_Sha3 sha3, sha3Cpy;
- const char* msg = TEST_STRING;
- word32 msglen = (word32)TEST_STRING_SZ;
- byte hash[WC_SHA3_384_DIGEST_SIZE];
- byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
- int ret = 0;
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Sha3_384_Free(&sha3);
- return TEST_FAIL;
- }
- ret = wc_Sha3_384_Update(&sha3, (byte*)msg, msglen);
- if (ret == 0) {
- ret = wc_Sha3_384_Copy(&sha3Cpy, &sha3);
- if (ret == 0) {
- ret = wc_Sha3_384_Final(&sha3, hash);
- if (ret == 0) {
- ret = wc_Sha3_384_Final(&sha3Cpy, hashCpy);
- }
- }
- if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Sha3_384_Copy(NULL, &sha3);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_384_Copy(&sha3Cpy, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Sha3_384_Copy */
- /*
- * Testing wc_Sha3_512_Copy()
- */
- static int test_wc_Sha3_512_Copy(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
- wc_Sha3 sha3, sha3Cpy;
- const char* msg = TEST_STRING;
- word32 msglen = (word32)TEST_STRING_SZ;
- byte hash[WC_SHA3_512_DIGEST_SIZE];
- byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
- int ret = 0;
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Sha3_512_Free(&sha3);
- return TEST_FAIL;
- }
- ret = wc_Sha3_512_Update(&sha3, (byte*)msg, msglen);
- if (ret == 0) {
- ret = wc_Sha3_512_Copy(&sha3Cpy, &sha3);
- if (ret == 0) {
- ret = wc_Sha3_512_Final(&sha3, hash);
- if (ret == 0) {
- ret = wc_Sha3_512_Final(&sha3Cpy, hashCpy);
- }
- }
- if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Sha3_512_Copy(NULL, &sha3);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Sha3_512_Copy(&sha3Cpy, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Sha3_512_Copy */
- /*
- * Unit test function for wc_Sha3_GetFlags()
- */
- static int test_wc_Sha3_GetFlags(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha3 sha3;
- word32 flags = 0;
- int ret = 0;
- /* Initialize */
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- if (ret == 0) {
- ret = wc_Sha3_GetFlags(&sha3, &flags);
- }
- if (ret == 0) {
- if (flags & WC_HASH_FLAG_ISCOPY) {
- ret = 0;
- }
- }
- wc_Sha3_224_Free(&sha3);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Sha3_GetFlags */
- static int test_wc_InitShake256(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHAKE256
- wc_Shake shake;
- int ret = 0;
- ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitShake256(NULL, HEAP_HINT, testDevId);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Shake256_Free(&shake);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_InitSha3 */
- static int testing_wc_Shake256_Update(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHAKE256
- wc_Shake shake;
- byte msg[] = "Everybody's working for the weekend.";
- byte msg2[] = "Everybody gets Friday off.";
- byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
- "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
- "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
- "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
- "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
- word32 msglen = sizeof(msg) - 1;
- word32 msg2len = sizeof(msg2);
- word32 msgCmplen = sizeof(msgCmp);
- int ret = 0;
- ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_Shake256_Update(&shake, msg, msglen);
- if (XMEMCMP(msg, shake.t, msglen) || shake.i != msglen) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_Shake256_Update(&shake, msg2, msg2len);
- if (XMEMCMP(shake.t, msgCmp, msgCmplen) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass bad args. */
- if (ret == 0) {
- ret = wc_Shake256_Update(NULL, msg2, msg2len);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Shake256_Update(&shake, NULL, 5);
- }
- if (ret == BAD_FUNC_ARG) {
- wc_Shake256_Free(&shake);
- if (wc_InitShake256(&shake, HEAP_HINT, testDevId)) {
- return TEST_FAIL;
- }
- ret = wc_Shake256_Update(&shake, NULL, 0);
- if (ret == 0) {
- ret = wc_Shake256_Update(&shake, msg2, msg2len);
- }
- if (ret == 0 && XMEMCMP(msg2, shake.t, msg2len) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- wc_Shake256_Free(&shake);
- res = TEST_RES_CHECK(ret == 0);
- #endif /* WOLFSSL_SHAKE256 */
- return res;
- }
- static int test_wc_Shake256_Final(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHAKE256
- wc_Shake shake;
- const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
- "nopnopq";
- const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
- "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
- "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
- "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
- "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
- "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
- "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
- "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
- "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
- byte hash[114];
- int ret = 0;
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret= wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg));
- if (ret == 0) {
- ret = wc_Shake256_Final(&shake, hash, (word32)sizeof(hash));
- if (ret == 0 && XMEMCMP(expOut, hash, (word32)sizeof(hash)) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Shake256_Final(NULL, hash, (word32)sizeof(hash));
- if (ret == 0) {
- ret = wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Shake256_Free(&shake);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }
- /*
- * Testing wc_Shake256_Copy()
- */
- static int test_wc_Shake256_Copy(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHAKE256
- wc_Shake shake, shakeCpy;
- const char* msg = TEST_STRING;
- word32 msglen = (word32)TEST_STRING_SZ;
- byte hash[144];
- byte hashCpy[144];
- word32 hashLen = sizeof(hash);
- word32 hashLenCpy = sizeof(hashCpy);
- int ret;
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Shake256_Free(&shake);
- return TEST_FAIL;
- }
- ret = wc_Shake256_Update(&shake, (byte*)msg, msglen);
- if (ret == 0) {
- ret = wc_Shake256_Copy(&shakeCpy, &shake);
- if (ret == 0) {
- ret = wc_Shake256_Final(&shake, hash, hashLen);
- if (ret == 0) {
- ret = wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy);
- }
- }
- if (ret == 0 && XMEMCMP(hash, hashCpy, sizeof(hash)) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Shake256_Copy(NULL, &shake);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Shake256_Copy(&shakeCpy, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Shake256_Free(&shake);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Shake256_Copy */
- /*
- * Unit test function for wc_Shake256Hash()
- */
- static int test_wc_Shake256Hash(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_SHAKE256
- const byte data[] = { /* Hello World */
- 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
- 0x72,0x6c,0x64
- };
- word32 len = sizeof(data);
- byte hash[144];
- word32 hashLen = sizeof(hash);
- int ret;
- ret = wc_Shake256Hash(data, len, hash, hashLen);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Shake256Hash */
- /*
- * Test function for wc_HmacSetKey
- */
- static int test_wc_Md5HmacSetKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && !defined(NO_MD5)
- Hmac hmac;
- int ret, times, itr;
- int flag = 0;
- const char* keys[]=
- {
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
- #ifndef HAVE_FIPS
- "Jefe", /* smaller than minimum FIPS key size */
- #endif
- "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
- };
- times = sizeof(keys) / sizeof(char*);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return TEST_FAIL;
- for (itr = 0; itr < times; itr++) {
- ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
- (word32)XSTRLEN(keys[itr]));
- #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
- wc_HmacFree(&hmac);
- if (ret == BAD_FUNC_ARG) {
- return TEST_SUCCESS;
- }
- else {
- return TEST_FAIL;
- }
- #else
- if (ret != 0) {
- flag = ret;
- }
- #endif
- }
- /* Bad args. */
- if (!flag) {
- ret = wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
- #ifdef HAVE_FIPS
- if (ret != HMAC_MIN_KEYLEN_E) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #else
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #endif
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Md5HmacSetKey */
- /*
- * testing wc_HmacSetKey() on wc_Sha hash.
- */
- static int test_wc_ShaHmacSetKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && !defined(NO_SHA)
- Hmac hmac;
- int ret, times, itr;
- int flag = 0;
- const char* keys[]=
- {
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
- "\x0b\x0b\x0b",
- #ifndef HAVE_FIPS
- "Jefe", /* smaller than minimum FIPS key size */
- #endif
- "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
- "\xAA\xAA\xAA"
- };
- times = sizeof(keys) / sizeof(char*);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- for (itr = 0; itr < times; itr++) {
- ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
- (word32)XSTRLEN(keys[itr]));
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Bad args. */
- if (!flag) {
- ret = wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
- #ifdef HAVE_FIPS
- if (ret != HMAC_MIN_KEYLEN_E) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #else
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #endif
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_ShaHmacSetKey() */
- /*
- * testing wc_HmacSetKey() on Sha224 hash.
- */
- static int test_wc_Sha224HmacSetKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
- Hmac hmac;
- int ret, times, itr;
- int flag = 0;
- const char* keys[]=
- {
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
- "\x0b\x0b\x0b",
- #ifndef HAVE_FIPS
- "Jefe", /* smaller than minimum FIPS key size */
- #endif
- "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
- "\xAA\xAA\xAA"
- };
- times = sizeof(keys) / sizeof(char*);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- for (itr = 0; itr < times; itr++) {
- ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
- (word32)XSTRLEN(keys[itr]));
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Bad args. */
- if (!flag) {
- ret = wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
- #ifdef HAVE_FIPS
- if (ret != HMAC_MIN_KEYLEN_E) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #else
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #endif
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha224HmacSetKey() */
- /*
- * testing wc_HmacSetKey() on Sha256 hash
- */
- static int test_wc_Sha256HmacSetKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && !defined(NO_SHA256)
- Hmac hmac;
- int ret, times, itr;
- int flag = 0;
- const char* keys[]=
- {
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
- "\x0b\x0b\x0b",
- #ifndef HAVE_FIPS
- "Jefe", /* smaller than minimum FIPS key size */
- #endif
- "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
- "\xAA\xAA\xAA"
- };
- times = sizeof(keys) / sizeof(char*);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- for (itr = 0; itr < times; itr++) {
- ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
- (word32)XSTRLEN(keys[itr]));
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Bad args. */
- if (!flag) {
- ret = wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
- #ifdef HAVE_FIPS
- if (ret != HMAC_MIN_KEYLEN_E) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #else
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #endif
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha256HmacSetKey() */
- /*
- * testing wc_HmacSetKey on Sha384 hash.
- */
- static int test_wc_Sha384HmacSetKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
- Hmac hmac;
- int ret, times, itr;
- int flag = 0;
- const char* keys[]=
- {
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
- "\x0b\x0b\x0b",
- #ifndef HAVE_FIPS
- "Jefe", /* smaller than minimum FIPS key size */
- #endif
- "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
- "\xAA\xAA\xAA"
- };
- times = sizeof(keys) / sizeof(char*);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- for (itr = 0; itr < times; itr++) {
- ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
- (word32)XSTRLEN(keys[itr]));
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Bad args. */
- if (!flag) {
- ret = wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, 20, (byte*)keys[0],
- (word32)XSTRLEN(keys[0]));
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
- #ifdef HAVE_FIPS
- if (ret != HMAC_MIN_KEYLEN_E) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #else
- if (ret != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- #endif
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha384HmacSetKey() */
- /*
- * testing wc_HmacUpdate on wc_Md5 hash.
- */
- static int test_wc_Md5HmacUpdate(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
- Hmac hmac;
- testVector a, b;
- int ret;
- int flag = 0;
- #ifdef HAVE_FIPS
- const char* keys =
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
- #else
- const char* keys = "Jefe";
- #endif
- a.input = "what do ya want for nothing?";
- a.inLen = XSTRLEN(a.input);
- b.input = "Hi There";
- b.inLen = XSTRLEN(b.input);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys, (word32)XSTRLEN(keys));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update Hmac. */
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Md5HmacUpdate */
- /*
- * testing wc_HmacUpdate on SHA hash.
- */
- static int test_wc_ShaHmacUpdate(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && !defined(NO_SHA)
- Hmac hmac;
- testVector a, b;
- int ret;
- int flag = 0;
- #ifdef HAVE_FIPS
- const char* keys =
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
- #else
- const char* keys = "Jefe";
- #endif
- a.input = "what do ya want for nothing?";
- a.inLen = XSTRLEN(a.input);
- b.input = "Hi There";
- b.inLen = XSTRLEN(b.input);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys, (word32)XSTRLEN(keys));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update Hmac. */
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_ShaHmacUpdate */
- /*
- * testing wc_HmacUpdate on SHA224 hash.
- */
- static int test_wc_Sha224HmacUpdate(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
- Hmac hmac;
- testVector a, b;
- int ret;
- int flag = 0;
- #ifdef HAVE_FIPS
- const char* keys =
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
- #else
- const char* keys = "Jefe";
- #endif
- a.input = "what do ya want for nothing?";
- a.inLen = XSTRLEN(a.input);
- b.input = "Hi There";
- b.inLen = XSTRLEN(b.input);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys, (word32)XSTRLEN(keys));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update Hmac. */
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha224HmacUpdate */
- /*
- * testing wc_HmacUpdate on SHA256 hash.
- */
- static int test_wc_Sha256HmacUpdate(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && !defined(NO_SHA256)
- Hmac hmac;
- testVector a, b;
- int ret;
- int flag = 0;
- #ifdef HAVE_FIPS
- const char* keys =
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
- #else
- const char* keys = "Jefe";
- #endif
- a.input = "what do ya want for nothing?";
- a.inLen = XSTRLEN(a.input);
- b.input = "Hi There";
- b.inLen = XSTRLEN(b.input);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys, (word32)XSTRLEN(keys));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update Hmac. */
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha256HmacUpdate */
- /*
- * testing wc_HmacUpdate on SHA384 hash.
- */
- static int test_wc_Sha384HmacUpdate(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
- Hmac hmac;
- testVector a, b;
- int ret;
- int flag = 0;
- #ifdef HAVE_FIPS
- const char* keys =
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
- #else
- const char* keys = "Jefe";
- #endif
- a.input = "what do ya want for nothing?";
- a.inLen = XSTRLEN(a.input);
- b.input = "Hi There";
- b.inLen = XSTRLEN(b.input);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys, (word32)XSTRLEN(keys));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Update Hmac. */
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- /* Test bad args. */
- if (!flag) {
- ret = wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, NULL, (word32)a.inLen);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, 0);
- if (ret != 0) {
- flag = ret;
- }
- }
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha384HmacUpdate */
- /*
- * Testing wc_HmacFinal() with MD5
- */
- static int test_wc_Md5HmacFinal(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
- Hmac hmac;
- byte hash[WC_MD5_DIGEST_SIZE];
- testVector a;
- int ret;
- const char* key;
- int flag = 0;
- key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
- a.input = "Hi There";
- a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
- "\x9d";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_HmacFinal(&hmac, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Try bad parameters. */
- if (!flag) {
- ret = wc_HmacFinal(NULL, hash);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifndef HAVE_FIPS
- if (!flag) {
- ret = wc_HmacFinal(&hmac, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Md5HmacFinal */
- /*
- * Testing wc_HmacFinal() with SHA
- */
- static int test_wc_ShaHmacFinal(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && !defined(NO_SHA)
- Hmac hmac;
- byte hash[WC_SHA_DIGEST_SIZE];
- testVector a;
- int ret;
- int flag = 0;
- const char* key;
- key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
- "\x0b\x0b\x0b";
- a.input = "Hi There";
- a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
- "\x8e\xf1\x46\xbe\x00";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_HmacFinal(&hmac, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Try bad parameters. */
- if (!flag) {
- ret = wc_HmacFinal(NULL, hash);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifndef HAVE_FIPS
- if (!flag) {
- ret = wc_HmacFinal(&hmac, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_ShaHmacFinal */
- /*
- * Testing wc_HmacFinal() with SHA224
- */
- static int test_wc_Sha224HmacFinal(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
- Hmac hmac;
- byte hash[WC_SHA224_DIGEST_SIZE];
- testVector a;
- int ret;
- int flag = 0;
- const char* key;
- key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
- "\x0b\x0b\x0b";
- a.input = "Hi There";
- a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
- "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key, (word32)XSTRLEN(key));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_HmacFinal(&hmac, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Try bad parameters. */
- if (!flag) {
- ret = wc_HmacFinal(NULL, hash);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifndef HAVE_FIPS
- if (!flag) {
- ret = wc_HmacFinal(&hmac, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha224HmacFinal */
- /*
- * Testing wc_HmacFinal() with SHA256
- */
- static int test_wc_Sha256HmacFinal(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && !defined(NO_SHA256)
- Hmac hmac;
- byte hash[WC_SHA256_DIGEST_SIZE];
- testVector a;
- int ret;
- int flag = 0;
- const char* key;
- key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
- "\x0b\x0b\x0b";
- a.input = "Hi There";
- a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
- "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
- "\xcf\xf7";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return TEST_FAIL;
- ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key, (word32)XSTRLEN(key));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_HmacFinal(&hmac, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Try bad parameters. */
- if (!flag) {
- ret = wc_HmacFinal(NULL, hash);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifndef HAVE_FIPS
- if (!flag) {
- ret = wc_HmacFinal(&hmac, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha256HmacFinal */
- /*
- * Testing wc_HmacFinal() with SHA384
- */
- static int test_wc_Sha384HmacFinal(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
- Hmac hmac;
- byte hash[WC_SHA384_DIGEST_SIZE];
- testVector a;
- int ret;
- int flag = 0;
- const char* key;
- key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
- "\x0b\x0b\x0b";
- a.input = "Hi There";
- a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
- "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
- "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
- "\xfa\x9c\xb6";
- a.inLen = XSTRLEN(a.input);
- a.outLen = XSTRLEN(a.output);
- flag = 0;
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key, (word32)XSTRLEN(key));
- if (ret != 0) {
- flag = ret;
- }
- if (!flag) {
- ret = wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- ret = wc_HmacFinal(&hmac, hash);
- if (ret != 0) {
- flag = ret;
- }
- }
- if (!flag) {
- if (XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE) != 0) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Try bad parameters. */
- if (!flag) {
- ret = wc_HmacFinal(NULL, hash);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifndef HAVE_FIPS
- if (!flag) {
- ret = wc_HmacFinal(&hmac, NULL);
- if (ret != BAD_FUNC_ARG) {
- flag = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- wc_HmacFree(&hmac);
- res = TEST_RES_CHECK(flag == 0);
- #endif
- return res;
- } /* END test_wc_Sha384HmacFinal */
- /*
- * Testing wc_InitCmac()
- */
- static int test_wc_InitCmac(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
- Cmac cmac1, cmac2, cmac3;
- /* AES 128 key. */
- byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
- "\x09\x10\x11\x12\x13\x14\x15\x16";
- /* AES 192 key. */
- byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
- "\x09\x01\x11\x12\x13\x14\x15\x16"
- "\x01\x02\x03\x04\x05\x06\x07\x08";
- /* AES 256 key. */
- byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
- "\x09\x01\x11\x12\x13\x14\x15\x16"
- "\x01\x02\x03\x04\x05\x06\x07\x08"
- "\x09\x01\x11\x12\x13\x14\x15\x16";
- word32 key1Sz = (word32)sizeof(key1) - 1;
- word32 key2Sz = (word32)sizeof(key2) - 1;
- word32 key3Sz = (word32)sizeof(key3) - 1;
- int type = WC_CMAC_AES;
- int ret = 0;
- #ifdef WOLFSSL_AES_128
- ret = wc_InitCmac(&cmac1, key1, key1Sz, type, NULL);
- #endif
- #ifdef WOLFSSL_AES_192
- if (ret == 0) {
- wc_AesFree(&cmac1.aes);
- ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
- }
- #endif
- #ifdef WOLFSSL_AES_256
- if (ret == 0) {
- wc_AesFree(&cmac2.aes);
- ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
- }
- #endif
- /* Test bad args. */
- if (ret == 0) {
- wc_AesFree(&cmac3.aes);
- ret = wc_InitCmac(NULL, key3, key3Sz, type, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_InitCmac(&cmac3, key3, 0, type, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- (void)key1;
- (void)key1Sz;
- (void)key2;
- (void)key2Sz;
- (void)cmac1;
- (void)cmac2;
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_InitCmac */
- /*
- * Testing wc_CmacUpdate()
- */
- static int test_wc_CmacUpdate(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
- Cmac cmac;
- byte key[] =
- {
- 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
- 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
- };
- byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
- "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
- "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
- "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
- "\xf4";
- word32 inSz = (word32)sizeof(in) - 1;
- word32 keySz = (word32)sizeof(key);
- int type = WC_CMAC_AES;
- int ret = 0;
- ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
- if (ret != 0) {
- return ret;
- }
- ret = wc_CmacUpdate(&cmac, in, inSz);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_CmacUpdate(NULL, in, inSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_CmacUpdate(&cmac, NULL, 30);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_AesFree(&cmac.aes);
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_CmacUpdate */
- /*
- * Testing wc_CmacFinal()
- */
- static int test_wc_CmacFinal(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
- Cmac cmac;
- byte key[] =
- {
- 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
- 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
- };
- byte msg[] =
- {
- 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
- 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
- 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
- 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
- 0xf4
- };
- /* Test vectors from CMACGenAES128.rsp from
- * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
- * Per RFC4493 truncation of lsb is possible.
- */
- byte expMac[] =
- {
- 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
- 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
- };
- byte mac[AES_BLOCK_SIZE];
- word32 msgSz = (word32)sizeof(msg);
- word32 keySz = (word32)sizeof(key);
- word32 macSz = sizeof(mac);
- word32 badMacSz = 17;
- int expMacSz = sizeof(expMac);
- int type = WC_CMAC_AES;
- int ret = 0;
- XMEMSET(mac, 0, macSz);
- ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
- if (ret != 0) {
- return ret;
- }
- ret = wc_CmacUpdate(&cmac, msg, msgSz);
- if (ret == 0) {
- ret = wc_CmacFinal(&cmac, mac, &macSz);
- if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_CmacFinal(NULL, mac, &macSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_CmacFinal(&cmac, NULL, &macSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_CmacFinal(&cmac, mac, &badMacSz);
- if (ret == BUFFER_E) {
- ret = 0;
- }
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_CmacFinal */
- /*
- * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
- */
- static int test_wc_AesCmacGenerate(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
- Cmac cmac;
- byte key[] =
- {
- 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
- 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
- };
- byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
- "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
- byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
- "\x3d\x32\x65\x4c\x66\x23\xc5";
- byte mac[AES_BLOCK_SIZE];
- word32 keySz = sizeof(key);
- word32 macSz = sizeof(mac);
- word32 msgSz = sizeof(msg) - 1;
- word32 expMacSz = sizeof(expMac) - 1;
- int type = WC_CMAC_AES;
- int ret = 0;
- XMEMSET(mac, 0, macSz);
- ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
- if (ret != 0) {
- return ret;
- }
- ret = wc_CmacUpdate(&cmac, msg, msgSz);
- if (ret != 0) {
- return ret;
- }
- else {
- wc_AesFree(&cmac.aes);
- }
- ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz);
- if (ret == 0 && XMEMCMP(mac, expMac, expMacSz) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_AesCmacGenerate */
- /*
- * Testing streaming AES-GCM API.
- */
- static int test_wc_AesGcmStream(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
- defined(WOLFSSL_AESGCM_STREAM)
- int ret = 0;
- int i;
- WC_RNG rng[1];
- Aes aesEnc[1];
- Aes aesDec[1];
- byte tag[AES_BLOCK_SIZE];
- byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
- byte out[AES_BLOCK_SIZE * 3 + 2];
- byte plain[AES_BLOCK_SIZE * 3 + 2];
- byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
- byte key[AES_128_KEY_SIZE] = { 0, };
- byte iv[AES_IV_SIZE] = { 1, };
- byte ivOut[AES_IV_SIZE];
- static const byte expTagAAD1[AES_BLOCK_SIZE] = {
- 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
- 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
- };
- static const byte expTagPlain1[AES_BLOCK_SIZE] = {
- 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
- 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
- };
- static const byte expTag[AES_BLOCK_SIZE] = {
- 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
- 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
- };
- /* Create a random for generating IV/nonce. */
- AssertIntEQ(wc_InitRng(rng), 0);
- /* Initialize data structures. */
- AssertIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
- AssertIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
- /* BadParameters to streaming init. */
- AssertIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
- BAD_FUNC_ARG);
- /* Bad parameters to encrypt update. */
- AssertIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
- BAD_FUNC_ARG);
- /* Bad parameters to decrypt update. */
- AssertIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
- BAD_FUNC_ARG);
- /* Bad parameters to encrypt final. */
- AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
- BAD_FUNC_ARG);
- /* Bad parameters to decrypt final. */
- AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
- BAD_FUNC_ARG);
- /* Check calling final before setting key fails. */
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
- AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
- /* Check calling update before setting key else fails. */
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
- MISSING_KEY);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
- MISSING_KEY);
- /* Set key but not IV. */
- AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
- AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
- /* Check calling final before setting IV fails. */
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
- AssertIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
- /* Check calling update before setting IV else fails. */
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
- MISSING_IV);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
- MISSING_IV);
- /* Set IV using fixed part IV and external IV APIs. */
- AssertIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
- rng), 0);
- AssertIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
- GCM_NONCE_MID_SZ), 0);
- AssertIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
- AssertIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
- /* Encrypt and decrypt data. */
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
- AssertIntEQ(XMEMCMP(plain, in, 1), 0);
- /* Finalize and check tag matches. */
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
- AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
- /* Set key and IV through streaming init API. */
- AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
- AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
- /* Encrypt/decrypt one block and AAD of one block. */
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
- AES_BLOCK_SIZE), 0);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
- AES_BLOCK_SIZE), 0);
- AssertIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
- /* Finalize and check tag matches. */
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
- AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
- /* Set key and IV through streaming init API. */
- AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
- AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
- /* No data to encrypt/decrypt one byte of AAD. */
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
- /* Finalize and check tag matches. */
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
- AssertIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
- AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
- /* Set key and IV through streaming init API. */
- AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
- AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
- /* Encrypt/decrypt one byte and no AAD. */
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
- AssertIntEQ(XMEMCMP(plain, in, 1), 0);
- /* Finalize and check tag matches. */
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
- AssertIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
- AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
- /* Set key and IV through streaming init API. */
- AssertIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
- AssertIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
- /* Encryption AES is one byte at a time */
- for (i = 0; i < (int)sizeof(aad); i++) {
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
- 0);
- }
- for (i = 0; i < (int)sizeof(in); i++) {
- AssertIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
- 0);
- }
- /* Decryption AES is two bytes at a time */
- for (i = 0; i < (int)sizeof(aad); i += 2) {
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
- 0);
- }
- for (i = 0; i < (int)sizeof(aad); i += 2) {
- AssertIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
- 0), 0);
- }
- AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
- /* Finalize and check tag matches. */
- AssertIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
- AssertIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
- AssertIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
- /* Check streaming encryption can be decrypted with one shot. */
- AssertIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
- AssertIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
- AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
- AssertIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
- wc_AesFree(aesEnc);
- wc_AesFree(aesDec);
- wc_FreeRng(rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_AesGcmStream */
- /*
- * unit test for wc_Des3_SetIV()
- */
- static int test_wc_Des3_SetIV(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_DES3
- Des3 des;
- int ret = 0;
- const byte key[] =
- {
- 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
- 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
- 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
- };
- const byte iv[] =
- {
- 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
- };
- ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- /* DES_ENCRYPTION or DES_DECRYPTION */
- ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
- if (ret == 0) {
- if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
- /* Test explicitly wc_Des3_SetIV() */
- if (ret == 0) {
- ret = wc_Des3_SetIV(NULL, iv);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_SetIV(&des, NULL);
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- wc_Des3Free(&des);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Des3_SetIV */
- /*
- * unit test for wc_Des3_SetKey()
- */
- static int test_wc_Des3_SetKey(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_DES3
- Des3 des;
- int ret = 0;
- const byte key[] =
- {
- 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
- 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
- 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
- };
- const byte iv[] =
- {
- 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
- };
- ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- /* DES_ENCRYPTION or DES_DECRYPTION */
- ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
- if (ret == 0) {
- if (XMEMCMP(iv, des.reg, DES_BLOCK_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_SetKey(&des, key, iv, -1);
- }
- if (ret == BAD_FUNC_ARG) {
- /* Default case. Should return 0. */
- ret = wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION);
- }
- } /* END if ret != 0 */
- wc_Des3Free(&des);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Des3_SetKey */
- /*
- * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
- */
- static int test_wc_Des3_CbcEncryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_DES3
- Des3 des;
- int ret = 0;
- byte cipher[24];
- byte plain[24];
- const byte key[] =
- {
- 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
- 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
- 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
- };
- const byte iv[] =
- {
- 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
- };
- const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
- 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
- 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
- 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
- };
- ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
- if (ret == 0) {
- ret = wc_Des3_CbcEncrypt(&des, cipher, vector, 24);
- if (ret == 0) {
- ret = wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION);
- }
- if (ret == 0) {
- ret = wc_Des3_CbcDecrypt(&des, plain, cipher, 24);
- }
- }
- if (ret == 0) {
- if (XMEMCMP(plain, vector, 24) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_Des3_CbcEncrypt(NULL, cipher, vector, 24);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcEncrypt(&des, NULL, vector, 24);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector));
- }
- if (ret != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Des3_CbcDecrypt(NULL, plain, cipher, 24);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcDecrypt(&des, NULL, cipher, 24);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcDecrypt(&des, plain, NULL, 24);
- }
- if (ret != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- wc_Des3Free(&des);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END wc_Des3_CbcEncrypt */
- /*
- * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
- */
- static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_DES3
- int ret = 0;
- word32 vectorSz, cipherSz;
- byte cipher[24];
- byte plain[24];
- byte vector[] = /* Now is the time for all w/o trailing 0 */
- {
- 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
- 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
- 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
- };
- byte key[] =
- {
- 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
- 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
- 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
- };
- byte iv[] =
- {
- 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
- };
- vectorSz = sizeof(byte) * 24;
- cipherSz = sizeof(byte) * 24;
- ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv);
- if (ret == 0) {
- ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv);
- if (ret == 0) {
- if (XMEMCMP(plain, vector, 24) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* pass in bad args. */
- if (ret == 0) {
- ret = wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz,
- key, NULL);
- }
- else {
- /* Return code catch. */
- ret = WOLFSSL_FAILURE;
- }
- }
- if (ret == 0) {
- ret = wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL);
- }
- else {
- ret = WOLFSSL_FAILURE;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
- /*
- * Unit test for wc_Des3_EcbEncrypt
- */
- static int test_wc_Des3_EcbEncrypt(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
- Des3 des;
- int ret = 0;
- byte cipher[24];
- word32 cipherSz = sizeof(cipher);
- const byte key[] =
- {
- 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
- 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
- 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
- };
- const byte iv[] =
- {
- 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
- 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
- 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
- };
- const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
- 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
- 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
- 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
- };
- ret = wc_Des3Init(&des, NULL, INVALID_DEVID);
- if (ret != 0) {
- return ret;
- }
- if (ret == 0 ) {
- ret = wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION);
- }
- /* Bad Cases */
- if (ret == 0) {
- ret = wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Des3_EcbEncrypt(&des, cipher, vector, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Des3_EcbEncrypt(NULL, 0, NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Good Cases */
- if (ret == 0) {
- ret = wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz);
- }
- wc_Des3Free(&des);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Des3_EcbEncrypt */
- /*
- * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
- */
- static int test_wc_Chacha_SetKey(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_CHACHA
- ChaCha ctx;
- const byte key[] =
- {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
- };
- byte cipher[128];
- int ret = 0;
- ret = wc_Chacha_SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Chacha_SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Chacha_SetKey(&ctx, key, 18);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_Chacha_SetIV(&ctx, cipher, 0);
- }
- if (ret == 0) {
- /* Test bad args. */
- ret = wc_Chacha_SetIV(NULL, cipher, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FAILURE;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Chacha_SetKey */
- /*
- * unit test for wc_Poly1305SetKey()
- */
- static int test_wc_Poly1305SetKey(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_POLY1305
- Poly1305 ctx;
- const byte key[] =
- {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
- };
- int ret = 0;
- ret = wc_Poly1305SetKey(&ctx, key, (word32)(sizeof(key)/sizeof(byte)));
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Poly1305SetKey(NULL, key, (word32)(sizeof(key)/sizeof(byte)));
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Poly1305SetKey(&ctx, NULL, (word32)(sizeof(key)/sizeof(byte)));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Poly1305SetKey(&ctx, key, 18);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Poly1305_SetKey() */
- /*
- * Testing wc_Chacha_Process()
- */
- static int test_wc_Chacha_Process(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_CHACHA
- ChaCha enc, dec;
- byte cipher[128];
- byte plain[128];
- const byte key[] =
- {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
- };
- const char* input = "Everybody gets Friday off.";
- word32 keySz = sizeof(key)/sizeof(byte);
- unsigned long int inlen = XSTRLEN(input);
- int ret = 0;
- /*Initialize stack varialbes.*/
- XMEMSET(cipher, 0, 128);
- XMEMSET(plain, 0, 128);
- ret = wc_Chacha_SetKey(&enc, key, keySz);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_SetKey(&dec, key, keySz);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_SetIV(&enc, cipher, 0);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_SetIV(&dec, cipher, 0);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_Process(&dec, plain, cipher, (word32)inlen);
- AssertIntEQ(ret, 0);
- ret = XMEMCMP(input, plain, (int)inlen);
- AssertIntEQ(ret, 0);
- #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
- /* test checking and using leftovers, currently just in C code */
- ret = wc_Chacha_SetIV(&enc, cipher, 0);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_SetIV(&dec, cipher, 0);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen - 2);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_Process(&enc, cipher + (inlen - 2),
- (byte*)input + (inlen - 2), 2);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_Process(&dec, plain, (byte*)cipher, (word32)inlen - 2);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_Process(&dec, cipher + (inlen - 2),
- (byte*)input + (inlen - 2), 2);
- AssertIntEQ(ret, 0);
- ret = XMEMCMP(input, plain, (int)inlen);
- AssertIntEQ(ret, 0);
- /* check edge cases with counter increment */
- {
- /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
- const byte expected[] = {
- 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
- 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
- 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
- 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
- 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
- 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
- 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
- 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
- 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
- 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
- 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
- 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
- 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
- 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
- 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
- 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
- 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
- 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
- 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
- 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
- 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
- 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
- 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
- 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
- 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
- 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
- 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
- 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
- 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
- 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
- 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
- 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
- };
- const byte iv2[] = {
- 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
- 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
- };
- byte input2[256];
- int i;
- for (i = 0; i < 256; i++)
- input2[i] = i;
- ret = wc_Chacha_SetIV(&enc, iv2, 0);
- AssertIntEQ(ret, 0);
- ret = wc_Chacha_Process(&enc, cipher, input2, 64);
- AssertIntEQ(ret, 0);
- AssertIntEQ(XMEMCMP(expected, cipher, 64), 0);
- ret = wc_Chacha_Process(&enc, cipher, input2 + 64, 128);
- AssertIntEQ(ret, 0);
- AssertIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
- /* partial */
- ret = wc_Chacha_Process(&enc, cipher, input2 + 192, 32);
- AssertIntEQ(ret, 0);
- AssertIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
- ret = wc_Chacha_Process(&enc, cipher, input2 + 224, 32);
- AssertIntEQ(ret, 0);
- AssertIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
- }
- #endif
- /* Test bad args. */
- ret = wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Chacha_Process */
- /*
- * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
- */
- static int test_wc_ChaCha20Poly1305_aead(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- const byte key[] = {
- 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
- 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
- 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
- 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
- };
- const byte plaintext[] = {
- 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
- 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
- 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
- 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
- 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
- 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
- 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
- 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
- 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
- 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
- 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
- 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
- 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
- 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
- 0x74, 0x2e
- };
- const byte iv[] = {
- 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
- 0x44, 0x45, 0x46, 0x47
- };
- const byte aad[] = { /* additional data */
- 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
- 0xc4, 0xc5, 0xc6, 0xc7
- };
- const byte cipher[] = { /* expected output from operation */
- 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
- 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
- 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
- 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
- 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
- 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
- 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
- 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
- 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
- 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
- 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
- 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
- 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
- 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
- 0x61, 0x16
- };
- const byte authTag[] = { /* expected output from operation */
- 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
- 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
- };
- byte generatedCiphertext[272];
- byte generatedPlaintext[272];
- byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
- int ret = 0;
- /* Initialize stack variables. */
- XMEMSET(generatedCiphertext, 0, 272);
- XMEMSET(generatedPlaintext, 0, 272);
- /* Test Encrypt */
- ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), plaintext,
- sizeof(plaintext), generatedCiphertext, generatedAuthTag);
- AssertIntEQ(ret, 0);
- ret = XMEMCMP(generatedCiphertext, cipher, sizeof(cipher)/sizeof(byte));
- AssertIntEQ(ret, 0);
- /* Test bad args. */
- ret = wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad), plaintext,
- sizeof(plaintext), generatedCiphertext, generatedAuthTag);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
- plaintext, sizeof(plaintext),
- generatedCiphertext, generatedAuthTag);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
- sizeof(plaintext), generatedCiphertext, generatedAuthTag);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
- NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
- plaintext, sizeof(plaintext), NULL, generatedAuthTag);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
- plaintext, sizeof(plaintext), generatedCiphertext, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- (void)ret; /* suppress never read */
- }
- ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
- sizeof(cipher), authTag, generatedPlaintext);
- AssertIntEQ(ret, 0);
- ret = XMEMCMP(generatedPlaintext, plaintext,
- sizeof(plaintext)/sizeof(byte));
- AssertIntEQ(ret, 0);
- /* Test bad args. */
- ret = wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
- sizeof(cipher), authTag, generatedPlaintext);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
- cipher, sizeof(cipher), authTag, generatedPlaintext);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
- sizeof(cipher), authTag, generatedPlaintext);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
- sizeof(cipher), NULL, generatedPlaintext);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
- sizeof(cipher), authTag, NULL);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- ret = wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
- sizeof(cipher), authTag, generatedPlaintext);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
- /*
- * Testing function for wc_Rc2SetKey().
- */
- static int test_wc_Rc2SetKey(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WC_RC2
- Rc2 rc2;
- byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
- byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
- int ret = 0;
- /* valid key and IV */
- ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
- iv, 40);
- if (ret == 0) {
- /* valid key, no IV */
- ret = wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
- NULL, 40);
- }
- /* bad arguments */
- if (ret == 0) {
- /* null Rc2 struct */
- ret = wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
- iv, 40);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* null key */
- ret = wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
- iv, 40);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* key size == 0 */
- ret = wc_Rc2SetKey(&rc2, key40, 0, iv, 40);
- if (ret == WC_KEY_SIZE_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* key size > 128 */
- ret = wc_Rc2SetKey(&rc2, key40, 129, iv, 40);
- if (ret == WC_KEY_SIZE_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* effective bits == 0 */
- ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
- iv, 0);
- if (ret == WC_KEY_SIZE_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* effective bits > 1024 */
- ret = wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
- iv, 1025);
- if (ret == WC_KEY_SIZE_E) {
- ret = 0;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Rc2SetKey */
- /*
- * Testing function for wc_Rc2SetIV().
- */
- static int test_wc_Rc2SetIV(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WC_RC2
- Rc2 rc2;
- byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
- int ret = 0;
- /* valid IV */
- ret = wc_Rc2SetIV(&rc2, iv);
- if (ret == 0) {
- /* valid NULL IV */
- ret = wc_Rc2SetIV(&rc2, NULL);
- }
- /* bad arguments */
- if (ret == 0) {
- ret = wc_Rc2SetIV(NULL, iv);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Rc2SetKey */
- /*
- * Testing function for wc_Rc2EcbEncrypt().
- */
- static int test_wc_Rc2EcbEncryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WC_RC2
- Rc2 rc2;
- int ret = 0;
- int effectiveKeyBits = 63;
- byte cipher[RC2_BLOCK_SIZE];
- byte plain[RC2_BLOCK_SIZE];
- byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
- byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
- XMEMSET(cipher, 0, sizeof(cipher));
- XMEMSET(plain, 0, sizeof(plain));
- ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
- NULL, effectiveKeyBits);
- if (ret == 0) {
- ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE);
- if (ret != 0 || XMEMCMP(cipher, output, RC2_BLOCK_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE);
- if (ret != 0 || XMEMCMP(plain, input, RC2_BLOCK_SIZE) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Rc2EcbEncrypt bad arguments */
- if (ret == 0) {
- /* null Rc2 struct */
- ret = wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* null out buffer */
- ret = wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* null input buffer */
- ret = wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* output buffer sz != RC2_BLOCK_SIZE (8) */
- ret = wc_Rc2EcbEncrypt(&rc2, cipher, input, 7);
- if (ret == BUFFER_E) {
- ret = 0;
- }
- }
- /* Rc2EcbDecrypt bad arguments */
- if (ret == 0) {
- /* null Rc2 struct */
- ret = wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* null out buffer */
- ret = wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* null input buffer */
- ret = wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* output buffer sz != RC2_BLOCK_SIZE (8) */
- ret = wc_Rc2EcbDecrypt(&rc2, plain, output, 7);
- if (ret == BUFFER_E) {
- ret = 0;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Rc2SetKey */
- /*
- * Testing function for wc_Rc2CbcEncrypt().
- */
- static int test_wc_Rc2CbcEncryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WC_RC2
- Rc2 rc2;
- int ret = 0;
- int effectiveKeyBits = 63;
- byte cipher[RC2_BLOCK_SIZE*2];
- byte plain[RC2_BLOCK_SIZE*2];
- /* vector taken from test.c */
- byte key[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- byte iv[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- byte input[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
- };
- byte output[] = {
- 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
- 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
- };
- XMEMSET(cipher, 0, sizeof(cipher));
- XMEMSET(plain, 0, sizeof(plain));
- ret = wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
- iv, effectiveKeyBits);
- if (ret == 0) {
- ret = wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input));
- if (ret != 0 || XMEMCMP(cipher, output, sizeof(output)) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- /* reset IV for decrypt */
- ret = wc_Rc2SetIV(&rc2, iv);
- }
- if (ret == 0) {
- ret = wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher));
- if (ret != 0 || XMEMCMP(plain, input, sizeof(input)) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Rc2CbcEncrypt bad arguments */
- if (ret == 0) {
- /* null Rc2 struct */
- ret = wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input));
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* null out buffer */
- ret = wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input));
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* null input buffer */
- ret = wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input));
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Rc2CbcDecrypt bad arguments */
- if (ret == 0) {
- /* in size is 0 */
- ret = wc_Rc2CbcDecrypt(&rc2, plain, output, 0);
- if (ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- /* null Rc2 struct */
- ret = wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output));
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* null out buffer */
- ret = wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output));
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* null input buffer */
- ret = wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output));
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Rc2SetKey */
- /*
- * Testing function for wc_AesSetIV
- */
- static int test_wc_AesSetIV(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
- Aes aes;
- int ret = 0;
- byte key16[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- byte iv1[] = "1234567890abcdef";
- byte iv2[] = "0987654321fedcba";
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
- iv1, AES_ENCRYPTION);
- if (ret == 0) {
- ret = wc_AesSetIV(&aes, iv2);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_AesSetIV(NULL, iv1);
- if (ret == BAD_FUNC_ARG) {
- /* NULL iv should return 0. */
- ret = wc_AesSetIV(&aes, NULL);
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_AesFree(&aes);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* test_wc_AesSetIV */
- /*
- * Testing function for wc_AesSetKey().
- */
- static int test_wc_AesSetKey(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_AES
- Aes aes;
- int ret = 0;
- byte key16[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- #ifdef WOLFSSL_AES_192
- byte key24[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
- };
- #endif
- #ifdef WOLFSSL_AES_256
- byte key32[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- #endif
- byte badKey16[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
- };
- byte iv[] = "1234567890abcdef";
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- #ifdef WOLFSSL_AES_128
- ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
- iv, AES_ENCRYPTION);
- #endif
- #ifdef WOLFSSL_AES_192
- if (ret == 0) {
- ret = wc_AesSetKey (&aes, key24, (word32) sizeof(key24) / sizeof(byte),
- iv, AES_ENCRYPTION);
- }
- #endif
- #ifdef WOLFSSL_AES_256
- if (ret == 0) {
- ret = wc_AesSetKey (&aes, key32, (word32) sizeof(key32) / sizeof(byte),
- iv, AES_ENCRYPTION);
- }
- #endif
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_AesSetKey (NULL, key16, (word32) sizeof(key16) / sizeof(byte),
- iv, AES_ENCRYPTION);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesSetKey(&aes, badKey16,
- (word32) sizeof(badKey16) / sizeof(byte),
- iv, AES_ENCRYPTION);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_AesFree(&aes);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_AesSetKey */
- /*
- * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
- * and wc_AesCbcDecryptWithKey()
- */
- static int test_wc_AesCbcEncryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
- defined(WOLFSSL_AES_256)
- Aes aes;
- int ret = 0;
- byte key32[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- byte vector[] = /* Now is the time for all good men w/o trailing 0 */
- {
- 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
- 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
- 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20,
- 0x67,0x6f,0x6f,0x64,0x20,0x6d,0x65,0x6e
- };
- byte iv[] = "1234567890abcdef";
- byte enc[sizeof(vector)];
- byte dec[sizeof(vector)];
- int cbcE = WOLFSSL_FATAL_ERROR;
- int cbcD = WOLFSSL_FATAL_ERROR;
- int cbcDWK = WOLFSSL_FATAL_ERROR;
- byte dec2[sizeof(vector)];
- /* Init stack variables. */
- XMEMSET(enc, 0, sizeof(enc));
- XMEMSET(dec, 0, sizeof(vector));
- XMEMSET(dec2, 0, sizeof(vector));
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv, AES_ENCRYPTION);
- if (ret == 0) {
- ret = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector));
- if (ret == 0) {
- /* Re init for decrypt and set flag. */
- cbcE = 0;
- wc_AesFree(&aes);
- ret = wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2,
- iv, AES_DECRYPTION);
- }
- if (ret == 0) {
- ret = wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector));
- if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector)) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- /* Set flag. */
- cbcD = 0;
- }
- }
- }
- /* If encrypt succeeds but cbc decrypt fails, we can still test. */
- if (ret == 0 || cbcE == 0) {
- ret = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
- key32, sizeof(key32)/sizeof(byte), iv);
- if (ret == 0 || XMEMCMP(vector, dec2, AES_BLOCK_SIZE) == 0) {
- cbcDWK = 0;
- }
- }
- /* Pass in bad args */
- if (cbcE == 0) {
- cbcE = wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector));
- if (cbcE == BAD_FUNC_ARG) {
- cbcE = wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector));
- }
- if (cbcE == BAD_FUNC_ARG) {
- cbcE = wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector));
- }
- if (cbcE == BAD_FUNC_ARG) {
- cbcE = 0;
- }
- else {
- cbcE = WOLFSSL_FATAL_ERROR;
- }
- #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
- if (cbcE == 0) {
- cbcE = wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1);
- }
- if (cbcE == BAD_LENGTH_E) {
- cbcE = 0;
- }
- else {
- cbcE = WOLFSSL_FATAL_ERROR;
- }
- #endif
- }
- if (cbcE == 0) {
- #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
- (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
- fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
- "mode (v2), skip test");
- #else
- /* Test passing in size of 0 */
- XMEMSET(enc, 0, sizeof(enc));
- cbcE = wc_AesCbcEncrypt(&aes, enc, vector, 0);
- if (cbcE == 0) {
- /* Check enc was not modified */
- int i;
- for (i = 0; i < (int)sizeof(enc); i++)
- cbcE |= enc[i];
- }
- #endif
- }
- if (cbcE != 0) {
- wc_AesFree(&aes);
- return TEST_FAIL;
- }
- if (cbcD == 0) {
- cbcD = wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE);
- if (cbcD == BAD_FUNC_ARG) {
- cbcD = wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE);
- }
- if (cbcD == BAD_FUNC_ARG) {
- cbcD = wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE);
- }
- if (cbcD == BAD_FUNC_ARG) {
- cbcD = wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1);
- }
- #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
- if (cbcD == BAD_LENGTH_E) {
- cbcD = 0;
- }
- else {
- cbcD = WOLFSSL_FATAL_ERROR;
- }
- #else
- if (cbcD == BAD_FUNC_ARG) {
- cbcD = 0;
- }
- else {
- cbcD = WOLFSSL_FATAL_ERROR;
- }
- #endif
- }
- if (cbcD == 0) {
- /* Test passing in size of 0 */
- XMEMSET(dec, 0, sizeof(dec));
- cbcD = wc_AesCbcDecrypt(&aes, dec, enc, 0);
- if (cbcD == 0) {
- /* Check dec was not modified */
- int i;
- for (i = 0; i < (int)sizeof(dec); i++)
- cbcD |= dec[i];
- }
- }
- if (cbcD != 0) {
- wc_AesFree(&aes);
- return TEST_FAIL;
- }
- if (cbcDWK == 0) {
- cbcDWK = wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
- key32, sizeof(key32)/sizeof(byte), iv);
- if (cbcDWK == BAD_FUNC_ARG) {
- cbcDWK = wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
- key32, sizeof(key32)/sizeof(byte), iv);
- }
- if (cbcDWK == BAD_FUNC_ARG) {
- cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
- NULL, sizeof(key32)/sizeof(byte), iv);
- }
- if (cbcDWK == BAD_FUNC_ARG) {
- cbcDWK = wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
- key32, sizeof(key32)/sizeof(byte), NULL);
- }
- if (cbcDWK == BAD_FUNC_ARG) {
- cbcDWK = 0;
- }
- else {
- cbcDWK = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_AesFree(&aes);
- res = TEST_RES_CHECK(cbcDWK == 0);
- #endif
- return res;
- } /* END test_wc_AesCbcEncryptDecrypt */
- /*
- * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
- */
- static int test_wc_AesCtrEncryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
- Aes aesEnc, aesDec;
- int ret = 0;
- byte key32[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- byte vector[] = /* Now is the time for all w/o trailing 0 */
- {
- 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
- 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
- 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
- };
- byte iv[] = "1234567890abcdef";
- byte enc[AES_BLOCK_SIZE * 2];
- byte dec[AES_BLOCK_SIZE * 2];
- /* Init stack variables. */
- XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
- XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
- ret = wc_AesInit(&aesEnc, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_AesInit(&aesDec, NULL, INVALID_DEVID);
- if (ret != 0) {
- wc_AesFree(&aesEnc);
- return ret;
- }
- ret = wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2,
- iv, AES_ENCRYPTION);
- if (ret == 0) {
- ret = wc_AesCtrEncrypt(&aesEnc, enc, vector,
- sizeof(vector)/sizeof(byte));
- if (ret == 0) {
- /* Decrypt with wc_AesCtrEncrypt() */
- ret = wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2,
- iv, AES_ENCRYPTION);
- }
- if (ret == 0) {
- ret = wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte));
- if (ret != 0 || XMEMCMP(vector, dec, sizeof(vector))) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte));
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_AesFree(&aesEnc);
- wc_AesFree(&aesDec);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_AesCtrEncryptDecrypt */
- /*
- * test function for wc_AesGcmSetKey()
- */
- static int test_wc_AesGcmSetKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(HAVE_AESGCM)
- Aes aes;
- int ret = 0;
- #ifdef WOLFSSL_AES_128
- byte key16[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- #endif
- #ifdef WOLFSSL_AES_192
- byte key24[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
- };
- #endif
- #ifdef WOLFSSL_AES_256
- byte key32[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- #endif
- byte badKey16[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
- };
- byte badKey24[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
- };
- byte badKey32[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
- };
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- #ifdef WOLFSSL_AES_128
- ret = wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte));
- #endif
- #ifdef WOLFSSL_AES_192
- if (ret == 0) {
- ret = wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte));
- }
- #endif
- #ifdef WOLFSSL_AES_256
- if (ret == 0) {
- ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
- }
- #endif
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte));
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_AesFree(&aes);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_AesGcmSetKey */
- /*
- * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
- */
- static int test_wc_AesGcmEncryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- /* WOLFSSL_AFALG requires 12 byte IV */
- #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
- !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
- Aes aes;
- byte key32[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- byte vector[] = /* Now is the time for all w/o trailing 0 */
- {
- 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
- 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
- 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
- };
- const byte a[] =
- {
- 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
- 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
- 0xab, 0xad, 0xda, 0xd2
- };
- byte iv[] = "1234567890a";
- byte longIV[] = "1234567890abcdefghij";
- byte enc[sizeof(vector)];
- byte resultT[AES_BLOCK_SIZE];
- byte dec[sizeof(vector)];
- int gcmD = WOLFSSL_FATAL_ERROR;
- int gcmE = WOLFSSL_FATAL_ERROR;
- int ret = 0;
- /* Init stack variables. */
- XMEMSET(enc, 0, sizeof(vector));
- XMEMSET(dec, 0, sizeof(vector));
- XMEMSET(resultT, 0, AES_BLOCK_SIZE);
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte));
- if (ret == 0) {
- gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector),
- iv, sizeof(iv)/sizeof(byte), resultT,
- sizeof(resultT), a, sizeof(a));
- }
- if (gcmE == 0) { /* If encrypt fails, no decrypt. */
- gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector),
- iv, sizeof(iv)/sizeof(byte), resultT,
- sizeof(resultT), a, sizeof(a));
- if (gcmD == 0 && (XMEMCMP(vector, dec, sizeof(vector)) != 0)) {
- gcmD = WOLFSSL_FATAL_ERROR;
- }
- }
- /*Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
- if (gcmE == 0) {
- gcmE = wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector),
- iv, sizeof(iv)/sizeof(byte), resultT, sizeof(resultT),
- a, sizeof(a));
- if (gcmE == BAD_FUNC_ARG) {
- gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
- sizeof(vector), iv, sizeof(iv)/sizeof(byte),
- resultT, sizeof(resultT) + 1, a, sizeof(a));
- }
- if (gcmE == BAD_FUNC_ARG) {
- gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
- sizeof(vector), iv, sizeof(iv)/sizeof(byte),
- resultT, sizeof(resultT) - 5, a, sizeof(a));
- }
- #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
- (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
- defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
- /* FIPS does not check the lower bound of ivSz */
- #else
- if (gcmE == BAD_FUNC_ARG) {
- gcmE = wc_AesGcmEncrypt(&aes, enc, vector,
- sizeof(vector), iv, 0,
- resultT, sizeof(resultT), a, sizeof(a));
- }
- #endif
- if (gcmE == BAD_FUNC_ARG) {
- gcmE = 0;
- }
- else {
- gcmE = WOLFSSL_FATAL_ERROR;
- }
- }
- /* This case is now considered good. Long IVs are now allowed.
- * Except for the original FIPS release, it still has an upper
- * bound on the IV length. */
- #if (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
- !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
- if (gcmE == 0) {
- gcmE = wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
- sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT),
- a, sizeof(a));
- }
- #else
- (void)longIV;
- #endif /* Old FIPS */
- /* END wc_AesGcmEncrypt */
- if (gcmE != 0) {
- wc_AesFree(&aes);
- return TEST_FAIL;
- }
- #ifdef HAVE_AES_DECRYPT
- if (gcmD == 0) {
- gcmD = wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte),
- iv, sizeof(iv)/sizeof(byte), resultT,
- sizeof(resultT), a, sizeof(a));
- if (gcmD == BAD_FUNC_ARG) {
- gcmD = wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte),
- iv, sizeof(iv)/sizeof(byte), resultT,
- sizeof(resultT), a, sizeof(a));
- }
- if (gcmD == BAD_FUNC_ARG) {
- gcmD = wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte),
- iv, sizeof(iv)/sizeof(byte), resultT,
- sizeof(resultT), a, sizeof(a));
- }
- if (gcmD == BAD_FUNC_ARG) {
- gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
- NULL, sizeof(iv)/sizeof(byte), resultT,
- sizeof(resultT), a, sizeof(a));
- }
- if (gcmD == BAD_FUNC_ARG) {
- gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
- iv, sizeof(iv)/sizeof(byte), NULL,
- sizeof(resultT), a, sizeof(a));
- }
- if (gcmD == BAD_FUNC_ARG) {
- gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
- iv, sizeof(iv)/sizeof(byte), resultT,
- sizeof(resultT) + 1, a, sizeof(a));
- }
- #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
- (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
- !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
- /* FIPS does not check the lower bound of ivSz */
- #else
- if (gcmD == BAD_FUNC_ARG) {
- gcmD = wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
- iv, 0, resultT,
- sizeof(resultT), a, sizeof(a));
- }
- #endif
- if (gcmD == BAD_FUNC_ARG) {
- gcmD = 0;
- }
- else {
- gcmD = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(gcmD == 0);
- } /* END wc_AesGcmDecrypt */
- #endif /* HAVE_AES_DECRYPT */
- wc_AesFree(&aes);
- #endif
- return res;
- } /* END test_wc_AesGcmEncryptDecrypt */
- /*
- * unit test for wc_GmacSetKey()
- */
- static int test_wc_GmacSetKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(HAVE_AESGCM)
- Gmac gmac;
- byte key16[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- #ifdef WOLFSSL_AES_192
- byte key24[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
- };
- #endif
- #ifdef WOLFSSL_AES_256
- byte key32[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- #endif
- byte badKey16[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
- };
- byte badKey24[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
- };
- byte badKey32[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- int ret = 0;
- ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- #ifdef WOLFSSL_AES_128
- ret = wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte));
- #endif
- #ifdef WOLFSSL_AES_192
- if (ret == 0) {
- ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
- }
- #endif
- #ifdef WOLFSSL_AES_256
- if (ret == 0) {
- ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
- }
- #endif
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte));
- if (ret == BAD_FUNC_ARG) {
- ret = wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte));
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_AesFree(&gmac.aes);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_GmacSetKey */
- /*
- * unit test for wc_GmacUpdate
- */
- static int test_wc_GmacUpdate(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(HAVE_AESGCM)
- Gmac gmac;
- #ifdef WOLFSSL_AES_128
- const byte key16[] =
- {
- 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
- 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
- };
- #endif
- #ifdef WOLFSSL_AES_192
- byte key24[] =
- {
- 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
- 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
- 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
- };
- #endif
- #ifdef WOLFSSL_AES_256
- byte key32[] =
- {
- 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
- 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
- 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
- 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
- };
- #endif
- #ifdef WOLFSSL_AES_128
- const byte authIn[] =
- {
- 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
- 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
- };
- #endif
- #ifdef WOLFSSL_AES_192
- const byte authIn2[] =
- {
- 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
- 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
- };
- #endif
- const byte authIn3[] =
- {
- 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
- 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
- };
- #ifdef WOLFSSL_AES_128
- const byte tag1[] = /* Known. */
- {
- 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
- 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
- };
- #endif
- #ifdef WOLFSSL_AES_192
- const byte tag2[] = /* Known */
- {
- 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
- 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
- };
- #endif
- const byte tag3[] = /* Known */
- {
- 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
- 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
- };
- #ifdef WOLFSSL_AES_128
- const byte iv[] =
- {
- 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
- 0xe2, 0x8c, 0x8f, 0x16
- };
- #endif
- #ifdef WOLFSSL_AES_192
- const byte iv2[] =
- {
- 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
- 0x7e, 0x1a, 0x6f, 0xbc
- };
- #endif
- const byte iv3[] =
- {
- 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
- 0xc3, 0xfb, 0x6c, 0x8a
- };
- byte tagOut[16];
- byte tagOut2[24];
- byte tagOut3[32];
- int ret = 0;
- /* Init stack variables. */
- XMEMSET(tagOut, 0, sizeof(tagOut));
- XMEMSET(tagOut2, 0, sizeof(tagOut2));
- XMEMSET(tagOut3, 0, sizeof(tagOut3));
- ret = wc_AesInit(&gmac.aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- #ifdef WOLFSSL_AES_128
- ret = wc_GmacSetKey(&gmac, key16, sizeof(key16));
- if (ret == 0) {
- ret = wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
- tagOut, sizeof(tag1));
- if (ret == 0) {
- ret = XMEMCMP(tag1, tagOut, sizeof(tag1));
- }
- wc_AesFree(&gmac.aes);
- }
- #endif
- #ifdef WOLFSSL_AES_192
- if (ret == 0) {
- XMEMSET(&gmac, 0, sizeof(Gmac));
- ret = wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte));
- }
- if (ret == 0) {
- ret = wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2,
- sizeof(authIn2), tagOut2, sizeof(tag2));
- }
- if (ret == 0) {
- ret = XMEMCMP(tagOut2, tag2, sizeof(tag2));
- wc_AesFree(&gmac.aes);
- }
- #endif
- #ifdef WOLFSSL_AES_256
- if (ret == 0) {
- XMEMSET(&gmac, 0, sizeof(Gmac));
- ret = wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte));
- }
- if (ret == 0) {
- ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
- sizeof(authIn3), tagOut3, sizeof(tag3));
- }
- if (ret == 0) {
- ret = XMEMCMP(tag3, tagOut3, sizeof(tag3));
- }
- #endif
- /*Pass bad args. */
- if (ret == 0) {
- ret = wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3,
- sizeof(authIn3), tagOut3, sizeof(tag3));
- if (ret == BAD_FUNC_ARG) {
- ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
- sizeof(authIn3), tagOut3, sizeof(tag3) - 5);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3,
- sizeof(authIn3), tagOut3, sizeof(tag3) + 1);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_AesFree(&gmac.aes);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_GmacUpdate */
- /*
- * testing wc_CamelliaSetKey
- */
- static int test_wc_CamelliaSetKey(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_CAMELLIA
- Camellia camellia;
- /*128-bit key*/
- static const byte key16[] =
- {
- 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
- 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
- };
- /* 192-bit key */
- static const byte key24[] =
- {
- 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
- 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
- 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
- };
- /* 256-bit key */
- static const byte key32[] =
- {
- 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
- 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
- 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
- 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
- };
- static const byte iv[] =
- {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
- };
- int ret = 0;
- ret = wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv);
- if (ret == 0) {
- ret = wc_CamelliaSetKey(&camellia, key16,
- (word32)sizeof(key16), NULL);
- if (ret == 0) {
- ret = wc_CamelliaSetKey(&camellia, key24,
- (word32)sizeof(key24), iv);
- }
- if (ret == 0) {
- ret = wc_CamelliaSetKey(&camellia, key24,
- (word32)sizeof(key24), NULL);
- }
- if (ret == 0) {
- ret = wc_CamelliaSetKey(&camellia, key32,
- (word32)sizeof(key32), iv);
- }
- if (ret == 0) {
- ret = wc_CamelliaSetKey(&camellia, key32,
- (word32)sizeof(key32), NULL);
- }
- }
- /* Bad args. */
- if (ret == 0) {
- ret = wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv);
- if (ret != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- } /* END bad args. */
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_CammeliaSetKey */
- /*
- * Testing wc_CamelliaSetIV()
- */
- static int test_wc_CamelliaSetIV(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_CAMELLIA
- Camellia camellia;
- static const byte iv[] =
- {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
- };
- int ret = 0;
- ret = wc_CamelliaSetIV(&camellia, iv);
- if (ret == 0) {
- ret = wc_CamelliaSetIV(&camellia, NULL);
- }
- /* Bad args. */
- if (ret == 0) {
- ret = wc_CamelliaSetIV(NULL, NULL);
- if (ret != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_CamelliaSetIV*/
- /*
- * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
- */
- static int test_wc_CamelliaEncryptDecryptDirect(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_CAMELLIA
- Camellia camellia;
- static const byte key24[] =
- {
- 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
- 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
- 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
- };
- static const byte iv[] =
- {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
- };
- static const byte plainT[] =
- {
- 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
- 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
- };
- byte enc[sizeof(plainT)];
- byte dec[sizeof(enc)];
- int camE = WOLFSSL_FATAL_ERROR;
- int camD = WOLFSSL_FATAL_ERROR;
- int ret = 0;
- /*Init stack variables.*/
- XMEMSET(enc, 0, 16);
- XMEMSET(enc, 0, 16);
- ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv);
- if (ret == 0) {
- ret = wc_CamelliaEncryptDirect(&camellia, enc, plainT);
- if (ret == 0) {
- ret = wc_CamelliaDecryptDirect(&camellia, dec, enc);
- if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Pass bad args. */
- if (ret == 0) {
- camE = wc_CamelliaEncryptDirect(NULL, enc, plainT);
- if (camE == BAD_FUNC_ARG) {
- camE = wc_CamelliaEncryptDirect(&camellia, NULL, plainT);
- }
- if (camE == BAD_FUNC_ARG) {
- camE = wc_CamelliaEncryptDirect(&camellia, enc, NULL);
- }
- if (camE == BAD_FUNC_ARG) {
- camE = 0;
- }
- else {
- camE = WOLFSSL_FATAL_ERROR;
- }
- }
- if (camE != 0) {
- return TEST_FAIL;
- }
- if (ret == 0) {
- camD = wc_CamelliaDecryptDirect(NULL, dec, enc);
- if (camD == BAD_FUNC_ARG) {
- camD = wc_CamelliaDecryptDirect(&camellia, NULL, enc);
- }
- if (camD == BAD_FUNC_ARG) {
- camD = wc_CamelliaDecryptDirect(&camellia, dec, NULL);
- }
- if (camD == BAD_FUNC_ARG) {
- camD = 0;
- }
- else {
- camD = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(camD == 0);
- #endif
- return res;
- } /* END test-wc_CamelliaEncryptDecryptDirect */
- /*
- * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
- */
- static int test_wc_CamelliaCbcEncryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_CAMELLIA
- Camellia camellia;
- static const byte key24[] =
- {
- 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
- 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
- 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
- };
- static const byte plainT[] =
- {
- 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
- 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
- };
- byte enc[CAMELLIA_BLOCK_SIZE];
- byte dec[CAMELLIA_BLOCK_SIZE];
- int camCbcE = WOLFSSL_FATAL_ERROR;
- int camCbcD = WOLFSSL_FATAL_ERROR;
- int ret = 0;
- /* Init stack variables. */
- XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
- XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
- ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
- if (ret == 0) {
- ret = wc_CamelliaCbcEncrypt(&camellia, enc, plainT, CAMELLIA_BLOCK_SIZE);
- if (ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), NULL);
- if (ret == 0) {
- ret = wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE);
- if (XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Pass in bad args. */
- if (ret == 0) {
- camCbcE = wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE);
- if (camCbcE == BAD_FUNC_ARG) {
- camCbcE = wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
- CAMELLIA_BLOCK_SIZE);
- }
- if (camCbcE == BAD_FUNC_ARG) {
- camCbcE = wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
- CAMELLIA_BLOCK_SIZE);
- }
- if (camCbcE == BAD_FUNC_ARG) {
- camCbcE = 0;
- }
- else {
- camCbcE = WOLFSSL_FATAL_ERROR;
- }
- }
- if (camCbcE != 0) {
- return TEST_FAIL;
- }
- if (ret == 0) {
- camCbcD = wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE);
- if (camCbcD == BAD_FUNC_ARG) {
- camCbcD = wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
- CAMELLIA_BLOCK_SIZE);
- }
- if (camCbcD == BAD_FUNC_ARG) {
- camCbcD = wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
- CAMELLIA_BLOCK_SIZE);
- }
- if (camCbcD == BAD_FUNC_ARG) {
- camCbcD = 0;
- }
- else {
- camCbcD = WOLFSSL_FATAL_ERROR;
- }
- } /* END bad args. */
- res = TEST_RES_CHECK(camCbcD == 0);
- #endif
- return res;
- } /* END test_wc_CamelliaCbcEncryptDecrypt */
- /*
- * Testing wc_Arc4SetKey()
- */
- static int test_wc_Arc4SetKey(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_RC4
- Arc4 arc;
- const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
- int keyLen = 8;
- int ret = 0;
- ret = wc_Arc4SetKey(&arc, (byte*)key, keyLen);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Arc4SetKey(NULL, (byte*)key, keyLen);
- if (ret == BAD_FUNC_ARG)
- ret = wc_Arc4SetKey(&arc, NULL, keyLen); /* NULL key */
- if (ret == BAD_FUNC_ARG)
- ret = wc_Arc4SetKey(&arc, (byte*)key, 0); /* length == 0 */
- if (ret == BAD_FUNC_ARG)
- ret = WOLFSSL_ERROR_NONE;
- else
- ret = WOLFSSL_FATAL_ERROR;
- } /* END test bad args. */
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_Arc4SetKey */
- /*
- * Testing wc_Arc4Process for ENC/DEC.
- */
- static int test_wc_Arc4Process(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_RC4
- Arc4 enc, dec;
- const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
- int keyLen = 8;
- const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
- byte cipher[8];
- byte plain[8];
- int ret;
- /* Init stack variables */
- XMEMSET(cipher, 0, sizeof(cipher));
- XMEMSET(plain, 0, sizeof(plain));
- /* Use for async. */
- ret = wc_Arc4Init(&enc, NULL, INVALID_DEVID);
- if (ret == 0) {
- ret = wc_Arc4Init(&dec, NULL, INVALID_DEVID);
- }
- if (ret == 0) {
- ret = wc_Arc4SetKey(&enc, (byte*)key, keyLen);
- }
- if (ret == 0) {
- ret = wc_Arc4SetKey(&dec, (byte*)key, keyLen);
- }
- if (ret == 0) {
- ret = wc_Arc4Process(&enc, cipher, (byte*)input, keyLen);
- }
- if (ret == 0) {
- ret = wc_Arc4Process(&dec, plain, cipher, keyLen);
- if (ret != 0 || XMEMCMP(plain, input, keyLen)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- /* Bad args. */
- if (ret == 0) {
- ret = wc_Arc4Process(NULL, plain, cipher, keyLen);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Arc4Process(&dec, NULL, cipher, keyLen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Arc4Process(&dec, plain, NULL, keyLen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_Arc4Free(&enc);
- wc_Arc4Free(&dec);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* END test_wc_Arc4Process */
- /*
- * Testing wc_Init RsaKey()
- */
- static int test_wc_InitRsaKey(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_RSA
- RsaKey key;
- int ret = 0;
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitRsaKey(NULL, HEAP_HINT);
- #ifndef HAVE_USER_RSA
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- #else
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- #endif
- ret = WOLFSSL_FATAL_ERROR;
- }
- } /* end if */
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_InitRsaKey */
- /*
- * Testing wc_RsaPrivateKeyDecode()
- */
- static int test_wc_RsaPrivateKeyDecode(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
- || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
- RsaKey key;
- byte* tmp;
- word32 idx = 0;
- int bytes = 0;
- int ret = 0;
- tmp = (byte*)XMALLOC(FOURK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp == NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- }
- if (ret == 0) {
- #ifdef USE_CERT_BUFFERS_1024
- XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
- bytes = sizeof_client_key_der_1024;
- #else
- XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
- bytes = sizeof_client_key_der_2048;
- #endif /* Use cert buffers. */
- ret = wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes);
- }
- #ifndef HAVE_USER_RSA
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- /* Test bad args. User RSA. */
- if (ret == 0) {
- ret = wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes);
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- if (tmp != NULL) {
- XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaPrivateKeyDecode */
- /*
- * Testing wc_RsaPublicKeyDecode()
- */
- static int test_wc_RsaPublicKeyDecode(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
- || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
- RsaKey keyPub;
- byte* tmp;
- word32 idx = 0;
- int bytes = 0;
- word32 keySz = 0;
- word32 tstKeySz = 0;
- int ret = 0;
- #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
- XFILE f;
- const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
- const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
- byte buf[4096];
- #endif
- tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp == NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_InitRsaKey(&keyPub, HEAP_HINT);
- }
- if (ret == 0) {
- #ifdef USE_CERT_BUFFERS_1024
- XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
- bytes = sizeof_client_keypub_der_1024;
- keySz = 1024;
- #else
- XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
- bytes = sizeof_client_keypub_der_2048;
- keySz = 2048;
- #endif
- ret = wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes);
- }
- #ifndef HAVE_USER_RSA
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes);
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- if (wc_FreeRsaKey(&keyPub) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- /* Test for getting modulus key size */
- idx = 0;
- ret = wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
- &tstKeySz, NULL, NULL);
- ret = (ret == 0 && tstKeySz == keySz/8) ? 0 : WOLFSSL_FATAL_ERROR;
- }
- #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
- f = XFOPEN(rsaPssPubKey, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- idx = 0;
- AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
- NULL), 0);
- f = XFOPEN(rsaPssPubKeyNoParams, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- idx = 0;
- AssertIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
- NULL), 0);
- #endif
- if (tmp != NULL) {
- XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaPublicKeyDecode */
- /*
- * Testing wc_RsaPublicKeyDecodeRaw()
- */
- static int test_wc_RsaPublicKeyDecodeRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA)
- RsaKey key;
- const byte n = 0x23;
- const byte e = 0x03;
- int nSz = sizeof(n);
- int eSz = sizeof(e);
- int ret;
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key);
- }
- #ifndef HAVE_USER_RSA
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- /* Pass in bad args. User RSA. */
- if (ret == 0) {
- ret = wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key);
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaPublicKeyDecodeRaw */
- #if (!defined(NO_RSA) || !defined(HAVE_FAST_RSA)) && defined(WOLFSSL_KEY_GEN)
- /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
- * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
- * trying until it gets a probable prime. */
- #ifdef HAVE_FIPS
- static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
- {
- int ret;
- for (;;) {
- ret = wc_MakeRsaKey(key, size, e, rng);
- if (ret != PRIME_GEN_E) break;
- fprintf(stderr, "MakeRsaKey couldn't find prime; "
- "trying again.\n");
- }
- return ret;
- }
- #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
- #else
- #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
- #endif
- #endif
- /*
- * Testing wc_MakeRsaKey()
- */
- static int test_wc_MakeRsaKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey genKey;
- WC_RNG rng;
- #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
- int bits = 1024;
- #else
- int bits = 2048;
- #endif
- int ret = 0;
- ret = wc_InitRsaKey(&genKey, HEAP_HINT);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
- if (ret == 0 && wc_FreeRsaKey(&genKey) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- #ifndef HAVE_USER_RSA
- /* Test bad args. */
- if (ret == 0) {
- ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
- if (ret == BAD_FUNC_ARG) {
- ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- /* e < 3 */
- ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
- }
- if (ret == BAD_FUNC_ARG) {
- /* e & 1 == 0 */
- ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- /* Test bad args. */
- if (ret == 0) {
- ret = MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng);
- if (ret == USER_CRYPTO_ERROR) {
- ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL);
- }
- if (ret == USER_CRYPTO_ERROR) {
- /* e < 3 */
- ret = MAKE_RSA_KEY(&genKey, bits, 2, &rng);
- }
- if (ret == USER_CRYPTO_ERROR) {
- /* e & 1 == 0 */
- ret = MAKE_RSA_KEY(&genKey, bits, 6, &rng);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- if (wc_FreeRng(&rng) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_MakeRsaKey */
- /*
- * Test the bounds checking on the cipher text versus the key modulus.
- * 1. Make a new RSA key.
- * 2. Set c to 1.
- * 3. Decrypt c into k. (error)
- * 4. Copy the key modulus to c and sub 1 from the copy.
- * 5. Decrypt c into k. (error)
- * Valid bounds test cases are covered by all the other RSA tests.
- */
- static int test_RsaDecryptBoundsCheck(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
- (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
- defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
- WC_RNG rng;
- RsaKey key;
- byte flatC[256];
- word32 flatCSz;
- byte out[256];
- word32 outSz = sizeof(out);
- int ret;
- XMEMSET(&rng, 0, sizeof(rng));
- ret = wc_InitRng(&rng);
- if (ret == 0)
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- const byte* derKey;
- word32 derKeySz;
- word32 idx = 0;
- #ifdef USE_CERT_BUFFERS_1024
- derKey = server_key_der_1024;
- derKeySz = (word32)sizeof_server_key_der_1024;
- flatCSz = 128;
- #else
- derKey = server_key_der_2048;
- derKeySz = (word32)sizeof_server_key_der_2048;
- flatCSz = 256;
- #endif
- ret = wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz);
- }
- if (ret == 0) {
- XMEMSET(flatC, 0, flatCSz);
- flatC[flatCSz-1] = 1;
- ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
- RSA_PRIVATE_DECRYPT, &rng);
- if (ret == RSA_OUT_OF_RANGE_E) {
- mp_int c;
- mp_init_copy(&c, &key.n);
- mp_sub_d(&c, 1, &c);
- mp_to_unsigned_bin(&c, flatC);
- ret = wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
- RSA_PRIVATE_DECRYPT, NULL);
- mp_clear(&c);
- }
- if (ret == RSA_OUT_OF_RANGE_E)
- ret = 0;
- else
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRsaKey(&key) || wc_FreeRng(&rng) || ret != 0)
- ret = WOLFSSL_FATAL_ERROR;
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaDecryptBoundsCheck */
- /*
- * Testing wc_SetKeyUsage()
- */
- static int test_wc_SetKeyUsage(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
- Cert myCert;
- int ret = 0;
- ret = wc_InitCert(&myCert);
- if (ret == 0) {
- ret = wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement");
- if (ret == 0) {
- ret = wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation");
- }
- if (ret == 0) {
- ret = wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly");
- }
- if (ret == 0) {
- ret = wc_SetKeyUsage(&myCert, "decipherOnly");
- }
- if (ret == 0) {
- ret = wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign");
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_SetKeyUsage(NULL, "decipherOnly");
- if (ret == BAD_FUNC_ARG) {
- ret = wc_SetKeyUsage(&myCert, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_SetKeyUsage(&myCert, "");
- }
- if (ret == KEYUSAGE_E) {
- ret = wc_SetKeyUsage(&myCert, ",");
- }
- if (ret == KEYUSAGE_E) {
- ret = wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign");
- }
- if (ret == KEYUSAGE_E) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_SetKeyUsage */
- /*
- * Testing wc_CheckProbablePrime()
- */
- static int test_wc_CheckProbablePrime(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
- !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
- #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
- RsaKey key;
- WC_RNG rng;
- byte e[3];
- word32 eSz = (word32)sizeof(e);
- byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
- word32 nSz = (word32)sizeof(n);
- byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
- word32 dSz = (word32)sizeof(d);
- byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
- word32 pSz = (word32)sizeof(p);
- byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
- word32 qSz = (word32)sizeof(q);
- int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
- int ret = 0;
- int* isPrime;
- int test[5];
- isPrime = test;
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_RsaSetRNG(&key, &rng);
- }
- if (ret == 0) {
- ret = wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS, WC_RSA_EXPONENT, &rng);
- }
- if (ret == 0) {
- PRIVATE_KEY_UNLOCK();
- ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
- p, &pSz, q, &qSz);
- PRIVATE_KEY_LOCK();
- }
- /* Bad cases */
- if (ret == 0) {
- ret = wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz,
- nlen, isPrime);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_CheckProbablePrime(p, 0, q, qSz, e, eSz,
- nlen, isPrime);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz,
- nlen, isPrime);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_CheckProbablePrime(p, pSz, q, 0, e, eSz,
- nlen, isPrime);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz,
- nlen, isPrime);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, 0,
- nlen, isPrime);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0,
- nlen, isPrime);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Good case */
- if (ret == 0) {
- ret = wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz,
- nlen, isPrime);
- }
- wc_FreeRsaKey(&key);
- wc_FreeRng(&rng);
- #undef CHECK_PROBABLE_PRIME_KEY_BITS
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_CheckProbablePrime */
- /*
- * Testing wc_RsaPSS_Verify()
- */
- static int test_wc_RsaPSS_Verify(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
- !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
- RsaKey key;
- WC_RNG rng;
- int sz = 256;
- byte* pt;
- const char* szMessage = "This is the string to be signed";
- unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
- unsigned char pDecrypted[2048/8];
- word32 outLen = sizeof(pDecrypted);
- int ret = 0;
- pt = pDecrypted;
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_RsaSetRNG(&key, &rng);
- }
- if (ret == 0) {
- ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
- }
- if (ret == 0) {
- ret = wc_RsaPSS_Sign((byte*)szMessage, (word32)XSTRLEN(szMessage)+1,
- pSignature, sizeof(pSignature),
- WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
- if (ret > 0) {
- sz = ret;
- ret = 0;
- }
- }
- /* Bad cases */
- if (ret == 0) {
- ret = wc_RsaPSS_Verify(NULL, sz, pt, outLen,
- WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
- WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
- WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
- WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Good case */
- if (ret == 0) {
- ret = wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
- WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret > 0) {
- ret = 0;
- }
- }
- wc_FreeRsaKey(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaPSS_Verify */
- /*
- * Testing wc_RsaPSS_VerifyCheck()
- */
- static int test_wc_RsaPSS_VerifyCheck(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
- !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
- RsaKey key;
- WC_RNG rng;
- int sz = 256; /* 2048/8 */
- byte* pt;
- byte digest[32];
- word32 digestSz = sizeof(digest);
- unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
- word32 pSignatureSz = sizeof(pSignature);
- unsigned char pDecrypted[2048/8];
- word32 outLen = sizeof(pDecrypted);
- int ret = 0;
- pt = pDecrypted;
- XMEMSET(digest, 0, sizeof(digest));
- XMEMSET(pSignature, 0, sizeof(pSignature));
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_RsaSetRNG(&key, &rng);
- }
- if (ret == 0) {
- ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
- }
- if (ret == 0) {
- digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
- ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
- }
- if (ret == 0) {
- ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
- WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
- if (ret > 0) {
- sz = ret;
- ret = 0;
- }
- }
- /* Bad cases */
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen,
- digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen,
- digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen,
- digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen,
- digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Good case */
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen,
- digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret > 0) {
- ret = 0;
- }
- }
- wc_FreeRsaKey(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaPSS_VerifyCheck */
- /*
- * Testing wc_RsaPSS_VerifyCheckInline()
- */
- static int test_wc_RsaPSS_VerifyCheckInline(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
- !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
- RsaKey key;
- WC_RNG rng;
- int sz = 256;
- byte* pt;
- byte digest[32];
- word32 digestSz = sizeof(digest);
- unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
- unsigned char pDecrypted[2048/8];
- int ret;
- pt = pDecrypted;
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- XMEMSET(digest, 0, sizeof(digest));
- XMEMSET(pSignature, 0, sizeof(pSignature));
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_RsaSetRNG(&key, &rng);
- }
- if (ret == 0) {
- ret = wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng);
- }
- if (ret == 0) {
- digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256);
- ret = wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz);
- }
- if (ret == 0) {
- ret = wc_RsaPSS_Sign(digest, digestSz, pSignature, sizeof(pSignature),
- WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng);
- if (ret > 0) {
- sz = ret;
- ret = 0;
- }
- }
- /* Bad Cases */
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt,
- digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL,
- digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt,
- digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
- digest, digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Good case */
- if (ret == 0) {
- ret = wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt,
- digest, digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key);
- if (ret > 0) {
- ret = 0;
- }
- }
- wc_FreeRsaKey(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaPSS_VerifyCheckInline */
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- static void sample_mutex_cb (int flag, int type, const char* file, int line)
- {
- (void)flag;
- (void)type;
- (void)file;
- (void)line;
- }
- #endif
- /*
- * Testing wc_LockMutex_ex
- */
- static int test_wc_LockMutex_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- int ret = 0;
- int flag = CRYPTO_LOCK;
- int type = 0;
- const char* file = "./test-LockMutex_ex.txt";
- int line = 0;
- /* without SetMutexCb */
- ret = wc_LockMutex_ex(flag, type, file, line);
- if (ret == BAD_STATE_E) {
- ret = 0;
- }
- /* with SetMutexCb */
- if (ret == 0) {
- ret = wc_SetMutexCb(sample_mutex_cb);
- if (ret == 0) {
- ret = wc_LockMutex_ex(flag, type, file, line);
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/*End test_wc_LockMutex_ex*/
- /*
- * Testing wc_SetMutexCb
- */
- static int test_wc_SetMutexCb(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- int ret = wc_SetMutexCb(sample_mutex_cb);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/*End test_wc_SetMutexCb*/
- /*
- * Testing wc_RsaKeyToDer()
- */
- static int test_wc_RsaKeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey genKey;
- WC_RNG rng;
- byte* der;
- int ret = 0;
- #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
- int bits = 1024;
- word32 derSz = 611;
- /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
- + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
- #else
- int bits = 2048;
- word32 derSz = 1196;
- /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
- + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
- #endif
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&genKey, 0, sizeof(genKey));
- der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (der == NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Init structures. */
- if (ret == 0) {
- ret = wc_InitRsaKey(&genKey, HEAP_HINT);
- }
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- /* Make key. */
- if (ret == 0) {
- ret = MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng);
- if (ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_RsaKeyToDer(&genKey, der, derSz);
- if (ret > 0) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifndef HAVE_USER_RSA
- /* Pass good/bad args. */
- if (ret == 0) {
- ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
- if (ret == BAD_FUNC_ARG) {
- /* Get just the output length */
- ret = wc_RsaKeyToDer(&genKey, NULL, 0);
- }
- if (ret > 0) {
- /* Try Public Key. */
- genKey.type = 0;
- ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
- #ifdef WOLFSSL_CHECK_MEM_ZERO
- /* Put back to Private Key */
- genKey.type = 1;
- #endif
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- /* Pass good/bad args. */
- if (ret == 0) {
- ret = wc_RsaKeyToDer(NULL, der, FOURK_BUF);
- if (ret == USER_CRYPTO_ERROR) {
- /* Get just the output length */
- ret = wc_RsaKeyToDer(&genKey, NULL, 0);
- }
- if (ret > 0) {
- /* Try Public Key. */
- genKey.type = 0;
- ret = wc_RsaKeyToDer(&genKey, der, FOURK_BUF);
- #ifdef WOLFSSL_CHECK_MEM_ZERO
- /* Put back to Private Key */
- genKey.type = 1;
- #endif
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- if (der != NULL) {
- XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- if (wc_FreeRsaKey(&genKey) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaKeyToDer */
- /*
- * Testing wc_RsaKeyToPublicDer()
- */
- static int test_wc_RsaKeyToPublicDer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- byte* der;
- int ret = 0;
- #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
- int bits = 1024;
- word32 derLen = 162;
- #else
- int bits = 2048;
- word32 derLen = 294;
- #endif
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (der == NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- }
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
- }
- if (ret == 0) {
- /* test getting size only */
- ret = wc_RsaKeyToPublicDer(&key, NULL, derLen);
- if (ret >= 0)
- ret = 0;
- }
- if (ret == 0) {
- ret = wc_RsaKeyToPublicDer(&key, der, derLen);
- if (ret >= 0) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- /* test getting size only */
- ret = wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0);
- if (ret >= 0)
- ret = 0;
- }
- if (ret == 0) {
- ret = wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0);
- if (ret >= 0) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifndef HAVE_USER_RSA
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaKeyToPublicDer(&key, der, -1);
- }
- if (ret == BUFFER_E || ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_RsaKeyToPublicDer(NULL, der, derLen);
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaKeyToPublicDer(&key, der, -1);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- if (der != NULL) {
- XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaKeyToPublicDer */
- /*
- * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
- */
- static int test_wc_RsaPublicEncryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- int ret = 0;
- const char inStr[] = TEST_STRING;
- const word32 plainLen = (word32)TEST_STRING_SZ;
- const word32 inLen = (word32)TEST_STRING_SZ;
- int bits = TEST_RSA_BITS;
- const word32 cipherLen = TEST_RSA_BYTES;
- word32 cipherLenResult = cipherLen;
- WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
- WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
- WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
- #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
- if (in == NULL || plain == NULL || cipher == NULL) {
- fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt malloc failed\n");
- return MEMORY_E;
- }
- #endif
- XMEMCPY(in, inStr, inLen);
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
- }
- /* Encrypt. */
- if (ret == 0) {
- ret = wc_RsaPublicEncrypt(in, inLen, cipher, cipherLen, &key, &rng);
- if (ret >= 0) {
- cipherLenResult = ret;
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass bad args. */
- /* Tests PsaPublicEncryptEx() which, is tested by another fn. No need dup.*/
- if (ret != 0) {
- return TEST_FAIL;
- }
- /* Decrypt */
- #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
- /* Bind rng */
- if (ret == 0) {
- ret = wc_RsaSetRNG(&key, &rng);
- }
- #endif
- if (ret == 0) {
- ret = wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen, &key);
- }
- if (ret >= 0) {
- ret = XMEMCMP(plain, inStr, plainLen);
- }
- /* Pass in bad args. */
- /* Tests RsaPrivateDecryptEx() which, is tested by another fn. No need dup.*/
- WC_FREE_VAR(in, NULL);
- WC_FREE_VAR(plain, NULL);
- WC_FREE_VAR(cipher, NULL);
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaPublicEncryptDecrypt */
- /*
- * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
- */
- static int test_wc_RsaPublicEncryptDecrypt_ex(void)
- {
- int result = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
- && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
- && !defined(NO_SHA256)
- RsaKey key;
- WC_RNG rng;
- int ret;
- const char inStr[] = TEST_STRING;
- const word32 inLen = (word32)TEST_STRING_SZ;
- const word32 plainSz = (word32)TEST_STRING_SZ;
- byte* res = NULL;
- int idx = 0;
- int bits = TEST_RSA_BITS;
- const word32 cipherSz = TEST_RSA_BYTES;
- WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
- WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
- WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
- #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
- if (in == NULL || plain == NULL || cipher == NULL) {
- fprintf(stderr, "test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
- return TEST_FAIL;
- }
- #endif
- XMEMCPY(in, inStr, inLen);
- /* Initialize stack structures. */
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
- }
- /* Encrypt */
- if (ret == 0) {
- ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
- WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0);
- if (ret >= 0) {
- idx = ret;
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass bad args. */
- /* Tests RsaPublicEncryptEx again. No need duplicate. */
- if (ret != 0) {
- return TEST_FAIL;
- }
- #ifndef WOLFSSL_RSA_PUBLIC_ONLY
- /* Decrypt */
- #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
- if (ret == 0) {
- ret = wc_RsaSetRNG(&key, &rng);
- }
- #endif
- if (ret == 0) {
- ret = wc_RsaPrivateDecrypt_ex(cipher, (word32)idx,
- plain, plainSz, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
- WC_MGF1SHA256, NULL, 0);
- }
- if (ret >= 0) {
- if (!XMEMCMP(plain, inStr, plainSz)) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /*Pass bad args.*/
- /* Tests RsaPrivateDecryptEx() again. No need duplicate. */
- if (ret != 0) {
- return TEST_FAIL;
- }
- if (ret == 0) {
- ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
- &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256,
- WC_MGF1SHA256, NULL, 0);
- if (ret >= 0) {
- if (!XMEMCMP(inStr, res, plainSz)) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- #endif
- WC_FREE_VAR(in, NULL);
- WC_FREE_VAR(plain, NULL);
- WC_FREE_VAR(cipher, NULL);
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- result = TEST_RES_CHECK(ret == 0);
- #endif
- return result;
- } /* END test_wc_RsaPublicEncryptDecrypt_ex */
- /*
- * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
- */
- static int test_wc_RsaSSL_SignVerify(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- int ret = 0;
- const char inStr[] = TEST_STRING;
- const word32 plainSz = (word32)TEST_STRING_SZ;
- const word32 inLen = (word32)TEST_STRING_SZ;
- word32 idx = 0;
- int bits = TEST_RSA_BITS;
- const word32 outSz = TEST_RSA_BYTES;
- WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
- WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
- WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
- #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
- if (in == NULL || out == NULL || plain == NULL) {
- fprintf(stderr, "test_wc_RsaSSL_SignVerify failed\n");
- return TEST_FAIL;
- }
- #endif
- XMEMCPY(in, inStr, inLen);
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
- }
- /* Sign. */
- if (ret == 0) {
- ret = wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng);
- if (ret == (int)outSz) {
- idx = ret;
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifndef HAVE_USER_RSA
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng);
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- if (ret != 0) {
- return TEST_FAIL;
- }
- /* Verify. */
- ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, &key);
- if (ret == (int)inLen) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- #ifndef HAVE_USER_RSA
- /* Pass bad args. */
- if (ret == 0) {
- ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- /* Pass bad args. */
- if (ret == 0) {
- ret = wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key);
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaSSL_Verify(out, 0, plain, plainSz, &key);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- WC_FREE_VAR(in, NULL);
- WC_FREE_VAR(out, NULL);
- WC_FREE_VAR(plain, NULL);
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaSSL_SignVerify */
- /*
- * Testing wc_RsaEncryptSize()
- */
- static int test_wc_RsaEncryptSize(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- int ret;
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
- if (ret == 0) {
- ret = MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng);
- if (ret == 0) {
- ret = wc_RsaEncryptSize(&key);
- }
- if (ret == 128) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- #endif
- if (ret == 0) {
- ret = MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng);
- if (ret == 0) {
- ret = wc_RsaEncryptSize(&key);
- }
- if (ret == 256) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass in bad arg. */
- if (ret == 0) {
- ret = wc_RsaEncryptSize(NULL);
- #ifndef HAVE_USER_RSA
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- #endif
- }
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaEncryptSize*/
- /*
- * Testing wc_RsaFlattenPublicKey()
- */
- static int test_wc_RsaFlattenPublicKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- int ret = 0;
- byte e[256];
- byte n[256];
- word32 eSz = sizeof(e);
- word32 nSz = sizeof(n);
- #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
- int bits = 1024;
- #else
- int bits = 2048;
- #endif
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng);
- if (ret >= 0) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz);
- }
- #ifndef HAVE_USER_RSA
- /* Pass bad args. */
- if (ret == 0) {
- ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- /* Pass bad args. */
- if (ret == 0) {
- ret = wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz);
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL);
- }
- if (ret == USER_CRYPTO_ERROR) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- if (wc_FreeRsaKey(&key) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) || ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_RsaFlattenPublicKey */
- /*
- * unit test for wc_AesCcmSetKey
- */
- static int test_wc_AesCcmSetKey(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_AESCCM
- Aes aes;
- int ret = 0;
- const byte key16[] =
- {
- 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
- 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
- };
- const byte key24[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
- };
- const byte key32[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- #ifdef WOLFSSL_AES_128
- ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
- #endif
- #ifdef WOLFSSL_AES_192
- if (ret == 0) {
- ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24));
- }
- #endif
- #ifdef WOLFSSL_AES_256
- if (ret == 0) {
- ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32));
- }
- #endif
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1);
- }
- if (ret != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- wc_AesFree(&aes);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_AesCcmSetKey */
- /*
- * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
- */
- static int test_wc_AesCcmEncryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
- Aes aes;
- int ret = 0;
- const byte key16[] =
- {
- 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
- 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
- };
- /* plaintext */
- const byte plainT[] =
- {
- 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
- };
- /* nonce */
- const byte iv[] =
- {
- 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
- 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
- };
- const byte c[] = /* cipher text. */
- {
- 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
- 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
- 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
- };
- const byte t[] = /* Auth tag */
- {
- 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
- };
- const byte authIn[] =
- {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
- };
- byte cipherOut[sizeof(plainT)];
- byte authTag[sizeof(t)];
- int ccmE = WOLFSSL_FATAL_ERROR;
- #ifdef HAVE_AES_DECRYPT
- int ccmD = WOLFSSL_FATAL_ERROR;
- byte plainOut[sizeof(cipherOut)];
- #endif
- ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- ret = wc_AesCcmSetKey(&aes, key16, sizeof(key16));
- if (ret == 0) {
- ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
- iv, sizeof(iv), authTag, sizeof(authTag),
- authIn , sizeof(authIn));
- if ((XMEMCMP(cipherOut, c, sizeof(c)) && ccmE == 0) ||
- XMEMCMP(t, authTag, sizeof(t))) {
- ccmE = WOLFSSL_FATAL_ERROR;
- ret = WOLFSSL_FATAL_ERROR;
- }
- #ifdef HAVE_AES_DECRYPT
- if (ret == 0) {
- ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
- sizeof(plainOut), iv, sizeof(iv),
- authTag, sizeof(authTag),
- authIn, sizeof(authIn));
- if (XMEMCMP(plainOut, plainT, sizeof(plainT)) && ccmD == 0) {
- ccmD = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- }
- /* Pass in bad args. Encrypt*/
- if (ret == 0 && ccmE == 0) {
- ccmE = wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
- iv, sizeof(iv), authTag, sizeof(authTag),
- authIn , sizeof(authIn));
- if (ccmE == BAD_FUNC_ARG) {
- ccmE = wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
- iv, sizeof(iv), authTag, sizeof(authTag),
- authIn , sizeof(authIn));
- }
- if (ccmE == BAD_FUNC_ARG) {
- ccmE = wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
- iv, sizeof(iv), authTag, sizeof(authTag),
- authIn , sizeof(authIn));
- }
- if (ccmE == BAD_FUNC_ARG) {
- ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
- NULL, sizeof(iv), authTag, sizeof(authTag),
- authIn , sizeof(authIn));
- }
- if (ccmE == BAD_FUNC_ARG) {
- ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
- iv, sizeof(iv), NULL, sizeof(authTag),
- authIn , sizeof(authIn));
- }
- if (ccmE == BAD_FUNC_ARG) {
- ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
- iv, sizeof(iv) + 1, authTag, sizeof(authTag),
- authIn , sizeof(authIn));
- }
- if (ccmE == BAD_FUNC_ARG) {
- ccmE = wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
- iv, sizeof(iv) - 7, authTag, sizeof(authTag),
- authIn , sizeof(authIn));
- }
- if (ccmE != BAD_FUNC_ARG) {
- ccmE = WOLFSSL_FATAL_ERROR;
- }
- else {
- ccmE = 0;
- }
- } /* End Encrypt */
- if (ccmE != 0) {
- wc_AesFree(&aes);
- return TEST_FAIL;
- }
- #ifdef HAVE_AES_DECRYPT
- /* Pass in bad args. Decrypt*/
- if (ret == 0 && ccmD == 0) {
- ccmD = wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
- iv, sizeof(iv), authTag, sizeof(authTag),
- authIn, sizeof(authIn));
- if (ccmD == BAD_FUNC_ARG) {
- ccmD = wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
- iv, sizeof(iv), authTag, sizeof(authTag),
- authIn, sizeof(authIn));
- }
- if (ccmD == BAD_FUNC_ARG) {
- ccmD = wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
- iv, sizeof(iv), authTag, sizeof(authTag),
- authIn, sizeof(authIn));
- }
- if (ccmD == BAD_FUNC_ARG) {
- ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
- sizeof(plainOut), NULL, sizeof(iv),
- authTag, sizeof(authTag),
- authIn, sizeof(authIn));
- }
- if (ccmD == BAD_FUNC_ARG) {
- ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
- sizeof(plainOut), iv, sizeof(iv), NULL,
- sizeof(authTag), authIn, sizeof(authIn));
- }
- if (ccmD == BAD_FUNC_ARG) {
- ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
- sizeof(plainOut), iv, sizeof(iv) + 1,
- authTag, sizeof(authTag),
- authIn, sizeof(authIn));
- }
- if (ccmD == BAD_FUNC_ARG) {
- ccmD = wc_AesCcmDecrypt(&aes, plainOut, cipherOut,
- sizeof(plainOut), iv, sizeof(iv) - 7,
- authTag, sizeof(authTag),
- authIn, sizeof(authIn));
- }
- if (ccmD != BAD_FUNC_ARG) {
- ccmD = WOLFSSL_FATAL_ERROR;
- }
- else {
- ccmD = 0;
- }
- } /* END Decrypt */
- res = TEST_RES_CHECK(ccmD == 0);
- #endif
- wc_AesFree(&aes);
- #endif /* HAVE_AESCCM */
- return res;
- } /* END test_wc_AesCcmEncryptDecrypt */
- /*
- * Testing wc_InitDsaKey()
- */
- static int test_wc_InitDsaKey(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_DSA
- DsaKey key;
- int ret = 0;
- ret = wc_InitDsaKey(&key);
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_InitDsaKey(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_FreeDsaKey(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_InitDsaKey */
- /*
- * Testing wc_DsaSign() and wc_DsaVerify()
- */
- static int test_wc_DsaSignVerify(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DSA)
- DsaKey key;
- WC_RNG rng;
- wc_Sha sha;
- int ret = 0;
- byte signature[DSA_SIG_SIZE];
- byte hash[WC_SHA_DIGEST_SIZE];
- word32 idx = 0;
- word32 bytes;
- int answer;
- #ifdef USE_CERT_BUFFERS_1024
- byte tmp[ONEK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
- bytes = sizeof_dsa_key_der_1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- byte tmp[TWOK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
- bytes = sizeof_dsa_key_der_2048;
- #else
- byte tmp[TWOK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
- if (fp == XBADFILE) {
- return WOLFSSL_BAD_FILE;
- }
- bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
- XFCLOSE(fp);
- #endif /* END USE_CERT_BUFFERS_1024 */
- ret = wc_InitSha(&sha);
- if (ret == 0) {
- ret = wc_ShaUpdate(&sha, tmp, bytes);
- if (ret == 0) {
- ret = wc_ShaFinal(&sha, hash);
- }
- if (ret == 0) {
- ret = wc_InitDsaKey(&key);
- }
- if (ret == 0) {
- ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
- }
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- }
- /* Sign. */
- if (ret == 0) {
- ret = wc_DsaSign(hash, signature, &key, &rng);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_DsaSign(NULL, signature, &key, &rng);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_DsaSign(hash, NULL, &key, &rng);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_DsaSign(hash, signature, NULL, &rng);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_DsaSign(hash, signature, &key, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- /* Verify. */
- ret = wc_DsaVerify(hash, signature, &key, &answer);
- if (ret != 0 || answer != 1) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- else {
- ret = 0;
- }
- }
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_DsaVerify(NULL, signature, &key, &answer);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_DsaVerify(hash, NULL, &key, &answer);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_DsaVerify(hash, signature, NULL, &answer);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_DsaVerify(hash, signature, &key, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
- /* hard set q to 0 and test fail case */
- mp_free(&key.q);
- mp_init(&key.q);
- AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
- mp_set(&key.q, 1);
- AssertIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
- #endif
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_FreeDsaKey(&key);
- wc_ShaFree(&sha);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_DsaSign */
- /*
- * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
- */
- static int test_wc_DsaPublicPrivateKeyDecode(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DSA)
- DsaKey key;
- word32 bytes;
- word32 idx = 0;
- int priv = 0;
- int pub = 0;
- int ret = 0;
- #ifdef USE_CERT_BUFFERS_1024
- byte tmp[ONEK_BUF];
- XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
- bytes = sizeof_dsa_key_der_1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- byte tmp[TWOK_BUF];
- XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
- bytes = sizeof_dsa_key_der_2048;
- #else
- byte tmp[TWOK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
- if (fp == XBADFILE)
- {
- return WOLFSSL_BAD_FILE;
- }
- bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
- XFCLOSE(fp);
- #endif /* END USE_CERT_BUFFERS_1024 */
- ret = wc_InitDsaKey(&key);
- if (ret == 0) {
- priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
- /* Test bad args. */
- if (priv == 0) {
- priv = wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes);
- if (priv == BAD_FUNC_ARG) {
- priv = wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes);
- }
- if (priv == BAD_FUNC_ARG) {
- priv = wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes);
- }
- if (priv == BAD_FUNC_ARG) {
- priv = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes);
- }
- if (priv == ASN_PARSE_E || priv == BUFFER_E) {
- priv = 0;
- }
- else {
- priv = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_FreeDsaKey(&key);
- ret = wc_InitDsaKey(&key);
- }
- if (ret == 0) {
- idx = 0; /* Reset */
- pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
- /* Test bad args. */
- if (pub == 0) {
- pub = wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes);
- if (pub == BAD_FUNC_ARG) {
- pub = wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes);
- }
- if (pub == BAD_FUNC_ARG) {
- pub = wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes);
- }
- if (pub == BAD_FUNC_ARG) {
- pub = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes);
- }
- if (pub == ASN_PARSE_E || pub == BUFFER_E) {
- pub = 0;
- }
- else {
- pub = WOLFSSL_FATAL_ERROR;
- }
- }
- } /* END Public Key */
- wc_FreeDsaKey(&key);
- res = TEST_RES_CHECK(ret == 0 && pub == 0 && priv == 0);
- #endif /* !NO_DSA */
- return res;
- } /* END test_wc_DsaPublicPrivateKeyDecode */
- /*
- * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
- */
- static int test_wc_MakeDsaKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
- DsaKey genKey;
- WC_RNG rng;
- int ret = 0;
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&genKey, 0, sizeof(genKey));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_InitDsaKey(&genKey);
- }
- if (ret == 0) {
- ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_MakeDsaParameters(&rng, ONEK_BUF, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_MakeDsaKey(&rng, &genKey);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_MakeDsaKey(NULL, &genKey);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_MakeDsaKey(&rng, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FAILURE;
- }
- wc_FreeDsaKey(&genKey);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_MakeDsaKey */
- /*
- * Testing wc_DsaKeyToDer()
- */
- static int test_wc_DsaKeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
- DsaKey genKey;
- WC_RNG rng;
- word32 bytes;
- word32 idx = 0;
- int ret = 0;
- #ifdef USE_CERT_BUFFERS_1024
- byte tmp[ONEK_BUF];
- byte der[ONEK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMSET(der, 0, sizeof(der));
- XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
- bytes = sizeof_dsa_key_der_1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- byte tmp[TWOK_BUF];
- byte der[TWOK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMSET(der, 0, sizeof(der));
- XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
- bytes = sizeof_dsa_key_der_2048;
- #else
- byte tmp[TWOK_BUF];
- byte der[TWOK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMSET(der, 0, sizeof(der));
- XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
- if (fp == XBADFILE) {
- return WOLFSSL_BAD_FILE;
- }
- bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
- XFCLOSE(fp);
- #endif /* END USE_CERT_BUFFERS_1024 */
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&genKey, 0, sizeof(genKey));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_InitDsaKey(&genKey);
- }
- if (ret == 0) {
- ret = wc_MakeDsaParameters(&rng, sizeof(tmp), &genKey);
- if (ret == 0) {
- wc_FreeDsaKey(&genKey);
- ret = wc_InitDsaKey(&genKey);
- }
- }
- if (ret == 0) {
- ret = wc_DsaPrivateKeyDecode(tmp, &idx, &genKey, bytes);
- }
- if (ret == 0) {
- ret = wc_DsaKeyToDer(&genKey, der, bytes);
- if ( ret >= 0 && ( ret = XMEMCMP(der, tmp, bytes) ) == 0 ) {
- ret = 0;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_DsaKeyToDer(NULL, der, FOURK_BUF);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_DsaKeyToDer(&genKey, NULL, FOURK_BUF);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_FreeDsaKey(&genKey);
- res = TEST_RES_CHECK(ret == 0);
- #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
- return res;
- } /* END test_wc_DsaKeyToDer */
- /*
- * Testing wc_DsaKeyToPublicDer()
- * (indirectly testing setDsaPublicKey())
- */
- static int test_wc_DsaKeyToPublicDer(void)
- {
- int res = TEST_SKIPPED;
- #ifndef HAVE_SELFTEST
- #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
- DsaKey genKey;
- WC_RNG rng;
- byte* der;
- word32 sz;
- int ret = 0;
- der = (byte*)XMALLOC(ONEK_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (der == NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_InitDsaKey(&genKey);
- }
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey);
- }
- if (ret == 0) {
- ret = wc_MakeDsaKey(&rng, &genKey);
- }
- if (ret == 0) {
- ret = wc_DsaKeyToPublicDer(&genKey, der, ONEK_BUF);
- if (ret >= 0) {
- sz = ret;
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- word32 idx = 0;
- wc_FreeDsaKey(&genKey);
- ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
- }
- /* Test without the SubjectPublicKeyInfo header */
- if (ret == 0) {
- ret = wc_SetDsaPublicKey(der, &genKey, ONEK_BUF, 0);
- if (ret >= 0) {
- sz = ret;
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- word32 idx = 0;
- wc_FreeDsaKey(&genKey);
- ret = wc_DsaPublicKeyDecode(der, &idx, &genKey, sz);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_DsaKeyToPublicDer(&genKey, NULL, FOURK_BUF);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- wc_FreeDsaKey(&genKey);
- res = TEST_RES_CHECK(ret == 0);
- #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
- #endif /* !HAVE_SELFTEST */
- return res;
- } /* END test_wc_DsaKeyToPublicDer */
- /*
- * Testing wc_DsaImportParamsRaw()
- */
- static int test_wc_DsaImportParamsRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DSA)
- DsaKey key;
- int ret = 0;
- /* [mod = L=1024, N=160], from CAVP KeyPair */
- const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
- "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
- "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
- "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
- "47123188f8dc551054ee162b634d60f097f719076640e209"
- "80a0093113a8bd73";
- const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
- const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
- "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
- "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
- "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
- "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
- "76341a7e7d9";
- /* invalid p and q parameters */
- const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
- const char* invalidQ = "96c5390a";
- ret = wc_InitDsaKey(&key);
- if (ret == 0) {
- ret = wc_DsaImportParamsRaw(&key, p, q, g);
- }
- /* test bad args */
- if (ret == 0) {
- /* null key struct */
- ret = wc_DsaImportParamsRaw(NULL, p, q, g);
- if (ret == BAD_FUNC_ARG) {
- /* null param pointers */
- ret = wc_DsaImportParamsRaw(&key, NULL, NULL, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- /* illegal p length */
- ret = wc_DsaImportParamsRaw(&key, invalidP, q, g);
- }
- if (ret == BAD_FUNC_ARG) {
- /* illegal q length */
- ret = wc_DsaImportParamsRaw(&key, p, invalidQ, g);
- if (ret == BAD_FUNC_ARG)
- ret = 0;
- }
- }
- wc_FreeDsaKey(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_DsaImportParamsRaw */
- /*
- * Testing wc_DsaImportParamsRawCheck()
- */
- static int test_wc_DsaImportParamsRawCheck(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- DsaKey key;
- int ret = 0;
- int trusted = 0;
- /* [mod = L=1024, N=160], from CAVP KeyPair */
- const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
- "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
- "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
- "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
- "47123188f8dc551054ee162b634d60f097f719076640e209"
- "80a0093113a8bd73";
- const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
- const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
- "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
- "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
- "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
- "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
- "76341a7e7d9";
- /* invalid p and q parameters */
- const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
- const char* invalidQ = "96c5390a";
- ret = wc_InitDsaKey(&key);
- if (ret == 0) {
- ret = wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL);
- }
- /* test bad args */
- if (ret == 0) {
- /* null key struct */
- ret = wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL);
- if (ret == BAD_FUNC_ARG) {
- /* null param pointers */
- ret = wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- /* illegal p length */
- ret = wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- /* illegal q length */
- ret = wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL);
- if (ret == BAD_FUNC_ARG)
- ret = 0;
- }
- }
- wc_FreeDsaKey(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_DsaImportParamsRawCheck */
- /*
- * Testing wc_DsaExportParamsRaw()
- */
- static int test_wc_DsaExportParamsRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DSA)
- DsaKey key;
- int ret = 0;
- /* [mod = L=1024, N=160], from CAVP KeyPair */
- const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
- "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
- "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
- "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
- "47123188f8dc551054ee162b634d60f097f719076640e209"
- "80a0093113a8bd73";
- const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
- const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
- "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
- "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
- "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
- "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
- "76341a7e7d9";
- const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
- "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
- "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
- "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
- "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
- "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
- "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
- "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
- "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
- "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
- "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
- const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
- "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
- const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
- "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
- "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
- "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
- "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
- "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
- "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
- "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
- "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
- "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
- "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
- byte pOut[MAX_DSA_PARAM_SIZE];
- byte qOut[MAX_DSA_PARAM_SIZE];
- byte gOut[MAX_DSA_PARAM_SIZE];
- word32 pOutSz, qOutSz, gOutSz;
- ret = wc_InitDsaKey(&key);
- if (ret == 0) {
- /* first test using imported raw parameters, for expected */
- ret = wc_DsaImportParamsRaw(&key, p, q, g);
- }
- if (ret == 0) {
- pOutSz = sizeof(pOut);
- qOutSz = sizeof(qOut);
- gOutSz = sizeof(gOut);
- ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
- gOut, &gOutSz);
- }
- if (ret == 0) {
- /* validate exported parameters are correct */
- if ((XMEMCMP(pOut, pCompare, pOutSz) != 0) ||
- (XMEMCMP(qOut, qCompare, qOutSz) != 0) ||
- (XMEMCMP(gOut, gCompare, gOutSz) != 0) ) {
- ret = -1;
- }
- }
- /* test bad args */
- if (ret == 0) {
- /* null key struct */
- ret = wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz,
- gOut, &gOutSz);
- if (ret == BAD_FUNC_ARG) {
- /* null output pointers */
- ret = wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz,
- NULL, &gOutSz);
- }
- if (ret == LENGTH_ONLY_E) {
- /* null output size pointers */
- ret = wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL,
- gOut, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- /* p output buffer size too small */
- pOutSz = 1;
- ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
- gOut, &gOutSz);
- pOutSz = sizeof(pOut);
- }
- if (ret == BUFFER_E) {
- /* q output buffer size too small */
- qOutSz = 1;
- ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
- gOut, &gOutSz);
- qOutSz = sizeof(qOut);
- }
- if (ret == BUFFER_E) {
- /* g output buffer size too small */
- gOutSz = 1;
- ret = wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz,
- gOut, &gOutSz);
- if (ret == BUFFER_E)
- ret = 0;
- }
- }
- wc_FreeDsaKey(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_DsaExportParamsRaw */
- /*
- * Testing wc_DsaExportKeyRaw()
- */
- static int test_wc_DsaExportKeyRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
- DsaKey key;
- WC_RNG rng;
- int ret = 0;
- byte xOut[MAX_DSA_PARAM_SIZE];
- byte yOut[MAX_DSA_PARAM_SIZE];
- word32 xOutSz, yOutSz;
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_InitDsaKey(&key);
- }
- if (ret == 0) {
- ret = wc_MakeDsaParameters(&rng, 1024, &key);
- if (ret == 0) {
- ret = wc_MakeDsaKey(&rng, &key);
- }
- }
- /* try successful export */
- if (ret == 0) {
- xOutSz = sizeof(xOut);
- yOutSz = sizeof(yOut);
- ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
- }
- /* test bad args */
- if (ret == 0) {
- /* null key struct */
- ret = wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz);
- if (ret == BAD_FUNC_ARG) {
- /* null output pointers */
- ret = wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz);
- }
- if (ret == LENGTH_ONLY_E) {
- /* null output size pointers */
- ret = wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- /* x output buffer size too small */
- xOutSz = 1;
- ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
- xOutSz = sizeof(xOut);
- }
- if (ret == BUFFER_E) {
- /* y output buffer size too small */
- yOutSz = 1;
- ret = wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz);
- if (ret == BUFFER_E)
- ret = 0;
- }
- }
- wc_FreeDsaKey(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_DsaExportParamsRaw */
- /*
- * Testing wc_ed25519_make_key().
- */
- static int test_wc_ed25519_make_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519)
- ed25519_key key;
- WC_RNG rng;
- unsigned char pubkey[ED25519_PUB_KEY_SIZE];
- int ret = 0;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed25519_init(&key);
- }
- if (ret == 0) {
- ret = wc_ed25519_make_public(&key, pubkey, sizeof(pubkey));
- if (ret == ECC_PRIV_KEY_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = -1;
- }
- }
- if (ret == 0) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed25519_make_key */
- /*
- * Testing wc_ed25519_init()
- */
- static int test_wc_ed25519_init(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519)
- ed25519_key key;
- int ret = 0;
- ret = wc_ed25519_init(&key);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed25519_init(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_ed25519_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed25519_init */
- /*
- * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
- */
- static int test_wc_ed25519_sign_msg(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
- WC_RNG rng;
- ed25519_key key;
- int ret = 0;
- byte msg[] = "Everybody gets Friday off.\n";
- byte sig[ED25519_SIG_SIZE];
- word32 msglen = sizeof(msg);
- word32 siglen = sizeof(sig);
- word32 badSigLen = sizeof(sig) - 1;
- #ifdef HAVE_ED25519_VERIFY
- int verify_ok = 0; /*1 = Verify success.*/
- #endif
- /* Initialize stack variables. */
- XMEMSET(sig, 0, siglen);
- /* Initialize key. */
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed25519_init(&key);
- if (ret == 0) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
- }
- }
- if (ret == 0) {
- ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key);
- }
- /* Test bad args. */
- if (ret == 0 && siglen == ED25519_SIG_SIZE) {
- ret = wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key);
- }
- if (ret == BUFFER_E && badSigLen == ED25519_SIG_SIZE) {
- badSigLen -= 1;
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- } /* END sign */
- #ifdef HAVE_ED25519_VERIFY
- if (ret == 0) {
- ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key);
- if (ret == 0 && verify_ok == 1) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad args. */
- if (ret == 0) {
- AssertIntEQ(wc_ed25519_verify_msg(sig, siglen - 1, msg,
- msglen, &verify_ok, &key),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_ed25519_verify_msg(sig, siglen + 1, msg,
- msglen, &verify_ok, &key),
- BAD_FUNC_ARG);
- ret = wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
- &key);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_verify_msg(sig, siglen, NULL, msglen,
- &verify_ok, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
- NULL, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_verify_msg(sig, siglen, msg, msglen,
- &verify_ok, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_verify_msg(sig, badSigLen, msg, msglen,
- &verify_ok, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- } /* END verify. */
- #endif /* Verify. */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed25519_sign_msg */
- /*
- * Testing wc_ed25519_import_public()
- */
- static int test_wc_ed25519_import_public(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
- WC_RNG rng;
- ed25519_key pubKey;
- const byte in[] = "Ed25519PublicKeyUnitTest......\n";
- word32 inlen = sizeof(in);
- int ret = 0;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed25519_init(&pubKey);
- if (ret == 0) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey);
- }
- }
- if (ret == 0) {
- ret = wc_ed25519_import_public_ex(in, inlen, &pubKey, 1);
- if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed25519_import_public(NULL, inlen, &pubKey);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_import_public(in, inlen, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_import_public(in, inlen - 1, &pubKey);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&pubKey);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END wc_ed25519_import_public */
- /*
- * Testing wc_ed25519_import_private_key()
- */
- static int test_wc_ed25519_import_private_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
- WC_RNG rng;
- ed25519_key key;
- int ret;
- const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
- const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
- word32 privKeySz = sizeof(privKey);
- word32 pubKeySz = sizeof(pubKey);
- #ifdef HAVE_ED25519_KEY_EXPORT
- byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
- word32 bothKeysSz = sizeof(bothKeys);
- #endif
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return ret;
- }
- ret = wc_ed25519_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return ret;
- }
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
- if (ret == 0) {
- ret = wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
- pubKeySz, &key, 1);
- if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
- || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifdef HAVE_ED25519_KEY_EXPORT
- if (ret == 0)
- ret = wc_ed25519_export_private(&key, bothKeys, &bothKeysSz);
- if (ret == 0) {
- ret = wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
- &key, 1);
- if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
- || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
- &key);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
- pubKeySz, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
- pubKeySz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
- pubKeySz, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
- pubKeySz - 1, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_import_private_key(privKey, privKeySz, NULL,
- 0, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed25519_import_private_key */
- /*
- * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
- */
- static int test_wc_ed25519_export(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
- WC_RNG rng;
- ed25519_key key;
- int ret = 0;
- byte priv[ED25519_PRV_KEY_SIZE];
- byte pub[ED25519_PUB_KEY_SIZE];
- word32 privSz = sizeof(priv);
- word32 pubSz = sizeof(pub);
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return ret;
- }
- ret = wc_ed25519_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return ret;
- }
- if (ret == 0) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
- }
- if (ret == 0) {
- ret = wc_ed25519_export_public(&key, pub, &pubSz);
- if (ret == 0 && (pubSz != ED25519_KEY_SIZE
- || XMEMCMP(key.p, pub, pubSz) != 0)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_ed25519_export_public(NULL, pub, &pubSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_public(&key, NULL, &pubSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_public(&key, pub, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- if (ret == 0) {
- ret = wc_ed25519_export_private_only(&key, priv, &privSz);
- if (ret == 0 && (privSz != ED25519_KEY_SIZE
- || XMEMCMP(key.k, priv, privSz) != 0)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_ed25519_export_private_only(NULL, priv, &privSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_private_only(&key, NULL, &privSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_private_only(&key, priv, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed25519_export */
- /*
- * Testing wc_ed25519_size()
- */
- static int test_wc_ed25519_size(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519)
- WC_RNG rng;
- ed25519_key key;
- int ret;
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return ret;
- }
- ret = wc_ed25519_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return ret;
- }
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- wc_ed25519_free(&key);
- return ret;
- }
- ret = wc_ed25519_size(&key);
- /* Test bad args. */
- if (ret == ED25519_KEY_SIZE) {
- ret = wc_ed25519_size(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_ed25519_sig_size(&key);
- if (ret == ED25519_SIG_SIZE) {
- ret = 0;
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed25519_sig_size(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- } /* END wc_ed25519_sig_size() */
- if (ret == 0) {
- ret = wc_ed25519_pub_size(&key);
- if (ret == ED25519_PUB_KEY_SIZE) {
- ret = 0;
- }
- if (ret == 0) {
- ret = wc_ed25519_pub_size(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- } /* END wc_ed25519_pub_size */
- if (ret == 0) {
- ret = wc_ed25519_priv_size(&key);
- if (ret == ED25519_PRV_KEY_SIZE) {
- ret = 0;
- }
- if (ret == 0) {
- ret = wc_ed25519_priv_size(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- } /* END wc_ed25519_pub_size */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed25519_size */
- /*
- * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
- */
- static int test_wc_ed25519_exportKey(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
- WC_RNG rng;
- ed25519_key key;
- int ret = 0;
- byte priv[ED25519_PRV_KEY_SIZE];
- byte pub[ED25519_PUB_KEY_SIZE];
- byte privOnly[ED25519_PRV_KEY_SIZE];
- word32 privSz = sizeof(priv);
- word32 pubSz = sizeof(pub);
- word32 privOnlySz = sizeof(privOnly);
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_ed25519_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return TEST_FAIL;
- }
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- wc_ed25519_free(&key);
- return TEST_FAIL;
- }
- ret = wc_ed25519_export_private(&key, privOnly, &privOnlySz);
- if (ret == 0) {
- ret = wc_ed25519_export_private(NULL, privOnly, &privOnlySz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_private(&key, NULL, &privOnlySz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_private(&key, privOnly, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz);
- if (ret == 0) {
- ret = wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed25519_export_key(&key, priv, &privSz, pub, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- } /* END wc_ed25519_export_key() */
- /* Cross check output. */
- if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed25519_exportKey */
- /*
- * Testing wc_Ed25519PublicKeyToDer
- */
- static int test_wc_Ed25519PublicKeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
- (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
- int tmp;
- ed25519_key key;
- byte derBuf[1024];
- int ret = 0;
- /* Test bad args */
- tmp = wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0);
- if (tmp != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- wc_ed25519_init(&key);
- tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
- if (tmp != BUFFER_E) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- }
- /* Test good args */
- if (ret == 0) {
- WC_RNG rng;
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_ed25519_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return TEST_FAIL;
- }
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- wc_ed25519_free(&key);
- return TEST_FAIL;
- }
- tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
- if (tmp <= 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_FreeRng(&rng);
- wc_ed25519_free(&key);
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END testing wc_Ed25519PublicKeyToDer */
- /*
- * Testing wc_curve25519_init and wc_curve25519_free.
- */
- static int test_wc_curve25519_init(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519)
- curve25519_key key;
- int ret = 0;
- ret = wc_curve25519_init(&key);
- /* Test bad args for wc_curve25519_init */
- if (ret == 0) {
- ret = wc_curve25519_init(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test good args for wc_curve_25519_free */
- wc_curve25519_free(&key);
- wc_curve25519_free(NULL);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_curve25519_init and wc_curve_25519_free*/
- /*
- * Testing test_wc_curve25519_size.
- */
- static int test_wc_curve25519_size(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519)
- curve25519_key key;
- int ret = 0;
- ret = wc_curve25519_init(&key);
- /* Test good args for wc_curve25519_size */
- if (ret == 0) {
- ret = wc_curve25519_size(&key);
- }
- /* Test bad args for wc_curve25519_size */
- if (ret != 0) {
- ret = wc_curve25519_size(NULL);
- }
- wc_curve25519_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_curve25519_size*/
- /*
- * Testing test_wc_curve25519_export_key_raw().
- */
- static int test_wc_curve25519_export_key_raw(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
- curve25519_key key;
- WC_RNG rng;
- int ret = 0;
- byte privateKey[CURVE25519_KEYSIZE];
- byte publicKey[CURVE25519_KEYSIZE];
- word32 prvkSz;
- word32 pubkSz;
- byte prik[CURVE25519_KEYSIZE];
- byte pubk[CURVE25519_KEYSIZE];
- word32 prksz;
- word32 pbksz;
- if (0 != wc_InitRng(&rng)) {
- return TEST_FAIL;
- }
- if (0 != wc_curve25519_init(&key)) {
- wc_FreeRng(&rng);
- return TEST_FAIL;
- }
- ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
- /*
- bad-argument-test cases
- target function sould return BAD_FUNC_ARG
- */
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- NULL, privateKey, &prvkSz, publicKey, &pubkSz)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- &key, NULL, &prvkSz, publicKey, &pubkSz)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- &key, privateKey, NULL, publicKey, &pubkSz)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- /* prvkSz = CURVE25519_KEYSIZE; */
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- &key, privateKey, &prvkSz, NULL, &pubkSz)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- &key, privateKey, &prvkSz, publicKey, NULL )) {
- ret = -1;
- }
- }
- /*
- cross-testing
- */
- if (ret == 0) {
- prksz = CURVE25519_KEYSIZE;
- ret = wc_curve25519_export_private_raw(&key, prik, &prksz);
- }
- if (ret == 0) {
- pbksz = CURVE25519_KEYSIZE;
- ret = wc_curve25519_export_public(&key, pubk, &pbksz);
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- /* pubkSz = CURVE25519_KEYSIZE; */
- ret = wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
- publicKey, &pubkSz);
- }
- if (ret == 0) {
- if ((prksz == CURVE25519_KEYSIZE) &&
- (pbksz == CURVE25519_KEYSIZE) &&
- (prvkSz == CURVE25519_KEYSIZE) &&
- (pubkSz == CURVE25519_KEYSIZE)) {
- if (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
- 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE)) {
- ret = -1;
- }
- }
- }
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* end of test_wc_curve25519_export_key_raw */
- /*
- * Testing test_wc_curve25519_export_key_raw_ex().
- */
- static int test_wc_curve25519_export_key_raw_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
- curve25519_key key;
- WC_RNG rng;
- int ret;
- byte privateKey[CURVE25519_KEYSIZE];
- byte publicKey[CURVE25519_KEYSIZE];
- word32 prvkSz;
- word32 pubkSz;
- byte prik[CURVE25519_KEYSIZE];
- byte pubk[CURVE25519_KEYSIZE];
- word32 prksz;
- word32 pbksz;
- if (0 != wc_InitRng(&rng)) {
- return TEST_FAIL;
- }
- if (0 != wc_curve25519_init(&key)) {
- wc_FreeRng(&rng);
- return TEST_FAIL;
- }
- ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
- /*
- bad-argument-test cases
- target function sould return BAD_FUNC_ARG
- */
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
- &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
- &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
- NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- /* prvkSz = CURVE25519_KEYSIZE; */
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- /* pubkSz = CURVE25519_KEYSIZE; */
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
- &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL,
- &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- /* prvkSz = CURVE25519_KEYSIZE; */
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)) {
- ret = -1;
- }
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)) {
- ret = -1;
- }
- }
- /* illegal value for endien */
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- /* pubkSz = CURVE25519_KEYSIZE; */
- if (BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex(&key, privateKey,
- &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN + 10)) {
- ret = -1;
- }
- }
- /*
- cross-testing
- */
- if (ret == 0) {
- prksz = CURVE25519_KEYSIZE;
- ret = wc_curve25519_export_private_raw( &key, prik, &prksz);
- }
- if (ret == 0) {
- pbksz = CURVE25519_KEYSIZE;
- ret = wc_curve25519_export_public( &key, pubk, &pbksz);
- }
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- /* pubkSz = CURVE25519_KEYSIZE; */
- ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
- publicKey, &pubkSz, EC25519_BIG_ENDIAN);
- }
- if (ret == 0 && (prksz != CURVE25519_KEYSIZE ||
- pbksz != CURVE25519_KEYSIZE ||
- prvkSz != CURVE25519_KEYSIZE ||
- pubkSz != CURVE25519_KEYSIZE)) {
- ret = -1;
- }
- if (ret == 0 && (0 != XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE) ||
- 0 != XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE))) {
- ret = -1;
- }
- if (ret == 0) {
- ret = wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
- publicKey, &pubkSz, EC25519_LITTLE_ENDIAN);
- }
- if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
- pubkSz != CURVE25519_KEYSIZE)) {
- ret = -1;
- }
- /*
- try once with another endian
- */
- if (ret == 0) {
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- ret = wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
- publicKey, &pubkSz, EC25519_BIG_ENDIAN);
- }
- if (ret == 0 && (prvkSz != CURVE25519_KEYSIZE ||
- pubkSz != CURVE25519_KEYSIZE)) {
- ret = -1;
- }
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* end of test_wc_curve25519_export_key_raw_ex */
- /*
- * Testing wc_curve25519_make_key
- */
- static int test_wc_curve25519_make_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519)
- WC_RNG rng;
- curve25519_key key;
- int keysize;
- int ret;
- ret = wc_curve25519_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
- if (ret == 0) {
- keysize = wc_curve25519_size(&key);
- if (keysize != CURVE25519_KEYSIZE) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, keysize, &key);
- }
- }
- /*test bad cases*/
- if (ret == 0) {
- ret = wc_curve25519_make_key(NULL, 0, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, keysize, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(NULL, keysize, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, 0, &key);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve25519_make_key*/
- /*
- * Testing wc_curve25519_shared_secret_ex
- */
- static int test_wc_curve25519_shared_secret_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519)
- WC_RNG rng;
- curve25519_key private_key, public_key;
- byte out[CURVE25519_KEYSIZE];
- word32 outLen = sizeof(out);
- int endian = EC25519_BIG_ENDIAN;
- int ret;
- ret = wc_curve25519_init(&private_key);
- if (ret == 0) {
- ret = wc_curve25519_init(&public_key);
- }
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key);
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key);
- }
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
- &outLen, endian);
- }
- /*test bad cases*/
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(NULL, NULL, NULL,
- 0, endian);
- if (ret == 0) {
- ret = -1;
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
- &outLen, endian);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
- &outLen, endian);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
- &outLen, endian);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
- NULL, endian);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /*curve25519.c is checking for public_key size less than or equal to 0x7f,
- *increasing to 0x8f checks for error being returned*/
- public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
- ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
- &outLen, endian);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- outLen = outLen - 2;
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
- &outLen, endian);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- wc_curve25519_free(&private_key);
- wc_curve25519_free(&public_key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve25519_shared_secret_ex*/
- /*
- * Testing wc_curve25519_make_pub
- */
- static int test_wc_curve25519_make_pub(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_CURVE25519
- WC_RNG rng;
- curve25519_key key;
- byte out[CURVE25519_KEYSIZE];
- int ret;
- ret = wc_curve25519_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_make_pub((int)sizeof(out), out, (int)sizeof(key.k), key.k);
- }
- /*test bad cases*/
- if (ret == 0) {
- ret = wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k, (int)sizeof out, out);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), NULL);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_make_pub((int)sizeof out - 1, out, (int)sizeof(key.k), key.k);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof(key.k), key.k);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* verify clamping test */
- key.k[0] |= ~248;
- ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- key.k[0] &= 248;
- }
- /* repeat the expected-to-succeed test. */
- if (ret == 0) {
- ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k), key.k);
- }
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve25519_make_pub */
- /*
- * Testing test_wc_curve25519_export_public_ex
- */
- static int test_wc_curve25519_export_public_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519)
- WC_RNG rng;
- curve25519_key key;
- byte out[CURVE25519_KEYSIZE];
- word32 outLen = sizeof(out);
- int endian = EC25519_BIG_ENDIAN;
- int ret;
- ret = wc_curve25519_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
- if (ret == 0) {
- ret = wc_curve25519_export_public(&key, out, &outLen);
- }
- if (ret == 0) {
- ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
- }
- }
- /*test bad cases*/
- if (ret == 0) {
- ret = wc_curve25519_export_public_ex(NULL, NULL, NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_export_public_ex(NULL, out, &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_export_public_ex(&key, NULL, &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_export_public_ex(&key, out, NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- outLen = outLen - 2;
- if (ret == 0) {
- ret = wc_curve25519_export_public_ex(&key, out, &outLen, endian);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve25519_export_public_ex*/
- /*
- * Testing test_wc_curve25519_import_private_raw_ex
- */
- static int test_wc_curve25519_import_private_raw_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519)
- WC_RNG rng;
- curve25519_key key;
- byte priv[CURVE25519_KEYSIZE];
- byte pub[CURVE25519_KEYSIZE];
- word32 privSz = sizeof(priv);
- word32 pubSz = sizeof(pub);
- int endian = EC25519_BIG_ENDIAN;
- int ret;
- ret = wc_curve25519_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
- if (ret == 0) {
- ret = wc_curve25519_export_private_raw_ex(&key, priv, &privSz, endian);
- }
- if (ret == 0) {
- ret = wc_curve25519_export_public(&key, pub, &pubSz);
- }
- if (ret == 0) {
- ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
- &key, endian);
- }
- }
- /*test bad cases*/
- if (ret == 0) {
- ret = wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
- endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
- &key, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
- &key, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
- NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
- &key, endian);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
- &key, endian);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
- &key, EC25519_LITTLE_ENDIAN);
- }
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve25519_import_private_raw_ex*/
- /*
- * Testing test_wc_curve25519_import_private
- */
- static int test_wc_curve25519_import_private(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519)
- curve25519_key key;
- WC_RNG rng;
- byte priv[CURVE25519_KEYSIZE];
- word32 privSz = sizeof(priv);
- int ret;
- ret = wc_curve25519_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key);
- if (ret == 0) {
- ret = wc_curve25519_export_private_raw(&key, priv, &privSz);
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_import_private(priv, privSz, &key);
- }
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve25519_import*/
- /*
- * Testing test_wc_curve25519_export_private_raw_ex
- */
- static int test_wc_curve25519_export_private_raw_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE25519)
- curve25519_key key;
- byte out[CURVE25519_KEYSIZE];
- word32 outLen = sizeof(out);
- int endian = EC25519_BIG_ENDIAN;
- int ret;
- ret = wc_curve25519_init(&key);
- if (ret == 0) {
- ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
- }
- /*test bad cases*/
- if (ret == 0) {
- ret = wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_export_private_raw_ex(&key, NULL, &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_export_private_raw_ex(&key, out, NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen,
- EC25519_LITTLE_ENDIAN);
- }
- outLen = outLen - 2;
- if (ret == 0) {
- ret = wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- wc_curve25519_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/*END test_wc_curve25519_export_private_raw_ex*/
- /*
- * Testing wc_ed448_make_key().
- */
- static int test_wc_ed448_make_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448)
- ed448_key key;
- WC_RNG rng;
- unsigned char pubkey[ED448_PUB_KEY_SIZE];
- int ret;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed448_init(&key);
- }
- if (ret == 0) {
- ret = wc_ed448_make_public(&key, pubkey, sizeof(pubkey));
- if (ret == ECC_PRIV_KEY_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = -1;
- }
- }
- if (ret == 0) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed448_make_key */
- /*
- * Testing wc_ed448_init()
- */
- static int test_wc_ed448_init(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448)
- ed448_key key;
- int ret;
- ret = wc_ed448_init(&key);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed448_init(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_ed448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed448_init */
- /*
- * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
- */
- static int test_wc_ed448_sign_msg(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
- WC_RNG rng;
- ed448_key key;
- byte msg[] = "Everybody gets Friday off.\n";
- byte sig[ED448_SIG_SIZE];
- word32 msglen = sizeof(msg);
- word32 siglen = sizeof(sig);
- word32 badSigLen = sizeof(sig) - 1;
- #ifdef HAVE_ED448_VERIFY
- int verify_ok = 0; /*1 = Verify success.*/
- #endif
- int ret;
- /* Initialize stack variables. */
- XMEMSET(sig, 0, siglen);
- /* Initialize key. */
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed448_init(&key);
- if (ret == 0) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- }
- }
- if (ret == 0) {
- ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0);
- }
- /* Test bad args. */
- if (ret == 0 && siglen == ED448_SIG_SIZE) {
- ret = wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key,
- NULL, 0);
- }
- if (ret == BUFFER_E && badSigLen == ED448_SIG_SIZE) {
- badSigLen -= 1;
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- } /* END sign */
- #ifdef HAVE_ED448_VERIFY
- if (ret == 0) {
- ret = wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
- &key, NULL, 0);
- if (ret == 0 && verify_ok == 1) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad args. */
- if (ret == 0) {
- AssertIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg,
- msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg,
- msglen, &verify_ok, &key, NULL, 0), BAD_FUNC_ARG);
- ret = wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
- &key, NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_verify_msg(sig, siglen, NULL, msglen,
- &verify_ok, &key, NULL, 0);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
- NULL, &key, NULL, 0);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_verify_msg(sig, siglen, msg, msglen,
- &verify_ok, NULL, NULL, 0);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_verify_msg(sig, badSigLen, msg, msglen,
- &verify_ok, &key, NULL, 0);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- } /* END verify. */
- #endif /* Verify. */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed448_sign_msg */
- /*
- * Testing wc_ed448_import_public()
- */
- static int test_wc_ed448_import_public(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
- WC_RNG rng;
- ed448_key pubKey;
- const byte in[] =
- "Ed448PublicKeyUnitTest.................................\n";
- word32 inlen = sizeof(in);
- int ret = 0;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed448_init(&pubKey);
- if (ret == 0) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey);
- }
- }
- if (ret == 0) {
- ret = wc_ed448_import_public_ex(in, inlen, &pubKey, 1);
- if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed448_import_public(NULL, inlen, &pubKey);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_import_public(in, inlen, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_import_public(in, inlen - 1, &pubKey);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&pubKey);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END wc_ed448_import_public */
- /*
- * Testing wc_ed448_import_private_key()
- */
- static int test_wc_ed448_import_private_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
- WC_RNG rng;
- ed448_key key;
- const byte privKey[] =
- "Ed448PrivateKeyUnitTest................................\n";
- const byte pubKey[] =
- "Ed448PublicKeyUnitTest.................................\n";
- word32 privKeySz = sizeof(privKey);
- word32 pubKeySz = sizeof(pubKey);
- #ifdef HAVE_ED448_KEY_EXPORT
- byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
- word32 bothKeysSz = sizeof(bothKeys);
- #endif
- int ret;
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return TEST_FAIL;
- }
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- if (ret == 0) {
- ret = wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
- pubKeySz, &key, 1);
- if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
- XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifdef HAVE_ED448_KEY_EXPORT
- if (ret == 0)
- ret = wc_ed448_export_private(&key, bothKeys, &bothKeysSz);
- if (ret == 0) {
- ret = wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
- &key, 1);
- if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
- XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
- &key);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
- pubKeySz, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
- pubKeySz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
- pubKeySz, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_import_private_key(privKey, privKeySz, pubKey,
- pubKeySz - 1, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_import_private_key(privKey, privKeySz, NULL,
- 0, &key);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed448_import_private_key */
- /*
- * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
- */
- static int test_wc_ed448_export(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
- WC_RNG rng;
- ed448_key key;
- byte priv[ED448_PRV_KEY_SIZE];
- byte pub[ED448_PUB_KEY_SIZE];
- word32 privSz = sizeof(priv);
- word32 pubSz = sizeof(pub);
- int ret;
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return TEST_FAIL;
- }
- if (ret == 0) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- }
- if (ret == 0) {
- ret = wc_ed448_export_public(&key, pub, &pubSz);
- if (ret == 0 && (pubSz != ED448_KEY_SIZE ||
- XMEMCMP(key.p, pub, pubSz) != 0)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_ed448_export_public(NULL, pub, &pubSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_public(&key, NULL, &pubSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_public(&key, pub, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- if (ret == 0) {
- ret = wc_ed448_export_private_only(&key, priv, &privSz);
- if (ret == 0 && (privSz != ED448_KEY_SIZE ||
- XMEMCMP(key.k, priv, privSz) != 0)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_ed448_export_private_only(NULL, priv, &privSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_private_only(&key, NULL, &privSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_private_only(&key, priv, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed448_export */
- /*
- * Testing wc_ed448_size()
- */
- static int test_wc_ed448_size(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448)
- WC_RNG rng;
- ed448_key key;
- int ret = 0;
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return TEST_FAIL;
- }
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- wc_ed448_free(&key);
- return TEST_FAIL;
- }
- ret = wc_ed448_size(&key);
- /* Test bad args. */
- if (ret == ED448_KEY_SIZE) {
- ret = wc_ed448_size(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_ed448_sig_size(&key);
- if (ret == ED448_SIG_SIZE) {
- ret = 0;
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ed448_sig_size(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- } /* END wc_ed448_sig_size() */
- if (ret == 0) {
- ret = wc_ed448_pub_size(&key);
- if (ret == ED448_PUB_KEY_SIZE) {
- ret = 0;
- }
- if (ret == 0) {
- ret = wc_ed448_pub_size(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- } /* END wc_ed448_pub_size */
- if (ret == 0) {
- ret = wc_ed448_priv_size(&key);
- if (ret == ED448_PRV_KEY_SIZE) {
- ret = 0;
- }
- if (ret == 0) {
- ret = wc_ed448_priv_size(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- } /* END wc_ed448_pub_size */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed448_size */
- /*
- * Testing wc_ed448_export_private() and wc_ed448_export_key()
- */
- static int test_wc_ed448_exportKey(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
- WC_RNG rng;
- ed448_key key;
- byte priv[ED448_PRV_KEY_SIZE];
- byte pub[ED448_PUB_KEY_SIZE];
- byte privOnly[ED448_PRV_KEY_SIZE];
- word32 privSz = sizeof(priv);
- word32 pubSz = sizeof(pub);
- word32 privOnlySz = sizeof(privOnly);
- int ret;
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return TEST_FAIL;
- }
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- wc_ed448_free(&key);
- return TEST_FAIL;
- }
- ret = wc_ed448_export_private(&key, privOnly, &privOnlySz);
- if (ret == 0) {
- ret = wc_ed448_export_private(NULL, privOnly, &privOnlySz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_private(&key, NULL, &privOnlySz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_private(&key, privOnly, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz);
- if (ret == 0) {
- ret = wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_key(&key, priv, NULL, pub, &pubSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ed448_export_key(&key, priv, &privSz, pub, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- } /* END wc_ed448_export_key() */
- /* Cross check output. */
- if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ed448_exportKey */
- /*
- * Testing wc_Ed448PublicKeyToDer
- */
- static int test_wc_Ed448PublicKeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
- (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
- int tmp;
- ed448_key key;
- byte derBuf[1024];
- int ret = 0;
- /* Test bad args */
- tmp = wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0);
- if (tmp != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- wc_ed448_init(&key);
- tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
- if (tmp != BUFFER_E) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- }
- /* Test good args */
- if (ret == 0) {
- WC_RNG rng;
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return TEST_FAIL;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return TEST_FAIL;
- }
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- wc_ed448_free(&key);
- return TEST_FAIL;
- }
- tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
- if (tmp <= 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_FreeRng(&rng);
- wc_ed448_free(&key);
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END testing wc_Ed448PublicKeyToDer */
- /*
- * Testing wc_curve448_init and wc_curve448_free.
- */
- static int test_wc_curve448_init(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE448)
- curve448_key key;
- int ret = 0;
- ret = wc_curve448_init(&key);
- /* Test bad args for wc_curve448_init */
- if (ret == 0) {
- ret = wc_curve448_init(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test good args for wc_curve_448_free */
- wc_curve448_free(&key);
- wc_curve448_free(NULL);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_curve448_init and wc_curve_448_free*/
- /*
- * Testing wc_curve448_make_key
- */
- static int test_wc_curve448_make_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE448)
- WC_RNG rng;
- curve448_key key;
- int keysize;
- int ret;
- ret = wc_curve448_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
- if (ret == 0) {
- keysize = wc_curve448_size(&key);
- if (keysize != CURVE448_KEY_SIZE) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, keysize, &key);
- }
- }
- /* test bad cases */
- if (ret == 0) {
- ret = wc_curve448_make_key(NULL, 0, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, keysize, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_make_key(NULL, keysize, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, 0, &key);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (wc_FreeRng(&rng) != 0 && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_curve448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve448_make_key*/
- /*
- * Testing test_wc_curve448_shared_secret_ex
- */
- static int test_wc_curve448_shared_secret_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE448)
- WC_RNG rng;
- curve448_key private_key, public_key;
- byte out[CURVE448_KEY_SIZE];
- word32 outLen = sizeof(out);
- int endian = EC448_BIG_ENDIAN;
- int ret;
- ret = wc_curve448_init(&private_key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key);
- }
- }
- if (ret == 0) {
- ret = wc_curve448_init(&public_key);
- }
- if (ret == 0) {
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key);
- }
- }
- if (ret == 0) {
- ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
- &outLen, endian);
- }
- /* test bad cases */
- if (ret == 0) {
- ret = wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_shared_secret_ex(NULL, &public_key, out,
- &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_shared_secret_ex(&private_key, NULL, out,
- &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
- &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
- NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- outLen = outLen - 2;
- if (ret == 0) {
- ret = wc_curve448_shared_secret_ex(&private_key, &public_key, out,
- &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- wc_curve448_free(&private_key);
- wc_curve448_free(&public_key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve448_shared_secret_ex*/
- /*
- * Testing test_wc_curve448_export_public_ex
- */
- static int test_wc_curve448_export_public_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE448)
- WC_RNG rng;
- curve448_key key;
- byte out[CURVE448_KEY_SIZE];
- word32 outLen = sizeof(out);
- int endian = EC448_BIG_ENDIAN;
- int ret;
- ret = wc_curve448_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
- if (ret == 0) {
- ret = wc_curve448_export_public(&key, out, &outLen);
- }
- if (ret == 0) {
- ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
- }
- }
- /*test bad cases*/
- if (ret == 0) {
- ret = wc_curve448_export_public_ex(NULL, NULL, NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_export_public_ex(NULL, out, &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_export_public_ex(&key, NULL, &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_export_public_ex(&key, out, NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- outLen = outLen - 2;
- if (ret == 0) {
- ret = wc_curve448_export_public_ex(&key, out, &outLen, endian);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- wc_curve448_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve448_export_public_ex*/
- /*
- * Testing test_wc_curve448_export_private_raw_ex
- */
- static int test_wc_curve448_export_private_raw_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE448)
- curve448_key key;
- byte out[CURVE448_KEY_SIZE];
- word32 outLen = sizeof(out);
- int endian = EC448_BIG_ENDIAN;
- int ret;
- ret = wc_curve448_init(&key);
- if (ret == 0) {
- ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
- }
- /*test bad cases*/
- if (ret == 0) {
- ret = wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_export_private_raw_ex(&key, out, NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_export_private_raw_ex(&key, out, &outLen,
- EC448_LITTLE_ENDIAN);
- }
- outLen = outLen - 2;
- if (ret == 0) {
- ret = wc_curve448_export_private_raw_ex(&key, out, &outLen, endian);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- wc_curve448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/*END test_wc_curve448_export_private_raw_ex*/
- /*
- * Testing test_wc_curve448_import_private_raw_ex
- */
- static int test_wc_curve448_import_private_raw_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE448)
- WC_RNG rng;
- curve448_key key;
- byte priv[CURVE448_KEY_SIZE];
- byte pub[CURVE448_KEY_SIZE];
- word32 privSz = sizeof(priv);
- word32 pubSz = sizeof(pub);
- int endian = EC448_BIG_ENDIAN;
- int ret;
- ret = wc_curve448_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
- if (ret == 0) {
- ret = wc_curve448_export_private_raw(&key, priv, &privSz);
- }
- if (ret == 0) {
- ret = wc_curve448_export_public(&key, pub, &pubSz);
- }
- if (ret == 0) {
- ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
- &key, endian);
- }
- }
- /* test bad cases */
- if (ret == 0) {
- ret = wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
- &key, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
- &key, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
- NULL, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
- &key, endian);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
- &key, endian);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
- &key, EC448_LITTLE_ENDIAN);
- }
- if (wc_FreeRng(&rng) != 0 && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_curve448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve448_import_private_raw_ex*/
- /*
- * Testing test_curve448_export_key_raw
- */
- static int test_wc_curve448_export_key_raw(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE448)
- WC_RNG rng;
- curve448_key key;
- byte priv[CURVE448_KEY_SIZE];
- byte pub[CURVE448_KEY_SIZE];
- word32 privSz = sizeof(priv);
- word32 pubSz = sizeof(pub);
- int ret;
- ret = wc_curve448_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
- if (ret == 0) {
- ret = wc_curve448_export_private_raw(&key, priv, &privSz);
- }
- if (ret == 0) {
- ret = wc_curve448_export_public(&key, pub, &pubSz);
- }
- if (ret == 0) {
- ret = wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz);
- }
- }
- wc_curve448_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/*END test_wc_curve448_import_private_raw_ex*/
- /*
- * Testing test_wc_curve448_import_private
- */
- static int test_wc_curve448_import_private(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE448)
- curve448_key key;
- WC_RNG rng;
- byte priv[CURVE448_KEY_SIZE];
- word32 privSz = sizeof(priv);
- int ret;
- ret = wc_curve448_init(&key);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key);
- if (ret == 0) {
- ret = wc_curve448_export_private_raw(&key, priv, &privSz);
- }
- }
- if (ret == 0) {
- ret = wc_curve448_import_private(priv, privSz, &key);
- }
- wc_curve448_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /*END test_wc_curve448_import*/
- /*
- * Testing test_wc_curve448_size.
- */
- static int test_wc_curve448_size(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CURVE448)
- curve448_key key;
- int ret = 0;
- ret = wc_curve448_init(&key);
- /* Test good args for wc_curve448_size */
- if (ret == 0) {
- ret = wc_curve448_size(&key);
- }
- /* Test bad args for wc_curve448_size */
- if (ret != 0) {
- ret = wc_curve448_size(NULL);
- }
- wc_curve448_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_curve448_size*/
- /*
- * Testing wc_ecc_make_key.
- */
- static int test_wc_ecc_make_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecc_key key;
- int ret;
- ret = wc_InitRng(&rng);
- if (ret != 0)
- return TEST_FAIL;
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY14, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_ecc_make_key(NULL, KEY14, &key);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_make_key(&rng, KEY14, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_ecc_free(&key);
- }
- if (wc_FreeRng(&rng) != 0 && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_make_key */
- /*
- * Testing wc_ecc_init()
- */
- static int test_wc_ecc_init(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_ECC
- ecc_key key;
- int ret;
- ret = wc_ecc_init(&key);
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_ecc_init(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_ecc_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_init */
- /*
- * Testing wc_ecc_check_key()
- */
- static int test_wc_ecc_check_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecc_key key;
- int ret;
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY14, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- if (ret == 0) {
- ret = wc_ecc_check_key(&key);
- }
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_ecc_check_key(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_check_key */
- /*
- * Testing wc_ecc_get_generator()
- */
- static int test_wc_ecc_get_generator(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
- !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
- ecc_point* pt;
- int ret = 0;
- pt = wc_ecc_new_point();
- if (!pt) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1));
- }
- /* Test bad args. */
- if (ret == MP_OKAY) {
- /* Returns Zero for bad arg. */
- ret = wc_ecc_get_generator(pt, -1);
- if (ret != MP_OKAY)
- wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1));
- if (ret != MP_OKAY)
- wc_ecc_get_generator(pt, 1000); /* If we ever get to 1000 curves
- * increase this number */
- if (ret != MP_OKAY)
- wc_ecc_get_generator(NULL, -1);
- ret = (ret == MP_OKAY) ? WOLFSSL_FATAL_ERROR : 0;
- }
- wc_ecc_del_point(pt);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_get_generator */
- /*
- * Testing wc_ecc_size()
- */
- static int test_wc_ecc_size(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecc_key key;
- int ret;
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY14, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- if (ret == 0) {
- ret = wc_ecc_size(&key);
- if (ret == KEY14) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- /* Returns Zero for bad arg. */
- ret = wc_ecc_size(NULL);
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_size */
- static int test_wc_ecc_params(void)
- {
- int res = TEST_SKIPPED;
- /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
- It was added after certifications */
- #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- const ecc_set_type* ecc_set;
- #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
- /* Test for SECP256R1 curve */
- int curve_id = ECC_SECP256R1;
- int curve_idx = wc_ecc_get_curve_idx(curve_id);
- AssertIntNE(curve_idx, ECC_CURVE_INVALID);
- ecc_set = wc_ecc_get_curve_params(curve_idx);
- AssertNotNull(ecc_set);
- AssertIntEQ(ecc_set->id, curve_id);
- #endif
- /* Test case when SECP256R1 is not enabled */
- /* Test that we get curve params for index 0 */
- ecc_set = wc_ecc_get_curve_params(0);
- AssertNotNull(ecc_set);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
- return res;
- }
- /*
- * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
- */
- static int test_wc_ecc_signVerify_hash(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecc_key key;
- int ret;
- int signH = WOLFSSL_FATAL_ERROR;
- #ifdef HAVE_ECC_VERIFY
- int verifyH = WOLFSSL_FATAL_ERROR;
- int verify = 0;
- #endif
- word32 siglen = ECC_BUFSIZE;
- byte sig[ECC_BUFSIZE];
- byte adjustedSig[ECC_BUFSIZE+1];
- byte digest[] = TEST_STRING;
- word32 digestlen = (word32)TEST_STRING_SZ;
- /* Init stack var */
- XMEMSET(sig, 0, siglen);
- XMEMSET(&key, 0, sizeof(key));
- XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
- /* Init structs. */
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY14, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- if (ret == 0) {
- ret = wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key);
- }
- /* Check bad args. */
- if (ret == 0) {
- signH = wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key);
- if (signH == ECC_BAD_ARG_E) {
- signH = wc_ecc_sign_hash(digest, digestlen, NULL, &siglen,
- &rng, &key);
- }
- if (signH == ECC_BAD_ARG_E) {
- signH = wc_ecc_sign_hash(digest, digestlen, sig, NULL,
- &rng, &key);
- }
- if (signH == ECC_BAD_ARG_E) {
- signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
- NULL, &key);
- }
- if (signH == ECC_BAD_ARG_E) {
- signH = wc_ecc_sign_hash(digest, digestlen, sig, &siglen,
- &rng, NULL);
- }
- if (signH == ECC_BAD_ARG_E) {
- signH = 0;
- }
- else if (ret == 0) {
- signH = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifdef HAVE_ECC_VERIFY
- ret = wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify, &key);
- if (verify != 1 && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- /* test check on length of signature passed in */
- XMEMCPY(adjustedSig, sig, siglen);
- adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
- #ifndef NO_STRICT_ECDSA_LEN
- AssertIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
- &verify, &key), 0);
- #else
- /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
- * is allowed */
- AssertIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
- &verify, &key), 0);
- #endif
- /* Test bad args. */
- if (ret == 0) {
- verifyH = wc_ecc_verify_hash(NULL, siglen, digest, digestlen,
- &verify, &key);
- if (verifyH == ECC_BAD_ARG_E) {
- verifyH = wc_ecc_verify_hash(sig, siglen, NULL, digestlen,
- &verify, &key);
- }
- if (verifyH == ECC_BAD_ARG_E) {
- verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
- NULL, &key);
- }
- if (verifyH == ECC_BAD_ARG_E) {
- verifyH = wc_ecc_verify_hash(sig, siglen, digest, digestlen,
- &verify, NULL);
- }
- if (verifyH == ECC_BAD_ARG_E) {
- verifyH = 0;
- }
- else if (ret == 0) {
- verifyH = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif /* HAVE_ECC_VERIFY */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0 && signH == 0 && verifyH == 0);
- #endif
- return res;
- } /* END test_wc_ecc_sign_hash */
- /*
- * Testing wc_ecc_shared_secret()
- */
- static int test_wc_ecc_shared_secret(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
- ecc_key key, pubKey;
- WC_RNG rng;
- int ret;
- byte out[KEY32];
- int keySz = sizeof(out);
- word32 outlen = (word32)sizeof(out);
- #if defined(HAVE_ECC) && !defined(NO_ECC256)
- const char* qx =
- "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
- const char* qy =
- "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
- const char* d =
- "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
- const char* curveName = "SECP256R1";
- const byte expected_shared_secret[] =
- {
- 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
- 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
- 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
- 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
- };
- #endif
- PRIVATE_KEY_UNLOCK();
- /* Initialize variables. */
- XMEMSET(out, 0, keySz);
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- XMEMSET(&pubKey, 0, sizeof(pubKey));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_init(&pubKey);
- }
- }
- #if defined(HAVE_ECC) && !defined(NO_ECC256)
- if (ret == 0) {
- ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
- }
- if (ret == 0) {
- ret = wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName);
- }
- #else
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &pubKey);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &pubKey.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- #endif
- #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
- !defined(HAVE_SELFTEST)
- if (ret == 0) {
- ret = wc_ecc_set_rng(&key, &rng);
- }
- #endif
- if (ret == 0) {
- ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
- #if defined(HAVE_ECC) && !defined(NO_ECC256)
- if (ret == 0) {
- if (0 != XMEMCMP(out, expected_shared_secret, outlen)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_shared_secret(NULL, &pubKey, out, &outlen);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_shared_secret(&key, NULL, out, &outlen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_shared_secret(&key, &pubKey, out, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- /* Invalid length */
- outlen = 1;
- ret = wc_ecc_shared_secret(&key, &pubKey, out, &outlen);
- }
- if (ret == BUFFER_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- wc_ecc_free(&pubKey);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- PRIVATE_KEY_LOCK();
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END tests_wc_ecc_shared_secret */
- /*
- * testint wc_ecc_export_x963()
- */
- static int test_wc_ecc_export_x963(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
- ecc_key key;
- WC_RNG rng;
- byte out[ECC_ASN963_MAX_BUF_SZ];
- word32 outlen = sizeof(out);
- int ret = 0;
- PRIVATE_KEY_UNLOCK();
- /* Initialize variables. */
- XMEMSET(out, 0, outlen);
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY20, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- if (ret == 0) {
- ret = wc_ecc_export_x963(&key, out, &outlen);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_export_x963(NULL, out, &outlen);
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_export_x963(&key, NULL, &outlen);
- }
- if (ret == LENGTH_ONLY_E) {
- ret = wc_ecc_export_x963(&key, out, NULL);
- }
- if (ret == ECC_BAD_ARG_E) {
- key.idx = -4;
- ret = wc_ecc_export_x963(&key, out, &outlen);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- PRIVATE_KEY_LOCK();
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_export_x963 */
- /*
- * Testing wc_ecc_export_x963_ex()
- * compile with --enable-compkey will use compression.
- */
- static int test_wc_ecc_export_x963_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
- ecc_key key;
- WC_RNG rng;
- int ret = 0;
- byte out[ECC_ASN963_MAX_BUF_SZ];
- word32 outlen = sizeof(out);
- #ifdef HAVE_COMP_KEY
- word32 badOutLen = 5;
- #endif
- /* Init stack variables. */
- XMEMSET(out, 0, outlen);
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY64, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- #ifdef HAVE_COMP_KEY
- if (ret == 0) {
- ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
- }
- #else
- if (ret == 0) {
- ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
- }
- #endif
- /* Test bad args. */
- #ifdef HAVE_COMP_KEY
- if (ret == 0) {
- ret = wc_ecc_export_x963_ex(NULL, out, &outlen, COMP);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_export_x963_ex(&key, out, NULL, COMP);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP);
- }
- #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
- if (ret == BUFFER_E)
- #else
- if (ret == LENGTH_ONLY_E)
- #endif
- {
- key.idx = -4;
- ret = wc_ecc_export_x963_ex(&key, out, &outlen, COMP);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #else
- if (ret == 0) {
- ret = wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_export_x963_ex(&key, out, &outlen, 1);
- }
- if (ret == NOT_COMPILED_IN) {
- ret = wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP);
- }
- if (ret == BAD_FUNC_ARG) {
- key.idx = -4;
- ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_export_x963_ex */
- /*
- * testing wc_ecc_import_x963()
- */
- static int test_wc_ecc_import_x963(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
- defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
- ecc_key pubKey, key;
- WC_RNG rng;
- byte x963[ECC_ASN963_MAX_BUF_SZ];
- word32 x963Len = (word32)sizeof(x963);
- int ret;
- /* Init stack variables. */
- XMEMSET(x963, 0, x963Len);
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- XMEMSET(&pubKey, 0, sizeof(pubKey));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&pubKey);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- }
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY24, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- if (ret == 0) {
- PRIVATE_KEY_UNLOCK();
- ret = wc_ecc_export_x963(&key, x963, &x963Len);
- PRIVATE_KEY_LOCK();
- }
- }
- if (ret == 0) {
- ret = wc_ecc_import_x963(x963, x963Len, &pubKey);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_import_x963(NULL, x963Len, &pubKey);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_x963(x963, x963Len, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_x963(x963, x963Len + 1, &pubKey);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- wc_ecc_free(&pubKey);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END wc_ecc_import_x963 */
- /*
- * testing wc_ecc_import_private_key()
- */
- static int ecc_import_private_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
- defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
- ecc_key key, keyImp;
- WC_RNG rng;
- byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
- byte x963Key[ECC_ASN963_MAX_BUF_SZ];
- word32 privKeySz = (word32)sizeof(privKey);
- word32 x963KeySz = (word32)sizeof(x963Key);
- int ret;
- /* Init stack variables. */
- XMEMSET(privKey, 0, privKeySz);
- XMEMSET(x963Key, 0, x963KeySz);
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- XMEMSET(&keyImp, 0, sizeof(keyImp));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_init(&keyImp);
- }
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY48, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- if (ret == 0) {
- PRIVATE_KEY_UNLOCK();
- ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
- PRIVATE_KEY_LOCK();
- }
- if (ret == 0) {
- ret = wc_ecc_export_private_only(&key, privKey, &privKeySz);
- }
- }
- if (ret == 0) {
- ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
- x963KeySz, &keyImp);
- }
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_ecc_import_private_key(privKey, privKeySz, x963Key,
- x963KeySz, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_private_key(NULL, privKeySz, x963Key,
- x963KeySz, &keyImp);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- wc_ecc_free(&keyImp);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END wc_ecc_import_private_key */
- /*
- * Testing wc_ecc_export_private_only()
- */
- static int test_wc_ecc_export_private_only(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
- ecc_key key;
- WC_RNG rng;
- byte out[ECC_PRIV_KEY_BUF];
- word32 outlen = sizeof(out);
- int ret;
- /* Init stack variables. */
- XMEMSET(out, 0, outlen);
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY32, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- if (ret == 0) {
- ret = wc_ecc_export_private_only(&key, out, &outlen);
- }
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_ecc_export_private_only(NULL, out, &outlen);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_export_private_only(&key, NULL, &outlen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_export_private_only(&key, out, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_export_private_only */
- /*
- * Testing wc_ecc_rs_to_sig()
- */
- static int test_wc_ecc_rs_to_sig(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(NO_ASN)
- /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
- const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
- const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
- const char* zeroStr = "0";
- byte sig[ECC_MAX_SIG_SIZE];
- word32 siglen = (word32)sizeof(sig);
- /*R and S max size is the order of curve. 2^192.*/
- int keySz = KEY24;
- byte r[KEY24];
- byte s[KEY24];
- word32 rlen = (word32)sizeof(r);
- word32 slen = (word32)sizeof(s);
- int ret;
- /* Init stack variables. */
- XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
- XMEMSET(r, 0, keySz);
- XMEMSET(s, 0, keySz);
- ret = wc_ecc_rs_to_sig(R, S, sig, &siglen);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_rs_to_sig(NULL, S, sig, &siglen);
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_rs_to_sig(R, NULL, sig, &siglen);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_rs_to_sig(R, S, sig, NULL);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_rs_to_sig(R, S, NULL, &siglen);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen);
- }
- if (ret == MP_ZERO_E) {
- ret = wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen);
- }
- if (ret == MP_ZERO_E) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen);
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_rs_to_sig */
- static int test_wc_ecc_import_raw(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(NO_ECC256)
- ecc_key key;
- int ret = 0;
- const char* qx =
- "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
- const char* qy =
- "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
- const char* d =
- "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
- const char* curveName = "SECP256R1";
- #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
- const char* kNullStr = "";
- #endif
- ret = wc_ecc_init(&key);
- /* Test good import */
- if (ret == 0) {
- ret = wc_ecc_import_raw(&key, qx, qy, d, curveName);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_import_raw(NULL, qx, qy, d, curveName);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_raw(&key, NULL, qy, d, curveName);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_raw(&key, qx, NULL, d, curveName);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_raw(&key, qx, qy, d, NULL);
- }
- #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
- if (ret == BAD_FUNC_ARG) {
- #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
- wc_ecc_free(&key);
- #endif
- ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr, curveName);
- if (ret == ECC_INF_E)
- ret = BAD_FUNC_ARG; /* This is expected by other tests */
- }
- #endif
- #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- if (ret == BAD_FUNC_ARG) {
- #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
- wc_ecc_free(&key);
- #endif
- ret = wc_ecc_import_raw(&key, "0", qy, d, curveName);
- /* Note: SP math "is point" failure returns MP_VAL */
- if (ret == ECC_INF_E || ret == MP_VAL) {
- ret = BAD_FUNC_ARG; /* This is expected by other tests */
- }
- }
- if (ret == BAD_FUNC_ARG) {
- #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
- wc_ecc_free(&key);
- #endif
- ret = wc_ecc_import_raw(&key, qx, "0", d, curveName);
- /* Note: SP math "is point" failure returns MP_VAL */
- if (ret == ECC_INF_E || ret == MP_VAL) {
- ret = BAD_FUNC_ARG; /* This is expected by other tests */
- }
- }
- #endif
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- wc_ecc_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_import_raw */
- static int test_wc_ecc_import_unsigned(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
- (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
- ecc_key key;
- const byte qx[] = {
- 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
- 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
- 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
- 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
- };
- const byte qy[] = {
- 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
- 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
- 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
- 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
- };
- const byte d[] = {
- 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
- 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
- 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
- 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
- };
- #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
- const byte nullBytes[32] = {0};
- #endif
- int curveId = ECC_SECP256R1;
- int ret;
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
- curveId);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
- curveId);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
- curveId);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
- curveId);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
- ECC_CURVE_INVALID);
- }
- #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
- (byte*)nullBytes, (byte*)nullBytes, curveId);
- }
- #endif
- if (ret == BAD_FUNC_ARG || ret == ECC_INF_E) {
- ret = 0;
- }
- }
- wc_ecc_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_import_unsigned */
- /*
- * Testing wc_ecc_sig_size()
- */
- static int test_wc_ecc_sig_size(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- ecc_key key;
- WC_RNG rng;
- int keySz = KEY16;
- int ret = 0;
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- if (ret == 0) {
- ret = wc_ecc_sig_size(&key);
- if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
- ret = 0;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_sig_size */
- /*
- * Testing wc_ecc_ctx_new()
- */
- static int test_wc_ecc_ctx_new(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
- WC_RNG rng;
- int ret = 0;
- ecEncCtx* cli = NULL;
- ecEncCtx* srv = NULL;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng);
- srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng);
- }
- if (ret == 0 && (cli == NULL || srv == NULL)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_ctx_free(cli);
- wc_ecc_ctx_free(srv);
- /* Test bad args. */
- if (ret == 0) {
- /* wc_ecc_ctx_new_ex() will free if returned NULL. */
- cli = wc_ecc_ctx_new(0, &rng);
- if (cli != NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL);
- if (cli != NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_ctx_free(cli);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_ctx_new */
- /*
- * Tesing wc_ecc_reset()
- */
- static int test_wc_ecc_ctx_reset(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
- ecEncCtx* ctx = NULL;
- WC_RNG rng;
- int ret = 0;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_ecc_ctx_reset(ctx, &rng);
- }
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_ecc_ctx_reset(NULL, &rng);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_ctx_reset(ctx, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_ctx_free(ctx);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_ctx_reset */
- /*
- * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
- */
- static int test_wc_ecc_ctx_set_peer_salt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecEncCtx* cliCtx = NULL;
- ecEncCtx* servCtx = NULL;
- const byte* cliSalt = NULL;
- const byte* servSalt = NULL;
- int ret = 0;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- if ( ( (cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) ||
- ( (servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng)) == NULL) ) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- cliSalt = wc_ecc_ctx_get_own_salt(NULL);
- if (cliSalt != NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- cliSalt = wc_ecc_ctx_get_own_salt(cliCtx);
- servSalt = wc_ecc_ctx_get_own_salt(servCtx);
- if (cliSalt == NULL || servSalt == NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_ecc_ctx_set_peer_salt(cliCtx, servSalt);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_ctx_set_peer_salt(NULL, servSalt);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_ctx_set_peer_salt(cliCtx, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_ctx_free(cliCtx);
- wc_ecc_ctx_free(servCtx);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_ctx_set_peer_salt */
- /*
- * Testing wc_ecc_ctx_set_info()
- */
- static int test_wc_ecc_ctx_set_info(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
- ecEncCtx* ctx = NULL;
- WC_RNG rng;
- int ret;
- const char* optInfo = "Optional Test Info.";
- int optInfoSz = (int)XSTRLEN(optInfo);
- const char* badOptInfo = NULL;
- ret = wc_InitRng(&rng);
- if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL || ret != 0 ) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_ctx_free(ctx);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_ctx_set_info */
- /*
- * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
- */
- static int test_wc_ecc_encryptDecrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
- defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
- ecc_key srvKey, cliKey, tmpKey;
- WC_RNG rng;
- int ret;
- const char* msg = "EccBlock Size 16";
- word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
- #ifdef WOLFSSL_ECIES_OLD
- byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
- #elif defined(WOLFSSL_ECIES_GEN_IV)
- byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
- (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
- #else
- byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
- #endif
- word32 outSz = (word32)sizeof(out);
- byte plain[sizeof("EccBlock Size 16")];
- word32 plainSz = (word32)sizeof(plain);
- int keySz = KEY20;
- /* Init stack variables. */
- XMEMSET(out, 0, outSz);
- XMEMSET(plain, 0, plainSz);
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&srvKey, 0, sizeof(srvKey));
- XMEMSET(&cliKey, 0, sizeof(cliKey));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&cliKey);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &cliKey);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- if (ret == 0) {
- ret = wc_ecc_init(&srvKey);
- }
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &srvKey);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- if (ret == 0) {
- ret = wc_ecc_init(&tmpKey);
- }
- }
- #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
- !defined(HAVE_SELFTEST)
- if (ret == 0) {
- ret = wc_ecc_set_rng(&srvKey, &rng);
- }
- if (ret == 0) {
- ret = wc_ecc_set_rng(&cliKey, &rng);
- }
- #endif
- if (ret == 0) {
- ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
- &outSz, NULL);
- }
- if (ret == 0) {
- ret = wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out,
- &outSz, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out,
- &outSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out,
- &outSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
- &outSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
- NULL, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #ifdef WOLFSSL_ECIES_OLD
- if (ret == 0) {
- tmpKey.dp = cliKey.dp;
- ret = wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey);
- }
- #endif
- if (ret == 0) {
- ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain,
- &plainSz, NULL);
- }
- if (ret == 0) {
- ret = wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain,
- &plainSz, NULL);
- #ifdef WOLFSSL_ECIES_OLD
- /* NULL parameter allowed in new implementations - public key comes from
- * the message. */
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
- &plainSz, NULL);
- }
- #endif
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain,
- &plainSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL,
- &plainSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz,
- plain, NULL, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (XMEMCMP(msg, plain, msgSz) != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&tmpKey);
- wc_ecc_free(&cliKey);
- wc_ecc_free(&srvKey);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_encryptDecrypt */
- /*
- * Testing wc_ecc_del_point() and wc_ecc_new_point()
- */
- static int test_wc_ecc_del_point(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC)
- ecc_point* pt;
- pt = wc_ecc_new_point();
- wc_ecc_del_point(pt);
- res = TEST_RES_CHECK(pt != NULL);
- #endif
- return res;
- } /* END test_wc_ecc_del_point */
- /*
- * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
- * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
- * and wc_ecc_cmp_point()
- */
- static int test_wc_ecc_pointFns(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
- !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
- !defined(WOLFSSL_ATECC608A)
- ecc_key key;
- WC_RNG rng;
- int ret;
- ecc_point* point = NULL;
- ecc_point* cpypt = NULL;
- int idx = 0;
- int keySz = KEY32;
- byte der[DER_SZ(KEY32)];
- word32 derlenChk = 0;
- word32 derSz = DER_SZ(KEY32);
- /* Init stack variables. */
- XMEMSET(der, 0, derSz);
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- if (ret == 0) {
- point = wc_ecc_new_point();
- if (!point) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- cpypt = wc_ecc_new_point();
- if (!cpypt) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Export */
- if (ret == 0) {
- ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
- NULL, &derlenChk);
- /* Check length value. */
- if (derSz == derlenChk && ret == LENGTH_ONLY_E) {
- ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
- der, &derSz);
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz);
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_export_point_der((idx = key.idx), &key.pubkey,
- der, NULL);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Import */
- if (ret == 0) {
- ret = wc_ecc_import_point_der(der, derSz, idx, point);
- /* Condition double checks wc_ecc_cmp_point(). */
- if (ret == 0 &&
- XMEMCMP((void *)&key.pubkey, (void *)point, sizeof(key.pubkey))) {
- ret = wc_ecc_cmp_point(&key.pubkey, point);
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_import_point_der(NULL, derSz, idx, point);
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_import_point_der(der, derSz, idx, NULL);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_import_point_der(der, derSz, -1, point);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_import_point_der(der, derSz + 1, idx, point);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Copy */
- if (ret == 0) {
- ret = wc_ecc_copy_point(point, cpypt);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_copy_point(NULL, cpypt);
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_copy_point(point, NULL);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Compare point */
- if (ret == 0) {
- ret = wc_ecc_cmp_point(point, cpypt);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_cmp_point(NULL, cpypt);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_cmp_point(point, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* At infinity if return == 1, otherwise return == 0. */
- if (ret == 0) {
- ret = wc_ecc_point_is_at_infinity(point);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_point_is_at_infinity(NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- #ifdef USE_ECC_B_PARAM
- /* On curve if ret == 0 */
- if (ret == 0) {
- ret = wc_ecc_point_is_on_curve(point, idx);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_point_is_on_curve(NULL, idx);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_point_is_on_curve(point, 1000);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- #endif /* USE_ECC_B_PARAM */
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- /* Free */
- wc_ecc_del_point(point);
- wc_ecc_del_point(cpypt);
- wc_ecc_free(&key);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_pointFns */
- /*
- * Testing wc_ecc_sahred_secret_ssh()
- */
- static int test_wc_ecc_shared_secret_ssh(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
- !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
- !defined(WOLFSSL_ATECC608A)
- ecc_key key, key2;
- WC_RNG rng;
- int ret;
- int keySz = KEY32;
- int key2Sz = KEY24;
- byte secret[KEY32];
- word32 secretLen = keySz;
- /* Init stack variables. */
- XMEMSET(secret, 0, secretLen);
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- XMEMSET(&key2, 0, sizeof(key2));
- /* Make keys */
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key2);
- }
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, key2Sz, &key2);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
- !defined(HAVE_SELFTEST)
- if (ret == 0) {
- ret = wc_ecc_set_rng(&key, &rng);
- }
- #endif
- if (ret == 0) {
- ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
- }
- /* Pass in bad args. */
- if (ret == 0) {
- ret = wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret, &secretLen);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- key.type = ECC_PUBLICKEY;
- ret = wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, &secretLen);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- wc_ecc_free(&key2);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_shared_secret_ssh */
- /*
- * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
- */
- static int test_wc_ecc_verify_hash_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
- && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
- !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
- ecc_key key;
- WC_RNG rng;
- int ret;
- mp_int r;
- mp_int s;
- mp_int z;
- unsigned char hash[] = "Everyone gets Friday off.EccSig";
- unsigned char iHash[] = "Everyone gets Friday off.......";
- unsigned char shortHash[] = TEST_STRING;
- word32 hashlen = sizeof(hash);
- word32 iHashLen = sizeof(iHash);
- word32 shortHashLen = sizeof(shortHash);
- int keySz = KEY32;
- int sig = WOLFSSL_FATAL_ERROR;
- int ver = WOLFSSL_FATAL_ERROR;
- int verify_ok = 0;
- /* Initialize r and s. */
- ret = mp_init_multi(&r, &s, &z, NULL, NULL, NULL);
- if (ret != MP_OKAY) {
- return MP_INIT_E;
- }
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- if (ret == 0) {
- ret = wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s);
- if (ret == 0) {
- /* verify_ok should be 1. */
- ret = wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key);
- if (verify_ok != 1 && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- /* verify_ok should be 0 */
- ret = wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen,
- &verify_ok, &key);
- if (verify_ok != 0 && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- /* verify_ok should be 0. */
- ret = wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
- &verify_ok, &key);
- if (verify_ok != 0 && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- if (wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s)
- == ECC_BAD_ARG_E) {
- sig = 0;
- }
- if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s)
- != ECC_BAD_ARG_E) {
- sig = WOLFSSL_FATAL_ERROR;
- }
- if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s)
- != ECC_BAD_ARG_E) {
- sig = WOLFSSL_FATAL_ERROR;
- }
- if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s)
- != ECC_BAD_ARG_E) {
- sig = WOLFSSL_FATAL_ERROR;
- }
- if (sig == 0 && wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL)
- != ECC_BAD_ARG_E) {
- sig = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- if (wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen, &verify_ok, &key)
- == ECC_BAD_ARG_E) {
- ver = 0;
- }
- if (ver == 0 && wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
- &verify_ok, &key) != ECC_BAD_ARG_E) {
- ver = WOLFSSL_FATAL_ERROR;
- }
- if (wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen, &verify_ok, &key)
- != MP_ZERO_E) {
- ver = WOLFSSL_FATAL_ERROR;
- }
- if (wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen, &verify_ok, &key)
- != MP_ZERO_E) {
- ver = WOLFSSL_FATAL_ERROR;
- }
- if (wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen, &verify_ok, &key)
- != MP_ZERO_E) {
- ver = WOLFSSL_FATAL_ERROR;
- }
- if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
- &key) != ECC_BAD_ARG_E) {
- ver = WOLFSSL_FATAL_ERROR;
- }
- if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
- NULL, &key) != ECC_BAD_ARG_E) {
- ver = WOLFSSL_FATAL_ERROR;
- }
- if (ver == 0 && wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
- &verify_ok, NULL) != ECC_BAD_ARG_E) {
- ver = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_ecc_free(&key);
- mp_free(&r);
- mp_free(&s);
- if (wc_FreeRng(&rng)) {
- return WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0 && (sig != 0 || ver != 0)) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_verify_hash_ex */
- /*
- * Testing wc_ecc_mulmod()
- */
- static int test_wc_ecc_mulmod(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
- !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
- defined(WOLFSSL_VALIDATE_ECC_IMPORT))
- ecc_key key1, key2, key3;
- WC_RNG rng;
- int ret = 0;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key1);
- if (ret == 0) {
- ret = wc_ecc_init(&key2);
- }
- if (ret == 0) {
- ret = wc_ecc_init(&key3);
- }
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY32, &key1);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- wc_FreeRng(&rng);
- }
- if (ret == 0) {
- ret = wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy, key1.dp->Af,
- ECC_SECP256R1);
- if (ret == 0) {
- ret = wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
- key1.dp->prime, ECC_SECP256R1);
- }
- }
- if (ret == 0) {
- ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey, &key2.k,
- &key3.k, 1);
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey, &key2.k,
- &key3.k, 1);
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_mulmod(&key1.k, NULL, &key3.pubkey, &key2.k,
- &key3.k, 1);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, NULL, &key2.k,
- &key3.k, 1);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = wc_ecc_mulmod(&key1.k, &key2.pubkey, &key3.pubkey,
- &key2.k, NULL, 1);
- }
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- else if (ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_ecc_free(&key1);
- wc_ecc_free(&key2);
- wc_ecc_free(&key3);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0);
- #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
- return res;
- } /* END test_wc_ecc_mulmod */
- /*
- * Testing wc_ecc_is_valid_idx()
- */
- static int test_wc_ecc_is_valid_idx(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- ecc_key key;
- WC_RNG rng;
- int ret;
- int iVal = -2;
- int iVal2 = 3000;
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&key, 0, sizeof(key));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, 32, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- }
- if (ret == 0) {
- ret = wc_ecc_is_valid_idx(key.idx);
- if (ret == 1) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_ecc_is_valid_idx(iVal); /* should return 0 */
- if (ret == 0) {
- ret = wc_ecc_is_valid_idx(iVal2);
- }
- if (ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_is_valid_idx */
- /*
- * Testing wc_ecc_get_curve_id_from_oid()
- */
- static int test_wc_ecc_get_curve_id_from_oid(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
- !defined(HAVE_FIPS)
- const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
- word32 len = sizeof(oid);
- int ret;
- /* Bad Cases */
- ret = wc_ecc_get_curve_id_from_oid(NULL, len);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- if (ret == 0) {
- ret = wc_ecc_get_curve_id_from_oid(oid, 0);
- if (ret == ECC_CURVE_INVALID) {
- ret = 0;
- }
- }
- /* Good Case */
- if (ret == 0) {
- ret = wc_ecc_get_curve_id_from_oid(oid, len);
- if (ret == ECC_SECP256R1) {
- ret = 0;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* END test_wc_ecc_get_curve_id_from_oid */
- /*
- * Testing wc_ecc_sig_size_calc()
- */
- static int test_wc_ecc_sig_size_calc(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
- ecc_key key;
- WC_RNG rng;
- int sz = 0;
- int ret = 0;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, 16, &key);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- sz = key.dp->size;
- }
- if (ret == 0) {
- ret = wc_ecc_sig_size_calc(sz);
- if (ret > 0) {
- ret = 0;
- }
- }
- wc_ecc_free(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_ecc_sig_size_calc */
- /*
- * Testing ToTraditional
- */
- static int test_ToTraditional(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
- (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
- defined(OPENSSL_EXTRA_X509_SMALL))
- XFILE f;
- byte input[TWOK_BUF];
- word32 sz;
- int ret;
- f = XFOPEN("./certs/server-keyPkcs8.der", "rb");
- AssertTrue((f != XBADFILE));
- sz = (word32)XFREAD(input, 1, sizeof(input), f);
- XFCLOSE(f);
- /* Good case */
- ret = ToTraditional(input, sz);
- if (ret > 0) {
- ret = 0;
- }
- /* Bad cases */
- if (ret == 0) {
- ret = ToTraditional(NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = ToTraditional(NULL, sz);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = ToTraditional(input, 0);
- if (ret == ASN_PARSE_E || ret == BUFFER_E) {
- ret = 0;
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_ToTraditional*/
- /*
- * Testing wc_EccPrivateKeyToDer
- */
- static int test_wc_EccPrivateKeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
- byte output[ONEK_BUF];
- ecc_key eccKey;
- WC_RNG rng;
- word32 inLen;
- int ret;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ecc_init(&eccKey);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- inLen = (word32)sizeof(output);
- /* Bad Cases */
- if (ret == 0) {
- ret = wc_EccPrivateKeyToDer(NULL, NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_EccPrivateKeyToDer(NULL, output, inLen);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_EccPrivateKeyToDer(&eccKey, NULL, inLen);
- if (ret == LENGTH_ONLY_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_EccPrivateKeyToDer(&eccKey, output, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /*Good Case */
- if (ret == 0) {
- ret = wc_EccPrivateKeyToDer(&eccKey, output, inLen);
- if (ret > 0) {
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
- /* test importing private only into a PKEY struct */
- EC_KEY* ec;
- EVP_PKEY* pkey;
- const unsigned char* der = output;
- pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, ret);
- AssertNotNull(pkey);
- der = output;
- ec = d2i_ECPrivateKey(NULL, &der, ret);
- AssertNotNull(ec);
- AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
- EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
- #endif
- ret = 0;
- }
- }
- wc_ecc_free(&eccKey);
- }
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_wc_EccPrivateKeyToDer*/
- /*
- * Testing wc_DhPublicKeyDecode
- */
- static int test_wc_DhPublicKeyDecode(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_DH
- #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
- DhKey key;
- word32 inOutIdx;
- AssertIntEQ(wc_InitDhKey(&key), 0);
- AssertIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
- BAD_FUNC_ARG);
- inOutIdx = 0;
- AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
- BAD_FUNC_ARG);
- inOutIdx = 0;
- AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
- BAD_FUNC_ARG);
- inOutIdx = 0;
- AssertIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
- sizeof_dh_pub_key_der_2048), 0);
- AssertTrue(key.p.used != 0 && key.g.used != 0 && key.q.used == 0 &&
- key.pub.used != 0 && key.priv.used == 0);
- wc_FreeDhKey(&key);
- res = TEST_RES_CHECK(1);
- #endif
- #endif /* !NO_DH */
- return res;
- }
- /*
- * Testing wc_Ed25519KeyToDer
- */
- static int test_wc_Ed25519KeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
- (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
- byte output[ONEK_BUF];
- ed25519_key ed25519Key;
- WC_RNG rng;
- word32 inLen;
- int ret;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed25519_init(&ed25519Key);
- if (ret == 0) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
- }
- inLen = (word32)sizeof(output);
- /* Bad Cases */
- if (ret == 0) {
- ret = wc_Ed25519KeyToDer(NULL, NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed25519KeyToDer(NULL, output, inLen);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed25519KeyToDer(&ed25519Key, output, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Good Cases */
- if (ret == 0) {
- /* length only */
- ret = wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen);
- if (ret > 0) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed25519KeyToDer(&ed25519Key, output, inLen);
- if (ret > 0) {
- ret = 0;
- }
- }
- wc_ed25519_free(&ed25519Key);
- }
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_wc_Ed25519KeyToDer*/
- /*
- * Testing wc_Ed25519PrivateKeyToDer
- */
- static int test_wc_Ed25519PrivateKeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
- (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
- byte output[ONEK_BUF];
- ed25519_key ed25519PrivKey;
- WC_RNG rng;
- word32 inLen;
- int ret;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed25519_init(&ed25519PrivKey);
- if (ret == 0) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey);
- }
- inLen = (word32)sizeof(output);
- /* Bad Cases */
- if (ret == 0) {
- ret = wc_Ed25519PrivateKeyToDer(NULL, NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed25519PrivateKeyToDer(NULL, output, inLen);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Good Cases */
- if (ret == 0) {
- /* length only */
- ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen);
- if (ret > 0) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen);
- if (ret > 0) {
- ret = 0;
- }
- }
- wc_ed25519_free(&ed25519PrivKey);
- }
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_wc_Ed25519PrivateKeyToDer*/
- /*
- * Testing wc_Ed448KeyToDer
- */
- static int test_wc_Ed448KeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
- (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
- byte output[ONEK_BUF];
- ed448_key ed448Key;
- WC_RNG rng;
- word32 inLen;
- int ret;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed448_init(&ed448Key);
- if (ret == 0) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
- }
- inLen = sizeof(output);
- /* Bad Cases */
- if (ret == 0) {
- ret = wc_Ed448KeyToDer(NULL, NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed448KeyToDer(NULL, output, inLen);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed448KeyToDer(&ed448Key, output, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Good Cases */
- if (ret == 0) {
- /* length only */
- ret = wc_Ed448KeyToDer(&ed448Key, NULL, inLen);
- if (ret > 0) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed448KeyToDer(&ed448Key, output, inLen);
- if (ret > 0) {
- ret = 0;
- }
- }
- wc_ed448_free(&ed448Key);
- }
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_wc_Ed448KeyToDer*/
- /*
- * Testing wc_Ed448PrivateKeyToDer
- */
- static int test_wc_Ed448PrivateKeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
- (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
- byte output[ONEK_BUF];
- ed448_key ed448PrivKey;
- WC_RNG rng;
- word32 inLen;
- int ret;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed448_init(&ed448PrivKey);
- if (ret == 0) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey);
- }
- inLen = sizeof(output);
- /* Bad Cases */
- if (ret == 0) {
- ret = wc_Ed448PrivateKeyToDer(NULL, NULL, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed448PrivateKeyToDer(NULL, output, inLen);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- /* Good cases */
- if (ret == 0) {
- /* length only */
- ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen);
- if (ret > 0) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen);
- if (ret > 0) {
- ret = 0;
- }
- }
- wc_ed448_free(&ed448PrivKey);
- }
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_wc_Ed448PrivateKeyToDer*/
- /*
- * Testing wc_SetSubjectBuffer
- */
- static int test_wc_SetSubjectBuffer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- Cert cert;
- FILE* file;
- byte* der;
- word32 derSz;
- int ret = 0;
- derSz = FOURK_BUF;
- der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- if (der == NULL) {
- ret = -1;
- }
- if (ret == 0) {
- file = XFOPEN("./certs/ca-cert.der", "rb");
- if (file != NULL) {
- derSz = (word32)XFREAD(der, 1, FOURK_BUF, file);
- XFCLOSE(file);
- }
- else {
- ret = -1;
- }
- }
- if (ret == 0) {
- ret = wc_InitCert(&cert);
- }
- if (ret == 0) {
- ret = wc_SetSubjectBuffer(&cert, der, derSz);
- }
- if (ret == 0) {
- ret = wc_SetSubjectBuffer(NULL, der, derSz);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_wc_SetSubjectBuffer*/
- /*
- * Testing wc_SetSubjectKeyIdFromPublicKey_ex
- */
- static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
- WC_RNG rng;
- Cert cert;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
- ed25519_key ed25519Key;
- #endif
- #if !defined(NO_RSA) && defined(HAVE_RSA)
- RsaKey rsaKey;
- int bits = 2048;
- #endif
- #if defined(HAVE_ECC)
- ecc_key eccKey;
- #endif
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
- ed448_key ed448Key;
- #endif
- int ret = 0;
- #ifndef HAVE_FIPS
- ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
- #else
- ret = wc_InitRng(&rng);
- #endif
- wc_InitCert(&cert);
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
- if (ret == 0) { /*ED25519*/
- ret = wc_ed25519_init(&ed25519Key);
- if (ret == 0) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
- }
- if (ret == 0) {
- ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
- &ed25519Key);
- }
- wc_ed25519_free(&ed25519Key);
- }
- #endif
- #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
- if (ret == 0) { /*RSA*/
- ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
- if (ret == 0) {
- MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
- }
- if (ret == 0) {
- ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
- }
- wc_FreeRsaKey(&rsaKey);
- }
- #endif
- #if defined(HAVE_ECC)
- if (ret == 0) { /*ECC*/
- ret = wc_ecc_init(&eccKey);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- if (ret == 0) {
- ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
- }
- wc_ecc_free(&eccKey);
- }
- #endif
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
- if (ret == 0) { /*ED448*/
- ret = wc_ed448_init(&ed448Key);
- if (ret == 0) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
- }
- if (ret == 0) {
- ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
- &ed448Key);
- }
- wc_ed448_free(&ed448Key);
- }
- #endif
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
- /*
- * Testing wc_SetAuthKeyIdFromPublicKey_ex
- */
- static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
- WC_RNG rng;
- Cert cert;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
- ed25519_key ed25519Key;
- #endif
- #if !defined(NO_RSA) && defined(HAVE_RSA)
- RsaKey rsaKey;
- int bits = 2048;
- #endif
- #if defined(HAVE_ECC)
- ecc_key eccKey;
- #endif
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
- ed448_key ed448Key;
- #endif
- int ret = 0;
- #ifndef HAVE_FIPS
- ret = wc_InitRng_ex(&rng, HEAP_HINT, testDevId);
- #else
- ret = wc_InitRng(&rng);
- #endif
- wc_InitCert(&cert);
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
- if (ret == 0) { /*ED25519*/
- ret = wc_ed25519_init(&ed25519Key);
- if (ret == 0) {
- ret = wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key);
- }
- if (ret == 0) {
- ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
- &ed25519Key);
- }
- wc_ed25519_free(&ed25519Key);
- }
- #endif
- #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
- if (ret == 0) { /*RSA*/
- ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
- if (ret == 0) {
- MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng);
- }
- if (ret == 0) {
- ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey);
- }
- wc_FreeRsaKey(&rsaKey);
- }
- #endif
- #if defined(HAVE_ECC)
- if (ret == 0) { /*ECC*/
- ret = wc_ecc_init(&eccKey);
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
- #endif
- }
- if (ret == 0) {
- ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
- }
- wc_ecc_free(&eccKey);
- }
- #endif
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
- if (ret == 0) { /*ED448*/
- ret = wc_ed448_init(&ed448Key);
- if (ret == 0) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key);
- }
- if (ret == 0) {
- ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
- &ed448Key);
- }
- wc_ed448_free(&ed448Key);
- }
- #endif
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
- return res;
- }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
- /*
- * Testing wc_PKCS7_New()
- */
- static int test_wc_PKCS7_New(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- pkcs7 = wc_PKCS7_New(NULL, testDevId);
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(pkcs7 != NULL);
- #endif
- return res;
- } /* END test-wc_PKCS7_New */
- /*
- * Testing wc_PKCS7_Init()
- */
- static int test_wc_PKCS7_Init(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- void* heap = NULL;
- pkcs7 = wc_PKCS7_New(heap, testDevId);
- AssertNotNull(pkcs7);
- AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
- /* Pass in bad args. */
- AssertIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test-wc_PKCS7_Init */
- /*
- * Testing wc_PKCS7_InitWithCert()
- */
- static int test_wc_PKCS7_InitWithCert(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- #ifndef NO_RSA
- #if defined(USE_CERT_BUFFERS_2048)
- unsigned char cert[sizeof(client_cert_der_2048)];
- int certSz = (int)sizeof(cert);
- XMEMSET(cert, 0, certSz);
- XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
- #elif defined(USE_CERT_BUFFERS_1024)
- unsigned char cert[sizeof(client_cert_der_1024)];
- int certSz = (int)sizeof(cert);
- XMEMSET(cert, 0, certSz);
- XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
- #else
- unsigned char cert[ONEK_BUF];
- XFILE fp;
- int certSz;
- fp = XFOPEN("./certs/1024/client-cert.der", "rb");
- AssertTrue(fp != XBADFILE);
- certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
- XFCLOSE(fp);
- #endif
- #elif defined(HAVE_ECC)
- #if defined(USE_CERT_BUFFERS_256)
- unsigned char cert[sizeof(cliecc_cert_der_256)];
- int certSz = (int)sizeof(cert);
- XMEMSET(cert, 0, certSz);
- XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
- #else
- unsigned char cert[ONEK_BUF];
- XFILE fp;
- int certSz;
- fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
- AssertTrue(fp != XBADFILE);
- certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256), fp);
- XFCLOSE(fp);
- #endif
- #else
- #error PKCS7 requires ECC or RSA
- #endif
- #ifdef HAVE_ECC
- {
- /* bad test case from ZD 11011, malformed cert gives bad ECC key */
- static unsigned char certWithInvalidEccKey[] = {
- 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
- 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
- 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
- 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
- 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
- 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
- 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
- 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
- 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
- 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
- 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
- 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
- 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
- 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
- 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
- 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
- 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
- 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
- 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
- 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
- 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
- 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
- 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
- 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
- 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
- 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
- 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
- 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
- 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
- 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
- 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
- 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
- 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
- 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
- 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
- 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
- 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
- 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
- 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
- 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
- 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
- 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
- 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
- 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
- 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
- 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
- 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
- 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
- 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
- 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
- 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
- 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
- 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
- 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
- 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
- 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
- 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
- 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
- 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
- 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
- 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
- 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
- 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
- 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
- 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
- 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
- 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
- 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
- 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
- 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
- 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
- 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
- 0x08, 0xA8, 0xB4};
- #endif
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- /* If initialization is not successful, it's free'd in init func. */
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
- wc_PKCS7_Free(pkcs7);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- /* Valid initialization usage. */
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- /* Pass in bad args. No need free for null checks, free at end.*/
- AssertIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
- BAD_FUNC_ARG);
- #ifdef HAVE_ECC
- AssertIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
- sizeof(certWithInvalidEccKey)), 0);
- }
- #endif
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_PKCS7_InitWithCert */
- /*
- * Testing wc_PKCS7_EncodeData()
- */
- static int test_wc_PKCS7_EncodeData(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- byte output[FOURK_BUF];
- byte data[] = "My encoded DER cert.";
- #ifndef NO_RSA
- #if defined(USE_CERT_BUFFERS_2048)
- unsigned char cert[sizeof(client_cert_der_2048)];
- unsigned char key[sizeof(client_key_der_2048)];
- int certSz = (int)sizeof(cert);
- int keySz = (int)sizeof(key);
- XMEMSET(cert, 0, certSz);
- XMEMSET(key, 0, keySz);
- XMEMCPY(cert, client_cert_der_2048, certSz);
- XMEMCPY(key, client_key_der_2048, keySz);
- #elif defined(USE_CERT_BUFFERS_1024)
- unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
- unsigned char key[sizeof_client_key_der_1024];
- int certSz = (int)sizeof(cert);
- int keySz = (int)sizeof(key);
- XMEMSET(cert, 0, certSz);
- XMEMSET(key, 0, keySz);
- XMEMCPY(cert, client_cert_der_1024, certSz);
- XMEMCPY(key, client_key_der_1024, keySz);
- #else
- unsigned char cert[ONEK_BUF];
- unsigned char key[ONEK_BUF];
- XFILE fp;
- int certSz;
- int keySz;
- fp = XFOPEN("./certs/1024/client-cert.der", "rb");
- AssertTrue(fp != XBADFILE);
- certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
- XFCLOSE(fp);
- fp = XFOPEN("./certs/1024/client-key.der", "rb");
- AssertTrue(fp != XBADFILE);
- keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
- XFCLOSE(fp);
- #endif
- #elif defined(HAVE_ECC)
- #if defined(USE_CERT_BUFFERS_256)
- unsigned char cert[sizeof(cliecc_cert_der_256)];
- unsigned char key[sizeof(ecc_clikey_der_256)];
- int certSz = (int)sizeof(cert);
- int keySz = (int)sizeof(key);
- XMEMSET(cert, 0, certSz);
- XMEMSET(key, 0, keySz);
- XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
- XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
- #else
- unsigned char cert[ONEK_BUF];
- unsigned char key[ONEK_BUF];
- XFILE fp;
- int certSz, keySz;
- fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
- AssertTrue(fp != XBADFILE);
- certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
- XFCLOSE(fp);
- fp = XFOPEN("./certs/client-ecc-key.der", "rb");
- AssertTrue(fp != XBADFILE);
- keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
- XFCLOSE(fp);
- #endif
- #endif
- XMEMSET(output, 0, sizeof(output));
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
- pkcs7->content = data;
- pkcs7->contentSz = sizeof(data);
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = keySz;
- AssertIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
- /* Test bad args. */
- AssertIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_PKCS7_EncodeData */
- #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
- !defined(NO_RSA) && !defined(NO_SHA256)
- /* RSA sign raw digest callback */
- static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
- byte* out, word32 outSz, byte* privateKey,
- word32 privateKeySz, int devid, int hashOID)
- {
- /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
- byte digInfoEncoding[] = {
- 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
- 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
- 0x00, 0x04, 0x20
- };
- int ret;
- byte digestInfo[ONEK_BUF];
- byte sig[FOURK_BUF];
- word32 digestInfoSz = 0;
- word32 idx = 0;
- RsaKey rsa;
- /* SHA-256 required only for this example callback due to above
- * digInfoEncoding[] */
- if (pkcs7 == NULL || digest == NULL || out == NULL ||
- (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
- (hashOID != SHA256h)) {
- return -1;
- }
- /* build DigestInfo */
- XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
- digestInfoSz += sizeof(digInfoEncoding);
- XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
- digestInfoSz += digestSz;
- /* set up RSA key */
- ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
- if (ret != 0) {
- return ret;
- }
- ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
- /* sign DigestInfo */
- if (ret == 0) {
- ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
- &rsa, pkcs7->rng);
- if (ret > 0) {
- if (ret > (int)outSz) {
- /* output buffer too small */
- ret = -1;
- }
- else {
- /* success, ret holds sig size */
- XMEMCPY(out, sig, ret);
- }
- }
- }
- wc_FreeRsaKey(&rsa);
- return ret;
- }
- #endif
- /*
- * Testing wc_PKCS7_EncodeSignedData()
- */
- static int test_wc_PKCS7_EncodeSignedData(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- WC_RNG rng;
- byte output[FOURK_BUF];
- byte badOut[1];
- word32 outputSz = (word32)sizeof(output);
- word32 badOutSz = 0;
- byte data[] = "Test data to encode.";
- #ifndef NO_RSA
- #if defined(USE_CERT_BUFFERS_2048)
- byte key[sizeof(client_key_der_2048)];
- byte cert[sizeof(client_cert_der_2048)];
- word32 keySz = (word32)sizeof(key);
- word32 certSz = (word32)sizeof(cert);
- XMEMSET(key, 0, keySz);
- XMEMSET(cert, 0, certSz);
- XMEMCPY(key, client_key_der_2048, keySz);
- XMEMCPY(cert, client_cert_der_2048, certSz);
- #elif defined(USE_CERT_BUFFERS_1024)
- byte key[sizeof_client_key_der_1024];
- byte cert[sizeof(sizeof_client_cert_der_1024)];
- word32 keySz = (word32)sizeof(key);
- word32 certSz = (word32)sizeof(cert);
- XMEMSET(key, 0, keySz);
- XMEMSET(cert, 0, certSz);
- XMEMCPY(key, client_key_der_1024, keySz);
- XMEMCPY(cert, client_cert_der_1024, certSz);
- #else
- unsigned char cert[ONEK_BUF];
- unsigned char key[ONEK_BUF];
- XFILE fp;
- int certSz;
- int keySz;
- fp = XFOPEN("./certs/1024/client-cert.der", "rb");
- AssertTrue(fp != XBADFILE);
- certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
- XFCLOSE(fp);
- fp = XFOPEN("./certs/1024/client-key.der", "rb");
- AssertTrue(fp != XBADFILE);
- keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
- XFCLOSE(fp);
- #endif
- #elif defined(HAVE_ECC)
- #if defined(USE_CERT_BUFFERS_256)
- unsigned char cert[sizeof(cliecc_cert_der_256)];
- unsigned char key[sizeof(ecc_clikey_der_256)];
- int certSz = (int)sizeof(cert);
- int keySz = (int)sizeof(key);
- XMEMSET(cert, 0, certSz);
- XMEMSET(key, 0, keySz);
- XMEMCPY(cert, cliecc_cert_der_256, certSz);
- XMEMCPY(key, ecc_clikey_der_256, keySz);
- #else
- unsigned char cert[ONEK_BUF];
- unsigned char key[ONEK_BUF];
- XFILE fp;
- int certSz, keySz;
- fp = XOPEN("./certs/client-ecc-cert.der", "rb");
- AssertTrue(fp != XBADFILE);
- certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp);
- XFCLOSE(fp);
- fp = XFOPEN("./certs/client-ecc-key.der", "rb");
- AssertTrue(fp != XBADFILE);
- keySz = (int)XFREAD(key, 1, ONEK_BUF, fp);
- XFCLOSE(fp);
- #endif
- #endif
- XMEMSET(output, 0, outputSz);
- AssertIntEQ(wc_InitRng(&rng), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
- pkcs7->content = data;
- pkcs7->contentSz = (word32)sizeof(data);
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = (word32)sizeof(key);
- pkcs7->encryptOID = RSAk;
- #ifdef NO_SHA
- pkcs7->hashOID = SHA256h;
- #else
- pkcs7->hashOID = SHAh;
- #endif
- pkcs7->rng = &rng;
- AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
- wc_PKCS7_Free(pkcs7);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
- /* Pass in bad args. */
- AssertIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
- badOutSz), BAD_FUNC_ARG);
- pkcs7->hashOID = 0; /* bad hashOID */
- AssertIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), BAD_FUNC_ARG);
- #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
- !defined(NO_RSA) && !defined(NO_SHA256)
- /* test RSA sign raw digest callback, if using RSA and compiled in.
- * Example callback assumes SHA-256, so only run test if compiled in. */
- wc_PKCS7_Free(pkcs7);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
- pkcs7->content = data;
- pkcs7->contentSz = (word32)sizeof(data);
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = (word32)sizeof(key);
- pkcs7->encryptOID = RSAk;
- pkcs7->hashOID = SHA256h;
- pkcs7->rng = &rng;
- AssertIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
- AssertIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
- #endif
- wc_PKCS7_Free(pkcs7);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_PKCS7_EncodeSignedData */
- /*
- * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
- */
- static int test_wc_PKCS7_EncodeSignedData_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7)
- int ret, i;
- PKCS7* pkcs7;
- WC_RNG rng;
- byte outputHead[FOURK_BUF/2];
- byte outputFoot[FOURK_BUF/2];
- word32 outputHeadSz = (word32)sizeof(outputHead);
- word32 outputFootSz = (word32)sizeof(outputFoot);
- byte data[FOURK_BUF];
- wc_HashAlg hash;
- #ifdef NO_SHA
- enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
- #else
- enum wc_HashType hashType = WC_HASH_TYPE_SHA;
- #endif
- byte hashBuf[WC_MAX_DIGEST_SIZE];
- word32 hashSz = wc_HashGetDigestSize(hashType);
- #ifndef NO_RSA
- #if defined(USE_CERT_BUFFERS_2048)
- byte key[sizeof(client_key_der_2048)];
- byte cert[sizeof(client_cert_der_2048)];
- word32 keySz = (word32)sizeof(key);
- word32 certSz = (word32)sizeof(cert);
- XMEMSET(key, 0, keySz);
- XMEMSET(cert, 0, certSz);
- XMEMCPY(key, client_key_der_2048, keySz);
- XMEMCPY(cert, client_cert_der_2048, certSz);
- #elif defined(USE_CERT_BUFFERS_1024)
- byte key[sizeof_client_key_der_1024];
- byte cert[sizeof(sizeof_client_cert_der_1024)];
- word32 keySz = (word32)sizeof(key);
- word32 certSz = (word32)sizeof(cert);
- XMEMSET(key, 0, keySz);
- XMEMSET(cert, 0, certSz);
- XMEMCPY(key, client_key_der_1024, keySz);
- XMEMCPY(cert, client_cert_der_1024, certSz);
- #else
- unsigned char cert[ONEK_BUF];
- unsigned char key[ONEK_BUF];
- XFILE fp;
- int certSz;
- int keySz;
- fp = XFOPEN("./certs/1024/client-cert.der", "rb");
- AssertTrue((fp != XBADFILE));
- certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
- XFCLOSE(fp);
- fp = XFOPEN("./certs/1024/client-key.der", "rb");
- AssertTrue(fp != XBADFILE);
- keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
- XFCLOSE(fp);
- #endif
- #elif defined(HAVE_ECC)
- #if defined(USE_CERT_BUFFERS_256)
- unsigned char cert[sizeof(cliecc_cert_der_256)];
- unsigned char key[sizeof(ecc_clikey_der_256)];
- int certSz = (int)sizeof(cert);
- int keySz = (int)sizeof(key);
- XMEMSET(cert, 0, certSz);
- XMEMSET(key, 0, keySz);
- XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
- XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
- #else
- unsigned char cert[ONEK_BUF];
- unsigned char key[ONEK_BUF];
- XFILE fp;
- int certSz, keySz;
- fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
- AssertTrue(fp != XBADFILE);
- certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
- XFCLOSE(fp);
- fp = XFOPEN("./certs/client-ecc-key.der", "rb");
- AssertTrue(fp != XBADFILE);
- keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
- XFCLOSE(fp);
- #endif
- #endif
- /* initialize large data with sequence */
- for (i=0; i<(int)sizeof(data); i++)
- data[i] = i & 0xff;
- XMEMSET(outputHead, 0, outputHeadSz);
- XMEMSET(outputFoot, 0, outputFootSz);
- AssertIntEQ(wc_InitRng(&rng), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
- pkcs7->content = NULL; /* not used for ex */
- pkcs7->contentSz = (word32)sizeof(data);
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = (word32)sizeof(key);
- pkcs7->encryptOID = RSAk;
- #ifdef NO_SHA
- pkcs7->hashOID = SHA256h;
- #else
- pkcs7->hashOID = SHAh;
- #endif
- pkcs7->rng = &rng;
- /* calculate hash for content */
- ret = wc_HashInit(&hash, hashType);
- if (ret == 0) {
- ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
- if (ret == 0) {
- ret = wc_HashFinal(&hash, hashType, hashBuf);
- }
- wc_HashFree(&hash, hashType);
- }
- AssertIntEQ(ret, 0);
- /* Perform PKCS7 sign using hash directly */
- AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
- AssertIntGT(outputHeadSz, 0);
- AssertIntGT(outputFootSz, 0);
- wc_PKCS7_Free(pkcs7);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- /* required parameter even on verify when using _ex, if using outputHead
- * and outputFoot */
- pkcs7->contentSz = (word32)sizeof(data);
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
- wc_PKCS7_Free(pkcs7);
- /* assembly complete PKCS7 sign and use normal verify */
- {
- byte* output = (byte*)XMALLOC(
- outputHeadSz + sizeof(data) + outputFootSz,
- HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- word32 outputSz = 0;
- AssertNotNull(output);
- XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
- outputSz += outputHeadSz;
- XMEMCPY(&output[outputSz], data, sizeof(data));
- outputSz += sizeof(data);
- XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
- outputSz += outputFootSz;
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
- XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- }
- /* Pass in bad args. */
- AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
- &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
- &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
- &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
- &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
- pkcs7->hashOID = 0; /* bad hashOID */
- AssertIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
- outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
- outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
- #ifndef NO_PKCS7_STREAM
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
- outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
- #else
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
- outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
- #endif
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
- outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
- #ifndef NO_PKCS7_STREAM
- /* can pass in 0 buffer length with streaming API */
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
- #else
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
- #ifndef NO_PKCS7_STREAM
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
- #else
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
- outputHead, outputHeadSz, outputFoot, 0), ASN_PARSE_E);
- #endif
- wc_PKCS7_Free(pkcs7);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_PKCS7_EncodeSignedData_ex */
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
- /**
- * Loads certs/keys from files or buffers into the argument buffers,
- * helper function called by CreatePKCS7SignedData().
- *
- * Returns 0 on success, negative on error.
- */
- static int LoadPKCS7SignedDataCerts(
- int useIntermediateCertChain, int pkAlgoType,
- byte* intCARoot, word32* intCARootSz,
- byte* intCA1, word32* intCA1Sz,
- byte* intCA2, word32* intCA2Sz,
- byte* cert, word32* certSz,
- byte* key, word32* keySz)
- {
- int ret = 0;
- FILE* fp = NULL;
- #ifndef NO_RSA
- const char* intCARootRSA = "./certs/ca-cert.der";
- const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
- const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
- const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
- const char* intServKeyRSA = "./certs/server-key.der";
- #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
- const char* cli1024Cert = "./certs/1024/client-cert.der";
- const char* cli1024Key = "./certs/1024/client-key.der";
- #endif
- #endif
- #ifdef HAVE_ECC
- const char* intCARootECC = "./certs/ca-ecc-cert.der";
- const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
- const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
- const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
- const char* intServKeyECC = "./certs/ecc-key.der";
- #ifndef USE_CERT_BUFFERS_256
- const char* cliEccCert = "./certs/client-ecc-cert.der";
- const char* cliEccKey = "./certs/client-ecc-key.der";
- #endif
- #endif
- if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
- ((useIntermediateCertChain == 1) &&
- (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
- intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
- return BAD_FUNC_ARG;
- }
- /* Read/load certs and keys to use for signing based on PK type and chain */
- switch (pkAlgoType) {
- #ifndef NO_RSA
- case RSA_TYPE:
- if (useIntermediateCertChain == 1) {
- fp = XFOPEN(intCARootRSA, "rb");
- AssertNotNull(fp);
- *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
- XFCLOSE(fp);
- AssertIntGT(*intCARootSz, 0);
- fp = XFOPEN(intCA1RSA, "rb");
- AssertNotNull(fp);
- *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
- XFCLOSE(fp);
- AssertIntGT(*intCA1Sz, 0);
- fp = XFOPEN(intCA2RSA, "rb");
- AssertNotNull(fp);
- *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
- XFCLOSE(fp);
- AssertIntGT(*intCA2Sz, 0);
- fp = XFOPEN(intServCertRSA, "rb");
- AssertNotNull(fp);
- *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
- XFCLOSE(fp);
- AssertIntGT(*certSz, 0);
- fp = XFOPEN(intServKeyRSA, "rb");
- AssertNotNull(fp);
- *keySz = (word32)XFREAD(key, 1, *keySz, fp);
- XFCLOSE(fp);
- AssertIntGT(*keySz, 0);
- }
- else {
- #if defined(USE_CERT_BUFFERS_2048)
- *keySz = sizeof_client_key_der_2048;
- *certSz = sizeof_client_cert_der_2048;
- XMEMCPY(key, client_key_der_2048, *keySz);
- XMEMCPY(cert, client_cert_der_2048, *certSz);
- #elif defined(USE_CERT_BUFFERS_1024)
- *keySz = sizeof_client_key_der_1024;
- *certSz = sizeof_client_cert_der_1024;
- XMEMCPY(key, client_key_der_1024, *keySz);
- XMEMCPY(cert, client_cert_der_1024, *certSz);
- #else
- fp = XFOPEN(cli1024Key, "rb");
- AssertNotNull(fp);
- *keySz = (word32)XFREAD(key, 1, *keySz, fp);
- XFCLOSE(fp);
- AssertIntGT(*keySz, 0);
- fp = XFOPEN(cli1024Cert, "rb");
- AssertNotNull(fp);
- *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
- XFCLOSE(fp);
- AssertIntGT(*certSz, 0);
- #endif /* USE_CERT_BUFFERS_2048 */
- }
- break;
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- case ECC_TYPE:
- if (useIntermediateCertChain == 1) {
- fp = XFOPEN(intCARootECC, "rb");
- AssertNotNull(fp);
- *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
- XFCLOSE(fp);
- AssertIntGT(*intCARootSz, 0);
- fp = XFOPEN(intCA1ECC, "rb");
- AssertNotNull(fp);
- *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
- XFCLOSE(fp);
- AssertIntGT(*intCA1Sz, 0);
- fp = XFOPEN(intCA2ECC, "rb");
- AssertNotNull(fp);
- *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
- XFCLOSE(fp);
- AssertIntGT(*intCA2Sz, 0);
- fp = XFOPEN(intServCertECC, "rb");
- AssertNotNull(fp);
- *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
- XFCLOSE(fp);
- AssertIntGT(*certSz, 0);
- fp = XFOPEN(intServKeyECC, "rb");
- AssertNotNull(fp);
- *keySz = (word32)XFREAD(key, 1, *keySz, fp);
- XFCLOSE(fp);
- AssertIntGT(*keySz, 0);
- }
- else {
- #if defined(USE_CERT_BUFFERS_256)
- *keySz = sizeof_ecc_clikey_der_256;
- *certSz = sizeof_cliecc_cert_der_256;
- XMEMCPY(key, ecc_clikey_der_256, *keySz);
- XMEMCPY(cert, cliecc_cert_der_256, *certSz);
- #else
- fp = XFOPEN(cliEccKey, "rb");
- AssertNotNull(fp);
- *keySz = (word32)XFREAD(key, 1, *keySz, fp);
- XFCLOSE(fp);
- AssertIntGT(*keySz, 0);
- fp = XFOPEN(cliEccCert, "rb");
- AssertNotNull(fp);
- *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
- XFCLOSE(fp);
- AssertIntGT(*certSz, 0);
- #endif /* USE_CERT_BUFFERS_256 */
- }
- break;
- #endif /* HAVE_ECC */
- default:
- WOLFSSL_MSG("Unsupported SignedData PK type");
- ret = BAD_FUNC_ARG;
- break;
- }
- return ret;
- }
- /**
- * Creates a PKCS7/CMS SignedData bundle to use for testing.
- *
- * output output buffer to place SignedData
- * outputSz size of output buffer
- * data data buffer to be signed
- * dataSz size of data buffer
- * withAttribs [1/0] include attributes in SignedData message
- * detachedSig [1/0] create detached signature, no content
- * useIntCertChain [1/0] use certificate chain and include intermediate and
- * root CAs in bundle
- * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
- *
- * Return size of bundle created on success, negative on error */
- static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
- byte* data, word32 dataSz,
- int withAttribs, int detachedSig,
- int useIntermediateCertChain,
- int pkAlgoType)
- {
- int ret = 0;
- WC_RNG rng;
- PKCS7* pkcs7 = NULL;
- static byte messageTypeOid[] =
- { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
- 0x09, 0x02 };
- static byte messageType[] = { 0x13, 2, '1', '9' };
- PKCS7Attrib attribs[] =
- {
- { messageTypeOid, sizeof(messageTypeOid), messageType,
- sizeof(messageType) }
- };
- byte intCARoot[TWOK_BUF];
- byte intCA1[TWOK_BUF];
- byte intCA2[TWOK_BUF];
- byte cert[TWOK_BUF];
- byte key[TWOK_BUF];
- word32 intCARootSz = sizeof(intCARoot);
- word32 intCA1Sz = sizeof(intCA1);
- word32 intCA2Sz = sizeof(intCA2);
- word32 certSz = sizeof(cert);
- word32 keySz = sizeof(key);
- XMEMSET(intCARoot, 0, intCARootSz);
- XMEMSET(intCA1, 0, intCA1Sz);
- XMEMSET(intCA2, 0, intCA2Sz);
- XMEMSET(cert, 0, certSz);
- XMEMSET(key, 0, keySz);
- ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
- intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
- cert, &certSz, key, &keySz);
- AssertIntEQ(ret, 0);
- XMEMSET(output, 0, outputSz);
- AssertIntEQ(wc_InitRng(&rng), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
- if (useIntermediateCertChain == 1) {
- /* Add intermediate and root CA certs into SignedData Certs SET */
- AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
- AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
- AssertIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
- }
- pkcs7->content = data;
- pkcs7->contentSz = dataSz;
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = (word32)sizeof(key);
- if (pkAlgoType == RSA_TYPE) {
- pkcs7->encryptOID = RSAk;
- }
- else {
- pkcs7->encryptOID = ECDSAk;
- }
- #ifdef NO_SHA
- pkcs7->hashOID = SHA256h;
- #else
- pkcs7->hashOID = SHAh;
- #endif
- pkcs7->rng = &rng;
- if (withAttribs) {
- /* include a signed attribute */
- pkcs7->signedAttribs = attribs;
- pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
- }
- if (detachedSig) {
- AssertIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
- }
- outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
- AssertIntGT(outputSz, 0);
- wc_PKCS7_Free(pkcs7);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- if (detachedSig) {
- pkcs7->content = data;
- pkcs7->contentSz = dataSz;
- }
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
- wc_PKCS7_Free(pkcs7);
- wc_FreeRng(&rng);
- return outputSz;
- }
- #endif
- /*
- * Testing wc_PKCS_VerifySignedData()
- */
- static int test_wc_PKCS7_VerifySignedData(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
- PKCS7* pkcs7;
- byte output[6000]; /* Large size needed for bundles with int CA certs */
- word32 outputSz = sizeof(output);
- byte data[] = "Test data to encode.";
- byte badOut[1];
- word32 badOutSz = 0;
- byte badContent[] = "This is different content than was signed";
- int ret;
- wc_HashAlg hash;
- #ifdef NO_SHA
- enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
- #else
- enum wc_HashType hashType = WC_HASH_TYPE_SHA;
- #endif
- byte hashBuf[WC_MAX_DIGEST_SIZE];
- word32 hashSz = wc_HashGetDigestSize(hashType);
- #ifndef NO_RSA
- /* Success test with RSA certs/key */
- AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
- (word32)sizeof(data),
- 0, 0, 0, RSA_TYPE)), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
- #endif
- #ifdef HAVE_ECC
- #ifndef NO_RSA
- wc_PKCS7_Free(pkcs7);
- #endif
- /* Success test with ECC certs/key */
- outputSz = sizeof(output);
- XMEMSET(output, 0, outputSz);
- AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
- (word32)sizeof(data),
- 0, 0, 0, ECC_TYPE)), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
- #endif
- /* Test bad args. */
- #if !defined(NO_RSA) || defined(HAVE_ECC)
- AssertIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
- BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
- BAD_FUNC_ARG);
- #ifndef NO_PKCS7_STREAM
- /* can pass in 0 buffer length with streaming API */
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
- badOutSz), WC_PKCS7_WANT_READ_E);
- #else
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
- badOutSz), BAD_FUNC_ARG);
- #endif
- wc_PKCS7_Free(pkcs7);
- #endif /* !NO_RSA || HAVE_ECC */
- /* Invalid content should error, use detached signature so we can
- * easily change content */
- #ifndef NO_RSA
- /* Try RSA certs/key/sig first */
- outputSz = sizeof(output);
- XMEMSET(output, 0, outputSz);
- AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
- (word32)sizeof(data),
- 1, 1, 0, RSA_TYPE)), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- pkcs7->content = badContent;
- pkcs7->contentSz = sizeof(badContent);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
- SIG_VERIFY_E);
- wc_PKCS7_Free(pkcs7);
- /* Test success case with detached signature and valid content */
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- pkcs7->content = data;
- pkcs7->contentSz = sizeof(data);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
- wc_PKCS7_Free(pkcs7);
- /* verify using pre-computed content digest only (no content) */
- {
- /* calculate hash for content */
- ret = wc_HashInit(&hash, hashType);
- if (ret == 0) {
- ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
- if (ret == 0) {
- ret = wc_HashFinal(&hash, hashType, hashBuf);
- }
- wc_HashFree(&hash, hashType);
- }
- AssertIntEQ(ret, 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
- output, outputSz,
- NULL, 0), 0);
- wc_PKCS7_Free(pkcs7);
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- /* Try ECC certs/key/sig next */
- outputSz = sizeof(output);
- XMEMSET(output, 0, outputSz);
- AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
- (word32)sizeof(data),
- 1, 1, 0, ECC_TYPE)), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- pkcs7->content = badContent;
- pkcs7->contentSz = sizeof(badContent);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
- SIG_VERIFY_E);
- wc_PKCS7_Free(pkcs7);
- /* Test success case with detached signature and valid content */
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- pkcs7->content = data;
- pkcs7->contentSz = sizeof(data);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
- wc_PKCS7_Free(pkcs7);
- /* verify using pre-computed content digest only (no content) */
- {
- /* calculate hash for content */
- ret = wc_HashInit(&hash, hashType);
- if (ret == 0) {
- ret = wc_HashUpdate(&hash, hashType, data, sizeof(data));
- if (ret == 0) {
- ret = wc_HashFinal(&hash, hashType, hashBuf);
- }
- wc_HashFree(&hash, hashType);
- }
- AssertIntEQ(ret, 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
- output, outputSz,
- NULL, 0), 0);
- wc_PKCS7_Free(pkcs7);
- }
- #endif
- /* Test verify on signedData containing intermediate/root CA certs */
- #ifndef NO_RSA
- outputSz = sizeof(output);
- XMEMSET(output, 0, outputSz);
- AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
- (word32)sizeof(data),
- 0, 0, 1, RSA_TYPE)), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
- wc_PKCS7_Free(pkcs7);
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- outputSz = sizeof(output);
- XMEMSET(output, 0, outputSz);
- AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
- (word32)sizeof(data),
- 0, 0, 1, ECC_TYPE)), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
- wc_PKCS7_Free(pkcs7);
- #endif /* HAVE_ECC */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_PKCS7_VerifySignedData() */
- #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
- !defined(NO_AES_256)
- static const byte defKey[] = {
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
- };
- static byte aesHandle[32]; /* simulated hardware key handle */
- /* return 0 on success */
- static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
- byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
- byte* in, int inSz, byte* out, void* usrCtx)
- {
- int ret;
- Aes aes;
- if (usrCtx == NULL) {
- /* no simulated handle passed in */
- return -1;
- }
- switch (encryptOID) {
- case AES256CBCb:
- if (ivSz != AES_BLOCK_SIZE)
- return BAD_FUNC_ARG;
- break;
- default:
- WOLFSSL_MSG("Unsupported content cipher type for test");
- return ALGO_ID_E;
- };
- /* simulate using handle to get key */
- ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
- if (ret == 0) {
- ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
- if (ret == 0)
- ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
- wc_AesFree(&aes);
- }
- (void)aad;
- (void)aadSz;
- (void)authTag;
- (void)authTagSz;
- (void)pkcs7;
- return ret;
- }
- /* returns key size on success */
- static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
- word32 keyIdSz, byte* orginKey, word32 orginKeySz,
- byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
- {
- int ret = -1;
- if (out == NULL)
- return BAD_FUNC_ARG;
- if (keyId[0] != 0x00) {
- return -1;
- }
- if (type != (int)PKCS7_KEKRI) {
- return -1;
- }
- switch (keyWrapAlgo) {
- case AES256_WRAP:
- /* simulate setting a handle for later decryption but use key
- * as handle in the test case here */
- ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
- aesHandle, sizeof(aesHandle), NULL);
- if (ret < 0)
- return ret;
- ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
- if (ret < 0)
- return ret;
- /* return key size on success */
- return sizeof(defKey);
- default:
- WOLFSSL_MSG("Unsupported key wrap algorithm in example");
- return BAD_KEYWRAP_ALG_E;
- };
- (void)cekSz;
- (void)cek;
- (void)outSz;
- (void)keyIdSz;
- (void)direction;
- (void)orginKey; /* used with KAKRI */
- (void)orginKeySz;
- return ret;
- }
- #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
- /*
- * Testing wc_PKCS7_EncodeEnvelopedData()
- */
- static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- #ifdef ECC_TIMING_RESISTANT
- WC_RNG rng;
- #endif
- word32 tempWrd32 = 0;
- byte* tmpBytePtr = NULL;
- const char input[] = "Test data to encode.";
- int i;
- int testSz = 0;
- #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
- !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
- byte* rsaCert = NULL;
- byte* rsaPrivKey = NULL;
- word32 rsaCertSz;
- word32 rsaPrivKeySz;
- #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
- !defined(USE_CERT_BUFFERS_2048) )
- static const char* rsaClientCert = "./certs/client-cert.der";
- static const char* rsaClientKey = "./certs/client-key.der";
- rsaCertSz = (word32)sizeof(rsaClientCert);
- rsaPrivKeySz = (word32)sizeof(rsaClientKey);
- #endif
- #endif
- #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
- !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
- byte* eccCert = NULL;
- byte* eccPrivKey = NULL;
- word32 eccCertSz;
- word32 eccPrivKeySz;
- #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
- static const char* eccClientCert = "./certs/client-ecc-cert.der";
- static const char* eccClientKey = "./certs/ecc-client-key.der";
- #endif
- #endif
- /* Generic buffer size. */
- byte output[ONEK_BUF];
- byte decoded[sizeof(input)/sizeof(char)];
- int decodedSz = 0;
- #ifndef NO_FILESYSTEM
- XFILE certFile;
- XFILE keyFile;
- #endif
- #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
- !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
- /* RSA certs and keys. */
- #if defined(USE_CERT_BUFFERS_1024)
- rsaCertSz = (word32)sizeof_client_cert_der_1024;
- /* Allocate buffer space. */
- AssertNotNull(rsaCert =
- (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- /* Init buffer. */
- XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
- rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
- AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
- #elif defined(USE_CERT_BUFFERS_2048)
- rsaCertSz = (word32)sizeof_client_cert_der_2048;
- /* Allocate buffer */
- AssertNotNull(rsaCert =
- (byte*)XMALLOC(rsaCertSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- /* Init buffer. */
- XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
- rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
- AssertNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
- #else
- /* File system. */
- certFile = XFOPEN(rsaClientCert, "rb");
- AssertTrue(certFile != XBADFILE);
- rsaCertSz = (word32)FOURK_BUF;
- AssertNotNull(rsaCert =
- (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz, certFile);
- XFCLOSE(certFile);
- keyFile = XFOPEN(rsaClientKey, "rb");
- AssertTrue(keyFile != XBADFILE);
- AssertNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- rsaPrivKeySz = (word32)FOURK_BUF;
- rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz, keyFile);
- XFCLOSE(keyFile);
- #endif /* USE_CERT_BUFFERS */
- #endif /* NO_RSA */
- /* ECC */
- #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
- !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
- #ifdef USE_CERT_BUFFERS_256
- AssertNotNull(eccCert =
- (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- /* Init buffer. */
- eccCertSz = (word32)sizeof_cliecc_cert_der_256;
- XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
- AssertNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
- XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
- #else /* File system. */
- certFile = XFOPEN(eccClientCert, "rb");
- AssertTrue(certFile != XBADFILE);
- eccCertSz = (word32)FOURK_BUF;
- AssertNotNull(eccCert =
- (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz, certFile);
- XFCLOSE(certFile);
- keyFile = XFOPEN(eccClientKey, "rb");
- AssertTrue(keyFile != XBADFILE);
- eccPrivKeySz = (word32)FOURK_BUF;
- AssertNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz, keyFile);
- XFCLOSE(keyFile);
- #endif /* USE_CERT_BUFFERS_256 */
- #endif /* END HAVE_ECC */
- #ifndef NO_FILESYSTEM
- /* Silence. */
- (void)keyFile;
- (void)certFile;
- #endif
- {
- const pkcs7EnvelopedVector testVectors[] = {
- /* DATA is a global variable defined in the makefile. */
- #if !defined(NO_RSA)
- #ifndef NO_DES3
- {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
- rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
- #endif /* NO_DES3 */
- #if !defined(NO_AES) && defined(HAVE_AES_CBC)
- #ifndef NO_AES_128
- {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
- 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
- #endif
- #ifndef NO_AES_192
- {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
- 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
- #endif
- #ifndef NO_AES_256
- {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
- 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
- #endif
- #endif /* NO_AES && HAVE_AES_CBC */
- #endif /* NO_RSA */
- #if defined(HAVE_ECC)
- #if !defined(NO_AES) && defined(HAVE_AES_CBC)
- #if !defined(NO_SHA) && !defined(NO_AES_128)
- {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
- AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme, eccCert,
- eccCertSz, eccPrivKey, eccPrivKeySz},
- #endif
- #if !defined(NO_SHA256) && !defined(NO_AES_256)
- {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
- AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme, eccCert,
- eccCertSz, eccPrivKey, eccPrivKeySz},
- #endif
- #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
- {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
- AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme, eccCert,
- eccCertSz, eccPrivKey, eccPrivKeySz},
- #endif
- #endif /* NO_AES && HAVE_AES_CBC*/
- #endif /* END HAVE_ECC */
- }; /* END pkcs7EnvelopedVector */
- #ifdef ECC_TIMING_RESISTANT
- AssertIntEQ(wc_InitRng(&rng), 0);
- #endif
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
- testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
- for (i = 0; i < testSz; i++) {
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
- (word32)(testVectors + i)->certSz), 0);
- #ifdef ECC_TIMING_RESISTANT
- pkcs7->rng = &rng;
- #endif
- pkcs7->content = (byte*)(testVectors + i)->content;
- pkcs7->contentSz = (testVectors + i)->contentSz;
- pkcs7->contentOID = (testVectors + i)->contentOID;
- pkcs7->encryptOID = (testVectors + i)->encryptOID;
- pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
- pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
- pkcs7->privateKey = (testVectors + i)->privateKey;
- pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
- AssertIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
- (word32)sizeof(output)), 0);
- decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
- (word32)sizeof(output), decoded, (word32)sizeof(decoded));
- AssertIntGE(decodedSz, 0);
- /* Verify the size of each buffer. */
- AssertIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
- /* Don't free the last time through the loop. */
- if (i < testSz - 1) {
- wc_PKCS7_Free(pkcs7);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- }
- } /* END test loop. */
- }
- /* Test bad args. */
- AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
- (word32)sizeof(output)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
- (word32)sizeof(output)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
- /* Decode. */
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
- (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
- (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
- (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
- (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
- (word32)sizeof(decoded)), BAD_FUNC_ARG);
- /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
- #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
- /* only a failure for KARI test cases */
- tempWrd32 = pkcs7->singleCertSz;
- pkcs7->singleCertSz = 0;
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
- (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
- pkcs7->singleCertSz = tempWrd32;
- tmpBytePtr = pkcs7->singleCert;
- pkcs7->singleCert = NULL;
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
- (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
- pkcs7->singleCert = tmpBytePtr;
- #endif
- tempWrd32 = pkcs7->privateKeySz;
- pkcs7->privateKeySz = 0;
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
- (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
- pkcs7->privateKeySz = tempWrd32;
- tmpBytePtr = pkcs7->privateKey;
- pkcs7->privateKey = NULL;
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
- (word32)sizeof(output), decoded, (word32)sizeof(decoded)), BAD_FUNC_ARG);
- pkcs7->privateKey = tmpBytePtr;
- wc_PKCS7_Free(pkcs7);
- #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
- /* test of decrypt callback with KEKRI enveloped data */
- {
- int envelopedSz;
- const byte keyId[] = { 0x00 };
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- pkcs7->content = (byte*)input;
- pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
- pkcs7->contentOID = DATA;
- pkcs7->encryptOID = AES256CBCb;
- AssertIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
- (byte*)defKey, sizeof(defKey), (byte*)keyId,
- sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
- AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
- AssertIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
- (word32)sizeof(output))), 0);
- wc_PKCS7_Free(pkcs7);
- /* decode envelopedData */
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
- AssertIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
- AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
- envelopedSz, decoded, sizeof(decoded))), 0);
- wc_PKCS7_Free(pkcs7);
- }
- #endif /* !NO_AES && !NO_AES_256 */
- #ifndef NO_RSA
- if (rsaCert) {
- XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- }
- if (rsaPrivKey) {
- XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- }
- #endif /*NO_RSA */
- #ifdef HAVE_ECC
- if (eccCert) {
- XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- }
- if (eccPrivKey) {
- XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- }
- #endif /* HAVE_ECC */
- #ifdef ECC_TIMING_RESISTANT
- wc_FreeRng(&rng);
- #endif
- #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
- !defined(NO_RSA) && !defined(NO_SHA)
- {
- byte out[7];
- byte *cms;
- word32 cmsSz;
- XFILE cmsFile;
- XMEMSET(out, 0, sizeof(out));
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb");
- AssertTrue(cmsFile != XBADFILE);
- cmsSz = (word32)FOURK_BUF;
- AssertNotNull(cms =
- (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile);
- XFCLOSE(cmsFile);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
- sizeof_client_cert_der_2048), 0);
- pkcs7->privateKey = (byte*)client_key_der_2048;
- pkcs7->privateKeySz = sizeof_client_key_der_2048;
- AssertIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
- 2), 0);
- AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
- sizeof(out)), 0);
- XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- AssertIntEQ(XMEMCMP(out, "test", 4), 0);
- wc_PKCS7_Free(pkcs7);
- }
- #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_PKCS7 */
- return res;
- } /* END test_wc_PKCS7_EncodeEnvelopedData() */
- /*
- * Testing wc_PKCS7_EncodeEncryptedData()
- */
- static int test_wc_PKCS7_EncodeEncryptedData(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
- PKCS7* pkcs7 = NULL;
- byte* tmpBytePtr = NULL;
- byte encrypted[TWOK_BUF];
- byte decoded[TWOK_BUF];
- word32 tmpWrd32 = 0;
- int tmpInt = 0;
- int decodedSz;
- int encryptedSz;
- int testSz;
- int i;
- const byte data[] = { /* Hello World */
- 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
- 0x72,0x6c,0x64
- };
- #ifndef NO_DES3
- byte desKey[] = {
- 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
- };
- byte des3Key[] = {
- 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
- 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
- 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
- };
- #endif
- #if !defined(NO_AES) && defined(HAVE_AES_CBC)
- #ifndef NO_AES_128
- byte aes128Key[] = {
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
- };
- #endif
- #ifndef NO_AES_192
- byte aes192Key[] = {
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
- };
- #endif
- #ifndef NO_AES_256
- byte aes256Key[] = {
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
- 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
- };
- #endif
- #endif /* !NO_AES && HAVE_AES_CBC */
- const pkcs7EncryptedVector testVectors[] =
- {
- #ifndef NO_DES3
- {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
- {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
- #endif /* !NO_DES3 */
- #if !defined(NO_AES) && defined(HAVE_AES_CBC)
- #ifndef NO_AES_128
- {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
- sizeof(aes128Key)},
- #endif
- #ifndef NO_AES_192
- {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
- sizeof(aes192Key)},
- #endif
- #ifndef NO_AES_256
- {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
- sizeof(aes256Key)},
- #endif
- #endif /* !NO_AES && HAVE_AES_CBC */
- };
- testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
- for (i = 0; i < testSz; i++) {
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
- pkcs7->content = (byte*)testVectors[i].content;
- pkcs7->contentSz = testVectors[i].contentSz;
- pkcs7->contentOID = testVectors[i].contentOID;
- pkcs7->encryptOID = testVectors[i].encryptOID;
- pkcs7->encryptionKey = testVectors[i].encryptionKey;
- pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
- pkcs7->heap = HEAP_HINT;
- /* encode encryptedData */
- encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
- sizeof(encrypted));
- AssertIntGT(encryptedSz, 0);
- /* Decode encryptedData */
- decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
- decoded, sizeof(decoded));
- AssertIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
- /* Keep values for last itr. */
- if (i < testSz - 1) {
- wc_PKCS7_Free(pkcs7);
- }
- }
- if (pkcs7 == NULL || testSz == 0) {
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
- }
- AssertIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
- sizeof(encrypted)),BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
- sizeof(encrypted)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
- 0), BAD_FUNC_ARG);
- /* Testing the struct. */
- tmpBytePtr = pkcs7->content;
- pkcs7->content = NULL;
- AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
- sizeof(encrypted)), BAD_FUNC_ARG);
- pkcs7->content = tmpBytePtr;
- tmpWrd32 = pkcs7->contentSz;
- pkcs7->contentSz = 0;
- AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
- sizeof(encrypted)), BAD_FUNC_ARG);
- pkcs7->contentSz = tmpWrd32;
- tmpInt = pkcs7->encryptOID;
- pkcs7->encryptOID = 0;
- AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
- sizeof(encrypted)), BAD_FUNC_ARG);
- pkcs7->encryptOID = tmpInt;
- tmpBytePtr = pkcs7->encryptionKey;
- pkcs7->encryptionKey = NULL;
- AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
- sizeof(encrypted)), BAD_FUNC_ARG);
- pkcs7->encryptionKey = tmpBytePtr;
- tmpWrd32 = pkcs7->encryptionKeySz;
- pkcs7->encryptionKeySz = 0;
- AssertIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
- sizeof(encrypted)), BAD_FUNC_ARG);
- pkcs7->encryptionKeySz = tmpWrd32;
- AssertIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
- decoded, sizeof(decoded)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
- decoded, sizeof(decoded)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
- decoded, sizeof(decoded)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
- NULL, sizeof(decoded)), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
- decoded, 0), BAD_FUNC_ARG);
- /* Test struct fields */
- tmpBytePtr = pkcs7->encryptionKey;
- pkcs7->encryptionKey = NULL;
- AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
- decoded, sizeof(decoded)), BAD_FUNC_ARG);
- pkcs7->encryptionKey = tmpBytePtr;
- pkcs7->encryptionKeySz = 0;
- AssertIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
- decoded, sizeof(decoded)), BAD_FUNC_ARG);
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_PKCS7_EncodeEncryptedData() */
- /*
- * Testing wc_PKCS7_Degenerate()
- */
- static int test_wc_PKCS7_Degenerate(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
- PKCS7* pkcs7;
- char fName[] = "./certs/test-degenerate.p7b";
- XFILE f;
- byte der[4096];
- word32 derSz;
- int ret;
- AssertNotNull(f = XFOPEN(fName, "rb"));
- AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
- derSz = (word32)ret;
- XFCLOSE(f);
- /* test degenerate success */
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- #ifndef NO_RSA
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
- #else
- AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
- #endif
- wc_PKCS7_Free(pkcs7);
- /* test with turning off degenerate cases */
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), PKCS7_NO_SIGNER_E);
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_PKCS7_Degenerate() */
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
- defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
- static byte berContent[] = {
- 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
- 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
- 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
- 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
- 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
- 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
- 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
- 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
- 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
- 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
- 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
- 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
- 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
- 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
- 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
- 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
- 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
- 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
- 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
- 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
- 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
- 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
- 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
- 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
- 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
- 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
- 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
- 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
- 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
- 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
- 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
- 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
- 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
- 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
- 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
- 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
- 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
- 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
- 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
- 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
- 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
- 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
- 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
- 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
- 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
- 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
- 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
- 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
- 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
- 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
- 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
- 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
- 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
- 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
- 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
- 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
- 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
- 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
- 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
- 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
- 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
- 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
- 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
- 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
- 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
- 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
- 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
- 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
- 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
- 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
- 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
- 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
- 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
- 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
- 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
- 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
- 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
- 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
- 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
- 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
- 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
- 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
- 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
- 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
- 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
- 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
- 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
- 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
- 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
- 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
- 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
- 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
- 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
- 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
- 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
- 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
- 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
- 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
- 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
- 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
- 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
- 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
- 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
- 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
- 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
- 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
- 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
- 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
- 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
- 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
- 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
- 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
- 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
- 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
- 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
- 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
- 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
- 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
- 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
- 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
- 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
- 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
- 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
- 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
- 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
- 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
- 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
- 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
- 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
- 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
- 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
- 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
- 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
- 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
- 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
- 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
- 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
- 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
- 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
- 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
- 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
- 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
- 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
- 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
- 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
- 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
- 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
- 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
- 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
- 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
- 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
- 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
- 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
- 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
- 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
- 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
- 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
- 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
- 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
- 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
- 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
- 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
- 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
- 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
- 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
- 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
- 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
- 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
- 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
- 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
- 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
- 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
- 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
- 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
- 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
- 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
- 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
- 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
- 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
- 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
- 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
- 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
- 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
- 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
- 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
- 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00
- };
- #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
- * !NO_DES3 && !NO_SHA
- */
- /*
- * Testing wc_PKCS7_BER()
- */
- static int test_wc_PKCS7_BER(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
- !defined(NO_SHA) && defined(ASN_BER_TO_DER)
- PKCS7* pkcs7;
- char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
- XFILE f;
- byte der[4096];
- #ifndef NO_DES3
- byte decoded[2048];
- #endif
- word32 derSz;
- int ret;
- AssertNotNull(f = XFOPEN(fName, "rb"));
- AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
- derSz = (word32)ret;
- XFCLOSE(f);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- #ifndef NO_RSA
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
- #else
- AssertIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
- #endif
- wc_PKCS7_Free(pkcs7);
- #ifndef NO_DES3
- /* decode BER content */
- AssertNotNull(f = XFOPEN("./certs/1024/client-cert.der", "rb"));
- AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
- derSz = (word32)ret;
- XFCLOSE(f);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- #ifndef NO_RSA
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
- #else
- AssertIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
- #endif
- AssertNotNull(f = XFOPEN("./certs/1024/client-key.der", "rb"));
- AssertIntGT((ret = (int)fread(der, 1, sizeof(der), f)), 0);
- derSz = (word32)ret;
- XFCLOSE(f);
- pkcs7->privateKey = der;
- pkcs7->privateKeySz = derSz;
- #ifndef NO_RSA
- #ifdef WOLFSSL_SP_MATH
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
- sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
- #else
- AssertIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
- sizeof(berContent), decoded, sizeof(decoded)), 0);
- #endif
- #else
- AssertIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
- sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
- #endif
- wc_PKCS7_Free(pkcs7);
- #endif /* !NO_DES3 */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_PKCS7_BER() */
- static int test_PKCS7_signed_enveloped(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
- !defined(NO_FILESYSTEM)
- XFILE f;
- PKCS7* pkcs7;
- #ifdef HAVE_AES_CBC
- PKCS7* inner;
- #endif
- void* pt;
- WC_RNG rng;
- unsigned char key[FOURK_BUF/2];
- unsigned char cert[FOURK_BUF/2];
- unsigned char env[FOURK_BUF];
- int envSz = FOURK_BUF;
- int keySz;
- int certSz;
- unsigned char sig[FOURK_BUF * 2];
- int sigSz = FOURK_BUF * 2;
- #ifdef HAVE_AES_CBC
- unsigned char decoded[FOURK_BUF];
- int decodedSz = FOURK_BUF;
- #endif
- /* load cert */
- AssertNotNull(f = XFOPEN(cliCertDerFile, "rb"));
- AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
- XFCLOSE(f);
- /* load key */
- AssertNotNull(f = XFOPEN(cliKeyFile, "rb"));
- AssertIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
- XFCLOSE(f);
- keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL);
- /* sign cert for envelope */
- AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
- AssertIntEQ(wc_InitRng(&rng), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
- pkcs7->content = cert;
- pkcs7->contentSz = certSz;
- pkcs7->contentOID = DATA;
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = keySz;
- pkcs7->encryptOID = RSAk;
- pkcs7->hashOID = SHA256h;
- pkcs7->rng = &rng;
- AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
- wc_PKCS7_Free(pkcs7);
- wc_FreeRng(&rng);
- #ifdef HAVE_AES_CBC
- /* create envelope */
- AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
- pkcs7->content = sig;
- pkcs7->contentSz = sigSz;
- pkcs7->contentOID = DATA;
- pkcs7->encryptOID = AES256CBCb;
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = keySz;
- AssertIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
- AssertIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
- wc_PKCS7_Free(pkcs7);
- #endif
- /* create bad signed enveloped data */
- sigSz = FOURK_BUF * 2;
- AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
- AssertIntEQ(wc_InitRng(&rng), 0);
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
- pkcs7->content = env;
- pkcs7->contentSz = envSz;
- pkcs7->contentOID = DATA;
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = keySz;
- pkcs7->encryptOID = RSAk;
- pkcs7->hashOID = SHA256h;
- pkcs7->rng = &rng;
- /* Set no certs in bundle for this test. Hang on to the pointer though to
- * free it later. */
- pt = (void*)pkcs7->certList;
- pkcs7->certList = NULL; /* no certs in bundle */
- AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
- pkcs7->certList = (Pkcs7Cert*)pt; /* restore pointer for PKCS7 free call */
- wc_PKCS7_Free(pkcs7);
- /* check verify fails */
- AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
- PKCS7_SIGNEEDS_CHECK);
- /* try verifying the signature manually */
- {
- RsaKey rKey;
- word32 idx = 0;
- byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
- WC_MAX_DIGEST_SIZE];
- int digestSz;
- AssertIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
- AssertIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
- digestSz = wc_RsaSSL_Verify(pkcs7->signature, pkcs7->signatureSz,
- digest, sizeof(digest), &rKey);
- AssertIntGT(digestSz, 0);
- AssertIntEQ(digestSz, pkcs7->pkcs7DigestSz);
- AssertIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
- AssertIntEQ(wc_FreeRsaKey(&rKey), 0);
- /* verify was success */
- }
- wc_PKCS7_Free(pkcs7);
- /* initializing the PKCS7 struct with the signing certificate should pass */
- AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
- wc_PKCS7_Free(pkcs7);
- /* create valid degenerate bundle */
- sigSz = FOURK_BUF * 2;
- AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
- pkcs7->content = env;
- pkcs7->contentSz = envSz;
- pkcs7->contentOID = DATA;
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = keySz;
- pkcs7->encryptOID = RSAk;
- pkcs7->hashOID = SHA256h;
- pkcs7->rng = &rng;
- AssertIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
- AssertIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
- wc_PKCS7_Free(pkcs7);
- wc_FreeRng(&rng);
- /* check verify */
- AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
- AssertNotNull(pkcs7->content);
- #ifdef HAVE_AES_CBC
- /* check decode */
- AssertNotNull(inner = wc_PKCS7_New(NULL, 0));
- AssertIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
- inner->privateKey = key;
- inner->privateKeySz = keySz;
- AssertIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
- pkcs7->contentSz, decoded, decodedSz)), 0);
- wc_PKCS7_Free(inner);
- #endif
- wc_PKCS7_Free(pkcs7);
- #ifdef HAVE_AES_CBC
- /* check cert set */
- AssertNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
- AssertNotNull(pkcs7->singleCert);
- AssertIntNE(pkcs7->singleCertSz, 0);
- wc_PKCS7_Free(pkcs7);
- #endif
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
- return res;
- }
- static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
- && !defined(NO_AES)
- PKCS7* pkcs7;
- void* heap = NULL;
- pkcs7 = wc_PKCS7_New(heap, testDevId);
- AssertNotNull(pkcs7);
- AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
- AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_PKCS7_SetOriEncryptCtx(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
- && !defined(NO_AES)
- PKCS7* pkcs7;
- void* heap = NULL;
- WOLFSSL_CTX* ctx;
- ctx = NULL;
- pkcs7 = wc_PKCS7_New(heap, testDevId);
- AssertNotNull(pkcs7);
- AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
- AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_PKCS7_SetOriDecryptCtx(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
- && !defined(NO_AES)
- PKCS7* pkcs7;
- void* heap = NULL;
- WOLFSSL_CTX* ctx;
- ctx = NULL;
- pkcs7 = wc_PKCS7_New(heap, testDevId);
- AssertNotNull(pkcs7);
- AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
- AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_PKCS7_DecodeCompressedData(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
- && !defined(NO_AES) && defined(HAVE_LIBZ)
- PKCS7* pkcs7;
- void* heap = NULL;
- byte out[4096];
- byte *decompressed;
- int outSz, decompressedSz;
- const char* cert = "./certs/client-cert.pem";
- byte* cert_buf = NULL;
- size_t cert_sz = 0;
- AssertIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
- AssertNotNull((decompressed =
- (byte*)XMALLOC(cert_sz, heap, DYNAMIC_TYPE_TMP_BUFFER)));
- decompressedSz = (int)cert_sz;
- AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
- pkcs7->content = (byte*)cert_buf;
- pkcs7->contentSz = (word32)cert_sz;
- pkcs7->contentOID = DATA;
- AssertIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
- sizeof(out))), 0);
- wc_PKCS7_Free(pkcs7);
- /* compressed key should be smaller than when started */
- AssertIntLT(outSz, cert_sz);
- /* test decompression */
- AssertNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
- AssertIntEQ(pkcs7->contentOID, 0);
- /* fail case with out buffer too small */
- AssertIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
- decompressed, outSz), 0);
- /* success case */
- AssertIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
- decompressed, decompressedSz), cert_sz);
- AssertIntEQ(pkcs7->contentOID, DATA);
- AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
- XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
- decompressed = NULL;
- /* test decompression function with different 'max' inputs */
- outSz = sizeof(out);
- AssertIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
- 0);
- AssertIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
- out, outSz, 0, heap), 0);
- AssertNull(decompressed);
- AssertIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
- out, outSz, 0, heap), 0);
- AssertNotNull(decompressed);
- AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
- XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
- decompressed = NULL;
- AssertIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
- out, outSz, 0, heap), 0);
- AssertNotNull(decompressed);
- AssertIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
- XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
- if (cert_buf)
- free(cert_buf);
- wc_PKCS7_Free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_i2d_PKCS12(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
- && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
- && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
- WC_PKCS12* pkcs12 = NULL;
- unsigned char der[FOURK_BUF * 2];
- unsigned char* pt;
- int derSz;
- unsigned char out[FOURK_BUF * 2];
- int outSz = FOURK_BUF * 2;
- const char p12_f[] = "./certs/test-servercert.p12";
- XFILE f;
- f = XFOPEN(p12_f, "rb");
- AssertNotNull(f);
- derSz = (int)XFREAD(der, 1, sizeof(der), f);
- AssertIntGT(derSz, 0);
- XFCLOSE(f);
- AssertNotNull(pkcs12 = wc_PKCS12_new());
- AssertIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
- AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
- AssertIntEQ(outSz, derSz);
- outSz = derSz - 1;
- pt = out;
- AssertIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
- outSz = derSz;
- AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
- AssertIntEQ((pt == out), 0);
- pt = NULL;
- AssertIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
- XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
- wc_PKCS12_free(pkcs12);
- /* Run the same test but use wc_d2i_PKCS12_fp. */
- AssertNotNull(pkcs12 = wc_PKCS12_new());
- AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
- AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
- AssertIntEQ(outSz, derSz);
- wc_PKCS12_free(pkcs12);
- /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
- pkcs12 = NULL;
- AssertIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
- AssertIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
- AssertIntEQ(outSz, derSz);
- wc_PKCS12_free(pkcs12);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /* Testing wc_SignatureGetSize() for signature type ECC */
- static int test_wc_SignatureGetSize_ecc(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SIG_WRAPPER
- int ret;
- #if defined(HAVE_ECC) && !defined(NO_ECC256)
- enum wc_SignatureType sig_type;
- word32 key_len;
- /* Initialize ECC Key */
- ecc_key ecc;
- const char* qx =
- "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
- const char* qy =
- "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
- const char* d =
- "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
- ret = wc_ecc_init(&ecc);
- if (ret == 0) {
- ret = wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1");
- }
- if (ret == 0) {
- /* Input for signature type ECC */
- sig_type = WC_SIGNATURE_TYPE_ECC;
- key_len = sizeof(ecc_key);
- ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
- /* Test bad args */
- if (ret > 0) {
- sig_type = (enum wc_SignatureType) 100;
- ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
- if (ret == BAD_FUNC_ARG) {
- sig_type = WC_SIGNATURE_TYPE_ECC;
- ret = wc_SignatureGetSize(sig_type, NULL, key_len);
- }
- if (ret >= 0) {
- key_len = (word32) 0;
- ret = wc_SignatureGetSize(sig_type, &ecc, key_len);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = SIG_TYPE_E;
- }
- }
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&ecc);
- #else
- ret = SIG_TYPE_E;
- #endif
- res = TEST_RES_CHECK(ret == SIG_TYPE_E);
- #endif /* NO_SIG_WRAPPER */
- return res;
- }/* END test_wc_SignatureGetSize_ecc() */
- /* Testing wc_SignatureGetSize() for signature type rsa */
- static int test_wc_SignatureGetSize_rsa(void)
- {
- int res = TEST_SKIPPED;
- #ifndef NO_SIG_WRAPPER
- int ret = 0;
- #ifndef NO_RSA
- enum wc_SignatureType sig_type;
- word32 key_len;
- word32 idx = 0;
- /* Initialize RSA Key */
- RsaKey rsa_key;
- byte* tmp = NULL;
- size_t bytes;
- #ifdef USE_CERT_BUFFERS_1024
- bytes = (size_t)sizeof_client_key_der_1024;
- if (bytes < (size_t)sizeof_client_key_der_1024)
- bytes = (size_t)sizeof_client_cert_der_1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- bytes = (size_t)sizeof_client_key_der_2048;
- if (bytes < (size_t)sizeof_client_cert_der_2048)
- bytes = (size_t)sizeof_client_cert_der_2048;
- #else
- bytes = FOURK_BUF;
- #endif
- tmp = (byte*)XMALLOC(bytes, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp != NULL) {
- #ifdef USE_CERT_BUFFERS_1024
- XMEMCPY(tmp, client_key_der_1024,
- (size_t)sizeof_client_key_der_1024);
- #elif defined(USE_CERT_BUFFERS_2048)
- XMEMCPY(tmp, client_key_der_2048,
- (size_t)sizeof_client_key_der_2048);
- #elif !defined(NO_FILESYSTEM)
- file = XFOPEN(clientKey, "rb");
- if (file != XBADFILE) {
- bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file);
- XFCLOSE(file);
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- #else
- ret = WOLFSSL_FATAL_ERROR;
- #endif
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId);
- }
- if (ret == 0) {
- ret = wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes);
- }
- if (ret == 0) {
- /* Input for signature type RSA */
- sig_type = WC_SIGNATURE_TYPE_RSA;
- key_len = sizeof(RsaKey);
- ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
- /* Test bad args */
- if (ret > 0) {
- sig_type = (enum wc_SignatureType) 100;
- ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
- if (ret == BAD_FUNC_ARG) {
- sig_type = WC_SIGNATURE_TYPE_RSA;
- ret = wc_SignatureGetSize(sig_type, NULL, key_len);
- }
- #ifndef HAVE_USER_RSA
- if (ret == BAD_FUNC_ARG)
- #else
- if (ret == 0)
- #endif
- {
- key_len = (word32)0;
- ret = wc_SignatureGetSize(sig_type, &rsa_key, key_len);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = SIG_TYPE_E;
- }
- }
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_FreeRsaKey(&rsa_key);
- XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- #else
- ret = SIG_TYPE_E;
- #endif
- res = TEST_RES_CHECK(ret == SIG_TYPE_E);
- #endif /* NO_SIG_WRAPPER */
- return res;
- }/* END test_wc_SignatureGetSize_rsa(void) */
- /*----------------------------------------------------------------------------*
- | hash.h Tests
- *----------------------------------------------------------------------------*/
- static int test_wc_HashInit(void)
- {
- int ret = 0, i; /* 0 indicates tests passed, 1 indicates failure */
- wc_HashAlg hash;
- /* enum for holding supported algorithms, #ifndef's restrict if disabled */
- enum wc_HashType enumArray[] = {
- #ifndef NO_MD5
- WC_HASH_TYPE_MD5,
- #endif
- #ifndef NO_SHA
- WC_HASH_TYPE_SHA,
- #endif
- #ifndef WOLFSSL_SHA224
- WC_HASH_TYPE_SHA224,
- #endif
- #ifndef NO_SHA256
- WC_HASH_TYPE_SHA256,
- #endif
- #ifndef WOLFSSL_SHA384
- WC_HASH_TYPE_SHA384,
- #endif
- #ifndef WOLFSSL_SHA512
- WC_HASH_TYPE_SHA512,
- #endif
- };
- /* dynamically finds the length */
- int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
- /* For loop to test various arguments... */
- for (i = 0; i < enumlen; i++) {
- /* check for bad args */
- if (wc_HashInit(&hash, enumArray[i]) == BAD_FUNC_ARG) {
- ret = 1;
- break;
- }
- wc_HashFree(&hash, enumArray[i]);
- /* check for null ptr */
- if (wc_HashInit(NULL, enumArray[i]) != BAD_FUNC_ARG) {
- ret = 1;
- break;
- }
- } /* end of for loop */
- return TEST_RES_CHECK(ret == 0);
- } /* end of test_wc_HashInit */
- /*
- * Unit test function for wc_HashSetFlags()
- */
- static int test_wc_HashSetFlags(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_HASH_FLAGS
- wc_HashAlg hash;
- int ret = 0;
- word32 flags = 0;
- int i, j;
- int notSupportedLen;
- /* enum for holding supported algorithms, #ifndef's restrict if disabled */
- enum wc_HashType enumArray[] = {
- #ifndef NO_MD5
- WC_HASH_TYPE_MD5,
- #endif
- #ifndef NO_SHA
- WC_HASH_TYPE_SHA,
- #endif
- #ifdef WOLFSSL_SHA224
- WC_HASH_TYPE_SHA224,
- #endif
- #ifndef NO_SHA256
- WC_HASH_TYPE_SHA256,
- #endif
- #ifdef WOLFSSL_SHA384
- WC_HASH_TYPE_SHA384,
- #endif
- #ifdef WOLFSSL_SHA512
- WC_HASH_TYPE_SHA512,
- #endif
- #ifdef WOLFSSL_SHA3
- WC_HASH_TYPE_SHA3_224,
- #endif
- };
- enum wc_HashType notSupported[] = {
- WC_HASH_TYPE_MD5_SHA,
- WC_HASH_TYPE_MD2,
- WC_HASH_TYPE_MD4,
- WC_HASH_TYPE_BLAKE2B,
- WC_HASH_TYPE_BLAKE2S,
- WC_HASH_TYPE_NONE,
- };
- /* dynamically finds the length */
- int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
- /* For loop to test various arguments... */
- for (i = 0; i < enumlen; i++) {
- ret = wc_HashInit(&hash, enumArray[i]);
- if (ret == 0) {
- ret = wc_HashSetFlags(&hash, enumArray[i], flags);
- }
- if (ret == 0) {
- if (flags & WC_HASH_FLAG_ISCOPY) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_HashSetFlags(NULL, enumArray[i], flags);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- wc_HashFree(&hash, enumArray[i]);
- }
- /* For loop to test not supported cases */
- notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
- for (j = 0; ret == 0 && j < notSupportedLen; j++) {
- ret = wc_HashInit(&hash, notSupported[j]);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = wc_HashSetFlags(&hash, notSupported[j], flags);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_HashFree(&hash, notSupported[j]);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_HashSetFlags */
- /*
- * Unit test function for wc_HashGetFlags()
- */
- static int test_wc_HashGetFlags(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_HASH_FLAGS
- wc_HashAlg hash;
- int ret = 0;
- word32 flags = 0;
- int i, j;
- /* enum for holding supported algorithms, #ifndef's restrict if disabled */
- enum wc_HashType enumArray[] = {
- #ifndef NO_MD5
- WC_HASH_TYPE_MD5,
- #endif
- #ifndef NO_SHA
- WC_HASH_TYPE_SHA,
- #endif
- #ifdef WOLFSSL_SHA224
- WC_HASH_TYPE_SHA224,
- #endif
- #ifndef NO_SHA256
- WC_HASH_TYPE_SHA256,
- #endif
- #ifdef WOLFSSL_SHA384
- WC_HASH_TYPE_SHA384,
- #endif
- #ifdef WOLFSSL_SHA512
- WC_HASH_TYPE_SHA512,
- #endif
- #ifdef WOLFSSL_SHA3
- WC_HASH_TYPE_SHA3_224,
- #endif
- };
- enum wc_HashType notSupported[] = {
- WC_HASH_TYPE_MD5_SHA,
- WC_HASH_TYPE_MD2,
- WC_HASH_TYPE_MD4,
- WC_HASH_TYPE_BLAKE2B,
- WC_HASH_TYPE_BLAKE2S,
- WC_HASH_TYPE_NONE,
- };
- int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
- int notSupportedLen;
- /* For loop to test various arguments... */
- for (i = 0; i < enumlen; i++) {
- ret = wc_HashInit(&hash, enumArray[i]);
- if (ret == 0) {
- ret = wc_HashGetFlags(&hash, enumArray[i], &flags);
- }
- if (ret == 0) {
- if (flags & WC_HASH_FLAG_ISCOPY) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_HashGetFlags(NULL, enumArray[i], &flags);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- wc_HashFree(&hash, enumArray[i]);
- if (ret != 0) {
- break;
- }
- }
- /* For loop to test not supported cases */
- notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
- for (j = 0; ret == 0 && j < notSupportedLen; j++) {
- ret = wc_HashInit(&hash, notSupported[j]);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
- if (ret == 0) {
- ret = -1;
- }
- else if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_HashFree(&hash, notSupported[j]);
- if (ret == 0) {
- ret = -1;
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- } /* END test_wc_HashGetFlags */
- /*----------------------------------------------------------------------------*
- | Compatibility Tests
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_lhash(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_ALL
- const char testStr[] = "Like a true nature's child\n"
- "We were born\n"
- "Born to be wild";
- #ifdef NO_SHA
- AssertIntEQ(lh_strhash(testStr), 0xf9dc8a43);
- #else
- AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_NAME(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
- !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
- && !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
- (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
- defined(OPENSSL_EXTRA))
- X509* x509;
- const unsigned char* c;
- unsigned char buf[4096];
- int bytes;
- XFILE f;
- const X509_NAME* a;
- const X509_NAME* b;
- X509_NAME* d2i_name = NULL;
- int sz;
- unsigned char* tmp;
- char file[] = "./certs/ca-cert.der";
- #ifndef OPENSSL_EXTRA_X509_SMALL
- byte empty[] = { /* CN=empty emailAddress= */
- 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
- 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
- 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
- 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
- 0x01, 0x16, 0x00
- };
- #endif
- #ifndef OPENSSL_EXTRA_X509_SMALL
- /* test compile of deprecated function, returns 0 */
- AssertIntEQ(CRYPTO_thread_id(), 0);
- #endif
- AssertNotNull(a = X509_NAME_new());
- X509_NAME_free((X509_NAME*)a);
- f = XFOPEN(file, "rb");
- AssertTrue(f != XBADFILE);
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- c = buf;
- AssertNotNull(x509 = wolfSSL_X509_d2i(NULL, c, bytes));
- /* test cmp function */
- AssertNotNull(a = X509_get_issuer_name(x509));
- AssertNotNull(b = X509_get_subject_name(x509));
- #ifndef OPENSSL_EXTRA_X509_SMALL
- AssertIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
- #endif
- tmp = buf;
- AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
- if (sz > 0 && tmp == buf) {
- fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
- __LINE__);
- fprintf(stderr, " Expected pointer to be incremented\n");
- abort();
- }
- #ifndef OPENSSL_EXTRA_X509_SMALL
- tmp = buf;
- AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
- #endif
- /* if output parameter is NULL, should still return required size. */
- AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
- /* retry but with the function creating a buffer */
- tmp = NULL;
- AssertIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
- XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
- AssertNotNull(b = X509_NAME_dup((X509_NAME*)a));
- #ifndef OPENSSL_EXTRA_X509_SMALL
- AssertIntEQ(X509_NAME_cmp(a, b), 0);
- #endif
- X509_NAME_free((X509_NAME*)b);
- X509_NAME_free(d2i_name);
- X509_free(x509);
- #ifndef OPENSSL_EXTRA_X509_SMALL
- /* test with an empty domain component */
- tmp = empty;
- sz = sizeof(empty);
- AssertNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
- AssertIntEQ(X509_NAME_entry_count(d2i_name), 2);
- /* size of empty emailAddress will be 0 */
- tmp = buf;
- AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
- (char*)tmp, sizeof(buf)), 0);
- /* should contain no organization name */
- tmp = buf;
- AssertIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
- (char*)tmp, sizeof(buf)), -1);
- X509_NAME_free(d2i_name);
- #endif
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
- return res;
- }
- static int test_wolfSSL_X509_NAME_hash(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
- && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
- BIO* bio;
- X509* x509 = NULL;
- AssertNotNull(bio = BIO_new(BIO_s_file()));
- AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
- AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
- AssertIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
- AssertIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
- X509_free(x509);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_NAME_print_ex(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
- (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
- defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
- defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
- !defined(NO_BIO) && !defined(NO_RSA)
- int memSz;
- byte* mem = NULL;
- BIO* bio = NULL;
- BIO* membio = NULL;
- X509* x509 = NULL;
- X509_NAME* name = NULL;
- const char* expNormal = "C=US, CN=wolfssl.com";
- const char* expReverse = "CN=wolfssl.com, C=US";
- const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
- const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
- const char* expRFC5523 =
- "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
- /* Test with real cert (svrCertFile) first */
- AssertNotNull(bio = BIO_new(BIO_s_file()));
- AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
- AssertNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
- AssertNotNull(name = X509_get_subject_name(x509));
- /* Test without flags */
- AssertNotNull(membio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
- BIO_free(membio);
- /* Test flag: XN_FLAG_RFC2253 */
- AssertNotNull(membio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
- XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
- BIO_free(membio);
- /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
- AssertNotNull(membio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
- XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
- BIO_free(membio);
- X509_free(x509);
- BIO_free(bio);
- /* Test normal case without escaped characters */
- {
- /* Create name: "/C=US/CN=wolfssl.com" */
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
- MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
- WOLFSSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
- MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
- WOLFSSL_SUCCESS);
- /* Test without flags */
- AssertNotNull(membio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
- AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
- AssertIntEQ(memSz, XSTRLEN(expNormal));
- AssertIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
- BIO_free(membio);
- /* Test flags: XN_FLAG_RFC2253 - should be reversed */
- AssertNotNull(membio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
- XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
- AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
- AssertIntEQ(memSz, XSTRLEN(expReverse));
- BIO_free(membio);
- /* Test flags: XN_FLAG_DN_REV - reversed */
- AssertNotNull(membio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
- XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
- AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
- AssertIntEQ(memSz, XSTRLEN(expReverse));
- AssertIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
- BIO_free(membio);
- X509_NAME_free(name);
- }
- /* Test RFC2253 characters are escaped with backslashes */
- {
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
- /* space at beginning and end, and: ,+"\ */
- MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
- WOLFSSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
- /* # at beginning, and: <>;*/
- MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
- WOLFSSL_SUCCESS);
- /* Test without flags */
- AssertNotNull(membio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
- AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
- AssertIntEQ(memSz, XSTRLEN(expNotEscaped));
- AssertIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
- XSTRLEN(expNotEscaped)), 0);
- BIO_free(membio);
- /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
- AssertNotNull(membio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
- XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
- AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
- AssertIntEQ(memSz, XSTRLEN(expRFC5523));
- AssertIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
- BIO_free(membio);
- /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
- AssertNotNull(membio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_NAME_print_ex(membio, name, 0,
- XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
- AssertIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
- AssertIntEQ(memSz, XSTRLEN(expNotEscapedRev));
- AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
- XSTRLEN(expNotEscapedRev)), 0);
- BIO_free(membio);
- X509_NAME_free(name);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_X509_INFO_multiple_info(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- STACK_OF(X509_INFO) *info_stack;
- X509_INFO *info;
- int len;
- int i;
- const char* files[] = {
- cliCertFile,
- cliKeyFile,
- /* This needs to be the order as svrCertFile contains the
- * intermediate cert as well. */
- svrKeyFile,
- svrCertFile,
- NULL,
- };
- const char** curFile;
- BIO *fileBIO;
- BIO *concatBIO = NULL;
- byte tmp[FOURK_BUF];
- /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
- * to group objects together. */
- AssertNotNull(concatBIO = BIO_new(BIO_s_mem()));
- for (curFile = files; *curFile != NULL; curFile++) {
- int fileLen;
- AssertNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
- fileLen = wolfSSL_BIO_get_len(fileBIO);
- while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
- AssertIntEQ(BIO_write(concatBIO, tmp, len), len);
- fileLen -= len;
- }
- /* Make sure we read the entire file */
- AssertIntEQ(fileLen, 0);
- BIO_free(fileBIO);
- }
- AssertNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
- NULL));
- AssertIntEQ(sk_X509_INFO_num(info_stack), 3);
- for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
- AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
- AssertNotNull(info->x509);
- AssertNull(info->crl);
- if (i != 0) {
- AssertNotNull(info->x_pkey);
- AssertIntEQ(X509_check_private_key(info->x509,
- info->x_pkey->dec_pkey), 1);
- }
- else {
- AssertNull(info->x_pkey);
- }
- }
- sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
- BIO_free(concatBIO);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif
- #ifndef NO_BIO
- static int test_wolfSSL_X509_INFO(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- STACK_OF(X509_INFO) *info_stack;
- X509_INFO *info;
- BIO *cert;
- int i;
- /* PEM in hex format to avoid null terminator */
- byte data[] = {
- 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
- 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
- 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
- 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
- 0x2d, 0x2d, 0x2d
- };
- /* PEM in hex format to avoid null terminator */
- byte data2[] = {
- 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
- 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
- 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
- 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
- 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
- 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
- 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
- };
- AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
- AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
- for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
- AssertNotNull(info = sk_X509_INFO_value(info_stack, i));
- AssertNotNull(info->x509);
- AssertNull(info->crl);
- AssertNull(info->x_pkey);
- }
- sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
- BIO_free(cert);
- AssertNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
- AssertNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
- sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
- BIO_free(cert);
- /* This case should fail due to invalid input. */
- AssertNotNull(cert = BIO_new(BIO_s_mem()));
- AssertIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
- AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
- sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
- BIO_free(cert);
- AssertNotNull(cert = BIO_new(BIO_s_mem()));
- AssertIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
- AssertNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
- sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
- BIO_free(cert);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif
- static int test_wolfSSL_X509_subject_name_hash(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
- && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
- X509* x509;
- X509_NAME* subjectName = NULL;
- unsigned long ret1 = 0;
- unsigned long ret2 = 0;
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM));
- AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
- /* These two
- * - X509_subject_name_hash(x509)
- * - X509_NAME_hash(X509_get_subject_name(x509))
- * should give the same hash, if !defined(NO_SHA) is true. */
- ret1 = X509_subject_name_hash(x509);
- AssertIntNE(ret1, 0);
- #if !defined(NO_SHA)
- ret2 = X509_NAME_hash(X509_get_subject_name(x509));
- AssertIntNE(ret2, 0);
- AssertIntEQ(ret1, ret2);
- #else
- (void) ret2;
- #endif
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_issuer_name_hash(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
- && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
- X509* x509;
- X509_NAME* issuertName = NULL;
- unsigned long ret1 = 0;
- unsigned long ret2 = 0;
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM));
- AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
- /* These two
- * - X509_issuer_name_hash(x509)
- * - X509_NAME_hash(X509_get_issuer_name(x509))
- * should give the same hash, if !defined(NO_SHA) is true. */
- ret1 = X509_issuer_name_hash(x509);
- AssertIntNE(ret1, 0);
- #if !defined(NO_SHA)
- ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
- AssertIntNE(ret2, 0);
- AssertIntEQ(ret1, ret2);
- #else
- (void) ret2;
- #endif
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_check_host(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
- && !defined(NO_SHA) && !defined(NO_RSA)
- X509* x509;
- const char altName[] = "example.com";
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM));
- AssertIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
- WOLFSSL_SUCCESS);
- AssertIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
- WOLFSSL_FAILURE);
- X509_free(x509);
- AssertIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
- WOLFSSL_FAILURE);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_check_email(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
- X509* x509;
- const char goodEmail[] = "info@wolfssl.com";
- const char badEmail[] = "disinfo@wolfssl.com";
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM));
- /* Should fail on non-matching email address */
- AssertIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
- WOLFSSL_FAILURE);
- /* Should succeed on matching email address */
- AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
- WOLFSSL_SUCCESS);
- /* Should compute length internally when not provided */
- AssertIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
- WOLFSSL_SUCCESS);
- /* Should fail when email address is NULL */
- AssertIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
- WOLFSSL_FAILURE);
- X509_free(x509);
- /* Should fail when x509 is NULL */
- AssertIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
- WOLFSSL_FAILURE);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
- return res;
- }
- static int test_wolfSSL_DES(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
- const_DES_cblock myDes;
- DES_cblock iv;
- DES_key_schedule key;
- word32 i;
- DES_LONG dl;
- unsigned char msg[] = "hello wolfssl";
- DES_check_key(1);
- DES_set_key(&myDes, &key);
- /* check, check of odd parity */
- XMEMSET(myDes, 4, sizeof(const_DES_cblock)); myDes[0] = 6; /*set even parity*/
- XMEMSET(key, 5, sizeof(DES_key_schedule));
- AssertIntEQ(DES_set_key_checked(&myDes, &key), -1);
- AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
- /* set odd parity for success case */
- DES_set_odd_parity(&myDes);
- AssertIntEQ(DES_check_key_parity(&myDes), 1);
- fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
- myDes[3]);
- AssertIntEQ(DES_set_key_checked(&myDes, &key), 0);
- for (i = 0; i < sizeof(DES_key_schedule); i++) {
- AssertIntEQ(key[i], myDes[i]);
- }
- AssertIntEQ(DES_is_weak_key(&myDes), 0);
- /* check weak key */
- XMEMSET(myDes, 1, sizeof(const_DES_cblock));
- XMEMSET(key, 5, sizeof(DES_key_schedule));
- AssertIntEQ(DES_set_key_checked(&myDes, &key), -2);
- AssertIntNE(key[0], myDes[0]); /* should not have copied over key */
- /* now do unchecked copy of a weak key over */
- DES_set_key_unchecked(&myDes, &key);
- /* compare arrays, should be the same */
- for (i = 0; i < sizeof(DES_key_schedule); i++) {
- AssertIntEQ(key[i], myDes[i]);
- }
- AssertIntEQ(DES_is_weak_key(&myDes), 1);
- /* check DES_key_sched API */
- XMEMSET(key, 1, sizeof(DES_key_schedule));
- AssertIntEQ(DES_key_sched(&myDes, NULL), 0);
- AssertIntEQ(DES_key_sched(NULL, &key), 0);
- AssertIntEQ(DES_key_sched(&myDes, &key), 0);
- /* compare arrays, should be the same */
- for (i = 0; i < sizeof(DES_key_schedule); i++) {
- AssertIntEQ(key[i], myDes[i]);
- }
- /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
- * DES_cbc_encrypt on the input */
- XMEMSET(iv, 0, sizeof(DES_cblock));
- XMEMSET(myDes, 5, sizeof(DES_key_schedule));
- AssertIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
- AssertIntEQ(dl, 480052723);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
- return res;
- }
- static int test_wc_PemToDer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
- int ret;
- DerBuffer* pDer = NULL;
- const char* ca_cert = "./certs/server-cert.pem";
- byte* cert_buf = NULL;
- size_t cert_sz = 0;
- int eccKey = 0;
- EncryptedInfo info;
- XMEMSET(&info, 0, sizeof(info));
- ret = load_file(ca_cert, &cert_buf, &cert_sz);
- if (ret == 0) {
- ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE,
- &pDer, NULL, &info, &eccKey);
- AssertIntEQ(ret, 0);
- wc_FreeDer(&pDer);
- }
- if (cert_buf)
- free(cert_buf);
- #ifdef HAVE_ECC
- {
- const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
- byte key_buf[256] = {0};
- /* Test fail of loading a key with cert type */
- AssertIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
- key_buf[0] = '\n';
- XMEMCPY(key_buf + 1, cert_buf, cert_sz);
- AssertIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
- &pDer, NULL, &info, &eccKey)), 0);
- #ifdef OPENSSL_EXTRA
- AssertIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
- &pDer, NULL, &info, &eccKey)), 0);
- #endif
- wc_FreeDer(&pDer);
- if (cert_buf)
- free(cert_buf);
- }
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_AllocDer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS)
- int ret;
- DerBuffer* pDer = NULL;
- word32 testSize = 1024;
- ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
- AssertIntEQ(ret, 0);
- AssertNotNull(pDer);
- wc_FreeDer(&pDer);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_CertPemToDer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
- int ret;
- const char* ca_cert = "./certs/ca-cert.pem";
- byte* cert_buf = NULL;
- size_t cert_sz = 0, cert_dersz = 0;
- byte* cert_der = NULL;
- ret = load_file(ca_cert, &cert_buf, &cert_sz);
- if (ret == 0) {
- cert_dersz = cert_sz; /* DER will be smaller than PEM */
- cert_der = (byte*)malloc(cert_dersz);
- if (cert_der) {
- ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
- cert_der, (int)cert_dersz, CERT_TYPE);
- AssertIntGE(ret, 0);
- }
- }
- if (cert_der)
- free(cert_der);
- if (cert_buf)
- free(cert_buf);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_PubKeyPemToDer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
- (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
- int ret;
- const char* key = "./certs/ecc-client-keyPub.pem";
- byte* cert_buf = NULL;
- size_t cert_sz = 0, cert_dersz = 0;
- byte* cert_der = NULL;
- ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
- cert_der, (int)cert_dersz);
- AssertIntGE(ret, BAD_FUNC_ARG);
- ret = load_file(key, &cert_buf, &cert_sz);
- if (ret == 0) {
- cert_dersz = cert_sz; /* DER will be smaller than PEM */
- cert_der = (byte*)malloc(cert_dersz);
- if (cert_der) {
- ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
- cert_der, (int)cert_dersz);
- AssertIntGE(ret, 0);
- }
- }
- if (cert_der)
- free(cert_der);
- if (cert_buf)
- free(cert_buf);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_PemPubKeyToDer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && \
- (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
- int ret;
- const char* key = "./certs/ecc-client-keyPub.pem";
- size_t cert_dersz = 1024;
- byte* cert_der = (byte*)malloc(cert_dersz);
- ret = wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz);
- AssertIntGE(ret, BAD_FUNC_ARG);
- if (cert_der) {
- ret = wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz);
- AssertIntGE(ret, 0);
- free(cert_der);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_GetPubKeyDerFromCert(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) || defined(HAVE_ECC)
- int ret;
- word32 idx = 0;
- byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
- word32 keyDerSz = (word32)sizeof(keyDer);
- DecodedCert decoded;
- #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
- byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
- word32 certBufSz = sizeof(certBuf);
- #endif
- #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
- defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- XFILE fp;
- #endif
- #ifndef NO_RSA
- RsaKey rsaKey;
- #if defined(USE_CERT_BUFFERS_2048)
- byte* rsaCertDer = (byte*)client_cert_der_2048;
- word32 rsaCertDerSz = sizeof_client_cert_der_2048;
- #elif defined(USE_CERT_BUFFERS_1024)
- byte* rsaCertDer = (byte*)client_cert_der_1024;
- word32 rsaCertDerSz = sizeof_client_cert_der_1024;
- #else
- unsigned char rsaCertDer[TWOK_BUF];
- word32 rsaCertDerSz;
- #endif
- #endif
- #ifdef HAVE_ECC
- ecc_key eccKey;
- #if defined(USE_CERT_BUFFERS_256)
- byte* eccCert = (byte*)cliecc_cert_der_256;
- word32 eccCertSz = sizeof_cliecc_cert_der_256;
- #else
- unsigned char eccCert[ONEK_BUF];
- word32 eccCertSz;
- XFILE fp2;
- #endif
- #endif
- #ifndef NO_RSA
- #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
- fp = XFOPEN("./certs/1024/client-cert.der", "rb");
- AssertTrue((fp != XBADFILE));
- rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer), fp);
- XFCLOSE(fp);
- #endif
- /* good test case - RSA DER cert */
- wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
- ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
- AssertIntEQ(ret, 0);
- ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
- AssertIntEQ(ret, 0);
- AssertIntGT(keyDerSz, 0);
- /* sanity check, verify we can import DER public key */
- ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
- AssertIntEQ(ret, 0);
- ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
- AssertIntEQ(ret, 0);
- wc_FreeRsaKey(&rsaKey);
- /* test LENGTH_ONLY_E case */
- keyDerSz = 0;
- ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
- AssertIntEQ(ret, LENGTH_ONLY_E);
- AssertIntGT(keyDerSz, 0);
- /* bad args: DecodedCert NULL */
- ret = wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- /* bad args: output key buff size */
- ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- /* bad args: zero size output key buffer */
- keyDerSz = 0;
- ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- wc_FreeDecodedCert(&decoded);
- /* Certificate Request Tests */
- #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
- {
- XMEMSET(certBuf, 0, sizeof(certBuf));
- fp = XFOPEN("./certs/csr.signed.der", "rb");
- AssertTrue((fp != XBADFILE));
- certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp);
- XFCLOSE(fp);
- wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
- ret = wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL);
- AssertIntEQ(ret, 0);
- /* good test case - RSA DER certificate request */
- keyDerSz = sizeof(keyDer);
- ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
- AssertIntEQ(ret, 0);
- AssertIntGT(keyDerSz, 0);
- /* sanity check, verify we can import DER public key */
- ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
- AssertIntEQ(ret, 0);
- idx = 0;
- ret = wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz);
- AssertIntEQ(ret, 0);
- wc_FreeRsaKey(&rsaKey);
- wc_FreeDecodedCert(&decoded);
- }
- #endif /* WOLFSSL_CERT_REQ */
- #endif /* NO_RSA */
- #ifdef HAVE_ECC
- #ifndef USE_CERT_BUFFERS_256
- fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb");
- AssertTrue((fp2 != XBADFILE));
- eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2);
- XFCLOSE(fp2);
- #endif
- wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
- ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
- AssertIntEQ(ret, 0);
- /* good test case - ECC */
- XMEMSET(keyDer, 0, sizeof(keyDer));
- keyDerSz = sizeof(keyDer);
- ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
- AssertIntEQ(ret, 0);
- AssertIntGT(keyDerSz, 0);
- /* sanity check, verify we can import DER public key */
- ret = wc_ecc_init(&eccKey);
- AssertIntEQ(ret, 0);
- idx = 0; /* reset idx to 0, used above in RSA case */
- ret = wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz);
- AssertIntEQ(ret, 0);
- wc_ecc_free(&eccKey);
- /* test LENGTH_ONLY_E case */
- keyDerSz = 0;
- ret = wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz);
- AssertIntEQ(ret, LENGTH_ONLY_E);
- AssertIntGT(keyDerSz, 0);
- wc_FreeDecodedCert(&decoded);
- #endif
- res = TEST_RES_CHECK(1);
- #endif /* !NO_RSA || HAVE_ECC */
- return res;
- }
- static int test_wc_CheckCertSigPubKey(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
- int ret;
- const char* ca_cert = "./certs/ca-cert.pem";
- byte* cert_buf = NULL;
- size_t cert_sz = 0;
- byte* cert_der = NULL;
- word32 cert_dersz = 0;
- byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
- word32 keyDerSz = (word32)sizeof(keyDer);
- DecodedCert decoded;
- ret = load_file(ca_cert, &cert_buf, &cert_sz);
- if (ret == 0) {
- cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
- cert_der = (byte*)malloc(cert_dersz);
- if (cert_der) {
- ret = wc_CertPemToDer(cert_buf, (int)cert_sz,
- cert_der, (int)cert_dersz, CERT_TYPE);
- AssertIntGE(ret, 0);
- }
- }
- wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
- ret = wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL);
- AssertIntEQ(ret, 0);
- ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz);
- AssertIntEQ(ret, 0);
- AssertIntGT(keyDerSz, 0);
- /* Good test case. */
- ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
- RSAk);
- AssertIntEQ(ret, 0);
- /* No certificate. */
- ret = wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
- ECDSAk);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- /* Bad cert size. */
- ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
- RSAk);
- AssertTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
- /* No public key. */
- ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL, keyDerSz,
- RSAk);
- AssertIntEQ(ret, ASN_NO_SIGNER_E);
- /* Bad public key size. */
- ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
- RSAk);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- /* Wrong aglo. */
- ret = wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, keyDerSz,
- ECDSAk);
- AssertIntEQ(ret, ASN_PARSE_E);
- wc_FreeDecodedCert(&decoded);
- if (cert_der)
- free(cert_der);
- if (cert_buf)
- free(cert_buf);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_certs(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA)
- X509* x509ext;
- #ifdef OPENSSL_ALL
- X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- ASN1_OBJECT* obj;
- #endif
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- STACK_OF(ASN1_OBJECT)* sk;
- ASN1_STRING* asn1_str;
- AUTHORITY_KEYID* akey;
- BASIC_CONSTRAINTS* bc;
- int crit;
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
- #endif
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
- #endif
- AssertNotNull(ssl = SSL_new(ctx));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- #ifdef HAVE_PK_CALLBACKS
- AssertIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
- #endif /* HAVE_PK_CALLBACKS */
- /* create and use x509 */
- #ifdef OPENSSL_ALL
- x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- #endif
- x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509ext);
- AssertIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- /* with loading in a new cert the check on private key should now fail */
- AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- #if defined(USE_CERT_BUFFERS_2048)
- AssertIntEQ(SSL_use_certificate_ASN1(ssl,
- (unsigned char*)server_cert_der_2048,
- sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
- #endif
- #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
- /************* Get Digest of Certificate ******************/
- {
- byte digest[64]; /* max digest size */
- word32 digestSz;
- XMEMSET(digest, 0, sizeof(digest));
- AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
- WOLFSSL_FAILURE);
- }
- #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
- /* test and checkout X509 extensions */
- bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
- &crit, NULL);
- AssertNotNull(bc);
- AssertIntEQ(crit, 0);
- #ifdef OPENSSL_ALL
- ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
- AssertNotNull(ext);
- X509_EXTENSION_free(ext);
- AssertNotNull(ext = X509_EXTENSION_new());
- X509_EXTENSION_set_critical(ext, 1);
- AssertNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
- AssertIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
- ASN1_OBJECT_free(obj);
- X509_EXTENSION_free(ext);
- AssertNotNull(ext = X509_EXTENSION_new());
- X509_EXTENSION_set_critical(ext, 0);
- AssertIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
- asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
- NULL);
- AssertIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
- ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
- * and X509_get_ext_d2i has created new */
- X509_EXTENSION_free(ext);
- #endif
- BASIC_CONSTRAINTS_free(bc);
- asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit, NULL);
- AssertNotNull(asn1_str);
- AssertIntEQ(crit, 1);
- AssertIntEQ(asn1_str->type, NID_key_usage);
- #ifdef OPENSSL_ALL
- ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str);
- AssertNotNull(ext);
- X509_EXTENSION_free(ext);
- #endif
- ASN1_STRING_free(asn1_str);
- #ifdef OPENSSL_ALL
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509, NID_ext_key_usage,
- &crit, NULL);
- AssertNotNull(sk);
- ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk);
- AssertNotNull(ext);
- X509_EXTENSION_free(ext);
- sk_ASN1_OBJECT_pop_free(sk, NULL);
- #else
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
- &crit, NULL);
- AssertNull(sk);
- #endif
- akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
- NID_authority_key_identifier, &crit, NULL);
- AssertNotNull(akey);
- #ifdef OPENSSL_ALL
- ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit, akey);
- AssertNotNull(ext);
- X509_EXTENSION_free(ext);
- #endif
- wolfSSL_AUTHORITY_KEYID_free(akey);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
- NID_private_key_usage_period, &crit, NULL);
- /* AssertNotNull(sk); NID not yet supported */
- AssertIntEQ(crit, -1);
- sk_ASN1_OBJECT_free(sk);
- sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext, NID_subject_alt_name,
- &crit, NULL);
- {
- int i;
- for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
- GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
- AssertIntEQ(gen->type, GEN_DNS);
- AssertIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
- }
- }
- /* AssertNotNull(sk); no alt names set */
- sk_GENERAL_NAME_free(sk);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_issuer_alt_name,
- &crit, NULL);
- /* AssertNotNull(sk); NID not yet supported */
- AssertIntEQ(crit, -1);
- sk_ASN1_OBJECT_free(sk);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_info_access, &crit,
- NULL);
- /* AssertNotNull(sk); no auth info set */
- sk_ASN1_OBJECT_free(sk);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_sinfo_access,
- &crit, NULL);
- /* AssertNotNull(sk); NID not yet supported */
- AssertIntEQ(crit, -1);
- sk_ASN1_OBJECT_free(sk);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_name_constraints,
- &crit, NULL);
- /* AssertNotNull(sk); NID not yet supported */
- AssertIntEQ(crit, -1);
- sk_ASN1_OBJECT_free(sk);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
- NID_certificate_policies, &crit, NULL);
- #if !defined(WOLFSSL_SEP) && !defined(WOLFSSL_CERT_EXT)
- AssertNull(sk);
- #else
- /* AssertNotNull(sk); no cert policy set */
- #endif
- sk_ASN1_OBJECT_free(sk);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_mappings,
- &crit, NULL);
- /* AssertNotNull(sk); NID not yet supported */
- AssertIntEQ(crit, -1);
- sk_ASN1_OBJECT_free(sk);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_policy_constraints,
- &crit, NULL);
- /* AssertNotNull(sk); NID not yet supported */
- AssertIntEQ(crit, -1);
- sk_ASN1_OBJECT_free(sk);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_inhibit_any_policy,
- &crit, NULL);
- /* AssertNotNull(sk); NID not yet supported */
- AssertIntEQ(crit, -1);
- sk_ASN1_OBJECT_free(sk);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_tlsfeature, &crit,
- NULL);
- /* AssertNotNull(sk); NID not yet supported */
- AssertIntEQ(crit, -1);
- sk_ASN1_OBJECT_free(sk);
- /* test invalid cases */
- crit = 0;
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit, NULL);
- AssertNull(sk);
- AssertIntEQ(crit, -1);
- sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL, NID_tlsfeature,
- NULL, NULL);
- AssertNull(sk);
- AssertIntEQ(SSL_get_hit(ssl), 0);
- #ifdef OPENSSL_ALL
- X509_free(x509);
- #endif
- X509_free(x509ext);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_CERTS */
- return res;
- }
- static int test_wolfSSL_X509_check_private_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
- X509* x509;
- EVP_PKEY* pkey = NULL;
- const byte* key;
- /* Check with correct key */
- AssertNotNull((x509 = X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM)));
- key = client_key_der_2048;
- AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
- &key, (long)sizeof_client_key_der_2048));
- AssertIntEQ(X509_check_private_key(x509, pkey), 1);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- /* Check with wrong key */
- key = server_key_der_2048;
- AssertNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
- &key, (long)sizeof_server_key_der_2048));
- AssertIntEQ(X509_check_private_key(x509, pkey), 0);
- /* test for incorrect parameter */
- AssertIntEQ(X509_check_private_key(NULL, pkey), 0);
- AssertIntEQ(X509_check_private_key(x509, NULL), 0);
- AssertIntEQ(X509_check_private_key(NULL, NULL), 0);
- EVP_PKEY_free(pkey);
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_TIME_print(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) \
- && (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
- defined(WOLFSSL_HAPROXY)) && defined(USE_CERT_BUFFERS_2048) && \
- !defined(NO_BIO)
- BIO* bio;
- X509* x509;
- const unsigned char* der = client_cert_der_2048;
- ASN1_TIME* t;
- unsigned char buf[25];
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
- sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(ASN1_TIME_print(bio, X509_get_notBefore(x509)), 1);
- AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
- AssertIntEQ(XMEMCMP(buf, "Dec 16 21:17:49 2022 GMT", sizeof(buf) - 1), 0);
- /* create a bad time and test results */
- AssertNotNull(t = X509_get_notAfter(x509));
- AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_SUCCESS);
- t->data[8] = 0;
- t->data[3] = 0;
- AssertIntNE(ASN1_TIME_print(bio, t), 1);
- AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
- AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
- AssertIntEQ(ASN1_TIME_check(t), WOLFSSL_FAILURE);
- BIO_free(bio);
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_UTCTIME_print(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
- BIO* bio;
- ASN1_UTCTIME* utc = NULL;
- unsigned char buf[25];
- const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
- const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
- /* NULL parameter check */
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
- BIO_free(bio);
- /* Valid date */
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
- DYNAMIC_TYPE_ASN1));
- utc->type = ASN_UTC_TIME;
- utc->length = ASN_UTC_TIME_SIZE;
- XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
- AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
- AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
- AssertIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
- XMEMSET(buf, 0, sizeof(buf));
- BIO_free(bio);
- /* Invalid format */
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- utc->type = ASN_UTC_TIME;
- utc->length = ASN_UTC_TIME_SIZE;
- XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
- AssertIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
- AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
- AssertIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
- XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
- return res;
- }
- static int test_wolfSSL_ASN1_TIME_diff_compare(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
- ASN1_TIME* fromTime;
- ASN1_TIME* toTime;
- int daysDiff;
- int secsDiff;
- AssertNotNull((fromTime = ASN1_TIME_new()));
- /* Feb 22, 2003, 21:15:15 */
- AssertIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), WOLFSSL_SUCCESS);
- AssertNotNull((toTime = ASN1_TIME_new()));
- /* Dec 19, 2010, 18:10:11 */
- AssertIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), WOLFSSL_SUCCESS);
- AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), WOLFSSL_SUCCESS);
- /* Error conditions. */
- AssertIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime),
- WOLFSSL_FAILURE);
- AssertIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime),
- WOLFSSL_FAILURE);
- /* If both times are NULL, difference is 0. */
- AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL),
- WOLFSSL_SUCCESS);
- AssertIntEQ(daysDiff, 0);
- AssertIntEQ(secsDiff, 0);
- /* If one time is NULL, it defaults to the current time. */
- AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime),
- WOLFSSL_SUCCESS);
- AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL),
- WOLFSSL_SUCCESS);
- /* Normal operation. Both times non-NULL. */
- AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
- WOLFSSL_SUCCESS);
- AssertIntEQ(daysDiff, 2856);
- AssertIntEQ(secsDiff, 75296);
- /* Swapping the times should return negative values. */
- AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime),
- WOLFSSL_SUCCESS);
- AssertIntEQ(daysDiff, -2856);
- AssertIntEQ(secsDiff, -75296);
- AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
- AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
- AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
- /* Compare regression test: No seconds difference, just difference in days.
- */
- ASN1_TIME_set_string(fromTime, "19700101000000Z");
- ASN1_TIME_set_string(toTime, "19800101000000Z");
- AssertIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
- AssertIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
- AssertIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
- /* Edge case with Unix epoch. */
- AssertNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
- AssertNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
- AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
- WOLFSSL_SUCCESS);
- AssertIntEQ(daysDiff, 3652);
- AssertIntEQ(secsDiff, 0);
- /* Edge case with year > 2038 (year 2038 problem). */
- AssertNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
- AssertIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime),
- WOLFSSL_SUCCESS);
- AssertIntEQ(daysDiff, 2932896);
- AssertIntEQ(secsDiff, 86399);
- ASN1_TIME_free(fromTime);
- ASN1_TIME_free(toTime);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
- unsigned char nullstr[32];
- XMEMSET(nullstr, 0, 32);
- asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
- sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (asn1_gtime) {
- XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
- wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
- AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
- XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_private_keys(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- EVP_PKEY* pkey = NULL;
- OpenSSL_add_all_digests();
- OpenSSL_add_all_algorithms();
- #ifndef NO_RSA
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- /* Have to load a cert before you can check the private key against that
- * certificates public key! */
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
- #endif
- AssertNotNull(ssl = SSL_new(ctx));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- #ifdef USE_CERT_BUFFERS_2048
- {
- const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
- unsigned char buf[FOURK_BUF];
- word32 bufSz;
- AssertIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
- (unsigned char*)client_key_der_2048,
- sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- /* Should mismatch now that a different private key loaded */
- AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- AssertIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
- (unsigned char*)server_key,
- sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- /* After loading back in DER format of original key, should match */
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- /* test loading private key to the WOLFSSL_CTX */
- AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
- (unsigned char*)client_key_der_2048,
- sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- /* Should mismatch now that a different private key loaded */
- AssertIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
- #endif
- AssertIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
- (unsigned char*)server_key,
- sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- /* After loading back in DER format of original key, should match */
- AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
- #endif
- /* pkey not set yet, expecting to fail */
- AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
- /* set PKEY and test again */
- AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
- &server_key, (long)sizeof_server_key_der_2048));
- AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
- /* reuse PKEY structure and test
- * this should be checked with a memory management sanity checker */
- AssertFalse(server_key == (const unsigned char*)server_key_der_2048);
- server_key = (const unsigned char*)server_key_der_2048;
- AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
- &server_key, (long)sizeof_server_key_der_2048));
- AssertIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
- /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
- bufSz = FOURK_BUF;
- AssertIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
- (byte*)server_key_der_2048, sizeof_server_key_der_2048,
- RSAk, NULL, 0)), 0);
- server_key = (const unsigned char*)buf;
- AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
- (long)bufSz));
- }
- #endif
- EVP_PKEY_free(pkey);
- SSL_free(ssl); /* frees x509 also since loaded into ssl */
- SSL_CTX_free(ctx);
- #endif /* end of RSA private key match tests */
- #ifdef HAVE_ECC
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- SSL_free(ssl);
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
- AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- #endif /* end of ECC private key match tests */
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- SSL_free(ssl);
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- #endif /* end of Ed25519 private key match tests */
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- SSL_free(ssl);
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- #if !defined(HAVE_USER_RSA) && !defined(NO_CHECK_PRIVATE_KEY)
- AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #endif
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- #endif /* end of Ed448 private key match tests */
- EVP_cleanup();
- /* test existence of no-op macros in wolfssl/openssl/ssl.h */
- CONF_modules_free();
- ENGINE_cleanup();
- CONF_modules_unload();
- (void)ssl;
- (void)ctx;
- (void)pkey;
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- return res;
- }
- static int test_wolfSSL_PEM_read_PrivateKey(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
- && !defined(NO_FILESYSTEM)
- XFILE file;
- const char* fname = "./certs/server-key.pem";
- EVP_PKEY* pkey;
- RSA* rsa;
- WOLFSSL_EVP_PKEY_CTX* ctx;
- unsigned char* sig;
- size_t sigLen;
- const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
- size_t tbsLen = sizeof(tbs);
- /* Check error case. */
- AssertNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
- /* Read in an RSA key. */
- file = XFOPEN(fname, "rb");
- AssertTrue(file != XBADFILE);
- AssertNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
- XFCLOSE(file);
- /* Make sure the key is usable by signing some data with it. */
- AssertNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
- AssertIntGT((sigLen = RSA_size(rsa)), 0);
- AssertNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
- WOLFSSL_SUCCESS);
- XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- EVP_PKEY_CTX_free(ctx);
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PEM_read_PUBKEY(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
- && !defined(NO_FILESYSTEM)
- XFILE file;
- const char* fname = "./certs/client-keyPub.pem";
- EVP_PKEY* pkey;
- /* Check error case. */
- AssertNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
- /* Read in an RSA key. */
- file = XFOPEN(fname, "rb");
- AssertTrue(file != XBADFILE);
- AssertNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
- EVP_PKEY_free(pkey);
- XFCLOSE(file);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PEM_PrivateKey(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
- #ifndef NO_BIO
- BIO* bio = NULL;
- #endif
- EVP_PKEY* pkey = NULL;
- const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
- #ifndef NO_BIO
- /* test creating new EVP_PKEY with bad arg */
- AssertNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
- /* test loading RSA key using BIO */
- #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- {
- XFILE file;
- const char* fname = "./certs/server-key.pem";
- const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
- size_t sz;
- byte* buf;
- EVP_PKEY* pkey2;
- EVP_PKEY* pkey3;
- RSA* rsa_key = NULL;
- file = XFOPEN(fname, "rb");
- AssertTrue((file != XBADFILE));
- AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
- sz = XFTELL(file);
- XREWIND(file);
- AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
- if (buf) {
- AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
- }
- XFCLOSE(file);
- /* Test using BIO new mem and loading PEM private key */
- bio = BIO_new_mem_buf(buf, (int)sz);
- AssertNotNull(bio);
- AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
- XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
- BIO_free(bio);
- bio = NULL;
- AssertNotNull(pkey2 = EVP_PKEY_new());
- pkey2->type = EVP_PKEY_RSA;
- /* Test parameter copy */
- AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
- EVP_PKEY_free(pkey2);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- /* Qt unit test case : rsa pkcs8 key */
- file = XFOPEN(fname_rsa_p8, "rb");
- AssertTrue((file != XBADFILE));
- AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
- sz = XFTELL(file);
- XREWIND(file);
- AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
- if (buf)
- AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
- XFCLOSE(file);
- AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
- AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
- XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
- BIO_free(bio);
- bio = NULL;
- AssertNotNull(pkey3 = EVP_PKEY_new());
- AssertNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
- AssertIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ERROR_CODE_OPENSSL
- AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
- #else
- AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
- #endif
- RSA_free(rsa_key);
- EVP_PKEY_free(pkey3);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- }
- #endif
- /* test loading ECC key using BIO */
- #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
- {
- XFILE file;
- const char* fname = "./certs/ecc-key.pem";
- const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
- size_t sz;
- byte* buf;
- EVP_PKEY* pkey2;
- EVP_PKEY* pkey3;
- EC_KEY* ec_key;
- int nid = 0;
- file = XFOPEN(fname, "rb");
- AssertTrue((file != XBADFILE));
- AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
- sz = XFTELL(file);
- XREWIND(file);
- AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
- if (buf)
- AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
- XFCLOSE(file);
- /* Test using BIO new mem and loading PEM private key */
- AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
- AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
- XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
- BIO_free(bio);
- bio = NULL;
- AssertNotNull(pkey2 = EVP_PKEY_new());
- AssertNotNull(pkey3 = EVP_PKEY_new());
- pkey2->type = EVP_PKEY_EC;
- /* Test parameter copy */
- AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
- /* Qt unit test case 1*/
- AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
- AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ERROR_CODE_OPENSSL
- AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
- #else
- AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
- #endif
- /* Test default digest */
- AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
- AssertIntEQ(nid, NID_sha256);
- EC_KEY_free(ec_key);
- EVP_PKEY_free(pkey3);
- EVP_PKEY_free(pkey2);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- /* Qt unit test case ec pkcs8 key */
- file = XFOPEN(fname_ecc_p8, "rb");
- AssertTrue((file != XBADFILE));
- AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
- sz = XFTELL(file);
- XREWIND(file);
- AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
- if (buf)
- AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
- XFCLOSE(file);
- AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
- AssertNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
- XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
- BIO_free(bio);
- bio = NULL;
- AssertNotNull(pkey3 = EVP_PKEY_new());
- /* Qt unit test case */
- AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
- AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ERROR_CODE_OPENSSL
- AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
- #else
- AssertIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
- #endif
- EC_KEY_free(ec_key);
- EVP_PKEY_free(pkey3);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- }
- #endif
- #if !defined(NO_BIO) && !defined(NO_RSA) && (defined(WOLFSSL_KEY_GEN) || \
- defined(WOLFSSL_CERT_GEN))
- {
- #define BIO_PEM_TEST_CHAR 'a'
- EVP_PKEY* pkey2 = NULL;
- unsigned char extra[10];
- int i;
- BIO* pub_bio = NULL;
- XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
- AssertNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
- AssertNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey,
- &server_key, (long)sizeof_server_key_der_2048));
- AssertNull(pkey);
- AssertNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
- &server_key, (long)sizeof_server_key_der_2048));
- AssertIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
- NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
- NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
- NULL), WOLFSSL_SUCCESS);
- AssertIntGT(BIO_pending(bio), 0);
- AssertIntEQ(BIO_pending(bio), 1679);
- /* Check if the pubkey API writes only the public key */
- #ifdef WOLFSSL_KEY_GEN
- AssertIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
- AssertIntGT(BIO_pending(pub_bio), 0);
- /* Previously both the private key and the pubkey calls would write
- * out the private key and the PEM header was the only difference.
- * The public PEM should be significantly shorter than the
- * private key versison. */
- AssertIntEQ(BIO_pending(pub_bio), 451);
- #endif
- /* test creating new EVP_PKEY with good args */
- AssertNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
- if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr)
- AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz), 0);
- /* test of reuse of EVP_PKEY */
- AssertNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
- AssertIntEQ(BIO_pending(bio), 0);
- AssertIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
- SSL_SUCCESS);
- AssertIntEQ(BIO_write(bio, extra, 10), 10); /* add 10 extra bytes after PEM */
- AssertNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
- AssertNotNull(pkey);
- if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
- AssertIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr, pkey->pkey_sz),0);
- }
- AssertIntEQ(BIO_pending(bio), 10); /* check 10 extra bytes still there */
- AssertIntEQ(BIO_read(bio, extra, 10), 10);
- for (i = 0; i < 10; i++) {
- AssertIntEQ(extra[i], BIO_PEM_TEST_CHAR);
- }
- BIO_free(pub_bio);
- BIO_free(bio);
- bio = NULL;
- EVP_PKEY_free(pkey);
- pkey = NULL;
- EVP_PKEY_free(pkey2);
- }
- #endif
- /* key is DES encrypted */
- #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
- !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
- !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_USER_RSA) && !defined(NO_RSA)
- {
- XFILE f;
- wc_pem_password_cb* passwd_cb;
- void* passwd_cb_userdata;
- SSL_CTX* ctx;
- char passwd[] = "bad password";
- #ifndef WOLFSSL_NO_TLS12
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
- #endif
- #else
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
- #endif
- #endif
- AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
- SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- AssertNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
- AssertNull(passwd_cb_userdata =
- SSL_CTX_get_default_passwd_cb_userdata(ctx));
- /* fail case with password call back */
- AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
- (void*)passwd));
- BIO_free(bio);
- AssertNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
- AssertNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
- (void*)passwd));
- BIO_free(bio);
- f = XFOPEN("./certs/server-keyEnc.pem", "rb");
- AssertNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
- /* use callback that works */
- AssertNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
- (void*)"yassl123"));
- AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- BIO_free(bio);
- bio = NULL;
- SSL_CTX_free(ctx);
- }
- #endif /* !defined(NO_DES3) */
- #endif /* !NO_BIO */
- #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
- {
- unsigned char buf[2048];
- size_t bytes;
- XFILE f;
- SSL_CTX* ctx;
- #ifndef WOLFSSL_NO_TLS12
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
- #endif
- #else
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
- #endif
- #endif
- f = XFOPEN("./certs/ecc-key.der", "rb");
- AssertTrue((f != XBADFILE));
- bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- server_key = buf;
- pkey = NULL;
- AssertNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
- AssertNull(pkey);
- AssertNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
- AssertIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- SSL_CTX_free(ctx);
- }
- #endif
- res = TEST_RES_CHECK(1);
- #ifndef NO_BIO
- (void)bio;
- #endif
- (void)pkey;
- (void)server_key;
- #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
- return res;
- }
- static int test_wolfSSL_PEM_file_RSAKey(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
- defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
- !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
- RSA* rsa = NULL;
- XFILE fp;
- AssertTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
- AssertNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
- XFCLOSE(fp);
- AssertIntEQ(RSA_size(rsa), 256);
- AssertIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
- AssertIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
- (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
- return res;
- }
- static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && \
- (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
- RSA* rsa = NULL;
- XFILE f = NULL;
- f = XFOPEN(svrKeyFile, "r");
- AssertTrue((f != XBADFILE));
- AssertNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
- AssertIntEQ(RSA_size(rsa), 256);
- AssertIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
- NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
- NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
- WOLFSSL_SUCCESS);
- RSA_free(rsa);
- XFCLOSE(f);
- #ifdef HAVE_ECC
- f = XFOPEN(eccKeyFile, "r");
- AssertTrue((f != XBADFILE));
- AssertNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
- XFCLOSE(f);
- #endif /* HAVE_ECC */
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- return res;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_bio_RSAKey(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
- defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
- !defined(HAVE_USER_RSA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
- RSA* rsa = NULL;
- BIO* bio = NULL;
- /* PrivateKey */
- AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
- AssertNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
- AssertNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
- AssertNotNull(rsa);
- AssertIntEQ(RSA_size(rsa), 256);
- AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
- NULL), WOLFSSL_FAILURE);
- BIO_free(bio);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
- NULL), WOLFSSL_SUCCESS);
- BIO_free(bio);
- RSA_free(rsa);
- /* PUBKEY */
- AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
- AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
- AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
- AssertIntEQ(RSA_size(rsa), 256);
- AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
- BIO_free(bio);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
- BIO_free(bio);
- RSA_free(rsa);
- /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
- AssertNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
- AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
- BIO_free(bio);
- RSA_free(rsa);
- #ifdef HAVE_ECC
- /* ensure that non-rsa keys do not work */
- AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
- AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
- AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
- BIO_free(bio);
- RSA_free(rsa);
- #endif /* HAVE_ECC */
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
- (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
- return res;
- }
- static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- RSA* rsa = NULL;
- RSA* rsa_dup = NULL;
- BIO* bio = NULL;
- AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
- AssertNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
- AssertIntEQ(RSA_size(rsa), 256);
- #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
- AssertNull(rsa_dup = RSAPublicKey_dup(NULL));
- /* Test duplicating empty key. */
- rsa_dup = RSA_new();
- AssertNull(RSAPublicKey_dup(rsa_dup));
- RSA_free(rsa_dup);
- AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
- AssertPtrNE(rsa_dup, rsa);
- #endif
- /* test if valgrind complains about unreleased memory */
- RSA_up_ref(rsa);
- RSA_free(rsa);
- BIO_free(bio);
- RSA_free(rsa);
- RSA_free(rsa_dup);
- #ifdef HAVE_ECC
- AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
- AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
- BIO_free(bio);
- #endif /* HAVE_ECC */
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- return res;
- }
- static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- XFILE file;
- const char* fname = "./certs/client-keyPub.pem";
- RSA *rsa;
- AssertNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
- file = XFOPEN(fname, "rb");
- AssertTrue((file != XBADFILE));
- AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
- AssertIntEQ(RSA_size(rsa), 256);
- RSA_free(rsa);
- XFCLOSE(file);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- return res;
- }
- static int test_wolfSSL_PEM_bio_DSAKey(void)
- {
- int res = TEST_SKIPPED;
- #ifndef HAVE_SELFTEST
- #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
- defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
- DSA* dsa = NULL;
- BIO* bio = NULL;
- /* PrivateKey */
- AssertNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
- AssertNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
- AssertNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
- AssertIntEQ(BN_num_bytes(dsa->g), 128);
- AssertIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, NULL),
- WOLFSSL_FAILURE);
- BIO_free(bio);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL, NULL),
- WOLFSSL_SUCCESS);
- BIO_free(bio);
- DSA_free(dsa);
- /* PUBKEY */
- AssertNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
- AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
- AssertNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
- AssertIntEQ(BN_num_bytes(dsa->g), 128);
- AssertIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
- BIO_free(bio);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
- BIO_free(bio);
- DSA_free(dsa);
- #ifdef HAVE_ECC
- /* ensure that non-dsa keys do not work */
- AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
- AssertNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
- AssertNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
- BIO_free(bio);
- DSA_free(dsa);
- #endif /* HAVE_ECC */
- res = TEST_RES_CHECK(1);
- #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
- !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
- !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
- #endif /* HAVE_SELFTEST */
- return res;
- }
- static int test_wolfSSL_PEM_bio_ECKey(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
- defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
- EC_KEY* ec = NULL;
- BIO* bio = NULL;
- /* PrivateKey */
- AssertNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
- AssertNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
- AssertNotNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
- AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
- AssertIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
- NULL),WOLFSSL_FAILURE);
- BIO_free(bio);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
- NULL), WOLFSSL_SUCCESS);
- BIO_free(bio);
- EC_KEY_free(ec);
- /* PUBKEY */
- AssertNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
- AssertNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
- AssertNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
- AssertIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
- AssertIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
- BIO_free(bio);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
- BIO_free(bio);
- /* Same test as above, but with a file pointer rather than a BIO. */
- AssertIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
- EC_KEY_free(ec);
- #ifndef NO_RSA
- /* ensure that non-ec keys do not work */
- AssertNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
- AssertNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
- AssertNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
- BIO_free(bio);
- EC_KEY_free(ec);
- #endif /* HAVE_ECC */
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- return res;
- }
- static int test_wolfSSL_PEM_PUBKEY(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
- BIO* bio = NULL;
- EVP_PKEY* pkey = NULL;
- /* test creating new EVP_PKEY with bad arg */
- AssertNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
- /* test loading ECC key using BIO */
- #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
- {
- XFILE file;
- const char* fname = "./certs/ecc-client-keyPub.pem";
- size_t sz;
- byte* buf;
- EVP_PKEY* pkey2;
- EC_KEY* ec_key;
- file = XFOPEN(fname, "rb");
- AssertTrue((file != XBADFILE));
- AssertIntGE(XFSEEK(file, 0, XSEEK_END), 0);
- sz = XFTELL(file);
- XREWIND(file);
- AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
- if (buf)
- AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
- XFCLOSE(file);
- /* Test using BIO new mem and loading PEM private key */
- AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
- AssertNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
- XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
- BIO_free(bio);
- bio = NULL;
- /* Qt unit test case*/
- AssertNotNull(pkey2 = EVP_PKEY_new());
- AssertNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
- AssertIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ERROR_CODE_OPENSSL
- AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
- #else
- AssertIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
- #endif
- EC_KEY_free(ec_key);
- EVP_PKEY_free(pkey2);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- }
- #endif
- (void)bio;
- (void)pkey;
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif /* !NO_BIO */
- static int test_DSA_do_sign_verify(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
- !defined(NO_DSA)
- unsigned char digest[WC_SHA_DIGEST_SIZE];
- DSA_SIG* sig;
- DSA* dsa;
- word32 bytes;
- byte sigBin[DSA_SIG_SIZE];
- int dsacheck;
- #ifdef USE_CERT_BUFFERS_1024
- byte tmp[ONEK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
- bytes = sizeof_dsa_key_der_1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- byte tmp[TWOK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
- bytes = sizeof_dsa_key_der_2048;
- #else
- byte tmp[TWOK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
- if (fp == XBADFILE) {
- return WOLFSSL_BAD_FILE;
- }
- bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
- XFCLOSE(fp);
- #endif /* END USE_CERT_BUFFERS_1024 */
- XMEMSET(digest, 202, sizeof(digest));
- AssertNotNull(dsa = DSA_new());
- AssertIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
- AssertIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
- AssertIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
- AssertNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
- AssertIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
- DSA_SIG_free(sig);
- DSA_free(dsa);
- res = TEST_RES_CHECK(1);
- #endif
- #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
- return res;
- }
- static int test_wolfSSL_tmp_dh(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
- !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- byte buff[6000];
- char file[] = "./certs/dsaparams.pem";
- XFILE f;
- int bytes;
- DSA* dsa;
- DH* dh;
- #if defined(WOLFSSL_DH_EXTRA) && \
- (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
- DH* dh2;
- #endif
- BIO* bio;
- SSL* ssl;
- SSL_CTX* ctx;
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- f = XFOPEN(file, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- bio = BIO_new_mem_buf((void*)buff, bytes);
- AssertNotNull(bio);
- dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
- AssertNotNull(dsa);
- dh = wolfSSL_DSA_dup_DH(dsa);
- AssertNotNull(dh);
- #if defined(WOLFSSL_DH_EXTRA) && \
- (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
- AssertNotNull(dh2 = wolfSSL_DH_dup(dh));
- #endif
- AssertIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
- #endif
- BIO_free(bio);
- DSA_free(dsa);
- DH_free(dh);
- #if defined(WOLFSSL_DH_EXTRA) && \
- (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
- DH_free(dh2);
- #endif
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return res;
- }
- static int test_wolfSSL_ctrl(void)
- {
- int res = TEST_SKIPPED;
- #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
- byte buff[6000];
- BIO* bio;
- int bytes;
- BUF_MEM* ptr = NULL;
- XMEMSET(buff, 0, sizeof(buff));
- bytes = sizeof(buff);
- bio = BIO_new_mem_buf((void*)buff, bytes);
- AssertNotNull(bio);
- AssertNotNull(BIO_s_socket());
- AssertIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
- /* needs tested after stubs filled out @TODO
- SSL_ctrl
- SSL_CTX_ctrl
- */
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_EXTRA
- static const unsigned char pw[] = "password";
- static const int pwSz = sizeof(pw) - 1;
- size_t checkPwSz = 0;
- const unsigned char* checkPw = NULL;
- WOLFSSL_EVP_PKEY* key = NULL;
- AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
- AssertNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
- AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, pwSz));
- if (key) {
- AssertIntEQ(key->type, EVP_PKEY_HMAC);
- AssertIntEQ(key->save_type, EVP_PKEY_HMAC);
- AssertIntEQ(key->pkey_sz, pwSz);
- AssertIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
- }
- AssertNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
- AssertIntEQ((int)checkPwSz, pwSz);
- if (checkPw) {
- AssertIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
- }
- wolfSSL_EVP_PKEY_free(key);
- AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw, 0));
- if (key) {
- AssertIntEQ(key->pkey_sz, 0);
- }
- checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
- (void)checkPw;
- AssertIntEQ((int)checkPwSz, 0);
- wolfSSL_EVP_PKEY_free(key);
- AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL, 0));
- if (key) {
- AssertIntEQ(key->pkey_sz, 0);
- }
- checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
- (void)checkPw;
- AssertIntEQ((int)checkPwSz, 0);
- wolfSSL_EVP_PKEY_free(key);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_EXTRA
- #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
- const char *priv = "ABCDEFGHIJKLMNOP";
- const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
- WOLFSSL_EVP_PKEY* key = NULL;
- AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
- NULL, NULL, AES_128_KEY_SIZE, cipher));
- AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
- NULL, (const unsigned char *)priv, 0, cipher));
- AssertNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
- NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
- AssertNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
- NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
- wolfSSL_EVP_PKEY_free(key);
- res = TEST_RES_CHECK(1);
- #endif /* defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT) */
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_EVP_Digest(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
- const char* in = "abc";
- int inLen = (int)XSTRLEN(in);
- byte out[WC_SHA256_DIGEST_SIZE];
- unsigned int outLen;
- const char* expOut = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
- "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
- "\x15\xAD";
- AssertIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen, "SHA256", NULL), 1);
- AssertIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
- AssertIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
- res = TEST_RES_CHECK(1);
- #endif /* OPEN_EXTRA && ! NO_SHA256 */
- return res;
- }
- static int test_wolfSSL_EVP_Digest_all(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_EXTRA
- const char* digests[] = {
- #ifndef NO_MD5
- "MD5",
- #endif
- #ifndef NO_SHA
- "SHA",
- #endif
- #ifdef WOLFSSL_SHA224
- "SHA224",
- #endif
- #ifndef NO_SHA256
- "SHA256",
- #endif
- #ifdef WOLFSSL_SHA384
- "SHA384",
- #endif
- #ifdef WOLFSSL_SHA512
- "SHA512",
- #endif
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- "SHA512_224",
- #endif
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- "SHA512_256",
- #endif
- #ifdef WOLFSSL_SHA3
- #ifndef WOLFSSL_NOSHA3_224
- "SHA3_224",
- #endif
- #ifndef WOLFSSL_NOSHA3_256
- "SHA3_256",
- #endif
- "SHA3_384",
- #ifndef WOLFSSL_NOSHA3_512
- "SHA3_512",
- #endif
- #endif /* WOLFSSL_SHA3 */
- NULL
- };
- const char** d;
- const unsigned char in[] = "abc";
- int inLen = XSTR_SIZEOF(in);
- byte out[WC_MAX_DIGEST_SIZE];
- unsigned int outLen;
- for (d = digests; *d != NULL; d++) {
- AssertIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
- AssertIntGT(outLen, 0);
- AssertIntEQ(EVP_MD_size(*d), outLen);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_MD_size(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_EXTRA
- WOLFSSL_EVP_MD_CTX mdCtx;
- #ifdef WOLFSSL_SHA3
- #ifndef WOLFSSL_NOSHA3_224
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #endif
- #ifndef WOLFSSL_NOSHA3_256
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #endif
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #ifndef WOLFSSL_NOSHA3_512
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #endif
- #endif /* WOLFSSL_SHA3 */
- #ifndef NO_SHA256
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA256_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #endif
- #ifndef NO_MD5
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_MD5_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #endif
- #ifdef WOLFSSL_SHA224
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA224_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #endif
- #ifdef WOLFSSL_SHA384
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA384_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #endif
- #ifdef WOLFSSL_SHA512
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA512_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #endif
- #ifndef NO_SHA
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
- AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), WC_SHA_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #endif
- /* error case */
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
- /* Cleanup is valid on uninit'ed struct */
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_EVP_MD_pkey_type(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_EXTRA
- const WOLFSSL_EVP_MD* md;
- #ifndef NO_MD5
- AssertNotNull(md = EVP_md5());
- AssertIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
- #endif
- #ifndef NO_SHA
- AssertNotNull(md = EVP_sha1());
- AssertIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
- #endif
- #ifdef WOLFSSL_SHA224
- AssertNotNull(md = EVP_sha224());
- AssertIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
- #endif
- AssertNotNull(md = EVP_sha256());
- AssertIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
- #ifdef WOLFSSL_SHA384
- AssertNotNull(md = EVP_sha384());
- AssertIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
- #endif
- #ifdef WOLFSSL_SHA512
- AssertNotNull(md = EVP_sha512());
- AssertIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #ifdef OPENSSL_EXTRA
- static void test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
- size_t testKeySz, const char* testData, size_t testDataSz,
- const byte* testResult, size_t testResultSz)
- {
- unsigned char check[WC_MAX_DIGEST_SIZE];
- size_t checkSz = -1;
- WOLFSSL_EVP_PKEY* key;
- WOLFSSL_EVP_MD_CTX mdCtx;
- AssertNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
- testKey, (int)testKeySz));
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
- (unsigned int)testDataSz), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
- AssertIntEQ((int)checkSz, (int)testResultSz);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ((int)checkSz,(int)testResultSz);
- AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
- (unsigned int)testDataSz), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
- AssertIntEQ((int)checkSz, (int)testResultSz);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ((int)checkSz,(int)testResultSz);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
- (unsigned int)testDataSz - 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ((int)checkSz,(int)testResultSz);
- AssertIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
- (unsigned int)testDataSz - 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_PKEY_free(key);
- }
- #endif
- static int test_wolfSSL_EVP_MD_hmac_signing(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_EXTRA
- static const unsigned char testKey[] =
- {
- 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
- 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
- 0x0b, 0x0b, 0x0b, 0x0b
- };
- static const char testData[] = "Hi There";
- #ifdef WOLFSSL_SHA224
- static const unsigned char testResultSha224[] =
- {
- 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
- 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
- 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
- 0x53, 0x68, 0x4b, 0x22
- };
- #endif
- #ifndef NO_SHA256
- static const unsigned char testResultSha256[] =
- {
- 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
- 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
- 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
- 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
- };
- #endif
- #ifdef WOLFSSL_SHA384
- static const unsigned char testResultSha384[] =
- {
- 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
- 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
- 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
- 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
- 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
- 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
- };
- #endif
- #ifdef WOLFSSL_SHA512
- static const unsigned char testResultSha512[] =
- {
- 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
- 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
- 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
- 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
- 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
- 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
- 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
- 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
- };
- #endif
- #ifdef WOLFSSL_SHA3
- #ifndef WOLFSSL_NOSHA3_224
- static const unsigned char testResultSha3_224[] =
- {
- 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
- 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
- 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
- 0xf3, 0xc8, 0x60, 0xf7
- };
- #endif
- #ifndef WOLFSSL_NOSHA3_256
- static const unsigned char testResultSha3_256[] =
- {
- 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
- 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
- 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
- 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
- };
- #endif
- #ifndef WOLFSSL_NOSHA3_384
- static const unsigned char testResultSha3_384[] =
- {
- 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
- 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
- 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
- 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
- 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
- 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
- };
- #endif
- #ifndef WOLFSSL_NOSHA3_512
- static const unsigned char testResultSha3_512[] =
- {
- 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
- 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
- 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
- 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
- 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
- 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
- 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
- 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
- };
- #endif
- #endif
- #ifndef NO_SHA256
- test_hmac_signing(wolfSSL_EVP_sha256(), testKey, sizeof(testKey), testData,
- XSTRLEN(testData), testResultSha256, sizeof(testResultSha256));
- #endif
- #ifdef WOLFSSL_SHA224
- test_hmac_signing(wolfSSL_EVP_sha224(), testKey, sizeof(testKey), testData,
- XSTRLEN(testData), testResultSha224, sizeof(testResultSha224));
- #endif
- #ifdef WOLFSSL_SHA384
- test_hmac_signing(wolfSSL_EVP_sha384(), testKey, sizeof(testKey), testData,
- XSTRLEN(testData), testResultSha384, sizeof(testResultSha384));
- #endif
- #ifdef WOLFSSL_SHA512
- test_hmac_signing(wolfSSL_EVP_sha512(), testKey, sizeof(testKey), testData,
- XSTRLEN(testData), testResultSha512, sizeof(testResultSha512));
- #endif
- #ifdef WOLFSSL_SHA3
- #ifndef WOLFSSL_NOSHA3_224
- test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey, sizeof(testKey),
- testData, XSTRLEN(testData), testResultSha3_224,
- sizeof(testResultSha3_224));
- #endif
- #ifndef WOLFSSL_NOSHA3_256
- test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey, sizeof(testKey),
- testData, XSTRLEN(testData), testResultSha3_256,
- sizeof(testResultSha3_256));
- #endif
- #ifndef WOLFSSL_NOSHA3_384
- test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey, sizeof(testKey),
- testData, XSTRLEN(testData), testResultSha3_384,
- sizeof(testResultSha3_384));
- #endif
- #ifndef WOLFSSL_NOSHA3_512
- test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey, sizeof(testKey),
- testData, XSTRLEN(testData), testResultSha3_512,
- sizeof(testResultSha3_512));
- #endif
- #endif
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_EVP_MD_rsa_signing(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
- defined(USE_CERT_BUFFERS_2048)
- WOLFSSL_EVP_PKEY* privKey;
- WOLFSSL_EVP_PKEY* pubKey;
- WOLFSSL_EVP_PKEY_CTX* keyCtx;
- const char testData[] = "Hi There";
- WOLFSSL_EVP_MD_CTX mdCtx;
- WOLFSSL_EVP_MD_CTX mdCtxCopy;
- size_t checkSz = -1;
- int sz = 2048 / 8;
- const unsigned char* cp;
- const unsigned char* p;
- unsigned char check[2048/8];
- size_t i;
- int paddings[] = {
- RSA_PKCS1_PADDING,
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
- RSA_PKCS1_PSS_PADDING,
- #endif
- };
- cp = client_key_der_2048;
- AssertNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
- sizeof_client_key_der_2048)));
- p = client_keypub_der_2048;
- AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
- sizeof_client_keypub_der_2048)));
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
- AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
- NULL, privKey), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
- (unsigned int)XSTRLEN(testData)), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
- AssertIntEQ((int)checkSz, sz);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ((int)checkSz,sz);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
- NULL, pubKey), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
- (unsigned int)XSTRLEN(testData)),
- 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
- NULL, privKey), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
- AssertIntEQ((int)checkSz, sz);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ((int)checkSz, sz);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
- (unsigned int)XSTRLEN(testData) - 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ((int)checkSz, sz);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
- NULL, pubKey), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
- (unsigned int)XSTRLEN(testData) - 4),
- 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- /* Check all signing padding types */
- for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
- wolfSSL_EVP_sha256(), NULL, privKey), 1);
- AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
- paddings[i]), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
- (unsigned int)XSTRLEN(testData)), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
- AssertIntEQ((int)checkSz, sz);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ((int)checkSz,sz);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
- wolfSSL_EVP_sha256(), NULL, pubKey), 1);
- AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
- paddings[i]), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
- (unsigned int)XSTRLEN(testData)), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- }
- wolfSSL_EVP_PKEY_free(pubKey);
- wolfSSL_EVP_PKEY_free(privKey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_MD_ecc_signing(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
- WOLFSSL_EVP_PKEY* privKey;
- WOLFSSL_EVP_PKEY* pubKey;
- const char testData[] = "Hi There";
- WOLFSSL_EVP_MD_CTX mdCtx;
- size_t checkSz = -1;
- const unsigned char* cp;
- const unsigned char* p;
- unsigned char check[2048/8];
- cp = ecc_clikey_der_256;
- privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
- sizeof_ecc_clikey_der_256);
- AssertNotNull(privKey);
- p = ecc_clikeypub_der_256;
- AssertNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
- sizeof_ecc_clikeypub_der_256)));
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
- NULL, privKey), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
- (unsigned int)XSTRLEN(testData)), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
- NULL, pubKey), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
- (unsigned int)XSTRLEN(testData)),
- 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
- NULL, privKey), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
- (unsigned int)XSTRLEN(testData) - 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
- NULL, pubKey), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
- (unsigned int)XSTRLEN(testData) - 4),
- 1);
- AssertIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- wolfSSL_EVP_PKEY_free(pubKey);
- wolfSSL_EVP_PKEY_free(privKey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_add_extra_chain_cert(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- char caFile[] = "./certs/client-ca.pem";
- char clientFile[] = "./certs/client-cert.pem";
- SSL_CTX* ctx;
- X509* x509;
- BIO *bio = NULL;
- X509 *cert = NULL;
- X509 *ca;
- STACK_OF(X509) *chain = NULL;
- STACK_OF(X509) *chain2 = NULL;
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
- x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
- /* additional test of getting EVP_PKEY key size from X509
- * Do not run with user RSA because wolfSSL_RSA_size is not currently
- * allowed with user RSA */
- {
- EVP_PKEY* pkey;
- #if defined(HAVE_ECC)
- X509* ecX509;
- #endif /* HAVE_ECC */
- AssertNotNull(pkey = X509_get_pubkey(x509));
- /* current RSA key is 2048 bit (256 bytes) */
- AssertIntEQ(EVP_PKEY_size(pkey), 256);
- EVP_PKEY_free(pkey);
- #if defined(HAVE_ECC)
- #if defined(USE_CERT_BUFFERS_256)
- AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
- cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
- SSL_FILETYPE_ASN1));
- #else
- AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(cliEccCertFile,
- SSL_FILETYPE_PEM));
- #endif
- pkey = X509_get_pubkey(ecX509);
- AssertNotNull(pkey);
- /* current ECC key is 256 bit (32 bytes) */
- AssertIntEQ(EVP_PKEY_size(pkey), 32);
- X509_free(ecX509);
- EVP_PKEY_free(pkey);
- #endif /* HAVE_ECC */
- }
- #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
- AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
- AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
- #endif
- SSL_CTX_free(ctx);
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- /* Test haproxy use case */
- AssertNotNull(bio = BIO_new_file(svrCertFile, "r"));
- /* Read Certificate */
- AssertNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
- AssertNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
- AssertNotNull(chain = sk_X509_new_null());
- AssertIntEQ(sk_X509_push(chain, ca), 1);
- AssertNotNull(chain2 = X509_chain_up_ref(chain));
- AssertNotNull(ca = sk_X509_shift(chain2));
- AssertIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
- AssertIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
- BIO_free(bio);
- X509_free(cert);
- sk_X509_pop_free(chain, X509_free);
- sk_X509_pop_free(chain2, X509_free);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
- return res;
- }
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- static int test_wolfSSL_ERR_peek_last_error_line(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
- !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- #ifndef SINGLE_THREADED
- THREAD_TYPE serverThread;
- #endif
- callback_functions client_cb;
- callback_functions server_cb;
- int line = 0;
- int flag = ERR_TXT_STRING;
- const char* file = NULL;
- const char* data = NULL;
- /* create a failed connection and inspect the error */
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- XMEMSET(&client_cb, 0, sizeof(callback_functions));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- client_cb.method = wolfTLSv1_1_client_method;
- server_cb.method = wolfTLSv1_2_server_method;
- server_args.signal = &ready;
- server_args.callbacks = &server_cb;
- client_args.signal = &ready;
- client_args.callbacks = &client_cb;
- #ifndef SINGLE_THREADED
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- #endif
- FreeTcpReady(&ready);
- AssertIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
- AssertNotNull(data);
- /* check clearing error state */
- ERR_remove_state(0);
- AssertIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
- ERR_peek_last_error_line(NULL, &line);
- AssertIntEQ(line, 0);
- ERR_peek_last_error_line(&file, NULL);
- AssertNull(file);
- /* retry connection to fill error queue */
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- client_cb.method = wolfTLSv1_1_client_method;
- server_cb.method = wolfTLSv1_2_server_method;
- server_args.signal = &ready;
- server_args.callbacks = &server_cb;
- client_args.signal = &ready;
- client_args.callbacks = &client_cb;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- /* check that error code was stored */
- AssertIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
- ERR_peek_last_error_line(NULL, &line);
- AssertIntNE(line, 0);
- ERR_peek_last_error_line(&file, NULL);
- AssertNotNull(file);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- fprintf(stderr, "\nTesting error print out\n");
- ERR_print_errors_fp(stderr);
- fprintf(stderr, "Done testing print out\n\n");
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
- return res;
- }
- #endif
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- static int verify_cb(int ok, X509_STORE_CTX *ctx)
- {
- (void) ok;
- (void) ctx;
- fprintf(stderr, "ENTER verify_cb\n");
- return SSL_SUCCESS;
- }
- #endif
- static int test_wolfSSL_X509_Name_canon(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
- defined(WOLFSSL_CERT_GEN) && \
- (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
- const long ex_hash1 = 0x0fdb2da4;
- const long ex_hash2 = 0x9f3e8c9e;
- X509_NAME *name = NULL;
- X509 *x509 = NULL;
- FILE* file = NULL;
- unsigned long hash = 0;
- byte digest[WC_MAX_DIGEST_SIZE] = {0};
- byte *pbuf = NULL;
- word32 len = 0;
- (void) ex_hash2;
- file = XFOPEN(caCertFile, "rb");
- AssertNotNull(file);
- AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
- AssertNotNull(name = X509_get_issuer_name(x509));
- /* When output buffer is NULL, should return necessary output buffer
- * length.*/
- AssertIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
- AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
- AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
- hash = (((unsigned long)digest[3] << 24) |
- ((unsigned long)digest[2] << 16) |
- ((unsigned long)digest[1] << 8) |
- ((unsigned long)digest[0]));
- AssertIntEQ(hash, ex_hash1);
- XFCLOSE(file);
- X509_free(x509);
- XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
- pbuf = NULL;
- file = XFOPEN(cliCertFile, "rb");
- AssertNotNull(file);
- AssertNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
- AssertNotNull(name = X509_get_issuer_name(x509));
- AssertIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
- AssertIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
- hash = (((unsigned long)digest[3] << 24) |
- ((unsigned long)digest[2] << 16) |
- ((unsigned long)digest[1] << 8) |
- ((unsigned long)digest[0]));
- AssertIntEQ(hash, ex_hash2);
- XFCLOSE(file);
- X509_free(x509);
- XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
- const int MAX_DIR = 4;
- const char paths[][32] = {
- "./certs/ed25519",
- "./certs/ecc",
- "./certs/crl",
- "./certs/",
- };
- char CertCrl_path[MAX_FILENAME_SZ];
- char *p;
- X509_STORE* str;
- X509_LOOKUP* lookup;
- WOLFSSL_STACK* sk = NULL;
- int len, total_len, i;
- (void) sk;
- XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
- /* illegal string */
- AssertNotNull((str = wolfSSL_X509_STORE_new()));
- AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
- AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
- SSL_FILETYPE_PEM,NULL), 0);
- /* free store */
- X509_STORE_free(str);
- /* short folder string */
- AssertNotNull((str = wolfSSL_X509_STORE_new()));
- AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
- AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
- SSL_FILETYPE_PEM,NULL), 1);
- #if defined(WOLFSSL_INT_H)
- /* only available when including internal.h */
- AssertNotNull(sk = lookup->dirs->dir_entry);
- #endif
- /* free store */
- X509_STORE_free(str);
- /* typical function check */
- p = &CertCrl_path[0];
- total_len = 0;
- for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
- len = (int)XSTRLEN((const char*)&paths[i]);
- total_len += len;
- XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
- p += len;
- if (i != 0) *(p++) = SEPARATOR_CHAR;
- }
- AssertNotNull((str = wolfSSL_X509_STORE_new()));
- AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
- AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
- SSL_FILETYPE_PEM,NULL), 1);
- #if defined(WOLFSSL_INT_H)
- /* only available when including internal.h */
- AssertNotNull(sk = lookup->dirs->dir_entry);
- #endif
- X509_STORE_free(str);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
- defined(WOLFSSL_SIGNER_DER_CERT)
- X509_STORE_CTX* ctx;
- X509_STORE* str;
- X509_LOOKUP* lookup;
- X509* cert1;
- X509* x509Ca;
- X509* x509Svr;
- X509* issuer;
- WOLFSSL_STACK* sk = NULL;
- X509_NAME* caName;
- X509_NAME* issuerName;
- FILE* file1 = NULL;
- int i, cert_count, cmp;
- char der[] = "certs/ca-cert.der";
- #ifdef HAVE_CRL
- char pem[][100] = {
- "./certs/crl/crl.pem",
- "./certs/crl/crl2.pem",
- "./certs/crl/caEccCrl.pem",
- "./certs/crl/eccCliCRL.pem",
- "./certs/crl/eccSrvCRL.pem",
- ""
- };
- #endif
- AssertNotNull(file1=fopen("./certs/ca-cert.pem", "rb"));
- AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
- fclose(file1);
- AssertNotNull(ctx = X509_STORE_CTX_new());
- AssertNotNull((str = wolfSSL_X509_STORE_new()));
- AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
- AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
- SSL_FILETYPE_PEM,NULL), 1);
- AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
- AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
- /* check if CA cert is loaded into the store */
- for (i = 0; i < cert_count; i++) {
- x509Ca = sk_X509_value(sk, i);
- AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
- }
- AssertNotNull((x509Svr =
- wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
- AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
- issuer = X509_STORE_CTX_get0_current_issuer(ctx);
- AssertNotNull(issuer);
- caName = X509_get_subject_name(x509Ca);
- AssertNotNull(caName);
- issuerName = X509_get_subject_name(issuer);
- AssertNotNull(issuerName);
- cmp = X509_NAME_cmp(caName, issuerName);
- AssertIntEQ(cmp, 0);
- /* load der format */
- X509_free(issuer);
- X509_STORE_CTX_free(ctx);
- X509_STORE_free(str);
- sk_X509_pop_free(sk, NULL);
- X509_free(x509Svr);
- AssertNotNull((str = wolfSSL_X509_STORE_new()));
- AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
- AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
- SSL_FILETYPE_ASN1,NULL), 1);
- AssertNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
- AssertIntEQ((cert_count = sk_X509_num(sk)), 1);
- /* check if CA cert is loaded into the store */
- for (i = 0; i < cert_count; i++) {
- x509Ca = sk_X509_value(sk, i);
- AssertIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
- }
- X509_STORE_free(str);
- sk_X509_pop_free(sk, NULL);
- X509_free(cert1);
- #ifdef HAVE_CRL
- AssertNotNull(str = wolfSSL_X509_STORE_new());
- AssertNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
- AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
- SSL_FILETYPE_PEM,NULL), 1);
- AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
- "certs/server-revoked-cert.pem",
- SSL_FILETYPE_PEM,NULL), 1);
- if (str) {
- AssertIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
- WOLFSSL_FILETYPE_PEM), 1);
- /* since store hasn't yet known the revoked cert*/
- AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
- "certs/server-revoked-cert.pem",
- WOLFSSL_FILETYPE_PEM), 1);
- }
- for (i = 0; pem[i][0] != '\0'; i++)
- {
- AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
- SSL_FILETYPE_PEM, NULL), 1);
- }
- if (str) {
- /* since store knows crl list */
- AssertIntEQ(wolfSSL_CertManagerVerify(str->cm,
- "certs/server-revoked-cert.pem",
- WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
- }
- AssertIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
- X509_STORE_free(str);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- X509_STORE_CTX_cleanup(NULL);
- X509_STORE_CTX_trusted_stack(NULL, NULL);
- AssertTrue(1); /* to confirm previous call gives no harm */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- #ifdef WOLFSSL_SIGNER_DER_CERT
- int cmp;
- #endif
- X509_STORE_CTX* ctx;
- X509_STORE* str;
- X509* x509Ca;
- X509* x509Svr;
- X509* issuer;
- X509_NAME* caName;
- X509_NAME* issuerName;
- AssertNotNull(ctx = X509_STORE_CTX_new());
- AssertNotNull((str = wolfSSL_X509_STORE_new()));
- AssertNotNull((x509Ca =
- wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
- AssertNotNull((x509Svr =
- wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
- AssertNull(X509_STORE_CTX_get0_current_issuer(NULL));
- issuer = X509_STORE_CTX_get0_current_issuer(ctx);
- AssertNotNull(issuer);
- caName = X509_get_subject_name(x509Ca);
- AssertNotNull(caName);
- issuerName = X509_get_subject_name(issuer);
- AssertNotNull(issuerName);
- #ifdef WOLFSSL_SIGNER_DER_CERT
- cmp = X509_NAME_cmp(caName, issuerName);
- AssertIntEQ(cmp, 0);
- #endif
- X509_free(issuer);
- X509_STORE_CTX_free(ctx);
- X509_free(x509Svr);
- X509_STORE_free(str);
- X509_free(x509Ca);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PKCS7_certs(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
- STACK_OF(X509)* sk = NULL;
- STACK_OF(X509_INFO)* info_sk = NULL;
- PKCS7 *p7 = NULL;
- BIO* bio;
- const byte* p = NULL;
- int buflen = 0;
- int i;
- /* Test twice. Once with d2i and once without to test
- * that everything is free'd correctly. */
- for (i = 0; i < 2; i++) {
- AssertNotNull(p7 = PKCS7_new());
- p7->version = 1;
- #ifdef NO_SHA
- p7->hashOID = SHA256h;
- #else
- p7->hashOID = SHAh;
- #endif
- AssertNotNull(bio = BIO_new(BIO_s_file()));
- AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
- AssertNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
- AssertIntEQ(sk_X509_INFO_num(info_sk), 2);
- AssertNotNull(sk = sk_X509_new_null());
- while (sk_X509_INFO_num(info_sk)) {
- X509_INFO* info;
- AssertNotNull(info = sk_X509_INFO_shift(info_sk));
- AssertIntEQ(sk_X509_push(sk, info->x509), 1);
- info->x509 = NULL;
- X509_INFO_free(info);
- }
- sk_X509_INFO_free(info_sk);
- BIO_free(bio);
- bio = BIO_new(BIO_s_mem());
- AssertIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
- AssertIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
- if (i == 0) {
- PKCS7_free(p7);
- AssertNotNull(d2i_PKCS7(&p7, &p, buflen));
- /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate them */
- ((WOLFSSL_PKCS7*)p7)->certs = NULL;
- /* PKCS7_free free's the certs */
- AssertNotNull(wolfSSL_PKCS7_to_stack(p7));
- }
- BIO_free(bio);
- PKCS7_free(p7);
- }
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
- return res;
- }
- static int test_wolfSSL_X509_STORE_CTX(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- X509_STORE_CTX* ctx;
- X509_STORE* str;
- X509* x509;
- #ifdef OPENSSL_ALL
- X509* x5092;
- STACK_OF(X509) *sk, *sk2, *sk3;
- #endif
- AssertNotNull(ctx = X509_STORE_CTX_new());
- AssertNotNull((str = wolfSSL_X509_STORE_new()));
- AssertNotNull((x509 =
- wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
- #ifdef OPENSSL_ALL
- /* sk_X509_new only in OPENSSL_ALL */
- sk = sk_X509_new_null();
- AssertNotNull(sk);
- AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
- #else
- AssertIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
- #endif
- AssertIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
- X509_STORE_CTX_set_error(ctx, -5);
- X509_STORE_CTX_set_error(NULL, -5);
- X509_STORE_CTX_free(ctx);
- #ifdef OPENSSL_ALL
- sk_X509_pop_free(sk, NULL);
- #endif
- X509_STORE_free(str);
- X509_free(x509);
- AssertNotNull(ctx = X509_STORE_CTX_new());
- X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
- X509_STORE_CTX_free(ctx);
- #ifdef OPENSSL_ALL
- /* test X509_STORE_CTX_get(1)_chain */
- AssertNotNull((x509 = X509_load_certificate_file(svrCertFile,
- SSL_FILETYPE_PEM)));
- AssertNotNull((x5092 = X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM)));
- AssertNotNull((sk = sk_X509_new_null()));
- AssertIntEQ(sk_X509_push(sk, x509), 1);
- AssertNotNull((str = X509_STORE_new()));
- AssertNotNull((ctx = X509_STORE_CTX_new()));
- AssertIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
- AssertNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
- AssertNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
- AssertIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
- AssertNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
- AssertNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
- AssertIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
- X509_STORE_CTX_free(ctx);
- X509_STORE_free(str);
- /* CTX certs not freed yet */
- X509_free(x5092);
- sk_X509_pop_free(sk, NULL);
- /* sk3 is dup so free here */
- sk_X509_pop_free(sk3, NULL);
- #endif
- /* test X509_STORE_CTX_get/set_ex_data */
- {
- int i = 0, tmpData = 5;
- void* tmpDataRet;
- AssertNotNull(ctx = X509_STORE_CTX_new());
- #ifdef HAVE_EX_DATA
- for (i = 0; i < MAX_EX_DATA; i++) {
- AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
- WOLFSSL_SUCCESS);
- tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
- AssertNotNull(tmpDataRet);
- AssertIntEQ(tmpData, *(int*)tmpDataRet);
- }
- #else
- AssertIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
- WOLFSSL_FAILURE);
- tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
- AssertNull(tmpDataRet);
- #endif
- X509_STORE_CTX_free(ctx);
- }
- /* test X509_STORE_get/set_ex_data */
- {
- int i = 0, tmpData = 99;
- void* tmpDataRet;
- AssertNotNull(str = X509_STORE_new());
- #ifdef HAVE_EX_DATA
- for (i = 0; i < MAX_EX_DATA; i++) {
- AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
- WOLFSSL_SUCCESS);
- tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
- AssertNotNull(tmpDataRet);
- AssertIntEQ(tmpData, *(int*)tmpDataRet);
- }
- #else
- AssertIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
- WOLFSSL_FAILURE);
- tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
- AssertNull(tmpDataRet);
- #endif
- X509_STORE_free(str);
- }
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return res;
- }
- static int test_wolfSSL_X509_STORE_set_flags(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- X509_STORE* store;
- X509* x509;
- AssertNotNull((store = wolfSSL_X509_STORE_new()));
- AssertNotNull((x509 =
- wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
- #ifdef HAVE_CRL
- AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
- NOT_COMPILED_IN);
- #endif
- wolfSSL_X509_free(x509);
- wolfSSL_X509_STORE_free(store);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return res;
- }
- static int test_wolfSSL_X509_LOOKUP_load_file(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
- WOLFSSL_X509_STORE* store;
- WOLFSSL_X509_LOOKUP* lookup;
- AssertNotNull(store = wolfSSL_X509_STORE_new());
- AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
- AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
- X509_FILETYPE_PEM), 1);
- AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
- X509_FILETYPE_PEM), 1);
- if (store) {
- AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
- WOLFSSL_FILETYPE_PEM), 1);
- AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
- WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
- }
- AssertIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
- X509_FILETYPE_PEM), 1);
- if (store) {
- AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
- WOLFSSL_FILETYPE_PEM), 1);
- }
- wolfSSL_X509_STORE_free(store);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return res;
- }
- static int test_wolfSSL_X509_STORE_CTX_set_time(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- WOLFSSL_X509_STORE_CTX* ctx;
- time_t c_time;
- AssertNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
- c_time = 365*24*60*60;
- wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
- AssertTrue(
- (ctx->param->flags & WOLFSSL_USE_CHECK_TIME) == WOLFSSL_USE_CHECK_TIME);
- AssertTrue(ctx->param->check_time == c_time);
- wolfSSL_X509_STORE_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_CTX_get0_set1_param(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- int ret;
- SSL_CTX* ctx;
- WOLFSSL_X509_VERIFY_PARAM* pParam;
- WOLFSSL_X509_VERIFY_PARAM* pvpm;
- char testIPv4[] = "127.0.0.1";
- char testhostName[] = "foo.hoge.com";
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertNull(SSL_CTX_get0_param(NULL));
- AssertNotNull(pParam = SSL_CTX_get0_param(ctx));
- pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
- sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL);
- AssertNotNull(pvpm);
- XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
- wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
- (int)XSTRLEN(testhostName));
- wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4);
- wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
- ret = SSL_CTX_set1_param(ctx, pvpm);
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
- (int)XSTRLEN(testhostName)));
- AssertIntEQ(0x01, pParam->hostFlags);
- AssertIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
- /* test for incorrect patameter */
- AssertIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
- AssertIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
- AssertIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
- SSL_CTX_free(ctx);
- XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
- return res;
- }
- static int test_wolfSSL_get0_param(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- SSL_CTX* ctx;
- SSL* ssl;
- WOLFSSL_X509_VERIFY_PARAM* pParam;
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- pParam = SSL_get0_param(ssl);
- (void)pParam;
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
- return res;
- }
- static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- const char host[] = "www.example.com";
- WOLFSSL_X509_VERIFY_PARAM* pParam;
- AssertNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
- sizeof(WOLFSSL_X509_VERIFY_PARAM),
- HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
- XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
- X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
- AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
- XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
- AssertIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
- XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_set1_host(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- const char host[] = "www.test_wolfSSL_set1_host.com";
- const char emptyStr[] = "";
- SSL_CTX* ctx;
- SSL* ssl;
- WOLFSSL_X509_VERIFY_PARAM* pParam;
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- pParam = SSL_get0_param(ssl);
- /* we should get back host string */
- SSL_set1_host(ssl, host);
- AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
- /* we should get back empty string */
- SSL_set1_host(ssl, emptyStr);
- AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
- /* we should get back host string */
- SSL_set1_host(ssl, host);
- AssertIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
- /* we should get back empty string */
- SSL_set1_host(ssl, NULL);
- AssertIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- unsigned char buf[16] = {0};
- WOLFSSL_X509_VERIFY_PARAM* param;
- AssertNotNull(param = X509_VERIFY_PARAM_new());
- /* test 127.0.0.1 */
- buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
- AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
- AssertIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
- /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
- buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
- buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
- buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
- buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
- AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
- AssertIntEQ(XSTRNCMP(param->ipasc,
- "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
- /* test 2001:db8:: */
- buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
- buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
- buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
- buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
- AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
- AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
- /* test ::1234:5678 */
- buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
- buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
- buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
- buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
- AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
- AssertIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
- /* test 2001:db8::1234:5678 */
- buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
- buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
- buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
- buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
- AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
- AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
- sizeof(param->ipasc)), 0);
- /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
- /* 2001:db8:1::ab9:c0a8:102 */
- buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
- buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
- buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
- buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
- AssertIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
- AssertIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
- sizeof(param->ipasc)), 0);
- XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- X509_STORE* store;
- X509_STORE_CTX* ctx;
- X509_STORE_CTX* ctx_no_init;
- AssertNotNull((store = X509_STORE_new()));
- AssertNotNull(ctx = X509_STORE_CTX_new());
- AssertNotNull(ctx_no_init = X509_STORE_CTX_new());
- AssertIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
- AssertNull(X509_STORE_CTX_get0_store(NULL));
- /* should return NULL if ctx has not bee initialized */
- AssertNull(X509_STORE_CTX_get0_store(ctx_no_init));
- AssertNotNull(X509_STORE_CTX_get0_store(ctx));
- wolfSSL_X509_STORE_CTX_free(ctx);
- wolfSSL_X509_STORE_CTX_free(ctx_no_init);
- X509_STORE_free(store);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_CTX_set_client_CA_list(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- X509_NAME* name = NULL;
- STACK_OF(X509_NAME)* names = NULL;
- STACK_OF(X509_NAME)* ca_list = NULL;
- int i, names_len;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- /* Send two X501 names in cert request */
- names = SSL_load_client_CA_file(cliCertFile);
- AssertNotNull(names);
- ca_list = SSL_load_client_CA_file(caCertFile);
- AssertNotNull(ca_list);
- AssertIntEQ(sk_X509_NAME_push(names, sk_X509_NAME_value(ca_list, 0)), 1);
- SSL_CTX_set_client_CA_list(ctx, names);
- /* This should only free the stack structure */
- sk_X509_NAME_free(ca_list);
- AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
- AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
- AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
- for (i=0; i<names_len; i++) {
- AssertNotNull(name = sk_X509_NAME_value(names, i));
- AssertIntEQ(sk_X509_NAME_find(names, name), i);
- }
- /* Needed to be able to create ssl object */
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
- AssertNotNull(ssl = wolfSSL_new(ctx));
- /* load again as old names are responsibility of ctx to free*/
- names = SSL_load_client_CA_file(cliCertFile);
- AssertNotNull(names);
- SSL_set_client_CA_list(ssl, names);
- AssertNotNull(ca_list = SSL_get_client_CA_list(ssl));
- AssertIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
- AssertIntGT((names_len = sk_X509_NAME_num(names)), 0);
- for (i=0; i<names_len; i++) {
- AssertNotNull(name = sk_X509_NAME_value(names, i));
- AssertIntEQ(sk_X509_NAME_find(names, name), i);
- }
- #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
- {
- tcp_ready ready;
- func_args server_args;
- callback_functions server_cb;
- THREAD_TYPE serverThread;
- WOLFSSL* ssl_client;
- WOLFSSL_CTX* ctx_client;
- SOCKET_T sockfd = 0;
- /* wolfSSL_get_client_CA_list() with handshake */
- StartTCP();
- InitTcpReady(&ready);
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- server_args.signal = &ready;
- server_args.callbacks = &server_cb;
- /* we are responsible for free'ing WOLFSSL_CTX */
- server_cb.ctx = ctx;
- server_cb.isSharedCtx = 1;
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
- AssertNotNull(ctx_client = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx_client, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx_client, cliCertFile, SSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
- AssertNotNull(ssl_client = wolfSSL_new(ctx_client));
- AssertIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
- AssertNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
- /* We are expecting two cert names to be sent */
- AssertIntEQ(sk_X509_NAME_num(ca_list), 2);
- AssertNotNull(names = SSL_CTX_get_client_CA_list(ctx));
- for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
- AssertNotNull(name = sk_X509_NAME_value(ca_list, i));
- AssertIntGE(sk_X509_NAME_find(names, name), 0);
- }
- wolfSSL_shutdown(ssl_client);
- wolfSSL_free(ssl_client);
- wolfSSL_CTX_free(ctx_client);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- }
- #endif
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
- * !NO_BIO */
- return res;
- }
- static int test_wolfSSL_CTX_add_client_CA(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
- !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX* ctx;
- WOLFSSL_X509* x509;
- WOLFSSL_X509* x509_a;
- STACK_OF(X509_NAME)* ca_list;
- int ret = 0;
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- /* Add client cert */
- x509 = X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
- AssertNotNull(x509);
- ret = SSL_CTX_add_client_CA(ctx, x509);
- AssertIntEQ(ret, SSL_SUCCESS);
- AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
- /* Add another client cert */
- AssertNotNull(x509_a = X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM));
- AssertIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
- /* test for incorrect parameter */
- AssertIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
- AssertIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
- AssertIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
- X509_free(x509);
- X509_free(x509_a);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
- return res;
- }
- #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
- static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
- {
- callback_functions* callbacks = ((func_args*)args)->callbacks;
- WOLFSSL_CTX* ctx = callbacks->ctx;
- WOLFSSL* ssl = NULL;
- SOCKET_T sfd = 0;
- SOCKET_T cfd = 0;
- word16 port;
- char input[1024];
- int idx;
- int ret, err = 0;
- const char* privateName = "ech-private-name.com";
- int privateNameLen = (int)XSTRLEN(privateName);
- ((func_args*)args)->return_code = TEST_FAIL;
- port = ((func_args*)args)->signal->port;
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- if (callbacks->ctx_ready)
- callbacks->ctx_ready(ctx);
- ssl = wolfSSL_new(ctx);
- /* set the sni for the server */
- wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
- tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
- CloseSocket(sfd);
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
- if (callbacks->ssl_ready)
- callbacks->ssl_ready(ssl);
- do {
- err = 0; /* Reset error */
- ret = wolfSSL_accept(ssl);
- if (ret != WOLFSSL_SUCCESS) {
- err = wolfSSL_get_error(ssl, 0);
- }
- } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
- }
- else {
- if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
- input[idx] = 0;
- printf("Client message: %s\n", input);
- }
- AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
- privateNameLen));
- ((func_args*)args)->return_code = TEST_SUCCESS;
- }
- if (callbacks->on_result)
- callbacks->on_result(ssl);
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(cfd);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- return 0;
- }
- #endif /* HAVE_ECH && WOLFSSL_TLS13 */
- #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
- static THREAD_RETURN WOLFSSL_THREAD server_task(void* args)
- {
- callback_functions* callbacks = ((func_args*)args)->callbacks;
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
- WOLFSSL* ssl = NULL;
- SOCKET_T sfd = 0;
- SOCKET_T cfd = 0;
- word16 port;
- char msg[] = "I hear you fa shizzle!";
- int len = (int) XSTRLEN(msg);
- char input[1024];
- int idx;
- int ret, err = 0;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- ((func_args*)args)->return_code = TEST_FAIL;
- port = ((func_args*)args)->signal->port;
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- if (callbacks->ctx_ready)
- callbacks->ctx_ready(ctx);
- ssl = wolfSSL_new(ctx);
- tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
- CloseSocket(sfd);
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
- if (callbacks->ssl_ready)
- callbacks->ssl_ready(ssl);
- do {
- err = 0; /* Reset error */
- ret = wolfSSL_accept(ssl);
- if (ret != WOLFSSL_SUCCESS) {
- err = wolfSSL_get_error(ssl, 0);
- }
- } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- char buff[WOLFSSL_MAX_ERROR_SZ];
- fprintf(stderr, "error = %d, %s\n", err,
- wolfSSL_ERR_error_string(err, buff));
- }
- else {
- if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
- input[idx] = 0;
- fprintf(stderr, "Client message: %s\n", input);
- }
- AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
- #ifdef WOLFSSL_TIRTOS
- Task_yield();
- #endif
- ((func_args*)args)->return_code = TEST_SUCCESS;
- }
- if (callbacks->on_result)
- callbacks->on_result(ssl);
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(cfd);
- #ifdef WOLFSSL_TIRTOS
- fdCloseSession(Task_self());
- #endif
- #ifndef WOLFSSL_TIRTOS
- return 0;
- #endif
- }
- static void keyLog_callback(const WOLFSSL* ssl, const char* line )
- {
- AssertNotNull(ssl);
- AssertNotNull(line);
- XFILE fp;
- const byte lf = '\n';
- fp = XFOPEN("./MyKeyLog.txt", "a");
- XFWRITE( line, 1, strlen(line),fp);
- XFWRITE( (void*)&lf,1,1,fp);
- XFCLOSE(fp);
- }
- #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
- static int test_wolfSSL_CTX_set_keylog_callback(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
- !defined(NO_WOLFSSL_CLIENT)
- SSL_CTX* ctx;
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
- SSL_CTX_free(ctx);
- SSL_CTX_set_keylog_callback(NULL, NULL);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
- return res;
- }
- static int test_wolfSSL_CTX_get_keylog_callback(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
- !defined(NO_WOLFSSL_CLIENT)
- SSL_CTX* ctx;
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
- SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
- AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
- SSL_CTX_set_keylog_callback(ctx, NULL );
- AssertPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
- return res;
- }
- static int test_wolfSSL_Tls12_Key_Logging_test(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
- /* This test is intended for checking whether keylog callback is called
- * in client during TLS handshake between the client and a server.
- */
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- callback_functions server_cbf;
- callback_functions client_cbf;
- SOCKET_T sockfd = 0;
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- XFILE fp;
- char msg[64] = "hello wolfssl!";
- char reply[1024];
- int msgSz = (int)XSTRLEN(msg);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- InitTcpReady(&ready);
- ready.port = 22222;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&server_cbf, 0, sizeof(callback_functions));
- XMEMSET(&client_cbf, 0, sizeof(callback_functions));
- server_cbf.method = wolfTLSv1_2_server_method;
- server_args.callbacks = &server_cbf;
- server_args.signal = &ready;
- /* clean up keylog file */
- fp = XFOPEN("./MyKeyLog.txt", "w");
- XFCLOSE(fp);
- /* start server task */
- start_thread(server_task, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- /* run as a TLS1.2 client */
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
- /* set keylog callback */
- wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
- /* get connected the server task */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
- AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(sockfd);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- /* check if the keylog file exists */
- char buff[300] = {0};
- int found = 0;
- fp = XFOPEN("./MyKeyLog.txt", "r");
- AssertNotNull(fp);
- while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
- if (0 == strncmp(buff,"CLIENT_RANDOM ",
- sizeof("CLIENT_RANDOM ")-1)) {
- found = 1;
- break;
- }
- }
- XFCLOSE(fp);
- /* a log starting with "CLIENT_RANDOM " should exit in the file */
- AssertNotNull( found );
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
- return res;
- }
- static int test_wolfSSL_Tls13_Key_Logging_test(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
- defined(HAVE_SECRET_CALLBACK)
- /* This test is intended for checking whether keylog callback is called
- * in client during TLS handshake between the client and a server.
- */
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- callback_functions server_cbf;
- callback_functions client_cbf;
- SOCKET_T sockfd = 0;
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- XFILE fp;
- char msg[64] = "hello wolfssl!";
- char reply[1024];
- int msgSz = (int)XSTRLEN(msg);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- InitTcpReady(&ready);
- ready.port = 22222;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&server_cbf, 0, sizeof(callback_functions));
- XMEMSET(&client_cbf, 0, sizeof(callback_functions));
- server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
- server_args.callbacks = &server_cbf;
- server_args.signal = &ready;
- /* clean up keylog file */
- fp = XFOPEN("./MyKeyLog.txt", "w");
- XFCLOSE(fp);
- /* start server task */
- start_thread(server_task, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- /* run as a TLS1.3 client */
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
- /* set keylog callback */
- wolfSSL_CTX_set_keylog_callback(ctx,keyLog_callback);
- /* get connected the server task */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
- AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- /* check if the keylog file exists */
- {
- char buff[300] = {0};
- int found[4] = {0};
- int numfnd = 0;
- int i;
- fp = XFOPEN("./MyKeyLog.txt", "r");
- AssertNotNull(fp);
- while (XFGETS( buff, (int)sizeof(buff),fp) != NULL ) {
- if (0 == strncmp(buff,"CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
- sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
- found[0] = 1;
- continue;
- }
- else if (0 == strncmp(buff,"SERVER_HANDSHAKE_TRAFFIC_SECRET ",
- sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
- found[1] = 1;
- continue;
- }
- else if (0 == strncmp(buff,"CLIENT_TRAFFIC_SECRET_0 ",
- sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
- found[2] = 1;
- continue;
- }
- else if (0 == strncmp(buff,"SERVER_TRAFFIC_SECRET_0 ",
- sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
- found[3] = 1;
- continue;
- }
- }
- XFCLOSE(fp);
- for (i = 0; i < 4; i++) {
- if (found[i] != 0)
- numfnd++;
- }
- AssertIntEQ(numfnd, 4);
- }
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
- return res;
- }
- #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
- static int test_wolfSSL_Tls13_ECH_params(void)
- {
- #if !defined(NO_WOLFSSL_CLIENT)
- word32 outputLen = 0;
- byte testBuf[72];
- WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
- WOLFSSL *ssl = wolfSSL_new(ctx);
- AssertNotNull(ctx);
- AssertNotNull(ssl);
- /* invalid ctx */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
- "ech-public-name.com", 0, 0, 0));
- /* invalid public name */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
- 0, 0));
- /* invalid algorithms */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
- "ech-public-name.com", 1000, 1000, 1000));
- /* invalid ctx */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
- &outputLen));
- /* invalid output len */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
- /* invalid ssl */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
- (char*)testBuf, sizeof(testBuf)));
- /* invalid configs64 */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
- sizeof(testBuf)));
- /* invalid size */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
- (char*)testBuf, 0));
- /* invalid ssl */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
- sizeof(testBuf)));
- /* invalid configs */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
- sizeof(testBuf)));
- /* invalid size */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
- /* invalid ssl */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
- /* invalid size */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- #endif /* !NO_WOLFSSL_CLIENT */
- return TEST_SUCCESS;
- }
- static int test_wolfSSL_Tls13_ECH(void)
- {
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- callback_functions server_cbf;
- callback_functions client_cbf;
- SOCKET_T sockfd = 0;
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- const char* publicName = "ech-public-name.com";
- const char* privateName = "ech-private-name.com";
- int privateNameLen = 20;
- char reply[1024];
- int replyLen = 0;
- byte rawEchConfig[128];
- word32 rawEchConfigLen = sizeof(rawEchConfig);
- InitTcpReady(&ready);
- ready.port = 22222;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&server_cbf, 0, sizeof(callback_functions));
- XMEMSET(&client_cbf, 0, sizeof(callback_functions));
- server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
- /* create the server context here so we can get the ech config */
- AssertNotNull(server_cbf.ctx =
- wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
- /* generate ech config */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
- publicName, 0, 0, 0));
- /* get the config for the client to use */
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
- &rawEchConfigLen));
- server_args.callbacks = &server_cbf;
- server_args.signal = &ready;
- /* start server task */
- start_thread(server_task_ech, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- /* run as a TLS1.3 client */
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
- /* get connected the server task */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- /* set the ech configs for the client */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
- rawEchConfigLen));
- /* set the sni for the client */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
- privateName, privateNameLen));
- AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
- privateNameLen);
- AssertIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
- /* add th null terminator for string compare */
- reply[replyLen] = 0;
- /* check that the server replied with the private name */
- AssertStrEQ(privateName, reply);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- return TEST_SUCCESS;
- }
- #endif /* HAVE_ECH && WOLFSSL_TLS13 */
- #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- static void post_auth_version_cb(WOLFSSL* ssl)
- {
- /* do handshake and then test version error */
- AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
- AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
- }
- static void post_auth_version_client_cb(WOLFSSL* ssl)
- {
- /* do handshake and then test version error */
- AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
- AssertStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
- AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
- #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
- /* check was added to error queue */
- AssertIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
- /* check the string matches expected string */
- AssertStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
- "WRONG_SSL_VERSION");
- #endif
- }
- static void post_auth_cb(WOLFSSL* ssl)
- {
- WOLFSSL_X509* x509;
- /* do handshake and then test version error */
- AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
- AssertStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
- AssertNull(x509 = wolfSSL_get_peer_certificate(ssl));
- wolfSSL_X509_free(x509);
- AssertIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
- }
- static void set_post_auth_cb(WOLFSSL* ssl)
- {
- if (!wolfSSL_is_server(ssl)) {
- AssertIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
- }
- else {
- wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
- }
- }
- #endif
- static int test_wolfSSL_Tls13_postauth(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- callback_functions server_cbf;
- callback_functions client_cbf;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- /* test version failure doing post auth with TLS 1.2 connection */
- XMEMSET(&server_cbf, 0, sizeof(callback_functions));
- XMEMSET(&client_cbf, 0, sizeof(callback_functions));
- server_cbf.method = wolfTLSv1_2_server_method;
- server_cbf.ssl_ready = set_post_auth_cb;
- server_cbf.on_result = post_auth_version_cb;
- client_cbf.ssl_ready = set_post_auth_cb;
- client_cbf.on_result = post_auth_version_client_cb;
- server_args.callbacks = &server_cbf;
- client_args.callbacks = &client_cbf;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- /* tests on post auth with TLS 1.3 */
- XMEMSET(&server_cbf, 0, sizeof(callback_functions));
- XMEMSET(&client_cbf, 0, sizeof(callback_functions));
- server_cbf.method = wolfTLSv1_3_server_method;
- server_cbf.ssl_ready = set_post_auth_cb;
- client_cbf.ssl_ready = set_post_auth_cb;
- server_cbf.on_result = post_auth_cb;
- client_cbf.on_result = NULL;
- server_args.callbacks = &server_cbf;
- client_args.callbacks = &client_cbf;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_NID(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
- !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
- int sigType;
- int nameSz;
- X509* cert;
- EVP_PKEY* pubKeyTmp;
- X509_NAME* name;
- char commonName[80];
- char countryName[80];
- char localityName[80];
- char stateName[80];
- char orgName[80];
- char orgUnit[80];
- /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
- /* convert cert from DER to internal WOLFSSL_X509 struct */
- AssertNotNull(cert = wolfSSL_X509_d2i(&cert, client_cert_der_2048,
- sizeof_client_cert_der_2048));
- /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
- /* extract PUBLIC KEY from cert */
- AssertNotNull(pubKeyTmp = X509_get_pubkey(cert));
- /* extract signatureType */
- AssertIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
- /* extract subjectName info */
- AssertNotNull(name = X509_get_subject_name(cert));
- AssertIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
- AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
- NULL, 0)), 0);
- AssertIntEQ(nameSz, 15);
- AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
- commonName, sizeof(commonName))), 0);
- AssertIntEQ(nameSz, 15);
- AssertIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
- AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
- commonName, 9)), 0);
- AssertIntEQ(nameSz, 8);
- AssertIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
- AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
- countryName, sizeof(countryName))), 0);
- AssertIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
- AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
- localityName, sizeof(localityName))), 0);
- AssertIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
- AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_stateOrProvinceName,
- stateName, sizeof(stateName))), 0);
- AssertIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
- AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
- orgName, sizeof(orgName))), 0);
- AssertIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
- AssertIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationalUnitName,
- orgUnit, sizeof(orgUnit))), 0);
- AssertIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
- EVP_PKEY_free(pubKeyTmp);
- X509_free(cert);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_set_srp_username(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
- && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- const char *username = "TESTUSER";
- const char *password = "TESTPASSWORD";
- int r;
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
- AssertIntEQ(r,SSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
- AssertIntEQ(r,SSL_SUCCESS);
- r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
- AssertIntEQ(r,SSL_SUCCESS);
- AssertNotNull(ssl = SSL_new(ctx));
- AssertNotNull(SSL_get_srp_username(ssl));
- AssertStrEQ(SSL_get_srp_username(ssl), username);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
- /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
- return res;
- }
- static int test_wolfSSL_CTX_set_srp_password(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
- && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX* ctx;
- const char *username = "TESTUSER";
- const char *password = "TESTPASSWORD";
- int r;
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
- AssertIntEQ(r,SSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- r = wolfSSL_CTX_set_srp_username(ctx, (char *)username);
- AssertIntEQ(r,SSL_SUCCESS);
- r = wolfSSL_CTX_set_srp_password(ctx, (char *)password);
- AssertIntEQ(r,SSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
- /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
- return res;
- }
- static int test_wolfSSL_X509_STORE(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- X509_STORE *store;
- #ifdef HAVE_CRL
- X509_STORE_CTX *storeCtx;
- X509_CRL *crl;
- X509 *ca, *cert;
- const char crlPem[] = "./certs/crl/crl.revoked";
- const char srvCert[] = "./certs/server-revoked-cert.pem";
- const char caCert[] = "./certs/ca-cert.pem";
- XFILE fp;
- AssertNotNull(store = (X509_STORE *)X509_STORE_new());
- AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
- SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
- AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
- SSL_FILETYPE_PEM)));
- AssertNotNull((storeCtx = X509_STORE_CTX_new()));
- AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
- AssertIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
- X509_STORE_free(store);
- X509_STORE_CTX_free(storeCtx);
- X509_free(cert);
- X509_free(ca);
- /* should fail to verify now after adding in CRL */
- AssertNotNull(store = (X509_STORE *)X509_STORE_new());
- AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
- SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
- fp = XFOPEN(crlPem, "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
- NULL, NULL));
- XFCLOSE(fp);
- AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
- AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
- AssertNotNull((storeCtx = X509_STORE_CTX_new()));
- AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
- SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
- AssertIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
- AssertIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_CERT_REVOKED);
- X509_CRL_free(crl);
- X509_STORE_free(store);
- X509_STORE_CTX_free(storeCtx);
- X509_free(cert);
- X509_free(ca);
- #endif /* HAVE_CRL */
- #ifndef WOLFCRYPT_ONLY
- {
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- SSL_CTX* ctx;
- SSL* ssl;
- int i;
- for (i = 0; i < 2; i++) {
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertNotNull(store = (X509_STORE *)X509_STORE_new());
- SSL_CTX_set_cert_store(ctx, store);
- AssertNotNull(store = (X509_STORE *)X509_STORE_new());
- SSL_CTX_set_cert_store(ctx, store);
- AssertNotNull(store = (X509_STORE *)X509_STORE_new());
- AssertIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
- SSL_FILETYPE_PEM), SSL_SUCCESS);
- AssertIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- SSL_FILETYPE_PEM), SSL_SUCCESS);
- AssertNotNull(ssl = SSL_new(ctx));
- if (i == 0) {
- AssertIntEQ(SSL_set0_verify_cert_store(ssl, store), SSL_SUCCESS);
- }
- else {
- AssertIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
- X509_STORE_free(store);
- }
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- }
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- }
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_STORE_load_locations(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
- !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
- SSL_CTX *ctx;
- X509_STORE *store;
- const char ca_file[] = "./certs/ca-cert.pem";
- const char client_pem_file[] = "./certs/client-cert.pem";
- const char client_der_file[] = "./certs/client-cert.der";
- const char ecc_file[] = "./certs/ecc-key.pem";
- const char certs_path[] = "./certs/";
- const char bad_path[] = "./bad-path/";
- #ifdef HAVE_CRL
- const char crl_path[] = "./certs/crl/";
- const char crl_file[] = "./certs/crl/crl.pem";
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
- #endif
- AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
- AssertIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL), WOLFSSL_SUCCESS);
- /* Test bad arguments */
- AssertIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_STORE_load_locations(store, client_der_file, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_STORE_load_locations(store, ecc_file, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_STORE_load_locations(store, NULL, bad_path), WOLFSSL_FAILURE);
- #ifdef HAVE_CRL
- /* Test with CRL */
- AssertIntEQ(X509_STORE_load_locations(store, crl_file, NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_STORE_load_locations(store, NULL, crl_path), WOLFSSL_SUCCESS);
- #endif
- /* Test with CA */
- AssertIntEQ(X509_STORE_load_locations(store, ca_file, NULL), WOLFSSL_SUCCESS);
- /* Test with client_cert and certs path */
- AssertIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_STORE_load_locations(store, NULL, certs_path), WOLFSSL_SUCCESS);
- #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
- /* Clear nodes */
- ERR_clear_error();
- #endif
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_X509_STORE_get0_objects(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
- !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
- X509_STORE *store;
- X509_STORE *store_cpy;
- SSL_CTX *ctx;
- X509_OBJECT *obj;
- STACK_OF(X509_OBJECT) *objs;
- int i;
- /* Setup store */
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
- #endif
- AssertNotNull(store_cpy = X509_STORE_new());
- AssertNotNull(store = SSL_CTX_get_cert_store(ctx));
- AssertIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_STORE_load_locations(store, caCertFile, NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL), WOLFSSL_SUCCESS);
- #ifdef HAVE_CRL
- AssertIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir), WOLFSSL_SUCCESS);
- #endif
- /* Store ready */
- /* Similar to HaProxy ssl_set_cert_crl_file use case */
- AssertNotNull(objs = X509_STORE_get0_objects(store));
- #ifdef HAVE_CRL
- #ifdef WOLFSSL_SIGNER_DER_CERT
- AssertIntEQ(sk_X509_OBJECT_num(objs), 4);
- #else
- AssertIntEQ(sk_X509_OBJECT_num(objs), 1);
- #endif
- #else
- #ifdef WOLFSSL_SIGNER_DER_CERT
- AssertIntEQ(sk_X509_OBJECT_num(objs), 3);
- #else
- AssertIntEQ(sk_X509_OBJECT_num(objs), 0);
- #endif
- #endif
- for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
- obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
- switch (X509_OBJECT_get_type(obj)) {
- case X509_LU_X509:
- AssertNotNull(X509_OBJECT_get0_X509(obj));
- AssertIntEQ(X509_STORE_add_cert(store_cpy,
- X509_OBJECT_get0_X509(obj)), WOLFSSL_SUCCESS);
- break;
- case X509_LU_CRL:
- #ifdef HAVE_CRL
- AssertNotNull(X509_OBJECT_get0_X509_CRL(obj));
- AssertIntEQ(X509_STORE_add_crl(store_cpy,
- X509_OBJECT_get0_X509_CRL(obj)), WOLFSSL_SUCCESS);
- break;
- #endif
- case X509_LU_NONE:
- default:
- Fail(("X509_OBJECT_get_type should return x509 or crl "
- "(when built with crl support)"),
- ("Unrecognized X509_OBJECT type or none"));
- }
- }
- X509_STORE_free(store_cpy);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BN(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
- BIGNUM* a;
- BIGNUM* b;
- BIGNUM* c;
- BIGNUM* d;
- ASN1_INTEGER* ai;
- AssertNotNull(b = BN_new());
- AssertNotNull(c = BN_new());
- AssertNotNull(d = BN_new());
- ai = ASN1_INTEGER_new();
- AssertNotNull(ai);
- /* at the moment hard setting since no set function */
- ai->data[0] = 0x02; /* tag for ASN_INTEGER */
- ai->data[1] = 0x01; /* length of integer */
- ai->data[2] = 0x03;
- AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
- ASN1_INTEGER_free(ai);
- AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
- AssertIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
- /* a + 3 = */
- AssertIntEQ(BN_add_word(NULL, 3), WOLFSSL_FAILURE);
- AssertIntEQ(BN_add_word(a, 3), WOLFSSL_SUCCESS);
- /* check result 3 + 3*/
- AssertIntEQ(BN_get_word(a), 6);
- /* set a back to 3 */
- AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
- /* a - 3 = */
- AssertIntEQ(BN_sub_word(NULL, 3), WOLFSSL_FAILURE);
- AssertIntEQ(BN_sub_word(a, 3), WOLFSSL_SUCCESS);
- /* check result 3 - 3*/
- AssertIntEQ(BN_get_word(a), 0);
- /* set a back to 3 */
- AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
- /* a^b mod c = */
- AssertIntEQ(BN_mod_exp(d, NULL, b, c, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(BN_mod_exp(d, a, b, c, NULL), WOLFSSL_SUCCESS);
- /* check result 3^2 mod 5 */
- AssertIntEQ(BN_get_word(d), 4);
- /* a*b = */
- AssertIntEQ(BN_mul(d, NULL, b, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(BN_mul(d, a, b, NULL), WOLFSSL_SUCCESS);
- /* check result 3*2 */
- AssertIntEQ(BN_get_word(d), 6);
- /* c/b => db + a */
- AssertIntEQ(BN_div(d, NULL, c, b, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(BN_div(d, a, c, b, NULL), WOLFSSL_SUCCESS);
- /* check result 5/2 */
- AssertIntEQ(BN_get_word(d), 2); /* check quotient */
- AssertIntEQ(BN_get_word(a), 1); /* check remainder */
- /* set a back to 3 */
- AssertIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
- /* a*b mod c = */
- AssertIntEQ(BN_mod_mul(d, NULL, b, c, NULL), SSL_FAILURE);
- AssertIntEQ(BN_mod_mul(d, a, b, c, NULL), SSL_SUCCESS);
- /* check result 3*2 mod 5 */
- AssertIntEQ(BN_get_word(d), 1);
- AssertIntEQ(BN_set_word(a, 16), SSL_SUCCESS);
- AssertIntEQ(BN_set_word(b, 24), SSL_SUCCESS);
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- /* gcd of a and b */
- AssertIntEQ(BN_gcd(d, NULL, b, NULL), SSL_FAILURE);
- AssertIntEQ(BN_gcd(d, a, b, NULL), SSL_SUCCESS);
- /* check result gcd(16, 24) */
- AssertIntEQ(BN_get_word(d), 8);
- #endif /* !NO_RSA && WOLFSSL_KEY_GEN */
- AssertIntEQ(BN_set_word(a, 1 << 6), SSL_SUCCESS);
- AssertIntEQ(BN_rshift(b, a, 6), SSL_SUCCESS);
- AssertIntEQ(BN_is_zero(b), 0);
- AssertIntEQ(BN_rshift(b, a, 7), SSL_SUCCESS);
- AssertIntEQ(BN_is_zero(b), 1);
- AssertIntEQ(BN_rshift1(b, a), SSL_SUCCESS);
- AssertIntEQ(BN_is_zero(b), 0);
- /* set b back to 2 */
- AssertIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
- {
- /* BN_mod_inverse test */
- BIGNUM *r = BN_new();
- BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
- AssertIntEQ((int)(BN_get_word(r) & 0x03), 3);
- BN_free(val);
- }
- #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
- defined(WOLFSSL_SP_INT_NEGATIVE))
- AssertIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
- AssertIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
- AssertIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
- AssertIntEQ(BN_is_word(a, 3), SSL_FAILURE);
- AssertIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
- #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
- {
- char* ret;
- AssertNotNull(ret = BN_bn2dec(c));
- AssertIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
- XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
- }
- #endif
- AssertIntEQ(BN_get_word(c), 4);
- #endif
- BN_free(a);
- BN_free(b);
- BN_free(c);
- BN_clear_free(d);
- /* check that converting NULL and the null string returns an error */
- a = NULL;
- AssertIntLE(BN_hex2bn(&a, NULL), 0);
- AssertIntLE(BN_hex2bn(&a, ""), 0);
- AssertNull(a);
- /* check that getting a string and a bin of the same number are equal,
- * and that the comparison works EQ, LT and GT */
- AssertIntGT(BN_hex2bn(&a, "03"), 0);
- AssertNotNull(b = BN_new());
- AssertIntEQ(BN_set_word(b, 3), SSL_SUCCESS);
- AssertNotNull(c = BN_new());
- AssertIntEQ(BN_set_word(c, 4), SSL_SUCCESS);
- AssertIntEQ(BN_cmp(a, b), 0);
- AssertIntLT(BN_cmp(a, c), 0);
- AssertIntGT(BN_cmp(c, b), 0);
- AssertIntEQ(BN_set_word(a, 0), 1);
- AssertIntEQ(BN_is_zero(a), 1);
- AssertIntEQ(BN_set_bit(a, 0x45), 1);
- AssertIntEQ(BN_is_zero(a), 0);
- AssertIntEQ(BN_is_bit_set(a, 0x45), 1);
- AssertIntEQ(BN_clear_bit(a, 0x45), 1);
- AssertIntEQ(BN_is_bit_set(a, 0x45), 0);
- AssertIntEQ(BN_is_zero(a), 1);
- BN_free(a);
- BN_free(b);
- BN_free(c);
- #if defined(USE_FAST_MATH) && !defined(HAVE_WOLF_BIGINT)
- {
- BIGNUM *ap;
- BIGNUM bv;
- BIGNUM cv;
- BIGNUM dv;
- AssertNotNull(ap = BN_new());
- BN_init(&bv);
- BN_init(&cv);
- BN_init(&dv);
- AssertIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
- AssertIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
- AssertIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
- /* a^b mod c = */
- AssertIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
- /* check result 3^2 mod 5 */
- AssertIntEQ(BN_get_word(&dv), 4);
- /* a*b mod c = */
- AssertIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
- AssertIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
- /* check result 3*2 mod 5 */
- AssertIntEQ(BN_get_word(&dv), 1);
- BN_free(ap);
- }
- #endif
- #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
- AssertNotNull(a = BN_new());
- AssertIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL),
- SSL_SUCCESS);
- AssertIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), SSL_SUCCESS);
- BN_free(a);
- #endif
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
- return res;
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- #define TEST_ARG 0x1234
- static void msg_cb(int write_p, int version, int content_type,
- const void *buf, size_t len, SSL *ssl, void *arg)
- {
- (void)write_p;
- (void)version;
- (void)content_type;
- (void)buf;
- (void)len;
- (void)ssl;
- AssertTrue(arg == (void*)TEST_ARG);
- }
- #endif
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
- !defined(NO_WOLFSSL_SERVER)
- #ifndef SINGLE_THREADED
- #if defined(SESSION_CERTS)
- #include "wolfssl/internal.h"
- #endif
- static int msgCb(SSL_CTX *ctx, SSL *ssl)
- {
- #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
- STACK_OF(X509)* sk;
- X509* x509;
- int i, num;
- BIO* bio;
- #endif
- (void) ctx;
- fprintf(stderr, "\n===== msgcb called ====\n");
- #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
- AssertTrue(SSL_get_peer_cert_chain(ssl) != NULL);
- AssertIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
- AssertNotNull(SSL_get0_verified_chain(ssl));
- #else
- (void) ssl;
- #endif
- #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
- bio = BIO_new(BIO_s_file());
- BIO_set_fp(bio, stderr, BIO_NOCLOSE);
- sk = SSL_get_peer_cert_chain(ssl);
- AssertNotNull(sk);
- if (!sk) {
- BIO_free(bio);
- return SSL_FAILURE;
- }
- num = sk_X509_num(sk);
- AssertTrue(num > 0);
- for (i = 0; i < num; i++) {
- x509 = sk_X509_value(sk,i);
- AssertNotNull(x509);
- if (!x509)
- break;
- fprintf(stderr, "Certificate at index [%d] = :\n",i);
- X509_print(bio,x509);
- fprintf(stderr, "\n\n");
- }
- BIO_free(bio);
- #endif
- return SSL_SUCCESS;
- }
- #endif
- #endif
- static int test_wolfSSL_msgCb(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_WOLFSSL_CLIENT) && \
- !defined(NO_WOLFSSL_SERVER)
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- #ifndef SINGLE_THREADED
- THREAD_TYPE serverThread;
- #endif
- callback_functions client_cb;
- callback_functions server_cb;
- /* create a failed connection and inspect the error */
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- XMEMSET(&client_cb, 0, sizeof(callback_functions));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- #ifndef WOLFSSL_NO_TLS12
- client_cb.method = wolfTLSv1_2_client_method;
- server_cb.method = wolfTLSv1_2_server_method;
- #else
- client_cb.method = wolfTLSv1_3_client_method;
- server_cb.method = wolfTLSv1_3_server_method;
- #endif
- server_args.signal = &ready;
- server_args.callbacks = &server_cb;
- client_args.signal = &ready;
- client_args.callbacks = &client_cb;
- client_args.return_code = TEST_FAIL;
- #ifndef SINGLE_THREADED
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, msgCb);
- join_thread(serverThread);
- #endif
- FreeTcpReady(&ready);
- #ifndef SINGLE_THREADED
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- #endif
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_either_side(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
- !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- #ifndef SINGLE_THREADED
- THREAD_TYPE serverThread;
- #endif
- callback_functions client_cb;
- callback_functions server_cb;
- /* create a failed connection and inspect the error */
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- XMEMSET(&client_cb, 0, sizeof(callback_functions));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- /* Use different CTX for client and server */
- client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
- AssertNotNull(client_cb.ctx);
- server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
- AssertNotNull(server_cb.ctx);
- /* we are responsible for free'ing WOLFSSL_CTX */
- server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
- server_args.signal = &ready;
- server_args.callbacks = &server_cb;
- client_args.signal = &ready;
- client_args.callbacks = &client_cb;
- client_args.return_code = TEST_FAIL;
- #ifndef SINGLE_THREADED
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- #endif
- wolfSSL_CTX_free(client_cb.ctx);
- wolfSSL_CTX_free(server_cb.ctx);
- FreeTcpReady(&ready);
- #ifndef SINGLE_THREADED
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- #endif
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_DTLS_either_side(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
- !defined(NO_FILESYSTEM) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
- defined(WOLFSSL_DTLS)
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- #ifndef SINGLE_THREADED
- THREAD_TYPE serverThread;
- #endif
- callback_functions client_cb;
- callback_functions server_cb;
- /* create a failed connection and inspect the error */
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- XMEMSET(&client_cb, 0, sizeof(callback_functions));
- XMEMSET(&server_cb, 0, sizeof(callback_functions));
- /* Use different CTX for client and server */
- client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
- AssertNotNull(client_cb.ctx);
- server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
- AssertNotNull(server_cb.ctx);
- /* we are responsible for free'ing WOLFSSL_CTX */
- server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
- server_args.signal = &ready;
- server_args.callbacks = &server_cb;
- client_args.signal = &ready;
- client_args.callbacks = &client_cb;
- client_args.return_code = TEST_FAIL;
- #ifndef SINGLE_THREADED
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- #endif
- wolfSSL_CTX_free(client_cb.ctx);
- wolfSSL_CTX_free(server_cb.ctx);
- FreeTcpReady(&ready);
- #ifndef SINGLE_THREADED
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- #endif
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_generate_cookie(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
- SSL_CTX* ctx;
- SSL* ssl;
- byte buf[FOURK_BUF] = {0};
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
- AssertNotNull(ssl = SSL_new(ctx));
- /* Test unconnected */
- AssertIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
- wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
- wolfSSL_SetCookieCtx(ssl, ctx);
- AssertNotNull(wolfSSL_GetCookieCtx(ssl));
- AssertNull(wolfSSL_GetCookieCtx(NULL));
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_set_options(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- char appData[] = "extra msg";
- #endif
- #ifdef OPENSSL_EXTRA
- unsigned char protos[] = {
- 7, 't', 'l', 's', '/', '1', '.', '2',
- 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
- };
- unsigned int len = sizeof(protos);
- void *arg = (void *)TEST_ARG;
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
- == WOLFSSL_OP_NO_TLSv1);
- AssertTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
- AssertIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
- WOLFSSL_OP_NO_SSLv2)), 0);
- AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
- WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
- AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
- WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
- AssertTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
- WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
- AssertFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
- WOLFSSL_OP_NO_COMPRESSION));
- wolfSSL_CTX_free(ctx);
- #ifndef NO_WOLFSSL_SERVER
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #else
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #endif
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- #ifdef OPENSSL_EXTRA
- AssertTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
- #endif
- AssertNotNull(ssl = wolfSSL_new(ctx));
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
- #ifdef HAVE_EX_DATA
- AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
- AssertNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
- if (ssl) {
- AssertIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
- appData, sizeof(appData)), 0);
- }
- #else
- AssertIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
- AssertNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
- #endif
- #endif
- AssertTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
- WOLFSSL_OP_NO_TLSv1);
- AssertTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
- AssertIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
- WOLFSSL_OP_NO_SSLv2)), 0);
- AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
- WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
- AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
- WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
- AssertTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
- WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
- #ifdef OPENSSL_EXTRA
- AssertFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
- WOLFSSL_OP_NO_COMPRESSION));
- #endif
- #ifdef OPENSSL_EXTRA
- AssertTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
- wolfSSL_set_msg_callback_arg(ssl, arg);
- #ifdef WOLFSSL_ERROR_CODE_OPENSSL
- AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
- #else
- AssertTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
- #endif
- #endif
- #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
- defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
- defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
- #if defined(HAVE_ALPN) && !defined(NO_BIO)
- #ifdef WOLFSSL_ERROR_CODE_OPENSSL
- AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
- #else
- AssertTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
- #endif
- #endif /* HAVE_ALPN && !NO_BIO */
- #endif
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return res;
- }
- static int test_wolfSSL_sk_SSL_CIPHER(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- SSL* ssl;
- SSL_CTX* ctx;
- STACK_OF(SSL_CIPHER) *sk, *dupSk;
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertNotNull(sk = SSL_get_ciphers(ssl));
- AssertNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
- AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
- AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
- /* error case because connection has not been established yet */
- AssertIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
- sk_SSL_CIPHER_free(dupSk);
- /* sk is pointer to internal struct that should be free'd in SSL_free */
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return res;
- }
- static int test_wolfSSL_set1_curves_list(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- SSL* ssl = NULL;
- SSL_CTX* ctx = NULL;
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
- SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
- #ifdef HAVE_ECC
- AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
- AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
- #endif
- #ifdef HAVE_CURVE25519
- AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
- #endif
- #ifdef HAVE_CURVE448
- AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
- #endif
- AssertIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
- #ifdef HAVE_ECC
- AssertIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
- AssertIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
- #endif
- #ifdef HAVE_CURVE25519
- AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
- #endif
- #ifdef HAVE_CURVE448
- AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
- #endif
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return res;
- }
- static int test_wolfSSL_set1_sigalgs_list(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
- #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
- SSL* ssl;
- SSL_CTX* ctx;
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
- SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
- #ifndef NO_RSA
- #ifndef NO_SHA256
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
- WOLFSSL_FAILURE);
- #ifdef WC_RSA_PSS
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
- WOLFSSL_SUCCESS);
- #endif
- #ifdef WOLFSSL_SHA512
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
- "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
- "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
- #elif defined(WOLFSSL_SHA384)
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
- "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
- "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
- #endif
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
- WOLFSSL_FAILURE);
- #endif
- #endif
- #ifdef HAVE_ECC
- #ifndef NO_SHA256
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"), WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_SHA512
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
- "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
- "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
- #elif defined(WOLFSSL_SHA384)
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
- "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl,
- "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
- #endif
- #endif
- #endif
- #ifdef HAVE_ED25519
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
- #endif
- #ifdef HAVE_ED448
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
- #endif
- #ifndef NO_DSA
- #ifndef NO_SHA256
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
- WOLFSSL_SUCCESS);
- #endif
- #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
- defined(WOLFSSL_ALLOW_TLS_SHA1))
- AssertIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
- WOLFSSL_SUCCESS);
- #endif
- #endif
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return res;
- }
- /* Testing wolfSSL_set_tlsext_status_type function.
- * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
- */
- static int test_wolfSSL_set_tlsext_status_type(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
- !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
- SSL* ssl;
- SSL_CTX* ctx;
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
- SSL_SUCCESS);
- AssertIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
- return res;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_read_bio(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- byte buff[6000];
- XFILE f;
- int bytes;
- X509* x509;
- BIO* bio = NULL;
- BUF_MEM* buf;
- f = XFOPEN(cliCertFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- AssertNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
- AssertNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
- AssertIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
- AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
- AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
- /* BIO should return the set EOF value */
- AssertIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
- AssertIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
- AssertIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
- AssertIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
- BIO_free(bio);
- BUF_MEM_free(buf);
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return res;
- }
- #if defined(OPENSSL_EXTRA)
- static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
- long argl, long ret)
- {
- (void)bio;
- (void)cmd;
- (void)argp;
- (void)argi;
- (void)argl;
- return ret;
- }
- #endif
- static int test_wolfSSL_BIO(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- const unsigned char* p;
- byte buff[20];
- BIO* bio1;
- BIO* bio2;
- BIO* bio3;
- char* bufPt;
- int i;
- for (i = 0; i < 20; i++) {
- buff[i] = i;
- }
- /* test BIO_free with NULL */
- AssertIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
- /* Creating and testing type BIO_s_bio */
- AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
- AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
- AssertNotNull(bio3 = BIO_new(BIO_s_bio()));
- /* read/write before set up */
- AssertIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
- AssertIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
- AssertIntEQ(BIO_set_nbio(bio1, 1), 1);
- AssertIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
- AssertIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
- AssertIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
- XMEMCPY(bufPt, buff, 10);
- AssertIntEQ(BIO_write(bio1, buff + 10, 10), 10);
- /* write buffer full */
- AssertIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
- AssertIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
- AssertIntEQ((int)BIO_ctrl_pending(bio1), 0);
- /* write the other direction with pair */
- AssertIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
- XMEMCPY(bufPt, buff, 8);
- AssertIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
- /* try read */
- AssertIntEQ((int)BIO_ctrl_pending(bio1), 8);
- AssertIntEQ((int)BIO_ctrl_pending(bio2), 20);
- /* try read using ctrl function */
- AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
- AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
- AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
- AssertIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
- AssertIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
- for (i = 0; i < 20; i++) {
- AssertIntEQ((int)bufPt[i], i);
- }
- AssertIntEQ(BIO_nread(bio2, &bufPt, 1), WOLFSSL_BIO_ERROR);
- AssertIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
- for (i = 0; i < 8; i++) {
- AssertIntEQ((int)bufPt[i], i);
- }
- AssertIntEQ(BIO_nread(bio1, &bufPt, 1), WOLFSSL_BIO_ERROR);
- AssertIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
- /* new pair */
- AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
- BIO_free(bio2); /* free bio2 and automatically remove from pair */
- AssertIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
- AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
- AssertIntEQ(BIO_nread(bio3, &bufPt, 10), WOLFSSL_BIO_ERROR);
- /* test wrap around... */
- AssertIntEQ(BIO_reset(bio1), 0);
- AssertIntEQ(BIO_reset(bio3), 0);
- /* fill write buffer, read only small amount then write again */
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
- XMEMCPY(bufPt, buff, 20);
- AssertIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
- for (i = 0; i < 4; i++) {
- AssertIntEQ(bufPt[i], i);
- }
- /* try writing over read index */
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
- XMEMSET(bufPt, 0, 4);
- AssertIntEQ((int)BIO_ctrl_pending(bio3), 20);
- /* read and write 0 bytes */
- AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
- /* should read only to end of write buffer then need to read again */
- AssertIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
- for (i = 0; i < 16; i++) {
- AssertIntEQ(bufPt[i], buff[4 + i]);
- }
- AssertIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
- AssertIntEQ(BIO_nread0(bio3, &bufPt), 4);
- for (i = 0; i < 4; i++) {
- AssertIntEQ(bufPt[i], 0);
- }
- /* read index should not have advanced with nread0 */
- AssertIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
- for (i = 0; i < 4; i++) {
- AssertIntEQ(bufPt[i], 0);
- }
- /* write and fill up buffer checking reset of index state */
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
- XMEMCPY(bufPt, buff, 20);
- /* test reset on data in bio1 write buffer */
- AssertIntEQ(BIO_reset(bio1), 0);
- AssertIntEQ((int)BIO_ctrl_pending(bio3), 0);
- AssertIntEQ(BIO_nread(bio3, &bufPt, 3), WOLFSSL_BIO_ERROR);
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
- AssertIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
- AssertNotNull(p);
- XMEMCPY(bufPt, buff, 20);
- AssertIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
- for (i = 0; i < 6; i++) {
- AssertIntEQ(bufPt[i], i);
- }
- /* test case of writing twice with offset read index */
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
- AssertIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
- AssertIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
- AssertIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
- BIO_free(bio1);
- BIO_free(bio3);
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
- {
- BIO* bioA = NULL;
- BIO* bioB = NULL;
- AssertIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
- AssertIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
- BIO_free(bioA);
- bioA = NULL;
- BIO_free(bioB);
- bioB = NULL;
- }
- #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
- /* BIOs with file pointers */
- #if !defined(NO_FILESYSTEM)
- {
- XFILE f1;
- XFILE f2;
- BIO* f_bio1;
- BIO* f_bio2;
- unsigned char cert[300];
- char testFile[] = "tests/bio_write_test.txt";
- char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
- AssertNotNull(f_bio1 = BIO_new(BIO_s_file()));
- AssertNotNull(f_bio2 = BIO_new(BIO_s_file()));
- /* Failure due to wrong BIO type */
- AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
- AssertIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
- f1 = XFOPEN(svrCertFile, "rwb");
- AssertTrue((f1 != XBADFILE));
- AssertIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
- AssertIntEQ(BIO_write_filename(f_bio2, testFile),
- WOLFSSL_SUCCESS);
- AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
- AssertIntEQ(BIO_tell(f_bio1),sizeof(cert));
- AssertIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
- AssertIntEQ(BIO_tell(f_bio2),sizeof(msg));
- AssertIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
- AssertIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
- AssertIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
- AssertIntEQ(BIO_reset(f_bio2), 0);
- AssertIntEQ(BIO_tell(NULL),-1);
- AssertIntEQ(BIO_tell(f_bio2),0);
- AssertIntEQ(BIO_seek(f_bio2, 4), 0);
- AssertIntEQ(BIO_tell(f_bio2),4);
- BIO_free(f_bio1);
- BIO_free(f_bio2);
- AssertNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
- AssertIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
- AssertIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
- BIO_free(f_bio1);
- }
- #endif /* !defined(NO_FILESYSTEM) */
- /* BIO info callback */
- {
- const char* testArg = "test";
- BIO* cb_bio;
- AssertNotNull(cb_bio = BIO_new(BIO_s_mem()));
- BIO_set_callback(cb_bio, bioCallback);
- AssertNotNull(BIO_get_callback(cb_bio));
- BIO_set_callback(cb_bio, NULL);
- AssertNull(BIO_get_callback(cb_bio));
- BIO_set_callback_arg(cb_bio, (char*)testArg);
- AssertStrEQ(BIO_get_callback_arg(cb_bio), testArg);
- AssertNull(BIO_get_callback_arg(NULL));
- BIO_free(cb_bio);
- }
- /* BIO_vfree */
- AssertNotNull(bio1 = BIO_new(BIO_s_bio()));
- BIO_vfree(NULL);
- BIO_vfree(bio1);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif /* !NO_BIO */
- static int test_wolfSSL_ASN1_STRING(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- ASN1_STRING* str = NULL;
- const char data[] = "hello wolfSSL";
- AssertNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
- AssertIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
- AssertIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
- AssertIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
- AssertIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
- ASN1_STRING_free(str);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_BIT_STRING(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_ALL
- ASN1_BIT_STRING* str;
- AssertNotNull(str = ASN1_BIT_STRING_new());
- AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
- AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
- AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
- AssertIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
- AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
- AssertIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
- ASN1_BIT_STRING_free(str);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_a2i_ASN1_INTEGER(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
- BIO *bio, *out;
- ASN1_INTEGER* ai;
- char buf[] = "123456\n12345\n112345678912345678901234567890\n";
- char tmp[1024];
- int tmpSz;
- const char expected1[] = "123456";
- const char expected2[] = "112345678912345678901234567890";
- AssertNotNull(bio = BIO_new_mem_buf(buf, -1));
- AssertNotNull(out = BIO_new(BIO_s_mem()));
- AssertNotNull(ai = ASN1_INTEGER_new());
- /* read first line */
- AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
- AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
- XMEMSET(tmp, 0, 1024);
- tmpSz = BIO_read(out, tmp, 1024);
- AssertIntEQ(tmpSz, 6);
- AssertIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
- /* fail on second line (not % 2) */
- AssertIntNE(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
- /* read 3rd long line */
- AssertIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), SSL_SUCCESS);
- AssertIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
- XMEMSET(tmp, 0, 1024);
- tmpSz = BIO_read(out, tmp, 1024);
- AssertIntEQ(tmpSz, 30);
- AssertIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
- BIO_free(out);
- BIO_free(bio);
- ASN1_INTEGER_free(ai);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_a2i_IPADDRESS(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
- const unsigned char* data;
- int dataSz = 0;
- ASN1_OCTET_STRING *st;
- const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
- const unsigned char ipv6_exp[] = {
- 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
- };
- const unsigned char ipv6_home[] = {
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
- };
- AssertNull(st = a2i_IPADDRESS("127.0.0.1bad"));
- AssertNotNull(st = a2i_IPADDRESS("127.0.0.1"));
- data = ASN1_STRING_get0_data(st);
- dataSz = ASN1_STRING_length(st);
- AssertIntEQ(dataSz, WOLFSSL_IP4_ADDR_LEN);
- AssertIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
- ASN1_STRING_free(st);
- AssertNotNull(st = a2i_IPADDRESS("::1"));
- data = ASN1_STRING_get0_data(st);
- dataSz = ASN1_STRING_length(st);
- AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
- AssertIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
- ASN1_STRING_free(st);
- AssertNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
- data = ASN1_STRING_get0_data(st);
- dataSz = ASN1_STRING_length(st);
- AssertIntEQ(dataSz, WOLFSSL_IP6_ADDR_LEN);
- AssertIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
- ASN1_STRING_free(st);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_DES_ecb_encrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
- WOLFSSL_DES_cblock input1,input2,output1,output2,back1,back2;
- WOLFSSL_DES_key_schedule key;
- XMEMCPY(key,"12345678",sizeof(WOLFSSL_DES_key_schedule));
- XMEMCPY(input1, "Iamhuman",sizeof(WOLFSSL_DES_cblock));
- XMEMCPY(input2, "Whoisit?",sizeof(WOLFSSL_DES_cblock));
- XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
- XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
- XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
- XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
- /* Encrypt messages */
- wolfSSL_DES_ecb_encrypt(&input1,&output1,&key,DES_ENCRYPT);
- wolfSSL_DES_ecb_encrypt(&input2,&output2,&key,DES_ENCRYPT);
- {
- /* Decrypt messages */
- int ret1 = 0;
- int ret2 = 0;
- wolfSSL_DES_ecb_encrypt(&output1,&back1,&key,DES_DECRYPT);
- ret1 = XMEMCMP((unsigned char *) back1,(unsigned char *) input1,sizeof(WOLFSSL_DES_cblock));
- AssertIntEQ(ret1,0);
- wolfSSL_DES_ecb_encrypt(&output2,&back2,&key,DES_DECRYPT);
- ret2 = XMEMCMP((unsigned char *) back2,(unsigned char *) input2,sizeof(WOLFSSL_DES_cblock));
- AssertIntEQ(ret2,0);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_TIME_adj(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
- && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
- const int year = 365*24*60*60;
- const int day = 24*60*60;
- const int hour = 60*60;
- const int mini = 60;
- const byte asn_utc_time = ASN_UTC_TIME;
- #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
- const byte asn_gen_time = ASN_GENERALIZED_TIME;
- #endif
- WOLFSSL_ASN1_TIME *asn_time, *s;
- int offset_day;
- long offset_sec;
- char date_str[CTC_DATE_SIZE + 1];
- time_t t;
- AssertNotNull(s = wolfSSL_ASN1_TIME_new());
- /* UTC notation test */
- /* 2000/2/15 20:30:00 */
- t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
- offset_day = 7;
- offset_sec = 45 * mini;
- /* offset_sec = -45 * min;*/
- AssertNotNull(asn_time =
- wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
- AssertTrue(asn_time->type == asn_utc_time);
- XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
- date_str[CTC_DATE_SIZE] = '\0';
- AssertIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
- /* negative offset */
- offset_sec = -45 * mini;
- asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
- AssertNotNull(asn_time);
- AssertTrue(asn_time->type == asn_utc_time);
- XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
- date_str[CTC_DATE_SIZE] = '\0';
- AssertIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
- XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
- XMEMSET(date_str, 0, sizeof(date_str));
- /* Generalized time will overflow time_t if not long */
- #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
- s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
- DYNAMIC_TYPE_OPENSSL);
- /* GeneralizedTime notation test */
- /* 2055/03/01 09:00:00 */
- t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
- offset_day = 12;
- offset_sec = 10 * mini;
- asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
- AssertTrue(asn_time->type == asn_gen_time);
- XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
- date_str[CTC_DATE_SIZE] = '\0';
- AssertIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
- XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
- XMEMSET(date_str, 0, sizeof(date_str));
- #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
- /* if WOLFSSL_ASN1_TIME struct is not allocated */
- s = NULL;
- t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
- offset_day = 7;
- offset_sec = 45 * mini;
- asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
- AssertTrue(asn_time->type == asn_utc_time);
- XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
- date_str[CTC_DATE_SIZE] = '\0';
- AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
- XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
- asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, offset_sec);
- AssertTrue(asn_time->type == asn_utc_time);
- XSTRNCPY(date_str, (const char*)&asn_time->data, CTC_DATE_SIZE);
- date_str[CTC_DATE_SIZE] = '\0';
- AssertIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
- XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_TIME_to_tm(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
- defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) \
- && !defined(NO_ASN_TIME)
- ASN1_TIME asnTime;
- struct tm tm;
- XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
- AssertIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
- AssertIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
- AssertIntEQ(tm.tm_sec, 15);
- AssertIntEQ(tm.tm_min, 15);
- AssertIntEQ(tm.tm_hour, 21);
- AssertIntEQ(tm.tm_mday, 22);
- AssertIntEQ(tm.tm_mon, 1);
- AssertIntEQ(tm.tm_year, 100);
- AssertIntEQ(tm.tm_isdst, 0);
- #ifdef XMKTIME
- AssertIntEQ(tm.tm_wday, 2);
- AssertIntEQ(tm.tm_yday, 52);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_cmp_time(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
- && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
- WOLFSSL_ASN1_TIME asn_time;
- time_t t;
- AssertIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
- XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
- AssertIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
- AssertIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
- AssertIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_time_adj(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
- !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
- defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
- !defined(NO_ASN_TIME)
- X509* x509;
- time_t t, not_before, not_after;
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
- client_cert_der_2048, sizeof_client_cert_der_2048,
- WOLFSSL_FILETYPE_ASN1));
- t = 0;
- not_before = wc_Time(0);
- not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
- AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
- AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
- /* Check X509_gmtime_adj, too. */
- AssertNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)\
- && !defined(NO_RSA)
- X509* x509;
- #ifndef NO_BIO
- BIO* bio;
- X509_STORE_CTX* ctx;
- X509_STORE* store;
- #endif
- char der[] = "certs/ca-cert.der";
- XFILE fp;
- AssertNotNull(x509 = X509_new());
- X509_free(x509);
- #ifndef NO_BIO
- x509 = wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM);
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- #ifdef WOLFSSL_CERT_GEN
- AssertIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
- #endif
- AssertNotNull(ctx = X509_STORE_CTX_new());
- AssertIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
- AssertNotNull(store = X509_STORE_new());
- AssertIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
- AssertIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
- AssertIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
- X509_STORE_CTX_free(ctx);
- X509_STORE_free(store);
- X509_free(x509);
- BIO_free(bio);
- #endif
- /** d2i_X509_fp test **/
- fp = XFOPEN(der, "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
- AssertNotNull(x509);
- X509_free(x509);
- XFCLOSE(fp);
- fp = XFOPEN(der, "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
- AssertNotNull(x509);
- X509_free(x509);
- XFCLOSE(fp);
- /* X509_up_ref test */
- AssertIntEQ(X509_up_ref(NULL), 0);
- AssertNotNull(x509 = X509_new()); /* refCount = 1 */
- AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
- AssertIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
- X509_free(x509); /* refCount = 2 */
- X509_free(x509); /* refCount = 1 */
- X509_free(x509); /* refCount = 0, free */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_get_ext_count(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA)
- int ret = 0;
- WOLFSSL_X509* x509;
- const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
- FILE* f;
- /* NULL parameter check */
- AssertIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
- SSL_FILETYPE_PEM));
- AssertIntEQ(X509_get_ext_count(x509), 5);
- wolfSSL_X509_free(x509);
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
- SSL_FILETYPE_PEM));
- AssertIntEQ(X509_get_ext_count(x509), 5);
- wolfSSL_X509_free(x509);
- AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
- fclose(f);
- /* wolfSSL_X509_get_ext_count() valid input */
- AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
- /* wolfSSL_X509_get_ext_count() NULL argument */
- AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_sign2(void)
- {
- int res = TEST_SKIPPED;
- /* test requires WOLFSSL_AKID_NAME to match expected output */
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
- (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
- WOLFSSL_X509 *x509, *ca;
- const unsigned char *der;
- const unsigned char *pt;
- WOLFSSL_EVP_PKEY *priv;
- WOLFSSL_X509_NAME *name;
- WOLFSSL_ASN1_TIME *notBefore, *notAfter;
- int derSz;
- const int year = 365*24*60*60;
- const int day = 24*60*60;
- const int hour = 60*60;
- const int mini = 60;
- time_t t;
- const unsigned char expected[] = {
- 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
- 0x02, 0x02, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07, 0x84,
- 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53, 0x30,
- 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
- 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
- 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
- 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
- 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
- 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
- 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
- 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
- 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
- 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
- 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
- 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
- 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
- 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
- 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
- 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
- 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
- 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
- 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
- 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
- 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
- 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
- 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
- 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
- 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
- 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
- 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
- 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
- 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
- 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
- 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
- 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
- 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
- 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
- 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
- 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
- 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
- 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
- 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
- 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
- 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
- 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
- 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
- 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
- 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
- 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
- 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
- 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
- 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
- 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
- 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
- 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
- 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
- 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
- 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
- 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
- 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
- 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
- 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
- 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
- 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
- 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
- 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
- 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
- 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
- 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
- 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
- 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
- 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
- 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
- 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
- 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
- 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
- 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
- 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
- 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
- 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
- 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
- 0x6F, 0x6D, 0x82, 0x14, 0x73, 0xFB, 0x54, 0xD6, 0x03, 0x7D, 0x4C, 0x07,
- 0x84, 0xE2, 0x00, 0x11, 0x8C, 0xDD, 0x90, 0xDC, 0x48, 0x8D, 0xEA, 0x53,
- 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
- 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
- 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
- 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
- 0x01, 0x01, 0x00, 0x4A, 0xFD, 0x81, 0xC9, 0xE9, 0xE6, 0x2D, 0xC7, 0x1F,
- 0xFA, 0x0A, 0xDC, 0x80, 0x21, 0xCE, 0xD9, 0x27, 0xD4, 0xA4, 0xA1, 0xEC,
- 0x87, 0x50, 0xA9, 0xE4, 0x6D, 0xF6, 0x04, 0x93, 0x5A, 0x1E, 0x51, 0xF4,
- 0x8F, 0x92, 0x3E, 0x58, 0x90, 0xD7, 0xE5, 0xD7, 0x4A, 0x3D, 0xF3, 0xC6,
- 0x1E, 0xE4, 0x78, 0x57, 0xCB, 0xE7, 0xED, 0x3F, 0x6A, 0x7D, 0x1E, 0xE2,
- 0xF1, 0x9F, 0xAA, 0x18, 0x0A, 0xC9, 0x1A, 0xD6, 0x78, 0x71, 0xB3, 0xB6,
- 0xE9, 0x55, 0x84, 0x27, 0x36, 0xA0, 0x89, 0x5C, 0x5A, 0x0A, 0x97, 0x53,
- 0x95, 0x36, 0x68, 0x39, 0xA9, 0x17, 0x51, 0x84, 0x2A, 0x68, 0x5F, 0xAE,
- 0xF3, 0x26, 0x32, 0x57, 0x99, 0x4A, 0x65, 0xE2, 0x14, 0x1E, 0xD8, 0x00,
- 0x24, 0xC1, 0xD1, 0x75, 0x56, 0xD3, 0x99, 0xD3, 0x55, 0x10, 0x88, 0xEC,
- 0x13, 0x05, 0x89, 0x18, 0x58, 0x55, 0x86, 0xFF, 0xA1, 0x2C, 0xB1, 0x96,
- 0xE5, 0x63, 0x1C, 0x83, 0xCA, 0xF6, 0x58, 0x0C, 0xD5, 0xD2, 0x27, 0x70,
- 0x61, 0x87, 0xCC, 0x17, 0x36, 0x6A, 0x75, 0x55, 0xB1, 0x13, 0xB6, 0xC8,
- 0x94, 0x0B, 0x1F, 0xE0, 0x32, 0xCA, 0x94, 0xA2, 0x46, 0x95, 0xBC, 0xA2,
- 0xA0, 0x2A, 0x4C, 0xEB, 0xFE, 0x14, 0xA3, 0x1D, 0x38, 0x13, 0x07, 0xB9,
- 0x98, 0x62, 0x88, 0xF1, 0x8F, 0xBC, 0xD7, 0x3F, 0x72, 0xD4, 0x2F, 0x77,
- 0xF2, 0x48, 0x0E, 0x9C, 0xAC, 0xE1, 0x44, 0x88, 0x58, 0x9A, 0x8E, 0x81,
- 0xBD, 0xB8, 0x6E, 0xF4, 0x64, 0x9B, 0x3A, 0xF1, 0x1D, 0x13, 0xE3, 0x51,
- 0xB9, 0xD1, 0x4D, 0xA3, 0xB5, 0x5D, 0x7B, 0x18, 0xBD, 0xDE, 0xAB, 0x1F,
- 0x82, 0x23, 0xAE, 0x6E, 0xB7, 0xE9, 0xEA, 0x54, 0xE6, 0xF5, 0x3E, 0x10,
- 0x80, 0x25, 0x36, 0x83, 0x46, 0xB2, 0x97, 0x8D, 0x3A, 0x06, 0xB6, 0xCC,
- 0x8D, 0xBE, 0xB4, 0xE6, 0x5E, 0xCA, 0x7B
- };
- pt = ca_key_der_2048;
- AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
- sizeof_ca_key_der_2048));
- pt = client_cert_der_2048;
- AssertNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
- sizeof_client_cert_der_2048));
- pt = ca_cert_der_2048;
- AssertNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
- AssertNotNull(name = wolfSSL_X509_get_subject_name(ca));
- AssertIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
- AssertNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
- AssertNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
- AssertIntEQ(notAfter->length, 13);
- AssertTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
- AssertTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- AssertNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
- AssertIntEQ(derSz, sizeof(expected));
- AssertIntEQ(XMEMCMP(der, expected, derSz), 0);
- wolfSSL_X509_free(ca);
- wolfSSL_X509_free(x509);
- wolfSSL_EVP_PKEY_free(priv);
- wolfSSL_ASN1_TIME_free(notBefore);
- wolfSSL_ASN1_TIME_free(notAfter);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_sign(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
- int ret;
- char *cn;
- word32 cnSz;
- X509_NAME *name;
- X509 *x509, *ca;
- DecodedCert dCert;
- EVP_PKEY *pub;
- EVP_PKEY *priv;
- EVP_MD_CTX *mctx;
- #if defined(USE_CERT_BUFFERS_1024)
- const unsigned char* rsaPriv = client_key_der_1024;
- const unsigned char* rsaPub = client_keypub_der_1024;
- const unsigned char* certIssuer = client_cert_der_1024;
- long clientKeySz = (long)sizeof_client_key_der_1024;
- long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
- long certIssuerSz = (long)sizeof_client_cert_der_1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- const unsigned char* rsaPriv = client_key_der_2048;
- const unsigned char* rsaPub = client_keypub_der_2048;
- const unsigned char* certIssuer = client_cert_der_2048;
- long clientKeySz = (long)sizeof_client_key_der_2048;
- long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
- long certIssuerSz = (long)sizeof_client_cert_der_2048;
- #endif
- byte sn[16];
- int snSz = sizeof(sn);
- /* Set X509_NAME fields */
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
- (byte*)"US", 2, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
- (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
- /* Get private and public keys */
- AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
- clientKeySz));
- AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
- AssertNotNull(x509 = X509_new());
- /* Set version 3 */
- AssertIntNE(X509_set_version(x509, 2L), 0);
- /* Set subject name, add pubkey, and sign certificate */
- AssertIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
- X509_NAME_free(name);
- AssertIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
- #ifdef WOLFSSL_ALT_NAMES
- /* Add some subject alt names */
- AssertIntNE(wolfSSL_X509_add_altname(NULL,
- "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_add_altname(x509,
- NULL, ASN_DNS_TYPE), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_add_altname(x509,
- "sphygmomanometer",
- ASN_DNS_TYPE), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_add_altname(x509,
- "supercalifragilisticexpialidocious",
- ASN_DNS_TYPE), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_add_altname(x509,
- "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
- ASN_DNS_TYPE), SSL_SUCCESS);
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
- {
- unsigned char ip4_type[] = {127,128,0,255};
- unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
- 0xff, 0xee, 0x99, 0x88,
- 0x77, 0x66, 0x55, 0x44,
- 0x00, 0x33, 0x22, 0x11};
- AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
- sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
- sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
- }
- #endif
- #endif /* WOLFSSL_ALT_NAMES */
- /* test valid sign case */
- ret = X509_sign(x509, priv, EVP_sha256());
- /* test valid X509_sign_ctx case */
- AssertNotNull(mctx = EVP_MD_CTX_new());
- AssertIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
- AssertIntGT(X509_sign_ctx(x509, mctx), 0);
- #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
- AssertIntEQ(X509_get_ext_count(x509), 1);
- #endif
- #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
- AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
- AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
- #endif
- AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
- WOLFSSL_SUCCESS);
- DEBUG_WRITE_CERT_X509(x509, "signed.pem");
- /* Variation in size depends on ASN.1 encoding when MSB is set.
- * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
- * with the MSB set. See GenerateInteger in asn.c */
- #ifndef USE_CERT_BUFFERS_1024
- #ifndef WOLFSSL_ALT_NAMES
- /* Valid case - size should be 798-797 with 16 byte serial number */
- AssertTrue((ret == 781 + snSz) || (ret == 782 + snSz));
- #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
- /* Valid case - size should be 955-956 with 16 byte serial number */
- AssertTrue((ret == 939 + snSz) || (ret == 940 + snSz));
- #else
- /* Valid case - size should be 926-927 with 16 byte serial number */
- AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
- #endif
- #else
- #ifndef WOLFSSL_ALT_NAMES
- /* Valid case - size should be 537-538 with 16 byte serial number */
- AssertTrue((ret == 521 + snSz) || (ret == 522 + snSz));
- #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
- /* Valid case - size should be 695-696 with 16 byte serial number */
- AssertTrue((ret == 679 + snSz) || (ret == 680 + snSz));
- #else
- /* Valid case - size should be 666-667 with 16 byte serial number */
- AssertTrue((ret == 650 + snSz) || (ret == 651 + snSz));
- #endif
- #endif
- /* check that issuer name is as expected after signature */
- InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
- AssertIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
- AssertNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
- AssertNotNull(name = X509_get_subject_name(ca));
- cnSz = X509_NAME_get_sz(name);
- AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
- AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
- AssertIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
- XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
- #ifdef WOLFSSL_MULTI_ATTRIB
- /* test adding multiple OU's to the signer */
- AssertNotNull(name = X509_get_subject_name(ca));
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
- (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
- (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
- AssertIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
- #endif
- AssertNotNull(name = X509_get_subject_name(ca));
- AssertIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
- AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
- AssertNotNull(name = X509_get_issuer_name(x509));
- cnSz = X509_NAME_get_sz(name);
- AssertNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
- AssertNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
- /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
- AssertIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
- XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
- FreeDecodedCert(&dCert);
- /* Test invalid parameters */
- AssertIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
- AssertIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
- AssertIntEQ(X509_sign(x509, priv, NULL), 0);
- AssertIntEQ(X509_sign_ctx(NULL, mctx), 0);
- EVP_MD_CTX_free(mctx);
- AssertNotNull(mctx = EVP_MD_CTX_new());
- AssertIntEQ(X509_sign_ctx(x509, mctx), 0);
- AssertIntEQ(X509_sign_ctx(x509, NULL), 0);
- /* test invalid version number */
- #if defined(OPENSSL_ALL)
- AssertIntNE(X509_set_version(x509, 6L), 0);
- AssertIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
- /* uses ParseCert which fails on bad version number */
- AssertIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
- #endif
- EVP_MD_CTX_free(mctx);
- EVP_PKEY_free(priv);
- EVP_PKEY_free(pub);
- X509_free(x509);
- X509_free(ca);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_get0_tbs_sigalg(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
- X509* x509 = NULL;
- const X509_ALGOR* alg;
- AssertNotNull(x509 = X509_new());
- AssertNull(alg = X509_get0_tbs_sigalg(NULL));
- AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_ALGOR_get0(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
- !defined(NO_SHA256) && !defined(NO_RSA)
- X509* x509 = NULL;
- const ASN1_OBJECT* obj = NULL;
- const X509_ALGOR* alg;
- int pptype = 0;
- const void *ppval = NULL;
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM));
- AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
- /* Invalid case */
- X509_ALGOR_get0(&obj, NULL, NULL, NULL);
- AssertNull(obj);
- /* Valid case */
- X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
- AssertNotNull(obj);
- AssertNull(ppval);
- AssertIntNE(pptype, 0);
- /* Make sure NID of X509_ALGOR is Sha256 with RSA */
- AssertIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_VERIFY_PARAM(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- X509_VERIFY_PARAM *paramTo;
- X509_VERIFY_PARAM *paramFrom;
- int ret;
- char testIPv4[] = "127.0.0.1";
- char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
- char testhostName1[] = "foo.hoge.com";
- char testhostName2[] = "foobar.hoge.com";
- paramTo = X509_VERIFY_PARAM_new();
- AssertNotNull(paramTo);
- XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM ));
- paramFrom = X509_VERIFY_PARAM_new();
- AssertNotNull(paramFrom);
- XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM ));
- ret = X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
- (int)XSTRLEN(testhostName1));
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
- (int)XSTRLEN(testhostName1)));
- X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
- X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
- AssertIntEQ(0x01, paramFrom->hostFlags);
- ret = X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
- AssertIntEQ(0, ret);
- ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
- ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
- AssertIntEQ(1, ret);
- ret = X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
- /* null pointer */
- ret = X509_VERIFY_PARAM_set1(NULL, paramFrom);
- AssertIntEQ(WOLFSSL_FAILURE, ret);
- /* in the case of "from" null, returns success */
- ret = X509_VERIFY_PARAM_set1(paramTo, NULL);
- AssertIntEQ(WOLFSSL_SUCCESS, ret);
- ret = X509_VERIFY_PARAM_set1(NULL, NULL);
- AssertIntEQ(WOLFSSL_FAILURE, ret);
- /* inherit flags test : VPARAM_DEFAULT */
- ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
- (int)XSTRLEN(testhostName1)));
- AssertIntEQ(0x01, paramTo->hostFlags);
- AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
- /* inherit flags test : VPARAM OVERWRITE */
- X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
- (int)XSTRLEN(testhostName2));
- X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
- X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
- paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
- ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
- (int)XSTRLEN(testhostName1)));
- AssertIntEQ(0x01, paramTo->hostFlags);
- AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
- /* inherit flags test : VPARAM_RESET_FLAGS */
- X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
- (int)XSTRLEN(testhostName2));
- X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
- X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
- paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
- ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
- (int)XSTRLEN(testhostName1)));
- AssertIntEQ(0x01, paramTo->hostFlags);
- AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
- /* inherit flags test : VPARAM_LOCKED */
- X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
- (int)XSTRLEN(testhostName2));
- X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
- X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
- paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
- ret = X509_VERIFY_PARAM_set1(paramTo, paramFrom);
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
- (int)XSTRLEN(testhostName2)));
- AssertIntEQ(0x00, paramTo->hostFlags);
- AssertIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
- /* test for incorrect parameters */
- ret = X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL );
- AssertIntEQ(0, ret);
- ret = X509_VERIFY_PARAM_set_flags(NULL, 0 );
- AssertIntEQ(0, ret);
- /* inherit flags test : VPARAM_ONCE, not testable yet */
- ret = X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
- AssertIntEQ(1, ret);
- ret = X509_VERIFY_PARAM_get_flags(paramTo);
- AssertIntEQ(X509_V_FLAG_CRL_CHECK_ALL, ret);
- ret = X509_VERIFY_PARAM_clear_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL);
- AssertIntEQ(1, ret);
- ret = X509_VERIFY_PARAM_get_flags(paramTo);
- AssertIntEQ(0, ret);
- X509_VERIFY_PARAM_free(paramTo);
- X509_VERIFY_PARAM_free(paramFrom);
- X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- static int test_wolfSSL_check_domain_verify_count = 0;
- static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
- WOLFSSL_X509_STORE_CTX* store)
- {
- AssertIntEQ(X509_STORE_CTX_get_error(store), 0);
- AssertIntEQ(preverify, 1);
- test_wolfSSL_check_domain_verify_count++;
- return 1;
- }
- static void test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
- {
- X509_VERIFY_PARAM *param = SSL_get0_param(ssl);
- /* Domain check should only be done on the leaf cert */
- X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
- AssertIntEQ(X509_VERIFY_PARAM_set1_host(param,
- "wolfSSL Server Chain", 0), 1);
- wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
- test_wolfSSL_check_domain_verify_cb);
- }
- static void test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
- {
- /* Use a cert with different domains in chain */
- AssertIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
- "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
- }
- static int test_wolfSSL_check_domain(void)
- {
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- callback_functions func_cb_client;
- callback_functions func_cb_server;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
- XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
- func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
- client_args.callbacks = &func_cb_client;
- server_args.callbacks = &func_cb_server;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- /* Should have been called once for each cert in sent chain */
- #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
- AssertIntEQ(test_wolfSSL_check_domain_verify_count, 3);
- #else
- AssertIntEQ(test_wolfSSL_check_domain_verify_count, 1);
- #endif
- return TEST_RES_CHECK(1);
- }
- #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
- static int test_wolfSSL_X509_get_X509_PUBKEY(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
- X509* x509 = NULL;
- X509_PUBKEY* pubKey;
- AssertNotNull(x509 = X509_new());
- AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
- AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_PUBKEY_RSA(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
- !defined(NO_SHA256) && !defined(NO_RSA)
- X509* x509 = NULL;
- ASN1_OBJECT* obj = NULL;
- const ASN1_OBJECT* pa_oid = NULL;
- X509_PUBKEY* pubKey;
- X509_PUBKEY* pubKey2;
- EVP_PKEY* evpKey;
- const unsigned char *pk;
- int ppklen, pptype;
- X509_ALGOR *pa;
- const void *pval;
- AssertNotNull(x509 = X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM));
- AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
- AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
- AssertNotNull(pk);
- AssertNotNull(pa);
- AssertNotNull(pubKey);
- AssertIntGT(ppklen, 0);
- AssertIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
- AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
- AssertNotNull(pubKey2 = X509_PUBKEY_new());
- AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
- AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
- AssertNotNull(pk);
- AssertNotNull(pa);
- AssertIntGT(ppklen, 0);
- X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
- AssertNotNull(pa_oid);
- AssertNull(pval);
- AssertIntEQ(pptype, V_ASN1_NULL);
- AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
- X509_PUBKEY_free(pubKey2);
- X509_free(x509);
- EVP_PKEY_free(evpKey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_PUBKEY_EC(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
- X509* x509 = NULL;
- ASN1_OBJECT* obj = NULL;
- ASN1_OBJECT* poid;
- const ASN1_OBJECT* pa_oid = NULL;
- X509_PUBKEY* pubKey;
- X509_PUBKEY* pubKey2;
- EVP_PKEY* evpKey;
- const unsigned char *pk;
- int ppklen, pptype;
- X509_ALGOR *pa;
- const void *pval;
- char buf[50];
- AssertNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
- SSL_FILETYPE_PEM));
- AssertNotNull(pubKey = X509_get_X509_PUBKEY(x509));
- AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
- AssertNotNull(pubKey2 = X509_PUBKEY_new());
- AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
- AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
- AssertNotNull(pk);
- AssertNotNull(pa);
- AssertIntGT(ppklen, 0);
- X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
- AssertNotNull(pa_oid);
- AssertNotNull(pval);
- AssertIntEQ(pptype, V_ASN1_OBJECT);
- AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
- poid = (ASN1_OBJECT *)pval;
- AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
- AssertIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
- X509_PUBKEY_free(pubKey2);
- X509_free(x509);
- EVP_PKEY_free(evpKey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_PUBKEY_DSA(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
- word32 bytes;
- #ifdef USE_CERT_BUFFERS_1024
- byte tmp[ONEK_BUF];
- #elif defined(USE_CERT_BUFFERS_2048)
- byte tmp[TWOK_BUF];
- #else
- byte tmp[TWOK_BUF];
- #endif /* END USE_CERT_BUFFERS_1024 */
- const unsigned char* dsaKeyDer = tmp;
- ASN1_OBJECT* obj = NULL;
- ASN1_STRING* str;
- const ASN1_OBJECT* pa_oid = NULL;
- X509_PUBKEY* pubKey = NULL;
- EVP_PKEY* evpKey = NULL;
- const unsigned char *pk;
- int ppklen, pptype;
- X509_ALGOR *pa;
- const void *pval;
- #ifdef USE_CERT_BUFFERS_1024
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
- bytes = sizeof_dsa_key_der_1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
- bytes = sizeof_dsa_key_der_2048;
- #else
- {
- XFILE fp;
- XMEMSET(tmp, 0, sizeof(tmp));
- fp = XFOPEN("./certs/dsa2048.der", "rb");
- if (fp == XBADFILE) {
- return WOLFSSL_BAD_FILE;
- }
- bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
- XFCLOSE(fp);
- }
- #endif
- /* Initialize pkey with der format dsa key */
- AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
- AssertNotNull(pubKey = X509_PUBKEY_new());
- AssertIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
- AssertIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
- AssertNotNull(pk);
- AssertNotNull(pa);
- AssertIntGT(ppklen, 0);
- X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
- AssertNotNull(pa_oid);
- AssertNotNull(pval);
- AssertIntEQ(pptype, V_ASN1_SEQUENCE);
- AssertIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
- str = (ASN1_STRING *)pval;
- DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
- #ifdef USE_CERT_BUFFERS_1024
- AssertIntEQ(ASN1_STRING_length(str), 291);
- #else
- AssertIntEQ(ASN1_STRING_length(str), 549);
- #endif /* END USE_CERT_BUFFERS_1024 */
- X509_PUBKEY_free(pubKey);
- EVP_PKEY_free(evpKey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RAND(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- byte seed[16];
- XMEMSET(seed, 0, sizeof(seed));
- RAND_seed(seed, sizeof(seed));
- AssertIntEQ(RAND_poll(), 1);
- RAND_cleanup();
- AssertIntEQ(RAND_egd(NULL), -1);
- #ifndef NO_FILESYSTEM
- {
- char fname[100];
- AssertNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
- AssertIntEQ(RAND_write_file(NULL), 0);
- }
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BUF(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- BUF_MEM* buf;
- AssertNotNull(buf = BUF_MEM_new());
- AssertIntEQ(BUF_MEM_grow(buf, 10), 10);
- AssertIntEQ(BUF_MEM_grow(buf, -1), 0);
- BUF_MEM_free(buf);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
- static int stub_rand_seed(const void *buf, int num)
- {
- (void)buf;
- (void)num;
- return 123;
- }
- static int stub_rand_bytes(unsigned char *buf, int num)
- {
- (void)buf;
- (void)num;
- return 456;
- }
- static byte* was_stub_rand_cleanup_called(void)
- {
- static byte was_called = 0;
- return &was_called;
- }
- static void stub_rand_cleanup(void)
- {
- byte* was_called = was_stub_rand_cleanup_called();
- *was_called = 1;
- return;
- }
- static byte* was_stub_rand_add_called(void)
- {
- static byte was_called = 0;
- return &was_called;
- }
- static int stub_rand_add(const void *buf, int num, double entropy)
- {
- byte* was_called = was_stub_rand_add_called();
- (void)buf;
- (void)num;
- (void)entropy;
- *was_called = 1;
- return 0;
- }
- static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
- {
- (void)buf;
- (void)num;
- return 9876;
- }
- static int stub_rand_status(void)
- {
- return 5432;
- }
- #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
- static int test_wolfSSL_RAND_set_rand_method(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
- RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
- unsigned char* buf = NULL;
- int num = 0;
- double entropy = 0;
- byte* was_cleanup_called = was_stub_rand_cleanup_called();
- byte* was_add_called = was_stub_rand_add_called();
- buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- AssertIntNE(wolfSSL_RAND_status(), 5432);
- AssertIntEQ(*was_cleanup_called, 0);
- RAND_cleanup();
- AssertIntEQ(*was_cleanup_called, 0);
- rand_methods.seed = &stub_rand_seed;
- rand_methods.bytes = &stub_rand_bytes;
- rand_methods.cleanup = &stub_rand_cleanup;
- rand_methods.add = &stub_rand_add;
- rand_methods.pseudorand = &stub_rand_pseudo_bytes;
- rand_methods.status = &stub_rand_status;
- AssertIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
- AssertIntEQ(RAND_seed(buf, num), 123);
- AssertIntEQ(RAND_bytes(buf, num), 456);
- AssertIntEQ(RAND_pseudo_bytes(buf, num), 9876);
- AssertIntEQ(RAND_status(), 5432);
- AssertIntEQ(*was_add_called, 0);
- /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
- RAND_add(buf, num, entropy);
- AssertIntEQ(*was_add_called, 1);
- was_add_called = 0;
- AssertIntEQ(*was_cleanup_called, 0);
- RAND_cleanup();
- AssertIntEQ(*was_cleanup_called, 1);
- *was_cleanup_called = 0;
- AssertIntEQ(RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
- AssertIntNE(RAND_status(), 5432);
- AssertIntEQ(*was_cleanup_called, 0);
- RAND_cleanup();
- AssertIntEQ(*was_cleanup_called, 0);
- XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
- return res;
- }
- static int test_wolfSSL_RAND_bytes(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
- const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
- const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
- const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
- int max_bufsize;
- byte *my_buf;
- /* sanity check */
- AssertIntEQ(RAND_bytes(NULL, 16), 0);
- AssertIntEQ(RAND_bytes(NULL, 0), 0);
- max_bufsize = size4;
- my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- AssertIntEQ(RAND_bytes(my_buf, 0), 1);
- AssertIntEQ(RAND_bytes(my_buf, -1), 0);
- AssertNotNull(my_buf);
- XMEMSET(my_buf, 0, max_bufsize);
- AssertIntEQ(RAND_bytes(my_buf, size1), 1);
- AssertIntEQ(RAND_bytes(my_buf, size2), 1);
- AssertIntEQ(RAND_bytes(my_buf, size3), 1);
- AssertIntEQ(RAND_bytes(my_buf, size4), 1);
- XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BN_rand(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- BIGNUM* bn;
- BIGNUM* range;
- /* Error conditions. */
- /* NULL BN. */
- AssertIntEQ(BN_rand(NULL, 0, 0, 0), SSL_FAILURE);
- AssertNotNull(bn = BN_new());
- /* Negative bits. */
- AssertIntEQ(BN_rand(bn, -2, 0, 0), SSL_FAILURE);
- /* 0 bits and top is not -1. */
- AssertIntEQ(BN_rand(bn, 0, 1, 0), SSL_FAILURE);
- /* 0 bits and bottom is not 0. */
- AssertIntEQ(BN_rand(bn, 0, 0, 1), SSL_FAILURE);
- /* 1 bit and top is 1. */
- AssertIntEQ(BN_rand(bn, 1, 1, 0), SSL_FAILURE);
- AssertIntEQ(BN_rand(bn, 0, -1, 0), SSL_SUCCESS);
- AssertIntEQ(BN_num_bits(bn), 0);
- AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
- AssertIntEQ(BN_num_bits(bn), 8);
- /* When top is 0, top bit should be 1. */
- AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
- AssertIntEQ(BN_rand(bn, 8, 1, 0), SSL_SUCCESS);
- /* When top is 1, top 2 bits should be 1. */
- AssertIntEQ(BN_is_bit_set(bn, 7), SSL_SUCCESS);
- AssertIntEQ(BN_is_bit_set(bn, 6), SSL_SUCCESS);
- AssertIntEQ(BN_rand(bn, 8, 0, 1), SSL_SUCCESS);
- /* When bottom is 1, bottom bit should be 1. */
- AssertIntEQ(BN_is_bit_set(bn, 0), SSL_SUCCESS);
- /* Regression test: Older versions of wolfSSL_BN_rand would round the
- * requested number of bits up to the nearest multiple of 8. E.g. in this
- * case, requesting a 13-bit random number would actually return a 16-bit
- * random number. */
- AssertIntEQ(BN_rand(bn, 13, 0, 0), SSL_SUCCESS);
- AssertIntEQ(BN_num_bits(bn), 13);
- AssertNotNull(range = BN_new());
- AssertIntEQ(BN_rand(range, 64, 0, 0), SSL_SUCCESS);
- AssertIntEQ(BN_rand_range(bn, range), SSL_SUCCESS);
- BN_free(bn);
- BN_free(range);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_pseudo_rand(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- BIGNUM* bn;
- unsigned char bin[8];
- int i;
- /* BN_pseudo_rand returns 1 on success 0 on failure
- * int BN_pseudo_rand(BIGNUM* bn, int bits, int top, int bottom) */
- for (i = 0; i < 10; i++) {
- AssertNotNull(bn = BN_new());
- AssertIntEQ(BN_pseudo_rand(bn, 8, 0, 0), SSL_SUCCESS);
- AssertIntGT(BN_bn2bin(bn, bin),0);
- AssertIntEQ((bin[0] & 0x80), 0x80); /* top bit should be set */
- BN_free(bn);
- }
- for (i = 0; i < 10; i++) {
- AssertNotNull(bn = BN_new());
- AssertIntEQ(BN_pseudo_rand(bn, 8, 1, 1), SSL_SUCCESS);
- AssertIntGT(BN_bn2bin(bn, bin),0);
- AssertIntEQ((bin[0] & 0xc1), 0xc1); /* top bit should be set */
- BN_free(bn);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PKCS8_Compat(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
- #ifndef NO_BIO
- PKCS8_PRIV_KEY_INFO* pt;
- BIO* bio;
- XFILE f;
- int bytes;
- char pkcs8_buffer[512];
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
- EVP_PKEY *pkey = NULL;
- #endif
- /* file from wolfssl/certs/ directory */
- f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb");
- AssertTrue(f != XBADFILE);
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)), 0);
- XFCLOSE(f);
- AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
- AssertNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
- AssertNotNull(pkey = EVP_PKCS82PKEY(pt));
- AssertIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
- /* gets PKCS8 pointer to pkey */
- AssertNotNull(EVP_PKEY2PKCS8(pkey));
- EVP_PKEY_free(pkey);
- #endif
- BIO_free(bio);
- PKCS8_PRIV_KEY_INFO_free(pt);
- res = TEST_RES_CHECK(1);
- #endif
- #endif
- return res;
- }
- static int test_wolfSSL_PKCS8_d2i(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
- /* This test ends up using HMAC as a part of PBKDF2, and HMAC
- * requires a 12 byte password in FIPS mode. This test ends up
- * trying to use an 8 byte password. */
- #ifndef NO_FILESYSTEM
- unsigned char pkcs8_buffer[2048];
- const unsigned char* p;
- int bytes;
- XFILE file;
- WOLFSSL_EVP_PKEY* pkey = NULL;
- #ifndef NO_BIO
- BIO* bio;
- #if defined(OPENSSL_ALL) && \
- ((!defined(NO_RSA) && !defined(NO_DES3)) || \
- defined(HAVE_ECC)) && \
- !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
- WOLFSSL_EVP_PKEY* evpPkey = NULL;
- #endif
- #endif
- #ifndef NO_RSA
- const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
- const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
- #ifndef NO_DES3
- const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
- #endif
- #endif /* NO_RSA */
- #ifdef HAVE_ECC
- const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
- const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
- #ifndef NO_DES3
- const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
- #endif
- #endif /* HAVE_ECC */
- #endif /* !NO_FILESYSTEM */
- #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
- #ifndef NO_RSA
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* rsa = (unsigned char*)server_key_der_1024;
- int rsaSz = sizeof_server_key_der_1024;
- #else
- const unsigned char* rsa = (unsigned char*)server_key_der_2048;
- int rsaSz = sizeof_server_key_der_2048;
- #endif
- #endif
- #ifdef HAVE_ECC
- const unsigned char* ec = (unsigned char*)ecc_key_der_256;
- int ecSz = sizeof_ecc_key_der_256;
- #endif
- #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
- #ifndef NO_FILESYSTEM
- (void)pkcs8_buffer;
- (void)p;
- (void)bytes;
- (void)file;
- #ifndef NO_BIO
- (void)bio;
- #endif
- #endif
- #ifdef OPENSSL_ALL
- #ifndef NO_RSA
- /* Try to auto-detect normal RSA private key */
- AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
- EVP_PKEY_free(pkey);
- #endif
- #ifdef HAVE_ECC
- /* Try to auto-detect normal EC private key */
- AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
- EVP_PKEY_free(pkey);
- #endif
- #endif /* OPENSSL_ALL */
- #ifndef NO_FILESYSTEM
- #ifndef NO_RSA
- /* Get DER encoded RSA PKCS#8 data. */
- file = XFOPEN(rsaDerPkcs8File, "rb");
- AssertTrue(file != XBADFILE);
- XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- p = pkcs8_buffer;
- #ifdef OPENSSL_ALL
- /* Try to decode - auto-detect key type. */
- AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
- #else
- AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
- #endif
- /* Get PEM encoded RSA PKCS#8 data. */
- file = XFOPEN(rsaPemPkcs8File, "rb");
- AssertTrue(file != XBADFILE);
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- #if defined(OPENSSL_ALL) && \
- !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- /* Write PKCS#8 PEM to BIO. */
- AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
- NULL), bytes);
- /* Compare file and written data */
- AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
- AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
- BIO_free(bio);
- #if !defined(NO_DES3) && !defined(NO_SHA)
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- /* Write Encrypted PKCS#8 PEM to BIO. */
- bytes = 1834;
- AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
- NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
- AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
- (void*)"yassl123"));
- EVP_PKEY_free(evpPkey);
- BIO_free(bio);
- #endif /* !NO_DES3 && !NO_SHA */
- #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
- EVP_PKEY_free(pkey);
- /* PKCS#8 encrypted RSA key */
- #ifndef NO_DES3
- file = XFOPEN(rsaDerPkcs8EncFile, "rb");
- AssertTrue(file != XBADFILE);
- XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- #if defined(OPENSSL_ALL) && \
- !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
- AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
- AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
- (void*)"yassl123"));
- EVP_PKEY_free(pkey);
- BIO_free(bio);
- #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
- #endif /* !NO_DES3 */
- #endif /* NO_RSA */
- #ifdef HAVE_ECC
- /* PKCS#8 encode EC key */
- file = XFOPEN(ecDerPkcs8File, "rb");
- AssertTrue(file != XBADFILE);
- XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- p = pkcs8_buffer;
- #ifdef OPENSSL_ALL
- /* Try to decode - auto-detect key type. */
- AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
- #else
- AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
- #endif
- /* Get PEM encoded RSA PKCS#8 data. */
- file = XFOPEN(ecPemPkcs8File, "rb");
- AssertTrue(file != XBADFILE);
- XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- #if defined(OPENSSL_ALL) && \
- !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
- defined(HAVE_AES_CBC)
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- /* Write PKCS#8 PEM to BIO. */
- AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
- NULL), bytes);
- /* Compare file and written data */
- AssertIntEQ(BIO_get_mem_data(bio, &p), bytes);
- AssertIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
- BIO_free(bio);
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- /* Write Encrypted PKCS#8 PEM to BIO. */
- bytes = 379;
- AssertIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
- NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
- AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
- (void*)"yassl123"));
- EVP_PKEY_free(evpPkey);
- BIO_free(bio);
- #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
- EVP_PKEY_free(pkey);
- /* PKCS#8 encrypted EC key */
- #ifndef NO_DES3
- file = XFOPEN(ecDerPkcs8EncFile, "rb");
- AssertTrue(file != XBADFILE);
- XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer));
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- #if defined(OPENSSL_ALL) && \
- !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
- AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
- AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
- (void*)"yassl123"));
- EVP_PKEY_free(pkey);
- BIO_free(bio);
- #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
- #endif /* !NO_DES3 */
- #endif /* HAVE_ECC */
- #endif /* !NO_FILESYSTEM */
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_FIPS && OPENSSL_EXTRA */
- return res;
- }
- #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
- defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
- #define LOGGING_THREADS 5
- #define ERROR_COUNT 10
- /* copied from logging.c since this is not exposed otherwise */
- #ifndef ERROR_QUEUE_MAX
- #ifdef ERROR_QUEUE_PER_THREAD
- #define ERROR_QUEUE_MAX 16
- #else
- /* this breaks from compat of unlimited error queue size */
- #define ERROR_QUEUE_MAX 100
- #endif
- #endif
- static volatile int loggingThreadsReady;
- static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
- {
- const char* file;
- int line;
- unsigned long err;
- int errorCount = 0;
- int i;
- (void)args;
- while (!loggingThreadsReady);
- for (i = 0; i < ERROR_COUNT; i++)
- ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
- while ((err = ERR_get_error_line(&file, &line))) {
- AssertIntEQ(err, 990 + errorCount);
- errorCount++;
- }
- AssertIntEQ(errorCount, ERROR_COUNT);
- /* test max queue behavior, trying to add an arbitrary 3 errors over */
- ERR_clear_error(); /* ERR_get_error_line() does not remove */
- errorCount = 0;
- for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
- ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
- while ((err = ERR_get_error_line(&file, &line))) {
- AssertIntEQ(err, 990 + errorCount);
- errorCount++;
- }
- /* test that the 3 errors over the max were dropped */
- AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
- return 0;
- }
- #endif
- static int test_error_queue_per_thread(void)
- {
- int res = TEST_SKIPPED;
- #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
- defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
- THREAD_TYPE loggingThreads[LOGGING_THREADS];
- int i;
- ERR_clear_error(); /* clear out any error nodes */
- loggingThreadsReady = 0;
- for (i = 0; i < LOGGING_THREADS; i++)
- start_thread(test_logging, NULL, &loggingThreads[i]);
- loggingThreadsReady = 1;
- for (i = 0; i < LOGGING_THREADS; i++)
- join_thread(loggingThreads[i]);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ERR_put_error(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
- defined(DEBUG_WOLFSSL)
- const char* file;
- int line;
- ERR_clear_error(); /* clear out any error nodes */
- ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
- AssertIntEQ(ERR_get_error_line(&file, &line), 0);
- ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
- AssertIntEQ(ERR_get_error_line(&file, &line), 1);
- ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
- AssertIntEQ(ERR_get_error_line(&file, &line), 2);
- ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
- AssertIntEQ(ERR_get_error_line(&file, &line), 3);
- ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
- AssertIntEQ(ERR_get_error_line(&file, &line), 4);
- ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
- AssertIntEQ(ERR_get_error_line(&file, &line), 5);
- ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
- AssertIntEQ(ERR_get_error_line(&file, &line), 6);
- ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
- AssertIntEQ(ERR_get_error_line(&file, &line), 7);
- ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
- AssertIntEQ(ERR_get_error_line(&file, &line), 8);
- ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
- AssertIntEQ(ERR_get_error_line(&file, &line), 9);
- ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
- AssertIntEQ(ERR_get_error_line(&file, &line), 10);
- ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
- AssertIntEQ(ERR_get_error_line(&file, &line), 11);
- ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
- AssertIntEQ(ERR_get_error_line(&file, &line), 12);
- ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
- AssertIntEQ(ERR_get_error_line(&file, &line), 13);
- ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
- AssertIntEQ(ERR_get_error_line(&file, &line), 14);
- ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
- AssertIntEQ(ERR_get_error_line(&file, &line), 15);
- #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
- ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
- "this file", 100);
- AssertIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
- (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
- AssertIntEQ(line, 100);
- AssertIntEQ(wolfSSL_ERR_peek_error(),
- (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
- AssertIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
- #endif
- /* try reading past end of error queue */
- file = NULL;
- AssertIntEQ(ERR_get_error_line(&file, &line), 0);
- AssertNull(file);
- AssertIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
- PEMerr(4,4);
- AssertIntEQ(ERR_get_error(), 4);
- /* Empty and free up all error nodes */
- ERR_clear_error();
- /* Verify all nodes are cleared */
- ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
- ERR_clear_error();
- AssertIntEQ(ERR_get_error_line(&file, &line), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*
- * This is a regression test for a bug where the peek/get error functions were
- * drawing from the end of the queue rather than the front.
- */
- static int test_wolfSSL_ERR_get_error_order(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_HAVE_ERROR_QUEUE
- /* Empty the queue. */
- wolfSSL_ERR_clear_error();
- wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
- wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
- AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
- AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
- AssertIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
- AssertIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
- res = TEST_RES_CHECK(1);
- #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
- return res;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_ERR_print_errors(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
- defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
- BIO* bio;
- char buf[1024];
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- ERR_clear_error(); /* clear out any error nodes */
- ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
- /* Choosing -299 as an unused errno between MIN_CODE_E < x < WC_LAST_E. */
- ERR_put_error(0,SYS_F_BIND, -299, "asn.c", 100);
- ERR_print_errors(bio);
- AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
- AssertIntEQ(XSTRNCMP("error:173:wolfSSL library:Bad function argument:ssl.c:0",
- buf, 55), 0);
- AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
- AssertIntEQ(XSTRNCMP("error:299:wolfSSL library:unknown error number:asn.c:100",
- buf, 56), 0);
- AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
- AssertIntEQ(buf[0], '\0');
- AssertIntEQ(ERR_get_error_line(NULL, NULL), 0);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
- defined(DEBUG_WOLFSSL)
- static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
- {
- wolfSSL_BIO_write((BIO*)u, str, (int)len);
- return 0;
- }
- #endif
- static int test_wolfSSL_ERR_print_errors_cb(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
- defined(DEBUG_WOLFSSL)
- BIO* bio;
- char buf[1024];
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- ERR_clear_error(); /* clear out any error nodes */
- ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
- ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
- ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
- AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
- AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 173 line:0 file:ssl.c",
- buf, 53), 0);
- AssertIntEQ(XSTRNCMP("wolfSSL error occurred, error = 275 line:100 file:asn.c",
- buf + 53, 55), 0);
- AssertIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*
- * Testing WOLFSSL_ERROR_MSG
- */
- static int test_WOLFSSL_ERROR_MSG(void)
- {
- int res = TEST_SKIPPED;
- #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
- defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
- const char* msg = TEST_STRING;
- WOLFSSL_ERROR_MSG(msg);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }/*End test_WOLFSSL_ERROR_MSG*/
- /*
- * Testing wc_ERR_remove_state
- */
- static int test_wc_ERR_remove_state(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
- wc_ERR_remove_state();
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }/*End test_wc_ERR_remove_state*/
- /*
- * Testing wc_ERR_print_errors_fp
- */
- static int test_wc_ERR_print_errors_fp(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
- (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
- long sz;
- XFILE fp;
- int ret = 0;
- WOLFSSL_ERROR(BAD_FUNC_ARG);
- fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar");
- wc_ERR_print_errors_fp(fp);
- #if defined(DEBUG_WOLFSSL)
- AssertTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
- sz = XFTELL(fp);
- #ifdef NO_ERROR_QUEUE
- /* File should be empty when NO_ERROR_QUEUE is defined */
- if (sz != 0) {
- ret = BAD_FUNC_ARG;
- }
- #else
- if (sz == 0) {
- ret = BAD_FUNC_ARG;
- }
- #endif
- #endif
- XFCLOSE(fp);
- (void)sz;
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/*End test_wc_ERR_print_errors_fp*/
- #ifdef DEBUG_WOLFSSL
- static void Logging_cb(const int logLevel, const char *const logMessage)
- {
- (void)logLevel;
- (void)logMessage;
- }
- #endif
- /*
- * Testing wolfSSL_GetLoggingCb
- */
- static int test_wolfSSL_GetLoggingCb(void)
- {
- int ret = 0;
- #ifdef DEBUG_WOLFSSL
- /* Testing without wolfSSL_SetLoggingCb() */
- if (ret == 0) {
- if (wolfSSL_GetLoggingCb() == NULL) { /* Should be true */
- ret = 0;
- }
- if (wolfSSL_GetLoggingCb() != NULL) { /* Should not be true */
- ret = -1;
- }
- }
- /* Testing with wolfSSL_SetLoggingCb() */
- if (ret == 0) {
- ret = wolfSSL_SetLoggingCb(Logging_cb);
- if (ret == 0) {
- if (wolfSSL_GetLoggingCb() == NULL) { /* Should not be true */
- ret = -1;
- }
- if (ret == 0) {
- if (wolfSSL_GetLoggingCb() == Logging_cb) { /* Should be true */
- ret = 0;
- }
- }
- /* reset logging callback */
- wolfSSL_SetLoggingCb(NULL);
- }
- }
- #endif
- if (ret == 0) {
- if (wolfSSL_GetLoggingCb() != NULL) {
- ret = -1;
- }
- }
- return TEST_RES_CHECK(ret == 0);
- }/*End test_wolfSSL_GetLoggingCb*/
- #endif /* !NO_BIO */
- #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
- defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
- defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
- static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
- {
- static const unsigned char key[] = "simple test key";
- HMAC_CTX* hmac;
- ENGINE* e = NULL;
- unsigned char hash[WC_MAX_DIGEST_SIZE];
- unsigned int len;
- AssertNotNull(hmac = HMAC_CTX_new());
- HMAC_CTX_init(hmac);
- AssertIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e),
- SSL_SUCCESS);
- /* re-using test key as data to hash */
- AssertIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(hmac, NULL, 0), SSL_SUCCESS);
- AssertIntEQ(HMAC_Final(hmac, hash, &len), SSL_SUCCESS);
- AssertIntEQ(len, md_len);
- AssertIntEQ(HMAC_size(hmac), md_len);
- AssertStrEQ(HMAC_CTX_get_md(hmac), md);
- HMAC_cleanup(hmac);
- HMAC_CTX_free(hmac);
- len = 0;
- AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
- AssertIntEQ(len, md_len);
- return 0;
- }
- #endif
- static int test_wolfSSL_HMAC(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
- defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
- defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
- #ifndef NO_SHA256
- test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE);
- #endif
- #ifdef WOLFSSL_SHA224
- test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE);
- #endif
- #ifdef WOLFSSL_SHA384
- test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE);
- #endif
- #ifdef WOLFSSL_SHA512
- test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE);
- #endif
- #ifdef WOLFSSL_SHA3
- #ifndef WOLFSSL_NOSHA3_224
- test_openssl_hmac(EVP_sha3_224(), (int)WC_SHA3_224_DIGEST_SIZE);
- #endif
- #ifndef WOLFSSL_NOSHA3_256
- test_openssl_hmac(EVP_sha3_256(), (int)WC_SHA3_256_DIGEST_SIZE);
- #endif
- #ifndef WOLFSSL_NOSHA3_384
- test_openssl_hmac(EVP_sha3_384(), (int)WC_SHA3_384_DIGEST_SIZE);
- #endif
- #ifndef WOLFSSL_NOSHA3_512
- test_openssl_hmac(EVP_sha3_512(), (int)WC_SHA3_512_DIGEST_SIZE);
- #endif
- #endif
- #ifndef NO_SHA
- test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CMAC(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_AES_DIRECT)
- int i;
- byte key[AES_128_KEY_SIZE];
- CMAC_CTX* cmacCtx = NULL;
- byte out[AES_BLOCK_SIZE];
- size_t outLen = AES_BLOCK_SIZE;
- for (i=0; i < AES_128_KEY_SIZE; ++i) {
- key[i] = i;
- }
- AssertNotNull(cmacCtx = CMAC_CTX_new());
- /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
- AssertNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
- AssertIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
- NULL), SSL_SUCCESS);
- /* re-using test key as data to hash */
- AssertIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), SSL_SUCCESS);
- AssertIntEQ(CMAC_Update(cmacCtx, NULL, 0), SSL_SUCCESS);
- AssertIntEQ(CMAC_Final(cmacCtx, out, &outLen), SSL_SUCCESS);
- AssertIntEQ(outLen, AES_BLOCK_SIZE);
- CMAC_CTX_free(cmacCtx);
- res = TEST_RES_CHECK(1);
- #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
- return res;
- }
- static int test_wolfSSL_OBJ(void)
- {
- /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
- * mode
- */
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
- !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
- defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO)
- ASN1_OBJECT *obj = NULL;
- ASN1_OBJECT *obj2 = NULL;
- char buf[50];
- XFILE fp;
- X509 *x509 = NULL;
- X509_NAME *x509Name;
- X509_NAME_ENTRY *x509NameEntry;
- ASN1_OBJECT *asn1Name = NULL;
- int numNames;
- BIO *bio = NULL;
- int nid;
- int i, j;
- const char *f[] = {
- #ifndef NO_RSA
- "./certs/ca-cert.der",
- #endif
- #ifdef HAVE_ECC
- "./certs/ca-ecc-cert.der",
- "./certs/ca-ecc384-cert.der",
- #endif
- NULL};
- ASN1_OBJECT *field_name_obj = NULL;
- int lastpos = -1;
- int tmp = -1;
- ASN1_STRING *asn1 = NULL;
- unsigned char *buf_dyn = NULL;
- AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
- AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
- AssertIntEQ(OBJ_obj2nid(obj), NID_any_policy);
- AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
- AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
- ASN1_OBJECT_free(obj);
- AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
- AssertIntEQ(OBJ_obj2nid(obj), NID_sha256);
- AssertIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
- #ifdef WOLFSSL_CERT_EXT
- AssertIntEQ(OBJ_txt2nid(buf), NID_sha256);
- #endif
- AssertIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
- AssertNotNull(obj2 = OBJ_dup(obj));
- AssertIntEQ(OBJ_cmp(obj, obj2), 0);
- ASN1_OBJECT_free(obj);
- ASN1_OBJECT_free(obj2);
- for (i = 0; f[i] != NULL; i++)
- {
- AssertTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
- AssertNotNull(x509 = d2i_X509_fp(fp, NULL));
- XFCLOSE(fp);
- AssertNotNull(x509Name = X509_get_issuer_name(x509));
- AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
- /* Get the Common Name by using OBJ_txt2obj */
- AssertNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
- do
- {
- lastpos = tmp;
- tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
- } while (tmp > -1);
- AssertIntNE(lastpos, -1);
- ASN1_OBJECT_free(field_name_obj);
- AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
- AssertNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
- AssertIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
- /*
- * All Common Names should be www.wolfssl.com
- * This makes testing easier as we can test for the expected value.
- */
- AssertStrEQ((char*)buf_dyn, "www.wolfssl.com");
- OPENSSL_free(buf_dyn);
- bio = BIO_new(BIO_s_mem());
- AssertTrue(bio != NULL);
- for (j = 0; j < numNames; j++)
- {
- AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
- AssertNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
- AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
- }
- BIO_free(bio);
- X509_free(x509);
- }
- #ifdef HAVE_PKCS12
- {
- PKCS12 *p12;
- int boolRet;
- EVP_PKEY *pkey = NULL;
- const char *p12_f[] = {
- #if !defined(NO_DES3) && !defined(NO_RSA)
- "./certs/test-servercert.p12",
- #endif
- NULL};
- for (i = 0; p12_f[i] != NULL; i++)
- {
- AssertTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
- AssertNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
- XFCLOSE(fp);
- AssertTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
- &pkey, &x509, NULL)) > 0);
- wc_PKCS12_free(p12);
- EVP_PKEY_free(pkey);
- x509Name = X509_get_issuer_name(x509);
- AssertNotNull(x509Name);
- AssertIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
- AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
- for (j = 0; j < numNames; j++)
- {
- AssertNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
- AssertNotNull(asn1Name =
- X509_NAME_ENTRY_get_object(x509NameEntry));
- AssertTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
- }
- BIO_free(bio);
- X509_free(x509);
- }
- }
- #endif /* HAVE_PKCS12 */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_i2a_ASN1_OBJECT(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
- ASN1_OBJECT *obj = NULL;
- BIO *bio = NULL;
- AssertNotNull(obj = OBJ_nid2obj(NID_sha256));
- AssertTrue((bio = BIO_new(BIO_s_mem())) != NULL);
- AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
- AssertIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
- AssertIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
- BIO_free(bio);
- ASN1_OBJECT_free(obj);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OBJ_cmp(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
- ASN1_OBJECT *obj = NULL;
- ASN1_OBJECT *obj2 = NULL;
- AssertNotNull(obj = OBJ_nid2obj(NID_any_policy));
- AssertNotNull(obj2 = OBJ_nid2obj(NID_sha256));
- AssertIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
- AssertIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
- AssertIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
- AssertIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
- AssertIntEQ(OBJ_cmp(obj, obj), 0);
- AssertIntEQ(OBJ_cmp(obj2, obj2), 0);
- ASN1_OBJECT_free(obj);
- ASN1_OBJECT_free(obj2);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OBJ_txt2nid(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_WOLFSSL_STUB) && defined(WOLFSSL_APACHE_HTTPD)
- int i;
- static const struct {
- const char* sn;
- const char* ln;
- const char* oid;
- int nid;
- } testVals[] = {
- { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
- { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
- NID_id_on_dnsSRV },
- { "msUPN", "Microsoft User Principal Name",
- "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
- { NULL, NULL, NULL, NID_undef }
- };
- /* Invalid cases */
- AssertIntEQ(OBJ_txt2nid(NULL), NID_undef);
- AssertIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
- /* Valid cases */
- for (i = 0; testVals[i].sn != NULL; i++) {
- AssertIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
- AssertIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
- AssertIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OBJ_txt2obj(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
- int i;
- char buf[50];
- ASN1_OBJECT* obj;
- static const struct {
- const char* oidStr;
- const char* sn;
- const char* ln;
- } objs_list[] = {
- #if defined(WOLFSSL_APACHE_HTTPD)
- { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
- { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
- #endif
- { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
- { NULL, NULL, NULL }
- };
- static const struct {
- const char* numeric;
- const char* name;
- } objs_named[] = {
- /* In dictionary but not in normal list. */
- { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
- /* Made up OID. */
- { "1.3.5.7", "1.3.5.7" },
- { NULL, NULL }
- };
- AssertNull(obj = OBJ_txt2obj("Bad name", 0));
- AssertNull(obj = OBJ_txt2obj(NULL, 0));
- for (i = 0; objs_list[i].oidStr != NULL; i++) {
- /* Test numerical value of oid (oidStr) */
- AssertNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
- /* Convert object back to text to confirm oid is correct */
- wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
- AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
- ASN1_OBJECT_free(obj);
- XMEMSET(buf, 0, sizeof(buf));
- /* Test short name (sn) */
- AssertNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
- AssertNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
- /* Convert object back to text to confirm oid is correct */
- wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
- AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
- ASN1_OBJECT_free(obj);
- XMEMSET(buf, 0, sizeof(buf));
- /* Test long name (ln) - should fail when no_name = 1 */
- AssertNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
- AssertNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
- /* Convert object back to text to confirm oid is correct */
- wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
- AssertIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
- ASN1_OBJECT_free(obj);
- XMEMSET(buf, 0, sizeof(buf));
- }
- for (i = 0; objs_named[i].numeric != NULL; i++) {
- AssertNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
- wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
- AssertIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
- wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
- AssertIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
- ASN1_OBJECT_free(obj);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_i2t_ASN1_OBJECT(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
- char buf[50] = {0};
- ASN1_OBJECT* obj;
- const char* oid = "2.5.29.19";
- const char* ln = "X509v3 Basic Constraints";
- obj = NULL;
- AssertIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), WOLFSSL_FAILURE);
- AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), WOLFSSL_FAILURE);
- AssertIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), WOLFSSL_FAILURE);
- AssertNotNull(obj = OBJ_txt2obj(oid, 0));
- XMEMSET(buf, 0, sizeof(buf));
- AssertIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
- AssertIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
- ASN1_OBJECT_free(obj);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
- return res;
- }
- static int test_wolfSSL_PEM_write_bio_X509(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
- defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
- defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
- !defined(NO_FILESYSTEM)
- /* This test contains the hard coded expected
- * lengths. Update if necessary */
- FILE* fp = NULL;
- WOLFSSL_EVP_PKEY *priv = NULL;
- BIO* input = NULL;
- BIO* output = NULL;
- X509* x509a = NULL;
- X509* x509b = NULL;
- ASN1_TIME* notBeforeA = NULL;
- ASN1_TIME* notAfterA = NULL;
- ASN1_TIME* notBeforeB = NULL;
- ASN1_TIME* notAfterB = NULL;
- int expectedLen;
- fp = XFOPEN("certs/server-key.pem", "rb");
- AssertNotNull(fp);
- priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL);
- XFCLOSE(fp);
- fp = NULL;
- AssertNotNull(priv);
- AssertNotNull(input = BIO_new_file(
- "certs/test/cert-ext-multiple.pem", "rb"));
- AssertIntEQ(wolfSSL_BIO_get_len(input), 2000);
- /* read PEM into X509 struct, get notBefore / notAfter to verify against */
- AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
- AssertNotNull(notBeforeA = X509_get_notBefore(x509a));
- AssertNotNull(notAfterA = X509_get_notAfter(x509a));
- /* write X509 back to PEM BIO; no need to sign as nothing changed. */
- AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
- /* compare length against expected */
- expectedLen = 2000;
- AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
- /* read exported X509 PEM back into struct, sanity check on export,
- * make sure notBefore/notAfter are the same and certs are identical. */
- AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
- AssertNotNull(notBeforeB = X509_get_notBefore(x509b));
- AssertNotNull(notAfterB = X509_get_notAfter(x509b));
- AssertIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
- AssertIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
- AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
- X509_free(x509b);
- /* Reset output buffer */
- BIO_free(output);
- AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
- /* Test forcing the AKID to be generated just from KeyIdentifier */
- if (x509a->authKeyIdSrc != NULL) {
- XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
- x509a->authKeyId = x509a->authKeyIdSrc;
- x509a->authKeyIdSrc = NULL;
- x509a->authKeyIdSrcSz = 0;
- }
- /* Resign to re-generate the der */
- AssertIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
- AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
- /* Check that we generate a smaller output since the AKID will
- * only contain the KeyIdentifier without any additional
- * information */
- /* Here we copy the validity struct from the original */
- expectedLen = 1688;
- AssertIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
- /* Reset buffers and x509 */
- BIO_free(input);
- BIO_free(output);
- X509_free(x509a);
- /* test CA and basicConstSet values are encoded when
- * the cert is a CA */
- AssertNotNull(input = BIO_new_file(
- "certs/server-cert.pem", "rb"));
- /* read PEM into X509 struct */
- AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
- /* write X509 back to PEM BIO; no need to sign as nothing changed */
- AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
- /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
- * values are maintained and certs are identical.*/
- AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
- AssertIntEQ(x509b->isCa, 1);
- AssertIntEQ(x509b->basicConstSet, 1);
- AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
- X509_free(x509a);
- X509_free(x509b);
- BIO_free(input);
- BIO_free(output);
- /* test CA and basicConstSet values are encoded when
- * the cert is not CA */
- AssertNotNull(input = BIO_new_file(
- "certs/client-uri-cert.pem", "rb"));
- /* read PEM into X509 struct */
- AssertNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
- /* write X509 back to PEM BIO; no need to sign as nothing changed */
- AssertNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
- /* read exported X509 PEM back into struct, ensure isCa and
- * basicConstSet values are maintained and certs are identical */
- AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
- AssertIntEQ(x509b->isCa, 0);
- AssertIntEQ(x509b->basicConstSet, 1);
- AssertIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
- wolfSSL_EVP_PKEY_free(priv);
- X509_free(x509a);
- X509_free(x509b);
- BIO_free(input);
- BIO_free(output);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_NAME_ENTRY(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
- X509* x509;
- #ifndef NO_BIO
- BIO* bio;
- #endif
- X509_NAME* nm;
- X509_NAME_ENTRY* entry;
- unsigned char cn[] = "another name to add";
- #ifdef OPENSSL_ALL
- int i, names_len;
- #endif
- AssertNotNull(x509 =
- wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
- #ifndef NO_BIO
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
- #endif
- #ifdef WOLFSSL_CERT_REQ
- {
- X509_REQ* req;
- #ifndef NO_BIO
- BIO* bReq;
- #endif
- AssertNotNull(req =
- wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
- #ifndef NO_BIO
- AssertNotNull(bReq = BIO_new(BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
- BIO_free(bReq);
- #endif
- X509_free(req);
- }
- #endif
- AssertNotNull(nm = X509_get_subject_name(x509));
- /* Test add entry */
- AssertNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
- 0x0c, cn, (int)sizeof(cn)));
- AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
- #ifdef WOLFSSL_CERT_EXT
- AssertIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
- (byte*)"support@wolfssl.com", 19, -1,
- 1), WOLFSSL_SUCCESS);
- #endif
- X509_NAME_ENTRY_free(entry);
- #ifdef WOLFSSL_CERT_REQ
- {
- unsigned char srv_pkcs9p[] = "Server";
- char* subject;
- AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
- MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
- subject = X509_NAME_oneline(nm, 0, 0);
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "\n\t%s\n", subject);
- #endif
- XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
- }
- #endif
- /* Test add entry by text */
- AssertNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
- 0x0c, cn, (int)sizeof(cn)));
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
- || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
- AssertNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
- V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
- #endif
- AssertIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
- X509_NAME_ENTRY_free(entry);
- /* Test add entry by NID */
- AssertIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
- cn, -1, -1, 0), SSL_SUCCESS);
- #ifdef OPENSSL_ALL
- /* stack of name entry */
- AssertIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
- for (i=0; i<names_len; i++) {
- AssertNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
- }
- #endif
- #ifndef NO_BIO
- BIO_free(bio);
- #endif
- X509_free(x509); /* free's nm */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_set_name(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
- X509* x509;
- X509_NAME* name;
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, 0, 1),
- WOLFSSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
- (byte*)"support@wolfssl.com", 19, -1,
- 1), WOLFSSL_SUCCESS);
- AssertNotNull(x509 = X509_new());
- AssertIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
- AssertIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
- AssertIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
- X509_free(x509);
- X509_NAME_free(name);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_ALL && !NO_CERTS */
- return res;
- }
- static int test_wolfSSL_X509_set_notAfter(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
- && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
- !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
- !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
- /* Generalized time will overflow time_t if not long */
- X509* x;
- BIO* bio;
- ASN1_TIME *asn_time, *time_check;
- const int year = 365*24*60*60;
- const int day = 24*60*60;
- const int hour = 60*60;
- const int mini = 60;
- int offset_day;
- unsigned char buf[25];
- time_t t;
- /*
- * Setup asn_time. APACHE HTTPD uses time(NULL)
- */
- t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
- offset_day = 7;
- /*
- * Free these.
- */
- asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
- AssertNotNull(asn_time);
- AssertNotNull(x = X509_new());
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- /*
- * Tests
- */
- AssertTrue(wolfSSL_X509_set_notAfter(x, asn_time));
- /* time_check is simply (ANS1_TIME*)x->notAfter */
- AssertNotNull(time_check = X509_get_notAfter(x));
- /* ANS1_TIME_check validates by checking if argument can be parsed */
- AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
- /* Convert to human readable format and compare to intended date */
- AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
- AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
- AssertIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
- /*
- * Cleanup
- */
- XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
- X509_free(x);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_set_notBefore(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
- && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
- !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
- X509* x;
- BIO* bio;
- ASN1_TIME *asn_time, *time_check;
- const int year = 365*24*60*60;
- const int day = 24*60*60;
- const int hour = 60*60;
- const int mini = 60;
- int offset_day;
- unsigned char buf[25];
- time_t t;
- /*
- * Setup asn_time. APACHE HTTPD uses time(NULL)
- */
- t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
- offset_day = 7;
- /*
- * Free these.
- */
- asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
- AssertNotNull(asn_time);
- AssertNotNull(x = X509_new());
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
- /*
- * Main Tests
- */
- AssertTrue(wolfSSL_X509_set_notBefore(x, asn_time));
- /* time_check == (ANS1_TIME*)x->notBefore */
- AssertNotNull(time_check = X509_get_notBefore(x));
- /* ANS1_TIME_check validates by checking if argument can be parsed */
- AssertIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
- /* Convert to human readable format and compare to intended date */
- AssertIntEQ(ASN1_TIME_print(bio, time_check), 1);
- AssertIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
- AssertIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
- /*
- * Cleanup
- */
- XFREE(asn_time,NULL,DYNAMIC_TYPE_OPENSSL);
- X509_free(x);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_set_version(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
- !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
- X509* x509;
- long v = 2L;
- long maxInt = INT_MAX;
- AssertNotNull(x509 = X509_new());
- /* These should pass. */
- AssertTrue(wolfSSL_X509_set_version(x509, v));
- AssertIntEQ(v, wolfSSL_X509_get_version(x509));
- /* Fail Case: When v(long) is greater than x509->version(int). */
- v = maxInt+1;
- AssertFalse(wolfSSL_X509_set_version(x509, v));
- /* Cleanup */
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_BIO_gets(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- BIO* bio;
- BIO* bio2;
- char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
- char emp[] = "";
- char bio_buffer[20];
- int bufferSz = 20;
- /* try with bad args */
- AssertNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
- /* try with real msg */
- AssertNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
- XMEMSET(bio_buffer, 0, bufferSz);
- AssertNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
- AssertNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
- AssertNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
- AssertFalse(bio2 != BIO_next(bio));
- /* make buffer filled with no terminating characters */
- XMEMSET(bio_buffer, 1, bufferSz);
- /* BIO_gets reads a line of data */
- AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
- AssertStrEQ(bio_buffer, "hello wolfSSL\n");
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
- AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
- /* check not null terminated string */
- BIO_free(bio);
- msg[0] = 0x33;
- msg[1] = 0x33;
- msg[2] = 0x33;
- AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
- AssertIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
- AssertIntEQ(bio_buffer[0], msg[0]);
- AssertIntEQ(bio_buffer[1], msg[1]);
- AssertIntNE(bio_buffer[2], msg[2]);
- BIO_free(bio);
- msg[3] = 0x33;
- bio_buffer[3] = 0x33;
- AssertNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
- AssertIntEQ(bio_buffer[0], msg[0]);
- AssertIntEQ(bio_buffer[1], msg[1]);
- AssertIntEQ(bio_buffer[2], msg[2]);
- AssertIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
- /* check reading an empty string */
- BIO_free(bio);
- AssertNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
- AssertStrEQ(emp, bio_buffer);
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
- /* check error cases */
- BIO_free(bio);
- AssertIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
- #if !defined(NO_FILESYSTEM)
- {
- BIO* f_bio;
- XFILE f;
- AssertNotNull(f_bio = BIO_new(BIO_s_file()));
- AssertIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
- f = XFOPEN(svrCertFile, "rb");
- AssertTrue((f != XBADFILE));
- AssertIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
- AssertIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
- BIO_free(f_bio);
- }
- #endif /* NO_FILESYSTEM */
- BIO_free(bio);
- BIO_free(bio2);
- /* try with type BIO */
- XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
- sizeof(msg));
- AssertNotNull(bio = BIO_new(BIO_s_bio()));
- AssertIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
- AssertNotNull(bio2 = BIO_new(BIO_s_bio()));
- AssertIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
- AssertIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
- AssertIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
- AssertIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
- AssertIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
- AssertStrEQ(bio_buffer, "hello wolfSSL\n");
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
- AssertIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
- BIO_free(bio);
- BIO_free(bio2);
- /* check reading an empty string */
- AssertNotNull(bio = BIO_new(BIO_s_bio()));
- AssertIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
- AssertIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
- AssertStrEQ(emp, bio_buffer);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BIO_puts(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- BIO* bio;
- char input[] = "hello\0world\n.....ok\n\0";
- char output[128];
- XMEMSET(output, 0, sizeof(output));
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(BIO_puts(bio, input), 5);
- AssertIntEQ(BIO_pending(bio), 5);
- AssertIntEQ(BIO_puts(bio, input + 6), 14);
- AssertIntEQ(BIO_pending(bio), 19);
- AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
- AssertStrEQ(output, "helloworld\n");
- AssertIntEQ(BIO_pending(bio), 8);
- AssertIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
- AssertStrEQ(output, ".....ok\n");
- AssertIntEQ(BIO_pending(bio), 0);
- AssertIntEQ(BIO_puts(bio, ""), -1);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BIO_dump(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- BIO* bio;
- static const unsigned char data[] = {
- 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
- 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
- 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
- 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
- 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
- 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
- 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
- 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
- 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
- 0xB4
- };
- /* Generated with OpenSSL. */
- static const char expected[] =
- "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
- "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
- "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
- "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
- "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
- "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
- static const char expectedAll[] =
- "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
- "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
- "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
- "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
- "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
- "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
- "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
- "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
- "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
- "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
- "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
- "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
- "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
- "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
- "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
- "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
- char output[16 * 80];
- int i;
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- /* Example key dumped. */
- AssertIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
- sizeof(expected) - 1);
- AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
- AssertIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
- /* Try every possible value for a character. */
- for (i = 0; i < 256; i++)
- output[i] = i;
- AssertIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
- AssertIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
- AssertIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
- static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
- {
- (void)ssl;
- (void)buf;
- (void)sz;
- (void)ctx;
- return WOLFSSL_CBIO_ERR_WANT_READ;
- }
- #endif
- static int test_wolfSSL_BIO_should_retry(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
- tcp_ready ready;
- func_args server_args;
- THREAD_TYPE serverThread;
- SOCKET_T sockfd = 0;
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- char msg[64] = "hello wolfssl!";
- char reply[1024];
- int msgSz = (int)XSTRLEN(msg);
- int ret;
- BIO* bio;
- XMEMSET(&server_args, 0, sizeof(func_args));
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #ifdef OPENSSL_COMPATIBLE_DEFAULTS
- AssertIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
- /* force retry */
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
- wolfSSL_SSLSetIORecv(ssl, forceWantRead);
- AssertNotNull(bio = BIO_new(BIO_f_ssl()));
- BIO_set_ssl(bio, ssl, BIO_CLOSE);
- AssertIntLE(BIO_write(bio, msg, msgSz), 0);
- AssertIntNE(BIO_should_retry(bio), 0);
- /* now perform successful connection */
- wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
- AssertIntEQ(BIO_write(bio, msg, msgSz), msgSz);
- BIO_read(bio, reply, sizeof(reply));
- ret = wolfSSL_get_error(ssl, -1);
- if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
- AssertIntNE(BIO_should_retry(bio), 0);
- }
- else {
- AssertIntEQ(BIO_should_retry(bio), 0);
- }
- AssertIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
- XSTRLEN("I hear you fa shizzle!")), 0);
- BIO_free(bio);
- wolfSSL_CTX_free(ctx);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BIO_connect(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
- tcp_ready ready;
- func_args server_args;
- THREAD_TYPE serverThread;
- BIO *tcpBio;
- BIO *sslBio;
- SSL_CTX* ctx;
- SSL *ssl;
- SSL *sslPtr;
- char msg[] = "hello wolfssl!";
- char reply[30];
- char buff[10] = {0};
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- /* Setup server */
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
- /* Start the test proper */
- /* Setup the TCP BIO */
- AssertNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
- AssertIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
- /* Setup the SSL object */
- AssertNotNull(ssl = SSL_new(ctx));
- SSL_set_connect_state(ssl);
- /* Setup the SSL BIO */
- AssertNotNull(sslBio = BIO_new(BIO_f_ssl()));
- AssertIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
- /* Verify that BIO_get_ssl works. */
- AssertIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
- AssertPtrEq(ssl, sslPtr);
- /* Link BIO's so that sslBio uses tcpBio for IO */
- AssertPtrEq(BIO_push(sslBio, tcpBio), sslBio);
- /* Do TCP connect */
- AssertIntEQ(BIO_do_connect(sslBio), 1);
- /* Do TLS handshake */
- AssertIntEQ(BIO_do_handshake(sslBio), 1);
- /* Test writing */
- AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
- /* Expect length of default wolfSSL reply */
- AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
- /* Clean it all up */
- BIO_free_all(sslBio);
- /* Server clean up */
- join_thread(serverThread);
- FreeTcpReady(&ready);
- /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
- * after. */
- XMEMSET(&server_args, 0, sizeof(func_args));
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- AssertIntGT(XSPRINTF(buff, "%d", ready.port), 0);
- AssertNotNull(sslBio = BIO_new_ssl_connect(ctx));
- AssertIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
- AssertIntEQ(BIO_set_conn_port(sslBio, buff), 1);
- AssertIntEQ(BIO_do_connect(sslBio), 1);
- AssertIntEQ(BIO_do_handshake(sslBio), 1);
- AssertIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
- AssertIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
- /* Attempt to close the TLS connection gracefully. */
- BIO_ssl_shutdown(sslBio);
- BIO_free_all(sslBio);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- SSL_CTX_free(ctx);
- #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BIO_tls(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
- SSL_CTX* ctx;
- SSL *ssl;
- BIO *readBio;
- BIO *writeBio;
- int ret, err = 0;
- AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertNotNull(readBio = BIO_new(BIO_s_mem()));
- AssertNotNull(writeBio = BIO_new(BIO_s_mem()));
- /* Qt reads data from write-bio,
- * then writes the read data into plain packet.
- * Qt reads data from plain packet,
- * then writes the read data into read-bio.
- */
- SSL_set_bio(ssl, readBio, writeBio);
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = SSL_connect(ssl);
- err = SSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
- /* in this use case, should return WANT READ
- * so that Qt will read the data from plain packet for next state.
- */
- AssertIntEQ(err, SSL_ERROR_WANT_READ);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
- static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
- {
- BIO* clientBio;
- SSL* sslClient;
- SSL_CTX* ctx;
- char connectAddr[20]; /* IP + port */;
- (void)args;
- AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
- AssertNotNull(clientBio = BIO_new_connect(connectAddr));
- AssertIntEQ(BIO_do_connect(clientBio), 1);
- AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
- AssertNotNull(sslClient = SSL_new(ctx));
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
- SSL_set_bio(sslClient, clientBio, clientBio);
- AssertIntEQ(SSL_connect(sslClient), 1);
- SSL_free(sslClient);
- SSL_CTX_free(ctx);
- #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- return 0;
- }
- #endif
- static int test_wolfSSL_BIO_accept(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_HTTP_CLIENT)
- BIO* serverBindBio;
- BIO* serverAcceptBio;
- SSL* sslServer;
- SSL_CTX* ctx;
- func_args args;
- THREAD_TYPE thread;
- char port[10]; /* 10 bytes should be enough to store the string
- * representation of the port */
- AssertIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
- AssertNotNull(serverBindBio = BIO_new_accept(port));
- /* First BIO_do_accept binds the port */
- AssertIntEQ(BIO_do_accept(serverBindBio), 1);
- XMEMSET(&args, 0, sizeof(func_args));
- start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
- AssertIntEQ(BIO_do_accept(serverBindBio), 1);
- /* Let's plug it into SSL to test */
- AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
- AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- AssertNotNull(sslServer = SSL_new(ctx));
- AssertNotNull(serverAcceptBio = BIO_pop(serverBindBio));
- SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
- AssertIntEQ(SSL_accept(sslServer), 1);
- join_thread(thread);
- BIO_free(serverBindBio);
- SSL_free(sslServer);
- SSL_CTX_free(ctx);
- #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BIO_write(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
- BIO* bio;
- BIO* bio64;
- BIO* ptr;
- int sz;
- char msg[] = "conversion test";
- char out[40];
- char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
- void* bufPtr = NULL;
- BUF_MEM* buf = NULL;
- AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
- AssertNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
- /* now should convert to base64 then write to memory */
- AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
- BIO_flush(bio);
- /* test BIO chain */
- AssertIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
- AssertNotNull(buf);
- AssertIntEQ(buf->length, 25);
- AssertIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
- AssertPtrEq(buf->data, bufPtr);
- AssertNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
- sz = sizeof(out);
- XMEMSET(out, 0, sz);
- AssertIntEQ((sz = BIO_read(ptr, out, sz)), 25);
- AssertIntEQ(XMEMCMP(out, expected, sz), 0);
- /* write then read should return the same message */
- AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
- sz = sizeof(out);
- XMEMSET(out, 0, sz);
- AssertIntEQ(BIO_read(bio, out, sz), 16);
- AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
- /* now try encoding with no line ending */
- BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
- #ifdef HAVE_EX_DATA
- BIO_set_ex_data(bio64, 0, (void*) "data");
- AssertIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
- #endif
- AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
- BIO_flush(bio);
- sz = sizeof(out);
- XMEMSET(out, 0, sz);
- AssertIntEQ((sz = BIO_read(ptr, out, sz)), 24);
- AssertIntEQ(XMEMCMP(out, expected, sz), 0);
- BIO_free_all(bio); /* frees bio64 also */
- /* test with more than one bio64 in list */
- AssertNotNull(bio64 = BIO_new(BIO_f_base64()));
- AssertNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
- AssertNotNull(BIO_push(bio64, BIO_new(BIO_s_mem())));
- /* now should convert to base64 when stored and then decode with read */
- AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
- BIO_flush(bio);
- sz = sizeof(out);
- XMEMSET(out, 0, sz);
- AssertIntEQ((sz = BIO_read(bio, out, sz)), 16);
- AssertIntEQ(XMEMCMP(out, msg, sz), 0);
- BIO_clear_flags(bio64, ~0);
- BIO_set_retry_read(bio);
- BIO_free_all(bio); /* frees bio64s also */
- AssertNotNull(bio = BIO_new_mem_buf(out, 0));
- AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BIO_printf(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL)
- BIO* bio;
- int sz = 7;
- char msg[] = "TLS 1.3 for the world";
- char out[60];
- char expected[] = "TLS 1.3 for the world : sz = 7";
- XMEMSET(out, 0, sizeof(out));
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
- AssertIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
- AssertIntEQ(BIO_read(bio, out, sizeof(out)), 30);
- AssertIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BIO_f_md(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
- BIO *bio, *mem;
- char msg[] = "message to hash";
- char out[60];
- EVP_MD_CTX* ctx;
- const unsigned char testKey[] =
- {
- 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
- 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
- 0x0b, 0x0b, 0x0b, 0x0b
- };
- const char testData[] = "Hi There";
- const unsigned char testResult[] =
- {
- 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
- 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
- 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
- 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
- };
- const unsigned char expectedHash[] =
- {
- 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
- 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
- 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
- 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
- };
- const unsigned char emptyHash[] =
- {
- 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
- 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
- 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
- 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
- };
- unsigned char check[sizeof(testResult) + 1];
- size_t checkSz = -1;
- EVP_PKEY* key;
- XMEMSET(out, 0, sizeof(out));
- AssertNotNull(bio = BIO_new(BIO_f_md()));
- AssertNotNull(mem = BIO_new(BIO_s_mem()));
- AssertIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
- AssertIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
- /* should not be able to write/read yet since just digest wrapper and no
- * data is passing through the bio */
- AssertIntEQ(BIO_write(bio, msg, 0), 0);
- AssertIntEQ(BIO_pending(bio), 0);
- AssertIntEQ(BIO_read(bio, out, sizeof(out)), 0);
- AssertIntEQ(BIO_gets(bio, out, 3), 0);
- AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
- AssertIntEQ(XMEMCMP(emptyHash, out, 32), 0);
- BIO_reset(bio);
- /* append BIO mem to bio in order to read/write */
- AssertNotNull(bio = BIO_push(bio, mem));
- XMEMSET(out, 0, sizeof(out));
- AssertIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
- AssertIntEQ(BIO_pending(bio), 16);
- /* this just reads the message and does not hash it (gets calls final) */
- AssertIntEQ(BIO_read(bio, out, sizeof(out)), 16);
- AssertIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
- /* create a message digest using BIO */
- XMEMSET(out, 0, sizeof(out));
- AssertIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
- AssertIntEQ(BIO_pending(mem), 16);
- AssertIntEQ(BIO_pending(bio), 16);
- AssertIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
- AssertIntEQ(XMEMCMP(expectedHash, out, 32), 0);
- BIO_free(bio);
- BIO_free(mem);
- /* test with HMAC */
- XMEMSET(out, 0, sizeof(out));
- AssertNotNull(bio = BIO_new(BIO_f_md()));
- AssertNotNull(mem = BIO_new(BIO_s_mem()));
- BIO_get_md_ctx(bio, &ctx);
- AssertNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
- testKey, (int)sizeof(testKey)));
- EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
- AssertNotNull(bio = BIO_push(bio, mem));
- BIO_write(bio, testData, (int)strlen(testData));
- EVP_DigestSignFinal(ctx, NULL, &checkSz);
- EVP_DigestSignFinal(ctx, check, &checkSz);
- AssertIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
- EVP_PKEY_free(key);
- BIO_free(bio);
- BIO_free(mem);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BIO_up_ref(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
- BIO* bio;
- AssertNotNull(bio = BIO_new(BIO_f_md()));
- AssertIntEQ(BIO_up_ref(NULL), 0);
- AssertIntEQ(BIO_up_ref(bio), 1);
- BIO_free(bio);
- AssertIntEQ(BIO_up_ref(bio), 1);
- BIO_free(bio);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif /* !NO_BIO */
- #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- /* test that the callback arg is correct */
- static int certCbArg = 0;
- static int clientCertCb(WOLFSSL* ssl, void* arg)
- {
- if (ssl == NULL || arg != &certCbArg)
- return 0;
- if (wolfSSL_use_certificate_file(ssl, cliCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
- return 0;
- if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
- return 0;
- return 1;
- }
- static void clientCertSetupCb(WOLFSSL_CTX* ctx)
- {
- SSL_CTX_set_cert_cb(ctx, clientCertCb, &certCbArg);
- }
- /**
- * This is only done because test_client_nofail has no way to stop
- * certificate and key loading
- */
- static void clientCertClearCb(WOLFSSL* ssl)
- {
- /* Clear the loaded certs to force the callbacks to set them up */
- SSL_certs_clear(ssl);
- }
- static int serverCertCb(WOLFSSL* ssl, void* arg)
- {
- if (ssl == NULL || arg != &certCbArg)
- return 0;
- if (wolfSSL_use_certificate_file(ssl, svrCertFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
- return 0;
- if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
- return 0;
- return 1;
- }
- static void serverCertSetupCb(WOLFSSL_CTX* ctx)
- {
- SSL_CTX_set_cert_cb(ctx, serverCertCb, &certCbArg);
- }
- /**
- * This is only done because test_server_nofail has no way to stop
- * certificate and key loading
- */
- static void serverCertClearCb(WOLFSSL* ssl)
- {
- /* Clear the loaded certs to force the callbacks to set them up */
- SSL_certs_clear(ssl);
- }
- #endif
- static int test_wolfSSL_cert_cb(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- callback_functions func_cb_client;
- callback_functions func_cb_server;
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
- XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- client_args.callbacks = &func_cb_client;
- server_args.callbacks = &func_cb_server;
- func_cb_client.ctx_ready = clientCertSetupCb;
- func_cb_client.ssl_ready = clientCertClearCb;
- func_cb_server.ctx_ready = serverCertSetupCb;
- func_cb_server.ssl_ready = serverCertClearCb;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SESSION(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- !defined(NO_SESSION_CACHE)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- WOLFSSL_SESSION* sess;
- WOLFSSL_SESSION* sess_copy;
- #ifdef OPENSSL_EXTRA
- unsigned char* sessDer = NULL;
- unsigned char* ptr = NULL;
- const unsigned char context[] = "user app context";
- unsigned int contextSz = (unsigned int)sizeof(context);
- int sz;
- #endif
- int ret, err;
- SOCKET_T sockfd;
- tcp_ready ready;
- func_args server_args;
- THREAD_TYPE serverThread;
- char msg[80];
- const char* sendGET = "GET";
- /* TLS v1.3 requires session tickets */
- /* CHACHA and POLY1305 required for myTicketEncCb */
- #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
- !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
- defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
- WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- #endif
- #ifdef HAVE_SESSION_TICKET
- /* Use session tickets, for ticket tests below */
- AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
- #endif
- XMEMSET(&server_args, 0, sizeof(func_args));
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- /* client connection */
- ssl = wolfSSL_new(ctx);
- tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
- AssertIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_connect(ssl);
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- AssertIntEQ(ret, (int)XSTRLEN(sendGET));
- #ifdef WOLFSSL_ASYNC_CRYPT
- err = 0; /* Reset error */
- #endif
- do {
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (err == WC_PENDING_E) {
- ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
- if (ret < 0) { break; } else if (ret == 0) { continue; }
- }
- #endif
- ret = wolfSSL_read(ssl, msg, sizeof(msg));
- err = wolfSSL_get_error(ssl, 0);
- } while (err == WC_PENDING_E);
- AssertIntEQ(ret, 23);
- AssertPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
- AssertPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
- #ifdef HAVE_EXT_CACHE
- AssertPtrEq(sess, sess_copy); /* they should be the same pointer but without
- * HAVE_EXT_CACHE we get new objects each time */
- #endif
- wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
- wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
- sess = wolfSSL_get_session(ssl);
- #ifdef OPENSSL_EXTRA
- AssertIntEQ(SSL_SESSION_is_resumable(NULL), 0);
- AssertIntEQ(SSL_SESSION_is_resumable(sess), 1);
- AssertIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
- AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
- #ifdef HAVE_SESSION_TICKET
- AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
- AssertIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
- SESSION_TICKET_HINT_DEFAULT);
- #else
- AssertIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
- #endif
- #else
- (void)sess;
- #endif /* OPENSSL_EXTRA */
- /* Retain copy of the session for later testing */
- AssertNotNull(sess = wolfSSL_get1_session(ssl));
- wolfSSL_shutdown(ssl);
- wolfSSL_free(ssl);
- join_thread(serverThread);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
- {
- X509 *x509;
- char buf[30];
- int bufSz;
- AssertNotNull(x509 = SSL_SESSION_get0_peer(sess));
- AssertIntGT((bufSz = X509_NAME_get_text_by_NID(
- X509_get_subject_name(x509), NID_organizationalUnitName,
- buf, sizeof(buf))), 0);
- AssertIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
- if (bufSz == 7) {
- AssertIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
- }
- if (bufSz == 16) {
- AssertIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
- }
- }
- #endif
- #ifdef HAVE_EXT_CACHE
- AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
- wolfSSL_SESSION_free(sess_copy);
- sess_copy = NULL;
- #endif
- #ifdef OPENSSL_EXTRA
- /* get session from DER and update the timeout */
- AssertIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
- AssertIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
- wolfSSL_SESSION_free(sess);
- sess = NULL;
- ptr = sessDer;
- AssertNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
- AssertNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
- (const unsigned char**)&ptr, sz));
- XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
- sessDer = NULL;
- AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
- AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
- #endif
- /* successful set session test */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
- #ifdef HAVE_SESSION_TICKET
- /* Test set/get session ticket */
- {
- const char* ticket = "This is a session ticket";
- char buf[64] = {0};
- word32 bufSz = (word32)sizeof(buf);
- AssertIntEQ(SSL_SUCCESS,
- wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
- (word32)XSTRLEN(ticket)));
- AssertIntEQ(SSL_SUCCESS,
- wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
- AssertStrEQ(ticket, buf);
- }
- #endif
- #ifdef OPENSSL_EXTRA
- /* session timeout case */
- /* make the session to be expired */
- AssertIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
- XSLEEP_MS(1200);
- /* SSL_set_session should reject specified session but return success
- * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
- */
- #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
- AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
- #else
- AssertIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
- #endif
- AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
- /* fail case with miss match session context IDs (use compatibility API) */
- AssertIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
- SSL_SUCCESS);
- AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
- wolfSSL_free(ssl);
- AssertIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
- SSL_FAILURE);
- AssertIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
- SSL_SUCCESS);
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
- #endif /* OPENSSL_EXTRA */
- wolfSSL_free(ssl);
- wolfSSL_SESSION_free(sess);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- defined(HAVE_EX_DATA)
- static int clientSessRemCountMalloc = 0;
- static int serverSessRemCountMalloc = 0;
- static int clientSessRemCountFree = 0;
- static int serverSessRemCountFree = 0;
- static WOLFSSL_CTX* serverSessCtx = NULL;
- static WOLFSSL_SESSION* serverSess = NULL;
- #ifndef NO_SESSION_CACHE_REF
- static WOLFSSL_CTX* clientSessCtx = NULL;
- static WOLFSSL_SESSION* clientSess = NULL;
- #endif
- static int serverSessRemIdx = 3;
- static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
- {
- int* mallocedData = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
- (void)ctx;
- AssertNotNull(mallocedData);
- if (!*mallocedData)
- clientSessRemCountFree++;
- else
- serverSessRemCountFree++;
- XFREE(mallocedData, NULL, DYNAMIC_TYPE_SESSION);
- SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
- }
- static void SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
- {
- SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
- #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
- !defined(NO_SESSION_CACHE_REF)
- /* Allow downgrade, set min version, and disable TLS 1.3.
- * Do this because without NO_SESSION_CACHE_REF we will want to return a
- * reference to the session cache. But with WOLFSSL_TLS13 and without
- * HAVE_SESSION_TICKET we won't have a session ID to be able to place the
- * session in the cache. In this case we need to downgrade to previous
- * versions to just use the legacy session ID field. */
- AssertIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), SSL_SUCCESS);
- AssertIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION), SSL_SUCCESS);
- #endif
- }
- static void SessRemSslSetupCb(WOLFSSL* ssl)
- {
- int* mallocedData = (int*)XMALLOC(sizeof(int), NULL, DYNAMIC_TYPE_SESSION);
- AssertNotNull(mallocedData);
- *mallocedData = SSL_is_server(ssl);
- if (!*mallocedData) {
- clientSessRemCountMalloc++;
- #ifndef NO_SESSION_CACHE_REF
- AssertNotNull(clientSess = SSL_get1_session(ssl));
- AssertIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
- SSL_SUCCESS);
- #endif
- }
- else {
- serverSessRemCountMalloc++;
- AssertNotNull(serverSess = SSL_get1_session(ssl));
- AssertIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
- SSL_SUCCESS);
- }
- AssertIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl), serverSessRemIdx,
- mallocedData), SSL_SUCCESS);
- }
- #endif
- static int test_wolfSSL_CTX_sess_set_remove_cb(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
- defined(HAVE_EX_DATA)
- /* Check that the remove callback gets called for external data in a
- * session object */
- callback_functions func_cb;
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&func_cb, 0, sizeof(callback_functions));
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- client_args.callbacks = &func_cb;
- server_args.callbacks = &func_cb;
- func_cb.ctx_ready = SessRemCtxSetupCb;
- func_cb.on_result = SessRemSslSetupCb;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- /* Both should have been allocated */
- AssertIntEQ(clientSessRemCountMalloc, 1);
- AssertIntEQ(serverSessRemCountMalloc, 1);
- #ifdef NO_SESSION_CACHE_REF
- /* Client session should not be added to cache so this should be free'd when
- * the SSL object was being free'd */
- AssertIntEQ(clientSessRemCountFree, 1);
- #else
- /* Client session is in cache due to requiring a persistent reference */
- AssertIntEQ(clientSessRemCountFree, 0);
- /* Force a cache lookup */
- AssertNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
- /* Force a cache update */
- AssertNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
- /* This should set the timeout to 0 and call the remove callback from within
- * the session cache. */
- AssertIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
- AssertNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
- AssertIntEQ(clientSessRemCountFree, 1);
- #endif
- /* Server session is in the cache so ex_data isn't free'd with the SSL
- * object */
- AssertIntEQ(serverSessRemCountFree, 0);
- /* Force a cache lookup */
- AssertNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
- /* Force a cache update */
- AssertNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
- /* This should set the timeout to 0 and call the remove callback from within
- * the session cache. */
- AssertIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
- AssertNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
- AssertIntEQ(serverSessRemCountFree, 1);
- /* Need to free the references that we kept */
- SSL_CTX_free(serverSessCtx);
- SSL_SESSION_free(serverSess);
- #ifndef NO_SESSION_CACHE_REF
- SSL_CTX_free(clientSessCtx);
- SSL_SESSION_free(clientSess);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ticket_keys(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
- !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX* ctx;
- byte keys[WOLFSSL_TICKET_KEYS_SZ];
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
- WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
- WOLFSSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_d2i_PUBKEY(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- BIO* bio;
- EVP_PKEY* pkey;
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertNull(d2i_PUBKEY_bio(NULL, NULL));
- #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
- /* RSA PUBKEY test */
- AssertIntGT(BIO_write(bio, client_keypub_der_2048,
- sizeof_client_keypub_der_2048), 0);
- AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
- EVP_PKEY_free(pkey);
- #endif
- #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
- /* ECC PUBKEY test */
- AssertIntGT(BIO_write(bio, ecc_clikeypub_der_256,
- sizeof_ecc_clikeypub_der_256), 0);
- AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
- EVP_PKEY_free(pkey);
- #endif
- #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
- /* DSA PUBKEY test */
- AssertIntGT(BIO_write(bio, dsa_pub_key_der_2048,
- sizeof_dsa_pub_key_der_2048), 0);
- AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
- EVP_PKEY_free(pkey);
- #endif
- #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
- defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
- (HAVE_FIPS_VERSION > 2))
- /* DH PUBKEY test */
- AssertIntGT(BIO_write(bio, dh_pub_key_der_2048,
- sizeof_dh_pub_key_der_2048), 0);
- AssertNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
- EVP_PKEY_free(pkey);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
- BIO_free(bio);
- (void)pkey;
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
- static int test_wolfSSL_d2i_PrivateKeys_bio(void)
- {
- BIO* bio = NULL;
- EVP_PKEY* pkey = NULL;
- #ifndef NO_RSA
- #endif
- WOLFSSL_CTX* ctx;
- #if defined(WOLFSSL_KEY_GEN)
- unsigned char buff[4096];
- unsigned char* bufPtr = buff;
- #endif
- /* test creating new EVP_PKEY with bad arg */
- AssertNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
- /* test loading RSA key using BIO */
- #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- {
- XFILE file;
- const char* fname = "./certs/server-key.der";
- size_t sz;
- byte* buf;
- file = XFOPEN(fname, "rb");
- AssertTrue((file != XBADFILE));
- AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
- sz = XFTELL(file);
- XREWIND(file);
- AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
- AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
- XFCLOSE(file);
- /* Test using BIO new mem and loading DER private key */
- AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
- AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
- XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
- BIO_free(bio);
- bio = NULL;
- EVP_PKEY_free(pkey);
- pkey = NULL;
- }
- #endif
- /* test loading ECC key using BIO */
- #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
- {
- XFILE file;
- const char* fname = "./certs/ecc-key.der";
- size_t sz;
- byte* buf;
- file = XFOPEN(fname, "rb");
- AssertTrue((file != XBADFILE));
- AssertTrue(XFSEEK(file, 0, XSEEK_END) == 0);
- sz = XFTELL(file);
- XREWIND(file);
- AssertNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
- AssertIntEQ(XFREAD(buf, 1, sz, file), sz);
- XFCLOSE(file);
- /* Test using BIO new mem and loading DER private key */
- AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
- AssertNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
- XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
- BIO_free(bio);
- bio = NULL;
- EVP_PKEY_free(pkey);
- pkey = NULL;
- }
- #endif
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- #ifndef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- #else
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
- #endif
- #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(NO_RSA) && !defined(HAVE_USER_RSA)
- {
- RSA* rsa = NULL;
- /* Tests bad parameters */
- AssertNull(d2i_RSAPrivateKey_bio(NULL, NULL));
- /* RSA not set yet, expecting to fail*/
- AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
- #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
- /* set RSA using bio*/
- AssertIntGT(BIO_write(bio, client_key_der_2048,
- sizeof_client_key_der_2048), 0);
- AssertNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
- AssertNotNull(rsa);
- AssertIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
- /*i2d RSAprivate key tests */
- AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
- AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
- sizeof_client_key_der_2048);
- bufPtr -= sizeof_client_key_der_2048;
- AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
- sizeof_client_key_der_2048), 0);
- bufPtr = NULL;
- AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
- sizeof_client_key_der_2048);
- AssertNotNull(bufPtr);
- AssertIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
- sizeof_client_key_der_2048), 0);
- XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
- RSA_free(rsa);
- rsa = RSA_new();
- AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
- #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
- RSA_free(rsa);
- }
- #endif /* !HAVE_FAST_RSA && WOLFSSL_KEY_GEN && !NO_RSA && !HAVE_USER_RSA*/
- SSL_CTX_free(ctx);
- ctx = NULL;
- BIO_free(bio);
- bio = NULL;
- return TEST_RES_CHECK(1);
- }
- #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
- #endif /* !NO_BIO */
- static int test_wolfSSL_sk_GENERAL_NAME(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_RSA)
- X509* x509;
- GENERAL_NAME* gn;
- unsigned char buf[4096];
- const unsigned char* bufPt;
- int bytes, i;
- int j;
- XFILE f;
- STACK_OF(GENERAL_NAME)* sk;
- f = XFOPEN(cliCertDerFileExt, "rb");
- AssertTrue((f != XBADFILE));
- AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
- XFCLOSE(f);
- for (j = 0; j < 2; ++j) {
- bufPt = buf;
- AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
- AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
- NID_subject_alt_name, NULL, NULL));
- AssertIntEQ(sk_GENERAL_NAME_num(sk), 1);
- for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
- AssertNotNull(gn = sk_GENERAL_NAME_value(sk, i));
- switch (gn->type) {
- case GEN_DNS:
- fprintf(stderr, "found type GEN_DNS\n");
- break;
- case GEN_EMAIL:
- fprintf(stderr, "found type GEN_EMAIL\n");
- break;
- case GEN_URI:
- fprintf(stderr, "found type GEN_URI\n");
- break;
- }
- }
- X509_free(x509);
- if (j == 0) {
- sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
- }
- else {
- /*
- * We had a bug where GENERAL_NAMES_free didn't free all the memory
- * it was supposed to. This is a regression test for that bug.
- */
- GENERAL_NAMES_free(sk);
- }
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_GENERAL_NAME_print(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
- X509* x509;
- GENERAL_NAME* gn;
- unsigned char buf[4096];
- const unsigned char* bufPt;
- int bytes;
- XFILE f;
- STACK_OF(GENERAL_NAME)* sk;
- BIO* out;
- unsigned char outbuf[128];
- X509_EXTENSION* ext;
- AUTHORITY_INFO_ACCESS* aia;
- ACCESS_DESCRIPTION* ad;
- const unsigned char v4Addr[] = {192,168,53,1};
- const unsigned char v6Addr[] =
- {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
- const unsigned char email[] =
- {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
- 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
- const char* dnsStr = "DNS:example.com";
- const char* uriStr = "URI:http://127.0.0.1:22220";
- const char* v4addStr = "IP Address:192.168.53.1";
- const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
- const char* emailStr = "email:info@wolfssl.com";
- const char* othrStr = "othername:<unsupported>";
- const char* x400Str = "X400Name:<unsupported>";
- const char* ediStr = "EdiPartyName:<unsupported>";
- /* BIO to output */
- AssertNotNull(out = BIO_new(BIO_s_mem()));
- /* test for NULL param */
- gn = NULL;
- AssertIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
- AssertIntEQ(GENERAL_NAME_print(NULL, gn), 0);
- AssertIntEQ(GENERAL_NAME_print(out, NULL), 0);
- /* test for GEN_DNS */
- f = XFOPEN(cliCertDerFileExt, "rb");
- AssertTrue((f != XBADFILE));
- AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
- XFCLOSE(f);
- bufPt = buf;
- AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
- AssertNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
- NID_subject_alt_name, NULL, NULL));
- AssertNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
- AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
- XMEMSET(outbuf,0,sizeof(outbuf));
- BIO_read(out, outbuf, sizeof(outbuf));
- AssertIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
- sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
- X509_free(x509);
- /* test for GEN_URI */
- f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb");
- AssertTrue((f != XBADFILE));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
- XFCLOSE(f);
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
- aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
- AssertNotNull(aia);
- ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0);
- gn = ad->location;
- AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
- XMEMSET(outbuf,0,sizeof(outbuf));
- AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
- AssertIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
- wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
- aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
- AssertNotNull(aia);
- AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
- X509_free(x509);
- /* test for GEN_IPADD */
- /* ip v4 address */
- AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
- gn->type = GEN_IPADD;
- gn->d.iPAddress->length = sizeof(v4Addr);
- AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
- sizeof(v4Addr)), 1);
- AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
- XMEMSET(outbuf,0,sizeof(outbuf));
- AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
- AssertIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
- GENERAL_NAME_free(gn);
- /* ip v6 address */
- AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
- gn->type = GEN_IPADD;
- gn->d.iPAddress->length = sizeof(v6Addr);
- AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
- sizeof(v6Addr)), 1);
- AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
- XMEMSET(outbuf,0,sizeof(outbuf));
- AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
- AssertIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
- GENERAL_NAME_free(gn);
- /* test for GEN_EMAIL */
- AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
- gn->type = GEN_EMAIL;
- gn->d.rfc822Name->length = sizeof(email);
- AssertIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email,
- sizeof(email)), 1);
- AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
- XMEMSET(outbuf,0,sizeof(outbuf));
- AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
- AssertIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
- GENERAL_NAME_free(gn);
- /* test for GEN_OTHERNAME */
- AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
- gn->type = GEN_OTHERNAME;
- AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
- XMEMSET(outbuf,0,sizeof(outbuf));
- AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
- AssertIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
- GENERAL_NAME_free(gn);
- /* test for GEN_X400 */
- AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
- gn->type = GEN_X400;
- AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
- XMEMSET(outbuf,0,sizeof(outbuf));
- AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
- AssertIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
- GENERAL_NAME_free(gn);
- /* test for GEN_EDIPARTY */
- AssertNotNull(gn = wolfSSL_GENERAL_NAME_new());
- gn->type = GEN_EDIPARTY;
- AssertIntEQ(GENERAL_NAME_print(out, gn), 1);
- XMEMSET(outbuf,0,sizeof(outbuf));
- AssertIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
- AssertIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
- GENERAL_NAME_free(gn);
- BIO_free(out);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_ALL */
- return res;
- }
- static int test_wolfSSL_sk_DIST_POINT(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_RSA)
- X509* x509;
- unsigned char buf[4096];
- const unsigned char* bufPt;
- int bytes, i, j;
- XFILE f;
- DIST_POINT* dp;
- DIST_POINT_NAME* dpn;
- GENERAL_NAME* gn;
- ASN1_IA5STRING* uri;
- STACK_OF(DIST_POINT)* dps;
- STACK_OF(GENERAL_NAME)* gns;
- const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
- f = XFOPEN(cliCertDerCrlDistPoint, "rb");
- AssertTrue((f != XBADFILE));
- AssertIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
- XFCLOSE(f);
- bufPt = buf;
- AssertNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
- AssertNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
- NID_crl_distribution_points, NULL, NULL));
- AssertIntEQ(sk_DIST_POINT_num(dps), 1);
- for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
- AssertNotNull(dp = sk_DIST_POINT_value(dps, i));
- AssertNotNull(dpn = dp->distpoint);
- /* this should be type 0, fullname */
- AssertIntEQ(dpn->type, 0);
- gns = dp->distpoint->name.fullname;
- AssertNotNull(gns);
- AssertIntEQ(sk_GENERAL_NAME_num(gns), 1);
- for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
- gn = sk_GENERAL_NAME_value(gns, j);
- AssertIntEQ(gn->type, GEN_URI);
- AssertNotNull(uri = gn->d.uniformResourceIdentifier);
- AssertNotNull(uri->data);
- AssertIntGT(uri->length, 0);
- }
- }
- X509_free(x509);
- CRL_DIST_POINTS_free(dps);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_MD4(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
- MD4_CTX md4;
- unsigned char out[16]; /* MD4_DIGEST_SIZE */
- const char* msg = "12345678901234567890123456789012345678901234567890123456"
- "789012345678901234567890";
- const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
- "\xcc\x05\x36";
- int msgSz = (int)XSTRLEN(msg);
- XMEMSET(out, 0, sizeof(out));
- MD4_Init(&md4);
- MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
- MD4_Final(out, &md4);
- AssertIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_verify_mode(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
- SSL_free(ssl);
- SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
- wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
- AssertIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
- SSL_free(ssl);
- wolfSSL_CTX_set_verify(ctx,
- WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
- AssertIntEQ(SSL_get_verify_mode(ssl),
- WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
- wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
- AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
- WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
- wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
- wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
- wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
- #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
- AssertIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
- #endif
- AssertIntEQ(SSL_CTX_get_verify_mode(ctx),
- WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_verify_depth(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- long depth;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), SSL_SUCCESS);
- AssertIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
- SSL_free(ssl);
- SSL_CTX_set_verify_depth(ctx, -1);
- AssertIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
- SSL_CTX_set_verify_depth(ctx, 2);
- AssertIntEQ(2, SSL_CTX_get_verify_depth(ctx));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(2, SSL_get_verify_depth(ssl));
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
- /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
- * buffer of 64 bytes.
- *
- * returns the size of the digest buffer on success and a negative value on
- * failure.
- */
- static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest)
- {
- HMAC_CTX ctx1;
- HMAC_CTX ctx2;
- unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
- "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
- unsigned char long_key[] =
- "0123456789012345678901234567890123456789"
- "0123456789012345678901234567890123456789"
- "0123456789012345678901234567890123456789"
- "0123456789012345678901234567890123456789";
- unsigned char msg[] = "message to hash";
- unsigned int digestSz = 64;
- int keySz = sizeof(key);
- int long_keySz = sizeof(long_key);
- int msgSz = sizeof(msg);
- unsigned char digest2[64];
- unsigned int digestSz2 = 64;
- HMAC_CTX_init(&ctx1);
- AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
- HMAC_CTX_cleanup(&ctx1);
- AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
- HMAC_CTX_cleanup(&ctx2);
- AssertIntEQ(digestSz, digestSz2);
- AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
- /* test HMAC_Init with NULL key */
- /* init after copy */
- HMAC_CTX_init(&ctx1);
- AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
- AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
- HMAC_CTX_cleanup(&ctx1);
- AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
- HMAC_CTX_cleanup(&ctx2);
- AssertIntEQ(digestSz, digestSz2);
- AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
- /* long key */
- HMAC_CTX_init(&ctx1);
- AssertIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
- AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
- HMAC_CTX_cleanup(&ctx1);
- AssertIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
- HMAC_CTX_cleanup(&ctx2);
- AssertIntEQ(digestSz, digestSz2);
- AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
- /* init before copy */
- HMAC_CTX_init(&ctx1);
- AssertIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
- AssertIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
- HMAC_CTX_cleanup(&ctx1);
- AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
- AssertIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
- HMAC_CTX_cleanup(&ctx2);
- AssertIntEQ(digestSz, digestSz2);
- AssertIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
- return digestSz;
- }
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
- static int test_wolfSSL_HMAC_CTX(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
- unsigned char digest[64];
- int digestSz;
- #ifndef NO_SHA
- AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha1(), digest)), 20);
- AssertIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
- "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
- #endif /* !NO_SHA */
- #ifdef WOLFSSL_SHA224
- AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha224(), digest)), 28);
- AssertIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
- "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
- "\x02\x0E", digest, digestSz), 0);
- #endif /* WOLFSSL_SHA224 */
- #ifndef NO_SHA256
- AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha256(), digest)), 32);
- AssertIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
- "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
- "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
- #endif /* !NO_SHA256 */
- #ifdef WOLFSSL_SHA384
- AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha384(), digest)), 48);
- AssertIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
- "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
- "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
- "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
- digest, digestSz), 0);
- #endif /* WOLFSSL_SHA384 */
- #ifdef WOLFSSL_SHA512
- AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_sha512(), digest)), 64);
- AssertIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
- "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
- "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
- "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
- "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
- digest, digestSz), 0);
- #endif /* WOLFSSL_SHA512 */
- #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || HAVE_FIPS_VERSION <= 2)
- AssertIntEQ((digestSz = test_HMAC_CTX_helper(EVP_md5(), digest)), 16);
- AssertIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
- "\xE4\x98\xDD", digest, digestSz), 0);
- #endif /* !NO_MD5 */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
- static void sslMsgCb(int w, int version, int type, const void* buf,
- size_t sz, SSL* ssl, void* arg)
- {
- int i;
- unsigned char* pt = (unsigned char*)buf;
- fprintf(stderr, "%s %d bytes of version %d , type %d : ",
- (w)?"Writing":"Reading", (int)sz, version, type);
- for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
- fprintf(stderr, "\n");
- (void)ssl;
- (void)arg;
- }
- #endif /* OPENSSL_EXTRA */
- static int test_wolfSSL_msg_callback(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
- SSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
- SSL_FILETYPE_PEM));
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
- SSL_SUCCESS);
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
- AssertIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
- AssertIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SHA(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
- #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
- (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
- {
- const unsigned char in[] = "abc";
- unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
- "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
- unsigned char out[WC_SHA_DIGEST_SIZE];
- XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
- AssertNotNull(SHA1(in, XSTRLEN((char*)in), out));
- AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
- /* SHA interface test */
- XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
- AssertNull(SHA(NULL, XSTRLEN((char*)in), out));
- AssertNotNull(SHA(in, 0, out));
- AssertNotNull(SHA(in, XSTRLEN((char*)in), NULL));
- AssertNotNull(SHA(NULL, 0, out));
- AssertNotNull(SHA(NULL, 0, NULL));
- AssertNotNull(SHA(in, XSTRLEN((char*)in), out));
- AssertIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
- }
- #endif
- #if !defined(NO_SHA256)
- {
- const unsigned char in[] = "abc";
- unsigned char expected[] = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
- "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
- "\x15\xAD";
- unsigned char out[WC_SHA256_DIGEST_SIZE];
- XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
- #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
- AssertNotNull(SHA256(in, XSTRLEN((char*)in), out));
- #else
- AssertNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
- #endif
- AssertIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
- }
- #endif
- #if defined(WOLFSSL_SHA384)
- {
- const unsigned char in[] = "abc";
- unsigned char expected[] = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
- "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
- "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
- "\xc8\x25\xa7";
- unsigned char out[WC_SHA384_DIGEST_SIZE];
- XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
- #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
- AssertNotNull(SHA384(in, XSTRLEN((char*)in), out));
- #else
- AssertNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
- #endif
- AssertIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
- }
- #endif
- #if defined(WOLFSSL_SHA512)
- {
- const unsigned char in[] = "abc";
- unsigned char expected[] = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
- "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
- "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
- "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
- "\xa5\x4c\xa4\x9f";
- unsigned char out[WC_SHA512_DIGEST_SIZE];
- XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
- #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
- AssertNotNull(SHA512(in, XSTRLEN((char*)in), out));
- #else
- AssertNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
- #endif
- AssertIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
- }
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
- (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
- static void binary_dump(void *ptr, int size)
- {
- #ifdef WOLFSSL_EVP_PRINT
- int i = 0;
- unsigned char *p = (unsigned char *) ptr;
- fprintf(stderr, "{");
- while ((p != NULL) && (i < size)) {
- if ((i % 8) == 0) {
- fprintf(stderr, "\n");
- fprintf(stderr, " ");
- }
- fprintf(stderr, "0x%02x, ", p[i]);
- i++;
- }
- fprintf(stderr, "\n};\n");
- #else
- (void) ptr;
- (void) size;
- #endif
- }
- static int last_val = 0x0f;
- static int check_result(unsigned char *data, int len)
- {
- int i;
- for ( ; len; ) {
- last_val = (last_val + 1) % 16;
- for (i = 0; i < 16; len--, i++, data++)
- if (*data != last_val) {
- return -1;
- }
- }
- return 0;
- }
- static int r_offset;
- static int w_offset;
- static void init_offset(void)
- {
- r_offset = 0;
- w_offset = 0;
- }
- static void get_record(unsigned char *data, unsigned char *buf, int len)
- {
- XMEMCPY(buf, data+r_offset, len);
- r_offset += len;
- }
- static void set_record(unsigned char *data, unsigned char *buf, int len)
- {
- XMEMCPY(data+w_offset, buf, len);
- w_offset += len;
- }
- static void set_plain(unsigned char *plain, int rec)
- {
- int i, j;
- unsigned char *p = plain;
- #define BLOCKSZ 16
- for (i=0; i<(rec/BLOCKSZ); i++) {
- for (j=0; j<BLOCKSZ; j++)
- *p++ = (i % 16);
- }
- }
- #endif
- static int test_wolfSSL_EVP_Cipher_extra(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
- (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
- /* aes128-cbc, keylen=16, ivlen=16 */
- byte aes128_cbc_key[] = {
- 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
- 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
- };
- byte aes128_cbc_iv[] = {
- 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
- 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
- };
- /* teset data size table */
- int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
- int test_drive2[] = {8, 3, 8, 512, 0};
- int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
- int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
- int test_drive_len[100];
- int ret = 0;
- EVP_CIPHER_CTX *evp = NULL;
- int ilen = 0;
- int klen = 0;
- int i, j;
- const EVP_CIPHER *type;
- byte *iv;
- byte *key;
- int ivlen;
- int keylen;
- #define RECORDS 16
- #define BUFFSZ 512
- byte plain [BUFFSZ * RECORDS];
- byte cipher[BUFFSZ * RECORDS];
- byte inb[BUFFSZ];
- byte outb[BUFFSZ+16];
- int outl, inl;
- iv = aes128_cbc_iv;
- ivlen = sizeof(aes128_cbc_iv);
- key = aes128_cbc_key;
- keylen = sizeof(aes128_cbc_key);
- type = EVP_aes_128_cbc();
- set_plain(plain, BUFFSZ * RECORDS);
- SSL_library_init();
- AssertNotNull(evp = EVP_CIPHER_CTX_new());
- AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
- AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
- klen = EVP_CIPHER_CTX_key_length(evp);
- if (klen > 0 && keylen != klen) {
- AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
- }
- ilen = EVP_CIPHER_CTX_iv_length(evp);
- if (ilen > 0 && ivlen != ilen) {
- AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
- }
- AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
- for (j = 0; j<RECORDS; j++)
- {
- inl = BUFFSZ;
- get_record(plain, inb, inl);
- AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
- set_record(cipher, outb, outl);
- }
- for (i = 0; test_drive[i]; i++) {
- AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
- init_offset();
- test_drive_len[i] = 0;
- for (j = 0; test_drive[i][j]; j++)
- {
- inl = test_drive[i][j];
- test_drive_len[i] += inl;
- get_record(plain, inb, inl);
- AssertIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)), 0);
- /* output to cipher buffer, so that following Dec test can detect
- if any error */
- set_record(cipher, outb, outl);
- }
- EVP_CipherFinal(evp, outb, &outl);
- if (outl > 0)
- set_record(cipher, outb, outl);
- }
- for (i = 0; test_drive[i]; i++) {
- last_val = 0x0f;
- AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
- init_offset();
- for (j = 0; test_drive[i][j]; j++) {
- inl = test_drive[i][j];
- get_record(cipher, inb, inl);
- AssertIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)), 0);
- binary_dump(outb, outl);
- AssertIntEQ((ret = check_result(outb, outl)), 0);
- AssertFalse(outl > ((inl/16+1)*16) && outl > 16);
- }
- ret = EVP_CipherFinal(evp, outb, &outl);
- binary_dump(outb, outl);
- ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
- (((test_drive_len[i] % 16) == 0) && (ret == 1));
- AssertTrue(ret);
- }
- EVP_CIPHER_CTX_free(evp);
- /* Do an extra test to verify correct behavior with empty input. */
- AssertNotNull(evp = EVP_CIPHER_CTX_new());
- AssertIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
- AssertIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
- klen = EVP_CIPHER_CTX_key_length(evp);
- if (klen > 0 && keylen != klen) {
- AssertIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
- }
- ilen = EVP_CIPHER_CTX_iv_length(evp);
- if (ilen > 0 && ivlen != ilen) {
- AssertIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
- }
- AssertIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
- /* outl should be set to 0 after passing NULL, 0 for input args. */
- outl = -1;
- AssertIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
- AssertIntEQ(outl, 0);
- EVP_CIPHER_CTX_free(evp);
- res = TEST_RES_CHECK(1);
- #endif /* test_EVP_Cipher */
- return res;
- }
- static int test_wolfSSL_PEM_read_DHparams(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
- !defined(NO_FILESYSTEM)
- DH* dh;
- XFILE fp;
- unsigned char derOut[300];
- unsigned char* derOutBuf = derOut;
- int derOutSz = 0;
- unsigned char derExpected[300];
- int derExpectedSz = 0;
- XMEMSET(derOut, 0, sizeof(derOut));
- XMEMSET(derExpected, 0, sizeof(derExpected));
- /* open DH param file, read into DH struct */
- AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
- /* bad args */
- AssertNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
- AssertNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
- /* good args */
- AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
- XFCLOSE(fp);
- /* read in certs/dh2048.der for comparison against exported params */
- fp = XFOPEN("./certs/dh2048.der", "rb");
- AssertTrue(fp != XBADFILE);
- derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected), fp);
- XFCLOSE(fp);
- /* export DH back to DER and compare */
- derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
- AssertIntEQ(derOutSz, derExpectedSz);
- AssertIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
- DH_free(dh);
- dh = NULL;
- /* Test parsing with X9.42 header */
- fp = XFOPEN("./certs/x942dh2048.pem", "rb");
- AssertNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
- XFCLOSE(fp);
- DH_free(dh);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_AES_ecb_encrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB)
- AES_KEY aes;
- const byte msg[] =
- {
- 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
- 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
- };
- const byte verify[] =
- {
- 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
- 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
- };
- const byte key[] =
- {
- 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
- 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
- 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
- 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
- };
- byte out[AES_BLOCK_SIZE];
- AssertIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
- XMEMSET(out, 0, AES_BLOCK_SIZE);
- AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
- AssertIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
- #ifdef HAVE_AES_DECRYPT
- AssertIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
- XMEMSET(out, 0, AES_BLOCK_SIZE);
- AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
- AssertIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
- #endif
- /* test bad arguments */
- AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
- AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
- AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_MD5(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
- byte input1[] = "";
- byte input2[] = "message digest";
- byte hash[WC_MD5_DIGEST_SIZE];
- unsigned char output1[] =
- "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
- unsigned char output2[] =
- "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
- WOLFSSL_MD5_CTX md5;
- XMEMSET(&md5, 0, sizeof(md5));
- /* Test cases for illegal parameters */
- AssertIntEQ(MD5_Init(NULL), 0);
- AssertIntEQ(MD5_Init(&md5), 1);
- AssertIntEQ(MD5_Update(NULL, input1, 0), 0);
- AssertIntEQ(MD5_Update(NULL, NULL, 0), 0);
- AssertIntEQ(MD5_Update(&md5, NULL, 1), 0);
- AssertIntEQ(MD5_Final(NULL, &md5), 0);
- AssertIntEQ(MD5_Final(hash, NULL), 0);
- AssertIntEQ(MD5_Final(NULL, NULL), 0);
- /* Init MD5 CTX */
- AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
- AssertIntEQ(wolfSSL_MD5_Update(&md5, input1,
- XSTRLEN((const char*)&input1)), 1);
- AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
- AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
- /* Init MD5 CTX */
- AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
- AssertIntEQ(wolfSSL_MD5_Update(&md5, input2,
- (int)XSTRLEN((const char*)input2)), 1);
- AssertIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
- AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
- #if !defined(NO_OLD_NAMES) && \
- (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
- AssertPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
- AssertPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
- AssertPtrNE(MD5(input1, 1, NULL), NULL);
- AssertPtrNE(MD5(NULL, 0, NULL), NULL);
- AssertPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), &hash);
- AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
- AssertPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), &hash);
- AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
- {
- byte data[] = "Data to be hashed.";
- XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
- AssertNotNull(MD5(data, sizeof(data), NULL));
- AssertNotNull(MD5(data, sizeof(data), hash));
- AssertNotNull(MD5(NULL, 0, hash));
- AssertNull(MD5(NULL, sizeof(data), hash));
- }
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_MD5_Transform(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
- byte input1[] = "";
- byte input2[] = "abc";
- byte local[WC_MD5_BLOCK_SIZE];
- word32 sLen = 0;
- #ifdef BIG_ENDIAN_ORDER
- unsigned char output1[] =
- "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
- unsigned char output2[] =
- "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
- #else
- unsigned char output1[] =
- "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
- unsigned char output2[] =
- "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
- #endif
- union {
- wc_Md5 native;
- MD5_CTX compat;
- } md5;
- XMEMSET(&md5.compat, 0, sizeof(md5.compat));
- XMEMSET(&local, 0, sizeof(local));
- /* sanity check */
- AssertIntEQ(MD5_Transform(NULL, NULL), 0);
- AssertIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
- AssertIntEQ(MD5_Transform(&md5.compat, NULL), 0);
- AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
- AssertIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
- /* Init MD5 CTX */
- AssertIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(md5.native.digest, output1,
- WC_MD5_DIGEST_SIZE), 0);
- /* Init MD5 CTX */
- AssertIntEQ(MD5_Init(&md5.compat), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(md5.native.digest, output2,
- WC_MD5_DIGEST_SIZE), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SHA224(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
- !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
- unsigned char input[] =
- "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
- unsigned char output[] =
- "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
- "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
- size_t inLen;
- byte hash[WC_SHA224_DIGEST_SIZE];
- inLen = XSTRLEN((char*)input);
- XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
- AssertNull(SHA224(NULL, inLen, hash));
- AssertNotNull(SHA224(input, 0, hash));
- AssertNotNull(SHA224(input, inLen, NULL));
- AssertNotNull(SHA224(NULL, 0, hash));
- AssertNotNull(SHA224(NULL, 0, NULL));
- AssertNotNull(SHA224(input, inLen, hash));
- AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SHA_Transform(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
- #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
- byte input1[] = "";
- byte input2[] = "abc";
- byte local[WC_SHA_BLOCK_SIZE];
- word32 sLen = 0;
- #ifdef BIG_ENDIAN_ORDER
- unsigned char output1[] =
- "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
- "\x09\xf7\x3a\x93";
- unsigned char output2[] =
- "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
- "\x7c\x6e\x08\xb8";
- #else
- unsigned char output1[] =
- "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
- "\x93\x3a\xf7\x09";
- unsigned char output2[] =
- "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
- "\xb8\x08\x6e\x7c";
- #endif
- union {
- wc_Sha native;
- SHA_CTX compat;
- } sha;
- union {
- wc_Sha native;
- SHA_CTX compat;
- } sha1;
- XMEMSET(&sha.compat, 0, sizeof(sha.compat));
- XMEMSET(&local, 0, sizeof(local));
- /* sanity check */
- AssertIntEQ(SHA_Transform(NULL, NULL), 0);
- AssertIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
- AssertIntEQ(SHA_Transform(&sha.compat, NULL), 0);
- AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
- AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
- AssertIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
- AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
- AssertIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
- /* Init SHA CTX */
- AssertIntEQ(SHA_Init(&sha.compat), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(sha.native.digest, output1,
- WC_SHA_DIGEST_SIZE), 0);
- AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
- /* Init SHA CTX */
- AssertIntEQ(SHA_Init(&sha.compat), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(sha.native.digest, output2,
- WC_SHA_DIGEST_SIZE), 0);
- AssertIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
- /* SHA1 */
- XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
- /* Init SHA CTX */
- AssertIntEQ(SHA1_Init(&sha1.compat), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(sha1.native.digest, output1,
- WC_SHA_DIGEST_SIZE), 0);
- AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
- /* Init SHA CTX */
- AssertIntEQ(SHA1_Init(&sha1.compat), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(sha1.native.digest, output2,
- WC_SHA_DIGEST_SIZE), 0);
- AssertIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
- res = TEST_RES_CHECK(1);
- #endif
- #endif
- return res;
- }
- static int test_wolfSSL_SHA256_Transform(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
- #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
- !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
- !defined(WOLFSSL_KCAPI_HASH)
- byte input1[] = "";
- byte input2[] = "abc";
- byte local[WC_SHA256_BLOCK_SIZE];
- word32 sLen = 0;
- #ifdef BIG_ENDIAN_ORDER
- unsigned char output1[] =
- "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
- "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
- unsigned char output2[] =
- "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
- "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
- #else
- unsigned char output1[] =
- "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
- "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
- unsigned char output2[] =
- "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
- "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
- #endif
- union {
- wc_Sha256 native;
- SHA256_CTX compat;
- } sha256;
- XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
- XMEMSET(&local, 0, sizeof(local));
- /* sanity check */
- AssertIntEQ(SHA256_Transform(NULL, NULL), 0);
- AssertIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
- AssertIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
- AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
- AssertIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
- /* Init SHA256 CTX */
- AssertIntEQ(SHA256_Init(&sha256.compat), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(sha256.native.digest, output1,
- WC_SHA256_DIGEST_SIZE), 0);
- AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
- /* Init SHA256 CTX */
- AssertIntEQ(SHA256_Init(&sha256.compat), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(sha256.native.digest, output2,
- WC_SHA256_DIGEST_SIZE), 0);
- AssertIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
- res = TEST_RES_CHECK(1);
- #endif
- #endif
- return res;
- }
- static int test_wolfSSL_SHA256(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
- defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- unsigned char input[] =
- "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
- unsigned char output[] =
- "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
- "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
- "\x06\xC1";
- size_t inLen;
- byte hash[WC_SHA256_DIGEST_SIZE];
- inLen = XSTRLEN((char*)input);
- XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
- AssertNotNull(SHA256(input, inLen, hash));
- AssertIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SHA512_Transform(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
- #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
- !defined(WOLFSSL_KCAPI_HASH)
- byte input1[] = "";
- byte input2[] = "abc";
- byte local[WC_SHA512_BLOCK_SIZE];
- word32 sLen = 0;
- #ifdef BIG_ENDIAN_ORDER
- unsigned char output1[] =
- "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
- "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
- "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
- "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
- unsigned char output2[] =
- "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
- "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
- "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
- "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
- #else
- unsigned char output1[] =
- "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
- "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
- "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
- "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
- unsigned char output2[] =
- "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
- "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
- "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
- "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
- #endif
- union {
- wc_Sha512 native;
- SHA512_CTX compat;
- } sha512;
- XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
- XMEMSET(&local, 0, sizeof(local));
- /* sanity check */
- AssertIntEQ(SHA512_Transform(NULL, NULL), 0);
- AssertIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
- AssertIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
- AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
- AssertIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
- /* Init SHA512 CTX */
- AssertIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(sha512.native.digest, output1,
- WC_SHA512_DIGEST_SIZE), 0);
- AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
- /* Init SHA512 CTX */
- AssertIntEQ(SHA512_Init(&sha512.compat), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(sha512.native.digest, output2,
- WC_SHA512_DIGEST_SIZE), 0);
- AssertIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
- (void)input1;
- res = TEST_RES_CHECK(1);
- #endif
- #endif
- return res;
- }
- static int test_wolfSSL_X509_get_serialNumber(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
- ASN1_INTEGER* a;
- BIGNUM* bn;
- X509* x509;
- char *serialHex;
- byte serial[3];
- int serialSz;
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
- SSL_FILETYPE_PEM));
- AssertNotNull(a = X509_get_serialNumber(x509));
- /* check on value of ASN1 Integer */
- AssertNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
- /* test setting serial number and then retrieving it */
- AssertNotNull(a = ASN1_INTEGER_new());
- ASN1_INTEGER_set(a, 3);
- AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
- serialSz = sizeof(serial);
- AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(serialSz, 1);
- AssertIntEQ(serial[0], 3);
- ASN1_INTEGER_free(a);
- /* test setting serial number with 0's in it */
- serial[0] = 0x01;
- serial[1] = 0x00;
- serial[2] = 0x02;
- AssertNotNull(a = wolfSSL_ASN1_INTEGER_new());
- a->data[0] = ASN_INTEGER;
- a->data[1] = sizeof(serial);
- XMEMCPY(&a->data[2], serial, sizeof(serial));
- a->length = sizeof(serial) + 2;
- AssertIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
- XMEMSET(serial, 0, sizeof(serial));
- serialSz = sizeof(serial);
- AssertIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(serialSz, 3);
- AssertIntEQ(serial[0], 0x01);
- AssertIntEQ(serial[1], 0x00);
- AssertIntEQ(serial[2], 0x02);
- ASN1_INTEGER_free(a);
- X509_free(x509); /* free's a */
- AssertNotNull(serialHex = BN_bn2hex(bn));
- #ifndef WC_DISABLE_RADIX_ZERO_PAD
- AssertStrEQ(serialHex, "01");
- #else
- AssertStrEQ(serialHex, "1");
- #endif
- OPENSSL_free(serialHex);
- AssertIntEQ(BN_get_word(bn), 1);
- BN_free(bn);
- /* hard test free'ing with dynamic buffer to make sure there is no leaks */
- a = ASN1_INTEGER_new();
- if (a) {
- AssertNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
- DYNAMIC_TYPE_OPENSSL));
- a->isDynamic = 1;
- ASN1_INTEGER_free(a);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- AssertIntEQ(wolfSSL_add_all_algorithms(),WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OPENSSL_hexstr2buf(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- #define MAX_HEXSTR_BUFSZ 9
- #define NUM_CASES 5
- struct Output {
- const unsigned char buffer[MAX_HEXSTR_BUFSZ];
- long ret;
- };
- int i;
- int j;
- const char* inputs[NUM_CASES] = {
- "aabcd1357e",
- "01:12:23:34:a5:b6:c7:d8:e9",
- ":01:02",
- "012",
- ":ab:ac:d"
- };
- struct Output expectedOutputs[NUM_CASES] = {
- {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
- {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
- {{0x01, 0x02}, 2},
- {{0x00}, 0},
- {{0x00}, 0}
- };
- long len = 0;
- unsigned char* returnedBuf = NULL;
- for (i = 0; i < NUM_CASES; ++i) {
- returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
- if (returnedBuf == NULL) {
- AssertIntEQ(expectedOutputs[i].ret, 0);
- continue;
- }
- AssertIntEQ(expectedOutputs[i].ret, len);
- for (j = 0; j < len; ++j) {
- AssertIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
- }
- OPENSSL_free(returnedBuf);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_STRING_print_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
- #ifndef NO_BIO
- ASN1_STRING* asn_str;
- const char data[] = "Hello wolfSSL!";
- ASN1_STRING* esc_str;
- const char esc_data[] = "a+;<>";
- BIO *bio;
- unsigned long flags;
- int p_len;
- unsigned char rbuf[255];
- /* setup */
- XMEMSET(rbuf, 0, 255);
- bio = BIO_new(BIO_s_mem());
- BIO_set_write_buf_size(bio,255);
- asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
- ASN1_STRING_set(asn_str, (const void*)data, sizeof(data));
- esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
- ASN1_STRING_set(esc_str, (const void*)esc_data, sizeof(esc_data));
- /* no flags */
- XMEMSET(rbuf, 0, 255);
- flags = 0;
- p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
- AssertIntEQ(p_len, 15);
- BIO_read(bio, (void*)rbuf, 15);
- AssertStrEQ((char*)rbuf, "Hello wolfSSL!");
- /* RFC2253 Escape */
- XMEMSET(rbuf, 0, 255);
- flags = ASN1_STRFLGS_ESC_2253;
- p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags);
- AssertIntEQ(p_len, 9);
- BIO_read(bio, (void*)rbuf, 9);
- AssertStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
- /* Show type */
- XMEMSET(rbuf, 0, 255);
- flags = ASN1_STRFLGS_SHOW_TYPE;
- p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
- AssertIntEQ(p_len, 28);
- BIO_read(bio, (void*)rbuf, 28);
- AssertStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
- /* Dump All */
- XMEMSET(rbuf, 0, 255);
- flags = ASN1_STRFLGS_DUMP_ALL;
- p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
- AssertIntEQ(p_len, 31);
- BIO_read(bio, (void*)rbuf, 31);
- AssertStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
- /* Dump Der */
- XMEMSET(rbuf, 0, 255);
- flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
- p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
- AssertIntEQ(p_len, 35);
- BIO_read(bio, (void*)rbuf, 35);
- AssertStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
- /* Dump All + Show type */
- XMEMSET(rbuf, 0, 255);
- flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
- p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags);
- AssertIntEQ(p_len, 44);
- BIO_read(bio, (void*)rbuf, 44);
- AssertStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
- BIO_free(bio);
- ASN1_STRING_free(asn_str);
- ASN1_STRING_free(esc_str);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_BIO */
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
- WOLFSSL_ASN1_TIME *t;
- WOLFSSL_ASN1_TIME *out;
- WOLFSSL_ASN1_TIME *gtime;
- int tlen = 0;
- unsigned char *data;
- /* UTC Time test */
- AssertNotNull(t = wolfSSL_ASN1_TIME_new());
- XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
- AssertNotNull(out = wolfSSL_ASN1_TIME_new());
- t->type = ASN_UTC_TIME;
- t->length = ASN_UTC_TIME_SIZE;
- XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
- tlen = wolfSSL_ASN1_TIME_get_length(t);
- AssertIntEQ(tlen, ASN_UTC_TIME_SIZE);
- data = wolfSSL_ASN1_TIME_get_data(t);
- AssertStrEQ((char*)data, "050727123456Z");
- gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
- AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
- AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
- AssertStrEQ((char*)gtime->data, "20050727123456Z");
- /* Generalized Time test */
- XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
- XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE);
- XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE);
- t->type = ASN_GENERALIZED_TIME;
- t->length = ASN_GENERALIZED_TIME_SIZE;
- XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
- tlen = wolfSSL_ASN1_TIME_get_length(t);
- AssertIntEQ(tlen, ASN_GENERALIZED_TIME_SIZE);
- data = wolfSSL_ASN1_TIME_get_data(t);
- AssertStrEQ((char*)data, "20050727123456Z");
- gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out);
- AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
- AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
- AssertStrEQ((char*)gtime->data, "20050727123456Z");
- XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- /* Null parameter test */
- XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE);
- gtime = NULL;
- out = NULL;
- t->type = ASN_UTC_TIME;
- t->length = ASN_UTC_TIME_SIZE;
- XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
- AssertNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
- AssertIntEQ(gtime->type, ASN_GENERALIZED_TIME);
- AssertIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
- AssertStrEQ((char*)gtime->data, "20050727123456Z");
- XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_CA_num(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
- defined(HAVE_ECC) && !defined(NO_RSA)
- WOLFSSL_X509_STORE *store;
- WOLFSSL_X509 *x509_1, *x509_2;
- int ca_num = 0;
- store = wolfSSL_X509_STORE_new();
- x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
- wolfSSL_X509_STORE_add_cert(store, x509_1);
- ca_num = wolfSSL_X509_CA_num(store);
- AssertIntEQ(ca_num, 1);
- x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile, WOLFSSL_FILETYPE_PEM);
- wolfSSL_X509_STORE_add_cert(store, x509_2);
- ca_num = wolfSSL_X509_CA_num(store);
- AssertIntEQ(ca_num, 2);
- wolfSSL_X509_free(x509_1);
- wolfSSL_X509_free(x509_2);
- wolfSSL_X509_STORE_free(store);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_check_ca(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- WOLFSSL_X509 *x509;
- x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
- AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_check_ip_asc(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- WOLFSSL_X509 *x509;
- x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
- #if 0
- /* TODO: add cert gen for testing positive case */
- AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
- #endif
- AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
- AssertIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_make_cert(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
- int ret;
- Cert cert;
- CertName name;
- RsaKey key;
- WC_RNG rng;
- byte der[FOURK_BUF];
- word32 idx;
- const byte mySerial[8] = {1,2,3,4,5,6,7,8};
- #ifdef OPENSSL_EXTRA
- const unsigned char* pt;
- int certSz;
- X509* x509;
- X509_NAME* x509name;
- X509_NAME_ENTRY* entry;
- ASN1_STRING* entryValue;
- #endif
- XMEMSET(&name, 0, sizeof(CertName));
- /* set up cert name */
- XMEMCPY(name.country, "US", sizeof("US"));
- name.countryEnc = CTC_PRINTABLE;
- XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
- name.stateEnc = CTC_UTF8;
- XMEMCPY(name.locality, "Portland", sizeof("Portland"));
- name.localityEnc = CTC_UTF8;
- XMEMCPY(name.sur, "Test", sizeof("Test"));
- name.surEnc = CTC_UTF8;
- XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
- name.orgEnc = CTC_UTF8;
- XMEMCPY(name.unit, "Development", sizeof("Development"));
- name.unitEnc = CTC_UTF8;
- XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
- name.commonNameEnc = CTC_UTF8;
- XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
- name.serialDevEnc = CTC_PRINTABLE;
- XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
- name.userIdEnc = CTC_PRINTABLE;
- #ifdef WOLFSSL_MULTI_ATTRIB
- #if CTC_MAX_ATTRIB > 2
- {
- NameAttrib* n;
- n = &name.name[0];
- n->id = ASN_DOMAIN_COMPONENT;
- n->type = CTC_UTF8;
- n->sz = sizeof("com");
- XMEMCPY(n->value, "com", sizeof("com"));
- n = &name.name[1];
- n->id = ASN_DOMAIN_COMPONENT;
- n->type = CTC_UTF8;
- n->sz = sizeof("wolfssl");
- XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
- }
- #endif
- #endif /* WOLFSSL_MULTI_ATTRIB */
- AssertIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
- #ifndef HAVE_FIPS
- AssertIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
- #else
- AssertIntEQ(wc_InitRng(&rng), 0);
- #endif
- /* load test RSA key */
- idx = 0;
- #if defined(USE_CERT_BUFFERS_1024)
- AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
- sizeof_server_key_der_1024), 0);
- #elif defined(USE_CERT_BUFFERS_2048)
- AssertIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
- sizeof_server_key_der_2048), 0);
- #else
- /* error case, no RSA key loaded, happens later */
- (void)idx;
- #endif
- XMEMSET(&cert, 0 , sizeof(Cert));
- AssertIntEQ(wc_InitCert(&cert), 0);
- XMEMCPY(&cert.subject, &name, sizeof(CertName));
- XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
- cert.serialSz = (int)sizeof(mySerial);
- cert.isCA = 1;
- #ifndef NO_SHA256
- cert.sigType = CTC_SHA256wRSA;
- #else
- cert.sigType = CTC_SHAwRSA;
- #endif
- /* add SKID from the Public Key */
- AssertIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
- /* add AKID from the Public Key */
- AssertIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
- ret = 0;
- do {
- #if defined(WOLFSSL_ASYNC_CRYPT)
- ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
- #endif
- if (ret >= 0) {
- ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
- }
- } while (ret == WC_PENDING_E);
- AssertIntGT(ret, 0);
- #ifdef OPENSSL_EXTRA
- /* der holds a certificate with DC's now check X509 parsing of it */
- certSz = ret;
- pt = der;
- AssertNotNull(x509 = d2i_X509(NULL, &pt, certSz));
- AssertNotNull(x509name = X509_get_subject_name(x509));
- #ifdef WOLFSSL_MULTI_ATTRIB
- AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
- -1)), 5);
- AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
- idx)), 6);
- AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
- idx)), -1);
- #endif /* WOLFSSL_MULTI_ATTRIB */
- /* compare DN at index 0 */
- AssertNotNull(entry = X509_NAME_get_entry(x509name, 0));
- AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
- AssertIntEQ(ASN1_STRING_length(entryValue), 2);
- AssertStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
- #ifdef WOLFSSL_MULTI_ATTRIB
- /* get first and second DC and compare result */
- AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
- -1)), 5);
- AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
- AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
- AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
- AssertIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
- idx)), 6);
- AssertNotNull(entry = X509_NAME_get_entry(x509name, idx));
- AssertNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
- AssertStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
- #endif /* WOLFSSL_MULTI_ATTRIB */
- /* try invalid index locations for regression test and sanity check */
- AssertNull(entry = X509_NAME_get_entry(x509name, 11));
- AssertNull(entry = X509_NAME_get_entry(x509name, 20));
- X509_free(x509);
- #endif /* OPENSSL_EXTRA */
- wc_FreeRsaKey(&key);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_get_version(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- WOLFSSL_X509 *x509;
- x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_DES_ncbc(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
- const_DES_cblock myDes;
- DES_cblock iv = {1};
- DES_key_schedule key = {0};
- unsigned char msg[] = "hello wolfssl";
- unsigned char out[DES_BLOCK_SIZE * 2] = {0};
- unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
- unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
- unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
- /* partial block test */
- DES_set_key(&key, &myDes);
- DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
- AssertIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
- AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
- DES_set_key(&key, &myDes);
- XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
- *((byte*)&iv) = 1;
- DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
- AssertIntEQ(XMEMCMP(msg, pln, 3), 0);
- AssertIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
- /* full block test */
- DES_set_key(&key, &myDes);
- XMEMSET(pln, 0, DES_BLOCK_SIZE);
- XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
- *((byte*)&iv) = 1;
- DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
- AssertIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
- AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
- DES_set_key(&key, &myDes);
- XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
- *((byte*)&iv) = 1;
- DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
- AssertIntEQ(XMEMCMP(msg, pln, 8), 0);
- AssertIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_AES_cbc_encrypt(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA)
- AES_KEY aes;
- AES_KEY* aesN = NULL;
- size_t len = 0;
- size_t lenB = 0;
- int keySz0 = 0;
- int keySzN = -1;
- byte out[AES_BLOCK_SIZE] = {0};
- byte* outN = NULL;
- /* Test vectors retrieved from:
- * <begin URL>
- * https://csrc.nist.gov/
- * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
- * documents/aes/KAT_AES.zip
- * </end URL>
- */
- const byte* pt128N = NULL;
- byte* key128N = NULL;
- byte* iv128N = NULL;
- byte iv128tmp[AES_BLOCK_SIZE] = {0};
- const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
- const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
- 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
- const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
- byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
- 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
- len = sizeof(pt128);
- #define STRESS_T(a, b, c, d, e, f, g, h, i) \
- wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
- AssertIntNE(XMEMCMP(b, g, h), i)
- #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
- /* Stressing wolfSSL_AES_cbc_encrypt() */
- STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
- STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
- wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
- AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
- wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
- AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
- STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
- /* Stressing wolfSSL_AES_set_encrypt_key */
- AssertIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
- AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
- AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
- AssertIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
- /* Stressing wolfSSL_AES_set_decrypt_key */
- AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
- AssertIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
- AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
- AssertIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
- #ifdef WOLFSSL_AES_128
- /* wolfSSL_AES_cbc_encrypt() 128-bit */
- XMEMSET(out, 0, AES_BLOCK_SIZE);
- RESET_IV(iv128tmp, iv128);
- AssertIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
- wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
- AssertIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
- #ifdef HAVE_AES_DECRYPT
- /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
- XMEMSET(out, 0, AES_BLOCK_SIZE);
- RESET_IV(iv128tmp, iv128);
- len = sizeof(ct128);
- AssertIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
- wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
- AssertIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
- #endif
- #endif /* WOLFSSL_AES_128 */
- #ifdef WOLFSSL_AES_192
- {
- /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
- * Appendix F.2.3 */
- byte iv192tmp[AES_BLOCK_SIZE] = {0};
- const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
- 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
- const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
- 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
- const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
- 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
- byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
- 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
- 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
- len = sizeof(pt192);
- /* wolfSSL_AES_cbc_encrypt() 192-bit */
- XMEMSET(out, 0, AES_BLOCK_SIZE);
- RESET_IV(iv192tmp, iv192);
- AssertIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
- wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
- AssertIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
- #ifdef HAVE_AES_DECRYPT
- /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
- len = sizeof(ct192);
- RESET_IV(iv192tmp, iv192);
- XMEMSET(out, 0, AES_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
- wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
- AssertIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
- #endif
- }
- #endif /* WOLFSSL_AES_192 */
- #ifdef WOLFSSL_AES_256
- {
- /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
- * Appendix F.2.5 */
- byte iv256tmp[AES_BLOCK_SIZE] = {0};
- const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
- 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
- const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
- 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
- const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
- 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
- byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
- 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
- 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
- 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
- len = sizeof(pt256);
- /* wolfSSL_AES_cbc_encrypt() 256-bit */
- XMEMSET(out, 0, AES_BLOCK_SIZE);
- RESET_IV(iv256tmp, iv256);
- AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
- wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
- AssertIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
- #ifdef HAVE_AES_DECRYPT
- /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
- len = sizeof(ct256);
- RESET_IV(iv256tmp, iv256);
- XMEMSET(out, 0, AES_BLOCK_SIZE);
- AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
- wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
- AssertIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
- #endif
- #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- {
- byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
- byte wrapPlain[sizeof(key256)] = { 0 };
- byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
- /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
- AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
- AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
- 15), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
- sizeof(key256)), sizeof(wrapCipher));
- /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
- AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
- AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
- 23), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
- sizeof(wrapCipher)), sizeof(wrapPlain));
- AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
- XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
- XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
- /* wolfSSL_AES_wrap_key() 256-bit custom iv */
- AssertIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
- AssertIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
- sizeof(key256)), sizeof(wrapCipher));
- /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
- AssertIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
- AssertIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
- sizeof(wrapCipher)), sizeof(wrapPlain));
- AssertIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
- }
- #endif /* HAVE_AES_KEYWRAP */
- }
- #endif /* WOLFSSL_AES_256 */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CRYPTO_cts128(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) \
- && defined(HAVE_CTS)
- byte tmp[64]; /* Largest vector size */
- /* Test vectors taken form RFC3962 Appendix B */
- const testVector vects[] = {
- {
- "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
- "\x20",
- "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
- "\x97",
- 17, 17
- },
- {
- "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
- "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
- "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
- "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
- 31, 31
- },
- {
- "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
- "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
- "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
- "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
- 32, 32
- },
- {
- "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
- "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
- "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
- "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
- "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
- "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
- 47, 47
- },
- {
- "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
- "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
- "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
- "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
- "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
- "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
- 48, 48
- },
- {
- "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
- "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
- "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
- "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
- "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
- "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
- "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
- "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
- 64, 64
- }
- };
- byte keyBytes[AES_128_KEY_SIZE] = {
- 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
- 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
- };
- size_t i;
- XMEMSET(tmp, 0, sizeof(tmp));
- for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
- AES_KEY encKey;
- AES_KEY decKey;
- byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
- XMEMSET(iv, 0, sizeof(iv));
- AssertIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &encKey), 0);
- AssertIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8, &decKey), 0);
- AssertIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
- tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
- vects[i].outLen);
- AssertIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
- XMEMSET(iv, 0, sizeof(iv));
- AssertIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
- tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
- vects[i].inLen);
- AssertIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
- }
- res = TEST_RES_CHECK(1);
- #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
- return res;
- }
- #if defined(OPENSSL_ALL)
- #if !defined(NO_ASN)
- static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA)
- WOLFSSL_X509* x509;
- WOLFSSL_X509_NAME* subject;
- WOLFSSL_X509_NAME_ENTRY* e;
- WOLFSSL_ASN1_STRING* a;
- FILE* file;
- int idx = 0;
- char targetOutput[16] = "www.wolfssl.com";
- unsigned char* actual_output;
- int len = 0;
- int result = 0;
- AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
- fclose(file);
- /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
- AssertNotNull(subject = wolfSSL_X509_get_subject_name(x509));
- AssertIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
- NID_commonName, -1)), 5);
- AssertNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
- AssertNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
- AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
- result = strncmp((const char*)actual_output, targetOutput, len);
- AssertIntEQ(result, 0);
- /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
- AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
- WOLFSSL_FATAL_ERROR);
- /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
- AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
- WOLFSSL_FATAL_ERROR);
- /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
- AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
- WOLFSSL_FATAL_ERROR);
- wolfSSL_X509_free(x509);
- XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
- {
- ASN1_STRING* asn1str_test;
- ASN1_STRING* asn1str_answer;
- /* Each character is encoded using 4 bytes */
- char input[] = {
- 0, 0, 0, 'T',
- 0, 0, 0, 'e',
- 0, 0, 0, 's',
- 0, 0, 0, 't',
- };
- char output[] = "Test";
- AssertNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
- AssertIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
- AssertIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
- AssertNotNull(asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
- AssertIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
- AssertIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
- ASN1_STRING_free(asn1str_test);
- ASN1_STRING_free(asn1str_answer);
- return TEST_RES_CHECK(1);
- }
- #endif /* !defined(NO_ASN) */
- static int test_wolfSSL_sk_CIPHER_description(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA)
- const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
- int i,j,k;
- int numCiphers = 0;
- const SSL_METHOD *method = NULL;
- const SSL_CIPHER *cipher = NULL;
- STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
- SSL_CTX *ctx = NULL;
- SSL *ssl = NULL;
- char buf[256];
- char test_str[9] = "0000000";
- const char badStr[] = "unknown";
- const char certPath[] = "./certs/client-cert.pem";
- XMEMSET(buf, 0, sizeof(buf));
- AssertNotNull(method = TLSv1_2_client_method());
- AssertNotNull(ctx = SSL_CTX_new(method));
- SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
- SSL_CTX_set_verify_depth(ctx, 4);
- SSL_CTX_set_options(ctx, flags);
- AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
- WOLFSSL_SUCCESS);
- AssertNotNull(ssl = SSL_new(ctx));
- /* SSL_get_ciphers returns a stack of all configured ciphers
- * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
- */
- AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
- /* loop through the amount of supportedCiphers */
- numCiphers = sk_num(supportedCiphers);
- for (i = 0; i < numCiphers; ++i) {
- /* sk_value increments "sk->data.cipher->cipherOffset".
- * wolfSSL_sk_CIPHER_description sets the description for
- * the cipher based on the provided offset.
- */
- if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
- SSL_CIPHER_description(cipher, buf, sizeof(buf));
- }
- /* Search cipher description string for "unknown" descriptor */
- for (j = 0; j < (int)XSTRLEN(buf); j++) {
- k = 0;
- while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
- test_str[k] = badStr[k];
- j++;
- k++;
- }
- }
- /* Fail if test_str == badStr == "unknown" */
- AssertStrNE(test_str,badStr);
- }
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_get_ciphers_compat(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA)
- const SSL_METHOD *method = NULL;
- const char certPath[] = "./certs/client-cert.pem";
- STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
- SSL_CTX *ctx = NULL;
- WOLFSSL *ssl = NULL;
- const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
- method = SSLv23_client_method();
- AssertNotNull(method);
- ctx = SSL_CTX_new(method);
- AssertNotNull(ctx);
- SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
- SSL_CTX_set_verify_depth(ctx, 4);
- SSL_CTX_set_options(ctx, flags);
- AssertIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
- WOLFSSL_SUCCESS);
- AssertNotNull(ssl = SSL_new(ctx));
- /* Test Bad NULL input */
- AssertNull(supportedCiphers = SSL_get_ciphers(NULL));
- /* Test for Good input */
- AssertNotNull(supportedCiphers = SSL_get_ciphers(ssl));
- /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
- * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_PUBKEY_get(void)
- {
- WOLFSSL_X509_PUBKEY pubkey;
- WOLFSSL_X509_PUBKEY* key;
- WOLFSSL_EVP_PKEY evpkey ;
- WOLFSSL_EVP_PKEY* evpPkey;
- WOLFSSL_EVP_PKEY* retEvpPkey;
- XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
- XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
- key = &pubkey;
- evpPkey = &evpkey;
- evpPkey->type = WOLFSSL_SUCCESS;
- key->pkey = evpPkey;
- AssertNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
- AssertIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
- AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
- key->pkey = NULL;
- AssertNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
- return TEST_RES_CHECK(retEvpPkey == NULL);
- }
- static int test_wolfSSL_EC_KEY_dup(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || \
- defined(OPENSSL_EXTRA_X509_SMALL))
- WOLFSSL_EC_KEY* ecKey;
- WOLFSSL_EC_KEY* dupKey;
- ecc_key* srcKey;
- ecc_key* destKey;
- AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
- /* Valid cases */
- AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
- AssertIntEQ(EC_KEY_check_key(dupKey), 1);
- /* Compare pubkey */
- srcKey = (ecc_key*)ecKey->internal;
- destKey = (ecc_key*)dupKey->internal;
- AssertIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
- /* compare EC_GROUP */
- AssertIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
- /* compare EC_POINT */
- AssertIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
- dupKey->pub_key, NULL), MP_EQ);
- /* compare BIGNUM */
- AssertIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
- wolfSSL_EC_KEY_free(dupKey);
- /* Invalid cases */
- /* NULL key */
- AssertNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
- /* NULL ecc_key */
- wc_ecc_free((ecc_key*)ecKey->internal);
- XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
- ecKey->internal = NULL; /* Set ecc_key to NULL */
- AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
- wolfSSL_EC_KEY_free(ecKey);
- wolfSSL_EC_KEY_free(dupKey);
- /* NULL Group */
- AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
- wolfSSL_EC_GROUP_free(ecKey->group);
- ecKey->group = NULL; /* Set group to NULL */
- AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
- wolfSSL_EC_KEY_free(ecKey);
- wolfSSL_EC_KEY_free(dupKey);
- /* NULL public key */
- AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
- wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
- ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
- AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
- wolfSSL_EC_POINT_free(ecKey->pub_key);
- ecKey->pub_key = NULL; /* Set pub_key to NULL */
- AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
- wolfSSL_EC_KEY_free(ecKey);
- wolfSSL_EC_KEY_free(dupKey);
- /* NULL private key */
- AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
- wolfSSL_BN_free(ecKey->priv_key);
- ecKey->priv_key = NULL; /* Set priv_key to NULL */
- AssertNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
- wolfSSL_EC_KEY_free(ecKey);
- wolfSSL_EC_KEY_free(dupKey);
- /* Test EC_KEY_up_ref */
- AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
- /* reference count doesn't follow duplicate */
- AssertNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
- AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
- AssertIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
- wolfSSL_EC_KEY_free(dupKey); /* 3 */
- wolfSSL_EC_KEY_free(dupKey); /* 2 */
- wolfSSL_EC_KEY_free(dupKey); /* 1, free */
- wolfSSL_EC_KEY_free(ecKey); /* 2 */
- wolfSSL_EC_KEY_free(ecKey); /* 1, free */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
- {
- int res = TEST_SKIPPED;
- #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
- DSA *dsa = NULL;
- DSA *setDsa = NULL;
- EVP_PKEY *pkey = NULL;
- EVP_PKEY *set1Pkey = NULL;
- SHA_CTX sha;
- byte signature[DSA_SIG_SIZE];
- byte hash[WC_SHA_DIGEST_SIZE];
- word32 bytes;
- int answer;
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* dsaKeyDer = dsa_key_der_1024;
- int dsaKeySz = sizeof_dsa_key_der_1024;
- byte tmp[ONEK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
- bytes = dsaKeySz;
- #elif defined(USE_CERT_BUFFERS_2048)
- const unsigned char* dsaKeyDer = dsa_key_der_2048;
- int dsaKeySz = sizeof_dsa_key_der_2048;
- byte tmp[TWOK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
- bytes = dsaKeySz;
- #else
- byte tmp[TWOK_BUF];
- const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
- int dsaKeySz;
- XMEMSET(tmp, 0, sizeof(tmp));
- XFILE fp = XFOPEN("./certs/dsa2048.der", "rb");
- if (fp == XBADFILE) {
- return WOLFSSL_BAD_FILE;
- }
- dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp);
- XFCLOSE(fp);
- #endif /* END USE_CERT_BUFFERS_1024 */
- /* Create hash to later Sign and Verify */
- AssertIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
- AssertIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
- AssertIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
- /* Initialize pkey with der format dsa key */
- AssertNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey,
- &dsaKeyDer ,(long)dsaKeySz));
- /* Test wolfSSL_EVP_PKEY_get1_DSA */
- /* Should Fail: NULL argument */
- AssertNull(dsa = EVP_PKEY_get0_DSA(NULL));
- AssertNull(dsa = EVP_PKEY_get1_DSA(NULL));
- /* Should Pass: Initialized pkey argument */
- AssertNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
- AssertNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
- #ifdef USE_CERT_BUFFERS_1024
- AssertIntEQ(DSA_bits(dsa), 1024);
- #else
- AssertIntEQ(DSA_bits(dsa), 2048);
- #endif
- /* Sign */
- AssertIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
- /* Verify. */
- AssertIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
- WOLFSSL_SUCCESS);
- /* Test wolfSSL_EVP_PKEY_set1_DSA */
- /* Should Fail: set1Pkey not initialized */
- AssertIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
- /* Initialize set1Pkey */
- set1Pkey = EVP_PKEY_new();
- /* Should Fail Verify: setDsa not initialized from set1Pkey */
- AssertIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
- WOLFSSL_SUCCESS);
- /* Should Pass: set dsa into set1Pkey */
- AssertIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
- DSA_free(dsa);
- DSA_free(setDsa);
- EVP_PKEY_free(pkey);
- EVP_PKEY_free(set1Pkey);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
- return res;
- } /* END test_EVP_PKEY_set1_get1_DSA */
- static int test_wolfSSL_DSA_SIG(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_FIPS)
- DSA *dsa = NULL;
- DSA *dsa2 = NULL;
- DSA_SIG *sig = NULL;
- const BIGNUM *p = NULL;
- const BIGNUM *q = NULL;
- const BIGNUM *g = NULL;
- const BIGNUM *pub = NULL;
- const BIGNUM *priv = NULL;
- const byte digest[WC_SHA_DIGEST_SIZE] = {0};
- AssertNotNull(dsa = DSA_generate_parameters(2048,
- NULL, 0, NULL, NULL, NULL, NULL));
- DSA_free(dsa);
- AssertNotNull(dsa = DSA_new());
- AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
- NULL, 0, NULL, NULL, NULL), 1);
- AssertIntEQ(DSA_generate_key(dsa), 1);
- DSA_get0_pqg(dsa, &p, &q, &g);
- DSA_get0_key(dsa, &pub, &priv);
- AssertNotNull(p = BN_dup(p));
- AssertNotNull(q = BN_dup(q));
- AssertNotNull(g = BN_dup(g));
- AssertNotNull(pub = BN_dup(pub));
- AssertNotNull(priv = BN_dup(priv));
- AssertNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
- AssertNotNull(dsa2 = DSA_new());
- AssertIntEQ(DSA_set0_pqg(dsa2, (BIGNUM*)p, (BIGNUM*)q, (BIGNUM*)g), 1);
- AssertIntEQ(DSA_set0_key(dsa2, (BIGNUM*)pub, (BIGNUM*)priv), 1);
- AssertIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
- DSA_free(dsa);
- DSA_free(dsa2);
- DSA_SIG_free(sig);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_ECC
- WOLFSSL_EC_KEY *ecKey = NULL;
- WOLFSSL_EC_KEY *ecGet1 = NULL;
- EVP_PKEY *pkey = NULL;
- AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
- AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
- /* Should fail since ecKey is empty */
- AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
- AssertIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
- /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
- AssertNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
- AssertNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
- wolfSSL_EC_KEY_free(ecKey);
- wolfSSL_EC_KEY_free(ecGet1);
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ECC */
- return res;
- } /* END test_EVP_PKEY_set1_get1_EC_KEY */
- static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
- DH *dh = NULL;
- DH *setDh = NULL;
- EVP_PKEY *pkey = NULL;
- FILE* f = NULL;
- unsigned char buf[4096];
- const unsigned char* pt = buf;
- const char* dh2048 = "./certs/dh2048.der";
- long len = 0;
- int code = -1;
- XMEMSET(buf, 0, sizeof(buf));
- f = XFOPEN(dh2048, "rb");
- AssertTrue(f != XBADFILE);
- len = (long)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- /* Load dh2048.der into DH with internal format */
- AssertNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
- AssertIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
- AssertIntEQ(code, 0);
- code = -1;
- pkey = wolfSSL_EVP_PKEY_new();
- /* Set DH into PKEY */
- AssertIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
- /* Get DH from PKEY */
- AssertNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
- AssertIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
- AssertIntEQ(code, 0);
- EVP_PKEY_free(pkey);
- DH_free(setDh);
- DH_free(dh);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
- return res;
- } /* END test_EVP_PKEY_set1_get1_DH */
- static int test_wolfSSL_CTX_ctrl(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- char caFile[] = "./certs/client-ca.pem";
- char clientFile[] = "./certs/client-cert.pem";
- SSL_CTX* ctx;
- X509* x509 = NULL;
- #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
- byte buf[6000];
- char file[] = "./certs/dsaparams.pem";
- XFILE f;
- int bytes;
- BIO* bio;
- DSA* dsa;
- DH* dh;
- #endif
- #ifdef HAVE_ECC
- WOLFSSL_EC_KEY* ecKey;
- #endif
- AssertNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
- x509 = wolfSSL_X509_load_certificate_file(caFile, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- AssertIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
- x509 = wolfSSL_X509_load_certificate_file(clientFile, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
- /* Initialize DH */
- f = XFOPEN(file, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- bio = BIO_new_mem_buf((void*)buf, bytes);
- AssertNotNull(bio);
- dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
- AssertNotNull(dsa);
- dh = wolfSSL_DSA_dup_DH(dsa);
- AssertNotNull(dh);
- #endif
- #ifdef HAVE_ECC
- /* Initialize WOLFSSL_EC_KEY */
- AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey),1);
- #endif
- #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
- /* additional test of getting EVP_PKEY key size from X509
- * Do not run with user RSA because wolfSSL_RSA_size is not currently
- * allowed with user RSA */
- {
- EVP_PKEY* pkey;
- #if defined(HAVE_ECC)
- X509* ecX509;
- #endif /* HAVE_ECC */
- AssertNotNull(pkey = X509_get_pubkey(x509));
- /* current RSA key is 2048 bit (256 bytes) */
- AssertIntEQ(EVP_PKEY_size(pkey), 256);
- EVP_PKEY_free(pkey);
- #if defined(HAVE_ECC)
- #if defined(USE_CERT_BUFFERS_256)
- AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
- cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
- SSL_FILETYPE_ASN1));
- #else
- AssertNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
- cliEccCertFile, SSL_FILETYPE_PEM));
- #endif
- AssertNotNull(pkey = X509_get_pubkey(ecX509));
- /* current ECC key is 256 bit (32 bytes) */
- AssertIntEQ(EVP_PKEY_size(pkey), 32);
- X509_free(ecX509);
- EVP_PKEY_free(pkey);
- #endif /* HAVE_ECC */
- }
- #endif /* !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA) */
- /* Tests should fail with passed in NULL pointer */
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,NULL),
- SSL_FAILURE);
- #if !defined(NO_DH) && !defined(NO_DSA)
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,NULL),
- SSL_FAILURE);
- #endif
- #ifdef HAVE_ECC
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,NULL),
- SSL_FAILURE);
- #endif
- /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
- * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
- */
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_EXTRA_CHAIN_CERT,0,x509),
- SSL_SUCCESS);
- /* Test with SSL_CTRL_OPTIONS
- * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
- */
- AssertTrue(wolfSSL_CTX_ctrl(ctx,SSL_CTRL_OPTIONS,SSL_OP_NO_TLSv1,NULL)
- == SSL_OP_NO_TLSv1);
- AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
- /* Test with SSL_CTRL_SET_TMP_DH
- * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
- */
- #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_DH,0,dh),
- SSL_SUCCESS);
- #endif
- /* Test with SSL_CTRL_SET_TMP_ECDH
- * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
- */
- #ifdef HAVE_ECC
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx,SSL_CTRL_SET_TMP_ECDH,0,ecKey),
- SSL_SUCCESS);
- #endif
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- AssertNull(SSL_CTX_get_default_passwd_cb(ctx));
- AssertNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
- #endif
- /* Test for min/max proto */
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
- 0, NULL), SSL_SUCCESS);
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
- TLS1_2_VERSION, NULL), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
- #endif
- #ifdef WOLFSSL_TLS13
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
- 0, NULL), SSL_SUCCESS);
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
- TLS1_3_VERSION, NULL), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
- TLS1_2_VERSION, NULL), SSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
- #endif
- #endif
- /* Cleanup and Pass */
- #if !defined(NO_DH) && !defined(NO_DSA)
- #ifndef NO_BIO
- BIO_free(bio);
- DSA_free(dsa);
- DH_free(dh);
- #endif
- #endif
- #ifdef HAVE_ECC
- wolfSSL_EC_KEY_free(ecKey);
- #endif
- SSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_assign(void)
- {
- int res = TEST_SKIPPED;
- int type;
- WOLFSSL_EVP_PKEY* pkey;
- #ifndef NO_RSA
- WOLFSSL_RSA* rsa;
- #endif
- #ifndef NO_DSA
- WOLFSSL_DSA* dsa;
- #endif
- #ifdef HAVE_ECC
- WOLFSSL_EC_KEY* ecKey;
- #endif
- (void)pkey;
- #ifndef NO_RSA
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- type = EVP_PKEY_RSA;
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertNotNull(rsa = wolfSSL_RSA_new());
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,rsa), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,rsa), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,rsa), WOLFSSL_SUCCESS);
- wolfSSL_EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- }
- #endif /* NO_RSA */
- #ifndef NO_DSA
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- type = EVP_PKEY_DSA;
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertNotNull(dsa = wolfSSL_DSA_new());
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,dsa), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,dsa), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,dsa), WOLFSSL_SUCCESS);
- wolfSSL_EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- }
- #endif /* NO_DSA */
- #ifdef HAVE_ECC
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- type = EVP_PKEY_EC;
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(NULL,type,ecKey), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,-1,ecKey), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign(pkey,type,ecKey), WOLFSSL_SUCCESS);
- wolfSSL_EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- }
- #endif /* HAVE_ECC */
- (void)type;
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_base_id(void)
- {
- WOLFSSL_EVP_PKEY* pkey;
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
- AssertIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
- EVP_PKEY_free(pkey);
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_PKEY_id(void)
- {
- WOLFSSL_EVP_PKEY* pkey;
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
- AssertIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
- EVP_PKEY_free(pkey);
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_PKEY_paramgen(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && \
- !defined(NO_ECC_SECP) && \
- /* This last bit is taken from ecc.c. It is the condition that
- * defines ECC256 */ \
- ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
- ECC_MIN_KEY_SZ <= 256)
- EVP_PKEY_CTX* ctx;
- EVP_PKEY* pkey = NULL;
- /* Test error conditions. */
- AssertIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
- AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
- AssertIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
- #ifndef NO_RSA
- EVP_PKEY_CTX_free(ctx);
- /* Parameter generation for RSA not supported yet. */
- AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
- AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
- #endif
- #ifdef HAVE_ECC
- EVP_PKEY_CTX_free(ctx);
- AssertNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
- AssertIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
- NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
- #endif
- EVP_PKEY_CTX_free(ctx);
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_keygen(void)
- {
- WOLFSSL_EVP_PKEY* pkey = NULL;
- EVP_PKEY_CTX* ctx = NULL;
- #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
- WOLFSSL_EVP_PKEY* params = NULL;
- DH* dh = NULL;
- const BIGNUM* pubkey = NULL;
- const BIGNUM* privkey = NULL;
- ASN1_INTEGER* asn1int = NULL;
- unsigned int length = 0;
- byte* derBuffer = NULL;
- #endif
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- /* Bad cases */
- AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
- /* Good case */
- AssertIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
- EVP_PKEY_CTX_free(ctx);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
- /* Test DH keygen */
- {
- AssertNotNull(params = wolfSSL_EVP_PKEY_new());
- AssertNotNull(dh = DH_get_2048_256());
- AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
- AssertNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
- AssertIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
- DH_free(dh);
- EVP_PKEY_CTX_free(ctx);
- EVP_PKEY_free(params);
- /* try exporting generated key to DER, to verify */
- AssertNotNull(dh = EVP_PKEY_get1_DH(pkey));
- DH_get0_key(dh, &pubkey, &privkey);
- AssertNotNull(pubkey);
- AssertNotNull(privkey);
- AssertNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
- AssertIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
- ASN1_INTEGER_free(asn1int);
- DH_free(dh);
- XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- EVP_PKEY_free(pkey);
- }
- #endif
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_PKEY_keygen_init(void)
- {
- WOLFSSL_EVP_PKEY* pkey;
- EVP_PKEY_CTX *ctx;
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- AssertIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
- EVP_PKEY_CTX_free(ctx);
- EVP_PKEY_free(pkey);
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
- WOLFSSL_EVP_PKEY* pkey;
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
- defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
- !defined(NO_FILESYSTEM)
- WOLFSSL_EVP_PKEY* params = NULL;
- WOLFSSL_EVP_PKEY* copy = NULL;
- DH* dh = NULL;
- BIGNUM* p1;
- BIGNUM* g1;
- BIGNUM* q1;
- BIGNUM* p2;
- BIGNUM* g2;
- BIGNUM* q2;
- /* create DH with DH_get_2048_256 params */
- AssertNotNull(params = wolfSSL_EVP_PKEY_new());
- AssertNotNull(dh = DH_get_2048_256());
- AssertIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
- DH_get0_pqg(dh, (const BIGNUM**)&p1,
- (const BIGNUM**)&q1,
- (const BIGNUM**)&g1);
- DH_free(dh);
- /* create DH with random generated DH params */
- AssertNotNull(copy = wolfSSL_EVP_PKEY_new());
- AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
- AssertIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
- DH_free(dh);
- AssertIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
- AssertNotNull(dh = EVP_PKEY_get1_DH(copy));
- AssertNotNull(dh->p);
- AssertNotNull(dh->g);
- AssertNotNull(dh->q);
- DH_get0_pqg(dh, (const BIGNUM**)&p2,
- (const BIGNUM**)&q2,
- (const BIGNUM**)&g2);
- AssertIntEQ(BN_cmp(p1, p2), 0);
- AssertIntEQ(BN_cmp(q1, q2), 0);
- AssertIntEQ(BN_cmp(g1, g2), 0);
- DH_free(dh);
- EVP_PKEY_free(copy);
- EVP_PKEY_free(params);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
- {
- WOLFSSL_EVP_PKEY* pkey;
- EVP_PKEY_CTX *ctx;
- int bits = 2048;
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- AssertIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
- WOLFSSL_SUCCESS);
- EVP_PKEY_CTX_free(ctx);
- EVP_PKEY_free(pkey);
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
- {
- /* This is large enough to be used for all key sizes */
- byte key[AES_256_KEY_SIZE] = {0};
- byte iv[AES_BLOCK_SIZE] = {0};
- int i, enumlen;
- EVP_CIPHER_CTX *ctx;
- const EVP_CIPHER *init;
- int enumArray[] = {
- #ifdef HAVE_AES_CBC
- NID_aes_128_cbc,
- #endif
- #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
- #ifdef HAVE_AESGCM
- NID_aes_128_gcm,
- #endif
- #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
- #ifdef WOLFSSL_AES_COUNTER
- NID_aes_128_ctr,
- #endif
- #ifndef NO_DES3
- NID_des_cbc,
- NID_des_ede3_cbc,
- #endif
- };
- int iv_lengths[] = {
- #ifdef HAVE_AES_CBC
- AES_BLOCK_SIZE,
- #endif
- #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
- #ifdef HAVE_AESGCM
- GCM_NONCE_MID_SZ,
- #endif
- #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
- #ifdef WOLFSSL_AES_COUNTER
- AES_BLOCK_SIZE,
- #endif
- #ifndef NO_DES3
- DES_BLOCK_SIZE,
- DES_BLOCK_SIZE,
- #endif
- };
- enumlen = (sizeof(enumArray)/sizeof(int));
- for (i = 0; i < enumlen; i++) {
- ctx = EVP_CIPHER_CTX_new();
- init = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
- EVP_CIPHER_CTX_free(ctx);
- }
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DES3)
- byte key[AES_256_KEY_SIZE] = {0};
- byte iv[AES_BLOCK_SIZE] = {0};
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- const EVP_CIPHER *init = EVP_des_ede3_cbc();
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), 24);
- EVP_CIPHER_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DES3)
- byte key[AES_256_KEY_SIZE] = {0};
- byte iv[AES_BLOCK_SIZE] = {0};
- int keylen;
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- const EVP_CIPHER *init = EVP_des_ede3_cbc();
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- keylen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, keylen),
- WOLFSSL_SUCCESS);
- EVP_CIPHER_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_AESGCM) && !defined(NO_DES3)
- byte key[DES3_KEY_SIZE] = {0};
- byte iv[DES_BLOCK_SIZE] = {0};
- int ivLen, keyLen;
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- const EVP_CIPHER *init = EVP_des_ede3_cbc();
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
- keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
- /* Bad cases */
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen), WOLFSSL_FAILURE);
- /* Good case */
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
- EVP_CIPHER_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
- {
- WOLFSSL_ENGINE* e = NULL;
- int id = 0;
- EVP_PKEY_CTX *ctx;
- AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
- EVP_PKEY_CTX_free(ctx);
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_rc4(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RC4)
- res = TEST_RES_CHECK(wolfSSL_EVP_rc4() != NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_enc_null(void)
- {
- return TEST_RES_CHECK(wolfSSL_EVP_enc_null() != NULL);
- }
- static int test_wolfSSL_EVP_rc2_cbc(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
- res = TEST_RES_CHECK(wolfSSL_EVP_rc2_cbc() == NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_mdc2(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_WOLFSSL_STUB)
- res = TEST_RES_CHECK(wolfSSL_EVP_mdc2() == NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_md4(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_MD4)
- res = TEST_RES_CHECK(wolfSSL_EVP_md4() != NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_aes_256_gcm(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_AESGCM
- res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_gcm() != NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_aes_192_gcm(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_AESGCM
- res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_gcm() != NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_aes_256_ccm(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_AESCCM
- res = TEST_RES_CHECK(wolfSSL_EVP_aes_256_ccm() != NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_aes_192_ccm(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_AESCCM
- res = TEST_RES_CHECK(wolfSSL_EVP_aes_192_ccm() != NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_aes_128_ccm(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_AESCCM
- res = TEST_RES_CHECK(wolfSSL_EVP_aes_128_ccm() != NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_ripemd160(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_WOLFSSL_STUB)
- res = TEST_RES_CHECK(wolfSSL_EVP_ripemd160() == NULL);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_get_digestbynid(void)
- {
- #ifndef NO_MD5
- AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
- #endif
- #ifndef NO_SHA
- AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
- #endif
- #ifndef NO_SHA256
- AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
- #endif
- AssertNull(wolfSSL_EVP_get_digestbynid(0));
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_MD_nid(void)
- {
- #ifndef NO_MD5
- AssertIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
- #endif
- #ifndef NO_SHA
- AssertIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
- #endif
- #ifndef NO_SHA256
- AssertIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
- #endif
- AssertIntEQ(EVP_MD_nid(NULL), NID_undef);
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC)
- WOLFSSL_EVP_PKEY* pkey;
- AssertNotNull(pkey = EVP_PKEY_new());
- AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_X_STATE(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DES3) && !defined(NO_RC4)
- byte key[DES3_KEY_SIZE] = {0};
- byte iv[DES_IV_SIZE] = {0};
- EVP_CIPHER_CTX *ctx;
- const EVP_CIPHER *init;
- /* Bad test cases */
- ctx = EVP_CIPHER_CTX_new();
- init = EVP_des_ede3_cbc();
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- AssertNull(wolfSSL_EVP_X_STATE(NULL));
- AssertNull(wolfSSL_EVP_X_STATE(ctx));
- EVP_CIPHER_CTX_free(ctx);
- /* Good test case */
- ctx = EVP_CIPHER_CTX_new();
- init = wolfSSL_EVP_rc4();
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- AssertNotNull(wolfSSL_EVP_X_STATE(ctx));
- EVP_CIPHER_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_X_STATE_LEN(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DES3) && !defined(NO_RC4)
- byte key[DES3_KEY_SIZE] = {0};
- byte iv[DES_IV_SIZE] = {0};
- EVP_CIPHER_CTX *ctx;
- const EVP_CIPHER *init;
- /* Bad test cases */
- ctx = EVP_CIPHER_CTX_new();
- init = EVP_des_ede3_cbc();
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
- AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
- EVP_CIPHER_CTX_free(ctx);
- /* Good test case */
- ctx = EVP_CIPHER_CTX_new();
- init = wolfSSL_EVP_rc4();
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
- EVP_CIPHER_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_CIPHER_block_size(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_AES_CBC
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
- #endif
- #ifdef WOLFSSL_AES_192
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
- #endif
- #ifdef WOLFSSL_AES_256
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
- #endif
- res = TEST_RES_CHECK(1);
- }
- #endif
- #ifdef HAVE_AESGCM
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
- #endif
- #ifdef WOLFSSL_AES_192
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
- #endif
- #ifdef WOLFSSL_AES_256
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
- #endif
- res = TEST_RES_CHECK(1);
- }
- #endif
- #ifdef HAVE_AESCCM
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
- #endif
- #ifdef WOLFSSL_AES_192
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
- #endif
- #ifdef WOLFSSL_AES_256
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
- #endif
- res = TEST_RES_CHECK(1);
- }
- #endif
- #ifdef WOLFSSL_AES_COUNTER
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
- #endif
- #ifdef WOLFSSL_AES_192
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
- #endif
- #ifdef WOLFSSL_AES_256
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
- #endif
- res = TEST_RES_CHECK(1);
- }
- #endif
- #ifdef HAVE_AES_ECB
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
- #endif
- #ifdef WOLFSSL_AES_192
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
- #endif
- #ifdef WOLFSSL_AES_256
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
- #endif
- res = TEST_RES_CHECK(1);
- }
- #endif
- #ifdef WOLFSSL_AES_OFB
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
- #endif
- #ifdef WOLFSSL_AES_192
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
- #endif
- #ifdef WOLFSSL_AES_256
- AssertIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
- #endif
- res = TEST_RES_CHECK(1);
- }
- #endif
- #ifndef NO_RC4
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
- res = TEST_RES_CHECK(1);
- }
- #endif
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
- res = TEST_RES_CHECK(1);
- }
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_CIPHER_iv_length(void)
- {
- int i, enumlen;
- int enumArray[] = {
- #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
- #ifdef WOLFSSL_AES_128
- NID_aes_128_cbc,
- #endif
- #ifdef WOLFSSL_AES_192
- NID_aes_192_cbc,
- #endif
- #ifdef WOLFSSL_AES_256
- NID_aes_256_cbc,
- #endif
- #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
- #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
- #ifdef HAVE_AESGCM
- #ifdef WOLFSSL_AES_128
- NID_aes_128_gcm,
- #endif
- #ifdef WOLFSSL_AES_192
- NID_aes_192_gcm,
- #endif
- #ifdef WOLFSSL_AES_256
- NID_aes_256_gcm,
- #endif
- #endif /* HAVE_AESGCM */
- #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
- #ifdef WOLFSSL_AES_COUNTER
- #ifdef WOLFSSL_AES_128
- NID_aes_128_ctr,
- #endif
- #ifdef WOLFSSL_AES_192
- NID_aes_192_ctr,
- #endif
- #ifdef WOLFSSL_AES_256
- NID_aes_256_ctr,
- #endif
- #endif
- #ifndef NO_DES3
- NID_des_cbc,
- NID_des_ede3_cbc,
- #endif
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- NID_chacha20_poly1305,
- #endif
- };
- int iv_lengths[] = {
- #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
- #ifdef WOLFSSL_AES_128
- AES_BLOCK_SIZE,
- #endif
- #ifdef WOLFSSL_AES_192
- AES_BLOCK_SIZE,
- #endif
- #ifdef WOLFSSL_AES_256
- AES_BLOCK_SIZE,
- #endif
- #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
- #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
- #ifdef HAVE_AESGCM
- #ifdef WOLFSSL_AES_128
- GCM_NONCE_MID_SZ,
- #endif
- #ifdef WOLFSSL_AES_192
- GCM_NONCE_MID_SZ,
- #endif
- #ifdef WOLFSSL_AES_256
- GCM_NONCE_MID_SZ,
- #endif
- #endif /* HAVE_AESGCM */
- #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
- #ifdef WOLFSSL_AES_COUNTER
- #ifdef WOLFSSL_AES_128
- AES_BLOCK_SIZE,
- #endif
- #ifdef WOLFSSL_AES_192
- AES_BLOCK_SIZE,
- #endif
- #ifdef WOLFSSL_AES_256
- AES_BLOCK_SIZE,
- #endif
- #endif
- #ifndef NO_DES3
- DES_BLOCK_SIZE,
- DES_BLOCK_SIZE,
- #endif
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- CHACHA20_POLY1305_AEAD_IV_SIZE,
- #endif
- };
- enumlen = (sizeof(enumArray)/sizeof(int));
- for (i = 0; i < enumlen; i++) {
- const EVP_CIPHER *c = EVP_get_cipherbynid(enumArray[i]);
- AssertIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
- }
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_SignInit_ex(void)
- {
- WOLFSSL_EVP_MD_CTX mdCtx;
- WOLFSSL_ENGINE* e = 0;
- const EVP_MD* md;
- md = "SHA256";
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_EVP_DigestFinal_ex(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_SHA256)
- WOLFSSL_EVP_MD_CTX mdCtx;
- unsigned int s = 0;
- unsigned char md[WC_SHA256_DIGEST_SIZE];
- unsigned char md2[WC_SHA256_DIGEST_SIZE];
- /* Bad Case */
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- #else
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
- #endif
- /* Good Case */
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_assign_DH(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DH) && \
- !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
- FILE* f = NULL;
- unsigned char buf[4096];
- const unsigned char* pt = buf;
- const char* params1 = "./certs/dh2048.der";
- long len = 0;
- WOLFSSL_DH* dh = NULL;
- WOLFSSL_EVP_PKEY* pkey;
- XMEMSET(buf, 0, sizeof(buf));
- f = XFOPEN(params1, "rb");
- AssertTrue(f != XBADFILE);
- len = (long)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
- AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- /* Bad cases */
- AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
- /* Good case */
- AssertIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- EVP_PKEY* pkey;
- EVP_PKEY_CTX* ctx;
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
- /* void */
- EVP_PKEY_CTX_free(ctx);
- AssertTrue(1);
- #else
- /* int */
- AssertIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
- #endif
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_param_check(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
- DH *dh = NULL;
- DH *setDh = NULL;
- EVP_PKEY *pkey = NULL;
- EVP_PKEY_CTX* ctx = NULL;
- FILE* f = NULL;
- unsigned char buf[512];
- const unsigned char* pt = buf;
- const char* dh2048 = "./certs/dh2048.der";
- long len = 0;
- int code = -1;
- XMEMSET(buf, 0, sizeof(buf));
- f = XFOPEN(dh2048, "rb");
- AssertTrue(f != XBADFILE);
- len = (long)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- /* Load dh2048.der into DH with internal format */
- AssertNotNull(setDh = d2i_DHparams(NULL, &pt, len));
- AssertIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
- AssertIntEQ(code, 0);
- code = -1;
- pkey = wolfSSL_EVP_PKEY_new();
- /* Set DH into PKEY */
- AssertIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
- /* create ctx from pkey */
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- AssertIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
- /* */
- /* TO DO invlaid case */
- /* */
- EVP_PKEY_CTX_free(ctx);
- EVP_PKEY_free(pkey);
- DH_free(setDh);
- DH_free(dh);
- res = TEST_RES_CHECK(1);
- #endif
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_BytesToKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_AES) && defined(HAVE_AES_CBC)
- byte key[AES_BLOCK_SIZE] = {0};
- byte iv[AES_BLOCK_SIZE] = {0};
- int sz = 5;
- int count = 0;
- const EVP_MD* md = "SHA256";
- const EVP_CIPHER *type;
- const unsigned char *salt = (unsigned char *)"salt1234";
- const byte data[] = {
- 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
- 0x72,0x6c,0x64
- };
- type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
- /* Bad cases */
- AssertIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
- 0);
- AssertIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
- 16);
- md = "2";
- AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
- WOLFSSL_FAILURE);
- /* Good case */
- md = "SHA256";
- AssertIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
- 16);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_evp_cipher_aes_gcm(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
- !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
- (HAVE_FIPS_VERSION >= 2)))
- /*
- * This test checks data at various points in the encrypt/decrypt process
- * against known values produced using the same test with OpenSSL. This
- * interop testing is critical for verifying the correctness of our
- * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
- * a flow supported by OpenSSL that uses the control command
- * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
- * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
- * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
- * wolfSSL OpenSSH clients because there was a bug in this flow that
- * happened to "cancel out" if both sides of the connection had the bug.
- */
- enum {
- NUM_ENCRYPTIONS = 3,
- AAD_SIZE = 4
- };
- byte plainText1[] = {
- 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
- 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
- };
- byte plainText2[] = {
- 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
- 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
- 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
- };
- byte plainText3[] = {
- 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
- 0x5b, 0x57, 0x10
- };
- byte* plainTexts[NUM_ENCRYPTIONS] = {
- plainText1,
- plainText2,
- plainText3
- };
- const int plainTextSzs[NUM_ENCRYPTIONS] = {
- sizeof(plainText1),
- sizeof(plainText2),
- sizeof(plainText3)
- };
- byte aad1[AAD_SIZE] = {
- 0x00, 0x00, 0x00, 0x01
- };
- byte aad2[AAD_SIZE] = {
- 0x00, 0x00, 0x00, 0x10
- };
- byte aad3[AAD_SIZE] = {
- 0x00, 0x00, 0x01, 0x00
- };
- byte* aads[NUM_ENCRYPTIONS] = {
- aad1,
- aad2,
- aad3
- };
- const byte iv[GCM_NONCE_MID_SZ] = {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
- };
- byte currentIv[GCM_NONCE_MID_SZ];
- const byte key[] = {
- 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
- 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
- 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
- };
- const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
- {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
- 0xEF
- },
- {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
- 0xF0
- },
- {
- 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
- 0xF1
- }
- };
- const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
- {
- 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
- 0x3D, 0x32, 0x18, 0x34, 0xA9
- },
- {
- 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
- 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
- },
- {
- 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
- 0x5B, 0xEC, 0x52, 0x49, 0x32,
- }
- };
- const byte expCipherText1[] = {
- 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
- 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
- 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
- };
- const byte expCipherText2[] = {
- 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
- 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
- 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
- };
- const byte expCipherText3[] = {
- 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
- 0x80, 0x5E, 0x6B,
- };
- const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
- expCipherText1,
- expCipherText2,
- expCipherText3
- };
- byte* cipherText;
- byte* calcPlainText;
- byte tag[AES_BLOCK_SIZE];
- EVP_CIPHER_CTX* encCtx = NULL;
- EVP_CIPHER_CTX* decCtx = NULL;
- int i, j, outl;
- /****************************************************/
- for (i = 0; i < 3; ++i) {
- AssertNotNull(encCtx = EVP_CIPHER_CTX_new());
- AssertNotNull(decCtx = EVP_CIPHER_CTX_new());
- /* First iteration, set key before IV. */
- if (i == 0) {
- AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
- SSL_SUCCESS);
- /*
- * The call to EVP_CipherInit below (with NULL key) should clear the
- * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
- * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
- * behavior.
- */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
- (void*)iv), SSL_SUCCESS);
- AssertIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
- SSL_SUCCESS);
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
- currentIv), SSL_FAILURE);
- AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
- SSL_SUCCESS);
- AssertIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
- SSL_SUCCESS);
- }
- /* Second iteration, IV before key. */
- else {
- AssertIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
- SSL_SUCCESS);
- AssertIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
- SSL_SUCCESS);
- AssertIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
- SSL_SUCCESS);
- AssertIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
- SSL_SUCCESS);
- }
- /*
- * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
- * been issued first.
- */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
- currentIv), SSL_FAILURE);
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
- (void*)iv), SSL_SUCCESS);
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
- (void*)iv), SSL_SUCCESS);
- for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
- /*************** Encrypt ***************/
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
- currentIv), SSL_SUCCESS);
- /* Check current IV against expected. */
- AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
- /* Add AAD. */
- if (i == 2) {
- /* Test streaming API. */
- AssertIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
- AAD_SIZE), SSL_SUCCESS);
- }
- else {
- AssertIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
- AAD_SIZE);
- }
- AssertNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- /* Encrypt plaintext. */
- if (i == 2) {
- AssertIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
- plainTexts[j], plainTextSzs[j]),
- SSL_SUCCESS);
- }
- else {
- AssertIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
- plainTextSzs[j]), plainTextSzs[j]);
- }
- if (i == 2) {
- AssertIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
- SSL_SUCCESS);
- }
- else {
- /*
- * Calling EVP_Cipher with NULL input and output for AES-GCM is
- * akin to calling EVP_CipherFinal.
- */
- AssertIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
- }
- /* Check ciphertext against expected. */
- AssertIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
- 0);
- /* Get and check tag against expected. */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
- sizeof(tag), tag), SSL_SUCCESS);
- AssertIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
- /*************** Decrypt ***************/
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
- currentIv), SSL_SUCCESS);
- /* Check current IV against expected. */
- AssertIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
- /* Add AAD. */
- if (i == 2) {
- /* Test streaming API. */
- AssertIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
- AAD_SIZE), SSL_SUCCESS);
- }
- else {
- AssertIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
- AAD_SIZE);
- }
- /* Set expected tag. */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
- sizeof(tag), tag), SSL_SUCCESS);
- /* Decrypt ciphertext. */
- AssertNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- if (i == 2) {
- AssertIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
- cipherText, plainTextSzs[j]),
- SSL_SUCCESS);
- }
- else {
- /* This first EVP_Cipher call will check the tag, too. */
- AssertIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
- plainTextSzs[j]), plainTextSzs[j]);
- }
- if (i == 2) {
- AssertIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
- SSL_SUCCESS);
- }
- else {
- AssertIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
- }
- /* Check plaintext against expected. */
- AssertIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
- 0);
- XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- EVP_CIPHER_CTX_free(encCtx);
- EVP_CIPHER_CTX_free(decCtx);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OBJ_ln(void)
- {
- const int nid_set[] = {
- NID_commonName,
- NID_serialNumber,
- NID_countryName,
- NID_localityName,
- NID_stateOrProvinceName,
- NID_organizationName,
- NID_organizationalUnitName,
- NID_domainComponent,
- NID_businessCategory,
- NID_jurisdictionCountryName,
- NID_jurisdictionStateOrProvinceName,
- NID_emailAddress
- };
- const char* ln_set[] = {
- "commonName",
- "serialNumber",
- "countryName",
- "localityName",
- "stateOrProvinceName",
- "organizationName",
- "organizationalUnitName",
- "domainComponent",
- "businessCategory",
- "jurisdictionCountryName",
- "jurisdictionStateOrProvinceName",
- "emailAddress",
- };
- size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
- AssertIntEQ(OBJ_ln2nid(NULL), NID_undef);
- #ifdef HAVE_ECC
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- {
- EC_builtin_curve r[27];
- size_t nCurves = sizeof(r) / sizeof(r[0]);
- nCurves = EC_get_builtin_curves(r,nCurves);
- for (i = 0; i < nCurves; i++) {
- /* skip ECC_CURVE_INVALID */
- if (r[i].nid != ECC_CURVE_INVALID) {
- AssertIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
- AssertStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
- }
- }
- }
- #endif
- #endif
- for (i = 0; i < maxIdx; i++) {
- AssertIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
- AssertStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
- }
- return TEST_RES_CHECK(1);
- }
- static int test_wolfSSL_OBJ_sn(void)
- {
- int i = 0, maxIdx = 7;
- const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
- NID_stateOrProvinceName,NID_organizationName,
- NID_organizationalUnitName,NID_emailAddress};
- const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
- const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
- WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
- WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
- WOLFSSL_EMAIL_ADDR};
- AssertIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
- for (i = 0; i < maxIdx; i++) {
- AssertIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
- AssertStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
- }
- return TEST_RES_CHECK(1);
- }
- #if !defined(NO_BIO)
- static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
- {
- return lh_strhash(s[3]);
- }
- static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
- {
- return XSTRCMP(a[3], b[3]);
- }
- #endif
- static int test_wolfSSL_TXT_DB(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
- BIO *bio;
- TXT_DB *db = NULL;
- const int columns = 6;
- const char *fields[6] = {
- "V",
- "320926161116Z",
- "",
- "12BD",
- "unknown",
- "/CN=rsa doe",
- };
- char** fields_copy;
- /* Test read */
- AssertNotNull(bio = BIO_new(BIO_s_file()));
- AssertIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
- AssertNotNull(db = TXT_DB_read(bio, columns));
- AssertNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
- DYNAMIC_TYPE_OPENSSL));
- XMEMCPY(fields_copy, fields, sizeof(fields));
- AssertIntEQ(TXT_DB_insert(db, fields_copy), 1);
- BIO_free(bio);
- /* Test write */
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(TXT_DB_write(bio, db), 1484);
- BIO_free(bio);
- /* Test index */
- AssertIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
- (wolf_lh_compare_cb)TXT_DB_cmp), 1);
- AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
- fields[3] = "12DA";
- AssertNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
- fields[3] = "FFFF";
- AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
- fields[3] = "";
- AssertNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
- TXT_DB_free(db);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_NCONF(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
- const char* confFile = "./tests/NCONF_test.cnf";
- CONF* conf = NULL;
- long eline = 0;
- long num = 0;
- AssertNotNull(conf = NCONF_new(NULL));
- AssertIntEQ(NCONF_load(conf, confFile, &eline), 1);
- AssertIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
- AssertIntEQ(num, 1234);
- AssertIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
- AssertIntEQ(num, 4321);
- AssertStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
- AssertStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
- "./test-dir/file1");
- AssertStrEQ(NCONF_get_string(conf, "section2", "file_list"),
- "./test-dir/file1:./test-dir/file2:./section1:file2");
- NCONF_free(conf);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif /* OPENSSL_ALL */
- static int test_wolfSSL_EC_KEY_set_group(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
- defined(OPENSSL_EXTRA)
- EC_KEY *key = NULL;
- EC_GROUP *group = NULL;
- const EC_GROUP *group2 = NULL;
- AssertNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
- AssertNotNull(key = EC_KEY_new());
- AssertIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
- AssertNotNull(group2 = EC_KEY_get0_group(key));
- AssertIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
- EC_GROUP_free(group);
- EC_KEY_free(key);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EC_KEY_set_conv_form(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
- BIO* bio;
- EC_KEY* key;
- /* Error condition: NULL key. */
- AssertIntLT(EC_KEY_get_conv_form(NULL), 0);
- AssertNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
- AssertNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
- /* Conversion form defaults to uncompressed. */
- AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
- #ifdef HAVE_COMP_KEY
- /* Explicitly set to compressed. */
- EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
- AssertIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
- #endif
- BIO_free(bio);
- EC_KEY_free(key);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EC_KEY_print_fp(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
- defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
- defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
- !defined(NO_STDIO_FILESYSTEM)
- EC_KEY* key = NULL;
- /* Bad file pointer. */
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
- /* NULL key. */
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
- AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
- /* Negative indent. */
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
- wolfSSL_EC_KEY_free(key);
- AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
- NID_X9_62_prime256v1)));
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
- wolfSSL_EC_KEY_free(key);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509V3_EXT_get(void) {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
- FILE* f;
- int numOfExt =0;
- int extNid = 0;
- int i = 0;
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- const WOLFSSL_v3_ext_method* method;
- AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
- fclose(f);
- /* wolfSSL_X509V3_EXT_get() return struct and nid test */
- AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
- for (i = 0; i < numOfExt; i++) {
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
- AssertIntNE((extNid = ext->obj->nid), NID_undef);
- AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
- AssertIntEQ(method->ext_nid, extNid);
- }
- /* wolfSSL_X509V3_EXT_get() NULL argument test */
- AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509V3_EXT_nconf(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_ALL
- const char *ext_names[] = {
- "subjectKeyIdentifier",
- "authorityKeyIdentifier",
- "subjectAltName",
- "keyUsage",
- };
- size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
- int ext_nids[] = {
- NID_subject_key_identifier,
- NID_authority_key_identifier,
- NID_subject_alt_name,
- NID_key_usage,
- };
- size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
- const char *ext_values[] = {
- "hash",
- "hash",
- "DNS:example.com, IP:127.0.0.1",
- "digitalSignature,keyEncipherment,dataEncipherment",
- };
- size_t i;
- X509_EXTENSION* ext;
- X509* x509 = X509_new();
- for (i = 0; i < ext_names_count; i++) {
- ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
- AssertNotNull(ext);
- X509_EXTENSION_free(ext);
- }
- for (i = 0; i < ext_nids_count; i++) {
- ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i], ext_values[i]);
- AssertNotNull(ext);
- X509_EXTENSION_free(ext);
- }
- /* Test adding extension to X509 */
- for (i = 0; i < ext_nids_count; i++) {
- ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i], ext_values[i]);
- AssertIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
- X509_EXTENSION_free(ext);
- }
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509V3_EXT(void) {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
- FILE* f;
- int numOfExt = 0, nid = 0, i = 0, expected, actual;
- char* str;
- unsigned char* data;
- const WOLFSSL_v3_ext_method* method;
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- WOLFSSL_X509_EXTENSION* ext2;
- WOLFSSL_ASN1_OBJECT *obj, *adObj;
- WOLFSSL_ASN1_STRING* asn1str;
- WOLFSSL_AUTHORITY_KEYID* aKeyId;
- WOLFSSL_AUTHORITY_INFO_ACCESS* aia;
- WOLFSSL_BASIC_CONSTRAINTS* bc;
- WOLFSSL_ACCESS_DESCRIPTION* ad;
- WOLFSSL_GENERAL_NAME* gn;
- /* Check NULL argument */
- AssertNull(wolfSSL_X509V3_EXT_d2i(NULL));
- /* Using OCSP cert with X509V3 extensions */
- AssertNotNull(f = fopen("./certs/ocsp/root-ca-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
- fclose(f);
- AssertIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
- /* Basic Constraints */
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
- AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
- AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
- AssertNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
- AssertIntEQ(bc->ca, 1);
- AssertNull(bc->pathlen);
- wolfSSL_BASIC_CONSTRAINTS_free(bc);
- i++;
- /* Subject Key Identifier */
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
- AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
- AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
- AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
- AssertNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
- asn1str));
- X509_EXTENSION_free(ext2);
- AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
- AssertNotNull(method->i2s);
- AssertNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
- wolfSSL_ASN1_STRING_free(asn1str);
- actual = strcmp(str,
- "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
- AssertIntEQ(actual, 0);
- XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- i++;
- /* Authority Key Identifier */
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
- AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
- AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
- AssertNotNull(aKeyId =
- (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(ext));
- AssertNotNull(method = wolfSSL_X509V3_EXT_get(ext));
- AssertNotNull(asn1str = aKeyId->keyid);
- AssertNotNull(str =
- wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method, asn1str));
- actual = strcmp(str,
- "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
- AssertIntEQ(actual, 0);
- XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_AUTHORITY_KEYID_free(aKeyId);
- i++;
- /* Key Usage */
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
- AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
- AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
- AssertNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
- #if defined(WOLFSSL_QT)
- AssertNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
- #else
- AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
- #endif
- expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
- #ifdef BIG_ENDIAN_ORDER
- actual = data[1];
- #else
- actual = data[0];
- #endif
- AssertIntEQ(actual, expected);
- wolfSSL_ASN1_STRING_free(asn1str);
- #if 1
- i++;
- /* Authority Info Access */
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, i));
- AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
- AssertIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
- AssertNotNull(aia =
- (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext));
- #if defined(WOLFSSL_QT)
- AssertIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
- #else
- AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
- #endif
- /* URI entry is an ACCESS_DESCRIPTION type */
- #if defined(WOLFSSL_QT)
- AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
- #else
- AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
- #endif
- AssertNotNull(adObj = ad->method);
- /* Make sure nid is OCSP */
- AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
- /* GENERAL_NAME stores URI as an ASN1_STRING */
- AssertNotNull(gn = ad->location);
- AssertIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
- AssertNotNull(asn1str = gn->d.uniformResourceIdentifier);
- AssertIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
- #if defined(WOLFSSL_QT)
- str = (char*)ASN1_STRING_get0_data(asn1str);
- #else
- str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
- #endif
- actual = strcmp(str, "http://127.0.0.1:22220");
- AssertIntEQ(actual, 0);
- wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
- #else
- (void) aia; (void) ad; (void) adObj; (void) gn;
- #endif
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_get_extension_flags(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- XFILE f;
- X509* x509;
- unsigned int extFlags;
- unsigned int keyUsageFlags;
- unsigned int extKeyUsageFlags;
- /* client-int-cert.pem has the following extension flags. */
- extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
- /* and the following key usage flags. */
- keyUsageFlags = KU_DIGITAL_SIGNATURE
- | KU_NON_REPUDIATION
- | KU_KEY_ENCIPHERMENT;
- /* and the following extended key usage flags. */
- extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
- f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb");
- AssertTrue(f != XBADFILE);
- AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
- XFCLOSE(f);
- AssertIntEQ(X509_get_extension_flags(x509), extFlags);
- AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
- AssertIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
- X509_free(x509);
- /* client-cert-ext.pem has the following extension flags. */
- extFlags = EXFLAG_KUSAGE;
- /* and the following key usage flags. */
- keyUsageFlags = KU_DIGITAL_SIGNATURE
- | KU_KEY_CERT_SIGN
- | KU_CRL_SIGN;
- AssertNotNull(f = fopen("./certs/client-cert-ext.pem", "rb"));
- AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
- XFCLOSE(f);
- AssertIntEQ(X509_get_extension_flags(x509), extFlags);
- AssertIntEQ(X509_get_key_usage(x509), keyUsageFlags);
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_ALL */
- return res;
- }
- static int test_wolfSSL_X509_get_ext(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
- int ret = 0;
- FILE* f;
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* foundExtension;
- AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
- fclose(f);
- AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
- /* wolfSSL_X509_get_ext() valid input */
- AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
- /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
- AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
- AssertNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
- /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
- AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
- AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
- /* wolfSSL_X509_get_ext() NULL x509, valid idx */
- AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_get_ext_by_NID(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- int rc;
- FILE* f;
- WOLFSSL_X509* x509;
- ASN1_OBJECT* obj = NULL;
- AssertNotNull(f = fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
- fclose(f);
- rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
- AssertIntGE(rc, 0);
- /* Start search from last location (should fail) */
- rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, rc);
- AssertIntGE(rc, -1);
- rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -2);
- AssertIntGE(rc, -1);
- rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints, -1);
- AssertIntEQ(rc, -1);
- rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1);
- AssertIntEQ(rc, -1);
- /* NID_ext_key_usage, check also its nid and oid */
- rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1);
- AssertIntGT(rc, -1);
- AssertNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(x509, rc)));
- AssertIntEQ(obj->nid, NID_ext_key_usage);
- AssertIntEQ(obj->type, EXT_KEY_USAGE_OID);
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- int rc;
- XFILE f;
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- WOLFSSL_ASN1_STRING* sanString;
- byte* sanDer;
- const byte expectedDer[] = {
- 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
- 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
- f = XFOPEN("./certs/server-cert.pem", "rb");
- AssertTrue(f != XBADFILE);
- AssertNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
- fclose(f);
- rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1);
- AssertIntNE(rc, -1);
- AssertNotNull(ext = X509_get_ext(x509, rc));
- AssertNotNull(sanString = X509_EXTENSION_get_data(ext));
- AssertIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
- AssertNotNull(sanDer = ASN1_STRING_data(sanString));
- AssertIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_EXTENSION_new(void)
- {
- int res = TEST_SKIPPED;
- #if defined (OPENSSL_ALL)
- WOLFSSL_X509_EXTENSION* ext;
- AssertNotNull(ext = wolfSSL_X509_EXTENSION_new());
- AssertNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
- ext->obj->nid = WOLFSSL_SUCCESS;
- AssertIntEQ(WOLFSSL_SUCCESS, ext->obj->nid);
- wolfSSL_X509_EXTENSION_free(ext);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_EXTENSION_get_object(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- WOLFSSL_ASN1_OBJECT* o;
- FILE* file;
- AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
- fclose(file);
- /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
- AssertNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
- AssertIntEQ(o->nid, 128);
- /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
- AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_EXTENSION_get_data(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- WOLFSSL_ASN1_STRING* str;
- FILE* file;
- AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
- fclose(file);
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
- AssertNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_EXTENSION_get_critical(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- FILE* file;
- int crit;
- AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
- fclose(file);
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
- crit = wolfSSL_X509_EXTENSION_get_critical(ext);
- AssertIntEQ(crit, 0);
- wolfSSL_X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509V3_EXT_print(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
- !defined(NO_RSA)
- {
- FILE* f;
- WOLFSSL_X509* x509;
- X509_EXTENSION * ext = NULL;
- int loc;
- BIO *bio = NULL;
- AssertNotNull(f = fopen(svrCertFile, "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
- fclose(f);
- AssertNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
- loc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints, -1);
- AssertIntGT(loc, -1);
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
- AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
- loc = wolfSSL_X509_get_ext_by_NID(x509, NID_subject_key_identifier, -1);
- AssertIntGT(loc, -1);
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
- AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
- loc = wolfSSL_X509_get_ext_by_NID(x509, NID_authority_key_identifier, -1);
- AssertIntGT(loc, -1);
- AssertNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
- AssertIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
- wolfSSL_BIO_free(bio);
- wolfSSL_X509_free(x509);
- }
- {
- X509 *x509;
- BIO *bio;
- X509_EXTENSION *ext;
- unsigned int i;
- unsigned int idx;
- /* Some NIDs to test with */
- int nids[] = {
- /* NID_key_usage, currently X509_get_ext returns this as a bit
- * string, which messes up X509V3_EXT_print */
- /* NID_ext_key_usage, */
- NID_subject_alt_name,
- };
- int* n;
- AssertNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
- WOLFSSL_FILETYPE_PEM));
- fprintf(stderr, "\nPrinting extension values:\n");
- for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
- /* X509_get_ext_by_NID should return 3 for now. If that changes then
- * update the index */
- AssertIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
- AssertNotNull(ext = X509_get_ext(x509, idx));
- AssertIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
- fprintf(stderr, "\n");
- }
- BIO_free(bio);
- X509_free(x509);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_cmp(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- FILE* file1;
- FILE* file2;
- WOLFSSL_X509* cert1;
- WOLFSSL_X509* cert2;
- AssertNotNull(file1=fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(file2=fopen("./certs/3072/client-cert.pem", "rb"));
- AssertNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
- AssertNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
- fclose(file1);
- fclose(file2);
- /* wolfSSL_X509_cmp() testing matching certs */
- AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
- /* wolfSSL_X509_cmp() testing mismatched certs */
- AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
- /* wolfSSL_X509_cmp() testing NULL, valid args */
- AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
- /* wolfSSL_X509_cmp() testing valid, NULL args */
- AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
- /* wolfSSL_X509_cmp() testing NULL, NULL args */
- AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
- wolfSSL_X509_free(cert1);
- wolfSSL_X509_free(cert2);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PKEY_up_ref(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL)
- EVP_PKEY* pkey;
- pkey = EVP_PKEY_new();
- AssertIntEQ(EVP_PKEY_up_ref(NULL), 0);
- AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
- EVP_PKEY_free(pkey);
- AssertIntEQ(EVP_PKEY_up_ref(pkey), 1);
- EVP_PKEY_free(pkey);
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- EVP_PKEY* pkey;
- const unsigned char* p;
- unsigned char* der = NULL;
- int derLen;
- p = client_keypub_der_2048;
- /* Check that key can be successfully decoded. */
- AssertNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
- sizeof_client_keypub_der_2048));
- /* Check that key can be successfully encoded. */
- AssertIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
- /* Ensure that the encoded version matches the original. */
- AssertIntEQ(derLen, sizeof_client_keypub_der_2048);
- AssertIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
- XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
- DSA* dsa;
- char file[] = "./certs/dsaparams.der";
- XFILE f;
- int derInLen;
- byte* derIn;
- int derOutLen;
- byte* derOut = NULL;
- f = XFOPEN(file, "rb");
- AssertTrue(f != XBADFILE);
- AssertTrue(XFSEEK(f, 0, XSEEK_END) == 0);
- derInLen = (int)XFTELL(f);
- XREWIND(f);
- AssertNotNull(derIn = (byte*)XMALLOC(derInLen, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- AssertIntEQ(XFREAD(derIn, 1, derInLen, f), derInLen);
- XFCLOSE(f);
- /* Check that params can be successfully decoded. */
- AssertNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&derIn, derInLen));
- /* Check that params can be successfully encoded. */
- AssertIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
- /* Ensure that the encoded version matches the original. */
- AssertIntEQ(derInLen, derOutLen);
- AssertIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
- XFREE(derIn, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
- DSA_free(dsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_i2d_PrivateKey(void)
- {
- int res = TEST_SKIPPED;
- #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
- #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
- {
- EVP_PKEY* pkey;
- const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
- unsigned char buf[FOURK_BUF];
- unsigned char* pt = NULL;
- int bufSz;
- AssertNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
- (long)sizeof_server_key_der_2048));
- AssertIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
- pt = buf;
- AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
- AssertIntNE((pt - buf), 0);
- AssertIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
- EVP_PKEY_free(pkey);
- }
- #endif
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
- {
- EVP_PKEY* pkey;
- const unsigned char* client_key =
- (const unsigned char*)ecc_clikey_der_256;
- unsigned char buf[FOURK_BUF];
- unsigned char* pt = NULL;
- int bufSz;
- AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
- sizeof_ecc_clikey_der_256)));
- AssertIntEQ(i2d_PrivateKey(pkey, NULL), 121);
- pt = buf;
- AssertIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
- AssertIntNE((pt - buf), 0);
- AssertIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
- EVP_PKEY_free(pkey);
- }
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OCSP_id_get0_info(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- X509* cert;
- X509* issuer;
- OCSP_CERTID* id;
- OCSP_CERTID* id2;
- ASN1_STRING* name = NULL;
- ASN1_OBJECT* pmd = NULL;
- ASN1_STRING* keyHash = NULL;
- ASN1_INTEGER* serial = NULL;
- ASN1_INTEGER* x509Int;
- AssertNotNull(cert =
- wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM));
- AssertNotNull(issuer =
- wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
- id = OCSP_cert_to_id(NULL, cert, issuer);
- AssertNotNull(id);
- id2 = OCSP_cert_to_id(NULL, cert, issuer);
- AssertNotNull(id2);
- AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
- AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
- /* name, pmd, keyHash not supported yet, expect failure if not NULL */
- AssertIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
- AssertIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
- AssertIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
- AssertIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
- AssertNotNull(serial);
- /* compare serial number to one in cert, should be equal */
- x509Int = X509_get_serialNumber(cert);
- AssertNotNull(x509Int);
- AssertIntEQ(x509Int->length, serial->length);
- AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
- /* test OCSP_id_cmp */
- AssertIntNE(OCSP_id_cmp(NULL, NULL), 0);
- AssertIntNE(OCSP_id_cmp(id, NULL), 0);
- AssertIntNE(OCSP_id_cmp(NULL, id2), 0);
- AssertIntEQ(OCSP_id_cmp(id, id2), 0);
- id->issuerHash[0] = ~id->issuerHash[0];
- AssertIntNE(OCSP_id_cmp(id, id2), 0);
- OCSP_CERTID_free(id);
- OCSP_CERTID_free(id2);
- X509_free(cert); /* free's x509Int */
- X509_free(issuer);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_i2d_OCSP_CERTID(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
- WOLFSSL_OCSP_CERTID certId;
- byte* targetBuffer;
- byte* beginTargetBuffer;
- /* OCSP CertID bytes taken from PCAP */
- byte rawCertId[] = {
- 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
- 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
- 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
- 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
- 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
- 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
- 0xcf, 0xbc, 0x91
- };
- int ret, i;
- XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
- certId.rawCertId = rawCertId;
- certId.rawCertIdSize = sizeof(rawCertId);
- targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL, DYNAMIC_TYPE_TMP_BUFFER);
- beginTargetBuffer = targetBuffer;
- ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
- /* If target buffer is not null, function increments targetBuffer to point
- just past the end of the encoded data. */
- AssertPtrEq(targetBuffer, (beginTargetBuffer + sizeof(rawCertId)));
- /* Function returns the size of the encoded data. */
- AssertIntEQ(ret, sizeof(rawCertId));
- for (i = 0; i < ret; ++i)
- {
- AssertIntEQ(beginTargetBuffer[i], rawCertId[i]);
- }
- XFREE(beginTargetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- targetBuffer = NULL;
- ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer);
- /* If target buffer is null, function allocates memory for a buffer and
- copies the encoded data into it. targetBuffer then points to the start of
- this newly allocate buffer. */
- AssertIntEQ(ret, sizeof(rawCertId));
- for (i = 0; i < ret; ++i)
- {
- AssertIntEQ(targetBuffer[i], rawCertId[i]);
- }
- XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_d2i_OCSP_CERTID(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
- WOLFSSL_OCSP_CERTID* certId;
- WOLFSSL_OCSP_CERTID* certIdBad;
- const unsigned char* rawCertIdPtr;
- const unsigned char rawCertId[] = {
- 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
- 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
- 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
- 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
- 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
- 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
- 0xcf, 0xbc, 0x91
- };
- rawCertIdPtr = &rawCertId[0];
- /* If the cert ID is NULL the function should allocate it and copy the
- * data to it. */
- certId = NULL;
- certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
- AssertNotNull(certId);
- AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
- XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
- XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
- /* If the cert ID is not NULL the function will just copy the data to it. */
- certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- AssertNotNull(certId);
- XMEMSET(certId, 0, sizeof(*certId));
- /* Reset rawCertIdPtr since it was push forward in the previous call. */
- rawCertIdPtr = &rawCertId[0];
- certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, sizeof(rawCertId));
- AssertNotNull(certId);
- AssertIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
- XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
- XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- /* The below tests should fail when passed bad parameters. NULL should
- * always be returned. */
- certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr, sizeof(rawCertId));
- AssertNull(certIdBad);
- certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL, sizeof(rawCertId));
- AssertNull(certIdBad);
- certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0);
- AssertNull(certIdBad);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OCSP_id_cmp(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
- OCSP_CERTID id1;
- OCSP_CERTID id2;
- XMEMSET(&id1, 0, sizeof(id1));
- XMEMSET(&id2, 0, sizeof(id2));
- AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
- WOLFSSL_OCSP_SINGLERESP single;
- const WOLFSSL_OCSP_CERTID* certId;
- XMEMSET(&single, 0, sizeof(single));
- certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
- AssertPtrEq(&single, certId);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OCSP_single_get0_status(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
- WOLFSSL_OCSP_SINGLERESP single;
- CertStatus certStatus;
- WOLFSSL_ASN1_TIME* thisDate;
- WOLFSSL_ASN1_TIME* nextDate;
- int ret, i;
- XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
- XMEMSET(&certStatus, 0, sizeof(CertStatus));
- /* Fill the date fields with some dummy data. */
- for (i = 0; i < CTC_DATE_SIZE; ++i) {
- certStatus.thisDateParsed.data[i] = i;
- certStatus.nextDateParsed.data[i] = i;
- }
- certStatus.status = CERT_GOOD;
- single.status = &certStatus;
- ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
- &nextDate);
- AssertIntEQ(ret, CERT_GOOD);
- AssertPtrEq(thisDate, &certStatus.thisDateParsed);
- AssertPtrEq(nextDate, &certStatus.nextDateParsed);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OCSP_resp_count(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
- WOLFSSL_OCSP_BASICRESP basicResp;
- WOLFSSL_OCSP_SINGLERESP singleRespOne;
- WOLFSSL_OCSP_SINGLERESP singleRespTwo;
- int count;
- XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
- XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
- XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
- count = wolfSSL_OCSP_resp_count(&basicResp);
- AssertIntEQ(count, 0);
- basicResp.single = &singleRespOne;
- count = wolfSSL_OCSP_resp_count(&basicResp);
- AssertIntEQ(count, 1);
- singleRespOne.next = &singleRespTwo;
- count = wolfSSL_OCSP_resp_count(&basicResp);
- AssertIntEQ(count, 2);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_OCSP_resp_get0(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
- WOLFSSL_OCSP_BASICRESP basicResp;
- WOLFSSL_OCSP_SINGLERESP singleRespOne;
- WOLFSSL_OCSP_SINGLERESP singleRespTwo;
- WOLFSSL_OCSP_SINGLERESP* ret;
- XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
- XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
- XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
- basicResp.single = &singleRespOne;
- singleRespOne.next = &singleRespTwo;
- ret = wolfSSL_OCSP_resp_get0(&basicResp, 0);
- AssertPtrEq(ret, &singleRespOne);
- ret = wolfSSL_OCSP_resp_get0(&basicResp, 1);
- AssertPtrEq(ret, &singleRespTwo);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_derive(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
- #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
- EVP_PKEY_CTX *ctx;
- unsigned char *skey;
- size_t skeylen;
- EVP_PKEY *pkey, *peerkey;
- const unsigned char* key;
- #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
- /* DH */
- key = dh_key_der_2048;
- AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
- sizeof_dh_key_der_2048)));
- AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
- key = dh_key_der_2048;
- AssertNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
- sizeof_dh_key_der_2048)));
- AssertIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
- AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
- AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
- AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
- AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
- EVP_PKEY_CTX_free(ctx);
- EVP_PKEY_free(peerkey);
- EVP_PKEY_free(pkey);
- XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
- #endif
- #ifdef HAVE_ECC
- /* ECDH */
- key = ecc_clikey_der_256;
- AssertNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
- sizeof_ecc_clikey_der_256)));
- key = ecc_clikeypub_der_256;
- AssertNotNull((peerkey = d2i_PUBKEY(NULL, &key,
- sizeof_ecc_clikeypub_der_256)));
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- AssertIntEQ(EVP_PKEY_derive_init(ctx), 1);
- AssertIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
- AssertIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
- AssertNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL, DYNAMIC_TYPE_OPENSSL));
- AssertIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
- EVP_PKEY_CTX_free(ctx);
- EVP_PKEY_free(peerkey);
- EVP_PKEY_free(pkey);
- XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
- #endif /* HAVE_ECC */
- res = TEST_RES_CHECK(1);
- #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
- #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
- return res;
- }
- static int test_wolfSSL_EVP_PBE_scrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
- (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
- #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
- int ret;
- const char pwd[] = {'p','a','s','s','w','o','r','d'};
- int pwdlen = sizeof(pwd);
- const byte salt[] = {'N','a','C','l'};
- int saltlen = sizeof(salt);
- byte key[80];
- word64 numOvr32 = (word64)INT32_MAX + 1;
- /* expected derived key for N:16, r:1, p:1 */
- const byte expectedKey[] = {
- 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
- 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
- 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
- 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
- 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
- 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
- 0xE7, 0xE9, 0xC0, 0x9A};
- /* N r p mx key keylen */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
- AssertIntEQ(ret, 0); /* N must be greater than 1 */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
- AssertIntEQ(ret, 0); /* N must be power of 2 */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
- AssertIntEQ(ret, 0); /* r must be greater than 0 */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
- AssertIntEQ(ret, 0); /* p must be greater than 0 */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
- AssertIntEQ(ret, 0); /* keylen must be greater than 0 */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
- AssertIntEQ(ret, 0); /* r must be smaller than 9 */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
- AssertIntEQ(ret, 1); /* should succeed if key is NULL */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
- AssertIntEQ(ret, 1); /* should succeed */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
- key, 64);
- AssertIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
- key, 64);
- AssertIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
- ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
- AssertIntEQ(ret, 1); /* should succeed even if salt is NULL */
- ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
- AssertIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
- ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
- AssertIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
- ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
- AssertIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
- ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
- AssertIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
- ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
- AssertIntEQ(ret, 1);
- ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
- AssertIntEQ(ret, 0); /* derived key must be the same as expected-key */
- res = TEST_RES_CHECK(1);
- #endif /* !NO_PWDBASED && !NO_SHA256 */
- #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
- return res;
- }
- static int test_wolfSSL_EC_get_builtin_curves(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL))
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- EC_builtin_curve* curves = NULL;
- size_t crv_len = 0;
- size_t i = 0;
- AssertIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
- AssertNotNull(curves = (EC_builtin_curve*)
- XMALLOC(sizeof(EC_builtin_curve)*crv_len, NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- AssertIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
- for (i = 0; i < crv_len; i++)
- {
- if (curves[i].comment != NULL)
- AssertStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
- }
- XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(1);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
- return res;
- }
- static int test_no_op_functions(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- /* this makes sure wolfSSL can compile and run these no-op functions */
- SSL_load_error_strings();
- ENGINE_load_builtin_engines();
- OpenSSL_add_all_ciphers();
- AssertIntEQ(CRYPTO_malloc_init(), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CRYPTO_memcmp(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_EXTRA
- char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
- "implementation of TLS/SSL for embedded devices to the cloud.";
- char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
- "implementation of TLS/SSL for embedded devices to the cloud.";
- char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
- "implementation of TLS/SSL for embedded devices to the cloud!";
- AssertIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
- AssertIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*----------------------------------------------------------------------------*
- | wolfCrypt ASN
- *----------------------------------------------------------------------------*/
- static int test_wc_CreateEncryptedPKCS8Key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
- && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
- WC_RNG rng;
- byte* encKey = NULL;
- word32 encKeySz = 0;
- word32 decKeySz = 0;
- const char password[] = "Lorem ipsum dolor sit amet";
- word32 passwordSz = (word32)XSTRLEN(password);
- word32 tradIdx = 0;
- AssertIntEQ(wc_InitRng(&rng), 0);
- /* Call with NULL for out buffer to get necessary length. */
- AssertIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
- sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
- PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
- LENGTH_ONLY_E);
- AssertNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- /* Call with the allocated out buffer. */
- AssertIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
- sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
- PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
- 0);
- /* Decrypt the encrypted PKCS8 key we just made. */
- AssertIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
- passwordSz)), 0);
- /* encKey now holds the decrypted key (decrypted in place). */
- AssertIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
- /* Check that the decrypted key matches the key prior to encryption. */
- AssertIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
- sizeof_server_key_der_2048), 0);
- if (encKey != NULL)
- XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_GetPkcs8TraditionalOffset(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
- int length, derSz;
- word32 inOutIdx;
- const char* path = "./certs/server-keyPkcs8.der";
- XFILE file;
- byte der[2048];
- file = XFOPEN(path, "rb");
- AssertTrue(file != XBADFILE);
- derSz = (int)XFREAD(der, 1, sizeof(der), file);
- XFCLOSE(file);
- /* valid case */
- inOutIdx = 0;
- length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
- AssertIntGT(length, 0);
- /* inOutIdx > sz */
- inOutIdx = 4000;
- length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
- AssertIntEQ(length, BAD_FUNC_ARG);
- /* null input */
- inOutIdx = 0;
- length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0);
- AssertIntEQ(length, BAD_FUNC_ARG);
- /* invalid input, fill buffer with 1's */
- XMEMSET(der, 1, sizeof(der));
- inOutIdx = 0;
- length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz);
- AssertIntEQ(length, ASN_PARSE_E);
- res = TEST_RES_CHECK(1);
- #endif /* NO_ASN */
- return res;
- }
- static int test_wc_SetSubjectRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
- const char* joiCertFile = "./certs/test/cert-ext-joi.der";
- WOLFSSL_X509* x509;
- int peerCertSz;
- const byte* peerCertBuf;
- Cert forgedCert;
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
- AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
- AssertIntEQ(0, wc_InitCert(&forgedCert));
- AssertIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
- wolfSSL_FreeX509(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_GetSubjectRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
- Cert cert;
- byte *subjectRaw;
- AssertIntEQ(0, wc_InitCert(&cert));
- AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_SetIssuerRaw(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
- const char* joiCertFile = "./certs/test/cert-ext-joi.der";
- WOLFSSL_X509* x509;
- int peerCertSz;
- const byte* peerCertBuf;
- Cert forgedCert;
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
- AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
- AssertIntEQ(0, wc_InitCert(&forgedCert));
- AssertIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
- wolfSSL_FreeX509(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_SetIssueBuffer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
- const char* joiCertFile = "./certs/test/cert-ext-joi.der";
- WOLFSSL_X509* x509;
- int peerCertSz;
- const byte* peerCertBuf;
- Cert forgedCert;
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile, WOLFSSL_FILETYPE_ASN1));
- AssertNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
- AssertIntEQ(0, wc_InitCert(&forgedCert));
- AssertIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
- wolfSSL_FreeX509(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*
- * Testing wc_SetSubjectKeyId
- */
- static int test_wc_SetSubjectKeyId(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
- Cert cert;
- const char* file = "certs/ecc-client-keyPub.pem";
- AssertIntEQ(0, wc_InitCert(&cert));
- AssertIntEQ(0, wc_SetSubjectKeyId(&cert, file));
- AssertIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
- AssertIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_SetSubjectKeyId */
- /*
- * Testing wc_SetSubject
- */
- static int test_wc_SetSubject(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
- Cert cert;
- const char* file = "./certs/ca-ecc-cert.pem";
- AssertIntEQ(0, wc_InitCert(&cert));
- AssertIntEQ(0, wc_SetSubject(&cert, file));
- AssertIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
- AssertIntGT(0, wc_SetSubject(&cert, "badfile.name"));
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- } /* END test_wc_SetSubject */
- static int test_CheckCertSignature(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
- WOLFSSL_CERT_MANAGER* cm = NULL;
- #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
- FILE* fp;
- byte cert[4096];
- int certSz;
- #endif
- AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
- AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
- AssertIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
- #ifndef NO_RSA
- #ifdef USE_CERT_BUFFERS_1024
- AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
- sizeof_server_cert_der_1024, NULL, cm));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
- ca_cert_der_1024, sizeof_ca_cert_der_1024,
- WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(0, CheckCertSignature(server_cert_der_1024,
- sizeof_server_cert_der_1024, NULL, cm));
- #elif defined(USE_CERT_BUFFERS_2048)
- AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
- sizeof_server_cert_der_2048, NULL, cm));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
- ca_cert_der_2048, sizeof_ca_cert_der_2048,
- WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(0, CheckCertSignature(server_cert_der_2048,
- sizeof_server_cert_der_2048, NULL, cm));
- #endif
- #endif
- #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
- AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
- sizeof_serv_ecc_der_256, NULL, cm));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
- ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
- WOLFSSL_FILETYPE_ASN1));
- AssertIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
- NULL, cm));
- #endif
- #if !defined(NO_FILESYSTEM)
- wolfSSL_CertManagerFree(cm);
- AssertNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
- #ifndef NO_RSA
- AssertNotNull(fp = XFOPEN("./certs/server-cert.der", "rb"));
- AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
- XFCLOSE(fp);
- AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
- "./certs/ca-cert.pem", NULL));
- AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
- #endif
- #ifdef HAVE_ECC
- AssertNotNull(fp = XFOPEN("./certs/server-ecc.der", "rb"));
- AssertIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
- XFCLOSE(fp);
- AssertIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
- "./certs/ca-ecc-cert.pem", NULL));
- AssertIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
- #endif
- #endif
- #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
- (void)fp;
- (void)cert;
- (void)certSz;
- #endif
- wolfSSL_CertManagerFree(cm);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wc_ParseCert(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_CERTS) && !defined(NO_RSA)
- DecodedCert decodedCert;
- const byte* rawCert = client_cert_der_2048;
- const int rawCertSize = sizeof_client_cert_der_2048;
- wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
- AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
- #ifndef IGNORE_NAME_CONSTRAINTS
- /* check that the subjects emailAddress was not put in the alt name list */
- AssertNotNull(decodedCert.subjectEmail);
- AssertNull(decodedCert.altEmailNames);
- #endif
- wc_FreeDecodedCert(&decodedCert);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_MakeCertWithPathLen(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CERT_REQ) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
- const byte expectedPathLen = 7;
- Cert cert;
- DecodedCert decodedCert;
- byte der[FOURK_BUF];
- int derSize = 0;
- WC_RNG rng;
- ecc_key key;
- AssertIntEQ(wc_InitRng(&rng), 0);
- AssertIntEQ(wc_ecc_init(&key), 0);
- AssertIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
- AssertIntEQ(wc_InitCert(&cert), 0);
- (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
- (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
- (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
- (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
- (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
- (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com", CTC_NAME_SIZE);
- (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com", CTC_NAME_SIZE);
- cert.selfSigned = 1;
- cert.isCA = 1;
- cert.pathLen = expectedPathLen;
- cert.pathLenSet = 1;
- cert.sigType = CTC_SHA256wECDSA;
- #ifdef WOLFSSL_CERT_EXT
- cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
- #endif
- AssertIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
- derSize = wc_SignCert(cert.bodySz, cert.sigType, der, FOURK_BUF, NULL,
- &key, &rng);
- AssertIntGE(derSize, 0);
- wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
- AssertIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
- AssertIntEQ(decodedCert.pathLength, expectedPathLen);
- wc_FreeDecodedCert(&decodedCert);
- AssertIntEQ(wc_ecc_free(&key), 0);
- AssertIntEQ(wc_FreeRng(&rng), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*----------------------------------------------------------------------------*
- | wolfCrypt ECC
- *----------------------------------------------------------------------------*/
- static int test_wc_ecc_get_curve_size_from_name(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_ECC
- int ret;
- #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
- ret = wc_ecc_get_curve_size_from_name("SECP256R1");
- AssertIntEQ(ret, 32);
- #endif
- /* invalid case */
- ret = wc_ecc_get_curve_size_from_name("BADCURVE");
- AssertIntEQ(ret, -1);
- /* NULL input */
- ret = wc_ecc_get_curve_size_from_name(NULL);
- AssertIntEQ(ret, BAD_FUNC_ARG);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ECC */
- return res;
- }
- static int test_wc_ecc_get_curve_id_from_name(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_ECC
- int id;
- #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
- id = wc_ecc_get_curve_id_from_name("SECP256R1");
- AssertIntEQ(id, ECC_SECP256R1);
- #endif
- /* invalid case */
- id = wc_ecc_get_curve_id_from_name("BADCURVE");
- AssertIntEQ(id, -1);
- /* NULL input */
- id = wc_ecc_get_curve_id_from_name(NULL);
- AssertIntEQ(id, BAD_FUNC_ARG);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_ECC */
- return res;
- }
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
- !defined(HAVE_SELFTEST) && \
- !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
- static int test_wc_ecc_get_curve_id_from_dp_params(void)
- {
- int id;
- #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
- int curve_id;
- ecc_key* key;
- const ecc_set_type* params;
- int ret;
- #endif
- WOLFSSL_EC_KEY *ecKey = NULL;
- #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
- id = wc_ecc_get_curve_id_from_name("SECP256R1");
- AssertIntEQ(id, ECC_SECP256R1);
- ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
- AssertNotNull(ecKey);
- ret = EC_KEY_generate_key(ecKey);
- if (ret == 0) {
- /* normal test */
- key = (ecc_key*)ecKey->internal;
- params = key->dp;
- curve_id = wc_ecc_get_curve_id_from_dp_params(params);
- AssertIntEQ(curve_id, id);
- }
- #endif
- /* invalid case, NULL input*/
- id = wc_ecc_get_curve_id_from_dp_params(NULL);
- AssertIntEQ(id, BAD_FUNC_ARG);
- wolfSSL_EC_KEY_free(ecKey);
- return TEST_RES_CHECK(1);
- }
- #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
- static int test_wc_ecc_get_curve_id_from_params(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_ECC
- int id;
- const byte prime[] =
- {
- 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
- };
- const byte primeInvalid[] =
- {
- 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
- };
- const byte Af[] =
- {
- 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
- };
- const byte Bf[] =
- {
- 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
- 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
- 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
- 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
- };
- const byte order[] =
- {
- 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
- 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
- };
- const byte Gx[] =
- {
- 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
- 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
- 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
- 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
- };
- const byte Gy[] =
- {
- 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
- 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
- 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
- 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
- };
- int cofactor = 1;
- int fieldSize = 256;
- #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
- id = wc_ecc_get_curve_id_from_params(fieldSize, prime, sizeof(prime),
- Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
- Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
- AssertIntEQ(id, ECC_SECP256R1);
- #endif
- /* invalid case, fieldSize = 0 */
- id = wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
- Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
- Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
- AssertIntEQ(id, ECC_CURVE_INVALID);
- /* invalid case, NULL prime */
- id = wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
- Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
- Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
- AssertIntEQ(id, BAD_FUNC_ARG);
- /* invalid case, invalid prime */
- id = wc_ecc_get_curve_id_from_params(fieldSize,
- primeInvalid, sizeof(primeInvalid),
- Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
- Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor);
- AssertIntEQ(id, ECC_CURVE_INVALID);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_encrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_FAST_RSA)
- WOLFSSL_RSA* rsa = NULL;
- WOLFSSL_EVP_PKEY* pkey = NULL;
- WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
- const char* in = "What is easy to do is easy not to do.";
- size_t inlen = XSTRLEN(in);
- size_t outEncLen = 0;
- byte* outEnc = NULL;
- byte* outDec = NULL;
- size_t outDecLen = 0;
- size_t rsaKeySz = 2048/8; /* Bytes */
- #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
- byte* inTmp = NULL;
- byte* outEncTmp = NULL;
- byte* outDecTmp = NULL;
- #endif
- AssertNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- XMEMSET(outEnc, 0, rsaKeySz);
- AssertNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- XMEMSET(outDec, 0, rsaKeySz);
- AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
- WOLFSSL_SUCCESS);
- /* Test pkey references count is decremented. pkey shouldn't be destroyed
- since ctx uses it.*/
- AssertIntEQ(pkey->references, 2);
- EVP_PKEY_free(pkey);
- AssertIntEQ(pkey->references, 1);
- /* Encrypt data */
- /* Check that we can get the required output buffer length by passing in a
- * NULL output buffer. */
- AssertIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
- (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
- AssertIntEQ(rsaKeySz, outEncLen);
- /* Now do the actual encryption. */
- AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
- (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
- /* Decrypt data */
- AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
- /* Check that we can get the required output buffer length by passing in a
- * NULL output buffer. */
- AssertIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
- WOLFSSL_SUCCESS);
- AssertIntEQ(rsaKeySz, outDecLen);
- /* Now do the actual decryption. */
- AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
- WOLFSSL_SUCCESS);
- AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
- #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
- /* The input length must be the same size as the RSA key.*/
- AssertNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- XMEMSET(inTmp, 9, rsaKeySz);
- AssertNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- XMEMSET(outEncTmp, 0, rsaKeySz);
- AssertNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- XMEMSET(outDecTmp, 0, rsaKeySz);
- AssertIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp, outEncLen),
- WOLFSSL_SUCCESS);
- AssertIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
- #endif
- EVP_PKEY_CTX_free(ctx);
- XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
- XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_sign_verify(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
- WOLFSSL_DSA* dsa = NULL;
- #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
- WOLFSSL_EVP_PKEY* pkey = NULL;
- WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
- WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
- const char* in = "What is easy to do is easy not to do.";
- size_t inlen = XSTRLEN(in);
- byte hash[SHA256_DIGEST_LENGTH] = {0};
- byte zero[SHA256_DIGEST_LENGTH] = {0};
- SHA256_CTX c;
- byte* sig = NULL;
- byte* sigVerify = NULL;
- size_t siglen;
- size_t siglenOnlyLen;
- size_t keySz = 2048/8; /* Bytes */
- int i;
- int encs[3] = {0};
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- encs[0] = EVP_PKEY_RSA;
- #endif
- #endif
- #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
- encs[1] = EVP_PKEY_DSA;
- #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- encs[2] = EVP_PKEY_EC;
- #endif
- #endif
- AssertNotNull(sig =
- (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- AssertNotNull(sigVerify =
- (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- for (i = 0; i < 3; i++) {
- if (encs[i] == 0)
- continue;
- siglen = keySz;
- XMEMSET(sig, 0, keySz);
- XMEMSET(sigVerify, 0, keySz);
- /* Generate hash */
- SHA256_Init(&c);
- SHA256_Update(&c, in, inlen);
- SHA256_Final(hash, &c);
- #ifdef WOLFSSL_SMALL_STACK_CACHE
- /* workaround for small stack cache case */
- wc_Sha256Free((wc_Sha256*)&c);
- #endif
- /* Generate key */
- AssertNotNull(pkey = EVP_PKEY_new());
- switch (encs[i]) {
- case EVP_PKEY_RSA:
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- {
- WOLFSSL_RSA* rsa = NULL;
- AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
- AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
- }
- #endif
- #endif
- break;
- case EVP_PKEY_DSA:
- #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
- AssertNotNull(dsa = DSA_new());
- AssertIntEQ(DSA_generate_parameters_ex(dsa, 2048,
- NULL, 0, NULL, NULL, NULL), 1);
- AssertIntEQ(DSA_generate_key(dsa), 1);
- AssertIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
- #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
- break;
- case EVP_PKEY_EC:
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- {
- WOLFSSL_EC_KEY* ecKey = NULL;
- AssertNotNull(ecKey = EC_KEY_new());
- AssertIntEQ(EC_KEY_generate_key(ecKey), 1);
- AssertIntEQ(
- EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
- }
- #endif
- #endif
- break;
- }
- AssertNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
- AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- if (encs[i] == EVP_PKEY_RSA)
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
- WOLFSSL_SUCCESS);
- #endif
- #endif
- /* Check returning only length */
- AssertIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
- SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
- AssertIntGT(siglenOnlyLen, 0);
- /* Sign data */
- AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
- SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
- AssertIntGE(siglenOnlyLen, siglen);
- /* Verify signature */
- AssertNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
- AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- if (encs[i] == EVP_PKEY_RSA)
- AssertIntEQ(
- EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
- WOLFSSL_SUCCESS);
- #endif
- #endif
- AssertIntEQ(EVP_PKEY_verify(
- ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_verify(
- ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
- WOLFSSL_FAILURE);
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_FAST_RSA) && !defined(HAVE_SELFTEST)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- if (encs[i] == EVP_PKEY_RSA) {
- #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
- /* Try RSA sign/verify with no padding. */
- AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
- siglen), WOLFSSL_SUCCESS);
- AssertIntGE(siglenOnlyLen, siglen);
- AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
- RSA_NO_PADDING), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
- siglen), WOLFSSL_SUCCESS);
- #endif
- /* Wrong padding schemes. */
- AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
- RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
- AssertIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
- siglen), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
- RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
- AssertIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
- siglen), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
- RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
- }
- #endif
- #endif
- /* error cases */
- siglen = keySz; /* Reset because sig size may vary slightly */
- AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
- AssertIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
- WOLFSSL_SUCCESS);
- EVP_PKEY_free(pkey);
- #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
- DSA_free(dsa);
- dsa = NULL;
- #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
- EVP_PKEY_CTX_free(ctx_verify);
- EVP_PKEY_CTX_free(ctx);
- }
- XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_EVP_PKEY_rsa(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- WOLFSSL_RSA* rsa;
- WOLFSSL_EVP_PKEY* pkey;
- AssertNotNull(rsa = wolfSSL_RSA_new());
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
- AssertIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
- AssertPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
- wolfSSL_EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_EVP_PKEY_ec(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- WOLFSSL_EC_KEY* ecKey;
- WOLFSSL_EVP_PKEY* pkey;
- AssertNotNull(ecKey = wolfSSL_EC_KEY_new());
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
- AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
- /* Should fail since ecKey is empty */
- AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
- AssertIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
- wolfSSL_EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- #endif
- #endif
- return res;
- }
- static int test_EVP_PKEY_cmp(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- EVP_PKEY *a, *b;
- const unsigned char *in;
- #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
- in = client_key_der_2048;
- AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
- &in, (long)sizeof_client_key_der_2048));
- in = client_key_der_2048;
- AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
- &in, (long)sizeof_client_key_der_2048));
- /* Test success case RSA */
- #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
- AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
- #else
- AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
- #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
- EVP_PKEY_free(b);
- EVP_PKEY_free(a);
- #endif
- #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
- in = ecc_clikey_der_256;
- AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
- &in, (long)sizeof_ecc_clikey_der_256));
- in = ecc_clikey_der_256;
- AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
- &in, (long)sizeof_ecc_clikey_der_256));
- /* Test success case ECC */
- #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
- AssertIntEQ(EVP_PKEY_cmp(a, b), 1);
- #else
- AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
- #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
- EVP_PKEY_free(b);
- EVP_PKEY_free(a);
- #endif
- /* Test failure cases */
- #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
- defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
- in = client_key_der_2048;
- AssertNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
- &in, (long)sizeof_client_key_der_2048));
- in = ecc_clikey_der_256;
- AssertNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
- &in, (long)sizeof_ecc_clikey_der_256));
- #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
- AssertIntEQ(EVP_PKEY_cmp(a, b), -1);
- #else
- AssertIntNE(EVP_PKEY_cmp(a, b), 0);
- #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
- EVP_PKEY_free(b);
- EVP_PKEY_free(a);
- #endif
- /* invalid or empty failure cases */
- a = EVP_PKEY_new();
- b = EVP_PKEY_new();
- #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
- AssertIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
- AssertIntEQ(EVP_PKEY_cmp(a, NULL), 0);
- AssertIntEQ(EVP_PKEY_cmp(NULL, b), 0);
- #ifdef NO_RSA
- /* Type check will fail since RSA is the default EVP key type */
- AssertIntEQ(EVP_PKEY_cmp(a, b), -2);
- #else
- AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
- #endif
- #else
- AssertIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
- AssertIntNE(EVP_PKEY_cmp(a, NULL), 0);
- AssertIntNE(EVP_PKEY_cmp(NULL, b), 0);
- AssertIntNE(EVP_PKEY_cmp(a, b), 0);
- #endif
- EVP_PKEY_free(b);
- EVP_PKEY_free(a);
- (void)in;
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_ERR_load_crypto_strings(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL)
- ERR_load_crypto_strings();
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
- static void free_x509(X509* x)
- {
- AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
- }
- #endif
- static int test_sk_X509(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
- {
- STACK_OF(X509)* s;
- AssertNotNull(s = sk_X509_new_null());
- AssertIntEQ(sk_X509_num(s), 0);
- sk_X509_pop_free(s, NULL);
- AssertNotNull(s = sk_X509_new_null());
- AssertIntEQ(sk_X509_num(s), 0);
- sk_X509_pop_free(s, NULL);
- AssertNotNull(s = sk_X509_new_null());
- sk_X509_push(s, (X509*)1);
- AssertIntEQ(sk_X509_num(s), 1);
- AssertIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
- sk_X509_push(s, (X509*)2);
- AssertIntEQ(sk_X509_num(s), 2);
- AssertIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
- AssertIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
- sk_X509_push(s, (X509*)2);
- sk_X509_pop_free(s, free_x509);
- }
- {
- /* Push a list of 10 X509s onto stack, then verify that
- * value(), push(), shift(), and pop() behave as expected. */
- STACK_OF(X509)* s;
- X509* xList[10];
- int i = 0;
- const int len = (sizeof(xList) / sizeof(xList[0]));
- for (i = 0; i < len; ++i)
- AssertNotNull(xList[i] = X509_new());
- /* test push, pop, and free */
- AssertNotNull(s = sk_X509_new_null());
- for (i = 0; i < len; ++i) {
- sk_X509_push(s, xList[i]);
- AssertIntEQ(sk_X509_num(s), i + 1);
- AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
- AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
- }
- /* pop returns and removes last pushed on stack, which is index 0
- * in sk_x509_value */
- for (i = 0; i < len; ++i) {
- X509 * x = sk_X509_value(s, 0);
- X509 * y = sk_X509_pop(s);
- X509 * z = xList[len - 1 - i];
- AssertIntEQ((x == y), 1);
- AssertIntEQ((x == z), 1);
- AssertIntEQ(sk_X509_num(s), len - 1 - i);
- }
- sk_free(s);
- /* test push, shift, and free */
- AssertNotNull(s = sk_X509_new_null());
- for (i = 0; i < len; ++i) {
- sk_X509_push(s, xList[i]);
- AssertIntEQ(sk_X509_num(s), i + 1);
- AssertIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
- AssertIntEQ((sk_X509_value(s, i) == xList[0]), 1);
- }
- /* shift returns and removes first pushed on stack, which is index i
- * in sk_x509_value() */
- for (i = 0; i < len; ++i) {
- X509 * x = sk_X509_value(s, len - 1 - i);
- X509 * y = sk_X509_shift(s);
- X509 * z = xList[i];
- AssertIntEQ((x == y), 1);
- AssertIntEQ((x == z), 1);
- AssertIntEQ(sk_X509_num(s), len - 1 - i);
- }
- sk_free(s);
- for (i = 0; i < len; ++i)
- X509_free(xList[i]);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_sk_X509_CRL(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
- X509_CRL* crl;
- XFILE fp;
- STACK_OF(X509_CRL)* s;
- fp = XFOPEN("./certs/crl/crl.pem", "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
- XFCLOSE(fp);
- AssertNotNull(s = sk_X509_CRL_new());
- AssertIntEQ(sk_X509_CRL_num(s), 0);
- AssertIntEQ(sk_X509_CRL_push(s, crl), 1);
- AssertIntEQ(sk_X509_CRL_num(s), 1);
- AssertPtrEq(sk_X509_CRL_value(s, 0), crl);
- sk_X509_CRL_free(s);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_X509_get_signature_nid(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- X509* x509;
- AssertIntEQ(X509_get_signature_nid(NULL), 0);
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
- SSL_FILETYPE_PEM));
- AssertIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_X509_REQ(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
- X509_NAME* name;
- #ifndef NO_RSA
- X509_NAME* subject;
- #endif
- #if !defined(NO_RSA) || defined(HAVE_ECC)
- X509_REQ* req;
- EVP_PKEY* priv;
- EVP_PKEY* pub;
- unsigned char* der = NULL;
- int len;
- #endif
- #ifndef NO_RSA
- EVP_MD_CTX *mctx = NULL;
- EVP_PKEY_CTX *pkctx = NULL;
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
- const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
- const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
- #endif
- #endif
- #ifdef HAVE_ECC
- const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
- const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
- #endif
- AssertNotNull(name = X509_NAME_new());
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
- (byte*)"wolfssl.com", 11, 0, 1),
- WOLFSSL_SUCCESS);
- AssertIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
- (byte*)"support@wolfssl.com", 19, -1,
- 1), WOLFSSL_SUCCESS);
- #ifndef NO_RSA
- AssertNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
- (long)sizeof_client_key_der_2048));
- AssertNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
- (long)sizeof_client_keypub_der_2048));
- AssertNotNull(req = X509_REQ_new());
- AssertIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
- AssertIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
- AssertIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
- AssertIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
- AssertIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
- len = i2d_X509_REQ(req, &der);
- DEBUG_WRITE_DER(der, len, "req.der");
- #ifdef USE_CERT_BUFFERS_1024
- AssertIntEQ(len, 381);
- #else
- AssertIntEQ(len, 643);
- #endif
- XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
- der = NULL;
- mctx = EVP_MD_CTX_new();
- AssertIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
- EVP_MD_CTX_free(mctx);
- X509_REQ_free(NULL);
- X509_REQ_free(req);
- /* Test getting the subject from a newly created X509_REQ */
- AssertNotNull(req = X509_REQ_new());
- AssertNotNull(subject = X509_REQ_get_subject_name(req));
- AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
- MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
- AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
- MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
- AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
- MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
- AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
- MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
- AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
- MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
- AssertIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
- MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
- AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
- len = i2d_X509_REQ(req, &der);
- DEBUG_WRITE_DER(der, len, "req2.der");
- #ifdef USE_CERT_BUFFERS_1024
- AssertIntEQ(len, 435);
- #else
- AssertIntEQ(len, 696);
- #endif
- XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
- der = NULL;
- EVP_PKEY_free(pub);
- EVP_PKEY_free(priv);
- X509_REQ_free(req);
- #endif
- #ifdef HAVE_ECC
- AssertNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
- sizeof_ecc_clikey_der_256));
- AssertNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
- sizeof_ecc_clikeypub_der_256));
- AssertNotNull(req = X509_REQ_new());
- AssertIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
- AssertIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
- /* Signature is random and may be shorter or longer. */
- AssertIntGE((len = i2d_X509_REQ(req, &der)), 245);
- AssertIntLE(len, 253);
- XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
- X509_REQ_free(req);
- EVP_PKEY_free(pub);
- EVP_PKEY_free(priv);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #endif /* HAVE_ECC */
- X509_NAME_free(name);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfssl_PKCS7(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO)
- PKCS7* pkcs7;
- byte data[FOURK_BUF];
- word32 len = sizeof(data);
- const byte* p = data;
- byte content[] = "Test data to encode.";
- #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
- BIO* bio;
- byte key[sizeof(client_key_der_2048)];
- word32 keySz = (word32)sizeof(key);
- byte* out = NULL;
- #endif
- AssertIntGT((len = CreatePKCS7SignedData(data, len, content,
- (word32)sizeof(content),
- 0, 0, 0, RSA_TYPE)), 0);
- AssertNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
- AssertNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
- AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
- AssertIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
- PKCS7_NOVERIFY), WOLFSSL_FAILURE);
- PKCS7_free(pkcs7);
- /* fail case, without PKCS7_NOVERIFY */
- p = data;
- AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
- AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
- 0), WOLFSSL_FAILURE);
- PKCS7_free(pkcs7);
- /* success case, with PKCS7_NOVERIFY */
- p = data;
- AssertNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
- AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
- PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
- #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
- /* test i2d */
- XMEMCPY(key, client_key_der_2048, keySz);
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = (word32)sizeof(key);
- pkcs7->encryptOID = RSAk;
- #ifdef NO_SHA
- pkcs7->hashOID = SHA256h;
- #else
- pkcs7->hashOID = SHAh;
- #endif
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
- AssertIntEQ(i2d_PKCS7(pkcs7, &out), 655);
- XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- BIO_free(bio);
- #endif
- PKCS7_free(NULL);
- PKCS7_free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PKCS7_sign(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- PKCS7* p7 = NULL;
- PKCS7* p7Ver = NULL;
- byte* out = NULL;
- byte* tmpPtr = NULL;
- int outLen = 0;
- int flags = 0;
- byte data[] = "Test data to encode.";
- const char* cert = "./certs/server-cert.pem";
- const char* key = "./certs/server-key.pem";
- const char* ca = "./certs/ca-cert.pem";
- WOLFSSL_BIO* certBio = NULL;
- WOLFSSL_BIO* keyBio = NULL;
- WOLFSSL_BIO* caBio = NULL;
- WOLFSSL_BIO* inBio = NULL;
- X509* signCert = NULL;
- EVP_PKEY* signKey = NULL;
- X509* caCert = NULL;
- X509_STORE* store = NULL;
- /* read signer cert/key into BIO */
- AssertNotNull(certBio = BIO_new_file(cert, "r"));
- AssertNotNull(keyBio = BIO_new_file(key, "r"));
- AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
- AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
- /* read CA cert into store (for verify) */
- AssertNotNull(caBio = BIO_new_file(ca, "r"));
- AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
- AssertNotNull(store = X509_STORE_new());
- AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
- /* data to be signed into BIO */
- AssertNotNull(inBio = BIO_new(BIO_s_mem()));
- AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
- /* PKCS7_sign, bad args: signer NULL */
- AssertNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
- /* PKCS7_sign, bad args: signer key NULL */
- AssertNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
- /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
- AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
- /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
- AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
- /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
- AssertNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
- /* TEST SUCCESS: Not detached, not streaming, not MIME */
- {
- flags = PKCS7_BINARY;
- AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
- AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
- /* verify with d2i_PKCS7 */
- tmpPtr = out;
- AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
- AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
- PKCS7_free(p7Ver);
- /* verify with wc_PKCS7_VerifySignedData */
- AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
- AssertIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
- /* compare the signer found to expected signer */
- AssertIntNE(p7Ver->verifyCertSz, 0);
- tmpPtr = NULL;
- AssertIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
- AssertIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
- XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
- tmpPtr = NULL;
- wc_PKCS7_Free(p7Ver);
- AssertNotNull(out);
- XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- out = NULL;
- PKCS7_free(p7);
- }
- /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
- * tests for PKCS7_final() while we have a PKCS7 pointer to use */
- {
- /* re-populate input BIO, may have been consumed */
- BIO_free(inBio);
- AssertNotNull(inBio = BIO_new(BIO_s_mem()));
- AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
- flags = PKCS7_BINARY | PKCS7_STREAM;
- AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
- AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
- AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
- /* PKCS7_final, bad args: PKCS7 null */
- AssertIntEQ(PKCS7_final(NULL, inBio, 0), 0);
- /* PKCS7_final, bad args: PKCS7 null */
- AssertIntEQ(PKCS7_final(p7, NULL, 0), 0);
- tmpPtr = out;
- AssertNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
- AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
- PKCS7_free(p7Ver);
- AssertNotNull(out);
- XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- out = NULL;
- PKCS7_free(p7);
- }
- /* TEST SUCCESS: Detached, not streaming, not MIME */
- {
- /* re-populate input BIO, may have been consumed */
- BIO_free(inBio);
- AssertNotNull(inBio = BIO_new(BIO_s_mem()));
- AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
- flags = PKCS7_BINARY | PKCS7_DETACHED;
- AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
- AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
- /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
- AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
- p7Ver->content = data;
- p7Ver->contentSz = sizeof(data);
- AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
- wc_PKCS7_Free(p7Ver);
- /* verify expected failure (NULL return) from d2i_PKCS7, it does not
- * yet support detached content */
- tmpPtr = out;
- AssertNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
- PKCS7_free(p7Ver);
- AssertNotNull(out);
- XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- out = NULL;
- PKCS7_free(p7);
- }
- /* TEST SUCCESS: Detached, streaming, not MIME */
- {
- /* re-populate input BIO, may have been consumed */
- BIO_free(inBio);
- AssertNotNull(inBio = BIO_new(BIO_s_mem()));
- AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
- flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
- AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
- AssertIntEQ(PKCS7_final(p7, inBio, flags), 1);
- AssertIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
- /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
- AssertNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
- p7Ver->content = data;
- p7Ver->contentSz = sizeof(data);
- AssertIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
- wc_PKCS7_Free(p7Ver);
- AssertNotNull(out);
- XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- PKCS7_free(p7);
- }
- X509_STORE_free(store);
- X509_free(caCert);
- X509_free(signCert);
- EVP_PKEY_free(signKey);
- BIO_free(inBio);
- BIO_free(keyBio);
- BIO_free(certBio);
- BIO_free(caBio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PKCS7_SIGNED_new(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
- PKCS7_SIGNED* pkcs7;
- pkcs7 = PKCS7_SIGNED_new();
- AssertNotNull(pkcs7);
- AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
- PKCS7_SIGNED_free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_write_bio_PKCS7(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
- PKCS7* pkcs7 = NULL;
- BIO* bio = NULL;
- const byte* cert_buf = NULL;
- int ret = 0;
- WC_RNG rng;
- const byte data[] = { /* Hello World */
- 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
- 0x72,0x6c,0x64
- };
- #ifndef NO_RSA
- #if defined(USE_CERT_BUFFERS_2048)
- byte key[sizeof(client_key_der_2048)];
- byte cert[sizeof(client_cert_der_2048)];
- word32 keySz = (word32)sizeof(key);
- word32 certSz = (word32)sizeof(cert);
- XMEMSET(key, 0, keySz);
- XMEMSET(cert, 0, certSz);
- XMEMCPY(key, client_key_der_2048, keySz);
- XMEMCPY(cert, client_cert_der_2048, certSz);
- #elif defined(USE_CERT_BUFFERS_1024)
- byte key[sizeof_client_key_der_1024];
- byte cert[sizeof(sizeof_client_cert_der_1024)];
- word32 keySz = (word32)sizeof(key);
- word32 certSz = (word32)sizeof(cert);
- XMEMSET(key, 0, keySz);
- XMEMSET(cert, 0, certSz);
- XMEMCPY(key, client_key_der_1024, keySz);
- XMEMCPY(cert, client_cert_der_1024, certSz);
- #else
- unsigned char cert[ONEK_BUF];
- unsigned char key[ONEK_BUF];
- XFILE fp;
- int certSz;
- int keySz;
- fp = XFOPEN("./certs/1024/client-cert.der", "rb");
- AssertTrue((fp != XBADFILE));
- certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024, fp);
- XFCLOSE(fp);
- fp = XFOPEN("./certs/1024/client-key.der", "rb");
- AssertTrue(fp != XBADFILE);
- keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp);
- XFCLOSE(fp);
- #endif
- #elif defined(HAVE_ECC)
- #if defined(USE_CERT_BUFFERS_256)
- unsigned char cert[sizeof(cliecc_cert_der_256)];
- unsigned char key[sizeof(ecc_clikey_der_256)];
- int certSz = (int)sizeof(cert);
- int keySz = (int)sizeof(key);
- XMEMSET(cert, 0, certSz);
- XMEMSET(key, 0, keySz);
- XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
- XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
- #else
- unsigned char cert[ONEK_BUF];
- unsigned char key[ONEK_BUF];
- XFILE fp;
- int certSz, keySz;
- fp = XFOPEN("./certs/client-ecc-cert.der", "rb");
- AssertTrue(fp != XBADFILE);
- certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256, fp);
- XFCLOSE(fp);
- fp = XFOPEN("./certs/client-ecc-key.der", "rb");
- AssertTrue(fp != XBADFILE);
- keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp);
- XFCLOSE(fp);
- #endif
- #else
- #error PKCS7 requires ECC or RSA
- #endif
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
- /* initialize with DER encoded cert */
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
- /* init rng */
- AssertIntEQ(wc_InitRng(&rng), 0);
- pkcs7->rng = &rng;
- pkcs7->content = (byte*)data; /* not used for ex */
- pkcs7->contentSz = (word32)sizeof(data);
- pkcs7->contentOID = SIGNED_DATA;
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = (word32)sizeof(key);
- pkcs7->encryptOID = RSAk;
- #ifdef NO_SHA
- pkcs7->hashOID = SHA256h;
- #else
- pkcs7->hashOID = SHAh;
- #endif
- pkcs7->signedAttribs = NULL;
- pkcs7->signedAttribsSz = 0;
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
- AssertIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
- /* Read PKCS#7 PEM from BIO */
- ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
- AssertIntGE(ret, 0);
- BIO_free(bio);
- wc_PKCS7_Free(pkcs7);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #ifdef HAVE_SMIME
- static int test_wolfSSL_SMIME_read_PKCS7(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA)
- PKCS7* pkcs7 = NULL;
- BIO* bio = NULL;
- BIO* bcont = NULL;
- BIO* out = NULL;
- const byte* outBuf = NULL;
- int outBufLen = 0;
- static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
- XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
- /* smime-test.p7s */
- bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
- AssertNotNull(bio);
- AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
- pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
- AssertNotNull(pkcs7);
- AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
- PKCS7_NOVERIFY), SSL_SUCCESS);
- XFCLOSE(smimeTestFile);
- if (bcont) BIO_free(bcont);
- wolfSSL_PKCS7_free(pkcs7);
- /* smime-test-multipart.p7s */
- smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
- AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
- pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
- AssertNotNull(pkcs7);
- AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
- PKCS7_NOVERIFY), SSL_SUCCESS);
- XFCLOSE(smimeTestFile);
- if (bcont) BIO_free(bcont);
- wolfSSL_PKCS7_free(pkcs7);
- /* smime-test-multipart-badsig.p7s */
- smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
- AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
- pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
- AssertNull(pkcs7);
- AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
- PKCS7_NOVERIFY), SSL_FAILURE);
- XFCLOSE(smimeTestFile);
- if (bcont) BIO_free(bcont);
- wolfSSL_PKCS7_free(pkcs7);
- /* smime-test-canon.p7s */
- smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
- AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
- pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
- AssertNotNull(pkcs7);
- AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
- PKCS7_NOVERIFY), SSL_SUCCESS);
- XFCLOSE(smimeTestFile);
- if (bcont) BIO_free(bcont);
- wolfSSL_PKCS7_free(pkcs7);
- /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
- smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
- AssertIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
- pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
- AssertNotNull(pkcs7);
- out = wolfSSL_BIO_new(BIO_s_mem());
- AssertNotNull(out);
- AssertIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
- PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
- AssertIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
- /* Content-Type should not show up at beginning of output buffer */
- AssertIntGT(outBufLen, XSTRLEN(contTypeText));
- AssertIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
- BIO_free(out);
- BIO_free(bio);
- if (bcont) BIO_free(bcont);
- wolfSSL_PKCS7_free(pkcs7);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SMIME_write_PKCS7(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
- PKCS7* p7 = NULL;
- PKCS7* p7Ver = NULL;
- int flags = 0;
- byte data[] = "Test data to encode.";
- const char* cert = "./certs/server-cert.pem";
- const char* key = "./certs/server-key.pem";
- const char* ca = "./certs/ca-cert.pem";
- WOLFSSL_BIO* certBio = NULL;
- WOLFSSL_BIO* keyBio = NULL;
- WOLFSSL_BIO* caBio = NULL;
- WOLFSSL_BIO* inBio = NULL;
- WOLFSSL_BIO* outBio = NULL;
- WOLFSSL_BIO* content = NULL;
- X509* signCert = NULL;
- EVP_PKEY* signKey = NULL;
- X509* caCert = NULL;
- X509_STORE* store = NULL;
- /* read signer cert/key into BIO */
- AssertNotNull(certBio = BIO_new_file(cert, "r"));
- AssertNotNull(keyBio = BIO_new_file(key, "r"));
- AssertNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
- AssertNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
- /* read CA cert into store (for verify) */
- AssertNotNull(caBio = BIO_new_file(ca, "r"));
- AssertNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
- AssertNotNull(store = X509_STORE_new());
- AssertIntEQ(X509_STORE_add_cert(store, caCert), 1);
- /* generate and verify SMIME: not detached */
- {
- AssertNotNull(inBio = BIO_new(BIO_s_mem()));
- AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
- flags = PKCS7_STREAM;
- AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
- AssertNotNull(outBio = BIO_new(BIO_s_mem()));
- AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
- /* bad arg: out NULL */
- AssertIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
- /* bad arg: pkcs7 NULL */
- AssertIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
- AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
- AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
- BIO_free(content);
- BIO_free(inBio);
- BIO_free(outBio);
- PKCS7_free(p7Ver);
- PKCS7_free(p7);
- }
- /* generate and verify SMIME: not detached, add Content-Type */
- {
- AssertNotNull(inBio = BIO_new(BIO_s_mem()));
- AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
- flags = PKCS7_STREAM | PKCS7_TEXT;
- AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
- AssertNotNull(outBio = BIO_new(BIO_s_mem()));
- AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
- AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
- AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
- BIO_free(content);
- BIO_free(inBio);
- BIO_free(outBio);
- PKCS7_free(p7Ver);
- PKCS7_free(p7);
- }
- /* generate and verify SMIME: detached */
- {
- AssertNotNull(inBio = BIO_new(BIO_s_mem()));
- AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
- flags = PKCS7_DETACHED | PKCS7_STREAM;
- AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
- AssertNotNull(outBio = BIO_new(BIO_s_mem()));
- AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
- AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
- AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
- BIO_free(content);
- BIO_free(inBio);
- BIO_free(outBio);
- PKCS7_free(p7Ver);
- PKCS7_free(p7);
- }
- /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
- {
- AssertNotNull(inBio = BIO_new(BIO_s_mem()));
- AssertIntGT(BIO_write(inBio, data, sizeof(data)), 0);
- flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
- AssertNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
- AssertNotNull(outBio = BIO_new(BIO_s_mem()));
- AssertIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
- AssertNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
- AssertIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
- BIO_free(content);
- BIO_free(inBio);
- BIO_free(outBio);
- PKCS7_free(p7Ver);
- PKCS7_free(p7);
- }
- X509_STORE_free(store);
- X509_free(caCert);
- X509_free(signCert);
- EVP_PKEY_free(signKey);
- BIO_free(keyBio);
- BIO_free(certBio);
- BIO_free(caBio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif /* HAVE_SMIME */
- #endif /* !NO_BIO */
- /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
- * returns 0) */
- static int test_X509_STORE_No_SSL_CTX(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
- !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
- (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
- (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
- X509_STORE * store;
- X509_STORE_CTX * storeCtx;
- X509_CRL * crl;
- X509 * ca;
- X509 * cert;
- const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
- const char srvCert[] = "./certs/server-cert.pem";
- const char caCert[] = "./certs/ca-cert.pem";
- const char caDir[] = "./certs/crl/hash_pem";
- XFILE fp;
- X509_LOOKUP * lookup;
- AssertNotNull(store = (X509_STORE *)X509_STORE_new());
- /* Set up store with CA */
- AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
- SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
- /* Add CRL lookup directory to store
- * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
- * of crl.pem */
- AssertNotNull((lookup = X509_STORE_add_lookup(store,
- X509_LOOKUP_hash_dir())));
- AssertIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
- X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
- AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
- SSL_SUCCESS);
- /* Add CRL to store NOT containing the verified certificate, which
- * forces use of the CRL lookup directory */
- fp = XFOPEN(cliCrlPem, "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
- NULL, NULL));
- XFCLOSE(fp);
- AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
- /* Create verification context outside of an SSL session */
- AssertNotNull((storeCtx = X509_STORE_CTX_new()));
- AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
- SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
- /* Perform verification, which should NOT indicate CRL missing due to the
- * store CM's X509 store pointer being NULL */
- AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
- X509_CRL_free(crl);
- X509_STORE_free(store);
- X509_STORE_CTX_free(storeCtx);
- X509_free(cert);
- X509_free(ca);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /* Test of X509 store use outside of SSL context w/ CRL lookup, but
- * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
- static int test_X509_LOOKUP_add_dir(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
- !defined(NO_WOLFSSL_DIR) && defined(HAVE_CRL) && \
- (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
- (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
- X509_STORE * store;
- X509_STORE_CTX * storeCtx;
- X509_CRL * crl;
- X509 * ca;
- X509 * cert;
- const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
- const char srvCert[] = "./certs/server-cert.pem";
- const char caCert[] = "./certs/ca-cert.pem";
- const char caDir[] = "./certs/crl/hash_der";
- XFILE fp;
- X509_LOOKUP * lookup;
- AssertNotNull(store = (X509_STORE *)X509_STORE_new());
- /* Set up store with CA */
- AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
- SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
- /* Add CRL lookup directory to store.
- * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
- * of crl.der */
- AssertNotNull((lookup = X509_STORE_add_lookup(store,
- X509_LOOKUP_hash_dir())));
- AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
- SSL_SUCCESS);
- AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
- SSL_SUCCESS);
- /* Add CRL to store NOT containing the verified certificate, which
- * forces use of the CRL lookup directory */
- fp = XFOPEN(cliCrlPem, "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
- NULL, NULL));
- XFCLOSE(fp);
- AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
- /* Create verification context outside of an SSL session */
- AssertNotNull((storeCtx = X509_STORE_CTX_new()));
- AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
- SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
- /* Perform verification, which should NOT return CRL missing */
- AssertIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
- X509_CRL_free(crl);
- X509_STORE_free(store);
- X509_STORE_CTX_free(storeCtx);
- X509_free(cert);
- X509_free(ca);
- /* Now repeat the same, but look for X509_FILETYPE_PEM.
- * We should get CRL_MISSING at the end, because the lookup
- * dir has only ASN1 CRLs. */
- AssertNotNull(store = (X509_STORE *)X509_STORE_new());
- AssertNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
- SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
- AssertNotNull((lookup = X509_STORE_add_lookup(store,
- X509_LOOKUP_hash_dir())));
- AssertIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
- SSL_SUCCESS);
- AssertIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
- SSL_SUCCESS);
- fp = XFOPEN(cliCrlPem, "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
- NULL, NULL));
- XFCLOSE(fp);
- AssertIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
- AssertNotNull((storeCtx = X509_STORE_CTX_new()));
- AssertNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
- SSL_FILETYPE_PEM)));
- AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
- /* Now we SHOULD get CRL_MISSING, because we looked for PEM
- * in dir containing only ASN1/DER. */
- AssertIntEQ(X509_verify_cert(storeCtx), CRL_MISSING);
- X509_CRL_free(crl);
- X509_STORE_free(store);
- X509_STORE_CTX_free(storeCtx);
- X509_free(cert);
- X509_free(ca);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /*----------------------------------------------------------------------------*
- | Certificate Failure Checks
- *----------------------------------------------------------------------------*/
- #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
- !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
- /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
- static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
- int type)
- {
- int ret;
- WOLFSSL_CERT_MANAGER* cm = NULL;
- switch (type) {
- case TESTING_RSA:
- #ifdef NO_RSA
- fprintf(stderr, "RSA disabled, skipping test\n");
- return ASN_SIG_CONFIRM_E;
- #else
- break;
- #endif
- case TESTING_ECC:
- #ifndef HAVE_ECC
- fprintf(stderr, "ECC disabled, skipping test\n");
- return ASN_SIG_CONFIRM_E;
- #else
- break;
- #endif
- default:
- fprintf(stderr, "Bad function argument\n");
- return BAD_FUNC_ARG;
- }
- cm = wolfSSL_CertManagerNew();
- if (cm == NULL) {
- fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
- return -1;
- }
- #ifndef NO_FILESYSTEM
- ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
- if (ret != WOLFSSL_SUCCESS) {
- fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
- wolfSSL_CertManagerFree(cm);
- return ret;
- }
- #else
- (void)ca;
- #endif
- ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz, WOLFSSL_FILETYPE_ASN1);
- /* Let AssertIntEQ handle return code */
- wolfSSL_CertManagerFree(cm);
- return ret;
- }
- #if !defined(NO_FILESYSTEM)
- static int test_RsaSigFailure_cm(void)
- {
- int ret = 0;
- const char* ca_cert = "./certs/ca-cert.pem";
- const char* server_cert = "./certs/server-cert.der";
- byte* cert_buf = NULL;
- size_t cert_sz = 0;
- ret = load_file(server_cert, &cert_buf, &cert_sz);
- if (ret == 0) {
- /* corrupt DER - invert last byte, which is signature */
- cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
- /* test bad cert */
- ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA);
- }
- if (cert_buf)
- free(cert_buf);
- #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
- if (ret == WOLFSSL_FATAL_ERROR) {
- ret = 0;
- }
- #else
- if (ret == ASN_SIG_CONFIRM_E) {
- ret = 0;
- }
- #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
- return TEST_RES_CHECK(ret == 0);
- }
- static int test_EccSigFailure_cm(void)
- {
- int ret = 0;
- /* self-signed ECC cert, so use server cert as CA */
- const char* ca_cert = "./certs/ca-ecc-cert.pem";
- const char* server_cert = "./certs/server-ecc.der";
- byte* cert_buf = NULL;
- size_t cert_sz = 0;
- ret = load_file(server_cert, &cert_buf, &cert_sz);
- if (ret == 0) {
- /* corrupt DER - invert last byte, which is signature */
- cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
- /* test bad cert */
- ret = verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC);
- }
- if (cert_buf)
- free(cert_buf);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
- if (ret == WOLFSSL_FATAL_ERROR) {
- ret = 0;
- }
- #else
- if (ret == ASN_SIG_CONFIRM_E) {
- ret = 0;
- }
- #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
- return TEST_RES_CHECK(ret == 0);
- }
- #endif /* !NO_FILESYSTEM */
- #endif /* NO_CERTS */
- #ifdef WOLFSSL_TLS13
- #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
- #ifdef WC_SHA384_DIGEST_SIZE
- static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
- #else
- static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
- #endif
- #endif
- #ifdef WOLFSSL_EARLY_DATA
- static const char earlyData[] = "Early Data";
- static char earlyDataBuffer[1];
- #endif
- static int test_tls13_apis(void)
- {
- int ret = 0;
- #ifndef WOLFSSL_NO_TLS12
- #ifndef NO_WOLFSSL_CLIENT
- WOLFSSL_CTX* clientTls12Ctx;
- WOLFSSL* clientTls12Ssl;
- #endif
- #ifndef NO_WOLFSSL_SERVER
- WOLFSSL_CTX* serverTls12Ctx;
- WOLFSSL* serverTls12Ssl;
- #endif
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- WOLFSSL_CTX* clientCtx;
- WOLFSSL* clientSsl;
- #endif
- #ifndef NO_WOLFSSL_SERVER
- WOLFSSL_CTX* serverCtx;
- WOLFSSL* serverSsl;
- #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
- const char* ourCert = svrCertFile;
- const char* ourKey = svrKeyFile;
- #endif
- #endif
- int required;
- #ifdef WOLFSSL_EARLY_DATA
- int outSz;
- #endif
- #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
- int groups[2] = { WOLFSSL_ECC_SECP256R1,
- #ifdef HAVE_PQC
- WOLFSSL_KYBER_LEVEL1
- #else
- WOLFSSL_ECC_SECP256R1
- #endif
- };
- #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
- int bad_groups[2] = { 0xDEAD, 0xBEEF };
- #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
- int numGroups = 2;
- #endif
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
- char groupList[] =
- #ifndef NO_ECC_SECP
- #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
- "P-521:"
- #endif
- #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
- "P-384:"
- #endif
- #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
- "P-256"
- #ifdef HAVE_PQC
- ":P256_KYBER_LEVEL1"
- #endif
- #endif
- #ifdef HAVE_PQC
- ":KYBER_LEVEL1"
- #endif
- "";
- #endif /* !defined(NO_ECC_SECP) */
- #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
- (void)ret;
- #ifndef WOLFSSL_NO_TLS12
- #ifndef NO_WOLFSSL_CLIENT
- clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
- clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
- #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
- wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
- wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey, WOLFSSL_FILETYPE_PEM);
- #endif
- serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
- #endif
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
- clientSsl = wolfSSL_new(clientCtx);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
- #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
- wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
- wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
- #endif
- serverSsl = wolfSSL_new(serverCtx);
- #endif
- #ifdef WOLFSSL_SEND_HRR_COOKIE
- AssertIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
- WOLFSSL_SUCCESS);
- #endif
- #endif
- #ifdef HAVE_SUPPORTED_CURVES
- #ifdef HAVE_ECC
- AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_SERVER
- do {
- ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
- #endif
- } while (ret == WC_PENDING_E);
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- do {
- ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
- #endif
- } while (ret == WC_PENDING_E);
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- #endif
- do {
- ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
- #ifdef WOLFSSL_ASYNC_CRYPT
- if (ret == WC_PENDING_E)
- wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
- #endif
- } while (ret == WC_PENDING_E);
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- #endif
- #elif defined(HAVE_CURVE25519)
- AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
- WOLFSSL_SUCCESS);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
- WOLFSSL_SUCCESS);
- #endif
- AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
- WOLFSSL_SUCCESS);
- #endif
- #elif defined(HAVE_CURVE448)
- AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
- WOLFSSL_SUCCESS);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
- WOLFSSL_SUCCESS);
- #endif
- AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
- WOLFSSL_SUCCESS);
- #endif
- #else
- AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
- NOT_COMPILED_IN);
- #endif
- AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
- NOT_COMPILED_IN);
- #endif
- #endif
- #if defined(HAVE_PQC)
- AssertIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
- WOLFSSL_SUCCESS);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
- WOLFSSL_SUCCESS);
- #endif
- #endif
- AssertIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
- #endif
- AssertIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
- #endif
- #endif /* HAVE_SUPPORTED_CURVES */
- AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
- #endif
- AssertIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
- #endif
- AssertIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
- #endif
- AssertIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
- #endif
- AssertIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
- #endif
- AssertIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
- #endif
- #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
- AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
- #endif
- AssertIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
- #endif
- AssertIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
- #endif
- #endif
- #ifdef HAVE_ECC
- #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
- AssertIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
- #endif
- #endif
- #ifdef HAVE_SUPPORTED_CURVES
- AssertIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
- WOLFSSL_MAX_GROUP_COUNT + 1),
- BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
- BAD_FUNC_ARG);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_set_groups(clientSsl, groups,
- WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
- BAD_FUNC_ARG);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
- BAD_FUNC_ARG);
- #endif
- #ifdef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL), WOLFSSL_FAILURE);
- #endif
- AssertIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
- WOLFSSL_FAILURE);
- #endif
- AssertIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
- WOLFSSL_SUCCESS);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
- WOLFSSL_SUCCESS);
- #endif
- AssertIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
- #endif
- AssertIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
- WOLFSSL_FAILURE);
- #endif
- AssertIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
- WOLFSSL_SUCCESS);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
- WOLFSSL_SUCCESS);
- #endif
- #endif /* OPENSSL_EXTRA */
- #endif /* HAVE_SUPPORTED_CURVES */
- #endif /* HAVE_ECC */
- #ifdef WOLFSSL_EARLY_DATA
- #ifndef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
- #else
- AssertIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
- AssertIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
- #else
- AssertIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
- AssertIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
- #endif
- #endif
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- #ifndef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
- #else
- AssertIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
- BAD_FUNC_ARG);
- AssertIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
- #endif
- #endif
- #ifndef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
- AssertIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
- #else
- AssertIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
- AssertIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
- #endif
- #endif
- #ifndef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
- #else
- AssertIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
- AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
- #else
- AssertIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_get_max_early_data(clientSsl), 17);
- #endif
- #endif
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- #ifndef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
- #else
- AssertIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
- AssertIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
- #endif
- #endif
- #ifndef OPENSSL_EXTRA
- AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
- AssertIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
- #else
- AssertIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
- AssertIntEQ(SSL_get_max_early_data(serverSsl), 16);
- #endif
- #endif
- AssertIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
- &outSz), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
- &outSz), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
- BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
- sizeof(earlyData), NULL),
- BAD_FUNC_ARG);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
- sizeof(earlyData), &outSz),
- SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
- sizeof(earlyData), &outSz),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
- sizeof(earlyData), &outSz),
- WOLFSSL_FATAL_ERROR);
- #endif
- AssertIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
- sizeof(earlyDataBuffer), &outSz),
- BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
- sizeof(earlyDataBuffer), &outSz),
- BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1, &outSz),
- BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
- sizeof(earlyDataBuffer), NULL),
- BAD_FUNC_ARG);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
- sizeof(earlyDataBuffer), &outSz),
- SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
- sizeof(earlyDataBuffer), &outSz),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
- sizeof(earlyDataBuffer), &outSz),
- WOLFSSL_FATAL_ERROR);
- #endif
- #endif
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
- AssertIntLT(SSL_get_early_data_status(NULL), 0);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- wolfSSL_free(serverSsl);
- wolfSSL_CTX_free(serverCtx);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- wolfSSL_free(clientSsl);
- wolfSSL_CTX_free(clientCtx);
- #endif
- #ifndef WOLFSSL_NO_TLS12
- #ifndef NO_WOLFSSL_SERVER
- wolfSSL_free(serverTls12Ssl);
- wolfSSL_CTX_free(serverTls12Ctx);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- wolfSSL_free(clientTls12Ssl);
- wolfSSL_CTX_free(clientTls12Ctx);
- #endif
- #endif
- return TEST_RES_CHECK(1);
- }
- #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
- defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
- defined(BUILD_TLS_AES_256_GCM_SHA384)
- /* Called when writing. */
- static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
- {
- (void)ssl;
- (void)buf;
- (void)sz;
- (void)ctx;
- /* Force error return from wolfSSL_accept_TLSv13(). */
- return WANT_WRITE;
- }
- /* Called when reading. */
- static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
- {
- WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
- int len = (int)msg->length;
- (void)ssl;
- (void)sz;
- /* Pass back as much of message as will fit in buffer. */
- if (len > sz)
- len = sz;
- XMEMCPY(buf, msg->buffer, len);
- /* Move over returned data. */
- msg->buffer += len;
- msg->length -= len;
- /* Amount actually copied. */
- return len;
- }
- #endif
- static int test_tls13_cipher_suites(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
- defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
- defined(BUILD_TLS_AES_256_GCM_SHA384)
- WOLFSSL_CTX* ctx;
- WOLFSSL *ssl;
- int i;
- byte clientHello[] = {
- 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
- 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
- 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
- 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
- 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
- 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
- 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
- 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
- /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
- 0x13, 0x01,
- 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
- 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
- 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
- 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
- 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
- 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
- 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
- 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
- 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
- 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
- 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
- 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
- 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
- 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
- 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
- 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
- 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
- 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
- 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
- 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
- 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
- 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
- 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
- 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
- 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
- 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
- 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
- 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
- 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
- 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
- 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
- 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
- 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
- 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
- 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
- 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
- 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
- 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
- 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
- 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
- };
- WOLFSSL_BUFFER_INFO msg;
- /* Offset into ClientHello message data of first cipher suite. */
- const int csOff = 78;
- /* Server cipher list. */
- const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
- /* Suite list with duplicates. */
- const char* dupCs = "TLS13-AES128-GCM-SHA256:"
- "TLS13-AES128-GCM-SHA256:"
- "TLS13-AES256-GCM-SHA384:"
- "TLS13-AES256-GCM-SHA384:"
- "TLS13-AES128-GCM-SHA256";
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
- const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
- TLS13_BYTE, TLS_AES_256_GCM_SHA384,
- TLS13_BYTE, TLS_AES_128_GCM_SHA256,
- TLS13_BYTE, TLS_AES_128_GCM_SHA256,
- TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
- #endif
- /* Set up wolfSSL context. */
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
- WOLFSSL_FILETYPE_PEM));
- /* Read from 'msg'. */
- wolfSSL_SetIORecv(ctx, CsRecv);
- /* No where to send to - dummy sender. */
- wolfSSL_SetIOSend(ctx, CsSend);
- /* Test cipher suite list with many copies of a cipher suite. */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- msg.buffer = clientHello;
- msg.length = (unsigned int)sizeof(clientHello);
- wolfSSL_SetIOReadCtx(ssl, &msg);
- /* Force server to have as many occurrences of same cipher suite as
- * possible. */
- ssl->suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
- for (i = 0; i < ssl->suites->suiteSz; i += 2) {
- ssl->suites->suites[i + 0] = TLS13_BYTE;
- ssl->suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
- }
- /* Test multiple occurrences of same cipher suite. */
- wolfSSL_accept_TLSv13(ssl);
- wolfSSL_free(ssl);
- /* Set client order opposite to server order:
- * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
- clientHello[csOff + 0] = TLS13_BYTE;
- clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
- clientHello[csOff + 2] = TLS13_BYTE;
- clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
- /* Test server order negotiation. */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- msg.buffer = clientHello;
- msg.length = (unsigned int)sizeof(clientHello);
- wolfSSL_SetIOReadCtx(ssl, &msg);
- /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
- AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
- /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
- wolfSSL_accept_TLSv13(ssl);
- /* Check refined order - server order. */
- AssertIntEQ(ssl->suites->suiteSz, 4);
- AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
- AssertIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
- AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
- AssertIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
- wolfSSL_free(ssl);
- /* Test client order negotiation. */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- msg.buffer = clientHello;
- msg.length = (unsigned int)sizeof(clientHello);
- wolfSSL_SetIOReadCtx(ssl, &msg);
- /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
- AssertIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_UseClientSuites(ssl), 0);
- /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
- wolfSSL_accept_TLSv13(ssl);
- /* Check refined order - client order. */
- AssertIntEQ(ssl->suites->suiteSz, 4);
- AssertIntEQ(ssl->suites->suites[0], TLS13_BYTE);
- AssertIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
- AssertIntEQ(ssl->suites->suites[2], TLS13_BYTE);
- AssertIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
- wolfSSL_free(ssl);
- /* Check duplicate detection is working. */
- AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
- AssertIntEQ(ctx->suites->suiteSz, 4);
- AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
- AssertIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
- AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
- AssertIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
- #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
- AssertIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
- sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
- AssertIntEQ(ctx->suites->suiteSz, 4);
- AssertIntEQ(ctx->suites->suites[0], TLS13_BYTE);
- AssertIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
- AssertIntEQ(ctx->suites->suites[2], TLS13_BYTE);
- AssertIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
- #endif
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif
- #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
- !defined(NO_OLD_TLS))
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
- !defined(NO_AES) && defined(HAVE_AES_CBC) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
- static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
- const unsigned char* priv, unsigned int privSz,
- const unsigned char* pubKeyDer, unsigned int pubKeySz,
- unsigned char* out, unsigned int* outlen,
- void* ctx)
- {
- int result;
- /* Test fail when context associated with WOLFSSL is NULL */
- if (ctx == NULL) {
- return -1;
- }
- (void)ssl;
- /* return 0 on success */
- PRIVATE_KEY_UNLOCK();
- result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
- PRIVATE_KEY_LOCK();
- return result;
- }
- static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
- wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
- #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
- AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
- WOLFSSL_SUCCESS);
- #endif
- #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
- AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
- WOLFSSL_SUCCESS);
- #endif
- }
- static void test_dh_ssl_setup(WOLFSSL* ssl)
- {
- static int dh_test_ctx = 1;
- int ret;
- wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
- AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
- ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
- if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- }
- }
- static void test_dh_ssl_setup_fail(WOLFSSL* ssl)
- {
- int ret;
- wolfSSL_SetDhAgreeCtx(ssl, NULL);
- AssertNull(wolfSSL_GetDhAgreeCtx(ssl));
- ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
- if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- }
- }
- #endif
- static int test_DhCallbacks(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_DH) && \
- !defined(NO_AES) && defined(HAVE_AES_CBC) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
- WOLFSSL_CTX *ctx;
- WOLFSSL *ssl;
- tcp_ready ready;
- func_args server_args;
- func_args client_args;
- THREAD_TYPE serverThread;
- callback_functions func_cb_client;
- callback_functions func_cb_server;
- int test;
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #endif
- AssertIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
- wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
- /* load client ca cert */
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
- WOLFSSL_SUCCESS);
- /* test with NULL arguments */
- wolfSSL_SetDhAgreeCtx(NULL, &test);
- AssertNull(wolfSSL_GetDhAgreeCtx(NULL));
- /* test success case */
- test = 1;
- AssertNotNull(ssl = wolfSSL_new(ctx));
- wolfSSL_SetDhAgreeCtx(ssl, &test);
- AssertIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- /* test a connection where callback is used */
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
- XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- server_args.return_code = TEST_FAIL;
- client_args.return_code = TEST_FAIL;
- /* set callbacks to use DH functions */
- func_cb_client.ctx_ready = &test_dh_ctx_setup;
- func_cb_client.ssl_ready = &test_dh_ssl_setup;
- #ifndef WOLFSSL_NO_TLS12
- func_cb_client.method = wolfTLSv1_2_client_method;
- #else
- func_cb_client.method = wolfTLSv1_3_client_method;
- #endif
- client_args.callbacks = &func_cb_client;
- func_cb_server.ctx_ready = &test_dh_ctx_setup;
- func_cb_server.ssl_ready = &test_dh_ssl_setup;
- #ifndef WOLFSSL_NO_TLS12
- func_cb_server.method = wolfTLSv1_2_server_method;
- #else
- func_cb_server.method = wolfTLSv1_3_server_method;
- #endif
- server_args.callbacks = &func_cb_server;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- /* now set user ctx to not be 1 so that the callback returns fail case */
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
- XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- server_args.return_code = TEST_FAIL;
- client_args.return_code = TEST_FAIL;
- /* set callbacks to use DH functions */
- func_cb_client.ctx_ready = &test_dh_ctx_setup;
- func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
- #ifndef WOLFSSL_NO_TLS12
- func_cb_client.method = wolfTLSv1_2_client_method;
- #else
- func_cb_client.method = wolfTLSv1_3_client_method;
- #endif
- client_args.callbacks = &func_cb_client;
- func_cb_server.ctx_ready = &test_dh_ctx_setup;
- func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
- #ifndef WOLFSSL_NO_TLS12
- func_cb_server.method = wolfTLSv1_2_server_method;
- #else
- func_cb_server.method = wolfTLSv1_3_server_method;
- #endif
- server_args.callbacks = &func_cb_server;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- AssertIntEQ(client_args.return_code, TEST_FAIL);
- AssertIntEQ(server_args.return_code, TEST_FAIL);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif /* HAVE_PK_CALLBACKS */
- #ifdef HAVE_HASHDRBG
- #ifdef TEST_RESEED_INTERVAL
- static int test_wc_RNG_GenerateBlock_Reseed(void)
- {
- int i, ret;
- WC_RNG rng;
- byte key[32];
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
- ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
- if (ret != 0) {
- break;
- }
- }
- }
- wc_FreeRng(&rng);
- return TEST_RES_CHECK(ret == 0);
- }
- #endif /* TEST_RESEED_INTERVAL */
- static int test_wc_RNG_GenerateBlock(void)
- {
- int i, ret;
- WC_RNG rng;
- byte key[32];
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- for (i = 0; i < 10; i++) {
- ret = wc_RNG_GenerateBlock(&rng, key, sizeof(key));
- if (ret != 0) {
- break;
- }
- }
- }
- wc_FreeRng(&rng);
- (void)rng; /* for WC_NO_RNG case */
- (void)key;
- return TEST_RES_CHECK(ret == 0);
- }
- #endif
- /*
- * Testing get_rand_digit
- */
- static int test_get_rand_digit(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
- int ret = 0;
- WC_RNG rng;
- mp_digit d;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = get_rand_digit(&rng, &d);
- }
- if (ret == 0) {
- ret = get_rand_digit(NULL, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = get_rand_digit(NULL, &d);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = get_rand_digit(&rng, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_FreeRng(&rng);
- }
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_get_rand_digit*/
- /*
- * Testing get_digit_count
- */
- static int test_get_digit_count(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
- int ret = 0;
- mp_int a;
- if (mp_init(&a) != MP_OKAY) {
- ret = -1;
- }
- if (ret == 0) {
- ret = get_digit_count(NULL);
- }
- if (ret == 0) {
- ret = get_digit_count(&a);
- }
- mp_clear(&a);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_get_digit_count*/
- /*
- * Testing mp_cond_copy
- */
- static int test_mp_cond_copy(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
- defined(WOLFSSL_PUBLIC_MP)
- int ret = 0;
- mp_int a;
- mp_int b;
- int copy = 0;
- if (mp_init(&a) != MP_OKAY) {
- ret = -1;
- }
- if (ret == 0) {
- if (mp_init(&b) != MP_OKAY) {
- ret = -1;
- }
- }
- if (ret == 0) {
- ret = mp_cond_copy(NULL, copy, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = mp_cond_copy(NULL, copy, &b);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = mp_cond_copy(&a, copy, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = mp_cond_copy(&a, copy, &b);
- }
- mp_clear(&a);
- mp_clear(&b);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_mp_cond_copy*/
- /*
- * Testing mp_rand
- */
- static int test_mp_rand(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
- int ret = 0;
- mp_int a;
- int digits = 1;
- WC_RNG rng;
- if (mp_init(&a) != MP_OKAY) {
- ret = -1;
- }
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- if (ret == 0) {
- ret = mp_rand(&a, digits, NULL);
- if (ret == MISSING_RNG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = mp_rand(NULL, digits, &rng);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = mp_rand(&a, 0, &rng);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = mp_rand(&a, digits, &rng);
- }
- mp_clear(&a);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_mp_rand*/
- /*
- * Testing get_digit
- */
- static int test_get_digit(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_PUBLIC_MP)
- int ret = 0;
- mp_int a;
- int n = 0;
- if (mp_init(&a) != MP_OKAY) {
- ret = -1;
- }
- if (ret == 0) {
- if (get_digit(NULL, n) != 0) { /* Should not hit this */
- ret = -1;
- }
- }
- if (ret == 0) {
- if (get_digit(NULL, n) == 0) { /* Should hit this */
- ret = 0;
- }
- }
- if (ret == 0) {
- if (get_digit(&a, n) != 0) { /* Should not hit this */
- ret = -1;
- }
- }
- if (ret == 0) {
- if (get_digit(&a, n) == 0) { /* Should hit this */
- ret = 0;
- }
- }
- mp_clear(&a);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_get_digit*/
- /*
- * Testing wc_export_int
- */
- static int test_wc_export_int(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
- defined(WOLFSSL_PUBLIC_MP)
- int ret = 0;
- mp_int mp;
- byte buf[32];
- word32 keySz = (word32)sizeof(buf);
- word32 len = (word32)sizeof(buf);
- if (mp_init(&mp) != MP_OKAY) {
- ret = -1;
- }
- if (ret == 0) {
- ret = mp_set(&mp, 1234);
- }
- if (ret == 0) {
- ret = wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- len = sizeof(buf)-1;
- ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
- if (ret == BUFFER_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- len = sizeof(buf);
- ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN);
- }
- if (ret == 0) {
- len = 4; /* test input too small */
- ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
- if (ret == BUFFER_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- len = sizeof(buf);
- ret = wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR);
- /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
- if (ret == 0 && len != 5) {
- ret = BAD_FUNC_ARG;
- }
- }
- mp_clear(&mp);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_wc_export_int*/
- static int test_wc_InitRngNonce(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
- (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
- int ret;
- WC_RNG rng;
- byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
- "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
- word32 nonceSz = sizeof(nonce);
- ret = wc_InitRngNonce(&rng, nonce, nonceSz);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/* End test_wc_InitRngNonce*/
- /*
- * Testing wc_InitRngNonce_ex
- */
- static int test_wc_InitRngNonce_ex(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
- (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
- int ret;
- WC_RNG rng;
- byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
- "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
- word32 nonceSz = sizeof(nonce);
- ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
- wc_FreeRng(&rng);
- res = TEST_RES_CHECK(ret == 0);
- #endif
- return res;
- }/*End test_wc_InitRngNonce_ex*/
- static int test_wolfSSL_X509_CRL(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
- X509_CRL *crl;
- char pem[][100] = {
- "./certs/crl/crl.pem",
- "./certs/crl/crl2.pem",
- "./certs/crl/caEccCrl.pem",
- "./certs/crl/eccCliCRL.pem",
- "./certs/crl/eccSrvCRL.pem",
- ""
- };
- #ifndef NO_BIO
- BIO *bio;
- #endif
- #ifdef HAVE_TEST_d2i_X509_CRL_fp
- char der[][100] = {
- "./certs/crl/crl.der",
- "./certs/crl/crl2.der",
- ""};
- #endif
- XFILE fp;
- int i;
- for (i = 0; pem[i][0] != '\0'; i++)
- {
- fp = XFOPEN(pem[i], "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL, NULL, NULL));
- AssertNotNull(crl);
- X509_CRL_free(crl);
- XFCLOSE(fp);
- fp = XFOPEN(pem[i], "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL, NULL));
- AssertNotNull(crl);
- X509_CRL_free(crl);
- XFCLOSE(fp);
- }
- #ifndef NO_BIO
- for (i = 0; pem[i][0] != '\0'; i++)
- {
- AssertNotNull(bio = BIO_new_file(pem[i], "rb"));
- AssertNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
- X509_CRL_free(crl);
- BIO_free(bio);
- }
- #endif
- #ifdef HAVE_TEST_d2i_X509_CRL_fp
- for (i = 0; der[i][0] != '\0'; i++) {
- fp = XFOPEN(der[i], "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
- AssertNotNull(crl);
- X509_CRL_free(crl);
- XFCLOSE(fp);
- fp = XFOPEN(der[i], "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
- AssertNotNull(crl);
- X509_CRL_free(crl);
- XFCLOSE(fp);
- }
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_load_crl_file(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA) && !defined(NO_BIO)
- int i;
- char pem[][100] = {
- "./certs/crl/crl.pem",
- "./certs/crl/crl2.pem",
- "./certs/crl/caEccCrl.pem",
- "./certs/crl/eccCliCRL.pem",
- "./certs/crl/eccSrvCRL.pem",
- ""
- };
- char der[][100] = {
- "./certs/crl/crl.der",
- "./certs/crl/crl2.der",
- ""
- };
- WOLFSSL_X509_STORE* store;
- WOLFSSL_X509_LOOKUP* lookup;
- AssertNotNull(store = wolfSSL_X509_STORE_new());
- AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
- AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
- X509_FILETYPE_PEM), 1);
- AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
- X509_FILETYPE_PEM), 1);
- if (store) {
- AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
- WOLFSSL_FILETYPE_PEM), 1);
- /* since store hasn't yet known the revoked cert*/
- AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
- WOLFSSL_FILETYPE_PEM), 1);
- }
- for (i = 0; pem[i][0] != '\0'; i++)
- {
- AssertIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM), 1);
- }
- if (store) {
- /* since store knows crl list */
- AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
- WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
- }
- /* once feeing store */
- X509_STORE_free(store);
- store = NULL;
- AssertNotNull(store = wolfSSL_X509_STORE_new());
- AssertNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
- AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
- X509_FILETYPE_PEM), 1);
- AssertIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
- X509_FILETYPE_PEM), 1);
- if (store) {
- AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
- WOLFSSL_FILETYPE_PEM), 1);
- /* since store hasn't yet known the revoked cert*/
- AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
- WOLFSSL_FILETYPE_PEM), 1);
- }
- for (i = 0; der[i][0] != '\0'; i++)
- {
- AssertIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1), 1);
- }
- if (store) {
- /* since store knows crl list */
- AssertIntEQ(wolfSSL_CertManagerVerify(store->cm, "certs/server-revoked-cert.pem",
- WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
- }
- /* test for incorrect parameter */
- AssertIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
- AssertIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
- AssertIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
- X509_STORE_free(store);
- store = NULL;
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_d2i_X509_REQ(void)
- {
- int res = TEST_SKIPPED;
- #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
- (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
- !defined(WOLFSSL_SP_MATH)
- /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der were
- * generated by libest
- * ./certs/csr.attr.der contains sample attributes
- * ./certs/csr.ext.der contains sample extensions */
- const char* csrFile = "./certs/csr.signed.der";
- const char* csrPopFile = "./certs/csr.attr.der";
- const char* csrExtFile = "./certs/csr.ext.der";
- /* ./certs/csr.dsa.pem is generated using
- * openssl req -newkey dsa:certs/dsaparams.pem \
- * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
- * -outform PEM
- * with the passphrase "wolfSSL"
- */
- #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
- const char* csrDsaFile = "./certs/csr.dsa.pem";
- XFILE f;
- #endif
- BIO* bio = NULL;
- X509* req = NULL;
- EVP_PKEY *pub_key = NULL;
- {
- AssertNotNull(bio = BIO_new_file(csrFile, "rb"));
- AssertNotNull(d2i_X509_REQ_bio(bio, &req));
- /*
- * Extract the public key from the CSR
- */
- AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
- /*
- * Verify the signature in the CSR
- */
- AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
- X509_free(req);
- BIO_free(bio);
- EVP_PKEY_free(pub_key);
- }
- {
- #ifdef OPENSSL_ALL
- X509_ATTRIBUTE* attr;
- ASN1_TYPE *at;
- #endif
- AssertNotNull(bio = BIO_new_file(csrPopFile, "rb"));
- AssertNotNull(d2i_X509_REQ_bio(bio, &req));
- /*
- * Extract the public key from the CSR
- */
- AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
- /*
- * Verify the signature in the CSR
- */
- AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
- #ifdef OPENSSL_ALL
- /*
- * Obtain the challenge password from the CSR
- */
- AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
- 1);
- AssertNotNull(attr = X509_REQ_get_attr(req, 1));
- AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
- AssertNotNull(at->value.asn1_string);
- AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "2xIE+qqp/rhyTXP+");
- AssertIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
- #endif
- X509_free(req);
- BIO_free(bio);
- EVP_PKEY_free(pub_key);
- }
- {
- #ifdef OPENSSL_ALL
- X509_ATTRIBUTE* attr;
- ASN1_TYPE *at;
- STACK_OF(X509_EXTENSION) *exts = NULL;
- #endif
- AssertNotNull(bio = BIO_new_file(csrExtFile, "rb"));
- /* This CSR contains an Extension Request attribute so
- * we test extension parsing in a CSR attribute here. */
- AssertNotNull(d2i_X509_REQ_bio(bio, &req));
- /*
- * Extract the public key from the CSR
- */
- AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
- /*
- * Verify the signature in the CSR
- */
- AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
- #ifdef OPENSSL_ALL
- AssertNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(req));
- AssertIntEQ(sk_X509_EXTENSION_num(exts), 2);
- sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
- /*
- * Obtain the challenge password from the CSR
- */
- AssertIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword, -1),
- 0);
- AssertNotNull(attr = X509_REQ_get_attr(req, 0));
- AssertNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
- AssertNotNull(at->value.asn1_string);
- AssertStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
- AssertIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
- AssertIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
- #endif
- X509_free(req);
- BIO_free(bio);
- EVP_PKEY_free(pub_key);
- }
- #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
- {
- AssertNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
- AssertNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
- /*
- * Extract the public key from the CSR
- */
- AssertNotNull(pub_key = X509_REQ_get_pubkey(req));
- /*
- * Verify the signature in the CSR
- */
- AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
- X509_free(req);
- BIO_free(bio);
- /* Run the same test, but with a file pointer instead of a BIO.
- * (PEM_read_X509_REQ)*/
- AssertTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
- AssertNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
- AssertIntEQ(X509_REQ_verify(req, pub_key), 1);
- X509_free(req);
- EVP_PKEY_free(pub_key);
- }
- res = TEST_RES_CHECK(1);
- #endif /* !NO_DSA && !HAVE_SELFTEST */
- #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
- return res;
- }
- static int test_wolfSSL_PEM_read_X509(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA)
- X509 *x509 = NULL;
- XFILE fp;
- fp = XFOPEN(svrCertFile, "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
- X509_free(x509);
- XFCLOSE(fp);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PEM_read(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
- const char* filename = "./certs/server-keyEnc.pem";
- XFILE fp;
- char* name = NULL;
- char* header = NULL;
- byte* data = NULL;
- long len;
- EVP_CIPHER_INFO cipher;
- WOLFSSL_BIO* bio;
- byte* fileData;
- size_t fileDataSz;
- byte* out;
- fp = XFOPEN(filename, "rb");
- AssertTrue((fp != XBADFILE));
- /* Fail cases. */
- AssertIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
- AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
- AssertIntGT(XSTRLEN(header), 0);
- AssertIntGT(len, 0);
- AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
- AssertIntGT((fileDataSz = XFTELL(fp)), 0);
- AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
- AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
- XFCLOSE(fp);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- /* Fail cases. */
- AssertIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
- AssertIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
- AssertIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
- AssertIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
- AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
- AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
- AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
- /* Fail cases. */
- AssertIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
- #ifndef NO_DES3
- AssertIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
- #endif
- /* Fail cases. */
- AssertIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
- (void*)"yassl123"), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
- (void*)"yassl123"), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_do_header(&cipher, data, &len, NULL,
- (void*)"yassl123"), WOLFSSL_FAILURE);
- #if !defined(NO_DES3) && !defined(NO_MD5)
- AssertIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
- (void*)"yassl123"), WOLFSSL_SUCCESS);
- #endif
- BIO_free(bio);
- XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- name = NULL;
- header = NULL;
- data = NULL;
- fp = XFOPEN(svrKeyFile, "rb");
- AssertTrue((fp != XBADFILE));
- AssertIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
- AssertIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
- AssertIntEQ(XSTRLEN(header), 0);
- AssertIntGT(len, 0);
- AssertIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
- AssertIntGT((fileDataSz = XFTELL(fp)), 0);
- AssertIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
- AssertNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- AssertIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
- XFCLOSE(fp);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
- AssertIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
- AssertIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
- BIO_free(bio);
- XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
- !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- const byte iv[12] = { 0 };
- const byte key[16] = { 0 };
- const byte cleartext[16] = { 0 };
- const byte aad[] = {
- 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
- 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
- 0x4f
- };
- byte out1Part[16];
- byte outTag1Part[16];
- byte out2Part[16];
- byte outTag2Part[16];
- byte decryptBuf[16];
- int len;
- int tlen;
- EVP_CIPHER_CTX* ctx = NULL;
- /* ENCRYPT */
- /* Send AAD and data in 1 part */
- AssertNotNull(ctx = EVP_CIPHER_CTX_new());
- tlen = 0;
- AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
- 1);
- AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
- AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
- AssertIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
- sizeof(cleartext)), 1);
- tlen += len;
- AssertIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
- tlen += len;
- AssertIntEQ(tlen, sizeof(cleartext));
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
- outTag1Part), 1);
- EVP_CIPHER_CTX_free(ctx);
- /* DECRYPT */
- /* Send AAD and data in 1 part */
- AssertNotNull(ctx = EVP_CIPHER_CTX_new());
- tlen = 0;
- AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
- 1);
- AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
- AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
- AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
- sizeof(cleartext)), 1);
- tlen += len;
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
- outTag1Part), 1);
- AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
- tlen += len;
- AssertIntEQ(tlen, sizeof(cleartext));
- EVP_CIPHER_CTX_free(ctx);
- AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
- /* ENCRYPT */
- /* Send AAD and data in 2 parts */
- AssertNotNull(ctx = EVP_CIPHER_CTX_new());
- tlen = 0;
- AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
- 1);
- AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
- AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
- AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
- 1);
- AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
- tlen += len;
- AssertIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
- sizeof(cleartext) - 1), 1);
- tlen += len;
- AssertIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
- tlen += len;
- AssertIntEQ(tlen, sizeof(cleartext));
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
- outTag2Part), 1);
- AssertIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
- AssertIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
- EVP_CIPHER_CTX_free(ctx);
- /* DECRYPT */
- /* Send AAD and data in 2 parts */
- AssertNotNull(ctx = EVP_CIPHER_CTX_new());
- tlen = 0;
- AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
- 1);
- AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
- AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
- AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
- 1);
- AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
- tlen += len;
- AssertIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
- sizeof(cleartext) - 1), 1);
- tlen += len;
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
- outTag1Part), 1);
- AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
- tlen += len;
- AssertIntEQ(tlen, sizeof(cleartext));
- AssertIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
- /* Test AAD re-use */
- EVP_CIPHER_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
- !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- /* Zero length plain text */
- byte key[] = {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
- }; /* align */
- byte iv[] = {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
- }; /* align */
- byte plaintxt[1];
- int ivSz = 12;
- int plaintxtSz = 0;
- unsigned char tag[16];
- unsigned char tag_kat[] =
- {0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
- 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b};
- byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
- byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
- int ciphertxtSz = 0;
- int decryptedtxtSz = 0;
- int len = 0;
- EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
- EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
- AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
- AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
- plaintxtSz));
- AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
- ciphertxtSz += len;
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
- AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
- AssertIntEQ(0, ciphertxtSz);
- AssertIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
- EVP_CIPHER_CTX_init(de);
- AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
- AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
- decryptedtxtSz = len;
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
- AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
- decryptedtxtSz += len;
- AssertIntEQ(0, decryptedtxtSz);
- EVP_CIPHER_CTX_free(en);
- EVP_CIPHER_CTX_free(de);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfssl_EVP_aes_gcm(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
- !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- /* A 256 bit key, AES_128 will use the first 128 bit*/
- byte *key = (byte*)"01234567890123456789012345678901";
- /* A 128 bit IV */
- byte *iv = (byte*)"0123456789012345";
- int ivSz = AES_BLOCK_SIZE;
- /* Message to be encrypted */
- byte *plaintxt = (byte*)"for things to change you have to change";
- /* Additional non-confidential data */
- byte *aad = (byte*)"Don't spend major time on minor things.";
- unsigned char tag[AES_BLOCK_SIZE] = {0};
- int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
- int aadSz = (int)XSTRLEN((char*)aad);
- byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
- byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
- int ciphertxtSz = 0;
- int decryptedtxtSz = 0;
- int len = 0;
- int i = 0;
- EVP_CIPHER_CTX en[2];
- EVP_CIPHER_CTX de[2];
- for (i = 0; i < 2; i++) {
- EVP_CIPHER_CTX_init(&en[i]);
- if (i == 0) {
- /* Default uses 96-bits IV length */
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, key, iv));
- #elif defined(WOLFSSL_AES_192)
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, key, iv));
- #elif defined(WOLFSSL_AES_256)
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, key, iv));
- #endif
- }
- else {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
- #elif defined(WOLFSSL_AES_192)
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
- #elif defined(WOLFSSL_AES_256)
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
- #endif
- /* non-default must to set the IV length first */
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
- }
- AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
- AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
- ciphertxtSz = len;
- AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
- ciphertxtSz += len;
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, AES_BLOCK_SIZE, tag));
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
- EVP_CIPHER_CTX_init(&de[i]);
- if (i == 0) {
- /* Default uses 96-bits IV length */
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, key, iv));
- #elif defined(WOLFSSL_AES_192)
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, key, iv));
- #elif defined(WOLFSSL_AES_256)
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, key, iv));
- #endif
- }
- else {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL, NULL, NULL));
- #elif defined(WOLFSSL_AES_192)
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL, NULL, NULL));
- #elif defined(WOLFSSL_AES_256)
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL, NULL, NULL));
- #endif
- /* non-default must to set the IV length first */
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
- }
- AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
- AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
- decryptedtxtSz = len;
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
- AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
- decryptedtxtSz += len;
- AssertIntEQ(ciphertxtSz, decryptedtxtSz);
- AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
- /* modify tag*/
- tag[AES_BLOCK_SIZE-1]+=0xBB;
- AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, AES_BLOCK_SIZE, tag));
- /* fail due to wrong tag */
- AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
- AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
- AssertIntEQ(0, len);
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
- }
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
- return res;
- }
- static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
- !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- /* Zero length plain text */
- byte key[] = {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
- }; /* align */
- byte iv[] = {
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
- }; /* align */
- byte plaintxt[1];
- int ivSz = 12;
- int plaintxtSz = 0;
- unsigned char tag[16];
- byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
- byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
- int ciphertxtSz = 0;
- int decryptedtxtSz = 0;
- int len = 0;
- EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
- EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
- AssertIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
- AssertIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
- plaintxtSz));
- AssertIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
- ciphertxtSz += len;
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
- AssertIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
- AssertIntEQ(0, ciphertxtSz);
- EVP_CIPHER_CTX_init(de);
- AssertIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
- AssertIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
- decryptedtxtSz = len;
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
- AssertIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
- decryptedtxtSz += len;
- AssertIntEQ(0, decryptedtxtSz);
- EVP_CIPHER_CTX_free(en);
- EVP_CIPHER_CTX_free(de);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfssl_EVP_aes_ccm(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
- !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- /* A 256 bit key, AES_128 will use the first 128 bit*/
- byte *key = (byte*)"01234567890123456789012345678901";
- /* A 128 bit IV */
- byte *iv = (byte*)"0123456789012";
- int ivSz = (int)XSTRLEN((char*)iv);
- /* Message to be encrypted */
- byte *plaintxt = (byte*)"for things to change you have to change";
- /* Additional non-confidential data */
- byte *aad = (byte*)"Don't spend major time on minor things.";
- unsigned char tag[AES_BLOCK_SIZE] = {0};
- int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
- int aadSz = (int)XSTRLEN((char*)aad);
- byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
- byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
- int ciphertxtSz = 0;
- int decryptedtxtSz = 0;
- int len = 0;
- int i = 0;
- EVP_CIPHER_CTX en[2];
- EVP_CIPHER_CTX de[2];
- for (i = 0; i < 2; i++) {
- EVP_CIPHER_CTX_init(&en[i]);
- if (i == 0) {
- /* Default uses 96-bits IV length */
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
- EVP_aes_128_ccm(), NULL, key, iv));
- #elif defined(WOLFSSL_AES_192)
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
- EVP_aes_192_ccm(), NULL, key, iv));
- #elif defined(WOLFSSL_AES_256)
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
- EVP_aes_256_ccm(), NULL, key, iv));
- #endif
- }
- else {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
- EVP_aes_128_ccm(), NULL, NULL, NULL));
- #elif defined(WOLFSSL_AES_192)
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
- EVP_aes_192_ccm(), NULL, NULL, NULL));
- #elif defined(WOLFSSL_AES_256)
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
- EVP_aes_256_ccm(), NULL, NULL, NULL));
- #endif
- /* non-default must to set the IV length first */
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
- EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
- AssertIntEQ(1, EVP_EncryptInit_ex(&en[i],
- NULL, NULL, key, iv));
- }
- AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
- AssertIntEQ(1, EVP_EncryptUpdate(&en[i],
- ciphertxt, &len, plaintxt, plaintxtSz));
- ciphertxtSz = len;
- AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
- ciphertxtSz += len;
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i],
- EVP_CTRL_CCM_GET_TAG, AES_BLOCK_SIZE, tag));
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
- EVP_CIPHER_CTX_init(&de[i]);
- if (i == 0) {
- /* Default uses 96-bits IV length */
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
- EVP_aes_128_ccm(), NULL, key, iv));
- #elif defined(WOLFSSL_AES_192)
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
- EVP_aes_192_ccm(), NULL, key, iv));
- #elif defined(WOLFSSL_AES_256)
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
- EVP_aes_256_ccm(), NULL, key, iv));
- #endif
- }
- else {
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
- EVP_aes_128_ccm(), NULL, NULL, NULL));
- #elif defined(WOLFSSL_AES_192)
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
- EVP_aes_192_ccm(), NULL, NULL, NULL));
- #elif defined(WOLFSSL_AES_256)
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i],
- EVP_aes_256_ccm(), NULL, NULL, NULL));
- #endif
- /* non-default must to set the IV length first */
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
- EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
- AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
- }
- AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
- AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
- decryptedtxt, &len, ciphertxt, ciphertxtSz));
- decryptedtxtSz = len;
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
- EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
- AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i],
- decryptedtxt, &len));
- decryptedtxtSz += len;
- AssertIntEQ(ciphertxtSz, decryptedtxtSz);
- AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
- /* modify tag*/
- tag[AES_BLOCK_SIZE-1]+=0xBB;
- AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
- AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i],
- EVP_CTRL_CCM_SET_TAG, AES_BLOCK_SIZE, tag));
- /* fail due to wrong tag */
- AssertIntEQ(1, EVP_DecryptUpdate(&de[i],
- decryptedtxt, &len, ciphertxt, ciphertxtSz));
- AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
- AssertIntEQ(0, len);
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
- }
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
- return res;
- }
- static int test_wolfssl_EVP_chacha20_poly1305(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
- byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
- byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
- byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
- byte cipherText[sizeof(plainText)];
- byte decryptedText[sizeof(plainText)];
- byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
- EVP_CIPHER_CTX* ctx;
- int outSz;
- /* Encrypt. */
- AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
- AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
- NULL), WOLFSSL_SUCCESS);
- /* Invalid IV length. */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
- CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
- /* Valid IV length. */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
- CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
- /* Invalid tag length. */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
- CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
- /* Valid tag length. */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
- CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
- WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, sizeof(aad));
- AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
- sizeof(plainText)), WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, sizeof(plainText));
- AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, 0);
- /* Invalid tag length. */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
- CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
- /* Valid tag length. */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
- CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
- EVP_CIPHER_CTX_free(ctx);
- /* Decrypt. */
- AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
- AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
- NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
- CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
- CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
- WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, sizeof(aad));
- AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
- sizeof(cipherText)), WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, sizeof(cipherText));
- AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, 0);
- EVP_CIPHER_CTX_free(ctx);
- /* Test partial Inits. CipherInit() allow setting of key and iv
- * in separate calls. */
- AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
- AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
- key, NULL, 1), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
- aad, sizeof(aad)), WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, sizeof(aad));
- AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
- sizeof(cipherText)), WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, sizeof(cipherText));
- AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, 0);
- EVP_CIPHER_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfssl_EVP_chacha20(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
- byte key[CHACHA_MAX_KEY_SZ];
- byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
- byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
- byte cipherText[sizeof(plainText)];
- byte decryptedText[sizeof(plainText)];
- EVP_CIPHER_CTX* ctx;
- int outSz;
- /* Encrypt. */
- AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
- AssertIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
- NULL), WOLFSSL_SUCCESS);
- /* Any tag length must fail - not an AEAD cipher. */
- AssertIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
- 16, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
- sizeof(plainText)), WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, sizeof(plainText));
- AssertIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, 0);
- EVP_CIPHER_CTX_free(ctx);
- /* Decrypt. */
- AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
- AssertIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
- NULL), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
- sizeof(cipherText)), WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, sizeof(cipherText));
- AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, 0);
- EVP_CIPHER_CTX_free(ctx);
- /* Test partial Inits. CipherInit() allow setting of key and iv
- * in separate calls. */
- AssertNotNull((ctx = EVP_CIPHER_CTX_new()));
- AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
- key, NULL, 1), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
- sizeof(cipherText)), WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, sizeof(cipherText));
- AssertIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(outSz, 0);
- EVP_CIPHER_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_PKEY_hkdf(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
- EVP_PKEY_CTX* ctx;
- byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
- 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
- byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
- 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
- byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
- byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
- byte outKey[34];
- size_t outKeySz = sizeof(outKey);
- /* These expected outputs were gathered by running the same test below using
- * OpenSSL. */
- const byte extractAndExpand[] = {
- 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
- 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
- 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
- };
- const byte extractOnly[] = {
- 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
- 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
- 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
- };
- const byte expandOnly[] = {
- 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
- 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
- 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
- };
- const byte extractAndExpandAddInfo[] = {
- 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
- 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
- 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
- };
- AssertNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
- AssertIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
- /* NULL ctx. */
- AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
- /* NULL md. */
- AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
- /* NULL ctx. */
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
- WOLFSSL_FAILURE);
- /* NULL salt is ok. */
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
- WOLFSSL_SUCCESS);
- /* Salt length <= 0. */
- /* Length 0 salt is ok. */
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
- WOLFSSL_SUCCESS);
- /* NULL ctx. */
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
- WOLFSSL_FAILURE);
- /* NULL key. */
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
- WOLFSSL_FAILURE);
- /* Key length <= 0 */
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
- AssertIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
- WOLFSSL_SUCCESS);
- /* NULL ctx. */
- AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
- WOLFSSL_FAILURE);
- /* NULL info is ok. */
- AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
- WOLFSSL_SUCCESS);
- /* Info length <= 0 */
- /* Length 0 info is ok. */
- AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
- AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
- WOLFSSL_SUCCESS);
- /* NULL ctx. */
- AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
- WOLFSSL_FAILURE);
- /* Extract and expand (default). */
- AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
- AssertIntEQ(outKeySz, sizeof(extractAndExpand));
- AssertIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
- /* Extract only. */
- AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
- AssertIntEQ(outKeySz, sizeof(extractOnly));
- AssertIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
- outKeySz = sizeof(outKey);
- /* Expand only. */
- AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
- AssertIntEQ(outKeySz, sizeof(expandOnly));
- AssertIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
- outKeySz = sizeof(outKey);
- /* Extract and expand with appended additional info. */
- AssertIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
- WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
- EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
- AssertIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
- AssertIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
- EVP_PKEY_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && HAVE_HKDF */
- return res;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- BIO* bio;
- X509_INFO* info;
- STACK_OF(X509_INFO)* sk;
- char* subject;
- char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
- char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
- AssertNotNull(bio = BIO_new(BIO_s_file()));
- AssertIntGT(BIO_read_filename(bio, svrCertFile), 0);
- AssertNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
- AssertIntEQ(sk_X509_INFO_num(sk), 2);
- /* using dereference to maintain testing for Apache port*/
- AssertNotNull(info = sk_X509_INFO_pop(sk));
- AssertNotNull(subject =
- X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
- AssertIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
- XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
- X509_INFO_free(info);
- AssertNotNull(info = sk_X509_INFO_pop(sk));
- AssertNotNull(subject =
- X509_NAME_oneline(X509_get_subject_name(info->x509), 0, 0));
- AssertIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
- XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
- X509_INFO_free(info);
- AssertNull(info = sk_X509_INFO_pop(sk));
- sk_X509_INFO_pop_free(sk, X509_INFO_free);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #endif /* !NO_BIO */
- static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- X509 *x509;
- X509_NAME* name;
- int idx = 0;
- X509_NAME_ENTRY *ne;
- ASN1_OBJECT *object = NULL;
- x509 = wolfSSL_X509_load_certificate_file(cliCertFile, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- name = X509_get_subject_name(x509);
- idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1);
- AssertIntGE(idx, 0);
- ne = X509_NAME_get_entry(name, idx);
- AssertNotNull(ne);
- AssertNotNull(object = X509_NAME_ENTRY_get_object(ne));
- X509_free(x509);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_INTEGER_get_set(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
- ASN1_INTEGER *a;
- long val;
- int ret;
- a = ASN1_INTEGER_new();
- val = 0;
- ret = ASN1_INTEGER_set(NULL, val);
- AssertIntEQ(ret, 0);
- ASN1_INTEGER_free(a);
- /* 0 */
- a = ASN1_INTEGER_new();
- val = 0;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(ASN1_INTEGER_get(a), val);
- ASN1_INTEGER_free(a);
- /* 40 */
- a = ASN1_INTEGER_new();
- val = 40;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(ASN1_INTEGER_get(a), val);
- ASN1_INTEGER_free(a);
- /* -40 */
- a = ASN1_INTEGER_new();
- val = -40;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(ASN1_INTEGER_get(a), val);
- ASN1_INTEGER_free(a);
- /* 128 */
- a = ASN1_INTEGER_new();
- val = 128;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(ASN1_INTEGER_get(a), val);
- ASN1_INTEGER_free(a);
- /* -128 */
- a = ASN1_INTEGER_new();
- val = -128;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(ASN1_INTEGER_get(a), val);
- ASN1_INTEGER_free(a);
- /* 200 */
- a = ASN1_INTEGER_new();
- val = 200;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(ASN1_INTEGER_get(a), val);
- ASN1_INTEGER_free(a);
- /* int max (2147483647) */
- a = ASN1_INTEGER_new();
- val = 2147483647;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(ASN1_INTEGER_get(a), val);
- ASN1_INTEGER_free(a);
- /* int min (-2147483648) */
- a = ASN1_INTEGER_new();
- val = -2147483647 - 1;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(ASN1_INTEGER_get(a), val);
- ASN1_INTEGER_free(a);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_EXTRA)
- typedef struct ASN1IntTestVector {
- const byte* der;
- const size_t derSz;
- const long value;
- } ASN1IntTestVector;
- #endif
- static int test_wolfSSL_d2i_ASN1_INTEGER(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- size_t i;
- WOLFSSL_ASN1_INTEGER* a = NULL;
- WOLFSSL_ASN1_INTEGER* b = NULL;
- WOLFSSL_ASN1_INTEGER* c = NULL;
- const byte* p = NULL;
- byte* reEncoded = NULL;
- int reEncodedSz;
- static const byte zeroDer[] = {
- 0x02, 0x01, 0x00
- };
- static const byte oneDer[] = {
- 0x02, 0x01, 0x01
- };
- static const byte negativeDer[] = {
- 0x02, 0x03, 0xC1, 0x16, 0x0D
- };
- static const byte positiveDer[] = {
- 0x02, 0x03, 0x01, 0x00, 0x01
- };
- static const byte primeDer[] = {
- 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
- 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
- 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
- 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
- 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
- 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
- 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
- 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
- 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
- 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
- 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
- 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
- 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
- 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
- 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
- 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
- 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
- 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
- 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
- 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
- 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
- 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
- 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
- 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
- };
- static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
- static const ASN1IntTestVector testVectors[] = {
- {zeroDer, sizeof(zeroDer), 0},
- {oneDer, sizeof(oneDer), 1},
- {negativeDer, sizeof(negativeDer), -4123123},
- {positiveDer, sizeof(positiveDer), 65537},
- {primeDer, sizeof(primeDer), 0}
- };
- static const size_t NUM_TEST_VECTORS = sizeof(testVectors)/sizeof(testVectors[0]);
- /* Check d2i error conditions */
- /* NULL pointer to input. */
- AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
- AssertNull(b);
- /* NULL input. */
- AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
- AssertNull(b);
- /* 0 length. */
- p = testVectors[0].der;
- AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
- AssertNull(b);
- /* Negative length. */
- p = testVectors[0].der;
- AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
- AssertNull(b);
- /* Garbage DER input. */
- p = garbageDer;
- AssertNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
- AssertNull(b);
- {
- /* Check i2d error conditions */
- /* NULL input. */
- byte* p2 = NULL;
- AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
- /* 0 length input data buffer (a->length == 0). */
- AssertNotNull((a = wolfSSL_ASN1_INTEGER_new()));
- AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
- a->data = NULL;
- /* NULL input data buffer. */
- AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
- /* Reset a->data. */
- a->data = a->intData;
- /* Set a to valid value. */
- AssertIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
- /* NULL output buffer. */
- AssertIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
- wolfSSL_ASN1_INTEGER_free(a);
- }
- for (i = 0; i < NUM_TEST_VECTORS; ++i) {
- p = testVectors[i].der;
- a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, testVectors[i].derSz);
- AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
- if (testVectors[i].derSz <= sizeof(long)) {
- c = wolfSSL_ASN1_INTEGER_new();
- wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value);
- AssertIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
- wolfSSL_ASN1_INTEGER_free(c);
- }
- /* Convert to DER without a pre-allocated output buffer. */
- AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
- AssertIntEQ(reEncodedSz, testVectors[i].derSz);
- AssertIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
- /* Convert to DER with a pre-allocated output buffer. In this case, the
- * output buffer pointer should be incremented just past the end of the
- * encoded data. */
- p = reEncoded;
- AssertIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
- AssertIntEQ(reEncodedSz, testVectors[i].derSz);
- AssertPtrEq(p, reEncoded - reEncodedSz);
- AssertIntEQ(XMEMCMP(p, testVectors[i].der, reEncodedSz), 0);
- XFREE(reEncoded - reEncodedSz, NULL, DYNAMIC_TYPE_ASN1);
- reEncoded = NULL;
- wolfSSL_ASN1_INTEGER_free(a);
- }
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_X509_STORE_get1_certs(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- X509_STORE_CTX *storeCtx;
- X509_STORE *store;
- X509 *caX509;
- X509 *svrX509;
- X509_NAME *subject;
- WOLF_STACK_OF(WOLFSSL_X509) *certs;
- AssertNotNull(caX509 =
- X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM));
- AssertNotNull((svrX509 =
- wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
- AssertNotNull(storeCtx = X509_STORE_CTX_new());
- AssertNotNull(store = X509_STORE_new());
- AssertNotNull(subject = X509_get_subject_name(caX509));
- /* Errors */
- AssertNull(X509_STORE_get1_certs(storeCtx, subject));
- AssertNull(X509_STORE_get1_certs(NULL, subject));
- AssertNull(X509_STORE_get1_certs(storeCtx, NULL));
- AssertIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
- AssertIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL), SSL_SUCCESS);
- /* Should find the cert */
- AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
- AssertIntEQ(1, wolfSSL_sk_X509_num(certs));
- sk_X509_pop_free(certs, NULL);
- /* Should not find the cert */
- AssertNotNull(subject = X509_get_subject_name(svrX509));
- AssertNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
- AssertIntEQ(0, wolfSSL_sk_X509_num(certs));
- sk_X509_pop_free(certs, NULL);
- X509_STORE_free(store);
- X509_STORE_CTX_free(storeCtx);
- X509_free(svrX509);
- X509_free(caX509);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
- return res;
- }
- /* Testing code used in dpp.c in hostap */
- #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
- typedef struct {
- /* AlgorithmIdentifier ecPublicKey with optional parameters present
- * as an OID identifying the curve */
- X509_ALGOR *alg;
- /* Compressed format public key per ANSI X9.63 */
- ASN1_BIT_STRING *pub_key;
- } DPP_BOOTSTRAPPING_KEY;
- ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
- ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
- ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
- } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
- IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
- #endif
- static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
- {
- int res = TEST_SKIPPED;
- /* Testing code used in dpp.c in hostap */
- #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- EC_KEY *eckey;
- EVP_PKEY *key;
- size_t len;
- unsigned char *der = NULL;
- DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
- const unsigned char *in = ecc_clikey_der_256;
- const EC_GROUP *group;
- const EC_POINT *point;
- int nid;
- AssertNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
- AssertNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
- (long)sizeof_ecc_clikey_der_256));
- AssertNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
- AssertNotNull(group = EC_KEY_get0_group(eckey));
- AssertNotNull(point = EC_KEY_get0_public_key(eckey));
- nid = EC_GROUP_get_curve_name(group);
- AssertIntEQ(X509_ALGOR_set0(bootstrap->alg, OBJ_nid2obj(EVP_PKEY_EC),
- V_ASN1_OBJECT, OBJ_nid2obj(nid)), 1);
- #ifdef HAVE_COMP_KEY
- AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
- NULL, 0, NULL)), 0);
- #else
- AssertIntGT((len = EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
- NULL, 0, NULL)), 0);
- #endif
- AssertNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
- #ifdef HAVE_COMP_KEY
- AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
- der, len, NULL), len);
- #else
- AssertIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
- der, len, NULL), len);
- #endif
- bootstrap->pub_key->data = der;
- bootstrap->pub_key->length = (int)len;
- /* Not actually used */
- bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
- bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
- der = NULL;
- AssertIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
- XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
- EVP_PKEY_free(key);
- EC_KEY_free(eckey);
- DPP_BOOTSTRAPPING_KEY_free(bootstrap);
- res = TEST_RES_CHECK(1);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
- return res;
- }
- static int test_wolfSSL_i2c_ASN1_INTEGER(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
- ASN1_INTEGER *a;
- unsigned char *pp,*tpp;
- int ret;
- a = wolfSSL_ASN1_INTEGER_new();
- /* 40 */
- a->intData[0] = ASN_INTEGER;
- a->intData[1] = 1;
- a->intData[2] = 40;
- ret = i2c_ASN1_INTEGER(a, NULL);
- AssertIntEQ(ret, 1);
- AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- tpp = pp;
- XMEMSET(pp, 0, ret + 1);
- i2c_ASN1_INTEGER(a, &pp);
- pp--;
- AssertIntEQ(*pp, 40);
- XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- /* 128 */
- a->intData[0] = ASN_INTEGER;
- a->intData[1] = 1;
- a->intData[2] = 128;
- ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
- AssertIntEQ(ret, 2);
- AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- tpp = pp;
- XMEMSET(pp, 0, ret + 1);
- wolfSSL_i2c_ASN1_INTEGER(a, &pp);
- pp--;
- AssertIntEQ(*(pp--), 128);
- AssertIntEQ(*pp, 0);
- XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- /* -40 */
- a->intData[0] = ASN_INTEGER;
- a->intData[1] = 1;
- a->intData[2] = 40;
- a->negative = 1;
- ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
- AssertIntEQ(ret, 1);
- AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- tpp = pp;
- XMEMSET(pp, 0, ret + 1);
- wolfSSL_i2c_ASN1_INTEGER(a, &pp);
- pp--;
- AssertIntEQ(*pp, 216);
- XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- /* -128 */
- a->intData[0] = ASN_INTEGER;
- a->intData[1] = 1;
- a->intData[2] = 128;
- a->negative = 1;
- ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
- AssertIntEQ(ret, 1);
- AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- tpp = pp;
- XMEMSET(pp, 0, ret + 1);
- wolfSSL_i2c_ASN1_INTEGER(a, &pp);
- pp--;
- AssertIntEQ(*pp, 128);
- XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- /* -200 */
- a->intData[0] = ASN_INTEGER;
- a->intData[1] = 1;
- a->intData[2] = 200;
- a->negative = 1;
- ret = wolfSSL_i2c_ASN1_INTEGER(a, NULL);
- AssertIntEQ(ret, 2);
- AssertNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
- DYNAMIC_TYPE_TMP_BUFFER));
- tpp = pp;
- XMEMSET(pp, 0, ret + 1);
- wolfSSL_i2c_ASN1_INTEGER(a, &pp);
- pp--;
- AssertIntEQ(*(pp--), 56);
- AssertIntEQ(*pp, 255);
- XFREE(tpp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- wolfSSL_ASN1_INTEGER_free(a);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_ASN */
- return res;
- }
- #ifndef NO_INLINE
- #define WOLFSSL_MISC_INCLUDED
- #include <wolfcrypt/src/misc.c>
- #else
- #include <wolfssl/wolfcrypt/misc.h>
- #endif
- static int test_ForceZero(void)
- {
- unsigned char data[32];
- unsigned int i, j, len;
- /* Test case with 0 length */
- ForceZero(data, 0);
- /* Test ForceZero */
- for (i = 0; i < sizeof(data); i++) {
- for (len = 1; len < sizeof(data) - i; len++) {
- for (j = 0; j < sizeof(data); j++)
- data[j] = j + 1;
- ForceZero(data + i, len);
- for (j = 0; j < sizeof(data); j++) {
- if (j < i || j >= i + len) {
- if (data[j] == 0x00)
- return -10200;
- }
- else if (data[j] != 0x00)
- return -10201;
- }
- }
- }
- return TEST_RES_CHECK(1);
- }
- #ifndef NO_BIO
- static int test_wolfSSL_X509_print(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(XSNPRINTF)
- X509 *x509;
- BIO *bio;
- #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
- const X509_ALGOR *cert_sig_alg;
- #endif
- x509 = X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- /* print to memory */
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
- #if defined(WC_DISABLE_RADIX_ZERO_PAD)
- /* Will print IP address subject alt name. */
- AssertIntEQ(BIO_get_mem_data(bio, NULL), 3349);
- #else
- /* Will print IP address subject alt name. */
- AssertIntEQ(BIO_get_mem_data(bio, NULL), 3350);
- #endif
- #else
- AssertIntEQ(BIO_get_mem_data(bio, NULL), 3328);
- #endif
- BIO_free(bio);
- AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
- #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
- /* Print signature */
- AssertNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
- AssertIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
- #endif
- /* print to stderr */
- #if !defined(NO_WOLFSSL_DIR)
- AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
- #endif
- /* print again */
- AssertIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
- X509_free(x509);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_X509_CRL_print(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
- && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
- X509_CRL* crl;
- BIO *bio;
- XFILE fp;
- fp = XFOPEN("./certs/crl/crl.pem", "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
- NULL, NULL));
- XFCLOSE(fp);
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
- X509_CRL_free(crl);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_BIO_get_len(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
- BIO *bio = NULL;
- const char txt[] = "Some example text to push to the BIO.";
- AssertIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
- AssertIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
- BIO_free(bio);
- AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
- AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
- BIO_free(bio);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ASN1_STRING_print(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS)
- ASN1_STRING* asnStr = NULL;
- const char HELLO_DATA[]= \
- {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
- #define MAX_UNPRINTABLE_CHAR 32
- #define MAX_BUF 255
- unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
- unsigned char expected[sizeof(unprintableData)+1];
- unsigned char rbuf[MAX_BUF];
- BIO *bio;
- int p_len, i;
- /* setup */
- for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
- unprintableData[i] = HELLO_DATA[i];
- expected[i] = HELLO_DATA[i];
- }
- for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
- unprintableData[sizeof(HELLO_DATA)+i] = i;
- if (i == (int)'\n' || i == (int)'\r')
- expected[sizeof(HELLO_DATA)+i] = i;
- else
- expected[sizeof(HELLO_DATA)+i] = '.';
- }
- unprintableData[sizeof(unprintableData)-1] = '\0';
- expected[sizeof(expected)-1] = '\0';
- XMEMSET(rbuf, 0, MAX_BUF);
- bio = BIO_new(BIO_s_mem());
- BIO_set_write_buf_size(bio, MAX_BUF);
- asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING);
- ASN1_STRING_set(asnStr,(const void*)unprintableData,
- (int)sizeof(unprintableData));
- /* test */
- p_len = wolfSSL_ASN1_STRING_print(bio, asnStr);
- AssertIntEQ(p_len, 46);
- BIO_read(bio, (void*)rbuf, 46);
- AssertStrEQ((char*)rbuf, (const char*)expected);
- BIO_free(bio);
- ASN1_STRING_free(asnStr);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
- return res;
- }
- #endif /* !NO_BIO */
- static int test_wolfSSL_ASN1_get_object(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
- const unsigned char* derBuf = cliecc_cert_der_256;
- int len = sizeof_cliecc_cert_der_256;
- long asnLen = 0;
- int tag = 0, cls = 0;
- ASN1_OBJECT *a;
- /* Read a couple TLV triplets and make sure they match the expected values */
- AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
- AssertIntEQ(asnLen, 862);
- AssertIntEQ(tag, 0x10);
- AssertIntEQ(cls, 0);
- AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
- len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
- AssertIntEQ(asnLen, 772);
- AssertIntEQ(tag, 0x10);
- AssertIntEQ(cls, 0);
- AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
- len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
- AssertIntEQ(asnLen, 3);
- AssertIntEQ(tag, 0);
- AssertIntEQ(cls, 0x80);
- AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
- len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
- AssertIntEQ(asnLen, 1);
- AssertIntEQ(tag, 0x2);
- AssertIntEQ(cls, 0);
- derBuf += asnLen;
- AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
- len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
- AssertIntEQ(asnLen, 20);
- AssertIntEQ(tag, 0x2);
- AssertIntEQ(cls, 0);
- derBuf += asnLen;
- AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
- len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
- AssertIntEQ(asnLen, 10);
- AssertIntEQ(tag, 0x10);
- AssertIntEQ(cls, 0);
- /* Read an ASN OBJECT */
- AssertNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
- ASN1_OBJECT_free(a);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
- return res;
- }
- static int test_wolfSSL_RSA(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA) && \
- defined(WOLFSSL_KEY_GEN)
- RSA* rsa;
- const BIGNUM *n;
- const BIGNUM *e;
- const BIGNUM *d;
- const BIGNUM *p;
- const BIGNUM *q;
- const BIGNUM *dmp1;
- const BIGNUM *dmq1;
- const BIGNUM *iqmp;
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(RSA_size(NULL), 0);
- AssertIntEQ(RSA_size(rsa), 0);
- AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
- AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
- AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
- #ifdef WOLFSSL_RSA_KEY_CHECK
- AssertIntEQ(RSA_check_key(rsa), 0);
- #endif
- RSA_free(rsa);
- AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
- AssertIntEQ(RSA_size(rsa), 256);
- #ifdef WOLFSSL_RSA_KEY_CHECK
- AssertIntEQ(RSA_check_key(NULL), 0);
- AssertIntEQ(RSA_check_key(rsa), 1);
- #endif
- /* sanity check */
- AssertIntEQ(RSA_bits(NULL), 0);
- /* key */
- AssertIntEQ(RSA_bits(rsa), 2048);
- RSA_get0_key(rsa, &n, &e, &d);
- AssertPtrEq(rsa->n, n);
- AssertPtrEq(rsa->e, e);
- AssertPtrEq(rsa->d, d);
- AssertNotNull(n = BN_new());
- AssertNotNull(e = BN_new());
- AssertNotNull(d = BN_new());
- AssertIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
- AssertPtrEq(rsa->n, n);
- AssertPtrEq(rsa->e, e);
- AssertPtrEq(rsa->d, d);
- AssertIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
- AssertIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
- /* crt_params */
- RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
- AssertPtrEq(rsa->dmp1, dmp1);
- AssertPtrEq(rsa->dmq1, dmq1);
- AssertPtrEq(rsa->iqmp, iqmp);
- AssertNotNull(dmp1 = BN_new());
- AssertNotNull(dmq1 = BN_new());
- AssertNotNull(iqmp = BN_new());
- AssertIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
- (BIGNUM*)iqmp), 1);
- AssertPtrEq(rsa->dmp1, dmp1);
- AssertPtrEq(rsa->dmq1, dmq1);
- AssertPtrEq(rsa->iqmp, iqmp);
- AssertIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
- AssertIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
- (BIGNUM*)iqmp), 0);
- RSA_get0_crt_params(NULL, NULL, NULL, NULL);
- RSA_get0_crt_params(rsa, NULL, NULL, NULL);
- RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
- AssertNull(dmp1);
- AssertNull(dmq1);
- AssertNull(iqmp);
- /* factors */
- RSA_get0_factors(rsa, NULL, NULL);
- RSA_get0_factors(rsa, &p, &q);
- AssertPtrEq(rsa->p, p);
- AssertPtrEq(rsa->q, q);
- AssertNotNull(p = BN_new());
- AssertNotNull(q = BN_new());
- AssertIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
- AssertPtrEq(rsa->p, p);
- AssertPtrEq(rsa->q, q);
- AssertIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
- AssertIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
- RSA_get0_factors(NULL, NULL, NULL);
- RSA_get0_factors(NULL, &p, &q);
- AssertNull(p);
- AssertNull(q);
- AssertIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
- AssertIntEQ(RSA_bits(rsa), 21);
- RSA_free(rsa);
- #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
- AssertNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
- AssertIntEQ(RSA_size(rsa), 384);
- AssertIntEQ(RSA_bits(rsa), 3072);
- RSA_free(rsa);
- #endif
- /* remove for now with odd key size until adjusting rsa key size check with
- wc_MakeRsaKey()
- AssertNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
- RSA_free(rsa);
- */
- AssertNull(RSA_generate_key(-1, 3, NULL, NULL));
- AssertNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
- AssertNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
- AssertNull(RSA_generate_key(2048, 0, NULL, NULL));
- #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
- {
- byte buff[FOURK_BUF];
- byte der[FOURK_BUF];
- const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
- XFILE f;
- int bytes;
- /* test loading encrypted RSA private pem w/o password */
- f = XFOPEN(PrivKeyPemFile, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buff, 1, sizeof(buff), f);
- XFCLOSE(f);
- XMEMSET(der, 0, sizeof(der));
- /* test that error value is returned with no password */
- AssertIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""), 0);
- }
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_DER(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(NO_RSA) && !defined(HAVE_USER_RSA) && defined(OPENSSL_EXTRA)
- RSA *rsa;
- int i;
- const unsigned char *buff = NULL;
- unsigned char *newBuff = NULL;
- struct tbl_s
- {
- const unsigned char *der;
- int sz;
- } tbl[] = {
- #ifdef USE_CERT_BUFFERS_1024
- {client_key_der_1024, sizeof_client_key_der_1024},
- {server_key_der_1024, sizeof_server_key_der_1024},
- #endif
- #ifdef USE_CERT_BUFFERS_2048
- {client_key_der_2048, sizeof_client_key_der_2048},
- {server_key_der_2048, sizeof_server_key_der_2048},
- #endif
- {NULL, 0}
- };
- /* Public Key DER */
- struct tbl_s pub[] = {
- #ifdef USE_CERT_BUFFERS_1024
- {client_keypub_der_1024, sizeof_client_keypub_der_1024},
- #endif
- #ifdef USE_CERT_BUFFERS_2048
- {client_keypub_der_2048, sizeof_client_keypub_der_2048},
- #endif
- {NULL, 0}
- };
- AssertNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
- buff = pub[0].der;
- AssertNull(d2i_RSAPublicKey(&rsa, &buff, 1));
- AssertNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
- buff = tbl[0].der;
- AssertNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
- AssertIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
- rsa = RSA_new();
- AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
- RSA_free(rsa);
- for (i = 0; tbl[i].der != NULL; i++)
- {
- /* Passing in pointer results in pointer moving. */
- buff = tbl[i].der;
- AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
- AssertNotNull(rsa);
- RSA_free(rsa);
- }
- for (i = 0; tbl[i].der != NULL; i++)
- {
- /* Passing in pointer results in pointer moving. */
- buff = tbl[i].der;
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
- AssertNotNull(rsa);
- RSA_free(rsa);
- }
- for (i = 0; pub[i].der != NULL; i++)
- {
- buff = pub[i].der;
- AssertNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
- AssertNotNull(rsa);
- AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
- newBuff = NULL;
- AssertIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
- AssertNotNull(newBuff);
- AssertIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
- XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- RSA_free(rsa);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_print(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_FAST_RSA) && !defined(NO_BIO) && defined(XFPRINTF)
- BIO *bio;
- WOLFSSL_RSA* rsa = NULL;
- AssertNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(RSA_print(NULL, rsa, 0), -1);
- AssertIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
- AssertIntEQ(RSA_print(bio, NULL, 0), -1);
- AssertIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
- /* Some very large number of indent spaces. */
- AssertIntEQ(RSA_print(bio, rsa, 128), -1);
- /* RSA is empty. */
- AssertIntEQ(RSA_print(bio, rsa, 0), 0);
- AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
- RSA_free(rsa);
- AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
- AssertIntEQ(RSA_print(bio, rsa, 0), 1);
- AssertIntEQ(RSA_print(bio, rsa, 4), 1);
- AssertIntEQ(RSA_print(bio, rsa, -1), 1);
- AssertIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
- AssertIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
- AssertIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
- BIO_free(bio);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #ifndef NO_RSA
- static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- RSA *rsa;
- const unsigned char *derBuf = client_key_der_2048;
- unsigned char em[256] = {0}; /* len = 2048/8 */
- /* Random data simulating a hash */
- const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
- 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
- 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
- 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
- };
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
- AssertIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
- RSA_PSS_SALTLEN_DIGEST), 0);
- AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
- RSA_PSS_SALTLEN_DIGEST), 0);
- AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
- RSA_PSS_SALTLEN_DIGEST), 0);
- AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
- RSA_PSS_SALTLEN_DIGEST), 0);
- AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
- AssertIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
- RSA_PSS_SALTLEN_MAX_SIGN), 0);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
- RSA_PSS_SALTLEN_MAX_SIGN), 0);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
- RSA_PSS_SALTLEN_MAX_SIGN), 0);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
- RSA_PSS_SALTLEN_MAX_SIGN), 0);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
- RSA_PSS_SALTLEN_MAX_SIGN), 0);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
- AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
- RSA_PSS_SALTLEN_DIGEST), 1);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
- RSA_PSS_SALTLEN_DIGEST), 1);
- AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
- RSA_PSS_SALTLEN_MAX_SIGN), 1);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
- RSA_PSS_SALTLEN_MAX_SIGN), 1);
- AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
- RSA_PSS_SALTLEN_MAX), 1);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
- RSA_PSS_SALTLEN_MAX), 1);
- AssertIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
- return res;
- }
- #endif
- static int test_wolfSSL_RSA_sign_sha3(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
- #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
- RSA *rsa;
- const unsigned char *derBuf = client_key_der_2048;
- unsigned char sigRet[256] = {0};
- unsigned int sigLen = sizeof(sigRet);
- /* Random data simulating a hash */
- const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
- 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
- 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
- 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
- };
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
- AssertIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet,
- &sigLen, rsa), 1);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
- #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
- return res;
- }
- static int test_wolfSSL_RSA_get0_key(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_USER_RSA)
- RSA *rsa = NULL;
- const BIGNUM* n = NULL;
- const BIGNUM* e = NULL;
- const BIGNUM* d = NULL;
- const unsigned char* der;
- int derSz;
- #ifdef USE_CERT_BUFFERS_1024
- der = client_key_der_1024;
- derSz = sizeof_client_key_der_1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- der = client_key_der_2048;
- derSz = sizeof_client_key_der_2048;
- #else
- der = NULL;
- derSz = 0;
- #endif
- if (der != NULL) {
- RSA_get0_key(NULL, NULL, NULL, NULL);
- RSA_get0_key(rsa, NULL, NULL, NULL);
- RSA_get0_key(NULL, &n, &e, &d);
- AssertNull(n);
- AssertNull(e);
- AssertNull(d);
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
- AssertNotNull(rsa);
- RSA_get0_key(rsa, NULL, NULL, NULL);
- RSA_get0_key(rsa, &n, NULL, NULL);
- AssertNotNull(n);
- RSA_get0_key(rsa, NULL, &e, NULL);
- AssertNotNull(e);
- RSA_get0_key(rsa, NULL, NULL, &d);
- AssertNotNull(d);
- RSA_get0_key(rsa, &n, &e, &d);
- AssertNotNull(n);
- AssertNotNull(e);
- AssertNotNull(d);
- RSA_free(rsa);
- }
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_meth(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
- RSA *rsa;
- RSA_METHOD *rsa_meth;
- #ifdef WOLFSSL_KEY_GEN
- AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
- RSA_free(rsa);
- #else
- AssertNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
- #endif
- AssertNotNull(RSA_get_default_method());
- wolfSSL_RSA_meth_free(NULL);
- AssertNull(wolfSSL_RSA_meth_new(NULL, 0));
- AssertNotNull(rsa_meth =
- RSA_meth_new("placeholder RSA method", RSA_METHOD_FLAG_NO_CHECK));
- #ifndef NO_WOLFSSL_STUB
- AssertIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
- AssertIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
- AssertIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
- AssertIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
- AssertIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
- AssertIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
- AssertIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
- #endif
- AssertIntEQ(RSA_flags(NULL), 0);
- RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
- RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
- AssertIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
- AssertNotNull(rsa = RSA_new());
- /* No method set. */
- AssertIntEQ(RSA_flags(rsa), 0);
- RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
- RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
- AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
- AssertIntEQ(RSA_set_method(NULL, rsa_meth), 1);
- AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
- AssertNull(RSA_get_method(NULL));
- AssertPtrEq(RSA_get_method(rsa), rsa_meth);
- AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
- RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
- AssertIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
- AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
- RSA_METHOD_FLAG_NO_CHECK);
- RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
- AssertIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
- AssertIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
- /* rsa_meth is freed here */
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_verify(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
- !defined(NO_FILESYSTEM)
- #ifndef NO_BIO
- XFILE fp;
- RSA *pKey, *pubKey;
- X509 *cert;
- const char *text = "Hello wolfSSL !";
- unsigned char hash[SHA256_DIGEST_LENGTH];
- unsigned char signature[2048/8];
- unsigned int signatureLength;
- byte *buf;
- BIO *bio;
- SHA256_CTX c;
- EVP_PKEY *evpPkey, *evpPubkey;
- size_t sz;
- /* generate hash */
- SHA256_Init(&c);
- SHA256_Update(&c, text, strlen(text));
- SHA256_Final(hash, &c);
- #ifdef WOLFSSL_SMALL_STACK_CACHE
- /* workaround for small stack cache case */
- wc_Sha256Free((wc_Sha256*)&c);
- #endif
- /* read privete key file */
- fp = XFOPEN(svrKeyFile, "rb");
- AssertTrue((fp != XBADFILE));
- AssertIntGE(XFSEEK(fp, 0, XSEEK_END), 0);
- sz = XFTELL(fp);
- XREWIND(fp);
- AssertNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
- AssertIntEQ(XFREAD(buf, 1, sz, fp), sz);
- XFCLOSE(fp);
- /* read private key and sign hash data */
- AssertNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
- AssertNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
- AssertNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
- AssertIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
- signature, &signatureLength, pKey), SSL_SUCCESS);
- /* read public key and verify signed data */
- fp = XFOPEN(svrCertFile,"rb");
- AssertTrue((fp != XBADFILE));
- cert = PEM_read_X509(fp, 0, 0, 0 );
- XFCLOSE(fp);
- evpPubkey = X509_get_pubkey(cert);
- pubKey = EVP_PKEY_get1_RSA(evpPubkey);
- AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
- signatureLength, pubKey), SSL_SUCCESS);
- AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
- signatureLength, NULL), SSL_FAILURE);
- AssertIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
- signatureLength, pubKey), SSL_FAILURE);
- AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
- signatureLength, pubKey), SSL_FAILURE);
- AssertIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
- signatureLength, NULL), SSL_FAILURE);
- RSA_free(pKey);
- EVP_PKEY_free(evpPkey);
- RSA_free(pubKey);
- EVP_PKEY_free(evpPubkey);
- X509_free(cert);
- BIO_free(bio);
- XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
- res = TEST_RES_CHECK(1);
- #endif
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_sign(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
- RSA *rsa;
- unsigned char hash[SHA256_DIGEST_LENGTH];
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* privDer = client_key_der_1024;
- size_t privDerSz = sizeof_client_key_der_1024;
- const unsigned char* pubDer = client_keypub_der_1024;
- size_t pubDerSz = sizeof_client_keypub_der_1024;
- unsigned char signature[1024/8];
- #else
- const unsigned char* privDer = client_key_der_2048;
- size_t privDerSz = sizeof_client_key_der_2048;
- const unsigned char* pubDer = client_keypub_der_2048;
- size_t pubDerSz = sizeof_client_keypub_der_2048;
- unsigned char signature[2048/8];
- #endif
- unsigned int signatureLen;
- const unsigned char* der;
- XMEMSET(hash, 0, sizeof(hash));
- der = privDer;
- rsa = NULL;
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
- AssertIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
- AssertIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
- &signatureLen, rsa), 0);
- AssertIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
- &signatureLen, rsa), 0);
- AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
- &signatureLen, rsa), 0);
- AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
- NULL, rsa), 0);
- AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
- &signatureLen, NULL), 0);
- AssertIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
- &signatureLen, rsa), 1);
- RSA_free(rsa);
- der = pubDer;
- rsa = NULL;
- AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
- AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
- signatureLen, rsa), 1);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_sign_ex(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
- RSA *rsa;
- unsigned char hash[SHA256_DIGEST_LENGTH];
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* privDer = client_key_der_1024;
- size_t privDerSz = sizeof_client_key_der_1024;
- const unsigned char* pubDer = client_keypub_der_1024;
- size_t pubDerSz = sizeof_client_keypub_der_1024;
- unsigned char signature[1024/8];
- #else
- const unsigned char* privDer = client_key_der_2048;
- size_t privDerSz = sizeof_client_key_der_2048;
- const unsigned char* pubDer = client_keypub_der_2048;
- size_t pubDerSz = sizeof_client_keypub_der_2048;
- unsigned char signature[2048/8];
- #endif
- unsigned int signatureLen;
- const unsigned char* der;
- unsigned char encodedHash[51];
- unsigned int encodedHashLen;
- const unsigned char expEncHash[] = {
- 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
- 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
- 0x00, 0x04, 0x20,
- /* Hash data */
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
- };
- XMEMSET(hash, 0, sizeof(hash));
- AssertNotNull(rsa = wolfSSL_RSA_new());
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
- &signatureLen, rsa, 1), 0);
- wolfSSL_RSA_free(rsa);
- der = privDer;
- rsa = NULL;
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
- -1), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
- signature, &signatureLen, rsa, 1), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
- &signatureLen, rsa, 1), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
- &signatureLen, rsa, 1), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
- NULL, rsa, 1), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
- &signatureLen, NULL, 1), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
- &signatureLen, rsa, -1), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
- &signatureLen, rsa, 0), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
- &signatureLen, rsa, 0), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
- NULL, rsa, 0), 0);
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
- &signatureLen, rsa, 1), 1);
- /* Test returning encoded hash. */
- AssertIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
- &encodedHashLen, rsa, 0), 1);
- AssertIntEQ(encodedHashLen, sizeof(expEncHash));
- AssertIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
- RSA_free(rsa);
- der = pubDer;
- rsa = NULL;
- AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
- AssertIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
- signatureLen, rsa), 1);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_public_decrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
- RSA *rsa;
- unsigned char msg[SHA256_DIGEST_LENGTH];
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* pubDer = client_keypub_der_1024;
- size_t pubDerSz = sizeof_client_keypub_der_1024;
- unsigned char decMsg[1024/8];
- const unsigned char encMsg[] = {
- 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
- 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
- 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
- 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
- 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
- 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
- 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
- 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
- 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
- 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
- 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
- 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
- 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
- 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
- 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
- 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
- };
- #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
- defined(WC_RSA_NO_PADDING)
- const unsigned char encMsgNoPad[] = {
- 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
- 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
- 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
- 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
- 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
- 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
- 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
- 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
- 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
- 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
- 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
- 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
- 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
- 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
- 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
- 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
- };
- #endif
- #else
- const unsigned char* pubDer = client_keypub_der_2048;
- size_t pubDerSz = sizeof_client_keypub_der_2048;
- unsigned char decMsg[2048/8];
- const unsigned char encMsg[] = {
- 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
- 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
- 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
- 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
- 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
- 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
- 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
- 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
- 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
- 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
- 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
- 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
- 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
- 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
- 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
- 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
- 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
- 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
- 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
- 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
- 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
- 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
- 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
- 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
- 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
- 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
- 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
- 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
- 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
- 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
- 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
- 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
- };
- #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
- defined(WC_RSA_NO_PADDING)
- const unsigned char encMsgNoPad[] = {
- 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
- 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
- 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
- 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
- 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
- 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
- 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
- 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
- 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
- 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
- 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
- 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
- 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
- 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
- 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
- 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
- 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
- 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
- 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
- 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
- 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
- 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
- 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
- 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
- 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
- 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
- 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
- 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
- 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
- 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
- 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
- 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
- };
- #endif
- #endif
- const unsigned char* der;
- #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
- defined(WC_RSA_NO_PADDING)
- int i;
- #endif
- XMEMSET(msg, 0, sizeof(msg));
- der = pubDer;
- rsa = NULL;
- AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
- AssertIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
- AssertIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
- RSA_PKCS1_PSS_PADDING), -1);
- AssertIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
- RSA_PKCS1_PADDING), 32);
- AssertIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
- #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
- defined(WC_RSA_NO_PADDING)
- AssertIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
- rsa, RSA_NO_PADDING), sizeof(decMsg));
- /* Zeros before actual data. */
- for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
- AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
- }
- /* Check actual data. */
- XMEMSET(msg, 0x01, sizeof(msg));
- AssertIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
- #endif
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_private_encrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
- RSA *rsa;
- unsigned char msg[SHA256_DIGEST_LENGTH];
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* privDer = client_key_der_1024;
- size_t privDerSz = sizeof_client_key_der_1024;
- unsigned char encMsg[1024/8];
- const unsigned char expEncMsg[] = {
- 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
- 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
- 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
- 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
- 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
- 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
- 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
- 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
- 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
- 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
- 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
- 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
- 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
- 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
- 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
- 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
- };
- #ifdef WC_RSA_NO_PADDING
- const unsigned char expEncMsgNoPad[] = {
- 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
- 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
- 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
- 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
- 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
- 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
- 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
- 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
- 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
- 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
- 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
- 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
- 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
- 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
- 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
- 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
- };
- #endif
- #else
- const unsigned char* privDer = client_key_der_2048;
- size_t privDerSz = sizeof_client_key_der_2048;
- unsigned char encMsg[2048/8];
- const unsigned char expEncMsg[] = {
- 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
- 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
- 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
- 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
- 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
- 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
- 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
- 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
- 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
- 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
- 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
- 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
- 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
- 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
- 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
- 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
- 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
- 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
- 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
- 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
- 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
- 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
- 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
- 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
- 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
- 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
- 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
- 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
- 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
- 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
- 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
- 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
- };
- #ifdef WC_RSA_NO_PADDING
- const unsigned char expEncMsgNoPad[] = {
- 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
- 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
- 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
- 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
- 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
- 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
- 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
- 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
- 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
- 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
- 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
- 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
- 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
- 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
- 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
- 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
- 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
- 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
- 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
- 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
- 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
- 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
- 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
- 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
- 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
- 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
- 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
- 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
- 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
- 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
- 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
- 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
- };
- #endif
- #endif
- const unsigned char* der;
- XMEMSET(msg, 0x00, sizeof(msg));
- der = privDer;
- rsa = NULL;
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
- AssertIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
- AssertIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
- -1);
- AssertIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
- RSA_PKCS1_PSS_PADDING), -1);
- AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
- RSA_PKCS1_PADDING), sizeof(encMsg));
- AssertIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
- #ifdef WC_RSA_NO_PADDING
- /* Non-zero message. */
- XMEMSET(msg, 0x01, sizeof(msg));
- AssertIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
- RSA_NO_PADDING), sizeof(encMsg));
- AssertIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
- #endif
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_public_encrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
- RSA* rsa;
- const unsigned char msg[2048/8] = { 0 };
- unsigned char encMsg[2048/8];
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
- RSA_PKCS1_PSS_PADDING), -1);
- /* Empty RSA key. */
- AssertIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
- RSA_PKCS1_PADDING), -1);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_private_decrypt(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
- RSA* rsa;
- unsigned char msg[2048/8];
- const unsigned char encMsg[2048/8] = { 0 };
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
- RSA_PKCS1_PADDING), -1);
- AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
- RSA_PKCS1_PSS_PADDING), -1);
- /* Empty RSA key. */
- AssertIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
- RSA_PKCS1_PADDING), -1);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_GenAdd(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- RSA *rsa;
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* privDer = client_key_der_1024;
- size_t privDerSz = sizeof_client_key_der_1024;
- const unsigned char* pubDer = client_keypub_der_1024;
- size_t pubDerSz = sizeof_client_keypub_der_1024;
- #else
- const unsigned char* privDer = client_key_der_2048;
- size_t privDerSz = sizeof_client_key_der_2048;
- const unsigned char* pubDer = client_keypub_der_2048;
- size_t pubDerSz = sizeof_client_keypub_der_2048;
- #endif
- const unsigned char* der;
- der = privDer;
- rsa = NULL;
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
- AssertIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
- #ifndef RSA_LOW_MEM
- AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
- #else
- /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
- AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
- #endif
- RSA_free(rsa);
- der = pubDer;
- rsa = NULL;
- AssertNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
- /* Need private values. */
- AssertIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_blinding_on(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
- RSA *rsa;
- WOLFSSL_BN_CTX *bnCtx;
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* privDer = client_key_der_1024;
- size_t privDerSz = sizeof_client_key_der_1024;
- #else
- const unsigned char* privDer = client_key_der_2048;
- size_t privDerSz = sizeof_client_key_der_2048;
- #endif
- const unsigned char* der;
- der = privDer;
- rsa = NULL;
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
- AssertNotNull(bnCtx = wolfSSL_BN_CTX_new());
- /* Does nothing so all parameters are valid. */
- AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
- AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
- AssertIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
- AssertIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
- wolfSSL_BN_CTX_free(bnCtx);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_RSA_ex_data(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
- RSA* rsa;
- unsigned char data[1];
- rsa = RSA_new();
- AssertNull(wolfSSL_RSA_get_ex_data(NULL, 0));
- AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
- #ifdef MAX_EX_DATA
- AssertNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
- AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
- #endif
- AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
- AssertIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
- #ifdef HAVE_EX_DATA
- AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
- AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
- AssertPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
- #else
- AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
- AssertIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
- AssertNull(wolfSSL_RSA_get_ex_data(rsa, 0));
- #endif
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_RSA && OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_RSA_LoadDer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
- defined(OPENSSL_EXTRA_X509_SMALL))
- RSA *rsa;
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* privDer = client_key_der_1024;
- size_t privDerSz = sizeof_client_key_der_1024;
- #else
- const unsigned char* privDer = client_key_der_2048;
- size_t privDerSz = sizeof_client_key_der_2048;
- #endif
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
- AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
- AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
- AssertIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_RSA && OPENSSL_EXTRA */
- return res;
- }
- /* Local API. */
- static int test_wolfSSL_RSA_To_Der(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_TEST_STATIC_BUILD
- #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
- defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- RSA* rsa;
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* privDer = client_key_der_1024;
- size_t privDerSz = sizeof_client_key_der_1024;
- const unsigned char* pubDer = client_keypub_der_1024;
- size_t pubDerSz = sizeof_client_keypub_der_1024;
- unsigned char out[sizeof(client_key_der_1024)];
- #else
- const unsigned char* privDer = client_key_der_2048;
- size_t privDerSz = sizeof_client_key_der_2048;
- const unsigned char* pubDer = client_keypub_der_2048;
- size_t pubDerSz = sizeof_client_keypub_der_2048;
- unsigned char out[sizeof(client_key_der_2048)];
- #endif
- const unsigned char* der;
- unsigned char* outDer = NULL;
- der = privDer;
- rsa = NULL;
- AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
- AssertIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
- outDer = out;
- AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
- AssertIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
- outDer = NULL;
- AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
- AssertNotNull(outDer);
- AssertIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
- XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- AssertIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
- outDer = out;
- AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
- AssertIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
- RSA_free(rsa);
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
- RSA_free(rsa);
- der = pubDer;
- rsa = NULL;
- AssertNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
- AssertIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- #endif
- return res;
- }
- /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
- static int test_wolfSSL_PEM_read_RSAPublicKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
- XFILE file;
- const char* fname = "./certs/server-keyPub.pem";
- RSA *rsa;
- AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
- file = XFOPEN(fname, "rb");
- AssertTrue((file != XBADFILE));
- AssertNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
- AssertIntEQ(RSA_size(rsa), 256);
- RSA_free(rsa);
- XFCLOSE(file);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
- static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
- defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
- RSA* rsa = NULL;
- AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
- AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
- /* Valid but stub so returns 0. */
- AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
- defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
- RSA* rsa;
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* privDer = client_key_der_1024;
- size_t privDerSz = sizeof_client_key_der_1024;
- #else
- const unsigned char* privDer = client_key_der_2048;
- size_t privDerSz = sizeof_client_key_der_2048;
- #endif
- const unsigned char* der;
- #ifndef NO_AES
- unsigned char passwd[] = "password";
- #endif
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
- NULL, NULL), 0);
- RSA_free(rsa);
- der = privDer;
- rsa = NULL;
- AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
- NULL, NULL), 0);
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
- NULL, NULL), 0);
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
- NULL, NULL), 1);
- #ifndef NO_AES
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
- NULL, 0, NULL, NULL), 1);
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
- passwd, sizeof(passwd) - 1, NULL, NULL), 1);
- #endif
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
- !defined(HAVE_USER_RSA) && (defined(WOLFSSL_PEM_TO_DER) || \
- defined(WOLFSSL_DER_TO_PEM))
- RSA* rsa;
- #ifdef USE_CERT_BUFFERS_1024
- const unsigned char* privDer = client_key_der_1024;
- size_t privDerSz = sizeof_client_key_der_1024;
- #else
- const unsigned char* privDer = client_key_der_2048;
- size_t privDerSz = sizeof_client_key_der_2048;
- #endif
- const unsigned char* der;
- #ifndef NO_AES
- unsigned char passwd[] = "password";
- #endif
- unsigned char* pem;
- int plen;
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
- &plen), 0);
- RSA_free(rsa);
- der = privDer;
- rsa = NULL;
- AssertNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
- AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
- &plen), 0);
- AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
- &plen), 0);
- AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
- NULL), 0);
- AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
- &plen), 1);
- XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
- #ifndef NO_AES
- AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
- NULL, 0, &pem, &plen), 1);
- XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
- AssertIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
- passwd, sizeof(passwd) - 1, &pem, &plen), 1);
- XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
- #endif
- RSA_free(rsa);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_DH(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
- DH *dh = NULL;
- BIGNUM* p;
- BIGNUM* q;
- BIGNUM* g;
- BIGNUM* pub;
- BIGNUM* priv;
- #if defined(OPENSSL_ALL)
- #if !defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
- FILE* f = NULL;
- unsigned char buf[268];
- const unsigned char* pt = buf;
- long len = 0;
- dh = NULL;
- XMEMSET(buf, 0, sizeof(buf));
- /* Test 2048 bit parameters */
- f = XFOPEN("./certs/dh2048.der", "rb");
- AssertTrue(f != XBADFILE);
- len = (long)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- AssertNotNull(dh = d2i_DHparams(NULL, &pt, len));
- AssertNotNull(dh->p);
- AssertNotNull(dh->g);
- AssertTrue(pt == buf);
- AssertIntEQ(DH_generate_key(dh), 1);
- AssertIntEQ(DH_generate_key(dh), 1);
- AssertIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
- AssertNotNull(pub = BN_new());
- AssertIntEQ(BN_set_word(pub, 1), 1);
- AssertIntEQ(DH_compute_key(buf, NULL, NULL), -1);
- AssertIntEQ(DH_compute_key(NULL, pub, NULL), -1);
- AssertIntEQ(DH_compute_key(NULL, NULL, dh), -1);
- AssertIntEQ(DH_compute_key(buf, pub, NULL), -1);
- AssertIntEQ(DH_compute_key(buf, NULL, dh), -1);
- AssertIntEQ(DH_compute_key(NULL, pub, dh), -1);
- AssertIntEQ(DH_compute_key(buf, pub, dh), -1);
- BN_free(pub);
- DH_get0_pqg(dh, (const BIGNUM**)&p,
- (const BIGNUM**)&q,
- (const BIGNUM**)&g);
- AssertPtrEq(p, dh->p);
- AssertPtrEq(q, dh->q);
- AssertPtrEq(g, dh->g);
- DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
- DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
- AssertPtrEq(pub, dh->pub_key);
- AssertPtrEq(priv, dh->priv_key);
- DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
- AssertPtrEq(pub, dh->pub_key);
- DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
- AssertPtrEq(priv, dh->priv_key);
- AssertNotNull(pub = BN_new());
- AssertNotNull(priv = BN_new());
- AssertIntEQ(DH_set0_key(NULL, pub, priv), 0);
- AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
- AssertNotNull(pub = BN_new());
- AssertIntEQ(DH_set0_key(dh, pub, NULL), 1);
- AssertNotNull(priv = BN_new());
- AssertIntEQ(DH_set0_key(dh, NULL, priv), 1);
- AssertPtrEq(pub, dh->pub_key);
- AssertPtrEq(priv, dh->priv_key);
- DH_free(dh);
- AssertNotNull(dh = DH_new());
- AssertNotNull(p = BN_new());
- AssertIntEQ(BN_set_word(p, 1), 1);
- AssertIntEQ(DH_compute_key(buf, p, dh), -1);
- AssertNotNull(pub = BN_new());
- AssertNotNull(priv = BN_new());
- AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
- AssertIntEQ(DH_compute_key(buf, p, dh), -1);
- BN_free(p);
- DH_free(dh);
- #ifdef WOLFSSL_KEY_GEN
- AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
- AssertIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
- DH_free(dh);
- #endif
- #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
- #endif /* OPENSSL_ALL */
- (void)dh;
- (void)p;
- (void)q;
- (void)g;
- (void)pub;
- (void)priv;
- dh = wolfSSL_DH_new();
- AssertNotNull(dh);
- /* invalid parameters test */
- DH_get0_pqg(NULL, (const BIGNUM**)&p,
- (const BIGNUM**)&q,
- (const BIGNUM**)&g);
- DH_get0_pqg(dh, NULL,
- (const BIGNUM**)&q,
- (const BIGNUM**)&g);
- DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
- DH_get0_pqg(dh, NULL, NULL, NULL);
- AssertTrue(1);
- DH_get0_pqg(dh, (const BIGNUM**)&p,
- (const BIGNUM**)&q,
- (const BIGNUM**)&g);
- AssertPtrEq(p, NULL);
- AssertPtrEq(q, NULL);
- AssertPtrEq(g, NULL);
- DH_free(dh);
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
- || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
- #if defined(OPENSSL_ALL) || \
- defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
- dh = wolfSSL_DH_new();
- AssertNotNull(dh);
- p = wolfSSL_BN_new();
- AssertNotNull(p);
- AssertIntEQ(BN_set_word(p, 11), 1);
- g = wolfSSL_BN_new();
- AssertNotNull(g);
- AssertIntEQ(BN_set_word(g, 2), 1);
- q = wolfSSL_BN_new();
- AssertNotNull(q);
- AssertIntEQ(BN_set_word(q, 5), 1);
- AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
- AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
- AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
- AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
- AssertIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
- /* Don't need q. */
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
- /* Setting again will free the p and g. */
- wolfSSL_BN_free(q);
- DH_free(dh);
- dh = wolfSSL_DH_new();
- AssertNotNull(dh);
- p = wolfSSL_BN_new();
- AssertNotNull(p);
- AssertIntEQ(BN_set_word(p, 11), 1);
- g = wolfSSL_BN_new();
- AssertNotNull(g);
- AssertIntEQ(BN_set_word(g, 2), 1);
- q = wolfSSL_BN_new();
- AssertNotNull(q);
- AssertIntEQ(BN_set_word(q, 5), 1);
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
- /* p, q and g are now owned by dh - don't free. */
- p = wolfSSL_BN_new();
- AssertNotNull(p);
- AssertIntEQ(BN_set_word(p, 11), 1);
- g = wolfSSL_BN_new();
- AssertNotNull(g);
- AssertIntEQ(BN_set_word(g, 2), 1);
- q = wolfSSL_BN_new();
- AssertNotNull(q);
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
- /* p, q and g are now owned by dh - don't free. */
- DH_free(dh);
- AssertIntEQ(DH_generate_key(NULL), 0);
- AssertNotNull(dh = DH_new());
- AssertIntEQ(DH_generate_key(dh), 0);
- p = wolfSSL_BN_new();
- AssertNotNull(p);
- AssertIntEQ(BN_set_word(p, 0), 1);
- g = wolfSSL_BN_new();
- AssertNotNull(g);
- AssertIntEQ(BN_set_word(g, 2), 1);
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
- AssertIntEQ(DH_generate_key(dh), 0);
- DH_free(dh);
- #endif
- #endif
- /* Test DH_up_ref() */
- dh = wolfSSL_DH_new();
- AssertNotNull(dh);
- AssertIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
- DH_free(dh); /* decrease ref count */
- DH_free(dh); /* free WOLFSSL_DH */
- AssertNull((dh = DH_new_by_nid(NID_sha1)));
- #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
- FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
- (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
- #ifdef HAVE_FFDHE_2048
- AssertNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
- DH_free(dh);
- #endif
- #ifdef HAVE_FFDHE_3072
- AssertNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
- DH_free(dh);
- #endif
- #ifdef HAVE_FFDHE_4096
- AssertNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
- DH_free(dh);
- #endif
- #else
- AssertNull((dh = DH_new_by_nid(NID_ffdhe2048)));
- #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
- * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
- AssertIntEQ(wolfSSL_DH_size(NULL), -1);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_DH */
- return res;
- }
- static int test_wolfSSL_DH_dup(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
- #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
- defined(OPENSSL_EXTRA)
- DH *dh;
- DH *dhDup;
- WOLFSSL_BIGNUM* p;
- WOLFSSL_BIGNUM* g;
- AssertNotNull(p = wolfSSL_BN_new());
- AssertNotNull(g = wolfSSL_BN_new());
- AssertIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
- dhDup = wolfSSL_DH_dup(NULL);
- AssertNull(dhDup);
- dh = wolfSSL_DH_new();
- AssertNotNull(dh);
- dhDup = wolfSSL_DH_dup(dh);
- AssertNull(dhDup);
- #if defined(OPENSSL_ALL) || \
- defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
- AssertIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
- dhDup = wolfSSL_DH_dup(dh);
- AssertNotNull(dhDup);
- wolfSSL_DH_free(dhDup);
- #else
- wolfSSL_BN_free(p);
- wolfSSL_BN_free(g);
- #endif
- wolfSSL_DH_free(dh);
- res = TEST_RES_CHECK(1);
- #endif
- #endif
- return res;
- }
- static int test_wolfSSL_DH_check(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_ALL
- #ifndef NO_DH
- #ifndef NO_BIO
- #ifndef NO_DSA
- byte buf[6000];
- char file[] = "./certs/dsaparams.pem";
- XFILE f;
- int bytes;
- BIO* bio;
- DSA* dsa;
- #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
- static const byte dh2048[] = {
- 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
- 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
- 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
- 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
- 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
- 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
- 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
- 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
- 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
- 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
- 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
- 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
- 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
- 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
- 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
- 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
- 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
- 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
- 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
- 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
- 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
- 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
- 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
- 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
- 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
- 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
- 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
- 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
- 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
- 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
- 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
- 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
- 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
- 0x93, 0x02, 0x01, 0x02
- };
- const byte* params;
- #endif
- DH* dh = NULL;
- WOLFSSL_BIGNUM* p;
- WOLFSSL_BIGNUM* g;
- WOLFSSL_BIGNUM* pTmp = NULL;
- WOLFSSL_BIGNUM* gTmp = NULL;
- int codes = -1;
- #ifndef NO_DSA
- /* Initialize DH */
- f = XFOPEN(file, "rb");
- AssertTrue((f != XBADFILE));
- bytes = (int)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- bio = BIO_new_mem_buf((void*)buf, bytes);
- AssertNotNull(bio);
- dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
- AssertNotNull(dsa);
- dh = wolfSSL_DSA_dup_DH(dsa);
- AssertNotNull(dh);
- BIO_free(bio);
- DSA_free(dsa);
- #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
- params = dh2048;
- dh = wolfSSL_d2i_DHparams(NULL, ¶ms, (long)sizeof(dh2048));
- AssertNotNull(dh);
- #else
- dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048);
- AssertNotNull(dh);
- #endif
- /* Test assumed to be valid dh.
- * Should return WOLFSSL_SUCCESS
- * codes should be 0
- * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
- */
- AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
- AssertIntEQ(codes, 0);
- /* Test NULL dh: expected BAD_FUNC_ARG */
- AssertIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
- /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
- pTmp = dh->p;
- dh->p = NULL;
- AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
- AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
- AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
- /* set dh->p back to normal so it wont fail on next tests */
- dh->p = pTmp;
- pTmp = NULL;
- /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
- gTmp = dh->g;
- dh->g = NULL;
- AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
- AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
- AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
- dh->g = gTmp;
- gTmp = NULL;
- /* Cleanup */
- DH_free(dh);
- dh = DH_new();
- AssertNotNull(dh);
- /* Check empty DH. */
- AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
- AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
- AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
- /* Check non-prime valued p. */
- AssertNotNull(p = BN_new());
- AssertIntEQ(BN_set_word(p, 4), 1);
- AssertNotNull(g = BN_new());
- AssertIntEQ(BN_set_word(g, 2), 1);
- AssertIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
- AssertIntEQ(wolfSSL_DH_check(dh, &codes), 1);
- AssertIntEQ(wolfSSL_DH_check(dh, NULL), 0);
- AssertIntEQ(codes, DH_CHECK_P_NOT_PRIME);
- DH_free(dh);
- res = TEST_RES_CHECK(1);
- #endif
- #endif /* !NO_DH && !NO_DSA */
- #endif
- return res;
- }
- static int test_wolfSSL_DH_prime(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
- WOLFSSL_BIGNUM* bn;
- #if WOLFSSL_MAX_BN_BITS >= 768
- WOLFSSL_BIGNUM* bn2;
- #endif
- bn = wolfSSL_DH_768_prime(NULL);
- #if WOLFSSL_MAX_BN_BITS >= 768
- AssertNotNull(bn);
- bn2 = wolfSSL_DH_768_prime(bn);
- AssertNotNull(bn2);
- AssertTrue(bn == bn2);
- wolfSSL_BN_free(bn);
- #else
- AssertNull(bn);
- #endif
- bn = wolfSSL_DH_1024_prime(NULL);
- #if WOLFSSL_MAX_BN_BITS >= 1024
- AssertNotNull(bn);
- wolfSSL_BN_free(bn);
- #else
- AssertNull(bn);
- #endif
- bn = wolfSSL_DH_2048_prime(NULL);
- #if WOLFSSL_MAX_BN_BITS >= 2048
- AssertNotNull(bn);
- wolfSSL_BN_free(bn);
- #else
- AssertNull(bn);
- #endif
- bn = wolfSSL_DH_3072_prime(NULL);
- #if WOLFSSL_MAX_BN_BITS >= 3072
- AssertNotNull(bn);
- wolfSSL_BN_free(bn);
- #else
- AssertNull(bn);
- #endif
- bn = wolfSSL_DH_4096_prime(NULL);
- #if WOLFSSL_MAX_BN_BITS >= 4096
- AssertNotNull(bn);
- wolfSSL_BN_free(bn);
- #else
- AssertNull(bn);
- #endif
- bn = wolfSSL_DH_6144_prime(NULL);
- #if WOLFSSL_MAX_BN_BITS >= 6144
- AssertNotNull(bn);
- wolfSSL_BN_free(bn);
- #else
- AssertNull(bn);
- #endif
- bn = wolfSSL_DH_8192_prime(NULL);
- #if WOLFSSL_MAX_BN_BITS >= 8192
- AssertNotNull(bn);
- wolfSSL_BN_free(bn);
- #else
- AssertNull(bn);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_DH_1536_prime(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
- BIGNUM* bn;
- unsigned char bits[200];
- int sz = 192; /* known binary size */
- const byte expected[] = {
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
- 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
- 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
- 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
- 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
- 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
- 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
- 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
- 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
- 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
- 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
- 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
- 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
- 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
- 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
- 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
- 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
- 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
- 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
- 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
- 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
- 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- };
- bn = get_rfc3526_prime_1536(NULL);
- AssertNotNull(bn);
- AssertIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
- AssertIntEQ(0, XMEMCMP(expected, bits, sz));
- BN_free(bn);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_DH_get_2048_256(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
- WOLFSSL_DH* dh;
- const WOLFSSL_BIGNUM* pBn;
- const WOLFSSL_BIGNUM* gBn;
- const WOLFSSL_BIGNUM* qBn;
- const byte pExpected[] = {
- 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
- 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
- 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
- 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
- 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
- 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
- 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
- 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
- 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
- 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
- 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
- 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
- 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
- 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
- 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
- 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
- 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
- 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
- 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
- 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
- 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
- 0x1E, 0x1A, 0x15, 0x97
- };
- const byte gExpected[] = {
- 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
- 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
- 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
- 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
- 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
- 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
- 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
- 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
- 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
- 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
- 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
- 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
- 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
- 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
- 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
- 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
- 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
- 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
- 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
- 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
- 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
- 0x6C, 0xC4, 0x16, 0x59
- };
- const byte qExpected[] = {
- 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
- 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
- 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
- };
- int pSz;
- int qSz;
- int gSz;
- byte* pReturned;
- byte* qReturned;
- byte* gReturned;
- AssertNotNull((dh = wolfSSL_DH_get_2048_256()));
- wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
- AssertIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
- AssertNotNull(pReturned = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
- AssertIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
- AssertIntEQ(pSz, sizeof(pExpected));
- AssertIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
- AssertIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
- AssertNotNull(qReturned = (byte*)XMALLOC(qSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
- AssertIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
- AssertIntEQ(qSz, sizeof(qExpected));
- AssertIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
- AssertIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
- AssertNotNull(gReturned = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
- AssertIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
- AssertIntEQ(gSz, sizeof(gExpected));
- AssertIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
- wolfSSL_DH_free(dh);
- XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_PEM_write_DHparams(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
- !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
- DH* dh;
- BIO* bio;
- XFILE fp;
- byte pem[2048];
- int pemSz;
- const char expected[] =
- "-----BEGIN DH PARAMETERS-----\n"
- "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
- "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
- "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
- "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
- "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
- "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
- "-----END DH PARAMETERS-----\n";
- const char badPem[] =
- "-----BEGIN DH PARAMETERS-----\n"
- "-----END DH PARAMETERS-----\n";
- const char emptySeqPem[] =
- "-----BEGIN DH PARAMETERS-----\n"
- "MAA=\n"
- "-----END DH PARAMETERS-----\n";
- AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
- AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
- XFCLOSE(fp);
- AssertNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
- AssertIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
- (int)sizeof(badPem));
- AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
- BIO_free(bio);
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
- AssertIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
- (int)sizeof(emptySeqPem));
- AssertNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
- BIO_free(bio);
- AssertNotNull(bio = BIO_new(BIO_s_mem()));
- AssertIntEQ(BIO_write(bio, pem, pemSz), pemSz);
- AssertNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
- BIO_free(bio);
- AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
- AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
- AssertIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
- DH_free(dh);
- dh = wolfSSL_DH_new();
- AssertIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
- XFCLOSE(fp);
- wolfSSL_DH_free(dh);
- /* check results */
- XMEMSET(pem, 0, sizeof(pem));
- AssertNotNull(fp = XFOPEN("./test-write-dhparams.pem", "rb"));
- AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
- AssertIntEQ(XMEMCMP(pem, expected, pemSz), 0);
- XFCLOSE(fp);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_d2i_DHparams(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_ALL
- #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- FILE* f = NULL;
- unsigned char buf[4096];
- const unsigned char* pt = buf;
- #ifdef HAVE_FFDHE_2048
- const char* params1 = "./certs/dh2048.der";
- #endif
- #ifdef HAVE_FFDHE_3072
- const char* params2 = "./certs/dh3072.der";
- #endif
- long len = 0;
- WOLFSSL_DH* dh = NULL;
- XMEMSET(buf, 0, sizeof(buf));
- /* Test 2048 bit parameters */
- #ifdef HAVE_FFDHE_2048
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- f = XFOPEN(params1, "rb");
- AssertTrue(f != XBADFILE);
- len = (long)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- /* Valid case */
- AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
- AssertNotNull(dh->p);
- AssertNotNull(dh->g);
- AssertTrue(pt == buf);
- AssertIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
- AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
- AssertIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
- /* Invalid cases */
- AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
- AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
- AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
- DH_free(dh);
- *buf = 0;
- pt = buf;
- res = TEST_RES_CHECK(1);
- }
- #endif /* HAVE_FFDHE_2048 */
- /* Test 3072 bit parameters */
- #ifdef HAVE_FFDHE_3072
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- f = XFOPEN(params2, "rb");
- AssertTrue(f != XBADFILE);
- len = (long)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- /* Valid case */
- AssertNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
- AssertNotNull(dh->p);
- AssertNotNull(dh->g);
- AssertTrue(pt != buf);
- AssertIntEQ(DH_generate_key(dh), 1);
- /* Invalid cases */
- AssertNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
- AssertNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
- DH_free(dh);
- res = TEST_RES_CHECK(1);
- }
- #endif /* HAVE_FFDHE_3072 */
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* !NO_DH */
- #endif
- return res;
- }
- static int test_wolfSSL_DH_LoadDer(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
- defined(OPENSSL_EXTRA)
- static const byte dh2048[] = {
- 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
- 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
- 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
- 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
- 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
- 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
- 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
- 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
- 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
- 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
- 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
- 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
- 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
- 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
- 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
- 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
- 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
- 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
- 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
- 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
- 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
- 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
- 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
- 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
- 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
- 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
- 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
- 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
- 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
- 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
- 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
- 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
- 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
- 0x93, 0x02, 0x01, 0x02
- };
- WOLFSSL_DH* dh;
- dh = wolfSSL_DH_new();
- AssertNotNull(dh);
- AssertIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
- AssertIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
- AssertIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
- AssertIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
- wolfSSL_DH_free(dh);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_i2d_DHparams(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_ALL
- #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- FILE* f;
- unsigned char buf[4096];
- const unsigned char* pt;
- unsigned char* pt2;
- #ifdef HAVE_FFDHE_2048
- const char* params1 = "./certs/dh2048.der";
- #endif
- #ifdef HAVE_FFDHE_3072
- const char* params2 = "./certs/dh3072.der";
- #endif
- long len;
- WOLFSSL_DH* dh;
- /* Test 2048 bit parameters */
- #ifdef HAVE_FFDHE_2048
- pt = buf;
- pt2 = buf;
- f = XFOPEN(params1, "rb");
- AssertTrue(f != XBADFILE);
- len = (long)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- /* Valid case */
- AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
- AssertTrue(pt == buf);
- AssertIntEQ(DH_generate_key(dh), 1);
- AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
- /* Invalid case */
- AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
- /* Return length only */
- AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
- DH_free(dh);
- *buf = 0;
- #endif
- /* Test 3072 bit parameters */
- #ifdef HAVE_FFDHE_3072
- pt = buf;
- pt2 = buf;
- f = XFOPEN(params2, "rb");
- AssertTrue(f != XBADFILE);
- len = (long)XFREAD(buf, 1, sizeof(buf), f);
- XFCLOSE(f);
- /* Valid case */
- AssertNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
- AssertTrue(pt == buf);
- AssertIntEQ(DH_generate_key(dh), 1);
- AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
- /* Invalid case */
- AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
- /* Return length only */
- AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
- DH_free(dh);
- #endif
- dh = DH_new();
- AssertNotNull(dh);
- pt2 = buf;
- AssertIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
- DH_free(dh);
- res = TEST_RES_CHECK(1);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
- #endif
- return res;
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
- static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey)
- {
- X509* x509 = NULL;
- BIGNUM* serial_number = NULL;
- X509_NAME* name = NULL;
- time_t epoch_off = 0;
- ASN1_INTEGER* asn1_serial_number;
- long not_before, not_after;
- AssertNotNull(x509 = X509_new());
- AssertIntNE(X509_set_pubkey(x509, pkey), 0);
- AssertNotNull(serial_number = BN_new());
- AssertIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
- AssertNotNull(asn1_serial_number = X509_get_serialNumber(x509));
- AssertNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
- /* version 3 */
- AssertIntNE(X509_set_version(x509, 2L), 0);
- AssertNotNull(name = X509_NAME_new());
- AssertIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
- (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
- AssertIntNE(X509_set_subject_name(x509, name), 0);
- AssertIntNE(X509_set_issuer_name(x509, name), 0);
- not_before = (long)wc_Time(NULL);
- not_after = not_before + (365 * 24 * 60 * 60);
- AssertNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &epoch_off));
- AssertNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &epoch_off));
- AssertIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
- BN_free(serial_number);
- X509_NAME_free(name);
- X509_free(x509);
- return 0;
- }
- #endif
- static int test_openssl_generate_key_and_cert(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- #if !defined(NO_RSA)
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- EVP_PKEY* pkey = EVP_PKEY_new();
- int key_length = 2048;
- BIGNUM* exponent = BN_new();
- RSA* rsa = RSA_new();
- AssertNotNull(pkey);
- AssertNotNull(exponent);
- AssertNotNull(rsa);
- AssertIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
- #ifndef WOLFSSL_KEY_GEN
- AssertIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
- #if defined(USE_CERT_BUFFERS_1024)
- AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
- sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
- key_length = 1024;
- #elif defined(USE_CERT_BUFFERS_2048)
- AssertIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
- sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
- #else
- RSA_free(rsa);
- rsa = NULL;
- #endif
- #else
- AssertIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
- AssertIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
- AssertIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
- AssertIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
- #endif
- if (rsa) {
- AssertIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
- BN_free(exponent);
- #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
- defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
- test_openssl_make_self_signed_certificate(pkey);
- #endif
- }
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- if (res == TEST_SKIPPED || res == TEST_SUCCESS) {
- EVP_PKEY* pkey = EVP_PKEY_new();
- EC_KEY* ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1);
- AssertNotNull(pkey);
- AssertNotNull(ec_key);
- #ifndef NO_WOLFSSL_STUB
- EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
- #endif
- AssertIntNE(EC_KEY_generate_key(ec_key), 0);
- AssertIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
- #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
- defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
- test_openssl_make_self_signed_certificate(pkey);
- #endif
- EVP_PKEY_free(pkey);
- res = TEST_RES_CHECK(1);
- }
- #endif /* HAVE_ECC */
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_stubs_are_stubs(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB)
- WOLFSSL_CTX* ctx = NULL;
- WOLFSSL_CTX* ctxN = NULL;
- #ifndef NO_WOLFSSL_CLIENT
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #elif !defined(NO_WOLFSSL_SERVER)
- ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #else
- return res;
- #endif
- #define CHECKZERO_RET(x, y, z) AssertIntEQ((int) x(y), 0); \
- AssertIntEQ((int) x(z), 0)
- /* test logic, all stubs return same result regardless of ctx being NULL
- * as there are no sanity checks, it's just a stub! If at some
- * point a stub is not a stub it should begin to return BAD_FUNC_ARG
- * if invalid inputs are supplied. Test calling both
- * with and without valid inputs, if a stub functionality remains unchanged.
- */
- CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
- CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
- wolfSSL_CTX_free(ctx);
- ctx = NULL;
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
- return res;
- }
- static int test_CONF_modules_xxx(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- CONF_modules_free();
- AssertTrue(1); /* to confirm previous call gives no harm */
- CONF_modules_unload(0);
- AssertTrue(1);
- CONF_modules_unload(1);
- AssertTrue(1);
- CONF_modules_unload(-1);
- AssertTrue(1);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_CRYPTO_set_dynlock_xxx(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- CRYPTO_set_dynlock_create_callback(
- (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
- CRYPTO_set_dynlock_create_callback(
- (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
- CRYPTO_set_dynlock_destroy_callback(
- (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
- CRYPTO_set_dynlock_destroy_callback(
- (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
- CRYPTO_set_dynlock_lock_callback(
- (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
- CRYPTO_set_dynlock_lock_callback(
- (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
- AssertTrue(1); /* to confirm previous call gives no harm */
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_CRYPTO_THREADID_xxx(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
- CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
- AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_ENGINE_cleanup(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- ENGINE_cleanup();
- AssertTrue(1); /* to confirm previous call gives no harm */
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_CTX_LoadCRL(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- WOLFSSL_CTX* ctx = NULL;
- WOLFSSL* ssl = NULL;
- const char* badPath = "dummypath";
- const char* validPath = "./certs/crl";
- const char* validFilePath = "./certs/crl/cliCrl.pem";
- const char* issuerCert = "./certs/client-cert.pem";
- int derType = WOLFSSL_FILETYPE_ASN1;
- int pemType = WOLFSSL_FILETYPE_PEM;
- int monitor = WOLFSSL_CRL_MONITOR;
- WOLFSSL_CERT_MANAGER* cm = NULL;
- #define FAIL_T1(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
- BAD_FUNC_ARG)
- #define SUCC_T(x, y, z, p, d) AssertIntEQ((int) x(y, z, p, d), \
- WOLFSSL_SUCCESS)
- FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #elif !defined(NO_WOLFSSL_SERVER)
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #else
- return;
- #endif
- SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
- SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
- SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
- wolfSSL_CTX_free(ctx);
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #elif !defined(NO_WOLFSSL_SERVER)
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #else
- return;
- #endif
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- #ifndef NO_WOLFSSL_CLIENT
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #elif !defined(NO_WOLFSSL_SERVER)
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #else
- return;
- #endif
- AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
- WOLFSSL_SUCCESS);
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- AssertNotNull(cm = wolfSSL_CertManagerNew());
- AssertIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType), WOLFSSL_SUCCESS);
- wolfSSL_CertManagerFree(cm);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_SetTmpEC_DHE_Sz(void)
- {
- int res = TEST_SKIPPED;
- #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX *ctx;
- WOLFSSL *ssl;
- ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_get0_privatekey(void)
- {
- int res = TEST_SKIPPED;
- #ifdef OPENSSL_ALL
- WOLFSSL_CTX* ctx = NULL;
- #ifndef NO_RSA
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
- AssertNull(SSL_CTX_get0_privatekey(ctx));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNull(SSL_CTX_get0_privatekey(ctx));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(SSL_CTX_get0_privatekey(ctx));
- wolfSSL_CTX_free(ctx);
- #endif
- #ifdef HAVE_ECC
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
- AssertNull(SSL_CTX_get0_privatekey(ctx));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNull(SSL_CTX_get0_privatekey(ctx));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(SSL_CTX_get0_privatekey(ctx));
- wolfSSL_CTX_free(ctx);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_dtls_set_mtu(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
- !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS)
- WOLFSSL_CTX* ctx = NULL;
- WOLFSSL* ssl = NULL;
- const char* testCertFile;
- const char* testKeyFile;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
- #ifndef NO_RSA
- testCertFile = svrCertFile;
- testKeyFile = svrKeyFile;
- #elif defined(HAVE_ECC)
- testCertFile = eccCertFile;
- testKeyFile = eccKeyFile;
- #endif
- if (testCertFile != NULL && testKeyFile != NULL) {
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
- WOLFSSL_FILETYPE_PEM));
- }
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
- defined(WOLFSSL_DTLS)
- static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
- enum HandShakeType hsType, word16 length)
- {
- size_t idx = 0;
- byte* l;
- /* record layer */
- /* handshake type */
- out[idx++] = handshake;
- /* protocol version */
- out[idx++] = 0xfe;
- out[idx++] = 0xfd; /* DTLS 1.2 */
- /* epoch 0 */
- XMEMSET(out + idx, 0, 2);
- idx += 2;
- /* sequence number */
- XMEMSET(out + idx, 0, 6);
- c32toa(seq, out + idx + 2);
- idx += 6;
- /* length in BE */
- if (length)
- c16toa(length, out + idx);
- else
- c16toa(outSz - idx - 2, out + idx);
- idx += 2;
- /* handshake layer */
- /* handshake type */
- out[idx++] = (byte)hsType;
- /* length */
- l = out + idx;
- idx += 3;
- /* message seq */
- c16toa(0, out + idx);
- idx += 2;
- /* frag offset */
- c32to24(0, out + idx);
- idx += 3;
- /* frag length */
- c32to24((word32)outSz - (word32)idx - 3, l);
- c32to24((word32)outSz - (word32)idx - 3, out + idx);
- idx += 3;
- XMEMSET(out + idx, 0, outSz - idx);
- }
- static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
- {
- byte msg[] = "This is a msg for the client";
- byte reply[40];
- AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
- reply[sizeof(reply) - 1] = '\0';
- fprintf(stderr, "Client message: %s\n", reply);
- AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
- }
- static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
- {
- byte ch[50];
- int fd = wolfSSL_get_fd(ssl);
- byte msg[] = "This is a msg for the server";
- byte reply[40];
- generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
- /* Server should ignore this datagram */
- AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
- generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
- /* Server should ignore this datagram */
- AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
- AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
- AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
- reply[sizeof(reply) - 1] = '\0';
- fprintf(stderr, "Server response: %s\n", reply);
- }
- static int test_wolfSSL_dtls_plaintext(void)
- {
- callback_functions func_cb_client;
- callback_functions func_cb_server;
- size_t i;
- struct test_params {
- method_provider client_meth;
- method_provider server_meth;
- ssl_callback on_result_server;
- ssl_callback on_result_client;
- } params[] = {
- {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
- test_wolfSSL_dtls_plaintext_server,
- test_wolfSSL_dtls_plaintext_client},
- };
- for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
- XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
- XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
- func_cb_client.doUdp = func_cb_server.doUdp = 1;
- func_cb_server.method = params[i].server_meth;
- func_cb_client.method = params[i].client_meth;
- func_cb_client.on_result = params[i].on_result_client;
- func_cb_server.on_result = params[i].on_result_server;
- test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
- if (!func_cb_client.return_code)
- return TEST_FAIL;
- if (!func_cb_server.return_code)
- return TEST_FAIL;
- }
- return TEST_RES_CHECK(1);
- }
- #else
- static int test_wolfSSL_dtls_plaintext(void) {
- return TEST_SKIPPED;
- }
- #endif
- #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
- defined(WOLFSSL_DTLS)
- static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
- {
- byte b[1100]; /* buffer for the messages to send */
- size_t idx = 0;
- size_t seq_offset = 0;
- size_t msg_offset = 0;
- int i;
- int fd = wolfSSL_get_fd(ssl);
- int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
- word32 seq_number = 100; /* start high so server definitely reads this */
- word16 msg_number = 50; /* start high so server has to buffer this */
- AssertIntEQ(ret, 1);
- /* Now let's start spamming the peer with fragments it needs to store */
- XMEMSET(b, -1, sizeof(b));
- /* record layer */
- /* handshake type */
- b[idx++] = 22;
- /* protocol version */
- b[idx++] = 0xfe;
- b[idx++] = 0xfd; /* DTLS 1.2 */
- /* epoch 0 */
- XMEMSET(b + idx, 0, 2);
- idx += 2;
- /* sequence number */
- XMEMSET(b + idx, 0, 6);
- seq_offset = idx + 2; /* increment only the low 32 bits */
- idx += 6;
- /* static length in BE */
- c16toa(42, b + idx);
- idx += 2;
- /* handshake layer */
- /* cert type */
- b[idx++] = 11;
- /* length */
- c32to24(1000, b + idx);
- idx += 3;
- /* message seq */
- c16toa(0, b + idx);
- msg_offset = idx;
- idx += 2;
- /* frag offset */
- c32to24(500, b + idx);
- idx += 3;
- /* frag length */
- c32to24(30, b + idx);
- idx += 3;
- (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
- for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
- seq_number++, msg_number++, i++) {
- struct timespec delay;
- XMEMSET(&delay, 0, sizeof(delay));
- delay.tv_nsec = 10000000; /* wait 0.01 seconds */
- c32toa(seq_number, b + seq_offset);
- c16toa(msg_number, b + msg_offset);
- ret = (int)send(fd, b, 55, 0);
- nanosleep(&delay, NULL);
- }
- }
- #ifdef WOLFSSL_DTLS13
- static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
- {
- byte b[150]; /* buffer for the messages to send */
- size_t idx = 0;
- size_t msg_offset = 0;
- int fd = wolfSSL_get_fd(ssl);
- word16 msg_number = 10; /* start high so server has to buffer this */
- int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
- AssertIntEQ(ret, 1);
- /* Now let's start spamming the peer with fragments it needs to store */
- XMEMSET(b, -1, sizeof(b));
- /* handshake type */
- b[idx++] = 11;
- /* length */
- c32to24(10000, b + idx);
- idx += 3;
- /* message_seq */
- msg_offset = idx;
- idx += 2;
- /* fragment_offset */
- c32to24(5000, b + idx);
- idx += 3;
- /* fragment_length */
- c32to24(100, b + idx);
- idx += 3;
- /* fragment contents */
- idx += 100;
- for (; ret > 0; msg_number++) {
- byte sendBuf[150];
- int sendSz = sizeof(sendBuf);
- struct timespec delay;
- XMEMSET(&delay, 0, sizeof(delay));
- delay.tv_nsec = 10000000; /* wait 0.01 seconds */
- c16toa(msg_number, b + msg_offset);
- sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
- (int)idx, handshake, 0, 0, 0);
- ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
- nanosleep(&delay, NULL);
- }
- }
- #endif
- static int test_wolfSSL_dtls_fragments(void)
- {
- callback_functions func_cb_client;
- callback_functions func_cb_server;
- size_t i;
- struct test_params {
- method_provider client_meth;
- method_provider server_meth;
- ssl_callback spammer;
- } params[] = {
- {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
- test_wolfSSL_dtls12_fragments_spammer},
- #ifdef WOLFSSL_DTLS13
- {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
- test_wolfSSL_dtls13_fragments_spammer},
- #endif
- };
- for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
- XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
- XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
- func_cb_client.doUdp = func_cb_server.doUdp = 1;
- func_cb_server.method = params[i].server_meth;
- func_cb_client.method = params[i].client_meth;
- func_cb_client.ssl_ready = params[i].spammer;
- test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
- AssertFalse(func_cb_client.return_code);
- AssertFalse(func_cb_server.return_code);
- /* The socket should be closed by the server resulting in a
- * socket error */
- AssertIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
- /* Check the server returned an error indicating the msg buffer
- * was full */
- AssertIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
- }
- return TEST_RES_CHECK(1);
- }
- static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
- {
- int fd, ret;
- byte alert_msg[] = {
- 0x15, /* alert type */
- 0xfe, 0xfd, /* version */
- 0x00, 0x00, /* epoch */
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
- 0x00, 0x02, /* length */
- 0x02, /* level: fatal */
- 0x46 /* protocol version */
- };
- fd = wolfSSL_get_fd(ssl);
- ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
- AssertIntGT(ret, 0);
- }
- static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
- {
- callback_functions client_cbs, server_cbs;
- XMEMSET(&client_cbs, 0, sizeof(client_cbs));
- XMEMSET(&server_cbs, 0, sizeof(server_cbs));
- client_cbs.doUdp = server_cbs.doUdp = 1;
- if (version12) {
- client_cbs.method = wolfDTLSv1_2_client_method;
- server_cbs.method = wolfDTLSv1_2_server_method;
- }
- else {
- #ifdef WOLFSSL_DTLS13
- client_cbs.method = wolfDTLSv1_3_client_method;
- server_cbs.method = wolfDTLSv1_3_server_method;
- #else
- return TEST_SKIPPED;
- #endif /* WOLFSSL_DTLS13 */
- }
- client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
- test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
- if (!client_cbs.return_code)
- return TEST_FAIL;
- if (!server_cbs.return_code)
- return TEST_FAIL;
- return TEST_SUCCESS;
- }
- static int test_wolfSSL_ignore_alert_before_cookie(void)
- {
- int ret;
- ret =_test_wolfSSL_ignore_alert_before_cookie(0);
- if (ret != 0)
- return ret;
- ret =_test_wolfSSL_ignore_alert_before_cookie(1);
- if (ret != 0)
- return ret;
- return 0;
- }
- static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
- {
- int ret;
- int fd;
- byte bad_msg[] = {
- 0x17, /* app data */
- 0xaa, 0xfd, /* bad version */
- 0x00, 0x01, /* epoch 1 */
- 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
- 0x00, 0x26, /* length: 38 bytes */
- 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
- 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
- 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
- 0x03, 0x18, 0x72
- };
- fd = wolfSSL_get_fd(ssl);
- AssertIntGE(fd, 0);
- ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
- AssertIntEQ(ret, sizeof(bad_msg));
- ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
- AssertIntEQ(ret, sizeof("badrecordtest"));
- }
- static void test_wolfSSL_read_string(WOLFSSL* ssl)
- {
- byte buf[100];
- int ret;
- ret = wolfSSL_read(ssl, buf, sizeof(buf));
- AssertIntGT(ret, 0);
- AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
- }
- static int _test_wolfSSL_dtls_bad_record(
- method_provider client_method, method_provider server_method)
- {
- callback_functions client_cbs, server_cbs;
- XMEMSET(&client_cbs, 0, sizeof(client_cbs));
- XMEMSET(&server_cbs, 0, sizeof(server_cbs));
- client_cbs.doUdp = server_cbs.doUdp = 1;
- client_cbs.method = client_method;
- server_cbs.method = server_method;
- client_cbs.on_result = test_wolfSSL_send_bad_record;
- server_cbs.on_result = test_wolfSSL_read_string;
- test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
- if (!client_cbs.return_code)
- return TEST_FAIL;
- if (!server_cbs.return_code)
- return TEST_FAIL;
- return TEST_SUCCESS;
- }
- static int test_wolfSSL_dtls_bad_record(void)
- {
- int ret;
- ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
- wolfDTLSv1_2_server_method);
- #ifdef WOLFSSL_DTLS13
- if (ret != TEST_SUCCESS)
- return ret;
- return _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
- wolfDTLSv1_3_server_method);
- #else
- return ret;
- #endif /* WOLFSSL_DTLS13 */
- }
- #else
- static int test_wolfSSL_dtls_fragments(void) {
- return TEST_SKIPPED;
- }
- static int test_wolfSSL_ignore_alert_before_cookie(void) {
- return TEST_SKIPPED;
- }
- static int test_wolfSSL_dtls_bad_record(void) {
- return TEST_SKIPPED;
- }
- #endif
- #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES)
- static byte test_AEAD_fail_decryption = 0;
- static byte test_AEAD_seq_num = 0;
- static byte test_AEAD_done = 0;
- static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
- {
- int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
- if (ret > 0) {
- if (test_AEAD_fail_decryption) {
- /* Modify the packet to trigger a decryption failure */
- buf[ret/2] ^= 0xFF;
- if (test_AEAD_fail_decryption == 1)
- test_AEAD_fail_decryption = 0;
- }
- }
- (void)ctx;
- return ret;
- }
- static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
- w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
- {
- if (sendLimit)
- w64Zero(sendLimit);
- switch (ssl->specs.bulk_cipher_algorithm) {
- case wolfssl_aes_gcm:
- if (sendLimit)
- *sendLimit = AEAD_AES_LIMIT;
- FALL_THROUGH;
- case wolfssl_chacha:
- if (hardLimit)
- *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
- if (keyUpdateLimit)
- *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
- break;
- case wolfssl_aes_ccm:
- if (sendLimit)
- *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
- if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
- if (hardLimit)
- *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
- if (keyUpdateLimit)
- *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
- }
- else {
- if (hardLimit)
- *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
- if (keyUpdateLimit)
- *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
- }
- break;
- default:
- fprintf(stderr, "Unrecognized bulk cipher");
- AssertFalse(1);
- break;
- }
- }
- static void test_AEAD_limit_client(WOLFSSL* ssl)
- {
- int ret;
- int i;
- int didReKey = 0;
- char msgBuf[20];
- w64wrapper hardLimit;
- w64wrapper keyUpdateLimit;
- w64wrapper counter;
- w64wrapper sendLimit;
- test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
- w64Zero(&counter);
- AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
- wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
- for (i = 0; i < 10; i++) {
- /* Test some failed decryptions */
- test_AEAD_fail_decryption = 1;
- w64Increment(&counter);
- ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
- /* Should succeed since decryption failures are dropped */
- AssertIntGT(ret, 0);
- AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
- }
- test_AEAD_fail_decryption = 1;
- Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
- w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
- /* 100 read calls should be enough to complete the key update */
- w64Zero(&counter);
- for (i = 0; i < 100; i++) {
- /* Key update should be sent and negotiated */
- ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
- AssertIntGT(ret, 0);
- /* Epoch after one key update is 4 */
- if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
- w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
- didReKey = 1;
- break;
- }
- }
- AssertTrue(didReKey);
- if (!w64IsZero(sendLimit)) {
- /* Test the sending limit for AEAD ciphers */
- Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
- test_AEAD_seq_num = 1;
- ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
- AssertIntGT(ret, 0);
- didReKey = 0;
- w64Zero(&counter);
- /* 100 read calls should be enough to complete the key update */
- for (i = 0; i < 100; i++) {
- /* Key update should be sent and negotiated */
- ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
- AssertIntGT(ret, 0);
- /* Epoch after another key update is 5 */
- if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
- w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
- didReKey = 1;
- break;
- }
- }
- AssertTrue(didReKey);
- }
- test_AEAD_fail_decryption = 2;
- Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
- w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
- /* Connection should fail with a DECRYPT_ERROR */
- ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
- AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
- AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
- test_AEAD_done = 1;
- }
- int counter = 0;
- static void test_AEAD_limit_server(WOLFSSL* ssl)
- {
- char msgBuf[] = "Sending data";
- int ret = WOLFSSL_SUCCESS;
- w64wrapper sendLimit;
- SOCKET_T fd = wolfSSL_get_fd(ssl);
- struct timespec delay;
- XMEMSET(&delay, 0, sizeof(delay));
- delay.tv_nsec = 100000000; /* wait 0.1 seconds */
- tcp_set_nonblocking(&fd); /* So that read doesn't block */
- test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
- while (!test_AEAD_done && ret > 0) {
- counter++;
- if (test_AEAD_seq_num) {
- /* We need to update the seq number so that we can understand the
- * peer. Otherwise we will incorrectly interpret the seq number. */
- Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
- AssertNotNull(e);
- e->nextPeerSeqNumber = sendLimit;
- test_AEAD_seq_num = 0;
- }
- (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
- ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
- nanosleep(&delay, NULL);
- }
- }
- static int test_wolfSSL_dtls_AEAD_limit(void)
- {
- callback_functions func_cb_client;
- callback_functions func_cb_server;
- XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
- XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
- func_cb_client.doUdp = func_cb_server.doUdp = 1;
- func_cb_server.method = wolfDTLSv1_3_server_method;
- func_cb_client.method = wolfDTLSv1_3_client_method;
- func_cb_server.on_result = test_AEAD_limit_server;
- func_cb_client.on_result = test_AEAD_limit_client;
- test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
- if (!func_cb_client.return_code)
- return TEST_FAIL;
- if (!func_cb_server.return_code)
- return TEST_FAIL;
- return TEST_SUCCESS;
- }
- #else
- static int test_wolfSSL_dtls_AEAD_limit(void)
- {
- return TEST_SKIPPED;
- }
- #endif
- #if defined(WOLFSSL_DTLS) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
- static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
- {
- int fd, ret;
- byte ch_msg[] = {
- 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
- 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
- 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
- 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
- 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
- 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
- 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
- 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
- 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
- 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
- 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
- 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
- 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
- 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
- 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
- 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
- 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
- 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
- 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
- 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
- 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
- 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
- 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
- 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
- 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
- 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
- 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
- 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
- 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
- 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
- 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
- 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
- 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
- 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
- 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
- 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
- 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
- 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
- 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
- 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
- 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
- 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
- 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
- 0x31, 0x68, 0x4c
- };
- fd = wolfSSL_get_fd(ssl);
- ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
- AssertIntGT(ret, 0);
- /* consume the HRR otherwise handshake will fail */
- ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
- AssertIntGT(ret, 0);
- }
- #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
- static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
- {
- int ret;
- ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- }
- #endif
- static int test_wolfSSL_dtls_stateless(void)
- {
- callback_functions client_cbs, server_cbs;
- size_t i;
- struct {
- method_provider client_meth;
- method_provider server_meth;
- ssl_callback client_ssl_ready;
- ssl_callback server_ssl_ready;
- } test_params[] = {
- {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
- test_wolfSSL_dtls_send_ch, NULL},
- #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
- {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
- test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
- #endif
- };
- for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
- XMEMSET(&client_cbs, 0, sizeof(client_cbs));
- XMEMSET(&server_cbs, 0, sizeof(server_cbs));
- client_cbs.doUdp = server_cbs.doUdp = 1;
- client_cbs.method = test_params[i].client_meth;
- server_cbs.method = test_params[i].server_meth;
- client_cbs.ssl_ready = test_params[i].client_ssl_ready;
- server_cbs.ssl_ready = test_params[i].server_ssl_ready;
- test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
- if (!client_cbs.return_code)
- return TEST_FAIL;
- if (!server_cbs.return_code)
- return TEST_FAIL;
- }
- return TEST_SUCCESS;
- }
- #else
- static int test_wolfSSL_dtls_stateless(void)
- {
- return TEST_SKIPPED;
- }
- #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
- * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
- #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
- !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
- !defined(WOLFSSL_NO_CLIENT_AUTH))
- static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
- {
- int ret;
- if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
- fprintf(stderr, "loading cert %s failed\n", certA);
- fprintf(stderr, "Error: (%d): %s\n", ret,
- wolfSSL_ERR_reason_error_string(ret));
- return -1;
- }
- return 0;
- }
- static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
- {
- int ret;
- if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
- != WOLFSSL_SUCCESS) {
- fprintf(stderr, "could not verify the cert: %s\n", certA);
- fprintf(stderr, "Error: (%d): %s\n", ret,
- wolfSSL_ERR_reason_error_string(ret));
- return -1;
- }
- else {
- fprintf(stderr, "successfully verified: %s\n", certA);
- }
- return 0;
- }
- #define LOAD_ONE_CA(a, b, c, d) \
- do { \
- (a) = load_ca_into_cm(c, d); \
- if ((a) != 0) \
- return (b); \
- else \
- (b)--; \
- } while(0)
- #define VERIFY_ONE_CERT(a, b, c, d) \
- do { \
- (a) = verify_cert_with_cm(c, d); \
- if ((a) != 0) \
- return (b); \
- else \
- (b)--; \
- } while(0)
- static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
- {
- int ret;
- int i = -1;
- /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
- char chainGArr[9][50] = {"certs/ca-cert.pem",
- "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
- "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
- "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
- "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
- "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
- "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
- "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
- "certs/test-pathlen/chainG-entity.pem"};
- LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
- LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
- LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
- LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
- LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
- LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
- LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
- LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
- VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
- VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
- VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
- VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
- VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
- VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
- VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
- VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
- /* test validating the entity twice, should have no effect on pathLen since
- * entity/leaf cert */
- VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
- return ret;
- }
- static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
- {
- int ret;
- int i = -1;
- /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
- * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
- * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
- * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
- * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
- * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
- */
- char chainHArr[6][50] = {"certs/ca-cert.pem",
- "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
- "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
- "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
- "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
- "certs/test-pathlen/chainH-entity.pem"};
- LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
- LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
- LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
- LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
- LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
- VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
- VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
- VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
- VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
- VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
- return ret;
- }
- static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
- {
- int ret;
- int i = -1;
- /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
- * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
- * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
- * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
- * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
- */
- char chainIArr[5][50] = {"certs/ca-cert.pem",
- "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
- "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
- "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
- "certs/test-pathlen/chainI-entity.pem"};
- LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
- LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
- LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
- LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
- VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
- VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
- VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
- VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
- return ret;
- }
- static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
- {
- int ret;
- int i = -1;
- /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
- * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
- * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
- * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
- * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
- */
- char chainJArr[6][50] = {"certs/ca-cert.pem",
- "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
- "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
- "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
- "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
- "certs/test-pathlen/chainJ-entity.pem"};
- LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
- LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
- LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
- LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
- LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
- VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
- VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
- VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
- VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
- VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
- return ret;
- }
- static int test_various_pathlen_chains(void)
- {
- int ret;
- WOLFSSL_CERT_MANAGER* cm;
- /* Test chain G (large chain with varying pathLens) */
- if ((cm = wolfSSL_CertManagerNew()) == NULL) {
- fprintf(stderr, "cert manager new failed\n");
- return -1;
- }
- #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
- AssertIntEQ(test_chainG(cm), -1);
- #else
- AssertIntEQ(test_chainG(cm), 0);
- #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
- ret = wolfSSL_CertManagerUnloadCAs(cm);
- if (ret != WOLFSSL_SUCCESS)
- return -1;
- wolfSSL_CertManagerFree(cm);
- /* end test chain G */
- /* Test chain H (5 chain with same pathLens) */
- if ((cm = wolfSSL_CertManagerNew()) == NULL) {
- fprintf(stderr, "cert manager new failed\n");
- return -1;
- }
- AssertIntLT(test_chainH(cm), 0);
- wolfSSL_CertManagerUnloadCAs(cm);
- wolfSSL_CertManagerFree(cm);
- if ((cm = wolfSSL_CertManagerNew()) == NULL) {
- fprintf(stderr, "cert manager new failed\n");
- return -1;
- }
- ret = wolfSSL_CertManagerUnloadCAs(cm);
- if (ret != WOLFSSL_SUCCESS)
- return -1;
- wolfSSL_CertManagerFree(cm);
- /* end test chain H */
- /* Test chain I (only first ICA has pathLen set and it's set to 2,
- * followed by 2 ICA's, should pass) */
- if ((cm = wolfSSL_CertManagerNew()) == NULL) {
- fprintf(stderr, "cert manager new failed\n");
- return -1;
- }
- #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
- AssertIntEQ(test_chainI(cm), -1);
- #else
- AssertIntEQ(test_chainI(cm), 0);
- #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
- wolfSSL_CertManagerUnloadCAs(cm);
- wolfSSL_CertManagerFree(cm);
- if ((cm = wolfSSL_CertManagerNew()) == NULL) {
- fprintf(stderr, "cert manager new failed\n");
- return -1;
- }
- ret = wolfSSL_CertManagerUnloadCAs(cm);
- if (ret != WOLFSSL_SUCCESS)
- return -1;
- wolfSSL_CertManagerFree(cm);
- /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
- * this time followed by 3 ICA's, should fail */
- if ((cm = wolfSSL_CertManagerNew()) == NULL) {
- fprintf(stderr, "cert manager new failed\n");
- return -1;
- }
- AssertIntLT(test_chainJ(cm), 0);
- wolfSSL_CertManagerUnloadCAs(cm);
- wolfSSL_CertManagerFree(cm);
- if ((cm = wolfSSL_CertManagerNew()) == NULL) {
- fprintf(stderr, "cert manager new failed\n");
- return -1;
- }
- ret = wolfSSL_CertManagerUnloadCAs(cm);
- wolfSSL_CertManagerFree(cm);
- return TEST_RES_CHECK(ret == WOLFSSL_SUCCESS);
- }
- #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
- #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
- {
- byte ekm[100] = {0};
- (void)ctx;
- /* Succes Cases */
- AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
- "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
- AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
- "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
- /* Use some random context */
- AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
- "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
- /* Failure cases */
- AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
- "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
- AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
- "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
- AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
- "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
- AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
- "extended master secret", XSTR_SIZEOF("extended master secret"), NULL, 0, 0), 0);
- AssertIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
- "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
- return TEST_RES_CHECK(1);
- }
- static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
- {
- wolfSSL_KeepArrays(ssl);
- }
- static int test_export_keying_material(void)
- {
- int res = TEST_SKIPPED;
- #ifndef SINGLE_THREADED
- tcp_ready ready;
- callback_functions clientCb;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&clientCb, 0, sizeof(callback_functions));
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- server_args.signal = &ready;
- client_args.signal = &ready;
- clientCb.ssl_ready = test_export_keying_material_ssl_cb;
- client_args.callbacks = &clientCb;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, test_export_keying_material_cb);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- res = TEST_RES_CHECK(1);
- #endif /* !SINGLE_THREADED */
- return res;
- }
- #endif /* HAVE_KEYING_MATERIAL */
- static int test_wolfSSL_THREADID_hash(void)
- {
- int result = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- unsigned long res;
- CRYPTO_THREADID id;
- CRYPTO_THREADID_current(NULL);
- AssertTrue(1);
- res = CRYPTO_THREADID_hash(NULL);
- AssertTrue( res == 0UL);
- XMEMSET(&id, 0, sizeof(id));
- res = CRYPTO_THREADID_hash(&id);
- AssertTrue( res == 0UL);
- result = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return result;
- }
- static int test_wolfSSL_CTX_set_ecdh_auto(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- WOLFSSL_CTX* ctx = NULL;
- AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,0),1);
- AssertIntEQ( SSL_CTX_set_ecdh_auto(NULL,1),1);
- AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,0),1);
- AssertIntEQ( SSL_CTX_set_ecdh_auto(ctx,1),1);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
- static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
- {
- callback_functions* callbacks = NULL;
- WOLFSSL_CTX* ctx = NULL;
- WOLFSSL* ssl = NULL;
- SOCKET_T sfd = 0;
- SOCKET_T cfd = 0;
- word16 port;
- char msg[] = "I hear you fa shizzle!";
- int len = (int) XSTRLEN(msg);
- char input[1024];
- int ret, err;
- if (!args)
- return 0;
- ((func_args*)args)->return_code = TEST_FAIL;
- callbacks = ((func_args*)args)->callbacks;
- ctx = wolfSSL_CTX_new(callbacks->method());
- #if defined(USE_WINDOWS_API)
- port = ((func_args*)args)->signal->port;
- #else
- /* Let tcp_listen assign port */
- port = 0;
- #endif
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
- AssertIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- #elif !defined(NO_DH)
- SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
- #endif
- if (callbacks->ctx_ready)
- callbacks->ctx_ready(ctx);
- ssl = wolfSSL_new(ctx);
- AssertNotNull(ssl);
- /* listen and accept */
- tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
- CloseSocket(sfd);
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
- if (callbacks->ssl_ready)
- callbacks->ssl_ready(ssl);
- do {
- err = 0; /* Reset error */
- ret = wolfSSL_accept(ssl);
- if (ret != WOLFSSL_SUCCESS) {
- err = wolfSSL_get_error(ssl, 0);
- }
- } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
- if (ret != WOLFSSL_SUCCESS) {
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(cfd);
- ((func_args*)args)->return_code = TEST_FAIL;
- return 0;
- }
- /* read and write data */
- XMEMSET( input, 0, sizeof(input));
- while (1) {
- ret = wolfSSL_read(ssl, input, sizeof(input));
- if (ret > 0) {
- break;
- }
- else {
- err = wolfSSL_get_error(ssl,ret);
- if (err == WOLFSSL_ERROR_WANT_READ) {
- continue;
- }
- break;
- }
- }
- if (err == WOLFSSL_ERROR_ZERO_RETURN) {
- do {
- ret = wolfSSL_write(ssl, msg, len);
- if (ret > 0) {
- break;
- }
- } while (ret < 0);
- }
- /* bidirectional shutdown */
- while (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
- continue;
- }
- /* wait for the peer to disconnect the tcp connection */
- do {
- ret = wolfSSL_read(ssl, input, sizeof(input));
- err = wolfSSL_get_error(ssl, ret);
- } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
- /* detect TCP disconnect */
- AssertIntLE(ret,WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
- ((func_args*)args)->return_code = TEST_SUCCESS;
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(cfd);
- #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- return 0;
- }
- static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
- {
- callback_functions* callbacks = NULL;
- WOLFSSL_CTX* ctx = NULL;
- WOLFSSL* ssl = NULL;
- SOCKET_T sfd = 0;
- char msg[] = "hello wolfssl server!";
- int len = (int) XSTRLEN(msg);
- char input[1024];
- int idx;
- int ret, err;
- if (!args)
- return 0;
- ((func_args*)args)->return_code = TEST_FAIL;
- callbacks = ((func_args*)args)->callbacks;
- ctx = wolfSSL_CTX_new(callbacks->method());
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(WOLFSSL_SUCCESS,
- wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull((ssl = wolfSSL_new(ctx)));
- tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
- do {
- err = 0; /* Reset error */
- ret = wolfSSL_connect(ssl);
- if (ret != WOLFSSL_SUCCESS) {
- err = wolfSSL_get_error(ssl, 0);
- }
- } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
- AssertIntGE(wolfSSL_write(ssl, msg, len), 0);
- if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
- input[idx] = 0;
- }
- ret = wolfSSL_shutdown(ssl);
- if ( ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
- ret = wolfSSL_shutdown(ssl);
- }
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- ((func_args*)args)->return_code = TEST_SUCCESS;
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- CloseSocket(sfd);
- #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- return 0;
- }
- #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
- HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
- /* This test is to check wolfSSL_read behaves as same as
- * openSSL when it is called after SSL_shutdown completes.
- */
- static int test_wolfSSL_read_detect_TCP_disconnect(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- THREAD_TYPE serverThread;
- THREAD_TYPE clientThread;
- callback_functions server_cbf;
- callback_functions client_cbf;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- StartTCP();
- InitTcpReady(&ready);
- #if defined(USE_WINDOWS_API)
- /* use RNG to get random port if using windows */
- ready.port = GetRandomPort();
- #endif
- XMEMSET(&client_args, 0, sizeof(func_args));
- XMEMSET(&server_args, 0, sizeof(func_args));
- XMEMSET(&server_cbf, 0, sizeof(callback_functions));
- XMEMSET(&client_cbf, 0, sizeof(callback_functions));
- server_cbf.method = wolfTLSv1_2_server_method;
- client_cbf.method = wolfTLSv1_2_client_method;
- server_args.callbacks = &server_cbf;
- client_args.callbacks = &client_cbf;
- server_args.signal = &ready;
- client_args.signal = &ready;
- start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
- join_thread(clientThread);
- join_thread(serverThread);
- AssertTrue(client_args.return_code);
- AssertTrue(server_args.return_code);
- FreeTcpReady(&ready);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_get_min_proto_version(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
- WOLFSSL_CTX *ctx;
- (void)ctx;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ALLOW_SSLV3
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
- #else
- AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
- #endif
- wolfSSL_CTX_free(ctx);
- #ifdef WOLFSSL_ALLOW_TLSV10
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
- #endif
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
- #ifdef WOLFSSL_ALLOW_TLSV10
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
- #else
- AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
- #endif
- wolfSSL_CTX_free(ctx);
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
- #ifndef NO_OLD_TLS
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
- #else
- AssertIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
- #endif
- wolfSSL_CTX_free(ctx);
- #ifndef WOLFSSL_NO_TLS12
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
- wolfSSL_CTX_free(ctx);
- #endif
- #ifdef WOLFSSL_TLS13
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
- wolfSSL_CTX_free(ctx);
- #endif
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
- return res;
- }
- #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
- (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
- defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
- defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
- static int test_wolfSSL_set_SSL_CTX(void)
- {
- int res = TEST_SKIPPED;
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
- && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13)
- WOLFSSL_CTX *ctx1, *ctx2;
- WOLFSSL *ssl;
- const byte *session_id1 = (const byte *)"CTX1";
- const byte *session_id2 = (const byte *)"CTX2";
- AssertNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
- AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
- AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
- WOLFSSL_SUCCESS);
- AssertNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
- WOLFSSL_FILETYPE_PEM));
- AssertTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
- WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
- AssertIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
- AssertIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
- WOLFSSL_SUCCESS);
- #ifdef HAVE_SESSION_TICKET
- AssertIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
- wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
- AssertIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
- #endif
- AssertNotNull(ssl = wolfSSL_new(ctx2));
- AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
- #ifdef WOLFSSL_INT_H
- AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
- AssertTrue(ssl->buffers.certificate == ctx2->certificate);
- AssertTrue(ssl->buffers.certChain == ctx2->certChain);
- #endif
- #ifdef HAVE_SESSION_TICKET
- AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
- #endif
- /* Set the ctx1 that has TLSv1.3 as max proto version */
- AssertNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
- /* MUST not change proto versions of ssl */
- AssertIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
- #ifdef HAVE_SESSION_TICKET
- /* MUST not change */
- AssertIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
- #endif
- /* MUST change */
- #ifdef WOLFSSL_INT_H
- AssertTrue(ssl->buffers.certificate == ctx1->certificate);
- AssertTrue(ssl->buffers.certChain == ctx1->certChain);
- AssertIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
- #endif
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx1);
- wolfSSL_CTX_free(ctx2);
- res = TEST_RES_CHECK(1);
- #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
- return res;
- }
- #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
- (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
- defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
- defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
- static int test_wolfSSL_security_level(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- SSL_CTX *ctx;
- #ifdef WOLFSSL_TLS13
- #ifdef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
- #endif
- SSL_CTX_set_security_level(ctx, 1);
- AssertTrue(1);
- AssertIntEQ(SSL_CTX_get_security_level(ctx), 0);
- SSL_CTX_free(ctx);
- #else
- (void)ctx;
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_SSL_in_init(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_BIO)
- SSL_CTX* ctx;
- SSL* ssl;
- const char* testCertFile;
- const char* testKeyFile;
- #ifdef WOLFSSL_TLS13
- #ifdef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
- #endif
- #ifndef NO_RSA
- testCertFile = svrCertFile;
- testKeyFile = svrKeyFile;
- #elif defined(HAVE_ECC)
- testCertFile = eccCertFile;
- testKeyFile = eccKeyFile;
- #else
- testCertFile = NULL;
- testKeyFile = NULL;
- #endif
- if (testCertFile != NULL && testKeyFile != NULL) {
- AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
- SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
- SSL_FILETYPE_PEM));
- }
- ssl = SSL_new(ctx);
- AssertNotNull(ssl);
- AssertIntEQ(SSL_in_init(ssl), 1);
- SSL_CTX_free(ctx);
- SSL_free(ssl);
- #else
- (void)ctx;
- (void)ssl;
- (void)testCertFile;
- (void)testKeyFile;
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EC_curve(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
- int nid = NID_secp160k1;
- const char* nid_name;
- AssertNotNull(nid_name = EC_curve_nid2nist(nid));
- AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
- AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_CTX_set_timeout(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
- int timeout;
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- (void)timeout;
- AssertNotNull(ctx);
- #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
- /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
- * wolfSSL_CTX_set_timeout returns previous timeout value on success.
- */
- AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
- /* giving 0 as timeout value sets default timeout */
- timeout = wolfSSL_CTX_set_timeout(ctx, 0);
- AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
- AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
- #else
- AssertIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
- AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
- AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
- #endif
- wolfSSL_CTX_free(ctx);
- res = TEST_RES_CHECK(1);
- #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
- return res;
- }
- static int test_wolfSSL_OpenSSL_version(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- const char* ver;
- #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
- AssertNotNull(ver = OpenSSL_version(0));
- #else
- AssertNotNull(ver = OpenSSL_version());
- #endif
- AssertIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
- XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_CONF_CTX_CMDLINE(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL)
- SSL_CTX* ctx = NULL;
- SSL_CONF_CTX* cctx = NULL;
- AssertNotNull(cctx = SSL_CONF_CTX_new());
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
- AssertTrue(1);
- /* set flags */
- AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
- WOLFSSL_CONF_FLAG_CMDLINE);
- AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
- WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
- /* cmd invalid command */
- AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
- AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
- AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
- AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
- /* cmd Certificate and Private Key*/
- {
- #if !defined(NO_CERTS) && !defined(NO_RSA)
- const char* ourCert = svrCertFile;
- const char* ourKey = svrKeyFile;
- AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert),
- WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
- #endif
- }
- /* cmd curves */
- {
- #if defined(HAVE_ECC)
- const char* curve = "secp256r1";
- AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
- #endif
- }
- /* cmd CipherString */
- {
- char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
- AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
- }
- /* cmd DH parameter */
- {
- #if !defined(NO_DH) && !defined(NO_BIO)
- const char* ourdhcert = "./certs/dh2048.pem";
- AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL),
- -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert),
- WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
- #endif
- }
- SSL_CTX_free(ctx);
- SSL_CONF_CTX_free(cctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_CONF_CTX_FILE(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL)
- SSL_CTX* ctx = NULL;
- SSL_CONF_CTX* cctx = NULL;
- AssertNotNull(cctx = SSL_CONF_CTX_new());
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
- AssertTrue(1);
- /* set flags */
- AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
- WOLFSSL_CONF_FLAG_FILE);
- AssertIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
- WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
- /* sanity check */
- AssertIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
- AssertIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
- AssertIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
- AssertIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
- /* cmd Certificate and Private Key*/
- {
- #if !defined(NO_CERTS) && !defined(NO_RSA)
- const char* ourCert = svrCertFile;
- const char* ourKey = svrKeyFile;
- AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
- WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
- #endif
- }
- /* cmd curves */
- {
- #if defined(HAVE_ECC)
- const char* curve = "secp256r1";
- AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
- #endif
- }
- /* cmd CipherString */
- {
- char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
- AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher), WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
- }
- /* cmd DH parameter */
- {
- #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
- const char* ourdhcert = "./certs/dh3072.pem";
- AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
- AssertIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
- WOLFSSL_SUCCESS);
- AssertIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
- #endif
- }
- SSL_CTX_free(ctx);
- SSL_CONF_CTX_free(cctx);
- res = TEST_RES_CHECK(1);
- #endif /* OPENSSL_EXTRA */
- return res;
- }
- static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
- {
- int res = TEST_SKIPPED;
- #ifdef HAVE_EX_DATA
- int idx1, idx2;
- /* test for unsupported class index */
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
- 0,NULL, NULL, NULL, NULL ), -1);
- AssertIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20, 0,NULL, NULL, NULL, NULL ), -1);
- /* test for supported class index */
- idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
- 0,NULL, NULL, NULL, NULL );
- idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
- 0,NULL, NULL, NULL, NULL );
- AssertIntNE(idx1, -1);
- AssertIntNE(idx2, -1);
- AssertIntNE(idx1, idx2);
- idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
- 0,NULL, NULL, NULL, NULL );
- idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
- 0,NULL, NULL, NULL, NULL );
- AssertIntNE(idx1, -1);
- AssertIntNE(idx2, -1);
- AssertIntNE(idx1, idx2);
- idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
- 0,NULL, NULL, NULL, NULL );
- idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
- 0,NULL, NULL, NULL, NULL );
- AssertIntNE(idx1, -1);
- AssertIntNE(idx2, -1);
- AssertIntNE(idx1, idx2);
- idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
- 0,NULL, NULL, NULL, NULL );
- idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
- 0,NULL, NULL, NULL, NULL );
- AssertIntNE(idx1, -1);
- AssertIntNE(idx2, -1);
- AssertIntNE(idx1, idx2);
- res = TEST_RES_CHECK(1);
- #endif /* HAVE_EX_DATA */
- return res;
- }
- static int test_wolfSSL_set_psk_use_session_callback(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
- SSL_CTX* ctx;
- SSL* ssl;
- const char* testCertFile;
- const char* testKeyFile;
- #ifdef WOLFSSL_TLS13
- #ifdef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
- #endif
- #ifndef NO_RSA
- testCertFile = svrCertFile;
- testKeyFile = svrKeyFile;
- #elif defined(HAVE_ECC)
- testCertFile = eccCertFile;
- testKeyFile = eccKeyFile;
- #else
- testCertFile = NULL;
- testKeyFile = NULL;
- #endif
- if (testCertFile != NULL && testKeyFile != NULL) {
- AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
- SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
- SSL_FILETYPE_PEM));
- }
- ssl = SSL_new(ctx);
- AssertNotNull(ssl);
- SSL_set_psk_use_session_callback(ssl,
- my_psk_use_session_cb);
- AssertTrue(1);
- SSL_CTX_free(ctx);
- SSL_free(ssl);
- #else
- (void)ctx;
- (void)ssl;
- (void)testCertFile;
- (void)testKeyFile;
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_ERR_strings(void)
- {
- int res = TEST_SKIPPED;
- #if !defined(NO_ERROR_STRINGS)
- const char* err1 = "unsupported cipher suite";
- const char* err2 = "wolfSSL PEM routines";
- const char* err = NULL;
- (void)err;
- (void)err1;
- (void)err2;
- #if defined(OPENSSL_EXTRA)
- err = ERR_reason_error_string(UNSUPPORTED_SUITE);
- AssertTrue(err != NULL);
- AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
- err = ERR_func_error_string(UNSUPPORTED_SUITE);
- AssertTrue(err != NULL);
- AssertIntEQ((*err == '\0'), 1);
- err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD);
- AssertTrue(err != NULL);
- AssertIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
- #else
- err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE);
- AssertTrue(err != NULL);
- AssertIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
- err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE);
- AssertTrue(err != NULL);
- AssertIntEQ((*err == '\0'), 1);
- /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
- err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2);
- AssertTrue(err != NULL);
- AssertIntEQ((*err == '\0'), 1);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_shake128(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
- defined(WOLFSSL_SHAKE128)
- const EVP_MD* md = NULL;
- md = EVP_shake128();
- AssertTrue(md != NULL);
- AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_EVP_shake256(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
- defined(WOLFSSL_SHAKE256)
- const EVP_MD* md = NULL;
- md = EVP_shake256();
- AssertTrue(md != NULL);
- AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_EVP_blake2(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
- const EVP_MD* md = NULL;
- (void)md;
- #if defined(HAVE_BLAKE2)
- md = EVP_blake2b512();
- AssertTrue(md != NULL);
- AssertIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
- #endif
- #if defined(HAVE_BLAKE2S)
- md = EVP_blake2s256();
- AssertTrue(md != NULL);
- AssertIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_EXTRA)
- static void list_md_fn(const EVP_MD* m, const char* from,
- const char* to, void* arg)
- {
- const char* mn;
- BIO *bio;
- (void) from;
- (void) to;
- (void) arg;
- (void) mn;
- (void) bio;
- if (!m) {
- /* alias */
- AssertNull(m);
- AssertNotNull(to);
- }
- else {
- AssertNotNull(m);
- AssertNull(to);
- }
- AssertNotNull(from);
- #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
- mn = EVP_get_digestbyname(from);
- /* print to stderr */
- AssertNotNull(arg);
- bio = BIO_new(BIO_s_file());
- BIO_set_fp(bio, arg, BIO_NOCLOSE);
- BIO_printf(bio, "Use %s message digest algorithm\n", mn);
- BIO_free(bio);
- #endif
- }
- #endif
- static int test_EVP_MD_do_all(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- EVP_MD_do_all(NULL, stderr);
- /* to confirm previous call gives no harm */
- AssertTrue(1);
- EVP_MD_do_all(list_md_fn, stderr);
- /* to confirm previous call gives no harm */
- AssertTrue(1);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(OPENSSL_EXTRA)
- static void obj_name_t(const OBJ_NAME* nm, void* arg)
- {
- (void)arg;
- (void)nm;
- AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
- #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
- /* print to stderr */
- AssertNotNull(arg);
- bio = BIO_new(BIO_s_file());
- BIO_set_fp(bio, arg, BIO_NOCLOSE);
- BIO_printf(bio, "%s\n", mn);
- BIO_free(bio);
- #endif
- }
- #endif
- static int test_OBJ_NAME_do_all(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA)
- OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
- /* to confirm previous call gives no harm */
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
- /* to confirm previous call gives no harm */
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
- AssertTrue(1);
- OBJ_NAME_do_all(-1, obj_name_t, stderr);
- AssertTrue(1);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_SSL_CIPHER_get_xxx(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM)
- const SSL_CIPHER* cipher = NULL;
- STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
- int i, numCiphers = 0;
- SSL_CTX* ctx = NULL;
- SSL* ssl = NULL;
- const char* testCertFile;
- const char* testKeyFile;
- char buf[256] = {0};
- const char* cipher_id = NULL;
- int expect_nid1 = NID_undef;
- int expect_nid2 = NID_undef;
- int expect_nid3 = NID_undef;
- int expect_nid4 = NID_undef;
- int expect_nid5 = 0;
- const char* cipher_id2 = NULL;
- int expect_nid21 = NID_undef;
- int expect_nid22 = NID_undef;
- int expect_nid23 = NID_undef;
- int expect_nid24 = NID_undef;
- int expect_nid25 = 0;
- (void)cipher;
- (void)supportedCiphers;
- (void)i;
- (void)numCiphers;
- (void)ctx;
- (void)ssl;
- (void)testCertFile;
- (void)testKeyFile;
- #if defined(WOLFSSL_TLS13)
- cipher_id = "TLS13-AES128-GCM-SHA256";
- expect_nid1 = NID_auth_rsa;
- expect_nid2 = NID_aes_128_gcm;
- expect_nid3 = NID_sha256;
- expect_nid4 = NID_kx_any;
- expect_nid5 = 1;
- #if !defined(WOLFSSL_NO_TLS12)
- cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
- expect_nid21 = NID_auth_rsa;
- expect_nid22 = NID_aes_256_gcm;
- expect_nid23 = NID_sha384;
- expect_nid24 = NID_kx_ecdhe;
- expect_nid25 = 1;
- #endif
- #endif
- #ifdef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #endif
- if (cipher_id) {
- #ifndef NO_RSA
- testCertFile = svrCertFile;
- testKeyFile = svrKeyFile;
- #elif defined(HAVE_ECC)
- testCertFile = eccCertFile;
- testKeyFile = eccKeyFile;
- #else
- testCertFile = NULL;
- testKeyFile = NULL;
- #endif
- if (testCertFile != NULL && testKeyFile != NULL) {
- AssertTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
- SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
- SSL_FILETYPE_PEM));
- }
- ssl = SSL_new(ctx);
- AssertNotNull(ssl);
- AssertIntEQ(SSL_in_init(ssl), 1);
- supportedCiphers = SSL_get_ciphers(ssl);
- numCiphers = sk_num(supportedCiphers);
- for (i = 0; i < numCiphers; ++i) {
- if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
- SSL_CIPHER_description(cipher, buf, sizeof(buf));
- }
- if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
- break;
- }
- }
- /* test case for */
- if (i != numCiphers) {
- AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
- AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
- AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
- AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
- AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
- }
- if (cipher_id2) {
- for (i = 0; i < numCiphers; ++i) {
- if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
- SSL_CIPHER_description(cipher, buf, sizeof(buf));
- }
- if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
- break;
- }
- }
- /* test case for */
- if (i != numCiphers) {
- AssertIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
- AssertIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
- AssertIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
- AssertIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
- AssertIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
- }
- }
- }
- if (ctx)
- SSL_CTX_free(ctx);
- if (ssl)
- SSL_free(ssl);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
- int* keyFormat)
- {
- int ret;
- byte* key_buf = NULL;
- size_t key_sz = 0;
- EncryptedInfo encInfo;
- XMEMSET(&encInfo, 0, sizeof(encInfo));
- ret = load_file(privKeyFile, &key_buf, &key_sz);
- if (ret == 0) {
- ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
- NULL, &encInfo, keyFormat);
- }
- if (key_buf != NULL) {
- free(key_buf); key_buf = NULL;
- }
- (void)encInfo; /* not used in this test */
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
- (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
- (*pDer)->length);
- #endif
- return ret;
- }
- static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
- {
- int ret = CRYPTOCB_UNAVAILABLE;
- const char* privKeyFile = (const char*)ctx;
- DerBuffer* pDer = NULL;
- int keyFormat = 0;
- if (info->algo_type == WC_ALGO_TYPE_PK) {
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
- #endif
- #ifndef NO_RSA
- if (info->pk.type == WC_PK_TYPE_RSA) {
- switch (info->pk.rsa.type) {
- case RSA_PUBLIC_ENCRYPT:
- case RSA_PUBLIC_DECRYPT:
- /* perform software based RSA public op */
- ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
- break;
- case RSA_PRIVATE_ENCRYPT:
- case RSA_PRIVATE_DECRYPT:
- {
- RsaKey key;
- /* perform software based RSA private op */
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
- #endif
- ret = load_pem_key_file_as_der(privKeyFile, &pDer,
- &keyFormat);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- word32 keyIdx = 0;
- /* load RSA private key and perform private transform */
- ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
- &key, pDer->length);
- if (ret == 0) {
- ret = wc_RsaFunction(
- info->pk.rsa.in, info->pk.rsa.inLen,
- info->pk.rsa.out, info->pk.rsa.outLen,
- info->pk.rsa.type, &key, info->pk.rsa.rng);
- }
- else {
- /* if decode fails, then fall-back to software based crypto */
- fprintf(stderr, "test_CryptoCb_Func: RSA private "
- "key decode failed %d, falling back to "
- "software\n", ret);
- ret = CRYPTOCB_UNAVAILABLE;
- }
- wc_FreeRsaKey(&key);
- }
- wc_FreeDer(&pDer); pDer = NULL;
- break;
- }
- }
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
- info->pk.rsa.type, ret, *info->pk.rsa.outLen);
- #endif
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
- /* mark this key as ephemeral */
- if (info->pk.eckg.key != NULL) {
- XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
- sizeof(info->pk.eckg.key->label));
- info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
- }
- }
- else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
- ecc_key key;
- /* perform software based ECC sign */
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
- #endif
- if (info->pk.eccsign.key != NULL &&
- XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
- /* this is an empheral key */
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
- "ephemeral key\n");
- #endif
- return CRYPTOCB_UNAVAILABLE;
- }
- ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
- if (ret != 0) {
- return ret;
- }
- ret = wc_ecc_init(&key);
- if (ret == 0) {
- word32 keyIdx = 0;
- /* load ECC private key and perform private transform */
- ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
- &key, pDer->length);
- if (ret == 0) {
- ret = wc_ecc_sign_hash(
- info->pk.eccsign.in, info->pk.eccsign.inlen,
- info->pk.eccsign.out, info->pk.eccsign.outlen,
- info->pk.eccsign.rng, &key);
- }
- else {
- /* if decode fails, then fall-back to software based crypto */
- fprintf(stderr, "test_CryptoCb_Func: ECC private key "
- "decode failed %d, falling back to software\n", ret);
- ret = CRYPTOCB_UNAVAILABLE;
- }
- wc_ecc_free(&key);
- }
- wc_FreeDer(&pDer); pDer = NULL;
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
- ret, *info->pk.eccsign.outlen);
- #endif
- }
- #endif /* HAVE_ECC */
- #ifdef HAVE_ED25519
- if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
- ed25519_key key;
- /* perform software based ED25519 sign */
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
- #endif
- ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
- if (ret != 0) {
- return ret;
- }
- ret = wc_ed25519_init(&key);
- if (ret == 0) {
- word32 keyIdx = 0;
- /* load ED25519 private key and perform private transform */
- ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
- &key, pDer->length);
- if (ret == 0) {
- /* calculate public key */
- ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
- if (ret == 0) {
- key.pubKeySet = 1;
- ret = wc_ed25519_sign_msg_ex(
- info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
- info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
- &key, info->pk.ed25519sign.type,
- info->pk.ed25519sign.context,
- info->pk.ed25519sign.contextLen);
- }
- }
- else {
- /* if decode fails, then fall-back to software based crypto */
- fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
- "decode failed %d, falling back to software\n", ret);
- ret = CRYPTOCB_UNAVAILABLE;
- }
- wc_ed25519_free(&key);
- }
- wc_FreeDer(&pDer); pDer = NULL;
- #ifdef DEBUG_WOLFSSL
- fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
- ret, *info->pk.ed25519sign.outLen);
- #endif
- }
- #endif /* HAVE_ED25519 */
- }
- (void)thisDevId;
- (void)keyFormat;
- return ret;
- }
- /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
- static void test_wc_CryptoCb_TLS(int tlsVer,
- const char* cliCaPemFile, const char* cliCertPemFile,
- const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
- const char* svrCaPemFile, const char* svrCertPemFile,
- const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
- {
- callback_functions client_cbf;
- callback_functions server_cbf;
- XMEMSET(&client_cbf, 0, sizeof(client_cbf));
- XMEMSET(&server_cbf, 0, sizeof(server_cbf));
- if (tlsVer == WOLFSSL_TLSV1_3) {
- #ifdef WOLFSSL_TLS13
- server_cbf.method = wolfTLSv1_3_server_method;
- client_cbf.method = wolfTLSv1_3_client_method;
- #endif
- }
- else if (tlsVer == WOLFSSL_TLSV1_2) {
- #ifndef WOLFSSL_NO_TLS12
- server_cbf.method = wolfTLSv1_2_server_method;
- client_cbf.method = wolfTLSv1_2_client_method;
- #endif
- }
- else if (tlsVer == WOLFSSL_TLSV1_1) {
- #ifndef NO_OLD_TLS
- server_cbf.method = wolfTLSv1_1_server_method;
- client_cbf.method = wolfTLSv1_1_client_method;
- #endif
- }
- else if (tlsVer == WOLFSSL_TLSV1) {
- #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
- server_cbf.method = wolfTLSv1_server_method;
- client_cbf.method = wolfTLSv1_client_method;
- #endif
- }
- else if (tlsVer == WOLFSSL_SSLV3) {
- #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
- defined(WOLFSSL_STATIC_RSA)
- server_cbf.method = wolfSSLv3_server_method;
- client_cbf.method = wolfSSLv3_client_method;
- #endif
- }
- else if (tlsVer == WOLFSSL_DTLSV1_2) {
- #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
- server_cbf.method = wolfDTLSv1_2_server_method;
- client_cbf.method = wolfDTLSv1_2_client_method;
- #endif
- }
- else if (tlsVer == WOLFSSL_DTLSV1) {
- #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
- server_cbf.method = wolfDTLSv1_server_method;
- client_cbf.method = wolfDTLSv1_client_method;
- #endif
- }
- if (server_cbf.method == NULL) {
- /* not enabled */
- return;
- }
- /* Setup the keys for the TLS test */
- client_cbf.certPemFile = cliCertPemFile;
- client_cbf.keyPemFile = cliPubKeyPemFile;
- client_cbf.caPemFile = cliCaPemFile;
- server_cbf.certPemFile = svrCertPemFile;
- server_cbf.keyPemFile = svrPubKeyPemFile;
- server_cbf.caPemFile = svrCaPemFile;
- /* Setup a crypto callback with pointer to private key file for testing */
- client_cbf.devId = 1;
- wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
- (void*)cliPrivKeyPemFile);
- server_cbf.devId = 2;
- wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
- (void*)svrPrivKeyPemFile);
- /* Perform TLS server and client test */
- /* First test is at WOLFSSL_CTX level */
- test_wolfSSL_client_server(&client_cbf, &server_cbf);
- /* Check for success */
- AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
- AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
- /* Second test is a WOLFSSL object level */
- client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
- test_wolfSSL_client_server(&client_cbf, &server_cbf);
- /* Check for success */
- AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
- AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
- /* Un register the devId's */
- wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
- client_cbf.devId = INVALID_DEVID;
- wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
- server_cbf.devId = INVALID_DEVID;
- }
- #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
- static int test_wc_CryptoCb(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLF_CRYPTO_CB
- /* TODO: Add crypto callback API tests */
- #ifdef HAVE_IO_TESTS_DEPENDENCIES
- #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
- int tlsVer;
- #endif
- #ifndef NO_RSA
- for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
- test_wc_CryptoCb_TLS(tlsVer,
- svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
- cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile);
- }
- #endif
- #ifdef HAVE_ECC
- for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
- test_wc_CryptoCb_TLS(tlsVer,
- caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
- cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile);
- }
- #endif
- #ifdef HAVE_ED25519
- for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
- if (tlsVer == WOLFSSL_DTLSV1) continue;
- test_wc_CryptoCb_TLS(tlsVer,
- caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
- cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile);
- }
- #endif
- #endif /* HAVE_IO_TESTS_DEPENDENCIES */
- res = TEST_RES_CHECK(1);
- #endif /* WOLF_CRYPTO_CB */
- return res;
- }
- #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
- static void test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
- const char* cliCaPemFile, const char* cliCertPemFile,
- const char* cliPrivKeyPemFile,
- const char* svrCaPemFile, const char* svrCertPemFile,
- const char* svrPrivKeyPemFile,
- byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
- {
- callback_functions client_cbf;
- callback_functions server_cbf;
- XMEMSET(&client_cbf, 0, sizeof(client_cbf));
- XMEMSET(&server_cbf, 0, sizeof(server_cbf));
- if (tlsVer == WOLFSSL_TLSV1_3) {
- #ifdef WOLFSSL_TLS13
- server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
- client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
- #endif
- }
- else if (tlsVer == WOLFSSL_TLSV1_2) {
- #ifndef WOLFSSL_NO_TLS12
- server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
- client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
- #endif
- }
- else if (tlsVer == WOLFSSL_TLSV1_1) {
- #ifndef NO_OLD_TLS
- server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
- client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
- #endif
- }
- else if (tlsVer == WOLFSSL_TLSV1) {
- #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
- server_cbf.method_ex = wolfTLSv1_server_method_ex;
- client_cbf.method_ex = wolfTLSv1_client_method_ex;
- #endif
- }
- else if (tlsVer == WOLFSSL_SSLV3) {
- #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
- defined(WOLFSSL_STATIC_RSA)
- server_cbf.method_ex = wolfSSLv3_server_method_ex;
- client_cbf.method_ex = wolfSSLv3_client_method_ex;
- #endif
- }
- else if (tlsVer == WOLFSSL_DTLSV1_2) {
- #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
- server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
- client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
- #endif
- }
- else if (tlsVer == WOLFSSL_DTLSV1) {
- #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
- server_cbf.method_ex = wolfDTLSv1_server_method_ex;
- client_cbf.method_ex = wolfDTLSv1_client_method_ex;
- #endif
- }
- if (server_cbf.method_ex == NULL) {
- /* not enabled */
- return;
- }
- /* Setup the keys for the TLS test */
- client_cbf.certPemFile = cliCertPemFile;
- client_cbf.keyPemFile = cliPrivKeyPemFile;
- client_cbf.caPemFile = cliCaPemFile;
- server_cbf.certPemFile = svrCertPemFile;
- server_cbf.keyPemFile = svrPrivKeyPemFile;
- server_cbf.caPemFile = svrCaPemFile;
- client_cbf.mem = cliMem;
- client_cbf.memSz = cliMemSz;
- server_cbf.mem = svrMem;
- server_cbf.memSz = svrMemSz;
- client_cbf.devId = INVALID_DEVID;
- server_cbf.devId = INVALID_DEVID;
- /* Perform TLS server and client test */
- /* First test is at WOLFSSL_CTX level */
- test_wolfSSL_client_server(&client_cbf, &server_cbf);
- /* Check for success */
- AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
- AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
- /* Second test is a WOLFSSL object level */
- client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
- test_wolfSSL_client_server(&client_cbf, &server_cbf);
- /* Check for success */
- AssertIntEQ(server_cbf.return_code, TEST_SUCCESS);
- AssertIntEQ(client_cbf.return_code, TEST_SUCCESS);
- }
- #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
- #ifdef WOLFSSL_STATIC_MEMORY
- #if (defined(HAVE_ECC) && !defined(ALT_ECC_SIZE)) || \
- defined(SESSION_CERTS)
- #ifdef OPENSSL_EXTRA
- #define TEST_TLS_STATIC_MEMSZ (400000)
- #else
- #define TEST_TLS_STATIC_MEMSZ (320000)
- #endif
- #else
- #define TEST_TLS_STATIC_MEMSZ (80000)
- #endif
- static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
- {
- WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
- WOLFSSL_MEM_STATS mem_stats;
- WOLFSSL_MEM_CONN_STATS ssl_stats;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
- AssertIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
- WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
- #endif
- AssertNotNull((ssl1 = wolfSSL_new(ctx)));
- AssertNotNull((ssl2 = wolfSSL_new(ctx)));
- /* this should fail because kMaxCtxClients == 2 */
- AssertNull((ssl3 = wolfSSL_new(ctx)));
- if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
- #ifdef DEBUG_WOLFSSL
- wolfSSL_PrintStatsConn(&ssl_stats);
- #endif
- (void)ssl_stats;
- }
- /* display collected statistics */
- if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
- #ifdef DEBUG_WOLFSSL
- wolfSSL_PrintStats(&mem_stats);
- #endif
- (void)mem_stats;
- }
- wolfSSL_free(ssl1);
- wolfSSL_free(ssl2);
- return TEST_RES_CHECK(1);
- }
- #endif /* WOLFSSL_STATIC_MEMORY */
- static int test_wolfSSL_CTX_StaticMemory(void)
- {
- int res = TEST_SKIPPED;
- #ifdef WOLFSSL_STATIC_MEMORY
- wolfSSL_method_func method_func;
- WOLFSSL_CTX* ctx;
- const int kMaxCtxClients = 2;
- #ifdef HAVE_IO_TESTS_DEPENDENCIES
- #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
- int tlsVer;
- byte cliMem[TEST_TLS_STATIC_MEMSZ];
- #endif
- #endif
- byte svrMem[TEST_TLS_STATIC_MEMSZ];
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- method_func = wolfTLSv1_2_server_method_ex;
- #else
- method_func = wolfTLSv1_3_server_method_ex;
- #endif
- #else
- #ifndef WOLFSSL_NO_TLS12
- method_func = wolfTLSv1_2_client_method_ex;
- #else
- method_func = wolfTLSv1_3_client_method_ex;
- #endif
- #endif
- /* Test creating CTX directly from static memory pool */
- ctx = NULL;
- AssertIntEQ(wolfSSL_CTX_load_static_memory(
- &ctx, method_func, svrMem, sizeof(svrMem),
- 0, kMaxCtxClients), WOLFSSL_SUCCESS);
- test_wolfSSL_CTX_StaticMemory_SSL(ctx);
- wolfSSL_CTX_free(ctx);
- ctx = NULL;
- /* Test for heap allocated CTX, then assigning static pool to it */
- AssertNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
- AssertIntEQ(wolfSSL_CTX_load_static_memory(&ctx,
- NULL, svrMem, sizeof(svrMem),
- 0, kMaxCtxClients), WOLFSSL_SUCCESS);
- test_wolfSSL_CTX_StaticMemory_SSL(ctx);
- wolfSSL_CTX_free(ctx);
- /* TLS Level Tests using static memory */
- #ifdef HAVE_IO_TESTS_DEPENDENCIES
- #ifndef NO_RSA
- for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
- test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
- svrCertFile, cliCertFile, cliKeyFile,
- cliCertFile, svrCertFile, svrKeyFile,
- cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
- }
- #endif
- #ifdef HAVE_ECC
- for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
- test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
- caEccCertFile, cliEccCertFile, cliEccKeyFile,
- cliEccCertFile, eccCertFile, eccKeyFile,
- cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
- }
- #endif
- #ifdef HAVE_ED25519
- for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
- if (tlsVer == WOLFSSL_DTLSV1) continue;
- test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
- caEdCertFile, cliEdCertFile, cliEdKeyFile,
- cliEdCertFile, edCertFile, edKeyFile,
- cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem));
- }
- #endif
- #endif /* HAVE_IO_TESTS_DEPENDENCIES */
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_openssl_FIPS_drbg(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
- DRBG_CTX* dctx;
- byte data1[32], data2[32], zeroData[32];
- byte testSeed[16];
- size_t dlen = sizeof(data1);
- int i;
- XMEMSET(data1, 0, dlen);
- XMEMSET(data2, 0, dlen);
- XMEMSET(zeroData, 0, sizeof(zeroData));
- for (i=0; i<(int)sizeof(testSeed); i++) {
- testSeed[i] = (byte)i;
- }
- AssertNotNull(dctx = FIPS_get_default_drbg());
- AssertIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
- AssertIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
- WOLFSSL_SUCCESS);
- AssertIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
- AssertIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
- WOLFSSL_SUCCESS);
- AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
- AssertIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
- WOLFSSL_SUCCESS);
- AssertIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
- WOLFSSL_SUCCESS);
- AssertIntNE(XMEMCMP(data1, zeroData, dlen), 0);
- AssertIntNE(XMEMCMP(data1, data2, dlen), 0);
- AssertIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- static int test_wolfSSL_FIPS_mode(void)
- {
- int res = TEST_SKIPPED;
- #if defined(OPENSSL_ALL)
- #ifdef HAVE_FIPS
- AssertIntEQ(wolfSSL_FIPS_mode(), 1);
- AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
- #else
- AssertIntEQ(wolfSSL_FIPS_mode(), 0);
- AssertIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
- #endif
- res = TEST_RES_CHECK(1);
- #endif
- return res;
- }
- #ifdef WOLFSSL_DTLS
- /* Prints out the current window */
- static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
- #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
- int i;
- for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
- word32 b = w[i];
- int j;
- /* Prints out a 32 bit binary number in big endian order */
- for (j = 0; j < 32; j++, b <<= 1) {
- if (b & (((word32)1) << 31))
- fprintf(stderr, "1");
- else
- fprintf(stderr, "0");
- }
- fprintf(stderr, " ");
- }
- fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
- #else
- (void)h;
- (void)l;
- (void)w;
- #endif
- }
- /* a - cur_hi
- * b - cur_lo
- * c - next_hi
- * d - next_lo
- * e - window
- * f - expected next_hi
- * g - expected next_lo
- * h - expected window[1]
- * i - expected window[0]
- */
- #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
- wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)); \
- DUW_TEST_print_window_binary((a), (b), (e)); \
- AssertIntEQ((c), (f)); \
- AssertIntEQ((d), (g)); \
- AssertIntEQ((e)[1], (h)); \
- AssertIntEQ((e)[0], (i)); \
- } while (0)
- static int test_wolfSSL_DtlsUpdateWindow(void)
- {
- word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
- word32 next_lo = 0;
- word16 next_hi = 0;
- #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
- fprintf(stderr, "\n");
- #endif
- XMEMSET(window, 0, sizeof window);
- DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
- DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
- DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
- DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
- DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
- DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
- DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
- DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
- DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
- DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
- DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
- DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
- DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
- DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
- DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
- DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
- DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
- DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
- DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
- DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
- DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
- DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
- DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
- DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
- DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
- DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
- DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
- DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
- DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
- DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
- DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
- DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
- return TEST_RES_CHECK(1);
- }
- #endif /* WOLFSSL_DTLS */
- #ifdef WOLFSSL_DTLS
- static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
- word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
- {
- DtlsMsg* cur;
- static byte msg[100];
- static byte msgInit = 0;
- if (!msgInit) {
- int i;
- for (i = 0; i < 100; i++)
- msg[i] = i + 1;
- msgInit = 1;
- }
- /* Sanitize test parameters */
- if (len > sizeof(msg))
- return -1;
- if (f_offset + f_len > sizeof(msg))
- return -1;
- DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
- if (ssl->dtls_rx_msg_list == NULL)
- return -100;
- if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
- return -200;
- if (cur->fragBucketListCount != f_count)
- return -300;
- if (cur->ready != ready)
- return -400;
- if (cur->bytesReceived != bytesReceived)
- return -500;
- if (ready) {
- if (cur->fragBucketList != NULL)
- return -600;
- if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
- return -700;
- }
- else {
- DtlsFragBucket* fb;
- if (cur->fragBucketList == NULL)
- return -800;
- for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
- if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
- return -900;
- }
- }
- return 0;
- }
- static void DFB_TEST_RESET(WOLFSSL* ssl)
- {
- DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
- ssl->dtls_rx_msg_list = NULL;
- ssl->dtls_rx_msg_list_sz = 0;
- }
- static int test_wolfSSL_DTLS_fragment_buckets(void)
- {
- WOLFSSL ssl[1];
- XMEMSET(ssl, 0, sizeof(*ssl));
- AssertIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
- AssertIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
- AssertIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
- AssertIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
- AssertIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
- AssertIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
- /* Test all permutations of 3 regions */
- /* 1 2 3 */
- AssertIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
- AssertIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
- AssertIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
- /* 1 3 2 */
- AssertIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
- AssertIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
- AssertIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
- /* 2 1 3 */
- AssertIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
- AssertIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
- AssertIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
- /* 2 3 1 */
- AssertIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
- AssertIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
- AssertIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
- /* 3 1 2 */
- AssertIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
- AssertIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
- AssertIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
- /* 3 2 1 */
- AssertIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
- AssertIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
- AssertIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
- /* Test overlapping regions */
- AssertIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
- AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
- AssertIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
- AssertIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
- AssertIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
- /* Test overlapping multiple regions */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
- AssertIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
- AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
- AssertIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
- AssertIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
- AssertIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
- DFB_TEST_RESET(ssl);
- return TEST_RES_CHECK(1);
- }
- #endif
- #if !defined(NO_FILESYSTEM) && \
- defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- static int test_wolfSSL_dtls_stateless2(void)
- {
- WOLFSSL *ssl_c, *ssl_c2, *ssl_s;
- struct test_memio_ctx test_ctx;
- WOLFSSL_CTX *ctx_c, *ctx_s;
- int ret;
- XMEMSET(&test_ctx, 0, sizeof(test_ctx));
- ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
- wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
- if (ret != 0)
- return -1;
- ssl_c2 = wolfSSL_new(ctx_c);
- if (ssl_c2 == NULL)
- return -2;
- wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
- wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
- /* send CH */
- ret = wolfSSL_connect(ssl_c2);
- if (ret == 0 || ssl_c2->error != WANT_READ)
- return -3;
- ret = wolfSSL_accept(ssl_s);
- if (ret == 0 || ssl_s->error != WANT_READ)
- return -4;
- if (test_ctx.c_len == 0)
- return -5;
- /* consume HRR */
- test_ctx.c_len = 0;
- ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
- if (ret != 0)
- return -6;
- wolfSSL_free(ssl_c2);
- wolfSSL_free(ssl_c);
- wolfSSL_free(ssl_s);
- wolfSSL_CTX_free(ctx_c);
- wolfSSL_CTX_free(ctx_s);
- return TEST_SUCCESS;
- }
- #ifdef HAVE_MAX_FRAGMENT
- static int test_wolfSSL_dtls_stateless_maxfrag(void)
- {
- WOLFSSL *ssl_c, *ssl_c2, *ssl_s;
- struct test_memio_ctx test_ctx;
- WOLFSSL_CTX *ctx_c, *ctx_s;
- word16 max_fragment;
- int ret;
- XMEMSET(&test_ctx, 0, sizeof(test_ctx));
- ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
- wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
- if (ret != 0)
- return -1;
- ssl_c2 = wolfSSL_new(ctx_c);
- if (ssl_c2 == NULL)
- return -2;
- ret = wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8);
- if (ret != WOLFSSL_SUCCESS)
- return -3;
- wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
- wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
- max_fragment = ssl_s->max_fragment;
- /* send CH */
- ret = wolfSSL_connect(ssl_c2);
- if (ret == 0 || ssl_c2->error != WANT_READ)
- return -4;
- ret = wolfSSL_accept(ssl_s);
- if (ret == 0 || ssl_s->error != WANT_READ)
- return -5;
- /* CH without cookie shouldn't change state */
- if (ssl_s->max_fragment != max_fragment)
- return -6;
- if (test_ctx.c_len == 0)
- return -7;
- /* consume HRR from buffer */
- test_ctx.c_len = 0;
- ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
- if (ret != 0)
- return -8;
- wolfSSL_free(ssl_c2);
- wolfSSL_free(ssl_c);
- wolfSSL_free(ssl_s);
- wolfSSL_CTX_free(ctx_c);
- wolfSSL_CTX_free(ctx_s);
- return TEST_SUCCESS;
- }
- #endif /* HAVE_MAX_FRAGMENT */
- #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
- #define ROUNDS_WITH_HVR 4
- #define ROUNDS_WITHOUT_HVR 2
- #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
- static int buf_is_hvr(const byte *data, int len)
- {
- if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
- return 0;
- return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
- }
- static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
- {
- struct test_memio_ctx test_ctx;
- WOLFSSL_CTX *ctx_c, *ctx_s;
- WOLFSSL *ssl_c, *ssl_s;
- WOLFSSL_SESSION *sess;
- int ret, round_trips;
- XMEMSET(&test_ctx, 0, sizeof(test_ctx));
- ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
- wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
- if (ret != 0)
- return -1;
- #ifdef HAVE_SESSION_TICKET
- if (useticket) {
- ret = wolfSSL_UseSessionTicket(ssl_c);
- if (ret != WOLFSSL_SUCCESS)
- return -2;
- }
- #endif
- round_trips = ROUNDS_WITH_HVR;
- ret = test_memio_do_handshake(ssl_c, ssl_s, round_trips, &round_trips);
- if (ret != 0)
- return -3;
- if (round_trips != ROUNDS_WITH_HVR)
- return -4;
- sess = wolfSSL_get1_session(ssl_c);
- if (sess == NULL)
- return -5;
- wolfSSL_shutdown(ssl_c);
- wolfSSL_shutdown(ssl_s);
- wolfSSL_free(ssl_c);
- wolfSSL_free(ssl_s);
- test_ctx.c_len = test_ctx.s_len = 0;
- /* make resumption invalid */
- if (bad) {
- if (useticket) {
- #ifdef HAVE_SESSION_TICKET
- sess->ticket[0] = !sess->ticket[0];
- #endif /* HAVE_SESSION_TICKET */
- }
- else {
- sess->sessionID[0] = !sess->sessionID[0];
- }
- }
- ssl_c = wolfSSL_new(ctx_c);
- ssl_s = wolfSSL_new(ctx_s);
- wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
- wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
- wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
- wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
- ret = wolfSSL_set_session(ssl_c, sess);
- if (ret != WOLFSSL_SUCCESS)
- return -6;
- ret = wolfSSL_connect(ssl_c);
- if (ret == WOLFSSL_SUCCESS || ssl_c->error != WANT_READ)
- return -7;
- ret = wolfSSL_accept(ssl_s);
- if (ret == WOLFSSL_SUCCESS || ssl_s->error != WANT_READ)
- return -8;
- if (bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
- return -9;
- if (!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len))
- return -10;
- if (!useticket) {
- ret = test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips);
- if (ret != 0)
- return -11;
- if (bad && round_trips != ROUNDS_WITH_HVR - 1)
- return -12;
- if (!bad && round_trips != ROUNDS_WITHOUT_HVR - 1)
- return -13;
- }
- wolfSSL_SESSION_free(sess);
- wolfSSL_free(ssl_c);
- wolfSSL_free(ssl_s);
- wolfSSL_CTX_free(ctx_c);
- wolfSSL_CTX_free(ctx_s);
- return 0;
- }
- static int test_wolfSSL_dtls_stateless_resume(void)
- {
- int ret;
- #ifdef HAVE_SESSION_TICKET
- ret = _test_wolfSSL_dtls_stateless_resume(1, 0);
- if (ret != 0)
- return TEST_RES_CHECK(ret);
- ret = _test_wolfSSL_dtls_stateless_resume(1, 1);
- if (ret != 0)
- return TEST_RES_CHECK(ret - 100);
- #endif /* HAVE_SESION_TICKET */
- ret = _test_wolfSSL_dtls_stateless_resume(0, 0);
- if (ret != 0)
- return TEST_RES_CHECK(ret - 200);
- ret = _test_wolfSSL_dtls_stateless_resume(0, 1);
- if (ret != 0)
- return TEST_RES_CHECK(ret - 300);
- return TEST_RES_CHECK(TEST_SUCCESS);
- }
- #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
- #if !defined(NO_OLD_TLS)
- static int test_wolfSSL_dtls_stateless_downgrade(void)
- {
- WOLFSSL_CTX *ctx_c, *ctx_c2, *ctx_s;
- WOLFSSL *ssl_c, *ssl_c2, *ssl_s;
- struct test_memio_ctx test_ctx;
- int ret;
- XMEMSET(&test_ctx, 0, sizeof(test_ctx));
- ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
- wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method);
- if (ret != 0)
- return -1;
- ret = wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1);
- if (ret != WOLFSSL_SUCCESS)
- return -2;
- ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method());
- if (ctx_c2 == NULL)
- return -3;
- wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
- wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
- ssl_c2 = wolfSSL_new(ctx_c2);
- if (ssl_c2 == NULL)
- return -4;
- wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
- wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
- /* send CH */
- ret = wolfSSL_connect(ssl_c2);
- if (ret == 0 || ssl_c2->error != WANT_READ)
- return -5;
- ret = wolfSSL_accept(ssl_s);
- if (ret == 0 || ssl_s->error != WANT_READ)
- return -6;
- if (test_ctx.c_len == 0)
- return -7;
- /* consume HRR */
- test_ctx.c_len = 0;
- ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
- if (ret != 0)
- return -8;
- wolfSSL_free(ssl_c2);
- wolfSSL_free(ssl_c);
- wolfSSL_free(ssl_s);
- wolfSSL_CTX_free(ctx_c);
- wolfSSL_CTX_free(ctx_c2);
- wolfSSL_CTX_free(ctx_s);
- return TEST_SUCCESS;
- }
- #endif /* !defined(NO_OLD_TLS) */
- #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
- #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
- !defined(NO_OLD_TLS)
- static int test_WOLFSSL_dtls_version_alert(void)
- {
- struct test_memio_ctx test_ctx;
- WOLFSSL_CTX *ctx_c, *ctx_s;
- WOLFSSL *ssl_c, *ssl_s;
- int ret;
- XMEMSET(&test_ctx, 0, sizeof(test_ctx));
- ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
- wolfDTLSv1_2_client_method, wolfDTLSv1_server_method);
- if (ret != 0)
- return -1;
- /* client hello */
- ret = wolfSSL_connect(ssl_c);
- if (ret == 0 || ssl_c->error != WANT_READ )
- return -2;
- /* hrr */
- ret = wolfSSL_accept(ssl_s);
- if (ret == 0 || ssl_s->error != WANT_READ )
- return -3;
- /* client hello 1 */
- ret = wolfSSL_connect(ssl_c);
- if (ret == 0 || ssl_c->error != WANT_READ )
- return -4;
- /* server hello */
- ret = wolfSSL_accept(ssl_s);
- if (ret == 0 || ssl_s->error != WANT_READ )
- return -5;
- /* should fail */
- ret = wolfSSL_connect(ssl_c);
- if (ret == 0 || ssl_c->error != VERSION_ERROR)
- return -6;
- /* shuould fail */
- ret = wolfSSL_accept(ssl_s);
- if (ret == 0 ||
- (ssl_s->error != VERSION_ERROR && ssl_s->error != FATAL_ERROR))
- return -7;
- wolfSSL_free(ssl_c);
- wolfSSL_free(ssl_s);
- wolfSSL_CTX_free(ctx_c);
- wolfSSL_CTX_free(ctx_s);
- return TEST_RES_CHECK(1);
- }
- #else
- static int test_WOLFSSL_dtls_version_alert(void)
- {
- return TEST_SKIPPED;
- }
- #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
- * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
- * !defined(NO_OLD_TLS)
- */
- #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
- && defined(WOLFSSL_TLS13) && \
- (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
- static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
- {
- struct test_memio_ctx *test_ctx;
- byte buf[2048];
- int idx, sz;
- word32 tmp;
- int ret;
- idx = 5; /* space for record header */
- buf[idx] = session_ticket; /* type */
- idx++;
- tmp = OPAQUE32_LEN +
- OPAQUE32_LEN +
- OPAQUE8_LEN + nonceLength +
- OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
- c32to24(tmp, buf + idx);
- idx += OPAQUE24_LEN;
- c32toa((word32)12345, buf+idx); /* lifetime */
- idx += OPAQUE32_LEN;
- c32toa((word32)12345, buf+idx); /* add */
- idx += OPAQUE32_LEN;
- buf[idx] = nonceLength; /* nonce length */
- idx++;
- XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
- idx += nonceLength;
- tmp = 1; /* ticket len */
- c16toa((word16)tmp, buf+idx);
- idx += 2;
- buf[idx] = 0xFF; /* ticket */
- idx++;
- tmp = 0; /* ext len */
- c16toa((word16)tmp, buf+idx);
- idx += 2;
- sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
- handshake, 0, 0, 0);
- test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
- ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
- return !(ret == sz);
- }
- static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
- {
- int i;
- if (sess == NULL)
- return -1;
- if (sess->ticketNonce.len != len)
- return -1;
- for (i = 0; i < len; i++)
- if (sess->ticketNonce.data[i] != len)
- return -1;
- return 0;
- }
- static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
- {
- char *buf[1024];
- int ret;
- ret = send_new_session_ticket(ssl_s, len, len);
- if (ret != 0)
- return -1;
- ret = wolfSSL_recv(ssl_c, buf, 1024, 0);
- if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
- return -1;
- return test_ticket_nonce_check(ssl_c->session, len);
- }
- static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
- {
- WOLFSSL_SESSION *sess, *cached;
- WOLFSSL_CTX *ctx;
- int ret;
- ctx = ssl_c->ctx;
- ret = test_ticket_nonce_malloc_do(ssl_s, ssl_c, len);
- if (ret != 0)
- return -1;
- sess = wolfSSL_get1_session(ssl_c);
- if (sess == NULL)
- return -1;
- ret = AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
- NULL, ssl_c->options.side, 1,NULL);
- if (ret != 0)
- return -1;
- cached = wolfSSL_SESSION_new();
- if (cached == NULL)
- return -1;
- ret = wolfSSL_GetSessionFromCache(ssl_c, cached);
- if (ret != WOLFSSL_SUCCESS)
- return -1;
- ret = test_ticket_nonce_check(cached, len);
- if (ret != 0)
- return -1;
- wolfSSL_SESSION_free(cached);
- wolfSSL_SESSION_free(sess);
- return 0;
- }
- static int test_ticket_nonce_malloc(void)
- {
- struct test_memio_ctx test_ctx;
- WOLFSSL_CTX *ctx_c, *ctx_s;
- byte small, medium, big;
- WOLFSSL *ssl_c, *ssl_s;
- int ret;
- XMEMSET(&test_ctx, 0, sizeof(test_ctx));
- ret = test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
- wolfTLSv1_3_client_method, wolfTLSv1_3_server_method);
- if (ret != 0)
- return -1;
- /* will send ticket manually */
- wolfSSL_no_ticket_TLSv13(ssl_s);
- wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
- wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
- while (!ssl_c->options.handShakeDone && !ssl_s->options.handShakeDone) {
- ret = wolfSSL_connect(ssl_c);
- if (ret != WOLFSSL_SUCCESS && ssl_c->error != WANT_READ)
- return -2;
- ret = wolfSSL_accept(ssl_s);
- if (ret != WOLFSSL_SUCCESS && ssl_s->error != WANT_READ)
- return -3;
- }
- small = TLS13_TICKET_NONCE_STATIC_SZ;
- medium = small + 20 <= 255 ? small + 20 : 255;
- big = medium + 20 <= 255 ? small + 20 : 255;
- if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
- return -1;
- if (ssl_c->session->ticketNonce.data !=
- ssl_c->session->ticketNonce.dataStatic)
- return -1;
- if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
- return -1;
- if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, big))
- return -1;
- if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium))
- return -5;
- if (test_ticket_nonce_malloc_do(ssl_s, ssl_c, small))
- return -6;
- if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
- return -1;
- if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
- return -1;
- if (test_ticket_nonce_cache(ssl_s, ssl_c, big))
- return -1;
- if (test_ticket_nonce_cache(ssl_s, ssl_c, medium))
- return -1;
- if (test_ticket_nonce_cache(ssl_s, ssl_c, small))
- return -1;
- wolfSSL_free(ssl_c);
- wolfSSL_free(ssl_s);
- wolfSSL_CTX_free(ctx_c);
- wolfSSL_CTX_free(ctx_s);
- return 0;
- }
- #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
- /*----------------------------------------------------------------------------*
- | Main
- *----------------------------------------------------------------------------*/
- typedef int (*TEST_FUNC)(void);
- typedef struct {
- const char *name;
- TEST_FUNC func;
- byte run:1;
- } TEST_CASE;
- #define TEST_DECL(func) { #func, func, 0 }
- int testAll = 1;
- TEST_CASE testCases[] = {
- TEST_DECL(test_fileAccess),
- TEST_DECL(test_wolfSSL_Init),
- TEST_DECL(test_wolfSSL_Method_Allocators),
- #ifndef NO_WOLFSSL_SERVER
- TEST_DECL(test_wolfSSL_CTX_new),
- #endif
- #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
- (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
- TEST_DECL(test_for_double_Free),
- #endif
- #ifdef HAVE_IO_TESTS_DEPENDENCIES
- TEST_DECL(test_wolfSSL_get_finished),
- TEST_DECL(test_wolfSSL_CTX_add_session),
- #endif
- TEST_DECL(test_SSL_CIPHER_get_xxx),
- TEST_DECL(test_wolfSSL_ERR_strings),
- TEST_DECL(test_wolfSSL_EVP_shake128),
- TEST_DECL(test_wolfSSL_EVP_shake256),
- TEST_DECL(test_EVP_blake2),
- TEST_DECL(test_EVP_MD_do_all),
- TEST_DECL(test_OBJ_NAME_do_all),
- TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
- TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
- TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
- TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
- TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
- TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
- TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
- TEST_DECL(test_wolfSSL_CheckOCSPResponse),
- TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
- TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
- TEST_DECL(test_wolfSSL_CertManagerGetCerts),
- TEST_DECL(test_wolfSSL_CertManagerSetVerify),
- TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
- TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
- TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
- TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
- TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
- TEST_DECL(test_wolfSSL_FPKI),
- TEST_DECL(test_wolfSSL_OtherName),
- TEST_DECL(test_wolfSSL_CertRsaPss),
- TEST_DECL(test_wolfSSL_CertManagerCRL),
- TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
- TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
- TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
- TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
- TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
- TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
- TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
- TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
- TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
- TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
- TEST_DECL(test_wolfSSL_CTX_enable_disable),
- TEST_DECL(test_wolfSSL_CTX_ticket_API),
- TEST_DECL(test_server_wolfSSL_new),
- TEST_DECL(test_client_wolfSSL_new),
- TEST_DECL(test_wolfSSL_SetTmpDH_file),
- TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
- TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
- TEST_DECL(test_SetTmpEC_DHE_Sz),
- TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
- TEST_DECL(test_wolfSSL_dtls_set_mtu),
- TEST_DECL(test_wolfSSL_dtls_plaintext),
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES)
- TEST_DECL(test_wolfSSL_read_write),
- TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
- TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
- TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
- TEST_DECL(test_wolfSSL_dtls_export),
- TEST_DECL(test_wolfSSL_tls_export),
- #endif
- TEST_DECL(test_wolfSSL_SetMinVersion),
- TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
- /* TLS extensions tests */
- #ifdef HAVE_IO_TESTS_DEPENDENCIES
- #ifdef HAVE_SNI
- TEST_DECL(test_wolfSSL_UseSNI_params),
- TEST_DECL(test_wolfSSL_UseSNI_connection),
- TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
- #endif /* HAVE_SNI */
- #endif
- TEST_DECL(test_wolfSSL_UseTrustedCA),
- TEST_DECL(test_wolfSSL_UseMaxFragment),
- TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
- TEST_DECL(test_wolfSSL_UseSupportedCurve),
- #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- TEST_DECL(test_wolfSSL_UseALPN_connection),
- TEST_DECL(test_wolfSSL_UseALPN_params),
- #endif
- #ifdef HAVE_ALPN_PROTOS_SUPPORT
- TEST_DECL(test_wolfSSL_set_alpn_protos),
- #endif
- TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
- TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
- TEST_DECL(test_tls_ext_duplicate),
- #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH)
- TEST_DECL(test_wolfSSL_Tls13_ECH_params),
- TEST_DECL(test_wolfSSL_Tls13_ECH),
- #endif
- /* X509 tests */
- TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
- TEST_DECL(test_wolfSSL_PKCS12),
- TEST_DECL(test_wolfSSL_no_password_cb),
- TEST_DECL(test_wolfSSL_PKCS8),
- TEST_DECL(test_wolfSSL_PKCS8_ED25519),
- TEST_DECL(test_wolfSSL_PKCS8_ED448),
- TEST_DECL(test_wolfSSL_PKCS5),
- TEST_DECL(test_wolfSSL_URI),
- TEST_DECL(test_wolfSSL_TBS),
- TEST_DECL(test_wolfSSL_X509_verify),
- TEST_DECL(test_wolfSSL_X509_TLS_version),
- TEST_DECL(test_wc_PemToDer),
- TEST_DECL(test_wc_AllocDer),
- TEST_DECL(test_wc_CertPemToDer),
- TEST_DECL(test_wc_PubKeyPemToDer),
- TEST_DECL(test_wc_PemPubKeyToDer),
- TEST_DECL(test_wc_GetPubKeyDerFromCert),
- TEST_DECL(test_wc_CheckCertSigPubKey),
- /* OCSP Stapling */
- TEST_DECL(test_wolfSSL_UseOCSPStapling),
- TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
- /* Multicast */
- TEST_DECL(test_wolfSSL_mcast),
- /* compatibility tests */
- TEST_DECL(test_wolfSSL_lhash),
- TEST_DECL(test_wolfSSL_X509_NAME),
- TEST_DECL(test_wolfSSL_X509_NAME_hash),
- TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
- TEST_DECL(test_wolfSSL_X509_INFO),
- #endif
- TEST_DECL(test_wolfSSL_X509_subject_name_hash),
- TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
- TEST_DECL(test_wolfSSL_X509_check_host),
- TEST_DECL(test_wolfSSL_X509_check_email),
- TEST_DECL(test_wolfSSL_DES),
- TEST_DECL(test_wolfSSL_certs),
- TEST_DECL(test_wolfSSL_X509_check_private_key),
- TEST_DECL(test_wolfSSL_ASN1_TIME_print),
- TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
- TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
- TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
- TEST_DECL(test_wolfSSL_private_keys),
- TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
- #endif
- TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
- TEST_DECL(test_wolfSSL_PEM_PrivateKey),
- TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
- TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
- TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
- TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
- TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
- TEST_DECL(test_wolfSSL_PEM_PUBKEY),
- #endif
- TEST_DECL(test_DSA_do_sign_verify),
- TEST_DECL(test_wolfSSL_tmp_dh),
- TEST_DECL(test_wolfSSL_ctrl),
- TEST_DECL(test_wolfSSL_EVP_MD_size),
- TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
- TEST_DECL(test_wolfSSL_EVP_Digest),
- TEST_DECL(test_wolfSSL_EVP_Digest_all),
- TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
- TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
- TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
- TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
- TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
- TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
- TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
- TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
- TEST_DECL(test_wolfSSL_EVP_EncodeInit),
- TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
- TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
- TEST_DECL(test_wolfSSL_EVP_DecodeInit),
- TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
- TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
- TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
- #endif
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
- TEST_DECL(test_wolfSSL_GetLoggingCb),
- TEST_DECL(test_WOLFSSL_ERROR_MSG),
- TEST_DECL(test_wc_ERR_remove_state),
- TEST_DECL(test_wc_ERR_print_errors_fp),
- #endif
- TEST_DECL(test_wolfSSL_set_options),
- TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
- TEST_DECL(test_wolfSSL_set1_curves_list),
- TEST_DECL(test_wolfSSL_set1_sigalgs_list),
- TEST_DECL(test_wolfSSL_PKCS7_certs),
- TEST_DECL(test_wolfSSL_X509_STORE_CTX),
- TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
- TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
- TEST_DECL(test_wolfSSL_msgCb),
- TEST_DECL(test_wolfSSL_either_side),
- TEST_DECL(test_wolfSSL_DTLS_either_side),
- TEST_DECL(test_wolfSSL_dtls_fragments),
- TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
- TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
- TEST_DECL(test_wolfSSL_dtls_bad_record),
- TEST_DECL(test_wolfSSL_dtls_stateless),
- TEST_DECL(test_generate_cookie),
- TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
- TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
- TEST_DECL(test_wolfSSL_X509_Name_canon),
- TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
- TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
- TEST_DECL(test_wolfSSL_X509_NID),
- TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
- TEST_DECL(test_wolfSSL_get0_param),
- TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
- TEST_DECL(test_wolfSSL_set1_host),
- TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
- TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
- TEST_DECL(test_wolfSSL_X509_STORE),
- TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
- TEST_DECL(test_X509_STORE_get0_objects),
- TEST_DECL(test_wolfSSL_X509_load_crl_file),
- TEST_DECL(test_wolfSSL_BN),
- TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_PEM_read_bio),
- TEST_DECL(test_wolfSSL_BIO),
- #endif
- TEST_DECL(test_wolfSSL_ASN1_STRING),
- TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
- TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
- TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
- TEST_DECL(test_wolfSSL_X509),
- TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
- TEST_DECL(test_wolfSSL_X509_sign),
- TEST_DECL(test_wolfSSL_X509_sign2),
- TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
- TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
- #if defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- TEST_DECL(test_wolfSSL_check_domain),
- #endif
- TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
- TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
- TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
- TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
- TEST_DECL(test_wolfSSL_RAND),
- TEST_DECL(test_wolfSSL_BUF),
- TEST_DECL(test_wolfSSL_set_tlsext_status_type),
- TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
- TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
- TEST_DECL(test_wolfSSL_X509_cmp_time),
- TEST_DECL(test_wolfSSL_X509_time_adj),
- TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
- TEST_DECL(test_wolfSSL_CTX_add_client_CA),
- TEST_DECL(test_wolfSSL_CTX_set_srp_username),
- TEST_DECL(test_wolfSSL_CTX_set_srp_password),
- TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
- TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
- TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
- TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
- TEST_DECL(test_wolfSSL_Tls13_postauth),
- TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
- TEST_DECL(test_wolfSSL_set_minmax_proto_version),
- TEST_DECL(test_wolfSSL_THREADID_hash),
- TEST_DECL(test_wolfSSL_RAND_set_rand_method),
- TEST_DECL(test_wolfSSL_RAND_bytes),
- TEST_DECL(test_wolfSSL_BN_rand),
- TEST_DECL(test_wolfSSL_pseudo_rand),
- TEST_DECL(test_wolfSSL_PKCS8_Compat),
- TEST_DECL(test_wolfSSL_PKCS8_d2i),
- TEST_DECL(test_error_queue_per_thread),
- TEST_DECL(test_wolfSSL_ERR_put_error),
- TEST_DECL(test_wolfSSL_ERR_get_error_order),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_ERR_print_errors),
- #endif
- TEST_DECL(test_wolfSSL_HMAC),
- TEST_DECL(test_wolfSSL_CMAC),
- TEST_DECL(test_wolfSSL_OBJ),
- TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
- TEST_DECL(test_wolfSSL_OBJ_cmp),
- TEST_DECL(test_wolfSSL_OBJ_txt2nid),
- TEST_DECL(test_wolfSSL_OBJ_txt2obj),
- TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
- TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
- TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
- TEST_DECL(test_wolfSSL_X509_set_name),
- TEST_DECL(test_wolfSSL_X509_set_notAfter),
- TEST_DECL(test_wolfSSL_X509_set_notBefore),
- TEST_DECL(test_wolfSSL_X509_set_version),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_BIO_gets),
- TEST_DECL(test_wolfSSL_BIO_puts),
- TEST_DECL(test_wolfSSL_BIO_dump),
- TEST_DECL(test_wolfSSL_BIO_should_retry),
- TEST_DECL(test_wolfSSL_d2i_PUBKEY),
- TEST_DECL(test_wolfSSL_BIO_write),
- TEST_DECL(test_wolfSSL_BIO_connect),
- TEST_DECL(test_wolfSSL_BIO_accept),
- TEST_DECL(test_wolfSSL_BIO_printf),
- TEST_DECL(test_wolfSSL_BIO_f_md),
- TEST_DECL(test_wolfSSL_BIO_up_ref),
- TEST_DECL(test_wolfSSL_BIO_tls),
- #endif
- TEST_DECL(test_wolfSSL_cert_cb),
- TEST_DECL(test_wolfSSL_SESSION),
- TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
- TEST_DECL(test_wolfSSL_ticket_keys),
- TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
- TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
- TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
- TEST_DECL(test_wolfSSL_sk_DIST_POINT),
- TEST_DECL(test_wolfSSL_MD4),
- TEST_DECL(test_wolfSSL_verify_mode),
- TEST_DECL(test_wolfSSL_verify_depth),
- TEST_DECL(test_wolfSSL_HMAC_CTX),
- TEST_DECL(test_wolfSSL_msg_callback),
- TEST_DECL(test_wolfSSL_SHA),
- TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
- TEST_DECL(test_wolfSSL_MD5),
- TEST_DECL(test_wolfSSL_MD5_Transform),
- TEST_DECL(test_wolfSSL_SHA_Transform),
- TEST_DECL(test_wolfSSL_SHA256),
- TEST_DECL(test_wolfSSL_SHA256_Transform),
- TEST_DECL(test_wolfSSL_SHA224),
- TEST_DECL(test_wolfSSL_SHA512_Transform),
- TEST_DECL(test_wolfSSL_X509_get_serialNumber),
- TEST_DECL(test_wolfSSL_X509_CRL),
- TEST_DECL(test_wolfSSL_d2i_X509_REQ),
- TEST_DECL(test_wolfSSL_PEM_read_X509),
- TEST_DECL(test_wolfSSL_PEM_read),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
- TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
- #endif
- TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
- TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
- TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
- TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
- TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
- TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
- TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
- TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
- TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
- TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
- TEST_DECL(test_wolfSSL_X509_check_ca),
- TEST_DECL(test_wolfSSL_X509_check_ip_asc),
- TEST_DECL(test_wolfSSL_make_cert),
- TEST_DECL(test_wolfSSL_DES_ncbc),
- TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
- TEST_DECL(test_wolfSSL_CRYPTO_cts128),
- TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
- TEST_DECL(test_wolfssl_EVP_aes_gcm),
- TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
- TEST_DECL(test_wolfssl_EVP_aes_ccm),
- TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
- TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
- TEST_DECL(test_wolfssl_EVP_chacha20),
- TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
- TEST_DECL(test_wolfSSL_PKEY_up_ref),
- TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
- TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
- TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
- TEST_DECL(test_wolfSSL_i2d_PrivateKey),
- TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
- TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
- TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
- TEST_DECL(test_wolfSSL_OCSP_id_cmp),
- TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
- TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
- TEST_DECL(test_wolfSSL_OCSP_resp_count),
- TEST_DECL(test_wolfSSL_OCSP_resp_get0),
- TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
- TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
- TEST_DECL(test_CONF_modules_xxx),
- TEST_DECL(test_CRYPTO_set_dynlock_xxx),
- TEST_DECL(test_CRYPTO_THREADID_xxx),
- TEST_DECL(test_ENGINE_cleanup),
- TEST_DECL(test_wolfSSL_EC_KEY_set_group),
- TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
- TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
- #ifdef OPENSSL_ALL
- TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
- TEST_DECL(test_wolfSSL_sk_CIPHER_description),
- TEST_DECL(test_wolfSSL_get_ciphers_compat),
- TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
- TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
- TEST_DECL(test_wolfSSL_EC_KEY_dup),
- TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
- TEST_DECL(test_wolfSSL_DSA_SIG),
- TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
- TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
- TEST_DECL(test_wolfSSL_CTX_ctrl),
- TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
- TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
- TEST_DECL(test_wolfSSL_EVP_PKEY_id),
- TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
- TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
- TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
- TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
- TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
- TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
- TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
- TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
- TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_key_length),
- TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
- TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
- TEST_DECL(test_wolfSSL_EVP_rc4),
- TEST_DECL(test_wolfSSL_EVP_enc_null),
- TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
- TEST_DECL(test_wolfSSL_EVP_mdc2),
- TEST_DECL(test_wolfSSL_EVP_md4),
- TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
- TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
- TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
- TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
- TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
- TEST_DECL(test_wolfSSL_EVP_ripemd160),
- TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
- TEST_DECL(test_wolfSSL_EVP_MD_nid),
- TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
- TEST_DECL(test_wolfSSL_EVP_X_STATE),
- TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
- TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
- TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
- TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
- TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
- TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
- TEST_DECL(test_wolfSSL_EVP_BytesToKey),
- TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
- TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
- TEST_DECL(test_evp_cipher_aes_gcm),
- TEST_DECL(test_wolfSSL_OBJ_ln),
- TEST_DECL(test_wolfSSL_OBJ_sn),
- TEST_DECL(test_wolfSSL_TXT_DB),
- TEST_DECL(test_wolfSSL_NCONF),
- #endif /* OPENSSL_ALL */
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
- TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
- #endif /* !NO_BIO */
- #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
- TEST_DECL(test_wolfSSL_X509_CA_num),
- TEST_DECL(test_wolfSSL_X509_get_version),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_X509_print),
- TEST_DECL(test_wolfSSL_X509_CRL_print),
- TEST_DECL(test_wolfSSL_BIO_get_len),
- #endif
- TEST_DECL(test_wolfSSL_RSA),
- TEST_DECL(test_wolfSSL_RSA_DER),
- TEST_DECL(test_wolfSSL_RSA_print),
- #ifndef NO_RSA
- TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
- #endif
- TEST_DECL(test_wolfSSL_RSA_sign_sha3),
- TEST_DECL(test_wolfSSL_RSA_get0_key),
- TEST_DECL(test_wolfSSL_RSA_meth),
- TEST_DECL(test_wolfSSL_RSA_verify),
- TEST_DECL(test_wolfSSL_RSA_sign),
- TEST_DECL(test_wolfSSL_RSA_sign_ex),
- TEST_DECL(test_wolfSSL_RSA_public_decrypt),
- TEST_DECL(test_wolfSSL_RSA_private_encrypt),
- TEST_DECL(test_wolfSSL_RSA_public_encrypt),
- TEST_DECL(test_wolfSSL_RSA_private_decrypt),
- TEST_DECL(test_wolfSSL_RSA_GenAdd),
- TEST_DECL(test_wolfSSL_RSA_blinding_on),
- TEST_DECL(test_wolfSSL_RSA_ex_data),
- TEST_DECL(test_wolfSSL_RSA_LoadDer),
- TEST_DECL(test_wolfSSL_RSA_To_Der),
- TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
- TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
- TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
- TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
- TEST_DECL(test_wolfSSL_DH),
- TEST_DECL(test_wolfSSL_DH_dup),
- TEST_DECL(test_wolfSSL_DH_check),
- TEST_DECL(test_wolfSSL_DH_prime),
- TEST_DECL(test_wolfSSL_DH_1536_prime),
- TEST_DECL(test_wolfSSL_DH_get_2048_256),
- TEST_DECL(test_wolfSSL_PEM_write_DHparams),
- TEST_DECL(test_wolfSSL_PEM_read_DHparams),
- TEST_DECL(test_wolfSSL_d2i_DHparams),
- TEST_DECL(test_wolfSSL_DH_LoadDer),
- TEST_DECL(test_wolfSSL_i2d_DHparams),
- TEST_DECL(test_wolfSSL_X509V3_EXT_get),
- TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
- TEST_DECL(test_wolfSSL_X509V3_EXT),
- TEST_DECL(test_wolfSSL_X509_get_extension_flags),
- TEST_DECL(test_wolfSSL_X509_get_ext),
- TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
- TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
- TEST_DECL(test_wolfSSL_X509_get_ext_count),
- TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
- TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
- TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
- TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
- TEST_DECL(test_wolfSSL_X509V3_EXT_print),
- TEST_DECL(test_wolfSSL_X509_cmp),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_ASN1_STRING_print),
- #endif
- TEST_DECL(test_wolfSSL_ASN1_get_object),
- TEST_DECL(test_openssl_generate_key_and_cert),
- TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
- TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
- TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
- /* test the no op functions for compatibility */
- TEST_DECL(test_no_op_functions),
- /* OpenSSL EVP_PKEY API tests */
- TEST_DECL(test_EVP_PKEY_rsa),
- TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
- TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify),
- TEST_DECL(test_EVP_PKEY_ec),
- TEST_DECL(test_EVP_PKEY_cmp),
- /* OpenSSL error API tests */
- TEST_DECL(test_ERR_load_crypto_strings),
- /* OpenSSL sk_X509 API test */
- TEST_DECL(test_sk_X509),
- /* OpenSSL sk_X509_CRL API test */
- TEST_DECL(test_sk_X509_CRL),
- /* OpenSSL X509 API test */
- TEST_DECL(test_X509_get_signature_nid),
- /* OpenSSL X509 REQ API test */
- TEST_DECL(test_X509_REQ),
- /* OpenSSL PKCS7 API test */
- TEST_DECL(test_wolfssl_PKCS7),
- TEST_DECL(test_wolfSSL_PKCS7_sign),
- TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
- #ifndef NO_BIO
- TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
- #ifdef HAVE_SMIME
- TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
- TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
- #endif /* HAVE_SMIME */
- #endif /* !NO_BIO */
- /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
- TEST_DECL(test_X509_STORE_No_SSL_CTX),
- TEST_DECL(test_X509_LOOKUP_add_dir),
- /* wolfCrypt ASN tests */
- TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
- TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
- TEST_DECL(test_wc_SetSubjectRaw),
- TEST_DECL(test_wc_GetSubjectRaw),
- TEST_DECL(test_wc_SetIssuerRaw),
- TEST_DECL(test_wc_SetIssueBuffer),
- TEST_DECL(test_wc_SetSubjectKeyId),
- TEST_DECL(test_wc_SetSubject),
- TEST_DECL(test_CheckCertSignature),
- TEST_DECL(test_wc_ParseCert),
- TEST_DECL(test_MakeCertWithPathLen),
- /* wolfCrypt ECC tests */
- TEST_DECL(test_wc_ecc_get_curve_size_from_name),
- TEST_DECL(test_wc_ecc_get_curve_id_from_name),
- TEST_DECL(test_wc_ecc_get_curve_id_from_params),
- #ifdef WOLFSSL_TLS13
- /* TLS v1.3 API tests */
- TEST_DECL(test_tls13_apis),
- TEST_DECL(test_tls13_cipher_suites),
- #endif
- #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
- !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
- /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
- /* Bad certificate signature tests */
- TEST_DECL(test_EccSigFailure_cm),
- TEST_DECL(test_RsaSigFailure_cm),
- #endif /* NO_CERTS */
- #if defined(HAVE_PK_CALLBACKS) && (!defined(WOLFSSL_NO_TLS12) || \
- !defined(NO_OLD_TLS))
- TEST_DECL(test_DhCallbacks),
- #endif
- #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- TEST_DECL(test_export_keying_material),
- #endif
- TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
- #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
- (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
- defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
- defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
- TEST_DECL(test_wolfSSL_set_SSL_CTX),
- #endif
- TEST_DECL(test_wolfSSL_security_level),
- TEST_DECL(test_wolfSSL_SSL_in_init),
- TEST_DECL(test_wolfSSL_EC_curve),
- TEST_DECL(test_wolfSSL_CTX_set_timeout),
- TEST_DECL(test_wolfSSL_OpenSSL_version),
- TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
- TEST_DECL(test_CONF_CTX_FILE),
- TEST_DECL(test_CONF_CTX_CMDLINE),
- TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
- /* wolfcrypt */
- TEST_DECL(test_wolfCrypt_Init),
- TEST_DECL(test_wc_InitMd5),
- TEST_DECL(test_wc_Md5Update),
- TEST_DECL(test_wc_Md5Final),
- TEST_DECL(test_wc_InitSha),
- TEST_DECL(test_wc_ShaUpdate),
- TEST_DECL(test_wc_ShaFinal),
- TEST_DECL(test_wc_InitSha256),
- TEST_DECL(test_wc_Sha256Update),
- TEST_DECL(test_wc_Sha256Final),
- TEST_DECL(test_wc_Sha256FinalRaw),
- TEST_DECL(test_wc_Sha256GetFlags),
- TEST_DECL(test_wc_Sha256Free),
- TEST_DECL(test_wc_Sha256GetHash),
- TEST_DECL(test_wc_Sha256Copy),
- TEST_DECL(test_wc_InitSha512),
- TEST_DECL(test_wc_Sha512Update),
- TEST_DECL(test_wc_Sha512Final),
- TEST_DECL(test_wc_Sha512GetFlags),
- TEST_DECL(test_wc_Sha512FinalRaw),
- TEST_DECL(test_wc_Sha512Free),
- TEST_DECL(test_wc_Sha512GetHash),
- TEST_DECL(test_wc_Sha512Copy),
- TEST_DECL(test_wc_InitSha512_224),
- TEST_DECL(test_wc_Sha512_224Update),
- TEST_DECL(test_wc_Sha512_224Final),
- TEST_DECL(test_wc_Sha512_224GetFlags),
- TEST_DECL(test_wc_Sha512_224FinalRaw),
- TEST_DECL(test_wc_Sha512_224Free),
- TEST_DECL(test_wc_Sha512_224GetHash),
- TEST_DECL(test_wc_Sha512_224Copy),
- TEST_DECL(test_wc_InitSha512_256),
- TEST_DECL(test_wc_Sha512_256Update),
- TEST_DECL(test_wc_Sha512_256Final),
- TEST_DECL(test_wc_Sha512_256GetFlags),
- TEST_DECL(test_wc_Sha512_256FinalRaw),
- TEST_DECL(test_wc_Sha512_256Free),
- TEST_DECL(test_wc_Sha512_256GetHash),
- TEST_DECL(test_wc_Sha512_256Copy),
- TEST_DECL(test_wc_InitSha384),
- TEST_DECL(test_wc_Sha384Update),
- TEST_DECL(test_wc_Sha384Final),
- TEST_DECL(test_wc_Sha384GetFlags),
- TEST_DECL(test_wc_Sha384FinalRaw),
- TEST_DECL(test_wc_Sha384Free),
- TEST_DECL(test_wc_Sha384GetHash),
- TEST_DECL(test_wc_Sha384Copy),
- TEST_DECL(test_wc_InitSha224),
- TEST_DECL(test_wc_Sha224Update),
- TEST_DECL(test_wc_Sha224Final),
- TEST_DECL(test_wc_Sha224SetFlags),
- TEST_DECL(test_wc_Sha224GetFlags),
- TEST_DECL(test_wc_Sha224Free),
- TEST_DECL(test_wc_Sha224GetHash),
- TEST_DECL(test_wc_Sha224Copy),
- TEST_DECL(test_wc_InitBlake2b),
- TEST_DECL(test_wc_InitBlake2b_WithKey),
- TEST_DECL(test_wc_InitBlake2s_WithKey),
- TEST_DECL(test_wc_InitRipeMd),
- TEST_DECL(test_wc_RipeMdUpdate),
- TEST_DECL(test_wc_RipeMdFinal),
- TEST_DECL(test_wc_InitSha3),
- TEST_DECL(testing_wc_Sha3_Update),
- TEST_DECL(test_wc_Sha3_224_Final),
- TEST_DECL(test_wc_Sha3_256_Final),
- TEST_DECL(test_wc_Sha3_384_Final),
- TEST_DECL(test_wc_Sha3_512_Final),
- TEST_DECL(test_wc_Sha3_224_Copy),
- TEST_DECL(test_wc_Sha3_256_Copy),
- TEST_DECL(test_wc_Sha3_384_Copy),
- TEST_DECL(test_wc_Sha3_512_Copy),
- TEST_DECL(test_wc_Sha3_GetFlags),
- TEST_DECL(test_wc_InitShake256),
- TEST_DECL(testing_wc_Shake256_Update),
- TEST_DECL(test_wc_Shake256_Final),
- TEST_DECL(test_wc_Shake256_Copy),
- TEST_DECL(test_wc_Shake256Hash),
- TEST_DECL(test_wc_Md5HmacSetKey),
- TEST_DECL(test_wc_Md5HmacUpdate),
- TEST_DECL(test_wc_Md5HmacFinal),
- TEST_DECL(test_wc_ShaHmacSetKey),
- TEST_DECL(test_wc_ShaHmacUpdate),
- TEST_DECL(test_wc_ShaHmacFinal),
- TEST_DECL(test_wc_Sha224HmacSetKey),
- TEST_DECL(test_wc_Sha224HmacUpdate),
- TEST_DECL(test_wc_Sha224HmacFinal),
- TEST_DECL(test_wc_Sha256HmacSetKey),
- TEST_DECL(test_wc_Sha256HmacUpdate),
- TEST_DECL(test_wc_Sha256HmacFinal),
- TEST_DECL(test_wc_Sha384HmacSetKey),
- TEST_DECL(test_wc_Sha384HmacUpdate),
- TEST_DECL(test_wc_Sha384HmacFinal),
- TEST_DECL(test_wc_HashInit),
- TEST_DECL(test_wc_HashSetFlags),
- TEST_DECL(test_wc_HashGetFlags),
- TEST_DECL(test_wc_InitCmac),
- TEST_DECL(test_wc_CmacUpdate),
- TEST_DECL(test_wc_CmacFinal),
- TEST_DECL(test_wc_AesCmacGenerate),
- TEST_DECL(test_wc_AesGcmStream),
- TEST_DECL(test_wc_Des3_SetIV),
- TEST_DECL(test_wc_Des3_SetKey),
- TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
- TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
- TEST_DECL(test_wc_Des3_EcbEncrypt),
- TEST_DECL(test_wc_Chacha_SetKey),
- TEST_DECL(test_wc_Chacha_Process),
- TEST_DECL(test_wc_ChaCha20Poly1305_aead),
- TEST_DECL(test_wc_Poly1305SetKey),
- TEST_DECL(test_wc_CamelliaSetKey),
- TEST_DECL(test_wc_CamelliaSetIV),
- TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
- TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
- TEST_DECL(test_wc_Arc4SetKey),
- TEST_DECL(test_wc_Arc4Process),
- TEST_DECL(test_wc_Rc2SetKey),
- TEST_DECL(test_wc_Rc2SetIV),
- TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
- TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
- TEST_DECL(test_wc_AesSetKey),
- TEST_DECL(test_wc_AesSetIV),
- TEST_DECL(test_wc_AesCbcEncryptDecrypt),
- TEST_DECL(test_wc_AesCtrEncryptDecrypt),
- TEST_DECL(test_wc_AesGcmSetKey),
- TEST_DECL(test_wc_AesGcmEncryptDecrypt),
- TEST_DECL(test_wc_GmacSetKey),
- TEST_DECL(test_wc_GmacUpdate),
- TEST_DECL(test_wc_InitRsaKey),
- TEST_DECL(test_wc_RsaPrivateKeyDecode),
- TEST_DECL(test_wc_RsaPublicKeyDecode),
- TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
- TEST_DECL(test_wc_MakeRsaKey),
- TEST_DECL(test_wc_SetKeyUsage),
- TEST_DECL(test_wc_CheckProbablePrime),
- TEST_DECL(test_wc_RsaPSS_Verify),
- TEST_DECL(test_wc_RsaPSS_VerifyCheck),
- TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
- TEST_DECL(test_wc_SetMutexCb),
- TEST_DECL(test_wc_LockMutex_ex),
- TEST_DECL(test_wc_RsaKeyToDer),
- TEST_DECL(test_wc_RsaKeyToPublicDer),
- TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
- TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
- TEST_DECL(test_wc_RsaEncryptSize),
- TEST_DECL(test_wc_RsaSSL_SignVerify),
- TEST_DECL(test_wc_RsaFlattenPublicKey),
- TEST_DECL(test_RsaDecryptBoundsCheck),
- TEST_DECL(test_wc_AesCcmSetKey),
- TEST_DECL(test_wc_AesCcmEncryptDecrypt),
- TEST_DECL(test_wc_InitDsaKey),
- TEST_DECL(test_wc_DsaSignVerify),
- TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
- TEST_DECL(test_wc_MakeDsaKey),
- TEST_DECL(test_wc_DsaKeyToDer),
- TEST_DECL(test_wc_DsaKeyToPublicDer),
- TEST_DECL(test_wc_DsaImportParamsRaw),
- TEST_DECL(test_wc_DsaImportParamsRawCheck),
- TEST_DECL(test_wc_DsaExportParamsRaw),
- TEST_DECL(test_wc_DsaExportKeyRaw),
- TEST_DECL(test_wc_SignatureGetSize_ecc),
- TEST_DECL(test_wc_SignatureGetSize_rsa),
- /*
- * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
- * avoid memory leaks.
- */
- TEST_DECL(test_wolfCrypt_Cleanup),
- #ifdef OPENSSL_EXTRA
- TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
- TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
- TEST_DECL(test_wolfSSL_EC),
- TEST_DECL(test_wolfSSL_ECDSA_SIG),
- TEST_DECL(test_ECDSA_size_sign),
- TEST_DECL(test_ED25519),
- TEST_DECL(test_ED448),
- TEST_DECL(test_EC_i2d),
- #endif
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
- !defined(HAVE_SELFTEST) && \
- !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
- TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
- #endif
- #ifdef HAVE_HASHDRBG
- #ifdef TEST_RESEED_INTERVAL
- TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
- #endif
- TEST_DECL(test_wc_RNG_GenerateBlock),
- #endif
- TEST_DECL(test_get_rand_digit),
- TEST_DECL(test_get_digit_count),
- TEST_DECL(test_mp_cond_copy),
- TEST_DECL(test_mp_rand),
- TEST_DECL(test_get_digit),
- TEST_DECL(test_wc_export_int),
- TEST_DECL(test_wc_InitRngNonce),
- TEST_DECL(test_wc_InitRngNonce_ex),
- TEST_DECL(test_wc_ed25519_make_key),
- TEST_DECL(test_wc_ed25519_init),
- TEST_DECL(test_wc_ed25519_sign_msg),
- TEST_DECL(test_wc_ed25519_import_public),
- TEST_DECL(test_wc_ed25519_import_private_key),
- TEST_DECL(test_wc_ed25519_export),
- TEST_DECL(test_wc_ed25519_size),
- TEST_DECL(test_wc_ed25519_exportKey),
- TEST_DECL(test_wc_Ed25519PublicKeyToDer),
- TEST_DECL(test_wc_curve25519_init),
- TEST_DECL(test_wc_curve25519_size),
- TEST_DECL(test_wc_curve25519_export_key_raw),
- TEST_DECL(test_wc_curve25519_export_key_raw_ex),
- TEST_DECL(test_wc_curve25519_make_key),
- TEST_DECL(test_wc_curve25519_shared_secret_ex),
- TEST_DECL(test_wc_curve25519_make_pub),
- TEST_DECL(test_wc_curve25519_export_public_ex),
- TEST_DECL(test_wc_curve25519_export_private_raw_ex),
- TEST_DECL(test_wc_curve25519_import_private_raw_ex),
- TEST_DECL(test_wc_curve25519_import_private),
- TEST_DECL(test_wc_ed448_make_key),
- TEST_DECL(test_wc_ed448_init),
- TEST_DECL(test_wc_ed448_sign_msg),
- TEST_DECL(test_wc_ed448_import_public),
- TEST_DECL(test_wc_ed448_import_private_key),
- TEST_DECL(test_wc_ed448_export),
- TEST_DECL(test_wc_ed448_size),
- TEST_DECL(test_wc_ed448_exportKey),
- TEST_DECL(test_wc_Ed448PublicKeyToDer),
- TEST_DECL(test_wc_curve448_make_key),
- TEST_DECL(test_wc_curve448_shared_secret_ex),
- TEST_DECL(test_wc_curve448_export_public_ex),
- TEST_DECL(test_wc_curve448_export_private_raw_ex),
- TEST_DECL(test_wc_curve448_export_key_raw),
- TEST_DECL(test_wc_curve448_import_private_raw_ex),
- TEST_DECL(test_wc_curve448_import_private),
- TEST_DECL(test_wc_curve448_init),
- TEST_DECL(test_wc_curve448_size),
- TEST_DECL(test_wc_ecc_make_key),
- TEST_DECL(test_wc_ecc_init),
- TEST_DECL(test_wc_ecc_check_key),
- TEST_DECL(test_wc_ecc_get_generator),
- TEST_DECL(test_wc_ecc_size),
- TEST_DECL(test_wc_ecc_params),
- TEST_DECL(test_wc_ecc_signVerify_hash),
- TEST_DECL(test_wc_ecc_shared_secret),
- TEST_DECL(test_wc_ecc_export_x963),
- TEST_DECL(test_wc_ecc_export_x963_ex),
- TEST_DECL(test_wc_ecc_import_x963),
- TEST_DECL(ecc_import_private_key),
- TEST_DECL(test_wc_ecc_export_private_only),
- TEST_DECL(test_wc_ecc_rs_to_sig),
- TEST_DECL(test_wc_ecc_import_raw),
- TEST_DECL(test_wc_ecc_import_unsigned),
- TEST_DECL(test_wc_ecc_sig_size),
- TEST_DECL(test_wc_ecc_ctx_new),
- TEST_DECL(test_wc_ecc_ctx_reset),
- TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
- TEST_DECL(test_wc_ecc_ctx_set_info),
- TEST_DECL(test_wc_ecc_encryptDecrypt),
- TEST_DECL(test_wc_ecc_del_point),
- TEST_DECL(test_wc_ecc_pointFns),
- TEST_DECL(test_wc_ecc_shared_secret_ssh),
- TEST_DECL(test_wc_ecc_verify_hash_ex),
- TEST_DECL(test_wc_ecc_mulmod),
- TEST_DECL(test_wc_ecc_is_valid_idx),
- TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
- TEST_DECL(test_wc_ecc_sig_size_calc),
- TEST_DECL(test_ToTraditional),
- TEST_DECL(test_wc_EccPrivateKeyToDer),
- TEST_DECL(test_wc_DhPublicKeyDecode),
- TEST_DECL(test_wc_Ed25519KeyToDer),
- TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
- TEST_DECL(test_wc_Ed448KeyToDer),
- TEST_DECL(test_wc_Ed448PrivateKeyToDer),
- TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
- TEST_DECL(test_wc_SetSubjectBuffer),
- TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
- TEST_DECL(test_wc_PKCS7_New),
- TEST_DECL(test_wc_PKCS7_Init),
- TEST_DECL(test_wc_PKCS7_InitWithCert),
- TEST_DECL(test_wc_PKCS7_EncodeData),
- TEST_DECL(test_wc_PKCS7_EncodeSignedData),
- TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
- TEST_DECL(test_wc_PKCS7_VerifySignedData),
- TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
- TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
- TEST_DECL(test_wc_PKCS7_Degenerate),
- TEST_DECL(test_wc_PKCS7_BER),
- TEST_DECL(test_PKCS7_signed_enveloped),
- TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
- TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
- TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
- TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
- TEST_DECL(test_wc_i2d_PKCS12),
- TEST_DECL(test_wolfSSL_CTX_LoadCRL),
- TEST_DECL(test_openssl_FIPS_drbg),
- TEST_DECL(test_wc_CryptoCb),
- TEST_DECL(test_wolfSSL_CTX_StaticMemory),
- TEST_DECL(test_wolfSSL_FIPS_mode),
- #ifdef WOLFSSL_DTLS
- TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
- TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
- #endif
- #if !defined(NO_FILESYSTEM) && \
- defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
- !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
- TEST_DECL(test_wolfSSL_dtls_stateless_resume),
- #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
- #ifdef HAVE_MAX_FRAGMENT
- TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
- #endif /* HAVE_MAX_FRAGMENT */
- TEST_DECL(test_wolfSSL_dtls_stateless2),
- #if !defined(NO_OLD_TLS)
- TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
- #endif /* !defined(NO_OLD_TLS) */
- #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
- * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
- TEST_DECL(test_WOLFSSL_dtls_version_alert),
- TEST_DECL(test_ForceZero),
- TEST_DECL(test_wolfSSL_Cleanup),
- #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
- && defined(WOLFSSL_TLS13) && \
- (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
- TEST_DECL(test_ticket_nonce_malloc),
- #endif
- #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
- !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
- !defined(WOLFSSL_NO_CLIENT_AUTH))
- TEST_DECL(test_various_pathlen_chains),
- #endif
- /* If at some point a stub get implemented this test should fail indicating
- * a need to implement a new test case
- */
- TEST_DECL(test_stubs_are_stubs)
- };
- #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
- static void TestSetup(void)
- {
- /* Stub, for now. Add common test setup code here. */
- }
- static void TestCleanup(void)
- {
- #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
- /* Clear any errors added to the error queue during the test run. */
- wolfSSL_ERR_clear_error();
- #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
- }
- /* Print out all API test cases with numeric identifier.
- */
- void ApiTest_PrintTestCases(void)
- {
- int i;
- printf("All Test Cases:");
- for (i = 0; i < TEST_CASE_CNT; i++) {
- printf("%3d: %s\n", i + 1, testCases[i].name);
- }
- }
- /* Add test case with index to the list to run.
- *
- * @param [in] idx Index of test case to run starting at 1.
- * @return 0 on success.
- * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
- */
- int ApiTest_RunIdx(int idx)
- {
- if (idx < 1 || idx > TEST_CASE_CNT) {
- printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
- return BAD_FUNC_ARG;
- }
- testAll = 0;
- testCases[idx-1].run = 1;
- return 0;
- }
- /* Add test case with name to the list to run.
- *
- * @param [in] name Name of test case to run.
- * @return 0 on success.
- * @return BAD_FUNC_ARG when name is not a known test case name.
- */
- int ApiTest_RunName(char* name)
- {
- int i;
- for (i = 0; i < TEST_CASE_CNT; i++) {
- if (XSTRCMP(testCases[i].name, name) == 0) {
- testAll = 0;
- testCases[i].run = 1;
- return 0;
- }
- }
- printf("Test case name not found: %s\n", name);
- printf("Use -list to see all test case names.\n");
- return BAD_FUNC_ARG;
- }
- /* Converts the result code to a string.
- *
- * @param [in] res Test result code.
- * @return String describing test result.
- */
- static const char* apitest_res_string(int res)
- {
- const char* str = "invalid result";
- switch (res) {
- case TEST_SUCCESS:
- str = "passed";
- break;
- case TEST_FAIL:
- str = "failed";
- break;
- case TEST_SKIPPED:
- str = "skipped";
- break;
- }
- return str;
- }
- #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
- static double gettime_secs(void)
- {
- struct timeval tv;
- LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
- return (double)tv.tv_sec + (double)tv.tv_usec / 1000000;
- }
- #endif
- void ApiTest(void)
- {
- int i;
- int ret;
- #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
- double timeDiff;
- #endif
- printf(" Begin API Tests\n");
- fflush(stdout);
- for (i = 0; i < TEST_CASE_CNT; ++i) {
- /* When not testing all cases then skip if not marked for running. */
- if (!testAll && !testCases[i].run) {
- continue;
- }
- TestSetup();
- printf(" %3d: %-52s:", i + 1, testCases[i].name);
- fflush(stdout);
- #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
- timeDiff = gettime_secs();
- #endif
- ret = testCases[i].func();
- #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
- timeDiff = gettime_secs() - timeDiff;
- #endif
- #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
- if (ret != TEST_SKIPPED) {
- printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
- }
- else
- #endif
- {
- printf(" %s\n", apitest_res_string(ret));
- }
- fflush(stdout);
- /* if return code is < 0 and not skipped then assert error */
- Assert((ret > 0 || ret == TEST_SKIPPED),
- ("Test failed\n"),
- ("ret %d", ret));
- TestCleanup();
- }
- #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
- && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- wolfSSL_Cleanup();
- (void)testDevId;
- printf(" End API Tests\n");
- fflush(stdout);
- }
|