123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984 |
- /* api.c API unit tests
- *
- * Copyright (C) 2006-2021 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>
- #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
- #ifdef HAVE_IDEA
- #include <wolfssl/wolfcrypt/idea.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_RABBIT
- #include <wolfssl/wolfcrypt/rabbit.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_HC128
- #include <wolfssl/wolfcrypt/hc128.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
- #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/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/crypto.h>
- #include <wolfssl/openssl/hmac.h>
- #include <wolfssl/openssl/objects.h>
- #include <wolfssl/openssl/rand.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)
- #include "wolfssl/internal.h" /* for testing SSL_get_peer_cert_chain */
- #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)
- #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 devId = WOLFSSL_CAAM_DEVID;
- #else
- static int devId = 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);
- return result;
- }
- static int test_wolfSSL_Cleanup(void)
- {
- int result;
- printf(testingFmt, "wolfSSL_Cleanup()");
- result = wolfSSL_Cleanup();
- printf(resultFmt, result == WOLFSSL_SUCCESS ? passed : failed);
- 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 */
- /*----------------------------------------------------------------------------*
- | 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,
- ntruCertFile, ntruKeyFile, 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 WOLFSSL_SUCCESS;
- }
- /*----------------------------------------------------------------------------*
- | Method Allocators
- *----------------------------------------------------------------------------*/
- static void 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 */
- }
- /*----------------------------------------------------------------------------*
- | Context
- *----------------------------------------------------------------------------*/
- #ifndef NO_WOLFSSL_SERVER
- static void test_wolfSSL_CTX_new(WOLFSSL_METHOD *method)
- {
- WOLFSSL_CTX *ctx;
- AssertNull(ctx = wolfSSL_CTX_new(NULL));
- AssertNotNull(method);
- AssertNotNull(ctx = wolfSSL_CTX_new(method));
- wolfSSL_CTX_free(ctx);
- }
- #endif
- #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
- (!defined(NO_RSA) || defined(HAVE_ECC))
- static void 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:HC128-MD5:HC128-SHA:RABBIT-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:IDEA-CBC-SHA: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";
- #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));
- 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));
- wolfSSL_CTX_free(ctx);
- wolfSSL_free(ssl);
- }
- }
- #endif
- static void 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
- }
- #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);
- return ret;
- #else
- return WOLFSSL_SUCCESS;
- #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);
- return ret;
- #else
- return WOLFSSL_SUCCESS;
- #endif
- } /*END test_wolfSSL_CTX_use_certificate_buffer*/
- static void 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
- }
- /* test both file and buffer versions along with unloading trusted peer certs */
- static void test_wolfSSL_CTX_trust_peer_cert(void)
- {
- #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
- !defined(NO_WOLFSSL_CLIENT)
- WOLFSSL_CTX *ctx;
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #if !defined(NO_FILESYSTEM)
- /* invalid file */
- assert(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
- assert(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
- WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS);
- assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
- WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
- /* success */
- assert(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile, WOLFSSL_FILETYPE_PEM)
- == WOLFSSL_SUCCESS);
- /* unload cert */
- assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
- assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
- #endif
- /* Test of loading certs from buffers */
- /* invalid buffer */
- assert(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
- WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS);
- /* success */
- #ifdef USE_CERT_BUFFERS_1024
- assert(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
- assert(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
- sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1) == WOLFSSL_SUCCESS);
- #endif
- /* unload cert */
- assert(wolfSSL_CTX_Unload_trust_peers(NULL) != WOLFSSL_SUCCESS);
- assert(wolfSSL_CTX_Unload_trust_peers(ctx) == WOLFSSL_SUCCESS);
- wolfSSL_CTX_free(ctx);
- #endif
- }
- static void 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)
- /* 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
- }
- #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 == 0) {
- /* test including null terminator in length */
- ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1, WOLFSSL_FILETYPE_PEM);
- }
- #if defined(WOLFSSL_PEM_TO_DER)
- if (ret == 0) {
- /* 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_CertManagerLoadCABuffer(void)
- {
- int ret = 0;
- #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);
- #ifdef NO_RSA
- AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
- #else
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- #endif
- ret = test_cm_load_ca_file(ca_expired_cert);
- #ifdef NO_RSA
- AssertIntEQ(ret, ASN_UNKNOWN_OID_E);
- #else
- AssertIntEQ(ret, ASN_AFTER_DATE_E);
- #endif
- #endif
- return ret;
- }
- static void 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;
- 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));
- 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_free(sk);
- wolfSSL_CertManagerFree(cm);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
- defined(WOLFSSL_SIGNER_DER_CERT) */
- }
- static int test_wolfSSL_CertManagerSetVerify(void)
- {
- int ret = 0;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
- (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
- 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);
- AssertIntEQ(ret, WOLFSSL_SUCCESS);
- /* 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 ret;
- }
- static void 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)
- WOLFSSL_CERT_MANAGER* cm;
- const char* ca_cert = "./certs/test/cert-ext-nc.der";
- 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;
- wc_InitRng(&rng);
- /* load in CA private key for signing */
- AssertIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, devId), 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);
- #endif
- }
- static void 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);
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- 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);
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- 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);
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- AssertNotNull((der = 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);
- /* 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);
- wolfSSL_X509_sign(x509, priv, EVP_sha256());
- AssertNotNull((der = 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
- }
- static void 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
- }
- static void 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 */
- AssertTrue(WOLFSSL_SUCCESS !=
- wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
- WOLFSSL_LOAD_FLAG_NONE));
- AssertTrue(WOLFSSL_SUCCESS ==
- wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
- WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY));
- wolfSSL_CTX_free(ctx);
- #endif
- }
- static void test_wolfSSL_CTX_load_verify_buffer_ex(void)
- {
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- defined(USE_CERT_BUFFERS_2048)
- 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 */
- AssertTrue(WOLFSSL_SUCCESS !=
- wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
- sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
- WOLFSSL_LOAD_FLAG_NONE));
- /* test expired CA success */
- AssertTrue(WOLFSSL_SUCCESS ==
- 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_CTX_free(ctx);
- #endif
- }
- static void 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)
- 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
- }
- static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
- {
- int ret = 0;
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
- const char* server_chain_der = "./certs/server-cert-chain.der";
- WOLFSSL_CTX* 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);
- wolfSSL_CTX_free(ctx);
- #endif
- return ret;
- }
- static void test_wolfSSL_CTX_SetTmpDH_file(void)
- {
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH)
- 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
- /* 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
- }
- static void test_wolfSSL_CTX_SetTmpDH_buffer(void)
- {
- #if !defined(NO_CERTS) && !defined(NO_DH)
- 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
- /* 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
- }
- static void test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
- {
- #if !defined(NO_CERTS) && !defined(NO_DH)
- WOLFSSL_CTX *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
- }
- static void test_wolfSSL_CTX_der_load_verify_locations(void)
- {
- #ifdef WOLFSSL_DER_LOAD
- 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
- }
- static void 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;
- #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 */
- }
- static void 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 */
- }
- /*----------------------------------------------------------------------------*
- | SSL
- *----------------------------------------------------------------------------*/
- static void 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));
- #ifndef WOLFSSL_SESSION_EXPORT
- 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
- }
- static void 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
- }
- static void 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
- }
- static void 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
- }
- static void 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
- }
- /* 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
- 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
- */
- # if defined(OPENSSL_EXTRA) && \
- (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
- static void test_wolfSSL_EC(void)
- {
- #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 && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
- #ifndef NO_BIO
- static void 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 */
- }
- #endif /* !NO_BIO */
- # if defined(OPENSSL_EXTRA)
- static void 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 */
- }
- static void 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);
- EC_KEY_free(key);
- EC_KEY_free(copy);
- #endif /* HAVE_ECC */
- }
- static void 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_BUFSIZE];
- 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);
- EC_KEY_free(key);
- #endif /* HAVE_ECC && !NO_ECC256 && !NO_ECC_SECP */
- }
- static void test_ED25519(void)
- {
- #if defined(HAVE_ED25519) && 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);
- 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);
- AssertIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(privSz, ED25519_PRV_KEY_SIZE);
- AssertIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
- AssertIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
- &sigSz), WOLFSSL_SUCCESS);
- AssertIntEQ(sigSz, ED25519_SIG_SIZE);
- AssertIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
- sigSz), WOLFSSL_SUCCESS);
- #endif /* HAVE_ED25519 && WOLFSSL_KEY_GEN */
- }
- static void test_ED448(void)
- {
- #if defined(HAVE_ED448) && 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);
- 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);
- AssertIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz),
- WOLFSSL_SUCCESS);
- AssertIntEQ(privSz, ED448_PRV_KEY_SIZE);
- AssertIntEQ(pubSz, ED448_PUB_KEY_SIZE);
- AssertIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
- &sigSz), WOLFSSL_SUCCESS);
- AssertIntEQ(sigSz, ED448_SIG_SIZE);
- AssertIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
- sigSz), WOLFSSL_SUCCESS);
- #endif /* HAVE_ED448 && WOLFSSL_KEY_GEN */
- }
- #endif /* OPENSSL_EXTRA */
- #include <wolfssl/openssl/pem.h>
- /*----------------------------------------------------------------------------*
- | EVP
- *----------------------------------------------------------------------------*/
- /* Test function for wolfSSL_EVP_get_cipherbynid.
- */
- #ifdef OPENSSL_EXTRA
- static void 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_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_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_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 */
- #ifdef HAVE_IDEA
- AssertNotNull(strcmp("EVP_IDEA_CBC", wolfSSL_EVP_get_cipherbynid(34)));
- #endif
- /* test for nid is out of range */
- AssertNull(wolfSSL_EVP_get_cipherbynid(1));
- }
- static void 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);
- #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
- }
- #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
- /* set up function for sending session information */
- static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
- {
- WOLFSSL_CTX* ctx;
- WOLFSSL* ssl;
- 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 WOLFSSL_SUCCESS;
- }
- /* 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;
- 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
- err = 0; /* Reset error */
- ret = wolfSSL_accept(ssl);
- if (ret != WOLFSSL_SUCCESS) {
- 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 (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));
- 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(HAVE_AESGCM) && \
- defined(OPENSSL_EXTRA)
- 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
- 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;
- char msg[] = "I hear you fa shizzle!";
- char input[1024];
- int idx;
- 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_server_method();
- }
- ctx = wolfSSL_CTX_new(method);
- }
- if (ctx == NULL) {
- goto done;
- }
- #if defined(HAVE_SESSION_TICKET) && \
- ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
- #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
- 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 = ((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);
- 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;
- }
- #ifdef WOLFSSL_SESSION_EXPORT
- /* only add in more complex nonblocking case with session export tests */
- if (args && ((func_args*)args)->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 (((func_args*)args)->argc > 0) {
- ret = nonblocking_accept_read(args, ssl, &clientfd);
- if (ret >= 0) {
- ((func_args*)args)->return_code = TEST_SUCCESS;
- }
- #ifdef WOLFSSL_TIRTOS
- Task_yield();
- #endif
- goto done;
- }
- #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
- 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));
- /*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
- }
- #ifdef WOLFSSL_TIRTOS
- Task_yield();
- #endif
- ((func_args*)args)->return_code = TEST_SUCCESS;
- done:
- 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 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
- }
- #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);
- CloseSocket(sockfd);
- if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
- /*err_sys("SSL_set_fd failed");*/
- goto done;
- }
- 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
- 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));
- /*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 void test_client_nofail(void* args, void *cb)
- {
- 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;
- 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);
- }
- #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 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 (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);
- }
- 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
- 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);
- 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)
- ((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
- #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
- && defined(HAVE_THREAD_LS)
- wc_ecc_fp_free(); /* free per thread cache */
- #endif
- return;
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
- 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);
- }
- 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
- 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);
- 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_get_session(ssl);
- if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
- /* err_sys ("SSL_clear failed"); */
- goto done;
- }
- wolfSSL_set_session(ssl, session);
- /* 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;
- }
- 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
- 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);
- 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) */
- /* SNI / ALPN / session export helper functions */
- #if defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLFSSL_SESSION_EXPORT)
- static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(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;
- #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
- #ifdef WOLFSSL_SESSION_EXPORT
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_dtls_set_export(ctx, test_export));
- #endif
- 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);
- 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);
- 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);
- CloseSocket(sfd);
- }
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
- #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);
- 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
- 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));
- /*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)
- 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
- }
- static void run_wolfssl_client(void* args)
- {
- callback_functions* callbacks = ((func_args*)args)->callbacks;
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(callbacks->method());
- 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 = 0;
- #ifdef WOLFSSL_TIRTOS
- fdOpenSession(Task_self());
- #endif
- ((func_args*)args)->return_code = TEST_FAIL;
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- #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));
- 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->ssl_ready)
- callbacks->ssl_ready(ssl);
- 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
- 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);
- 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 {
- AssertIntEQ(len, wolfSSL_write(ssl, msg, len));
- if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
- input[idx] = 0;
- 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 /* defined(HAVE_SNI) || defined(HAVE_ALPN) ||
- defined(WOLFSSL_SESSION_EXPORT) */
- static void 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
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
- static void 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
- }
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
- #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, 0x87, 0x00, 0x3b, 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, 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 void 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;
- struct sockaddr_in peerAddr;
- 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_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, 0, 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));
- peerAddr.sin_family = AF_INET;
- peerAddr.sin_port = XHTONS(server_args.signal->port);
- wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr));
- 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_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
- }
- /*----------------------------------------------------------------------------*
- | TLS extensions tests
- *----------------------------------------------------------------------------*/
- #if defined(HAVE_SNI) || defined(HAVE_ALPN)
- /* connection test runner */
- 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
- }
- #endif /* defined(HAVE_SNI) || defined(HAVE_ALPN) */
- #ifdef HAVE_SNI
- static void test_wolfSSL_UseSNI_params(void)
- {
- 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);
- }
- /* 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;
- char* request = (char*) &type; /* to be overwritten */
- AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
- AssertNotNull(request);
- AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
- AssertNull(request);
- }
- static void verify_SNI_real_matching(WOLFSSL* ssl)
- {
- byte type = WOLFSSL_SNI_HOST_NAME;
- char* request = NULL;
- AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
- AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
- AssertNotNull(request);
- AssertStrEQ("www.wolfssl.com", request);
- }
- static void verify_SNI_fake_matching(WOLFSSL* ssl)
- {
- byte type = WOLFSSL_SNI_HOST_NAME;
- char* request = NULL;
- AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
- AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, (void**) &request));
- AssertNotNull(request);
- AssertStrEQ("ww2.wolfssl.com", request);
- }
- static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
- {
- AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
- }
- /* END of connection tests callbacks */
- static void test_wolfSSL_UseSNI_connection(void)
- {
- unsigned long i;
- callback_functions callbacks[] = {
- /* success case at ctx */
- {0, use_SNI_at_ctx, 0, 0, 0, 0},
- {0, use_SNI_at_ctx, 0, verify_SNI_real_matching, 0, 0},
- /* success case at ssl */
- {0, 0, use_SNI_at_ssl, verify_SNI_real_matching, 0, 0},
- {0, 0, use_SNI_at_ssl, verify_SNI_real_matching, 0, 0},
- /* default mismatch behavior */
- {0, 0, different_SNI_at_ssl, verify_FATAL_ERROR_on_client, 0, 0},
- {0, 0, use_SNI_at_ssl, verify_UNKNOWN_SNI_on_server, 0, 0},
- /* continue on mismatch */
- {0, 0, different_SNI_at_ssl, 0, 0, 0},
- {0, 0, use_SNI_WITH_CONTINUE_at_ssl, verify_SNI_no_matching, 0, 0},
- /* fake answer on mismatch */
- {0, 0, different_SNI_at_ssl, 0, 0, 0},
- {0, 0, use_SNI_WITH_FAKE_ANSWER_at_ssl, verify_SNI_fake_matching, 0, 0},
- /* sni abort - success */
- {0, use_SNI_at_ctx, 0, 0, 0, 0},
- {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_real_matching, 0, 0},
- /* sni abort - abort when absent (ctx) */
- {0, 0, 0, verify_FATAL_ERROR_on_client, 0, 0},
- {0, use_MANDATORY_SNI_at_ctx, 0, verify_SNI_ABSENT_on_server, 0, 0},
- /* sni abort - abort when absent (ssl) */
- {0, 0, 0, verify_FATAL_ERROR_on_client, 0, 0},
- {0, 0, use_MANDATORY_SNI_at_ssl, verify_SNI_ABSENT_on_server, 0, 0},
- /* sni abort - success when overwritten */
- {0, 0, 0, 0, 0, 0},
- {0, use_MANDATORY_SNI_at_ctx, use_SNI_at_ssl, verify_SNI_no_matching, 0, 0},
- /* sni abort - success when allowing mismatches */
- {0, 0, different_SNI_at_ssl, 0, 0, 0},
- {0, use_PSEUDO_MANDATORY_SNI_at_ctx, 0, verify_SNI_fake_matching, 0, 0},
- };
- for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
- callbacks[i ].method = wolfSSLv23_client_method;
- callbacks[i + 1].method = wolfSSLv23_server_method;
- test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
- }
- }
- static void 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));
- }
- #endif /* HAVE_SNI */
- static void test_wolfSSL_UseSNI(void)
- {
- #ifdef HAVE_SNI
- test_wolfSSL_UseSNI_params();
- test_wolfSSL_UseSNI_connection();
- test_wolfSSL_SNI_GetFromBuffer();
- #endif
- }
- #endif /* HAVE_IO_TESTS_DEPENDENCIES */
- static void test_wolfSSL_UseTrustedCA(void)
- {
- #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
- 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, SSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_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 /* HAVE_TRUSTED_CA */
- }
- static void test_wolfSSL_UseMaxFragment(void)
- {
- #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
- #ifndef NO_WOLFSSL_SERVER
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- #else
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- #endif
- WOLFSSL *ssl = wolfSSL_new(ctx);
- AssertNotNull(ctx);
- AssertNotNull(ssl);
- /* error cases */
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
- AssertIntNE(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
- /* success case */
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
- AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
- wolfSSL_free(ssl);
- wolfSSL_CTX_free(ctx);
- #endif
- }
- static void test_wolfSSL_UseTruncatedHMAC(void)
- {
- #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
- #ifndef NO_WOLFSSL_SERVER
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
- AssertTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
- #else
- WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
- #endif
- WOLFSSL *ssl = wolfSSL_new(ctx);
- AssertNotNull(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
- }
- static void 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
- }
- #if defined(HAVE_ALPN) && !defined(NO_WOLFSSL_SERVER) && \
- 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 void test_wolfSSL_UseALPN_connection(void)
- {
- unsigned long i;
- callback_functions callbacks[] = {
- /* success case same list */
- {0, 0, use_ALPN_all, 0, 0, 0},
- {0, 0, use_ALPN_all, verify_ALPN_matching_http1, 0, 0},
- /* success case only one for server */
- {0, 0, use_ALPN_all, 0, 0, 0},
- {0, 0, use_ALPN_one, verify_ALPN_matching_spdy2, 0, 0},
- /* success case only one for client */
- {0, 0, use_ALPN_one, 0, 0, 0},
- {0, 0, use_ALPN_all, verify_ALPN_matching_spdy2, 0, 0},
- /* success case none for client */
- {0, 0, 0, 0, 0, 0},
- {0, 0, use_ALPN_all, 0, 0, 0},
- /* success case mismatch behavior but option 'continue' set */
- {0, 0, use_ALPN_all_continue, verify_ALPN_not_matching_continue, 0, 0},
- {0, 0, use_ALPN_unknown_continue, 0, 0, 0},
- /* success case read protocol send by client */
- {0, 0, use_ALPN_all, 0, 0, 0},
- {0, 0, use_ALPN_one, verify_ALPN_client_list, 0, 0},
- /* mismatch behavior with same list
- * the first and only this one must be taken */
- {0, 0, use_ALPN_all, 0, 0, 0},
- {0, 0, use_ALPN_all, verify_ALPN_not_matching_spdy3, 0, 0},
- /* default mismatch behavior */
- {0, 0, use_ALPN_all, 0, 0, 0},
- {0, 0, use_ALPN_unknown, verify_ALPN_FATAL_ERROR_on_client, 0, 0},
- };
- for (i = 0; i < sizeof(callbacks) / sizeof(callback_functions); i += 2) {
- callbacks[i ].method = wolfSSLv23_client_method;
- callbacks[i + 1].method = wolfSSLv23_server_method;
- test_wolfSSL_client_server(&callbacks[i], &callbacks[i + 1]);
- }
- }
- static void 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
- }
- #endif /* HAVE_ALPN */
- static void 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
- }
- static void 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
- }
- static void 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
- }
- /*----------------------------------------------------------------------------*
- | X509 Tests
- *----------------------------------------------------------------------------*/
- static void 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 */
- }
- /* Testing functions dealing with PKCS12 parsing out X509 certs */
- static void 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)
- byte buffer[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";
- #ifdef HAVE_ECC
- WOLFSSL_X509_NAME* subject;
- WOLFSSL_X509 *x509;
- #endif
- XFILE f;
- int bytes, ret;
- 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(buffer, 1, sizeof(buffer), f);
- XFCLOSE(f);
- bio = BIO_new_mem_buf((void*)buffer, bytes);
- AssertNotNull(bio);
- pkcs12 = d2i_PKCS12_bio(bio, NULL);
- AssertNotNull(pkcs12);
- PKCS12_free(pkcs12);
- d2i_PKCS12_bio(bio, &pkcs12);
- AssertNotNull(pkcs12);
- BIO_free(bio);
- /* 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(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 = sk_X509_dup(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
- /* 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_free(ca);
- 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_free(ca);
- /* 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_free(ca);
- 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_free(ca);
- #ifdef HAVE_ECC
- /* test order of parsing */
- f = XFOPEN(order, "rb");
- AssertTrue(f != XBADFILE);
- bytes = (int)XFREAD(buffer, 1, sizeof(buffer), f);
- XFCLOSE(f);
- AssertNotNull(bio = BIO_new_mem_buf((void*)buffer, bytes));
- AssertNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
- AssertIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
- WOLFSSL_SUCCESS);
- 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_free(ca); /* 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_free(ca);
- 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(buffer, 1, sizeof(buffer), f);
- XFCLOSE(f);
- AssertNotNull(bio = BIO_new_mem_buf((void*)buffer, 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_free(ca);
- 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 */
- }
- #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 void 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
- }
- #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 void test_wolfSSL_PKCS8(void)
- {
- #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8)
- 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;
- #ifdef HAVE_ECC
- 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
- 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);
- /* 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);
- /* 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_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
- }
- static void test_wolfSSL_PKCS8_ED25519(void)
- {
- #if !defined(NO_ASN) && defined(HAVE_PKCS8) && \
- defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519)
- 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);
- #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
- }
- static void test_wolfSSL_PKCS8_ED448(void)
- {
- #if !defined(NO_ASN) && defined(HAVE_PKCS8) && \
- defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448)
- 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);
- #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
- }
- /* Testing functions dealing with PKCS5 */
- static void 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) */
- }
- /* test parsing URI from certificate */
- static void 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);
- #ifndef IGNORE_NAME_CONSTRAINTS
- AssertNull(x509);
- #else
- AssertNotNull(x509);
- #endif
- printf(resultFmt, passed);
- #endif
- }
- static void 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, 981);
- wolfSSL_FreeX509(x509);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- #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)
- /* 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)
- {
- AssertIntEQ(store->error, ASN_VERSION_E);
- 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 void 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)
- 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);
- AssertIntEQ(client_args.return_code, TEST_FAIL);
- AssertIntEQ(server_args.return_code, TEST_FAIL);
- 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
- }
- /* 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
- 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);
- return ret;
- #else
- return WOLFSSL_SUCCESS;
- #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);
- return ret;
- #else
- return WOLFSSL_SUCCESS;
- #endif
- } /*END test_wolfSSL_UseOCSPStaplingV2*/
- /*----------------------------------------------------------------------------*
- | Multicast Tests
- *----------------------------------------------------------------------------*/
- static void 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) */
- }
- /*----------------------------------------------------------------------------*
- | Wolfcrypt
- *----------------------------------------------------------------------------*/
- /*
- * Unit test for the wc_InitBlake2b()
- */
- static int test_wc_InitBlake2b (void)
- {
- int ret = 0;
- #ifdef HAVE_BLAKE2
- Blake2b blake2b;
- printf(testingFmt, "wc_InitBlake2B()");
- /* Test good arg. */
- ret = wc_InitBlake2b(&blake2b, 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(&blake2b, 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(&blake2b, 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 blake2b;
- word32 digestSz = BLAKE2B_KEYBYTES;
- byte key[BLAKE2B_KEYBYTES];
- word32 keylen = BLAKE2B_KEYBYTES;
- printf(testingFmt, "wc_InitBlake2b_WithKey()");
- /* Test good arg. */
- ret = wc_InitBlake2b_WithKey(&blake2b, 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(&blake2b, digestSz, key, 256);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_InitBlake2b_WithKey(&blake2b, 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 blake2s;
- word32 digestSz = BLAKE2S_KEYBYTES;
- byte *key = (byte*)"01234567890123456789012345678901";
- word32 keylen = BLAKE2S_KEYBYTES;
- printf(testingFmt, "wc_InitBlake2s_WithKey()");
- /* Test good arg. */
- ret = wc_InitBlake2s_WithKey(&blake2s, 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(&blake2s, digestSz, key, 256);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_InitBlake2s_WithKey(&blake2s, 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, (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, (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) || defined(WOLF_CRYPTO_CB))
- 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 arguements*/
- 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 arguements*/
- 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, (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 */
- /*
- * 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) || defined(WOLF_CRYPTO_CB))
- 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)))
- 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 */
- /*
- * 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 arguements*/
- 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 arguements*/
- 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 */
- /*
- * 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, (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) || defined(WOLF_CRYPTO_CB))
- 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)))
- 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 arguements*/
- 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 arguements*/
- 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, (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) || defined(WOLF_CRYPTO_CB))
- 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) || defined(WOLF_CRYPTO_CB))
- 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 arguements*/
- 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 arguements*/
- 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, devId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitSha3_224(NULL, HEAP_HINT, devId);
- 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, devId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitSha3_256(NULL, HEAP_HINT, devId);
- 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, devId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitSha3_384(NULL, HEAP_HINT, devId);
- 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, devId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitSha3_512(NULL, HEAP_HINT, devId);
- 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, devId);
- 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, devId)) {
- 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, devId);
- 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, devId)) {
- 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, devId);
- 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, devId)) {
- 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, devId);
- 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, devId)) {
- 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, devId);
- 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, devId);
- 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, devId);
- 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, devId);
- 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, devId);
- 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, devId);
- 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, devId);
- 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, devId);
- 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, devId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitSha3_224(&sha3Cpy, HEAP_HINT, devId);
- 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, devId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitSha3_256(&sha3Cpy, HEAP_HINT, devId);
- 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, devId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitSha3_384(&sha3Cpy, HEAP_HINT, devId);
- 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, devId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitSha3_512(&sha3Cpy, HEAP_HINT, devId);
- 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) || defined(WOLF_CRYPTO_CB))
- wc_Sha3 sha3;
- word32 flags = 0;
- printf(testingFmt, "wc_Sha3_GetFlags()");
- /* Initialize */
- ret = wc_InitSha3_224(&sha3, HEAP_HINT, devId);
- 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;
- #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_SHAKE256)
- wc_Shake shake;
- printf(testingFmt, "wc_InitShake256()");
- ret = wc_InitShake256(&shake, HEAP_HINT, devId);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitShake256(NULL, HEAP_HINT, devId);
- 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;
- #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_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, devId);
- 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, devId)) {
- 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 && !WOLFSSL_NO_SHAKE256 */
- return ret;
- }
- static int test_wc_Shake256_Final (void)
- {
- int ret = 0;
- #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_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, devId);
- 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;
- #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_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, devId);
- if (ret != 0) {
- return ret;
- }
- ret = wc_InitShake256(&shakeCpy, HEAP_HINT, devId);
- 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;
- #if defined(WOLFSSL_SHAKE256) && !defined(WOLFSSL_NO_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 */
- /*
- * unit test for wc_IdeaSetKey()
- */
- static int test_wc_IdeaSetKey (void)
- {
- int ret = 0;
- #ifdef HAVE_IDEA
- Idea idea;
- const byte key[] =
- {
- 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
- 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
- };
- int flag = 0;
- printf(testingFmt, "wc_IdeaSetKey()");
- /*IV can be NULL, default value is 0*/
- ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
- if (ret == 0) {
- ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
- }
- /* Bad args. */
- if (ret == 0) {
- ret = wc_IdeaSetKey(NULL, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
- if (ret != BAD_FUNC_ARG) {
- flag = 1;
- }
- ret = wc_IdeaSetKey(&idea, NULL, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
- if (ret != BAD_FUNC_ARG) {
- flag = 1;
- }
- ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE - 1,
- NULL, IDEA_ENCRYPTION);
- if (ret != BAD_FUNC_ARG) {
- flag = 1;
- }
- ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, -1);
- if (ret != BAD_FUNC_ARG) {
- flag = 1;
- }
- if (flag == 1) {
- ret = WOLFSSL_FATAL_ERROR;
- } else {
- ret = 0;
- }
- } /* END Test Bad Args. */
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_IdeaSetKey */
- /*
- * Unit test for wc_IdeaSetIV()
- */
- static int test_wc_IdeaSetIV (void)
- {
- int ret = 0;
- #ifdef HAVE_IDEA
- Idea idea;
- printf(testingFmt, "wc_IdeaSetIV()");
- ret = wc_IdeaSetIV(&idea, NULL);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_IdeaSetIV(NULL, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- } else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_IdeaSetIV */
- /*
- * Unit test for wc_IdeaCipher()
- */
- static int test_wc_IdeaCipher (void)
- {
- int ret = 0;
- #ifdef HAVE_IDEA
- Idea idea;
- const byte key[] =
- {
- 0x2B, 0xD6, 0x45, 0x9F, 0x82, 0xC5, 0xB3, 0x00,
- 0x95, 0x2C, 0x49, 0x10, 0x48, 0x81, 0xFF, 0x48
- };
- const byte plain[] =
- {
- 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
- };
- byte enc[sizeof(plain)];
- byte dec[sizeof(enc)];
- printf(testingFmt, "wc_IdeaCipher()");
- ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_ENCRYPTION);
- if (ret == 0) {
- ret = wc_IdeaCipher(&idea, enc, plain);
- if (ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- if (ret == 0) {
- ret = wc_IdeaSetKey(&idea, key, IDEA_KEY_SIZE, NULL, IDEA_DECRYPTION);
- if (ret == 0) {
- ret = wc_IdeaCipher(&idea, dec, enc);
- }
- if (ret == 0) {
- ret = XMEMCMP(plain, dec, IDEA_BLOCK_SIZE);
- }
- if (ret != 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Pass Bad Args. */
- if (ret == 0) {
- ret = wc_IdeaCipher(NULL, enc, dec);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_IdeaCipher(&idea, NULL, dec);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_IdeaCipher(&idea, enc, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- } else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_IdeaCipher */
- /*
- * Unit test for functions wc_IdeaCbcEncrypt and wc_IdeaCbcDecrypt
- */
- static int test_wc_IdeaCbcEncyptDecrypt (void)
- {
- int ret = 0;
- #ifdef HAVE_IDEA
- Idea idea;
- const byte key[] =
- {
- 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37,
- 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37, 0x37
- };
- const char* message = "International Data Encryption Algorithm";
- byte msg_enc[40];
- byte msg_dec[40];
- printf(testingFmt, "wc_IdeaCbcEncrypt()");
- ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_ENCRYPTION);
- if (ret == 0) {
- ret = wc_IdeaCbcEncrypt(&idea, msg_enc, (byte *)message,
- (word32)XSTRLEN(message) + 1);
- }
- if (ret == 0) {
- ret = wc_IdeaSetKey(&idea, key, sizeof(key), NULL, IDEA_DECRYPTION);
- }
- if (ret == 0) {
- ret = wc_IdeaCbcDecrypt(&idea, msg_dec, msg_enc,
- (word32)XSTRLEN(message) + 1);
- if (XMEMCMP(message, msg_dec, (word32)XSTRLEN(message))) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* Test bad args. Enc */
- if (ret == 0) {
- ret = wc_IdeaCbcEncrypt(NULL, msg_enc, (byte*)message,
- (word32)XSTRLEN(message) + 1);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_IdeaCbcEncrypt(&idea, NULL, (byte*)message,
- (word32)XSTRLEN(message) + 1);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_IdeaCbcEncrypt(&idea, msg_enc, NULL,
- (word32)XSTRLEN(message) + 1);
- }
- if (ret != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- } else {
- ret = 0;
- }
- } /* END test bad args ENC */
- /* Test bad args DEC */
- if (ret == 0) {
- ret = wc_IdeaCbcDecrypt(NULL, msg_dec, msg_enc,
- (word32)XSTRLEN(message) + 1);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_IdeaCbcDecrypt(&idea, NULL, msg_enc,
- (word32)XSTRLEN(message) + 1);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_IdeaCbcDecrypt(&idea, msg_dec, NULL,
- (word32)XSTRLEN(message) + 1);
- }
- if (ret != BAD_FUNC_ARG) {
- ret = WOLFSSL_FATAL_ERROR;
- } else {
- ret = 0;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_IdeaCbcEncryptDecrypt */
- /*
- * 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 (ret != 0) {
- flag = ret;
- }
- }
- /* 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)
- 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)
- 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)
- ret = wc_InitCmac(&cmac2, key2, key2Sz, type, NULL);
- #endif
- #ifdef WOLFSSL_AES_256
- if (ret == 0)
- ret = wc_InitCmac(&cmac3, key3, key3Sz, type, NULL);
- #endif
- /* Test bad args. */
- if (ret == 0) {
- 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;
- }
- }
- 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;
- }
- 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);
- #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),
- plaintext, 0, 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), cipher,
- 0, 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) {
- /* 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 */
- cbcE |= enc[0];
- }
- }
- 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 */
- cbcD |= dec[0];
- }
- }
- 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)
- /* 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))
- 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)
- /* 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));
- }
- }
- #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));
- }
- #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_RabbitSetKey()
- */
- static int test_wc_RabbitSetKey (void)
- {
- int ret = 0;
- #ifndef NO_RABBIT
- Rabbit rabbit;
- const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
- "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
- const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
- printf(testingFmt, "wc_RabbitSetKey()");
- ret = wc_RabbitSetKey(&rabbit, (byte*)key, (byte*)iv);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_RabbitSetKey(NULL, (byte*)key, (byte*)iv);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RabbitSetKey(&rabbit, NULL, (byte*)iv);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RabbitSetKey(&rabbit, (byte*)key, NULL);
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RabbitSetKey */
- /*
- * Test wc_RabbitProcess()
- */
- static int test_wc_RabbitProcess (void)
- {
- int ret = 0;
- #ifndef NO_RABBIT
- Rabbit enc, dec;
- byte cipher[25];
- byte plain[25];
- const char* key = "\xAC\xC3\x51\xDC\xF1\x62\xFC\x3B"
- "\xFE\x36\x3D\x2E\x29\x13\x28\x91";
- const char* iv = "\x59\x7E\x26\xC1\x75\xF5\x73\xC3";
- const char* input = TEST_STRING;
- unsigned long int inlen = (unsigned long int)TEST_STRING_SZ;
- /* Initialize stack variables. */
- XMEMSET(cipher, 0, sizeof(cipher));
- XMEMSET(plain, 0, sizeof(plain));
- printf(testingFmt, "wc_RabbitProcess()");
- ret = wc_RabbitSetKey(&enc, (byte*)key, (byte*)iv);
- if (ret == 0) {
- ret = wc_RabbitSetKey(&dec, (byte*)key, (byte*)iv);
- }
- if (ret == 0) {
- ret = wc_RabbitProcess(&enc, cipher, (byte*)input, (word32)inlen);
- }
- if (ret == 0) {
- ret = wc_RabbitProcess(&dec, plain, cipher, (word32)inlen);
- if (ret != 0 || XMEMCMP(input, plain, inlen)) {
- ret = WOLFSSL_FATAL_ERROR;
- } else {
- ret = 0;
- }
- }
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_RabbitProcess(NULL, plain, cipher, (word32)inlen);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RabbitProcess(&dec, NULL, cipher, (word32)inlen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RabbitProcess(&dec, plain, NULL, (word32)inlen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- } else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_RabbitProcess */
- /*
- * 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, NULL);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_InitRsaKey(NULL, NULL);
- #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, NULL);
- }
- 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;
- tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- if (tmp == NULL) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- if (ret == 0) {
- ret = wc_InitRsaKey(&keyPub, NULL);
- }
- 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 (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, NULL);
- 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)
- int bits = 1024;
- #else
- int bits = 2048;
- #endif
- printf(testingFmt, "wc_MakeRsaKey()");
- ret = wc_InitRsaKey(&genKey, NULL);
- 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, NULL);
- 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, sizeof(flatC), out, &outSz, &key,
- RSA_PRIVATE_DECRYPT, NULL);
- mp_clear(&c);
- }
- if (ret == RSA_OUT_OF_RANGE_E)
- ret = 0;
- 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, NULL);
- 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) {
- ret = wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz,
- p, &pSz, q, &qSz);
- }
- /* 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)
- 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, NULL);
- 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)
- RsaKey key;
- WC_RNG rng;
- int sz = 256; /* 2048/8 */
- byte* pt;
- byte digest[32];
- word32 digestSz;
- 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, NULL);
- 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)
- RsaKey key;
- WC_RNG rng;
- int sz = 256;
- byte* pt;
- byte digest[32];
- word32 digestSz;
- 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, NULL);
- 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)
- 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, NULL);
- }
- 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);
- }
- 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);
- }
- 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(HAVE_FAST_RSA) && defined(WOLFSSL_KEY_GEN) &&\
- (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL))
- RsaKey key;
- WC_RNG rng;
- byte* der;
- #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
- int bits = 1024;
- word32 derLen = 162;
- #else
- int bits = 2048;
- word32 derLen = 290;
- #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, NULL);
- }
- 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) {
- ret = wc_RsaKeyToPublicDer(&key, der, derLen);
- 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, NULL, derLen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_RsaKeyToPublicDer(&key, der, -1);
- }
- if (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, NULL, 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;
- DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
- DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
- DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
- #ifdef 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, NULL);
- 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.*/
- FREE_VAR(in, NULL);
- FREE_VAR(plain, NULL);
- 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;
- DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
- DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
- DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
- #ifdef 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, NULL, 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
- FREE_VAR(in, NULL);
- FREE_VAR(plain, NULL);
- 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;
- DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
- DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
- DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
- #ifdef 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, NULL);
- 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
- FREE_VAR(in, NULL);
- FREE_VAR(out, NULL);
- 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, NULL);
- if (ret == 0) {
- ret = wc_InitRng(&rng);
- }
- printf(testingFmt, "wc_RsaEncryptSize()");
- #if !defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)
- 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)
- int bits = 1024;
- #else
- int bits = 2048;
- #endif
- ret = wc_InitRsaKey(&key, NULL);
- 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 */
- /*
- * Test wc_Hc128_SetKey()
- */
- static int test_wc_Hc128_SetKey (void)
- {
- int ret = 0;
- #ifdef HAVE_HC128
- HC128 ctx;
- const char* key = "\x80\x00\x00\x00\x00\x00\x00\x00"
- "\x00\x00\x00\x00\x00\x00\x00\x00";
- const char* iv = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
- "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
- printf(testingFmt, "wc_Hc128_SetKey()");
- ret = wc_Hc128_SetKey(&ctx, (byte*)key, (byte*)iv);
- /* Test bad args. */
- if (ret == 0) {
- ret = wc_Hc128_SetKey(NULL, (byte*)key, (byte*)iv);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Hc128_SetKey(&ctx, NULL, (byte*)iv);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Hc128_SetKey(&ctx, (byte*)key, NULL);
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Hc128_SetKey */
- /*
- * Testing wc_Hc128_Process()
- */
- static int test_wc_Hc128_Process (void)
- {
- int ret = 0;
- #ifdef HAVE_HC128
- HC128 enc;
- HC128 dec;
- const char* key = "\x0F\x62\xB5\x08\x5B\xAE\x01\x54"
- "\xA7\xFA\x4D\xA0\xF3\x46\x99\xEC";
- const char* input = "Encrypt Hc128, and then Decrypt.";
- size_t inlen = XSTRLEN(input) + 1; /* Add null terminator */
- byte cipher[inlen];
- byte plain[inlen];
- printf(testingFmt, "wc_Hc128_Process()");
- ret = wc_Hc128_SetKey(&enc, (byte*)key, NULL);
- if (ret == 0) {
- ret = wc_Hc128_SetKey(&dec, (byte*)key, NULL);
- }
- if (ret == 0) {
- ret = wc_Hc128_Process(&enc, cipher, (byte*)input, (word32)inlen);
- if (ret == 0) {
- ret = wc_Hc128_Process(&dec, plain, cipher, (word32)inlen);
- }
- }
- /* Bad args. */
- if (ret == 0) {
- ret = wc_Hc128_Process(NULL, plain, cipher, (word32)inlen);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Hc128_Process(&dec, NULL, cipher, (word32)inlen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_Hc128_Process(&dec, plain, NULL, (word32)inlen);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- } else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_Hc128_Process */
- /*
- * 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 (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 */
- #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
- XMEMSET(&rng, 0, sizeof(rng));
- XMEMSET(&genKey, 0, sizeof(genKey));
- #endif
- 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
- 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;
- 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) {
- ret = 0;
- } else {
- ret = WOLFSSL_FATAL_ERROR;
- }
- }
- /* 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 = SSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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 = SSL_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;
- int verify_ok = 0; /*1 = Verify success.*/
- /* 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 = SSL_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 = SSL_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 = SSL_FATAL_ERROR;
- }
- }
- } /* END verify. */
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif /* Verify. */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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(in, inlen, &pubKey);
- if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
- ret = 0;
- } else {
- ret = SSL_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 = SSL_FATAL_ERROR;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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);
- 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(privKey, privKeySz, pubKey,
- pubKeySz, &key);
- if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0
- || XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
- ret = SSL_FATAL_ERROR;
- }
- }
- /* 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 = 0;
- } else if (ret == 0) {
- ret = SSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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 = SSL_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 = SSL_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 = SSL_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 = SSL_FATAL_ERROR;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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 = SSL_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 = SSL_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 = SSL_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 = SSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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(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 = SSL_FATAL_ERROR;
- }
- if (ret == 0) {
- wc_ed25519_init(&key);
- tmp = wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0);
- if (tmp != BUFFER_E) {
- ret = SSL_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 = SSL_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 = SSL_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;
- }
- #endif
- return 0;
- } /* 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 = SSL_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_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 == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(NULL, &public_key, out,
- &outLen, endian);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(&private_key, NULL, out,
- &outLen, endian);
- 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 == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
- NULL, endian);
- 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 == 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 == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- 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;
- #if defined(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.point, key.k.point);
- }
- /*test bad cases*/
- if (ret == 0) {
- ret = wc_curve25519_make_pub((int)sizeof key.k.point - 1, key.k.point, (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.point, 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.point, key.k.point);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_curve25519_make_pub((int)sizeof out, NULL, (int)sizeof key.k.point, key.k.point);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- }
- if (ret == 0) {
- /* verify clamping test */
- key.k.point[0] |= ~248;
- ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, key.k.point);
- if (ret == ECC_BAD_ARG_E) {
- ret = 0;
- }
- key.k.point[0] &= 248;
- }
- /* repeat the expected-to-succeed test. */
- if (ret == 0) {
- ret = wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof key.k.point, key.k.point);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- 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);
- 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);
- 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);
- 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);
- 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 = SSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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 = SSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- 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;
- int verify_ok = 0; /*1 = Verify success.*/
- /* 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 = SSL_FATAL_ERROR;
- }
- } /* END sign */
- printf(resultFmt, ret == 0 ? passed : failed);
- #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 = SSL_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 = SSL_FATAL_ERROR;
- }
- }
- } /* END verify. */
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif /* Verify. */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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(in, inlen, &pubKey);
- if (ret == 0 && XMEMCMP(in, pubKey.p, inlen) == 0) {
- ret = 0;
- } else {
- ret = SSL_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 = SSL_FATAL_ERROR;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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);
- 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(privKey, privKeySz, pubKey, pubKeySz,
- &key);
- if (ret == 0 && (XMEMCMP(pubKey, key.p, privKeySz) != 0 ||
- XMEMCMP(privKey, key.k, pubKeySz) != 0)) {
- ret = SSL_FATAL_ERROR;
- }
- }
- /* 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 = 0;
- } else if (ret == 0) {
- ret = SSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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 = SSL_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 = SSL_FATAL_ERROR;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- 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 = SSL_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 = SSL_FATAL_ERROR;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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);
- 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);
- } /* 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);
- } /* 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);
- } /* END wc_ed448_pub_size */
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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 = SSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- 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 = SSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- } /* END wc_ed448_export_key() */
- /* Cross check output. */
- if (ret == 0 && XMEMCMP(priv, privOnly, privSz) != 0) {
- ret = SSL_FATAL_ERROR;
- }
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = SSL_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(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 = SSL_FATAL_ERROR;
- }
- if (ret == 0) {
- wc_ed448_init(&key);
- tmp = wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0);
- if (tmp != BUFFER_E) {
- ret = SSL_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 = SSL_FATAL_ERROR;
- }
- wc_FreeRng(&rng);
- wc_ed448_free(&key);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #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 = SSL_FATAL_ERROR;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- /* 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 = SSL_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);
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- /* 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);
- #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);
- 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);
- }
- }
- 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);
- 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);
- 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);
- }
- }
- 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);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- wc_ecc_free(&key);
- #endif
- return ret;
- } /* END test_wc_ecc_size */
- static void 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 */
- }
- /*
- * 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 digest[] = TEST_STRING;
- word32 digestlen = (word32)TEST_STRING_SZ;
- /* Init stack var */
- XMEMSET(sig, 0, siglen);
- XMEMSET(&key, 0, sizeof(key));
- /* 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);
- }
- }
- 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 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;
- int keySz = KEY16;
- byte out[KEY16];
- word32 outlen = (word32)sizeof(out);
- /* 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 (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &key);
- }
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &pubKey);
- }
- #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);
- /* 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) {
- 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_ecc_free(&key);
- wc_ecc_free(&pubKey);
- #ifdef FP_ECC
- wc_ecc_fp_free();
- #endif
- #endif
- 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);
- /* 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);
- }
- }
- 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);
- 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 */
- /*
- * 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);
- }
- }
- 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 (ret == BUFFER_E) {
- 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);
- 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 (ret == 0) {
- ret = wc_ecc_export_x963(&key, x963, &x963Len);
- }
- }
- 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);
- 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 (ret == 0) {
- ret = wc_ecc_export_x963(&key, x963Key, &x963KeySz);
- }
- 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);
- 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);
- }
- }
- 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);
- 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);
- 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);
- #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);
- 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);
- 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);
- }
- }
- 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);
- 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);
- 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);
- 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);
- 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);
- 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);
- 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(WOLFSSL_AES_128) \
- && !defined(WC_NO_RNG)
- ecc_key srvKey, cliKey;
- WC_RNG rng;
- const char* msg = "EccBlock Size 16";
- word32 msgSz = (word32)XSTRLEN(msg);
- byte out[XSTRLEN(msg) + WC_SHA256_DIGEST_SIZE];
- word32 outSz = (word32)sizeof(out);
- byte plain[XSTRLEN(msg) + 1];
- 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 (ret == 0) {
- ret = wc_ecc_init(&srvKey);
- }
- if (ret == 0) {
- ret = wc_ecc_make_key(&rng, keySz, &srvKey);
- }
- }
- #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);
- printf(testingFmt, "wc_ecc_decrypt()");
- if (ret == 0) {
- ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, plain,
- &plainSz, NULL);
- }
- if (ret == 0) {
- ret = wc_ecc_decrypt(NULL, &cliKey, out, outSz, plain,
- &plainSz, NULL);
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain,
- &plainSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_decrypt(&srvKey, &cliKey, NULL, outSz, plain,
- &plainSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_decrypt(&srvKey, &cliKey, out, outSz, NULL,
- &plainSz, NULL);
- }
- if (ret == BAD_FUNC_ARG) {
- ret = wc_ecc_decrypt(&srvKey, &cliKey, 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);
- if (wc_FreeRng(&rng) && ret == 0) {
- ret = WOLFSSL_FATAL_ERROR;
- }
- 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);
- 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 (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);
- /* 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(&key.pubkey, 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);
- /* 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);
- 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);
- 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);
- #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);
- #endif /* USE_ECC_B_PARAM */
- /* 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 (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);
- }
- }
- 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);
- 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)
- ecc_key key;
- WC_RNG rng;
- mp_int r;
- mp_int s;
- 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, NULL, 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 (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 (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);
- }
- 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);
- 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);
- }
- }
- 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);
- 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 == 7) {
- ret = 0;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #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);
- }
- 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);
- 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 = 0;
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- }/* End test_ToTraditional*/
- /*
- * Testing wc_EccPrivateKeyToDer
- */
- static int test_wc_EccPrivateKeyToDer (void)
- {
- int ret = 0;
- #if defined(HAVE_ECC) && !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);
- }
- 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) {
- ret = 0;
- }
- }
- wc_ecc_free(&eccKey);
- }
- wc_FreeRng(&rng);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- }/* End test_wc_EccPrivateKeyToDer*/
- /*
- * Testing wc_Ed25519KeyToDer
- */
- static int test_wc_Ed25519KeyToDer (void)
- {
- int ret = 0;
- #if defined(HAVE_ED25519) && (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, NULL, 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 Case */
- 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);
- #endif
- return ret;
- }/* End test_wc_Ed25519KeyToDer*/
- /*
- * Testing wc_Ed25519PrivateKeyToDer
- */
- static int test_wc_Ed25519PrivateKeyToDer (void)
- {
- int ret = 0;
- #if defined(HAVE_ED25519) && (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, NULL, 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 Case */
- 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);
- #endif
- return ret;
- }/* End test_wc_Ed25519PrivateKeyToDer*/
- /*
- * Testing wc_Ed448KeyToDer
- */
- static int test_wc_Ed448KeyToDer (void)
- {
- int ret = 0;
- #if defined(HAVE_ED448) && (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, NULL, 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 Case */
- 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);
- #endif
- return ret;
- }/* End test_wc_Ed448KeyToDer*/
- /*
- * Testing wc_Ed448PrivateKeyToDer
- */
- static int test_wc_Ed448PrivateKeyToDer (void)
- {
- int ret = 0;
- #if defined(HAVE_ED448) && (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, NULL, 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 case */
- 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);
- #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);
- }
- }
- 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);
- #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)
- 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)
- ed448_key ed448Key;
- #endif
- printf(testingFmt, "wc_SetSubjectKeyIdFromPublicKey_ex()");
- #ifndef HAVE_FIPS
- ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
- #else
- ret = wc_InitRng(&rng);
- #endif
- wc_InitCert(&cert);
- #if defined(HAVE_ED25519)
- if (ret == 0) { /*ED25519*/
- ret = wc_ed25519_init(&ed25519Key);
- if (ret == 0) {
- 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, NULL);
- 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) {
- wc_ecc_make_key(&rng, KEY14, &eccKey);
- }
- if (ret == 0) {
- ret = wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
- }
- wc_ecc_free(&eccKey);
- }
- #endif
- #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
- defined(WOLFSSL_KEY_GEN))
- if (ret == 0) { /*ED448*/
- ret = wc_ed448_init(&ed448Key);
- if (ret == 0) {
- 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);
- 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)
- 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)
- ed448_key ed448Key;
- #endif
- printf(testingFmt, "wc_SetAuthKeyIdFromPublicKey_ex()");
- #ifndef HAVE_FIPS
- ret = wc_InitRng_ex(&rng, HEAP_HINT, devId);
- #else
- ret = wc_InitRng(&rng);
- #endif
- wc_InitCert(&cert);
- #if defined(HAVE_ED25519)
- if (ret == 0) { /*ED25519*/
- ret = wc_ed25519_init(&ed25519Key);
- if (ret == 0) {
- 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, NULL);
- 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) {
- wc_ecc_make_key(&rng, KEY14, &eccKey);
- }
- if (ret == 0) {
- ret = wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey);
- }
- wc_ecc_free(&eccKey);
- }
- #endif
- #if defined(HAVE_ED448) && (defined(WOLFSSL_CERT_GEN) || \
- defined(WOLFSSL_KEY_GEN))
- if (ret == 0) { /*ED448*/
- ret = wc_ed448_init(&ed448Key);
- if (ret == 0) {
- 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);
- wc_FreeRng(&rng);
- #endif /*defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
- return ret;
- }/* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
- /*
- * Testing wc_PKCS7_New()
- */
- static void test_wc_PKCS7_New (void)
- {
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- void* heap = NULL;
- printf(testingFmt, "wc_PKCS7_New()");
- pkcs7 = wc_PKCS7_New(heap, devId);
- AssertNotNull(pkcs7);
- printf(resultFmt, passed);
- wc_PKCS7_Free(pkcs7);
- #endif
- } /* END test-wc_PKCS7_New */
- /*
- * Testing wc_PKCS7_Init()
- */
- static void test_wc_PKCS7_Init (void)
- {
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- void* heap = NULL;
- printf(testingFmt, "wc_PKCS7_Init()");
- pkcs7 = wc_PKCS7_New(heap, devId);
- AssertNotNull(pkcs7);
- AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
- /* Pass in bad args. */
- AssertIntEQ(wc_PKCS7_Init(NULL, heap, devId), BAD_FUNC_ARG);
- printf(resultFmt, passed);
- wc_PKCS7_Free(pkcs7);
- #endif
- } /* END test-wc_PKCS7_Init */
- /*
- * Testing wc_PKCS7_InitWithCert()
- */
- static void 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 */
- 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, devId));
- /* 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, devId));
- /* 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
- } /* END test_wc_PKCS7_InitWithCert */
- /*
- * Testing wc_PKCS7_EncodeData()
- */
- static void 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_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, devId));
- 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
- } /* 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 void test_wc_PKCS7_EncodeSignedData(void)
- {
- #if defined(HAVE_PKCS7)
- PKCS7* pkcs7;
- WC_RNG rng;
- byte output[FOURK_BUF];
- byte badOut[0];
- word32 outputSz = (word32)sizeof(output);
- word32 badOutSz = (word32)sizeof(badOut);
- 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_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 = XOPEN("./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, outputSz);
- AssertIntEQ(wc_InitRng(&rng), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
- 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, devId));
- 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, devId));
- 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
- } /* END test_wc_PKCS7_EncodeSignedData */
- /*
- * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
- */
- static void 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_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, devId));
- 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, devId));
- AssertIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
- /* required parameter even on verify when using _ex */
- 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, devId));
- 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
- } /* 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_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, devId));
- 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, devId));
- 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 void 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[0];
- word32 badOutSz = (word32)sizeof(badOut);
- byte badContent[] = "This is different content than was signed";
- AssertIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
- (word32)sizeof(data),
- 0, 0)), 0);
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
- 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, devId));
- 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, devId));
- 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);
- printf(resultFmt, passed);
- #endif
- } /* END test_wc_PKCS7_VerifySignedData() */
- #if defined(HAVE_PKCS7) && !defined(NO_AES) && !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 && !NO_AES_256 */
- /*
- * Testing wc_PKCS7_EncodeEnvelopedData()
- */
- static void 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 */
- #ifndef NO_AES
- #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 */
- #endif /* NO_RSA */
- #if defined(HAVE_ECC)
- #ifndef NO_AES
- #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 */
- #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, devId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 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, devId));
- }
- } /* 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)
- /* 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(NO_AES_256)
- /* test of decrypt callback with KEKRI enveloped data */
- {
- int envelopedSz;
- const byte keyId[] = { 0x00 };
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
- 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, devId));
- 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
- #endif /* HAVE_PKCS7 */
- } /* END test_wc_PKCS7_EncodeEnvelopedData() */
- /*
- * Testing wc_PKCS7_EncodeEncryptedData()
- */
- static void test_wc_PKCS7_EncodeEncryptedData (void)
- {
- #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
- PKCS7* pkcs7;
- 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
- #ifndef NO_AES
- #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
- 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 */
- #ifndef NO_AES
- #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 */
- };
- testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
- for (i = 0; i < testSz; i++) {
- AssertNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, devId));
- AssertIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, devId), 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);
- }
- }
- 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
- } /* END test_wc_PKCS7_EncodeEncryptedData() */
- /*
- * Testing wc_PKCS7_Degenerate()
- */
- static void 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, devId));
- 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, devId));
- 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
- } /* 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 void 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, devId));
- 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, devId));
- #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
- } /* END test_wc_PKCS7_BER() */
- static void test_PKCS7_signed_enveloped(void)
- {
- #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
- && !defined(NO_AES)
- XFILE f;
- PKCS7* pkcs7;
- PKCS7* inner;
- 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;
- unsigned char decoded[FOURK_BUF];
- int decodedSz = FOURK_BUF;
- 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);
- /* 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);
- wc_PKCS7_Free(pkcs7);
- /* 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, devId), 0);
- AssertIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
- AssertNotNull(pkcs7->content);
- /* 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);
- wc_PKCS7_Free(pkcs7);
- /* 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);
- printf(resultFmt, passed);
- #endif
- }
- static void 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, devId);
- AssertNotNull(pkcs7);
- AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 0);
- AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
- wc_PKCS7_Free(pkcs7);
- printf(resultFmt, passed);
- #endif
- }
- static void 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, devId);
- AssertNotNull(pkcs7);
- AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 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
- }
- static void 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, devId);
- AssertNotNull(pkcs7);
- AssertIntEQ(wc_PKCS7_Init(pkcs7, heap, devId), 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
- }
- static void 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, devId)));
- 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, devId)));
- /* 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(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
- }
- static void 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);
- printf(resultFmt, passed);
- #endif
- }
- /* 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);
- #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, devId);
- }
- 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;
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_HashAlg hash;
- word32 flags = 0;
- int i, j;
- printf(testingFmt, "wc_HashSetFlags()");
- /* enum for holding supported algorithms, #ifndef's restrict if disabled */
- enum wc_HashType enumArray[] = {
- #ifndef NO_MD5
- WC_HASH_TYPE_MD5,
- #endif
- #ifndef NO_SHA
- WC_HASH_TYPE_SHA,
- #endif
- #ifdef WOLFSSL_SHA224
- WC_HASH_TYPE_SHA224,
- #endif
- #ifndef NO_SHA256
- WC_HASH_TYPE_SHA256,
- #endif
- #ifdef WOLFSSL_SHA384
- WC_HASH_TYPE_SHA384,
- #endif
- #ifdef WOLFSSL_SHA512
- WC_HASH_TYPE_SHA512,
- #endif
- #ifdef WOLFSSL_SHA3
- WC_HASH_TYPE_SHA3_224,
- #endif
- };
- enum wc_HashType notSupported[] = {
- WC_HASH_TYPE_MD5_SHA,
- WC_HASH_TYPE_MD2,
- WC_HASH_TYPE_MD4,
- WC_HASH_TYPE_BLAKE2B,
- WC_HASH_TYPE_BLAKE2S,
- WC_HASH_TYPE_NONE,
- };
- /* dynamically finds the length */
- int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
- /* For loop to test various arguments... */
- for (i = 0; i < enumlen; i++) {
- ret = wc_HashInit(&hash, enumArray[i]);
- if (ret == 0) {
- ret = wc_HashSetFlags(&hash, enumArray[i], flags);
- }
- if (ret == 0) {
- if (flags & WC_HASH_FLAG_ISCOPY) {
- ret = 0;
- }
- }
- if (ret == 0) {
- ret = wc_HashSetFlags(NULL, enumArray[i], flags);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- wc_HashFree(&hash, enumArray[i]);
- }
- /* For loop to test not supported cases */
- int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
- for (j = 0; j < notSupportedLen; j++){
- if (ret == 0) {
- ret = wc_HashInit(&hash, notSupported[j]);
- if (ret == BAD_FUNC_ARG){
- ret = 0;
- if (ret == 0){
- ret = wc_HashSetFlags(&hash, notSupported[j], flags);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- }
- }
- if (ret == 0) {
- ret = wc_HashFree(&hash, notSupported[j]);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_HashSetFlags */
- /*
- * Unit test function for wc_HashGetFlags()
- */
- static int test_wc_HashGetFlags(void)
- {
- int ret = 0;
- #if defined(WOLFSSL_HASH_FLAGS) || defined(WOLF_CRYPTO_CB)
- wc_HashAlg hash;
- word32 flags = 0;
- int i, j;
- printf(testingFmt, "wc_HashGetFlags()");
- /* 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));
- /* 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 */
- int notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
- for (j = 0; j < notSupportedLen; j++){
- if (ret == 0) {
- ret = wc_HashInit(&hash, notSupported[j]);
- if (ret == BAD_FUNC_ARG){
- ret = 0;
- if (ret == 0){
- ret = wc_HashGetFlags(&hash, notSupported[j], &flags);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- }
- }
- if (ret == 0) {
- ret = wc_HashFree(&hash, notSupported[j]);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- } /* END test_wc_HashGetFlags */
- /*----------------------------------------------------------------------------*
- | Compatibility Tests
- *----------------------------------------------------------------------------*/
- static void 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), 0xb1231320);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- /* 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) */
- }
- #ifndef NO_BIO
- static void test_wolfSSL_X509_INFO(void)
- {
- #if defined(OPENSSL_ALL)
- STACK_OF(X509_INFO) *info_stack;
- X509_INFO *info;
- BIO *cert;
- int i;
- 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);
- }
- 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_free(info_stack);
- BIO_free(cert);
- printf(resultFmt, passed);
- #endif
- }
- #endif
- static void 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
- }
- static void 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
- }
- static void 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
- }
- static void 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) */
- }
- static void 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
- }
- static void 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
- }
- static void 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);
- #endif
- }
- static void 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);
- #endif
- #endif
- }
- static void 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);
- }
- #endif
- }
- static void 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;
- #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));
- #ifndef HAVE_USER_RSA
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
- AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
- AssertIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
- #endif
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #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);
- #ifndef HAVE_USER_RSA
- /* 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)
- /************* 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*/
- /* test and checkout X509 extensions */
- bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext, NID_basic_constraints,
- &crit, NULL);
- AssertNotNull(bc);
- #ifdef OPENSSL_ALL
- ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc);
- AssertNotNull(ext);
- X509_EXTENSION_free(ext);
- #endif
- AssertIntEQ(crit, 0);
- 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_free(sk);
- #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 */
- }
- static void test_wolfSSL_X509_check_private_key(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
- defined(USE_CERT_BUFFERS_2048)
- 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);
- EVP_PKEY_free(pkey);
- X509_free(x509);
- printf(resultFmt, passed);
- #endif
- }
- static void 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)
- 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 10 19:49:52 2021 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
- }
- static void 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 */
- }
- static void test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
- {
- #if defined(OPENSSL_EXTRA)
- WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime;
- unsigned char nullstr[32];
- XMEMSET(nullstr, 0, 32);
- asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
- sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL,
- DYNAMIC_TYPE_TMP_BUFFER);
- if (asn1_gtime) {
- XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
- wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
- AssertIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
- XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- }
- #endif /* OPENSSL_EXTRA */
- }
- static void test_wolfSSL_private_keys(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM)
- 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! */
- AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
- AssertTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
- AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- #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);
- #ifndef HAVE_USER_RSA
- /* 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);
- /* After loading back in DER format of original key, should match */
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- /* 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);
- #ifndef NO_CHECK_PRIVATE_KEY
- #ifndef HAVE_USER_RSA
- /* 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);
- /* After loading back in DER format of original key, should match */
- AssertIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
- #endif /* !NO_CHECK_PRIVATE_KEY */
- /* 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));
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- SSL_free(ssl);
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- #endif /* end of ECC private key match tests */
- #ifdef HAVE_ED25519
- #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));
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- SSL_free(ssl);
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- #endif /* end of Ed25519 private key match tests */
- #ifdef HAVE_ED448
- #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));
- AssertIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- SSL_free(ssl);
- AssertTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
- WOLFSSL_FILETYPE_PEM));
- AssertNotNull(ssl = SSL_new(ctx));
- AssertIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
- 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 /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- }
- static void 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";
- size_t sz;
- byte* buf;
- EVP_PKEY* pkey2;
- 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;
- }
- #endif
- /* test loading ECC key using BIO */
- #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
- {
- XFILE file;
- const char* fname = "./certs/ecc-key.pem";
- size_t sz;
- byte* buf;
- EVP_PKEY* pkey2;
- 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());
- pkey2->type = EVP_PKEY_EC;
- /* Test parameter copy */
- AssertIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
- /* Test default digest */
- AssertIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
- AssertIntEQ(nid, NID_sha256);
- EVP_PKEY_free(pkey2);
- EVP_PKEY_free(pkey);
- pkey = NULL;
- }
- #endif
- #if !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;
- 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);
- 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);
- /* 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(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_FILESYSTEM) && !defined(NO_MD5)
- {
- XFILE f;
- 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 */
- }
- #ifndef NO_BIO
- static void 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((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
- AssertIntEQ(RSA_size(rsa), 256);
- AssertIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
- NULL), WOLFSSL_FAILURE);
- BIO_free(bio);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
- NULL), WOLFSSL_SUCCESS);
- BIO_free(bio);
- RSA_free(rsa);
- /* PUBKEY */
- AssertNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
- AssertNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
- AssertNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
- AssertIntEQ(RSA_size(rsa), 256);
- AssertIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
- BIO_free(bio);
- AssertNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
- AssertIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
- BIO_free(bio);
- RSA_free(rsa);
- #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) */
- }
- static void 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;
- 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)
- AssertNotNull(rsa_dup = RSAPublicKey_dup(rsa));
- AssertPtrNE(rsa_dup, rsa);
- #endif
- /* test if valgrind complains about unreleased memory */
- RSA_up_ref(rsa);
- RSA_free(rsa);
- BIO_free(bio);
- RSA_free(rsa);
- RSA_free(rsa_dup);
- #ifdef HAVE_ECC
- AssertNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
- AssertNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
- BIO_free(bio);
- #endif /* HAVE_ECC */
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
- }
- static void 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 */
- }
- static void 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);
- 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) */
- }
- static void 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;
- file = XFOPEN(fname, "rb");
- AssertTrue((file != XBADFILE));
- XFSEEK(file, 0, XSEEK_END);
- 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;
- EVP_PKEY_free(pkey);
- pkey = NULL;
- }
- #endif
- (void)bio;
- (void)pkey;
- #endif
- }
- #endif /* !NO_BIO */
- static void 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 */
- }
- static void 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)
- byte buff[6000];
- char file[] = "./certs/dsaparams.pem";
- XFILE f;
- int bytes;
- DSA* dsa;
- DH* dh;
- #if !defined(NO_DH) && (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(NO_DH) && (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(NO_DH) && (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
- DH_free(dh2);
- #endif
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- printf(resultFmt, passed);
- #endif
- }
- static void 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) */
- }
- static void 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 */
- }
- static void 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 */
- }
- static void test_wolfSSL_EVP_MD_size(void)
- {
- #ifdef OPENSSL_EXTRA
- WOLFSSL_EVP_MD_CTX mdCtx;
- printf(testingFmt, "wolfSSL_EVP_MD_size()");
- #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_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_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_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_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_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_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_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);
- AssertIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 0);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- }
- #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 void 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 */
- }
- static void 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;
- 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);
- 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_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
- }
- static void 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
- }
- static void test_wolfSSL_CTX_add_extra_chain_cert(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;
- 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);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- }
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- static void 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) */
- }
- #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 void 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))
-
- 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));
-
- 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
- }
- static void 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
- }
- static void 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_free(sk);
- 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_free(sk);
- 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);
- }
-
- X509_STORE_free(str);
-
- #endif
- printf(resultFmt, passed);
- #endif
- }
- static void 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);
- #ifdef WOLFSSL_SIGNER_DER_CERT
- AssertNotNull(issuerName);
- cmp = X509_NAME_cmp(caName, issuerName);
- AssertIntEQ(cmp, 0);
- #else
- AssertNotNull(issuerName);
- #endif
- X509_free(issuer);
- X509_STORE_CTX_free(ctx);
- X509_free(x509Svr);
- X509_STORE_free(str);
- X509_free(x509Ca);
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_PKCS7_certs(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !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) */
- }
- static void 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_free(sk);
- #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_free(sk);
- /* sk3 is dup so free here */
- sk_X509_free(sk3);
- #endif
- /* test X509_STORE_CTX_get/set_ex_data */
- {
- int i = 0, tmpData = 5;
- void* tmpDataRet;
- AssertNotNull(ctx = X509_STORE_CTX_new());
- #if defined(HAVE_EX_DATA) || defined(FORTRESS)
- 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());
- #if defined(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) */
- }
- static void 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) */
- }
- static void 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) */
- }
- static void 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 */
- }
- static void test_wolfSSL_CTX_get0_set1_param(void)
- {
- #if defined(OPENSSL_EXTRA)
- 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
- 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));
- SSL_CTX_free(ctx);
- XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
- }
- static void test_wolfSSL_get0_param(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
- 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 /* OPENSSL_EXTRA && !defined(NO_RSA)*/
- }
- static void 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 */
- }
- static void 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 */
- }
- static void 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;
- 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_client_method()));
- names = SSL_load_client_CA_file(cliCertFile);
- AssertNotNull(names);
- SSL_CTX_set_client_CA_list(ctx,names);
- AssertNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
- 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);
- }
- wolfSSL_CTX_free(ctx);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT && !NO_BIO */
- }
- static void 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);
- X509_free(x509);
- X509_free(x509_a);
- SSL_CTX_free(ctx);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
- }
- static void 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
- }
- static void test_wolfSSL_CTX_set_srp_username(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
- && !defined(NO_SHA256) && !defined(WC_NO_RNG)
- 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 */
- }
- static void test_wolfSSL_CTX_set_srp_password(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
- && !defined(NO_SHA256) && !defined(WC_NO_RNG)
- 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 */
- }
- static void 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
- {
- 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
- 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);
- SSL_CTX_free(ctx);
- }
- #endif
- printf(resultFmt, passed);
- #endif
- return;
- }
- static void test_wolfSSL_X509_STORE_load_locations(void)
- {
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_FILESYSTEM)\
- && !defined(NO_WOLFSSL_DIR)
- 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);
- SSL_CTX_free(ctx);
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_BN(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
- BIGNUM* a;
- BIGNUM* b;
- BIGNUM* c;
- BIGNUM* d;
- ASN1_INTEGER* ai;
- unsigned char value[1];
- printf(testingFmt, "wolfSSL_BN()");
- AssertNotNull(b = BN_new());
- AssertNotNull(c = BN_new());
- AssertNotNull(d = BN_new());
- value[0] = 0x03;
- 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] = value[0];
- AssertNotNull(a = ASN1_INTEGER_to_BN(ai, NULL));
- ASN1_INTEGER_free(ai);
- value[0] = 0x02;
- AssertNotNull(BN_bin2bn(value, sizeof(value), b));
- value[0] = 0x05;
- AssertNotNull(BN_bin2bn(value, sizeof(value), c));
- /* 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 */
- value[0] = 0;
- AssertIntEQ(BN_bn2bin(d, value), sizeof(value));
- AssertIntEQ((int)(value[0]), 4);
- /* 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 */
- value[0] = 0;
- AssertIntEQ(BN_bn2bin(d, value), sizeof(value));
- AssertIntEQ((int)(value[0]), 1);
- /* BN_mod_inverse test */
- value[0] = 0;
- BIGNUM *r = BN_new();
- BIGNUM *val = BN_mod_inverse(r,b,c,NULL);
- AssertIntEQ(BN_bn2bin(r, value), 1);
- AssertIntEQ((int)(value[0] & 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);
- value[0] = 0x03;
- AssertNotNull(b = BN_new());
- AssertNotNull(BN_bin2bn(value, sizeof(value), b));
- value[0] = 0x04;
- AssertNotNull(c = BN_new());
- AssertNotNull(BN_bin2bn(value, sizeof(value), c));
- 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);
- value[0] = 0x3;
- AssertNotNull(BN_bin2bn(value, sizeof(value), ap));
- value[0] = 0x02;
- AssertNotNull(BN_bin2bn(value, sizeof(value), &bv));
- value[0] = 0x05;
- AssertNotNull(BN_bin2bn(value, sizeof(value), &cv));
- /* 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 */
- value[0] = 0;
- AssertIntEQ(BN_bn2bin(&dv, value), sizeof(value));
- AssertIntEQ((int)(value[0]), 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 */
- value[0] = 0;
- AssertIntEQ(BN_bn2bin(&dv, value), sizeof(value));
- AssertIntEQ((int)(value[0]), 1);
- BN_free(ap);
- }
- #endif
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
- }
- #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)
- {
- (void) ctx;
- (void) ssl;
- #if defined(OPENSSL_ALL) && defined(SESSION_CERTS)
- STACK_OF(X509)* sk;
- X509* x509;
- int i, num;
- BIO* bio;
- #endif
- 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);
- #endif
- #if defined(OPENSSL_ALL) && defined(SESSION_CERTS)
- 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 void 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, (void *)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
- }
- static void 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
- }
- static void 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
- }
- static void test_generate_cookie(void)
- {
- #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA)
- 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
- }
- static void test_wolfSSL_set_options(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- SSL* ssl;
- SSL_CTX* ctx;
- char appData[] = "extra msg";
- 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;
- printf(testingFmt, "wolfSSL_set_options()");
- #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));
- AssertTrue(SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
- AssertTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
- AssertIntGT((int)SSL_CTX_set_options(ctx, (SSL_OP_COOKIE_EXCHANGE |
- SSL_OP_NO_SSLv2)), 0);
- AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_COOKIE_EXCHANGE) &
- SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
- AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1_2) &
- SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
- AssertTrue((SSL_CTX_set_options(ctx, SSL_OP_NO_COMPRESSION) &
- SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
- AssertNull((SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION) &
- SSL_OP_NO_COMPRESSION));
- SSL_CTX_free(ctx);
- #ifndef NO_WOLFSSL_SERVER
- ctx = SSL_CTX_new(wolfSSLv23_server_method());
- AssertNotNull(ctx);
- #else
- ctx = SSL_CTX_new(wolfSSLv23_client_method());
- AssertNotNull(ctx);
- #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));
- #if defined(HAVE_EX_DATA) || defined(FORTRESS)
- AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_SUCCESS);
- AssertNotNull(SSL_get_app_data((const WOLFSSL*)ssl));
- if (ssl) {
- AssertIntEQ(XMEMCMP(SSL_get_app_data((const WOLFSSL*)ssl),
- appData, sizeof(appData)), 0);
- }
- #else
- AssertIntEQ(SSL_set_app_data(ssl, (void*)appData), SSL_FAILURE);
- AssertNull(SSL_get_app_data((const WOLFSSL*)ssl));
- #endif
- AssertTrue(SSL_set_options(ssl, SSL_OP_NO_TLSv1) == SSL_OP_NO_TLSv1);
- AssertTrue(SSL_get_options(ssl) == SSL_OP_NO_TLSv1);
- AssertIntGT((int)SSL_set_options(ssl, (SSL_OP_COOKIE_EXCHANGE |
- WOLFSSL_OP_NO_SSLv2)), 0);
- AssertTrue((SSL_set_options(ssl, SSL_OP_COOKIE_EXCHANGE) &
- SSL_OP_COOKIE_EXCHANGE) == SSL_OP_COOKIE_EXCHANGE);
- AssertTrue((SSL_set_options(ssl, SSL_OP_NO_TLSv1_2) &
- SSL_OP_NO_TLSv1_2) == SSL_OP_NO_TLSv1_2);
- AssertTrue((SSL_set_options(ssl, SSL_OP_NO_COMPRESSION) &
- SSL_OP_NO_COMPRESSION) == SSL_OP_NO_COMPRESSION);
- AssertNull((SSL_clear_options(ssl, SSL_OP_NO_COMPRESSION) &
- SSL_OP_NO_COMPRESSION));
- AssertTrue(SSL_set_msg_callback(ssl, msg_cb) == SSL_SUCCESS);
- SSL_set_msg_callback_arg(ssl, arg);
- AssertTrue(SSL_CTX_set_alpn_protos(ctx, protos, len) == SSL_SUCCESS);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- printf(resultFmt, passed);
- #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- }
- static void test_wolfSSL_sk_SSL_CIPHER(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA)
- SSL* ssl;
- SSL_CTX* ctx;
- STACK_OF(SSL_CIPHER) *sk, *dup;
- 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(dup = sk_SSL_CIPHER_dup(sk));
- AssertIntGT(sk_SSL_CIPHER_num(sk), 0);
- AssertIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dup));
- /* 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(dup);
- /* 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 /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
- }
- /* Testing wolfSSL_set_tlsext_status_type function.
- * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
- */
- static void 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));
- AssertTrue(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp)
- == SSL_SUCCESS);
- SSL_free(ssl);
- SSL_CTX_free(ctx);
- #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
- }
- #ifndef NO_BIO
- static void 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));
- AssertNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
- AssertIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
- 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) */
- }
- #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 void 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;
- }
- /* 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()));
- 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
- }
- #endif /* !NO_BIO */
- static void 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
- }
- static void 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
- }
- static void test_wolfSSL_a2i_ASN1_INTEGER(void)
- {
- #ifdef OPENSSL_EXTRA
- 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
- }
- static void 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
- }
- static void 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()");
- 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);
- 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
- }
- static void 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
- }
- static void 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 = XTIME(0);
- not_after = XTIME(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));
- X509_free(x509);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void test_wolfSSL_X509_get_ext_count(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
- 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
- }
- static void test_wolfSSL_X509_sign2(void)
- {
- #if defined(OPENSSL_EXTRA) && !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, 0x04, 0x25, 0x30, 0x82, 0x03, 0x0D,
- 0xA0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x09, 0x00,
- 0xF1, 0x5C, 0x99, 0x43, 0x66, 0x3D, 0x96, 0x04,
- 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, 0x6E, 0x30,
- 0x6C, 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, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01,
- 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C,
- 0x65, 0x2E, 0x63, 0x6F, 0x6D, 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, 0x1F, 0x06, 0x03,
- 0x55, 0x1D, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
- 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7, 0x68, 0x87,
- 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7,
- 0x26, 0xD7, 0x85, 0x65, 0xC0, 0x30, 0x0D, 0x06,
- 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01,
- 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01,
- 0x00, 0x19, 0xE7, 0xD0, 0x9A, 0xF9, 0x90, 0xAA,
- 0xAD, 0x63, 0x58, 0x21, 0x38, 0xA2, 0x4D, 0x30,
- 0x9A, 0x6F, 0x88, 0x9E, 0x9B, 0xFB, 0xDE, 0x73,
- 0xF2, 0x38, 0xFC, 0x7E, 0x60, 0xC5, 0xFA, 0xBB,
- 0x64, 0xA0, 0xD2, 0xC0, 0xBD, 0xB6, 0x4A, 0xAC,
- 0x38, 0x90, 0xF5, 0xEE, 0xEC, 0x43, 0x90, 0x7D,
- 0x5B, 0xF0, 0x22, 0xA0, 0xAC, 0x59, 0x10, 0xE2,
- 0x8D, 0x16, 0xDA, 0x3A, 0xAB, 0x0F, 0x94, 0x11,
- 0x6C, 0x0C, 0x61, 0xC1, 0xFD, 0xB5, 0xA3, 0xFC,
- 0xE7, 0xFD, 0x0C, 0x63, 0x20, 0xE5, 0x00, 0xCE,
- 0xFD, 0xEE, 0x21, 0xE1, 0xE1, 0x9D, 0x48, 0x9B,
- 0x71, 0x9C, 0x80, 0x39, 0x5E, 0x5A, 0xD3, 0x32,
- 0xA6, 0xAC, 0x3F, 0x84, 0x8C, 0xB6, 0xBC, 0x70,
- 0x90, 0xE9, 0xC1, 0x0F, 0xAB, 0xA5, 0x97, 0xD4,
- 0xE0, 0x8E, 0x3B, 0xBB, 0x02, 0xE0, 0xED, 0xB0,
- 0x10, 0xE8, 0x3F, 0x49, 0xD2, 0x46, 0x4E, 0xE7,
- 0x72, 0x0F, 0x1A, 0xFD, 0xE4, 0x59, 0x84, 0x24,
- 0xA9, 0x7B, 0x9D, 0x8E, 0x8C, 0xBC, 0xEA, 0xD1,
- 0x04, 0x1F, 0xC6, 0x30, 0x47, 0xBD, 0xCC, 0xD1,
- 0xBC, 0x87, 0x00, 0xB5, 0x23, 0x3C, 0x60, 0x8F,
- 0xB2, 0xDB, 0x71, 0xD2, 0xF5, 0xBA, 0xEB, 0xB1,
- 0xD0, 0x53, 0xAC, 0x2E, 0x2C, 0xA5, 0x5D, 0x41,
- 0xCD, 0x9B, 0x4F, 0x8B, 0x41, 0xA1, 0x5D, 0x8E,
- 0xD9, 0x89, 0x5B, 0x5C, 0x58, 0x1C, 0x4A, 0xE6,
- 0x22, 0xC8, 0x15, 0x2D, 0x8E, 0x24, 0x48, 0xF8,
- 0xB2, 0x3C, 0x7A, 0x72, 0x62, 0xEC, 0xB2, 0x76,
- 0xAD, 0x3D, 0x42, 0x29, 0xE9, 0x3B, 0x4E, 0x7F,
- 0x06, 0xA4, 0xA4, 0x72, 0x55, 0xDD, 0x1C, 0x69,
- 0x5E, 0x2B, 0x7E, 0xB7, 0x7C, 0xBD, 0xF6, 0x2F,
- 0xC9, 0x9A, 0x33, 0x31, 0xD9, 0x92, 0x32, 0xB6,
- 0x60, 0x4D, 0x8F, 0x5B, 0xF2, 0xAE, 0xD5, 0x72,
- 0x88, 0x92, 0x75, 0xC4, 0xDC, 0xBD, 0x0B, 0xB8, 0x9D
- };
- 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
- }
- static void test_wolfSSL_X509_sign(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
- int ret;
- char *caSubject;
- 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 ip_type[] = {127,0,0,1};
- AssertIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip_type,
- sizeof(ip_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.0.0.1", 0), 1);
- #endif
- AssertIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
- WOLFSSL_SUCCESS);
- #if 0
- /* example for writing to file */
- XFILE tmpFile = XFOPEN("./signed.der", "wb");
- if (tmpFile) {
- int derSz = 0;
- const byte* der = wolfSSL_X509_get_der(x509, &derSz);
- XFWRITE(der, 1, derSz, tmpFile);
- }
- XFCLOSE(tmpFile);
- #endif
- /* Variation in size depends on ASN.1 encoding when MSB is set */
- #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 935-936 with 16 byte serial number */
- AssertTrue((ret == 919 + snSz) || (ret == 920 + snSz));
- #else
- /* Valid case - size should be 926-927 with 16 byte serial number */
- AssertTrue((ret == 910 + snSz) || (ret == 911 + snSz));
- #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 = wolfSSL_d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
- AssertNotNull(caSubject = wolfSSL_X509_NAME_oneline(
- X509_get_subject_name(ca), 0, 0));
- AssertIntEQ(0, XSTRNCMP(caSubject, dCert.subject, XSTRLEN(caSubject)));
- XFREE(caSubject, 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(caSubject = wolfSSL_X509_NAME_oneline(
- X509_get_issuer_name(x509), 0, 0));
- XFREE(caSubject, 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
- }
- static void 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
- }
- static void test_wolfSSL_X509_ALGOR_get0(void)
- {
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_SHA256)
- 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);
- AssertNotNull(ppval);
- AssertIntNE(pptype, 0);
- /* Make sure NID of X509_ALGOR is Sha256 with RSA */
- AssertIntEQ(OBJ_obj2nid(obj), CTC_SHA256wRSA);
- X509_free(x509);
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_X509_VERIFY_PARAM(void)
- {
- #if defined(OPENSSL_EXTRA)
- WOLFSSL_X509_VERIFY_PARAM *paramTo;
- WOLFSSL_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 = wolfSSL_X509_VERIFY_PARAM_new();
- AssertNotNull(paramTo);
- XMEMSET(paramTo, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM ));
- paramFrom = wolfSSL_X509_VERIFY_PARAM_new();
- AssertNotNull(paramFrom);
- XMEMSET(paramFrom, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM ));
- ret = wolfSSL_X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
- (int)XSTRLEN(testhostName1));
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
- (int)XSTRLEN(testhostName1)));
- wolfSSL_X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
- wolfSSL_X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
- AssertIntEQ(0x01, paramFrom->hostFlags);
- ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4);
- AssertIntEQ(0, ret);
- ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4);
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
- ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL);
- AssertIntEQ(1, ret);
- ret = wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6);
- AssertIntEQ(1, ret);
- AssertIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
- /* null pointer */
- ret = wolfSSL_X509_VERIFY_PARAM_set1(NULL, paramFrom);
- AssertIntEQ(WOLFSSL_FAILURE, ret);
- /* in the case of "from" null, returns success */
- ret = wolfSSL_X509_VERIFY_PARAM_set1(paramTo, NULL);
- AssertIntEQ(WOLFSSL_SUCCESS, ret);
- ret = wolfSSL_X509_VERIFY_PARAM_set1(NULL, NULL);
- AssertIntEQ(WOLFSSL_FAILURE, ret);
- /* inherit flags test : VPARAM_DEFAULT */
- ret = wolfSSL_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 */
- wolfSSL_X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
- (int)XSTRLEN(testhostName2));
- wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
- wolfSSL_X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
- paramTo->inherit_flags = WOLFSSL_VPARAM_OVERWRITE;
- ret = wolfSSL_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 */
- wolfSSL_X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
- (int)XSTRLEN(testhostName2));
- wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
- wolfSSL_X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
- paramTo->inherit_flags = WOLFSSL_VPARAM_RESET_FLAGS;
- ret = wolfSSL_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 */
- wolfSSL_X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
- (int)XSTRLEN(testhostName2));
- wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4);
- wolfSSL_X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
- paramTo->inherit_flags = WOLFSSL_VPARAM_LOCKED;
- ret = wolfSSL_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));
- /* inherit flags test : VPARAM_ONCE, not testable yet */
- ret = wolfSSL_X509_VERIFY_PARAM_set_flags(paramTo, WOLFSSL_CRL_CHECKALL);
- AssertIntEQ(1, ret);
- ret = wolfSSL_X509_VERIFY_PARAM_get_flags(paramTo);
- AssertIntEQ(WOLFSSL_CRL_CHECKALL, ret);
- ret = wolfSSL_X509_VERIFY_PARAM_clear_flags(paramTo, WOLFSSL_CRL_CHECKALL);
- AssertIntEQ(1, ret);
- ret = wolfSSL_X509_VERIFY_PARAM_get_flags(paramTo);
- AssertIntEQ(0, ret);
- wolfSSL_X509_VERIFY_PARAM_free(paramTo);
- wolfSSL_X509_VERIFY_PARAM_free(paramFrom);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void test_wolfSSL_X509_PUBKEY(void)
- {
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_SHA256)
- X509* x509 = NULL;
- ASN1_OBJECT* obj = NULL;
- X509_PUBKEY* pubKey;
- X509_PUBKEY* pubKey2;
- EVP_PKEY* evpKey;
- const unsigned char *pk;
- int ppklen;
- WOLFSSL_X509_ALGOR *pa;
- printf(testingFmt, "wolfSSL_X509_get_X509_PUBKEY");
- 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), RSAk);
- AssertNotNull(evpKey = X509_PUBKEY_get(pubKey));
- AssertNotNull(pubKey2 = X509_PUBKEY_new());
- AssertIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
- X509_PUBKEY_free(pubKey2);
- X509_free(x509);
- EVP_PKEY_free(evpKey);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void 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 */
- }
- #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 void test_wolfSSL_RAND_set_rand_method(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
- WOLFSSL_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);
- wolfSSL_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(wolfSSL_RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_RAND_seed(buf, num), 123);
- AssertIntEQ(wolfSSL_RAND_bytes(buf, num), 456);
- AssertIntEQ(wolfSSL_RAND_pseudo_bytes(buf, num), 9876);
- AssertIntEQ(wolfSSL_RAND_status(), 5432);
- AssertIntEQ(*was_add_called, 0);
- /* The function pointer for RAND_add returns int, but RAND_add itself returns void. */
- wolfSSL_RAND_add(buf, num, entropy);
- AssertIntEQ(*was_add_called, 1);
- was_add_called = 0;
- AssertIntEQ(*was_cleanup_called, 0);
- wolfSSL_RAND_Cleanup();
- AssertIntEQ(*was_cleanup_called, 1);
- *was_cleanup_called = 0;
- AssertIntEQ(wolfSSL_RAND_set_rand_method(NULL), WOLFSSL_SUCCESS);
- AssertIntNE(wolfSSL_RAND_status(), 5432);
- AssertIntEQ(*was_cleanup_called, 0);
- wolfSSL_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 */
- }
- static void 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
- }
- static void test_wolfSSL_BN_rand(void)
- {
- #if defined(OPENSSL_EXTRA)
- BIGNUM* bn;
- printf(testingFmt, "wolfSSL_BN_rand()");
- AssertNotNull(bn = BN_new());
- AssertIntNE(BN_rand(bn, 0, 0, 0), SSL_SUCCESS);
- BN_free(bn);
- AssertNotNull(bn = BN_new());
- AssertIntEQ(BN_rand(bn, 8, 0, 0), SSL_SUCCESS);
- BN_free(bn);
- AssertNotNull(bn = BN_new());
- AssertIntEQ(BN_rand(bn, 64, 0, 0), SSL_SUCCESS);
- BN_free(bn);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void 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];
- 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));
- BIO_free(bio);
- PKCS8_PRIV_KEY_INFO_free(pt);
- printf(resultFmt, passed);
- #endif
- #endif
- }
- static void test_wolfSSL_PKCS8_d2i(void)
- {
- #ifndef HAVE_FIPS
- /* 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. */
- #ifdef OPENSSL_ALL
- WOLFSSL_EVP_PKEY* pkey = NULL;
- #ifndef NO_FILESYSTEM
- unsigned char pkcs8_buffer[2048];
- const unsigned char* p;
- int bytes;
- XFILE file;
- #ifndef NO_BIO
- BIO* bio;
- #if defined(HAVE_ECC)
- WOLFSSL_EVP_PKEY* evpPkey = NULL;
- #endif
- #endif
- #endif
- #ifndef NO_RSA
- #ifndef NO_FILESYSTEM
- 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
- #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;
- #ifndef NO_FILESYSTEM
- 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
- #endif
- #ifndef NO_FILESYSTEM
- (void)pkcs8_buffer;
- (void)p;
- (void)bytes;
- (void)file;
- #ifndef NO_BIO
- (void)bio;
- #endif
- #endif
- #ifndef NO_RSA
- /* Try to auto-detect normal RSA private key */
- AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
- wolfSSL_EVP_PKEY_free(pkey);
- #endif
- #ifdef HAVE_ECC
- /* Try to auto-detect normal EC private key */
- AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
- wolfSSL_EVP_PKEY_free(pkey);
- #endif
- #ifndef NO_FILESYSTEM
- #ifndef NO_RSA
- /* Get DER encoded RSA PKCS#8 data. */
- file = XFOPEN(rsaDerPkcs8File, "rb");
- AssertTrue(file != XBADFILE);
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- p = pkcs8_buffer;
- /* Try to decode - auto-detect key type. */
- AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
- /* 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);
- #ifndef NO_BIO
- 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(wolfSSL_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"));
- wolfSSL_EVP_PKEY_free(evpPkey);
- BIO_free(bio);
- #endif
- #endif /* !NO_BIO */
- wolfSSL_EVP_PKEY_free(pkey);
- /* PKCS#8 encrypted RSA key */
- #ifndef NO_DES3
- file = XFOPEN(rsaDerPkcs8EncFile, "rb");
- AssertTrue(file != XBADFILE);
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- #ifndef NO_BIO
- AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
- AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
- (void*)"yassl123"));
- wolfSSL_EVP_PKEY_free(pkey);
- BIO_free(bio);
- #endif
- #endif
- #endif
- #ifdef HAVE_ECC
- /* PKCS#8 encode EC key */
- file = XFOPEN(ecDerPkcs8File, "rb");
- AssertTrue(file != XBADFILE);
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- p = pkcs8_buffer;
- /* Try to decode - auto-detect key type. */
- AssertNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
- /* Get PEM encoded RSA PKCS#8 data. */
- file = XFOPEN(ecPemPkcs8File, "rb");
- AssertTrue(file != XBADFILE);
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- #ifndef NO_BIO
- 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(wolfSSL_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"));
- wolfSSL_EVP_PKEY_free(evpPkey);
- BIO_free(bio);
- #endif
- wolfSSL_EVP_PKEY_free(pkey);
- /* PKCS#8 encrypted EC key */
- #ifndef NO_DES3
- file = XFOPEN(ecDerPkcs8EncFile, "rb");
- AssertTrue(file != XBADFILE);
- AssertIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
- file)), 0);
- XFCLOSE(file);
- #ifndef NO_BIO
- AssertNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
- AssertNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
- (void*)"yassl123"));
- wolfSSL_EVP_PKEY_free(pkey);
- BIO_free(bio);
- #endif
- #endif
- #endif
- #endif
- printf(resultFmt, passed);
- #endif
- #endif /* HAVE_FIPS */
- }
- #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);
- return 0;
- }
- #endif
- static void 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
- }
- static void 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);
- /* 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
- }
- #ifndef NO_BIO
- static void 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
- }
- #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 void 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
- }
- /*
- * 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);
- #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);
- #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;
- printf(testingFmt, "wc_ERR_print_errors_fp()");
- WOLFSSL_ERROR(BAD_FUNC_ARG);
- XFILE 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);
- if (sz == 0) {
- ret = BAD_FUNC_ARG;
- }
- #endif
- printf(resultFmt, ret == 0 ? passed : failed);
- 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);
- 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 void 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);
- HMAC_cleanup(hmac);
- HMAC_CTX_free(hmac);
- len = 0;
- AssertNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
- AssertIntEQ(len, md_len);
- }
- #endif
- static void 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
- }
- static void 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)
- ASN1_OBJECT *obj = 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);
- ASN1_OBJECT_free(obj);
- 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
- }
- static void 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
- }
- static void 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
- }
- static void 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
- }
- static void 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 }
- };
- 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));
- }
- printf(resultFmt, passed);
- #endif
- }
- static void 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";
- 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);
- /* 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), WOLFSSL_SUCCESS);
- #ifndef NO_BIO
- BIO_free(bio);
- #endif
- X509_free(x509); /* free's nm */
- printf(resultFmt, passed);
- #endif
- }
- static void 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 */
- }
- static void 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)
- /* 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
- }
- static void 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)
- 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
- }
- static void 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
- }
- #ifndef NO_BIO
- static void 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
- }
- static void 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
- }
- #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES)
- 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 void 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)
- 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()));
- 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
- }
- static void test_wolfSSL_BIO_connect(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES)
- tcp_ready ready;
- func_args server_args;
- THREAD_TYPE serverThread;
- BIO *tcp_bio;
- BIO *ssl_bio;
- SSL_CTX* ctx;
- SSL *ssl;
- char msg[] = "hello wolfssl!";
- char reply[30];
- char buff[10] = {0};
- printf(testingFmt, "wolfSSL_BIO_new_connect()");
- /* 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(tcp_bio = BIO_new_connect(wolfSSLIP));
- AssertIntEQ(BIO_set_conn_port(tcp_bio, buff), 1);
- /* Setup the SSL object */
- 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));
- AssertNotNull(ssl = SSL_new(ctx));
- SSL_set_connect_state(ssl);
- /* Setup the SSL BIO */
- AssertNotNull(ssl_bio = BIO_new(BIO_f_ssl()));
- AssertIntEQ(BIO_set_ssl(ssl_bio, ssl, BIO_CLOSE), 1);
- /* Link BIO's so that ssl_bio uses tcp_bio for IO */
- AssertPtrEq(BIO_push(ssl_bio, tcp_bio), ssl_bio);
- /* Do TCP connect */
- AssertIntEQ(BIO_do_connect(ssl_bio), 1);
- /* Do TLS handshake */
- AssertIntEQ(BIO_do_handshake(ssl_bio), 1);
- /* Test writing */
- AssertIntEQ(BIO_write(ssl_bio, msg, sizeof(msg)), sizeof(msg));
- /* Expect length of default wolfSSL reply */
- AssertIntEQ(BIO_read(ssl_bio, reply, sizeof(reply)), 23);
- /* Clean it all up */
- BIO_free_all(ssl_bio);
- SSL_CTX_free(ctx);
- /* Server clean up */
- join_thread(serverThread);
- FreeTcpReady(&ready);
- printf(resultFmt, passed);
- #endif
- }
- static void 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 */
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void 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
- }
- #endif /* !NO_BIO */
- static void test_wolfSSL_SESSION(void)
- {
- #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
- !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
- WOLFSSL* ssl;
- WOLFSSL_CTX* ctx;
- WOLFSSL_SESSION* sess;
- WOLFSSL_SESSION* sess_copy;
- unsigned char* sessDer = NULL;
- unsigned char* ptr = NULL;
- #ifdef OPENSSL_EXTRA
- const unsigned char context[] = "user app context";
- unsigned int contextSz = (unsigned int)sizeof(context);
- #endif
- int ret, err, sockfd, sz;
- tcp_ready ready;
- func_args server_args;
- THREAD_TYPE serverThread;
- char msg[80];
- 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, 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);
- #ifdef WOLFSSL_ENCRYPTED_KEYS
- wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
- #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), SSL_SUCCESS);
- err = 0; /* Reset error */
- 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);
- if (ret != SSL_SUCCESS) {
- err = wolfSSL_get_error(ssl, 0);
- }
- } while (ret != SSL_SUCCESS && err == WC_PENDING_E);
- AssertIntEQ(ret, SSL_SUCCESS);
- AssertIntEQ(wolfSSL_write(ssl, "GET", 3), 3);
- AssertIntEQ(wolfSSL_read(ssl, msg, sizeof(msg)), 23);
- sess = wolfSSL_get_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
- AssertNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
- wolfSSL_SESSION_free(sess_copy);
- /* 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);
- 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);
- AssertIntGT(wolfSSL_SESSION_get_time(sess), 0);
- AssertIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
- /* successful set session test */
- AssertNotNull(ssl = wolfSSL_new(ctx));
- AssertIntEQ(wolfSSL_set_session(ssl, sess), SSL_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
- /* 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
- wolfSSL_free(ssl);
- SSL_SESSION_free(sess);
- wolfSSL_CTX_free(ctx);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- #ifndef NO_BIO
- static void 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
- BIO_free(bio);
- (void)pkey;
- printf(resultFmt, passed);
- #endif
- }
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
- static void 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(rsa = d2i_RSAPrivateKey_bio(bio, NULL));
- 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 = NULL;
- AssertIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
- sizeof_client_key_der_2048);
- AssertNotNull(bufPtr);
- XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
- #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);
- }
- #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
- #endif /* !NO_BIO */
- static void 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;
- 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);
- 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);
- sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void 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;
- printf(testingFmt, "wolfSSL_RSA()");
- AssertNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
- AssertIntEQ(RSA_size(rsa), 256);
-
- /* sanity check */
- AssertIntEQ(RSA_bits(NULL), 0);
-
- 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(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(511, 3, NULL, NULL)); /* RSA_MIN_SIZE - 1 */
- AssertNull(RSA_generate_key(4097, 3, NULL, NULL)); /* RSA_MAX_SIZE + 1 */
- 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
- }
- static void 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;
- 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()");
- for (i = 0; tbl[i].der != NULL; i++)
- {
- AssertNotNull(d2i_RSAPublicKey(&rsa, &tbl[i].der, tbl[i].sz));
- AssertNotNull(rsa);
- RSA_free(rsa);
- }
- for (i = 0; tbl[i].der != NULL; i++)
- {
- AssertNotNull(d2i_RSAPrivateKey(&rsa, &tbl[i].der, tbl[i].sz));
- AssertNotNull(rsa);
- RSA_free(rsa);
- }
- for (i = 0; pub[i].der != NULL; i++)
- {
- AssertNotNull(d2i_RSAPublicKey(&rsa, &pub[i].der, pub[i].sz));
- AssertNotNull(rsa);
- AssertIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
- buff = NULL;
- AssertIntEQ(i2d_RSAPublicKey(rsa, &buff), pub[i].sz);
- AssertNotNull(buff);
- AssertIntEQ(0, memcmp((void *)buff, (void *)pub[i].der, pub[i].sz));
- XFREE((void *)buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
- RSA_free(rsa);
- }
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void 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_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
- AssertNotNull(rsa = RSA_new());
- AssertIntEQ(RSA_set_method(rsa, rsa_meth), 1);
- AssertPtrEq(RSA_get_method(rsa), rsa_meth);
- AssertIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
- RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
- AssertIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
- /* rsa_meth is freed here */
- RSA_free(rsa);
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_verify_mode(void)
- {
- #if defined(OPENSSL_ALL)
- 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);
- 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
- }
- static void 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
- }
- #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 void 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 */
- #ifndef NO_MD5
- 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
- }
- #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 void 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
- }
- static void test_wolfSSL_SHA(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
- printf(testingFmt, "wolfSSL_SHA()");
- #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES)
- {
- 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);
- 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
- }
- static void 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
- }
- static void test_wolfSSL_PEM_write_DHparams(void)
- {
- #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
- #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && !defined(NO_FILESYSTEM)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- #ifndef NO_BIO
- 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 /* !NO_BIO */
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* OPENSSL_ALL || OPENSSL_QT */
- #endif
- }
- static void 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
- }
- static void 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));
- /* 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)))
- AssertIntEQ(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash), 0);
- AssertIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
- AssertIntEQ(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash), 0);
- AssertIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
- #endif
- printf(resultFmt, passed);
- #endif
- }
- static void 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;
- 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";
- MD5_CTX md5;
- printf(testingFmt, "wolfSSL_MD5_Transform()");
- XMEMSET(&md5, 0, sizeof(md5));
- 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, NULL), 0);
- AssertIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
- AssertIntEQ(wc_Md5Transform((wc_Md5*)&md5, NULL), BAD_FUNC_ARG);
- /* Init MD5 CTX */
- AssertIntEQ(wolfSSL_MD5_Init(&md5), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(MD5_Transform(&md5, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Md5*)&md5)->digest[0], output1,
- WC_MD5_DIGEST_SIZE), 0);
- /* Init MD5 CTX */
- AssertIntEQ(MD5_Init(&md5), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(MD5_Transform(&md5, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Md5*)&md5)->digest[0], output2,
- WC_MD5_DIGEST_SIZE), 0);
- printf(resultFmt, passed);
- #endif
- }
- static void 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);
- AssertNotNull(SHA224(input, inLen, hash));
- AssertIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
- printf(resultFmt, passed);
- #endif
- }
- static void 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;
- 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";
- SHA_CTX sha;
- SHA_CTX sha1;
- printf(testingFmt, "wolfSSL_SHA_Transform()");
- XMEMSET(&sha, 0, sizeof(sha));
- 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, NULL), 0);
- AssertIntEQ(SHA1_Transform(NULL, NULL), 0);
- AssertIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
- AssertIntEQ(SHA1_Transform(&sha, NULL), 0);
- AssertIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
- AssertIntEQ(wc_ShaTransform((wc_Sha*)&sha, NULL), BAD_FUNC_ARG);
- /* Init SHA CTX */
- AssertIntEQ(SHA_Init(&sha), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(SHA_Transform(&sha, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha)->digest[0], output1,
- WC_SHA_DIGEST_SIZE), 0);
- /* Init SHA CTX */
- AssertIntEQ(SHA_Init(&sha), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(SHA_Transform(&sha, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha)->digest[0], output2,
- WC_SHA_DIGEST_SIZE), 0);
- /* SHA1 */
- XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
- /* Init SHA CTX */
- AssertIntEQ(SHA1_Init(&sha1), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(SHA1_Transform(&sha1, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha1)->digest[0], output1,
- WC_SHA_DIGEST_SIZE), 0);
- /* Init SHA CTX */
- AssertIntEQ(SHA1_Init(&sha1), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(SHA1_Transform(&sha1, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Sha*)&sha1)->digest[0], output2,
- WC_SHA_DIGEST_SIZE), 0);
-
- printf(resultFmt, passed);
- #endif
- #endif
- }
- static void 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)))
- byte input1[] = "";
- byte input2[] = "abc";
- byte local[WC_SHA256_BLOCK_SIZE];
- word32 sLen = 0;
- 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";
- SHA256_CTX sha256;
- printf(testingFmt, "wolfSSL_SHA256_Transform()");
- XMEMSET(&sha256, 0, sizeof(sha256));
- 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, NULL), 0);
- AssertIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
- AssertIntEQ(wc_Sha256Transform((wc_Sha256*)&sha256, NULL), BAD_FUNC_ARG);
- /* Init SHA256 CTX */
- AssertIntEQ(SHA256_Init(&sha256), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(SHA256_Transform(&sha256, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Sha256*)&sha256)->digest[0], output1,
- WC_SHA256_DIGEST_SIZE), 0);
- /* Init SHA256 CTX */
- AssertIntEQ(SHA256_Init(&sha256), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(SHA256_Transform(&sha256, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Sha256*)&sha256)->digest[0], output2,
- WC_SHA256_DIGEST_SIZE), 0);
- printf(resultFmt, passed);
- #endif
- #endif
- }
- static void 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
- }
- static void 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;
- 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";
- SHA512_CTX sha512;
- printf(testingFmt, "wolfSSL_SHA512_Transform()");
- XMEMSET(&sha512, 0, sizeof(sha512));
- 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, NULL), 0);
- AssertIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
- AssertIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
- AssertIntEQ(wc_Sha512Transform((wc_Sha512*)&sha512, NULL), BAD_FUNC_ARG);
- /* Init SHA512 CTX */
- AssertIntEQ(wolfSSL_SHA512_Init(&sha512), 1);
- /* Do Transform*/
- sLen = (word32)XSTRLEN((char*)input1);
- XMEMCPY(local, input1, sLen);
- AssertIntEQ(SHA512_Transform(&sha512, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Sha512*)&sha512)->digest[0], output1,
- WC_SHA512_DIGEST_SIZE), 0);
- /* Init SHA512 CTX */
- AssertIntEQ(SHA512_Init(&sha512), 1);
- sLen = (word32)XSTRLEN((char*)input2);
- XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
- XMEMCPY(local, input2, sLen);
- AssertIntEQ(SHA512_Transform(&sha512, (const byte*)&local[0]), 1);
- AssertIntEQ(XMEMCMP(&((wc_Sha512*)&sha512)->digest[0], output2,
- WC_SHA512_DIGEST_SIZE), 0);
- (void)input1;
- printf(resultFmt, passed);
- #endif
- #endif
- }
- static void 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
- }
- static void 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);
- wolfSSL_Cleanup();
- AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(),WOLFSSL_SUCCESS);
- wolfSSL_Cleanup();
- AssertIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(),WOLFSSL_SUCCESS);
- wolfSSL_Cleanup();
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void 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
- }
- static void 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
- }
- static void 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);
- x509 = wolfSSL_X509_load_certificate_file(ntruCertFile, WOLFSSL_FILETYPE_PEM);
- AssertIntEQ(wolfSSL_X509_check_ca(x509), 0);
- wolfSSL_X509_free(x509);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void test_wolfSSL_DC_cert(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_KEY_GEN) && \
- defined(WOLFSSL_CERT_EXT)
- Cert cert;
- RsaKey key;
- WC_RNG rng;
- byte der[FOURK_BUF];
- int certSz;
- int ret, idx;
- const byte mySerial[8] = {1,2,3,4,5,6,7,8};
- const unsigned char* pt;
- X509* x509;
- X509_NAME* x509name;
- X509_NAME_ENTRY* entry;
- ASN1_STRING* entryValue;
- CertName name;
- printf(testingFmt, "wolfSSL Certs with DC");
- 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;
- #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, devId), 0);
- #else
- AssertIntEQ(wc_InitRng(&rng), 0);
- #endif
- AssertIntEQ(wc_MakeRsaKey(&key, 2048, 3, &rng), 0);
- 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);
- certSz = ret;
- /* der holds a certificate with DC's now check X509 parsing of it */
- 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));
- (void)idx;
- X509_free(x509);
- wc_FreeRsaKey(&key);
- wc_FreeRng(&rng);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void 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
- }
- static void 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;
- const int enc1 = AES_ENCRYPT;
- const int enc2 = AES_DECRYPT;
- /* 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, enc1, ct128, AES_BLOCK_SIZE, 0);
- STRESS_T(pt128, out, len, &aes, iv128N, enc1, ct128, AES_BLOCK_SIZE, 0);
- wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, enc1);
- AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
- wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, enc1);
- AssertIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
- STRESS_T(pt128, out, lenB, &aes, iv128tmp, enc1, 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, enc1);
- 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, enc2);
- 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, enc1);
- 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, enc2);
- 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, enc1);
- 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, enc2);
- 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,
- 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,
- 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
- }
- #if defined(OPENSSL_ALL)
- #if !defined(NO_ASN)
- static void test_wolfSSL_ASN1_STRING_to_UTF8(void)
- {
- 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);
- }
- static void 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");
- }
- #endif /* !defined(NO_ASN) */
- static void test_wolfSSL_sk_CIPHER_description(void)
- {
- 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);
- }
- static void test_wolfSSL_get_ciphers_compat(void)
- {
- 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);
- }
- static void 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);
- }
- static void test_wolfSSL_d2i_DHparams(void)
- {
- #if !defined(NO_DH)
- #if !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";
- const char* params2 = "./certs/dh3072.der";
- long len = 0;
- WOLFSSL_DH* dh = NULL;
- XMEMSET(buf, 0, sizeof(buf));
- /* Test 2048 bit parameters */
- 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);
- 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;
- /* Test 3072 bit parameters */
- 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_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* !NO_DH */
- }
- static void test_wolfSSL_i2d_DHparams(void)
- {
- #if !defined(NO_DH)
- #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;
- const char* params1 = "./certs/dh2048.der";
- const char* params2 = "./certs/dh3072.der";
- long len;
- WOLFSSL_DH* dh;
- /* Test 2048 bit parameters */
- 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 cases */
- AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
- AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 264);
- DH_free(dh);
- printf(resultFmt, passed);
- *buf = 0;
- pt = buf;
- pt2 = buf;
- /* Test 3072 bit parameters */
- 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 cases */
- AssertIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
- AssertIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 392);
- DH_free(dh);
- printf(resultFmt, passed);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif
- }
- static void 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(wc_ecc_check_key((ecc_key*)dupKey->internal), 0);
- /* 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);
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
- {
- #if !defined(NO_DSA)
- 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_der1024;
- int dsaKeySz = sizeof_dsa_key_der_1024;
- byte tmp[ONEK_BUF];
- XMEMSET(tmp, 0, sizeof(tmp));
- XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
- bytes = dsa_key_der_sz;
- #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
- 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);
- XFILE fp = XOPEN("./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,
- "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));
- AssertIntEQ(DSA_bits(dsa), 2048);
- /* 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 */
- } /* END test_EVP_PKEY_set1_get1_DSA */
- static void 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 */
- } /* END test_EVP_PKEY_set1_get1_EC_KEY */
- static void test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
- {
- #if !defined(NO_DH)
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- 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 /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* NO_DH */
- } /* END test_EVP_PKEY_set1_get1_DH */
- static void 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
- /* 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) */
- }
- static void 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 */
- }
- static void test_wolfSSL_EVP_PKEY_assign(void)
- {
- #if defined(OPENSSL_ALL)
- 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 */
- printf(resultFmt, passed);
- #endif /* OPENSSL_ALL */
- }
- static void test_wolfSSL_EVP_PKEY_base_id(void)
- {
- #if defined(OPENSSL_ALL)
- 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);
- #endif
- }
- static void test_wolfSSL_EVP_PKEY_id(void)
- {
- #if defined(OPENSSL_ALL)
- 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);
- #endif
- }
- static void test_wolfSSL_EVP_PKEY_keygen(void)
- {
- #if defined(OPENSSL_ALL)
- WOLFSSL_EVP_PKEY* pkey;
- EVP_PKEY_CTX *ctx;
- 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);
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_PKEY_keygen_init(void)
- {
- #if defined(OPENSSL_ALL)
- 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);
- #endif
- }
- static void 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
- }
- static void test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
- {
- #if defined(OPENSSL_ALL)
- 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);
- #endif
- }
- static void test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
- {
- #if defined(OPENSSL_ALL)
- /* 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
- #ifdef HAVE_IDEA
- NID_idea_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
- #ifdef HAVE_IDEA
- IDEA_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);
- #endif
- }
- static void test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
- {
- #if defined(OPENSSL_ALL) && !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
- }
- static void test_wolfSSL_EVP_CIPHER_CTX_set_key_length(void)
- {
- #if defined(OPENSSL_ALL) && !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
- }
- static void test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_AESGCM) && !defined(NO_DES3)
- byte key[DES3_KEY_SIZE] = {0};
- byte iv[DES_IV_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
- }
- static void test_wolfSSL_EVP_PKEY_CTX_new_id(void)
- {
- #if defined(OPENSSL_ALL)
- 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);
- #endif
- }
- static void test_wolfSSL_EVP_rc4(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_RC4)
- printf(testingFmt, "wolfSSL_EVP_rc4");
- AssertNotNull(wolfSSL_EVP_rc4());
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_enc_null(void)
- {
- #if defined(OPENSSL_ALL)
- printf(testingFmt, "wolfSSL_EVP_enc_null");
- AssertNotNull(wolfSSL_EVP_enc_null());
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_rc2_cbc(void)
- {
- #if defined(OPENSSL_ALL) && defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
- printf(testingFmt, "wolfSSL_EVP_rc2_cbc");
- AssertNull(wolfSSL_EVP_rc2_cbc());
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_mdc2(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
- printf(testingFmt, "wolfSSL_EVP_mdc2");
- AssertNull(wolfSSL_EVP_mdc2());
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_md4(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_MD4)
- printf(testingFmt, "wolfSSL_EVP_md4");
- AssertNotNull(wolfSSL_EVP_md4());
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_aes_256_gcm(void)
- {
- #if defined(OPENSSL_ALL)
- printf(testingFmt, "wolfSSL_EVP_aes_256_gcm");
- AssertNotNull(wolfSSL_EVP_aes_256_gcm());
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_aes_192_gcm(void)
- {
- #if defined(OPENSSL_ALL)
- printf(testingFmt, "wolfSSL_EVP_aes_192_gcm");
- AssertNotNull(wolfSSL_EVP_aes_192_gcm());
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_ripemd160(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
- printf(testingFmt, "wolfSSL_EVP_ripemd160");
- AssertNull(wolfSSL_EVP_ripemd160());
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_get_digestbynid(void)
- {
- #if defined(OPENSSL_ALL)
- printf(testingFmt, "wolfSSL_EVP_get_digestbynid");
- AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
- AssertNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
- AssertNull(wolfSSL_EVP_get_digestbynid(0));
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
- {
- #if defined(OPENSSL_ALL)
- WOLFSSL_EVP_PKEY* pkey;
- printf(testingFmt, "wolfSSL_EVP_PKEY_get0_EC_KEY");
- AssertNotNull(pkey = wolfSSL_EVP_PKEY_new());
- AssertNull(wolfSSL_EVP_PKEY_get0_EC_KEY(pkey));
- EVP_PKEY_free(pkey);
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_X_STATE(void)
- {
- #if defined(OPENSSL_ALL) && !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
- }
- static void test_wolfSSL_EVP_X_STATE_LEN(void)
- {
- #if defined(OPENSSL_ALL) && !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
- }
- static void test_wolfSSL_EVP_CIPHER_block_size(void)
- {
- #if defined(OPENSSL_ALL)
- #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_AES_GCM
- #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
- #endif /* OPENSSL_ALL */
- }
- static void test_wolfSSL_EVP_CIPHER_iv_length(void)
- {
- #if defined(OPENSSL_ALL)
- int i, enumlen;
- int enumArray[] = {
- #ifdef HAVE_AES_CBC
- NID_aes_128_cbc,
- #endif
- #ifdef WOLFSSL_AES_192
- NID_aes_192_cbc,
- #endif
- #ifdef WOLFSSL_AES_256
- NID_aes_256_cbc,
- #endif
- #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
- #ifdef HAVE_IDEA
- NID_idea_cbc,
- #endif
- };
- int iv_lengths[] = {
- #ifdef HAVE_AES_CBC
- AES_BLOCK_SIZE,
- #endif
- #ifdef WOLFSSL_AES_192
- AES_BLOCK_SIZE,
- #endif
- #ifdef WOLFSSL_AES_256
- AES_BLOCK_SIZE,
- #endif
- #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
- #ifdef HAVE_IDEA
- IDEA_BLOCK_SIZE,
- #endif
- };
- printf(testingFmt, "wolfSSL_EVP_CIPHER_iv_length");
- enumlen = (sizeof(enumArray)/sizeof(int));
- for(i = 0; i < enumlen; i++)
- {
- const EVP_CIPHER *c = wolfSSL_EVP_get_cipherbynid(enumArray[i]);
- AssertIntEQ(wolfSSL_EVP_CIPHER_iv_length(c), iv_lengths[i]);
- }
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_EVP_SignInit_ex(void)
- {
- #if defined(OPENSSL_ALL)
- 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);
- #endif
- }
- static void test_wolfSSL_EVP_DigestFinal_ex(void)
- {
- #if defined(OPENSSL_ALL) && !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), 0);
- #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
- }
- static void test_wolfSSL_EVP_PKEY_assign_DH(void)
- {
- #if defined(OPENSSL_ALL) && !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
- }
- static void test_wolfSSL_EVP_BytesToKey(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_DES3)
- byte key[AES_BLOCK_SIZE] = {0};
- byte iv[AES_BLOCK_SIZE] = {0};
- int sz = 5;
- int count = 0;
- const EVP_MD* 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, "wolfSSL_EVP_BytesToKey");
- /* Bad cases */
- AssertIntEQ(wolfSSL_EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
- 0);
- AssertIntEQ(wolfSSL_EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
- 16);
- md = "2";
- AssertIntEQ(wolfSSL_EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
- WOLFSSL_FAILURE);
- /* Good case */
- md = "SHA256";
- AssertIntEQ(wolfSSL_EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
- 16);
- printf(resultFmt, passed);
- #endif
- }
- static void test_IncCtr(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_AESGCM) && !defined(NO_DES3)
- byte key[DES3_KEY_SIZE] = {0};
- byte iv[DES_IV_SIZE] = {0};
- int type = EVP_CTRL_GCM_IV_GEN;
- int arg = 0;
- void *ptr;
- ptr = NULL;
- printf(testingFmt, "IncCtr");
- EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
- const EVP_CIPHER *init = EVP_des_ede3_cbc();
- AssertNotNull(ctx);
- wolfSSL_EVP_CIPHER_CTX_init(ctx);
- AssertIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
- ctx->cipher.aes.keylen = 128;
- AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_ctrl(ctx, type, arg, ptr), 0);
- EVP_CIPHER_CTX_free(ctx);
- printf(resultFmt, passed);
- #endif
- }
- static void 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))
- {
- size_t nCurves = 27;
- EC_builtin_curve r[nCurves];
- 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);
- }
- static void 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);
- }
- 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]);
- }
- static void test_wolfSSL_TXT_DB(void)
- {
- #if !defined(NO_FILESYSTEM)
- 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
- }
- static void test_wolfSSL_NCONF(void)
- {
- #if !defined(NO_FILESYSTEM)
- 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
- }
- #endif /* OPENSSL_ALL */
- static void 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
- }
- static void test_wolfSSL_X509V3_EXT_get(void) {
- #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
- 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));
- AssertNotNull(extNid = ext->obj->nid);
- 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
- }
- static void test_wolfSSL_X509V3_EXT(void) {
- #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
- 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));
- AssertNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
- 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));
- AssertIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
- /* URI entry is an ACCESS_DESCRIPTION type */
- AssertNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
- AssertNotNull(adObj = ad->method);
- /* Make sure nid is OCSP */
- AssertIntEQ(wolfSSL_OBJ_obj2nid(adObj), AIA_OCSP_OID);
- /* 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);
- str = (char*)wolfSSL_ASN1_STRING_data(asn1str);
- actual = strcmp(str, "http://127.0.0.1:22220");
- AssertIntEQ(actual, 0);
- wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
- XFREE(ad, NULL, DYNAMIC_TYPE_X509_EXT);
- #else
- (void) aia; (void) ad; (void) adObj; (void) gn;
- #endif
- wolfSSL_X509_free(x509);
- printf(resultFmt, "passed");
- #endif
- }
- static void test_wolfSSL_X509_get_ext(void){
- #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
- 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
- }
- static void test_wolfSSL_X509_get_ext_by_NID(void)
- {
- #if defined(OPENSSL_ALL)
- int rc;
- FILE* f;
- WOLFSSL_X509* x509;
- 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);
- wolfSSL_X509_free(x509);
- #endif
- }
- static void 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
- }
- static void test_wolfSSL_X509_EXTENSION_get_object(void)
- {
- #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
- 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
- }
- static void test_wolfSSL_X509_EXTENSION_get_data(void)
- {
- #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
- 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
- }
- static void test_wolfSSL_X509_EXTENSION_get_critical(void)
- {
- #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL)
- 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
- }
- static void test_wolfSSL_X509V3_EXT_print(void)
- {
- #if !defined(NO_FILESYSTEM) && defined (OPENSSL_ALL) && !defined(NO_BIO)
- 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
- }
- static void test_wolfSSL_X509_cmp(void)
- {
- #if defined(OPENSSL_ALL)
- 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);
- 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
- }
- static void 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
- }
- static void 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
- }
- static void test_wolfSSL_OCSP_id_get0_info(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_OCSP) && !defined(NO_FILESYSTEM)
- X509* cert;
- X509* issuer;
- OCSP_CERTID* id;
- 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);
- 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->dataMax, serial->dataMax);
- AssertIntEQ(XMEMCMP(x509Int->data, serial->data, serial->dataMax), 0);
- OCSP_CERTID_free(id);
- X509_free(cert); /* free's x509Int */
- X509_free(issuer);
- printf(resultFmt, "passed");
- #endif
- }
- static void test_wolfSSL_i2d_OCSP_CERTID(void)
- {
- #if defined(OPENSSL_ALL) && 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
- }
- static void 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
- }
- static void 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
- }
- static void 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
- }
- static void 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
- }
- static void test_wolfSSL_EVP_PKEY_derive(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_DH)
- printf(testingFmt, "wolfSSL_EVP_PKEY_derive()");
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- EVP_PKEY_CTX *ctx;
- unsigned char *skey;
- size_t skeylen;
- EVP_PKEY *pkey, *peerkey;
- const unsigned char* key;
- #ifndef NO_DH
- /* 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 */
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- printf(resultFmt, "passed");
- #endif /* OPENSSL_ALL */
- }
- #ifndef NO_RSA
- static void 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(rsa, em, mHash, EVP_sha256(), -1), 1);
- AssertIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -1), 1);
- RSA_free(rsa);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
- }
- #endif
- static void 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) */
- }
- static void 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
- }
- static void 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
- }
- /*----------------------------------------------------------------------------*
- | wolfCrypt ASN
- *----------------------------------------------------------------------------*/
- static void 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 */
- }
- static void 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.pem";
- 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_PEM));
- 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
- }
- static void 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
- }
- static void 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.pem";
- 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_PEM));
- 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
- }
- static void 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.pem";
- 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_PEM));
- 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
- }
- /*
- * Testing wc_SetSubjectKeyId
- */
- static void test_wc_SetSubjectKeyId(void)
- {
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
- 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
- } /* END test_wc_SetSubjectKeyId */
- /*
- * Testing wc_SetSubject
- */
- static void test_wc_SetSubject(void)
- {
- #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
- 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
- } /* END test_wc_SetSubject */
- static void 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
- }
- /*----------------------------------------------------------------------------*
- | wolfCrypt ECC
- *----------------------------------------------------------------------------*/
- static void 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 */
- }
- static void 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 */
- }
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
- !defined(HAVE_SELFTEST) && \
- !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
- static void 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);
- }
- #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
- static void 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
- }
- static void 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 */
- #ifdef 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 */
- AssertIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
- (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
- /* Decrypt data */
- AssertIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
- WOLFSSL_SUCCESS);
- AssertIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
- #ifdef 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);
- #ifdef 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
- }
- static void test_wolfSSL_EVP_PKEY_sign(void)
- {
- #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;
- 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);
- byte hash[SHA256_DIGEST_LENGTH] = {0};
- SHA256_CTX c;
- byte* sig = NULL;
- byte* sigVerify = NULL;
- size_t siglen = 0;
- size_t rsaKeySz = 2048/8; /* Bytes */
- printf(testingFmt, "wolfSSL_EVP_PKEY_sign()");
- sig = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- AssertNotNull(sig);
- XMEMSET(sig, 0, rsaKeySz);
- AssertNotNull(sigVerify = (byte*)XMALLOC(rsaKeySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
- XMEMSET(sigVerify, 0, rsaKeySz);
- /* 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
- 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_sign_init(ctx), WOLFSSL_SUCCESS);
- AssertIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
- WOLFSSL_SUCCESS);
- /* Sign data */
- AssertIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash, SHA256_DIGEST_LENGTH),
- WOLFSSL_SUCCESS);
- /* Verify signature.
- EVP_PKEY_verify() doesn't exist yet, so use RSA_public_decrypt(). */
- AssertIntEQ(RSA_public_decrypt((int)siglen, sig, sigVerify,
- rsa, RSA_PKCS1_PADDING), SHA256_DIGEST_LENGTH);
- AssertIntEQ(XMEMCMP(hash, sigVerify, SHA256_DIGEST_LENGTH), 0);
- /* error cases */
- AssertIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
- ctx->pkey->type = EVP_PKEY_RSA;
- 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);
- EVP_PKEY_CTX_free(ctx);
- XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
- #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- static void 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
- }
- static void 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);
- AssertIntEQ(EVP_PKEY_cmp(a, b), 0);
- #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
- }
- static void test_ERR_load_crypto_strings(void)
- {
- #if defined(OPENSSL_ALL)
- ERR_load_crypto_strings();
- printf(resultFmt, passed);
- #endif
- }
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
- static void free_x509(X509* x)
- {
- AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
- }
- #endif
- static void 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_free(s);
- AssertNotNull(s = sk_X509_new_null());
- AssertIntEQ(sk_X509_num(s), 0);
- sk_X509_free(s);
- 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
- }
- static void 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), CTC_SHA256wRSA);
- X509_free(x509);
- printf(resultFmt, passed);
- #endif
- }
- static void test_X509_REQ(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
- defined(WOLFSSL_CERT_REQ)
- X509_NAME* name;
- #if !defined(NO_RSA) || defined(HAVE_ECC)
- X509_REQ* req;
- EVP_PKEY* priv;
- EVP_PKEY* pub;
- unsigned char* der = NULL;
- #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;
- int len;
- #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);
- AssertIntEQ(i2d_X509_REQ(req, &der), 643);
- 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);
- EVP_PKEY_free(pub);
- EVP_PKEY_free(priv);
- #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
- }
- static void test_wolfssl_PKCS7(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
- 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);
- #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);
- BIO_free(bio);
- #endif
- PKCS7_free(NULL);
- PKCS7_free(pkcs7);
- printf(resultFmt, passed);
- #endif
- }
- static void 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
- }
- #ifndef NO_BIO
- static void 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_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, devId));
- /* 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;
- #ifndef NO_BIO
- 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);
- #endif
- wc_PKCS7_Free(pkcs7);
- wc_FreeRng(&rng);
- printf(resultFmt, passed);
- #endif
- }
- #ifdef HAVE_SMIME
- static void test_wolfSSL_SMIME_read_PKCS7(void)
- {
- #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
- PKCS7* pkcs7 = NULL;
- BIO* bio = NULL;
- BIO* bcont = NULL;
- XFILE smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r");
- printf(testingFmt, "wolfSSL_SMIME_read_PKCS7()");
- 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);
- 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);
- 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);
- BIO_free(bio);
- if (bcont) BIO_free(bcont);
- wolfSSL_PKCS7_free(pkcs7);
- printf(resultFmt, passed);
- #endif
- }
- #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);
- 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
- return ret;
- }
- #endif /* NO_CERTS */
- #ifdef WOLFSSL_TLS13
- #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
- static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
- #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_X25519, WOLFSSL_ECC_X448 };
- 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"
- #endif
- "";
- #endif /* !defined(NO_ECC_SECP) */
- #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
- #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
- AssertIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1),
- WOLFSSL_SUCCESS);
- #endif
- #ifndef NO_WOLFSSL_CLIENT
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
- WOLFSSL_SUCCESS);
- #endif
- AssertIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
- 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
- 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);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
- WOLFSSL_SUCCESS);
- #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);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- AssertIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
- WOLFSSL_SUCCESS);
- #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
- AssertIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
- BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 0), 0);
- #endif
- AssertIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
- #ifndef NO_WOLFSSL_CLIENT
- AssertIntEQ(wolfSSL_set_max_early_data(clientSsl, 0), SIDE_ERROR);
- #endif
- #ifndef NO_WOLFSSL_SERVER
- #ifndef WOLFSSL_NO_TLS12
- AssertIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
- #endif
- AssertIntEQ(wolfSSL_set_max_early_data(serverSsl, 0), 0);
- #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
- #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 ret;
- }
- #endif
- #ifdef HAVE_PK_CALLBACKS
- #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)
- 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)
- {
- /* Test fail when context associated with WOLFSSL is NULL */
- if (ctx == NULL) {
- return -1;
- }
- (void)ssl;
- /* return 0 on success */
- return wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
- };
- static void test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
- wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
- #ifdef WOLFSSL_AES_128
- AssertIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
- WOLFSSL_SUCCESS);
- #endif
- #ifdef 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 void 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_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
- }
- #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);
- #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);
- 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(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);
- 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);
- 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);
- 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(WOLFSSL_PUBLIC_MP)
- mp_int mp;
- byte buf[256];
- word32 keySz = (word32)sizeof(buf);
- word32 len = (word32)sizeof(buf);
- int encType = WC_TYPE_UNSIGNED_BIN;
- printf(testingFmt, "wc_export_int()");
- if (mp_init(&mp) != MP_OKAY) {
- ret = -1;
- }
- if (ret == 0) {
- ret = wc_export_int(NULL, buf, &len, keySz, encType);
- if (ret == BAD_FUNC_ARG) {
- ret = 0;
- }
- }
- len = sizeof(buf)-1;
- if (ret == 0) {
- ret = wc_export_int(&mp, buf, &len, keySz, encType);
- if (ret == BUFFER_E) {
- ret = 0;
- }
- }
- len = sizeof(buf);
- if (ret == 0) {
- ret = wc_export_int(&mp, buf, &len, keySz, WC_TYPE_HEX_STR);
- }
- if (ret == 0) {
- ret = wc_export_int(&mp, buf, &len, keySz, encType);
- }
- printf(resultFmt, ret == 0 ? passed : failed);
- 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);
- #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, devId);
- }
- wc_FreeRng(&rng);
- printf(resultFmt, ret == 0 ? passed : failed);
- #endif
- return ret;
- }/*End test_wc_InitRngNonce_ex*/
- static void 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);
- #endif
- return;
- }
- static void test_wolfSSL_X509_load_crl_file(void)
- {
- #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM)
- 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);
- }
- X509_STORE_free(store);
- store = NULL;
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_d2i_X509_REQ(void)
- {
- #if defined(WOLFSSL_CERT_REQ) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA))
- /* ./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";
- #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),
- NID_pkcs9_challengePassword);
- AssertNotNull(attr = X509_REQ_get_attr(req, NID_pkcs9_challengePassword));
- 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),
- NID_pkcs9_challengePassword);
- AssertNotNull(attr = X509_REQ_get_attr(req, NID_pkcs9_challengePassword));
- 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);
- EVP_PKEY_free(pub_key);
- }
- #endif /* !NO_DSA && !HAVE_SELFTEST */
- #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
- }
- static void 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
- }
- static void 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
- }
- static void 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
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
- !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
- static void 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[0];
- 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);
- }
- #endif
- static void 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 */
- }
- #ifndef NO_BIO
- static void test_wolfSSL_PEM_X509_INFO_read_bio(void)
- {
- #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM)
- 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(info->x_pkey);
- AssertNotNull(info->x_pkey->dec_pkey);
- AssertIntEQ(EVP_PKEY_bits(info->x_pkey->dec_pkey), 2048);
- 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
- }
- #endif /* !NO_BIO */
- static void 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
- }
- static void test_wolfSSL_ASN1_INTEGER_set(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
- ASN1_INTEGER *a;
- long val;
- int ret;
- printf(testingFmt, "wolfSSL_ASN1_INTEGER_set");
- a = wolfSSL_ASN1_INTEGER_new();
- val = 0;
- ret = ASN1_INTEGER_set(NULL, val);
- AssertIntEQ(ret, 0);
- wolfSSL_ASN1_INTEGER_free(a);
- /* 0 */
- a = wolfSSL_ASN1_INTEGER_new();
- val = 0;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- wolfSSL_ASN1_INTEGER_free(a);
- /* 40 */
- a = wolfSSL_ASN1_INTEGER_new();
- val = 40;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- wolfSSL_ASN1_INTEGER_free(a);
- /* -40 */
- a = wolfSSL_ASN1_INTEGER_new();
- val = -40;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(a->negative, 1);
- wolfSSL_ASN1_INTEGER_free(a);
- /* 128 */
- a = wolfSSL_ASN1_INTEGER_new();
- val = 128;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- wolfSSL_ASN1_INTEGER_free(a);
- /* -128 */
- a = wolfSSL_ASN1_INTEGER_new();
- val = -128;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- AssertIntEQ(a->negative, 1);
- wolfSSL_ASN1_INTEGER_free(a);
- /* 200 */
- a = wolfSSL_ASN1_INTEGER_new();
- val = 200;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- wolfSSL_ASN1_INTEGER_free(a);
- #ifndef TIME_T_NOT_64BIT
- /* int max (2147483647) */
- a = wolfSSL_ASN1_INTEGER_new();
- val = 2147483647;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(ret, 1);
- wolfSSL_ASN1_INTEGER_free(a);
- /* int min (-2147483648) */
- a = wolfSSL_ASN1_INTEGER_new();
- val = -2147483647 - 1;
- ret = ASN1_INTEGER_set(a, val);
- AssertIntEQ(a->negative, 1);
- AssertIntEQ(ret, 1);
- wolfSSL_ASN1_INTEGER_free(a);
- #endif
- printf(resultFmt, passed);
- #endif
- }
- /* 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 void 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 */
- }
- static void 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 */
- }
- #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 void 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(WOLFSSL_QT)
- AssertIntEQ(BIO_get_mem_data(bio, NULL), 3113);
- #else
- AssertIntEQ(BIO_get_mem_data(bio, NULL), 3103);
- #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
- }
- static void 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(rsa = RSA_generate_key(2048, 3, NULL, NULL));
- AssertNotNull(bio = BIO_new_fd(STDOUT_FILENO, BIO_NOCLOSE));
- AssertIntEQ(RSA_print(bio, rsa, 0), SSL_SUCCESS);
- BIO_free(bio);
- wolfSSL_RSA_free(rsa);
- printf(resultFmt, passed);
- #endif
- }
- static void test_wolfSSL_BIO_get_len(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
- BIO *bio;
- const char txt[] = "Some example text to push to the BIO.";
- printf(testingFmt, "wolfSSL_BIO_get_len");
- 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);
- printf(resultFmt, passed);
- #endif
- }
- static void 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','!'};
- const unsigned int MAX_UNPRINTABLE_CHAR = 32;
- const unsigned int MAX_BUF = 255;
- const int LF = 10, CR = 13;
- 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 == LF || i == CR)
- 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 */
- }
- #endif /* !NO_BIO */
- static void 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, 841);
- AssertIntEQ(tag, 0x10);
- AssertIntEQ(cls, 0);
- AssertIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
- len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
- AssertIntEQ(asnLen, 750);
- 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, 9);
- 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 */
- }
- static void test_wolfSSL_RSA_verify(void)
- {
- #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(HAVE_FAST_RSA) && \
- !defined(NO_FILESYSTEM) && defined(HAVE_CRL)
- #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));
- XFSEEK(fp, 0, XSEEK_END);
- 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);
- 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
- }
- #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
- defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
- static void 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)XTIME(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);
- }
- #endif
- static void 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), WOLFSSL_FAILURE);
- #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
- 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)
- 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)
- test_openssl_make_self_signed_certificate(pkey);
- #endif
- EVP_PKEY_free(pkey);
- }
- #endif /* HAVE_ECC */
- #endif /* OPENSSL_EXTRA */
- }
- static void 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 */
- }
- static void 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 */
- }
- static void 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 */
- }
- static void 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 */
- }
- static void 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 */
- }
- static void test_wolfSSL_CTX_LoadCRL(void)
- {
- #ifdef HAVE_CRL
- WOLFSSL_CTX* ctx = NULL;
- const char* badPath = "dummypath";
- const char* validPath = "./certs/crl";
- int derType = WOLFSSL_FILETYPE_ASN1;
- int rawType = WOLFSSL_FILETYPE_RAW;
- int pemType = WOLFSSL_FILETYPE_PEM;
- int monitor = WOLFSSL_CRL_MONITOR;
- #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);
- SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, rawType, monitor);
- wolfSSL_CTX_free(ctx);
- ctx = NULL;
- #endif
- }
- static void 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
- }
- static void 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
- }
- #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;
- }
- AssertIntEQ(test_chainG(cm), 0);
- 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;
- }
- AssertIntEQ(test_chainI(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);
- /* 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);
- return ret;
- }
- #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
- #ifdef HAVE_KEYING_MATERIAL
- 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 void 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, (void*)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 */
- }
- #endif /* HAVE_KEYING_MATERIAL */
- static int test_wolfSSL_THREADID_hash(void)
- {
- int ret = 0;
- WOLFSSL_CRYPTO_THREADID id;
- unsigned long res;
- #if defined(OPENSSL_EXTRA)
- printf(testingFmt, "wolfSSL_THREADID_hash");
- res = wolfSSL_THREADID_hash(NULL);
- AssertTrue( res == 0UL);
- res = wolfSSL_THREADID_hash(&id);
- AssertTrue( res == 0UL);
- printf(resultFmt, passed);
- #endif /* OPENSSL_EXTRA */
- (void)id;
- (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;
- }
- static void test_wolfSSL_CTX_get_min_proto_version(void)
- {
- #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
- WOLFSSL_CTX *ctx;
- printf(testingFmt, "wolfSSL_CTX_get_min_proto_version()");
- #ifndef NO_OLD_TLS
- #ifdef WOLFSSL_ALLOW_SSLV3
- #ifdef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
- #endif
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
- wolfSSL_CTX_free(ctx);
- #endif
- #ifdef WOLFSSL_ALLOW_TLSV10
- #ifdef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_server_method()));
- #endif
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
- wolfSSL_CTX_free(ctx);
- #endif
- #ifdef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_1_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_1_server_method()));
- #endif
- AssertIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION), WOLFSSL_SUCCESS);
- AssertIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
- wolfSSL_CTX_free(ctx);
- #endif
- #ifndef WOLFSSL_NO_TLS12
- #ifdef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
- #endif
- 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
- #ifdef NO_WOLFSSL_SERVER
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
- #else
- AssertNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
- #endif
- 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) */
- }
- /*----------------------------------------------------------------------------*
- | Main
- *----------------------------------------------------------------------------*/
- void ApiTest(void)
- {
- printf("\n-----------------Porting tests------------------\n");
- AssertTrue(test_fileAccess());
- printf(" Begin API Tests\n");
- AssertIntEQ(test_wolfSSL_Init(), WOLFSSL_SUCCESS);
- /* wolfcrypt initialization tests */
- test_wolfSSL_Method_Allocators();
- #ifndef NO_WOLFSSL_SERVER
- test_wolfSSL_CTX_new(wolfSSLv23_server_method());
- #endif
- #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
- (!defined(NO_RSA) || defined(HAVE_ECC))
- test_for_double_Free();
- #endif
- test_wolfSSL_CTX_use_certificate_file();
- AssertIntEQ(test_wolfSSL_CTX_use_certificate_buffer(), WOLFSSL_SUCCESS);
- test_wolfSSL_CTX_use_PrivateKey_file();
- test_wolfSSL_CTX_load_verify_locations();
- test_wolfSSL_CertManagerLoadCABuffer();
- test_wolfSSL_CertManagerGetCerts();
- test_wolfSSL_CertManagerSetVerify();
- test_wolfSSL_CertManagerNameConstraint();
- test_wolfSSL_CertManagerNameConstraint2();
- test_wolfSSL_CertManagerCRL();
- test_wolfSSL_CTX_load_verify_locations_ex();
- test_wolfSSL_CTX_load_verify_buffer_ex();
- test_wolfSSL_CTX_load_verify_chain_buffer_format();
- test_wolfSSL_CTX_use_certificate_chain_file_format();
- test_wolfSSL_CTX_trust_peer_cert();
- test_wolfSSL_CTX_SetTmpDH_file();
- test_wolfSSL_CTX_SetTmpDH_buffer();
- test_wolfSSL_CTX_SetMinMaxDhKey_Sz();
- test_wolfSSL_CTX_der_load_verify_locations();
- test_wolfSSL_CTX_enable_disable();
- test_wolfSSL_CTX_ticket_API();
- test_server_wolfSSL_new();
- test_client_wolfSSL_new();
- test_wolfSSL_SetTmpDH_file();
- test_wolfSSL_SetTmpDH_buffer();
- test_wolfSSL_SetMinMaxDhKey_Sz();
- test_SetTmpEC_DHE_Sz();
- test_wolfSSL_dtls_set_mtu();
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
- defined(HAVE_IO_TESTS_DEPENDENCIES)
- test_wolfSSL_read_write();
- #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13)
- test_wolfSSL_reuse_WOLFSSLobj();
- #endif
- test_wolfSSL_dtls_export();
- #endif
- AssertIntEQ(test_wolfSSL_SetMinVersion(), WOLFSSL_SUCCESS);
- AssertIntEQ(test_wolfSSL_CTX_SetMinVersion(), WOLFSSL_SUCCESS);
- /* TLS extensions tests */
- #ifdef HAVE_IO_TESTS_DEPENDENCIES
- test_wolfSSL_UseSNI();
- #endif
- test_wolfSSL_UseTrustedCA();
- test_wolfSSL_UseMaxFragment();
- test_wolfSSL_UseTruncatedHMAC();
- test_wolfSSL_UseSupportedCurve();
- test_wolfSSL_UseALPN();
- test_wolfSSL_DisableExtendedMasterSecret();
- test_wolfSSL_wolfSSL_UseSecureRenegotiation();
- /* X509 tests */
- test_wolfSSL_X509_NAME_get_entry();
- test_wolfSSL_PKCS12();
- test_wolfSSL_no_password_cb();
- test_wolfSSL_PKCS8();
- test_wolfSSL_PKCS8_ED25519();
- test_wolfSSL_PKCS8_ED448();
- test_wolfSSL_PKCS5();
- test_wolfSSL_URI();
- test_wolfSSL_TBS();
- test_wolfSSL_X509_verify();
- test_wolfSSL_X509_TLS_version();
- test_wc_PemToDer();
- test_wc_AllocDer();
- test_wc_CertPemToDer();
- test_wc_PubKeyPemToDer();
- test_wc_PemPubKeyToDer();
- /*OCSP Stapling. */
- AssertIntEQ(test_wolfSSL_UseOCSPStapling(), WOLFSSL_SUCCESS);
- AssertIntEQ(test_wolfSSL_UseOCSPStaplingV2(), WOLFSSL_SUCCESS);
- /* Multicast */
- test_wolfSSL_mcast();
- /* compatibility tests */
- test_wolfSSL_lhash();
- test_wolfSSL_X509_NAME();
- #ifndef NO_BIO
- test_wolfSSL_X509_INFO();
- #endif
- test_wolfSSL_X509_subject_name_hash();
- test_wolfSSL_X509_issuer_name_hash();
- test_wolfSSL_X509_check_host();
- test_wolfSSL_DES();
- test_wolfSSL_certs();
- test_wolfSSL_X509_check_private_key();
- test_wolfSSL_ASN1_TIME_print();
- test_wolfSSL_ASN1_UTCTIME_print();
- test_wolfSSL_ASN1_GENERALIZEDTIME_free();
- test_wolfSSL_private_keys();
- test_wolfSSL_PEM_PrivateKey();
- #ifndef NO_BIO
- test_wolfSSL_PEM_bio_RSAKey();
- test_wolfSSL_PEM_bio_DSAKey();
- test_wolfSSL_PEM_bio_ECKey();
- test_wolfSSL_PEM_RSAPrivateKey();
- test_wolfSSL_PEM_PUBKEY();
- #endif
- test_DSA_do_sign_verify();
- test_wolfSSL_tmp_dh();
- test_wolfSSL_ctrl();
- test_wolfSSL_EVP_MD_size();
- test_wolfSSL_EVP_Digest();
- test_wolfSSL_EVP_PKEY_new_mac_key();
- test_wolfSSL_EVP_MD_hmac_signing();
- test_wolfSSL_EVP_MD_rsa_signing();
- test_wolfSSL_EVP_MD_ecc_signing();
- test_wolfSSL_CTX_add_extra_chain_cert();
- #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
- test_wolfSSL_ERR_peek_last_error_line();
- #endif
- #ifndef NO_BIO
- test_wolfSSL_ERR_print_errors_cb();
- AssertFalse(test_wolfSSL_GetLoggingCb());
- AssertFalse(test_WOLFSSL_ERROR_MSG());
- AssertFalse(test_wc_ERR_remove_state());
- AssertFalse(test_wc_ERR_print_errors_fp());
- #endif
- test_wolfSSL_set_options();
- test_wolfSSL_sk_SSL_CIPHER();
- test_wolfSSL_PKCS7_certs();
- test_wolfSSL_X509_STORE_CTX();
- test_wolfSSL_X509_STORE_CTX_get0_current_issuer();
- test_wolfSSL_msgCb();
- test_wolfSSL_either_side();
- test_wolfSSL_DTLS_either_side();
- test_generate_cookie();
- test_wolfSSL_X509_STORE_set_flags();
- test_wolfSSL_X509_LOOKUP_load_file();
- test_wolfSSL_X509_Name_canon();
- test_wolfSSL_X509_LOOKUP_ctrl_file();
- test_wolfSSL_X509_LOOKUP_ctrl_hash_dir();
- test_wolfSSL_X509_NID();
- test_wolfSSL_X509_STORE_CTX_set_time();
- test_wolfSSL_get0_param();
- test_wolfSSL_X509_VERIFY_PARAM_set1_host();
- test_wolfSSL_X509_STORE_CTX_get0_store();
- test_wolfSSL_X509_STORE();
- test_wolfSSL_X509_STORE_load_locations();
- test_wolfSSL_X509_load_crl_file();
- test_wolfSSL_BN();
- test_wolfSSL_CTX_get0_set1_param();
- #ifndef NO_BIO
- test_wolfSSL_PEM_read_bio();
- test_wolfSSL_BIO();
- #endif
- test_wolfSSL_ASN1_STRING();
- test_wolfSSL_ASN1_BIT_STRING();
- test_wolfSSL_a2i_ASN1_INTEGER();
- test_wolfSSL_X509();
- test_wolfSSL_X509_VERIFY_PARAM();
- test_wolfSSL_X509_sign();
- test_wolfSSL_X509_sign2();
- test_wolfSSL_X509_get0_tbs_sigalg();
- test_wolfSSL_X509_ALGOR_get0();
- test_wolfSSL_X509_get_X509_PUBKEY();
- test_wolfSSL_X509_PUBKEY();
- test_wolfSSL_RAND();
- test_wolfSSL_BUF();
- test_wolfSSL_set_tlsext_status_type();
- test_wolfSSL_ASN1_TIME_adj();
- test_wolfSSL_X509_cmp_time();
- test_wolfSSL_X509_time_adj();
- test_wolfSSL_CTX_set_client_CA_list();
- test_wolfSSL_CTX_add_client_CA();
- test_wolfSSL_CTX_set_srp_username();
- test_wolfSSL_CTX_set_srp_password();
- test_wolfSSL_CTX_set_ecdh_auto();
- test_wolfSSL_THREADID_hash();
- test_wolfSSL_RAND_set_rand_method();
- test_wolfSSL_RAND_bytes();
- test_wolfSSL_BN_rand();
- test_wolfSSL_pseudo_rand();
- test_wolfSSL_PKCS8_Compat();
- test_wolfSSL_PKCS8_d2i();
- test_error_queue_per_thread();
- test_wolfSSL_ERR_put_error();
- #ifndef NO_BIO
- test_wolfSSL_ERR_print_errors();
- #endif
- test_wolfSSL_HMAC();
- test_wolfSSL_OBJ();
- test_wolfSSL_i2a_ASN1_OBJECT();
- test_wolfSSL_OBJ_cmp();
- test_wolfSSL_OBJ_txt2nid();
- test_wolfSSL_OBJ_txt2obj();
- test_wolfSSL_X509_NAME_ENTRY();
- test_wolfSSL_X509_set_name();
- test_wolfSSL_X509_set_notAfter();
- test_wolfSSL_X509_set_notBefore();
- test_wolfSSL_X509_set_version();
- #ifndef NO_BIO
- test_wolfSSL_BIO_gets();
- test_wolfSSL_BIO_puts();
- test_wolfSSL_BIO_should_retry();
- test_wolfSSL_d2i_PUBKEY();
- test_wolfSSL_BIO_write();
- test_wolfSSL_BIO_connect();
- test_wolfSSL_BIO_printf();
- test_wolfSSL_BIO_f_md();
- #endif
- test_wolfSSL_SESSION();
- test_wolfSSL_ticket_keys();
- test_wolfSSL_DES_ecb_encrypt();
- test_wolfSSL_sk_GENERAL_NAME();
- test_wolfSSL_MD4();
- test_wolfSSL_RSA();
- test_wolfSSL_RSA_DER();
- test_wolfSSL_RSA_get0_key();
- test_wolfSSL_RSA_meth();
- test_wolfSSL_verify_mode();
- test_wolfSSL_verify_depth();
- test_wolfSSL_HMAC_CTX();
- test_wolfSSL_msg_callback();
- test_wolfSSL_SHA();
- test_wolfSSL_DH_1536_prime();
- test_wolfSSL_PEM_write_DHparams();
- test_wolfSSL_AES_ecb_encrypt();
- test_wolfSSL_MD5();
- test_wolfSSL_MD5_Transform();
- test_wolfSSL_SHA_Transform();
- test_wolfSSL_SHA256();
- test_wolfSSL_SHA256_Transform();
- test_wolfSSL_SHA224();
- test_wolfSSL_SHA512_Transform();
- test_wolfSSL_X509_get_serialNumber();
- test_wolfSSL_X509_CRL();
- test_wolfSSL_d2i_X509_REQ();
- test_wolfSSL_PEM_read_X509();
- test_wolfSSL_PEM_read();
- #ifndef NO_BIO
- test_wolfSSL_PEM_X509_INFO_read_bio();
- test_wolfSSL_PEM_read_bio_ECPKParameters();
- #endif
- test_wolfSSL_X509_NAME_ENTRY_get_object();
- test_wolfSSL_OpenSSL_add_all_algorithms();
- test_wolfSSL_ASN1_STRING_print_ex();
- test_wolfSSL_ASN1_TIME_to_generalizedtime();
- test_wolfSSL_ASN1_INTEGER_set();
- test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS();
- test_wolfSSL_i2c_ASN1_INTEGER();
- test_wolfSSL_X509_check_ca();
- test_wolfSSL_X509_check_ip_asc();
- test_wolfSSL_DC_cert();
- test_wolfSSL_DES_ncbc();
- test_wolfSSL_AES_cbc_encrypt();
- test_wolfssl_EVP_aes_gcm_AAD_2_parts();
- test_wolfssl_EVP_aes_gcm();
- test_wolfSSL_PKEY_up_ref();
- test_wolfSSL_i2d_PrivateKey();
- test_wolfSSL_OCSP_id_get0_info();
- test_wolfSSL_i2d_OCSP_CERTID();
- test_wolfSSL_OCSP_SINGLERESP_get0_id();
- test_wolfSSL_OCSP_single_get0_status();
- test_wolfSSL_OCSP_resp_count();
- test_wolfSSL_OCSP_resp_get0();
- test_wolfSSL_EVP_PKEY_derive();
- #ifndef NO_RSA
- test_wolfSSL_RSA_padding_add_PKCS1_PSS();
- #endif
- test_CONF_modules_xxx();
- test_CRYPTO_set_dynlock_xxx();
- test_CRYPTO_THREADID_xxx();
- test_ENGINE_cleanup();
- test_wolfSSL_EC_KEY_set_group();
- #if defined(OPENSSL_ALL)
- test_wolfSSL_X509_PUBKEY_get();
- test_wolfSSL_sk_CIPHER_description();
- test_wolfSSL_get_ciphers_compat();
- test_wolfSSL_d2i_DHparams();
- test_wolfSSL_i2d_DHparams();
- test_wolfSSL_ASN1_STRING_to_UTF8();
- test_wolfSSL_ASN1_UNIVERSALSTRING_to_string();
- test_wolfSSL_EC_KEY_dup();
- test_wolfSSL_EVP_PKEY_set1_get1_DSA();
- test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY();
- test_wolfSSL_EVP_PKEY_set1_get1_DH();
- test_wolfSSL_CTX_ctrl();
- test_wolfSSL_DH_check();
- test_wolfSSL_EVP_PKEY_assign();
- test_wolfSSL_EVP_PKEY_base_id();
- test_wolfSSL_EVP_PKEY_id();
- test_wolfSSL_EVP_PKEY_keygen();
- test_wolfSSL_EVP_PKEY_keygen_init();
- test_wolfSSL_EVP_PKEY_missing_parameters();
- test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits();
- test_wolfSSL_EVP_CIPHER_CTX_iv_length();
- test_wolfSSL_EVP_CIPHER_CTX_key_length();
- test_wolfSSL_EVP_CIPHER_CTX_set_key_length();
- test_wolfSSL_EVP_CIPHER_CTX_set_iv();
- test_wolfSSL_EVP_PKEY_CTX_new_id();
- test_wolfSSL_EVP_rc4();
- test_wolfSSL_EVP_enc_null();
- test_wolfSSL_EVP_rc2_cbc();
- test_wolfSSL_EVP_mdc2();
- test_wolfSSL_EVP_md4();
- test_wolfSSL_EVP_aes_256_gcm();
- test_wolfSSL_EVP_aes_192_gcm();
- test_wolfSSL_EVP_ripemd160();
- test_wolfSSL_EVP_get_digestbynid();
- test_wolfSSL_EVP_PKEY_get0_EC_KEY();
- test_wolfSSL_EVP_X_STATE();
- test_wolfSSL_EVP_X_STATE_LEN();
- test_wolfSSL_EVP_CIPHER_block_size();
- test_wolfSSL_EVP_CIPHER_iv_length();
- test_wolfSSL_EVP_SignInit_ex();
- test_wolfSSL_EVP_DigestFinal_ex();
- test_wolfSSL_EVP_PKEY_assign_DH();
- test_wolfSSL_EVP_BytesToKey();
- test_IncCtr();
- test_wolfSSL_OBJ_ln();
- test_wolfSSL_OBJ_sn();
- test_wolfSSL_TXT_DB();
- test_wolfSSL_NCONF();
- #endif /* OPENSSL_ALL */
- #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
- AssertIntEQ(test_wolfSSL_CTX_use_certificate_ASN1(), WOLFSSL_SUCCESS);
- #ifndef NO_BIO
- test_wolfSSL_d2i_PrivateKeys_bio();
- #endif
- #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
- test_wolfSSL_X509_CA_num();
- test_wolfSSL_X509_get_version();
- #ifndef NO_BIO
- test_wolfSSL_X509_print();
- test_wolfSSL_BIO_get_len();
- #endif
- test_wolfSSL_RSA_verify();
- test_wolfSSL_X509V3_EXT_get();
- test_wolfSSL_X509V3_EXT();
- test_wolfSSL_X509_get_ext();
- test_wolfSSL_X509_get_ext_by_NID();
- test_wolfSSL_X509_get_ext_count();
- test_wolfSSL_X509_EXTENSION_new();
- test_wolfSSL_X509_EXTENSION_get_object();
- test_wolfSSL_X509_EXTENSION_get_data();
- test_wolfSSL_X509_EXTENSION_get_critical();
- test_wolfSSL_X509V3_EXT_print();
- test_wolfSSL_X509_cmp();
- #ifndef NO_BIO
- test_wolfSSL_RSA_print();
- test_wolfSSL_ASN1_STRING_print();
- #endif
- test_wolfSSL_ASN1_get_object();
- test_openssl_generate_key_and_cert();
- test_wolfSSL_EC_get_builtin_curves();
- test_wolfSSL_CRYPTO_memcmp();
- /* test the no op functions for compatibility */
- test_no_op_functions();
- /* OpenSSL EVP_PKEY API tests */
- test_EVP_PKEY_rsa();
- test_wolfSSL_EVP_PKEY_encrypt();
- test_wolfSSL_EVP_PKEY_sign();
- test_EVP_PKEY_ec();
- test_EVP_PKEY_cmp();
- /* OpenSSL error API tests */
- test_ERR_load_crypto_strings();
- /* OpenSSL sk_X509 API test */
- test_sk_X509();
- /* OpenSSL X509 API test */
- test_X509_get_signature_nid();
- /* OpenSSL X509 REQ API test */
- test_X509_REQ();
- /* OpenSSL PKCS7 API test */
- test_wolfssl_PKCS7();
- test_wolfSSL_PKCS7_SIGNED_new();
- #ifndef NO_BIO
- test_wolfSSL_PEM_write_bio_PKCS7();
- #ifdef HAVE_SMIME
- test_wolfSSL_SMIME_read_PKCS7();
- #endif
- #endif
- /* wolfCrypt ASN tests */
- test_wc_GetPkcs8TraditionalOffset();
- test_wc_SetSubjectRaw();
- test_wc_GetSubjectRaw();
- test_wc_SetIssuerRaw();
- test_wc_SetIssueBuffer();
- test_wc_SetSubjectKeyId();
- test_wc_SetSubject();
- test_CheckCertSignature();
- /* wolfCrypt ECC tests */
- test_wc_ecc_get_curve_size_from_name();
- test_wc_ecc_get_curve_id_from_name();
- test_wc_ecc_get_curve_id_from_params();
- #ifdef WOLFSSL_TLS13
- /* TLS v1.3 API tests */
- 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 */
- AssertIntEQ(test_EccSigFailure_cm(), ASN_SIG_CONFIRM_E);
- AssertIntEQ(test_RsaSigFailure_cm(), ASN_SIG_CONFIRM_E);
- #endif /* NO_CERTS */
- #ifdef HAVE_PK_CALLBACKS
- /* public key callback tests */
- test_DhCallbacks();
- #endif
- #ifdef HAVE_KEYING_MATERIAL
- test_export_keying_material();
- #endif /* HAVE_KEYING_MATERIAL */
- test_wolfSSL_CTX_get_min_proto_version();
- /*wolfcrypt */
- printf("\n-----------------wolfcrypt unit tests------------------\n");
- AssertFalse(test_wolfCrypt_Init());
- AssertFalse(test_wc_InitMd5());
- AssertFalse(test_wc_Md5Update());
- AssertFalse(test_wc_Md5Final());
- AssertFalse(test_wc_InitSha());
- AssertFalse(test_wc_ShaUpdate());
- AssertFalse(test_wc_ShaFinal());
- AssertFalse(test_wc_InitSha256());
- AssertFalse(test_wc_Sha256Update());
- AssertFalse(test_wc_Sha256Final());
- AssertFalse(test_wc_Sha256FinalRaw());
- AssertFalse(test_wc_Sha256GetFlags());
- AssertFalse(test_wc_Sha256Free());
- AssertFalse(test_wc_Sha256GetHash());
- AssertFalse(test_wc_Sha256Copy());
- AssertFalse(test_wc_InitSha512());
- AssertFalse(test_wc_Sha512Update());
- AssertFalse(test_wc_Sha512Final());
- AssertFalse(test_wc_Sha512GetFlags());
- AssertFalse(test_wc_Sha512FinalRaw());
- AssertFalse(test_wc_Sha512Free());
- AssertFalse(test_wc_Sha512GetHash());
- AssertFalse(test_wc_Sha512Copy());
- AssertFalse(test_wc_InitSha384());
- AssertFalse(test_wc_Sha384Update());
- AssertFalse(test_wc_Sha384Final());
- AssertFalse(test_wc_Sha384GetFlags());
- AssertFalse(test_wc_Sha384FinalRaw());
- AssertFalse(test_wc_Sha384Free());
- AssertFalse(test_wc_Sha384GetHash());
- AssertFalse(test_wc_Sha384Copy());
- AssertFalse(test_wc_InitSha224());
- AssertFalse(test_wc_Sha224Update());
- AssertFalse(test_wc_Sha224Final());
- AssertFalse(test_wc_Sha224SetFlags());
- AssertFalse(test_wc_Sha224GetFlags());
- AssertFalse(test_wc_Sha224Free());
- AssertFalse(test_wc_Sha224GetHash());
- AssertFalse(test_wc_Sha224Copy());
- AssertFalse(test_wc_InitBlake2b());
- AssertFalse(test_wc_InitBlake2b_WithKey());
- AssertFalse(test_wc_InitBlake2s_WithKey());
- AssertFalse(test_wc_InitRipeMd());
- AssertFalse(test_wc_RipeMdUpdate());
- AssertFalse(test_wc_RipeMdFinal());
- AssertIntEQ(test_wc_InitSha3(), 0);
- AssertIntEQ(testing_wc_Sha3_Update(), 0);
- AssertIntEQ(test_wc_Sha3_224_Final(), 0);
- AssertIntEQ(test_wc_Sha3_256_Final(), 0);
- AssertIntEQ(test_wc_Sha3_384_Final(), 0);
- AssertIntEQ(test_wc_Sha3_512_Final(), 0);
- AssertIntEQ(test_wc_Sha3_224_Copy(), 0);
- AssertIntEQ(test_wc_Sha3_256_Copy(), 0);
- AssertIntEQ(test_wc_Sha3_384_Copy(), 0);
- AssertIntEQ(test_wc_Sha3_512_Copy(), 0);
- AssertIntEQ(test_wc_Sha3_GetFlags(), 0);
- AssertIntEQ(test_wc_InitShake256(), 0);
- AssertIntEQ(testing_wc_Shake256_Update(), 0);
- AssertIntEQ(test_wc_Shake256_Final(), 0);
- AssertIntEQ(test_wc_Shake256_Copy(), 0);
- AssertIntEQ(test_wc_Shake256Hash(), 0);
- AssertFalse(test_wc_Md5HmacSetKey());
- AssertFalse(test_wc_Md5HmacUpdate());
- AssertFalse(test_wc_Md5HmacFinal());
- AssertFalse(test_wc_ShaHmacSetKey());
- AssertFalse(test_wc_ShaHmacUpdate());
- AssertFalse(test_wc_ShaHmacFinal());
- AssertFalse(test_wc_Sha224HmacSetKey());
- AssertFalse(test_wc_Sha224HmacUpdate());
- AssertFalse(test_wc_Sha224HmacFinal());
- AssertFalse(test_wc_Sha256HmacSetKey());
- AssertFalse(test_wc_Sha256HmacUpdate());
- AssertFalse(test_wc_Sha256HmacFinal());
- AssertFalse(test_wc_Sha384HmacSetKey());
- AssertFalse(test_wc_Sha384HmacUpdate());
- AssertFalse(test_wc_Sha384HmacFinal());
- AssertIntEQ(test_wc_HashInit(), 0);
- AssertIntEQ(test_wc_HashSetFlags(), 0);
- AssertIntEQ(test_wc_HashGetFlags(), 0);
- AssertIntEQ(test_wc_InitCmac(), 0);
- AssertIntEQ(test_wc_CmacUpdate(), 0);
- AssertIntEQ(test_wc_CmacFinal(), 0);
- AssertIntEQ(test_wc_AesCmacGenerate(), 0);
- AssertIntEQ(test_wc_AesGcmStream(), 0);
- AssertIntEQ(test_wc_Des3_SetIV(), 0);
- AssertIntEQ(test_wc_Des3_SetKey(), 0);
- AssertIntEQ(test_wc_Des3_CbcEncryptDecrypt(), 0);
- AssertIntEQ(test_wc_Des3_CbcEncryptDecryptWithKey(), 0);
- AssertIntEQ(test_wc_Des3_EcbEncrypt(), 0);
- AssertIntEQ(test_wc_IdeaSetKey(), 0);
- AssertIntEQ(test_wc_IdeaSetIV(), 0);
- AssertIntEQ(test_wc_IdeaCipher(), 0);
- AssertIntEQ(test_wc_IdeaCbcEncyptDecrypt(), 0);
- AssertIntEQ(test_wc_Chacha_SetKey(), 0);
- AssertIntEQ(test_wc_Chacha_Process(), 0);
- AssertIntEQ(test_wc_ChaCha20Poly1305_aead(), 0);
- AssertIntEQ(test_wc_Poly1305SetKey(), 0);
- AssertIntEQ(test_wc_CamelliaSetKey(), 0);
- AssertIntEQ(test_wc_CamelliaSetIV(), 0);
- AssertIntEQ(test_wc_CamelliaEncryptDecryptDirect(), 0);
- AssertIntEQ(test_wc_CamelliaCbcEncryptDecrypt(), 0);
- AssertIntEQ(test_wc_RabbitSetKey(), 0);
- AssertIntEQ(test_wc_RabbitProcess(), 0);
- AssertIntEQ(test_wc_Arc4SetKey(), 0);
- AssertIntEQ(test_wc_Arc4Process(), 0);
- AssertIntEQ(test_wc_Rc2SetKey(), 0);
- AssertIntEQ(test_wc_Rc2SetIV(), 0);
- AssertIntEQ(test_wc_Rc2EcbEncryptDecrypt(), 0);
- AssertIntEQ(test_wc_Rc2CbcEncryptDecrypt(), 0);
- AssertIntEQ(test_wc_AesSetKey(), 0);
- AssertIntEQ(test_wc_AesSetIV(), 0);
- AssertIntEQ(test_wc_AesCbcEncryptDecrypt(), 0);
- AssertIntEQ(test_wc_AesCtrEncryptDecrypt(), 0);
- AssertIntEQ(test_wc_AesGcmSetKey(), 0);
- AssertIntEQ(test_wc_AesGcmEncryptDecrypt(), 0);
- AssertIntEQ(test_wc_GmacSetKey(), 0);
- AssertIntEQ(test_wc_GmacUpdate(), 0);
- AssertIntEQ(test_wc_InitRsaKey(), 0);
- AssertIntEQ(test_wc_RsaPrivateKeyDecode(), 0);
- AssertIntEQ(test_wc_RsaPublicKeyDecode(), 0);
- AssertIntEQ(test_wc_RsaPublicKeyDecodeRaw(), 0);
- AssertIntEQ(test_wc_MakeRsaKey(), 0);
- AssertIntEQ(test_wc_SetKeyUsage (), 0);
- AssertIntEQ(test_wc_CheckProbablePrime (), 0);
- AssertIntEQ(test_wc_RsaPSS_Verify (), 0);
- AssertIntEQ(test_wc_RsaPSS_VerifyCheck (), 0);
- AssertIntEQ(test_wc_RsaPSS_VerifyCheckInline (), 0);
- AssertIntEQ(test_wc_SetMutexCb(), 0);
- AssertIntEQ(test_wc_LockMutex_ex(), 0);
- AssertIntEQ(test_wc_RsaKeyToDer(), 0);
- AssertIntEQ(test_wc_RsaKeyToPublicDer(), 0);
- AssertIntEQ(test_wc_RsaPublicEncryptDecrypt(), 0);
- AssertIntEQ(test_wc_RsaPublicEncryptDecrypt_ex(), 0);
- AssertIntEQ(test_wc_RsaEncryptSize(), 0);
- AssertIntEQ(test_wc_RsaSSL_SignVerify(), 0);
- AssertIntEQ(test_wc_RsaFlattenPublicKey(), 0);
- AssertIntEQ(test_RsaDecryptBoundsCheck(), 0);
- AssertIntEQ(test_wc_AesCcmSetKey(), 0);
- AssertIntEQ(test_wc_AesCcmEncryptDecrypt(), 0);
- AssertIntEQ(test_wc_Hc128_SetKey(), 0);
- AssertIntEQ(test_wc_Hc128_Process(), 0);
- AssertIntEQ(test_wc_InitDsaKey(), 0);
- AssertIntEQ(test_wc_DsaSignVerify(), 0);
- AssertIntEQ(test_wc_DsaPublicPrivateKeyDecode(), 0);
- AssertIntEQ(test_wc_MakeDsaKey(), 0);
- AssertIntEQ(test_wc_DsaKeyToDer(), 0);
- AssertIntEQ(test_wc_DsaKeyToPublicDer(), 0);
- AssertIntEQ(test_wc_DsaImportParamsRaw(), 0);
- AssertIntEQ(test_wc_DsaImportParamsRawCheck(), 0);
- AssertIntEQ(test_wc_DsaExportParamsRaw(), 0);
- AssertIntEQ(test_wc_DsaExportKeyRaw(), 0);
- AssertIntEQ(test_wc_SignatureGetSize_ecc(), 0);
- AssertIntEQ(test_wc_SignatureGetSize_rsa(), 0);
- wolfCrypt_Cleanup();
- #ifdef OPENSSL_EXTRA
- /*wolfSSL_EVP_get_cipherbynid test*/
- test_wolfSSL_EVP_get_cipherbynid();
- test_wolfSSL_EVP_CIPHER_CTX();
- #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
- test_wolfSSL_EC();
- #endif
- test_wolfSSL_ECDSA_SIG();
- test_ECDSA_size_sign();
- test_ED25519();
- test_ED448();
- test_EC_i2d();
- #endif
- #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
- !defined(HAVE_SELFTEST) && \
- !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
- test_wc_ecc_get_curve_id_from_dp_params();
- #endif
- #ifdef HAVE_HASHDRBG
- #ifdef TEST_RESEED_INTERVAL
- AssertIntEQ(test_wc_RNG_GenerateBlock_Reseed(), 0);
- #endif
- AssertIntEQ(test_wc_RNG_GenerateBlock(), 0);
- #endif
- AssertIntEQ(test_get_rand_digit(), 0);
- AssertIntEQ(test_get_digit_count(), 0);
- AssertIntEQ(test_mp_cond_copy(), 0);
- AssertIntEQ(test_mp_rand(), 0);
- AssertIntEQ(test_get_digit(), 0);
- AssertIntEQ(test_wc_export_int(), 0);
- AssertIntEQ(test_wc_InitRngNonce(), 0);
- AssertIntEQ(test_wc_InitRngNonce_ex(), 0);
- AssertIntEQ(test_wc_ed25519_make_key(), 0);
- AssertIntEQ(test_wc_ed25519_init(), 0);
- AssertIntEQ(test_wc_ed25519_sign_msg(), 0);
- AssertIntEQ(test_wc_ed25519_import_public(), 0);
- AssertIntEQ(test_wc_ed25519_import_private_key(), 0);
- AssertIntEQ(test_wc_ed25519_export(), 0);
- AssertIntEQ(test_wc_ed25519_size(), 0);
- AssertIntEQ(test_wc_ed25519_exportKey(), 0);
- AssertIntEQ(test_wc_Ed25519PublicKeyToDer(), 0);
- AssertIntEQ(test_wc_curve25519_init(), 0);
- AssertIntEQ(test_wc_curve25519_size(), 0);
- AssertIntEQ(test_wc_curve25519_export_key_raw(), 0);
- AssertIntEQ(test_wc_curve25519_export_key_raw_ex(), 0);
- AssertIntEQ(test_wc_curve25519_size (), 0);
- AssertIntEQ(test_wc_curve25519_make_key (), 0);
- AssertIntEQ(test_wc_curve25519_shared_secret_ex (), 0);
- AssertIntEQ(test_wc_curve25519_make_pub (), 0);
- AssertIntEQ(test_wc_curve25519_export_public_ex (), 0);
- AssertIntEQ(test_wc_curve25519_export_private_raw_ex (), 0);
- AssertIntEQ(test_wc_curve25519_import_private_raw_ex (), 0);
- AssertIntEQ(test_wc_curve25519_import_private (), 0);
- AssertIntEQ(test_wc_ed448_make_key(), 0);
- AssertIntEQ(test_wc_ed448_init(), 0);
- AssertIntEQ(test_wc_ed448_sign_msg(), 0);
- AssertIntEQ(test_wc_ed448_import_public(), 0);
- AssertIntEQ(test_wc_ed448_import_private_key(), 0);
- AssertIntEQ(test_wc_ed448_export(), 0);
- AssertIntEQ(test_wc_ed448_size(), 0);
- AssertIntEQ(test_wc_ed448_exportKey(), 0);
- AssertIntEQ(test_wc_Ed448PublicKeyToDer(), 0);
- AssertIntEQ(test_wc_curve448_make_key (), 0);
- AssertIntEQ(test_wc_curve448_shared_secret_ex (), 0);
- AssertIntEQ(test_wc_curve448_export_public_ex (), 0);
- AssertIntEQ(test_wc_curve448_export_private_raw_ex (), 0);
- AssertIntEQ(test_wc_curve448_export_key_raw (), 0);
- AssertIntEQ(test_wc_curve448_import_private_raw_ex (), 0);
- AssertIntEQ(test_wc_curve448_import_private (), 0);
- AssertIntEQ(test_wc_curve448_init(), 0);
- AssertIntEQ(test_wc_curve448_size (), 0);
- AssertIntEQ(test_wc_ecc_make_key(), 0);
- AssertIntEQ(test_wc_ecc_init(), 0);
- AssertIntEQ(test_wc_ecc_check_key(), 0);
- AssertIntEQ(test_wc_ecc_get_generator(), 0);
- AssertIntEQ(test_wc_ecc_size(), 0);
- test_wc_ecc_params();
- AssertIntEQ(test_wc_ecc_signVerify_hash(), 0);
- AssertIntEQ(test_wc_ecc_shared_secret(), 0);
- AssertIntEQ(test_wc_ecc_export_x963(), 0);
- AssertIntEQ(test_wc_ecc_export_x963_ex(), 0);
- AssertIntEQ(test_wc_ecc_import_x963(), 0);
- AssertIntEQ(ecc_import_private_key(), 0);
- AssertIntEQ(test_wc_ecc_export_private_only(), 0);
- AssertIntEQ(test_wc_ecc_rs_to_sig(), 0);
- AssertIntEQ(test_wc_ecc_import_raw(), 0);
- AssertIntEQ(test_wc_ecc_import_unsigned(), 0);
- AssertIntEQ(test_wc_ecc_sig_size(), 0);
- AssertIntEQ(test_wc_ecc_ctx_new(), 0);
- AssertIntEQ(test_wc_ecc_ctx_reset(), 0);
- AssertIntEQ(test_wc_ecc_ctx_set_peer_salt(), 0);
- AssertIntEQ(test_wc_ecc_ctx_set_info(), 0);
- AssertIntEQ(test_wc_ecc_encryptDecrypt(), 0);
- AssertIntEQ(test_wc_ecc_del_point(), 0);
- AssertIntEQ(test_wc_ecc_pointFns(), 0);
- AssertIntEQ(test_wc_ecc_shared_secret_ssh(), 0);
- AssertIntEQ(test_wc_ecc_verify_hash_ex(), 0);
- AssertIntEQ(test_wc_ecc_mulmod(), 0);
- AssertIntEQ(test_wc_ecc_is_valid_idx(), 0);
- AssertIntEQ(test_wc_ecc_get_curve_id_from_oid(), 0);
- AssertIntEQ(test_wc_ecc_sig_size_calc(), 0);
- AssertIntEQ(test_ToTraditional(), 0);
- AssertIntEQ(test_wc_EccPrivateKeyToDer(), 0);
- AssertIntEQ(test_wc_Ed25519KeyToDer(), 0);
- AssertIntEQ(test_wc_Ed25519PrivateKeyToDer(), 0);
- AssertIntEQ(test_wc_Ed448KeyToDer(), 0);
- AssertIntEQ(test_wc_Ed448PrivateKeyToDer(), 0);
- AssertIntEQ(test_wc_SetAuthKeyIdFromPublicKey_ex(), 0);
- AssertIntEQ(test_wc_SetSubjectBuffer(), 0);
- AssertIntEQ(test_wc_SetSubjectKeyIdFromPublicKey_ex(), 0);
- test_wc_PKCS7_New();
- test_wc_PKCS7_Init();
- test_wc_PKCS7_InitWithCert();
- test_wc_PKCS7_EncodeData();
- test_wc_PKCS7_EncodeSignedData();
- test_wc_PKCS7_EncodeSignedData_ex();
- test_wc_PKCS7_VerifySignedData();
- test_wc_PKCS7_EncodeDecodeEnvelopedData();
- test_wc_PKCS7_EncodeEncryptedData();
- test_wc_PKCS7_Degenerate();
- test_wc_PKCS7_BER();
- test_PKCS7_signed_enveloped();
- test_wc_PKCS7_NoDefaultSignedAttribs();
- test_wc_PKCS7_SetOriEncryptCtx();
- test_wc_PKCS7_SetOriDecryptCtx();
- test_wc_PKCS7_DecodeCompressedData();
- test_wc_i2d_PKCS12();
- test_wolfSSL_CTX_LoadCRL();
- AssertIntEQ(test_ForceZero(), 0);
- AssertIntEQ(test_wolfSSL_Cleanup(), WOLFSSL_SUCCESS);
- #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
- !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
- !defined(WOLFSSL_NO_CLIENT_AUTH))
- AssertIntEQ(test_various_pathlen_chains(), WOLFSSL_SUCCESS);
- #endif
- /* If at some point a stub get implemented this test should fail indicating
- * a need to implement a new test case
- */
- test_stubs_are_stubs();
- #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)devId;
- printf(" End API Tests\n");
- }
|