12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491384923849338494384953849638497384983849938500385013850238503385043850538506385073850838509385103851138512385133851438515385163851738518385193852038521385223852338524385253852638527385283852938530385313853238533385343853538536385373853838539385403854138542385433854438545385463854738548385493855038551385523855338554385553855638557385583855938560385613856238563385643856538566385673856838569385703857138572385733857438575385763857738578385793858038581385823858338584385853858638587385883858938590385913859238593385943859538596385973859838599386003860138602386033860438605386063860738608386093861038611386123861338614386153861638617386183861938620386213862238623386243862538626386273862838629386303863138632386333863438635386363863738638386393864038641386423864338644386453864638647386483864938650386513865238653386543865538656386573865838659386603866138662386633866438665386663866738668386693867038671386723867338674386753867638677386783867938680386813868238683386843868538686386873868838689386903869138692386933869438695386963869738698386993870038701387023870338704387053870638707387083870938710387113871238713387143871538716387173871838719387203872138722387233872438725387263872738728387293873038731387323873338734387353873638737387383873938740387413874238743387443874538746387473874838749387503875138752387533875438755387563875738758387593876038761387623876338764387653876638767387683876938770387713877238773387743877538776387773877838779387803878138782387833878438785387863878738788387893879038791387923879338794387953879638797387983879938800388013880238803388043880538806388073880838809388103881138812388133881438815388163881738818388193882038821388223882338824388253882638827388283882938830388313883238833388343883538836388373883838839388403884138842388433884438845388463884738848388493885038851388523885338854388553885638857388583885938860388613886238863388643886538866388673886838869388703887138872388733887438875388763887738878388793888038881388823888338884388853888638887388883888938890388913889238893388943889538896388973889838899389003890138902389033890438905389063890738908389093891038911389123891338914389153891638917389183891938920389213892238923389243892538926389273892838929389303893138932389333893438935389363893738938389393894038941389423894338944389453894638947389483894938950389513895238953389543895538956389573895838959389603896138962389633896438965389663896738968389693897038971389723897338974389753897638977389783897938980389813898238983389843898538986389873898838989389903899138992389933899438995389963899738998389993900039001390023900339004390053900639007390083900939010390113901239013390143901539016390173901839019390203902139022390233902439025390263902739028390293903039031390323903339034390353903639037390383903939040390413904239043390443904539046390473904839049390503905139052390533905439055390563905739058390593906039061390623906339064390653906639067390683906939070390713907239073390743907539076390773907839079390803908139082390833908439085390863908739088390893909039091390923909339094390953909639097390983909939100391013910239103391043910539106391073910839109391103911139112391133911439115391163911739118391193912039121391223912339124391253912639127391283912939130391313913239133391343913539136391373913839139391403914139142391433914439145391463914739148391493915039151391523915339154391553915639157391583915939160391613916239163391643916539166391673916839169391703917139172391733917439175391763917739178391793918039181391823918339184391853918639187391883918939190391913919239193391943919539196391973919839199392003920139202392033920439205392063920739208392093921039211392123921339214392153921639217392183921939220392213922239223392243922539226392273922839229392303923139232392333923439235392363923739238392393924039241392423924339244392453924639247392483924939250392513925239253392543925539256392573925839259392603926139262392633926439265392663926739268392693927039271392723927339274392753927639277392783927939280392813928239283392843928539286392873928839289392903929139292392933929439295392963929739298392993930039301393023930339304393053930639307393083930939310393113931239313393143931539316393173931839319393203932139322393233932439325393263932739328393293933039331393323933339334393353933639337393383933939340393413934239343393443934539346393473934839349393503935139352393533935439355393563935739358393593936039361393623936339364393653936639367393683936939370393713937239373393743937539376393773937839379393803938139382393833938439385393863938739388393893939039391393923939339394393953939639397393983939939400394013940239403394043940539406394073940839409394103941139412394133941439415394163941739418394193942039421394223942339424394253942639427394283942939430394313943239433394343943539436394373943839439394403944139442394433944439445394463944739448394493945039451394523945339454394553945639457394583945939460394613946239463394643946539466394673946839469394703947139472394733947439475394763947739478394793948039481394823948339484394853948639487394883948939490394913949239493394943949539496394973949839499395003950139502395033950439505395063950739508395093951039511395123951339514395153951639517395183951939520395213952239523395243952539526395273952839529395303953139532395333953439535395363953739538395393954039541395423954339544395453954639547395483954939550395513955239553395543955539556395573955839559395603956139562395633956439565395663956739568395693957039571395723957339574395753957639577395783957939580395813958239583395843958539586395873958839589395903959139592395933959439595395963959739598395993960039601396023960339604396053960639607396083960939610396113961239613396143961539616396173961839619396203962139622396233962439625396263962739628396293963039631396323963339634396353963639637396383963939640396413964239643396443964539646396473964839649396503965139652396533965439655396563965739658396593966039661396623966339664396653966639667396683966939670396713967239673396743967539676396773967839679396803968139682396833968439685396863968739688396893969039691396923969339694396953969639697396983969939700397013970239703397043970539706397073970839709397103971139712397133971439715397163971739718397193972039721397223972339724397253972639727397283972939730397313973239733397343973539736397373973839739397403974139742397433974439745397463974739748397493975039751397523975339754397553975639757397583975939760397613976239763397643976539766397673976839769397703977139772397733977439775397763977739778397793978039781397823978339784397853978639787397883978939790397913979239793397943979539796397973979839799398003980139802398033980439805398063980739808398093981039811398123981339814398153981639817398183981939820398213982239823398243982539826398273982839829398303983139832398333983439835398363983739838398393984039841398423984339844398453984639847398483984939850398513985239853398543985539856398573985839859398603986139862398633986439865398663986739868398693987039871398723987339874398753987639877398783987939880398813988239883398843988539886398873988839889398903989139892398933989439895398963989739898398993990039901399023990339904399053990639907399083990939910399113991239913399143991539916399173991839919399203992139922399233992439925399263992739928399293993039931399323993339934399353993639937399383993939940399413994239943399443994539946399473994839949399503995139952399533995439955399563995739958399593996039961399623996339964399653996639967399683996939970399713997239973399743997539976399773997839979399803998139982399833998439985399863998739988399893999039991399923999339994399953999639997399983999940000400014000240003400044000540006400074000840009400104001140012400134001440015400164001740018400194002040021400224002340024400254002640027400284002940030400314003240033400344003540036400374003840039400404004140042400434004440045400464004740048400494005040051400524005340054400554005640057400584005940060400614006240063400644006540066400674006840069400704007140072400734007440075400764007740078400794008040081400824008340084400854008640087400884008940090400914009240093400944009540096400974009840099401004010140102401034010440105401064010740108401094011040111401124011340114401154011640117401184011940120401214012240123401244012540126401274012840129401304013140132401334013440135401364013740138401394014040141401424014340144401454014640147401484014940150401514015240153401544015540156401574015840159401604016140162401634016440165401664016740168401694017040171401724017340174401754017640177401784017940180401814018240183401844018540186401874018840189401904019140192401934019440195401964019740198401994020040201402024020340204402054020640207402084020940210402114021240213402144021540216402174021840219402204022140222402234022440225402264022740228402294023040231402324023340234402354023640237402384023940240402414024240243402444024540246402474024840249402504025140252402534025440255402564025740258402594026040261402624026340264402654026640267402684026940270402714027240273402744027540276402774027840279402804028140282402834028440285402864028740288402894029040291402924029340294402954029640297402984029940300403014030240303403044030540306403074030840309403104031140312403134031440315403164031740318403194032040321403224032340324403254032640327403284032940330403314033240333403344033540336403374033840339403404034140342403434034440345403464034740348403494035040351403524035340354403554035640357403584035940360403614036240363403644036540366403674036840369403704037140372403734037440375403764037740378403794038040381403824038340384403854038640387403884038940390403914039240393403944039540396403974039840399404004040140402404034040440405404064040740408404094041040411404124041340414404154041640417404184041940420404214042240423404244042540426404274042840429404304043140432404334043440435404364043740438404394044040441404424044340444404454044640447404484044940450404514045240453404544045540456404574045840459404604046140462404634046440465404664046740468404694047040471404724047340474404754047640477404784047940480404814048240483404844048540486404874048840489404904049140492404934049440495404964049740498404994050040501405024050340504405054050640507405084050940510405114051240513405144051540516405174051840519405204052140522405234052440525405264052740528405294053040531405324053340534405354053640537405384053940540405414054240543405444054540546405474054840549405504055140552405534055440555405564055740558405594056040561405624056340564405654056640567405684056940570405714057240573405744057540576405774057840579405804058140582405834058440585405864058740588405894059040591405924059340594405954059640597405984059940600406014060240603406044060540606406074060840609406104061140612406134061440615406164061740618406194062040621406224062340624406254062640627406284062940630406314063240633406344063540636406374063840639406404064140642406434064440645406464064740648406494065040651406524065340654406554065640657406584065940660406614066240663406644066540666406674066840669406704067140672406734067440675406764067740678406794068040681406824068340684406854068640687406884068940690406914069240693406944069540696406974069840699407004070140702407034070440705407064070740708407094071040711407124071340714407154071640717407184071940720407214072240723407244072540726407274072840729407304073140732407334073440735407364073740738407394074040741407424074340744407454074640747407484074940750407514075240753407544075540756407574075840759407604076140762407634076440765407664076740768407694077040771407724077340774407754077640777407784077940780407814078240783407844078540786407874078840789407904079140792407934079440795407964079740798407994080040801408024080340804408054080640807408084080940810408114081240813408144081540816408174081840819408204082140822408234082440825408264082740828408294083040831408324083340834408354083640837408384083940840408414084240843408444084540846408474084840849408504085140852408534085440855408564085740858408594086040861408624086340864408654086640867408684086940870408714087240873408744087540876408774087840879408804088140882408834088440885408864088740888408894089040891408924089340894408954089640897408984089940900409014090240903409044090540906409074090840909409104091140912409134091440915409164091740918409194092040921409224092340924409254092640927409284092940930409314093240933409344093540936409374093840939409404094140942409434094440945409464094740948409494095040951409524095340954409554095640957409584095940960409614096240963409644096540966409674096840969409704097140972409734097440975409764097740978409794098040981409824098340984409854098640987409884098940990409914099240993409944099540996409974099840999410004100141002410034100441005410064100741008410094101041011410124101341014410154101641017410184101941020410214102241023410244102541026410274102841029410304103141032410334103441035410364103741038410394104041041410424104341044410454104641047410484104941050410514105241053410544105541056410574105841059410604106141062410634106441065410664106741068410694107041071410724107341074410754107641077410784107941080410814108241083410844108541086410874108841089410904109141092410934109441095410964109741098410994110041101411024110341104411054110641107411084110941110411114111241113411144111541116411174111841119411204112141122411234112441125411264112741128411294113041131411324113341134411354113641137411384113941140411414114241143411444114541146411474114841149411504115141152411534115441155411564115741158411594116041161411624116341164411654116641167411684116941170411714117241173411744117541176411774117841179411804118141182411834118441185411864118741188411894119041191411924119341194411954119641197411984119941200412014120241203412044120541206412074120841209412104121141212412134121441215412164121741218412194122041221412224122341224412254122641227412284122941230412314123241233412344123541236412374123841239412404124141242412434124441245412464124741248412494125041251412524125341254412554125641257412584125941260412614126241263412644126541266412674126841269412704127141272412734127441275412764127741278412794128041281412824128341284412854128641287412884128941290412914129241293412944129541296412974129841299413004130141302413034130441305413064130741308413094131041311413124131341314413154131641317413184131941320413214132241323413244132541326413274132841329413304133141332413334133441335413364133741338413394134041341413424134341344413454134641347413484134941350413514135241353413544135541356413574135841359413604136141362413634136441365413664136741368413694137041371413724137341374413754137641377413784137941380413814138241383413844138541386413874138841389413904139141392413934139441395413964139741398413994140041401414024140341404414054140641407414084140941410414114141241413414144141541416414174141841419414204142141422414234142441425414264142741428414294143041431414324143341434414354143641437414384143941440414414144241443414444144541446414474144841449414504145141452414534145441455414564145741458414594146041461414624146341464414654146641467414684146941470414714147241473414744147541476414774147841479414804148141482414834148441485414864148741488414894149041491414924149341494414954149641497414984149941500415014150241503415044150541506415074150841509415104151141512415134151441515415164151741518415194152041521415224152341524415254152641527415284152941530415314153241533415344153541536415374153841539415404154141542415434154441545415464154741548415494155041551415524155341554415554155641557415584155941560415614156241563415644156541566415674156841569415704157141572415734157441575415764157741578415794158041581415824158341584415854158641587415884158941590415914159241593415944159541596415974159841599416004160141602416034160441605416064160741608416094161041611416124161341614416154161641617416184161941620416214162241623416244162541626416274162841629416304163141632416334163441635416364163741638416394164041641416424164341644416454164641647416484164941650416514165241653416544165541656416574165841659416604166141662416634166441665416664166741668416694167041671416724167341674416754167641677416784167941680416814168241683416844168541686416874168841689416904169141692416934169441695416964169741698416994170041701417024170341704417054170641707417084170941710417114171241713417144171541716417174171841719417204172141722417234172441725417264172741728417294173041731417324173341734417354173641737417384173941740417414174241743417444174541746417474174841749417504175141752417534175441755417564175741758417594176041761417624176341764417654176641767417684176941770417714177241773417744177541776417774177841779417804178141782417834178441785417864178741788417894179041791417924179341794417954179641797417984179941800418014180241803418044180541806418074180841809418104181141812418134181441815418164181741818418194182041821418224182341824418254182641827418284182941830418314183241833418344183541836418374183841839418404184141842418434184441845418464184741848418494185041851418524185341854418554185641857418584185941860418614186241863418644186541866418674186841869418704187141872418734187441875418764187741878418794188041881418824188341884418854188641887418884188941890418914189241893418944189541896418974189841899419004190141902419034190441905419064190741908419094191041911419124191341914419154191641917419184191941920419214192241923419244192541926419274192841929419304193141932419334193441935419364193741938419394194041941419424194341944419454194641947419484194941950419514195241953419544195541956419574195841959419604196141962419634196441965419664196741968419694197041971419724197341974419754197641977419784197941980419814198241983419844198541986419874198841989419904199141992419934199441995419964199741998419994200042001420024200342004420054200642007420084200942010420114201242013420144201542016420174201842019420204202142022420234202442025420264202742028420294203042031420324203342034420354203642037420384203942040420414204242043420444204542046420474204842049420504205142052420534205442055420564205742058420594206042061420624206342064420654206642067420684206942070420714207242073420744207542076420774207842079420804208142082420834208442085420864208742088420894209042091420924209342094420954209642097420984209942100421014210242103421044210542106421074210842109421104211142112421134211442115421164211742118421194212042121421224212342124421254212642127421284212942130421314213242133421344213542136421374213842139421404214142142421434214442145421464214742148421494215042151421524215342154421554215642157421584215942160421614216242163421644216542166421674216842169421704217142172421734217442175421764217742178421794218042181421824218342184421854218642187421884218942190421914219242193421944219542196421974219842199422004220142202422034220442205422064220742208422094221042211422124221342214422154221642217422184221942220422214222242223422244222542226422274222842229422304223142232422334223442235422364223742238422394224042241422424224342244422454224642247422484224942250422514225242253422544225542256422574225842259422604226142262422634226442265422664226742268422694227042271422724227342274422754227642277422784227942280422814228242283422844228542286422874228842289422904229142292422934229442295422964229742298422994230042301423024230342304423054230642307423084230942310423114231242313423144231542316423174231842319423204232142322423234232442325423264232742328423294233042331423324233342334423354233642337423384233942340423414234242343423444234542346423474234842349423504235142352423534235442355423564235742358423594236042361423624236342364423654236642367423684236942370423714237242373423744237542376423774237842379423804238142382423834238442385423864238742388423894239042391423924239342394423954239642397423984239942400424014240242403424044240542406424074240842409424104241142412424134241442415424164241742418424194242042421424224242342424424254242642427424284242942430424314243242433424344243542436424374243842439424404244142442424434244442445424464244742448424494245042451424524245342454424554245642457424584245942460424614246242463424644246542466424674246842469424704247142472424734247442475424764247742478424794248042481424824248342484424854248642487424884248942490424914249242493424944249542496424974249842499425004250142502425034250442505425064250742508425094251042511425124251342514425154251642517425184251942520425214252242523425244252542526425274252842529425304253142532425334253442535425364253742538425394254042541425424254342544425454254642547425484254942550425514255242553425544255542556425574255842559425604256142562425634256442565425664256742568425694257042571425724257342574425754257642577425784257942580425814258242583425844258542586425874258842589425904259142592425934259442595425964259742598425994260042601426024260342604426054260642607426084260942610426114261242613426144261542616426174261842619426204262142622426234262442625426264262742628426294263042631426324263342634426354263642637426384263942640426414264242643426444264542646426474264842649426504265142652426534265442655426564265742658426594266042661426624266342664426654266642667426684266942670426714267242673426744267542676426774267842679426804268142682426834268442685426864268742688426894269042691426924269342694426954269642697426984269942700427014270242703427044270542706427074270842709427104271142712427134271442715427164271742718427194272042721427224272342724427254272642727427284272942730427314273242733427344273542736427374273842739427404274142742427434274442745427464274742748427494275042751427524275342754427554275642757427584275942760427614276242763427644276542766427674276842769427704277142772427734277442775427764277742778427794278042781427824278342784427854278642787427884278942790427914279242793427944279542796427974279842799428004280142802428034280442805428064280742808428094281042811428124281342814428154281642817428184281942820428214282242823428244282542826428274282842829428304283142832428334283442835428364283742838428394284042841428424284342844428454284642847428484284942850428514285242853428544285542856428574285842859428604286142862428634286442865428664286742868428694287042871428724287342874428754287642877428784287942880428814288242883428844288542886428874288842889428904289142892428934289442895428964289742898428994290042901429024290342904429054290642907429084290942910429114291242913429144291542916429174291842919429204292142922429234292442925429264292742928429294293042931429324293342934429354293642937429384293942940429414294242943429444294542946429474294842949429504295142952429534295442955429564295742958429594296042961429624296342964429654296642967429684296942970429714297242973429744297542976429774297842979429804298142982429834298442985429864298742988429894299042991429924299342994429954299642997429984299943000430014300243003430044300543006430074300843009430104301143012430134301443015430164301743018430194302043021430224302343024430254302643027430284302943030430314303243033430344303543036430374303843039430404304143042430434304443045430464304743048430494305043051430524305343054430554305643057430584305943060430614306243063430644306543066430674306843069430704307143072430734307443075430764307743078430794308043081430824308343084430854308643087430884308943090430914309243093430944309543096430974309843099431004310143102431034310443105431064310743108431094311043111431124311343114431154311643117431184311943120431214312243123431244312543126431274312843129431304313143132431334313443135431364313743138431394314043141431424314343144431454314643147431484314943150431514315243153431544315543156431574315843159431604316143162431634316443165431664316743168431694317043171431724317343174431754317643177431784317943180431814318243183431844318543186431874318843189431904319143192431934319443195431964319743198431994320043201432024320343204432054320643207432084320943210432114321243213432144321543216432174321843219432204322143222432234322443225432264322743228432294323043231432324323343234432354323643237432384323943240432414324243243432444324543246432474324843249432504325143252432534325443255432564325743258432594326043261432624326343264432654326643267432684326943270432714327243273432744327543276432774327843279432804328143282432834328443285432864328743288432894329043291432924329343294432954329643297432984329943300433014330243303433044330543306433074330843309433104331143312433134331443315433164331743318433194332043321433224332343324433254332643327433284332943330433314333243333433344333543336433374333843339433404334143342433434334443345433464334743348433494335043351433524335343354433554335643357433584335943360433614336243363433644336543366433674336843369433704337143372433734337443375433764337743378433794338043381433824338343384433854338643387433884338943390433914339243393433944339543396433974339843399434004340143402434034340443405434064340743408434094341043411434124341343414434154341643417434184341943420434214342243423434244342543426434274342843429434304343143432434334343443435434364343743438434394344043441434424344343444434454344643447434484344943450434514345243453434544345543456434574345843459434604346143462434634346443465434664346743468434694347043471434724347343474434754347643477434784347943480434814348243483434844348543486434874348843489434904349143492434934349443495434964349743498434994350043501435024350343504435054350643507435084350943510435114351243513435144351543516435174351843519435204352143522435234352443525435264352743528435294353043531435324353343534435354353643537435384353943540435414354243543435444354543546435474354843549435504355143552435534355443555435564355743558435594356043561435624356343564435654356643567435684356943570435714357243573435744357543576435774357843579435804358143582435834358443585435864358743588435894359043591435924359343594435954359643597435984359943600436014360243603436044360543606436074360843609436104361143612436134361443615436164361743618436194362043621436224362343624436254362643627436284362943630436314363243633436344363543636436374363843639436404364143642436434364443645436464364743648436494365043651436524365343654436554365643657436584365943660436614366243663436644366543666436674366843669436704367143672436734367443675436764367743678436794368043681436824368343684436854368643687436884368943690436914369243693436944369543696436974369843699437004370143702437034370443705437064370743708437094371043711437124371343714437154371643717437184371943720437214372243723437244372543726437274372843729437304373143732437334373443735437364373743738437394374043741437424374343744437454374643747437484374943750437514375243753437544375543756437574375843759437604376143762437634376443765437664376743768437694377043771437724377343774437754377643777437784377943780437814378243783437844378543786437874378843789437904379143792437934379443795437964379743798437994380043801438024380343804438054380643807438084380943810438114381243813438144381543816438174381843819438204382143822438234382443825438264382743828438294383043831438324383343834438354383643837438384383943840438414384243843438444384543846438474384843849438504385143852438534385443855438564385743858438594386043861438624386343864438654386643867438684386943870438714387243873438744387543876438774387843879438804388143882438834388443885438864388743888438894389043891438924389343894438954389643897438984389943900439014390243903439044390543906439074390843909439104391143912439134391443915439164391743918439194392043921439224392343924439254392643927439284392943930439314393243933439344393543936439374393843939439404394143942439434394443945439464394743948439494395043951439524395343954439554395643957439584395943960439614396243963439644396543966439674396843969439704397143972439734397443975439764397743978439794398043981439824398343984439854398643987439884398943990439914399243993439944399543996439974399843999440004400144002440034400444005440064400744008440094401044011440124401344014440154401644017440184401944020440214402244023440244402544026440274402844029440304403144032440334403444035440364403744038440394404044041440424404344044440454404644047440484404944050440514405244053440544405544056440574405844059440604406144062440634406444065440664406744068440694407044071440724407344074440754407644077440784407944080440814408244083440844408544086440874408844089440904409144092440934409444095440964409744098440994410044101441024410344104441054410644107441084410944110441114411244113441144411544116441174411844119441204412144122441234412444125441264412744128441294413044131441324413344134441354413644137441384413944140441414414244143441444414544146441474414844149441504415144152441534415444155441564415744158441594416044161441624416344164441654416644167441684416944170441714417244173441744417544176441774417844179441804418144182441834418444185441864418744188441894419044191441924419344194441954419644197441984419944200442014420244203442044420544206442074420844209442104421144212442134421444215442164421744218442194422044221442224422344224442254422644227442284422944230442314423244233442344423544236442374423844239442404424144242442434424444245442464424744248442494425044251442524425344254442554425644257442584425944260442614426244263442644426544266442674426844269442704427144272442734427444275442764427744278442794428044281442824428344284442854428644287442884428944290442914429244293442944429544296442974429844299443004430144302443034430444305443064430744308443094431044311443124431344314443154431644317443184431944320443214432244323443244432544326443274432844329443304433144332443334433444335443364433744338443394434044341443424434344344443454434644347443484434944350443514435244353443544435544356443574435844359443604436144362443634436444365443664436744368443694437044371443724437344374443754437644377443784437944380443814438244383443844438544386443874438844389443904439144392443934439444395443964439744398443994440044401444024440344404444054440644407444084440944410444114441244413444144441544416444174441844419444204442144422444234442444425444264442744428444294443044431444324443344434444354443644437444384443944440444414444244443444444444544446444474444844449444504445144452444534445444455444564445744458444594446044461444624446344464444654446644467444684446944470444714447244473444744447544476444774447844479444804448144482444834448444485444864448744488444894449044491444924449344494444954449644497444984449944500445014450244503445044450544506445074450844509445104451144512445134451444515445164451744518445194452044521445224452344524445254452644527445284452944530445314453244533445344453544536445374453844539445404454144542445434454444545445464454744548445494455044551445524455344554445554455644557445584455944560445614456244563445644456544566445674456844569445704457144572445734457444575445764457744578445794458044581445824458344584445854458644587445884458944590445914459244593445944459544596445974459844599446004460144602446034460444605446064460744608446094461044611446124461344614446154461644617446184461944620446214462244623446244462544626446274462844629446304463144632446334463444635446364463744638446394464044641446424464344644446454464644647446484464944650446514465244653446544465544656446574465844659446604466144662446634466444665446664466744668446694467044671446724467344674446754467644677446784467944680446814468244683446844468544686446874468844689446904469144692446934469444695446964469744698446994470044701447024470344704447054470644707447084470944710447114471244713447144471544716447174471844719447204472144722447234472444725447264472744728447294473044731447324473344734447354473644737447384473944740447414474244743447444474544746447474474844749447504475144752447534475444755447564475744758447594476044761447624476344764447654476644767447684476944770447714477244773447744477544776447774477844779447804478144782447834478444785447864478744788447894479044791447924479344794447954479644797447984479944800448014480244803448044480544806448074480844809448104481144812448134481444815448164481744818448194482044821448224482344824448254482644827448284482944830448314483244833448344483544836448374483844839448404484144842448434484444845448464484744848448494485044851448524485344854448554485644857448584485944860448614486244863448644486544866448674486844869448704487144872448734487444875448764487744878448794488044881448824488344884448854488644887448884488944890448914489244893448944489544896448974489844899449004490144902449034490444905449064490744908449094491044911449124491344914449154491644917449184491944920449214492244923449244492544926449274492844929449304493144932449334493444935449364493744938449394494044941449424494344944449454494644947449484494944950449514495244953449544495544956449574495844959449604496144962449634496444965449664496744968449694497044971449724497344974449754497644977449784497944980449814498244983449844498544986449874498844989449904499144992449934499444995449964499744998449994500045001450024500345004450054500645007450084500945010450114501245013450144501545016450174501845019450204502145022450234502445025450264502745028450294503045031450324503345034450354503645037450384503945040450414504245043450444504545046450474504845049450504505145052450534505445055450564505745058450594506045061450624506345064450654506645067450684506945070450714507245073450744507545076450774507845079450804508145082450834508445085450864508745088450894509045091450924509345094450954509645097450984509945100451014510245103451044510545106451074510845109451104511145112451134511445115451164511745118451194512045121451224512345124451254512645127451284512945130451314513245133451344513545136451374513845139451404514145142451434514445145451464514745148451494515045151451524515345154451554515645157451584515945160451614516245163451644516545166451674516845169451704517145172451734517445175451764517745178451794518045181451824518345184451854518645187451884518945190451914519245193451944519545196451974519845199452004520145202452034520445205452064520745208452094521045211452124521345214452154521645217452184521945220452214522245223452244522545226452274522845229452304523145232452334523445235452364523745238452394524045241452424524345244452454524645247452484524945250452514525245253452544525545256452574525845259452604526145262452634526445265452664526745268452694527045271452724527345274452754527645277452784527945280452814528245283452844528545286452874528845289452904529145292452934529445295452964529745298452994530045301453024530345304453054530645307453084530945310453114531245313453144531545316453174531845319453204532145322453234532445325453264532745328453294533045331453324533345334453354533645337453384533945340453414534245343453444534545346453474534845349453504535145352453534535445355453564535745358453594536045361453624536345364453654536645367453684536945370453714537245373453744537545376453774537845379453804538145382453834538445385453864538745388453894539045391453924539345394453954539645397453984539945400454014540245403454044540545406454074540845409454104541145412454134541445415454164541745418454194542045421454224542345424454254542645427454284542945430454314543245433454344543545436454374543845439454404544145442454434544445445454464544745448454494545045451454524545345454454554545645457454584545945460454614546245463454644546545466454674546845469454704547145472454734547445475454764547745478454794548045481454824548345484454854548645487454884548945490454914549245493454944549545496454974549845499455004550145502455034550445505455064550745508455094551045511455124551345514455154551645517455184551945520455214552245523455244552545526455274552845529455304553145532455334553445535455364553745538455394554045541455424554345544455454554645547455484554945550455514555245553455544555545556455574555845559455604556145562455634556445565455664556745568455694557045571455724557345574455754557645577455784557945580455814558245583455844558545586455874558845589455904559145592455934559445595455964559745598455994560045601456024560345604456054560645607456084560945610456114561245613456144561545616456174561845619456204562145622456234562445625456264562745628456294563045631456324563345634456354563645637456384563945640456414564245643456444564545646456474564845649456504565145652456534565445655456564565745658456594566045661456624566345664456654566645667456684566945670456714567245673456744567545676456774567845679456804568145682456834568445685456864568745688456894569045691456924569345694456954569645697456984569945700457014570245703457044570545706457074570845709457104571145712457134571445715457164571745718457194572045721457224572345724457254572645727457284572945730457314573245733457344573545736457374573845739457404574145742457434574445745457464574745748457494575045751457524575345754457554575645757457584575945760457614576245763457644576545766457674576845769457704577145772457734577445775457764577745778457794578045781457824578345784457854578645787457884578945790457914579245793457944579545796457974579845799458004580145802458034580445805458064580745808458094581045811458124581345814458154581645817458184581945820458214582245823458244582545826458274582845829458304583145832458334583445835458364583745838458394584045841458424584345844458454584645847458484584945850458514585245853458544585545856458574585845859458604586145862458634586445865458664586745868458694587045871458724587345874458754587645877458784587945880458814588245883458844588545886458874588845889458904589145892458934589445895458964589745898458994590045901459024590345904459054590645907459084590945910459114591245913459144591545916459174591845919459204592145922459234592445925459264592745928459294593045931459324593345934459354593645937459384593945940459414594245943459444594545946459474594845949459504595145952459534595445955459564595745958459594596045961459624596345964459654596645967459684596945970459714597245973459744597545976459774597845979459804598145982459834598445985459864598745988459894599045991459924599345994459954599645997459984599946000460014600246003460044600546006460074600846009460104601146012460134601446015460164601746018460194602046021460224602346024460254602646027460284602946030460314603246033460344603546036460374603846039460404604146042460434604446045460464604746048460494605046051460524605346054460554605646057460584605946060460614606246063460644606546066460674606846069460704607146072460734607446075460764607746078460794608046081460824608346084460854608646087460884608946090460914609246093460944609546096460974609846099461004610146102461034610446105461064610746108461094611046111461124611346114461154611646117461184611946120461214612246123461244612546126461274612846129461304613146132461334613446135461364613746138461394614046141461424614346144461454614646147461484614946150461514615246153461544615546156461574615846159461604616146162461634616446165461664616746168461694617046171461724617346174461754617646177461784617946180461814618246183461844618546186461874618846189461904619146192461934619446195461964619746198461994620046201462024620346204462054620646207462084620946210462114621246213462144621546216462174621846219462204622146222462234622446225462264622746228462294623046231462324623346234462354623646237462384623946240462414624246243462444624546246462474624846249462504625146252462534625446255462564625746258462594626046261462624626346264462654626646267462684626946270462714627246273462744627546276462774627846279462804628146282462834628446285462864628746288462894629046291462924629346294462954629646297462984629946300463014630246303463044630546306463074630846309463104631146312463134631446315463164631746318463194632046321463224632346324463254632646327463284632946330463314633246333463344633546336463374633846339463404634146342463434634446345463464634746348463494635046351463524635346354463554635646357463584635946360463614636246363463644636546366463674636846369463704637146372463734637446375463764637746378463794638046381463824638346384463854638646387463884638946390463914639246393463944639546396463974639846399464004640146402464034640446405464064640746408464094641046411464124641346414464154641646417464184641946420464214642246423464244642546426464274642846429464304643146432464334643446435464364643746438464394644046441464424644346444464454644646447464484644946450464514645246453464544645546456464574645846459464604646146462464634646446465464664646746468464694647046471464724647346474464754647646477464784647946480464814648246483464844648546486464874648846489464904649146492464934649446495464964649746498464994650046501465024650346504465054650646507465084650946510465114651246513465144651546516465174651846519465204652146522465234652446525465264652746528465294653046531465324653346534465354653646537465384653946540465414654246543465444654546546465474654846549465504655146552465534655446555465564655746558465594656046561465624656346564465654656646567465684656946570465714657246573465744657546576465774657846579465804658146582465834658446585465864658746588465894659046591465924659346594465954659646597465984659946600466014660246603466044660546606466074660846609466104661146612466134661446615466164661746618466194662046621466224662346624466254662646627466284662946630466314663246633466344663546636466374663846639466404664146642466434664446645466464664746648466494665046651466524665346654466554665646657466584665946660466614666246663466644666546666466674666846669466704667146672466734667446675466764667746678466794668046681466824668346684466854668646687466884668946690466914669246693466944669546696466974669846699467004670146702467034670446705467064670746708467094671046711467124671346714467154671646717467184671946720467214672246723467244672546726467274672846729467304673146732467334673446735467364673746738467394674046741467424674346744467454674646747467484674946750467514675246753467544675546756467574675846759467604676146762467634676446765467664676746768467694677046771467724677346774467754677646777467784677946780467814678246783467844678546786467874678846789467904679146792467934679446795467964679746798467994680046801468024680346804468054680646807468084680946810468114681246813468144681546816468174681846819468204682146822468234682446825468264682746828468294683046831468324683346834468354683646837468384683946840468414684246843468444684546846468474684846849468504685146852468534685446855468564685746858468594686046861468624686346864468654686646867468684686946870468714687246873468744687546876468774687846879468804688146882468834688446885468864688746888468894689046891468924689346894468954689646897468984689946900469014690246903469044690546906469074690846909469104691146912469134691446915469164691746918469194692046921469224692346924469254692646927469284692946930469314693246933469344693546936469374693846939469404694146942469434694446945469464694746948469494695046951469524695346954469554695646957469584695946960469614696246963469644696546966469674696846969469704697146972469734697446975469764697746978469794698046981469824698346984469854698646987469884698946990469914699246993469944699546996469974699846999470004700147002470034700447005470064700747008470094701047011470124701347014470154701647017470184701947020470214702247023470244702547026470274702847029470304703147032470334703447035470364703747038470394704047041470424704347044470454704647047470484704947050470514705247053470544705547056470574705847059470604706147062470634706447065470664706747068470694707047071470724707347074470754707647077470784707947080470814708247083470844708547086470874708847089470904709147092470934709447095470964709747098470994710047101471024710347104471054710647107471084710947110471114711247113471144711547116471174711847119471204712147122471234712447125471264712747128471294713047131471324713347134471354713647137471384713947140471414714247143471444714547146471474714847149471504715147152471534715447155471564715747158471594716047161471624716347164471654716647167471684716947170471714717247173471744717547176471774717847179471804718147182471834718447185471864718747188471894719047191471924719347194471954719647197471984719947200472014720247203472044720547206472074720847209472104721147212472134721447215472164721747218472194722047221472224722347224472254722647227472284722947230472314723247233472344723547236472374723847239472404724147242472434724447245472464724747248472494725047251472524725347254472554725647257472584725947260472614726247263472644726547266472674726847269472704727147272472734727447275472764727747278472794728047281472824728347284472854728647287472884728947290472914729247293472944729547296472974729847299473004730147302473034730447305473064730747308473094731047311473124731347314473154731647317473184731947320473214732247323473244732547326473274732847329473304733147332473334733447335473364733747338473394734047341473424734347344473454734647347473484734947350473514735247353473544735547356473574735847359473604736147362473634736447365473664736747368473694737047371473724737347374473754737647377473784737947380473814738247383473844738547386473874738847389473904739147392473934739447395473964739747398473994740047401474024740347404474054740647407474084740947410474114741247413474144741547416474174741847419474204742147422474234742447425474264742747428474294743047431474324743347434474354743647437474384743947440474414744247443474444744547446474474744847449474504745147452474534745447455474564745747458474594746047461474624746347464474654746647467474684746947470474714747247473474744747547476474774747847479474804748147482474834748447485474864748747488474894749047491474924749347494474954749647497474984749947500475014750247503475044750547506475074750847509475104751147512475134751447515475164751747518475194752047521475224752347524475254752647527475284752947530475314753247533475344753547536475374753847539475404754147542475434754447545475464754747548475494755047551475524755347554475554755647557475584755947560475614756247563475644756547566475674756847569475704757147572475734757447575475764757747578475794758047581475824758347584475854758647587475884758947590475914759247593475944759547596475974759847599476004760147602476034760447605476064760747608476094761047611476124761347614476154761647617476184761947620476214762247623476244762547626476274762847629476304763147632476334763447635476364763747638476394764047641476424764347644476454764647647476484764947650476514765247653476544765547656476574765847659476604766147662476634766447665476664766747668476694767047671476724767347674476754767647677476784767947680476814768247683476844768547686476874768847689476904769147692476934769447695476964769747698476994770047701477024770347704477054770647707477084770947710477114771247713477144771547716477174771847719477204772147722477234772447725477264772747728477294773047731477324773347734477354773647737477384773947740477414774247743477444774547746477474774847749477504775147752477534775447755477564775747758477594776047761477624776347764477654776647767477684776947770477714777247773477744777547776477774777847779477804778147782477834778447785477864778747788477894779047791477924779347794477954779647797477984779947800478014780247803478044780547806478074780847809478104781147812478134781447815478164781747818478194782047821478224782347824478254782647827478284782947830478314783247833478344783547836478374783847839478404784147842478434784447845478464784747848478494785047851478524785347854478554785647857478584785947860478614786247863478644786547866478674786847869478704787147872478734787447875478764787747878478794788047881478824788347884478854788647887478884788947890478914789247893478944789547896478974789847899479004790147902479034790447905479064790747908479094791047911479124791347914479154791647917479184791947920479214792247923479244792547926479274792847929479304793147932479334793447935479364793747938479394794047941479424794347944479454794647947479484794947950479514795247953479544795547956479574795847959479604796147962479634796447965479664796747968479694797047971479724797347974479754797647977479784797947980479814798247983479844798547986479874798847989479904799147992479934799447995479964799747998479994800048001480024800348004480054800648007480084800948010480114801248013480144801548016480174801848019480204802148022480234802448025480264802748028480294803048031480324803348034480354803648037480384803948040480414804248043480444804548046480474804848049480504805148052480534805448055480564805748058480594806048061480624806348064480654806648067480684806948070480714807248073480744807548076480774807848079480804808148082480834808448085480864808748088480894809048091480924809348094480954809648097480984809948100481014810248103481044810548106481074810848109481104811148112481134811448115481164811748118481194812048121481224812348124481254812648127481284812948130481314813248133481344813548136481374813848139481404814148142481434814448145481464814748148481494815048151481524815348154481554815648157481584815948160481614816248163481644816548166481674816848169481704817148172481734817448175481764817748178481794818048181481824818348184481854818648187481884818948190481914819248193481944819548196481974819848199482004820148202482034820448205482064820748208482094821048211482124821348214482154821648217482184821948220482214822248223482244822548226482274822848229482304823148232482334823448235482364823748238482394824048241482424824348244482454824648247482484824948250482514825248253482544825548256482574825848259482604826148262482634826448265482664826748268482694827048271482724827348274482754827648277482784827948280482814828248283482844828548286482874828848289482904829148292482934829448295482964829748298482994830048301483024830348304483054830648307483084830948310483114831248313483144831548316483174831848319483204832148322483234832448325483264832748328483294833048331483324833348334483354833648337483384833948340483414834248343483444834548346483474834848349483504835148352483534835448355483564835748358483594836048361483624836348364483654836648367483684836948370483714837248373483744837548376483774837848379483804838148382483834838448385483864838748388483894839048391483924839348394483954839648397483984839948400484014840248403484044840548406484074840848409484104841148412484134841448415484164841748418484194842048421484224842348424484254842648427484284842948430484314843248433484344843548436484374843848439484404844148442484434844448445484464844748448484494845048451484524845348454484554845648457484584845948460484614846248463484644846548466484674846848469484704847148472484734847448475484764847748478484794848048481484824848348484484854848648487484884848948490484914849248493484944849548496484974849848499485004850148502485034850448505485064850748508485094851048511485124851348514485154851648517485184851948520485214852248523485244852548526485274852848529485304853148532485334853448535485364853748538485394854048541485424854348544485454854648547485484854948550485514855248553485544855548556485574855848559485604856148562485634856448565485664856748568485694857048571485724857348574485754857648577485784857948580485814858248583485844858548586485874858848589485904859148592485934859448595485964859748598485994860048601486024860348604486054860648607486084860948610486114861248613486144861548616486174861848619486204862148622486234862448625486264862748628486294863048631486324863348634486354863648637486384863948640486414864248643486444864548646486474864848649486504865148652486534865448655486564865748658486594866048661486624866348664486654866648667486684866948670486714867248673486744867548676486774867848679486804868148682486834868448685486864868748688486894869048691486924869348694486954869648697486984869948700487014870248703487044870548706487074870848709487104871148712487134871448715487164871748718487194872048721487224872348724487254872648727487284872948730487314873248733487344873548736487374873848739487404874148742487434874448745487464874748748487494875048751487524875348754487554875648757487584875948760487614876248763487644876548766487674876848769487704877148772487734877448775487764877748778487794878048781487824878348784487854878648787487884878948790487914879248793487944879548796487974879848799488004880148802488034880448805488064880748808488094881048811488124881348814488154881648817488184881948820488214882248823488244882548826488274882848829488304883148832488334883448835488364883748838488394884048841488424884348844488454884648847488484884948850488514885248853488544885548856488574885848859488604886148862488634886448865488664886748868488694887048871488724887348874488754887648877488784887948880488814888248883488844888548886488874888848889488904889148892488934889448895488964889748898488994890048901489024890348904489054890648907489084890948910489114891248913489144891548916489174891848919489204892148922489234892448925489264892748928489294893048931489324893348934489354893648937489384893948940489414894248943489444894548946489474894848949489504895148952489534895448955489564895748958489594896048961489624896348964489654896648967489684896948970489714897248973489744897548976489774897848979489804898148982489834898448985489864898748988489894899048991489924899348994489954899648997489984899949000490014900249003490044900549006490074900849009490104901149012490134901449015490164901749018490194902049021490224902349024490254902649027490284902949030490314903249033490344903549036490374903849039490404904149042490434904449045490464904749048490494905049051490524905349054490554905649057490584905949060490614906249063490644906549066490674906849069490704907149072490734907449075490764907749078490794908049081490824908349084490854908649087490884908949090490914909249093490944909549096490974909849099491004910149102491034910449105491064910749108491094911049111491124911349114491154911649117491184911949120491214912249123491244912549126491274912849129491304913149132491334913449135491364913749138491394914049141491424914349144491454914649147491484914949150491514915249153491544915549156491574915849159491604916149162491634916449165491664916749168491694917049171491724917349174491754917649177491784917949180491814918249183491844918549186491874918849189491904919149192491934919449195491964919749198491994920049201492024920349204492054920649207492084920949210492114921249213492144921549216492174921849219492204922149222492234922449225492264922749228492294923049231492324923349234492354923649237492384923949240492414924249243492444924549246492474924849249492504925149252492534925449255492564925749258492594926049261492624926349264492654926649267492684926949270492714927249273492744927549276492774927849279492804928149282492834928449285492864928749288492894929049291492924929349294492954929649297492984929949300493014930249303493044930549306493074930849309493104931149312493134931449315493164931749318493194932049321493224932349324493254932649327493284932949330493314933249333493344933549336493374933849339493404934149342493434934449345493464934749348493494935049351493524935349354493554935649357493584935949360493614936249363493644936549366493674936849369493704937149372493734937449375493764937749378493794938049381493824938349384493854938649387493884938949390493914939249393493944939549396493974939849399494004940149402494034940449405494064940749408494094941049411494124941349414494154941649417494184941949420494214942249423494244942549426494274942849429494304943149432494334943449435494364943749438494394944049441494424944349444494454944649447494484944949450494514945249453494544945549456494574945849459494604946149462494634946449465494664946749468494694947049471494724947349474494754947649477494784947949480494814948249483494844948549486494874948849489494904949149492494934949449495494964949749498494994950049501495024950349504495054950649507495084950949510495114951249513495144951549516495174951849519495204952149522495234952449525495264952749528495294953049531495324953349534495354953649537495384953949540495414954249543495444954549546495474954849549495504955149552495534955449555495564955749558495594956049561495624956349564495654956649567495684956949570495714957249573495744957549576495774957849579495804958149582495834958449585495864958749588495894959049591495924959349594495954959649597495984959949600496014960249603496044960549606496074960849609496104961149612496134961449615496164961749618496194962049621496224962349624496254962649627496284962949630496314963249633496344963549636496374963849639496404964149642496434964449645496464964749648496494965049651496524965349654496554965649657496584965949660496614966249663496644966549666496674966849669496704967149672496734967449675496764967749678496794968049681496824968349684496854968649687496884968949690496914969249693496944969549696496974969849699497004970149702497034970449705497064970749708497094971049711497124971349714497154971649717497184971949720497214972249723497244972549726497274972849729497304973149732497334973449735497364973749738497394974049741497424974349744497454974649747497484974949750497514975249753497544975549756497574975849759497604976149762497634976449765497664976749768497694977049771497724977349774497754977649777497784977949780497814978249783497844978549786497874978849789497904979149792497934979449795497964979749798497994980049801498024980349804498054980649807498084980949810498114981249813498144981549816498174981849819498204982149822498234982449825498264982749828498294983049831498324983349834498354983649837498384983949840498414984249843498444984549846498474984849849498504985149852498534985449855498564985749858498594986049861498624986349864498654986649867498684986949870498714987249873498744987549876498774987849879498804988149882498834988449885498864988749888498894989049891498924989349894498954989649897498984989949900499014990249903499044990549906499074990849909499104991149912499134991449915499164991749918499194992049921499224992349924499254992649927499284992949930499314993249933499344993549936499374993849939499404994149942499434994449945499464994749948499494995049951499524995349954499554995649957499584995949960499614996249963499644996549966499674996849969499704997149972499734997449975499764997749978499794998049981499824998349984499854998649987499884998949990499914999249993499944999549996499974999849999500005000150002500035000450005500065000750008500095001050011500125001350014500155001650017500185001950020500215002250023500245002550026500275002850029500305003150032500335003450035500365003750038500395004050041500425004350044500455004650047500485004950050500515005250053500545005550056500575005850059500605006150062500635006450065500665006750068500695007050071500725007350074500755007650077500785007950080500815008250083500845008550086500875008850089500905009150092500935009450095500965009750098500995010050101501025010350104501055010650107501085010950110501115011250113501145011550116501175011850119501205012150122501235012450125501265012750128501295013050131501325013350134501355013650137501385013950140501415014250143501445014550146501475014850149501505015150152501535015450155501565015750158501595016050161501625016350164501655016650167501685016950170501715017250173501745017550176501775017850179501805018150182501835018450185501865018750188501895019050191501925019350194501955019650197501985019950200502015020250203502045020550206502075020850209502105021150212502135021450215502165021750218502195022050221502225022350224502255022650227502285022950230502315023250233502345023550236502375023850239502405024150242502435024450245502465024750248502495025050251502525025350254502555025650257502585025950260502615026250263502645026550266502675026850269502705027150272502735027450275502765027750278502795028050281502825028350284502855028650287502885028950290502915029250293502945029550296502975029850299503005030150302503035030450305503065030750308503095031050311503125031350314503155031650317503185031950320503215032250323503245032550326503275032850329503305033150332503335033450335503365033750338503395034050341503425034350344503455034650347503485034950350503515035250353503545035550356503575035850359503605036150362503635036450365503665036750368503695037050371503725037350374503755037650377503785037950380503815038250383503845038550386503875038850389503905039150392503935039450395503965039750398503995040050401504025040350404504055040650407504085040950410504115041250413504145041550416504175041850419504205042150422504235042450425504265042750428504295043050431504325043350434504355043650437504385043950440504415044250443504445044550446504475044850449504505045150452504535045450455504565045750458504595046050461504625046350464504655046650467504685046950470504715047250473504745047550476504775047850479504805048150482504835048450485504865048750488504895049050491504925049350494504955049650497504985049950500505015050250503505045050550506505075050850509505105051150512505135051450515505165051750518505195052050521505225052350524505255052650527505285052950530505315053250533505345053550536505375053850539505405054150542505435054450545505465054750548505495055050551505525055350554505555055650557505585055950560505615056250563505645056550566505675056850569505705057150572505735057450575505765057750578505795058050581505825058350584505855058650587505885058950590505915059250593505945059550596505975059850599506005060150602506035060450605506065060750608506095061050611506125061350614506155061650617506185061950620506215062250623506245062550626506275062850629506305063150632506335063450635506365063750638506395064050641506425064350644506455064650647506485064950650506515065250653506545065550656506575065850659506605066150662506635066450665506665066750668506695067050671506725067350674506755067650677506785067950680506815068250683506845068550686506875068850689506905069150692506935069450695506965069750698506995070050701507025070350704507055070650707507085070950710507115071250713507145071550716507175071850719507205072150722507235072450725507265072750728507295073050731507325073350734507355073650737507385073950740507415074250743507445074550746507475074850749507505075150752507535075450755507565075750758507595076050761507625076350764507655076650767507685076950770507715077250773507745077550776507775077850779507805078150782507835078450785507865078750788507895079050791507925079350794507955079650797507985079950800508015080250803508045080550806508075080850809508105081150812508135081450815508165081750818508195082050821508225082350824508255082650827508285082950830508315083250833508345083550836508375083850839508405084150842508435084450845508465084750848508495085050851508525085350854508555085650857508585085950860508615086250863508645086550866508675086850869508705087150872508735087450875508765087750878508795088050881508825088350884508855088650887508885088950890508915089250893508945089550896508975089850899509005090150902509035090450905509065090750908509095091050911509125091350914509155091650917509185091950920509215092250923509245092550926509275092850929509305093150932509335093450935509365093750938509395094050941509425094350944509455094650947509485094950950509515095250953509545095550956509575095850959509605096150962509635096450965509665096750968509695097050971509725097350974509755097650977509785097950980509815098250983509845098550986509875098850989509905099150992509935099450995509965099750998509995100051001510025100351004510055100651007510085100951010510115101251013510145101551016510175101851019510205102151022510235102451025510265102751028510295103051031510325103351034510355103651037510385103951040510415104251043510445104551046510475104851049510505105151052510535105451055510565105751058510595106051061510625106351064510655106651067510685106951070510715107251073510745107551076510775107851079510805108151082510835108451085510865108751088510895109051091510925109351094510955109651097510985109951100511015110251103511045110551106511075110851109511105111151112511135111451115511165111751118511195112051121511225112351124511255112651127511285112951130511315113251133511345113551136511375113851139511405114151142511435114451145511465114751148511495115051151511525115351154511555115651157511585115951160511615116251163511645116551166511675116851169511705117151172511735117451175511765117751178511795118051181511825118351184511855118651187511885118951190511915119251193511945119551196511975119851199512005120151202512035120451205512065120751208512095121051211512125121351214512155121651217512185121951220512215122251223512245122551226512275122851229512305123151232512335123451235512365123751238512395124051241512425124351244512455124651247512485124951250512515125251253512545125551256512575125851259512605126151262512635126451265512665126751268512695127051271512725127351274512755127651277512785127951280512815128251283512845128551286512875128851289512905129151292512935129451295512965129751298512995130051301513025130351304513055130651307513085130951310513115131251313513145131551316513175131851319513205132151322513235132451325513265132751328513295133051331513325133351334513355133651337513385133951340513415134251343513445134551346513475134851349513505135151352513535135451355513565135751358513595136051361513625136351364513655136651367513685136951370513715137251373513745137551376513775137851379513805138151382513835138451385513865138751388513895139051391513925139351394513955139651397513985139951400514015140251403514045140551406514075140851409514105141151412514135141451415514165141751418514195142051421514225142351424514255142651427514285142951430514315143251433514345143551436514375143851439514405144151442514435144451445514465144751448514495145051451514525145351454514555145651457514585145951460514615146251463514645146551466514675146851469514705147151472514735147451475514765147751478514795148051481514825148351484514855148651487514885148951490514915149251493514945149551496514975149851499515005150151502515035150451505515065150751508515095151051511515125151351514515155151651517515185151951520515215152251523515245152551526515275152851529515305153151532515335153451535515365153751538515395154051541515425154351544515455154651547515485154951550515515155251553515545155551556515575155851559515605156151562515635156451565515665156751568515695157051571515725157351574515755157651577515785157951580515815158251583515845158551586515875158851589515905159151592515935159451595515965159751598515995160051601516025160351604516055160651607516085160951610516115161251613516145161551616516175161851619516205162151622516235162451625516265162751628516295163051631516325163351634516355163651637516385163951640516415164251643516445164551646516475164851649516505165151652516535165451655516565165751658516595166051661516625166351664516655166651667516685166951670516715167251673516745167551676516775167851679516805168151682516835168451685516865168751688516895169051691516925169351694516955169651697516985169951700517015170251703517045170551706517075170851709517105171151712517135171451715517165171751718517195172051721517225172351724517255172651727517285172951730517315173251733517345173551736517375173851739517405174151742517435174451745517465174751748517495175051751517525175351754517555175651757517585175951760517615176251763517645176551766517675176851769517705177151772517735177451775517765177751778517795178051781517825178351784517855178651787517885178951790517915179251793517945179551796517975179851799518005180151802518035180451805518065180751808518095181051811518125181351814518155181651817518185181951820518215182251823518245182551826518275182851829518305183151832518335183451835518365183751838518395184051841518425184351844518455184651847518485184951850518515185251853518545185551856518575185851859518605186151862518635186451865518665186751868518695187051871518725187351874518755187651877518785187951880518815188251883518845188551886518875188851889518905189151892518935189451895518965189751898518995190051901519025190351904519055190651907519085190951910519115191251913519145191551916519175191851919519205192151922519235192451925519265192751928519295193051931519325193351934519355193651937519385193951940519415194251943519445194551946519475194851949519505195151952519535195451955519565195751958519595196051961519625196351964519655196651967519685196951970519715197251973519745197551976519775197851979519805198151982519835198451985519865198751988519895199051991519925199351994519955199651997519985199952000520015200252003520045200552006520075200852009520105201152012520135201452015520165201752018520195202052021520225202352024520255202652027520285202952030520315203252033520345203552036520375203852039520405204152042520435204452045520465204752048520495205052051520525205352054520555205652057520585205952060520615206252063520645206552066520675206852069520705207152072520735207452075520765207752078520795208052081520825208352084520855208652087520885208952090520915209252093520945209552096520975209852099521005210152102521035210452105521065210752108521095211052111521125211352114521155211652117521185211952120521215212252123521245212552126521275212852129521305213152132521335213452135521365213752138521395214052141521425214352144521455214652147521485214952150521515215252153521545215552156521575215852159521605216152162521635216452165521665216752168521695217052171521725217352174521755217652177521785217952180521815218252183521845218552186521875218852189521905219152192521935219452195521965219752198521995220052201522025220352204522055220652207522085220952210522115221252213522145221552216522175221852219522205222152222522235222452225522265222752228522295223052231522325223352234522355223652237522385223952240522415224252243522445224552246522475224852249522505225152252522535225452255522565225752258522595226052261522625226352264522655226652267522685226952270522715227252273522745227552276522775227852279522805228152282522835228452285522865228752288522895229052291522925229352294522955229652297522985229952300523015230252303523045230552306523075230852309523105231152312523135231452315523165231752318523195232052321523225232352324523255232652327523285232952330523315233252333523345233552336523375233852339523405234152342523435234452345523465234752348523495235052351523525235352354523555235652357523585235952360523615236252363523645236552366523675236852369523705237152372523735237452375523765237752378523795238052381523825238352384523855238652387523885238952390523915239252393523945239552396523975239852399524005240152402524035240452405524065240752408524095241052411524125241352414524155241652417524185241952420524215242252423524245242552426524275242852429524305243152432524335243452435524365243752438524395244052441524425244352444524455244652447524485244952450524515245252453524545245552456524575245852459524605246152462524635246452465524665246752468524695247052471524725247352474524755247652477524785247952480524815248252483524845248552486524875248852489524905249152492524935249452495524965249752498524995250052501525025250352504525055250652507525085250952510525115251252513525145251552516525175251852519525205252152522525235252452525525265252752528525295253052531525325253352534525355253652537525385253952540525415254252543525445254552546525475254852549525505255152552525535255452555525565255752558525595256052561525625256352564525655256652567525685256952570525715257252573525745257552576525775257852579525805258152582525835258452585525865258752588525895259052591525925259352594525955259652597525985259952600526015260252603526045260552606526075260852609526105261152612526135261452615526165261752618526195262052621526225262352624526255262652627526285262952630526315263252633526345263552636526375263852639526405264152642526435264452645526465264752648526495265052651526525265352654526555265652657526585265952660526615266252663526645266552666526675266852669526705267152672526735267452675526765267752678526795268052681526825268352684526855268652687526885268952690526915269252693526945269552696526975269852699527005270152702527035270452705527065270752708527095271052711527125271352714527155271652717527185271952720527215272252723527245272552726527275272852729527305273152732527335273452735527365273752738527395274052741527425274352744527455274652747527485274952750527515275252753527545275552756527575275852759527605276152762527635276452765527665276752768527695277052771527725277352774527755277652777527785277952780527815278252783527845278552786527875278852789527905279152792527935279452795527965279752798527995280052801528025280352804528055280652807528085280952810528115281252813528145281552816528175281852819528205282152822528235282452825528265282752828528295283052831528325283352834528355283652837528385283952840528415284252843528445284552846528475284852849528505285152852528535285452855528565285752858528595286052861528625286352864528655286652867528685286952870528715287252873528745287552876528775287852879528805288152882528835288452885528865288752888528895289052891528925289352894528955289652897528985289952900529015290252903529045290552906529075290852909529105291152912529135291452915529165291752918529195292052921529225292352924529255292652927529285292952930529315293252933529345293552936529375293852939529405294152942529435294452945529465294752948529495295052951529525295352954529555295652957529585295952960529615296252963529645296552966529675296852969529705297152972529735297452975529765297752978529795298052981529825298352984529855298652987529885298952990529915299252993529945299552996529975299852999530005300153002530035300453005530065300753008530095301053011530125301353014530155301653017530185301953020530215302253023530245302553026530275302853029530305303153032530335303453035530365303753038530395304053041530425304353044530455304653047530485304953050530515305253053530545305553056530575305853059530605306153062530635306453065530665306753068530695307053071530725307353074530755307653077530785307953080530815308253083530845308553086530875308853089530905309153092530935309453095530965309753098530995310053101531025310353104531055310653107531085310953110531115311253113531145311553116531175311853119531205312153122531235312453125531265312753128531295313053131531325313353134531355313653137531385313953140531415314253143531445314553146531475314853149531505315153152531535315453155531565315753158531595316053161531625316353164531655316653167531685316953170531715317253173531745317553176531775317853179531805318153182531835318453185531865318753188531895319053191531925319353194531955319653197531985319953200532015320253203532045320553206532075320853209532105321153212532135321453215532165321753218532195322053221532225322353224532255322653227532285322953230532315323253233532345323553236532375323853239532405324153242532435324453245532465324753248532495325053251532525325353254532555325653257532585325953260532615326253263532645326553266532675326853269532705327153272532735327453275532765327753278532795328053281532825328353284532855328653287532885328953290532915329253293532945329553296532975329853299533005330153302533035330453305533065330753308533095331053311533125331353314533155331653317533185331953320533215332253323533245332553326533275332853329533305333153332533335333453335533365333753338533395334053341533425334353344533455334653347533485334953350533515335253353533545335553356533575335853359533605336153362533635336453365533665336753368533695337053371533725337353374533755337653377533785337953380533815338253383533845338553386533875338853389533905339153392533935339453395533965339753398533995340053401534025340353404534055340653407534085340953410534115341253413534145341553416534175341853419534205342153422534235342453425534265342753428534295343053431534325343353434534355343653437534385343953440534415344253443534445344553446534475344853449534505345153452534535345453455534565345753458534595346053461534625346353464534655346653467534685346953470534715347253473534745347553476534775347853479534805348153482534835348453485534865348753488534895349053491534925349353494534955349653497534985349953500535015350253503535045350553506535075350853509535105351153512535135351453515535165351753518535195352053521535225352353524535255352653527535285352953530535315353253533535345353553536535375353853539535405354153542535435354453545535465354753548535495355053551535525355353554535555355653557535585355953560535615356253563535645356553566535675356853569535705357153572535735357453575535765357753578535795358053581535825358353584535855358653587535885358953590535915359253593535945359553596535975359853599536005360153602536035360453605536065360753608536095361053611536125361353614536155361653617536185361953620536215362253623536245362553626536275362853629536305363153632536335363453635536365363753638536395364053641536425364353644536455364653647536485364953650536515365253653536545365553656536575365853659536605366153662536635366453665536665366753668536695367053671536725367353674536755367653677536785367953680536815368253683536845368553686536875368853689536905369153692536935369453695536965369753698536995370053701537025370353704537055370653707537085370953710537115371253713537145371553716537175371853719537205372153722537235372453725537265372753728537295373053731537325373353734537355373653737537385373953740537415374253743537445374553746537475374853749537505375153752537535375453755537565375753758537595376053761537625376353764537655376653767537685376953770537715377253773537745377553776537775377853779537805378153782537835378453785537865378753788537895379053791537925379353794537955379653797537985379953800538015380253803538045380553806538075380853809538105381153812538135381453815538165381753818538195382053821538225382353824538255382653827538285382953830538315383253833538345383553836538375383853839538405384153842538435384453845538465384753848538495385053851538525385353854538555385653857538585385953860538615386253863538645386553866538675386853869538705387153872538735387453875538765387753878538795388053881538825388353884538855388653887538885388953890538915389253893538945389553896538975389853899539005390153902539035390453905539065390753908539095391053911539125391353914539155391653917539185391953920539215392253923539245392553926539275392853929539305393153932539335393453935539365393753938539395394053941539425394353944539455394653947539485394953950539515395253953539545395553956539575395853959539605396153962539635396453965539665396753968539695397053971539725397353974539755397653977539785397953980539815398253983539845398553986539875398853989539905399153992539935399453995539965399753998539995400054001540025400354004540055400654007540085400954010540115401254013540145401554016540175401854019540205402154022540235402454025540265402754028540295403054031540325403354034540355403654037540385403954040540415404254043540445404554046540475404854049540505405154052540535405454055540565405754058540595406054061540625406354064540655406654067540685406954070540715407254073540745407554076540775407854079540805408154082540835408454085540865408754088540895409054091540925409354094540955409654097540985409954100541015410254103541045410554106541075410854109541105411154112541135411454115541165411754118541195412054121541225412354124541255412654127541285412954130541315413254133541345413554136541375413854139541405414154142541435414454145541465414754148541495415054151541525415354154541555415654157541585415954160541615416254163541645416554166541675416854169541705417154172541735417454175541765417754178541795418054181541825418354184541855418654187541885418954190541915419254193541945419554196541975419854199542005420154202542035420454205542065420754208542095421054211542125421354214542155421654217542185421954220542215422254223542245422554226542275422854229542305423154232542335423454235542365423754238542395424054241542425424354244542455424654247542485424954250542515425254253542545425554256542575425854259542605426154262542635426454265542665426754268542695427054271542725427354274542755427654277542785427954280542815428254283542845428554286542875428854289542905429154292542935429454295542965429754298542995430054301543025430354304543055430654307543085430954310543115431254313543145431554316543175431854319543205432154322543235432454325543265432754328543295433054331543325433354334543355433654337543385433954340543415434254343543445434554346543475434854349543505435154352543535435454355543565435754358543595436054361543625436354364543655436654367543685436954370543715437254373543745437554376543775437854379543805438154382543835438454385543865438754388543895439054391543925439354394543955439654397543985439954400544015440254403544045440554406544075440854409544105441154412544135441454415544165441754418544195442054421544225442354424544255442654427544285442954430544315443254433544345443554436544375443854439544405444154442544435444454445544465444754448544495445054451544525445354454544555445654457544585445954460544615446254463544645446554466544675446854469544705447154472544735447454475544765447754478544795448054481544825448354484544855448654487544885448954490544915449254493544945449554496544975449854499545005450154502545035450454505545065450754508545095451054511545125451354514545155451654517545185451954520545215452254523545245452554526545275452854529545305453154532545335453454535545365453754538545395454054541545425454354544545455454654547545485454954550545515455254553545545455554556545575455854559545605456154562545635456454565545665456754568545695457054571545725457354574545755457654577545785457954580545815458254583545845458554586545875458854589545905459154592545935459454595545965459754598545995460054601546025460354604546055460654607546085460954610546115461254613546145461554616546175461854619546205462154622546235462454625546265462754628546295463054631546325463354634546355463654637546385463954640546415464254643546445464554646546475464854649546505465154652546535465454655546565465754658546595466054661546625466354664546655466654667546685466954670546715467254673546745467554676546775467854679546805468154682546835468454685546865468754688546895469054691546925469354694546955469654697546985469954700547015470254703547045470554706547075470854709547105471154712547135471454715547165471754718547195472054721547225472354724547255472654727547285472954730547315473254733547345473554736547375473854739547405474154742547435474454745547465474754748547495475054751547525475354754547555475654757547585475954760547615476254763547645476554766547675476854769547705477154772547735477454775547765477754778547795478054781547825478354784547855478654787547885478954790547915479254793547945479554796547975479854799548005480154802548035480454805548065480754808548095481054811548125481354814548155481654817548185481954820548215482254823548245482554826548275482854829548305483154832548335483454835548365483754838548395484054841548425484354844548455484654847548485484954850548515485254853548545485554856548575485854859548605486154862548635486454865548665486754868548695487054871548725487354874548755487654877548785487954880548815488254883548845488554886548875488854889548905489154892548935489454895548965489754898548995490054901549025490354904549055490654907549085490954910549115491254913549145491554916549175491854919549205492154922549235492454925549265492754928549295493054931549325493354934549355493654937549385493954940549415494254943549445494554946549475494854949549505495154952549535495454955549565495754958549595496054961549625496354964549655496654967549685496954970549715497254973549745497554976549775497854979549805498154982549835498454985549865498754988549895499054991549925499354994549955499654997549985499955000550015500255003550045500555006550075500855009550105501155012550135501455015550165501755018550195502055021550225502355024550255502655027550285502955030550315503255033550345503555036550375503855039550405504155042550435504455045550465504755048550495505055051550525505355054550555505655057550585505955060550615506255063550645506555066550675506855069550705507155072550735507455075550765507755078550795508055081550825508355084550855508655087550885508955090550915509255093550945509555096550975509855099551005510155102551035510455105551065510755108551095511055111551125511355114551155511655117551185511955120551215512255123551245512555126551275512855129551305513155132551335513455135551365513755138551395514055141551425514355144551455514655147551485514955150551515515255153551545515555156551575515855159551605516155162551635516455165551665516755168551695517055171551725517355174551755517655177551785517955180551815518255183551845518555186551875518855189551905519155192551935519455195551965519755198551995520055201552025520355204552055520655207552085520955210552115521255213552145521555216552175521855219552205522155222552235522455225552265522755228552295523055231552325523355234552355523655237552385523955240552415524255243552445524555246552475524855249552505525155252552535525455255552565525755258552595526055261552625526355264552655526655267552685526955270552715527255273552745527555276552775527855279552805528155282552835528455285552865528755288552895529055291552925529355294552955529655297552985529955300553015530255303553045530555306553075530855309553105531155312553135531455315553165531755318553195532055321553225532355324553255532655327553285532955330553315533255333553345533555336553375533855339553405534155342553435534455345553465534755348553495535055351553525535355354553555535655357553585535955360553615536255363553645536555366553675536855369553705537155372553735537455375553765537755378553795538055381553825538355384553855538655387553885538955390553915539255393553945539555396553975539855399554005540155402554035540455405554065540755408554095541055411554125541355414554155541655417554185541955420554215542255423554245542555426554275542855429554305543155432554335543455435554365543755438554395544055441554425544355444554455544655447554485544955450554515545255453554545545555456554575545855459554605546155462554635546455465554665546755468554695547055471554725547355474554755547655477554785547955480554815548255483554845548555486554875548855489554905549155492554935549455495554965549755498554995550055501555025550355504555055550655507555085550955510555115551255513555145551555516555175551855519555205552155522555235552455525555265552755528555295553055531555325553355534555355553655537555385553955540555415554255543555445554555546555475554855549555505555155552555535555455555555565555755558555595556055561555625556355564555655556655567555685556955570555715557255573555745557555576555775557855579555805558155582555835558455585555865558755588555895559055591555925559355594555955559655597555985559955600556015560255603556045560555606556075560855609556105561155612556135561455615556165561755618556195562055621556225562355624556255562655627556285562955630556315563255633556345563555636556375563855639556405564155642556435564455645556465564755648556495565055651556525565355654556555565655657556585565955660556615566255663556645566555666556675566855669556705567155672556735567455675556765567755678556795568055681556825568355684556855568655687556885568955690556915569255693556945569555696556975569855699557005570155702557035570455705557065570755708557095571055711557125571355714557155571655717557185571955720557215572255723557245572555726557275572855729557305573155732557335573455735557365573755738557395574055741557425574355744557455574655747557485574955750557515575255753557545575555756557575575855759557605576155762557635576455765557665576755768557695577055771557725577355774557755577655777557785577955780557815578255783557845578555786557875578855789557905579155792557935579455795557965579755798557995580055801558025580355804558055580655807558085580955810558115581255813558145581555816558175581855819558205582155822558235582455825558265582755828558295583055831558325583355834558355583655837558385583955840558415584255843558445584555846558475584855849558505585155852558535585455855558565585755858558595586055861558625586355864558655586655867558685586955870558715587255873558745587555876558775587855879558805588155882558835588455885558865588755888558895589055891558925589355894558955589655897558985589955900559015590255903559045590555906559075590855909559105591155912559135591455915559165591755918559195592055921559225592355924559255592655927559285592955930559315593255933559345593555936559375593855939559405594155942559435594455945559465594755948559495595055951559525595355954559555595655957559585595955960559615596255963559645596555966559675596855969559705597155972559735597455975559765597755978559795598055981559825598355984559855598655987559885598955990559915599255993559945599555996559975599855999560005600156002560035600456005560065600756008560095601056011560125601356014560155601656017560185601956020560215602256023560245602556026560275602856029560305603156032560335603456035560365603756038560395604056041560425604356044560455604656047560485604956050560515605256053560545605556056560575605856059560605606156062560635606456065560665606756068560695607056071560725607356074560755607656077560785607956080560815608256083560845608556086560875608856089560905609156092560935609456095560965609756098560995610056101561025610356104561055610656107561085610956110561115611256113561145611556116561175611856119561205612156122561235612456125561265612756128561295613056131561325613356134561355613656137561385613956140561415614256143561445614556146561475614856149561505615156152561535615456155561565615756158561595616056161561625616356164561655616656167561685616956170561715617256173561745617556176561775617856179561805618156182561835618456185561865618756188561895619056191561925619356194561955619656197561985619956200562015620256203562045620556206562075620856209562105621156212562135621456215562165621756218562195622056221562225622356224562255622656227562285622956230562315623256233562345623556236562375623856239562405624156242562435624456245562465624756248562495625056251562525625356254562555625656257562585625956260562615626256263562645626556266562675626856269562705627156272562735627456275562765627756278562795628056281562825628356284562855628656287562885628956290562915629256293562945629556296562975629856299563005630156302563035630456305563065630756308563095631056311563125631356314563155631656317563185631956320563215632256323563245632556326563275632856329563305633156332563335633456335563365633756338563395634056341563425634356344563455634656347563485634956350563515635256353563545635556356563575635856359563605636156362563635636456365563665636756368563695637056371563725637356374563755637656377563785637956380563815638256383563845638556386563875638856389563905639156392563935639456395563965639756398563995640056401564025640356404564055640656407564085640956410564115641256413564145641556416564175641856419564205642156422564235642456425564265642756428564295643056431564325643356434564355643656437564385643956440564415644256443564445644556446564475644856449564505645156452564535645456455564565645756458564595646056461564625646356464564655646656467564685646956470564715647256473564745647556476564775647856479564805648156482564835648456485564865648756488564895649056491564925649356494564955649656497564985649956500565015650256503565045650556506565075650856509565105651156512565135651456515565165651756518565195652056521565225652356524565255652656527565285652956530565315653256533565345653556536565375653856539565405654156542565435654456545565465654756548565495655056551565525655356554565555655656557565585655956560565615656256563565645656556566565675656856569565705657156572565735657456575565765657756578565795658056581565825658356584565855658656587565885658956590565915659256593565945659556596565975659856599566005660156602566035660456605566065660756608566095661056611566125661356614566155661656617566185661956620566215662256623566245662556626566275662856629566305663156632566335663456635566365663756638566395664056641566425664356644566455664656647566485664956650566515665256653566545665556656566575665856659566605666156662566635666456665566665666756668566695667056671566725667356674566755667656677566785667956680566815668256683566845668556686566875668856689566905669156692566935669456695566965669756698566995670056701567025670356704567055670656707567085670956710567115671256713567145671556716567175671856719567205672156722567235672456725567265672756728567295673056731567325673356734567355673656737567385673956740567415674256743567445674556746567475674856749567505675156752567535675456755567565675756758567595676056761567625676356764567655676656767567685676956770567715677256773567745677556776567775677856779567805678156782567835678456785567865678756788567895679056791567925679356794567955679656797567985679956800568015680256803568045680556806568075680856809568105681156812568135681456815568165681756818568195682056821568225682356824568255682656827568285682956830568315683256833568345683556836568375683856839568405684156842568435684456845568465684756848568495685056851568525685356854568555685656857568585685956860568615686256863568645686556866568675686856869568705687156872568735687456875568765687756878568795688056881568825688356884568855688656887568885688956890568915689256893568945689556896568975689856899569005690156902569035690456905569065690756908569095691056911569125691356914569155691656917569185691956920569215692256923569245692556926569275692856929569305693156932569335693456935569365693756938569395694056941569425694356944569455694656947569485694956950569515695256953569545695556956569575695856959569605696156962569635696456965569665696756968569695697056971569725697356974569755697656977569785697956980569815698256983569845698556986569875698856989569905699156992569935699456995569965699756998569995700057001570025700357004570055700657007570085700957010570115701257013570145701557016570175701857019570205702157022570235702457025570265702757028570295703057031570325703357034570355703657037570385703957040570415704257043570445704557046570475704857049570505705157052570535705457055570565705757058570595706057061570625706357064570655706657067570685706957070570715707257073570745707557076570775707857079570805708157082570835708457085570865708757088570895709057091570925709357094570955709657097570985709957100571015710257103571045710557106571075710857109571105711157112571135711457115571165711757118571195712057121571225712357124571255712657127571285712957130571315713257133571345713557136571375713857139571405714157142571435714457145571465714757148571495715057151571525715357154571555715657157571585715957160571615716257163571645716557166571675716857169571705717157172571735717457175571765717757178571795718057181571825718357184571855718657187571885718957190571915719257193571945719557196571975719857199572005720157202572035720457205572065720757208572095721057211572125721357214572155721657217572185721957220572215722257223572245722557226572275722857229572305723157232572335723457235572365723757238572395724057241572425724357244572455724657247572485724957250572515725257253572545725557256572575725857259572605726157262572635726457265572665726757268572695727057271572725727357274572755727657277572785727957280572815728257283572845728557286572875728857289572905729157292572935729457295572965729757298572995730057301573025730357304573055730657307573085730957310573115731257313573145731557316573175731857319573205732157322573235732457325573265732757328573295733057331573325733357334573355733657337573385733957340573415734257343573445734557346573475734857349573505735157352573535735457355573565735757358573595736057361573625736357364573655736657367573685736957370573715737257373573745737557376573775737857379573805738157382573835738457385573865738757388573895739057391573925739357394573955739657397573985739957400574015740257403574045740557406574075740857409574105741157412574135741457415574165741757418574195742057421574225742357424574255742657427574285742957430574315743257433574345743557436574375743857439574405744157442574435744457445574465744757448574495745057451574525745357454574555745657457574585745957460574615746257463574645746557466574675746857469574705747157472574735747457475574765747757478574795748057481574825748357484574855748657487574885748957490574915749257493574945749557496574975749857499575005750157502575035750457505575065750757508575095751057511575125751357514575155751657517575185751957520575215752257523575245752557526575275752857529575305753157532575335753457535575365753757538575395754057541575425754357544575455754657547575485754957550575515755257553575545755557556575575755857559575605756157562575635756457565575665756757568575695757057571575725757357574575755757657577575785757957580575815758257583575845758557586575875758857589575905759157592575935759457595575965759757598575995760057601576025760357604576055760657607576085760957610576115761257613576145761557616576175761857619576205762157622576235762457625576265762757628576295763057631576325763357634576355763657637576385763957640576415764257643576445764557646576475764857649576505765157652576535765457655576565765757658576595766057661576625766357664576655766657667576685766957670576715767257673576745767557676576775767857679576805768157682576835768457685576865768757688576895769057691576925769357694576955769657697576985769957700577015770257703577045770557706577075770857709577105771157712577135771457715577165771757718577195772057721577225772357724577255772657727577285772957730577315773257733577345773557736577375773857739577405774157742577435774457745577465774757748577495775057751577525775357754577555775657757577585775957760577615776257763577645776557766577675776857769577705777157772577735777457775577765777757778577795778057781577825778357784577855778657787577885778957790577915779257793577945779557796577975779857799578005780157802578035780457805578065780757808578095781057811578125781357814578155781657817578185781957820578215782257823578245782557826578275782857829578305783157832578335783457835578365783757838578395784057841578425784357844578455784657847578485784957850578515785257853578545785557856578575785857859578605786157862578635786457865578665786757868578695787057871578725787357874578755787657877578785787957880578815788257883578845788557886578875788857889578905789157892578935789457895578965789757898578995790057901579025790357904579055790657907579085790957910579115791257913579145791557916579175791857919579205792157922579235792457925579265792757928579295793057931579325793357934579355793657937579385793957940579415794257943579445794557946579475794857949579505795157952579535795457955579565795757958579595796057961579625796357964579655796657967579685796957970579715797257973579745797557976579775797857979579805798157982579835798457985579865798757988579895799057991579925799357994579955799657997579985799958000580015800258003580045800558006580075800858009580105801158012580135801458015580165801758018580195802058021580225802358024580255802658027580285802958030580315803258033580345803558036580375803858039580405804158042580435804458045580465804758048580495805058051580525805358054580555805658057580585805958060580615806258063580645806558066580675806858069580705807158072580735807458075580765807758078580795808058081580825808358084580855808658087580885808958090580915809258093580945809558096580975809858099581005810158102581035810458105581065810758108581095811058111581125811358114581155811658117581185811958120581215812258123581245812558126581275812858129581305813158132581335813458135581365813758138581395814058141581425814358144581455814658147581485814958150581515815258153581545815558156581575815858159581605816158162581635816458165581665816758168581695817058171581725817358174581755817658177581785817958180581815818258183581845818558186581875818858189581905819158192581935819458195581965819758198581995820058201582025820358204582055820658207582085820958210582115821258213582145821558216582175821858219582205822158222582235822458225582265822758228582295823058231582325823358234582355823658237582385823958240582415824258243582445824558246582475824858249582505825158252582535825458255582565825758258582595826058261582625826358264582655826658267582685826958270582715827258273582745827558276582775827858279582805828158282582835828458285582865828758288582895829058291582925829358294582955829658297582985829958300583015830258303583045830558306583075830858309583105831158312583135831458315583165831758318583195832058321583225832358324583255832658327583285832958330583315833258333583345833558336583375833858339583405834158342583435834458345583465834758348583495835058351583525835358354583555835658357583585835958360583615836258363583645836558366583675836858369583705837158372583735837458375583765837758378583795838058381583825838358384583855838658387583885838958390583915839258393583945839558396583975839858399584005840158402584035840458405584065840758408584095841058411584125841358414584155841658417584185841958420584215842258423584245842558426584275842858429584305843158432584335843458435584365843758438584395844058441584425844358444584455844658447584485844958450584515845258453584545845558456584575845858459584605846158462584635846458465584665846758468584695847058471584725847358474584755847658477584785847958480584815848258483584845848558486584875848858489584905849158492584935849458495584965849758498584995850058501585025850358504585055850658507585085850958510585115851258513585145851558516585175851858519585205852158522585235852458525585265852758528585295853058531585325853358534585355853658537585385853958540585415854258543585445854558546585475854858549585505855158552585535855458555585565855758558585595856058561585625856358564585655856658567585685856958570585715857258573585745857558576585775857858579585805858158582 |
- /* api.c API unit tests
- *
- * Copyright (C) 2006-2022 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)
- /* 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
- *----------------------------------------------------------------------------*/
- #define TEST_SUCCESS (1)
- #define TEST_FAIL (0)
- #define testingFmt " %s:"
- #define resultFmt " %s\n"
- static const char* passed = "passed";
- static const char* failed = "failed";
- #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;
- printf(testingFmt, "wolfSSL_Init()");
- result = wolfSSL_Init();
- printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
- if (result == WOLFSSL_SUCCESS) {
- result = 0;
- }
- return result;
- }
- static int test_wolfSSL_Cleanup(void)
- {
- int result;
- printf(testingFmt, "wolfSSL_Cleanup()");
- result = wolfSSL_Cleanup();
- printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
- if (result == WOLFSSL_SUCCESS) {
- result = 0;
- }
- return result;
- }
- /* Initialize the wolfCrypt state.
- * POST: 0 success.
- */
- static int test_wolfCrypt_Init(void)
- {
- int result;
- printf(testingFmt, "wolfCrypt_Init()");
- result = wolfCrypt_Init();
- printf(resultFmt, result == 0 ? passed : failed);
- return result;
- } /* END test_wolfCrypt_Init */
- static int test_wolfCrypt_Cleanup(void)
- {
- int result;
- printf(testingFmt, "wolfCrypt_Cleanup()");
- result = wolfCrypt_Cleanup();
- printf(resultFmt, result == 0 ? passed : failed);
- return result;
- }
- /*----------------------------------------------------------------------------*
- | Platform dependent function test
- *----------------------------------------------------------------------------*/
- static int test_fileAccess(void)
- {
- #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;
- printf(testingFmt, "test_fileAccess()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /*----------------------------------------------------------------------------*
- | 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 0;
- }
- /*----------------------------------------------------------------------------*
- | 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 0;
- }
- #endif
- #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
- (!defined(NO_RSA) || defined(HAVE_ECC))
- 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 0;
- }
- #endif
- static int test_wolfSSL_CTX_use_certificate_file(void)
- {
- #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);
- #endif
- return 0;
- }
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
- static int test_wolfSSL_CTX_use_certificate_ASN1(void)
- {
- #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
- WOLFSSL_CTX* ctx;
- int ret;
- printf(testingFmt, "wolfSSL_CTX_use_certificate_ASN1()");
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- ret = SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
- server_cert_der_2048);
- printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
- wolfSSL_CTX_free(ctx);
- if (ret == WOLFSSL_SUCCESS) {
- ret = 0;
- }
- return ret;
- #else
- return 0;
- #endif
- }
- #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)
- {
- #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
- !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
- WOLFSSL_CTX* ctx;
- int ret;
- printf(testingFmt, "wolfSSL_CTX_use_certificate_buffer()");
- 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);
- printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
- wolfSSL_CTX_free(ctx);
- if (ret == WOLFSSL_SUCCESS) {
- ret = 0;
- }
- return ret;
- #else
- return 0;
- #endif
- } /*END test_wolfSSL_CTX_use_certificate_buffer*/
- static int test_wolfSSL_CTX_use_PrivateKey_file(void)
- {
- #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);
- #endif
- return 0;
- }
- /* test both file and buffer versions along with unloading trusted peer certs */
- static int test_wolfSSL_CTX_trust_peer_cert(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_load_verify_locations(void)
- {
- #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);
- #endif
- return 0;
- }
- #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) {
- printf("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;
- }
- #endif /* !NO_FILESYSTEM && !NO_CERTS */
- static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
- {
- #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};
- printf(testingFmt, "wolfSSL_CertManagerCheckOCSPResponse()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
- * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
- #endif /* HAVE_OCSP */
- return 0;
- }
- static int test_wolfSSL_CertManagerLoadCABuffer(void)
- {
- int ret;
- #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";
- 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);
- if (ret == WOLFSSL_FATAL_ERROR)
- #elif defined(NO_RSA)
- AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
- if (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);
- if (ret == ASN_AFTER_DATE_E)
- #else
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- if (ret == WOLFSSL_SUCCESS)
- #endif
- #endif
- {
- ret = 0;
- }
- return ret;
- }
- static int test_wolfSSL_CertManagerGetCerts(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_CertManagerGetCerts()");
- 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, stdout, 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);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
- defined(WOLFSSL_SIGNER_DER_CERT) */
- return 0;
- }
- static int test_wolfSSL_CertManagerSetVerify(void)
- {
- #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;
- #endif
- return 0;
- }
- #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)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CertManagerNameConstraint2(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CertManagerNameConstraint3(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CertManagerNameConstraint4(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CertManagerNameConstraint5(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_FPKI(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_FPKI");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_CertRsaPss(void)
- {
- /* 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) && RSA_MAX_SIZE >= 3072
- const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
- const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
- #endif
- DecodedCert cert;
- byte buf[4096];
- int bytes;
- WOLFSSL_CERT_MANAGER* cm;
- printf(testingFmt, "test_CertRsaPss");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_CertManagerCRL(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_load_verify_locations_ex(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
- {
- #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 */
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_add1_chain_cert(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
- {
- int ret = 0;
- #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;
- (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);
- #endif
- return ret;
- }
- static int test_wolfSSL_CTX_SetTmpDH_file(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_der_load_verify_locations(void)
- {
- #if 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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_enable_disable(void)
- {
- #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 0;
- #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);
- #endif /* NO_CERTS */
- return 0;
- }
- static int test_wolfSSL_CTX_ticket_API(void)
- {
- #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));
- #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
- return 0;
- }
- static int test_wolfSSL_set_minmax_proto_version(void)
- {
- #ifdef OPENSSL_EXTRA
- WOLFSSL_CTX *ctx;
- WOLFSSL *ssl;
- int ret;
- (void)ret;
- (void)ssl;
- printf(testingFmt, "test_wolfSSL_set_minmax_proto_version");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /*----------------------------------------------------------------------------*
- | SSL
- *----------------------------------------------------------------------------*/
- static int test_server_wolfSSL_new(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_client_wolfSSL_new(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_SetTmpDH_file(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_SetTmpDH_buffer(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
- {
- #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);
- #endif
- return 0;
- }
- /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
- * allowed.
- * POST: return 1 on success.
- */
- static int test_wolfSSL_SetMinVersion(void)
- {
- int failFlag = WOLFSSL_SUCCESS;
- #ifndef NO_WOLFSSL_CLIENT
- 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);
- printf(testingFmt, "wolfSSL_SetMinVersion()");
- for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
- if(wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS){
- failFlag = WOLFSSL_FAILURE;
- }
- }
- printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- #endif
- if (failFlag == WOLFSSL_SUCCESS) {
- failFlag = 0;
- }
- return failFlag;
- } /* 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)
- {
- #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);
- #ifndef NO_FILESYSTEM
- BN_print_fp(stdout, k);
- printf("\n");
- #endif
- XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
- hexStr = BN_bn2hex(Gx);
- AssertStrEQ(hexStr, kGx);
- #ifndef NO_FILESYSTEM
- BN_print_fp(stdout, Gx);
- printf("\n");
- #endif
- XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
- hexStr = BN_bn2hex(Gy);
- AssertStrEQ(hexStr, kGy);
- #ifndef NO_FILESYSTEM
- BN_print_fp(stdout, Gy);
- printf("\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);
- #endif /* HAVE_ECC */
- #endif /* OPENSSL_EXTRA && !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
- return 0;
- }
- #endif /* OPENSSL_EXTRA */
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
- {
- #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);
- #endif /* HAVE_ECC */
- return 0;
- }
- #endif /* !NO_BIO */
- # if defined(OPENSSL_EXTRA)
- static int test_wolfSSL_ECDSA_SIG(void)
- {
- #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);
- #endif /* HAVE_ECC */
- return 0;
- }
- static int test_EC_i2d(void)
- {
- #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);
- #endif /* HAVE_ECC */
- return 0;
- }
- static int test_ECDSA_size_sign(void)
- {
- #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);
- #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
- return 0;
- }
- static int test_ED25519(void)
- {
- #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 */
- #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
- return 0;
- }
- static int test_ED448(void)
- {
- #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 */
- #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
- return 0;
- }
- #endif /* OPENSSL_EXTRA */
- #include <wolfssl/openssl/pem.h>
- /*----------------------------------------------------------------------------*
- | EVP
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_EVP_PKEY_print_public(void)
- {
- #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;
- printf(testingFmt, "EVP_PKEY_print_public()");
- /* 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;
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- /* Test functions for base64 encode/decode */
- static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
- {
- #if defined(OPENSSL_EXTRA) && \
- ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
- EVP_ENCODE_CTX* ctx = NULL;
- printf(testingFmt, "EVP_ENCODE_CTX_new()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
- return 0;
- }
- static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
- {
- #if defined(OPENSSL_EXTRA) && \
- ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
- EVP_ENCODE_CTX* ctx = NULL;
- printf(testingFmt, "EVP_ENCODE_CTX_free()");
- AssertNotNull( ctx = EVP_ENCODE_CTX_new());
- EVP_ENCODE_CTX_free(ctx);
- printf(resultFmt, passed);
- #endif /*OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE)*/
- return 0;
- }
- static int test_wolfSSL_EVP_EncodeInit(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
- EVP_ENCODE_CTX* ctx = NULL;
- printf(testingFmt, "EVP_EncodeInit()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
- return 0;
- }
- static int test_wolfSSL_EVP_EncodeUpdate(void)
- {
- #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;
- printf(testingFmt, "EVP_EncodeUpdate()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
- return 0;
- }
- static int test_wolfSSL_EVP_EncodeFinal(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
- printf(testingFmt, "wolfSSL_EVP_EncodeFinal()");
- /* tests for wolfSSL_EVP_EncodeFinal are included in
- * test_wolfSSL_EVP_EncodeUpdate
- */
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
- return 0;
- }
- static int test_wolfSSL_EVP_DecodeInit(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
- EVP_ENCODE_CTX* ctx = NULL;
- printf(testingFmt, "EVP_DecodeInit()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
- return 0;
- }
- static int test_wolfSSL_EVP_DecodeUpdate(void)
- {
- #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."} */
- printf(testingFmt, "EVP_DecodeUpdate()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
- return 0;
- }
- static int test_wolfSSL_EVP_DecodeFinal(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
- printf(testingFmt, "EVP_DecodeFinal()");
- /* tests for wolfSSL_EVP_DecodeFinal are included in
- * test_wolfSSL_EVP_DecodeUpdate
- */
- printf(resultFmt, passed);
- #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
- return 0;
- }
- /* 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 0;
- }
- static int test_wolfSSL_EVP_CIPHER_CTX(void)
- {
- #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);
- #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
- return 0;
- }
- #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];
- printf("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';
- printf("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];
- printf("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';
- printf("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];
- printf("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';
- printf("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];
- printf("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';
- printf("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;
- }
- #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];
- printf("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';
- printf("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);
- printf("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];
- printf("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';
- printf("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];
- printf("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';
- printf("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) {
- printf("CTX static new failed %d\n", ret);
- return 0;
- }
- }
- #endif
- if (ctx == NULL) {
- ctx = wolfSSL_CTX_new(callbacks->method());
- }
- if (ctx == NULL) {
- printf("CTX new failed\n");
- return 0;
- }
- /* 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)
- 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) {
- printf("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, 0);
- } while (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));
- /*err_sys("SSL_accept failed");*/
- }
- else {
- if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
- input[idx] = 0;
- printf("Client message: %s\n", input);
- }
- AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
- #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) {
- printf("CTX static new failed %d\n", ret);
- return;
- }
- }
- #endif
- if (ctx == NULL) {
- ctx = wolfSSL_CTX_new(callbacks->method());
- }
- if (ctx == NULL) {
- printf("CTX new failed\n");
- return;
- }
- #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, 0);
- } while (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));
- /*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, 0);
- } 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, 0);
- } while (err == WC_PENDING_E);
- if (ret > 0) {
- input[ret] = '\0'; /* null term */
- printf("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 0;
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
- static int test_wolfSSL_reuse_WOLFSSLobj(void)
- {
- /* 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
- return 0;
- }
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
- static int test_wolfSSL_CTX_verifyDepth_ServerClient(void)
- {
- #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));
- printf(testingFmt, "test_wolfSSL_CTX_verifyDepth_ServerClient()\n");
- #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);
- printf(resultFmt, passed);
- #else
- (void)test_client_verifyDepth;
- #endif /* (OPENSSL_EXTRA) && !(WOLFSSL_TIRTOS) && (NO_WOLFSSL_CLIENT) */
- return 0;
- }
- 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];
- printf("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';
- printf("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)
- {
- #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);
- #else
- (void)test_client_get_finished;
- #endif /* !NO_RSA && WOLFSSL_HAVE_TLS_UNIQUE */
- return 0;
- }
- #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);
- /* 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));
- AssertNotNull(wolfSSL_SESSION_get0_peer(*sess));
- }
- #endif
- }
- 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)
- {
- #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;
- printf(testingFmt, "wolfSSL_CTX_add_session()");
- 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #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);
- printf("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
- }
- printf(testingFmt, "wolfSSL_dtls_export()");
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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';
- printf("Client message export/import: %s\n", input);
- }
- else {
- printf("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 0;
- }
- #endif
- static int test_wolfSSL_tls_export(void)
- {
- #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
- printf(testingFmt, "wolfSSL_tls_export()");
- test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
- #ifdef WOLFSSL_TLS13
- test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
- #endif
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /*----------------------------------------------------------------------------*
- | 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)
- {
- #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);
- #endif /* !NO_WOLFSSL_CLIENT */
- return 0;
- }
- /* 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)
- {
- #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 */
- 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 */
- 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 */
- 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 */
- 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 */
- 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 */
- 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) */
- 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) */
- 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 */
- 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 */
- 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);
- }
- #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
- return 0;
- }
- 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 0;
- }
- #endif /* HAVE_SNI */
- static int test_wolfSSL_UseSNI(void)
- {
- #ifdef HAVE_SNI
- test_wolfSSL_UseSNI_params();
- test_wolfSSL_UseSNI_connection();
- test_wolfSSL_SNI_GetFromBuffer();
- #endif
- return 0;
- }
- #endif /* HAVE_IO_TESTS_DEPENDENCIES */
- static int test_wolfSSL_UseTrustedCA(void)
- {
- #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);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* HAVE_TRUSTED_CA */
- return 0;
- }
- static int test_wolfSSL_UseMaxFragment(void)
- {
- #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);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return 0;
- }
- static int test_wolfSSL_UseTruncatedHMAC(void)
- {
- #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);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return 0;
- }
- static int test_wolfSSL_UseSupportedCurve(void)
- {
- #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);
- #endif
- return 0;
- }
- #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)
- {
- #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);
- #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
- return 0;
- }
- static int test_wolfSSL_UseALPN_params(void)
- {
- #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);
- #endif
- return 0;
- }
- #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)
- {
- #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);
- #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
- return 0;
- }
- #endif /* HAVE_ALPN_PROTOS_SUPPORT */
- static int test_wolfSSL_UseALPN(void)
- {
- #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) &&\
- defined(HAVE_IO_TESTS_DEPENDENCIES)
- test_wolfSSL_UseALPN_connection();
- test_wolfSSL_UseALPN_params();
- #endif
- #ifdef HAVE_ALPN_PROTOS_SUPPORT
- test_wolfSSL_set_alpn_protos();
- #endif
- return 0;
- }
- static int test_wolfSSL_DisableExtendedMasterSecret(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
- {
- #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);
- #endif
- return 0;
- }
- /*----------------------------------------------------------------------------*
- | X509 Tests
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_X509_NAME_get_entry(void)
- {
- #if !defined(NO_CERTS) && !defined(NO_RSA)
- #if defined(OPENSSL_ALL) || \
- (defined(OPENSSL_EXTRA) && \
- (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
- printf(testingFmt, "wolfSSL_X509_NAME_get_entry()");
- {
- /* 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(stdout, 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);
- }
- printf(resultFmt, passed);
- #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
- #endif /* !NO_CERTS && !NO_RSA */
- return 0;
- }
- /* Testing functions dealing with PKCS12 parsing out X509 certs */
- static int test_wolfSSL_PKCS12(void)
- {
- /* .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
- printf(testingFmt, "wolfSSL_PKCS12()");
- 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;
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- #endif /* HAVE_FIPS */
- return 0;
- }
- #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)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_no_password_cb()");
- #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."));
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #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;
- printf(testingFmt, "wolfSSL_PKCS8()");
- #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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
- return 0;
- }
- static int test_wolfSSL_PKCS8_ED25519(void)
- {
- #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);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return 0;
- }
- static int test_wolfSSL_PKCS8_ED448(void)
- {
- #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);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return 0;
- }
- /* Testing functions dealing with PKCS5 */
- static int test_wolfSSL_PKCS5(void)
- {
- #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);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
- return 0;
- }
- /* test parsing URI from certificate */
- static int test_wolfSSL_URI(void)
- {
- #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";
- printf(testingFmt, "wolfSSL URI parse");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_TBS(void)
- {
- #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
- && defined(OPENSSL_EXTRA)
- WOLFSSL_X509* x509;
- const unsigned char* tbs;
- int tbsSz;
- printf(testingFmt, "wolfSSL TBS");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_verify(void)
- {
- #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;
- printf(testingFmt, "wolfSSL X509 verify");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_X509_TLS_version");
- /* 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
- * version allowed.
- * POST: 1 on success.
- */
- static int test_wolfSSL_CTX_SetMinVersion(void)
- {
- int failFlag = WOLFSSL_SUCCESS;
- #ifndef NO_WOLFSSL_CLIENT
- 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());
- printf(testingFmt, "wolfSSL_CTX_SetMinVersion()");
- for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++){
- if(wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr)) != WOLFSSL_SUCCESS){
- failFlag = WOLFSSL_FAILURE;
- }
- }
- printf(resultFmt, failFlag == WOLFSSL_SUCCESS ? passed : failed);
- wolfSSL_CTX_free(ctx);
- #endif
- if (failFlag == WOLFSSL_SUCCESS) {
- failFlag = 0;
- }
- return failFlag;
- } /* 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)
- {
- #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);
- printf(testingFmt, "wolfSSL_UseOCSPStapling()");
- ret = wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
- WOLFSSL_CSR2_OCSP_USE_NONCE);
- printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- if (ret == WOLFSSL_SUCCESS) {
- ret = 0;
- }
- return ret;
- #else
- return 0;
- #endif
- } /*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)
- {
- #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);
- printf(testingFmt, "wolfSSL_UseOCSPStaplingV2()");
- ret = wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
- WOLFSSL_CSR2_OCSP_USE_NONCE );
- printf(resultFmt, ret == WOLFSSL_SUCCESS ? passed : failed);
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- if (ret == WOLFSSL_SUCCESS) {
- ret = 0;
- }
- return ret;
- #else
- return 0;
- #endif
- } /*END test_wolfSSL_UseOCSPStaplingV2*/
- /*----------------------------------------------------------------------------*
- | Multicast Tests
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_mcast(void)
- {
- #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);
- #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 || WOLFSSL_SNIFFER) */
- return 0;
- }
- /*----------------------------------------------------------------------------*
- | Wolfcrypt
- *----------------------------------------------------------------------------*/
- /*
- * Unit test for the wc_InitBlake2b()
- */
- static int test_wc_InitBlake2b(void)
- {
- int ret = 0;
- #ifdef HAVE_BLAKE2
- Blake2b blake;
- printf(testingFmt, "wc_InitBlake2B()");
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /*END test_wc_InitBlake2b*/
- /*
- * Unit test for the wc_InitBlake2b_WithKey()
- */
- static int test_wc_InitBlake2b_WithKey(void)
- {
- int ret = 0;
- #ifdef HAVE_BLAKE2
- Blake2b blake;
- word32 digestSz = BLAKE2B_KEYBYTES;
- byte key[BLAKE2B_KEYBYTES];
- word32 keylen = BLAKE2B_KEYBYTES;
- printf(testingFmt, "wc_InitBlake2b_WithKey()");
- /* 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /*END wc_InitBlake2b_WithKey*/
- /*
- * Unit test for the wc_InitBlake2s_WithKey()
- */
- static int test_wc_InitBlake2s_WithKey(void)
- {
- int ret = 0;
- #ifdef HAVE_BLAKE2S
- Blake2s blake;
- word32 digestSz = BLAKE2S_KEYBYTES;
- byte *key = (byte*)"01234567890123456789012345678901";
- word32 keylen = BLAKE2S_KEYBYTES;
- printf(testingFmt, "wc_InitBlake2s_WithKey()");
- /* 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /*END wc_InitBlake2s_WithKey*/
- /*
- * Unit test for the wc_InitMd5()
- */
- static int test_wc_InitMd5(void)
- {
- int flag = 0;
- #ifndef NO_MD5
- wc_Md5 md5;
- int ret;
- printf(testingFmt, "wc_InitMd5()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_InitMd5 */
- /*
- * Testing wc_UpdateMd5()
- */
- static int test_wc_Md5Update(void)
- {
- int flag = 0;
- #ifndef NO_MD5
- wc_Md5 md5;
- byte hash[WC_MD5_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- ret = wc_InitMd5(&md5);
- if (ret != 0) {
- flag = ret;
- }
- printf(testingFmt, "wc_Md5Update()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Md5Update() */
- /*
- * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
- */
- static int test_wc_Md5Final(void)
- {
- int flag = 0;
- #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;
- /* 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*);
- /* Test good args. */
- printf(testingFmt, "wc_Md5Final()");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- }
- /*
- * Unit test for the wc_InitSha()
- */
- static int test_wc_InitSha(void)
- {
- int flag = 0;
- #ifndef NO_SHA
- wc_Sha sha;
- int ret;
- printf(testingFmt, "wc_InitSha()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_InitSha */
- /*
- * Tesing wc_ShaUpdate()
- */
- static int test_wc_ShaUpdate(void)
- {
- int flag = 0;
- #ifndef NO_SHA
- wc_Sha sha;
- byte hash[WC_SHA_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- ret = wc_InitSha(&sha);
- if (ret != 0) {
- flag = ret;
- }
- printf(testingFmt, "wc_ShaUpdate()");
- /* 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);
- /* If not returned then the unit test passed test vectors. */
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_ShaUpdate() */
- /*
- * Unit test on wc_ShaFinal
- */
- static int test_wc_ShaFinal(void)
- {
- int flag = 0;
- #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;
- /*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*);
- /* Good test args. */
- printf(testingFmt, "wc_ShaFinal()");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_ShaFinal */
- /*
- * Unit test for wc_InitSha256()
- */
- static int test_wc_InitSha256(void)
- {
- int flag = 0;
- #ifndef NO_SHA256
- wc_Sha256 sha256;
- int ret;
- printf(testingFmt, "wc_InitSha256()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_InitSha256 */
- /*
- * Unit test for wc_Sha256Update()
- */
- static int test_wc_Sha256Update(void)
- {
- int flag = 0;
- #ifndef NO_SHA256
- wc_Sha256 sha256;
- byte hash[WC_SHA256_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- ret = wc_InitSha256(&sha256);
- if (ret != 0) {
- flag = ret;
- }
- printf(testingFmt, "wc_Sha256Update()");
- /* 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);
- /* If not returned then the unit test passed. */
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256Update */
- /*
- * Unit test function for wc_Sha256Final()
- */
- static int test_wc_Sha256Final(void)
- {
- int flag = 0;
- #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;
- /* 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*);
- /* Good test args. */
- printf(testingFmt, "wc_Sha256Final()");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256Final */
- /*
- * Unit test function for wc_Sha256FinalRaw()
- */
- static int test_wc_Sha256FinalRaw(void)
- {
- int flag = 0;
- #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;
- /* 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*);
- /* Good test args. */
- printf(testingFmt, "wc_Sha256FinalRaw()");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256FinalRaw */
- /*
- * Unit test function for wc_Sha256GetFlags()
- */
- static int test_wc_Sha256GetFlags(void)
- {
- int flag = 0;
- #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha256 sha256;
- word32 flags = 0;
- printf(testingFmt, "wc_Sha256GetFlags()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256GetFlags */
- /*
- * Unit test function for wc_Sha256Free()
- */
- static int test_wc_Sha256Free(void)
- {
- int flag = 0;
- #ifndef NO_SHA256
- printf(testingFmt, "wc_Sha256Free()");
- wc_Sha256Free(NULL);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256Free */
- /*
- * Unit test function for wc_Sha256GetHash()
- */
- static int test_wc_Sha256GetHash(void)
- {
- int flag = 0;
- #ifndef NO_SHA256
- wc_Sha256 sha256;
- byte hash1[WC_SHA256_DIGEST_SIZE];
- printf(testingFmt, "wc_Sha256GetHash()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256GetHash */
- /*
- * Unit test function for wc_Sha256Copy()
- */
- static int test_wc_Sha256Copy(void)
- {
- int flag = 0;
- #ifndef NO_SHA256
- wc_Sha256 sha256;
- wc_Sha256 temp;
- printf(testingFmt, "wc_Sha256Copy()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256Copy */
- /*
- * Testing wc_InitSha512()
- */
- static int test_wc_InitSha512(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA512
- wc_Sha512 sha512;
- int ret;
- printf(testingFmt, "wc_InitSha512()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_InitSha512 */
- /*
- * wc_Sha512Update() test.
- */
- static int test_wc_Sha512Update(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA512
- wc_Sha512 sha512;
- byte hash[WC_SHA512_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- ret = wc_InitSha512(&sha512);
- if (ret != 0) {
- flag = ret;
- }
- printf(testingFmt, "wc_Sha512Update()");
- /* 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);
- /* If not returned then the unit test passed test vectors. */
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* 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 BAD_FUNC_ARG;
- /* 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 flag = 0;
- #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;
- /* 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. */
- printf(testingFmt, "wc_Sha512Final()");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha512Final */
- /*
- * Unit test function for wc_Sha512GetFlags()
- */
- static int test_wc_Sha512GetFlags(void)
- {
- int flag = 0;
- #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha512 sha512;
- word32 flags = 0;
- printf(testingFmt, "wc_Sha512GetFlags()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha512GetFlags */
- /*
- * Unit test function for wc_Sha512FinalRaw()
- */
- static int test_wc_Sha512FinalRaw(void)
- {
- int flag = 0;
- #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;
- /* 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. */
- printf(testingFmt, "wc_Sha512FinalRaw()");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha512FinalRaw */
- /*
- * Unit test function for wc_Sha512Free()
- */
- static int test_wc_Sha512Free(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA512
- printf(testingFmt, "wc_Sha512Free()");
- wc_Sha512Free(NULL);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* 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 WOLFSSL_FATAL_ERROR;
- 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 flag = 0;
- #ifdef WOLFSSL_SHA512
- wc_Sha512 sha512;
- byte hash1[WC_SHA512_DIGEST_SIZE];
- printf(testingFmt, "wc_Sha512GetHash()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha512GetHash */
- /*
- * Unit test function for wc_Sha512Copy()
- */
- static int test_wc_Sha512Copy(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA512
- wc_Sha512 sha512;
- wc_Sha512 temp;
- printf(testingFmt, "wc_Sha512Copy()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha512Copy */
- static int test_wc_InitSha512_224(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- wc_Sha512 sha512;
- int ret;
- printf(testingFmt, "wc_InitSha512_224()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_224Update(void)
- {
- int flag = 0;
- #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;
- ret = wc_InitSha512_224(&sha512);
- if (ret != 0) {
- flag = ret;
- }
- printf(testingFmt, "wc_Sha512_224Update()");
- /* 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);
- /* If not returned then the unit test passed test vectors. */
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_224Final(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- printf(testingFmt, "wc_Sha512_224Final()");
- flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_224GetFlags(void)
- {
- int flag = 0;
- #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;
- printf(testingFmt, "wc_Sha512_224GetFlags()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_224FinalRaw(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
- defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
- !defined(WOLFSSL_NO_HASH_RAW)
- printf(testingFmt, "wc_Sha512_224FinalRaw()");
- flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- }
- static int test_wc_Sha512_224Free(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- printf(testingFmt, "wc_Sha512_224Free()");
- wc_Sha512_224Free(NULL);
- printf(resultFmt, passed);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_224GetHash(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- printf(testingFmt, "wc_Sha512_224GetHash()");
- flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_224Copy(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
- wc_Sha512 sha512;
- wc_Sha512 temp;
- printf(testingFmt, "wc_Sha512_224Copy()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_InitSha512_256(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- wc_Sha512 sha512;
- int ret;
- printf(testingFmt, "wc_InitSha512_256()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_256Update(void)
- {
- int flag = 0;
- #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;
- ret = wc_InitSha512_256(&sha512);
- if (ret != 0) {
- flag = ret;
- }
- printf(testingFmt, "wc_Sha512_256Update()");
- /* 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);
- /* If not returned then the unit test passed test vectors. */
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_256Final(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- printf(testingFmt, "wc_Sha512_256Final()");
- flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_256GetFlags(void)
- {
- int flag = 0;
- #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;
- printf(testingFmt, "wc_Sha512_256GetFlags()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_256FinalRaw(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
- defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
- !defined(WOLFSSL_NO_HASH_RAW)
- printf(testingFmt, "wc_Sha512_256FinalRaw()");
- flag = test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- }
- static int test_wc_Sha512_256Free(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- printf(testingFmt, "wc_Sha512_256Free()");
- wc_Sha512_256Free(NULL);
- printf(resultFmt, passed);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_256GetHash(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- printf(testingFmt, "wc_Sha512_256GetHash()");
- flag = test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- static int test_wc_Sha512_256Copy(void)
- {
- int flag = 0;
- #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
- wc_Sha512 sha512;
- wc_Sha512 temp;
- printf(testingFmt, "wc_Sha512_256Copy()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
- return flag;
- }
- /*
- * Testing wc_InitSha384()
- */
- static int test_wc_InitSha384(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA384
- wc_Sha384 sha384;
- int ret;
- printf(testingFmt, "wc_InitSha384()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_InitSha384 */
- /*
- * test wc_Sha384Update()
- */
- static int test_wc_Sha384Update(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA384
- wc_Sha384 sha384;
- byte hash[WC_SHA384_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- ret = wc_InitSha384(&sha384);
- if (ret != 0) {
- flag = ret;
- }
- printf(testingFmt, "wc_Sha384Update()");
- /* 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);
- /* If not returned then the unit test passed test vectors. */
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384Update */
- /*
- * Unit test function for wc_Sha384Final();
- */
- static int test_wc_Sha384Final(void)
- {
- int flag = 0;
- #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;
- /* 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. */
- printf(testingFmt, "wc_Sha384Final()");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384Final */
- /*
- * Unit test function for wc_Sha384GetFlags()
- */
- static int test_wc_Sha384GetFlags(void)
- {
- int flag = 0;
- #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha384 sha384;
- word32 flags = 0;
- printf(testingFmt, "wc_Sha384GetFlags()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384GetFlags */
- /*
- * Unit test function for wc_Sha384FinalRaw()
- */
- static int test_wc_Sha384FinalRaw(void)
- {
- int flag = 0;
- #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;
- /* 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. */
- printf(testingFmt, "wc_Sha384FinalRaw()");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384FinalRaw */
- /*
- * Unit test function for wc_Sha384Free()
- */
- static int test_wc_Sha384Free(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA384
- printf(testingFmt, "wc_Sha384Free()");
- wc_Sha384Free(NULL);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384Free */
- /*
- * Unit test function for wc_Sha384GetHash()
- */
- static int test_wc_Sha384GetHash(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA384
- wc_Sha384 sha384;
- byte hash1[WC_SHA384_DIGEST_SIZE];
- printf(testingFmt, "wc_Sha384GetHash()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384GetHash */
- /*
- * Unit test function for wc_Sha384Copy()
- */
- static int test_wc_Sha384Copy(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA384
- wc_Sha384 sha384;
- wc_Sha384 temp;
- printf(testingFmt, "wc_Sha384Copy()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384Copy */
- /*
- * Testing wc_InitSha224();
- */
- static int test_wc_InitSha224(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA224
- wc_Sha224 sha224;
- int ret;
- printf(testingFmt, "wc_InitSha224()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_InitSha224 */
- /*
- * Unit test on wc_Sha224Update
- */
- static int test_wc_Sha224Update(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA224
- wc_Sha224 sha224;
- byte hash[WC_SHA224_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- ret = wc_InitSha224(&sha224);
- if (ret != 0) {
- flag = ret;
- }
- printf(testingFmt, "wc_Sha224Update()");
- /* 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);
- /* If not returned then the unit test passed test vectors. */
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224Update */
- /*
- * Unit test for wc_Sha224Final();
- */
- static int test_wc_Sha224Final(void)
- {
- int flag = 0;
- #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;
- /* 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. */
- printf(testingFmt, "wc_sha224Final()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224Final */
- /*
- * Unit test function for wc_Sha224SetFlags()
- */
- static int test_wc_Sha224SetFlags(void)
- {
- int flag = 0;
- #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha224 sha224;
- word32 flags = 0;
- printf(testingFmt, "wc_Sha224SetFlags()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224SetFlags */
- /*
- * Unit test function for wc_Sha224GetFlags()
- */
- static int test_wc_Sha224GetFlags(void)
- {
- int flag = 0;
- #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha224 sha224;
- word32 flags = 0;
- printf(testingFmt, "wc_Sha224GetFlags()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224GetFlags */
- /*
- * Unit test function for wc_Sha224Free()
- */
- static int test_wc_Sha224Free(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA224
- printf(testingFmt, "wc_Sha224Free()");
- wc_Sha224Free(NULL);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224Free */
- /*
- * Unit test function for wc_Sha224GetHash()
- */
- static int test_wc_Sha224GetHash(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA224
- wc_Sha224 sha224;
- byte hash1[WC_SHA224_DIGEST_SIZE];
- printf(testingFmt, "wc_Sha224GetHash()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224GetHash */
- /*
- * Unit test function for wc_Sha224Copy()
- */
- static int test_wc_Sha224Copy(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_SHA224
- wc_Sha224 sha224;
- wc_Sha224 temp;
- printf(testingFmt, "wc_Sha224Copy()");
- /* 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224Copy */
- /*
- * Testing wc_InitRipeMd()
- */
- static int test_wc_InitRipeMd(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_RIPEMD
- RipeMd ripemd;
- int ret;
- printf(testingFmt, "wc_InitRipeMd()");
- /* 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;
- }
- }
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_InitRipeMd */
- /*
- * Testing wc_RipeMdUpdate()
- */
- static int test_wc_RipeMdUpdate(void)
- {
- int flag = 0;
- #ifdef WOLFSSL_RIPEMD
- RipeMd ripemd;
- byte hash[RIPEMD_DIGEST_SIZE];
- testVector a, b, c;
- int ret;
- ret = wc_InitRipeMd(&ripemd);
- if (ret != 0) {
- flag = ret;
- }
- printf(testingFmt, "wc_RipeMdUpdate()");
- /* 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;
- }
- }
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_RipeMdUdpate */
- /*
- * Unit test function for wc_RipeMdFinal()
- */
- static int test_wc_RipeMdFinal(void)
- {
- int flag = 0;
- #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;
- /* 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*);
- /* Good test args. */
- printf(testingFmt, "wc_RipeMdFinal()");
- /* 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;
- }
- }
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* 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 ret = 0;
- #if defined(WOLFSSL_SHA3)
- wc_Sha3 sha3;
- (void)sha3;
- #if !defined(WOLFSSL_NOSHA3_224)
- printf(testingFmt, "wc_InitSha3_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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif /* NOSHA3_224 */
- #if !defined(WOLFSSL_NOSHA3_256)
- if (ret == 0) {
- printf(testingFmt, "wc_InitSha3_256()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- } /* END sha3_256 */
- #endif /* NOSHA3_256 */
- #if !defined(WOLFSSL_NOSHA3_384)
- if (ret == 0) {
- printf(testingFmt, "wc_InitSha3_384()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- } /* END sha3_384 */
- #endif /* NOSHA3_384 */
- #if !defined(WOLFSSL_NOSHA3_512)
- if (ret == 0) {
- printf(testingFmt, "wc_InitSha3_512()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- } /* END sha3_512 */
- #endif /* NOSHA3_512 */
- #endif
- return ret;
- } /* END test_wc_InitSha3 */
- /*
- * Testing wc_Sha3_Update()
- */
- static int testing_wc_Sha3_Update(void)
- {
- int ret = 0;
- #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);
- #if !defined(WOLFSSL_NOSHA3_224)
- printf(testingFmt, "wc_Sha3_224_Update()");
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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 ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif /* SHA3_224 */
- #if !defined(WOLFSSL_NOSHA3_256)
- if (ret == 0) {
- printf(testingFmt, "wc_Sha3_256_Update()");
- ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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 ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- }
- #endif /* SHA3_256 */
- #if !defined(WOLFSSL_NOSHA3_384)
- if (ret == 0) {
- printf(testingFmt, "wc_Sha3_384_Update()");
- ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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 ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- }
- #endif /* SHA3_384 */
- #if !defined(WOLFSSL_NOSHA3_512)
- if (ret == 0) {
- printf(testingFmt, "wc_Sha3_512_Update()");
- ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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 ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- }
- #endif /* SHA3_512 */
- #endif /* WOLFSSL_SHA3 */
- return ret;
- } /* END testing_wc_Sha3_Update */
- /*
- * Testing wc_Sha3_224_Final()
- */
- static int test_wc_Sha3_224_Final(void)
- {
- int ret = 0;
- #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];
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- printf(testingFmt, "wc_Sha3_224_Final()");
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret == 0) {
- printf(testingFmt, "wc_Sha3_224_GetHash()");
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- }
- wc_Sha3_224_Free(&sha3);
- #endif
- return ret;
- } /* END test_wc_Sha3_224_Final */
- /*
- * Testing wc_Sha3_256_Final()
- */
- static int test_wc_Sha3_256_Final(void)
- {
- int ret = 0;
- #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];
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- printf(testingFmt, "wc_Sha3_256_Final()");
- ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret == 0) {
- printf(testingFmt, "wc_Sha3_256_GetHash()");
- ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- }
- wc_Sha3_256_Free(&sha3);
- #endif
- return ret;
- } /* END test_wc_Sha3_256_Final */
- /*
- * Testing wc_Sha3_384_Final()
- */
- static int test_wc_Sha3_384_Final(void)
- {
- int ret = 0;
- #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];
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- printf(testingFmt, "wc_Sha3_384_Final()");
- ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret == 0) {
- printf(testingFmt, "wc_Sha3_384_GetHash()");
- ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- }
- wc_Sha3_384_Free(&sha3);
- #endif
- return ret;
- } /* END test_wc_Sha3_384_Final */
- /*
- * Testing wc_Sha3_512_Final()
- */
- static int test_wc_Sha3_512_Final(void)
- {
- int ret = 0;
- #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];
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- printf(testingFmt, "wc_Sha3_512_Final()");
- ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret == 0) {
- printf(testingFmt, "wc_Sha3_512_GetHash()");
- ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- }
- wc_Sha3_512_Free(&sha3);
- #endif
- return ret;
- } /* END test_wc_Sha3_512_Final */
- /*
- * Testing wc_Sha3_224_Copy()
- */
- static int test_wc_Sha3_224_Copy(void)
- {
- int ret = 0;
- #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];
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- printf(testingFmt, "wc_Sha3_224_Copy()");
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Sha3_224_Free(&sha3);
- return ret;
- }
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Sha3_224_Copy */
- /*
- * Testing wc_Sha3_256_Copy()
- */
- static int test_wc_Sha3_256_Copy(void)
- {
- int ret = 0;
- #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];
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- printf(testingFmt, "wc_Sha3_256_Copy()");
- ret = wc_InitSha3_256(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Sha3_256_Free(&sha3);
- return ret;
- }
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Sha3_256_Copy */
- /*
- * Testing wc_Sha3_384_Copy()
- */
- static int test_wc_Sha3_384_Copy(void)
- {
- int ret = 0;
- #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];
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- printf(testingFmt, "wc_Sha3_384_Copy()");
- ret = wc_InitSha3_384(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Sha3_384_Free(&sha3);
- return ret;
- }
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Sha3_384_Copy */
- /*
- * Testing wc_Sha3_512_Copy()
- */
- static int test_wc_Sha3_512_Copy(void)
- {
- int ret = 0;
- #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];
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- printf(testingFmt, "wc_Sha3_512_Copy()");
- ret = wc_InitSha3_512(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Sha3_512_Free(&sha3);
- return ret;
- }
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Sha3_512_Copy */
- /*
- * Unit test function for wc_Sha3_GetFlags()
- */
- static int test_wc_Sha3_GetFlags(void)
- {
- int ret = 0;
- #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
- wc_Sha3 sha3;
- word32 flags = 0;
- printf(testingFmt, "wc_Sha3_GetFlags()");
- /* Initialize */
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Sha3_GetFlags */
- static int test_wc_InitShake256(void)
- {
- int ret = 0;
- #ifdef WOLFSSL_SHAKE256
- wc_Shake shake;
- printf(testingFmt, "wc_InitShake256()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_InitSha3 */
- static int testing_wc_Shake256_Update(void)
- {
- int ret = 0;
- #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);
- printf(testingFmt, "wc_Shake256_Update()");
- ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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 ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif /* WOLFSSL_SHAKE256 */
- return ret;
- }
- static int test_wc_Shake256_Final(void)
- {
- int ret = 0;
- #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];
- /* Init stack variables. */
- XMEMSET(hash, 0, sizeof(hash));
- printf(testingFmt, "wc_Shake256_Final()");
- ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- }
- /*
- * Testing wc_Shake256_Copy()
- */
- static int test_wc_Shake256_Copy(void)
- {
- int ret = 0;
- #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);
- XMEMSET(hash, 0, sizeof(hash));
- XMEMSET(hashCpy, 0, sizeof(hashCpy));
- printf(testingFmt, "wc_Shake256_Copy()");
- ret = wc_InitShake256(&shake, HEAP_HINT, testDevId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId);
- if (ret != 0) {
- wc_Shake256_Free(&shake);
- return ret;
- }
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Shake256_Copy */
- /*
- * Unit test function for wc_Shake256Hash()
- */
- static int test_wc_Shake256Hash(void)
- {
- int ret = 0;
- #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);
- printf(testingFmt, "wc_Shake256Hash()");
- ret = wc_Shake256Hash(data, len, hash, hashLen);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Shake256Hash */
- /*
- * Test function for wc_HmacSetKey
- */
- static int test_wc_Md5HmacSetKey(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && !defined(NO_MD5)
- Hmac hmac;
- int ret, times, itr;
- 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;
- printf(testingFmt, "wc_HmacSetKey() with MD5");
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- 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 0;
- else {
- return WOLFSSL_FATAL_ERROR;
- }
- #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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Md5HmacSetKey */
- /*
- * testing wc_HmacSetKey() on wc_Sha hash.
- */
- static int test_wc_ShaHmacSetKey(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && !defined(NO_SHA)
- Hmac hmac;
- int ret, times, itr;
- 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;
- printf(testingFmt, "wc_HmacSetKey() with SHA");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_ShaHmacSetKey() */
- /*
- * testing wc_HmacSetKey() on Sha224 hash.
- */
- static int test_wc_Sha224HmacSetKey(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
- Hmac hmac;
- int ret, times, itr;
- 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;
- printf(testingFmt, "wc_HmacSetKey() with SHA 224");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224HmacSetKey() */
- /*
- * testing wc_HmacSetKey() on Sha256 hash
- */
- static int test_wc_Sha256HmacSetKey(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && !defined(NO_SHA256)
- Hmac hmac;
- int ret, times, itr;
- 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;
- printf(testingFmt, "wc_HmacSetKey() with SHA256");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256HmacSetKey() */
- /*
- * testing wc_HmacSetKey on Sha384 hash.
- */
- static int test_wc_Sha384HmacSetKey(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
- Hmac hmac;
- int ret, times, itr;
- 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;
- printf(testingFmt, "wc_HmacSetKey() with SHA384");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384HmacSetKey() */
- /*
- * testing wc_HmacUpdate on wc_Md5 hash.
- */
- static int test_wc_Md5HmacUpdate(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
- Hmac hmac;
- testVector a, b;
- int ret;
- #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;
- printf(testingFmt, "wc_HmacUpdate() with MD5");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Md5HmacUpdate */
- /*
- * testing wc_HmacUpdate on SHA hash.
- */
- static int test_wc_ShaHmacUpdate(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && !defined(NO_SHA)
- Hmac hmac;
- testVector a, b;
- int ret;
- #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;
- printf(testingFmt, "wc_HmacUpdate() with SHA");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_ShaHmacUpdate */
- /*
- * testing wc_HmacUpdate on SHA224 hash.
- */
- static int test_wc_Sha224HmacUpdate(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
- Hmac hmac;
- testVector a, b;
- int ret;
- #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;
- printf(testingFmt, "wc_HmacUpdate() with SHA224");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224HmacUpdate */
- /*
- * testing wc_HmacUpdate on SHA256 hash.
- */
- static int test_wc_Sha256HmacUpdate(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && !defined(NO_SHA256)
- Hmac hmac;
- testVector a, b;
- int ret;
- #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;
- printf(testingFmt, "wc_HmacUpdate() with WC_SHA256");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256HmacUpdate */
- /*
- * testing wc_HmacUpdate on SHA384 hash.
- */
- static int test_wc_Sha384HmacUpdate(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
- Hmac hmac;
- testVector a, b;
- int ret;
- #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;
- printf(testingFmt, "wc_HmacUpdate() with SHA384");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384HmacUpdate */
- /*
- * Testing wc_HmacFinal() with MD5
- */
- static int test_wc_Md5HmacFinal(void)
- {
- int flag = 0;
- #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;
- 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;
- printf(testingFmt, "wc_HmacFinal() with MD5");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Md5HmacFinal */
- /*
- * Testing wc_HmacFinal() with SHA
- */
- static int test_wc_ShaHmacFinal(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && !defined(NO_SHA)
- Hmac hmac;
- byte hash[WC_SHA_DIGEST_SIZE];
- testVector a;
- int ret;
- 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;
- printf(testingFmt, "wc_HmacFinal() with SHA");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_ShaHmacFinal */
- /*
- * Testing wc_HmacFinal() with SHA224
- */
- static int test_wc_Sha224HmacFinal(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
- Hmac hmac;
- byte hash[WC_SHA224_DIGEST_SIZE];
- testVector a;
- int ret;
- 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;
- printf(testingFmt, "wc_HmacFinal() with SHA224");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha224HmacFinal */
- /*
- * Testing wc_HmacFinal() with SHA256
- */
- static int test_wc_Sha256HmacFinal(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && !defined(NO_SHA256)
- Hmac hmac;
- byte hash[WC_SHA256_DIGEST_SIZE];
- testVector a;
- int ret;
- 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;
- printf(testingFmt, "wc_HmacFinal() with WC_SHA256");
- ret = wc_HmacInit(&hmac, NULL, INVALID_DEVID);
- if (ret != 0)
- return ret;
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha256HmacFinal */
- /*
- * Testing wc_HmacFinal() with SHA384
- */
- static int test_wc_Sha384HmacFinal(void)
- {
- int flag = 0;
- #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
- Hmac hmac;
- byte hash[WC_SHA384_DIGEST_SIZE];
- testVector a;
- int ret;
- 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;
- printf(testingFmt, "wc_HmacFinal() with SHA384");
- 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);
- printf(resultFmt, flag == 0 ? passed : failed);
- #endif
- return flag;
- } /* END test_wc_Sha384HmacFinal */
- /*
- * Testing wc_InitCmac()
- */
- static int test_wc_InitCmac(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "wc_InitCmac()");
- #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;
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_InitCmac */
- /*
- * Testing wc_CmacUpdate()
- */
- static int test_wc_CmacUpdate(void)
- {
- int ret = 0;
- #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;
- ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
- if (ret != 0) {
- return ret;
- }
- printf(testingFmt, "wc_CmacUpdate()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_CmacUpdate */
- /*
- * Testing wc_CmacFinal()
- */
- static int test_wc_CmacFinal(void)
- {
- int ret = 0;
- #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;
- XMEMSET(mac, 0, macSz);
- ret = wc_InitCmac(&cmac, key, keySz, type, NULL);
- if (ret != 0) {
- return ret;
- }
- ret = wc_CmacUpdate(&cmac, msg, msgSz);
- printf(testingFmt, "wc_CmacFinal()");
- 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_CmacFinal */
- /*
- * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
- */
- static int test_wc_AesCmacGenerate(void)
- {
- int ret = 0;
- #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;
- 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);
- }
- printf(testingFmt, "wc_AesCmacGenerate()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret == 0) {
- printf(testingFmt, "wc_AesCmacVerify()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- }
- #endif
- return ret;
- } /* END test_wc_AesCmacGenerate */
- /*
- * Testing streaming AES-GCM API.
- */
- static int test_wc_AesGcmStream(void)
- {
- int ret = 0;
- #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
- defined(WOLFSSL_AESGCM_STREAM)
- 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);
- #endif
- return ret;
- } /* END test_wc_AesGcmStream */
- /*
- * unit test for wc_Des3_SetIV()
- */
- static int test_wc_Des3_SetIV(void)
- {
- int ret = 0;
- #ifndef NO_DES3
- Des3 des;
- 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
- };
- printf(testingFmt, "wc_Des3_SetIV()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Des3_SetIV */
- /*
- * unit test for wc_Des3_SetKey()
- */
- static int test_wc_Des3_SetKey(void)
- {
- int ret = 0;
- #ifndef NO_DES3
- Des3 des;
- 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
- };
- printf(testingFmt, "wc_Des3_SetKey()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Des3_SetKey */
- /*
- * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
- */
- static int test_wc_Des3_CbcEncryptDecrypt(void)
- {
- int ret = 0;
- #ifndef NO_DES3
- Des3 des;
- 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
- };
- printf(testingFmt, "wc_Des3_CbcEncrypt()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END wc_Des3_CbcEncrypt */
- /*
- * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
- */
- static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
- {
- int ret = 0;
- #ifndef NO_DES3
- 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;
- printf(testingFmt, "wc_Des3_CbcEncryptWithKey()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
- /*
- * Unit test for wc_Des3_EcbEncrypt
- */
- static int test_wc_Des3_EcbEncrypt(void)
- {
- int ret = 0;
- #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
- Des3 des;
- 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
- };
- printf(testingFmt, "wc_Des3_EcbEncrypt()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Des3_EcbEncrypt */
- /*
- * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
- */
- static int test_wc_Chacha_SetKey(void)
- {
- int ret = 0;
- #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];
- printf(testingFmt, "wc_Chacha_SetKey()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret != 0) {
- return ret;
- }
- printf(testingFmt, "wc_Chacha_SetIV");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Chacha_SetKey */
- /*
- * unit test for wc_Poly1305SetKey()
- */
- static int test_wc_Poly1305SetKey(void)
- {
- int ret = 0;
- #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
- };
- printf(testingFmt, "wc_Poly1305_SetKey()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Poly1305_SetKey() */
- /*
- * Testing wc_Chacha_Process()
- */
- static int test_wc_Chacha_Process(void)
- {
- int ret = 0;
- #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);
- /*Initialize stack varialbes.*/
- XMEMSET(cipher, 0, 128);
- XMEMSET(plain, 0, 128);
- printf(testingFmt, "wc_Chacha_Process()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Chacha_Process */
- /*
- * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
- */
- static int test_wc_ChaCha20Poly1305_aead(void)
- {
- int ret = 0;
- #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];
- /* Initialize stack variables. */
- XMEMSET(generatedCiphertext, 0, 272);
- XMEMSET(generatedPlaintext, 0, 272);
- /* Test Encrypt */
- printf(testingFmt, "wc_ChaCha20Poly1305_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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret != 0) {
- return ret;
- }
- printf(testingFmt, "wc_ChaCha20Poly1305_Decrypt()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test-wc_ChaCha20Poly1305_EncryptDecrypt */
- /*
- * Testing function for wc_Rc2SetKey().
- */
- static int test_wc_Rc2SetKey(void)
- {
- int ret = 0;
- #ifdef WC_RC2
- Rc2 rc2;
- byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
- byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
- printf(testingFmt, "wc_Rc2SetKey()");
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Rc2SetKey */
- /*
- * Testing function for wc_Rc2SetIV().
- */
- static int test_wc_Rc2SetIV(void)
- {
- int ret = 0;
- #ifdef WC_RC2
- Rc2 rc2;
- byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
- printf(testingFmt, "wc_Rc2SetIV()");
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Rc2SetKey */
- /*
- * Testing function for wc_Rc2EcbEncrypt().
- */
- static int test_wc_Rc2EcbEncryptDecrypt(void)
- {
- int ret = 0;
- #ifdef WC_RC2
- Rc2 rc2;
- 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 };
- printf(testingFmt, "wc_Rc2EcbEncryptDecrypt()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Rc2SetKey */
- /*
- * Testing function for wc_Rc2CbcEncrypt().
- */
- static int test_wc_Rc2CbcEncryptDecrypt(void)
- {
- int ret = 0;
- #ifdef WC_RC2
- Rc2 rc2;
- 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
- };
- printf(testingFmt, "wc_Rc2CbcEncryptDecrypt()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Rc2SetKey */
- /*
- * Testing function for wc_AesSetIV
- */
- static int test_wc_AesSetIV(void)
- {
- int ret = 0;
- #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
- Aes aes;
- byte key16[] =
- {
- 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
- 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
- };
- byte iv1[] = "1234567890abcdef";
- byte iv2[] = "0987654321fedcba";
- printf(testingFmt, "wc_AesSetIV()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* test_wc_AesSetIV */
- /*
- * Testing function for wc_AesSetKey().
- */
- static int test_wc_AesSetKey(void)
- {
- int ret = 0;
- #ifndef NO_AES
- Aes aes;
- 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";
- printf(testingFmt, "wc_AesSetKey()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_AesSetKey */
- /*
- * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
- * and wc_AesCbcDecryptWithKey()
- */
- static int test_wc_AesCbcEncryptDecrypt(void)
- {
- int ret = 0;
- #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
- defined(WOLFSSL_AES_256)
- 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 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;
- }
- }
- printf(testingFmt, "wc_AesCbcEncrypt()");
- /* 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)
- printf("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
- }
- printf(resultFmt, cbcE == 0 ? passed : failed);
- if (cbcE != 0) {
- wc_AesFree(&aes);
- return cbcE;
- }
- printf(testingFmt, "wc_AesCbcDecrypt()");
- 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];
- }
- }
- printf(resultFmt, cbcD == 0 ? passed : failed);
- if (cbcD != 0) {
- wc_AesFree(&aes);
- return cbcD;
- }
- printf(testingFmt, "wc_AesCbcDecryptWithKey()");
- 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);
- printf(resultFmt, cbcDWK == 0 ? passed : failed);
- if (cbcDWK != 0) {
- return cbcDWK;
- }
- #endif
- return ret;
- } /* END test_wc_AesCbcEncryptDecrypt */
- /*
- * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
- */
- static int test_wc_AesCtrEncryptDecrypt(void)
- {
- int ret = 0;
- #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
- Aes aesEnc, aesDec;
- 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);
- printf(testingFmt, "wc_AesCtrEncrypt()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_AesCtrEncryptDecrypt */
- /*
- * test function for wc_AesGcmSetKey()
- */
- static int test_wc_AesGcmSetKey(void)
- {
- int ret = 0;
- #if !defined(NO_AES) && defined(HAVE_AESGCM)
- Aes aes;
- #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
- };
- printf(testingFmt, "wc_AesGcmSetKey()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_AesGcmSetKey */
- /*
- * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
- */
- static int test_wc_AesGcmEncryptDecrypt(void)
- {
- int ret = 0;
- /* 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;
- /* 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;
- }
- }
- printf(testingFmt, "wc_AesGcmEncrypt()");
- /*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 */
- printf(resultFmt, gcmE == 0 ? passed : failed);
- if (gcmE != 0) {
- wc_AesFree(&aes);
- return gcmE;
- }
- #ifdef HAVE_AES_DECRYPT
- printf(testingFmt, "wc_AesGcmDecrypt()");
- 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;
- }
- } /* END wc_AesGcmDecrypt */
- printf(resultFmt, gcmD == 0 ? passed : failed);
- #endif /* HAVE_AES_DECRYPT */
- wc_AesFree(&aes);
- #endif
- return ret;
- } /* END test_wc_AesGcmEncryptDecrypt */
- /*
- * unit test for wc_GmacSetKey()
- */
- static int test_wc_GmacSetKey(void)
- {
- int ret = 0;
- #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
- };
- printf(testingFmt, "wc_GmacSetKey()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_GmacSetKey */
- /*
- * unit test for wc_GmacUpdate
- */
- static int test_wc_GmacUpdate(void)
- {
- int ret = 0;
- #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];
- /* Init stack variables. */
- XMEMSET(tagOut, 0, sizeof(tagOut));
- XMEMSET(tagOut2, 0, sizeof(tagOut2));
- XMEMSET(tagOut3, 0, sizeof(tagOut3));
- printf(testingFmt, "wc_GmacUpdate()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_GmacUpdate */
- /*
- * testing wc_CamelliaSetKey
- */
- static int test_wc_CamelliaSetKey(void)
- {
- int ret = 0;
- #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
- };
- printf(testingFmt, "wc_CamelliaSetKey()");
- 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. */
- #endif
- return ret;
- } /* END test_wc_CammeliaSetKey */
- /*
- * Testing wc_CamelliaSetIV()
- */
- static int test_wc_CamelliaSetIV(void)
- {
- int ret = 0;
- #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
- };
- printf(testingFmt, "wc_CamelliaSetIV()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /*END test_wc_CamelliaSetIV*/
- /*
- * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
- */
- static int test_wc_CamelliaEncryptDecryptDirect(void)
- {
- int ret = 0;
- #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;
- /*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;
- }
- }
- }
- printf(testingFmt, "wc_CamelliaEncryptDirect()");
- /* 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;
- }
- }
- printf(resultFmt, camE == 0 ? passed : failed);
- if (camE != 0) {
- return camE;
- }
- printf(testingFmt, "wc_CamelliaDecryptDirect()");
- 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;
- }
- }
- printf(resultFmt, camD == 0 ? passed : failed);
- if (camD != 0) {
- return camD;
- }
- #endif
- return ret;
- } /* END test-wc_CamelliaEncryptDecryptDirect */
- /*
- * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
- */
- static int test_wc_CamelliaCbcEncryptDecrypt(void)
- {
- int ret = 0;
- #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;
- /* 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;
- }
- }
- }
- printf(testingFmt, "wc_CamelliaCbcEncrypt");
- /* 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;
- }
- }
- printf(resultFmt, camCbcE == 0 ? passed : failed);
- if (camCbcE != 0) {
- return camCbcE;
- }
- printf(testingFmt, "wc_CamelliaCbcDecrypt()");
- 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. */
- printf(resultFmt, camCbcD == 0 ? passed : failed);
- if (camCbcD != 0) {
- return camCbcD;
- }
- #endif
- return ret;
- } /* END test_wc_CamelliaCbcEncryptDecrypt */
- /*
- * Testing wc_Arc4SetKey()
- */
- static int test_wc_Arc4SetKey(void)
- {
- int ret = 0;
- #ifndef NO_RC4
- Arc4 arc;
- const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
- int keyLen = 8;
- printf(testingFmt, "wc_Arch4SetKey()");
- 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. */
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Arc4SetKey */
- /*
- * Testing wc_Arc4Process for ENC/DEC.
- */
- static int test_wc_Arc4Process(void)
- {
- int ret = 0;
- #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];
- /* 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);
- }
- printf(testingFmt, "wc_Arc4Process()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_Arc4Free(&enc);
- wc_Arc4Free(&dec);
- #endif
- return ret;
- }/* END test_wc_Arc4Process */
- /*
- * Testing wc_Init RsaKey()
- */
- static int test_wc_InitRsaKey(void)
- {
- int ret = 0;
- #ifndef NO_RSA
- RsaKey key;
- printf(testingFmt, "wc_InitRsaKey()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_InitRsaKey */
- /*
- * Testing wc_RsaPrivateKeyDecode()
- */
- static int test_wc_RsaPrivateKeyDecode(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "wc_RsaPrivateKeyDecode()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaPrivateKeyDecode */
- /*
- * Testing wc_RsaPublicKeyDecode()
- */
- static int test_wc_RsaPublicKeyDecode(void)
- {
- int ret = 0;
- #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;
- #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
- printf(testingFmt, "wc_RsaPublicKeyDecode()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaPublicKeyDecode */
- /*
- * Testing wc_RsaPublicKeyDecodeRaw()
- */
- static int test_wc_RsaPublicKeyDecodeRaw(void)
- {
- int ret = 0;
- #if !defined(NO_RSA)
- RsaKey key;
- const byte n = 0x23;
- const byte e = 0x03;
- int nSz = sizeof(n);
- int eSz = sizeof(e);
- printf(testingFmt, "wc_RsaPublicKeyDecodeRaw()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* 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;
- printf("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 ret = 0;
- #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
- printf(testingFmt, "wc_MakeRsaKey()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* 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 ret = 0;
- #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)
- RsaKey key;
- byte flatC[256];
- word32 flatCSz;
- byte out[256];
- word32 outSz = sizeof(out);
- WC_RNG rng;
- printf(testingFmt, "RSA decrypt bounds check");
- 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;
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaDecryptBoundsCheck */
- /*
- * Testing wc_SetKeyUsage()
- */
- static int test_wc_SetKeyUsage(void)
- {
- int ret = 0;
- #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
- Cert myCert;
- ret = wc_InitCert(&myCert);
- printf(testingFmt, "wc_SetKeyUsage()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_SetKeyUsage */
- /*
- * Testing wc_CheckProbablePrime()
- */
- static int test_wc_CheckProbablePrime(void)
- {
- int ret = 0;
- #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* isPrime;
- int test[5];
- isPrime = test;
- printf(testingFmt, "wc_CheckProbablePrime()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #undef CHECK_PROBABLE_PRIME_KEY_BITS
- #endif
- return ret;
- } /* END test_wc_CheckProbablePrime */
- /*
- * Testing wc_RsaPSS_Verify()
- */
- static int test_wc_RsaPSS_Verify(void)
- {
- int ret = 0;
- #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);
- pt = pDecrypted;
- printf(testingFmt, "wc_RsaPSS_Verify()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaPSS_Verify */
- /*
- * Testing wc_RsaPSS_VerifyCheck()
- */
- static int test_wc_RsaPSS_VerifyCheck(void)
- {
- int ret = 0;
- #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);
- pt = pDecrypted;
- printf(testingFmt, "wc_RsaPSS_VerifyCheck()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaPSS_VerifyCheck */
- /*
- * Testing wc_RsaPSS_VerifyCheckInline()
- */
- static int test_wc_RsaPSS_VerifyCheckInline(void)
- {
- int ret = 0;
- #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];
- pt = pDecrypted;
- printf(testingFmt, "wc_RsaPSS_VerifyCheckInline()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* 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 ret = 0;
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- int flag = CRYPTO_LOCK;
- int type = 0;
- const char* file = "./test-LockMutex_ex.txt";
- int line = 0;
- printf(testingFmt, "wc_LockMutex_ex()");
- /*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);
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- }/*End test_wc_LockMutex_ex*/
- /*
- * Testing wc_SetMutexCb
- */
- static int test_wc_SetMutexCb(void)
- {
- int ret = 0;
- #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
- printf(testingFmt, "wc_SetMutexCb()");
- ret = wc_SetMutexCb(sample_mutex_cb);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- }/*End test_wc_SetMutexCb*/
- /*
- * Testing wc_RsaKeyToDer()
- */
- static int test_wc_RsaKeyToDer(void)
- {
- int ret = 0;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey genKey;
- WC_RNG rng;
- byte* der;
- #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;
- }
- }
- printf(testingFmt, "wc_RsaKeyToDer()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaKeyToDer */
- /*
- * Testing wc_RsaKeyToPublicDer()
- */
- static int test_wc_RsaKeyToPublicDer(void)
- {
- int ret = 0;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- byte* der;
- #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);
- }
- printf(testingFmt, "wc_RsaKeyToPublicDer()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaKeyToPublicDer */
- /*
- * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
- */
- static int test_wc_RsaPublicEncryptDecrypt(void)
- {
- int ret = 0;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- 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) {
- printf("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. */
- printf(testingFmt, "wc_RsaPublicEncrypt()");
- 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.*/
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret != 0) {
- return ret;
- }
- /* Decrypt */
- printf(testingFmt, "wc_RsaPrivateDecrypt()");
- #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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaPublicEncryptDecrypt */
- /*
- * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
- */
- static int test_wc_RsaPublicEncryptDecrypt_ex(void)
- {
- int ret = 0;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
- && !defined(WC_NO_RSA_OAEP) && !defined(HAVE_USER_RSA)\
- && !defined(NO_SHA)
- RsaKey key;
- WC_RNG rng;
- 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) {
- printf("test_wc_RsaPublicEncryptDecrypt_exmalloc failed\n");
- return MEMORY_E;
- }
- #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 */
- printf(testingFmt, "wc_RsaPublicEncrypt_ex()");
- if (ret == 0) {
- ret = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key, &rng,
- WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA, WC_MGF1SHA1, NULL, 0);
- if (ret >= 0) {
- idx = ret;
- ret = 0;
- } else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /*Pass bad args.*/
- /* Tests RsaPublicEncryptEx again. No need duplicate. */
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret != 0) {
- return ret;
- }
- #ifndef WOLFSSL_RSA_PUBLIC_ONLY
- /* Decrypt */
- printf(testingFmt, "wc_RsaPrivateDecrypt_ex()");
- #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_SHA,
- WC_MGF1SHA1, 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. */
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret != 0) {
- return ret;
- }
- printf(testingFmt, "wc_RsaPrivateDecryptInline_ex()");
- if (ret == 0) {
- ret = wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx,
- &res, &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA,
- WC_MGF1SHA1, 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaPublicEncryptDecrypt_ex */
- /*
- * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
- */
- static int test_wc_RsaSSL_SignVerify(void)
- {
- int ret = 0;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- 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) {
- printf("test_wc_RsaSSL_SignVerify 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);
- }
- /* Sign. */
- printf(testingFmt, "wc_RsaSSL_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
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret != 0) {
- return ret;
- }
- /* Verify. */
- printf(testingFmt, "wc_RsaSSL_Verify()");
- if (ret == 0) {
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaSSL_SignVerify */
- /*
- * Testing wc_RsaEncryptSize()
- */
- static int test_wc_RsaEncryptSize(void)
- {
- int ret = 0;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- ret = wc_InitRsaKey(&key, HEAP_HINT);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- printf(testingFmt, "wc_RsaEncryptSize()");
- #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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaEncryptSize*/
- /*
- * Testing wc_RsaFlattenPublicKey()
- */
- static int test_wc_RsaFlattenPublicKey(void)
- {
- int ret = 0;
- #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
- RsaKey key;
- WC_RNG rng;
- 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;
- }
- }
- printf(testingFmt, "wc_RsaFlattenPublicKey()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RsaFlattenPublicKey */
- /*
- * unit test for wc_AesCcmSetKey
- */
- static int test_wc_AesCcmSetKey(void)
- {
- int ret = 0;
- #ifdef HAVE_AESCCM
- Aes aes;
- 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
- };
- printf(testingFmt, "wc_AesCcmSetKey()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_AesCcmSetKey */
- /*
- * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
- */
- static int test_wc_AesCcmEncryptDecrypt(void)
- {
- int ret = 0;
- #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
- Aes aes;
- 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
- }
- printf(testingFmt, "wc_AesCcmEncrypt()");
- /* 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 */
- printf(resultFmt, ccmE == 0 ? passed : failed);
- if (ccmE != 0) {
- wc_AesFree(&aes);
- return ccmE;
- }
- #ifdef HAVE_AES_DECRYPT
- printf(testingFmt, "wc_AesCcmDecrypt()");
- /* 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 */
- printf(resultFmt, ccmD == 0 ? passed : failed);
- if (ccmD != 0) {
- return ccmD;
- }
- #endif
- wc_AesFree(&aes);
- #endif /* HAVE_AESCCM */
- return ret;
- } /* END test_wc_AesCcmEncryptDecrypt */
- /*
- * Testing wc_InitDsaKey()
- */
- static int test_wc_InitDsaKey(void)
- {
- int ret = 0;
- #ifndef NO_DSA
- DsaKey key;
- printf(testingFmt, "wc_InitDsaKey()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_FreeDsaKey(&key);
- #endif
- return ret;
- } /* END test_wc_InitDsaKey */
- /*
- * Testing wc_DsaSign() and wc_DsaVerify()
- */
- static int test_wc_DsaSignVerify(void)
- {
- int ret = 0;
- #if !defined(NO_DSA)
- DsaKey key;
- WC_RNG rng;
- wc_Sha sha;
- 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);
- }
- }
- printf(testingFmt, "wc_DsaSign()");
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret != 0) {
- return ret;
- }
- /* Verify. */
- printf(testingFmt, "wc_DsaVerify()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_FreeDsaKey(&key);
- wc_ShaFree(&sha);
- #endif
- return ret;
- } /* END test_wc_DsaSign */
- /*
- * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
- */
- static int test_wc_DsaPublicPrivateKeyDecode(void)
- {
- int ret = 0;
- #if !defined(NO_DSA)
- DsaKey key;
- word32 bytes;
- word32 idx = 0;
- int priv = WOLFSSL_FATAL_ERROR;
- int pub = WOLFSSL_FATAL_ERROR;
- #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);
- printf(testingFmt, "wc_DsaPrivateKeyDecode()");
- 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 = 0;
- } else {
- priv = WOLFSSL_FATAL_ERROR;
- }
- }
- wc_FreeDsaKey(&key);
- ret = wc_InitDsaKey(&key);
- }
- printf(resultFmt, priv == 0 ? passed : failed);
- printf(testingFmt, "wc_DsaPublicKeyDecode()");
- 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 = 0;
- } else {
- pub = WOLFSSL_FATAL_ERROR;
- }
- }
- } /* END Public Key */
- printf(resultFmt, pub == 0 ? passed : failed);
- wc_FreeDsaKey(&key);
- #endif
- return ret;
- } /* END test_wc_DsaPublicPrivateKeyDecode */
- /*
- * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
- */
- static int test_wc_MakeDsaKey(void)
- {
- int ret = 0;
- #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
- DsaKey genKey;
- WC_RNG rng;
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&genKey, 0, sizeof(genKey));
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_InitDsaKey(&genKey);
- }
- printf(testingFmt, "wc_MakeDsaParameters()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- printf(testingFmt, "wc_MakeDsaKey()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_FreeDsaKey(&genKey);
- #endif
- return ret;
- } /* END test_wc_MakeDsaKey */
- /*
- * Testing wc_DsaKeyToDer()
- */
- static int test_wc_DsaKeyToDer(void)
- {
- int ret = 0;
- #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
- DsaKey genKey;
- WC_RNG rng;
- word32 bytes;
- word32 idx = 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);
- }
- printf(testingFmt, "wc_DsaKeyToDer()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_FreeDsaKey(&genKey);
- #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
- return ret;
- } /* END test_wc_DsaKeyToDer */
- /*
- * Testing wc_DsaKeyToPublicDer()
- * (indirectly testing setDsaPublicKey())
- */
- static int test_wc_DsaKeyToPublicDer(void)
- {
- int ret = 0;
- #ifndef HAVE_SELFTEST
- #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
- DsaKey genKey;
- WC_RNG rng;
- byte* der;
- word32 sz;
- printf(testingFmt, "wc_DsaKeyToPublicDer()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- XFREE(der,NULL,DYNAMIC_TYPE_TMP_BUFFER);
- wc_FreeDsaKey(&genKey);
- #endif /* !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN) */
- #endif /* HAVE_SELFTEST */
- return ret;
- } /* END test_wc_DsaKeyToPublicDer */
- /*
- * Testing wc_DsaImportParamsRaw()
- */
- static int test_wc_DsaImportParamsRaw(void)
- {
- int ret = 0;
- #if !defined(NO_DSA)
- DsaKey key;
- /* [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";
- printf(testingFmt, "wc_DsaImportParamsRaw()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_FreeDsaKey(&key);
- #endif
- return ret;
- } /* END test_wc_DsaImportParamsRaw */
- /*
- * Testing wc_DsaImportParamsRawCheck()
- */
- static int test_wc_DsaImportParamsRawCheck(void)
- {
- int ret = 0;
- #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
- DsaKey key;
- 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";
- printf(testingFmt, "wc_DsaImportParamsRawCheck()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_FreeDsaKey(&key);
- #endif
- return ret;
- } /* END test_wc_DsaImportParamsRawCheck */
- /*
- * Testing wc_DsaExportParamsRaw()
- */
- static int test_wc_DsaExportParamsRaw(void)
- {
- int ret = 0;
- #if !defined(NO_DSA)
- DsaKey key;
- /* [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;
- printf(testingFmt, "wc_DsaExportParamsRaw()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_FreeDsaKey(&key);
- #endif
- return ret;
- } /* END test_wc_DsaExportParamsRaw */
- /*
- * Testing wc_DsaExportKeyRaw()
- */
- static int test_wc_DsaExportKeyRaw(void)
- {
- int ret = 0;
- #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
- DsaKey key;
- WC_RNG rng;
- byte xOut[MAX_DSA_PARAM_SIZE];
- byte yOut[MAX_DSA_PARAM_SIZE];
- word32 xOutSz, yOutSz;
- printf(testingFmt, "wc_DsaExportKeyRaw()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_FreeDsaKey(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /* END test_wc_DsaExportParamsRaw */
- /*
- * Testing wc_ed25519_make_key().
- */
- static int test_wc_ed25519_make_key(void)
- {
- int ret = 0;
- #if defined(HAVE_ED25519)
- ed25519_key key;
- WC_RNG rng;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed25519_init(&key);
- }
- printf(testingFmt, "wc_ed25519_make_key()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed25519_make_key */
- /*
- * Testing wc_ed25519_init()
- */
- static int test_wc_ed25519_init(void)
- {
- int ret = 0;
- #if defined(HAVE_ED25519)
- ed25519_key key;
- printf(testingFmt, "wc_ed25519_init()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_ed25519_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed25519_init */
- /*
- * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
- */
- static int test_wc_ed25519_sign_msg(void)
- {
- int ret = 0;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
- WC_RNG rng;
- ed25519_key key;
- 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);
- }
- }
- printf(testingFmt, "wc_ed25519_sign_msg()");
- 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 */
- printf(resultFmt, ret == 0 ? passed : failed);
- #ifdef HAVE_ED25519_VERIFY
- printf(testingFmt, "wc_ed25519_verify_msg()");
- 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. */
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif /* Verify. */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed25519_sign_msg */
- /*
- * Testing wc_ed25519_import_public()
- */
- static int test_wc_ed25519_import_public(void)
- {
- int ret = 0;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
- WC_RNG rng;
- ed25519_key pubKey;
- const byte in[] = "Ed25519PublicKeyUnitTest......\n";
- word32 inlen = sizeof(in);
- 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);
- }
- }
- printf(testingFmt, "wc_ed25519_import_public()");
- 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&pubKey);
- #endif
- return ret;
- } /* END wc_ed25519_import_public */
- /*
- * Testing wc_ed25519_import_private_key()
- */
- static int test_wc_ed25519_import_private_key(void)
- {
- int ret = 0;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
- WC_RNG rng;
- ed25519_key key;
- 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);
- printf(testingFmt, "wc_ed25519_import_private_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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- #endif
- return ret;
- } /* 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 ret = 0;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
- WC_RNG rng;
- ed25519_key key;
- 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);
- }
- printf(testingFmt, "wc_ed25519_export_public()");
- 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- printf(testingFmt, "wc_ed25519_export_private_only()");
- 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed25519_export */
- /*
- * Testing wc_ed25519_size()
- */
- static int test_wc_ed25519_size(void)
- {
- int ret = 0;
- #if defined(HAVE_ED25519)
- WC_RNG rng;
- ed25519_key key;
- 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;
- }
- printf(testingFmt, "wc_ed25519_size()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret == 0) {
- printf(testingFmt, "wc_ed25519_sig_size()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- } /* END wc_ed25519_sig_size() */
- if (ret == 0) {
- printf(testingFmt, "wc_ed25519_pub_size");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- } /* END wc_ed25519_pub_size */
- if (ret == 0) {
- printf(testingFmt, "wc_ed25519_priv_size");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- } /* END wc_ed25519_pub_size */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed25519_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed25519_size */
- /*
- * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
- */
- static int test_wc_ed25519_exportKey(void)
- {
- int ret = 0;
- #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
- WC_RNG rng;
- ed25519_key key;
- 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 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;
- }
- printf(testingFmt, "wc_ed25519_export_private()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (ret == 0) {
- printf(testingFmt, "wc_ed25519_export_key()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- } /* 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);
- #endif
- return ret;
- } /* END test_wc_ed25519_exportKey */
- /*
- * Testing wc_Ed25519PublicKeyToDer
- */
- static int test_wc_Ed25519PublicKeyToDer(void)
- {
- int ret = 0;
- #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];
- printf(testingFmt, "wc_Ed25519PublicKeyToDer()");
- /* 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 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;
- }
- tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1);
- if (tmp <= 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_FreeRng(&rng);
- wc_ed25519_free(&key);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END testing wc_Ed25519PublicKeyToDer */
- /*
- * Testing wc_curve25519_init and wc_curve25519_free.
- */
- static int test_wc_curve25519_init(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE25519)
- curve25519_key key;
- printf(testingFmt, "wc_curve25519_init()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- /* Test good args for wc_curve_25519_free */
- wc_curve25519_free(&key);
- wc_curve25519_free(NULL);
- #endif
- return ret;
- } /* END test_wc_curve25519_init and wc_curve_25519_free*/
- /*
- * Testing test_wc_curve25519_size.
- */
- static int test_wc_curve25519_size(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE25519)
- curve25519_key key;
- printf(testingFmt, "wc_curve25519_size()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_curve25519_free(&key);
- #endif
- return ret;
- } /* END test_wc_curve25519_size*/
- /*
- * Testing test_wc_curve25519_export_key_raw().
- */
- static int test_wc_curve25519_export_key_raw(void)
- {
- #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
- curve25519_key key;
- WC_RNG rng;
- byte privateKey[CURVE25519_KEYSIZE];
- byte publicKey[CURVE25519_KEYSIZE];
- word32 prvkSz;
- word32 pubkSz;
- byte prik[CURVE25519_KEYSIZE];
- byte pubk[CURVE25519_KEYSIZE];
- word32 prksz;
- word32 pbksz;
- printf(testingFmt, "wc_curve25519_export_key_raw()");
- if(0 != wc_InitRng(&rng)){
- printf(testingFmt, "failed due to wc_InitRng");
- fflush(stdout);
- return 1;
- }
- if(0 != wc_curve25519_init(&key)){
- printf(testingFmt, "failed due to wc_curve25519_init");
- fflush(stdout);
- wc_FreeRng(&rng);
- return 1;
- }
- if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
- printf(testingFmt, "failed due to wc_curve25519_make_key");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- /*
- bad-argument-test cases
- target function sould return BAD_FUNC_ARG
- */
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- NULL , privateKey, &prvkSz, publicKey, &pubkSz)){
- printf(testingFmt,"failed at bad-arg-case-1.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- &key , NULL, &prvkSz, publicKey, &pubkSz)){
- printf(testingFmt,"failed at bad-arg-case-2.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- &key , privateKey, NULL, publicKey, &pubkSz)){
- printf(testingFmt,"failed at bad-arg-case-3.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- /* prvkSz = CURVE25519_KEYSIZE; */
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- &key , privateKey, &prvkSz, NULL, &pubkSz)){
- printf(testingFmt,"failed at bad-arg-case-4.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw(
- &key , privateKey, &prvkSz, publicKey, NULL )){
- printf(testingFmt,"failed at bad-arg-case-5.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- /*
- cross-testing
- */
- prksz = CURVE25519_KEYSIZE;
- if( 0 != wc_curve25519_export_private_raw(&key, prik, &prksz)){
- printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- pbksz = CURVE25519_KEYSIZE;
- if(0 != wc_curve25519_export_public(&key, pubk, &pbksz)){
- printf(testingFmt,"failed due to wc_curve25519_export_public");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- /* pubkSz = CURVE25519_KEYSIZE; */
- if(0 != wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
- publicKey, &pubkSz)){
- printf(testingFmt,"failed due to wc_curve25519_export_key_raw");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- 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)){
- printf(resultFmt,passed);
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 0;
- }
- else{
- printf(testingFmt,"failed due to key-contents-inconsistency.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- }
- else{
- printf(testingFmt,"failed due to bad-key-size.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- #endif
- fflush(stdout);
- return 0;
- } /* 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)
- {
- #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
- curve25519_key key;
- WC_RNG rng;
- byte privateKey[CURVE25519_KEYSIZE];
- byte publicKey[CURVE25519_KEYSIZE];
- word32 prvkSz;
- word32 pubkSz;
- byte prik[CURVE25519_KEYSIZE];
- byte pubk[CURVE25519_KEYSIZE];
- word32 prksz;
- word32 pbksz;
- printf(testingFmt, "wc_curve25519_export_key_raw_ex()");
- if(0 != wc_InitRng(&rng)){
- printf(testingFmt, "failed due to wc_InitRng");
- fflush(stdout);
- return 1;
- }
- if(0 != wc_curve25519_init(&key)){
- printf(testingFmt, "failed due to wc_curve25519_init");
- fflush(stdout);
- wc_FreeRng(&rng);
- return 1;
- }
- if(0 != wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key)){
- printf(testingFmt, "failed due to wc_curve25519_make_key");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- /*
- bad-argument-test cases
- target function sould return BAD_FUNC_ARG
- */
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL , privateKey,
- &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-1.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key , NULL,
- &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-2.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key,privateKey,
- NULL,publicKey, &pubkSz,EC25519_LITTLE_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-3.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- /* prvkSz = CURVE25519_KEYSIZE; */
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-4.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-5.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- /* pubkSz = CURVE25519_KEYSIZE; */
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( NULL, privateKey,
- &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-6.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, NULL, &prvkSz,
- publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-7.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-8.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- /* prvkSz = CURVE25519_KEYSIZE; */
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-9.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if(BAD_FUNC_ARG != wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN)){
- printf(testingFmt,"failed at bad-arg-case-10.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- /* illegal value for endien */
- 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 )){
- printf(testingFmt,"failed at bad-arg-case-11.");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- /*
- cross-testing
- */
- prksz = CURVE25519_KEYSIZE;
- if(0 != wc_curve25519_export_private_raw( &key, prik, &prksz )){
- printf(testingFmt,"failed due to wc_curve25519_export_private_raw");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- pbksz = CURVE25519_KEYSIZE;
- if(0 != wc_curve25519_export_public( &key, pubk, &pbksz )){
- printf(testingFmt,"failed due to wc_curve25519_export_public");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- prvkSz = CURVE25519_KEYSIZE;
- /* pubkSz = CURVE25519_KEYSIZE; */
- if(0 != wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
- publicKey, &pubkSz, EC25519_BIG_ENDIAN)) {
- printf(testingFmt,"failed due to wc_curve25519_export_key_raw_ex");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- 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 )){
- if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN)){
- if( prvkSz == CURVE25519_KEYSIZE &&
- pubkSz == CURVE25519_KEYSIZE ){
- ; /* proceed to the next test */
- }
- else{
- printf(testingFmt,"failed due to key-size-inconsistency");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- }
- else{
- printf(testingFmt,
- "failed due to wc_curve25519_export_key_raw_ex");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- }
- else{
- printf(testingFmt,"failed due to key-contents-inconsistency");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- }
- else{
- printf(testingFmt,"failed due to bad-key-size");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- /*
- try once with another endian
- */
- prvkSz = CURVE25519_KEYSIZE;
- pubkSz = CURVE25519_KEYSIZE;
- if( 0 == wc_curve25519_export_key_raw_ex( &key, privateKey,
- &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN)){
- if( prvkSz == CURVE25519_KEYSIZE &&
- pubkSz == CURVE25519_KEYSIZE ){
- /* no more test*/
- printf(resultFmt, passed );
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 0;
- }
- else{
- printf(testingFmt,"failed due to key-size-inconsistency");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- }
- else{
- printf(testingFmt,
- "failed due to wc_curve25519_export_key_raw_ex(BIGENDIAN)");
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- return 1;
- }
- #else
- return 0;
- #endif
- } /* end of test_wc_curve25519_export_key_raw_ex */
- /*
- * Testing wc_curve25519_make_key
- */
- static int test_wc_curve25519_make_key(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE25519)
- WC_RNG rng;
- curve25519_key key;
- int keysize;
- printf(testingFmt, "wc_curve25519_make_key()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /*END test_wc_curve25519_make_key*/
- /*
- * Testing wc_curve25519_shared_secret_ex
- */
- static int test_wc_curve25519_shared_secret_ex(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "wc_curve25519_shared_secret_ex()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve25519_free(&private_key);
- wc_curve25519_free(&public_key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /*END test_wc_curve25519_shared_secret_ex*/
- /*
- * Testing wc_curve25519_make_pub
- */
- static int test_wc_curve25519_make_pub(void)
- {
- int ret = 0;
- #ifdef HAVE_CURVE25519
- WC_RNG rng;
- curve25519_key key;
- byte out[CURVE25519_KEYSIZE];
- printf(testingFmt, "wc_curve25519_make_pub()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /*END test_wc_curve25519_make_pub */
- /*
- * Testing test_wc_curve25519_export_public_ex
- */
- static int test_wc_curve25519_export_public_ex(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE25519)
- WC_RNG rng;
- curve25519_key key;
- byte out[CURVE25519_KEYSIZE];
- word32 outLen = sizeof(out);
- int endian = EC25519_BIG_ENDIAN;
- printf(testingFmt, "wc_curve25519_export_public_ex()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /*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 ret = 0;
- #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;
- printf(testingFmt, "wc_curve25519_import_private_raw_ex()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /*END test_wc_curve25519_import_private_raw_ex*/
- /*
- * Testing test_wc_curve25519_import_private
- */
- static int test_wc_curve25519_import_private(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE25519)
- curve25519_key key;
- WC_RNG rng;
- byte priv[CURVE25519_KEYSIZE];
- word32 privSz = sizeof(priv);
- printf(testingFmt, "wc_curve25519_import_private()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve25519_free(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /*END test_wc_curve25519_import*/
- /*
- * Testing test_wc_curve25519_export_private_raw_ex
- */
- static int test_wc_curve25519_export_private_raw_ex(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE25519)
- curve25519_key key;
- byte out[CURVE25519_KEYSIZE];
- word32 outLen = sizeof(out);
- int endian = EC25519_BIG_ENDIAN;
- printf(testingFmt, "wc_curve25519_export_private_raw_ex()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve25519_free(&key);
- #endif
- return ret;
- }/*END test_wc_curve25519_export_private_raw_ex*/
- /*
- * Testing wc_ed448_make_key().
- */
- static int test_wc_ed448_make_key(void)
- {
- int ret = 0;
- #if defined(HAVE_ED448)
- ed448_key key;
- WC_RNG rng;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- ret = wc_ed448_init(&key);
- }
- printf(testingFmt, "wc_ed448_make_key()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed448_make_key */
- /*
- * Testing wc_ed448_init()
- */
- static int test_wc_ed448_init(void)
- {
- int ret = 0;
- #if defined(HAVE_ED448)
- ed448_key key;
- printf(testingFmt, "wc_ed448_init()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_ed448_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed448_init */
- /*
- * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
- */
- static int test_wc_ed448_sign_msg(void)
- {
- int ret = 0;
- #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
- /* 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);
- }
- }
- printf(testingFmt, "wc_ed448_sign_msg()");
- 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 */
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #ifdef HAVE_ED448_VERIFY
- printf(testingFmt, "wc_ed448_verify_msg()");
- 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. */
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif /* Verify. */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed448_sign_msg */
- /*
- * Testing wc_ed448_import_public()
- */
- static int test_wc_ed448_import_public(void)
- {
- int ret = 0;
- #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
- WC_RNG rng;
- ed448_key pubKey;
- const byte in[] =
- "Ed448PublicKeyUnitTest.................................\n";
- word32 inlen = sizeof(in);
- 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);
- }
- }
- printf(testingFmt, "wc_ed448_import_public()");
- 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&pubKey);
- #endif
- return ret;
- } /* END wc_ed448_import_public */
- /*
- * Testing wc_ed448_import_private_key()
- */
- static int test_wc_ed448_import_private_key(void)
- {
- int ret = 0;
- #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
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return ret;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return ret;
- }
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- printf(testingFmt, "wc_ed448_import_private_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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- #endif
- return ret;
- } /* 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 ret = 0;
- #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);
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return ret;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return ret;
- }
- if (ret == 0) {
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- }
- printf(testingFmt, "wc_ed448_export_public()");
- 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- printf(testingFmt, "wc_ed448_export_private_only()");
- 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed448_export */
- /*
- * Testing wc_ed448_size()
- */
- static int test_wc_ed448_size(void)
- {
- int ret = 0;
- #if defined(HAVE_ED448)
- WC_RNG rng;
- ed448_key key;
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return ret;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return ret;
- }
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- wc_ed448_free(&key);
- return ret;
- }
- printf(testingFmt, "wc_ed448_size()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (ret == 0) {
- printf(testingFmt, "wc_ed448_sig_size()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- } /* END wc_ed448_sig_size() */
- if (ret == 0) {
- printf(testingFmt, "wc_ed448_pub_size");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- } /* END wc_ed448_pub_size */
- if (ret == 0) {
- printf(testingFmt, "wc_ed448_priv_size");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- } /* END wc_ed448_pub_size */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ed448_free(&key);
- #endif
- return ret;
- } /* END test_wc_ed448_size */
- /*
- * Testing wc_ed448_export_private() and wc_ed448_export_key()
- */
- static int test_wc_ed448_exportKey(void)
- {
- int ret = 0;
- #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);
- ret = wc_InitRng(&rng);
- if (ret != 0) {
- return ret;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return ret;
- }
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- wc_ed448_free(&key);
- return ret;
- }
- printf(testingFmt, "wc_ed448_export_private()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (ret == 0) {
- printf(testingFmt, "wc_ed448_export_key()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- } /* 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);
- #endif
- return ret;
- } /* END test_wc_ed448_exportKey */
- /*
- * Testing wc_Ed448PublicKeyToDer
- */
- static int test_wc_Ed448PublicKeyToDer(void)
- {
- int ret = 0;
- #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];
- printf(testingFmt, "wc_Ed448PublicKeyToDer()");
- /* 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 ret;
- }
- ret = wc_ed448_init(&key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- return ret;
- }
- ret = wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key);
- if (ret != 0) {
- wc_FreeRng(&rng);
- wc_ed448_free(&key);
- return ret;
- }
- tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1);
- if (tmp <= 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_FreeRng(&rng);
- wc_ed448_free(&key);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- } /* END testing wc_Ed448PublicKeyToDer */
- /*
- * Testing wc_curve448_init and wc_curve448_free.
- */
- static int test_wc_curve448_init(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE448)
- curve448_key key;
- printf(testingFmt, "wc_curve448_init()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- /* Test good args for wc_curve_448_free */
- wc_curve448_free(&key);
- wc_curve448_free(NULL);
- #endif
- return ret;
- } /* END test_wc_curve448_init and wc_curve_448_free*/
- /*
- * Testing wc_curve448_make_key
- */
- static int test_wc_curve448_make_key(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE448)
- WC_RNG rng;
- curve448_key key;
- int keysize;
- printf(testingFmt, "wc_curve448_make_key()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve448_free(&key);
- #endif
- return ret;
- } /*END test_wc_curve448_make_key*/
- /*
- * Testing test_wc_curve448_shared_secret_ex
- */
- static int test_wc_curve448_shared_secret_ex(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "wc_curve448_shared_secret_ex()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve448_free(&private_key);
- wc_curve448_free(&public_key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /*END test_wc_curve448_shared_secret_ex*/
- /*
- * Testing test_wc_curve448_export_public_ex
- */
- static int test_wc_curve448_export_public_ex(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE448)
- WC_RNG rng;
- curve448_key key;
- byte out[CURVE448_KEY_SIZE];
- word32 outLen = sizeof(out);
- int endian = EC448_BIG_ENDIAN;
- printf(testingFmt, "wc_curve448_export_public_ex()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve448_free(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /*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 ret = 0;
- #if defined(HAVE_CURVE448)
- curve448_key key;
- byte out[CURVE448_KEY_SIZE];
- word32 outLen = sizeof(out);
- int endian = EC448_BIG_ENDIAN;
- printf(testingFmt, "wc_curve448_export_private_raw_ex()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve448_free(&key);
- #endif
- return ret;
- }/*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 ret = 0;
- #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;
- printf(testingFmt, "wc_curve448_import_private_raw_ex()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve448_free(&key);
- #endif
- return ret;
- } /*END test_wc_curve448_import_private_raw_ex*/
- /*
- * Testing test_curve448_export_key_raw
- */
- static int test_wc_curve448_export_key_raw(void)
- {
- int ret = 0;
- #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);
- printf(testingFmt, "wc_curve448_export_key_raw()");
- 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);
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve448_free(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- }/*END test_wc_curve448_import_private_raw_ex*/
- /*
- * Testing test_wc_curve448_import_private
- */
- static int test_wc_curve448_import_private(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE448)
- curve448_key key;
- WC_RNG rng;
- byte priv[CURVE448_KEY_SIZE];
- word32 privSz = sizeof(priv);
- printf(testingFmt, "wc_curve448_import_private()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve448_free(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /*END test_wc_curve448_import*/
- /*
- * Testing test_wc_curve448_size.
- */
- static int test_wc_curve448_size(void)
- {
- int ret = 0;
- #if defined(HAVE_CURVE448)
- curve448_key key;
- printf(testingFmt, "wc_curve448_size()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_curve448_free(&key);
- #endif
- return ret;
- } /* END test_wc_curve448_size*/
- /*
- * Testing wc_ecc_make_key.
- */
- static int test_wc_ecc_make_key(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecc_key key;
- printf(testingFmt, "wc_ecc_make_key()");
- ret = wc_InitRng(&rng);
- if (ret != 0)
- return ret;
- 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
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- } /* END test_wc_ecc_make_key */
- /*
- * Testing wc_ecc_init()
- */
- static int test_wc_ecc_init(void)
- {
- int ret = 0;
- #ifdef HAVE_ECC
- ecc_key key;
- printf(testingFmt, "wc_ecc_init()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_ecc_free(&key);
- #endif
- return ret;
- } /* END test_wc_ecc_init */
- /*
- * Testing wc_ecc_check_key()
- */
- static int test_wc_ecc_check_key(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecc_key key;
- 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
- }
- }
- printf(testingFmt, "wc_ecc_check_key()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #endif
- return ret;
- } /* END test_wc_ecc_check_key */
- /*
- * Testing wc_ecc_get_generator()
- */
- static int test_wc_ecc_get_generator(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
- !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
- ecc_point* pt;
- printf(testingFmt, "wc_ecc_new_point()");
- pt = wc_ecc_new_point();
- if (!pt) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- printf(testingFmt, "wc_ecc_get_generator()");
- 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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_ecc_del_point(pt);
- #endif
- return ret;
- } /* END test_wc_ecc_get_generator */
- /*
- * Testing wc_ecc_size()
- */
- static int test_wc_ecc_size(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecc_key key;
- 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
- }
- }
- printf(testingFmt, "wc_ecc_size()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #endif
- return ret;
- } /* END test_wc_ecc_size */
- static int test_wc_ecc_params(void)
- {
- /* 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);
- #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
- return 0;
- }
- /*
- * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
- */
- static int test_wc_ecc_signVerify_hash(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecc_key key;
- 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
- }
- }
- printf(testingFmt, "wc_ecc_sign_hash()");
- 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;
- }
- }
- printf(resultFmt, signH == 0 ? passed : failed);
- #ifdef HAVE_ECC_VERIFY
- printf(testingFmt, "wc_ecc_verify_hash()");
- 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;
- }
- }
- printf(resultFmt, verifyH == 0 ? passed : failed);
- #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
- #endif
- return ret;
- } /* END test_wc_ecc_sign_hash */
- /*
- * Testing wc_ecc_shared_secret()
- */
- static int test_wc_ecc_shared_secret(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
- ecc_key key, pubKey;
- WC_RNG rng;
- 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
- printf(testingFmt, "wc_ecc_shared_secret()");
- 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- 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
- #endif
- PRIVATE_KEY_LOCK();
- return ret;
- } /* END tests_wc_ecc_shared_secret */
- /*
- * testint wc_ecc_export_x963()
- */
- static int test_wc_ecc_export_x963(void)
- {
- int ret = 0;
- #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);
- 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
- }
- }
- printf(testingFmt, "wc_ecc_export_x963()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #endif
- PRIVATE_KEY_LOCK();
- return ret;
- } /* 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 ret = 0;
- #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);
- #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
- }
- }
- printf(testingFmt, "wc_ecc_export_x963_ex()");
- #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
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #endif
- return ret;
- } /* END test_wc_ecc_export_x963_ex */
- /*
- * testing wc_ecc_import_x963()
- */
- static int test_wc_ecc_import_x963(void)
- {
- int ret = 0;
- #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);
- /* 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();
- }
- }
- printf(testingFmt, "wc_ecc_import_x963()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- 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
- #endif
- return ret;
- } /* END wc_ecc_import_x963 */
- /*
- * testing wc_ecc_import_private_key()
- */
- static int ecc_import_private_key (void)
- {
- int ret = 0;
- #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);
- /* 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);
- }
- }
- printf(testingFmt, "wc_ecc_import_private_key()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- 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
- #endif
- return ret;
- } /* END wc_ecc_import_private_key */
- /*
- * Testing wc_ecc_export_private_only()
- */
- static int test_wc_ecc_export_private_only(void)
- {
- int ret = 0;
- #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);
- /* 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
- }
- }
- printf(testingFmt, "wc_ecc_export_private_only()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #endif
- return ret;
- } /* END test_wc_ecc_export_private_only */
- /*
- * Testing wc_ecc_rs_to_sig()
- */
- static int test_wc_ecc_rs_to_sig(void)
- {
- int ret = 0;
- #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);
- /* Init stack variables. */
- XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
- XMEMSET(r, 0, keySz);
- XMEMSET(s, 0, keySz);
- printf(testingFmt, "wc_ecc_rs_to_sig()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- printf(testingFmt, "wc_ecc_sig_to_rs()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- } /* END test_wc_ecc_rs_to_sig */
- static int test_wc_ecc_import_raw(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && !defined(NO_ECC256)
- ecc_key key;
- 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);
- printf(testingFmt, "wc_ecc_import_raw()");
- 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);
- }
- 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);
- }
- #endif
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_ecc_free(&key);
- #endif
- return ret;
- } /* END test_wc_ecc_import_raw */
- static int test_wc_ecc_import_unsigned(void)
- {
- int ret = 0;
- #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;
- ret = wc_ecc_init(&key);
- printf(testingFmt, "wc_ecc_import_unsigned()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_ecc_free(&key);
- #endif
- return ret;
- } /* END test_wc_ecc_import_unsigned */
- /*
- * Testing wc_ecc_sig_size()
- */
- static int test_wc_ecc_sig_size(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- ecc_key key;
- WC_RNG rng;
- int keySz = KEY16;
- 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
- }
- }
- printf(testingFmt, "wc_ecc_sig_size()");
- if (ret == 0) {
- ret = wc_ecc_sig_size(&key);
- if (ret <= (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ)) {
- ret = 0;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #endif
- return ret;
- } /* END test_wc_ecc_sig_size */
- /*
- * Testing wc_ecc_ctx_new()
- */
- static int test_wc_ecc_ctx_new(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
- WC_RNG rng;
- ecEncCtx* cli = NULL;
- ecEncCtx* srv = NULL;
- ret = wc_InitRng(&rng);
- printf(testingFmt, "wc_ecc_ctx_new()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_ctx_free(cli);
- #endif
- return ret;
- } /* END test_wc_ecc_ctx_new */
- /*
- * Tesing wc_ecc_reset()
- */
- static int test_wc_ecc_ctx_reset(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
- ecEncCtx* ctx = NULL;
- WC_RNG rng;
- ret = wc_InitRng(&rng);
- if (ret == 0) {
- if ( (ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng)) == NULL ) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- printf(testingFmt, "wc_ecc_ctx_reset()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_ctx_free(ctx);
- #endif
- return ret;
- } /* 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 ret = 0;
- #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;
- 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;
- }
- }
- printf(testingFmt, "wc_ecc_ctx_get_own_salt()");
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- printf(testingFmt, "wc_ecc_ctx_set_peer_salt()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_ctx_free(cliCtx);
- wc_ecc_ctx_free(servCtx);
- #endif
- return ret;
- } /* END test_wc_ecc_ctx_set_peer_salt */
- /*
- * Testing wc_ecc_ctx_set_info()
- */
- static int test_wc_ecc_ctx_set_info(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
- ecEncCtx* ctx = NULL;
- WC_RNG rng;
- 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;
- }
- printf(testingFmt, "wc_ecc_ctx_set_info()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_ctx_free(ctx);
- #endif
- return ret;
- } /* END test_wc_ecc_ctx_set_info */
- /*
- * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
- */
- static int test_wc_ecc_encryptDecrypt(void)
- {
- int ret = 0;
- #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;
- 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
- printf(testingFmt, "wc_ecc_encrypt()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- printf(testingFmt, "wc_ecc_decrypt()");
- #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;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&tmpKey);
- wc_ecc_free(&cliKey);
- wc_ecc_free(&srvKey);
- #endif
- return ret;
- } /* END test_wc_ecc_encryptDecrypt */
- /*
- * Testing wc_ecc_del_point() and wc_ecc_new_point()
- */
- static int test_wc_ecc_del_point(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC)
- ecc_point* pt;
- printf(testingFmt, "wc_ecc_new_point()");
- pt = wc_ecc_new_point();
- if (!pt) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_ecc_del_point(pt);
- #endif
- return ret;
- } /* 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 ret = 0;
- #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;
- 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 */
- printf(testingFmt, "wc_ecc_export_point_der()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- /* Import */
- printf(testingFmt, "wc_ecc_import_point_der()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- /* Copy */
- printf(testingFmt, "wc_ecc_copy_point()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- printf(testingFmt, "wc_ecc_cmp_point()");
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- printf(testingFmt, "wc_ecc_point_is_at_infinity()");
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- #ifdef USE_ECC_B_PARAM
- printf(testingFmt, "wc_ecc_point_is_on_curve()");
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #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;
- }
- #endif
- return ret;
- } /* END test_wc_ecc_pointFns */
- /*
- * Testing wc_ecc_sahred_secret_ssh()
- */
- static int test_wc_ecc_shared_secret_ssh(void)
- {
- int ret = 0;
- #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 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
- }
- }
- printf(testingFmt, "ecc_shared_secret_ssh()");
- #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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- 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
- #endif
- return ret;
- } /* 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 ret = 0;
- #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;
- 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;
- }
- }
- }
- printf(testingFmt, "wc_ecc_sign_hash_ex()");
- /* 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;
- }
- }
- printf(resultFmt, sig == 0 ? passed : failed);
- printf(testingFmt, "wc_ecc_verify_hash_ex()");
- /* 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;
- }
- }
- printf(resultFmt, ver == 0 ? passed : failed);
- 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;
- }
- #endif
- return ret;
- } /* END test_wc_ecc_verify_hash_ex */
- /*
- * Testing wc_ecc_mulmod()
- */
- static int test_wc_ecc_mulmod(void)
- {
- int ret = 0;
- #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;
- 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);
- }
- }
- printf(testingFmt, "wc_ecc_mulmod()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_ecc_free(&key1);
- wc_ecc_free(&key2);
- wc_ecc_free(&key3);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
- return ret;
- } /* END test_wc_ecc_mulmod */
- /*
- * Testing wc_ecc_is_valid_idx()
- */
- static int test_wc_ecc_is_valid_idx(void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
- ecc_key key;
- WC_RNG rng;
- 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
- }
- }
- printf(testingFmt, "wc_ecc_is_valid_idx()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #endif
- return ret;
- } /* 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 ret = 0;
- #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);
- printf(testingFmt, "wc_ecc_get_curve_id_from_oid()");
- /* 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* 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 ret = 0;
- #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
- ecc_key key;
- WC_RNG rng;
- int sz = 0;
- printf(testingFmt, "wc_ecc_sig_size_calc()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_ecc_free(&key);
- wc_FreeRng(&rng);
- #endif
- return ret;
- } /* END test_wc_ecc_sig_size_calc */
- /*
- * Testing ToTraditional
- */
- static int test_ToTraditional(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "ToTraditional()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* End test_ToTraditional*/
- /*
- * Testing wc_EccPrivateKeyToDer
- */
- static int test_wc_EccPrivateKeyToDer(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "wc_EccPrivateKeyToDer()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* End test_wc_EccPrivateKeyToDer*/
- /*
- * Testing wc_DhPublicKeyDecode
- */
- static int test_wc_DhPublicKeyDecode(void)
- {
- int ret = 0;
- #ifndef NO_DH
- word32 inOutIdx;
- #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
- DhKey key;
- AssertIntEQ(wc_InitDhKey(&key), 0);
- printf(testingFmt, "wc_DhPublicKeyDecode()");
- 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);
- printf(resultFmt, passed);
- #endif
- (void)inOutIdx;
- #endif /* !NO_DH */
- return ret;
- }
- /*
- * Testing wc_Ed25519KeyToDer
- */
- static int test_wc_Ed25519KeyToDer(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "wc_Ed25519KeyToDer()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* End test_wc_Ed25519KeyToDer*/
- /*
- * Testing wc_Ed25519PrivateKeyToDer
- */
- static int test_wc_Ed25519PrivateKeyToDer(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "wc_Ed25519PrivateKeyToDer()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* End test_wc_Ed25519PrivateKeyToDer*/
- /*
- * Testing wc_Ed448KeyToDer
- */
- static int test_wc_Ed448KeyToDer(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "wc_Ed448KeyToDer()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* End test_wc_Ed448KeyToDer*/
- /*
- * Testing wc_Ed448PrivateKeyToDer
- */
- static int test_wc_Ed448PrivateKeyToDer(void)
- {
- int ret = 0;
- #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;
- printf(testingFmt, "wc_Ed448PrivateKeyToDer()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* End test_wc_Ed448PrivateKeyToDer*/
- /*
- * Testing wc_SetSubjectBuffer
- */
- static int test_wc_SetSubjectBuffer(void)
- {
- int ret = 0;
- #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
- Cert cert;
- FILE* file;
- byte* der;
- word32 derSz;
- printf(testingFmt, "wc_SetSubjectBuffer()");
- 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);
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* End test_wc_SetSubjectBuffer*/
- /*
- * Testing wc_SetSubjectKeyIdFromPublicKey_ex
- */
- static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
- {
- int ret = 0;
- #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
- printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
- #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
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_FreeRng(&rng);
- #endif
- return ret;
- }/* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
- /*
- * Testing wc_SetAuthKeyIdFromPublicKey_ex
- */
- static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
- {
- int ret = 0;
- #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
- printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
- #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
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- wc_FreeRng(&rng);
- #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
- return ret;
- }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
- /*
- * Testing wc_PKCS7_New()
- */
- static int test_wc_PKCS7_New (void)
- {
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- void* heap = NULL;
- printf(testingFmt, "wc_PKCS7_New()");
- pkcs7 = wc_PKCS7_New(heap, testDevId);
- AssertNotNull(pkcs7);
- printf(resultFmt, passed);
- wc_PKCS7_Free(pkcs7);
- #endif
- return 0;
- } /* END test-wc_PKCS7_New */
- /*
- * Testing wc_PKCS7_Init()
- */
- static int test_wc_PKCS7_Init (void)
- {
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- void* heap = NULL;
- printf(testingFmt, "wc_PKCS7_Init()");
- 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);
- printf(resultFmt, passed);
- wc_PKCS7_Free(pkcs7);
- #endif
- return 0;
- } /* END test-wc_PKCS7_Init */
- /*
- * Testing wc_PKCS7_InitWithCert()
- */
- static int test_wc_PKCS7_InitWithCert (void)
- {
- #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
- printf(testingFmt, "wc_PKCS7_InitWithCert()");
- 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
- printf(resultFmt, passed);
- wc_PKCS7_Free(pkcs7);
- #endif
- return 0;
- } /* END test_wc_PKCS7_InitWithCert */
- /*
- * Testing wc_PKCS7_EncodeData()
- */
- static int test_wc_PKCS7_EncodeData (void)
- {
- #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);
- printf(testingFmt, "wc_PKCS7_EncodeData()");
- 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);
- printf(resultFmt, passed);
- wc_PKCS7_Free(pkcs7);
- #endif
- return 0;
- } /* 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)
- {
- #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);
- printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
- pkcs7->content = data;
- pkcs7->contentSz = (word32)sizeof(data);
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = (word32)sizeof(key);
- pkcs7->encryptOID = RSAk;
- pkcs7->hashOID = SHAh;
- 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
- printf(resultFmt, passed);
- wc_PKCS7_Free(pkcs7);
- wc_FreeRng(&rng);
- #endif
- return 0;
- } /* END test_wc_PKCS7_EncodeSignedData */
- /*
- * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
- */
- static int test_wc_PKCS7_EncodeSignedData_ex(void)
- {
- #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;
- enum wc_HashType hashType = WC_HASH_TYPE_SHA;
- 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);
- printf(testingFmt, "wc_PKCS7_EncodeSignedData()");
- pkcs7->content = NULL; /* not used for ex */
- pkcs7->contentSz = (word32)sizeof(data);
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = (word32)sizeof(key);
- pkcs7->encryptOID = RSAk;
- pkcs7->hashOID = SHAh;
- 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
- printf(resultFmt, passed);
- wc_PKCS7_Free(pkcs7);
- wc_FreeRng(&rng);
- #endif
- return 0;
- } /* END test_wc_PKCS7_EncodeSignedData_ex */
- #if defined(HAVE_PKCS7)
- static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
- byte* data, word32 dataSz,
- int withAttribs, int detachedSig)
- {
- PKCS7* pkcs7;
- WC_RNG rng;
- 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) }
- };
- #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];
- FILE* fp;
- int certSz;
- int keySz;
- fp = fopen("./certs/1024/client-cert.der", "rb");
- AssertNotNull(fp);
- certSz = fread(cert, 1, sizeof_client_cert_der_1024, fp);
- fclose(fp);
- fp = fopen("./certs/1024/client-key.der", "rb");
- AssertNotNull(fp);
- keySz = fread(key, 1, sizeof_client_key_der_1024, fp);
- fclose(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];
- FILE* fp;
- int certSz, keySz;
- fp = fopen("./certs/client-ecc-cert.der", "rb");
- AssertNotNull(fp);
- certSz = fread(cert, 1, sizeof_cliecc_cert_der_256, fp);
- fclose(fp);
- fp = fopen("./certs/client-ecc-key.der", "rb");
- AssertNotNull(fp);
- keySz = fread(key, 1, sizeof_ecc_clikey_der_256, fp);
- fclose(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);
- printf(testingFmt, "wc_PKCS7_VerifySignedData()");
- pkcs7->content = data;
- pkcs7->contentSz = dataSz;
- pkcs7->privateKey = key;
- pkcs7->privateKeySz = (word32)sizeof(key);
- pkcs7->encryptOID = RSAk;
- pkcs7->hashOID = SHAh;
- 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);
- }
- 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);
- 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)
- {
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- byte output[FOURK_BUF];
- 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;
- enum wc_HashType hashType = WC_HASH_TYPE_SHA;
- byte hashBuf[WC_MAX_DIGEST_SIZE];
- word32 hashSz = wc_HashGetDigestSize(hashType);
- AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
- (word32)sizeof(data),
- 0, 0)), 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);
- /* Test bad args. */
- 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);
- /* Invalid content should error, use detached signature so we can
- * easily change content */
- AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
- (word32)sizeof(data),
- 1, 1)), 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- } /* 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)
- {
- #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)
- /* Allocate buffer space. */
- AssertNotNull(rsaCert =
- (byte*)XMALLOC(ONEK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- /* Init buffer. */
- rsaCertSz = (word32)sizeof_client_cert_der_1024;
- XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
- AssertNotNull(rsaPrivKey = (byte*)XMALLOC(ONEK_BUF, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
- XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
- #elif defined(USE_CERT_BUFFERS_2048)
- /* Allocate buffer */
- AssertNotNull(rsaCert =
- (byte*)XMALLOC(TWOK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- /* Init buffer. */
- rsaCertSz = (word32)sizeof_client_cert_der_2048;
- XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
- AssertNotNull(rsaPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
- DYNAMIC_TYPE_TMP_BUFFER));
- rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
- 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 */
- /* Silence. */
- (void)keyFile;
- (void)certFile;
- {
- 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
- printf(testingFmt, "wc_PKCS7_EncodeEnvelopedData()");
- 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);
- printf(resultFmt, passed);
- /* Decode. */
- printf(testingFmt, "wc_PKCS7_DecodeEnvelopedData()");
- 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 */
- printf(resultFmt, passed);
- #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)
- {
- 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 */
- #endif /* HAVE_PKCS7 */
- return 0;
- } /* END test_wc_PKCS7_EncodeEnvelopedData() */
- /*
- * Testing wc_PKCS7_EncodeEncryptedData()
- */
- static int test_wc_PKCS7_EncodeEncryptedData (void)
- {
- #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);
- }
- printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
- 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;
- printf(resultFmt, passed);
- printf(testingFmt, "wc_PKCS7_EncodeEncryptedData()");
- 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);
- printf(resultFmt, passed);
- wc_PKCS7_Free(pkcs7);
- #endif
- return 0;
- } /* END test_wc_PKCS7_EncodeEncryptedData() */
- /*
- * Testing wc_PKCS7_Degenerate()
- */
- static int test_wc_PKCS7_Degenerate(void)
- {
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
- PKCS7* pkcs7;
- char fName[] = "./certs/test-degenerate.p7b";
- XFILE f;
- byte der[4096];
- word32 derSz;
- int ret;
- printf(testingFmt, "wc_PKCS7_Degenerate()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- } /* END test_wc_PKCS7_Degenerate() */
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
- defined(ASN_BER_TO_DER) && !defined(NO_DES3)
- 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 */
- /*
- * Testing wc_PKCS7_BER()
- */
- static int test_wc_PKCS7_BER(void)
- {
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
- 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;
- printf(testingFmt, "wc_PKCS7_BER()");
- 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 */
- printf(resultFmt, passed);
- #endif
- return 0;
- } /* END test_wc_PKCS7_BER() */
- static int test_PKCS7_signed_enveloped(void)
- {
- #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
- printf(testingFmt, "PKCS7_signed_enveloped");
- /* 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
- printf(resultFmt, passed);
- #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
- return 0;
- }
- static int test_wc_PKCS7_NoDefaultSignedAttribs (void)
- {
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
- && !defined(NO_AES)
- PKCS7* pkcs7;
- void* heap = NULL;
- printf(testingFmt, "wc_PKCS7_NoDefaultSignedAttribs()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_PKCS7_SetOriEncryptCtx (void)
- {
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
- && !defined(NO_AES)
- PKCS7* pkcs7;
- void* heap = NULL;
- WOLFSSL_CTX* ctx;
- ctx = NULL;
- printf(testingFmt, "wc_PKCS7_SetOriEncryptCtx()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_PKCS7_SetOriDecryptCtx (void)
- {
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
- && !defined(NO_AES)
- PKCS7* pkcs7;
- void* heap = NULL;
- WOLFSSL_CTX* ctx;
- ctx = NULL;
- printf(testingFmt, "wc_PKCS7_SetOriDecryptCtx()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_PKCS7_DecodeCompressedData(void)
- {
- #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;
- printf(testingFmt, "wc_PKCS7_DecodeCompressedData()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_i2d_PKCS12(void)
- {
- #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;
- printf(testingFmt, "wc_i2d_PKCS12");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /* Testing wc_SignatureGetSize() for signature type ECC */
- static int test_wc_SignatureGetSize_ecc(void)
- {
- int ret = 0;
- #ifndef NO_SIG_WRAPPER
- #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");
- }
- printf(testingFmt, "wc_SigntureGetSize_ecc()");
- 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
- if (ret == SIG_TYPE_E) {
- ret = 0;
- }
- else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif /* NO_SIG_WRAPPER */
- return ret;
- }/* END test_wc_SignatureGetSize_ecc() */
- /* Testing wc_SignatureGetSize() for signature type rsa */
- static int test_wc_SignatureGetSize_rsa(void)
- {
- int ret = 0;
- #ifndef NO_SIG_WRAPPER
- #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);
- }
- printf(testingFmt, "wc_SigntureGetSize_rsa()");
- 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
- if (ret == SIG_TYPE_E) {
- ret = 0;
- }else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif /* NO_SIG_WRAPPER */
- return ret;
- }/* 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 */
- printf(testingFmt, "wc_HashInit()");
- if (ret==0) { /* all tests have passed */
- printf(resultFmt, passed);
- }
- else { /* a test has failed */
- printf(resultFmt, failed);
- }
- return ret;
- } /* end of test_wc_HashInit */
- /*
- * Unit test function for wc_HashSetFlags()
- */
- static int test_wc_HashSetFlags(void)
- {
- int ret = 0;
- #ifdef WOLFSSL_HASH_FLAGS
- wc_HashAlg hash;
- 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));
- printf(testingFmt, "wc_HashSetFlags()");
- /* 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- } /* END test_wc_HashSetFlags */
- /*
- * Unit test function for wc_HashGetFlags()
- */
- static int test_wc_HashGetFlags(void)
- {
- int ret = 0;
- #ifdef WOLFSSL_HASH_FLAGS
- wc_HashAlg hash;
- 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;
- printf(testingFmt, "wc_HashGetFlags()");
- /* 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;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- } /* END test_wc_HashGetFlags */
- /*----------------------------------------------------------------------------*
- | Compatibility Tests
- *----------------------------------------------------------------------------*/
- static int test_wolfSSL_lhash(void)
- {
- #ifdef OPENSSL_ALL
- const char testStr[] = "Like a true nature's child\n"
- "We were born\n"
- "Born to be wild";
- printf(testingFmt, "wolfSSL_LH_strhash()");
- AssertIntEQ(lh_strhash(testStr), 0x5b7541dc);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_NAME(void)
- {
- #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
- printf(testingFmt, "wolfSSL_X509_NAME()");
- #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) {
- printf("\nERROR - %s line %d failed with:", __FILE__, __LINE__);
- printf(" 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
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
- return 0;
- }
- static int test_wolfSSL_X509_NAME_hash(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) \
- && !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
- BIO* bio;
- X509* x509 = NULL;
- printf(testingFmt, "wolfSSL_X509_NAME_hash");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_NAME_print_ex(void)
- {
- #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\\,\\+\\\"\\\\\\ ";
- printf(testingFmt, "wolfSSL_X509_NAME_print_ex");
- /* 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)+1);
- 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)+1);
- 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)+1);
- 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)+1);
- 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)+1);
- 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)+1);
- AssertIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
- XSTRLEN(expNotEscapedRev)), 0);
- BIO_free(membio);
- X509_NAME_free(name);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_X509_INFO_multiple_info(void)
- {
- #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);
- #endif
- return 0;
- }
- #endif
- #ifndef NO_BIO
- static int test_wolfSSL_X509_INFO(void)
- {
- #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
- };
- printf(testingFmt, "wolfSSL_X509_INFO");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #endif
- static int test_wolfSSL_X509_subject_name_hash(void)
- {
- #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 ret = 0;
- printf(testingFmt, "wolfSSL_X509_subject_name_hash()");
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM));
- AssertNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
- ret = X509_subject_name_hash(x509);
- AssertIntNE(ret, 0);
- X509_free(x509);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_issuer_name_hash(void)
- {
- #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 ret = 0;
- printf(testingFmt, "wolfSSL_X509_issuer_name_hash()");
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
- SSL_FILETYPE_PEM));
- AssertNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
- ret = X509_issuer_name_hash(x509);
- AssertIntNE(ret, 0);
- X509_free(x509);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_check_host(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
- && !defined(NO_SHA) && !defined(NO_RSA)
- X509* x509;
- const char altName[] = "example.com";
- printf(testingFmt, "wolfSSL_X509_check_host()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_check_email(void)
- {
- #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";
- printf(testingFmt, "wolfSSL_X509_check_email()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
- return 0;
- }
- static int test_wolfSSL_DES(void)
- {
- #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";
- printf(testingFmt, "wolfSSL_DES()");
- 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);
- printf("%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);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
- return 0;
- }
- static int test_wc_PemToDer(void)
- {
- #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
- 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;
- printf(testingFmt, "wc_PemToDer()");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_AllocDer(void)
- {
- #if !defined(NO_CERTS)
- int ret;
- DerBuffer* pDer = NULL;
- word32 testSize = 1024;
- printf(testingFmt, "wc_AllocDer()");
- ret = wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT);
- AssertIntEQ(ret, 0);
- AssertNotNull(pDer);
- wc_FreeDer(&pDer);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_CertPemToDer(void)
- {
- #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER)
- 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;
- printf(testingFmt, "wc_CertPemToDer()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_PubKeyPemToDer(void)
- {
- #ifdef WOLFSSL_PEM_TO_DER
- #if 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;
- printf(testingFmt, "wc_PubKeyPemToDer()");
- 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);
- printf(resultFmt, passed);
- #endif
- #endif
- return 0;
- }
- static int test_wc_PemPubKeyToDer(void)
- {
- #if 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);
- printf(testingFmt, "wc_PemPubKeyToDer()");
- 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_GetPubKeyDerFromCert(void)
- {
- #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)
- 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)
- 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
- printf(testingFmt, "wc_GetPubKeyDerFromCert()");
- #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 */
- #ifdef WOLFSSL_CERT_REQ
- {
- 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
- printf(resultFmt, passed);
- #endif /* !NO_RSA || HAVE_ECC */
- return 0;
- }
- static int test_wc_CheckCertSigPubKey(void)
- {
- #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;
- printf(testingFmt, "wc_CheckCertSigPubKey()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_certs(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_certs()");
- #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);
- /* 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !NO_CERTS */
- return 0;
- }
- static int test_wolfSSL_X509_check_private_key(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_check_private_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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_TIME_print(void)
- {
- #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];
- printf(testingFmt, "wolfSSL_ASN1_TIME_print()");
- 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, "Feb 15 12:50:24 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_UTCTIME_print(void)
- {
- #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 */
- printf(testingFmt, "ASN1_UTCTIME_print()");
- /* 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
- return 0;
- }
- static int test_wolfSSL_ASN1_TIME_diff_compare(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
- ASN1_TIME* fromTime;
- ASN1_TIME* toTime;
- int daysDiff;
- int secsDiff;
- printf(testingFmt, "test_wolfSSL_ASN1_TIME_diff");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
- {
- #if defined(OPENSSL_EXTRA)
- WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
- unsigned char nullstr[32];
- printf(testingFmt, "test_wolfSSL_ASN1_GENERALIZEDTIME_free");
- 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);
- }
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_private_keys(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_private_keys()");
- 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;
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- return 0;
- }
- static int test_wolfSSL_PEM_read_PrivateKey(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
- && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
- 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);
- printf(testingFmt, "test_wolfSSL_PEM_read_PrivateKey()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_PEM_read_PUBKEY(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
- && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
- XFILE file;
- const char* fname = "./certs/client-keyPub.pem";
- EVP_PKEY* pkey;
- printf(testingFmt, "test_wolfSSL_PEM_read_PUBKEY()");
- /* 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);
- #endif
- return 0;
- }
- static int test_wolfSSL_PEM_PrivateKey(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_PEM_PrivateKey()");
- 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(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(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
- printf(resultFmt, passed);
- #ifndef NO_BIO
- (void)bio;
- #endif
- (void)pkey;
- (void)server_key;
- #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
- return 0;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_bio_RSAKey(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_PEM_bio_RSAKey");
- /* 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);
- /* Same test as above, but with a file pointer rather than a BIO. */
- AssertIntEQ(PEM_write_RSAPublicKey(NULL, rsa), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_RSAPublicKey(stdout, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_RSAPublicKey(stdout, rsa), WOLFSSL_SUCCESS);
- 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 */
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
- (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
- return 0;
- }
- static int test_wolfSSL_PEM_RSAPrivateKey(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- RSA* rsa = NULL;
- RSA* rsa_dup = NULL;
- BIO* bio = NULL;
- XFILE f = NULL;
- printf(testingFmt, "wolfSSL_PEM_RSAPrivateKey()");
- 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);
- f = XFOPEN(svrKeyFile, "r");
- AssertTrue((f != XBADFILE));
- AssertNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
- AssertIntEQ(RSA_size(rsa), 256);
- RSA_free(rsa);
- XFCLOSE(f);
- #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 */
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- return 0;
- }
- static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- XFILE file;
- const char* fname = "./certs/client-keyPub.pem";
- RSA *rsa;
- 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);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- return 0;
- }
- static int test_wolfSSL_PEM_bio_DSAKey(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_PEM_bio_DSAKey");
- /* 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 */
- printf(resultFmt, passed);
- #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
- !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
- !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
- #endif /* HAVE_SELFTEST */
- return 0;
- }
- static int test_wolfSSL_PEM_bio_ECKey(void)
- {
- #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
- defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
- EC_KEY* ec = NULL;
- BIO* bio = NULL;
- printf(testingFmt, "wolfSSL_PEM_bio_ECKey");
- /* 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(stdout, NULL), WOLFSSL_FAILURE);
- AssertIntEQ(PEM_write_EC_PUBKEY(stdout, 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 */
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- return 0;
- }
- static int test_wolfSSL_PEM_PUBKEY(void)
- {
- #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;
- #endif
- return 0;
- }
- #endif /* !NO_BIO */
- static int test_DSA_do_sign_verify(void)
- {
- #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 */
- printf(testingFmt, "DSA_do_sign_verify()");
- 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);
- #endif
- #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
- return 0;
- }
- static int test_wolfSSL_tmp_dh(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_tmp_dh()");
- #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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return 0;
- }
- static int test_wolfSSL_ctrl(void)
- {
- #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
- byte buff[6000];
- BIO* bio;
- int bytes;
- BUF_MEM* ptr = NULL;
- printf(testingFmt, "wolfSSL_crtl()");
- 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);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EVP_PKEY_new_mac_key()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_EVP_Digest(void)
- {
- #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";
- printf(testingFmt, "wolfSSL_EVP_Digest()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPEN_EXTRA && ! NO_SHA256 */
- return 0;
- }
- static int test_wolfSSL_EVP_Digest_all(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EVP_Digest_all");
- 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_MD_size(void)
- {
- #ifdef OPENSSL_EXTRA
- WOLFSSL_EVP_MD_CTX mdCtx;
- printf(testingFmt, "wolfSSL_EVP_MD_size()");
- #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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_EVP_MD_pkey_type(void)
- {
- #ifdef OPENSSL_EXTRA
- const WOLFSSL_EVP_MD* md;
- printf(testingFmt, "test_wolfSSL_EVP_MD_pkey_type()");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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;
- printf(testingFmt, "wolfSSL_EVP_MD_hmac_signing()");
- 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)
- {
- #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
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_EVP_MD_rsa_signing(void)
- {
- #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
- };
- printf(testingFmt, "wolfSSL_EVP_MD_rsa_signing()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_MD_ecc_signing(void)
- {
- #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];
- printf(testingFmt, "wolfSSL_EVP_MD_ecc_signing()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_add_extra_chain_cert(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_CTX_add_extra_chain_cert()");
- #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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
- return 0;
- }
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- static int test_wolfSSL_ERR_peek_last_error_line(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_ERR_peek_last_error_line()");
- /* 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
- printf(resultFmt, passed);
- printf("\nTesting error print out\n");
- ERR_print_errors_fp(stdout);
- printf("Done testing print out\n\n");
- fflush(stdout);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
- return 0;
- }
- #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;
- printf("ENTER verify_cb\n");
- return SSL_SUCCESS;
- }
- #endif
- static int test_wolfSSL_X509_Name_canon(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_X509_Name_canon()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_hash_dir()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
- {
- #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
- printf(testingFmt, "test_wolfSSL_X509_LOOKUP_ctrl_file()");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
- {
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup()");
- X509_STORE_CTX_cleanup(NULL);
- X509_STORE_CTX_trusted_stack(NULL, NULL);
- AssertTrue(1); /* to confirm previous call gives no harm */
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_current_issuer()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_PKCS7_certs(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_PKCS7_certs()");
- /* 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;
- p7->hashOID = SHAh;
- 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);
- }
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
- return 0;
- }
- static int test_wolfSSL_X509_STORE_CTX(void)
- {
- #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
- printf(testingFmt, "wolfSSL_X509_STORE_CTX()");
- 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();
- 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()));
- 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);
- }
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return 0;
- }
- static int test_wolfSSL_X509_STORE_set_flags(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- X509_STORE* store;
- X509* x509;
- printf(testingFmt, "wolfSSL_X509_STORE_set_flags()");
- 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);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return 0;
- }
- static int test_wolfSSL_X509_LOOKUP_load_file(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_LOOKUP_load_file()");
- 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);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return 0;
- }
- static int test_wolfSSL_X509_STORE_CTX_set_time(void)
- {
- #if defined(OPENSSL_EXTRA)
- WOLFSSL_X509_STORE_CTX* ctx;
- time_t c_time;
- printf(testingFmt, "wolfSSL_X509_set_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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_CTX_get0_set1_param(void)
- {
- #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";
- printf(testingFmt, "wolfSSL_CTX_get0_set1_param()");
- #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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
- return 0;
- }
- static int test_wolfSSL_get0_param(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_get0_param()");
- #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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
- return 0;
- }
- static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
- {
- #if defined(OPENSSL_EXTRA)
- const char host[] = "www.example.com";
- WOLFSSL_X509_VERIFY_PARAM* pParam;
- printf(testingFmt, "wolfSSL_X509_VERIFY_PARAM_set1_host()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
- {
- #if defined(OPENSSL_EXTRA)
- unsigned char buf[16] = {0};
- WOLFSSL_X509_VERIFY_PARAM* param;
- printf(testingFmt, "test_wolfSSL_X509_VERIFY_PARAM_set1_ip()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
- {
- #if defined(OPENSSL_EXTRA)
- X509_STORE* store;
- X509_STORE_CTX* ctx;
- X509_STORE_CTX* ctx_no_init;
- printf(testingFmt, "wolfSSL_X509_STORE_CTX_get0_store()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_CTX_set_client_CA_list(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_CTX_set_client_CA_list()");
- 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);
- }
- printf(resultFmt, passed);
- #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;
- printf(testingFmt, "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);
- printf(resultFmt, passed);
- }
- #endif
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT && !NO_BIO */
- return 0;
- }
- static int test_wolfSSL_CTX_add_client_CA(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_CTX_add_client_CA()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
- return 0;
- }
- #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];
- 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(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)
- {
- #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
- !defined(NO_WOLFSSL_CLIENT)
- SSL_CTX* ctx;
- printf( testingFmt, "wolfSSL_CTX_set_keylog_callback()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
- return 0;
- }
- static int test_wolfSSL_CTX_get_keylog_callback(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
- !defined(NO_WOLFSSL_CLIENT)
- SSL_CTX* ctx;
- printf( testingFmt, "wolfSSL_CTX_get_keylog_callback()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
- return 0;
- }
- static int test_wolfSSL_Tls12_Key_Logging_test(void)
- {
- #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);
- printf(testingFmt, "wolfSSL_Tls12_Key_Logging_test()");
- #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 );
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
- return 0;
- }
- static int test_wolfSSL_Tls13_Key_Logging_test(void)
- {
- #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);
- printf(testingFmt, "wolfSSL_Tls13_Key_Logging_test()");
- #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);
- }
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
- return 0;
- }
- #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));
- 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)
- {
- #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;
- printf(testingFmt, "wolfSSL_Tls13_postauth()");
- 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;
- client_cbf.ssl_ready = set_post_auth_cb;
- server_cbf.on_result = post_auth_version_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;
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_NID(void)
- {
- #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];
- printf(testingFmt, "wolfSSL_X509_NID()");
- /* ------ 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_set_srp_username(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_CTX_set_srp_username()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
- /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
- return 0;
- }
- static int test_wolfSSL_CTX_set_srp_password(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_CTX_set_srp_password()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
- /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
- return 0;
- }
- static int test_wolfSSL_X509_STORE(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_X509_STORE");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_STORE_load_locations(void)
- {
- #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
- printf(testingFmt, "wolfSSL_X509_STORE_load_locations");
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_X509_STORE_get0_objects(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_STORE_get0_objects");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BN(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
- BIGNUM* a;
- BIGNUM* b;
- BIGNUM* c;
- BIGNUM* d;
- ASN1_INTEGER* ai;
- printf(testingFmt, "wolfSSL_BN()");
- 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
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
- return 0;
- }
- #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;
- printf("\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, stdout, 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;
- printf("Certificate at index [%d] = :\n",i);
- X509_print(bio,x509);
- printf("\n\n");
- }
- BIO_free(bio);
- #endif
- return SSL_SUCCESS;
- }
- #endif
- #endif
- static int test_wolfSSL_msgCb(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_msgCb");
- /* 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_either_side(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_either_side");
- /* 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_DTLS_either_side(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_DTLS_either_side");
- /* 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_generate_cookie(void)
- {
- #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
- SSL_CTX* ctx;
- SSL* ssl;
- byte buf[FOURK_BUF] = {0};
- printf(testingFmt, "test_generate_cookie");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_set_options(void)
- {
- #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
- printf(testingFmt, "wolfSSL_set_options()");
- #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
- AssertNull((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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return 0;
- }
- static int test_wolfSSL_sk_SSL_CIPHER(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_sk_SSL_CIPHER_*()");
- #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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return 0;
- }
- static int test_wolfSSL_set1_curves_list(void)
- {
- #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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return 0;
- }
- static int test_wolfSSL_set1_sigalgs_list(void)
- {
- #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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
- #endif
- return 0;
- }
- /* Testing wolfSSL_set_tlsext_status_type function.
- * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
- */
- static int test_wolfSSL_set_tlsext_status_type(void){
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
- !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
- SSL* ssl;
- SSL_CTX* ctx;
- printf(testingFmt, "wolfSSL_set_tlsext_status_type()");
- 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);
- #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
- return 0;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_read_bio(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_PEM_read_bio()");
- 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);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return 0;
- }
- #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)
- {
- #if defined(OPENSSL_EXTRA)
- const unsigned char* p;
- byte buff[20];
- BIO* bio1;
- BIO* bio2;
- BIO* bio3;
- char* bufPt;
- int i;
- printf(testingFmt, "wolfSSL_BIO()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #endif /* !NO_BIO */
- static int test_wolfSSL_ASN1_STRING(void)
- {
- #if defined(OPENSSL_EXTRA)
- ASN1_STRING* str = NULL;
- const char data[] = "hello wolfSSL";
- printf(testingFmt, "wolfSSL_ASN1_STRING()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_BIT_STRING(void)
- {
- #ifdef OPENSSL_ALL
- ASN1_BIT_STRING* str;
- printf(testingFmt, "test_wolfSSL_ASN1_BIT_STRING()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_a2i_ASN1_INTEGER(void)
- {
- #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";
- printf(testingFmt, "test_wolfSSL_a2i_ASN1_INTEGER()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_a2i_IPADDRESS(void)
- {
- #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
- };
- printf(testingFmt, "test_wolfSSL_a2i_IPADDRESS()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_DES_ecb_encrypt(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_DES_ecb_encrypt()");
- 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_TIME_adj(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_ASN1_TIME_adj()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_TIME_to_tm(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_ASN1_TIME_to_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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_cmp_time(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
- && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
- WOLFSSL_ASN1_TIME asn_time;
- time_t t;
- printf(testingFmt, "wolfSSL_X509_cmp_time()");
- 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));
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_time_adj(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_time_adj()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509()");
- 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 */
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_get_ext_count(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_get_ext_count()");
- /* 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);
- printf(testingFmt, "wolfSSL_X509_get_ext_count() valid input");
- AssertIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
- printf(resultFmt, ret == 4 ? passed : failed);
- printf(testingFmt, "wolfSSL_X509_get_ext_count() NULL argument");
- AssertIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
- printf(resultFmt, ret == WOLFSSL_FAILURE ? passed : failed);
- wolfSSL_X509_free(x509);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_sign2(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
- defined(WOLFSSL_CERT_EXT) && \
- (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, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3, 0xa6,
- 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c, 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, 0x01, 0x1a, 0xeb, 0x56, 0xab, 0xdc, 0x8b, 0xf3,
- 0xa6, 0x1e, 0xf4, 0x93, 0x60, 0x89, 0xb7, 0x05, 0x07, 0x29, 0x01, 0x2c,
- 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, 0xa3, 0x41, 0x43, 0x93, 0x30, 0x92, 0x98, 0xfe, 0x57,
- 0xd0, 0x39, 0x7c, 0x50, 0x06, 0x50, 0x20, 0x80, 0x0e, 0x28, 0x95, 0x79,
- 0xb4, 0xf1, 0x6b, 0x6a, 0xab, 0x78, 0x30, 0x93, 0x49, 0x0a, 0x6a, 0x19,
- 0x09, 0xae, 0x31, 0xc6, 0x8e, 0xcc, 0x69, 0x26, 0x89, 0x37, 0xc1, 0x57,
- 0x58, 0x75, 0xae, 0xbf, 0x13, 0xc8, 0xd6, 0xad, 0xd0, 0x0f, 0x57, 0xcd,
- 0x32, 0xa8, 0xda, 0xa8, 0x1b, 0xbf, 0xb5, 0xcd, 0x16, 0x14, 0x56, 0x86,
- 0x84, 0xb4, 0xab, 0x93, 0x52, 0x74, 0xfd, 0x96, 0x9f, 0x6d, 0xbe, 0xdb,
- 0x75, 0x5e, 0x76, 0xfe, 0xa6, 0x37, 0xe5, 0x5f, 0xcb, 0x62, 0x77, 0xc7,
- 0xd6, 0xcb, 0xb4, 0xf6, 0x43, 0xc8, 0x47, 0xdf, 0x12, 0x16, 0x28, 0x29,
- 0x61, 0xd1, 0xdc, 0x9d, 0x37, 0x9f, 0xe5, 0x71, 0x52, 0xae, 0xb8, 0x12,
- 0xec, 0x32, 0x9f, 0x03, 0x1a, 0x66, 0x98, 0xd8, 0xb0, 0x40, 0x71, 0x4c,
- 0xee, 0x64, 0x15, 0x48, 0x0c, 0x5c, 0x8a, 0x47, 0x20, 0xbd, 0x07, 0xc0,
- 0x30, 0xf8, 0x84, 0xe6, 0x29, 0x6d, 0xa9, 0x32, 0x53, 0x02, 0x4d, 0x3c,
- 0x99, 0x6e, 0x63, 0xfe, 0x39, 0x9c, 0x05, 0xa6, 0xa0, 0x0c, 0x1e, 0x11,
- 0xa4, 0x86, 0x6a, 0x89, 0x76, 0x54, 0x17, 0x68, 0x5d, 0x35, 0x9a, 0xd7,
- 0x5e, 0x27, 0x0e, 0xbb, 0xba, 0x67, 0x4d, 0x62, 0x12, 0xa8, 0x46, 0x1f,
- 0x0e, 0xd8, 0x7d, 0xc0, 0xae, 0x30, 0xc2, 0x45, 0x71, 0xab, 0xb1, 0xc1,
- 0xfb, 0xdc, 0x03, 0x7a, 0x52, 0xe6, 0x57, 0xf9, 0x7f, 0x65, 0x6b, 0x4e,
- 0x44, 0x64, 0xe8, 0x77, 0x82, 0x1c, 0xc8, 0xfa, 0x09, 0xc7, 0x2f, 0xa9,
- 0x40, 0x87, 0x8e, 0x0e, 0x49, 0xc2, 0x7d, 0x97, 0x27, 0x79, 0x90, 0xc2,
- 0x90, 0x13, 0xa7, 0x49, 0xb7, 0xd7, 0xc5, 0x02, 0x32, 0x4f, 0x1e, 0x34,
- 0x4a, 0xa6, 0xe4, 0xbd, 0xa5, 0xc6, 0xec
- };
- printf(testingFmt, "wolfSSL_X509_sign2");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_sign(void)
- {
- #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);
- printf(testingFmt, "wolfSSL_X509_sign");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_get0_tbs_sigalg(void)
- {
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
- X509* x509 = NULL;
- const X509_ALGOR* alg;
- printf(testingFmt, "wolfSSL_X509_get0_tbs_sigalg");
- AssertNotNull(x509 = X509_new());
- AssertNull(alg = X509_get0_tbs_sigalg(NULL));
- AssertNotNull(alg = X509_get0_tbs_sigalg(x509));
- X509_free(x509);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_ALGOR_get0(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_ALGOR_get0");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_VERIFY_PARAM(void)
- {
- #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";
- printf(testingFmt, "wolfSSL_X509()");
- 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 */
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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;
- printf(testingFmt, "wolfSSL_check_domain");
- 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
- printf(resultFmt, passed);
- return 0;
- }
- #endif /* OPENSSL_EXTRA && HAVE_IO_TESTS_DEPENDENCIES */
- static int test_wolfSSL_X509_get_X509_PUBKEY(void)
- {
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
- X509* x509 = NULL;
- X509_PUBKEY* pubKey;
- printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
- AssertNotNull(x509 = X509_new());
- AssertNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
- AssertNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
- X509_free(x509);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_PUBKEY_RSA(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_PUBKEY_RSA");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_PUBKEY_EC(void)
- {
- #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];
- printf(testingFmt, "wolfSSL_X509_PUBKEY_EC");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_PUBKEY_DSA(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_PUBKEY_DSA");
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RAND(void)
- {
- #if defined(OPENSSL_EXTRA)
- byte seed[16];
- printf(testingFmt, "wolfSSL_RAND()");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BUF(void)
- {
- #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);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- #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)
- {
- #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();
- printf(testingFmt, "wolfSSL_RAND_set_rand_method()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
- return 0;
- }
- static int test_wolfSSL_RAND_bytes(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_RAND_bytes()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BN_rand(void)
- {
- #if defined(OPENSSL_EXTRA)
- BIGNUM* bn;
- BIGNUM* range;
- printf(testingFmt, "wolfSSL_BN_rand()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_pseudo_rand(void)
- {
- #if defined(OPENSSL_EXTRA)
- BIGNUM* bn;
- unsigned char bin[8];
- int i;
- printf(testingFmt, "wolfSSL_pseudo_rand()");
- /* 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_PKCS8_Compat(void)
- {
- #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
- printf(testingFmt, "wolfSSL_pkcs8()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- #endif
- return 0;
- }
- static int test_wolfSSL_PKCS8_d2i(void)
- {
- #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);
- #ifndef NO_DES3
- 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 */
- #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 */
- printf(resultFmt, passed);
- #endif /* HAVE_FIPS && OPENSSL_EXTRA */
- return 0;
- }
- #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
- defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
- #define LOGGING_THREADS 5
- #define ERROR_COUNT 10
- static volatile int loggingThreadsReady;
- static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
- {
- const char* file;
- int line;
- int 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 */
- 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)
- {
- #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
- defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
- THREAD_TYPE loggingThreads[LOGGING_THREADS];
- int i;
- printf(testingFmt, "error_queue_per_thread()");
- 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]);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ERR_put_error(void)
- {
- #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
- defined(DEBUG_WOLFSSL)
- const char* file;
- int line;
- printf(testingFmt, "wolfSSL_ERR_put_error()");
- 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);
- #ifdef 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /*
- * 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)
- {
- #ifdef WOLFSSL_HAVE_ERROR_QUEUE
- printf(testingFmt, "test_wolfSSL_ERR_get_error_order");
- /* 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);
- printf(resultFmt, passed);
- #endif /* WOLFSSL_HAVE_ERROR_QUEUE */
- return 0;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_ERR_print_errors(void)
- {
- #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
- defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
- BIO* bio;
- char buf[1024];
- printf(testingFmt, "wolfSSL_ERR_print_errors()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
- defined(DEBUG_WOLFSSL)
- BIO* bio;
- char buf[1024];
- printf(testingFmt, "wolfSSL_ERR_print_errors_cb()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /*
- * Testing WOLFSSL_ERROR_MSG
- */
- static int test_WOLFSSL_ERROR_MSG(void)
- {
- int ret = 0;
- #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
- defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
- const char* msg = TEST_STRING;
- printf(testingFmt, "WOLFSSL_ERROR_MSG()");
- WOLFSSL_ERROR_MSG(msg);
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/*End test_WOLFSSL_ERROR_MSG*/
- /*
- * Testing wc_ERR_remove_state
- */
- static int test_wc_ERR_remove_state(void)
- {
- int ret = 0;
- #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
- printf(testingFmt, "wc_ERR_remove_state()");
- wc_ERR_remove_state();
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/*End test_wc_ERR_remove_state*/
- /*
- * Testing wc_ERR_print_errors_fp
- */
- static int test_wc_ERR_print_errors_fp(void)
- {
- int ret = 0;
- #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
- (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
- long sz;
- XFILE fp;
- printf(testingFmt, "wc_ERR_print_errors_fp()");
- 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
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- XFCLOSE(fp);
- (void)sz;
- #endif
- return ret;
- }/*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;
- printf(testingFmt, "wolfSSL_GetLoggingCb()");
- #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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- return ret;
- }/*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)
- {
- #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
- defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
- defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
- printf(testingFmt, "wolfSSL_HMAC()");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_CMAC(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_CMAC()");
- 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);
- printf(resultFmt, passed);
- #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
- return 0;
- }
- static int test_wolfSSL_OBJ(void)
- {
- /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
- * mode
- */
- #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;
- printf(testingFmt, "wolfSSL_OBJ()");
- 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 */
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_i2a_ASN1_OBJECT(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_OBJ_cmp(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
- ASN1_OBJECT *obj = NULL;
- ASN1_OBJECT *obj2 = NULL;
- printf(testingFmt, "wolfSSL_OBJ_cmp()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_OBJ_txt2nid(void)
- {
- #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 }
- };
- printf(testingFmt, "wolfSSL_OBJ_txt2nid()");
- /* 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_OBJ_txt2obj(void)
- {
- #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 }
- };
- printf(testingFmt, "wolfSSL_OBJ_txt2obj()");
- 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_i2t_ASN1_OBJECT(void)
- {
- #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";
- printf(testingFmt, "test_wolfSSL_i2t_ASN1_OBJECT()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
- return 0;
- }
- static int test_wolfSSL_PEM_write_bio_X509(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_AKID_NAME) && \
- defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && \
- !defined(NO_BIO) && !defined(NO_RSA)
- /* This test contains the hard coded expected
- * lengths. Update if necessary */
- BIO* input;
- BIO* output;
- X509* x509a = NULL;
- X509* x509b = NULL;
- ASN1_TIME* notBeforeA = NULL;
- ASN1_TIME* notAfterA = NULL;
- ASN1_TIME* notBeforeB = NULL;
- ASN1_TIME* notAfterB = NULL;
- int expectedLen;
- printf(testingFmt, "wolfSSL_PEM_write_bio_X509()");
- 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 */
- 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. */
- 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);
- 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;
- }
- 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 */
- 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 */
- AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
- AssertIntEQ(x509b->isCa, 1);
- AssertIntEQ(x509b->basicConstSet, 1);
- 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 */
- 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 */
- AssertNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
- AssertIntEQ(x509b->isCa, 0);
- AssertIntEQ(x509b->basicConstSet, 1);
- X509_free(x509a);
- X509_free(x509b);
- BIO_free(input);
- BIO_free(output);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_NAME_ENTRY(void)
- {
- #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
- printf(testingFmt, "wolfSSL_X509_NAME_ENTRY()");
- 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
- printf("\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 */
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_set_name(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
- X509* x509;
- X509_NAME* name;
- printf(testingFmt, "wolfSSL_X509_set_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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_ALL && !NO_CERTS */
- return 0;
- }
- static int test_wolfSSL_X509_set_notAfter(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_set_notAfter()");
- /*
- * 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_set_notBefore(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_set_notBefore()");
- /*
- * 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_set_version(void)
- {
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_BIO_gets(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_BIO_gets()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BIO_puts(void)
- {
- #if defined(OPENSSL_EXTRA)
- BIO* bio;
- char input[] = "hello\0world\n.....ok\n\0";
- char output[128];
- printf(testingFmt, "wolfSSL_BIO_puts()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BIO_dump(void)
- {
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #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;
- printf(testingFmt, "wolfSSL_BIO_should_retry()");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BIO_connect(void)
- {
- #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};
- printf(testingFmt, "wolfSSL_BIO_new_connect()");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BIO_tls(void)
- {
- #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
- SSL_CTX* ctx;
- SSL *ssl;
- BIO *readBio;
- BIO *writeBio;
- int ret, err = 0;
- printf(testingFmt, "test_wolfSSL_BIO_tls()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #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 */
- printf(testingFmt, "wolfSSL_BIO_new_accept()");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BIO_write(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_BIO_write()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BIO_printf(void)
- {
- #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";
- printf(testingFmt, "wolfSSL_BIO_printf()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BIO_f_md(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_BIO_f_md()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BIO_up_ref(void)
- {
- #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
- BIO* bio;
- printf(testingFmt, "wolfSSL_BIO_up_ref()");
- 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);
- printf(resultFmt, "passed");
- #endif
- return 0;
- }
- #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)
- {
- #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
- #endif
- return 0;
- }
- static int test_wolfSSL_SESSION(void)
- {
- #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";
- printf(testingFmt, "wolfSSL_SESSION()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #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;
- printf(testingFmt, "wolfSSL_CTX_sess_set_remove_cb()");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ticket_keys(void)
- {
- #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);
- #endif
- return 0;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_d2i_PUBKEY(void)
- {
- #if defined(OPENSSL_EXTRA)
- BIO* bio;
- EVP_PKEY* pkey;
- printf(testingFmt, "wolfSSL_d2i_PUBKEY()");
- 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;
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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
- printf(testingFmt, "wolfSSL_d2i_PrivateKeys_bio()");
- /* 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;
- printf(resultFmt, passed);
- return 0;
- }
- #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
- #endif /* !NO_BIO */
- static int test_wolfSSL_sk_GENERAL_NAME(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_sk_GENERAL_NAME()");
- 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:
- printf("found type GEN_DNS\n");
- break;
- case GEN_EMAIL:
- printf("found type GEN_EMAIL\n");
- break;
- case GEN_URI:
- printf("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);
- }
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_GENERAL_NAME_print(void)
- {
- #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>";
- printf(testingFmt, "test_wolfSSL_GENERAL_NAME_print()");
- /* 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_ALL */
- return 0;
- }
- static int test_wolfSSL_sk_DIST_POINT(void)
- {
- #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;
- GENERAL_NAME* gn;
- ASN1_IA5STRING* uri;
- STACK_OF(DIST_POINT)* dps;
- STACK_OF(GENERAL_NAME)* gns;
- const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
- printf(testingFmt, "wolfSSL_sk_DIST_POINT()");
- 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));
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_MD4(void)
- {
- #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);
- printf(testingFmt, "wolfSSL_MD4()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_verify_mode(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- printf(testingFmt, "test_wolfSSL_verify()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_verify_depth(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- long depth;
- printf(testingFmt, "test_wolfSSL_verify_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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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 */
- printf("test HMAC_Init with NULL key (0)\n");
- 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 */
- printf("test HMAC_Init with NULL key (1)\n");
- 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 */
- printf("test HMAC_Init with NULL key (2)\n");
- 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)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
- unsigned char digest[64];
- int digestSz;
- printf(testingFmt, "wolfSSL_HMAC_CTX()");
- #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 */
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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;
- printf("%s %d bytes of version %d , type %d : ", (w)?"Writing":"Reading",
- (int)sz, version, type);
- for (i = 0; i < (int)sz; i++) printf("%02X", pt[i]);
- printf("\n");
- (void)ssl;
- (void)arg;
- }
- #endif /* OPENSSL_EXTRA */
- static int test_wolfSSL_msg_callback(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- printf(testingFmt, "wolfSSL_msg_callback()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_SHA(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
- printf(testingFmt, "wolfSSL_SHA()");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_DH_1536_prime(void)
- {
- #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,
- };
- printf(testingFmt, "wolfSSL_DH_1536_prime()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_DH_get_2048_256(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_DH_get_2048_256()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_PEM_write_DHparams(void)
- {
- #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";
- printf(testingFmt, "wolfSSL_PEM_write_DHparams()");
- AssertNotNull(fp = XFOPEN(dhParamFile, "rb"));
- AssertIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
- XFCLOSE(fp);
- 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);
- XFCLOSE(fp);
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /* 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;
- printf("{");
- while((p != NULL) && (i < size)) {
- if((i % 8) == 0) {
- printf("\n");
- printf(" ");
- }
- printf("0x%02x, ", p[i]);
- i++;
- }
- printf("\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)
- {
- #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);
- 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);
- #endif /* test_EVP_Cipher */
- return 0;
- }
- static int test_wolfSSL_PEM_read_DHparams(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_BIO) && \
- !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;
- printf(testingFmt, "wolfSSL_PEM_read_DHparams()");
- 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);
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_AES_ecb_encrypt(void)
- {
- #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];
- printf(testingFmt, "wolfSSL_AES_ecb_encrypt()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_MD5(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_MD5_Transform(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_MD5_Transform()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_SHA224(void)
- {
- #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];
- printf(testingFmt, "wolfSSL_SHA224()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_SHA_Transform(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_SHA_Transform()");
- 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 */
- printf(resultFmt, passed);
- #endif
- #endif
- return 0;
- }
- static int test_wolfSSL_SHA256_Transform(void)
- {
- #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)
- 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;
- printf(testingFmt, "wolfSSL_SHA256_Transform()");
- 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 */
- printf(resultFmt, passed);
- #endif
- #endif
- return 0;
- }
- static int test_wolfSSL_SHA256(void)
- {
- #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];
- printf(testingFmt, "wolfSSL_SHA256()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_SHA512_Transform(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
- #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
- (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
- 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;
- printf(testingFmt, "wolfSSL_SHA512_Transform()");
- 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;
- printf(resultFmt, passed);
- #endif
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_get_serialNumber(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
- ASN1_INTEGER* a;
- BIGNUM* bn;
- X509* x509;
- char *serialHex;
- byte serial[3];
- int serialSz;
- printf(testingFmt, "wolfSSL_X509_get_serialNumber()");
- 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_OpenSSL_add_all_algorithms(void){
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "wolfSSL_OpenSSL_add_all_algorithms()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_OPENSSL_hexstr2buf(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_OPENSSL_hexstr2buf()");
- 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_STRING_print_ex(void){
- #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];
- printf(testingFmt, "wolfSSL_ASN1_STRING_print_ex()");
- /* 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);
- printf(resultFmt, passed);
- #endif /* !NO_BIO */
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void){
- #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;
- printf(testingFmt, "wolfSSL_ASN1_TIME_to_generalizedtime()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_CA_num(void){
- #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;
- printf(testingFmt, "wolfSSL_X509_CA_num()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_check_ca(void){
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- WOLFSSL_X509 *x509;
- printf(testingFmt, "wolfSSL_X509_check_ca()");
- x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
- AssertIntEQ(wolfSSL_X509_check_ca(x509), 1);
- wolfSSL_X509_free(x509);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_check_ip_asc(void){
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
- WOLFSSL_X509 *x509;
- printf(testingFmt, "wolfSSL_X509_check_ip_asc()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_make_cert(void)
- {
- #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
- printf(testingFmt, "wolfSSL Make Certs");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_get_version(void){
- #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- WOLFSSL_X509 *x509;
- printf(testingFmt, "wolfSSL_X509_get_version()");
- x509 = wolfSSL_X509_load_certificate_file(svrCertFile, WOLFSSL_FILETYPE_PEM);
- AssertNotNull(x509);
- AssertIntEQ((int)wolfSSL_X509_get_version(x509), 2);
- wolfSSL_X509_free(x509);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_DES_ncbc(void){
- #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};
- printf(testingFmt, "wolfSSL_DES_ncbc()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_AES_cbc_encrypt(void)
- {
- #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)
- printf(testingFmt, "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);
- printf(resultFmt, "Stress Tests: passed");
- printf(testingFmt, "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);
- printf(resultFmt, "Stress Tests: passed");
- printf(testingFmt, "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);
- printf(resultFmt, "Stress Tests: passed");
- #ifdef WOLFSSL_AES_128
- printf(testingFmt, "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);
- printf(resultFmt, "passed");
- #ifdef HAVE_AES_DECRYPT
- printf(testingFmt, "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);
- printf(resultFmt, "passed");
- #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);
- printf(testingFmt, "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);
- printf(resultFmt, "passed");
- #ifdef HAVE_AES_DECRYPT
- printf(testingFmt, "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);
- printf(resultFmt, "passed");
- #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);
- printf(testingFmt, "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);
- printf(resultFmt, "passed");
- #ifdef HAVE_AES_DECRYPT
- printf(testingFmt, "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);
- printf(resultFmt, "passed");
- #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 };
- printf(testingFmt, "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));
- printf(resultFmt, "passed");
- printf(testingFmt, "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);
- printf(resultFmt, "passed");
- XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
- XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
- printf(testingFmt, "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));
- printf(resultFmt, "passed");
- printf(testingFmt, "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);
- printf(resultFmt, "passed");
- }
- #endif /* HAVE_AES_KEYWRAP */
- }
- #endif /* WOLFSSL_AES_256 */
- #endif
- return 0;
- }
- static int test_wolfSSL_CRYPTO_cts128(void)
- {
- #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);
- }
- #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
- return 0;
- }
- #if defined(OPENSSL_ALL)
- #if !defined(NO_ASN)
- static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
- {
- #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);
- printf(testingFmt, "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);
- printf(resultFmt, result == 0 ? passed : failed);
- printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, valid): ");
- AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)),
- WOLFSSL_FATAL_ERROR);
- printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
- printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(valid, NULL): ");
- AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)),
- WOLFSSL_FATAL_ERROR);
- printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
- printf(testingFmt, "wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL): ");
- AssertIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)),
- WOLFSSL_FATAL_ERROR);
- printf(resultFmt, len == WOLFSSL_FATAL_ERROR ? passed : failed);
- wolfSSL_X509_free(x509);
- XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- #endif
- return 0;
- }
- 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";
- printf(testingFmt, "test_wolfSSL_ASN1_UNIVERSALSTRING_to_string()");
- 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);
- printf(resultFmt, "passed");
- return 0;
- }
- #endif /* !defined(NO_ASN) */
- static int test_wolfSSL_sk_CIPHER_description(void)
- {
- #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));
- printf(testingFmt, "wolfSSL_sk_CIPHER_description");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_get_ciphers_compat(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_get_ciphers_compat");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- 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;
- printf(testingFmt, "wolfSSL_X509_PUBKEY_get()");
- 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));
- printf(resultFmt,retEvpPkey == NULL ? passed : failed);
- return 0;
- }
- static int test_wolfSSL_d2i_DHparams(void)
- {
- #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
- printf(testingFmt, "wolfSSL_d2i_DHparams() 2048-bit");
- 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);
- #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
- AssertIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), WOLFSSL_SUCCESS);
- #endif
- 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);
- printf(resultFmt, passed);
- *buf = 0;
- pt = buf;
- #endif /* HAVE_FFDHE_2048 */
- /* Test 3072 bit parameters */
- #ifdef HAVE_FFDHE_3072
- printf(testingFmt, "wolfSSL_d2i_DHparams() 3072-bit");
- 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));
- 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);
- printf(resultFmt, passed);
- #endif /* HAVE_FFDHE_3072 */
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* !NO_DH */
- return 0;
- }
- static int test_wolfSSL_i2d_DHparams(void)
- {
- #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 = buf;
- unsigned char* pt2 = 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;
- WOLFSSL_DH* dh;
- /* Test 2048 bit parameters */
- #ifdef HAVE_FFDHE_2048
- printf(testingFmt, "wolfSSL_i2d_DHparams() 2048-bit");
- 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);
- printf(resultFmt, passed);
- *buf = 0;
- pt = buf;
- pt2 = buf;
- #endif
- /* Test 3072 bit parameters */
- #ifdef HAVE_FFDHE_3072
- printf(testingFmt, "wolfSSL_i2d_DHparams() 3072-bit");
- 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);
- printf(resultFmt, passed);
- #endif
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif
- return 0;
- }
- static int test_wolfSSL_EC_KEY_dup(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EC_KEY_dup()");
- 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 */
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
- {
- #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 */
- printf(testingFmt,
- "wolfSSL_EVP_PKEY_set1_DSA and wolfSSL_EVP_PKEY_get1_DSA");
- /* 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);
- printf(resultFmt, passed);
- DSA_free(dsa);
- DSA_free(setDsa);
- EVP_PKEY_free(pkey);
- EVP_PKEY_free(set1Pkey);
- #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
- return 0;
- } /* END test_EVP_PKEY_set1_get1_DSA */
- static int test_wolfSSL_DSA_SIG(void)
- {
- #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};
- printf(testingFmt, "wolfSSL_DSA_SIG");
- 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);
- printf(resultFmt, passed);
- DSA_free(dsa);
- DSA_free(dsa2);
- DSA_SIG_free(sig);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
- {
- #ifdef HAVE_ECC
- WOLFSSL_EC_KEY *ecKey = NULL;
- WOLFSSL_EC_KEY *ecGet1 = NULL;
- EVP_PKEY *pkey = NULL;
- printf(testingFmt,
- "wolfSSL_EVP_PKEY_set1_EC_KEY and wolfSSL_EVP_PKEY_get1_EC_KEY");
- 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);
- /* PASSED */
- printf(resultFmt, passed);
- #endif /* HAVE_ECC */
- return 0;
- } /* END test_EVP_PKEY_set1_get1_EC_KEY */
- static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
- {
- #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;
- printf(testingFmt,"wolfSSL_EVP_PKEY_set1_DH and wolfSSL_EVP_PKEY_get1_DH");
- 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);
- printf(resultFmt, passed);
- #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
- return 0;
- } /* END test_EVP_PKEY_set1_get1_DH */
- static int test_wolfSSL_CTX_ctrl(void)
- {
- #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
- printf(testingFmt, "wolfSSL_CTX_ctrl");
- 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);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- return 0;
- }
- static int test_wolfSSL_DH_check(void)
- {
- #if !defined(NO_DH) && !defined(NO_DSA)
- #ifndef NO_BIO
- byte buf[6000];
- char file[] = "./certs/dsaparams.pem";
- XFILE f;
- int bytes;
- BIO* bio;
- DSA* dsa;
- DH* dh = NULL;
- WOLFSSL_BIGNUM* pTmp = NULL;
- WOLFSSL_BIGNUM* gTmp = NULL;
- int codes = -1;
- printf(testingFmt, "wolfSSL_DH_check");
- /* 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);
- /* 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), WOLFSSL_SUCCESS);
- AssertIntEQ(codes, 0);
- /* Test NULL dh: expected BAD_FUNC_ARG */
- AssertIntEQ(wolfSSL_DH_check(NULL, &codes), WOLFSSL_FAILURE);
- /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
- pTmp = dh->p;
- dh->p = NULL;
- AssertIntEQ(wolfSSL_DH_check(dh, &codes), WOLFSSL_FAILURE);
- 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), WOLFSSL_FAILURE);
- AssertIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
- dh->g = gTmp;
- gTmp = NULL;
- /* Cleanup and Pass Test */
- BIO_free(bio);
- DSA_free(dsa);
- DH_free(dh);
- printf(resultFmt, passed);
- #endif
- #endif /* !NO_DH && !NO_DSA */
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_assign(void)
- {
- 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;
- printf(testingFmt, "wolfSSL_EVP_PKEY_assign");
- #ifndef NO_RSA
- 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);
- #endif /* NO_RSA */
- #ifndef NO_DSA
- 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);
- #endif /* NO_DSA */
- #ifdef HAVE_ECC
- 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);
- #endif /* HAVE_ECC */
- (void)type;
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_base_id(void)
- {
- WOLFSSL_EVP_PKEY* pkey;
- printf(testingFmt, "wolfSSL_EVP_PKEY_base_id");
- 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);
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_id(void)
- {
- WOLFSSL_EVP_PKEY* pkey;
- printf(testingFmt, "wolfSSL_EVP_PKEY_id");
- 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);
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_paramgen(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EVP_PKEY_paramgen");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- 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
- printf(testingFmt, "wolfSSL_EVP_PKEY_keygen");
- 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
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_keygen_init(void)
- {
- WOLFSSL_EVP_PKEY* pkey;
- EVP_PKEY_CTX *ctx;
- printf(testingFmt, "wolfSSL_EVP_PKEY_keygen_init");
- 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);
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
- WOLFSSL_EVP_PKEY* pkey;
- printf(testingFmt, "wolfSSL_EVP_PKEY_missing_parameters");
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
- EVP_PKEY_free(pkey);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EVP_PKEY_copy_parameters");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
- {
- WOLFSSL_EVP_PKEY* pkey;
- EVP_PKEY_CTX *ctx;
- int bits = 2048;
- printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits");
- 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);
- printf(resultFmt, passed);
- return 0;
- }
- 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
- };
- printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_iv_length");
- 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);
- }
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
- {
- #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();
- printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_key_length");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
- {
- #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();
- printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_key_length");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
- {
- #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();
- printf(testingFmt, "wolfSSL_EVP_CIPHER_CTX_set_iv");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
- {
- WOLFSSL_ENGINE* e = NULL;
- int id = 0;
- EVP_PKEY_CTX *ctx;
- printf(testingFmt, "wolfSSL_EVP_PKEY_CTX_new_id");
- AssertNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
- EVP_PKEY_CTX_free(ctx);
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_rc4(void)
- {
- #if !defined(NO_RC4)
- printf(testingFmt, "wolfSSL_EVP_rc4");
- AssertNotNull(wolfSSL_EVP_rc4());
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_enc_null(void)
- {
- printf(testingFmt, "wolfSSL_EVP_enc_null");
- AssertNotNull(wolfSSL_EVP_enc_null());
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_rc2_cbc(void)
- {
- #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
- printf(testingFmt, "wolfSSL_EVP_rc2_cbc");
- AssertNull(wolfSSL_EVP_rc2_cbc());
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_mdc2(void)
- {
- #if !defined(NO_WOLFSSL_STUB)
- printf(testingFmt, "wolfSSL_EVP_mdc2");
- AssertNull(wolfSSL_EVP_mdc2());
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_md4(void)
- {
- #if !defined(NO_MD4)
- printf(testingFmt, "wolfSSL_EVP_md4");
- AssertNotNull(wolfSSL_EVP_md4());
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_aes_256_gcm(void)
- {
- printf(testingFmt, "wolfSSL_EVP_aes_256_gcm");
- AssertNotNull(wolfSSL_EVP_aes_256_gcm());
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_aes_192_gcm(void)
- {
- printf(testingFmt, "wolfSSL_EVP_aes_192_gcm");
- AssertNotNull(wolfSSL_EVP_aes_192_gcm());
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_ripemd160(void)
- {
- #if !defined(NO_WOLFSSL_STUB)
- printf(testingFmt, "wolfSSL_EVP_ripemd160");
- AssertNull(wolfSSL_EVP_ripemd160());
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_get_digestbynid(void)
- {
- printf(testingFmt, "wolfSSL_EVP_get_digestbynid");
- #ifndef NO_MD5
- AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
- #endif
- AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
- AssertNull(wolfSSL_EVP_get_digestbynid(0));
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_MD_nid(void)
- {
- printf(testingFmt, "wolfSSL_EVP_MD_nid");
- #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);
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
- {
- #if defined(HAVE_ECC)
- WOLFSSL_EVP_PKEY* pkey;
- printf(testingFmt, "wolfSSL_EVP_PKEY_get0_EC_KEY");
- AssertNotNull(pkey = EVP_PKEY_new());
- AssertNull(EVP_PKEY_get0_EC_KEY(pkey));
- EVP_PKEY_free(pkey);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_X_STATE(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EVP_X_STATE");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_X_STATE_LEN(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EVP_X_STATE_LEN");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_CIPHER_block_size(void)
- {
- #ifdef HAVE_AES_CBC
- #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
- #endif
- #ifdef HAVE_AESGCM
- #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
- #endif
- #ifdef WOLFSSL_AES_COUNTER
- #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
- #endif
- #ifdef HAVE_AES_ECB
- #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
- #endif
- #ifdef WOLFSSL_AES_OFB
- #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
- #endif
- #ifndef NO_RC4
- AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
- #endif
- #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
- AssertIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
- #endif
- return 0;
- }
- 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
- };
- printf(testingFmt, "wolfSSL_EVP_CIPHER_iv_length");
- 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]);
- }
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_SignInit_ex(void)
- {
- WOLFSSL_EVP_MD_CTX mdCtx;
- WOLFSSL_ENGINE* e = 0;
- const EVP_MD* md;
- md = "SHA256";
- printf(testingFmt, "wolfSSL_EVP_SignInit_ex");
- wolfSSL_EVP_MD_CTX_init(&mdCtx);
- AssertIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
- printf(resultFmt, passed);
- return 0;
- }
- static int test_wolfSSL_EVP_DigestFinal_ex(void)
- {
- #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];
- printf(testingFmt, "wolfSSL_EVP_DigestFinal_ex");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_assign_DH(void)
- {
- #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);
- printf(testingFmt, "wolfSSL_EVP_PKEY_assign_DH");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
- {
- #if defined(OPENSSL_EXTRA)
- EVP_PKEY* pkey;
- EVP_PKEY_CTX* ctx;
- printf(testingFmt, "test_wolfSSL_QT_EVP_PKEY_CTX_free");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_param_check(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_EVP_PKEY_param_check");
- 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);
- printf(resultFmt, passed);
- #endif
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_BytesToKey(void)
- {
- #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);
- printf(testingFmt, "EVP_BytesToKey");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_evp_cipher_aes_gcm(void)
- {
- #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;
- printf(testingFmt, "test_evp_cipher_aes_gcm");
- /****************************************************/
- 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
- * gcmIvGenEnable 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- 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*);
- printf(testingFmt, "wolfSSL_OBJ_ln");
- 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]);
- }
- printf(resultFmt, passed);
- return 0;
- }
- 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};
- printf(testingFmt, "wolfSSL_OBJ_sn");
- 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]);
- }
- printf(resultFmt, passed);
- return 0;
- }
- #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)
- {
- #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;
- printf(testingFmt, "wolfSSL_TXT_DB");
- /* 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_sk_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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_NCONF(void)
- {
- #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
- const char* confFile = "./tests/NCONF_test.cnf";
- CONF* conf = NULL;
- long eline = 0;
- long num = 0;
- printf(testingFmt, "wolfSSL_NCONF");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #endif /* OPENSSL_ALL */
- static int test_wolfSSL_EC_KEY_set_group(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EC_KEY_dup()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EC_KEY_set_conv_form(void)
- {
- #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA)
- BIO* bio;
- EC_KEY* key;
- printf(testingFmt, "test_wolfSSL_EC_KEY_set_conv_form");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EC_KEY_print_fp(void)
- {
- #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
- defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256 && \
- defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
- !defined(NO_STDIO_FILESYSTEM)
- EC_KEY* key = NULL;
- printf(testingFmt, "test_wolfSSL_EC_KEY_print_fp");
- /* Bad file pointer. */
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
- /* NULL key. */
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, NULL, 0), WOLFSSL_FAILURE);
- AssertNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
- /* Negative indent. */
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, -1), WOLFSSL_FAILURE);
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, key, 4), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_EC_KEY_print_fp(stdout, 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(stdout, key, 4), WOLFSSL_SUCCESS);
- wolfSSL_EC_KEY_free(key);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509V3_EXT_get(void) {
- #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);
- printf(testingFmt, "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);
- }
- printf(resultFmt, "passed");
- printf(testingFmt, "wolfSSL_X509V3_EXT_get() NULL argument test");
- AssertNull(method = wolfSSL_X509V3_EXT_get(NULL));
- printf(resultFmt, "passed");
- wolfSSL_X509_free(x509);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509V3_EXT_nconf(void)
- {
- #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();
- printf(testingFmt, "wolfSSL_X509V3_EXT_nconf()");
- 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);
- printf(resultFmt, "passed");
- #endif
- return 0;
- }
- static int test_wolfSSL_X509V3_EXT(void) {
- #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;
- printf(testingFmt, "wolfSSL_X509V3_EXT_d2i()");
- /* 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);
- printf(resultFmt, "passed");
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_get_extension_flags(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- XFILE f;
- X509* x509;
- unsigned int extFlags;
- unsigned int keyUsageFlags;
- unsigned int extKeyUsageFlags;
- printf(testingFmt, "test_wolfSSL_X509_get_extension_flags");
- /* 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_ALL */
- return 0;
- }
- static int test_wolfSSL_X509_get_ext(void){
- #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);
- printf(testingFmt, "wolfSSL_X509_get_ext() valid input");
- AssertNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
- printf(resultFmt, "passed");
- printf(testingFmt, "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));
- printf(resultFmt, "passed");
- printf(testingFmt, "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));
- printf(resultFmt, "passed");
- printf(testingFmt, "wolfSSL_X509_get_ext() NULL x509, valid idx");
- AssertNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
- printf(resultFmt, "passed");
- wolfSSL_X509_free(x509);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_get_ext_by_NID(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
- {
- #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};
- printf(testingFmt, "test_wolfSSL_X509_get_ext_subj_alt_name");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_EXTENSION_new(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_EXTENSION_get_object(void)
- {
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- WOLFSSL_ASN1_OBJECT* o;
- FILE* file;
- int nid = 0;
- AssertNotNull(file = fopen("./certs/server-cert.pem", "rb"));
- AssertNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
- fclose(file);
- printf(testingFmt, "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);
- nid = o->nid;
- printf(resultFmt, nid == 128 ? passed : failed);
- printf(testingFmt, "wolfSSL_X509_EXTENSION_get_object() NULL argument");
- AssertNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
- printf(resultFmt, passed);
- wolfSSL_X509_free(x509);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_EXTENSION_get_data(void)
- {
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- WOLFSSL_ASN1_STRING* str;
- FILE* file;
- printf(testingFmt, "wolfSSL_X509_EXTENSION_get_data");
- 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));
- printf(resultFmt, passed);
- wolfSSL_X509_free(x509);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_EXTENSION_get_critical(void)
- {
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
- WOLFSSL_X509* x509;
- WOLFSSL_X509_EXTENSION* ext;
- FILE* file;
- int crit;
- printf(testingFmt, "wolfSSL_X509_EXTENSION_get_critical");
- 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);
- printf(resultFmt, passed);
- wolfSSL_X509_free(x509);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509V3_EXT_print(void)
- {
- #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
- !defined(NO_RSA)
- printf(testingFmt, "wolfSSL_X509V3_EXT_print");
- {
- 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(stdout, BIO_NOCLOSE));
- AssertNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
- WOLFSSL_FILETYPE_PEM));
- printf("\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);
- printf("\n");
- }
- BIO_free(bio);
- X509_free(x509);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_cmp(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_RSA)
- FILE* file1;
- FILE* file2;
- WOLFSSL_X509* cert1;
- WOLFSSL_X509* cert2;
- int ret = 0;
- 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);
- printf(testingFmt, "wolfSSL_X509_cmp() testing matching certs");
- ret = wolfSSL_X509_cmp(cert1, cert1);
- AssertIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- printf(testingFmt, "wolfSSL_X509_cmp() testing mismatched certs");
- ret = wolfSSL_X509_cmp(cert1, cert2);
- AssertIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
- printf(resultFmt, ret == -1 ? passed : failed);
- printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, valid args");
- ret = wolfSSL_X509_cmp(NULL, cert2);
- AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
- printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
- printf(testingFmt, "wolfSSL_X509_cmp() testing valid, NULL args");
- ret = wolfSSL_X509_cmp(cert1, NULL);
- AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
- printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
- printf(testingFmt, "wolfSSL_X509_cmp() testing NULL, NULL args");
- ret = wolfSSL_X509_cmp(NULL, NULL);
- AssertIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
- printf(resultFmt, ret == BAD_FUNC_ARG ? passed : failed);
- wolfSSL_X509_free(cert1);
- wolfSSL_X509_free(cert2);
- #endif
- return 0;
- }
- static int test_wolfSSL_PKEY_up_ref(void)
- {
- #if defined(OPENSSL_ALL)
- EVP_PKEY* pkey;
- printf(testingFmt, "wolfSSL_PKEY_up_ref()");
- 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);
- printf(resultFmt, "passed");
- #endif
- return 0;
- }
- static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- EVP_PKEY* pkey;
- const unsigned char* p;
- unsigned char* der = NULL;
- int derLen;
- printf(testingFmt, "test_wolfSSL_d2i_and_i2d_PublicKey()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_d2i_and_i2d_DSAparams()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_i2d_PrivateKey(void)
- {
- #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_PWDBASED)
- printf(testingFmt, "wolfSSL_i2d_PrivateKey()");
- #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
- printf(resultFmt, "passed");
- #endif
- return 0;
- }
- static int test_wolfSSL_OCSP_id_get0_info(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_OCSP_id_get0_info()");
- 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);
- printf(resultFmt, "passed");
- #endif
- return 0;
- }
- static int test_wolfSSL_i2d_OCSP_CERTID(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_i2d_OCSP_CERTID()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_OCSP_id_cmp(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
- OCSP_CERTID id1;
- OCSP_CERTID id2;
- printf(testingFmt, "wolfSSL_OCSP_id_cmp()");
- XMEMSET(&id1, 0, sizeof(id1));
- XMEMSET(&id2, 0, sizeof(id2));
- AssertIntEQ(OCSP_id_cmp(&id1, &id2), 0);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
- {
- #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);
- printf(testingFmt, "wolfSSL_OCSP_SINGLERESP_get0_id()");
- AssertPtrEq(&single, certId);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_OCSP_single_get0_status(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
- WOLFSSL_OCSP_SINGLERESP single;
- CertStatus certStatus;
- WOLFSSL_ASN1_TIME* thisDate;
- WOLFSSL_ASN1_TIME* nextDate;
- int ret, i;
- printf(testingFmt, "wolfSSL_OCSP_single_get0_status()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_OCSP_resp_count(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
- WOLFSSL_OCSP_BASICRESP basicResp;
- WOLFSSL_OCSP_SINGLERESP singleRespOne;
- WOLFSSL_OCSP_SINGLERESP singleRespTwo;
- int count;
- printf(testingFmt, "wolfSSL_OCSP_resp_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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_OCSP_resp_get0(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
- WOLFSSL_OCSP_BASICRESP basicResp;
- WOLFSSL_OCSP_SINGLERESP singleRespOne;
- WOLFSSL_OCSP_SINGLERESP singleRespTwo;
- WOLFSSL_OCSP_SINGLERESP* ret;
- printf(testingFmt, "wolfSSL_OCSP_resp_get0()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_derive(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EVP_PKEY_derive()");
- #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 */
- printf(resultFmt, "passed");
- #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
- #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
- return 0;
- }
- static int test_wolfSSL_EVP_PBE_scrypt(void)
- {
- #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};
- printf(testingFmt, "wolfSSL_EVP_PBE_scrypt()");
- /* 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 */
- printf(resultFmt, "passed");
- #endif /* !NO_PWDBASED && !NO_SHA256 */
- #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
- return 0;
- }
- static int test_wolfSSL_EC_get_builtin_curves(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_EC_get_builtin_curves");
- 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);
- printf(resultFmt, passed);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* defined(HAVE_ECC) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
- return 0;
- }
- static int test_no_op_functions(void)
- {
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "no_op_functions()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_CRYPTO_memcmp(void)
- {
- #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);
- #endif
- return 0;
- }
- /*----------------------------------------------------------------------------*
- | wolfCrypt ASN
- *----------------------------------------------------------------------------*/
- static int test_wc_CreateEncryptedPKCS8Key(void)
- {
- #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;
- printf(testingFmt, "test_wc_CreateEncryptedPKCS8Key");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_GetPkcs8TraditionalOffset(void)
- {
- #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];
- printf(testingFmt, "wc_GetPkcs8TraditionalOffset");
- 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);
- printf(resultFmt, passed);
- #endif /* NO_ASN */
- return 0;
- }
- static int test_wc_SetSubjectRaw(void)
- {
- #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;
- printf(testingFmt, "test_wc_SetSubjectRaw()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_GetSubjectRaw(void)
- {
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
- Cert cert;
- byte *subjectRaw;
- printf(testingFmt, "test_wc_GetSubjectRaw()");
- AssertIntEQ(0, wc_InitCert(&cert));
- AssertIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_SetIssuerRaw(void)
- {
- #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;
- printf(testingFmt, "test_wc_SetIssuerRaw()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wc_SetIssueBuffer(void)
- {
- #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;
- printf(testingFmt, "test_wc_SetIssuerBuffer()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /*
- * Testing wc_SetSubjectKeyId
- */
- static int test_wc_SetSubjectKeyId(void)
- {
- #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";
- printf(testingFmt, "wc_SetSubjectKeyId()");
- 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"));
- printf(resultFmt, passed);
- #endif
- return 0;
- } /* END test_wc_SetSubjectKeyId */
- /*
- * Testing wc_SetSubject
- */
- static int test_wc_SetSubject(void)
- {
- #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";
- printf(testingFmt, "wc_SetSubject()");
- 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"));
- printf(resultFmt, passed);
- #endif
- return 0;
- } /* END test_wc_SetSubject */
- static int test_CheckCertSignature(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wc_ParseCert(void)
- {
- #if !defined(NO_CERTS) && !defined(NO_RSA)
- DecodedCert decodedCert;
- const byte* rawCert = client_cert_der_2048;
- const int rawCertSize = sizeof_client_cert_der_2048;
- printf(testingFmt, "wc_ParseCert");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_MakeCertWithPathLen(void)
- {
- #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;
- printf(testingFmt, "test_MakeCertWithPathLen");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /*----------------------------------------------------------------------------*
- | wolfCrypt ECC
- *----------------------------------------------------------------------------*/
- static int test_wc_ecc_get_curve_size_from_name(void)
- {
- #ifdef HAVE_ECC
- int ret;
- printf(testingFmt, "wc_ecc_get_curve_size_from_name");
- #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);
- printf(resultFmt, passed);
- #endif /* HAVE_ECC */
- return 0;
- }
- static int test_wc_ecc_get_curve_id_from_name(void)
- {
- #ifdef HAVE_ECC
- int id;
- printf(testingFmt, "wc_ecc_get_curve_id_from_name");
- #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);
- printf(resultFmt, passed);
- #endif /* HAVE_ECC */
- return 0;
- }
- #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;
- printf(testingFmt, "wc_ecc_get_curve_id_from_dp_params");
- #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);
- printf(resultFmt, passed);
- return 0;
- }
- #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
- static int test_wc_ecc_get_curve_id_from_params(void)
- {
- #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;
- printf(testingFmt, "wc_ecc_get_curve_id_from_params");
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_encrypt(void)
- {
- #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
- printf(testingFmt, "wolfSSL_EVP_PKEY_encrypt()");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_sign_verify(void)
- {
- #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
- printf(testingFmt, "wolfSSL_EVP_PKEY_sign_verify()");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_EVP_PKEY_rsa(void)
- {
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_EVP_PKEY_ec(void)
- {
- #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);
- printf(resultFmt, passed);
- #endif
- #endif
- return 0;
- }
- static int test_EVP_PKEY_cmp(void)
- {
- #if defined(OPENSSL_EXTRA)
- EVP_PKEY *a, *b;
- const unsigned char *in;
- printf(testingFmt, "wolfSSL_EVP_PKEY_cmp()");
- #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;
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_ERR_load_crypto_strings(void)
- {
- #if defined(OPENSSL_ALL)
- ERR_load_crypto_strings();
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
- STACK_OF(X509)* s;
- AssertNotNull(s = sk_X509_new());
- 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());
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_sk_X509_CRL(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
- X509_CRL* crl;
- XFILE fp;
- STACK_OF(X509_CRL)* s;
- printf(testingFmt, "test_sk_X509_CRL");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_X509_get_signature_nid(void)
- {
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_X509_REQ(void)
- {
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfssl_PKCS7(void)
- {
- #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);
- 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;
- pkcs7->hashOID = SHAh;
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_PKCS7_sign(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_PKCS7_sign()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_PKCS7_SIGNED_new(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
- PKCS7_SIGNED* pkcs7;
- printf(testingFmt, "wolfSSL_PKCS7_SIGNED_new()");
- pkcs7 = PKCS7_SIGNED_new();
- AssertNotNull(pkcs7);
- AssertIntEQ(pkcs7->contentOID, SIGNED_DATA);
- PKCS7_SIGNED_free(pkcs7);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_write_bio_PKCS7(void)
- {
- #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
- printf(testingFmt, "wolfSSL_PEM_write_bio_PKCS7()");
- 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;
- pkcs7->hashOID = SHAh;
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #ifdef HAVE_SMIME
- static int test_wolfSSL_SMIME_read_PKCS7(void)
- {
- #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");
- printf(testingFmt, "wolfSSL_SMIME_read_PKCS7()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_SMIME_write_PKCS7(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_SMIME_write_PKCS7()");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #endif /* HAVE_SMIME */
- #endif /* !NO_BIO */
- /*----------------------------------------------------------------------------*
- | Certificate Failure Checks
- *----------------------------------------------------------------------------*/
- #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
- !defined(WOLFSSL_NO_CLIENT_AUTH))
- /* 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
- printf("RSA disabled, skipping test\n");
- return ASN_SIG_CONFIRM_E;
- #else
- break;
- #endif
- case TESTING_ECC:
- #ifndef HAVE_ECC
- printf("ECC disabled, skipping test\n");
- return ASN_SIG_CONFIRM_E;
- #else
- break;
- #endif
- default:
- printf("Bad function argument\n");
- return BAD_FUNC_ARG;
- }
- cm = wolfSSL_CertManagerNew();
- if (cm == NULL) {
- printf("wolfSSL_CertManagerNew failed\n");
- return -1;
- }
- #ifndef NO_FILESYSTEM
- ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
- if (ret != WOLFSSL_SUCCESS) {
- printf("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;
- }
- 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);
- }
- printf("Signature failure test: RSA: Ret %d\n", ret);
- 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 ret;
- }
- 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);
- }
- printf("Signature failure test: ECC: Ret %d\n", ret);
- 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 ret;
- }
- #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;
- #ifndef NO_CERTS
- 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_SABER_LEVEL3
- #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_SABER_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());
- #ifndef NO_CERTS
- 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());
- #ifndef NO_CERTS
- 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, 0), SIDE_ERROR);
- AssertIntEQ(wolfSSL_get_max_early_data(clientSsl), SIDE_ERROR);
- #else
- AssertIntEQ(SSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
- AssertIntEQ(SSL_get_max_early_data(clientSsl), SIDE_ERROR);
- #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 0;
- }
- #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)
- {
- #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;
- printf(testingFmt, "test_DhCallbacks");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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 ret;
- }
- #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 ret;
- }
- #endif
- /*
- * Testing get_rand_digit
- */
- static int test_get_rand_digit(void)
- {
- int ret = 0;
- #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
- WC_RNG rng;
- mp_digit d;
- printf(testingFmt, "get_rand_digit()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* End test_get_rand_digit*/
- /*
- * Testing get_digit_count
- */
- static int test_get_digit_count(void)
- {
- int ret = 0;
- #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
- mp_int a;
- printf(testingFmt, "get_digit_count()");
- if (mp_init(&a) != MP_OKAY) {
- ret = -1;
- }
- if (ret == 0) {
- ret = get_digit_count(NULL);
- }
- if (ret == 0) {
- ret = get_digit_count(&a);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- mp_clear(&a);
- #endif
- return ret;
- }/* End test_get_digit_count*/
- /*
- * Testing mp_cond_copy
- */
- static int test_mp_cond_copy(void)
- {
- int ret = 0;
- #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
- defined(WOLFSSL_PUBLIC_MP)
- mp_int a;
- mp_int b;
- int copy = 0;
- printf(testingFmt, "mp_cond_copy()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- mp_clear(&a);
- mp_clear(&b);
- #endif
- return ret;
- }/* End test_mp_cond_copy*/
- /*
- * Testing mp_rand
- */
- static int test_mp_rand(void)
- {
- int ret = 0;
- #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
- mp_int a;
- int digits = 1;
- WC_RNG rng;
- printf(testingFmt, "mp_rand()");
- 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);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- mp_clear(&a);
- wc_FreeRng(&rng);
- #endif
- return ret;
- }/* End test_mp_rand*/
- /*
- * Testing get_digit
- */
- static int test_get_digit(void)
- {
- int ret = 0;
- #if defined(WOLFSSL_PUBLIC_MP)
- mp_int a;
- int n = 0;
- printf(testingFmt, "get_digit()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- mp_clear(&a);
- #endif
- return ret;
- }/* End test_get_digit*/
- /*
- * Testing wc_export_int
- */
- static int test_wc_export_int(void)
- {
- int ret = 0;
- #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
- defined(WOLFSSL_PUBLIC_MP)
- mp_int mp;
- byte buf[32];
- word32 keySz = (word32)sizeof(buf);
- word32 len = (word32)sizeof(buf);
- printf(testingFmt, "wc_export_int()");
- 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;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- mp_clear(&mp);
- #endif
- return ret;
- }/* End test_wc_export_int*/
- static int test_wc_InitRngNonce(void)
- {
- int ret=0;
- #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
- (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
- 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);
- printf(testingFmt, "wc_InitRngNonce()");
- if (ret == 0){
- ret = wc_InitRngNonce(&rng, nonce, nonceSz);
- }
- wc_FreeRng(&rng);
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/* End test_wc_InitRngNonce*/
- /*
- * Testing wc_InitRngNonce_ex
- */
- static int test_wc_InitRngNonce_ex(void)
- {
- int ret=0;
- #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
- (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION >= 2))
- 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);
- printf(testingFmt, "wc_InitRngNonce_ex()");
- if (ret == 0){
- ret = wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId);
- }
- wc_FreeRng(&rng);
- printf(resultFmt, ret == 0 ? passed : failed);
- fflush(stdout);
- #endif
- return ret;
- }/*End test_wc_InitRngNonce_ex*/
- static int test_wolfSSL_X509_CRL(void)
- {
- #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;
- printf(testingFmt, "test_wolfSSL_X509_CRL");
- 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
- printf(resultFmt, passed);
- fflush(stdout);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_load_crl_file(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_load_crl_file");
- 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;
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_d2i_X509_REQ(void)
- {
- #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);
- }
- #endif /* !NO_DSA && !HAVE_SELFTEST */
- #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
- return 0;
- }
- static int test_wolfSSL_PEM_read_X509(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
- !defined(NO_RSA)
- X509 *x509 = NULL;
- XFILE fp;
- printf(testingFmt, "wolfSSL_PEM_read_X509");
- fp = XFOPEN(svrCertFile, "rb");
- AssertTrue((fp != XBADFILE));
- AssertNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
- X509_free(x509);
- XFCLOSE(fp);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_PEM_read(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_PEM_read");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
- {
- #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;
- printf(testingFmt, "wolfssl_EVP_aes_gcm_AAD_2_parts");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
- !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
- {
- /* 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);
- return 0;
- }
- #endif
- static int test_wolfssl_EVP_aes_gcm(void)
- {
- #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];
- printf(testingFmt, "wolfssl_EVP_aes_gcm");
- 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);
- }
- test_wolfssl_EVP_aes_gcm_zeroLen();
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
- return 0;
- }
- static int test_wolfssl_EVP_chacha20_poly1305(void)
- {
- #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;
- printf(testingFmt, "test_wolfssl_EVP_chacha20_poly1305");
- /* 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_PKEY_hkdf(void)
- {
- #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
- };
- printf(testingFmt, "test_wolfSSL_EVP_PKEY_hkdf");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && HAVE_HKDF */
- return 0;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
- {
- #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";
- printf(testingFmt, "wolfSSL_PEM_X509_INFO_read_bio");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #endif /* !NO_BIO */
- static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_NAME_ENTRY_get_object");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_INTEGER_get_set(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
- ASN1_INTEGER *a;
- long val;
- int ret;
- printf(testingFmt, "test_wolfSSL_ASN1_INTEGER_get_set");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #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]);
- printf(testingFmt, "test_wolfSSL_d2i_ASN1_INTEGER");
- /* 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);
- }
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_X509_STORE_get1_certs(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_X509_STORE_get1_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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
- return 0;
- }
- /* 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)
- {
- /* 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);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* WOLFSSL_WPAS && HAVE_ECC && USE_CERT_BUFFERS_256 */
- return 0;
- }
- static int test_wolfSSL_i2c_ASN1_INTEGER(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
- ASN1_INTEGER *a;
- unsigned char *pp,*tpp;
- int ret;
- printf(testingFmt, "wolfSSL_i2c_ASN1_INTEGER");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !NO_ASN */
- return 0;
- }
- #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 0;
- }
- #ifndef NO_BIO
- static int test_wolfSSL_X509_print(void)
- {
- #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
- printf(testingFmt, "wolfSSL_X509_print");
- 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)
- /* Will print IP address subject alt name. */
- AssertIntEQ(BIO_get_mem_data(bio, NULL), 3255);
- #else
- AssertIntEQ(BIO_get_mem_data(bio, NULL), 3233);
- #endif
- BIO_free(bio);
- AssertNotNull(bio = BIO_new_fd(STDOUT_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 stdout */
- #if !defined(NO_WOLFSSL_DIR)
- AssertIntEQ(X509_print(bio, x509), SSL_SUCCESS);
- #endif
- /* print again */
- AssertIntEQ(X509_print_fp(stdout, x509), SSL_SUCCESS);
- X509_free(x509);
- BIO_free(bio);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_X509_CRL_print(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
- && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
- X509_CRL* crl;
- BIO *bio;
- XFILE fp;
- printf(testingFmt, "test_X509_CRL_print");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_BIO_get_len(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
- BIO *bio = NULL;
- const char txt[] = "Some example text to push to the BIO.";
- printf(testingFmt, "wolfSSL_BIO_get_len");
- 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(STDOUT_FILENO, BIO_NOCLOSE));
- AssertIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
- BIO_free(bio);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_ASN1_STRING_print(void){
- #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;
- printf(testingFmt, "wolfSSL_ASN1_STRING_print()");
- /* 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS */
- return 0;
- }
- #endif /* !NO_BIO */
- static int test_wolfSSL_ASN1_get_object(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_ASN1_get_object()");
- /* 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
- return 0;
- }
- static int test_wolfSSL_RSA(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_RSA()");
- 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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_DER(void)
- {
- #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}
- };
- printf(testingFmt, "test_wolfSSL_RSA_DER()");
- 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_print(void)
- {
- #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)
- BIO *bio;
- WOLFSSL_RSA* rsa = NULL;
- printf(testingFmt, "wolfSSL_RSA_print");
- AssertNotNull(bio = BIO_new_fd(STDOUT_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(stdout, 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(stdout, 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(stdout, rsa, 0), 1);
- AssertIntEQ(RSA_print_fp(stdout, rsa, 4), 1);
- AssertIntEQ(RSA_print_fp(stdout, rsa, -1), 1);
- BIO_free(bio);
- RSA_free(rsa);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #ifndef NO_RSA
- static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
- {
- #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);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
- return 0;
- }
- #endif
- static int test_wolfSSL_RSA_sign_sha3(void)
- {
- #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
- };
- printf(testingFmt, "wolfSSL_RSA_sign_sha3");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
- #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
- return 0;
- }
- static int test_wolfSSL_RSA_get0_key(void)
- {
- #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
- printf(testingFmt, "test_wolfSSL_RSA_get0_key()");
- 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);
- }
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_meth(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA)
- RSA *rsa;
- RSA_METHOD *rsa_meth;
- printf(testingFmt, "test_wolfSSL_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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_verify(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_RSA_verify");
- /* 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);
- printf(resultFmt, passed);
- #endif
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_sign(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_RSA_sign");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_sign_ex(void)
- {
- #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,
- };
- printf(testingFmt, "wolfSSL_RSA_sign_ex");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_public_decrypt(void)
- {
- #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
- printf(testingFmt, "wolfSSL_RSA_public_decrypt");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_private_encrypt(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_RSA_private_encrypt");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_public_encrypt(void)
- {
- #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];
- printf(testingFmt, "wolfSSL_RSA_public_decrypt");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_private_decrypt(void)
- {
- #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 };
- printf(testingFmt, "wolfSSL_RSA_private_decrypt");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_GenAdd(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_RSA_GenAdd");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_blinding_on(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_RSA_blinding_on");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_RSA_ex_data(void)
- {
- #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
- RSA* rsa;
- unsigned char data[1];
- printf(testingFmt, "wolfSSL_RSA_ex_data");
- 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);
- printf(resultFmt, passed);
- #endif /* !NO_RSA && OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_RSA_LoadDer(void)
- {
- #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
- printf(testingFmt, "wolfSSL_RSA_LoadDer");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /* Local API. */
- static int test_wolfSSL_RSA_To_Der(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_RSA_To_Der");
- 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);
- printf(resultFmt, passed);
- #endif
- #endif
- return 0;
- }
- /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
- static int test_wolfSSL_PEM_read_RSAPublicKey(void)
- {
- #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
- !defined(NO_WOLFSSL_STUB)
- RSA* rsa = NULL;
- XFILE fp;
- printf(testingFmt, "wolfSSL_PEM_read_RSAPublicKey");
- fp = XFOPEN("./certs/client-keyPub.pem", "rb");
- AssertNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
- /* Valid but stub so returns NULL. */
- AssertNull(wolfSSL_PEM_read_RSAPublicKey(fp, NULL, NULL, NULL));
- /* Valid but stub so returns NULL. */
- AssertNull(wolfSSL_PEM_read_RSAPublicKey(fp, &rsa, NULL, NULL));
- XFCLOSE(fp);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
- static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
- {
- #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
- !defined(NO_WOLFSSL_STUB)
- RSA* rsa = NULL;
- printf(testingFmt, "wolfSSL_PEM_write_RSA_PUBKEY");
- AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
- AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stdout, NULL), 0);
- /* Valid but stub so returns 0. */
- AssertIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stdout, rsa), 0);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
- {
- #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
- printf(testingFmt, "wolfSSL_PEM_write_RSAPrivateKey");
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, 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(stdout, NULL, NULL, NULL, 0,
- NULL, NULL), 0);
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, NULL, NULL, 0,
- NULL, NULL), 1);
- #ifndef NO_AES
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
- NULL, 0, NULL, NULL), 1);
- AssertIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stdout, rsa, EVP_aes_128_cbc(),
- passwd, sizeof(passwd) - 1, NULL, NULL), 1);
- #endif
- RSA_free(rsa);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
- {
- #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;
- printf(testingFmt, "wolfSSL_PEM_write_mem_RSAPrivateKey");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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)
- {
- #if defined(OPENSSL_EXTRA)
- #if !defined(NO_RSA)
- {
- 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);
- }
- #endif /* !NO_RSA */
- #ifdef HAVE_ECC
- {
- 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);
- }
- #endif /* HAVE_ECC */
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_stubs_are_stubs(void)
- {
- #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;
- #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;
- #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB */
- return 0;
- }
- static int test_CONF_modules_xxx(void)
- {
- #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);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_CRYPTO_set_dynlock_xxx(void)
- {
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "CRYPTO_set_dynlock_xxx()");
- 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 */
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_CRYPTO_THREADID_xxx(void)
- {
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "CRYPTO_THREADID_xxx()");
- CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
- CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
- AssertIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_ENGINE_cleanup(void)
- {
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "ENGINE_cleanup()");
- ENGINE_cleanup();
- AssertTrue(1); /* to confirm previous call gives no harm */
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_CTX_LoadCRL(void)
- {
- #if defined(HAVE_CRL) && !defined(NO_RSA)
- 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);
- #endif
- return 0;
- }
- static int test_SetTmpEC_DHE_Sz(void)
- {
- #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);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_get0_privatekey(void)
- {
- #ifdef OPENSSL_ALL
- WOLFSSL_CTX* ctx = NULL;
- printf(testingFmt, "wolfSSL_CTX_get0_privatekey()");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_dtls_set_mtu(void)
- {
- #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
- 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);
- printf(testingFmt, "wolfSSL_dtls_set_mtu()");
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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';
- printf("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';
- printf("Server response: %s\n", reply);
- }
- static int test_wolfSSL_dtls_plaintext(void)
- {
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- callback_functions func_cb_client;
- callback_functions func_cb_server;
- THREAD_TYPE serverThread;
- 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},
- };
- printf(testingFmt, "test_wolfSSL_dtls_plaintext");
- for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
- 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;
- server_args.callbacks = &func_cb_server;
- client_args.signal = &ready;
- client_args.callbacks = &func_cb_client;
- 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;
- 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
- }
- printf(resultFmt, passed);
- return 0;
- }
- #else
- static int test_wolfSSL_dtls_plaintext(void) {
- return 0;
- }
- #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)
- {
- tcp_ready ready;
- func_args client_args;
- func_args server_args;
- callback_functions func_cb_client;
- callback_functions func_cb_server;
- THREAD_TYPE serverThread;
- 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
- };
- printf(testingFmt, "test_wolfSSL_dtls_fragments");
- for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
- 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;
- server_args.callbacks = &func_cb_server;
- client_args.signal = &ready;
- client_args.callbacks = &func_cb_client;
- 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;
- start_thread(test_server_nofail, &server_args, &serverThread);
- wait_tcp_ready(&server_args);
- test_client_nofail(&client_args, NULL);
- join_thread(serverThread);
- AssertFalse(client_args.return_code);
- AssertFalse(server_args.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);
- FreeTcpReady(&ready);
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- }
- printf(resultFmt, passed);
- return 0;
- }
- #else
- static int test_wolfSSL_dtls_fragments(void) {
- return 0;
- }
- #endif
- #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) {
- printf("loading cert %s failed\n", certA);
- printf("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) {
- printf("could not verify the cert: %s\n", certA);
- printf("Error: (%d): %s\n", ret, wolfSSL_ERR_reason_error_string(ret));
- return -1;
- } else {
- printf("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) {
- printf("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) {
- printf("cert manager new failed\n");
- return -1;
- }
- AssertIntLT(test_chainH(cm), 0);
- wolfSSL_CertManagerUnloadCAs(cm);
- wolfSSL_CertManagerFree(cm);
- if ((cm = wolfSSL_CertManagerNew()) == NULL) {
- printf("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) {
- printf("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) {
- printf("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) {
- printf("cert manager new failed\n");
- return -1;
- }
- AssertIntLT(test_chainJ(cm), 0);
- wolfSSL_CertManagerUnloadCAs(cm);
- wolfSSL_CertManagerFree(cm);
- if ((cm = wolfSSL_CertManagerNew()) == NULL) {
- printf("cert manager new failed\n");
- return -1;
- }
- ret = wolfSSL_CertManagerUnloadCAs(cm);
- wolfSSL_CertManagerFree(cm);
- if (ret == WOLFSSL_SUCCESS) {
- ret = 0;
- }
- return ret;
- }
- #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 0;
- }
- static void test_export_keying_material_ssl_cb(WOLFSSL* ssl)
- {
- wolfSSL_KeepArrays(ssl);
- }
- static int test_export_keying_material(void)
- {
- #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
- #endif /* !SINGLE_THREADED */
- return 0;
- }
- #endif /* HAVE_KEYING_MATERIAL */
- static int test_wolfSSL_THREADID_hash(void)
- {
- int ret = 0;
- unsigned long res;
- #if defined(OPENSSL_EXTRA)
- CRYPTO_THREADID id;
- printf(testingFmt, "wolfSSL_THREADID_hash");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- (void)res;
- return ret;
- }
- static int test_wolfSSL_CTX_set_ecdh_auto(void)
- {
- int ret = 0;
- WOLFSSL_CTX* ctx = NULL;
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "SSL_CTX_set_ecdh_auto");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- (void)ctx;
- return ret;
- }
- #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 ret = 0;
- #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;
- printf(testingFmt, "wolfSSL_read_detect_TCP_disconnect()");
- #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);
- printf(resultFmt, passed);
- #endif
- return ret;
- }
- static int test_wolfSSL_CTX_get_min_proto_version(void)
- {
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
- WOLFSSL_CTX *ctx;
- (void)ctx;
- printf(testingFmt, "wolfSSL_CTX_get_min_proto_version()");
- 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
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
- return 0;
- }
- static int test_wolfSSL_security_level(void)
- {
- #if defined(OPENSSL_EXTRA)
- SSL_CTX *ctx;
- printf(testingFmt, "test_wolfSSL_security_level()");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_SSL_in_init(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_BIO)
- SSL_CTX* ctx;
- SSL* ssl;
- const char* testCertFile;
- const char* testKeyFile;
- printf(testingFmt, "test_wolfSSL_SSL_in_init()");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EC_curve(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
- int nid = NID_secp160k1;
- const char* nid_name;
- printf(testingFmt, "test_wolfSSL_EC_curve()");
- AssertNotNull(nid_name = EC_curve_nid2nist(nid));
- AssertIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
- AssertIntEQ(EC_curve_nist2nid(nid_name), nid);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_CTX_set_timeout(void)
- {
- #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
- int timeout;
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- (void)timeout;
- printf(testingFmt, "test_wolfSSL_CTX_set_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);
- printf(resultFmt, passed);
- #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
- return 0;
- }
- static int test_wolfSSL_OpenSSL_version(void)
- {
- #if defined(OPENSSL_EXTRA)
- const char* ver;
- printf(testingFmt, "test_wolfSSL_OpenSSL_version()");
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_CONF_CTX_CMDLINE(void)
- {
- #if defined(OPENSSL_ALL)
- SSL_CTX* ctx = NULL;
- SSL_CONF_CTX* cctx = NULL;
- printf(testingFmt, "test_CONF_CTX_CMDLINE");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_CONF_CTX_FILE(void)
- {
- #if defined(OPENSSL_ALL)
- SSL_CTX* ctx = NULL;
- SSL_CONF_CTX* cctx = NULL;
- printf(testingFmt, "test_CONF_CTX_FILE");
- 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);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- return 0;
- }
- static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
- {
- #ifdef HAVE_EX_DATA
- int idx1,idx2;
- printf(testingFmt, "test_wolfSSL_CRYPTO_get_ex_new_index()");
- /* 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);
- printf(resultFmt, "passed");
- #endif /* HAVE_EX_DATA */
- return 0;
- }
- static int test_wolfSSL_set_psk_use_session_callback(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
- SSL_CTX* ctx;
- SSL* ssl;
- const char* testCertFile;
- const char* testKeyFile;
- printf(testingFmt, "test_wolfSSL_set_psk_use_session_callback()");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_DH(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
- DH *dh = NULL;
- BIGNUM* p;
- BIGNUM* q;
- BIGNUM* g;
- BIGNUM* pub;
- BIGNUM* priv;
- #if defined(OPENSSL_ALL) && defined(WOLFSSL_KEY_GEN)
- #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), WOLFSSL_SUCCESS);
- 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(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
- AssertPtrEq(pub, dh->pub_key);
- AssertPtrEq(priv, dh->priv_key);
- AssertNotNull(pub = BN_new());
- AssertNotNull(priv = BN_new());
- AssertIntEQ(DH_set0_key(dh, pub, priv), 1);
- AssertPtrEq(pub, dh->pub_key);
- AssertPtrEq(priv, dh->priv_key);
- DH_free(dh);
- AssertNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
- DH_free(dh);
- #endif
- #endif
- (void)dh;
- (void)p;
- (void)q;
- (void)g;
- (void)pub;
- (void)priv;
- printf(testingFmt, "test_wolfSSL_DH");
- 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);
- /* 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 */
- #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))*/
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !NO_DH */
- return 0;
- }
- static int test_wolfSSL_ERR_strings(void)
- {
- const char* err1 = "unsupported cipher suite";
- const char* err2 = "wolfSSL PEM routines";
- const char* err = NULL;
- (void)err;
- (void)err1;
- (void)err2;
- #if !defined(NO_ERROR_STRINGS)
- printf(testingFmt, "test_wolfSSL_ERR_strings");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_shake128(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
- defined(WOLFSSL_SHAKE128)
- printf(testingFmt, "test_wolfSSL_EVP_shake128");
- const EVP_MD* md = NULL;
- md = EVP_shake128();
- AssertTrue(md != NULL);
- AssertIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_EVP_shake256(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
- defined(WOLFSSL_SHAKE256)
- const EVP_MD* md = NULL;
- printf(testingFmt, "test_wolfSSL_EVP_shake256");
- md = EVP_shake256();
- AssertTrue(md != NULL);
- AssertIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_EVP_blake2(void)
- {
- #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
- const EVP_MD* md = NULL;
- (void)md;
- printf(testingFmt, "test_EVP_blake2");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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 stdout */
- 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)
- {
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "test_EVP_MD_do_all");
- EVP_MD_do_all(NULL, stdout);
- /* to confirm previous call gives no harm */
- AssertTrue(1);
- EVP_MD_do_all(list_md_fn, stdout);
- /* to confirm previous call gives no harm */
- AssertTrue(1);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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 stdout */
- 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)
- {
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "test_OBJ_NAME_do_all");
- 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, stdout);
- /* to confirm previous call gives no harm */
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stdout);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stdout);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stdout);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stdout);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stdout);
- AssertTrue(1);
- OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stdout);
- AssertTrue(1);
- OBJ_NAME_do_all(-1, obj_name_t, stdout);
- AssertTrue(1);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_SSL_CIPHER_get_xxx(void)
- {
- #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;
- printf(testingFmt, "test_SSL_CIPHER_get_xxx");
- #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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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
- printf("%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
- printf("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
- printf("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 */
- printf("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
- printf("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
- printf("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
- printf("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 */
- printf("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
- printf("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
- printf("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 */
- printf("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
- printf("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)
- {
- #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 */
- #endif /* WOLF_CRYPTO_CB */
- return 0;
- }
- #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 0;
- }
- #endif /* WOLFSSL_STATIC_MEMORY */
- static int test_wolfSSL_CTX_StaticMemory(void)
- {
- #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];
- printf(testingFmt, "test_wolfSSL_CTX_StaticMemory()");
- #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 */
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_openssl_FIPS_drbg(void)
- {
- #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;
- }
- printf(testingFmt, "test_openssl_FIPS_drbg()");
- 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);
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- static int test_wolfSSL_FIPS_mode(void)
- {
- #if defined(OPENSSL_ALL)
- printf(testingFmt, "test_wolfSSL_FIPS_mode()");
- #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
- printf(resultFmt, passed);
- #endif
- return 0;
- }
- #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))
- printf("1");
- else
- printf("0");
- }
- printf(" ");
- }
- printf("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;
- printf(testingFmt, "wolfSSL_DtlsUpdateWindow()");
- #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
- printf("\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);
- printf(resultFmt, passed);
- fflush(stdout);
- return 0;
- }
- #endif /* WOLFSSL_DTLS */
- /*----------------------------------------------------------------------------*
- | Main
- *----------------------------------------------------------------------------*/
- typedef int (*TEST_FUNC)(void);
- typedef struct {
- const char *name;
- TEST_FUNC func;
- } TEST_CASE;
- #define TEST_DECL(func) { #func, func }
- 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))
- 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_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_CertManagerCheckOCSPResponse),
- TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
- 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_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),
- #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
- TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
- #endif
- TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient),
- 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
- TEST_DECL(test_wolfSSL_UseSNI),
- #endif
- TEST_DECL(test_wolfSSL_UseTrustedCA),
- TEST_DECL(test_wolfSSL_UseMaxFragment),
- TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
- TEST_DECL(test_wolfSSL_UseSupportedCurve),
- TEST_DECL(test_wolfSSL_UseALPN),
- TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
- TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
- /* 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),
- TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
- TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
- TEST_DECL(test_wolfSSL_PEM_PrivateKey),
- #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_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_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_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_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_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_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_chacha20_poly1305),
- 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_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_d2i_DHparams),
- TEST_DECL(test_wolfSSL_i2d_DHparams),
- 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_DH_check),
- 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_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_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 */
- /* 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),
- #endif
- #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
- !defined(WOLFSSL_NO_CLIENT_AUTH))
- /* 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),
- 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),
- TEST_DECL(test_wolfSSL_DH),
- /* 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),
- #endif
- TEST_DECL(test_ForceZero),
- TEST_DECL(test_wolfSSL_Cleanup),
- #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 */
- }
- void ApiTest(void)
- {
- int i;
- int ret;
- printf(" Begin API Tests\n");
- fflush(stdout);
- for (i = 0; i < TEST_CASE_CNT; ++i) {
- TestSetup();
- ret = testCases[i].func();
- if (ret != 0) {
- fprintf(stderr, "%s failed.\n", testCases[i].name);
- }
- AssertIntEQ(ret, 0);
- 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);
- }
|