pk.c 392 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781
  1. /* pk.c
  2. *
  3. * Copyright (C) 2006-2023 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <wolfssl/wolfcrypt/settings.h>
  25. #include <wolfssl/internal.h>
  26. #ifndef WC_NO_RNG
  27. #include <wolfssl/wolfcrypt/random.h>
  28. #endif
  29. #ifdef HAVE_ECC
  30. #include <wolfssl/wolfcrypt/ecc.h>
  31. #endif
  32. #ifndef WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  33. /* FIPS build has replaced ecc.h. */
  34. #define wc_ecc_key_get_priv(key) (&((key)->k))
  35. #define WOLFSSL_HAVE_ECC_KEY_GET_PRIV
  36. #endif
  37. #if !defined(WOLFSSL_PK_INCLUDED)
  38. #ifndef WOLFSSL_IGNORE_FILE_WARN
  39. #warning pk.c does not need to be compiled separately from ssl.c
  40. #endif
  41. #else
  42. #ifndef NO_RSA
  43. #include <wolfssl/wolfcrypt/rsa.h>
  44. #endif
  45. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && defined(WOLFSSL_KEY_GEN) && \
  46. (!defined(HAVE_USER_RSA) || defined(HAVE_ECC) || \
  47. (!defined(NO_DSA) && !defined(HAVE_SELFTEST)))
  48. /* Forward declaration for wolfSSL_PEM_write_bio_DSA_PUBKEY.
  49. * Implementation in ssl.c.
  50. */
  51. static int pem_write_bio_pubkey(WOLFSSL_BIO* bio, WOLFSSL_EVP_PKEY* key);
  52. #endif
  53. /*******************************************************************************
  54. * COMMON FUNCTIONS
  55. ******************************************************************************/
  56. /* Calculate the number of bytes require to represent a length value in ASN.
  57. *
  58. * @param [in] l Length value to use.
  59. * @return Number of bytes required to represent length value.
  60. */
  61. #define ASN_LEN_SIZE(l) \
  62. (((l) < 128) ? 1 : (((l) < 256) ? 2 : 3))
  63. #if defined(OPENSSL_EXTRA)
  64. #ifndef NO_ASN
  65. #if (!defined(NO_FILESYSTEM) && (defined(OPENSSL_EXTRA) || \
  66. defined(OPENSSL_ALL))) || (!defined(NO_BIO) && defined(OPENSSL_EXTRA))
  67. /* Convert the PEM encoding in the buffer to DER.
  68. *
  69. * @param [in] pem Buffer containing PEM encoded data.
  70. * @param [in] pemSz Size of data in buffer in bytes.
  71. * @param [in] cb Password callback when PEM encrypted.
  72. * @param [in] pass NUL terminated string for passphrase when PEM
  73. * encrypted.
  74. * @param [in] keyType Type of key to match against PEM header/footer.
  75. * @param [out] keyFormat Format of key.
  76. * @param [out] der Buffer holding DER encoding.
  77. * @return Negative on failure.
  78. * @return Number of bytes consumed on success.
  79. */
  80. static int pem_mem_to_der(const char* pem, int pemSz, wc_pem_password_cb* cb,
  81. void* pass, int keyType, int* keyFormat, DerBuffer** der)
  82. {
  83. #ifdef WOLFSSL_SMALL_STACK
  84. EncryptedInfo* info = NULL;
  85. #else
  86. EncryptedInfo info[1];
  87. #endif /* WOLFSSL_SMALL_STACK */
  88. wc_pem_password_cb* localCb = NULL;
  89. int ret = 0;
  90. if (cb != NULL) {
  91. localCb = cb;
  92. }
  93. else if (pass != NULL) {
  94. localCb = wolfSSL_PEM_def_callback;
  95. }
  96. #ifdef WOLFSSL_SMALL_STACK
  97. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  98. DYNAMIC_TYPE_ENCRYPTEDINFO);
  99. if (info == NULL) {
  100. WOLFSSL_ERROR_MSG("Error getting memory for EncryptedInfo structure");
  101. ret = MEMORY_E;
  102. }
  103. #endif /* WOLFSSL_SMALL_STACK */
  104. if (ret == 0) {
  105. XMEMSET(info, 0, sizeof(EncryptedInfo));
  106. info->passwd_cb = localCb;
  107. info->passwd_userdata = pass;
  108. /* Do not strip PKCS8 header */
  109. ret = PemToDer((const unsigned char *)pem, pemSz, keyType, der, NULL,
  110. info, keyFormat);
  111. if (ret < 0) {
  112. WOLFSSL_ERROR_MSG("Bad PEM To DER");
  113. }
  114. }
  115. if (ret >= 0) {
  116. ret = (int)info->consumed;
  117. }
  118. #ifdef WOLFSSL_SMALL_STACK
  119. XFREE(info, NULL, DYNAMIC_TYPE_ENCRYPTEDINFO);
  120. #endif
  121. return ret;
  122. }
  123. #endif
  124. #if !defined(NO_RSA) || !defined(WOLFCRYPT_ONLY)
  125. #ifndef NO_BIO
  126. /* Read PEM data from a BIO and decode to DER in a new buffer.
  127. *
  128. * @param [in, out] bio BIO object to read with.
  129. * @param [in] cb Password callback when PEM encrypted.
  130. * @param [in] pass NUL terminated string for passphrase when PEM
  131. * encrypted.
  132. * @param [in] keyType Type of key to match against PEM header/footer.
  133. * @param [out] keyFormat Format of key.
  134. * @param [out] der Buffer holding DER encoding.
  135. * @return Negative on failure.
  136. * @return Number of bytes consumed on success.
  137. */
  138. static int pem_read_bio_key(WOLFSSL_BIO* bio, wc_pem_password_cb* cb,
  139. void* pass, int keyType, int* keyFormat, DerBuffer** der)
  140. {
  141. int ret;
  142. char* mem = NULL;
  143. int memSz;
  144. int alloced = 0;
  145. ret = wolfssl_read_bio(bio, &mem, &memSz, &alloced);
  146. if (ret == 0) {
  147. ret = pem_mem_to_der(mem, memSz, cb, pass, keyType, keyFormat, der);
  148. /* Write left over data back to BIO if not a file BIO */
  149. if ((ret > 0) && ((memSz - ret) > 0) &&
  150. (bio->type != WOLFSSL_BIO_FILE)) {
  151. int res;
  152. res = wolfSSL_BIO_write(bio, mem + ret, memSz - ret);
  153. if (res != memSz - ret) {
  154. WOLFSSL_ERROR_MSG("Unable to write back excess data");
  155. if (res < 0) {
  156. ret = res;
  157. }
  158. else {
  159. ret = MEMORY_E;
  160. }
  161. }
  162. }
  163. if (alloced) {
  164. XFREE(mem, NULL, DYNAMIC_TYPE_OPENSSL);
  165. }
  166. }
  167. return ret;
  168. }
  169. #endif /* !NO_BIO */
  170. #if !defined(NO_FILESYSTEM)
  171. /* Read PEM data from a file and decode to DER in a new buffer.
  172. *
  173. * @param [in] fp File pointer to read with.
  174. * @param [in] cb Password callback when PEM encrypted.
  175. * @param [in] pass NUL terminated string for passphrase when PEM
  176. * encrypted.
  177. * @param [in] keyType Type of key to match against PEM header/footer.
  178. * @param [out] keyFormat Format of key.
  179. * @param [out] der Buffer holding DER encoding.
  180. * @return Negative on failure.
  181. * @return Number of bytes consumed on success.
  182. */
  183. static int pem_read_file_key(XFILE fp, wc_pem_password_cb* cb, void* pass,
  184. int keyType, int* keyFormat, DerBuffer** der)
  185. {
  186. int ret;
  187. char* mem = NULL;
  188. int memSz;
  189. ret = wolfssl_read_file(fp, &mem, &memSz);
  190. if (ret == 0) {
  191. ret = pem_mem_to_der(mem, memSz, cb, pass, keyType, keyFormat, der);
  192. XFREE(mem, NULL, DYNAMIC_TYPE_OPENSSL);
  193. }
  194. return ret;
  195. }
  196. #endif /* !NO_FILESYSTEM */
  197. #endif
  198. #if defined(OPENSSL_EXTRA) && ((!defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) \
  199. && !defined(HAVE_USER_RSA)) || !defined(WOLFCRYPT_ONLY))
  200. /* Convert DER data to PEM in an allocated buffer.
  201. *
  202. * @param [in] der Buffer containing DER data.
  203. * @param [in] derSz Size of DER data in bytes.
  204. * @param [in] type Type of key being encoded.
  205. * @param [in] heap Heap hint for dynamic memory allocation.
  206. * @param [out] out Allocated buffer containing PEM.
  207. * @param [out] outSz Size of PEM encoding.
  208. * @return WOLFSSL_FAILURE on error.
  209. * @return WOLFSSL_SUCCESS on success.
  210. */
  211. static int der_to_pem_alloc(const unsigned char* der, int derSz, int type,
  212. void* heap, byte** out, int* outSz)
  213. {
  214. int ret = WOLFSSL_SUCCESS;
  215. int pemSz;
  216. byte* pem = NULL;
  217. (void)heap;
  218. pemSz = wc_DerToPem(der, (word32)derSz, NULL, 0, type);
  219. if (pemSz < 0) {
  220. ret = WOLFSSL_FAILURE;
  221. }
  222. if (ret == WOLFSSL_SUCCESS) {
  223. pem = (byte*)XMALLOC((size_t)pemSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
  224. if (pem == NULL) {
  225. ret = WOLFSSL_FAILURE;
  226. }
  227. }
  228. if ((ret == WOLFSSL_SUCCESS) && (wc_DerToPem(der, (word32)derSz, pem,
  229. (word32)pemSz, type) < 0)) {
  230. ret = WOLFSSL_FAILURE;
  231. XFREE(pem, heap, DYNAMIC_TYPE_TMP_BUFFER);
  232. pem = NULL;
  233. }
  234. *out = pem;
  235. *outSz = pemSz;
  236. return ret;
  237. }
  238. #ifndef NO_BIO
  239. /* Write the DER data as PEM into BIO.
  240. *
  241. * @param [in] der Buffer containing DER data.
  242. * @param [in] derSz Size of DER data in bytes.
  243. * @param [in, out] bio BIO object to write with.
  244. * @param [in] type Type of key being encoded.
  245. * @return WOLFSSL_FAILURE on error.
  246. * @return WOLFSSL_SUCCESS on success.
  247. */
  248. static int der_write_to_bio_as_pem(const unsigned char* der, int derSz,
  249. WOLFSSL_BIO* bio, int type)
  250. {
  251. int ret;
  252. int pemSz;
  253. byte* pem = NULL;
  254. ret = der_to_pem_alloc(der, derSz, type, bio->heap, &pem, &pemSz);
  255. if (ret == WOLFSSL_SUCCESS) {
  256. int len = wolfSSL_BIO_write(bio, pem, pemSz);
  257. if (len != pemSz) {
  258. WOLFSSL_ERROR_MSG("Unable to write full PEM to BIO");
  259. ret = WOLFSSL_FAILURE;
  260. }
  261. }
  262. XFREE(pem, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  263. return ret;
  264. }
  265. #endif
  266. #endif
  267. #if (!defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  268. !defined(HAVE_USER_RSA)) || \
  269. (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || \
  270. (defined(HAVE_ECC) && defined(WOLFSSL_KEY_GEN))
  271. #if !defined(NO_FILESYSTEM)
  272. /* Write the DER data as PEM into file pointer.
  273. *
  274. * @param [in] der Buffer containing DER data.
  275. * @param [in] derSz Size of DER data in bytes.
  276. * @param [in] fp File pointer to write with.
  277. * @param [in] type Type of key being encoded.
  278. * @param [in] heap Heap hint for dynamic memory allocation.
  279. * @return WOLFSSL_FAILURE on error.
  280. * @return WOLFSSL_SUCCESS on success.
  281. */
  282. static int der_write_to_file_as_pem(const unsigned char* der, int derSz,
  283. XFILE fp, int type, void* heap)
  284. {
  285. int ret;
  286. int pemSz;
  287. byte* pem = NULL;
  288. ret = der_to_pem_alloc(der, derSz, type, heap, &pem, &pemSz);
  289. if (ret == WOLFSSL_SUCCESS) {
  290. int len = (int)XFWRITE(pem, 1, (size_t)pemSz, fp);
  291. if (len != pemSz) {
  292. WOLFSSL_ERROR_MSG("Unable to write full PEM to BIO");
  293. ret = WOLFSSL_FAILURE;
  294. }
  295. }
  296. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  297. return ret;
  298. }
  299. #endif
  300. #endif
  301. #if defined(WOLFSSL_KEY_GEN) && \
  302. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)) && \
  303. ((!defined(NO_RSA) && !defined(HAVE_USER_RSA)) || defined(HAVE_ECC))
  304. static int der_to_enc_pem_alloc(unsigned char* der, int derSz,
  305. const EVP_CIPHER *cipher, unsigned char *passwd, int passwdSz, int type,
  306. void* heap, byte** out, int* outSz)
  307. {
  308. int ret = 1;
  309. byte* tmp = NULL;
  310. byte* cipherInfo = NULL;
  311. int pemSz = 0;
  312. /* Macro doesn't always use it. */
  313. (void)heap;
  314. /* Encrypt DER buffer if required. */
  315. if ((ret == 1) && (passwd != NULL) && (passwdSz > 0) && (cipher != NULL)) {
  316. int blockSz = wolfSSL_EVP_CIPHER_block_size(cipher);
  317. byte *tmpBuf;
  318. /* Add space for padding. */
  319. tmpBuf = (byte*)XREALLOC(der, (size_t)(derSz + blockSz), heap,
  320. DYNAMIC_TYPE_TMP_BUFFER);
  321. if (tmpBuf == NULL) {
  322. WOLFSSL_ERROR_MSG("Extending DER buffer failed");
  323. ret = 0; /* der buffer is free'd at the end of the function */
  324. }
  325. else {
  326. der = tmpBuf;
  327. /* Encrypt DER inline. */
  328. ret = EncryptDerKey(der, &derSz, cipher, passwd, passwdSz,
  329. &cipherInfo, derSz + blockSz);
  330. if (ret != 1) {
  331. WOLFSSL_ERROR_MSG("EncryptDerKey failed");
  332. }
  333. }
  334. }
  335. if (ret == 1) {
  336. /* Calculate PEM encoding size. */
  337. pemSz = wc_DerToPemEx(der, (word32)derSz, NULL, 0, cipherInfo, type);
  338. if (pemSz <= 0) {
  339. WOLFSSL_ERROR_MSG("wc_DerToPemEx failed");
  340. ret = 0;
  341. }
  342. }
  343. if (ret == 1) {
  344. /* Allocate space for PEM encoding plus a NUL terminator. */
  345. tmp = (byte*)XMALLOC((size_t)(pemSz + 1), NULL, DYNAMIC_TYPE_KEY);
  346. if (tmp == NULL) {
  347. WOLFSSL_ERROR_MSG("malloc failed");
  348. ret = 0;
  349. }
  350. }
  351. if (ret == 1) {
  352. /* DER to PEM */
  353. pemSz = wc_DerToPemEx(der, (word32)derSz, tmp, (word32)pemSz,
  354. cipherInfo, type);
  355. if (pemSz <= 0) {
  356. WOLFSSL_ERROR_MSG("wc_DerToPemEx failed");
  357. ret = 0;
  358. }
  359. }
  360. if (ret == 1) {
  361. /* NUL terminate string - PEM. */
  362. tmp[pemSz] = 0x00;
  363. /* Return allocated buffer and size. */
  364. *out = tmp;
  365. *outSz = pemSz;
  366. /* Don't free returning buffer. */
  367. tmp = NULL;
  368. }
  369. XFREE(tmp, NULL, DYNAMIC_TYPE_KEY);
  370. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
  371. XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER);
  372. return ret;
  373. }
  374. #endif
  375. #endif /* !NO_ASN */
  376. #if !defined(NO_CERTS) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  377. !defined(NO_STDIO_FILESYSTEM) && (!defined(NO_RSA) || !defined(NO_DSA) || \
  378. defined(HAVE_ECC)) && defined(OPENSSL_EXTRA)
  379. /* Print the number bn in hex with name field and indentation indent to file fp.
  380. *
  381. * Used by wolfSSL_DSA_print_fp, wolfSSL_RSA_print_fp and
  382. * wolfSSL_EC_KEY_print_fp to print DSA, RSA and ECC keys and parameters.
  383. *
  384. * @param [in] fp File pointer to write to.
  385. * @param [in] indent Number of spaces to prepend to each line.
  386. * @param [in] field Name of field.
  387. * @param [in] bn Big number to print.
  388. * @return 1 on success.
  389. * @return 0 on failure.
  390. * @return BAD_FUNC_ARG when fp is invalid, indent is less than 0, or field or
  391. * bn or NULL.
  392. */
  393. static int pk_bn_field_print_fp(XFILE fp, int indent, const char* field,
  394. const WOLFSSL_BIGNUM* bn)
  395. {
  396. static const int HEX_INDENT = 4;
  397. static const int MAX_DIGITS_PER_LINE = 30;
  398. int ret = 1;
  399. int i = 0;
  400. char* buf = NULL;
  401. /* Internal function - assume parameters are valid. */
  402. /* Convert BN to hexadecimal character array (allocates buffer). */
  403. buf = wolfSSL_BN_bn2hex(bn);
  404. if (buf == NULL) {
  405. ret = 0;
  406. }
  407. if (ret == 1) {
  408. /* Print leading spaces, name and spaces before data. */
  409. if (indent > 0) {
  410. if (XFPRINTF(fp, "%*s", indent, "") < 0)
  411. ret = 0;
  412. }
  413. }
  414. if (ret == 1) {
  415. if (XFPRINTF(fp, "%s:\n", field) < 0)
  416. ret = 0;
  417. }
  418. if (ret == 1) {
  419. if (indent > 0) {
  420. if (XFPRINTF(fp, "%*s", indent, "") < 0)
  421. ret = 0;
  422. }
  423. }
  424. if (ret == 1) {
  425. if (XFPRINTF(fp, "%*s", HEX_INDENT, "") < 0)
  426. ret = 0;
  427. }
  428. if (ret == 1) {
  429. /* Print first byte - should always exist. */
  430. if ((buf[i] != '\0') && (buf[i+1] != '\0')) {
  431. if (XFPRINTF(fp, "%c", buf[i++]) < 0)
  432. ret = 0;
  433. else if (XFPRINTF(fp, "%c", buf[i++]) < 0)
  434. ret = 0;
  435. }
  436. }
  437. if (ret == 1) {
  438. /* Print each hexadecimal character with byte separator. */
  439. while ((buf[i] != '\0') && (buf[i+1] != '\0')) {
  440. /* Byte separator every two nibbles - one byte. */
  441. if (XFPRINTF(fp, ":") < 0) {
  442. ret = 0;
  443. break;
  444. }
  445. /* New line after every 15 bytes - 30 nibbles. */
  446. if (i % MAX_DIGITS_PER_LINE == 0) {
  447. if (XFPRINTF(fp, "\n") < 0) {
  448. ret = 0;
  449. break;
  450. }
  451. if (indent > 0) {
  452. if (XFPRINTF(fp, "%*s", indent, "") < 0) {
  453. ret = 0;
  454. break;
  455. }
  456. }
  457. if (XFPRINTF(fp, "%*s", HEX_INDENT, "") < 0) {
  458. ret = 0;
  459. break;
  460. }
  461. }
  462. /* Print two nibbles - one byte. */
  463. if (XFPRINTF(fp, "%c", buf[i++]) < 0) {
  464. ret = 0;
  465. break;
  466. }
  467. if (XFPRINTF(fp, "%c", buf[i++]) < 0) {
  468. ret = 0;
  469. break;
  470. }
  471. }
  472. /* Ensure on new line after data. */
  473. if (XFPRINTF(fp, "\n") < 0) {
  474. ret = 0;
  475. }
  476. }
  477. /* Dispose of any allocated character array. */
  478. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  479. return ret;
  480. }
  481. #endif /* !NO_CERTS && XFPRINTF && !NO_FILESYSTEM && !NO_STDIO_FILESYSTEM &&
  482. * (!NO_DSA || !NO_RSA || HAVE_ECC) */
  483. #if defined(XSNPRINTF) && !defined(NO_BIO) && !defined(NO_RSA) && \
  484. !defined(HAVE_FAST_RSA)
  485. /* snprintf() must be available */
  486. /* Maximum number of extra indent spaces on each line. */
  487. #define PRINT_NUM_MAX_INDENT 48
  488. /* Maximum size of a line containing a value. */
  489. #define PRINT_NUM_MAX_VALUE_LINE PRINT_NUM_MAX_INDENT
  490. /* Number of leading spaces on each line. */
  491. #define PRINT_NUM_INDENT_CNT 4
  492. /* Indent spaces for number lines. */
  493. #define PRINT_NUM_INDENT " "
  494. /* 4 leading spaces and 15 bytes with colons is a complete line. */
  495. #define PRINT_NUM_MAX_DIGIT_LINE (PRINT_NUM_INDENT_CNT + 3 * 15)
  496. /* Print indent to BIO.
  497. *
  498. * @param [in] bio BIO object to write to.
  499. * @param [in] line Buffer to put characters to before writing to BIO.
  500. * @param [in] lineLen Length of buffer.
  501. * @return 1 on success.
  502. * @return 0 on failure.
  503. */
  504. static int wolfssl_print_indent(WOLFSSL_BIO* bio, char* line, int lineLen,
  505. int indent)
  506. {
  507. int ret = 1;
  508. if (indent > 0) {
  509. /* Print indent spaces. */
  510. int len_wanted = XSNPRINTF(line, (size_t)lineLen, "%*s", indent, " ");
  511. if (len_wanted >= lineLen) {
  512. WOLFSSL_ERROR_MSG("Buffer overflow formatting indentation");
  513. ret = 0;
  514. }
  515. else {
  516. /* Write indents string to BIO */
  517. if (wolfSSL_BIO_write(bio, line, len_wanted) <= 0) {
  518. ret = 0;
  519. }
  520. }
  521. }
  522. return ret;
  523. }
  524. /* Print out name, and value in decimal and hex to BIO.
  525. *
  526. * @param [in] bio BIO object to write to.
  527. * @param [in] value MP integer to write.
  528. * @param [in] name Name of value.
  529. * @param [in] indent Number of leading spaces before line.
  530. * @return 1 on success.
  531. * @return 0 on failure.
  532. */
  533. static int wolfssl_print_value(WOLFSSL_BIO* bio, mp_int* value,
  534. const char* name, int indent)
  535. {
  536. int ret = 1;
  537. int len;
  538. char line[PRINT_NUM_MAX_VALUE_LINE + 1];
  539. /* Get the length of hex encoded value. */
  540. len = mp_unsigned_bin_size(value);
  541. /* Value must no more than 32-bits - 4 bytes. */
  542. if ((len < 0) || (len > 4)) {
  543. WOLFSSL_ERROR_MSG("Error getting exponent size");
  544. ret = 0;
  545. }
  546. if (ret == 1) {
  547. /* Print any indent spaces. */
  548. ret = wolfssl_print_indent(bio, line, sizeof(line), indent);
  549. }
  550. if (ret == 1) {
  551. /* Get 32-bits of value. */
  552. word32 v = (word32)value->dp[0];
  553. /* Print the line to the string. */
  554. len = (int)XSNPRINTF(line, sizeof(line), "%s %u (0x%x)\n", name, v,
  555. v);
  556. if (len >= (int)sizeof(line)) {
  557. WOLFSSL_ERROR_MSG("Buffer overflow while formatting value");
  558. ret = 0;
  559. } else {
  560. /* Write string to BIO */
  561. if (wolfSSL_BIO_write(bio, line, len) <= 0) {
  562. ret = 0;
  563. }
  564. }
  565. }
  566. return ret;
  567. }
  568. /* Print out name and multi-precision number to BIO.
  569. *
  570. * @param [in] bio BIO object to write to.
  571. * @param [in] num MP integer to write.
  572. * @param [in] name Name of value.
  573. * @param [in] indent Number of leading spaces before each line.
  574. * @return 1 on success.
  575. * @return 0 on failure.
  576. */
  577. static int wolfssl_print_number(WOLFSSL_BIO* bio, mp_int* num, const char* name,
  578. int indent)
  579. {
  580. int ret = 1;
  581. int rawLen = 0;
  582. byte* rawKey = NULL;
  583. char line[PRINT_NUM_MAX_DIGIT_LINE + 1];
  584. int li = 0; /* Line index. */
  585. int i;
  586. /* Allocate a buffer to hold binary encoded data. */
  587. rawLen = mp_unsigned_bin_size(num);
  588. if (rawLen == 0) {
  589. WOLFSSL_ERROR_MSG("Invalid number");
  590. ret = 0;
  591. }
  592. if (ret == 1) {
  593. rawKey = (byte*)XMALLOC((size_t)rawLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  594. if (rawKey == NULL) {
  595. WOLFSSL_ERROR_MSG("Memory allocation error");
  596. ret = 0;
  597. }
  598. }
  599. /* Encode number as big-endian byte array. */
  600. if ((ret == 1) && (mp_to_unsigned_bin(num, rawKey) < 0)) {
  601. ret = 0;
  602. }
  603. if (ret == 1) {
  604. /* Print any indent spaces. */
  605. ret = wolfssl_print_indent(bio, line, sizeof(line), indent);
  606. }
  607. if (ret == 1) {
  608. /* Print header string line to string. */
  609. li = XSNPRINTF(line, sizeof(line), "%s\n", name);
  610. if (li >= (int)sizeof(line)) {
  611. WOLFSSL_ERROR_MSG("Buffer overflow formatting name");
  612. ret = 0;
  613. }
  614. else {
  615. if (wolfSSL_BIO_write(bio, line, li) <= 0) {
  616. ret = 0;
  617. }
  618. }
  619. }
  620. if (ret == 1) {
  621. /* Print any indent spaces. */
  622. ret = wolfssl_print_indent(bio, line, sizeof(line), indent);
  623. }
  624. if (ret == 1) {
  625. /* Start first digit line with spaces.
  626. * Writing out zeros ensures number is a positive value. */
  627. li = XSNPRINTF(line, sizeof(line), PRINT_NUM_INDENT "%s",
  628. mp_leading_bit(num) ? "00:" : "");
  629. if (li >= (int)sizeof(line)) {
  630. WOLFSSL_ERROR_MSG("Buffer overflow formatting spaces");
  631. ret = 0;
  632. }
  633. }
  634. /* Put out each line of numbers. */
  635. for (i = 0; (ret == 1) && (i < rawLen); i++) {
  636. /* Encode another byte as 2 hex digits and append colon. */
  637. int len_wanted = XSNPRINTF(line + li, sizeof(line) - (size_t)li,
  638. "%02x:", rawKey[i]);
  639. /* Check if there was room -- if not, print the current line, not
  640. * including the newest octet.
  641. */
  642. if (len_wanted >= (int)sizeof(line) - li) {
  643. /* bump current octet to the next line. */
  644. --i;
  645. /* More bytes coming so add a line break. */
  646. line[li++] = '\n';
  647. /* Write out the line. */
  648. if (wolfSSL_BIO_write(bio, line, li) <= 0) {
  649. ret = 0;
  650. }
  651. if (ret == 1) {
  652. /* Print any indent spaces. */
  653. ret = wolfssl_print_indent(bio, line, sizeof(line), indent);
  654. }
  655. /* Put the leading spaces on new line. */
  656. XSTRNCPY(line, PRINT_NUM_INDENT, PRINT_NUM_INDENT_CNT + 1);
  657. li = PRINT_NUM_INDENT_CNT;
  658. }
  659. else {
  660. li += len_wanted;
  661. }
  662. }
  663. if (ret == 1) {
  664. /* Put out last line - replace last colon with carriage return. */
  665. line[li-1] = '\n';
  666. if (wolfSSL_BIO_write(bio, line, li) <= 0) {
  667. ret = 0;
  668. }
  669. }
  670. /* Dispose of any allocated data. */
  671. XFREE(rawKey, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  672. return ret;
  673. }
  674. #endif /* XSNPRINTF && !NO_BIO && !NO_RSA && !HAVE_FAST_RSA */
  675. #if !defined(NO_RSA) || (!defined(NO_DH) && !defined(NO_CERTS) && \
  676. defined(HAVE_FIPS) && !FIPS_VERSION_GT(2,0)) || defined(HAVE_ECC)
  677. /* Uses the DER SEQUENCE to determine size of DER data.
  678. *
  679. * Outer SEQUENCE encapsulates all the DER encoding.
  680. * Add the length of the SEQUENCE data to the length of the SEQUENCE header.
  681. *
  682. * @param [in] seq Buffer holding DER encoded sequence.
  683. * @param [in] len Length of data in buffer (may be larger than SEQ).
  684. * @return Size of complete DER encoding on success.
  685. * @return 0 on failure.
  686. */
  687. static int wolfssl_der_length(const unsigned char* seq, int len)
  688. {
  689. int ret = 0;
  690. word32 i = 0;
  691. /* Check it is a SEQUENCE and get the length of the underlying data.
  692. * i is updated to be after SEQUENCE header bytes.
  693. */
  694. if (GetSequence_ex(seq, &i, &ret, (word32)len, 0) >= 0) {
  695. /* Add SEQUENCE header length to underlying data length. */
  696. ret += (int)i;
  697. }
  698. return ret;
  699. }
  700. #endif /* !NO_RSA */
  701. #endif /* OPENSSL_EXTRA */
  702. /*******************************************************************************
  703. * START OF RSA API
  704. ******************************************************************************/
  705. #ifndef NO_RSA
  706. /*
  707. * RSA METHOD
  708. * Could be used to hold function pointers to implementations of RSA operations.
  709. */
  710. #if defined(OPENSSL_EXTRA)
  711. /* Return a blank RSA method and set the name and flags.
  712. *
  713. * Only one implementation of RSA operations.
  714. * name is duplicated.
  715. *
  716. * @param [in] name Name to use in method.
  717. * @param [in] flags Flags to set into method.
  718. * @return Newly allocated RSA method on success.
  719. * @return NULL on failure.
  720. */
  721. WOLFSSL_RSA_METHOD *wolfSSL_RSA_meth_new(const char *name, int flags)
  722. {
  723. WOLFSSL_RSA_METHOD* meth = NULL;
  724. int name_len = 0;
  725. int err;
  726. /* Validate name is not NULL. */
  727. err = (name == NULL);
  728. if (!err) {
  729. /* Allocate an RSA METHOD to return. */
  730. meth = (WOLFSSL_RSA_METHOD*)XMALLOC(sizeof(WOLFSSL_RSA_METHOD), NULL,
  731. DYNAMIC_TYPE_OPENSSL);
  732. err = (meth == NULL);
  733. }
  734. if (!err) {
  735. XMEMSET(meth, 0, sizeof(*meth));
  736. meth->flags = flags;
  737. meth->dynamic = 1;
  738. name_len = (int)XSTRLEN(name);
  739. meth->name = (char*)XMALLOC((size_t)(name_len + 1), NULL,
  740. DYNAMIC_TYPE_OPENSSL);
  741. err = (meth->name == NULL);
  742. }
  743. if (!err) {
  744. XMEMCPY(meth->name, name, (size_t)(name_len + 1));
  745. }
  746. if (err) {
  747. /* meth->name won't be allocated on error. */
  748. XFREE(meth, NULL, DYNAMIC_TYPE_OPENSSL);
  749. meth = NULL;
  750. }
  751. return meth;
  752. }
  753. /* Default RSA method is one with wolfSSL name and no flags.
  754. *
  755. * @return Newly allocated wolfSSL RSA method on success.
  756. * @return NULL on failure.
  757. */
  758. const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_default_method(void)
  759. {
  760. static const WOLFSSL_RSA_METHOD wolfssl_rsa_meth = {
  761. 0, /* No flags. */
  762. (char*)"wolfSSL RSA",
  763. 0 /* Static definition. */
  764. };
  765. return &wolfssl_rsa_meth;
  766. }
  767. /* Dispose of RSA method and allocated data.
  768. *
  769. * @param [in] meth RSA method to free.
  770. */
  771. void wolfSSL_RSA_meth_free(WOLFSSL_RSA_METHOD *meth)
  772. {
  773. /* Free method if available and dynamically allocated. */
  774. if ((meth != NULL) && meth->dynamic) {
  775. /* Name was duplicated and must be freed. */
  776. XFREE(meth->name, NULL, DYNAMIC_TYPE_OPENSSL);
  777. /* Dispose of RSA method. */
  778. XFREE(meth, NULL, DYNAMIC_TYPE_OPENSSL);
  779. }
  780. }
  781. #ifndef NO_WOLFSSL_STUB
  782. /* Stub function for any RSA method setting function.
  783. *
  784. * Nothing is stored - not even flags or name.
  785. *
  786. * @param [in] meth RSA method.
  787. * @param [in] p A pointer.
  788. * @return 1 to indicate success.
  789. */
  790. int wolfSSL_RSA_meth_set(WOLFSSL_RSA_METHOD *meth, void* p)
  791. {
  792. WOLFSSL_STUB("RSA_METHOD is not implemented.");
  793. (void)meth;
  794. (void)p;
  795. return 1;
  796. }
  797. #endif /* !NO_WOLFSSL_STUB */
  798. #endif /* OPENSSL_EXTRA */
  799. /*
  800. * RSA constructor/deconstructor APIs
  801. */
  802. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  803. /* Dispose of RSA key and allocated data.
  804. *
  805. * Cannot use rsa after this call.
  806. *
  807. * @param [in] rsa RSA key to free.
  808. */
  809. void wolfSSL_RSA_free(WOLFSSL_RSA* rsa)
  810. {
  811. int doFree = 1;
  812. WOLFSSL_ENTER("wolfSSL_RSA_free");
  813. /* Validate parameter. */
  814. if (rsa == NULL) {
  815. doFree = 0;
  816. }
  817. if (doFree) {
  818. int err;
  819. /* Decrement reference count. */
  820. wolfSSL_RefDec(&rsa->ref, &doFree, &err);
  821. #ifndef WOLFSSL_REFCNT_ERROR_RETURN
  822. (void)err;
  823. #endif
  824. }
  825. if (doFree) {
  826. void* heap = rsa->heap;
  827. /* Dispose of allocated reference counting data. */
  828. wolfSSL_RefFree(&rsa->ref);
  829. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  830. wolfSSL_CRYPTO_cleanup_ex_data(&rsa->ex_data);
  831. #endif
  832. if (rsa->internal != NULL) {
  833. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && \
  834. !defined(HAVE_FAST_RSA) && defined(WC_RSA_BLINDING)
  835. /* Check if RNG is owned before freeing it. */
  836. if (rsa->ownRng) {
  837. WC_RNG* rng = ((RsaKey*)(rsa->internal))->rng;
  838. if ((rng != NULL) && (rng != wolfssl_get_global_rng())) {
  839. wc_FreeRng(rng);
  840. XFREE(rng, heap, DYNAMIC_TYPE_RNG);
  841. }
  842. /* RNG isn't freed by wolfCrypt RSA free. */
  843. }
  844. #endif
  845. /* Dispose of allocated data in wolfCrypt RSA key. */
  846. wc_FreeRsaKey((RsaKey*)rsa->internal);
  847. /* Dispose of memory for wolfCrypt RSA key. */
  848. XFREE(rsa->internal, heap, DYNAMIC_TYPE_RSA);
  849. }
  850. /* Dispose of external representation of RSA values. */
  851. wolfSSL_BN_clear_free(rsa->iqmp);
  852. wolfSSL_BN_clear_free(rsa->dmq1);
  853. wolfSSL_BN_clear_free(rsa->dmp1);
  854. wolfSSL_BN_clear_free(rsa->q);
  855. wolfSSL_BN_clear_free(rsa->p);
  856. wolfSSL_BN_clear_free(rsa->d);
  857. wolfSSL_BN_free(rsa->e);
  858. wolfSSL_BN_free(rsa->n);
  859. #if defined(OPENSSL_EXTRA)
  860. if (rsa->meth) {
  861. wolfSSL_RSA_meth_free((WOLFSSL_RSA_METHOD*)rsa->meth);
  862. }
  863. #endif
  864. /* Set back to NULLs for safety. */
  865. ForceZero(rsa, sizeof(*rsa));
  866. XFREE(rsa, heap, DYNAMIC_TYPE_RSA);
  867. (void)heap;
  868. }
  869. }
  870. /* Allocate and initialize a new RSA key.
  871. *
  872. * Not OpenSSL API.
  873. *
  874. * @param [in] heap Heap hint for dynamic memory allocation.
  875. * @param [in] devId Device identifier value.
  876. * @return RSA key on success.
  877. * @return NULL on failure.
  878. */
  879. WOLFSSL_RSA* wolfSSL_RSA_new_ex(void* heap, int devId)
  880. {
  881. WOLFSSL_RSA* rsa = NULL;
  882. RsaKey* key = NULL;
  883. int err = 0;
  884. int rsaKeyInited = 0;
  885. WOLFSSL_ENTER("wolfSSL_RSA_new");
  886. /* Allocate memory for new wolfCrypt RSA key. */
  887. key = (RsaKey*)XMALLOC(sizeof(RsaKey), heap, DYNAMIC_TYPE_RSA);
  888. if (key == NULL) {
  889. WOLFSSL_ERROR_MSG("wolfSSL_RSA_new malloc RsaKey failure");
  890. err = 1;
  891. }
  892. if (!err) {
  893. /* Allocate memory for new RSA key. */
  894. rsa = (WOLFSSL_RSA*)XMALLOC(sizeof(WOLFSSL_RSA), heap,
  895. DYNAMIC_TYPE_RSA);
  896. if (rsa == NULL) {
  897. WOLFSSL_ERROR_MSG("wolfSSL_RSA_new malloc WOLFSSL_RSA failure");
  898. err = 1;
  899. }
  900. }
  901. if (!err) {
  902. /* Clear all fields of RSA key. */
  903. XMEMSET(rsa, 0, sizeof(WOLFSSL_RSA));
  904. /* Cache heap to use for all allocations. */
  905. rsa->heap = heap;
  906. #ifdef OPENSSL_EXTRA
  907. /* Always have a method set. */
  908. rsa->meth = wolfSSL_RSA_get_default_method();
  909. #endif
  910. /* Initialize reference counting. */
  911. wolfSSL_RefInit(&rsa->ref, &err);
  912. #ifdef WOLFSSL_REFCNT_ERROR_RETURN
  913. }
  914. if (!err) {
  915. #endif
  916. /* Initialize wolfCrypt RSA key. */
  917. if (wc_InitRsaKey_ex(key, heap, devId) != 0) {
  918. WOLFSSL_ERROR_MSG("InitRsaKey WOLFSSL_RSA failure");
  919. err = 1;
  920. }
  921. else {
  922. rsaKeyInited = 1;
  923. }
  924. }
  925. #if !defined(HAVE_FIPS) && !defined(HAVE_USER_RSA) && \
  926. !defined(HAVE_FAST_RSA) && defined(WC_RSA_BLINDING)
  927. if (!err) {
  928. WC_RNG* rng;
  929. /* Create a local RNG. */
  930. rng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), heap, DYNAMIC_TYPE_RNG);
  931. if ((rng != NULL) && (wc_InitRng_ex(rng, heap, devId) != 0)) {
  932. WOLFSSL_MSG("InitRng failure, attempting to use global RNG");
  933. XFREE(rng, heap, DYNAMIC_TYPE_RNG);
  934. rng = NULL;
  935. }
  936. rsa->ownRng = 1;
  937. if (rng == NULL) {
  938. /* Get the wolfSSL global RNG - not thread safe. */
  939. rng = wolfssl_get_global_rng();
  940. rsa->ownRng = 0;
  941. }
  942. if (rng == NULL) {
  943. /* Couldn't create global either. */
  944. WOLFSSL_ERROR_MSG("wolfSSL_RSA_new no WC_RNG for blinding");
  945. err = 1;
  946. }
  947. else {
  948. /* Set the local or global RNG into the wolfCrypt RSA key. */
  949. (void)wc_RsaSetRNG(key, rng);
  950. /* Won't fail as key and rng are not NULL. */
  951. }
  952. }
  953. #endif /* !HAVE_FIPS && !HAVE_USER_RSA && !HAVE_FAST_RSA &&
  954. * WC_RSA_BLINDING */
  955. if (!err) {
  956. /* Set wolfCrypt RSA key into RSA key. */
  957. rsa->internal = key;
  958. /* Data from external RSA key has not been set into internal one. */
  959. rsa->inSet = 0;
  960. }
  961. if (err) {
  962. /* Dispose of any allocated data on error. */
  963. /* No failure after RNG allocation - no need to free RNG. */
  964. if (rsaKeyInited) {
  965. wc_FreeRsaKey(key);
  966. }
  967. XFREE(key, heap, DYNAMIC_TYPE_RSA);
  968. XFREE(rsa, heap, DYNAMIC_TYPE_RSA);
  969. /* Return NULL. */
  970. rsa = NULL;
  971. }
  972. return rsa;
  973. }
  974. /* Allocate and initialize a new RSA key.
  975. *
  976. * @return RSA key on success.
  977. * @return NULL on failure.
  978. */
  979. WOLFSSL_RSA* wolfSSL_RSA_new(void)
  980. {
  981. /* Call wolfSSL API to do work. */
  982. return wolfSSL_RSA_new_ex(NULL, INVALID_DEVID);
  983. }
  984. /* Increments ref count of RSA key.
  985. *
  986. * @param [in, out] rsa RSA key.
  987. * @return 1 on success
  988. * @return 0 on error
  989. */
  990. int wolfSSL_RSA_up_ref(WOLFSSL_RSA* rsa)
  991. {
  992. int err = 0;
  993. if (rsa != NULL) {
  994. wolfSSL_RefInc(&rsa->ref, &err);
  995. }
  996. return !err;
  997. }
  998. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  999. #ifdef OPENSSL_EXTRA
  1000. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  1001. /* Allocate a new RSA key and make it a copy.
  1002. *
  1003. * Encodes to and from DER to copy.
  1004. *
  1005. * @param [in] rsa RSA key to duplicate.
  1006. * @return RSA key on success.
  1007. * @return NULL on error.
  1008. */
  1009. WOLFSSL_RSA* wolfSSL_RSAPublicKey_dup(WOLFSSL_RSA *rsa)
  1010. {
  1011. WOLFSSL_RSA* ret = NULL;
  1012. int derSz = 0;
  1013. byte* derBuf = NULL;
  1014. int err;
  1015. WOLFSSL_ENTER("wolfSSL_RSAPublicKey_dup");
  1016. err = (rsa == NULL);
  1017. if (!err) {
  1018. /* Create a new RSA key to return. */
  1019. ret = wolfSSL_RSA_new();
  1020. if (ret == NULL) {
  1021. WOLFSSL_ERROR_MSG("Error creating a new WOLFSSL_RSA structure");
  1022. err = 1;
  1023. }
  1024. }
  1025. if (!err) {
  1026. /* Encode RSA public key to copy to DER - allocates DER buffer. */
  1027. if ((derSz = wolfSSL_RSA_To_Der(rsa, &derBuf, 1, rsa->heap)) < 0) {
  1028. WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed");
  1029. err = 1;
  1030. }
  1031. }
  1032. if (!err) {
  1033. /* Decode DER of the RSA public key into new key. */
  1034. if (wolfSSL_RSA_LoadDer_ex(ret, derBuf, derSz,
  1035. WOLFSSL_RSA_LOAD_PUBLIC) != 1) {
  1036. WOLFSSL_ERROR_MSG("wolfSSL_RSA_LoadDer_ex failed");
  1037. err = 1;
  1038. }
  1039. }
  1040. /* Dispose of any allocated DER buffer. */
  1041. XFREE(derBuf, rsa ? rsa->heap : NULL, DYNAMIC_TYPE_ASN1);
  1042. if (err) {
  1043. /* Disposes of any created RSA key - on error. */
  1044. wolfSSL_RSA_free(ret);
  1045. ret = NULL;
  1046. }
  1047. return ret;
  1048. }
  1049. /* wolfSSL_RSAPrivateKey_dup not supported */
  1050. #endif /* WOLFSSL_KEY_GEN && !HAVE_USER_RSA */
  1051. #ifndef HAVE_USER_RSA
  1052. static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
  1053. void* heap);
  1054. #endif
  1055. /*
  1056. * RSA to/from bin APIs
  1057. */
  1058. /* Convert RSA public key data to internal.
  1059. *
  1060. * Creates new RSA key from the DER encoded RSA public key.
  1061. *
  1062. * @param [out] out Pointer to RSA key to return through. May be NULL.
  1063. * @param [in, out] derBuf Pointer to start of DER encoded data.
  1064. * @param [in] derSz Length of the data in the DER buffer.
  1065. * @return RSA key on success.
  1066. * @return NULL on failure.
  1067. */
  1068. WOLFSSL_RSA *wolfSSL_d2i_RSAPublicKey(WOLFSSL_RSA **out,
  1069. const unsigned char **derBuf, long derSz)
  1070. {
  1071. WOLFSSL_RSA *rsa = NULL;
  1072. int err = 0;
  1073. WOLFSSL_ENTER("wolfSSL_d2i_RSAPublicKey");
  1074. /* Validate parameters. */
  1075. if (derBuf == NULL) {
  1076. WOLFSSL_ERROR_MSG("Bad argument");
  1077. err = 1;
  1078. }
  1079. /* Create a new RSA key to return. */
  1080. if ((!err) && ((rsa = wolfSSL_RSA_new()) == NULL)) {
  1081. WOLFSSL_ERROR_MSG("RSA_new failed");
  1082. err = 1;
  1083. }
  1084. /* Decode RSA key from DER. */
  1085. if ((!err) && (wolfSSL_RSA_LoadDer_ex(rsa, *derBuf, (int)derSz,
  1086. WOLFSSL_RSA_LOAD_PUBLIC) != 1)) {
  1087. WOLFSSL_ERROR_MSG("RSA_LoadDer failed");
  1088. err = 1;
  1089. }
  1090. if ((!err) && (out != NULL)) {
  1091. /* Return through parameter too. */
  1092. *out = rsa;
  1093. /* Move buffer on by the used amount. */
  1094. *derBuf += wolfssl_der_length(*derBuf, (int)derSz);
  1095. }
  1096. if (err) {
  1097. /* Dispose of any created RSA key. */
  1098. wolfSSL_RSA_free(rsa);
  1099. rsa = NULL;
  1100. }
  1101. return rsa;
  1102. }
  1103. /* Convert RSA private key data to internal.
  1104. *
  1105. * Create a new RSA key from the DER encoded RSA private key.
  1106. *
  1107. * @param [out] out Pointer to RSA key to return through. May be NULL.
  1108. * @param [in, out] derBuf Pointer to start of DER encoded data.
  1109. * @param [in] derSz Length of the data in the DER buffer.
  1110. * @return RSA key on success.
  1111. * @return NULL on failure.
  1112. */
  1113. WOLFSSL_RSA *wolfSSL_d2i_RSAPrivateKey(WOLFSSL_RSA **out,
  1114. const unsigned char **derBuf, long derSz)
  1115. {
  1116. WOLFSSL_RSA *rsa = NULL;
  1117. int err = 0;
  1118. WOLFSSL_ENTER("wolfSSL_d2i_RSAPublicKey");
  1119. /* Validate parameters. */
  1120. if (derBuf == NULL) {
  1121. WOLFSSL_ERROR_MSG("Bad argument");
  1122. err = 1;
  1123. }
  1124. /* Create a new RSA key to return. */
  1125. if ((!err) && ((rsa = wolfSSL_RSA_new()) == NULL)) {
  1126. WOLFSSL_ERROR_MSG("RSA_new failed");
  1127. err = 1;
  1128. }
  1129. /* Decode RSA key from DER. */
  1130. if ((!err) && (wolfSSL_RSA_LoadDer_ex(rsa, *derBuf, (int)derSz,
  1131. WOLFSSL_RSA_LOAD_PRIVATE) != 1)) {
  1132. WOLFSSL_ERROR_MSG("RSA_LoadDer failed");
  1133. err = 1;
  1134. }
  1135. if ((!err) && (out != NULL)) {
  1136. /* Return through parameter too. */
  1137. *out = rsa;
  1138. /* Move buffer on by the used amount. */
  1139. *derBuf += wolfssl_der_length(*derBuf, (int)derSz);
  1140. }
  1141. if (err) {
  1142. /* Dispose of any created RSA key. */
  1143. wolfSSL_RSA_free(rsa);
  1144. rsa = NULL;
  1145. }
  1146. return rsa;
  1147. }
  1148. #if defined(OPENSSL_EXTRA) && !defined(HAVE_USER_RSA) && \
  1149. !defined(HAVE_FAST_RSA)
  1150. /* Converts an internal RSA structure to DER format for the private key.
  1151. *
  1152. * If "pp" is null then buffer size only is returned.
  1153. * If "*pp" is null then a created buffer is set in *pp and the caller is
  1154. * responsible for free'ing it.
  1155. *
  1156. * @param [in] rsa RSA key.
  1157. * @param [in, out] pp On in, pointer to allocated buffer or NULL.
  1158. * May be NULL.
  1159. * On out, newly allocated buffer or pointer to byte after
  1160. * encoding in passed in buffer.
  1161. *
  1162. * @return Size of DER encoding on success
  1163. * @return BAD_FUNC_ARG when rsa is NULL.
  1164. * @return 0 on failure.
  1165. */
  1166. int wolfSSL_i2d_RSAPrivateKey(WOLFSSL_RSA *rsa, unsigned char **pp)
  1167. {
  1168. int ret;
  1169. WOLFSSL_ENTER("wolfSSL_i2d_RSAPrivateKey");
  1170. /* Validate parameters. */
  1171. if (rsa == NULL) {
  1172. WOLFSSL_ERROR_MSG("Bad Function Arguments");
  1173. ret = BAD_FUNC_ARG;
  1174. }
  1175. /* Encode the RSA key as a DER. Call allocates buffer into pp.
  1176. * No heap hint as this gets returned to the user */
  1177. else if ((ret = wolfSSL_RSA_To_Der_ex(rsa, pp, 0, NULL)) < 0) {
  1178. WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed");
  1179. ret = 0;
  1180. }
  1181. /* Size of DER encoding. */
  1182. return ret;
  1183. }
  1184. /* Converts an internal RSA structure to DER format for the public key.
  1185. *
  1186. * If "pp" is null then buffer size only is returned.
  1187. * If "*pp" is null then a created buffer is set in *pp and the caller is
  1188. * responsible for free'ing it.
  1189. *
  1190. * @param [in] rsa RSA key.
  1191. * @param [in, out] pp On in, pointer to allocated buffer or NULL.
  1192. * May be NULL.
  1193. * On out, newly allocated buffer or pointer to byte after
  1194. * encoding in passed in buffer.
  1195. * @return Size of DER encoding on success
  1196. * @return BAD_FUNC_ARG when rsa is NULL.
  1197. * @return 0 on failure.
  1198. */
  1199. int wolfSSL_i2d_RSAPublicKey(WOLFSSL_RSA *rsa, unsigned char **pp)
  1200. {
  1201. int ret;
  1202. WOLFSSL_ENTER("wolfSSL_i2d_RSAPublicKey");
  1203. /* check for bad functions arguments */
  1204. if (rsa == NULL) {
  1205. WOLFSSL_ERROR_MSG("Bad Function Arguments");
  1206. ret = BAD_FUNC_ARG;
  1207. }
  1208. /* Encode the RSA key as a DER. Call allocates buffer into pp.
  1209. * No heap hint as this gets returned to the user */
  1210. else if ((ret = wolfSSL_RSA_To_Der_ex(rsa, pp, 1, NULL)) < 0) {
  1211. WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed");
  1212. ret = 0;
  1213. }
  1214. return ret;
  1215. }
  1216. #endif /* defined(OPENSSL_EXTRA) && !defined(HAVE_USER_RSA) &&
  1217. * !defined(HAVE_FAST_RSA) */
  1218. #endif /* OPENSSL_EXTRA */
  1219. /*
  1220. * RSA to/from BIO APIs
  1221. */
  1222. /* wolfSSL_d2i_RSAPublicKey_bio not supported */
  1223. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  1224. || defined(WOLFSSL_NGINX) || defined(WOLFSSL_QT)
  1225. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  1226. !defined(HAVE_FAST_RSA) && !defined(NO_BIO)
  1227. /* Read DER data from a BIO.
  1228. *
  1229. * DER structures start with a constructed sequence. Use this to calculate the
  1230. * total length of the DER data.
  1231. *
  1232. * @param [in] bio BIO object to read from.
  1233. * @param [out] out Buffer holding DER encoding.
  1234. * @return Number of bytes to DER encoding on success.
  1235. * @return 0 on failure.
  1236. */
  1237. static int wolfssl_read_der_bio(WOLFSSL_BIO* bio, unsigned char** out)
  1238. {
  1239. int err = 0;
  1240. unsigned char seq[MAX_SEQ_SZ];
  1241. unsigned char* der = NULL;
  1242. int derLen = 0;
  1243. /* Read in a minimal amount to get a SEQUENCE header of any size. */
  1244. if (wolfSSL_BIO_read(bio, seq, sizeof(seq)) != sizeof(seq)) {
  1245. WOLFSSL_ERROR_MSG("wolfSSL_BIO_read() of sequence failure");
  1246. err = 1;
  1247. }
  1248. /* Calculate complete DER encoding length. */
  1249. if ((!err) && ((derLen = wolfssl_der_length(seq, sizeof(seq))) <= 0)) {
  1250. WOLFSSL_ERROR_MSG("DER SEQUENCE decode failed");
  1251. err = 1;
  1252. }
  1253. /* Allocate a buffer to read DER data into. */
  1254. if ((!err) && ((der = (unsigned char*)XMALLOC((size_t)derLen, bio->heap,
  1255. DYNAMIC_TYPE_TMP_BUFFER)) == NULL)) {
  1256. WOLFSSL_ERROR_MSG("Malloc failure");
  1257. err = 1;
  1258. }
  1259. if (!err) {
  1260. /* Calculate the unread amount. */
  1261. int len = derLen - (int)sizeof(seq);
  1262. /* Copy the previously read data into the buffer. */
  1263. XMEMCPY(der, seq, sizeof(seq));
  1264. /* Read rest of DER data from BIO. */
  1265. if (wolfSSL_BIO_read(bio, der + sizeof(seq), len) != len) {
  1266. WOLFSSL_ERROR_MSG("wolfSSL_BIO_read() failure");
  1267. err = 1;
  1268. }
  1269. }
  1270. if (!err) {
  1271. /* Return buffer through parameter. */
  1272. *out = der;
  1273. }
  1274. if (err) {
  1275. /* Dispose of any allocated buffer on error. */
  1276. XFREE(der, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1277. derLen = 0;
  1278. }
  1279. return derLen;
  1280. }
  1281. /* Reads the RSA private key data from a BIO to the internal form.
  1282. *
  1283. * Creates new RSA key from the DER encoded RSA private key read from the BIO.
  1284. *
  1285. * @param [in] bio BIO object to read from.
  1286. * @param [out] out Pointer to RSA key to return through. May be NULL.
  1287. * @return RSA key on success.
  1288. * @return NULL on failure.
  1289. */
  1290. WOLFSSL_RSA* wolfSSL_d2i_RSAPrivateKey_bio(WOLFSSL_BIO *bio, WOLFSSL_RSA **out)
  1291. {
  1292. WOLFSSL_RSA* key = NULL;
  1293. unsigned char* der = NULL;
  1294. int derLen = 0;
  1295. int err;
  1296. WOLFSSL_ENTER("wolfSSL_d2i_RSAPrivateKey_bio");
  1297. /* Validate parameters. */
  1298. err = (bio == NULL);
  1299. /* Read just DER encoding from BIO - buffer allocated in call. */
  1300. if ((!err) && ((derLen = wolfssl_read_der_bio(bio, &der)) == 0)) {
  1301. err = 1;
  1302. }
  1303. if (!err) {
  1304. /* Keep der for call to deallocate. */
  1305. const unsigned char* cder = der;
  1306. /* Create an RSA key from the data from the BIO. */
  1307. key = wolfSSL_d2i_RSAPrivateKey(NULL, &cder, derLen);
  1308. err = (key == NULL);
  1309. }
  1310. if ((!err) && (out != NULL)) {
  1311. /* Return the created RSA key through the parameter. */
  1312. *out = key;
  1313. }
  1314. if (err) {
  1315. /* Dispose of created key on error. */
  1316. wolfSSL_RSA_free(key);
  1317. key = NULL;
  1318. }
  1319. /* Dispose of allocated data. */
  1320. XFREE(der, bio ? bio->heap : NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1321. return key;
  1322. }
  1323. #endif /* defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) &&
  1324. * !defined(HAVE_FAST_RSA) && !NO_BIO */
  1325. #endif /* OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY || WOLFSSL_QT */
  1326. /*
  1327. * RSA DER APIs
  1328. */
  1329. #ifdef OPENSSL_EXTRA
  1330. #ifndef HAVE_USER_RSA
  1331. /* Create a DER encoding of key.
  1332. *
  1333. * Not OpenSSL API.
  1334. *
  1335. * @param [in] rsa RSA key.
  1336. * @param [out] outBuf Allocated buffer containing DER encoding.
  1337. * May be NULL.
  1338. * @param [in] publicKey Whether to encode as public key.
  1339. * @param [in] heap Heap hint.
  1340. * @return Encoding size on success.
  1341. * @return Negative on failure.
  1342. */
  1343. int wolfSSL_RSA_To_Der(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
  1344. void* heap)
  1345. {
  1346. byte* p = NULL;
  1347. int ret;
  1348. if (outBuf != NULL) {
  1349. p = *outBuf;
  1350. }
  1351. ret = wolfSSL_RSA_To_Der_ex(rsa, outBuf, publicKey, heap);
  1352. if ((ret > 0) && (p != NULL)) {
  1353. *outBuf = p;
  1354. }
  1355. return ret;
  1356. }
  1357. /* Create a DER encoding of key.
  1358. *
  1359. * Buffer allocated with heap and DYNAMIC_TYPE_TMP_BUFFER.
  1360. *
  1361. * @param [in] rsa RSA key.
  1362. * @param [in, out] outBuf On in, pointer to allocated buffer or NULL.
  1363. * May be NULL.
  1364. * On out, newly allocated buffer or pointer to byte
  1365. * after encoding in passed in buffer.
  1366. * @param [in] publicKey Whether to encode as public key.
  1367. * @param [in] heap Heap hint.
  1368. * @return Encoding size on success.
  1369. * @return Negative on failure.
  1370. */
  1371. static int wolfSSL_RSA_To_Der_ex(WOLFSSL_RSA* rsa, byte** outBuf, int publicKey,
  1372. void* heap)
  1373. {
  1374. int ret = 1;
  1375. int derSz = 0;
  1376. byte* derBuf = NULL;
  1377. WOLFSSL_ENTER("wolfSSL_RSA_To_Der");
  1378. /* Unused if memory is disabled. */
  1379. (void)heap;
  1380. /* Validate parameters. */
  1381. if ((rsa == NULL) || ((publicKey != 0) && (publicKey != 1))) {
  1382. WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", BAD_FUNC_ARG);
  1383. ret = BAD_FUNC_ARG;
  1384. }
  1385. /* Push external RSA data into internal RSA key if not set. */
  1386. if ((ret == 1) && (!rsa->inSet)) {
  1387. ret = SetRsaInternal(rsa);
  1388. }
  1389. /* wc_RsaKeyToPublicDer encode regardless of values. */
  1390. if ((ret == 1) && publicKey && (mp_iszero(&((RsaKey*)rsa->internal)->n) ||
  1391. mp_iszero(&((RsaKey*)rsa->internal)->e))) {
  1392. ret = BAD_FUNC_ARG;
  1393. }
  1394. if (ret == 1) {
  1395. if (publicKey) {
  1396. /* Calculate length of DER encoded RSA public key. */
  1397. derSz = wc_RsaPublicKeyDerSize((RsaKey*)rsa->internal, 1);
  1398. if (derSz < 0) {
  1399. WOLFSSL_ERROR_MSG("wc_RsaPublicKeyDerSize failed");
  1400. ret = derSz;
  1401. }
  1402. }
  1403. else {
  1404. /* Calculate length of DER encoded RSA private key. */
  1405. derSz = wc_RsaKeyToDer((RsaKey*)rsa->internal, NULL, 0);
  1406. if (derSz < 0) {
  1407. WOLFSSL_ERROR_MSG("wc_RsaKeyToDer failed");
  1408. ret = derSz;
  1409. }
  1410. }
  1411. }
  1412. if ((ret == 1) && (outBuf != NULL)) {
  1413. derBuf = *outBuf;
  1414. if (derBuf == NULL) {
  1415. /* Allocate buffer to hold DER encoded RSA key. */
  1416. derBuf = (byte*)XMALLOC((size_t)derSz, heap,
  1417. DYNAMIC_TYPE_TMP_BUFFER);
  1418. if (derBuf == NULL) {
  1419. WOLFSSL_ERROR_MSG("Memory allocation failed");
  1420. ret = MEMORY_ERROR;
  1421. }
  1422. }
  1423. }
  1424. if ((ret == 1) && (outBuf != NULL)) {
  1425. if (publicKey > 0) {
  1426. /* RSA public key to DER. */
  1427. derSz = wc_RsaKeyToPublicDer((RsaKey*)rsa->internal, derBuf,
  1428. (word32)derSz);
  1429. }
  1430. else {
  1431. /* RSA private key to DER. */
  1432. derSz = wc_RsaKeyToDer((RsaKey*)rsa->internal, derBuf,
  1433. (word32)derSz);
  1434. }
  1435. if (derSz < 0) {
  1436. WOLFSSL_ERROR_MSG("RSA key encoding failed");
  1437. ret = derSz;
  1438. }
  1439. else if ((*outBuf) != NULL) {
  1440. derBuf = NULL;
  1441. *outBuf += derSz;
  1442. }
  1443. else {
  1444. /* Return allocated buffer. */
  1445. *outBuf = derBuf;
  1446. }
  1447. }
  1448. if (ret == 1) {
  1449. /* Success - return DER encoding size. */
  1450. ret = derSz;
  1451. }
  1452. if ((outBuf != NULL) && (*outBuf != derBuf)) {
  1453. /* Not returning buffer, needs to be disposed of. */
  1454. XFREE(derBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  1455. }
  1456. WOLFSSL_LEAVE("wolfSSL_RSA_To_Der", ret);
  1457. return ret;
  1458. }
  1459. #endif /* !HAVE_USER_RSA */
  1460. #endif /* OPENSSL_EXTRA */
  1461. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  1462. /* Load the DER encoded private RSA key.
  1463. *
  1464. * Not OpenSSL API.
  1465. *
  1466. * @param [in] rsa RSA key.
  1467. * @param [in] derBuf Buffer holding DER encoding.
  1468. * @param [in] derSz Length of DER encoding.
  1469. * @return 1 on success.
  1470. * @return -1 on failure.
  1471. */
  1472. int wolfSSL_RSA_LoadDer(WOLFSSL_RSA* rsa, const unsigned char* derBuf,
  1473. int derSz)
  1474. {
  1475. /* Call implementation that handles both private and public keys. */
  1476. return wolfSSL_RSA_LoadDer_ex(rsa, derBuf, derSz, WOLFSSL_RSA_LOAD_PRIVATE);
  1477. }
  1478. /* Load the DER encoded public or private RSA key.
  1479. *
  1480. * Not OpenSSL API.
  1481. *
  1482. * @param [in] rsa RSA key.
  1483. * @param [in] derBuf Buffer holding DER encoding.
  1484. * @param [in] derSz Length of DER encoding.
  1485. * @param [in] opt Indicates public or private key.
  1486. * (WOLFSSL_RSA_LOAD_PUBLIC or WOLFSSL_RSA_LOAD_PRIVATE)
  1487. * @return 1 on success.
  1488. * @return -1 on failure.
  1489. */
  1490. int wolfSSL_RSA_LoadDer_ex(WOLFSSL_RSA* rsa, const unsigned char* derBuf,
  1491. int derSz, int opt)
  1492. {
  1493. int ret = 1;
  1494. int res;
  1495. word32 idx = 0;
  1496. word32 algId;
  1497. WOLFSSL_ENTER("wolfSSL_RSA_LoadDer");
  1498. /* Validate parameters. */
  1499. if ((rsa == NULL) || (rsa->internal == NULL) || (derBuf == NULL) ||
  1500. (derSz <= 0)) {
  1501. WOLFSSL_ERROR_MSG("Bad function arguments");
  1502. ret = -1;
  1503. }
  1504. if (ret == 1) {
  1505. rsa->pkcs8HeaderSz = 0;
  1506. /* Check if input buffer has PKCS8 header. In the case that it does not
  1507. * have a PKCS8 header then do not error out. */
  1508. res = ToTraditionalInline_ex((const byte*)derBuf, &idx, (word32)derSz,
  1509. &algId);
  1510. if (res > 0) {
  1511. /* Store size of PKCS#8 header for encoding. */
  1512. WOLFSSL_MSG("Found PKCS8 header");
  1513. rsa->pkcs8HeaderSz = (word16)idx;
  1514. }
  1515. /* When decoding and not PKCS#8, return will be ASN_PARSE_E. */
  1516. else if (res != ASN_PARSE_E) {
  1517. /* Something went wrong while decoding. */
  1518. WOLFSSL_ERROR_MSG("Unexpected error with trying to remove PKCS#8 "
  1519. "header");
  1520. ret = -1;
  1521. }
  1522. }
  1523. if (ret == 1) {
  1524. /* Decode private or public key data. */
  1525. if (opt == WOLFSSL_RSA_LOAD_PRIVATE) {
  1526. res = wc_RsaPrivateKeyDecode(derBuf, &idx, (RsaKey*)rsa->internal,
  1527. (word32)derSz);
  1528. }
  1529. else {
  1530. res = wc_RsaPublicKeyDecode(derBuf, &idx, (RsaKey*)rsa->internal,
  1531. (word32)derSz);
  1532. }
  1533. /* Check for error. */
  1534. if (res < 0) {
  1535. if (opt == WOLFSSL_RSA_LOAD_PRIVATE) {
  1536. WOLFSSL_ERROR_MSG("RsaPrivateKeyDecode failed");
  1537. }
  1538. else {
  1539. WOLFSSL_ERROR_MSG("RsaPublicKeyDecode failed");
  1540. }
  1541. WOLFSSL_ERROR_VERBOSE(res);
  1542. ret = -1;
  1543. }
  1544. }
  1545. if (ret == 1) {
  1546. /* Set external RSA key data from wolfCrypt key. */
  1547. if (SetRsaExternal(rsa) != 1) {
  1548. ret = -1;
  1549. }
  1550. else {
  1551. rsa->inSet = 1;
  1552. }
  1553. }
  1554. return ret;
  1555. }
  1556. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  1557. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  1558. #if !defined(NO_BIO) || !defined(NO_FILESYSTEM)
  1559. /* Load DER encoded data into WOLFSSL_RSA object.
  1560. *
  1561. * Creates a new WOLFSSL_RSA object if one is not passed in.
  1562. *
  1563. * @param [in, out] rsa WOLFSSL_RSA object to load into.
  1564. * When rsa or *rsa is NULL a new object is created.
  1565. * When not NULL and *rsa is NULL then new object
  1566. * returned through pointer.
  1567. * @param [in] in DER encoded RSA key data.
  1568. * @param [in] inSz Size of DER encoded data in bytes.
  1569. * @param [in] opt Public or private key encoded in data. Valid values:
  1570. * WOLFSSL_RSA_LOAD_PRIVATE, WOLFSSL_RSA_LOAD_PUBLIC.
  1571. * @return NULL on failure.
  1572. * @return WOLFSSL_RSA object on success.
  1573. */
  1574. static WOLFSSL_RSA* wolfssl_rsa_d2i(WOLFSSL_RSA** rsa, const unsigned char* in,
  1575. long inSz, int opt)
  1576. {
  1577. WOLFSSL_RSA* ret = NULL;
  1578. if ((rsa != NULL) && (*rsa != NULL)) {
  1579. ret = *rsa;
  1580. }
  1581. else {
  1582. ret = wolfSSL_RSA_new();
  1583. }
  1584. if ((ret != NULL) && (wolfSSL_RSA_LoadDer_ex(ret, in, (int)inSz, opt)
  1585. != 1)) {
  1586. if ((rsa == NULL) || (ret != *rsa)) {
  1587. wolfSSL_RSA_free(ret);
  1588. }
  1589. ret = NULL;
  1590. }
  1591. if ((rsa != NULL) && (*rsa == NULL)) {
  1592. *rsa = ret;
  1593. }
  1594. return ret;
  1595. }
  1596. #endif
  1597. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  1598. /*
  1599. * RSA PEM APIs
  1600. */
  1601. #ifdef OPENSSL_EXTRA
  1602. #ifndef NO_BIO
  1603. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  1604. /* Writes PEM encoding of an RSA public key to a BIO.
  1605. *
  1606. * @param [in] bio BIO object to write to.
  1607. * @param [in] rsa RSA key to write.
  1608. * @return 1 on success.
  1609. * @return 0 on failure.
  1610. */
  1611. int wolfSSL_PEM_write_bio_RSA_PUBKEY(WOLFSSL_BIO* bio, WOLFSSL_RSA* rsa)
  1612. {
  1613. int ret = 1;
  1614. int derSz = 0;
  1615. byte* derBuf = NULL;
  1616. WOLFSSL_ENTER("wolfSSL_PEM_write_bio_RSA_PUBKEY");
  1617. /* Validate parameters. */
  1618. if ((bio == NULL) || (rsa == NULL)) {
  1619. WOLFSSL_ERROR_MSG("Bad Function Arguments");
  1620. return 0;
  1621. }
  1622. if ((derSz = wolfSSL_RSA_To_Der(rsa, &derBuf, 1, bio->heap)) < 0) {
  1623. WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed");
  1624. ret = 0;
  1625. }
  1626. if (derBuf == NULL) {
  1627. WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed to get buffer");
  1628. ret = 0;
  1629. }
  1630. if ((ret == 1) && (der_write_to_bio_as_pem(derBuf, derSz, bio,
  1631. PUBLICKEY_TYPE) != WOLFSSL_SUCCESS)) {
  1632. ret = 0;
  1633. }
  1634. /* Dispose of DER buffer. */
  1635. XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1636. return ret;
  1637. }
  1638. #endif /* WOLFSSL_KEY_GEN && !HAVE_USER_RSA */
  1639. #endif /* !NO_BIO */
  1640. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA)
  1641. #ifndef NO_FILESYSTEM
  1642. /* Writes PEM encoding of an RSA public key to a file pointer.
  1643. *
  1644. * @param [in] fp File pointer to write to.
  1645. * @param [in] rsa RSA key to write.
  1646. * @param [in] type PEM type to write out.
  1647. * @return 1 on success.
  1648. * @return 0 on failure.
  1649. */
  1650. static int wolfssl_pem_write_rsa_public_key(XFILE fp, WOLFSSL_RSA* rsa,
  1651. int type)
  1652. {
  1653. int ret = 1;
  1654. int derSz;
  1655. byte* derBuf = NULL;
  1656. /* Validate parameters. */
  1657. if ((fp == XBADFILE) || (rsa == NULL)) {
  1658. WOLFSSL_ERROR_MSG("Bad Function Arguments");
  1659. return 0;
  1660. }
  1661. if ((derSz = wolfSSL_RSA_To_Der(rsa, &derBuf, 1, rsa->heap)) < 0) {
  1662. WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed");
  1663. ret = 0;
  1664. }
  1665. if (derBuf == NULL) {
  1666. WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed to get buffer");
  1667. ret = 0;
  1668. }
  1669. if ((ret == 1) && (der_write_to_file_as_pem(derBuf, derSz, fp, type,
  1670. rsa->heap) != WOLFSSL_SUCCESS)) {
  1671. ret = 0;
  1672. }
  1673. /* Dispose of DER buffer. */
  1674. XFREE(derBuf, rsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
  1675. return ret;
  1676. }
  1677. /* Writes PEM encoding of an RSA public key to a file pointer.
  1678. *
  1679. * Header/footer will contain: PUBLIC KEY
  1680. *
  1681. * @param [in] fp File pointer to write to.
  1682. * @param [in] rsa RSA key to write.
  1683. * @return 1 on success.
  1684. * @return 0 on failure.
  1685. */
  1686. int wolfSSL_PEM_write_RSA_PUBKEY(XFILE fp, WOLFSSL_RSA* rsa)
  1687. {
  1688. return wolfssl_pem_write_rsa_public_key(fp, rsa, PUBLICKEY_TYPE);
  1689. }
  1690. /* Writes PEM encoding of an RSA public key to a file pointer.
  1691. *
  1692. * Header/footer will contain: RSA PUBLIC KEY
  1693. *
  1694. * @param [in] fp File pointer to write to.
  1695. * @param [in] rsa RSA key to write.
  1696. * @return 1 on success.
  1697. * @return 0 on failure.
  1698. */
  1699. int wolfSSL_PEM_write_RSAPublicKey(XFILE fp, WOLFSSL_RSA* rsa)
  1700. {
  1701. return wolfssl_pem_write_rsa_public_key(fp, rsa, RSA_PUBLICKEY_TYPE);
  1702. }
  1703. #endif /* !NO_FILESYSTEM */
  1704. #endif /* WOLFSSL_KEY_GEN && !HAVE_USER_RSA */
  1705. #ifndef NO_BIO
  1706. /* Create an RSA public key by reading the PEM encoded data from the BIO.
  1707. *
  1708. * @param [in] bio BIO object to read from.
  1709. * @param [out] out RSA key created.
  1710. * @param [in] cb Password callback when PEM encrypted.
  1711. * @param [in] pass NUL terminated string for passphrase when PEM encrypted.
  1712. * @return RSA key on success.
  1713. * @return NULL on failure.
  1714. */
  1715. WOLFSSL_RSA *wolfSSL_PEM_read_bio_RSA_PUBKEY(WOLFSSL_BIO* bio,
  1716. WOLFSSL_RSA** out, wc_pem_password_cb* cb, void *pass)
  1717. {
  1718. WOLFSSL_RSA* rsa = NULL;
  1719. DerBuffer* der = NULL;
  1720. int keyFormat = 0;
  1721. WOLFSSL_ENTER("wolfSSL_PEM_read_bio_RSA_PUBKEY");
  1722. if ((bio != NULL) && (pem_read_bio_key(bio, cb, pass, PUBLICKEY_TYPE,
  1723. &keyFormat, &der) >= 0)) {
  1724. rsa = wolfssl_rsa_d2i(out, der->buffer, der->length,
  1725. WOLFSSL_RSA_LOAD_PUBLIC);
  1726. if (rsa == NULL) {
  1727. WOLFSSL_ERROR_MSG("Error loading DER buffer into WOLFSSL_RSA");
  1728. }
  1729. }
  1730. FreeDer(&der);
  1731. if ((out != NULL) && (rsa != NULL)) {
  1732. *out = rsa;
  1733. }
  1734. return rsa;
  1735. }
  1736. #endif /* !NO_BIO */
  1737. #ifndef NO_FILESYSTEM
  1738. /* Create an RSA public key by reading the PEM encoded data from the BIO.
  1739. *
  1740. * Header/footer should contain: PUBLIC KEY
  1741. * PEM decoder supports either 'RSA PUBLIC KEY' or 'PUBLIC KEY'.
  1742. *
  1743. * @param [in] fp File pointer to read from.
  1744. * @param [out] out RSA key created.
  1745. * @param [in] cb Password callback when PEM encrypted.
  1746. * @param [in] pass NUL terminated string for passphrase when PEM encrypted.
  1747. * @return RSA key on success.
  1748. * @return NULL on failure.
  1749. */
  1750. WOLFSSL_RSA *wolfSSL_PEM_read_RSA_PUBKEY(XFILE fp,
  1751. WOLFSSL_RSA** out, wc_pem_password_cb* cb, void *pass)
  1752. {
  1753. WOLFSSL_RSA* rsa = NULL;
  1754. DerBuffer* der = NULL;
  1755. int keyFormat = 0;
  1756. WOLFSSL_ENTER("wolfSSL_PEM_read_RSA_PUBKEY");
  1757. if ((fp != XBADFILE) && (pem_read_file_key(fp, cb, pass, PUBLICKEY_TYPE,
  1758. &keyFormat, &der) >= 0)) {
  1759. rsa = wolfssl_rsa_d2i(out, der->buffer, der->length,
  1760. WOLFSSL_RSA_LOAD_PUBLIC);
  1761. if (rsa == NULL) {
  1762. WOLFSSL_ERROR_MSG("Error loading DER buffer into WOLFSSL_RSA");
  1763. }
  1764. }
  1765. FreeDer(&der);
  1766. if ((out != NULL) && (rsa != NULL)) {
  1767. *out = rsa;
  1768. }
  1769. return rsa;
  1770. }
  1771. /* Create an RSA public key by reading the PEM encoded data from the BIO.
  1772. *
  1773. * Header/footer should contain: RSA PUBLIC KEY
  1774. * PEM decoder supports either 'RSA PUBLIC KEY' or 'PUBLIC KEY'.
  1775. *
  1776. * @param [in] fp File pointer to read from.
  1777. * @param [out] rsa RSA key created.
  1778. * @param [in] cb Password callback when PEM encrypted. May be NULL.
  1779. * @param [in] pass NUL terminated string for passphrase when PEM encrypted.
  1780. * May be NULL.
  1781. * @return RSA key on success.
  1782. * @return NULL on failure.
  1783. */
  1784. WOLFSSL_RSA* wolfSSL_PEM_read_RSAPublicKey(XFILE fp, WOLFSSL_RSA** rsa,
  1785. wc_pem_password_cb* cb, void* pass)
  1786. {
  1787. return wolfSSL_PEM_read_RSA_PUBKEY(fp, rsa, cb, pass);
  1788. }
  1789. #endif /* NO_FILESYSTEM */
  1790. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_USER_RSA) && \
  1791. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  1792. /* Writes PEM encoding of an RSA private key to newly allocated buffer.
  1793. *
  1794. * Buffer returned was allocated with: DYNAMIC_TYPE_KEY.
  1795. *
  1796. * @param [in] rsa RSA key to write.
  1797. * @param [in] cipher Cipher to use when PEM encrypted. May be NULL.
  1798. * @param [in] passwd Password string when PEM encrypted. May be NULL.
  1799. * @param [in] passwdSz Length of password string when PEM encrypted.
  1800. * @param [out] pem Allocated buffer with PEM encoding.
  1801. * @param [out] pLen Length of PEM encoding.
  1802. * @return 1 on success.
  1803. * @return 0 on failure.
  1804. */
  1805. int wolfSSL_PEM_write_mem_RSAPrivateKey(RSA* rsa, const EVP_CIPHER* cipher,
  1806. unsigned char* passwd, int passwdSz, unsigned char **pem, int *pLen)
  1807. {
  1808. int ret = 1;
  1809. byte* derBuf = NULL;
  1810. int derSz = 0;
  1811. WOLFSSL_ENTER("wolfSSL_PEM_write_mem_RSAPrivateKey");
  1812. /* Validate parameters. */
  1813. if ((pem == NULL) || (pLen == NULL) || (rsa == NULL) ||
  1814. (rsa->internal == NULL)) {
  1815. WOLFSSL_ERROR_MSG("Bad function arguments");
  1816. ret = 0;
  1817. }
  1818. /* Set the RSA key data into the wolfCrypt RSA key if not done so. */
  1819. if ((ret == 1) && (!rsa->inSet) && (SetRsaInternal(rsa) != 1)) {
  1820. ret = 0;
  1821. }
  1822. /* Encode wolfCrypt RSA key to DER - derBuf allocated in call. */
  1823. if ((ret == 1) && ((derSz = wolfSSL_RSA_To_Der(rsa, &derBuf, 0,
  1824. rsa->heap)) < 0)) {
  1825. WOLFSSL_ERROR_MSG("wolfSSL_RSA_To_Der failed");
  1826. ret = 0;
  1827. }
  1828. if ((ret == 1) && (der_to_enc_pem_alloc(derBuf, derSz, cipher, passwd,
  1829. passwdSz, PRIVATEKEY_TYPE, NULL, pem, pLen) != 1)) {
  1830. WOLFSSL_ERROR_MSG("der_to_enc_pem_alloc failed");
  1831. ret = 0;
  1832. }
  1833. return ret;
  1834. }
  1835. #ifndef NO_BIO
  1836. /* Writes PEM encoding of an RSA private key to a BIO.
  1837. *
  1838. * @param [in] bio BIO object to write to.
  1839. * @param [in] rsa RSA key to write.
  1840. * @param [in] cipher Cipher to use when PEM encrypted.
  1841. * @param [in] passwd Password string when PEM encrypted.
  1842. * @param [in] len Length of password string when PEM encrypted.
  1843. * @param [in] cb Password callback to use when PEM encrypted.
  1844. * @param [in] arg NUL terminated string for passphrase when PEM encrypted.
  1845. * @return 1 on success.
  1846. * @return 0 on failure.
  1847. */
  1848. int wolfSSL_PEM_write_bio_RSAPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_RSA* rsa,
  1849. const WOLFSSL_EVP_CIPHER* cipher, unsigned char* passwd, int len,
  1850. wc_pem_password_cb* cb, void* arg)
  1851. {
  1852. int ret = 1;
  1853. byte* pem = NULL;
  1854. int pLen = 0;
  1855. (void)cb;
  1856. (void)arg;
  1857. WOLFSSL_ENTER("wolfSSL_PEM_write_bio_RSAPrivateKey");
  1858. /* Validate parameters. */
  1859. if ((bio == NULL) || (rsa == NULL) || (rsa->internal == NULL)) {
  1860. WOLFSSL_ERROR_MSG("Bad function arguments");
  1861. ret = 0;
  1862. }
  1863. if (ret == 1) {
  1864. /* Write PEM to buffer that is allocated in the call. */
  1865. ret = wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, cipher, passwd, len,
  1866. &pem, &pLen);
  1867. if (ret != 1) {
  1868. WOLFSSL_ERROR_MSG("wolfSSL_PEM_write_mem_RSAPrivateKey failed");
  1869. }
  1870. }
  1871. /* Write PEM to BIO. */
  1872. if ((ret == 1) && (wolfSSL_BIO_write(bio, pem, pLen) <= 0)) {
  1873. WOLFSSL_ERROR_MSG("RSA private key BIO write failed");
  1874. ret = 0;
  1875. }
  1876. /* Dispose of any allocated PEM buffer. */
  1877. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  1878. return ret;
  1879. }
  1880. #endif /* !NO_BIO */
  1881. #ifndef NO_FILESYSTEM
  1882. /* Writes PEM encoding of an RSA private key to a file pointer.
  1883. *
  1884. * TODO: Support use of the password callback and callback context.
  1885. *
  1886. * @param [in] fp File pointer to write to.
  1887. * @param [in] rsa RSA key to write.
  1888. * @param [in] cipher Cipher to use when PEM encrypted. May be NULL.
  1889. * @param [in] passwd Password string when PEM encrypted. May be NULL.
  1890. * @param [in] passwdSz Length of password string when PEM encrypted.
  1891. * @param [in] cb Password callback to use when PEM encrypted. Unused.
  1892. * @param [in] arg NUL terminated string for passphrase when PEM
  1893. * encrypted. Unused.
  1894. * @return 1 on success.
  1895. * @return 0 on failure.
  1896. */
  1897. int wolfSSL_PEM_write_RSAPrivateKey(XFILE fp, WOLFSSL_RSA *rsa,
  1898. const EVP_CIPHER *cipher, unsigned char *passwd, int passwdSz,
  1899. wc_pem_password_cb *cb, void *arg)
  1900. {
  1901. int ret = 1;
  1902. byte* pem = NULL;
  1903. int pLen;
  1904. (void)cb;
  1905. (void)arg;
  1906. WOLFSSL_ENTER("wolfSSL_PEM_write_RSAPrivateKey");
  1907. /* Validate parameters. */
  1908. if ((fp == XBADFILE) || (rsa == NULL) || (rsa->internal == NULL)) {
  1909. WOLFSSL_ERROR_MSG("Bad function arguments");
  1910. ret = 0;
  1911. }
  1912. if (ret == 1) {
  1913. /* Write PEM to buffer that is allocated in the call. */
  1914. ret = wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, cipher, passwd, passwdSz,
  1915. &pem, &pLen);
  1916. if (ret != 1) {
  1917. WOLFSSL_ERROR_MSG("wolfSSL_PEM_write_mem_RSAPrivateKey failed");
  1918. }
  1919. }
  1920. /* Write PEM to file pointer. */
  1921. if ((ret == 1) && ((int)XFWRITE(pem, 1, (size_t)pLen, fp) != pLen)) {
  1922. WOLFSSL_ERROR_MSG("RSA private key file write failed");
  1923. ret = 0;
  1924. }
  1925. /* Dispose of any allocated PEM buffer. */
  1926. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  1927. return ret;
  1928. }
  1929. #endif /* NO_FILESYSTEM */
  1930. #endif /* WOLFSSL_KEY_GEN && !HAVE_USER_RSA && WOLFSSL_PEM_TO_DER */
  1931. #ifndef NO_BIO
  1932. /* Create an RSA private key by reading the PEM encoded data from the BIO.
  1933. *
  1934. * @param [in] bio BIO object to read from.
  1935. * @param [out] out RSA key created.
  1936. * @param [in] cb Password callback when PEM encrypted.
  1937. * @param [in] pass NUL terminated string for passphrase when PEM encrypted.
  1938. * @return RSA key on success.
  1939. * @return NULL on failure.
  1940. */
  1941. WOLFSSL_RSA* wolfSSL_PEM_read_bio_RSAPrivateKey(WOLFSSL_BIO* bio,
  1942. WOLFSSL_RSA** out, wc_pem_password_cb* cb, void* pass)
  1943. {
  1944. WOLFSSL_RSA* rsa = NULL;
  1945. DerBuffer* der = NULL;
  1946. int keyFormat = 0;
  1947. WOLFSSL_ENTER("wolfSSL_PEM_read_bio_RSAPrivateKey");
  1948. if ((bio != NULL) && (pem_read_bio_key(bio, cb, pass, PRIVATEKEY_TYPE,
  1949. &keyFormat, &der) >= 0)) {
  1950. rsa = wolfssl_rsa_d2i(out, der->buffer, der->length,
  1951. WOLFSSL_RSA_LOAD_PRIVATE);
  1952. if (rsa == NULL) {
  1953. WOLFSSL_ERROR_MSG("Error loading DER buffer into WOLFSSL_RSA");
  1954. }
  1955. }
  1956. FreeDer(&der);
  1957. if ((out != NULL) && (rsa != NULL)) {
  1958. *out = rsa;
  1959. }
  1960. return rsa;
  1961. }
  1962. #endif /* !NO_BIO */
  1963. /* Create an RSA private key by reading the PEM encoded data from the file
  1964. * pointer.
  1965. *
  1966. * @param [in] fp File pointer to read from.
  1967. * @param [out] out RSA key created.
  1968. * @param [in] cb Password callback when PEM encrypted.
  1969. * @param [in] pass NUL terminated string for passphrase when PEM encrypted.
  1970. * @return RSA key on success.
  1971. * @return NULL on failure.
  1972. */
  1973. #ifndef NO_FILESYSTEM
  1974. WOLFSSL_RSA* wolfSSL_PEM_read_RSAPrivateKey(XFILE fp, WOLFSSL_RSA** out,
  1975. wc_pem_password_cb* cb, void* pass)
  1976. {
  1977. WOLFSSL_RSA* rsa = NULL;
  1978. DerBuffer* der = NULL;
  1979. int keyFormat = 0;
  1980. WOLFSSL_ENTER("wolfSSL_PEM_read_RSAPrivateKey");
  1981. if ((fp != XBADFILE) && (pem_read_file_key(fp, cb, pass, PRIVATEKEY_TYPE,
  1982. &keyFormat, &der) >= 0)) {
  1983. rsa = wolfssl_rsa_d2i(out, der->buffer, der->length,
  1984. WOLFSSL_RSA_LOAD_PRIVATE);
  1985. if (rsa == NULL) {
  1986. WOLFSSL_ERROR_MSG("Error loading DER buffer into WOLFSSL_RSA");
  1987. }
  1988. }
  1989. FreeDer(&der);
  1990. if ((out != NULL) && (rsa != NULL)) {
  1991. *out = rsa;
  1992. }
  1993. return rsa;
  1994. }
  1995. #endif /* !NO_FILESYSTEM */
  1996. /*
  1997. * RSA print APIs
  1998. */
  1999. #if defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  2000. !defined(NO_STDIO_FILESYSTEM)
  2001. /* Print an RSA key to a file pointer.
  2002. *
  2003. * @param [in] fp File pointer to write to.
  2004. * @param [in] rsa RSA key to write.
  2005. * @param [in] indent Number of spaces to prepend to each line.
  2006. * @return 1 on success.
  2007. * @return 0 on failure.
  2008. */
  2009. int wolfSSL_RSA_print_fp(XFILE fp, WOLFSSL_RSA* rsa, int indent)
  2010. {
  2011. int ret = 1;
  2012. WOLFSSL_ENTER("wolfSSL_RSA_print_fp");
  2013. /* Validate parameters. */
  2014. if ((fp == XBADFILE) || (rsa == NULL)) {
  2015. ret = 0;
  2016. }
  2017. /* Set the external data from the wolfCrypt RSA key if not done. */
  2018. if ((ret == 1) && (!rsa->exSet)) {
  2019. ret = SetRsaExternal(rsa);
  2020. }
  2021. /* Get the key size from modulus if available. */
  2022. if ((ret == 1) && (rsa->n != NULL)) {
  2023. int keySize = wolfSSL_BN_num_bits(rsa->n);
  2024. if (keySize == 0) {
  2025. ret = 0;
  2026. }
  2027. else {
  2028. if (XFPRINTF(fp, "%*s", indent, "") < 0)
  2029. ret = 0;
  2030. else if (XFPRINTF(fp, "RSA Private-Key: (%d bit, 2 primes)\n",
  2031. keySize) < 0)
  2032. ret = 0;
  2033. }
  2034. }
  2035. /* Print out any components available. */
  2036. if ((ret == 1) && (rsa->n != NULL)) {
  2037. ret = pk_bn_field_print_fp(fp, indent, "modulus", rsa->n);
  2038. }
  2039. if ((ret == 1) && (rsa->d != NULL)) {
  2040. ret = pk_bn_field_print_fp(fp, indent, "privateExponent", rsa->d);
  2041. }
  2042. if ((ret == 1) && (rsa->p != NULL)) {
  2043. ret = pk_bn_field_print_fp(fp, indent, "prime1", rsa->p);
  2044. }
  2045. if ((ret == 1) && (rsa->q != NULL)) {
  2046. ret = pk_bn_field_print_fp(fp, indent, "prime2", rsa->q);
  2047. }
  2048. if ((ret == 1) && (rsa->dmp1 != NULL)) {
  2049. ret = pk_bn_field_print_fp(fp, indent, "exponent1", rsa->dmp1);
  2050. }
  2051. if ((ret == 1) && (rsa->dmq1 != NULL)) {
  2052. ret = pk_bn_field_print_fp(fp, indent, "exponent2", rsa->dmq1);
  2053. }
  2054. if ((ret == 1) && (rsa->iqmp != NULL)) {
  2055. ret = pk_bn_field_print_fp(fp, indent, "coefficient", rsa->iqmp);
  2056. }
  2057. WOLFSSL_LEAVE("wolfSSL_RSA_print_fp", ret);
  2058. return ret;
  2059. }
  2060. #endif /* XFPRINTF && !NO_FILESYSTEM && !NO_STDIO_FILESYSTEM */
  2061. #if defined(XSNPRINTF) && !defined(NO_BIO) && !defined(HAVE_FAST_RSA)
  2062. /* snprintf() must be available */
  2063. /* Maximum size of a header line. */
  2064. #define RSA_PRINT_MAX_HEADER_LINE PRINT_NUM_MAX_INDENT
  2065. /* Writes the human readable form of RSA to a BIO.
  2066. *
  2067. * @param [in] bio BIO object to write to.
  2068. * @param [in] rsa RSA key to write.
  2069. * @param [in] indent Number of spaces before each line.
  2070. * @return 1 on success.
  2071. * @return 0 on failure.
  2072. */
  2073. int wolfSSL_RSA_print(WOLFSSL_BIO* bio, WOLFSSL_RSA* rsa, int indent)
  2074. {
  2075. int ret = 1;
  2076. int sz = 0;
  2077. RsaKey* key = NULL;
  2078. char line[RSA_PRINT_MAX_HEADER_LINE];
  2079. int i = 0;
  2080. mp_int *num = NULL;
  2081. /* Header strings. */
  2082. const char *name[] = {
  2083. "Modulus:", "Exponent:", "PrivateExponent:", "Prime1:", "Prime2:",
  2084. "Exponent1:", "Exponent2:", "Coefficient:"
  2085. };
  2086. WOLFSSL_ENTER("wolfSSL_RSA_print");
  2087. /* Validate parameters. */
  2088. if ((bio == NULL) || (rsa == NULL) || (indent > PRINT_NUM_MAX_INDENT)) {
  2089. ret = -1;
  2090. }
  2091. if (ret == 1) {
  2092. key = (RsaKey*)rsa->internal;
  2093. /* Get size in bits of key for printing out. */
  2094. sz = wolfSSL_RSA_bits(rsa);
  2095. if (sz <= 0) {
  2096. WOLFSSL_ERROR_MSG("Error getting RSA key size");
  2097. ret = 0;
  2098. }
  2099. }
  2100. if (ret == 1) {
  2101. /* Print any indent spaces. */
  2102. ret = wolfssl_print_indent(bio, line, sizeof(line), indent);
  2103. }
  2104. if (ret == 1) {
  2105. /* Print header line. */
  2106. int len = XSNPRINTF(line, sizeof(line), "\nRSA %s: (%d bit)\n",
  2107. (!mp_iszero(&key->d)) ? "Private-Key" : "Public-Key", sz);
  2108. if (len >= (int)sizeof(line)) {
  2109. WOLFSSL_ERROR_MSG("Buffer overflow while formatting key preamble");
  2110. ret = 0;
  2111. }
  2112. else {
  2113. if (wolfSSL_BIO_write(bio, line, len) <= 0) {
  2114. ret = 0;
  2115. }
  2116. }
  2117. }
  2118. for (i = 0; (ret == 1) && (i < RSA_INTS); i++) {
  2119. /* Get mp_int for index. */
  2120. switch (i) {
  2121. case 0:
  2122. /* Print out modulus */
  2123. num = &key->n;
  2124. break;
  2125. case 1:
  2126. num = &key->e;
  2127. break;
  2128. case 2:
  2129. num = &key->d;
  2130. break;
  2131. case 3:
  2132. num = &key->p;
  2133. break;
  2134. case 4:
  2135. num = &key->q;
  2136. break;
  2137. case 5:
  2138. num = &key->dP;
  2139. break;
  2140. case 6:
  2141. num = &key->dQ;
  2142. break;
  2143. case 7:
  2144. num = &key->u;
  2145. break;
  2146. default:
  2147. WOLFSSL_ERROR_MSG("Bad index value");
  2148. }
  2149. if (i == 1) {
  2150. /* Print exponent as a 32-bit value. */
  2151. ret = wolfssl_print_value(bio, num, name[i], indent);
  2152. }
  2153. else if (!mp_iszero(num)) {
  2154. /* Print name and MP integer. */
  2155. ret = wolfssl_print_number(bio, num, name[i], indent);
  2156. }
  2157. }
  2158. return ret;
  2159. }
  2160. #endif /* XSNPRINTF && !NO_BIO && !HAVE_FAST_RSA */
  2161. #endif /* OPENSSL_EXTRA */
  2162. /*
  2163. * RSA get/set/test APIs
  2164. */
  2165. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  2166. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  2167. /* Set RSA key data (external) from wolfCrypt RSA key (internal).
  2168. *
  2169. * @param [in, out] rsa RSA key.
  2170. * @return 1 on success.
  2171. * @return 0 on failure.
  2172. */
  2173. int SetRsaExternal(WOLFSSL_RSA* rsa)
  2174. {
  2175. int ret = 1;
  2176. WOLFSSL_ENTER("SetRsaExternal");
  2177. /* Validate parameters. */
  2178. if ((rsa == NULL) || (rsa->internal == NULL)) {
  2179. WOLFSSL_ERROR_MSG("rsa key NULL error");
  2180. ret = -1;
  2181. }
  2182. if (ret == 1) {
  2183. RsaKey* key = (RsaKey*)rsa->internal;
  2184. /* Copy modulus. */
  2185. ret = wolfssl_bn_set_value(&rsa->n, &key->n);
  2186. if (ret != 1) {
  2187. WOLFSSL_ERROR_MSG("rsa n error");
  2188. }
  2189. if (ret == 1) {
  2190. /* Copy public exponent. */
  2191. ret = wolfssl_bn_set_value(&rsa->e, &key->e);
  2192. if (ret != 1) {
  2193. WOLFSSL_ERROR_MSG("rsa e error");
  2194. }
  2195. }
  2196. if (key->type == RSA_PRIVATE) {
  2197. if (ret == 1) {
  2198. /* Copy private exponent. */
  2199. ret = wolfssl_bn_set_value(&rsa->d, &key->d);
  2200. if (ret != 1) {
  2201. WOLFSSL_ERROR_MSG("rsa d error");
  2202. }
  2203. }
  2204. if (ret == 1) {
  2205. /* Copy first prime. */
  2206. ret = wolfssl_bn_set_value(&rsa->p, &key->p);
  2207. if (ret != 1) {
  2208. WOLFSSL_ERROR_MSG("rsa p error");
  2209. }
  2210. }
  2211. if (ret == 1) {
  2212. /* Copy second prime. */
  2213. ret = wolfssl_bn_set_value(&rsa->q, &key->q);
  2214. if (ret != 1) {
  2215. WOLFSSL_ERROR_MSG("rsa q error");
  2216. }
  2217. }
  2218. #ifndef RSA_LOW_MEM
  2219. if (ret == 1) {
  2220. /* Copy d mod p-1. */
  2221. ret = wolfssl_bn_set_value(&rsa->dmp1, &key->dP);
  2222. if (ret != 1) {
  2223. WOLFSSL_ERROR_MSG("rsa dP error");
  2224. }
  2225. }
  2226. if (ret == 1) {
  2227. /* Copy d mod q-1. */
  2228. ret = wolfssl_bn_set_value(&rsa->dmq1, &key->dQ);
  2229. if (ret != 1) {
  2230. WOLFSSL_ERROR_MSG("rsa dq error");
  2231. }
  2232. }
  2233. if (ret == 1) {
  2234. /* Copy 1/q mod p. */
  2235. ret = wolfssl_bn_set_value(&rsa->iqmp, &key->u);
  2236. if (ret != 1) {
  2237. WOLFSSL_ERROR_MSG("rsa u error");
  2238. }
  2239. }
  2240. #endif /* !RSA_LOW_MEM */
  2241. }
  2242. }
  2243. if (ret == 1) {
  2244. /* External values set. */
  2245. rsa->exSet = 1;
  2246. }
  2247. else {
  2248. /* Return 0 on failure. */
  2249. ret = 0;
  2250. }
  2251. return ret;
  2252. }
  2253. #endif /* !HAVE_USER_RSA && !HAVE_FAST_RSA */
  2254. #endif /* (OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL) */
  2255. #ifdef OPENSSL_EXTRA
  2256. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  2257. /* Set wolfCrypt RSA key data (internal) from RSA key (external).
  2258. *
  2259. * @param [in, out] rsa RSA key.
  2260. * @return 1 on success.
  2261. * @return 0 on failure.
  2262. */
  2263. int SetRsaInternal(WOLFSSL_RSA* rsa)
  2264. {
  2265. int ret = 1;
  2266. WOLFSSL_ENTER("SetRsaInternal");
  2267. /* Validate parameters. */
  2268. if ((rsa == NULL) || (rsa->internal == NULL)) {
  2269. WOLFSSL_ERROR_MSG("rsa key NULL error");
  2270. ret = -1;
  2271. }
  2272. if (ret == 1) {
  2273. RsaKey* key = (RsaKey*)rsa->internal;
  2274. /* Copy down modulus if available. */
  2275. if ((rsa->n != NULL) && (wolfssl_bn_get_value(rsa->n, &key->n) != 1)) {
  2276. WOLFSSL_ERROR_MSG("rsa n key error");
  2277. ret = -1;
  2278. }
  2279. /* Copy down public exponent if available. */
  2280. if ((ret == 1) && (rsa->e != NULL) &&
  2281. (wolfssl_bn_get_value(rsa->e, &key->e) != 1)) {
  2282. WOLFSSL_ERROR_MSG("rsa e key error");
  2283. ret = -1;
  2284. }
  2285. /* Enough numbers for public key */
  2286. key->type = RSA_PUBLIC;
  2287. /* Copy down private exponent if available. */
  2288. if ((ret == 1) && (rsa->d != NULL)) {
  2289. if (wolfssl_bn_get_value(rsa->d, &key->d) != 1) {
  2290. WOLFSSL_ERROR_MSG("rsa d key error");
  2291. ret = -1;
  2292. }
  2293. else {
  2294. /* Enough numbers for private key */
  2295. key->type = RSA_PRIVATE;
  2296. }
  2297. }
  2298. /* Copy down first prime if available. */
  2299. if ((ret == 1) && (rsa->p != NULL) &&
  2300. (wolfssl_bn_get_value(rsa->p, &key->p) != 1)) {
  2301. WOLFSSL_ERROR_MSG("rsa p key error");
  2302. ret = -1;
  2303. }
  2304. /* Copy down second prime if available. */
  2305. if ((ret == 1) && (rsa->q != NULL) &&
  2306. (wolfssl_bn_get_value(rsa->q, &key->q) != 1)) {
  2307. WOLFSSL_ERROR_MSG("rsa q key error");
  2308. ret = -1;
  2309. }
  2310. #ifndef RSA_LOW_MEM
  2311. /* Copy down d mod p-1 if available. */
  2312. if ((ret == 1) && (rsa->dmp1 != NULL) &&
  2313. (wolfssl_bn_get_value(rsa->dmp1, &key->dP) != 1)) {
  2314. WOLFSSL_ERROR_MSG("rsa dP key error");
  2315. ret = -1;
  2316. }
  2317. /* Copy down d mod q-1 if available. */
  2318. if ((ret == 1) && (rsa->dmp1 != NULL) &&
  2319. (wolfssl_bn_get_value(rsa->dmq1, &key->dQ) != 1)) {
  2320. WOLFSSL_ERROR_MSG("rsa dQ key error");
  2321. ret = -1;
  2322. }
  2323. /* Copy down 1/q mod p if available. */
  2324. if ((ret == 1) && (rsa->iqmp != NULL) &&
  2325. (wolfssl_bn_get_value(rsa->iqmp, &key->u) != 1)) {
  2326. WOLFSSL_ERROR_MSG("rsa u key error");
  2327. ret = -1;
  2328. }
  2329. #endif /* !RSA_LOW_MEM */
  2330. if (ret == 1) {
  2331. /* All available numbers have been set down. */
  2332. rsa->inSet = 1;
  2333. }
  2334. }
  2335. return ret;
  2336. }
  2337. #endif /* HAVE_USER_RSA */
  2338. /* Set the RSA method into object.
  2339. *
  2340. * @param [in, out] rsa RSA key.
  2341. * @param [in] meth RSA method.
  2342. * @return 1 always.
  2343. */
  2344. int wolfSSL_RSA_set_method(WOLFSSL_RSA *rsa, WOLFSSL_RSA_METHOD *meth)
  2345. {
  2346. if (rsa != NULL) {
  2347. /* Store the method into object. */
  2348. rsa->meth = meth;
  2349. /* Copy over flags. */
  2350. rsa->flags = meth->flags;
  2351. }
  2352. /* OpenSSL always assumes it will work. */
  2353. return 1;
  2354. }
  2355. /* Get the RSA method from the RSA object.
  2356. *
  2357. * @param [in] rsa RSA key.
  2358. * @return RSA method on success.
  2359. * @return NULL when RSA is NULL or no method set.
  2360. */
  2361. const WOLFSSL_RSA_METHOD* wolfSSL_RSA_get_method(const WOLFSSL_RSA *rsa)
  2362. {
  2363. return (rsa != NULL) ? rsa->meth : NULL;
  2364. }
  2365. /* Get the size in bytes of the RSA key.
  2366. *
  2367. * Return compliant with OpenSSL
  2368. *
  2369. * @param [in] rsa RSA key.
  2370. * @return RSA modulus size in bytes.
  2371. * @return 0 on error.
  2372. */
  2373. int wolfSSL_RSA_size(const WOLFSSL_RSA* rsa)
  2374. {
  2375. int ret = 0;
  2376. WOLFSSL_ENTER("wolfSSL_RSA_size");
  2377. if (rsa != NULL) {
  2378. /* Make sure we have set the RSA values into wolfCrypt RSA key. */
  2379. if (rsa->inSet || (SetRsaInternal((WOLFSSL_RSA*)rsa) == 1)) {
  2380. /* Get key size in bytes using wolfCrypt RSA key. */
  2381. ret = wc_RsaEncryptSize((RsaKey*)rsa->internal);
  2382. }
  2383. }
  2384. return ret;
  2385. }
  2386. /* Get the size in bits of the RSA key.
  2387. *
  2388. * Uses external modulus field.
  2389. *
  2390. * @param [in] rsa RSA key.
  2391. * @return RSA modulus size in bits.
  2392. * @return 0 on error.
  2393. */
  2394. int wolfSSL_RSA_bits(const WOLFSSL_RSA* rsa)
  2395. {
  2396. int ret = 0;
  2397. WOLFSSL_ENTER("wolfSSL_RSA_bits");
  2398. if (rsa != NULL) {
  2399. /* Get number of bits in external modulus. */
  2400. ret = wolfSSL_BN_num_bits(rsa->n);
  2401. }
  2402. return ret;
  2403. }
  2404. #ifndef HAVE_USER_RSA
  2405. /* Get the BN objects that are the Chinese-Remainder Theorem (CRT) parameters.
  2406. *
  2407. * Only for those that are not NULL parameters.
  2408. *
  2409. * @param [in] rsa RSA key.
  2410. * @param [out] dmp1 BN that is d mod (p - 1). May be NULL.
  2411. * @param [out] dmq1 BN that is d mod (q - 1). May be NULL.
  2412. * @param [out] iqmp BN that is 1/q mod p. May be NULL.
  2413. */
  2414. void wolfSSL_RSA_get0_crt_params(const WOLFSSL_RSA *rsa,
  2415. const WOLFSSL_BIGNUM **dmp1, const WOLFSSL_BIGNUM **dmq1,
  2416. const WOLFSSL_BIGNUM **iqmp)
  2417. {
  2418. WOLFSSL_ENTER("wolfSSL_RSA_get0_crt_params");
  2419. /* For any parameters not NULL, return the BN from the key or NULL. */
  2420. if (dmp1 != NULL) {
  2421. *dmp1 = (rsa != NULL) ? rsa->dmp1 : NULL;
  2422. }
  2423. if (dmq1 != NULL) {
  2424. *dmq1 = (rsa != NULL) ? rsa->dmq1 : NULL;
  2425. }
  2426. if (iqmp != NULL) {
  2427. *iqmp = (rsa != NULL) ? rsa->iqmp : NULL;
  2428. }
  2429. }
  2430. /* Set the BN objects that are the Chinese-Remainder Theorem (CRT) parameters
  2431. * into RSA key.
  2432. *
  2433. * If CRT parameter is NULL then there must be one in the RSA key already.
  2434. *
  2435. * @param [in, out] rsa RSA key.
  2436. * @param [in] dmp1 BN that is d mod (p - 1). May be NULL.
  2437. * @param [in] dmq1 BN that is d mod (q - 1). May be NULL.
  2438. * @param [in] iqmp BN that is 1/q mod p. May be NULL.
  2439. * @return 1 on success.
  2440. * @return 0 on failure.
  2441. */
  2442. int wolfSSL_RSA_set0_crt_params(WOLFSSL_RSA *rsa, WOLFSSL_BIGNUM *dmp1,
  2443. WOLFSSL_BIGNUM *dmq1, WOLFSSL_BIGNUM *iqmp)
  2444. {
  2445. int ret = 1;
  2446. WOLFSSL_ENTER("wolfSSL_RSA_set0_crt_params");
  2447. /* If a param is NULL in rsa then it must be non-NULL in the
  2448. * corresponding user input. */
  2449. if ((rsa == NULL) || ((rsa->dmp1 == NULL) && (dmp1 == NULL)) ||
  2450. ((rsa->dmq1 == NULL) && (dmq1 == NULL)) ||
  2451. ((rsa->iqmp == NULL) && (iqmp == NULL))) {
  2452. WOLFSSL_ERROR_MSG("Bad parameters");
  2453. ret = 0;
  2454. }
  2455. if (ret == 1) {
  2456. /* Replace the BNs. */
  2457. if (dmp1 != NULL) {
  2458. wolfSSL_BN_clear_free(rsa->dmp1);
  2459. rsa->dmp1 = dmp1;
  2460. }
  2461. if (dmq1 != NULL) {
  2462. wolfSSL_BN_clear_free(rsa->dmq1);
  2463. rsa->dmq1 = dmq1;
  2464. }
  2465. if (iqmp != NULL) {
  2466. wolfSSL_BN_clear_free(rsa->iqmp);
  2467. rsa->iqmp = iqmp;
  2468. }
  2469. /* Set the values into the wolfCrypt RSA key. */
  2470. if (SetRsaInternal(rsa) != 1) {
  2471. if (dmp1 != NULL) {
  2472. rsa->dmp1 = NULL;
  2473. }
  2474. if (dmq1 != NULL) {
  2475. rsa->dmq1 = NULL;
  2476. }
  2477. if (iqmp != NULL) {
  2478. rsa->iqmp = NULL;
  2479. }
  2480. ret = 0;
  2481. }
  2482. }
  2483. return ret;
  2484. }
  2485. /* Get the BN objects that are the factors of the RSA key (two primes p and q).
  2486. *
  2487. * @param [in] rsa RSA key.
  2488. * @param [out] p BN that is first prime. May be NULL.
  2489. * @param [out] q BN that is second prime. May be NULL.
  2490. */
  2491. void wolfSSL_RSA_get0_factors(const WOLFSSL_RSA *rsa, const WOLFSSL_BIGNUM **p,
  2492. const WOLFSSL_BIGNUM **q)
  2493. {
  2494. WOLFSSL_ENTER("wolfSSL_RSA_get0_factors");
  2495. /* For any primes not NULL, return the BN from the key or NULL. */
  2496. if (p != NULL) {
  2497. *p = (rsa != NULL) ? rsa->p : NULL;
  2498. }
  2499. if (q != NULL) {
  2500. *q = (rsa != NULL) ? rsa->q : NULL;
  2501. }
  2502. }
  2503. /* Set the BN objects that are the factors of the RSA key (two primes p and q).
  2504. *
  2505. * If factor parameter is NULL then there must be one in the RSA key already.
  2506. *
  2507. * @param [in, out] rsa RSA key.
  2508. * @param [in] p BN that is first prime. May be NULL.
  2509. * @param [in] q BN that is second prime. May be NULL.
  2510. * @return 1 on success.
  2511. * @return 0 on failure.
  2512. */
  2513. int wolfSSL_RSA_set0_factors(WOLFSSL_RSA *rsa, WOLFSSL_BIGNUM *p,
  2514. WOLFSSL_BIGNUM *q)
  2515. {
  2516. int ret = 1;
  2517. WOLFSSL_ENTER("wolfSSL_RSA_set0_factors");
  2518. /* If a param is null in r then it must be non-null in the
  2519. * corresponding user input. */
  2520. if (rsa == NULL || ((rsa->p == NULL) && (p == NULL)) ||
  2521. ((rsa->q == NULL) && (q == NULL))) {
  2522. WOLFSSL_ERROR_MSG("Bad parameters");
  2523. ret = 0;
  2524. }
  2525. if (ret == 1) {
  2526. /* Replace the BNs. */
  2527. if (p != NULL) {
  2528. wolfSSL_BN_clear_free(rsa->p);
  2529. rsa->p = p;
  2530. }
  2531. if (q != NULL) {
  2532. wolfSSL_BN_clear_free(rsa->q);
  2533. rsa->q = q;
  2534. }
  2535. /* Set the values into the wolfCrypt RSA key. */
  2536. if (SetRsaInternal(rsa) != 1) {
  2537. if (p != NULL) {
  2538. rsa->p = NULL;
  2539. }
  2540. if (q != NULL) {
  2541. rsa->q = NULL;
  2542. }
  2543. ret = 0;
  2544. }
  2545. }
  2546. return ret;
  2547. }
  2548. /* Get the BN objects for the basic key numbers of the RSA key (modulus, public
  2549. * exponent, private exponent).
  2550. *
  2551. * @param [in] rsa RSA key.
  2552. * @param [out] n BN that is the modulus. May be NULL.
  2553. * @param [out] e BN that is the public exponent. May be NULL.
  2554. * @param [out] d BN that is the private exponent. May be NULL.
  2555. */
  2556. void wolfSSL_RSA_get0_key(const WOLFSSL_RSA *rsa, const WOLFSSL_BIGNUM **n,
  2557. const WOLFSSL_BIGNUM **e, const WOLFSSL_BIGNUM **d)
  2558. {
  2559. WOLFSSL_ENTER("wolfSSL_RSA_get0_key");
  2560. /* For any parameters not NULL, return the BN from the key or NULL. */
  2561. if (n != NULL) {
  2562. *n = (rsa != NULL) ? rsa->n : NULL;
  2563. }
  2564. if (e != NULL) {
  2565. *e = (rsa != NULL) ? rsa->e : NULL;
  2566. }
  2567. if (d != NULL) {
  2568. *d = (rsa != NULL) ? rsa->d : NULL;
  2569. }
  2570. }
  2571. /* Set the BN objects for the basic key numbers into the RSA key (modulus,
  2572. * public exponent, private exponent).
  2573. *
  2574. * If BN parameter is NULL then there must be one in the RSA key already.
  2575. *
  2576. * @param [in,out] rsa RSA key.
  2577. * @param [in] n BN that is the modulus. May be NULL.
  2578. * @param [in] e BN that is the public exponent. May be NULL.
  2579. * @param [in] d BN that is the private exponent. May be NULL.
  2580. * @return 1 on success.
  2581. * @return 0 on failure.
  2582. */
  2583. int wolfSSL_RSA_set0_key(WOLFSSL_RSA *rsa, WOLFSSL_BIGNUM *n, WOLFSSL_BIGNUM *e,
  2584. WOLFSSL_BIGNUM *d)
  2585. {
  2586. int ret = 1;
  2587. /* If the fields n and e in r are NULL, the corresponding input
  2588. * parameters MUST be non-NULL for n and e. d may be
  2589. * left NULL (in case only the public key is used).
  2590. */
  2591. if ((rsa == NULL) || ((rsa->n == NULL) && (n == NULL)) ||
  2592. ((rsa->e == NULL) && (e == NULL))) {
  2593. ret = 0;
  2594. }
  2595. if (ret == 1) {
  2596. /* Replace the BNs. */
  2597. if (n != NULL) {
  2598. wolfSSL_BN_free(rsa->n);
  2599. rsa->n = n;
  2600. }
  2601. if (e != NULL) {
  2602. wolfSSL_BN_free(rsa->e);
  2603. rsa->e = e;
  2604. }
  2605. if (d != NULL) {
  2606. /* Private key is sensitive data. */
  2607. wolfSSL_BN_clear_free(rsa->d);
  2608. rsa->d = d;
  2609. }
  2610. /* Set the values into the wolfCrypt RSA key. */
  2611. if (SetRsaInternal(rsa) != 1) {
  2612. if (n != NULL) {
  2613. rsa->n = NULL;
  2614. }
  2615. if (e != NULL) {
  2616. rsa->e = NULL;
  2617. }
  2618. if (d != NULL) {
  2619. rsa->d = NULL;
  2620. }
  2621. ret = 0;
  2622. }
  2623. }
  2624. return ret;
  2625. }
  2626. #endif /* !HAVE_USER_RSA */
  2627. /* Get the flags of the RSA key.
  2628. *
  2629. * @param [in] rsa RSA key.
  2630. * @return Flags set in RSA key on success.
  2631. * @return 0 when RSA key is NULL.
  2632. */
  2633. int wolfSSL_RSA_flags(const WOLFSSL_RSA *rsa)
  2634. {
  2635. int ret = 0;
  2636. /* Get flags from the RSA key if available. */
  2637. if (rsa != NULL) {
  2638. ret = rsa->flags;
  2639. }
  2640. return ret;
  2641. }
  2642. /* Set the flags into the RSA key.
  2643. *
  2644. * @param [in, out] rsa RSA key.
  2645. * @param [in] flags Flags to set.
  2646. */
  2647. void wolfSSL_RSA_set_flags(WOLFSSL_RSA *rsa, int flags)
  2648. {
  2649. /* Add the flags into RSA key if available. */
  2650. if (rsa != NULL) {
  2651. rsa->flags |= flags;
  2652. }
  2653. }
  2654. /* Clear the flags in the RSA key.
  2655. *
  2656. * @param [in, out] rsa RSA key.
  2657. * @param [in] flags Flags to clear.
  2658. */
  2659. void wolfSSL_RSA_clear_flags(WOLFSSL_RSA *rsa, int flags)
  2660. {
  2661. /* Clear the flags passed in that are on the RSA key if available. */
  2662. if (rsa != NULL) {
  2663. rsa->flags &= ~flags;
  2664. }
  2665. }
  2666. /* Test the flags in the RSA key.
  2667. *
  2668. * @param [in] rsa RSA key.
  2669. * @return Matching flags of RSA key on success.
  2670. * @return 0 when RSA key is NULL.
  2671. */
  2672. int wolfSSL_RSA_test_flags(const WOLFSSL_RSA *rsa, int flags)
  2673. {
  2674. /* Return the flags passed in that are set on the RSA key if available. */
  2675. return (rsa != NULL) ? (rsa->flags & flags) : 0;
  2676. }
  2677. /* Get the extra data, by index, associated with the RSA key.
  2678. *
  2679. * @param [in] rsa RSA key.
  2680. * @param [in] idx Index of extra data.
  2681. * @return Extra data (anonymous type) on success.
  2682. * @return NULL on failure.
  2683. */
  2684. void* wolfSSL_RSA_get_ex_data(const WOLFSSL_RSA *rsa, int idx)
  2685. {
  2686. WOLFSSL_ENTER("wolfSSL_RSA_get_ex_data");
  2687. #ifdef HAVE_EX_DATA
  2688. return (rsa == NULL) ? NULL :
  2689. wolfSSL_CRYPTO_get_ex_data(&rsa->ex_data, idx);
  2690. #else
  2691. (void)rsa;
  2692. (void)idx;
  2693. return NULL;
  2694. #endif
  2695. }
  2696. /* Set extra data against the RSA key at an index.
  2697. *
  2698. * @param [in, out] rsa RSA key.
  2699. * @param [in] idx Index set set extra data at.
  2700. * @param [in] data Extra data of anonymous type.
  2701. * @return 1 on success.
  2702. * @return 0 on failure.
  2703. */
  2704. int wolfSSL_RSA_set_ex_data(WOLFSSL_RSA *rsa, int idx, void *data)
  2705. {
  2706. WOLFSSL_ENTER("wolfSSL_RSA_set_ex_data");
  2707. #ifdef HAVE_EX_DATA
  2708. return (rsa == NULL) ? 0 :
  2709. wolfSSL_CRYPTO_set_ex_data(&rsa->ex_data, idx, data);
  2710. #else
  2711. (void)rsa;
  2712. (void)idx;
  2713. (void)data;
  2714. return 0;
  2715. #endif
  2716. }
  2717. #ifdef HAVE_EX_DATA_CLEANUP_HOOKS
  2718. /* Set the extra data and cleanup callback against the RSA key at an index.
  2719. *
  2720. * Not OpenSSL API.
  2721. *
  2722. * @param [in, out] rsa RSA key.
  2723. * @param [in] idx Index set set extra data at.
  2724. * @param [in] data Extra data of anonymous type.
  2725. * @param [in] freeCb Callback function to free extra data.
  2726. * @return 1 on success.
  2727. * @return 0 on failure.
  2728. */
  2729. int wolfSSL_RSA_set_ex_data_with_cleanup(WOLFSSL_RSA *rsa, int idx, void *data,
  2730. wolfSSL_ex_data_cleanup_routine_t freeCb)
  2731. {
  2732. WOLFSSL_ENTER("wolfSSL_RSA_set_ex_data_with_cleanup");
  2733. return (rsa == NULL) ? 0 :
  2734. wolfSSL_CRYPTO_set_ex_data_with_cleanup(&rsa->ex_data, idx, data,
  2735. freeCb);
  2736. }
  2737. #endif /* HAVE_EX_DATA_CLEANUP_HOOKS */
  2738. /*
  2739. * RSA check key APIs
  2740. */
  2741. #ifdef WOLFSSL_RSA_KEY_CHECK
  2742. /* Check that the RSA key is valid using wolfCrypt.
  2743. *
  2744. * @param [in] rsa RSA key.
  2745. * @return 1 on success.
  2746. * @return 0 on failure.
  2747. */
  2748. int wolfSSL_RSA_check_key(const WOLFSSL_RSA* rsa)
  2749. {
  2750. int ret = 1;
  2751. WOLFSSL_ENTER("wolfSSL_RSA_check_key");
  2752. /* Validate parameters. */
  2753. if ((rsa == NULL) || (rsa->internal == NULL)) {
  2754. ret = 0;
  2755. }
  2756. /* Constant RSA - assume internal data has been set. */
  2757. /* Check wolfCrypt RSA key. */
  2758. if ((ret == 1) && (wc_CheckRsaKey((RsaKey*)rsa->internal) != 0)) {
  2759. ret = 0;
  2760. }
  2761. WOLFSSL_LEAVE("wolfSSL_RSA_check_key", ret);
  2762. return ret;
  2763. }
  2764. #endif /* WOLFSSL_RSA_KEY_CHECK */
  2765. /*
  2766. * RSA generate APIs
  2767. */
  2768. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  2769. /* Get a random number generator associated with the RSA key.
  2770. *
  2771. * If not able, then get the global if possible.
  2772. * *tmpRng must not be an initialized RNG.
  2773. * *tmpRng is allocated when WOLFSSL_SMALL_STACK is defined and an RNG isn't
  2774. * associated with the wolfCrypt RSA key.
  2775. *
  2776. * @param [in] rsa RSA key.
  2777. * @param [out] tmpRng Temporary random number generator.
  2778. * @param [out] initTmpRng Temporary random number generator was initialized.
  2779. *
  2780. * @return A wolfCrypt RNG to use on success.
  2781. * @return NULL on error.
  2782. */
  2783. WC_RNG* WOLFSSL_RSA_GetRNG(WOLFSSL_RSA* rsa, WC_RNG** tmpRng, int* initTmpRng)
  2784. {
  2785. WC_RNG* rng = NULL;
  2786. int err = 0;
  2787. /* Check validity of parameters. */
  2788. if ((rsa == NULL) || (initTmpRng == NULL)) {
  2789. err = 1;
  2790. }
  2791. if (!err) {
  2792. /* Haven't initialized any RNG passed through tmpRng. */
  2793. *initTmpRng = 0;
  2794. #if !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  2795. /* Use wolfCrypt RSA key's RNG if available/set. */
  2796. rng = ((RsaKey*)rsa->internal)->rng;
  2797. #endif
  2798. }
  2799. if ((!err) && (rng == NULL) && (tmpRng != NULL)) {
  2800. /* Make an RNG with tmpRng or get global. */
  2801. rng = wolfssl_make_rng(*tmpRng, initTmpRng);
  2802. if ((rng != NULL) && *initTmpRng) {
  2803. *tmpRng = rng;
  2804. }
  2805. }
  2806. return rng;
  2807. }
  2808. #endif
  2809. /* Use the wolfCrypt RSA APIs to generate a new RSA key.
  2810. *
  2811. * @param [in, out] rsa RSA key.
  2812. * @param [in] bits Number of bits that the modulus must have.
  2813. * @param [in] e A BN object holding the public exponent to use.
  2814. * @param [in] cb Status callback. Unused.
  2815. * @return 0 on success.
  2816. * @return wolfSSL native error code on error.
  2817. */
  2818. static int wolfssl_rsa_generate_key_native(WOLFSSL_RSA* rsa, int bits,
  2819. WOLFSSL_BIGNUM* e, void* cb)
  2820. {
  2821. #ifdef WOLFSSL_KEY_GEN
  2822. int ret = 0;
  2823. #ifdef WOLFSSL_SMALL_STACK
  2824. WC_RNG* tmpRng = NULL;
  2825. #else
  2826. WC_RNG _tmpRng[1];
  2827. WC_RNG* tmpRng = _tmpRng;
  2828. #endif
  2829. int initTmpRng = 0;
  2830. WC_RNG* rng = NULL;
  2831. #endif
  2832. (void)cb;
  2833. WOLFSSL_ENTER("wolfssl_rsa_generate_key_native");
  2834. #ifdef WOLFSSL_KEY_GEN
  2835. /* Get RNG in wolfCrypt RSA key or initialize a new one (or global). */
  2836. rng = WOLFSSL_RSA_GetRNG(rsa, (WC_RNG**)&tmpRng, &initTmpRng);
  2837. if (rng == NULL) {
  2838. /* Something went wrong so return memory error. */
  2839. ret = MEMORY_E;
  2840. }
  2841. if (ret == 0) {
  2842. /* Generate an RSA key. */
  2843. ret = wc_MakeRsaKey((RsaKey*)rsa->internal, bits,
  2844. (long)wolfSSL_BN_get_word(e), rng);
  2845. if (ret != MP_OKAY) {
  2846. WOLFSSL_ERROR_MSG("wc_MakeRsaKey failed");
  2847. }
  2848. }
  2849. if (ret == 0) {
  2850. /* Get the values from wolfCrypt RSA key into external RSA key. */
  2851. ret = SetRsaExternal(rsa);
  2852. if (ret == 1) {
  2853. /* Internal matches external. */
  2854. rsa->inSet = 1;
  2855. /* Return success. */
  2856. ret = 0;
  2857. }
  2858. else {
  2859. /* Something went wrong so return memory error. */
  2860. ret = MEMORY_E;
  2861. }
  2862. }
  2863. /* Finalize RNG if initialized in WOLFSSL_RSA_GetRNG(). */
  2864. if (initTmpRng) {
  2865. wc_FreeRng(tmpRng);
  2866. }
  2867. #ifdef WOLFSSL_SMALL_STACK
  2868. /* Dispose of any allocated RNG. */
  2869. XFREE(tmpRng, NULL, DYNAMIC_TYPE_RNG);
  2870. #endif
  2871. return ret;
  2872. #else
  2873. WOLFSSL_ERROR_MSG("No Key Gen built in");
  2874. (void)rsa;
  2875. (void)e;
  2876. (void)bits;
  2877. return NOT_COMPILED_IN;
  2878. #endif
  2879. }
  2880. /* Generate an RSA key that has the specified modulus size and public exponent.
  2881. *
  2882. * Note: Because of wc_MakeRsaKey an RSA key size generated can be rounded
  2883. * down to nearest multiple of 8. For example generating a key of size
  2884. * 2999 bits will make a key of size 374 bytes instead of 375 bytes.
  2885. *
  2886. * @param [in] bits Number of bits that the modulus must have i.e. 2048.
  2887. * @param [in] e Public exponent to use i.e. 65537.
  2888. * @param [in] cb Status callback. Unused.
  2889. * @param [in] data Data to pass to status callback. Unused.
  2890. * @return A new RSA key on success.
  2891. * @return NULL on failure.
  2892. */
  2893. WOLFSSL_RSA* wolfSSL_RSA_generate_key(int bits, unsigned long e,
  2894. void(*cb)(int, int, void*), void* data)
  2895. {
  2896. WOLFSSL_RSA* rsa = NULL;
  2897. WOLFSSL_BIGNUM* bn = NULL;
  2898. int err = 0;
  2899. WOLFSSL_ENTER("wolfSSL_RSA_generate_key");
  2900. (void)cb;
  2901. (void)data;
  2902. /* Validate bits. */
  2903. if (bits < 0) {
  2904. WOLFSSL_ERROR_MSG("Bad argument: bits was less than 0");
  2905. err = 1;
  2906. }
  2907. /* Create a new BN to hold public exponent - for when wolfCrypt supports
  2908. * longer values. */
  2909. if ((!err) && ((bn = wolfSSL_BN_new()) == NULL)) {
  2910. WOLFSSL_ERROR_MSG("Error creating big number");
  2911. err = 1;
  2912. }
  2913. /* Set public exponent. */
  2914. if ((!err) && (wolfSSL_BN_set_word(bn, e) != 1)) {
  2915. WOLFSSL_ERROR_MSG("Error using e value");
  2916. err = 1;
  2917. }
  2918. /* Create an RSA key object to hold generated key. */
  2919. if ((!err) && ((rsa = wolfSSL_RSA_new()) == NULL)) {
  2920. WOLFSSL_ERROR_MSG("memory error");
  2921. err = 1;
  2922. }
  2923. while (!err) {
  2924. int ret;
  2925. /* Use wolfCrypt to generate RSA key. */
  2926. ret = wolfssl_rsa_generate_key_native(rsa, bits, bn, NULL);
  2927. #ifdef HAVE_FIPS
  2928. /* Keep trying if failed to find a prime. */
  2929. if (ret == PRIME_GEN_E) {
  2930. continue;
  2931. }
  2932. #endif
  2933. if (ret != WOLFSSL_ERROR_NONE) {
  2934. /* Unrecoverable error in generation. */
  2935. err = 1;
  2936. }
  2937. /* Done generating - unrecoverable error or success. */
  2938. break;
  2939. }
  2940. if (err) {
  2941. /* Dispose of RSA key object if generation didn't work. */
  2942. wolfSSL_RSA_free(rsa);
  2943. /* Returning NULL on error. */
  2944. rsa = NULL;
  2945. }
  2946. /* Dispose of the temporary BN used for the public exponent. */
  2947. wolfSSL_BN_free(bn);
  2948. return rsa;
  2949. }
  2950. /* Generate an RSA key that has the specified modulus size and public exponent.
  2951. *
  2952. * Note: Because of wc_MakeRsaKey an RSA key size generated can be rounded
  2953. * down to nearest multiple of 8. For example generating a key of size
  2954. * 2999 bits will make a key of size 374 bytes instead of 375 bytes.
  2955. *
  2956. * @param [in] bits Number of bits that the modulus must have i.e. 2048.
  2957. * @param [in] e Public exponent to use, i.e. 65537, as a BN.
  2958. * @param [in] cb Status callback. Unused.
  2959. * @return 1 on success.
  2960. * @return 0 on failure.
  2961. */
  2962. int wolfSSL_RSA_generate_key_ex(WOLFSSL_RSA* rsa, int bits, WOLFSSL_BIGNUM* e,
  2963. void* cb)
  2964. {
  2965. int ret = 1;
  2966. /* Validate parameters. */
  2967. if ((rsa == NULL) || (rsa->internal == NULL)) {
  2968. WOLFSSL_ERROR_MSG("bad arguments");
  2969. ret = 0;
  2970. }
  2971. else {
  2972. for (;;) {
  2973. /* Use wolfCrypt to generate RSA key. */
  2974. int gen_ret = wolfssl_rsa_generate_key_native(rsa, bits, e, cb);
  2975. #ifdef HAVE_FIPS
  2976. /* Keep trying again if public key value didn't work. */
  2977. if (gen_ret == PRIME_GEN_E) {
  2978. continue;
  2979. }
  2980. #endif
  2981. if (gen_ret != WOLFSSL_ERROR_NONE) {
  2982. /* Unrecoverable error in generation. */
  2983. ret = 0;
  2984. }
  2985. /* Done generating - unrecoverable error or success. */
  2986. break;
  2987. }
  2988. }
  2989. return ret;
  2990. }
  2991. #endif /* OPENSSL_EXTRA */
  2992. /*
  2993. * RSA padding APIs
  2994. */
  2995. #if defined(WC_RSA_PSS) && (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || \
  2996. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX))
  2997. #if !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  2998. /* Add PKCS#1 PSS padding to hash.
  2999. *
  3000. *
  3001. * +-----------+
  3002. * | M |
  3003. * +-----------+
  3004. * |
  3005. * V
  3006. * Hash
  3007. * |
  3008. * V
  3009. * +--------+----------+----------+
  3010. * M' = |Padding1| mHash | salt |
  3011. * +--------+----------+----------+
  3012. * |
  3013. * +--------+----------+ V
  3014. * DB = |Padding2|maskedseed| Hash
  3015. * +--------+----------+ |
  3016. * | |
  3017. * V | +--+
  3018. * xor <--- MGF <---| |bc|
  3019. * | | +--+
  3020. * | | |
  3021. * V V V
  3022. * +-------------------+----------+--+
  3023. * EM = | maskedDB |maskedseed|bc|
  3024. * +-------------------+----------+--+
  3025. * Diagram taken from https://tools.ietf.org/html/rfc3447#section-9.1
  3026. *
  3027. * @param [in] rsa RSA key.
  3028. * @param [out] em Encoded message.
  3029. * @param [in[ mHash Message hash.
  3030. * @param [in] hashAlg Hash algorithm.
  3031. * @param [in] saltLen Length of salt to generate.
  3032. * @return 1 on success.
  3033. * @return 0 on failure.
  3034. */
  3035. int wolfSSL_RSA_padding_add_PKCS1_PSS(WOLFSSL_RSA *rsa, unsigned char *em,
  3036. const unsigned char *mHash, const WOLFSSL_EVP_MD *hashAlg, int saltLen)
  3037. {
  3038. int ret = 1;
  3039. enum wc_HashType hashType;
  3040. int hashLen = 0;
  3041. int emLen = 0;
  3042. int mgf = 0;
  3043. int initTmpRng = 0;
  3044. WC_RNG *rng = NULL;
  3045. #ifdef WOLFSSL_SMALL_STACK
  3046. WC_RNG* tmpRng = NULL;
  3047. #else
  3048. WC_RNG _tmpRng[1];
  3049. WC_RNG* tmpRng = _tmpRng;
  3050. #endif
  3051. WOLFSSL_ENTER("wolfSSL_RSA_padding_add_PKCS1_PSS");
  3052. /* Validate parameters. */
  3053. if ((rsa == NULL) || (em == NULL) || (mHash == NULL) || (hashAlg == NULL)) {
  3054. ret = 0;
  3055. }
  3056. if (ret == 1) {
  3057. /* Get/create an RNG. */
  3058. rng = WOLFSSL_RSA_GetRNG(rsa, (WC_RNG**)&tmpRng, &initTmpRng);
  3059. if (rng == NULL) {
  3060. WOLFSSL_ERROR_MSG("WOLFSSL_RSA_GetRNG error");
  3061. ret = 0;
  3062. }
  3063. }
  3064. /* TODO: use wolfCrypt RSA key to get emLen and bits? */
  3065. /* Set the external data from the wolfCrypt RSA key if not done. */
  3066. if ((ret == 1) && (!rsa->exSet)) {
  3067. ret = SetRsaExternal(rsa);
  3068. }
  3069. if (ret == 1) {
  3070. /* Get the wolfCrypt hash algorithm type. */
  3071. hashType = EvpMd2MacType(hashAlg);
  3072. if (hashType > WC_HASH_TYPE_MAX) {
  3073. WOLFSSL_ERROR_MSG("EvpMd2MacType error");
  3074. ret = 0;
  3075. }
  3076. }
  3077. if (ret == 1) {
  3078. /* Get the wolfCrypt MGF algorithm from hash algorithm. */
  3079. mgf = wc_hash2mgf(hashType);
  3080. if (mgf == WC_MGF1NONE) {
  3081. WOLFSSL_ERROR_MSG("wc_hash2mgf error");
  3082. ret = 0;
  3083. }
  3084. }
  3085. if (ret == 1) {
  3086. /* Get the length of the hash output. */
  3087. hashLen = wolfSSL_EVP_MD_size(hashAlg);
  3088. if (hashLen < 0) {
  3089. WOLFSSL_ERROR_MSG("wolfSSL_EVP_MD_size error");
  3090. ret = 0;
  3091. }
  3092. }
  3093. if (ret == 1) {
  3094. /* Get length of RSA key - encrypted message length. */
  3095. emLen = wolfSSL_RSA_size(rsa);
  3096. if (ret <= 0) {
  3097. WOLFSSL_ERROR_MSG("wolfSSL_RSA_size error");
  3098. ret = 0;
  3099. }
  3100. }
  3101. if (ret == 1) {
  3102. /* Calculate the salt length to use for special cases. */
  3103. /* TODO: use special case wolfCrypt values? */
  3104. switch (saltLen) {
  3105. /* Negative saltLen values are treated differently. */
  3106. case RSA_PSS_SALTLEN_DIGEST:
  3107. saltLen = hashLen;
  3108. break;
  3109. case RSA_PSS_SALTLEN_MAX_SIGN:
  3110. case RSA_PSS_SALTLEN_MAX:
  3111. #ifdef WOLFSSL_PSS_LONG_SALT
  3112. saltLen = emLen - hashLen - 2;
  3113. #else
  3114. saltLen = hashLen;
  3115. #endif
  3116. break;
  3117. default:
  3118. if (saltLen < 0) {
  3119. /* No other negative values implemented. */
  3120. WOLFSSL_ERROR_MSG("invalid saltLen");
  3121. ret = 0;
  3122. }
  3123. }
  3124. }
  3125. if (ret == 1) {
  3126. /* Generate RSA PKCS#1 PSS padding for hash using wolfCrypt. */
  3127. if (wc_RsaPad_ex(mHash, (word32)hashLen, em, (word32)emLen,
  3128. RSA_BLOCK_TYPE_1, rng, WC_RSA_PSS_PAD, hashType, mgf, NULL, 0,
  3129. saltLen, wolfSSL_BN_num_bits(rsa->n), NULL) != MP_OKAY) {
  3130. WOLFSSL_ERROR_MSG("wc_RsaPad_ex error");
  3131. ret = 0;
  3132. }
  3133. }
  3134. /* Finalize RNG if initialized in WOLFSSL_RSA_GetRNG(). */
  3135. if (initTmpRng) {
  3136. wc_FreeRng(tmpRng);
  3137. }
  3138. #ifdef WOLFSSL_SMALL_STACK
  3139. /* Dispose of any allocated RNG. */
  3140. XFREE(tmpRng, NULL, DYNAMIC_TYPE_RNG);
  3141. #endif
  3142. return ret;
  3143. }
  3144. /* Checks that the hash is valid for the RSA PKCS#1 PSS encoded message.
  3145. *
  3146. * Refer to wolfSSL_RSA_padding_add_PKCS1_PSS for a diagram.
  3147. *
  3148. * @param [in] rsa RSA key.
  3149. * @param [in[ mHash Message hash.
  3150. * @param [in] hashAlg Hash algorithm.
  3151. * @param [in] em Encoded message.
  3152. * @param [in] saltLen Length of salt to generate.
  3153. * @return 1 on success.
  3154. * @return 0 on failure.
  3155. */
  3156. int wolfSSL_RSA_verify_PKCS1_PSS(WOLFSSL_RSA *rsa, const unsigned char *mHash,
  3157. const WOLFSSL_EVP_MD *hashAlg,
  3158. const unsigned char *em, int saltLen)
  3159. {
  3160. int ret = 1;
  3161. int hashLen = 0;
  3162. int mgf = 0;
  3163. int emLen = 0;
  3164. int mPrimeLen = 0;
  3165. enum wc_HashType hashType = WC_HASH_TYPE_NONE;
  3166. byte *mPrime = NULL;
  3167. byte *buf = NULL;
  3168. WOLFSSL_ENTER("wolfSSL_RSA_verify_PKCS1_PSS");
  3169. /* Validate parameters. */
  3170. if ((rsa == NULL) || (mHash == NULL) || (hashAlg == NULL) || (em == NULL)) {
  3171. ret = 0;
  3172. }
  3173. /* TODO: use wolfCrypt RSA key to get emLen and bits? */
  3174. /* Set the external data from the wolfCrypt RSA key if not done. */
  3175. if ((ret == 1) && (!rsa->exSet)) {
  3176. ret = SetRsaExternal(rsa);
  3177. }
  3178. if (ret == 1) {
  3179. /* Get hash length for hash algorithm. */
  3180. hashLen = wolfSSL_EVP_MD_size(hashAlg);
  3181. if (hashLen < 0) {
  3182. ret = 0;
  3183. }
  3184. }
  3185. if (ret == 1) {
  3186. /* Get length of RSA key - encrypted message length. */
  3187. emLen = wolfSSL_RSA_size(rsa);
  3188. if (emLen <= 0) {
  3189. WOLFSSL_ERROR_MSG("wolfSSL_RSA_size error");
  3190. ret = 0;
  3191. }
  3192. }
  3193. if (ret == 1) {
  3194. /* Calculate the salt length to use for special cases. */
  3195. /* TODO: use special case wolfCrypt values. */
  3196. switch (saltLen) {
  3197. /* Negative saltLen values are treated differently */
  3198. case RSA_PSS_SALTLEN_DIGEST:
  3199. saltLen = hashLen;
  3200. break;
  3201. case RSA_PSS_SALTLEN_MAX_SIGN:
  3202. case RSA_PSS_SALTLEN_MAX:
  3203. #ifdef WOLFSSL_PSS_LONG_SALT
  3204. saltLen = emLen - hashLen - 2;
  3205. #else
  3206. saltLen = hashLen;
  3207. #endif
  3208. break;
  3209. default:
  3210. if (saltLen < 0) {
  3211. /* No other negative values implemented. */
  3212. WOLFSSL_ERROR_MSG("invalid saltLen");
  3213. ret = 0;
  3214. }
  3215. }
  3216. }
  3217. if (ret == 1) {
  3218. /* Get the wolfCrypt hash algorithm type. */
  3219. hashType = EvpMd2MacType(hashAlg);
  3220. if (hashType > WC_HASH_TYPE_MAX) {
  3221. WOLFSSL_ERROR_MSG("EvpMd2MacType error");
  3222. ret = 0;
  3223. }
  3224. }
  3225. if (ret == 1) {
  3226. /* Get the wolfCrypt MGF algorithm from hash algorithm. */
  3227. if ((mgf = wc_hash2mgf(hashType)) == WC_MGF1NONE) {
  3228. WOLFSSL_ERROR_MSG("wc_hash2mgf error");
  3229. ret = 0;
  3230. }
  3231. }
  3232. if (ret == 1) {
  3233. /* Allocate buffer to unpad inline with. */
  3234. buf = (byte*)XMALLOC((size_t)emLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3235. if (buf == NULL) {
  3236. WOLFSSL_ERROR_MSG("malloc error");
  3237. ret = 0;
  3238. }
  3239. }
  3240. if (ret == 1) {
  3241. /* Copy encrypted message to temp for inline unpadding. */
  3242. XMEMCPY(buf, em, (size_t)emLen);
  3243. /* Remove and verify the PSS padding. */
  3244. mPrimeLen = wc_RsaUnPad_ex(buf, (word32)emLen, &mPrime,
  3245. RSA_BLOCK_TYPE_1, WC_RSA_PSS_PAD, hashType, mgf, NULL, 0, saltLen,
  3246. wolfSSL_BN_num_bits(rsa->n), NULL);
  3247. if (mPrimeLen < 0) {
  3248. WOLFSSL_ERROR_MSG("wc_RsaPad_ex error");
  3249. ret = 0;
  3250. }
  3251. }
  3252. if (ret == 1) {
  3253. /* Verify the hash is correct. */
  3254. if (wc_RsaPSS_CheckPadding_ex(mHash, (word32)hashLen, mPrime,
  3255. (word32)mPrimeLen, hashType, saltLen,
  3256. wolfSSL_BN_num_bits(rsa->n)) != MP_OKAY) {
  3257. WOLFSSL_ERROR_MSG("wc_RsaPSS_CheckPadding_ex error");
  3258. ret = 0;
  3259. }
  3260. }
  3261. /* Dispose of any allocated buffer. */
  3262. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3263. return ret;
  3264. }
  3265. #endif /* !HAVE_FIPS || FIPS_VERSION_GT(2,0) */
  3266. #endif /* WC_RSA_PSS && (OPENSSL_ALL || WOLFSSL_ASIO || WOLFSSL_HAPROXY ||
  3267. * WOLFSSL_NGINX) */
  3268. /*
  3269. * RSA sign/verify APIs
  3270. */
  3271. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  3272. #define DEF_PSS_SALT_LEN RSA_PSS_SALT_LEN_DEFAULT
  3273. #else
  3274. #define DEF_PSS_SALT_LEN RSA_PSS_SALT_LEN_DISCOVER
  3275. #endif
  3276. #if defined(OPENSSL_EXTRA)
  3277. #if !defined(HAVE_USER_RSA)
  3278. /* Encode the message hash.
  3279. *
  3280. * Used by signing and verification.
  3281. *
  3282. * @param [in] hashAlg Hash algorithm OID.
  3283. * @param [in] hash Hash of message to encode for signing.
  3284. * @param [in] hLen Length of hash of message.
  3285. * @param [out] enc Encoded message hash.
  3286. * @param [out] encLen Length of encoded message hash.
  3287. * @param [in] padding Which padding scheme is being used.
  3288. * @return 1 on success.
  3289. * @return 0 on failure.
  3290. */
  3291. static int wolfssl_rsa_sig_encode(int hashAlg, const unsigned char* hash,
  3292. unsigned int hLen, unsigned char* enc, unsigned int* encLen, int padding)
  3293. {
  3294. int ret = 1;
  3295. int hType = WC_HASH_TYPE_NONE;
  3296. /* Validate parameters. */
  3297. if ((hash == NULL) || (enc == NULL) || (encLen == NULL)) {
  3298. ret = 0;
  3299. }
  3300. if ((ret == 1) && (hashAlg != NID_undef) &&
  3301. (padding == RSA_PKCS1_PADDING)) {
  3302. /* Convert hash algorithm to hash type for PKCS#1.5 padding. */
  3303. hType = (int)nid2oid(hashAlg, oidHashType);
  3304. if (hType == -1) {
  3305. ret = 0;
  3306. }
  3307. }
  3308. if ((ret == 1) && (padding == RSA_PKCS1_PADDING)) {
  3309. /* PKCS#1.5 encoding. */
  3310. word32 encSz = wc_EncodeSignature(enc, hash, hLen, hType);
  3311. if (encSz == 0) {
  3312. WOLFSSL_ERROR_MSG("Bad Encode Signature");
  3313. ret = 0;
  3314. }
  3315. else {
  3316. *encLen = (unsigned int)encSz;
  3317. }
  3318. }
  3319. /* Other padding schemes require the hash as is. */
  3320. if ((ret == 1) && (padding != RSA_PKCS1_PADDING)) {
  3321. XMEMCPY(enc, hash, hLen);
  3322. *encLen = hLen;
  3323. }
  3324. return ret;
  3325. }
  3326. /* Sign the message hash using hash algorithm and RSA key.
  3327. *
  3328. * @param [in] hashAlg Hash algorithm OID.
  3329. * @param [in] hash Hash of message to encode for signing.
  3330. * @param [in] hLen Length of hash of message.
  3331. * @param [out] enc Encoded message hash.
  3332. * @param [out] encLen Length of encoded message hash.
  3333. * @param [in] rsa RSA key.
  3334. * @return 1 on success.
  3335. * @return 0 on failure.
  3336. */
  3337. int wolfSSL_RSA_sign(int hashAlg, const unsigned char* hash, unsigned int hLen,
  3338. unsigned char* sigRet, unsigned int* sigLen, WOLFSSL_RSA* rsa)
  3339. {
  3340. if (sigLen != NULL) {
  3341. /* No size checking in this API */
  3342. *sigLen = RSA_MAX_SIZE / CHAR_BIT;
  3343. }
  3344. /* flag is 1: output complete signature. */
  3345. return wolfSSL_RSA_sign_generic_padding(hashAlg, hash, hLen, sigRet,
  3346. sigLen, rsa, 1, RSA_PKCS1_PADDING);
  3347. }
  3348. /* Sign the message hash using hash algorithm and RSA key.
  3349. *
  3350. * Not OpenSSL API.
  3351. *
  3352. * @param [in] hashAlg Hash algorithm NID.
  3353. * @param [in] hash Hash of message to encode for signing.
  3354. * @param [in] hLen Length of hash of message.
  3355. * @param [out] enc Encoded message hash.
  3356. * @param [out] encLen Length of encoded message hash.
  3357. * @param [in] rsa RSA key.
  3358. * @param [in] flag When 1: Output encrypted signature.
  3359. * When 0: Output encoded hash.
  3360. * @return 1 on success.
  3361. * @return 0 on failure.
  3362. */
  3363. int wolfSSL_RSA_sign_ex(int hashAlg, const unsigned char* hash,
  3364. unsigned int hLen, unsigned char* sigRet, unsigned int* sigLen,
  3365. WOLFSSL_RSA* rsa, int flag)
  3366. {
  3367. int ret = 0;
  3368. if ((flag == 0) || (flag == 1)) {
  3369. if (sigLen != NULL) {
  3370. /* No size checking in this API */
  3371. *sigLen = RSA_MAX_SIZE / CHAR_BIT;
  3372. }
  3373. ret = wolfSSL_RSA_sign_generic_padding(hashAlg, hash, hLen, sigRet,
  3374. sigLen, rsa, flag, RSA_PKCS1_PADDING);
  3375. }
  3376. return ret;
  3377. }
  3378. /**
  3379. * Sign a message hash with the chosen message digest, padding, and RSA key.
  3380. *
  3381. * Not OpenSSL API.
  3382. *
  3383. * @param [in] hashAlg Hash NID
  3384. * @param [in] hash Message hash to sign.
  3385. * @param [in] mLen Length of message hash to sign.
  3386. * @param [out] sigRet Output buffer.
  3387. * @param [in, out] sigLen On Input: length of sigRet buffer.
  3388. * On Output: length of data written to sigRet.
  3389. * @param [in] rsa RSA key used to sign the input.
  3390. * @param [in] flag 1: Output the signature.
  3391. * 0: Output the value that the unpadded signature
  3392. * should be compared to.
  3393. * @param [in] padding Padding to use. Only RSA_PKCS1_PSS_PADDING and
  3394. * RSA_PKCS1_PADDING are currently supported for
  3395. * signing.
  3396. * @return 1 on success.
  3397. * @return 0 on failure.
  3398. */
  3399. int wolfSSL_RSA_sign_generic_padding(int hashAlg, const unsigned char* hash,
  3400. unsigned int hLen, unsigned char* sigRet, unsigned int* sigLen,
  3401. WOLFSSL_RSA* rsa, int flag, int padding)
  3402. {
  3403. int ret = 1;
  3404. word32 outLen = 0;
  3405. int signSz = 0;
  3406. WC_RNG* rng = NULL;
  3407. int initTmpRng = 0;
  3408. #ifdef WOLFSSL_SMALL_STACK
  3409. WC_RNG* tmpRng = NULL;
  3410. byte* encodedSig = NULL;
  3411. #else
  3412. WC_RNG _tmpRng[1];
  3413. WC_RNG* tmpRng = _tmpRng;
  3414. byte encodedSig[MAX_ENCODED_SIG_SZ];
  3415. #endif
  3416. unsigned int encSz = 0;
  3417. WOLFSSL_ENTER("wolfSSL_RSA_sign_generic_padding");
  3418. if (flag == 0) {
  3419. /* Only encode message. */
  3420. return wolfssl_rsa_sig_encode(hashAlg, hash, hLen, sigRet, sigLen,
  3421. padding);
  3422. }
  3423. /* Validate parameters. */
  3424. if ((hash == NULL) || (sigRet == NULL) || sigLen == NULL || rsa == NULL) {
  3425. WOLFSSL_ERROR_MSG("Bad function arguments");
  3426. ret = 0;
  3427. }
  3428. /* Set wolfCrypt RSA key data from external if not already done. */
  3429. if ((ret == 1) && (!rsa->inSet) && (SetRsaInternal(rsa) != 1)) {
  3430. ret = 0;
  3431. }
  3432. if (ret == 1) {
  3433. /* Get the maximum signature length. */
  3434. outLen = (word32)wolfSSL_BN_num_bytes(rsa->n);
  3435. /* Check not an error return. */
  3436. if (outLen == 0) {
  3437. WOLFSSL_ERROR_MSG("Bad RSA size");
  3438. ret = 0;
  3439. }
  3440. /* Check signature buffer is big enough. */
  3441. else if (outLen > *sigLen) {
  3442. WOLFSSL_ERROR_MSG("Output buffer too small");
  3443. ret = 0;
  3444. }
  3445. }
  3446. #ifdef WOLFSSL_SMALL_STACK
  3447. if (ret == 1) {
  3448. /* Allocate encoded signature buffer if doing PKCS#1 padding. */
  3449. encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, NULL,
  3450. DYNAMIC_TYPE_SIGNATURE);
  3451. if (encodedSig == NULL) {
  3452. ret = 0;
  3453. }
  3454. }
  3455. #endif
  3456. if (ret == 1) {
  3457. /* Get/create an RNG. */
  3458. rng = WOLFSSL_RSA_GetRNG(rsa, (WC_RNG**)&tmpRng, &initTmpRng);
  3459. if (rng == NULL) {
  3460. WOLFSSL_ERROR_MSG("WOLFSSL_RSA_GetRNG error");
  3461. ret = 0;
  3462. }
  3463. }
  3464. /* Either encodes with PKCS#1.5 or copies hash into encodedSig. */
  3465. if ((ret == 1) && (wolfssl_rsa_sig_encode(hashAlg, hash, hLen, encodedSig,
  3466. &encSz, padding) == 0)) {
  3467. WOLFSSL_ERROR_MSG("Bad Encode Signature");
  3468. ret = 0;
  3469. }
  3470. if (ret == 1) {
  3471. switch (padding) {
  3472. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  3473. case RSA_NO_PADDING:
  3474. if ((signSz = wc_RsaDirect(encodedSig, encSz, sigRet, &outLen,
  3475. (RsaKey*)rsa->internal, RSA_PRIVATE_ENCRYPT, rng)) <= 0) {
  3476. WOLFSSL_ERROR_MSG("Bad Rsa Sign no pad");
  3477. ret = 0;
  3478. }
  3479. break;
  3480. #endif
  3481. #if defined(WC_RSA_PSS) && !defined(HAVE_SELFTEST) && \
  3482. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,1))
  3483. case RSA_PKCS1_PSS_PADDING:
  3484. {
  3485. enum wc_HashType hType =
  3486. wc_OidGetHash((int)nid2oid(hashAlg, oidHashType));
  3487. #ifndef WOLFSSL_PSS_SALT_LEN_DISCOVER
  3488. WOLFSSL_MSG("Using RSA-PSS with hash length salt. "
  3489. "OpenSSL uses max length by default.");
  3490. #endif
  3491. /* Create RSA PSS signature. */
  3492. if ((signSz = wc_RsaPSS_Sign_ex(encodedSig, encSz, sigRet, outLen,
  3493. hType, wc_hash2mgf(hType), DEF_PSS_SALT_LEN,
  3494. (RsaKey*)rsa->internal, rng)) <= 0) {
  3495. WOLFSSL_ERROR_MSG("Bad Rsa Sign");
  3496. ret = 0;
  3497. }
  3498. break;
  3499. }
  3500. #endif
  3501. #ifndef WC_NO_RSA_OAEP
  3502. case RSA_PKCS1_OAEP_PADDING:
  3503. /* Not a signature padding scheme. */
  3504. WOLFSSL_ERROR_MSG("RSA_PKCS1_OAEP_PADDING not supported for "
  3505. "signing");
  3506. ret = 0;
  3507. break;
  3508. #endif
  3509. case RSA_PKCS1_PADDING:
  3510. {
  3511. /* Sign (private encrypt) PKCS#1 encoded signature. */
  3512. if ((signSz = wc_RsaSSL_Sign(encodedSig, encSz, sigRet, outLen,
  3513. (RsaKey*)rsa->internal, rng)) <= 0) {
  3514. WOLFSSL_ERROR_MSG("Bad Rsa Sign");
  3515. ret = 0;
  3516. }
  3517. break;
  3518. }
  3519. default:
  3520. WOLFSSL_ERROR_MSG("Unsupported padding");
  3521. ret = 0;
  3522. break;
  3523. }
  3524. }
  3525. if (ret == 1) {
  3526. /* Return the size of signature generated. */
  3527. *sigLen = (unsigned int)signSz;
  3528. }
  3529. /* Finalize RNG if initialized in WOLFSSL_RSA_GetRNG(). */
  3530. if (initTmpRng) {
  3531. wc_FreeRng(tmpRng);
  3532. }
  3533. #ifdef WOLFSSL_SMALL_STACK
  3534. /* Dispose of any allocated RNG and encoded signature. */
  3535. XFREE(tmpRng, NULL, DYNAMIC_TYPE_RNG);
  3536. XFREE(encodedSig, NULL, DYNAMIC_TYPE_SIGNATURE);
  3537. #endif
  3538. WOLFSSL_LEAVE("wolfSSL_RSA_sign_generic_padding", ret);
  3539. return ret;
  3540. }
  3541. /**
  3542. * Verify a message hash with the chosen message digest, padding, and RSA key.
  3543. *
  3544. * @param [in] hashAlg Hash NID
  3545. * @param [in] hash Message hash.
  3546. * @param [in] mLen Length of message hash.
  3547. * @param [in] sigRet Signature data.
  3548. * @param [in] sigLen Length of signature data.
  3549. * @param [in] rsa RSA key used to sign the input
  3550. * @return 1 on success.
  3551. * @return 0 on failure.
  3552. */
  3553. int wolfSSL_RSA_verify(int hashAlg, const unsigned char* hash,
  3554. unsigned int hLen, const unsigned char* sig, unsigned int sigLen,
  3555. WOLFSSL_RSA* rsa)
  3556. {
  3557. return wolfSSL_RSA_verify_ex(hashAlg, hash, hLen, sig, sigLen, rsa,
  3558. RSA_PKCS1_PADDING);
  3559. }
  3560. /**
  3561. * Verify a message hash with the chosen message digest, padding, and RSA key.
  3562. *
  3563. * Not OpenSSL API.
  3564. *
  3565. * @param [in] hashAlg Hash NID
  3566. * @param [in] hash Message hash.
  3567. * @param [in] mLen Length of message hash.
  3568. * @param [in] sigRet Signature data.
  3569. * @param [in] sigLen Length of signature data.
  3570. * @param [in] rsa RSA key used to sign the input
  3571. * @param [in] padding Padding to use. Only RSA_PKCS1_PSS_PADDING and
  3572. * RSA_PKCS1_PADDING are currently supported for
  3573. * signing.
  3574. * @return 1 on success.
  3575. * @return 0 on failure.
  3576. */
  3577. int wolfSSL_RSA_verify_ex(int hashAlg, const unsigned char* hash,
  3578. unsigned int hLen, const unsigned char* sig, unsigned int sigLen,
  3579. WOLFSSL_RSA* rsa, int padding)
  3580. {
  3581. int ret = 1;
  3582. #ifdef WOLFSSL_SMALL_STACK
  3583. unsigned char* encodedSig = NULL;
  3584. #else
  3585. unsigned char encodedSig[MAX_ENCODED_SIG_SZ];
  3586. #endif
  3587. unsigned char* sigDec = NULL;
  3588. unsigned int len = MAX_ENCODED_SIG_SZ;
  3589. int verLen = 0;
  3590. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 1)) && !defined(HAVE_SELFTEST)
  3591. enum wc_HashType hType = WC_HASH_TYPE_NONE;
  3592. #endif
  3593. WOLFSSL_ENTER("wolfSSL_RSA_verify");
  3594. /* Validate parameters. */
  3595. if ((hash == NULL) || (sig == NULL) || (rsa == NULL)) {
  3596. WOLFSSL_ERROR_MSG("Bad function arguments");
  3597. ret = 0;
  3598. }
  3599. if (ret == 1) {
  3600. /* Allocate memory for decrypted signature. */
  3601. sigDec = (unsigned char *)XMALLOC(sigLen, NULL,
  3602. DYNAMIC_TYPE_TMP_BUFFER);
  3603. if (sigDec == NULL) {
  3604. WOLFSSL_ERROR_MSG("Memory allocation failure");
  3605. ret = 0;
  3606. }
  3607. }
  3608. #ifdef WOLFSSL_SMALL_STACK
  3609. if ((ret == 1) && (padding != RSA_PKCS1_PSS_PADDING)) {
  3610. /* Allocate memory for encoded signature. */
  3611. encodedSig = (unsigned char *)XMALLOC(len, NULL,
  3612. DYNAMIC_TYPE_TMP_BUFFER);
  3613. if (encodedSig == NULL) {
  3614. WOLFSSL_ERROR_MSG("Memory allocation failure");
  3615. ret = 0;
  3616. }
  3617. }
  3618. #endif
  3619. if ((ret == 1) && (padding != RSA_PKCS1_PSS_PADDING)) {
  3620. /* Make encoded signature to compare with decrypted signature. */
  3621. if (wolfssl_rsa_sig_encode(hashAlg, hash, hLen, encodedSig, &len,
  3622. padding) <= 0) {
  3623. WOLFSSL_ERROR_MSG("Message Digest Error");
  3624. ret = 0;
  3625. }
  3626. }
  3627. if (ret == 1) {
  3628. /* Decrypt signature */
  3629. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 1)) && \
  3630. !defined(HAVE_SELFTEST)
  3631. hType = wc_OidGetHash((int)nid2oid(hashAlg, oidHashType));
  3632. if ((verLen = wc_RsaSSL_Verify_ex2(sig, sigLen, (unsigned char *)sigDec,
  3633. sigLen, (RsaKey*)rsa->internal, padding, hType)) <= 0) {
  3634. WOLFSSL_ERROR_MSG("RSA Decrypt error");
  3635. ret = 0;
  3636. }
  3637. #else
  3638. verLen = wc_RsaSSL_Verify(sig, sigLen, (unsigned char *)sigDec, sigLen,
  3639. (RsaKey*)rsa->internal);
  3640. if (verLen < 0) {
  3641. ret = 0;
  3642. }
  3643. #endif
  3644. }
  3645. if (ret == 1) {
  3646. #if defined(WC_RSA_PSS) && !defined(HAVE_SELFTEST) && \
  3647. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 1))
  3648. if (padding == RSA_PKCS1_PSS_PADDING) {
  3649. /* Check PSS padding is valid. */
  3650. if (wc_RsaPSS_CheckPadding_ex(hash, hLen, sigDec, (word32)verLen,
  3651. hType, DEF_PSS_SALT_LEN,
  3652. mp_count_bits(&((RsaKey*)rsa->internal)->n)) != 0) {
  3653. WOLFSSL_ERROR_MSG("wc_RsaPSS_CheckPadding_ex error");
  3654. ret = 0;
  3655. }
  3656. }
  3657. else
  3658. #endif /* WC_RSA_PSS && !HAVE_SELFTEST && (!HAVE_FIPS ||
  3659. * FIPS_VERSION >= 5.1) */
  3660. /* Compare decrypted signature to encoded signature. */
  3661. if (((int)len != verLen) ||
  3662. (XMEMCMP(encodedSig, sigDec, (size_t)verLen) != 0)) {
  3663. WOLFSSL_ERROR_MSG("wolfSSL_RSA_verify_ex failed");
  3664. ret = 0;
  3665. }
  3666. }
  3667. /* Dispose of any allocated data. */
  3668. #ifdef WOLFSSL_SMALL_STACK
  3669. XFREE(encodedSig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3670. #endif
  3671. XFREE(sigDec, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3672. return ret;
  3673. }
  3674. /*
  3675. * RSA public/private encrypt/decrypt APIs
  3676. */
  3677. #if !defined(HAVE_USER_RSA) && !defined(HAVE_FAST_RSA)
  3678. /* Encrypt with the RSA public key.
  3679. *
  3680. * Return compliant with OpenSSL.
  3681. *
  3682. * @param [in] len Length of data to encrypt.
  3683. * @param [in] from Data to encrypt.
  3684. * @param [out] to Encrypted data.
  3685. * @param [in] rsa RSA key.
  3686. * @param [in] padding Type of padding to place around plaintext.
  3687. * @return Size of encrypted data on success.
  3688. * @return -1 on failure.
  3689. */
  3690. int wolfSSL_RSA_public_encrypt(int len, const unsigned char* from,
  3691. unsigned char* to, WOLFSSL_RSA* rsa, int padding)
  3692. {
  3693. int ret = 0;
  3694. int initTmpRng = 0;
  3695. WC_RNG *rng = NULL;
  3696. #ifdef WOLFSSL_SMALL_STACK
  3697. WC_RNG* tmpRng = NULL;
  3698. #else
  3699. WC_RNG _tmpRng[1];
  3700. WC_RNG* tmpRng = _tmpRng;
  3701. #endif
  3702. #if !defined(HAVE_FIPS)
  3703. int mgf = WC_MGF1NONE;
  3704. enum wc_HashType hash = WC_HASH_TYPE_NONE;
  3705. int pad_type = WC_RSA_NO_PAD;
  3706. #endif
  3707. int outLen = 0;
  3708. WOLFSSL_ENTER("wolfSSL_RSA_public_encrypt");
  3709. /* Validate parameters. */
  3710. if ((len < 0) || (rsa == NULL) || (rsa->internal == NULL) ||
  3711. (from == NULL)) {
  3712. WOLFSSL_ERROR_MSG("Bad function arguments");
  3713. ret = -1;
  3714. }
  3715. if (ret == 0) {
  3716. #if !defined(HAVE_FIPS)
  3717. /* Convert to wolfCrypt padding, hash and MGF. */
  3718. switch (padding) {
  3719. case RSA_PKCS1_PADDING:
  3720. pad_type = WC_RSA_PKCSV15_PAD;
  3721. break;
  3722. case RSA_PKCS1_OAEP_PADDING:
  3723. pad_type = WC_RSA_OAEP_PAD;
  3724. hash = WC_HASH_TYPE_SHA;
  3725. mgf = WC_MGF1SHA1;
  3726. break;
  3727. case RSA_NO_PADDING:
  3728. pad_type = WC_RSA_NO_PAD;
  3729. break;
  3730. default:
  3731. WOLFSSL_ERROR_MSG("RSA_public_encrypt doesn't support padding "
  3732. "scheme");
  3733. ret = -1;
  3734. }
  3735. #else
  3736. /* Check for supported padding schemes in FIPS. */
  3737. /* TODO: Do we support more schemes in later versions of FIPS? */
  3738. if (padding != RSA_PKCS1_PADDING) {
  3739. WOLFSSL_ERROR_MSG("RSA_public_encrypt pad type not supported in "
  3740. "FIPS");
  3741. ret = -1;
  3742. }
  3743. #endif
  3744. }
  3745. /* Set wolfCrypt RSA key data from external if not already done. */
  3746. if ((ret == 0) && (!rsa->inSet) && (SetRsaInternal(rsa) != 1)) {
  3747. ret = -1;
  3748. }
  3749. if (ret == 0) {
  3750. /* Calculate maximum length of encrypted data. */
  3751. outLen = wolfSSL_RSA_size(rsa);
  3752. if (outLen == 0) {
  3753. WOLFSSL_ERROR_MSG("Bad RSA size");
  3754. ret = -1;
  3755. }
  3756. }
  3757. if (ret == 0) {
  3758. /* Get an RNG. */
  3759. rng = WOLFSSL_RSA_GetRNG(rsa, (WC_RNG**)&tmpRng, &initTmpRng);
  3760. if (rng == NULL) {
  3761. ret = -1;
  3762. }
  3763. }
  3764. if (ret == 0) {
  3765. /* Use wolfCrypt to public-encrypt with RSA key. */
  3766. #if !defined(HAVE_FIPS)
  3767. ret = wc_RsaPublicEncrypt_ex(from, (word32)len, to, (word32)outLen,
  3768. (RsaKey*)rsa->internal, rng, pad_type, hash, mgf, NULL, 0);
  3769. #else
  3770. ret = wc_RsaPublicEncrypt(from, (word32)len, to, (word32)outLen,
  3771. (RsaKey*)rsa->internal, rng);
  3772. #endif
  3773. }
  3774. /* Finalize RNG if initialized in WOLFSSL_RSA_GetRNG(). */
  3775. if (initTmpRng) {
  3776. wc_FreeRng(tmpRng);
  3777. }
  3778. #ifdef WOLFSSL_SMALL_STACK
  3779. /* Dispose of any allocated RNG. */
  3780. XFREE(tmpRng, NULL, DYNAMIC_TYPE_RNG);
  3781. #endif
  3782. /* wolfCrypt error means return -1. */
  3783. if (ret <= 0) {
  3784. ret = -1;
  3785. }
  3786. WOLFSSL_LEAVE("wolfSSL_RSA_public_encrypt", ret);
  3787. return ret;
  3788. }
  3789. /* Decrypt with the RSA public key.
  3790. *
  3791. * Return compliant with OpenSSL.
  3792. *
  3793. * @param [in] len Length of encrypted data.
  3794. * @param [in] from Encrypted data.
  3795. * @param [out] to Decrypted data.
  3796. * @param [in] rsa RSA key.
  3797. * @param [in] padding Type of padding to around plaintext to remove.
  3798. * @return Size of decrypted data on success.
  3799. * @return -1 on failure.
  3800. */
  3801. int wolfSSL_RSA_private_decrypt(int len, const unsigned char* from,
  3802. unsigned char* to, WOLFSSL_RSA* rsa, int padding)
  3803. {
  3804. int ret = 0;
  3805. #if !defined(HAVE_FIPS)
  3806. int mgf = WC_MGF1NONE;
  3807. enum wc_HashType hash = WC_HASH_TYPE_NONE;
  3808. int pad_type = WC_RSA_NO_PAD;
  3809. #endif
  3810. int outLen = 0;
  3811. WOLFSSL_ENTER("wolfSSL_RSA_private_decrypt");
  3812. /* Validate parameters. */
  3813. if ((len < 0) || (rsa == NULL) || (rsa->internal == NULL) ||
  3814. (from == NULL)) {
  3815. WOLFSSL_ERROR_MSG("Bad function arguments");
  3816. ret = -1;
  3817. }
  3818. if (ret == 0) {
  3819. #if !defined(HAVE_FIPS)
  3820. switch (padding) {
  3821. case RSA_PKCS1_PADDING:
  3822. pad_type = WC_RSA_PKCSV15_PAD;
  3823. break;
  3824. case RSA_PKCS1_OAEP_PADDING:
  3825. pad_type = WC_RSA_OAEP_PAD;
  3826. hash = WC_HASH_TYPE_SHA;
  3827. mgf = WC_MGF1SHA1;
  3828. break;
  3829. case RSA_NO_PADDING:
  3830. pad_type = WC_RSA_NO_PAD;
  3831. break;
  3832. default:
  3833. WOLFSSL_ERROR_MSG("RSA_private_decrypt unsupported padding");
  3834. ret = -1;
  3835. }
  3836. #else
  3837. /* Check for supported padding schemes in FIPS. */
  3838. /* TODO: Do we support more schemes in later versions of FIPS? */
  3839. if (padding != RSA_PKCS1_PADDING) {
  3840. WOLFSSL_ERROR_MSG("RSA_public_encrypt pad type not supported in "
  3841. "FIPS");
  3842. ret = -1;
  3843. }
  3844. #endif
  3845. }
  3846. /* Set wolfCrypt RSA key data from external if not already done. */
  3847. if ((ret == 0) && (!rsa->inSet) && (SetRsaInternal(rsa) != 1)) {
  3848. ret = -1;
  3849. }
  3850. if (ret == 0) {
  3851. /* Calculate maximum length of decrypted data. */
  3852. outLen = wolfSSL_RSA_size(rsa);
  3853. if (outLen == 0) {
  3854. WOLFSSL_ERROR_MSG("Bad RSA size");
  3855. ret = -1;
  3856. }
  3857. }
  3858. if (ret == 0) {
  3859. /* Use wolfCrypt to private-decrypt with RSA key.
  3860. * Size of 'to' buffer must be size of RSA key */
  3861. #if !defined(HAVE_FIPS)
  3862. ret = wc_RsaPrivateDecrypt_ex(from, (word32)len, to, (word32)outLen,
  3863. (RsaKey*)rsa->internal, pad_type, hash, mgf, NULL, 0);
  3864. #else
  3865. ret = wc_RsaPrivateDecrypt(from, (word32)len, to, (word32)outLen,
  3866. (RsaKey*)rsa->internal);
  3867. #endif
  3868. }
  3869. /* wolfCrypt error means return -1. */
  3870. if (ret <= 0) {
  3871. ret = -1;
  3872. }
  3873. WOLFSSL_LEAVE("wolfSSL_RSA_private_decrypt", ret);
  3874. return ret;
  3875. }
  3876. /* Decrypt with the RSA public key.
  3877. *
  3878. * @param [in] len Length of encrypted data.
  3879. * @param [in] from Encrypted data.
  3880. * @param [out] to Decrypted data.
  3881. * @param [in] rsa RSA key.
  3882. * @param [in] padding Type of padding to around plaintext to remove.
  3883. * @return Size of decrypted data on success.
  3884. * @return -1 on failure.
  3885. */
  3886. int wolfSSL_RSA_public_decrypt(int len, const unsigned char* from,
  3887. unsigned char* to, WOLFSSL_RSA* rsa, int padding)
  3888. {
  3889. int ret = 0;
  3890. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  3891. int pad_type = WC_RSA_NO_PAD;
  3892. #endif
  3893. int outLen = 0;
  3894. WOLFSSL_ENTER("wolfSSL_RSA_public_decrypt");
  3895. /* Validate parameters. */
  3896. if ((len < 0) || (rsa == NULL) || (rsa->internal == NULL) ||
  3897. (from == NULL)) {
  3898. WOLFSSL_ERROR_MSG("Bad function arguments");
  3899. ret = -1;
  3900. }
  3901. if (ret == 0) {
  3902. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  3903. switch (padding) {
  3904. case RSA_PKCS1_PADDING:
  3905. pad_type = WC_RSA_PKCSV15_PAD;
  3906. break;
  3907. case RSA_NO_PADDING:
  3908. pad_type = WC_RSA_NO_PAD;
  3909. break;
  3910. /* TODO: RSA_X931_PADDING not supported */
  3911. default:
  3912. WOLFSSL_ERROR_MSG("RSA_public_decrypt unsupported padding");
  3913. ret = -1;
  3914. }
  3915. #else
  3916. if (padding != RSA_PKCS1_PADDING) {
  3917. WOLFSSL_ERROR_MSG("RSA_public_decrypt pad type not supported in "
  3918. "FIPS");
  3919. ret = -1;
  3920. }
  3921. #endif
  3922. }
  3923. /* Set wolfCrypt RSA key data from external if not already done. */
  3924. if ((ret == 0) && (!rsa->inSet) && (SetRsaInternal(rsa) != 1)) {
  3925. ret = -1;
  3926. }
  3927. if (ret == 0) {
  3928. /* Calculate maximum length of encrypted data. */
  3929. outLen = wolfSSL_RSA_size(rsa);
  3930. if (outLen == 0) {
  3931. WOLFSSL_ERROR_MSG("Bad RSA size");
  3932. ret = -1;
  3933. }
  3934. }
  3935. if (ret == 0) {
  3936. /* Use wolfCrypt to public-decrypt with RSA key. */
  3937. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  3938. /* Size of 'to' buffer must be size of RSA key. */
  3939. ret = wc_RsaSSL_Verify_ex(from, (word32)len, to, (word32)outLen,
  3940. (RsaKey*)rsa->internal, pad_type);
  3941. #else
  3942. /* For FIPS v1/v2 only PKCSV15 padding is supported */
  3943. ret = wc_RsaSSL_Verify(from, (word32)len, to, (word32)outLen,
  3944. (RsaKey*)rsa->internal);
  3945. #endif
  3946. }
  3947. /* wolfCrypt error means return -1. */
  3948. if (ret <= 0) {
  3949. ret = -1;
  3950. }
  3951. WOLFSSL_LEAVE("wolfSSL_RSA_public_decrypt", ret);
  3952. return ret;
  3953. }
  3954. /* Encrypt with the RSA private key.
  3955. *
  3956. * Calls wc_RsaSSL_Sign.
  3957. *
  3958. * @param [in] len Length of data to encrypt.
  3959. * @param [in] from Data to encrypt.
  3960. * @param [out] to Encrypted data.
  3961. * @param [in] rsa RSA key.
  3962. * @param [in] padding Type of padding to place around plaintext.
  3963. * @return Size of encrypted data on success.
  3964. * @return -1 on failure.
  3965. */
  3966. int wolfSSL_RSA_private_encrypt(int len, const unsigned char* from,
  3967. unsigned char* to, WOLFSSL_RSA* rsa, int padding)
  3968. {
  3969. int ret = 0;
  3970. int initTmpRng = 0;
  3971. WC_RNG *rng = NULL;
  3972. #ifdef WOLFSSL_SMALL_STACK
  3973. WC_RNG* tmpRng = NULL;
  3974. #else
  3975. WC_RNG _tmpRng[1];
  3976. WC_RNG* tmpRng = _tmpRng;
  3977. #endif
  3978. WOLFSSL_ENTER("wolfSSL_RSA_private_encrypt");
  3979. /* Validate parameters. */
  3980. if ((len < 0) || (rsa == NULL) || (rsa->internal == NULL) ||
  3981. (from == NULL)) {
  3982. WOLFSSL_ERROR_MSG("Bad function arguments");
  3983. ret = -1;
  3984. }
  3985. if (ret == 0) {
  3986. switch (padding) {
  3987. case RSA_PKCS1_PADDING:
  3988. #ifdef WC_RSA_NO_PADDING
  3989. case RSA_NO_PADDING:
  3990. #endif
  3991. break;
  3992. /* TODO: RSA_X931_PADDING not supported */
  3993. default:
  3994. WOLFSSL_ERROR_MSG("RSA_private_encrypt unsupported padding");
  3995. ret = -1;
  3996. }
  3997. }
  3998. /* Set wolfCrypt RSA key data from external if not already done. */
  3999. if ((ret == 0) && (!rsa->inSet) && (SetRsaInternal(rsa) != 1)) {
  4000. ret = -1;
  4001. }
  4002. if (ret == 0) {
  4003. /* Get an RNG. */
  4004. rng = WOLFSSL_RSA_GetRNG(rsa, (WC_RNG**)&tmpRng, &initTmpRng);
  4005. if (rng == NULL) {
  4006. ret = -1;
  4007. }
  4008. }
  4009. if (ret == 0) {
  4010. /* Use wolfCrypt to private-encrypt with RSA key.
  4011. * Size of output buffer must be size of RSA key. */
  4012. if (padding == RSA_PKCS1_PADDING) {
  4013. ret = wc_RsaSSL_Sign(from, (word32)len, to,
  4014. (word32)wolfSSL_RSA_size(rsa), (RsaKey*)rsa->internal, rng);
  4015. }
  4016. #ifdef WC_RSA_NO_PADDING
  4017. else if (padding == RSA_NO_PADDING) {
  4018. word32 outLen = (word32)wolfSSL_RSA_size(rsa);
  4019. ret = wc_RsaFunction(from, (word32)len, to, &outLen,
  4020. RSA_PRIVATE_ENCRYPT, (RsaKey*)rsa->internal, rng);
  4021. if (ret == 0)
  4022. ret = (int)outLen;
  4023. }
  4024. #endif
  4025. }
  4026. /* Finalize RNG if initialized in WOLFSSL_RSA_GetRNG(). */
  4027. if (initTmpRng) {
  4028. wc_FreeRng(tmpRng);
  4029. }
  4030. #ifdef WOLFSSL_SMALL_STACK
  4031. /* Dispose of any allocated RNG. */
  4032. XFREE(tmpRng, NULL, DYNAMIC_TYPE_RNG);
  4033. #endif
  4034. /* wolfCrypt error means return -1. */
  4035. if (ret <= 0) {
  4036. ret = -1;
  4037. }
  4038. WOLFSSL_LEAVE("wolfSSL_RSA_private_encrypt", ret);
  4039. return ret;
  4040. }
  4041. #endif /* !HAVE_USER_RSA && !HAVE_FAST_RSA */
  4042. /*
  4043. * RSA misc operation APIs
  4044. */
  4045. /* Calculate d mod p-1 and q-1 into BNs.
  4046. *
  4047. * Not OpenSSL API.
  4048. *
  4049. * @param [in, out] rsa RSA key.
  4050. * @return 1 on success.
  4051. * @return -1 on failure.
  4052. */
  4053. int wolfSSL_RSA_GenAdd(WOLFSSL_RSA* rsa)
  4054. {
  4055. int ret = 1;
  4056. int err;
  4057. mp_int* t = NULL;
  4058. #ifdef WOLFSSL_SMALL_STACK
  4059. mp_int *tmp = NULL;
  4060. #else
  4061. mp_int tmp[1];
  4062. #endif
  4063. WOLFSSL_ENTER("wolfSSL_RsaGenAdd");
  4064. /* Validate parameters. */
  4065. if ((rsa == NULL) || (rsa->p == NULL) || (rsa->q == NULL) ||
  4066. (rsa->d == NULL) || (rsa->dmp1 == NULL) || (rsa->dmq1 == NULL)) {
  4067. WOLFSSL_ERROR_MSG("rsa no init error");
  4068. ret = -1;
  4069. }
  4070. #ifdef WOLFSSL_SMALL_STACK
  4071. if (ret == 1) {
  4072. tmp = (mp_int *)XMALLOC(sizeof(*tmp), rsa->heap,
  4073. DYNAMIC_TYPE_TMP_BUFFER);
  4074. if (tmp == NULL) {
  4075. WOLFSSL_ERROR_MSG("Memory allocation failure");
  4076. ret = -1;
  4077. }
  4078. }
  4079. #endif
  4080. if (ret == 1) {
  4081. /* Initialize temp MP integer. */
  4082. if (mp_init(tmp) != MP_OKAY) {
  4083. WOLFSSL_ERROR_MSG("mp_init error");
  4084. ret = -1;
  4085. }
  4086. }
  4087. if (ret == 1) {
  4088. t = tmp;
  4089. /* Sub 1 from p into temp. */
  4090. err = mp_sub_d((mp_int*)rsa->p->internal, 1, tmp);
  4091. if (err != MP_OKAY) {
  4092. WOLFSSL_ERROR_MSG("mp_sub_d error");
  4093. ret = -1;
  4094. }
  4095. }
  4096. if (ret == 1) {
  4097. /* Calculate d mod (p - 1) into dmp1 MP integer of BN. */
  4098. err = mp_mod((mp_int*)rsa->d->internal, tmp,
  4099. (mp_int*)rsa->dmp1->internal);
  4100. if (err != MP_OKAY) {
  4101. WOLFSSL_ERROR_MSG("mp_mod error");
  4102. ret = -1;
  4103. }
  4104. }
  4105. if (ret == 1) {
  4106. /* Sub 1 from q into temp. */
  4107. err = mp_sub_d((mp_int*)rsa->q->internal, 1, tmp);
  4108. if (err != MP_OKAY) {
  4109. WOLFSSL_ERROR_MSG("mp_sub_d error");
  4110. ret = -1;
  4111. }
  4112. }
  4113. if (ret == 1) {
  4114. /* Calculate d mod (q - 1) into dmq1 MP integer of BN. */
  4115. err = mp_mod((mp_int*)rsa->d->internal, tmp,
  4116. (mp_int*)rsa->dmq1->internal);
  4117. if (err != MP_OKAY) {
  4118. WOLFSSL_ERROR_MSG("mp_mod error");
  4119. ret = -1;
  4120. }
  4121. }
  4122. mp_clear(t);
  4123. #ifdef WOLFSSL_SMALL_STACK
  4124. if (tmp != NULL)
  4125. XFREE(tmp, rsa->heap, DYNAMIC_TYPE_TMP_BUFFER);
  4126. #endif
  4127. return ret;
  4128. }
  4129. #endif /* !HAVE_USER_RSA */
  4130. #ifndef NO_WOLFSSL_STUB
  4131. /* Enable blinding for RSA key operations.
  4132. *
  4133. * Blinding is a compile time option in wolfCrypt.
  4134. *
  4135. * @param [in] rsa RSA key. Unused.
  4136. * @param [in] bnCtx BN context to use for blinding. Unused.
  4137. * @return 1 always.
  4138. */
  4139. int wolfSSL_RSA_blinding_on(WOLFSSL_RSA* rsa, WOLFSSL_BN_CTX* bnCtx)
  4140. {
  4141. WOLFSSL_STUB("RSA_blinding_on");
  4142. WOLFSSL_ENTER("wolfSSL_RSA_blinding_on");
  4143. (void)rsa;
  4144. (void)bnCtx;
  4145. return 1; /* on by default */
  4146. }
  4147. #endif
  4148. #endif /* OPENSSL_EXTRA */
  4149. #endif /* !NO_RSA */
  4150. /*******************************************************************************
  4151. * END OF RSA API
  4152. ******************************************************************************/
  4153. /*******************************************************************************
  4154. * START OF DSA API
  4155. ******************************************************************************/
  4156. #ifndef NO_DSA
  4157. #if defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  4158. !defined(NO_STDIO_FILESYSTEM)
  4159. /* return code compliant with OpenSSL :
  4160. * 1 if success, 0 if error
  4161. */
  4162. int wolfSSL_DSA_print_fp(XFILE fp, WOLFSSL_DSA* dsa, int indent)
  4163. {
  4164. int ret = 1;
  4165. WOLFSSL_ENTER("wolfSSL_DSA_print_fp");
  4166. if (fp == XBADFILE || dsa == NULL) {
  4167. ret = 0;
  4168. }
  4169. if (ret == 1 && dsa->p != NULL) {
  4170. int pBits = wolfSSL_BN_num_bits(dsa->p);
  4171. if (pBits == 0) {
  4172. ret = 0;
  4173. }
  4174. else {
  4175. if (XFPRINTF(fp, "%*s", indent, "") < 0)
  4176. ret = 0;
  4177. else if (XFPRINTF(fp, "Private-Key: (%d bit)\n", pBits) < 0)
  4178. ret = 0;
  4179. }
  4180. }
  4181. if (ret == 1 && dsa->priv_key != NULL) {
  4182. ret = pk_bn_field_print_fp(fp, indent, "priv", dsa->priv_key);
  4183. }
  4184. if (ret == 1 && dsa->pub_key != NULL) {
  4185. ret = pk_bn_field_print_fp(fp, indent, "pub", dsa->pub_key);
  4186. }
  4187. if (ret == 1 && dsa->p != NULL) {
  4188. ret = pk_bn_field_print_fp(fp, indent, "P", dsa->p);
  4189. }
  4190. if (ret == 1 && dsa->q != NULL) {
  4191. ret = pk_bn_field_print_fp(fp, indent, "Q", dsa->q);
  4192. }
  4193. if (ret == 1 && dsa->g != NULL) {
  4194. ret = pk_bn_field_print_fp(fp, indent, "G", dsa->g);
  4195. }
  4196. WOLFSSL_LEAVE("wolfSSL_DSA_print_fp", ret);
  4197. return ret;
  4198. }
  4199. #endif /* OPENSSL_EXTRA && XSNPRINTF && !NO_FILESYSTEM && NO_STDIO_FILESYSTEM */
  4200. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  4201. static void InitwolfSSL_DSA(WOLFSSL_DSA* dsa)
  4202. {
  4203. if (dsa) {
  4204. dsa->p = NULL;
  4205. dsa->q = NULL;
  4206. dsa->g = NULL;
  4207. dsa->pub_key = NULL;
  4208. dsa->priv_key = NULL;
  4209. dsa->internal = NULL;
  4210. dsa->inSet = 0;
  4211. dsa->exSet = 0;
  4212. }
  4213. }
  4214. WOLFSSL_DSA* wolfSSL_DSA_new(void)
  4215. {
  4216. WOLFSSL_DSA* external;
  4217. DsaKey* key;
  4218. WOLFSSL_MSG("wolfSSL_DSA_new");
  4219. key = (DsaKey*) XMALLOC(sizeof(DsaKey), NULL, DYNAMIC_TYPE_DSA);
  4220. if (key == NULL) {
  4221. WOLFSSL_MSG("wolfSSL_DSA_new malloc DsaKey failure");
  4222. return NULL;
  4223. }
  4224. external = (WOLFSSL_DSA*) XMALLOC(sizeof(WOLFSSL_DSA), NULL,
  4225. DYNAMIC_TYPE_DSA);
  4226. if (external == NULL) {
  4227. WOLFSSL_MSG("wolfSSL_DSA_new malloc WOLFSSL_DSA failure");
  4228. XFREE(key, NULL, DYNAMIC_TYPE_DSA);
  4229. return NULL;
  4230. }
  4231. InitwolfSSL_DSA(external);
  4232. if (wc_InitDsaKey(key) != 0) {
  4233. WOLFSSL_MSG("wolfSSL_DSA_new InitDsaKey failure");
  4234. XFREE(key, NULL, DYNAMIC_TYPE_DSA);
  4235. wolfSSL_DSA_free(external);
  4236. return NULL;
  4237. }
  4238. external->internal = key;
  4239. return external;
  4240. }
  4241. void wolfSSL_DSA_free(WOLFSSL_DSA* dsa)
  4242. {
  4243. WOLFSSL_MSG("wolfSSL_DSA_free");
  4244. if (dsa) {
  4245. if (dsa->internal) {
  4246. FreeDsaKey((DsaKey*)dsa->internal);
  4247. XFREE(dsa->internal, NULL, DYNAMIC_TYPE_DSA);
  4248. dsa->internal = NULL;
  4249. }
  4250. wolfSSL_BN_free(dsa->priv_key);
  4251. wolfSSL_BN_free(dsa->pub_key);
  4252. wolfSSL_BN_free(dsa->g);
  4253. wolfSSL_BN_free(dsa->q);
  4254. wolfSSL_BN_free(dsa->p);
  4255. InitwolfSSL_DSA(dsa); /* set back to NULLs for safety */
  4256. XFREE(dsa, NULL, DYNAMIC_TYPE_DSA);
  4257. /* dsa = NULL, don't try to access or double free it */
  4258. }
  4259. }
  4260. /* wolfSSL -> OpenSSL */
  4261. int SetDsaExternal(WOLFSSL_DSA* dsa)
  4262. {
  4263. DsaKey* key;
  4264. WOLFSSL_MSG("Entering SetDsaExternal");
  4265. if (dsa == NULL || dsa->internal == NULL) {
  4266. WOLFSSL_MSG("dsa key NULL error");
  4267. return -1;
  4268. }
  4269. key = (DsaKey*)dsa->internal;
  4270. if (wolfssl_bn_set_value(&dsa->p, &key->p) != 1) {
  4271. WOLFSSL_MSG("dsa p key error");
  4272. return -1;
  4273. }
  4274. if (wolfssl_bn_set_value(&dsa->q, &key->q) != 1) {
  4275. WOLFSSL_MSG("dsa q key error");
  4276. return -1;
  4277. }
  4278. if (wolfssl_bn_set_value(&dsa->g, &key->g) != 1) {
  4279. WOLFSSL_MSG("dsa g key error");
  4280. return -1;
  4281. }
  4282. if (wolfssl_bn_set_value(&dsa->pub_key, &key->y) != 1) {
  4283. WOLFSSL_MSG("dsa y key error");
  4284. return -1;
  4285. }
  4286. if (wolfssl_bn_set_value(&dsa->priv_key, &key->x) != 1) {
  4287. WOLFSSL_MSG("dsa x key error");
  4288. return -1;
  4289. }
  4290. dsa->exSet = 1;
  4291. return 1;
  4292. }
  4293. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  4294. #ifdef OPENSSL_EXTRA
  4295. /* Openssl -> WolfSSL */
  4296. int SetDsaInternal(WOLFSSL_DSA* dsa)
  4297. {
  4298. DsaKey* key;
  4299. WOLFSSL_MSG("Entering SetDsaInternal");
  4300. if (dsa == NULL || dsa->internal == NULL) {
  4301. WOLFSSL_MSG("dsa key NULL error");
  4302. return -1;
  4303. }
  4304. key = (DsaKey*)dsa->internal;
  4305. if (dsa->p != NULL &&
  4306. wolfssl_bn_get_value(dsa->p, &key->p) != 1) {
  4307. WOLFSSL_MSG("rsa p key error");
  4308. return -1;
  4309. }
  4310. if (dsa->q != NULL &&
  4311. wolfssl_bn_get_value(dsa->q, &key->q) != 1) {
  4312. WOLFSSL_MSG("rsa q key error");
  4313. return -1;
  4314. }
  4315. if (dsa->g != NULL &&
  4316. wolfssl_bn_get_value(dsa->g, &key->g) != 1) {
  4317. WOLFSSL_MSG("rsa g key error");
  4318. return -1;
  4319. }
  4320. if (dsa->pub_key != NULL) {
  4321. if (wolfssl_bn_get_value(dsa->pub_key, &key->y) != 1) {
  4322. WOLFSSL_MSG("rsa pub_key error");
  4323. return -1;
  4324. }
  4325. /* public key */
  4326. key->type = DSA_PUBLIC;
  4327. }
  4328. if (dsa->priv_key != NULL) {
  4329. if (wolfssl_bn_get_value(dsa->priv_key, &key->x) != 1) {
  4330. WOLFSSL_MSG("rsa priv_key error");
  4331. return -1;
  4332. }
  4333. /* private key */
  4334. key->type = DSA_PRIVATE;
  4335. }
  4336. dsa->inSet = 1;
  4337. return 1;
  4338. }
  4339. /* return code compliant with OpenSSL :
  4340. * 1 if success, 0 if error
  4341. */
  4342. int wolfSSL_DSA_generate_key(WOLFSSL_DSA* dsa)
  4343. {
  4344. int ret = 0;
  4345. WOLFSSL_ENTER("wolfSSL_DSA_generate_key");
  4346. if (dsa == NULL || dsa->internal == NULL) {
  4347. WOLFSSL_MSG("Bad arguments");
  4348. return 0;
  4349. }
  4350. if (dsa->inSet == 0) {
  4351. WOLFSSL_MSG("No DSA internal set, do it");
  4352. if (SetDsaInternal(dsa) != 1) {
  4353. WOLFSSL_MSG("SetDsaInternal failed");
  4354. return ret;
  4355. }
  4356. }
  4357. #ifdef WOLFSSL_KEY_GEN
  4358. {
  4359. int initTmpRng = 0;
  4360. WC_RNG *rng = NULL;
  4361. #ifdef WOLFSSL_SMALL_STACK
  4362. WC_RNG *tmpRng;
  4363. #else
  4364. WC_RNG tmpRng[1];
  4365. #endif
  4366. #ifdef WOLFSSL_SMALL_STACK
  4367. tmpRng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  4368. if (tmpRng == NULL)
  4369. return -1;
  4370. #endif
  4371. if (wc_InitRng(tmpRng) == 0) {
  4372. rng = tmpRng;
  4373. initTmpRng = 1;
  4374. }
  4375. else {
  4376. WOLFSSL_MSG("Bad RNG Init, trying global");
  4377. rng = wolfssl_get_global_rng();
  4378. }
  4379. if (rng) {
  4380. /* These were allocated above by SetDsaInternal(). They should
  4381. * be cleared before wc_MakeDsaKey() which reinitializes
  4382. * x and y. */
  4383. mp_clear(&((DsaKey*)dsa->internal)->x);
  4384. mp_clear(&((DsaKey*)dsa->internal)->y);
  4385. if (wc_MakeDsaKey(rng, (DsaKey*)dsa->internal) != MP_OKAY)
  4386. WOLFSSL_MSG("wc_MakeDsaKey failed");
  4387. else if (SetDsaExternal(dsa) != 1)
  4388. WOLFSSL_MSG("SetDsaExternal failed");
  4389. else
  4390. ret = 1;
  4391. }
  4392. if (initTmpRng)
  4393. wc_FreeRng(tmpRng);
  4394. #ifdef WOLFSSL_SMALL_STACK
  4395. XFREE(tmpRng, NULL, DYNAMIC_TYPE_RNG);
  4396. #endif
  4397. }
  4398. #else /* WOLFSSL_KEY_GEN */
  4399. WOLFSSL_MSG("No Key Gen built in");
  4400. #endif
  4401. return ret;
  4402. }
  4403. /* Returns a pointer to a new WOLFSSL_DSA structure on success and NULL on fail
  4404. */
  4405. WOLFSSL_DSA* wolfSSL_DSA_generate_parameters(int bits, unsigned char* seed,
  4406. int seedLen, int* counterRet, unsigned long* hRet,
  4407. WOLFSSL_BN_CB cb, void* CBArg)
  4408. {
  4409. WOLFSSL_DSA* dsa;
  4410. WOLFSSL_ENTER("wolfSSL_DSA_generate_parameters");
  4411. (void)cb;
  4412. (void)CBArg;
  4413. dsa = wolfSSL_DSA_new();
  4414. if (dsa == NULL) {
  4415. return NULL;
  4416. }
  4417. if (wolfSSL_DSA_generate_parameters_ex(dsa, bits, seed, seedLen,
  4418. counterRet, hRet, NULL) != 1) {
  4419. wolfSSL_DSA_free(dsa);
  4420. return NULL;
  4421. }
  4422. return dsa;
  4423. }
  4424. /* return code compliant with OpenSSL :
  4425. * 1 if success, 0 if error
  4426. */
  4427. int wolfSSL_DSA_generate_parameters_ex(WOLFSSL_DSA* dsa, int bits,
  4428. unsigned char* seed, int seedLen,
  4429. int* counterRet,
  4430. unsigned long* hRet, void* cb)
  4431. {
  4432. int ret = 0;
  4433. (void)bits;
  4434. (void)seed;
  4435. (void)seedLen;
  4436. (void)counterRet;
  4437. (void)hRet;
  4438. (void)cb;
  4439. WOLFSSL_ENTER("wolfSSL_DSA_generate_parameters_ex");
  4440. if (dsa == NULL || dsa->internal == NULL) {
  4441. WOLFSSL_MSG("Bad arguments");
  4442. return 0;
  4443. }
  4444. #ifdef WOLFSSL_KEY_GEN
  4445. {
  4446. int initTmpRng = 0;
  4447. WC_RNG *rng = NULL;
  4448. #ifdef WOLFSSL_SMALL_STACK
  4449. WC_RNG *tmpRng;
  4450. #else
  4451. WC_RNG tmpRng[1];
  4452. #endif
  4453. #ifdef WOLFSSL_SMALL_STACK
  4454. tmpRng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  4455. if (tmpRng == NULL)
  4456. return -1;
  4457. #endif
  4458. if (wc_InitRng(tmpRng) == 0) {
  4459. rng = tmpRng;
  4460. initTmpRng = 1;
  4461. }
  4462. else {
  4463. WOLFSSL_MSG("Bad RNG Init, trying global");
  4464. rng = wolfssl_get_global_rng();
  4465. }
  4466. if (rng) {
  4467. if (wc_MakeDsaParameters(rng, bits,
  4468. (DsaKey*)dsa->internal) != MP_OKAY)
  4469. WOLFSSL_MSG("wc_MakeDsaParameters failed");
  4470. else if (SetDsaExternal(dsa) != 1)
  4471. WOLFSSL_MSG("SetDsaExternal failed");
  4472. else
  4473. ret = 1;
  4474. }
  4475. if (initTmpRng)
  4476. wc_FreeRng(tmpRng);
  4477. #ifdef WOLFSSL_SMALL_STACK
  4478. XFREE(tmpRng, NULL, DYNAMIC_TYPE_RNG);
  4479. #endif
  4480. }
  4481. #else /* WOLFSSL_KEY_GEN */
  4482. WOLFSSL_MSG("No Key Gen built in");
  4483. #endif
  4484. return ret;
  4485. }
  4486. void wolfSSL_DSA_get0_pqg(const WOLFSSL_DSA *d, const WOLFSSL_BIGNUM **p,
  4487. const WOLFSSL_BIGNUM **q, const WOLFSSL_BIGNUM **g)
  4488. {
  4489. WOLFSSL_ENTER("wolfSSL_DSA_get0_pqg");
  4490. if (d != NULL) {
  4491. if (p != NULL)
  4492. *p = d->p;
  4493. if (q != NULL)
  4494. *q = d->q;
  4495. if (g != NULL)
  4496. *g = d->g;
  4497. }
  4498. }
  4499. int wolfSSL_DSA_set0_pqg(WOLFSSL_DSA *d, WOLFSSL_BIGNUM *p,
  4500. WOLFSSL_BIGNUM *q, WOLFSSL_BIGNUM *g)
  4501. {
  4502. WOLFSSL_ENTER("wolfSSL_DSA_set0_pqg");
  4503. if (d == NULL || p == NULL || q == NULL || g == NULL) {
  4504. WOLFSSL_MSG("Bad parameter");
  4505. return 0;
  4506. }
  4507. wolfSSL_BN_free(d->p);
  4508. wolfSSL_BN_free(d->q);
  4509. wolfSSL_BN_free(d->g);
  4510. d->p = p;
  4511. d->q = q;
  4512. d->g = g;
  4513. return 1;
  4514. }
  4515. void wolfSSL_DSA_get0_key(const WOLFSSL_DSA *d,
  4516. const WOLFSSL_BIGNUM **pub_key, const WOLFSSL_BIGNUM **priv_key)
  4517. {
  4518. WOLFSSL_ENTER("wolfSSL_DSA_get0_key");
  4519. if (d != NULL) {
  4520. if (pub_key != NULL)
  4521. *pub_key = d->pub_key;
  4522. if (priv_key != NULL)
  4523. *priv_key = d->priv_key;
  4524. }
  4525. }
  4526. int wolfSSL_DSA_set0_key(WOLFSSL_DSA *d, WOLFSSL_BIGNUM *pub_key,
  4527. WOLFSSL_BIGNUM *priv_key)
  4528. {
  4529. WOLFSSL_ENTER("wolfSSL_DSA_set0_key");
  4530. /* The private key may be NULL */
  4531. if (pub_key == NULL) {
  4532. WOLFSSL_MSG("Bad parameter");
  4533. return 0;
  4534. }
  4535. wolfSSL_BN_free(d->pub_key);
  4536. wolfSSL_BN_free(d->priv_key);
  4537. d->pub_key = pub_key;
  4538. d->priv_key = priv_key;
  4539. return 1;
  4540. }
  4541. WOLFSSL_DSA_SIG* wolfSSL_DSA_SIG_new(void)
  4542. {
  4543. WOLFSSL_DSA_SIG* sig;
  4544. WOLFSSL_ENTER("wolfSSL_DSA_SIG_new");
  4545. sig = (WOLFSSL_DSA_SIG*)XMALLOC(sizeof(WOLFSSL_DSA_SIG), NULL,
  4546. DYNAMIC_TYPE_OPENSSL);
  4547. if (sig)
  4548. XMEMSET(sig, 0, sizeof(WOLFSSL_DSA_SIG));
  4549. return sig;
  4550. }
  4551. void wolfSSL_DSA_SIG_free(WOLFSSL_DSA_SIG *sig)
  4552. {
  4553. WOLFSSL_ENTER("wolfSSL_DSA_SIG_free");
  4554. if (sig) {
  4555. if (sig->r) {
  4556. wolfSSL_BN_free(sig->r);
  4557. }
  4558. if (sig->s) {
  4559. wolfSSL_BN_free(sig->s);
  4560. }
  4561. XFREE(sig, NULL, DYNAMIC_TYPE_OPENSSL);
  4562. }
  4563. }
  4564. void wolfSSL_DSA_SIG_get0(const WOLFSSL_DSA_SIG *sig,
  4565. const WOLFSSL_BIGNUM **r, const WOLFSSL_BIGNUM **s)
  4566. {
  4567. WOLFSSL_ENTER("wolfSSL_DSA_SIG_get0");
  4568. if (sig != NULL) {
  4569. *r = sig->r;
  4570. *s = sig->s;
  4571. }
  4572. }
  4573. int wolfSSL_DSA_SIG_set0(WOLFSSL_DSA_SIG *sig, WOLFSSL_BIGNUM *r,
  4574. WOLFSSL_BIGNUM *s)
  4575. {
  4576. WOLFSSL_ENTER("wolfSSL_DSA_SIG_set0");
  4577. if (r == NULL || s == NULL) {
  4578. WOLFSSL_MSG("Bad parameter");
  4579. return 0;
  4580. }
  4581. wolfSSL_BN_clear_free(sig->r);
  4582. wolfSSL_BN_clear_free(sig->s);
  4583. sig->r = r;
  4584. sig->s = s;
  4585. return 1;
  4586. }
  4587. #ifndef HAVE_SELFTEST
  4588. /**
  4589. *
  4590. * @param sig The input signature to encode
  4591. * @param out The output buffer. If *out is NULL then a new buffer is
  4592. * allocated. Otherwise the output is written to the buffer.
  4593. * @return length on success and -1 on error
  4594. */
  4595. int wolfSSL_i2d_DSA_SIG(const WOLFSSL_DSA_SIG *sig, byte **out)
  4596. {
  4597. /* Space for sequence + two asn ints */
  4598. byte buf[MAX_SEQ_SZ + 2*(ASN_TAG_SZ + MAX_LENGTH_SZ + DSA_MAX_HALF_SIZE)];
  4599. word32 bufLen = sizeof(buf);
  4600. WOLFSSL_ENTER("wolfSSL_i2d_DSA_SIG");
  4601. if (sig == NULL || sig->r == NULL || sig->s == NULL ||
  4602. out == NULL) {
  4603. WOLFSSL_MSG("Bad function arguments");
  4604. return -1;
  4605. }
  4606. if (StoreECC_DSA_Sig(buf, &bufLen,
  4607. (mp_int*)sig->r->internal, (mp_int*)sig->s->internal) != 0) {
  4608. WOLFSSL_MSG("StoreECC_DSA_Sig error");
  4609. return -1;
  4610. }
  4611. if (*out == NULL) {
  4612. byte* tmp = (byte*)XMALLOC(bufLen, NULL, DYNAMIC_TYPE_ASN1);
  4613. if (tmp == NULL) {
  4614. WOLFSSL_MSG("malloc error");
  4615. return -1;
  4616. }
  4617. *out = tmp;
  4618. }
  4619. XMEMCPY(*out, buf, bufLen);
  4620. return (int)bufLen;
  4621. }
  4622. /**
  4623. * Same as wolfSSL_DSA_SIG_new but also initializes the internal bignums as well.
  4624. * @return New WOLFSSL_DSA_SIG with r and s created as well
  4625. */
  4626. static WOLFSSL_DSA_SIG* wolfSSL_DSA_SIG_new_bn(void)
  4627. {
  4628. WOLFSSL_DSA_SIG* ret;
  4629. if ((ret = wolfSSL_DSA_SIG_new()) == NULL) {
  4630. WOLFSSL_MSG("wolfSSL_DSA_SIG_new error");
  4631. return NULL;
  4632. }
  4633. if ((ret->r = wolfSSL_BN_new()) == NULL) {
  4634. WOLFSSL_MSG("wolfSSL_BN_new error");
  4635. wolfSSL_DSA_SIG_free(ret);
  4636. return NULL;
  4637. }
  4638. if ((ret->s = wolfSSL_BN_new()) == NULL) {
  4639. WOLFSSL_MSG("wolfSSL_BN_new error");
  4640. wolfSSL_DSA_SIG_free(ret);
  4641. return NULL;
  4642. }
  4643. return ret;
  4644. }
  4645. /**
  4646. * This parses a DER encoded ASN.1 structure. The ASN.1 encoding is:
  4647. * ASN1_SEQUENCE
  4648. * ASN1_INTEGER (DSA r)
  4649. * ASN1_INTEGER (DSA s)
  4650. * Alternatively, if the input is DSA_160_SIG_SIZE or DSA_256_SIG_SIZE in
  4651. * length then this API interprets this as two unsigned binary numbers.
  4652. * @param sig If non-null then free'd first and then newly created
  4653. * WOLFSSL_DSA_SIG is assigned
  4654. * @param pp Input buffer that is moved forward on success
  4655. * @param length Length of input buffer
  4656. * @return Newly created WOLFSSL_DSA_SIG on success or NULL on failure
  4657. */
  4658. WOLFSSL_DSA_SIG* wolfSSL_d2i_DSA_SIG(WOLFSSL_DSA_SIG **sig,
  4659. const unsigned char **pp, long length)
  4660. {
  4661. WOLFSSL_DSA_SIG* ret;
  4662. mp_int* r;
  4663. mp_int* s;
  4664. WOLFSSL_ENTER("wolfSSL_d2i_DSA_SIG");
  4665. if (pp == NULL || *pp == NULL || length < 0) {
  4666. WOLFSSL_MSG("Bad function arguments");
  4667. return NULL;
  4668. }
  4669. if ((ret = wolfSSL_DSA_SIG_new_bn()) == NULL) {
  4670. WOLFSSL_MSG("wolfSSL_DSA_SIG_new_bn error");
  4671. return NULL;
  4672. }
  4673. r = (mp_int*)ret->r->internal;
  4674. s = (mp_int*)ret->s->internal;
  4675. if (DecodeECC_DSA_Sig(*pp, (word32)length, r, s) != 0) {
  4676. if (length == DSA_160_SIG_SIZE || length == DSA_256_SIG_SIZE) {
  4677. /* Two raw numbers of length/2 size each */
  4678. if (mp_read_unsigned_bin(r, *pp, (word32)length/2) != 0) {
  4679. WOLFSSL_MSG("r mp_read_unsigned_bin error");
  4680. wolfSSL_DSA_SIG_free(ret);
  4681. return NULL;
  4682. }
  4683. if (mp_read_unsigned_bin(s, *pp + (length/2), (word32)length/2) !=
  4684. 0) {
  4685. WOLFSSL_MSG("s mp_read_unsigned_bin error");
  4686. wolfSSL_DSA_SIG_free(ret);
  4687. return NULL;
  4688. }
  4689. *pp += length;
  4690. }
  4691. else {
  4692. WOLFSSL_MSG("DecodeECC_DSA_Sig error");
  4693. wolfSSL_DSA_SIG_free(ret);
  4694. return NULL;
  4695. }
  4696. }
  4697. else {
  4698. /* DecodeECC_DSA_Sig success move pointer forward */
  4699. #ifndef NO_STRICT_ECDSA_LEN
  4700. *pp += length;
  4701. #else
  4702. {
  4703. /* We need to figure out how much to move by ourselves */
  4704. word32 idx = 0;
  4705. int len = 0;
  4706. if (GetSequence(*pp, &idx, &len, (word32)length) < 0) {
  4707. WOLFSSL_MSG("GetSequence error");
  4708. wolfSSL_DSA_SIG_free(ret);
  4709. return NULL;
  4710. }
  4711. *pp += len;
  4712. }
  4713. #endif
  4714. }
  4715. if (sig != NULL) {
  4716. if (*sig != NULL)
  4717. wolfSSL_DSA_SIG_free(*sig);
  4718. *sig = ret;
  4719. }
  4720. return ret;
  4721. }
  4722. #endif /* HAVE_SELFTEST */
  4723. /* return 1 on success, < 0 otherwise */
  4724. int wolfSSL_DSA_do_sign(const unsigned char* d, unsigned char* sigRet,
  4725. WOLFSSL_DSA* dsa)
  4726. {
  4727. int ret = -1;
  4728. int initTmpRng = 0;
  4729. WC_RNG* rng = NULL;
  4730. #ifdef WOLFSSL_SMALL_STACK
  4731. WC_RNG* tmpRng = NULL;
  4732. #else
  4733. WC_RNG tmpRng[1];
  4734. #endif
  4735. WOLFSSL_ENTER("wolfSSL_DSA_do_sign");
  4736. if (d == NULL || sigRet == NULL || dsa == NULL) {
  4737. WOLFSSL_MSG("Bad function arguments");
  4738. return ret;
  4739. }
  4740. if (dsa->inSet == 0) {
  4741. WOLFSSL_MSG("No DSA internal set, do it");
  4742. if (SetDsaInternal(dsa) != 1) {
  4743. WOLFSSL_MSG("SetDsaInternal failed");
  4744. return ret;
  4745. }
  4746. }
  4747. #ifdef WOLFSSL_SMALL_STACK
  4748. tmpRng = (WC_RNG*)XMALLOC(sizeof(WC_RNG), NULL, DYNAMIC_TYPE_RNG);
  4749. if (tmpRng == NULL)
  4750. return -1;
  4751. #endif
  4752. if (wc_InitRng(tmpRng) == 0) {
  4753. rng = tmpRng;
  4754. initTmpRng = 1;
  4755. }
  4756. else {
  4757. WOLFSSL_MSG("Bad RNG Init, trying global");
  4758. rng = wolfssl_get_global_rng();
  4759. }
  4760. if (rng) {
  4761. if (wc_DsaSign(d, sigRet, (DsaKey*)dsa->internal, rng) < 0)
  4762. WOLFSSL_MSG("DsaSign failed");
  4763. else
  4764. ret = 1;
  4765. }
  4766. if (initTmpRng)
  4767. wc_FreeRng(tmpRng);
  4768. #ifdef WOLFSSL_SMALL_STACK
  4769. XFREE(tmpRng, NULL, DYNAMIC_TYPE_RNG);
  4770. #endif
  4771. return ret;
  4772. }
  4773. #ifndef HAVE_SELFTEST
  4774. WOLFSSL_DSA_SIG* wolfSSL_DSA_do_sign_ex(const unsigned char* digest,
  4775. int inLen, WOLFSSL_DSA* dsa)
  4776. {
  4777. byte sigBin[DSA_MAX_SIG_SIZE];
  4778. const byte *tmp = sigBin;
  4779. int sigLen;
  4780. WOLFSSL_ENTER("wolfSSL_DSA_do_sign_ex");
  4781. if (!digest || !dsa || inLen != WC_SHA_DIGEST_SIZE) {
  4782. WOLFSSL_MSG("Bad function arguments");
  4783. return NULL;
  4784. }
  4785. if (wolfSSL_DSA_do_sign(digest, sigBin, dsa) != 1) {
  4786. WOLFSSL_MSG("wolfSSL_DSA_do_sign error");
  4787. return NULL;
  4788. }
  4789. if (dsa->internal == NULL) {
  4790. WOLFSSL_MSG("dsa->internal is null");
  4791. return NULL;
  4792. }
  4793. sigLen = mp_unsigned_bin_size(&((DsaKey*)dsa->internal)->q);
  4794. if (sigLen <= 0) {
  4795. WOLFSSL_MSG("mp_unsigned_bin_size error");
  4796. return NULL;
  4797. }
  4798. /* 2 * sigLen for the two points r and s */
  4799. return wolfSSL_d2i_DSA_SIG(NULL, &tmp, 2 * sigLen);
  4800. }
  4801. #endif /* !HAVE_SELFTEST */
  4802. int wolfSSL_DSA_do_verify(const unsigned char* d, unsigned char* sig,
  4803. WOLFSSL_DSA* dsa, int *dsacheck)
  4804. {
  4805. int ret = -1;
  4806. WOLFSSL_ENTER("wolfSSL_DSA_do_verify");
  4807. if (d == NULL || sig == NULL || dsa == NULL) {
  4808. WOLFSSL_MSG("Bad function arguments");
  4809. return -1;
  4810. }
  4811. if (dsa->inSet == 0)
  4812. {
  4813. WOLFSSL_MSG("No DSA internal set, do it");
  4814. if (SetDsaInternal(dsa) != 1) {
  4815. WOLFSSL_MSG("SetDsaInternal failed");
  4816. return -1;
  4817. }
  4818. }
  4819. ret = DsaVerify(d, sig, (DsaKey*)dsa->internal, dsacheck);
  4820. if (ret != 0 || *dsacheck != 1) {
  4821. WOLFSSL_MSG("DsaVerify failed");
  4822. return ret;
  4823. }
  4824. return 1;
  4825. }
  4826. int wolfSSL_DSA_bits(const WOLFSSL_DSA *d)
  4827. {
  4828. if (!d)
  4829. return 0;
  4830. if (!d->exSet && SetDsaExternal((WOLFSSL_DSA*)d) != 1)
  4831. return 0;
  4832. return wolfSSL_BN_num_bits(d->p);
  4833. }
  4834. #ifndef HAVE_SELFTEST
  4835. int wolfSSL_DSA_do_verify_ex(const unsigned char* digest, int digest_len,
  4836. WOLFSSL_DSA_SIG* sig, WOLFSSL_DSA* dsa)
  4837. {
  4838. int dsacheck, sz;
  4839. byte sigBin[DSA_MAX_SIG_SIZE];
  4840. byte* sigBinPtr = sigBin;
  4841. DsaKey* key;
  4842. int qSz;
  4843. WOLFSSL_ENTER("wolfSSL_DSA_do_verify_ex");
  4844. if (!digest || !sig || !dsa || digest_len != WC_SHA_DIGEST_SIZE) {
  4845. WOLFSSL_MSG("Bad function arguments");
  4846. return 0;
  4847. }
  4848. if (!sig->r || !sig->s) {
  4849. WOLFSSL_MSG("No signature found in DSA_SIG");
  4850. return 0;
  4851. }
  4852. if (dsa->inSet == 0) {
  4853. WOLFSSL_MSG("No DSA internal set, do it");
  4854. if (SetDsaInternal(dsa) != 1) {
  4855. WOLFSSL_MSG("SetDsaInternal failed");
  4856. return 0;
  4857. }
  4858. }
  4859. key = (DsaKey*)dsa->internal;
  4860. if (key == NULL) {
  4861. WOLFSSL_MSG("dsa->internal is null");
  4862. return 0;
  4863. }
  4864. qSz = mp_unsigned_bin_size(&key->q);
  4865. if (qSz < 0 || qSz > DSA_MAX_HALF_SIZE) {
  4866. WOLFSSL_MSG("mp_unsigned_bin_size error");
  4867. return 0;
  4868. }
  4869. /* read r */
  4870. /* front pad with zeros */
  4871. if ((sz = wolfSSL_BN_num_bytes(sig->r)) < 0 || sz > DSA_MAX_HALF_SIZE)
  4872. return 0;
  4873. while (sz++ < qSz)
  4874. *sigBinPtr++ = 0;
  4875. if (wolfSSL_BN_bn2bin(sig->r, sigBinPtr) == -1)
  4876. return 0;
  4877. /* Move to s */
  4878. sigBinPtr = sigBin + qSz;
  4879. /* read s */
  4880. /* front pad with zeros */
  4881. if ((sz = wolfSSL_BN_num_bytes(sig->s)) < 0 || sz > DSA_MAX_HALF_SIZE)
  4882. return 0;
  4883. while (sz++ < qSz)
  4884. *sigBinPtr++ = 0;
  4885. if (wolfSSL_BN_bn2bin(sig->s, sigBinPtr) == -1)
  4886. return 0;
  4887. if ((wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck)
  4888. != 1) || dsacheck != 1) {
  4889. return 0;
  4890. }
  4891. return 1;
  4892. }
  4893. #endif /* !HAVE_SELFTEST */
  4894. WOLFSSL_API int wolfSSL_i2d_DSAparams(const WOLFSSL_DSA* dsa,
  4895. unsigned char** out)
  4896. {
  4897. int ret = 0;
  4898. word32 derLen = 0;
  4899. int preAllocated = 1;
  4900. DsaKey* key = NULL;
  4901. WOLFSSL_ENTER("wolfSSL_i2d_DSAparams");
  4902. if (dsa == NULL || dsa->internal == NULL || out == NULL) {
  4903. ret = BAD_FUNC_ARG;
  4904. }
  4905. if (ret == 0) {
  4906. key = (DsaKey*)dsa->internal;
  4907. ret = wc_DsaKeyToParamsDer_ex(key, NULL, &derLen);
  4908. if (ret == LENGTH_ONLY_E) {
  4909. ret = 0;
  4910. }
  4911. }
  4912. if (ret == 0 && *out == NULL) {
  4913. /* If we're allocating out for the caller, we don't increment out just
  4914. past the end of the DER buffer. If out is already allocated, we do.
  4915. (OpenSSL convention) */
  4916. preAllocated = 0;
  4917. *out = (unsigned char*)XMALLOC(derLen, key->heap, DYNAMIC_TYPE_OPENSSL);
  4918. if (*out == NULL) {
  4919. ret = MEMORY_E;
  4920. }
  4921. }
  4922. if (ret == 0) {
  4923. ret = wc_DsaKeyToParamsDer_ex(key, *out, &derLen);
  4924. }
  4925. if (ret >= 0 && preAllocated == 1) {
  4926. *out += derLen;
  4927. }
  4928. if (ret < 0 && preAllocated == 0) {
  4929. XFREE(*out, key ? key->heap : NULL, DYNAMIC_TYPE_OPENSSL);
  4930. }
  4931. WOLFSSL_LEAVE("wolfSSL_i2d_DSAparams", ret);
  4932. return ret;
  4933. }
  4934. WOLFSSL_DSA* wolfSSL_d2i_DSAparams(WOLFSSL_DSA** dsa, const unsigned char** der,
  4935. long derLen)
  4936. {
  4937. WOLFSSL_DSA* ret = NULL;
  4938. int err = 0;
  4939. word32 idx = 0;
  4940. int asnLen;
  4941. DsaKey* internalKey = NULL;
  4942. WOLFSSL_ENTER("wolfSSL_d2i_DSAparams");
  4943. if (der == NULL || *der == NULL || derLen <= 0) {
  4944. err = 1;
  4945. }
  4946. if (err == 0) {
  4947. ret = wolfSSL_DSA_new();
  4948. err = ret == NULL;
  4949. }
  4950. if (err == 0) {
  4951. err = GetSequence(*der, &idx, &asnLen, (word32)derLen) <= 0;
  4952. }
  4953. if (err == 0) {
  4954. internalKey = (DsaKey*)ret->internal;
  4955. err = GetInt(&internalKey->p, *der, &idx, (word32)derLen) != 0;
  4956. }
  4957. if (err == 0) {
  4958. err = GetInt(&internalKey->q, *der, &idx, (word32)derLen) != 0;
  4959. }
  4960. if (err == 0) {
  4961. err = GetInt(&internalKey->g, *der, &idx, (word32)derLen) != 0;
  4962. }
  4963. if (err == 0) {
  4964. err = wolfssl_bn_set_value(&ret->p, &internalKey->p)
  4965. != 1;
  4966. }
  4967. if (err == 0) {
  4968. err = wolfssl_bn_set_value(&ret->q, &internalKey->q)
  4969. != 1;
  4970. }
  4971. if (err == 0) {
  4972. err = wolfssl_bn_set_value(&ret->g, &internalKey->g)
  4973. != 1;
  4974. }
  4975. if (err == 0 && dsa != NULL) {
  4976. *dsa = ret;
  4977. }
  4978. if (err != 0 && ret != NULL) {
  4979. wolfSSL_DSA_free(ret);
  4980. ret = NULL;
  4981. }
  4982. return ret;
  4983. }
  4984. #if defined(WOLFSSL_KEY_GEN)
  4985. #ifndef NO_BIO
  4986. /* Takes a DSA Privatekey and writes it out to a WOLFSSL_BIO
  4987. * Returns 1 or 0
  4988. */
  4989. int wolfSSL_PEM_write_bio_DSAPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_DSA* dsa,
  4990. const EVP_CIPHER* cipher,
  4991. unsigned char* passwd, int len,
  4992. wc_pem_password_cb* cb, void* arg)
  4993. {
  4994. int ret = 0, der_max_len = 0, derSz = 0;
  4995. byte *derBuf;
  4996. WOLFSSL_EVP_PKEY* pkey;
  4997. WOLFSSL_ENTER("wolfSSL_PEM_write_bio_DSAPrivateKey");
  4998. if (bio == NULL || dsa == NULL) {
  4999. WOLFSSL_MSG("Bad Function Arguments");
  5000. return 0;
  5001. }
  5002. pkey = wolfSSL_EVP_PKEY_new_ex(bio->heap);
  5003. if (pkey == NULL) {
  5004. WOLFSSL_MSG("wolfSSL_EVP_PKEY_new_ex failed");
  5005. return 0;
  5006. }
  5007. pkey->type = EVP_PKEY_DSA;
  5008. pkey->dsa = dsa;
  5009. pkey->ownDsa = 0;
  5010. /* 4 > size of pub, priv, p, q, g + ASN.1 additional information */
  5011. der_max_len = MAX_DSA_PRIVKEY_SZ;
  5012. derBuf = (byte*)XMALLOC((size_t)der_max_len, bio->heap,
  5013. DYNAMIC_TYPE_TMP_BUFFER);
  5014. if (derBuf == NULL) {
  5015. WOLFSSL_MSG("Malloc failed");
  5016. wolfSSL_EVP_PKEY_free(pkey);
  5017. return 0;
  5018. }
  5019. /* convert key to der format */
  5020. derSz = wc_DsaKeyToDer((DsaKey*)dsa->internal, derBuf, (word32)der_max_len);
  5021. if (derSz < 0) {
  5022. WOLFSSL_MSG("wc_DsaKeyToDer failed");
  5023. XFREE(derBuf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5024. wolfSSL_EVP_PKEY_free(pkey);
  5025. return 0;
  5026. }
  5027. pkey->pkey.ptr = (char*)XMALLOC((size_t)derSz, bio->heap,
  5028. DYNAMIC_TYPE_TMP_BUFFER);
  5029. if (pkey->pkey.ptr == NULL) {
  5030. WOLFSSL_MSG("key malloc failed");
  5031. XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  5032. wolfSSL_EVP_PKEY_free(pkey);
  5033. return 0;
  5034. }
  5035. /* add der info to the evp key */
  5036. pkey->pkey_sz = derSz;
  5037. XMEMCPY(pkey->pkey.ptr, derBuf, (size_t)derSz);
  5038. XFREE(derBuf, bio->heap, DYNAMIC_TYPE_TMP_BUFFER);
  5039. ret = wolfSSL_PEM_write_bio_PrivateKey(bio, pkey, cipher, passwd, len,
  5040. cb, arg);
  5041. wolfSSL_EVP_PKEY_free(pkey);
  5042. return ret;
  5043. }
  5044. #ifndef HAVE_SELFTEST
  5045. /* Takes a DSA public key and writes it out to a WOLFSSL_BIO
  5046. * Returns 1 or 0
  5047. */
  5048. int wolfSSL_PEM_write_bio_DSA_PUBKEY(WOLFSSL_BIO* bio, WOLFSSL_DSA* dsa)
  5049. {
  5050. int ret = 0;
  5051. WOLFSSL_EVP_PKEY* pkey;
  5052. WOLFSSL_ENTER("wolfSSL_PEM_write_bio_DSA_PUBKEY");
  5053. if (bio == NULL || dsa == NULL) {
  5054. WOLFSSL_MSG("Bad function arguments");
  5055. return 0;
  5056. }
  5057. pkey = wolfSSL_EVP_PKEY_new_ex(bio->heap);
  5058. if (pkey == NULL) {
  5059. WOLFSSL_MSG("wolfSSL_EVP_PKEY_new_ex failed");
  5060. return 0;
  5061. }
  5062. pkey->type = EVP_PKEY_DSA;
  5063. pkey->dsa = dsa;
  5064. pkey->ownDsa = 0;
  5065. ret = pem_write_bio_pubkey(bio, pkey);
  5066. wolfSSL_EVP_PKEY_free(pkey);
  5067. return ret;
  5068. }
  5069. #endif /* HAVE_SELFTEST */
  5070. #endif /* !NO_BIO */
  5071. /* return code compliant with OpenSSL :
  5072. * 1 if success, 0 if error
  5073. */
  5074. int wolfSSL_PEM_write_mem_DSAPrivateKey(WOLFSSL_DSA* dsa,
  5075. const EVP_CIPHER* cipher,
  5076. unsigned char* passwd, int passwdSz,
  5077. unsigned char **pem, int *pLen)
  5078. {
  5079. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  5080. byte *derBuf, *tmp, *cipherInfo = NULL;
  5081. int der_max_len = 0, derSz = 0;
  5082. const int type = DSA_PRIVATEKEY_TYPE;
  5083. const char* header = NULL;
  5084. const char* footer = NULL;
  5085. WOLFSSL_MSG("wolfSSL_PEM_write_mem_DSAPrivateKey");
  5086. if (pem == NULL || pLen == NULL || dsa == NULL || dsa->internal == NULL) {
  5087. WOLFSSL_MSG("Bad function arguments");
  5088. return 0;
  5089. }
  5090. if (wc_PemGetHeaderFooter(type, &header, &footer) != 0)
  5091. return 0;
  5092. if (dsa->inSet == 0) {
  5093. WOLFSSL_MSG("No DSA internal set, do it");
  5094. if (SetDsaInternal(dsa) != 1) {
  5095. WOLFSSL_MSG("SetDsaInternal failed");
  5096. return 0;
  5097. }
  5098. }
  5099. der_max_len = MAX_DSA_PRIVKEY_SZ;
  5100. derBuf = (byte*)XMALLOC((size_t)der_max_len, NULL, DYNAMIC_TYPE_DER);
  5101. if (derBuf == NULL) {
  5102. WOLFSSL_MSG("malloc failed");
  5103. return 0;
  5104. }
  5105. /* Key to DER */
  5106. derSz = wc_DsaKeyToDer((DsaKey*)dsa->internal, derBuf, (word32)der_max_len);
  5107. if (derSz < 0) {
  5108. WOLFSSL_MSG("wc_DsaKeyToDer failed");
  5109. XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
  5110. return 0;
  5111. }
  5112. /* encrypt DER buffer if required */
  5113. if (passwd != NULL && passwdSz > 0 && cipher != NULL) {
  5114. int ret;
  5115. ret = EncryptDerKey(derBuf, &derSz, cipher,
  5116. passwd, passwdSz, &cipherInfo, der_max_len);
  5117. if (ret != 1) {
  5118. WOLFSSL_MSG("EncryptDerKey failed");
  5119. XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
  5120. return ret;
  5121. }
  5122. /* tmp buffer with a max size */
  5123. *pLen = (derSz * 2) + (int)XSTRLEN(header) + 1 +
  5124. (int)XSTRLEN(footer) + 1 + HEADER_ENCRYPTED_KEY_SIZE;
  5125. }
  5126. else { /* tmp buffer with a max size */
  5127. *pLen = (derSz * 2) + (int)XSTRLEN(header) + 1 +
  5128. (int)XSTRLEN(footer) + 1;
  5129. }
  5130. tmp = (byte*)XMALLOC((size_t)*pLen, NULL, DYNAMIC_TYPE_PEM);
  5131. if (tmp == NULL) {
  5132. WOLFSSL_MSG("malloc failed");
  5133. XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
  5134. if (cipherInfo != NULL)
  5135. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
  5136. return 0;
  5137. }
  5138. /* DER to PEM */
  5139. *pLen = wc_DerToPemEx(derBuf, (word32)derSz, tmp, (word32)*pLen, cipherInfo,
  5140. type);
  5141. if (*pLen <= 0) {
  5142. WOLFSSL_MSG("wc_DerToPemEx failed");
  5143. XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
  5144. XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
  5145. if (cipherInfo != NULL)
  5146. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
  5147. return 0;
  5148. }
  5149. XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
  5150. if (cipherInfo != NULL)
  5151. XFREE(cipherInfo, NULL, DYNAMIC_TYPE_STRING);
  5152. *pem = (byte*)XMALLOC((size_t)((*pLen)+1), NULL, DYNAMIC_TYPE_KEY);
  5153. if (*pem == NULL) {
  5154. WOLFSSL_MSG("malloc failed");
  5155. XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
  5156. return 0;
  5157. }
  5158. XMEMSET(*pem, 0, (size_t)((*pLen)+1));
  5159. if (XMEMCPY(*pem, tmp, (size_t)*pLen) == NULL) {
  5160. WOLFSSL_MSG("XMEMCPY failed");
  5161. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  5162. XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
  5163. return 0;
  5164. }
  5165. XFREE(tmp, NULL, DYNAMIC_TYPE_PEM);
  5166. return 1;
  5167. #else
  5168. (void)dsa;
  5169. (void)cipher;
  5170. (void)passwd;
  5171. (void)passwdSz;
  5172. (void)pem;
  5173. (void)pLen;
  5174. return 0;
  5175. #endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */
  5176. }
  5177. #ifndef NO_FILESYSTEM
  5178. /* return code compliant with OpenSSL :
  5179. * 1 if success, 0 if error
  5180. */
  5181. int wolfSSL_PEM_write_DSAPrivateKey(XFILE fp, WOLFSSL_DSA *dsa,
  5182. const EVP_CIPHER *enc,
  5183. unsigned char *kstr, int klen,
  5184. wc_pem_password_cb *cb, void *u)
  5185. {
  5186. byte *pem;
  5187. int pLen, ret;
  5188. (void)cb;
  5189. (void)u;
  5190. WOLFSSL_MSG("wolfSSL_PEM_write_DSAPrivateKey");
  5191. if (fp == XBADFILE || dsa == NULL || dsa->internal == NULL) {
  5192. WOLFSSL_MSG("Bad function arguments");
  5193. return 0;
  5194. }
  5195. ret = wolfSSL_PEM_write_mem_DSAPrivateKey(dsa, enc, kstr, klen, &pem,
  5196. &pLen);
  5197. if (ret != 1) {
  5198. WOLFSSL_MSG("wolfSSL_PEM_write_mem_DSAPrivateKey failed");
  5199. return 0;
  5200. }
  5201. ret = (int)XFWRITE(pem, (size_t)pLen, 1, fp);
  5202. if (ret != 1) {
  5203. WOLFSSL_MSG("DSA private key file write failed");
  5204. return 0;
  5205. }
  5206. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  5207. return 1;
  5208. }
  5209. #endif /* NO_FILESYSTEM */
  5210. #endif /* defined(WOLFSSL_KEY_GEN) */
  5211. #ifndef NO_FILESYSTEM
  5212. /* return code compliant with OpenSSL :
  5213. * 1 if success, 0 if error
  5214. */
  5215. #ifndef NO_WOLFSSL_STUB
  5216. int wolfSSL_PEM_write_DSA_PUBKEY(XFILE fp, WOLFSSL_DSA *x)
  5217. {
  5218. (void)fp;
  5219. (void)x;
  5220. WOLFSSL_STUB("PEM_write_DSA_PUBKEY");
  5221. WOLFSSL_MSG("wolfSSL_PEM_write_DSA_PUBKEY not implemented");
  5222. return 0;
  5223. }
  5224. #endif
  5225. #endif /* NO_FILESYSTEM */
  5226. #ifndef NO_BIO
  5227. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && (!defined(NO_CERTS) && \
  5228. !defined(NO_FILESYSTEM) && defined(WOLFSSL_KEY_GEN))
  5229. /* Uses the same format of input as wolfSSL_PEM_read_bio_PrivateKey but expects
  5230. * the results to be an DSA key.
  5231. *
  5232. * bio structure to read DSA private key from
  5233. * dsa if not null is then set to the result
  5234. * cb password callback for reading PEM
  5235. * pass password string
  5236. *
  5237. * returns a pointer to a new WOLFSSL_DSA structure on success and NULL on fail
  5238. */
  5239. WOLFSSL_DSA* wolfSSL_PEM_read_bio_DSAPrivateKey(WOLFSSL_BIO* bio,
  5240. WOLFSSL_DSA** dsa,
  5241. wc_pem_password_cb* cb,
  5242. void* pass)
  5243. {
  5244. WOLFSSL_EVP_PKEY* pkey = NULL;
  5245. WOLFSSL_DSA* local;
  5246. WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DSAPrivateKey");
  5247. pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, cb, pass);
  5248. if (pkey == NULL) {
  5249. WOLFSSL_MSG("Error in PEM_read_bio_PrivateKey");
  5250. return NULL;
  5251. }
  5252. /* Since the WOLFSSL_DSA structure is being taken from WOLFSSL_EVP_PKEY the
  5253. * flag indicating that the WOLFSSL_DSA structure is owned should be FALSE
  5254. * to avoid having it free'd */
  5255. pkey->ownDsa = 0;
  5256. local = pkey->dsa;
  5257. if (dsa != NULL) {
  5258. *dsa = local;
  5259. }
  5260. wolfSSL_EVP_PKEY_free(pkey);
  5261. return local;
  5262. }
  5263. /* Reads an DSA public key from a WOLFSSL_BIO into a WOLFSSL_DSA.
  5264. * Returns 1 or 0
  5265. */
  5266. WOLFSSL_DSA *wolfSSL_PEM_read_bio_DSA_PUBKEY(WOLFSSL_BIO* bio,WOLFSSL_DSA** dsa,
  5267. wc_pem_password_cb* cb, void* pass)
  5268. {
  5269. WOLFSSL_EVP_PKEY* pkey;
  5270. WOLFSSL_DSA* local;
  5271. WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DSA_PUBKEY");
  5272. pkey = wolfSSL_PEM_read_bio_PUBKEY(bio, NULL, cb, pass);
  5273. if (pkey == NULL) {
  5274. WOLFSSL_MSG("wolfSSL_PEM_read_bio_PUBKEY failed");
  5275. return NULL;
  5276. }
  5277. /* Since the WOLFSSL_DSA structure is being taken from WOLFSSL_EVP_PKEY the
  5278. * flag indicating that the WOLFSSL_DSA structure is owned should be FALSE
  5279. * to avoid having it free'd */
  5280. pkey->ownDsa = 0;
  5281. local = pkey->dsa;
  5282. if (dsa != NULL) {
  5283. *dsa = local;
  5284. }
  5285. wolfSSL_EVP_PKEY_free(pkey);
  5286. return local;
  5287. }
  5288. #endif /* (OPENSSL_EXTRA || OPENSSL_ALL) && (!NO_CERTS &&
  5289. !NO_FILESYSTEM && WOLFSSL_KEY_GEN) */
  5290. #endif /* NO_BIO */
  5291. #endif /* OPENSSL_EXTRA */
  5292. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  5293. /* return 1 if success, -1 if error */
  5294. int wolfSSL_DSA_LoadDer(WOLFSSL_DSA* dsa, const unsigned char* derBuf, int derSz)
  5295. {
  5296. word32 idx = 0;
  5297. int ret;
  5298. WOLFSSL_ENTER("wolfSSL_DSA_LoadDer");
  5299. if (dsa == NULL || dsa->internal == NULL || derBuf == NULL || derSz <= 0) {
  5300. WOLFSSL_MSG("Bad function arguments");
  5301. return -1;
  5302. }
  5303. ret = DsaPrivateKeyDecode(derBuf, &idx, (DsaKey*)dsa->internal,
  5304. (word32)derSz);
  5305. if (ret < 0) {
  5306. WOLFSSL_MSG("DsaPrivateKeyDecode failed");
  5307. return -1;
  5308. }
  5309. if (SetDsaExternal(dsa) != 1) {
  5310. WOLFSSL_MSG("SetDsaExternal failed");
  5311. return -1;
  5312. }
  5313. dsa->inSet = 1;
  5314. return 1;
  5315. }
  5316. /* Loads DSA key from DER buffer. opt = DSA_LOAD_PRIVATE or DSA_LOAD_PUBLIC.
  5317. returns 1 on success, or 0 on failure. */
  5318. int wolfSSL_DSA_LoadDer_ex(WOLFSSL_DSA* dsa, const unsigned char* derBuf,
  5319. int derSz, int opt)
  5320. {
  5321. word32 idx = 0;
  5322. int ret;
  5323. WOLFSSL_ENTER("wolfSSL_DSA_LoadDer");
  5324. if (dsa == NULL || dsa->internal == NULL || derBuf == NULL || derSz <= 0) {
  5325. WOLFSSL_MSG("Bad function arguments");
  5326. return -1;
  5327. }
  5328. if (opt == WOLFSSL_DSA_LOAD_PRIVATE) {
  5329. ret = DsaPrivateKeyDecode(derBuf, &idx, (DsaKey*)dsa->internal,
  5330. (word32)derSz);
  5331. }
  5332. else {
  5333. ret = DsaPublicKeyDecode(derBuf, &idx, (DsaKey*)dsa->internal,
  5334. (word32)derSz);
  5335. }
  5336. if (ret < 0 && opt == WOLFSSL_DSA_LOAD_PRIVATE) {
  5337. WOLFSSL_ERROR_VERBOSE(ret);
  5338. WOLFSSL_MSG("DsaPrivateKeyDecode failed");
  5339. return -1;
  5340. }
  5341. else if (ret < 0 && opt == WOLFSSL_DSA_LOAD_PUBLIC) {
  5342. WOLFSSL_ERROR_VERBOSE(ret);
  5343. WOLFSSL_MSG("DsaPublicKeyDecode failed");
  5344. return -1;
  5345. }
  5346. if (SetDsaExternal(dsa) != 1) {
  5347. WOLFSSL_MSG("SetDsaExternal failed");
  5348. return -1;
  5349. }
  5350. dsa->inSet = 1;
  5351. return 1;
  5352. }
  5353. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  5354. #ifdef OPENSSL_EXTRA
  5355. #ifndef NO_BIO
  5356. WOLFSSL_DSA *wolfSSL_PEM_read_bio_DSAparams(WOLFSSL_BIO *bp, WOLFSSL_DSA **x,
  5357. wc_pem_password_cb *cb, void *u)
  5358. {
  5359. WOLFSSL_DSA* dsa;
  5360. DsaKey* key;
  5361. int length;
  5362. unsigned char* buf;
  5363. word32 bufSz;
  5364. int ret;
  5365. word32 idx = 0;
  5366. DerBuffer* pDer;
  5367. WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DSAparams");
  5368. ret = wolfSSL_BIO_get_mem_data(bp, &buf);
  5369. if (ret <= 0) {
  5370. WOLFSSL_LEAVE("wolfSSL_PEM_read_bio_DSAparams", ret);
  5371. return NULL;
  5372. }
  5373. bufSz = (word32)ret;
  5374. if (cb != NULL || u != NULL) {
  5375. /*
  5376. * cb is for a call back when encountering encrypted PEM files
  5377. * if cb == NULL and u != NULL then u = null terminated password string
  5378. */
  5379. WOLFSSL_MSG("Not yet supporting call back or password for encrypted PEM");
  5380. }
  5381. if (PemToDer(buf, (long)bufSz, DSA_PARAM_TYPE, &pDer, NULL, NULL,
  5382. NULL) < 0 ) {
  5383. WOLFSSL_MSG("Issue converting from PEM to DER");
  5384. return NULL;
  5385. }
  5386. if (GetSequence(pDer->buffer, &idx, &length, pDer->length) < 0) {
  5387. WOLFSSL_LEAVE("wolfSSL_PEM_read_bio_DSAparams", ret);
  5388. FreeDer(&pDer);
  5389. return NULL;
  5390. }
  5391. dsa = wolfSSL_DSA_new();
  5392. if (dsa == NULL) {
  5393. FreeDer(&pDer);
  5394. WOLFSSL_MSG("Error creating DSA struct");
  5395. return NULL;
  5396. }
  5397. key = (DsaKey*)dsa->internal;
  5398. if (key == NULL) {
  5399. FreeDer(&pDer);
  5400. wolfSSL_DSA_free(dsa);
  5401. WOLFSSL_MSG("Error finding DSA key struct");
  5402. return NULL;
  5403. }
  5404. if (GetInt(&key->p, pDer->buffer, &idx, pDer->length) < 0 ||
  5405. GetInt(&key->q, pDer->buffer, &idx, pDer->length) < 0 ||
  5406. GetInt(&key->g, pDer->buffer, &idx, pDer->length) < 0 ) {
  5407. WOLFSSL_MSG("dsa key error");
  5408. FreeDer(&pDer);
  5409. wolfSSL_DSA_free(dsa);
  5410. return NULL;
  5411. }
  5412. if (wolfssl_bn_set_value(&dsa->p, &key->p) != 1) {
  5413. WOLFSSL_MSG("dsa p key error");
  5414. FreeDer(&pDer);
  5415. wolfSSL_DSA_free(dsa);
  5416. return NULL;
  5417. }
  5418. if (wolfssl_bn_set_value(&dsa->q, &key->q) != 1) {
  5419. WOLFSSL_MSG("dsa q key error");
  5420. FreeDer(&pDer);
  5421. wolfSSL_DSA_free(dsa);
  5422. return NULL;
  5423. }
  5424. if (wolfssl_bn_set_value(&dsa->g, &key->g) != 1) {
  5425. WOLFSSL_MSG("dsa g key error");
  5426. FreeDer(&pDer);
  5427. wolfSSL_DSA_free(dsa);
  5428. return NULL;
  5429. }
  5430. if (x != NULL) {
  5431. *x = dsa;
  5432. }
  5433. FreeDer(&pDer);
  5434. return dsa;
  5435. }
  5436. #endif /* !NO_BIO */
  5437. #if !defined(NO_DH)
  5438. WOLFSSL_DH *wolfSSL_DSA_dup_DH(const WOLFSSL_DSA *dsa)
  5439. {
  5440. WOLFSSL_DH* dh;
  5441. DhKey* key;
  5442. WOLFSSL_ENTER("wolfSSL_DSA_dup_DH");
  5443. if (dsa == NULL) {
  5444. return NULL;
  5445. }
  5446. dh = wolfSSL_DH_new();
  5447. if (dh == NULL) {
  5448. return NULL;
  5449. }
  5450. key = (DhKey*)dh->internal;
  5451. if (dsa->p != NULL &&
  5452. wolfssl_bn_get_value(((WOLFSSL_DSA*)dsa)->p, &key->p)
  5453. != 1) {
  5454. WOLFSSL_MSG("rsa p key error");
  5455. wolfSSL_DH_free(dh);
  5456. return NULL;
  5457. }
  5458. if (dsa->g != NULL &&
  5459. wolfssl_bn_get_value(((WOLFSSL_DSA*)dsa)->g, &key->g)
  5460. != 1) {
  5461. WOLFSSL_MSG("rsa g key error");
  5462. wolfSSL_DH_free(dh);
  5463. return NULL;
  5464. }
  5465. if (wolfssl_bn_set_value(&dh->p, &key->p) != 1) {
  5466. WOLFSSL_MSG("dsa p key error");
  5467. wolfSSL_DH_free(dh);
  5468. return NULL;
  5469. }
  5470. if (wolfssl_bn_set_value(&dh->g, &key->g) != 1) {
  5471. WOLFSSL_MSG("dsa g key error");
  5472. wolfSSL_DH_free(dh);
  5473. return NULL;
  5474. }
  5475. return dh;
  5476. }
  5477. #endif /* !NO_DH */
  5478. #endif /* OPENSSL_EXTRA */
  5479. #endif /* !NO_DSA */
  5480. /*******************************************************************************
  5481. * END OF DSA API
  5482. ******************************************************************************/
  5483. /*******************************************************************************
  5484. * START OF DH API
  5485. ******************************************************************************/
  5486. #ifndef NO_DH
  5487. #ifdef OPENSSL_EXTRA
  5488. /*
  5489. * DH constructor/deconstructor APIs
  5490. */
  5491. /* Allocate and initialize a new DH key.
  5492. *
  5493. * @return DH key on success.
  5494. * @return NULL on failure.
  5495. */
  5496. WOLFSSL_DH* wolfSSL_DH_new(void)
  5497. {
  5498. int err = 0;
  5499. WOLFSSL_DH* dh = NULL;
  5500. DhKey* key = NULL;
  5501. WOLFSSL_ENTER("wolfSSL_DH_new");
  5502. /* Allocate OpenSSL DH key. */
  5503. dh = (WOLFSSL_DH*)XMALLOC(sizeof(WOLFSSL_DH), NULL, DYNAMIC_TYPE_DH);
  5504. if (dh == NULL) {
  5505. WOLFSSL_ERROR_MSG("wolfSSL_DH_new malloc WOLFSSL_DH failure");
  5506. err = 1;
  5507. }
  5508. if (!err) {
  5509. /* Clear key data. */
  5510. XMEMSET(dh, 0, sizeof(WOLFSSL_DH));
  5511. /* Initialize reference counting. */
  5512. wolfSSL_RefInit(&dh->ref, &err);
  5513. #ifdef WOLFSSL_REFCNT_ERROR_RETURN
  5514. }
  5515. if (!err) {
  5516. #endif
  5517. /* Allocate wolfSSL DH key. */
  5518. key = (DhKey*)XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_DH);
  5519. if (key == NULL) {
  5520. WOLFSSL_ERROR_MSG("wolfSSL_DH_new malloc DhKey failure");
  5521. err = 1;
  5522. }
  5523. }
  5524. if (!err) {
  5525. /* Set and initialize wolfSSL DH key. */
  5526. dh->internal = key;
  5527. if (wc_InitDhKey(key) != 0) {
  5528. WOLFSSL_ERROR_MSG("wolfSSL_DH_new InitDhKey failure");
  5529. err = 1;
  5530. }
  5531. }
  5532. if (err && (dh != NULL)) {
  5533. /* Dispose of the allocated memory. */
  5534. XFREE(key, NULL, DYNAMIC_TYPE_DH);
  5535. wolfSSL_RefFree(&dh->ref);
  5536. XFREE(dh, NULL, DYNAMIC_TYPE_DH);
  5537. dh = NULL;
  5538. }
  5539. return dh;
  5540. }
  5541. #if defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && FIPS_VERSION_EQ(2,0))
  5542. /* Set the DH parameters based on the NID.
  5543. *
  5544. * @param [in, out] dh DH key to set.
  5545. * @param [in] nid Numeric ID of predefined DH parameters.
  5546. * @return 0 on success.
  5547. * @return 1 on failure.
  5548. */
  5549. static int wolfssl_dh_set_nid(WOLFSSL_DH* dh, int nid)
  5550. {
  5551. int err = 0;
  5552. const DhParams* params = NULL;
  5553. /* HAVE_PUBLIC_FFDHE not required to expose wc_Dh_ffdhe* functions in
  5554. * FIPS v2 module */
  5555. switch (nid) {
  5556. #ifdef HAVE_FFDHE_2048
  5557. case NID_ffdhe2048:
  5558. params = wc_Dh_ffdhe2048_Get();
  5559. break;
  5560. #endif /* HAVE_FFDHE_2048 */
  5561. #ifdef HAVE_FFDHE_3072
  5562. case NID_ffdhe3072:
  5563. params = wc_Dh_ffdhe3072_Get();
  5564. break;
  5565. #endif /* HAVE_FFDHE_3072 */
  5566. #ifdef HAVE_FFDHE_4096
  5567. case NID_ffdhe4096:
  5568. params = wc_Dh_ffdhe4096_Get();
  5569. break;
  5570. #endif /* HAVE_FFDHE_4096 */
  5571. default:
  5572. break;
  5573. }
  5574. if (params == NULL) {
  5575. WOLFSSL_ERROR_MSG("Unable to find DH params for nid.");
  5576. err = 1;
  5577. }
  5578. if (!err) {
  5579. /* Set prime from data retrieved. */
  5580. dh->p = wolfSSL_BN_bin2bn(params->p, (int)params->p_len, NULL);
  5581. if (dh->p == NULL) {
  5582. WOLFSSL_ERROR_MSG("Error converting p hex to WOLFSSL_BIGNUM.");
  5583. err = 1;
  5584. }
  5585. }
  5586. if (!err) {
  5587. /* Set generator from data retrieved. */
  5588. dh->g = wolfSSL_BN_bin2bn(params->g, (int)params->g_len, NULL);
  5589. if (dh->g == NULL) {
  5590. WOLFSSL_ERROR_MSG("Error converting g hex to WOLFSSL_BIGNUM.");
  5591. err = 1;
  5592. }
  5593. }
  5594. #ifdef HAVE_FFDHE_Q
  5595. if (!err) {
  5596. /* Set order from data retrieved. */
  5597. dh->q = wolfSSL_BN_bin2bn(params->q, params->q_len, NULL);
  5598. if (dh->q == NULL) {
  5599. WOLFSSL_ERROR_MSG("Error converting q hex to WOLFSSL_BIGNUM.");
  5600. err = 1;
  5601. }
  5602. }
  5603. #endif
  5604. /* Synchronize the external into internal DH key's parameters. */
  5605. if ((!err) && (SetDhInternal(dh) != 1)) {
  5606. WOLFSSL_ERROR_MSG("Failed to set internal DH params.");
  5607. err = 1;
  5608. }
  5609. if (!err) {
  5610. /* External DH key parameters were set. */
  5611. dh->exSet = 1;
  5612. }
  5613. if (err == 1) {
  5614. /* Dispose of any external parameters. */
  5615. #ifdef HAVE_FFDHE_Q
  5616. wolfSSL_BN_free(dh->q);
  5617. dh->q = NULL;
  5618. #endif
  5619. wolfSSL_BN_free(dh->p);
  5620. dh->p = NULL;
  5621. wolfSSL_BN_free(dh->g);
  5622. dh->g = NULL;
  5623. }
  5624. return err;
  5625. }
  5626. #elif !defined(HAVE_PUBLIC_FFDHE) && (!defined(HAVE_FIPS) || \
  5627. FIPS_VERSION_GT(2,0))
  5628. /* Set the DH parameters based on the NID.
  5629. *
  5630. * FIPS v2 and lower doesn't support wc_DhSetNamedKey.
  5631. *
  5632. * @param [in, out] dh DH key to set.
  5633. * @param [in] nid Numeric ID of predefined DH parameters.
  5634. * @return 0 on success.
  5635. * @return 1 on failure.
  5636. */
  5637. static int wolfssl_dh_set_nid(WOLFSSL_DH* dh, int nid)
  5638. {
  5639. int err = 0;
  5640. int name = 0;
  5641. #ifdef HAVE_FFDHE_Q
  5642. int elements = ELEMENT_P | ELEMENT_G | ELEMENT_Q;
  5643. #else
  5644. int elements = ELEMENT_P | ELEMENT_G;
  5645. #endif /* HAVE_FFDHE_Q */
  5646. switch (nid) {
  5647. #ifdef HAVE_FFDHE_2048
  5648. case NID_ffdhe2048:
  5649. name = WC_FFDHE_2048;
  5650. break;
  5651. #endif /* HAVE_FFDHE_2048 */
  5652. #ifdef HAVE_FFDHE_3072
  5653. case NID_ffdhe3072:
  5654. name = WC_FFDHE_3072;
  5655. break;
  5656. #endif /* HAVE_FFDHE_3072 */
  5657. #ifdef HAVE_FFDHE_4096
  5658. case NID_ffdhe4096:
  5659. name = WC_FFDHE_4096;
  5660. break;
  5661. #endif /* HAVE_FFDHE_4096 */
  5662. default:
  5663. err = 1;
  5664. WOLFSSL_ERROR_MSG("Unable to find DH params for nid.");
  5665. break;
  5666. }
  5667. /* Set the internal DH key's parameters based on name. */
  5668. if ((!err) && (wc_DhSetNamedKey((DhKey*)dh->internal, name) != 0)) {
  5669. WOLFSSL_ERROR_MSG("wc_DhSetNamedKey failed.");
  5670. err = 1;
  5671. }
  5672. /* Synchronize the internal into external DH key's parameters. */
  5673. if (!err && (SetDhExternal_ex(dh, elements) != 1)) {
  5674. WOLFSSL_ERROR_MSG("Failed to set external DH params.");
  5675. err = 1;
  5676. }
  5677. return err;
  5678. }
  5679. #else
  5680. /* Set the DH parameters based on the NID.
  5681. *
  5682. * Pre-defined DH parameters not available.
  5683. *
  5684. * @param [in, out] dh DH key to set.
  5685. * @param [in] nid Numeric ID of predefined DH parameters.
  5686. * @return 1 for failure.
  5687. */
  5688. static int wolfssl_dh_set_nid(WOLFSSL_DH* dh, int nid)
  5689. {
  5690. return 1;
  5691. }
  5692. #endif
  5693. /* Allocate and initialize a new DH key with the parameters based on the NID.
  5694. *
  5695. * @param [in] nid Numeric ID of DH parameters.
  5696. *
  5697. * @return DH key on success.
  5698. * @return NULL on failure.
  5699. */
  5700. WOLFSSL_DH* wolfSSL_DH_new_by_nid(int nid)
  5701. {
  5702. WOLFSSL_DH* dh = NULL;
  5703. int err = 0;
  5704. WOLFSSL_ENTER("wolfSSL_DH_new_by_nid");
  5705. /* Allocate a new DH key. */
  5706. dh = wolfSSL_DH_new();
  5707. if (dh == NULL) {
  5708. WOLFSSL_ERROR_MSG("Failed to create WOLFSSL_DH.");
  5709. err = 1;
  5710. }
  5711. if (!err) {
  5712. /* Set the parameters based on NID. */
  5713. err = wolfssl_dh_set_nid(dh, nid);
  5714. }
  5715. if (err && (dh != NULL)) {
  5716. /* Dispose of the key on failure to set. */
  5717. wolfSSL_DH_free(dh);
  5718. dh = NULL;
  5719. }
  5720. WOLFSSL_LEAVE("wolfSSL_DH_new_by_nid", err);
  5721. return dh;
  5722. }
  5723. /* Dispose of DH key and allocated data.
  5724. *
  5725. * Cannot use dh after this call.
  5726. *
  5727. * @param [in] dh DH key to free.
  5728. */
  5729. void wolfSSL_DH_free(WOLFSSL_DH* dh)
  5730. {
  5731. int doFree = 0;
  5732. WOLFSSL_ENTER("wolfSSL_DH_free");
  5733. if (dh != NULL) {
  5734. int err;
  5735. /* Only free if all references to it are done */
  5736. wolfSSL_RefDec(&dh->ref, &doFree, &err);
  5737. /* Ignore errors - doFree will be 0 on error. */
  5738. (void)err;
  5739. }
  5740. if (doFree) {
  5741. /* Dispose of allocated reference counting data. */
  5742. wolfSSL_RefFree(&dh->ref);
  5743. /* Dispose of wolfSSL DH key. */
  5744. if (dh->internal) {
  5745. wc_FreeDhKey((DhKey*)dh->internal);
  5746. XFREE(dh->internal, NULL, DYNAMIC_TYPE_DH);
  5747. dh->internal = NULL;
  5748. }
  5749. /* Dispose of any allocated BNs. */
  5750. wolfSSL_BN_free(dh->priv_key);
  5751. wolfSSL_BN_free(dh->pub_key);
  5752. wolfSSL_BN_free(dh->g);
  5753. wolfSSL_BN_free(dh->p);
  5754. wolfSSL_BN_free(dh->q);
  5755. /* Set back to NULLs for safety. */
  5756. XMEMSET(dh, 0, sizeof(WOLFSSL_DH));
  5757. XFREE(dh, NULL, DYNAMIC_TYPE_DH);
  5758. }
  5759. }
  5760. /* Increments ref count of DH key.
  5761. *
  5762. * @param [in, out] dh DH key.
  5763. * @return 1 on success
  5764. * @return 0 on error
  5765. */
  5766. int wolfSSL_DH_up_ref(WOLFSSL_DH* dh)
  5767. {
  5768. int err = 1;
  5769. WOLFSSL_ENTER("wolfSSL_DH_up_ref");
  5770. if (dh != NULL) {
  5771. wolfSSL_RefInc(&dh->ref, &err);
  5772. }
  5773. return !err;
  5774. }
  5775. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  5776. defined(OPENSSL_EXTRA)
  5777. #ifdef WOLFSSL_DH_EXTRA
  5778. /* Duplicate the DH key.
  5779. *
  5780. * Internal DH key in 'dh' is updated if necessary.
  5781. *
  5782. * @param [in, out] dh DH key to duplicate.
  5783. * @return NULL on failure.
  5784. * @return DH key on success.
  5785. */
  5786. WOLFSSL_DH* wolfSSL_DH_dup(WOLFSSL_DH* dh)
  5787. {
  5788. WOLFSSL_DH* ret = NULL;
  5789. int err = 0;
  5790. WOLFSSL_ENTER("wolfSSL_DH_dup");
  5791. /* Validate parameters. */
  5792. if (dh == NULL) {
  5793. WOLFSSL_ERROR_MSG("Bad parameter");
  5794. err = 1;
  5795. }
  5796. /* Ensure internal DH key is set. */
  5797. if ((!err) && (dh->inSet == 0) && (SetDhInternal(dh) != 1)) {
  5798. WOLFSSL_ERROR_MSG("Bad DH set internal");
  5799. err = 1;
  5800. }
  5801. /* Create a new DH key object. */
  5802. if ((!err) && (!(ret = wolfSSL_DH_new()))) {
  5803. WOLFSSL_ERROR_MSG("wolfSSL_DH_new error");
  5804. err = 1;
  5805. }
  5806. /* Copy internal DH key from original to new. */
  5807. if ((!err) && (wc_DhKeyCopy((DhKey*)dh->internal, (DhKey*)ret->internal) !=
  5808. MP_OKAY)) {
  5809. WOLFSSL_ERROR_MSG("wc_DhKeyCopy error");
  5810. err = 1;
  5811. }
  5812. if (!err) {
  5813. ret->inSet = 1;
  5814. /* Synchronize the internal into external DH key's parameters. */
  5815. if (SetDhExternal(ret) != 1) {
  5816. WOLFSSL_ERROR_MSG("SetDhExternal error");
  5817. err = 1;
  5818. }
  5819. }
  5820. /* Dispose of any allocated DH key on error. */
  5821. if (err && (ret != NULL)) {
  5822. wolfSSL_DH_free(ret);
  5823. ret = NULL;
  5824. }
  5825. return ret;
  5826. }
  5827. #endif /* WOLFSSL_DH_EXTRA */
  5828. #endif
  5829. /* Allocate and initialize a new DH key with 2048-bit parameters.
  5830. *
  5831. * See RFC 5114 section 2.3, "2048-bit MODP Group with 256-bit Prime Order
  5832. * Subgroup."
  5833. *
  5834. * @return NULL on failure.
  5835. * @return DH Key on success.
  5836. */
  5837. WOLFSSL_DH* wolfSSL_DH_get_2048_256(void)
  5838. {
  5839. WOLFSSL_DH* dh;
  5840. int err = 0;
  5841. static const byte pHex[] = {
  5842. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  5843. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  5844. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  5845. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  5846. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  5847. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  5848. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  5849. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  5850. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  5851. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  5852. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  5853. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  5854. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  5855. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  5856. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  5857. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  5858. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  5859. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  5860. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  5861. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  5862. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  5863. 0x1E, 0x1A, 0x15, 0x97
  5864. };
  5865. static const byte gHex[] = {
  5866. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  5867. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  5868. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  5869. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  5870. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  5871. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  5872. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  5873. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  5874. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  5875. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  5876. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  5877. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  5878. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  5879. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  5880. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  5881. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  5882. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  5883. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  5884. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  5885. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  5886. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  5887. 0x6C, 0xC4, 0x16, 0x59
  5888. };
  5889. static const byte qHex[] = {
  5890. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  5891. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  5892. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  5893. };
  5894. /* Create a new DH key to return. */
  5895. dh = wolfSSL_DH_new();
  5896. if (dh == NULL) {
  5897. err = 1;
  5898. }
  5899. if (!err) {
  5900. /* Set prime. */
  5901. dh->p = wolfSSL_BN_bin2bn(pHex, (int)sizeof(pHex), NULL);
  5902. if (dh->p == NULL) {
  5903. WOLFSSL_ERROR_MSG("Error converting p hex to WOLFSSL_BIGNUM.");
  5904. err = 1;
  5905. }
  5906. }
  5907. if (!err) {
  5908. /* Set generator. */
  5909. dh->g = wolfSSL_BN_bin2bn(gHex, (int)sizeof(gHex), NULL);
  5910. if (dh->g == NULL) {
  5911. WOLFSSL_ERROR_MSG("Error converting g hex to WOLFSSL_BIGNUM.");
  5912. err = 1;
  5913. }
  5914. }
  5915. if (!err) {
  5916. /* Set order. */
  5917. dh->q = wolfSSL_BN_bin2bn(qHex, (int)sizeof(qHex), NULL);
  5918. if (dh->q == NULL) {
  5919. WOLFSSL_ERROR_MSG("Error converting q hex to WOLFSSL_BIGNUM.");
  5920. err = 1;
  5921. }
  5922. }
  5923. /* Set values into wolfSSL DH key. */
  5924. if ((!err) && (SetDhInternal(dh) != 1)) {
  5925. WOLFSSL_ERROR_MSG("Error setting DH parameters.");
  5926. err = 1;
  5927. }
  5928. if (!err) {
  5929. /* External DH key parameters were set. */
  5930. dh->exSet = 1;
  5931. }
  5932. /* Dispose of any allocated DH key on error. */
  5933. if (err && (dh != NULL)) {
  5934. wolfSSL_DH_free(dh);
  5935. dh = NULL;
  5936. }
  5937. return dh;
  5938. }
  5939. /* TODO: consider changing strings to byte arrays. */
  5940. /* Returns a big number with the 768-bit prime from RFC 2409.
  5941. *
  5942. * @param [in, out] bn If not NULL then this BN is set and returned.
  5943. * If NULL then a new BN is created, set and returned.
  5944. *
  5945. * @return NULL on failure.
  5946. * @return WOLFSSL_BIGNUM with value set to 768-bit prime on success.
  5947. */
  5948. WOLFSSL_BIGNUM* wolfSSL_DH_768_prime(WOLFSSL_BIGNUM* bn)
  5949. {
  5950. #if WOLFSSL_MAX_BN_BITS >= 768
  5951. static const char prm[] = {
  5952. "FFFFFFFFFFFFFFFFC90FDAA22168C234"
  5953. "C4C6628B80DC1CD129024E088A67CC74"
  5954. "020BBEA63B139B22514A08798E3404DD"
  5955. "EF9519B3CD3A431B302B0A6DF25F1437"
  5956. "4FE1356D6D51C245E485B576625E7EC6"
  5957. "F44C42E9A63A3620FFFFFFFFFFFFFFFF"
  5958. };
  5959. WOLFSSL_ENTER("wolfSSL_DH_768_prime");
  5960. /* Set prime into BN. Creates a new BN when bn is NULL. */
  5961. if (wolfSSL_BN_hex2bn(&bn, prm) != 1) {
  5962. WOLFSSL_ERROR_MSG("Error converting DH 768 prime to big number");
  5963. bn = NULL;
  5964. }
  5965. return bn;
  5966. #else
  5967. (void)bn;
  5968. return NULL;
  5969. #endif
  5970. }
  5971. /* Returns a big number with the 1024-bit prime from RFC 2409.
  5972. *
  5973. * @param [in, out] bn If not NULL then this BN is set and returned.
  5974. * If NULL then a new BN is created, set and returned.
  5975. *
  5976. * @return NULL on failure.
  5977. * @return WOLFSSL_BIGNUM with value set to 1024-bit prime on success.
  5978. */
  5979. WOLFSSL_BIGNUM* wolfSSL_DH_1024_prime(WOLFSSL_BIGNUM* bn)
  5980. {
  5981. #if WOLFSSL_MAX_BN_BITS >= 1024
  5982. static const char prm[] = {
  5983. "FFFFFFFFFFFFFFFFC90FDAA22168C234"
  5984. "C4C6628B80DC1CD129024E088A67CC74"
  5985. "020BBEA63B139B22514A08798E3404DD"
  5986. "EF9519B3CD3A431B302B0A6DF25F1437"
  5987. "4FE1356D6D51C245E485B576625E7EC6"
  5988. "F44C42E9A637ED6B0BFF5CB6F406B7ED"
  5989. "EE386BFB5A899FA5AE9F24117C4B1FE6"
  5990. "49286651ECE65381FFFFFFFFFFFFFFFF"
  5991. };
  5992. WOLFSSL_ENTER("wolfSSL_DH_1024_prime");
  5993. /* Set prime into BN. Creates a new BN when bn is NULL. */
  5994. if (wolfSSL_BN_hex2bn(&bn, prm) != 1) {
  5995. WOLFSSL_ERROR_MSG("Error converting DH 1024 prime to big number");
  5996. bn = NULL;
  5997. }
  5998. return bn;
  5999. #else
  6000. (void)bn;
  6001. return NULL;
  6002. #endif
  6003. }
  6004. /* Returns a big number with the 1536-bit prime from RFC 3526.
  6005. *
  6006. * @param [in, out] bn If not NULL then this BN is set and returned.
  6007. * If NULL then a new BN is created, set and returned.
  6008. *
  6009. * @return NULL on failure.
  6010. * @return WOLFSSL_BIGNUM with value set to 1536-bit prime on success.
  6011. */
  6012. WOLFSSL_BIGNUM* wolfSSL_DH_1536_prime(WOLFSSL_BIGNUM* bn)
  6013. {
  6014. #if WOLFSSL_MAX_BN_BITS >= 1536
  6015. static const char prm[] = {
  6016. "FFFFFFFFFFFFFFFFC90FDAA22168C234"
  6017. "C4C6628B80DC1CD129024E088A67CC74"
  6018. "020BBEA63B139B22514A08798E3404DD"
  6019. "EF9519B3CD3A431B302B0A6DF25F1437"
  6020. "4FE1356D6D51C245E485B576625E7EC6"
  6021. "F44C42E9A637ED6B0BFF5CB6F406B7ED"
  6022. "EE386BFB5A899FA5AE9F24117C4B1FE6"
  6023. "49286651ECE45B3DC2007CB8A163BF05"
  6024. "98DA48361C55D39A69163FA8FD24CF5F"
  6025. "83655D23DCA3AD961C62F356208552BB"
  6026. "9ED529077096966D670C354E4ABC9804"
  6027. "F1746C08CA237327FFFFFFFFFFFFFFFF"
  6028. };
  6029. WOLFSSL_ENTER("wolfSSL_DH_1536_prime");
  6030. /* Set prime into BN. Creates a new BN when bn is NULL. */
  6031. if (wolfSSL_BN_hex2bn(&bn, prm) != 1) {
  6032. WOLFSSL_ERROR_MSG("Error converting DH 1536 prime to big number");
  6033. bn = NULL;
  6034. }
  6035. return bn;
  6036. #else
  6037. (void)bn;
  6038. return NULL;
  6039. #endif
  6040. }
  6041. /* Returns a big number with the 2048-bit prime from RFC 3526.
  6042. *
  6043. * @param [in, out] bn If not NULL then this BN is set and returned.
  6044. * If NULL then a new BN is created, set and returned.
  6045. *
  6046. * @return NULL on failure.
  6047. * @return WOLFSSL_BIGNUM with value set to 2048-bit prime on success.
  6048. */
  6049. WOLFSSL_BIGNUM* wolfSSL_DH_2048_prime(WOLFSSL_BIGNUM* bn)
  6050. {
  6051. #if WOLFSSL_MAX_BN_BITS >= 2048
  6052. static const char prm[] = {
  6053. "FFFFFFFFFFFFFFFFC90FDAA22168C234"
  6054. "C4C6628B80DC1CD129024E088A67CC74"
  6055. "020BBEA63B139B22514A08798E3404DD"
  6056. "EF9519B3CD3A431B302B0A6DF25F1437"
  6057. "4FE1356D6D51C245E485B576625E7EC6"
  6058. "F44C42E9A637ED6B0BFF5CB6F406B7ED"
  6059. "EE386BFB5A899FA5AE9F24117C4B1FE6"
  6060. "49286651ECE45B3DC2007CB8A163BF05"
  6061. "98DA48361C55D39A69163FA8FD24CF5F"
  6062. "83655D23DCA3AD961C62F356208552BB"
  6063. "9ED529077096966D670C354E4ABC9804"
  6064. "F1746C08CA18217C32905E462E36CE3B"
  6065. "E39E772C180E86039B2783A2EC07A28F"
  6066. "B5C55DF06F4C52C9DE2BCBF695581718"
  6067. "3995497CEA956AE515D2261898FA0510"
  6068. "15728E5A8AACAA68FFFFFFFFFFFFFFFF"
  6069. };
  6070. WOLFSSL_ENTER("wolfSSL_DH_2048_prime");
  6071. /* Set prime into BN. Creates a new BN when bn is NULL. */
  6072. if (wolfSSL_BN_hex2bn(&bn, prm) != 1) {
  6073. WOLFSSL_ERROR_MSG("Error converting DH 2048 prime to big number");
  6074. bn = NULL;
  6075. }
  6076. return bn;
  6077. #else
  6078. (void)bn;
  6079. return NULL;
  6080. #endif
  6081. }
  6082. /* Returns a big number with the 3072-bit prime from RFC 3526.
  6083. *
  6084. * @param [in, out] bn If not NULL then this BN is set and returned.
  6085. * If NULL then a new BN is created, set and returned.
  6086. *
  6087. * @return NULL on failure.
  6088. * @return WOLFSSL_BIGNUM with value set to 3072-bit prime on success.
  6089. */
  6090. WOLFSSL_BIGNUM* wolfSSL_DH_3072_prime(WOLFSSL_BIGNUM* bn)
  6091. {
  6092. #if WOLFSSL_MAX_BN_BITS >= 3072
  6093. static const char prm[] = {
  6094. "FFFFFFFFFFFFFFFFC90FDAA22168C234"
  6095. "C4C6628B80DC1CD129024E088A67CC74"
  6096. "020BBEA63B139B22514A08798E3404DD"
  6097. "EF9519B3CD3A431B302B0A6DF25F1437"
  6098. "4FE1356D6D51C245E485B576625E7EC6"
  6099. "F44C42E9A637ED6B0BFF5CB6F406B7ED"
  6100. "EE386BFB5A899FA5AE9F24117C4B1FE6"
  6101. "49286651ECE45B3DC2007CB8A163BF05"
  6102. "98DA48361C55D39A69163FA8FD24CF5F"
  6103. "83655D23DCA3AD961C62F356208552BB"
  6104. "9ED529077096966D670C354E4ABC9804"
  6105. "F1746C08CA18217C32905E462E36CE3B"
  6106. "E39E772C180E86039B2783A2EC07A28F"
  6107. "B5C55DF06F4C52C9DE2BCBF695581718"
  6108. "3995497CEA956AE515D2261898FA0510"
  6109. "15728E5A8AAAC42DAD33170D04507A33"
  6110. "A85521ABDF1CBA64ECFB850458DBEF0A"
  6111. "8AEA71575D060C7DB3970F85A6E1E4C7"
  6112. "ABF5AE8CDB0933D71E8C94E04A25619D"
  6113. "CEE3D2261AD2EE6BF12FFA06D98A0864"
  6114. "D87602733EC86A64521F2B18177B200C"
  6115. "BBE117577A615D6C770988C0BAD946E2"
  6116. "08E24FA074E5AB3143DB5BFCE0FD108E"
  6117. "4B82D120A93AD2CAFFFFFFFFFFFFFFFF"
  6118. };
  6119. WOLFSSL_ENTER("wolfSSL_DH_3072_prime");
  6120. /* Set prime into BN. Creates a new BN when bn is NULL. */
  6121. if (wolfSSL_BN_hex2bn(&bn, prm) != 1) {
  6122. WOLFSSL_ERROR_MSG("Error converting DH 3072 prime to big number");
  6123. bn = NULL;
  6124. }
  6125. return bn;
  6126. #else
  6127. (void)bn;
  6128. return NULL;
  6129. #endif
  6130. }
  6131. /* Returns a big number with the 4096-bit prime from RFC 3526.
  6132. *
  6133. * @param [in, out] bn If not NULL then this BN is set and returned.
  6134. * If NULL then a new BN is created, set and returned.
  6135. *
  6136. * @return NULL on failure.
  6137. * @return WOLFSSL_BIGNUM with value set to 4096-bit prime on success.
  6138. */
  6139. WOLFSSL_BIGNUM* wolfSSL_DH_4096_prime(WOLFSSL_BIGNUM* bn)
  6140. {
  6141. #if WOLFSSL_MAX_BN_BITS >= 4096
  6142. static const char prm[] = {
  6143. "FFFFFFFFFFFFFFFFC90FDAA22168C234"
  6144. "C4C6628B80DC1CD129024E088A67CC74"
  6145. "020BBEA63B139B22514A08798E3404DD"
  6146. "EF9519B3CD3A431B302B0A6DF25F1437"
  6147. "4FE1356D6D51C245E485B576625E7EC6"
  6148. "F44C42E9A637ED6B0BFF5CB6F406B7ED"
  6149. "EE386BFB5A899FA5AE9F24117C4B1FE6"
  6150. "49286651ECE45B3DC2007CB8A163BF05"
  6151. "98DA48361C55D39A69163FA8FD24CF5F"
  6152. "83655D23DCA3AD961C62F356208552BB"
  6153. "9ED529077096966D670C354E4ABC9804"
  6154. "F1746C08CA18217C32905E462E36CE3B"
  6155. "E39E772C180E86039B2783A2EC07A28F"
  6156. "B5C55DF06F4C52C9DE2BCBF695581718"
  6157. "3995497CEA956AE515D2261898FA0510"
  6158. "15728E5A8AAAC42DAD33170D04507A33"
  6159. "A85521ABDF1CBA64ECFB850458DBEF0A"
  6160. "8AEA71575D060C7DB3970F85A6E1E4C7"
  6161. "ABF5AE8CDB0933D71E8C94E04A25619D"
  6162. "CEE3D2261AD2EE6BF12FFA06D98A0864"
  6163. "D87602733EC86A64521F2B18177B200C"
  6164. "BBE117577A615D6C770988C0BAD946E2"
  6165. "08E24FA074E5AB3143DB5BFCE0FD108E"
  6166. "4B82D120A92108011A723C12A787E6D7"
  6167. "88719A10BDBA5B2699C327186AF4E23C"
  6168. "1A946834B6150BDA2583E9CA2AD44CE8"
  6169. "DBBBC2DB04DE8EF92E8EFC141FBECAA6"
  6170. "287C59474E6BC05D99B2964FA090C3A2"
  6171. "233BA186515BE7ED1F612970CEE2D7AF"
  6172. "B81BDD762170481CD0069127D5B05AA9"
  6173. "93B4EA988D8FDDC186FFB7DC90A6C08F"
  6174. "4DF435C934063199FFFFFFFFFFFFFFFF"
  6175. };
  6176. WOLFSSL_ENTER("wolfSSL_DH_4096_prime");
  6177. /* Set prime into BN. Creates a new BN when bn is NULL. */
  6178. if (wolfSSL_BN_hex2bn(&bn, prm) != 1) {
  6179. WOLFSSL_ERROR_MSG("Error converting DH 4096 prime to big number");
  6180. bn = NULL;
  6181. }
  6182. return bn;
  6183. #else
  6184. (void)bn;
  6185. return NULL;
  6186. #endif
  6187. }
  6188. /* Returns a big number with the 6144-bit prime from RFC 3526.
  6189. *
  6190. * @param [in, out] bn If not NULL then this BN is set and returned.
  6191. * If NULL then a new BN is created, set and returned.
  6192. *
  6193. * @return NULL on failure.
  6194. * @return WOLFSSL_BIGNUM with value set to 6144-bit prime on success.
  6195. */
  6196. WOLFSSL_BIGNUM* wolfSSL_DH_6144_prime(WOLFSSL_BIGNUM* bn)
  6197. {
  6198. #if WOLFSSL_MAX_BN_BITS >= 6144
  6199. static const char prm[] = {
  6200. "FFFFFFFFFFFFFFFFC90FDAA22168C234"
  6201. "C4C6628B80DC1CD129024E088A67CC74"
  6202. "020BBEA63B139B22514A08798E3404DD"
  6203. "EF9519B3CD3A431B302B0A6DF25F1437"
  6204. "4FE1356D6D51C245E485B576625E7EC6"
  6205. "F44C42E9A637ED6B0BFF5CB6F406B7ED"
  6206. "EE386BFB5A899FA5AE9F24117C4B1FE6"
  6207. "49286651ECE45B3DC2007CB8A163BF05"
  6208. "98DA48361C55D39A69163FA8FD24CF5F"
  6209. "83655D23DCA3AD961C62F356208552BB"
  6210. "9ED529077096966D670C354E4ABC9804"
  6211. "F1746C08CA18217C32905E462E36CE3B"
  6212. "E39E772C180E86039B2783A2EC07A28F"
  6213. "B5C55DF06F4C52C9DE2BCBF695581718"
  6214. "3995497CEA956AE515D2261898FA0510"
  6215. "15728E5A8AAAC42DAD33170D04507A33"
  6216. "A85521ABDF1CBA64ECFB850458DBEF0A"
  6217. "8AEA71575D060C7DB3970F85A6E1E4C7"
  6218. "ABF5AE8CDB0933D71E8C94E04A25619D"
  6219. "CEE3D2261AD2EE6BF12FFA06D98A0864"
  6220. "D87602733EC86A64521F2B18177B200C"
  6221. "BBE117577A615D6C770988C0BAD946E2"
  6222. "08E24FA074E5AB3143DB5BFCE0FD108E"
  6223. "4B82D120A92108011A723C12A787E6D7"
  6224. "88719A10BDBA5B2699C327186AF4E23C"
  6225. "1A946834B6150BDA2583E9CA2AD44CE8"
  6226. "DBBBC2DB04DE8EF92E8EFC141FBECAA6"
  6227. "287C59474E6BC05D99B2964FA090C3A2"
  6228. "233BA186515BE7ED1F612970CEE2D7AF"
  6229. "B81BDD762170481CD0069127D5B05AA9"
  6230. "93B4EA988D8FDDC186FFB7DC90A6C08F"
  6231. "4DF435C93402849236C3FAB4D27C7026"
  6232. "C1D4DCB2602646DEC9751E763DBA37BD"
  6233. "F8FF9406AD9E530EE5DB382F413001AE"
  6234. "B06A53ED9027D831179727B0865A8918"
  6235. "DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
  6236. "DB7F1447E6CC254B332051512BD7AF42"
  6237. "6FB8F401378CD2BF5983CA01C64B92EC"
  6238. "F032EA15D1721D03F482D7CE6E74FEF6"
  6239. "D55E702F46980C82B5A84031900B1C9E"
  6240. "59E7C97FBEC7E8F323A97A7E36CC88BE"
  6241. "0F1D45B7FF585AC54BD407B22B4154AA"
  6242. "CC8F6D7EBF48E1D814CC5ED20F8037E0"
  6243. "A79715EEF29BE32806A1D58BB7C5DA76"
  6244. "F550AA3D8A1FBFF0EB19CCB1A313D55C"
  6245. "DA56C9EC2EF29632387FE8D76E3C0468"
  6246. "043E8F663F4860EE12BF2D5B0B7474D6"
  6247. "E694F91E6DCC4024FFFFFFFFFFFFFFFF"
  6248. };
  6249. WOLFSSL_ENTER("wolfSSL_DH_6144_prime");
  6250. /* Set prime into BN. Creates a new BN when bn is NULL. */
  6251. if (wolfSSL_BN_hex2bn(&bn, prm) != 1) {
  6252. WOLFSSL_ERROR_MSG("Error converting DH 6144 prime to big number");
  6253. bn = NULL;
  6254. }
  6255. return bn;
  6256. #else
  6257. (void)bn;
  6258. return NULL;
  6259. #endif
  6260. }
  6261. /* Returns a big number with the 8192-bit prime from RFC 3526.
  6262. *
  6263. * @param [in, out] bn If not NULL then this BN is set and returned.
  6264. * If NULL then a new BN is created, set and returned.
  6265. *
  6266. * @return NULL on failure.
  6267. * @return WOLFSSL_BIGNUM with value set to 8192-bit prime on success.
  6268. */
  6269. WOLFSSL_BIGNUM* wolfSSL_DH_8192_prime(WOLFSSL_BIGNUM* bn)
  6270. {
  6271. #if WOLFSSL_MAX_BN_BITS >= 8192
  6272. static const char prm[] = {
  6273. "FFFFFFFFFFFFFFFFC90FDAA22168C234"
  6274. "C4C6628B80DC1CD129024E088A67CC74"
  6275. "020BBEA63B139B22514A08798E3404DD"
  6276. "EF9519B3CD3A431B302B0A6DF25F1437"
  6277. "4FE1356D6D51C245E485B576625E7EC6"
  6278. "F44C42E9A637ED6B0BFF5CB6F406B7ED"
  6279. "EE386BFB5A899FA5AE9F24117C4B1FE6"
  6280. "49286651ECE45B3DC2007CB8A163BF05"
  6281. "98DA48361C55D39A69163FA8FD24CF5F"
  6282. "83655D23DCA3AD961C62F356208552BB"
  6283. "9ED529077096966D670C354E4ABC9804"
  6284. "F1746C08CA18217C32905E462E36CE3B"
  6285. "E39E772C180E86039B2783A2EC07A28F"
  6286. "B5C55DF06F4C52C9DE2BCBF695581718"
  6287. "3995497CEA956AE515D2261898FA0510"
  6288. "15728E5A8AAAC42DAD33170D04507A33"
  6289. "A85521ABDF1CBA64ECFB850458DBEF0A"
  6290. "8AEA71575D060C7DB3970F85A6E1E4C7"
  6291. "ABF5AE8CDB0933D71E8C94E04A25619D"
  6292. "CEE3D2261AD2EE6BF12FFA06D98A0864"
  6293. "D87602733EC86A64521F2B18177B200C"
  6294. "BBE117577A615D6C770988C0BAD946E2"
  6295. "08E24FA074E5AB3143DB5BFCE0FD108E"
  6296. "4B82D120A92108011A723C12A787E6D7"
  6297. "88719A10BDBA5B2699C327186AF4E23C"
  6298. "1A946834B6150BDA2583E9CA2AD44CE8"
  6299. "DBBBC2DB04DE8EF92E8EFC141FBECAA6"
  6300. "287C59474E6BC05D99B2964FA090C3A2"
  6301. "233BA186515BE7ED1F612970CEE2D7AF"
  6302. "B81BDD762170481CD0069127D5B05AA9"
  6303. "93B4EA988D8FDDC186FFB7DC90A6C08F"
  6304. "4DF435C93402849236C3FAB4D27C7026"
  6305. "C1D4DCB2602646DEC9751E763DBA37BD"
  6306. "F8FF9406AD9E530EE5DB382F413001AE"
  6307. "B06A53ED9027D831179727B0865A8918"
  6308. "DA3EDBEBCF9B14ED44CE6CBACED4BB1B"
  6309. "DB7F1447E6CC254B332051512BD7AF42"
  6310. "6FB8F401378CD2BF5983CA01C64B92EC"
  6311. "F032EA15D1721D03F482D7CE6E74FEF6"
  6312. "D55E702F46980C82B5A84031900B1C9E"
  6313. "59E7C97FBEC7E8F323A97A7E36CC88BE"
  6314. "0F1D45B7FF585AC54BD407B22B4154AA"
  6315. "CC8F6D7EBF48E1D814CC5ED20F8037E0"
  6316. "A79715EEF29BE32806A1D58BB7C5DA76"
  6317. "F550AA3D8A1FBFF0EB19CCB1A313D55C"
  6318. "DA56C9EC2EF29632387FE8D76E3C0468"
  6319. "043E8F663F4860EE12BF2D5B0B7474D6"
  6320. "E694F91E6DBE115974A3926F12FEE5E4"
  6321. "38777CB6A932DF8CD8BEC4D073B931BA"
  6322. "3BC832B68D9DD300741FA7BF8AFC47ED"
  6323. "2576F6936BA424663AAB639C5AE4F568"
  6324. "3423B4742BF1C978238F16CBE39D652D"
  6325. "E3FDB8BEFC848AD922222E04A4037C07"
  6326. "13EB57A81A23F0C73473FC646CEA306B"
  6327. "4BCBC8862F8385DDFA9D4B7FA2C087E8"
  6328. "79683303ED5BDD3A062B3CF5B3A278A6"
  6329. "6D2A13F83F44F82DDF310EE074AB6A36"
  6330. "4597E899A0255DC164F31CC50846851D"
  6331. "F9AB48195DED7EA1B1D510BD7EE74D73"
  6332. "FAF36BC31ECFA268359046F4EB879F92"
  6333. "4009438B481C6CD7889A002ED5EE382B"
  6334. "C9190DA6FC026E479558E4475677E9AA"
  6335. "9E3050E2765694DFC81F56E880B96E71"
  6336. "60C980DD98EDD3DFFFFFFFFFFFFFFFFF"
  6337. };
  6338. WOLFSSL_ENTER("wolfSSL_DH_8192_prime");
  6339. /* Set prime into BN. Creates a new BN when bn is NULL. */
  6340. if (wolfSSL_BN_hex2bn(&bn, prm) != 1) {
  6341. WOLFSSL_ERROR_MSG("Error converting DH 8192 prime to big number");
  6342. bn = NULL;
  6343. }
  6344. return bn;
  6345. #else
  6346. (void)bn;
  6347. return NULL;
  6348. #endif
  6349. }
  6350. /*
  6351. * DH to/from bin APIs
  6352. */
  6353. #ifndef NO_CERTS
  6354. /* Load the DER encoded DH parameters/key into DH key.
  6355. *
  6356. * @param [in, out] dh DH key to load parameters into.
  6357. * @param [in] der Buffer holding DER encoded parameters data.
  6358. * @param [in, out] idx On in, index at which DH key DER data starts.
  6359. * On out, index after DH key DER data.
  6360. * @param [in] derSz Size of DER buffer in bytes.
  6361. *
  6362. * @return 0 on success.
  6363. * @return 1 when decoding DER or setting the external key fails.
  6364. */
  6365. static int wolfssl_dh_load_key(WOLFSSL_DH* dh, const unsigned char* der,
  6366. word32* idx, word32 derSz)
  6367. {
  6368. int err = 0;
  6369. #if !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  6370. int ret;
  6371. /* Decode DH parameters/key from DER. */
  6372. ret = wc_DhKeyDecode(der, idx, (DhKey*)dh->internal, derSz);
  6373. if (ret != 0) {
  6374. WOLFSSL_ERROR_MSG("DhKeyDecode() failed");
  6375. err = 1;
  6376. }
  6377. if (!err) {
  6378. /* wolfSSL DH key set. */
  6379. dh->inSet = 1;
  6380. /* Set the external DH key based on wolfSSL DH key. */
  6381. if (SetDhExternal(dh) != 1) {
  6382. WOLFSSL_ERROR_MSG("SetDhExternal failed");
  6383. err = 1;
  6384. }
  6385. }
  6386. #else
  6387. byte* p;
  6388. byte* g;
  6389. word32 pSz = MAX_DH_SIZE;
  6390. word32 gSz = MAX_DH_SIZE;
  6391. /* Only DH parameters supported. */
  6392. /* Load external and set internal. */
  6393. p = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  6394. g = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  6395. if ((p == NULL) || (g == NULL)) {
  6396. err = 1;
  6397. }
  6398. /* Extract the p and g as data from the DER encoded DH parameters. */
  6399. if ((!err) && (wc_DhParamsLoad(der + *idx, derSz - *idx, p, &pSz, g,
  6400. &gSz) < 0)) {
  6401. err = 1;
  6402. }
  6403. if (!err) {
  6404. /* Put p and g in as big numbers - free existing BNs. */
  6405. if (dh->p != NULL) {
  6406. wolfSSL_BN_free(dh->p);
  6407. dh->p = NULL;
  6408. }
  6409. if (dh->g != NULL) {
  6410. wolfSSL_BN_free(dh->g);
  6411. dh->g = NULL;
  6412. }
  6413. dh->p = wolfSSL_BN_bin2bn(p, (int)pSz, NULL);
  6414. dh->g = wolfSSL_BN_bin2bn(g, (int)gSz, NULL);
  6415. if (dh->p == NULL || dh->g == NULL) {
  6416. err = 1;
  6417. }
  6418. else {
  6419. /* External DH key parameters were set. */
  6420. dh->exSet = 1;
  6421. }
  6422. }
  6423. /* Set internal as the outside has been updated. */
  6424. if ((!err) && (SetDhInternal(dh) != 1)) {
  6425. WOLFSSL_ERROR_MSG("Unable to set internal DH structure");
  6426. err = 1;
  6427. }
  6428. if (!err) {
  6429. *idx += wolfssl_der_length(der + *idx, derSz - *idx);
  6430. }
  6431. XFREE(p, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  6432. XFREE(g, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  6433. #endif
  6434. return err;
  6435. }
  6436. #ifdef OPENSSL_ALL
  6437. #if !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  6438. /* Convert DER encoded DH parameters to a WOLFSSL_DH structure.
  6439. *
  6440. * @param [out] dh DH key to put parameters into. May be NULL.
  6441. * @param [in, out] pp Pointer to DER encoded DH parameters.
  6442. * Value updated to end of data when dh is not NULL.
  6443. * @param [in] length Length of data available in bytes.
  6444. *
  6445. * @return DH key on success.
  6446. * @return NULL on failure.
  6447. */
  6448. WOLFSSL_DH *wolfSSL_d2i_DHparams(WOLFSSL_DH** dh, const unsigned char** pp,
  6449. long length)
  6450. {
  6451. WOLFSSL_DH *newDh = NULL;
  6452. word32 idx = 0;
  6453. int err = 0;
  6454. WOLFSSL_ENTER("wolfSSL_d2i_DHparams");
  6455. /* Validate parameters. */
  6456. if ((pp == NULL) || (length <= 0)) {
  6457. WOLFSSL_ERROR_MSG("bad argument");
  6458. err = 1;
  6459. }
  6460. /* Create new DH key to return. */
  6461. if ((!err) && ((newDh = wolfSSL_DH_new()) == NULL)) {
  6462. WOLFSSL_ERROR_MSG("wolfSSL_DH_new() failed");
  6463. err = 1;
  6464. }
  6465. if ((!err) && (wolfssl_dh_load_key(newDh, *pp, &idx,
  6466. (word32)length) != 0)) {
  6467. WOLFSSL_ERROR_MSG("Loading DH parameters failed");
  6468. err = 1;
  6469. }
  6470. if ((!err) && (dh != NULL)) {
  6471. /* Return through parameter too. */
  6472. *dh = newDh;
  6473. /* Move buffer on by the used amount. */
  6474. *pp += idx;
  6475. }
  6476. if (err && (newDh != NULL)) {
  6477. /* Dispose of any created DH key. */
  6478. wolfSSL_DH_free(newDh);
  6479. newDh = NULL;
  6480. }
  6481. return newDh;
  6482. }
  6483. #endif /* !HAVE_FIPS || FIPS_VERSION_GT(2,0) */
  6484. /* Converts internal WOLFSSL_DH structure to DER encoded DH parameters.
  6485. *
  6486. * @params [in] dh DH key with parameters to encode.
  6487. * @params [in, out] out Pointer to buffer to encode into.
  6488. * When NULL or pointer to NULL, only length returned.
  6489. * @return 0 on error.
  6490. * @return Size of DER encoding in bytes on success.
  6491. */
  6492. int wolfSSL_i2d_DHparams(const WOLFSSL_DH *dh, unsigned char **out)
  6493. {
  6494. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GT(5,0)) && defined(WOLFSSL_DH_EXTRA)
  6495. /* Set length to an arbitrarily large value for wc_DhParamsToDer(). */
  6496. word32 len = (word32)-1;
  6497. int err = 0;
  6498. /* Validate parameters. */
  6499. if (dh == NULL) {
  6500. WOLFSSL_ERROR_MSG("Bad parameters");
  6501. err = 1;
  6502. }
  6503. /* Push external DH data into internal DH key if not set. */
  6504. if ((!err) && (!dh->inSet) && (SetDhInternal((WOLFSSL_DH*)dh) != 1)) {
  6505. WOLFSSL_ERROR_MSG("Bad DH set internal");
  6506. err = 1;
  6507. }
  6508. if (!err) {
  6509. int ret;
  6510. unsigned char* der = NULL;
  6511. /* Use *out when available otherwise NULL. */
  6512. if (out != NULL) {
  6513. der = *out;
  6514. }
  6515. /* Get length and/or encode. */
  6516. ret = wc_DhParamsToDer((DhKey*)dh->internal, der, &len);
  6517. /* Length of encoded data is returned on success. */
  6518. if (ret > 0) {
  6519. *out += len;
  6520. }
  6521. /* An error occurred unless only length returned. */
  6522. else if (ret != LENGTH_ONLY_E) {
  6523. err = 1;
  6524. }
  6525. }
  6526. /* Set return to 0 on error. */
  6527. if (err) {
  6528. len = 0;
  6529. }
  6530. return (int)len;
  6531. #else
  6532. word32 len;
  6533. int ret = 0;
  6534. int pSz;
  6535. int gSz;
  6536. WOLFSSL_ENTER("wolfSSL_i2d_DHparams");
  6537. /* Validate parameters. */
  6538. if (dh == NULL) {
  6539. WOLFSSL_ERROR_MSG("Bad parameters");
  6540. len = 0;
  6541. }
  6542. else {
  6543. /* SEQ <len>
  6544. * INT <len> [0x00] <prime>
  6545. * INT <len> [0x00] <generator>
  6546. * Integers have 0x00 prepended if the top bit of positive number is
  6547. * set.
  6548. */
  6549. /* Get total length of prime including any prepended zeros. */
  6550. pSz = mp_unsigned_bin_size((mp_int*)dh->p->internal) +
  6551. mp_leading_bit((mp_int*)dh->p->internal);
  6552. /* Get total length of generator including any prepended zeros. */
  6553. gSz = mp_unsigned_bin_size((mp_int*)dh->g->internal) +
  6554. mp_leading_bit((mp_int*)dh->g->internal);
  6555. /* Calculate length of data in sequence. */
  6556. len = 1 + ASN_LEN_SIZE(pSz) + pSz +
  6557. 1 + ASN_LEN_SIZE(gSz) + gSz;
  6558. /* Add in the length of the SEQUENCE. */
  6559. len += 1 + ASN_LEN_SIZE(len);
  6560. if ((out != NULL) && (*out != NULL)) {
  6561. /* Encode parameters. */
  6562. ret = StoreDHparams(*out, &len, (mp_int*)dh->p->internal,
  6563. (mp_int*)dh->g->internal);
  6564. if (ret != MP_OKAY) {
  6565. WOLFSSL_ERROR_MSG("StoreDHparams error");
  6566. len = 0;
  6567. }
  6568. else {
  6569. /* Move pointer on if encoded. */
  6570. *out += len;
  6571. }
  6572. }
  6573. }
  6574. return (int)len;
  6575. #endif
  6576. }
  6577. #endif /* OPENSSL_ALL */
  6578. #endif /* !NO_CERTS */
  6579. #endif /* OPENSSL_EXTRA */
  6580. #if defined(OPENSSL_EXTRA) || \
  6581. ((!defined(NO_BIO) || !defined(NO_FILESYSTEM)) && \
  6582. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL) || \
  6583. defined(WOLFSSL_MYSQL_COMPATIBLE))
  6584. /* Load the DER encoded DH parameters into DH key.
  6585. *
  6586. * @param [in, out] dh DH key to load parameters into.
  6587. * @param [in] derBuf Buffer holding DER encoded parameters data.
  6588. * @param [in] derSz Size of DER data in buffer in bytes.
  6589. *
  6590. * @return 1 on success.
  6591. * @return -1 when DH or derBuf is NULL,
  6592. * internal DH key in DH is NULL,
  6593. * derSz is 0 or less,
  6594. * error decoding DER data or
  6595. * setting external parameter values fails.
  6596. */
  6597. int wolfSSL_DH_LoadDer(WOLFSSL_DH* dh, const unsigned char* derBuf, int derSz)
  6598. {
  6599. int ret = 1;
  6600. word32 idx = 0;
  6601. /* Validate parameters. */
  6602. if ((dh == NULL) || (dh->internal == NULL) || (derBuf == NULL) ||
  6603. (derSz <= 0)) {
  6604. WOLFSSL_ERROR_MSG("Bad function arguments");
  6605. ret = -1;
  6606. }
  6607. if ((ret == 1) && (wolfssl_dh_load_key(dh, derBuf, &idx,
  6608. (word32)derSz) != 0)) {
  6609. WOLFSSL_ERROR_MSG("DH key decode failed");
  6610. ret = -1;
  6611. }
  6612. return ret;
  6613. }
  6614. #endif
  6615. /*
  6616. * DH PEM APIs
  6617. */
  6618. #if defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL) \
  6619. || defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_EXTRA)
  6620. #if !defined(NO_BIO) || !defined(NO_FILESYSTEM)
  6621. /* Create a DH key by reading the PEM encoded data from the BIO.
  6622. *
  6623. * @param [in] bio BIO object to read from.
  6624. * @param [in, out] dh DH key to use. May be NULL.
  6625. * @param [in] pem PEM data to decode.
  6626. * @param [in] pemSz Size of PEM data in bytes.
  6627. * @param [in] memAlloced Indicates that pem was allocated and is to be
  6628. * freed after use.
  6629. * @return DH key on success.
  6630. * @return NULL on failure.
  6631. */
  6632. static WOLFSSL_DH *wolfssl_dhparams_read_pem(WOLFSSL_DH **dh,
  6633. unsigned char* pem, int pemSz, int memAlloced)
  6634. {
  6635. WOLFSSL_DH* localDh = NULL;
  6636. DerBuffer *der = NULL;
  6637. int err = 0;
  6638. /* Convert PEM to DER assuming DH Parameter format. */
  6639. if ((!err) && (PemToDer(pem, pemSz, DH_PARAM_TYPE, &der, NULL, NULL,
  6640. NULL) < 0)) {
  6641. /* Convert PEM to DER assuming X9.42 DH Parameter format. */
  6642. if (PemToDer(pem, pemSz, X942_PARAM_TYPE, &der, NULL, NULL, NULL)
  6643. != 0) {
  6644. err = 1;
  6645. }
  6646. /* If Success on X9.42 DH format, clear error from failed DH format */
  6647. else {
  6648. unsigned long error;
  6649. CLEAR_ASN_NO_PEM_HEADER_ERROR(error);
  6650. }
  6651. }
  6652. if (memAlloced) {
  6653. /* PEM data no longer needed. */
  6654. XFREE(pem, NULL, DYNAMIC_TYPE_PEM);
  6655. }
  6656. if (!err) {
  6657. /* Use the DH key passed in or allocate a new one. */
  6658. if (dh != NULL) {
  6659. localDh = *dh;
  6660. }
  6661. if (localDh == NULL) {
  6662. localDh = wolfSSL_DH_new();
  6663. if (localDh == NULL) {
  6664. err = 1;
  6665. }
  6666. }
  6667. }
  6668. /* Load the DER encoded DH parameters from buffer into a DH key. */
  6669. if ((!err) && (wolfSSL_DH_LoadDer(localDh, der->buffer, (int)der->length)
  6670. != 1)) {
  6671. /* Free an allocated DH key. */
  6672. if ((dh == NULL) || (localDh != *dh)) {
  6673. wolfSSL_DH_free(localDh);
  6674. }
  6675. localDh = NULL;
  6676. err = 1;
  6677. }
  6678. /* Return the DH key on success. */
  6679. if ((!err) && (dh != NULL)) {
  6680. *dh = localDh;
  6681. }
  6682. /* Dispose of DER data. */
  6683. if (der != NULL) {
  6684. FreeDer(&der);
  6685. }
  6686. return localDh;
  6687. }
  6688. #endif /* !NO_BIO || !NO_FILESYSTEM */
  6689. #ifndef NO_BIO
  6690. /* Create a DH key by reading the PEM encoded data from the BIO.
  6691. *
  6692. * DH parameters are public data and are not expected to be encrypted.
  6693. *
  6694. * @param [in] bio BIO object to read from.
  6695. * @param [in, out] dh DH key to When pointer to
  6696. * NULL, a new DH key is created.
  6697. * @param [in] cb Password callback when PEM encrypted. Not used.
  6698. * @param [in] pass NUL terminated string for passphrase when PEM
  6699. * encrypted. Not used.
  6700. * @return DH key on success.
  6701. * @return NULL on failure.
  6702. */
  6703. WOLFSSL_DH *wolfSSL_PEM_read_bio_DHparams(WOLFSSL_BIO *bio, WOLFSSL_DH **dh,
  6704. wc_pem_password_cb *cb, void *pass)
  6705. {
  6706. WOLFSSL_DH* localDh = NULL;
  6707. int err = 0;
  6708. unsigned char* mem = NULL;
  6709. int size = 0;
  6710. int memAlloced = 0;
  6711. WOLFSSL_ENTER("wolfSSL_PEM_read_bio_DHparams");
  6712. (void)cb;
  6713. (void)pass;
  6714. /* Validate parameters. */
  6715. if (bio == NULL) {
  6716. WOLFSSL_ERROR_MSG("Bad Function Argument bio is NULL");
  6717. err = 1;
  6718. }
  6719. /* Get buffer of data from BIO or read data from the BIO into a new buffer.
  6720. */
  6721. if ((!err) && (wolfssl_read_bio(bio, (char**)&mem, &size, &memAlloced)
  6722. != 0)) {
  6723. err = 1;
  6724. }
  6725. if (!err) {
  6726. /* Create a DH key from the PEM - try two different headers. */
  6727. localDh = wolfssl_dhparams_read_pem(dh, mem, size, memAlloced);
  6728. }
  6729. return localDh;
  6730. }
  6731. #endif /* !NO_BIO */
  6732. #ifndef NO_FILESYSTEM
  6733. /* Read DH parameters from a file pointer into DH key.
  6734. *
  6735. * DH parameters are public data and are not expected to be encrypted.
  6736. *
  6737. * @param [in] fp File pointer to read DH parameter file from.
  6738. * @param [in, out] dh DH key with parameters if not NULL. When pointer to
  6739. * NULL, a new DH key is created.
  6740. * @param [in] cb Password callback when PEM encrypted. Not used.
  6741. * @param [in] pass NUL terminated string for passphrase when PEM
  6742. * encrypted. Not used.
  6743. *
  6744. * @return NULL on failure.
  6745. * @return DH key with parameters set on success.
  6746. */
  6747. WOLFSSL_DH* wolfSSL_PEM_read_DHparams(XFILE fp, WOLFSSL_DH** dh,
  6748. wc_pem_password_cb* cb, void* pass)
  6749. {
  6750. WOLFSSL_DH* localDh = NULL;
  6751. int err = 0;
  6752. unsigned char* mem = NULL;
  6753. int size = 0;
  6754. (void)cb;
  6755. (void)pass;
  6756. /* Read data from file pointer. */
  6757. if (wolfssl_read_file(fp, (char**)&mem, &size) != 0) {
  6758. err = 1;
  6759. }
  6760. if (!err) {
  6761. localDh = wolfssl_dhparams_read_pem(dh, mem, size, 1);
  6762. }
  6763. return localDh;
  6764. }
  6765. #endif /* !NO_FILESYSTEM */
  6766. #if defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  6767. /* Encoded parameter data in DH key as DER.
  6768. *
  6769. * @param [in, out] dh DH key object to encode.
  6770. * @param [out] out Buffer containing DER encoding.
  6771. * @param [in] heap Heap hint.
  6772. * @return <0 on error.
  6773. * @return Length of DER encoded DH parameters in bytes.
  6774. */
  6775. static int wolfssl_dhparams_to_der(WOLFSSL_DH* dh, unsigned char** out,
  6776. void* heap)
  6777. {
  6778. int ret = -1;
  6779. int err = 0;
  6780. byte* der = NULL;
  6781. word32 derSz;
  6782. DhKey* key;
  6783. (void)heap;
  6784. /* Set internal parameters based on external parameters. */
  6785. if ((dh->inSet == 0) && (SetDhInternal(dh) != 1)) {
  6786. WOLFSSL_ERROR_MSG("Unable to set internal DH structure");
  6787. err = 1;
  6788. }
  6789. if (!err) {
  6790. /* Use wolfSSL API to get length of DER encode DH parameters. */
  6791. key = (DhKey*)dh->internal;
  6792. ret = wc_DhParamsToDer(key, NULL, &derSz);
  6793. if (ret != LENGTH_ONLY_E) {
  6794. WOLFSSL_ERROR_MSG("Failed to get size of DH params");
  6795. err = 1;
  6796. }
  6797. }
  6798. if (!err) {
  6799. /* Allocate memory for DER encoding. */
  6800. der = (byte*)XMALLOC(derSz, heap, DYNAMIC_TYPE_TMP_BUFFER);
  6801. if (der == NULL) {
  6802. WOLFSSL_LEAVE("wolfssl_dhparams_to_der", MEMORY_E);
  6803. err = 1;
  6804. }
  6805. }
  6806. if (!err) {
  6807. /* Encode DH parameters into DER buffer. */
  6808. ret = wc_DhParamsToDer(key, der, &derSz);
  6809. if (ret < 0) {
  6810. WOLFSSL_ERROR_MSG("Failed to export DH params");
  6811. err = 1;
  6812. }
  6813. }
  6814. if (!err) {
  6815. *out = der;
  6816. der = NULL;
  6817. }
  6818. if (der != NULL) {
  6819. XFREE(der, heap, DYNAMIC_TYPE_TMP_BUFFER);
  6820. }
  6821. return ret;
  6822. }
  6823. /* Writes the DH parameters in PEM format from "dh" out to the file pointer
  6824. * passed in.
  6825. *
  6826. * @param [in] fp File pointer to write to.
  6827. * @param [in] dh DH key to write.
  6828. * @return 1 on success.
  6829. * @return 0 on failure.
  6830. */
  6831. int wolfSSL_PEM_write_DHparams(XFILE fp, WOLFSSL_DH* dh)
  6832. {
  6833. int ret = 1;
  6834. int derSz;
  6835. byte* derBuf = NULL;
  6836. void* heap = NULL;
  6837. WOLFSSL_ENTER("wolfSSL_PEM_write_DHparams");
  6838. /* Validate parameters. */
  6839. if ((fp == XBADFILE) || (dh == NULL)) {
  6840. WOLFSSL_ERROR_MSG("Bad Function Arguments");
  6841. ret = 0;
  6842. }
  6843. if (ret == 1) {
  6844. DhKey* key = (DhKey*)dh->internal;
  6845. if (key)
  6846. heap = key->heap;
  6847. if ((derSz = wolfssl_dhparams_to_der(dh, &derBuf, heap)) < 0) {
  6848. WOLFSSL_ERROR_MSG("DER encoding failed");
  6849. ret = 0;
  6850. }
  6851. if (derBuf == NULL) {
  6852. WOLFSSL_ERROR_MSG("DER encoding failed to get buffer");
  6853. ret = 0;
  6854. }
  6855. }
  6856. if ((ret == 1) && (der_write_to_file_as_pem(derBuf, derSz, fp,
  6857. DH_PARAM_TYPE, NULL) != WOLFSSL_SUCCESS)) {
  6858. ret = 0;
  6859. }
  6860. /* Dispose of DER buffer. */
  6861. XFREE(derBuf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  6862. WOLFSSL_LEAVE("wolfSSL_PEM_write_DHparams", ret);
  6863. return ret;
  6864. }
  6865. #endif /* WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  6866. #endif /* HAVE_LIGHTY || HAVE_STUNNEL || WOLFSSL_MYSQL_COMPATIBLE ||
  6867. * OPENSSL_EXTRA */
  6868. /*
  6869. * DH get/set APIs
  6870. */
  6871. #ifdef OPENSSL_EXTRA
  6872. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) \
  6873. || defined(WOLFSSL_OPENSSH) || defined(OPENSSL_EXTRA)
  6874. /* Set the members of DhKey into WOLFSSL_DH
  6875. * Specify elements to set via the 2nd parameter
  6876. *
  6877. * @param [in, out] dh DH key to synchronize.
  6878. * @param [in] elm Elements to synchronize.
  6879. * @return 1 on success.
  6880. * @return -1 on failure.
  6881. */
  6882. int SetDhExternal_ex(WOLFSSL_DH *dh, int elm)
  6883. {
  6884. int ret = 1;
  6885. DhKey *key = NULL;
  6886. WOLFSSL_ENTER("SetDhExternal_ex");
  6887. /* Validate parameters. */
  6888. if ((dh == NULL) || (dh->internal == NULL)) {
  6889. WOLFSSL_ERROR_MSG("dh key NULL error");
  6890. ret = -1;
  6891. }
  6892. if (ret == 1) {
  6893. /* Get the wolfSSL DH key. */
  6894. key = (DhKey*)dh->internal;
  6895. }
  6896. if ((ret == 1) && (elm & ELEMENT_P)) {
  6897. /* Set the prime. */
  6898. if (wolfssl_bn_set_value(&dh->p, &key->p) != 1) {
  6899. WOLFSSL_ERROR_MSG("dh param p error");
  6900. ret = -1;
  6901. }
  6902. }
  6903. if ((ret == 1) && (elm & ELEMENT_G)) {
  6904. /* Set the generator. */
  6905. if (wolfssl_bn_set_value(&dh->g, &key->g) != 1) {
  6906. WOLFSSL_ERROR_MSG("dh param g error");
  6907. ret = -1;
  6908. }
  6909. }
  6910. if ((ret == 1) && (elm & ELEMENT_Q)) {
  6911. /* Set the order. */
  6912. if (wolfssl_bn_set_value(&dh->q, &key->q) != 1) {
  6913. WOLFSSL_ERROR_MSG("dh param q error");
  6914. ret = -1;
  6915. }
  6916. }
  6917. #ifdef WOLFSSL_DH_EXTRA
  6918. if ((ret == 1) && (elm & ELEMENT_PRV)) {
  6919. /* Set the private key. */
  6920. if (wolfssl_bn_set_value(&dh->priv_key, &key->priv) != 1) {
  6921. WOLFSSL_ERROR_MSG("No DH Private Key");
  6922. ret = -1;
  6923. }
  6924. }
  6925. if ((ret == 1) && (elm & ELEMENT_PUB)) {
  6926. /* Set the public key. */
  6927. if (wolfssl_bn_set_value(&dh->pub_key, &key->pub) != 1) {
  6928. WOLFSSL_ERROR_MSG("No DH Public Key");
  6929. ret = -1;
  6930. }
  6931. }
  6932. #endif /* WOLFSSL_DH_EXTRA */
  6933. if (ret == 1) {
  6934. /* On success record that the external values have been set. */
  6935. dh->exSet = 1;
  6936. }
  6937. return ret;
  6938. }
  6939. /* Set the members of DhKey into WOLFSSL_DH
  6940. * DhKey was populated from wc_DhKeyDecode
  6941. * p, g, pub_key and priv_key are set.
  6942. *
  6943. * @param [in, out] dh DH key to synchronize.
  6944. * @return 1 on success.
  6945. * @return -1 on failure.
  6946. */
  6947. int SetDhExternal(WOLFSSL_DH *dh)
  6948. {
  6949. /* Assuming Q not required when using this API. */
  6950. int elements = ELEMENT_P | ELEMENT_G | ELEMENT_PUB | ELEMENT_PRV;
  6951. WOLFSSL_ENTER("SetDhExternal");
  6952. return SetDhExternal_ex(dh, elements);
  6953. }
  6954. #endif /* WOLFSSL_QT || OPENSSL_ALL || WOLFSSL_OPENSSH || OPENSSL_EXTRA */
  6955. /* Set the internal/wolfSSL DH key with data from the external parts.
  6956. *
  6957. * @param [in, out] dh DH key to synchronize.
  6958. * @return 1 on success.
  6959. * @return -1 on failure.
  6960. */
  6961. int SetDhInternal(WOLFSSL_DH* dh)
  6962. {
  6963. int ret = 1;
  6964. DhKey *key = NULL;
  6965. WOLFSSL_ENTER("SetDhInternal");
  6966. /* Validate parameters. */
  6967. if ((dh == NULL) || (dh->p == NULL) || (dh->g == NULL)) {
  6968. WOLFSSL_ERROR_MSG("Bad function arguments");
  6969. ret = -1;
  6970. }
  6971. if (ret == 1) {
  6972. /* Get the wolfSSL DH key. */
  6973. key = (DhKey*)dh->internal;
  6974. /* Clear out key and initialize. */
  6975. wc_FreeDhKey(key);
  6976. if (wc_InitDhKey(key) != 0) {
  6977. ret = -1;
  6978. }
  6979. }
  6980. if (ret == 1) {
  6981. /* Transfer prime. */
  6982. if (wolfssl_bn_get_value(dh->p, &key->p) != 1) {
  6983. ret = -1;
  6984. }
  6985. }
  6986. if (ret == 1) {
  6987. /* Transfer generator. */
  6988. if (wolfssl_bn_get_value(dh->g, &key->g) != 1) {
  6989. ret = -1;
  6990. }
  6991. }
  6992. #ifdef HAVE_FFDHE_Q
  6993. /* Transfer order if available. */
  6994. if ((ret == 1) && (dh->q != NULL)) {
  6995. if (wolfssl_bn_get_value(dh->q, &key->q) != 1) {
  6996. ret = -1;
  6997. }
  6998. }
  6999. #endif
  7000. #ifdef WOLFSSL_DH_EXTRA
  7001. /* Transfer private key if available. */
  7002. if ((ret == 1) && (dh->priv_key != NULL) &&
  7003. (!wolfSSL_BN_is_zero(dh->priv_key))) {
  7004. if (wolfssl_bn_get_value(dh->priv_key, &key->priv) != 1) {
  7005. ret = -1;
  7006. }
  7007. }
  7008. /* Transfer public key if available. */
  7009. if ((ret == 1) && (dh->pub_key != NULL) &&
  7010. (!wolfSSL_BN_is_zero(dh->pub_key))) {
  7011. if (wolfssl_bn_get_value(dh->pub_key, &key->pub) != 1) {
  7012. ret = -1;
  7013. }
  7014. }
  7015. #endif /* WOLFSSL_DH_EXTRA */
  7016. if (ret == 1) {
  7017. /* On success record that the internal values have been set. */
  7018. dh->inSet = 1;
  7019. }
  7020. return ret;
  7021. }
  7022. /* Get the size, in bytes, of the DH key.
  7023. *
  7024. * Return code compliant with OpenSSL.
  7025. *
  7026. * @param [in] dh DH key.
  7027. * @return -1 on error.
  7028. * @return Size of DH key in bytes on success.
  7029. */
  7030. int wolfSSL_DH_size(WOLFSSL_DH* dh)
  7031. {
  7032. int ret = -1;
  7033. WOLFSSL_ENTER("wolfSSL_DH_size");
  7034. /* Validate parameter. */
  7035. if (dh != NULL) {
  7036. /* Size of key is size of prime in bytes. */
  7037. ret = wolfSSL_BN_num_bytes(dh->p);
  7038. }
  7039. return ret;
  7040. }
  7041. /**
  7042. * Return parameters p, q and/or g of the DH key.
  7043. *
  7044. * @param [in] dh DH key to retrieve parameters from.
  7045. * @param [out] p Pointer to return prime in. May be NULL.
  7046. * @param [out] q Pointer to return order in. May be NULL.
  7047. * @param [out] g Pointer to return generator in. May be NULL.
  7048. */
  7049. void wolfSSL_DH_get0_pqg(const WOLFSSL_DH *dh, const WOLFSSL_BIGNUM **p,
  7050. const WOLFSSL_BIGNUM **q, const WOLFSSL_BIGNUM **g)
  7051. {
  7052. WOLFSSL_ENTER("wolfSSL_DH_get0_pqg");
  7053. if (dh != NULL) {
  7054. /* Return prime if required. */
  7055. if (p != NULL) {
  7056. *p = dh->p;
  7057. }
  7058. /* Return order if required. */
  7059. if (q != NULL) {
  7060. *q = dh->q;
  7061. }
  7062. /* Return generator if required. */
  7063. if (g != NULL) {
  7064. *g = dh->g;
  7065. }
  7066. }
  7067. }
  7068. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  7069. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  7070. #if defined(OPENSSL_ALL) || \
  7071. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  7072. /* Sets the parameters p, g and optionally q into the DH key.
  7073. *
  7074. * Ownership of p, q and g get taken over by "dh" on success and should be
  7075. * free'd with a call to wolfSSL_DH_free -- not individually.
  7076. *
  7077. * @param [in, out] dh DH key to set.
  7078. * @parma [in] p Prime value to set. May be NULL when value already
  7079. * present.
  7080. * @parma [in] q Order value to set. May be NULL.
  7081. * @parma [in] g Generator value to set. May be NULL when value already
  7082. * present.
  7083. * @return 1 on success.
  7084. * @return 0 on failure.
  7085. */
  7086. int wolfSSL_DH_set0_pqg(WOLFSSL_DH *dh, WOLFSSL_BIGNUM *p,
  7087. WOLFSSL_BIGNUM *q, WOLFSSL_BIGNUM *g)
  7088. {
  7089. int ret = 1;
  7090. WOLFSSL_ENTER("wolfSSL_DH_set0_pqg");
  7091. /* Validate parameters - q is optional. */
  7092. if (dh == NULL) {
  7093. WOLFSSL_ERROR_MSG("Bad function arguments");
  7094. ret = 0;
  7095. }
  7096. /* p can be NULL if we already have one set. */
  7097. if ((ret == 1) && (p == NULL) && (dh->p == NULL)) {
  7098. WOLFSSL_ERROR_MSG("Bad function arguments");
  7099. ret = 0;
  7100. }
  7101. /* g can be NULL if we already have one set. */
  7102. if ((ret == 1) && (g == NULL) && (dh->g == NULL)) {
  7103. WOLFSSL_ERROR_MSG("Bad function arguments");
  7104. ret = 0;
  7105. }
  7106. if (ret == 1) {
  7107. /* Invalidate internal key. */
  7108. dh->inSet = 0;
  7109. /* Free external representation of parameters and set with those passed
  7110. * in. */
  7111. if (p != NULL) {
  7112. wolfSSL_BN_free(dh->p);
  7113. dh->p = p;
  7114. }
  7115. if (q != NULL) {
  7116. wolfSSL_BN_free(dh->q);
  7117. dh->q = q;
  7118. }
  7119. if (g != NULL) {
  7120. wolfSSL_BN_free(dh->g);
  7121. dh->g = g;
  7122. }
  7123. /* External DH key parameters were set. */
  7124. dh->exSet = 1;
  7125. /* Set internal/wolfSSL DH key as well. */
  7126. if (SetDhInternal(dh) != 1) {
  7127. WOLFSSL_ERROR_MSG("Unable to set internal DH key");
  7128. /* Don't keep parameters on failure. */
  7129. dh->p = NULL;
  7130. dh->q = NULL;
  7131. dh->g = NULL;
  7132. /* Internal and external DH key not set. */
  7133. dh->inSet = 0;
  7134. dh->exSet = 0;
  7135. ret = 0;
  7136. }
  7137. }
  7138. return ret;
  7139. }
  7140. /* Set the length of the DH private key in bits.
  7141. *
  7142. * Length field is checked at generation.
  7143. *
  7144. * @param [in, out] dh DH key to set.
  7145. * @param [in] len Length of DH private key in bytes.
  7146. * @return 0 on failure.
  7147. * @return 1 on success.
  7148. */
  7149. int wolfSSL_DH_set_length(WOLFSSL_DH *dh, long len)
  7150. {
  7151. int ret = 1;
  7152. WOLFSSL_ENTER("wolfSSL_DH_set_length");
  7153. /* Validate parameter. */
  7154. if (dh == NULL) {
  7155. WOLFSSL_ERROR_MSG("Bad function arguments");
  7156. ret = 0;
  7157. }
  7158. else {
  7159. /* Store length. */
  7160. dh->length = (int)len;
  7161. }
  7162. return ret;
  7163. }
  7164. #endif /* OPENSSL_ALL || (v1.1.0 or later) */
  7165. #endif
  7166. /* Get the public and private keys requested.
  7167. *
  7168. * @param [in] dh DH key to get keys from.
  7169. * @param [out] pub_key Pointer to return public key in. May be NULL.
  7170. * @param [out] priv_key Pointer to return private key in. May be NULL.
  7171. */
  7172. void wolfSSL_DH_get0_key(const WOLFSSL_DH *dh, const WOLFSSL_BIGNUM **pub_key,
  7173. const WOLFSSL_BIGNUM **priv_key)
  7174. {
  7175. WOLFSSL_ENTER("wolfSSL_DH_get0_key");
  7176. /* Get only when valid DH passed in. */
  7177. if (dh != NULL) {
  7178. /* Return public key if required and available. */
  7179. if ((pub_key != NULL) && (dh->pub_key != NULL)) {
  7180. *pub_key = dh->pub_key;
  7181. }
  7182. /* Return private key if required and available. */
  7183. if ((priv_key != NULL) && (dh->priv_key != NULL)) {
  7184. *priv_key = dh->priv_key;
  7185. }
  7186. }
  7187. }
  7188. /* Set the public and/or private key.
  7189. *
  7190. * @param [in, out] dh DH key to have keys set into.
  7191. * @param [in] pub_key Public key to set. May be NULL.
  7192. * @param [in] priv_key Private key to set. May be NULL.
  7193. * @return 0 on failure.
  7194. * @return 1 on success.
  7195. */
  7196. int wolfSSL_DH_set0_key(WOLFSSL_DH *dh, WOLFSSL_BIGNUM *pub_key,
  7197. WOLFSSL_BIGNUM *priv_key)
  7198. {
  7199. int ret = 1;
  7200. #ifdef WOLFSSL_DH_EXTRA
  7201. DhKey *key = NULL;
  7202. #endif
  7203. WOLFSSL_ENTER("wolfSSL_DH_set0_key");
  7204. /* Validate parameters. */
  7205. if (dh == NULL) {
  7206. ret = 0;
  7207. }
  7208. #ifdef WOLFSSL_DH_EXTRA
  7209. else {
  7210. key = (DhKey*)dh->internal;
  7211. }
  7212. #endif
  7213. /* Replace public key when one passed in. */
  7214. if ((ret == 1) && (pub_key != NULL)) {
  7215. wolfSSL_BN_free(dh->pub_key);
  7216. dh->pub_key = pub_key;
  7217. #ifdef WOLFSSL_DH_EXTRA
  7218. if (wolfssl_bn_get_value(dh->pub_key, &key->pub) != 1) {
  7219. ret = 0;
  7220. }
  7221. #endif
  7222. }
  7223. /* Replace private key when one passed in. */
  7224. if ((ret == 1) && (priv_key != NULL)) {
  7225. wolfSSL_BN_clear_free(dh->priv_key);
  7226. dh->priv_key = priv_key;
  7227. #ifdef WOLFSSL_DH_EXTRA
  7228. if (wolfssl_bn_get_value(dh->priv_key, &key->priv) != 1) {
  7229. ret = 0;
  7230. }
  7231. #endif
  7232. }
  7233. return ret;
  7234. }
  7235. #endif /* OPENSSL_EXTRA */
  7236. /*
  7237. * DH check APIs
  7238. */
  7239. #ifdef OPENSSL_EXTRA
  7240. #ifndef NO_CERTS
  7241. #ifdef OPENSSL_ALL
  7242. /* Check whether BN number is a prime.
  7243. *
  7244. * @param [in] n Number to check.
  7245. * @param [out] isPrime MP_YES when prime and MP_NO when not.
  7246. * @return 1 on success.
  7247. * @return 0 on error.
  7248. */
  7249. static int wolfssl_dh_check_prime(WOLFSSL_BIGNUM* n, int* isPrime)
  7250. {
  7251. int ret = 1;
  7252. #ifdef WOLFSSL_SMALL_STACK
  7253. WC_RNG* tmpRng = NULL;
  7254. #else
  7255. WC_RNG tmpRng[1];
  7256. #endif
  7257. WC_RNG* rng;
  7258. int localRng;
  7259. /* Make an RNG with tmpRng or get global. */
  7260. rng = wolfssl_make_rng(tmpRng, &localRng);
  7261. if (rng == NULL) {
  7262. ret = 0;
  7263. }
  7264. if (ret == 1) {
  7265. mp_int* prime = (mp_int*)n->internal;
  7266. if (mp_prime_is_prime_ex(prime, 8, isPrime, rng) != 0) {
  7267. ret = 0;
  7268. }
  7269. /* Free local random number generator if created. */
  7270. if (localRng) {
  7271. wc_FreeRng(rng);
  7272. #ifdef WOLFSSL_SMALL_STACK
  7273. XFREE(rng, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7274. #endif
  7275. }
  7276. }
  7277. return ret;
  7278. }
  7279. /* Checks the Diffie-Hellman parameters.
  7280. *
  7281. * Checks that the generator and prime are available.
  7282. * Checks that the prime is prime.
  7283. * OpenSSL expects codes to be non-NULL.
  7284. *
  7285. * @param [in] dh DH key to check.
  7286. * @param [out] codes Codes of checks that failed.
  7287. * @return 1 on success.
  7288. * @return 0 when DH is NULL, there were errors or failed to create a random
  7289. * number generator.
  7290. */
  7291. int wolfSSL_DH_check(const WOLFSSL_DH *dh, int *codes)
  7292. {
  7293. int ret = 1;
  7294. int errors = 0;
  7295. WOLFSSL_ENTER("wolfSSL_DH_check");
  7296. /* Validate parameters. */
  7297. if (dh == NULL) {
  7298. ret = 0;
  7299. }
  7300. /* Check generator available. */
  7301. if ((ret == 1) && ((dh->g == NULL) || (dh->g->internal == NULL))) {
  7302. errors |= DH_NOT_SUITABLE_GENERATOR;
  7303. }
  7304. if (ret == 1) {
  7305. /* Check prime available. */
  7306. if ((dh->p == NULL) || (dh->p->internal == NULL)) {
  7307. errors |= DH_CHECK_P_NOT_PRIME;
  7308. }
  7309. else {
  7310. /* Test if dh->p is prime. */
  7311. int isPrime = MP_NO;
  7312. ret = wolfssl_dh_check_prime(dh->p, &isPrime);
  7313. /* Set error code if parameter p is not prime. */
  7314. if ((ret == 1) && (isPrime != MP_YES)) {
  7315. errors |= DH_CHECK_P_NOT_PRIME;
  7316. }
  7317. }
  7318. }
  7319. /* Return errors when user wants exact issues. */
  7320. if (codes != NULL) {
  7321. *codes = errors;
  7322. }
  7323. else if (errors) {
  7324. ret = 0;
  7325. }
  7326. return ret;
  7327. }
  7328. #endif /* OPENSSL_ALL */
  7329. #endif /* !NO_CERTS */
  7330. #endif /* OPENSSL_EXTRA */
  7331. /*
  7332. * DH generate APIs
  7333. */
  7334. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  7335. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  7336. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  7337. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  7338. #if defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST)
  7339. /* Generate DH parameters.
  7340. *
  7341. * @param [in] prime_len Length of prime in bits.
  7342. * @param [in] generator Gnerator value to use.
  7343. * @param [in] callback Called with progress information. Unused.
  7344. * @param [in] cb_arg User callback argument. Unused.
  7345. * @return NULL on failure.
  7346. * @return DH key on success.
  7347. */
  7348. WOLFSSL_DH *wolfSSL_DH_generate_parameters(int prime_len, int generator,
  7349. void (*callback) (int, int, void *), void *cb_arg)
  7350. {
  7351. WOLFSSL_DH* dh = NULL;
  7352. WOLFSSL_ENTER("wolfSSL_DH_generate_parameters");
  7353. /* Not supported by wolfSSl APIs. */
  7354. (void)callback;
  7355. (void)cb_arg;
  7356. /* Create an empty DH key. */
  7357. if ((dh = wolfSSL_DH_new()) == NULL) {
  7358. WOLFSSL_ERROR_MSG("wolfSSL_DH_new error");
  7359. }
  7360. /* Generate parameters into DH key. */
  7361. else if (wolfSSL_DH_generate_parameters_ex(dh, prime_len, generator, NULL)
  7362. != 1) {
  7363. WOLFSSL_ERROR_MSG("wolfSSL_DH_generate_parameters_ex error");
  7364. wolfSSL_DH_free(dh);
  7365. dh = NULL;
  7366. }
  7367. return dh;
  7368. }
  7369. /* Generate DH parameters.
  7370. *
  7371. * @param [in] dh DH key to generate parameters into.
  7372. * @param [in] prime_len Length of prime in bits.
  7373. * @param [in] generator Gnerator value to use.
  7374. * @param [in] callback Called with progress information. Unused.
  7375. * @param [in] cb_arg User callback argument. Unused.
  7376. * @return 0 on failure.
  7377. * @return 1 on success.
  7378. */
  7379. int wolfSSL_DH_generate_parameters_ex(WOLFSSL_DH* dh, int prime_len,
  7380. int generator, void (*callback) (int, int, void *))
  7381. {
  7382. int ret = 1;
  7383. DhKey* key;
  7384. #ifdef WOLFSSL_SMALL_STACK
  7385. WC_RNG* tmpRng = NULL;
  7386. #else
  7387. WC_RNG tmpRng[1];
  7388. #endif
  7389. WC_RNG* rng = NULL;
  7390. int localRng = 0;
  7391. WOLFSSL_ENTER("wolfSSL_DH_generate_parameters_ex");
  7392. /* Not supported by wolfSSL APIs. */
  7393. (void)callback;
  7394. (void)generator;
  7395. /* Validate parameters. */
  7396. if (dh == NULL) {
  7397. WOLFSSL_ERROR_MSG("Bad parameter");
  7398. ret = 0;
  7399. }
  7400. if (ret == 1) {
  7401. /* Make an RNG with tmpRng or get global. */
  7402. rng = wolfssl_make_rng(tmpRng, &localRng);
  7403. if (rng == NULL) {
  7404. WOLFSSL_ERROR_MSG("No RNG to use");
  7405. ret = 0;
  7406. }
  7407. }
  7408. if (ret == 1) {
  7409. /* Get internal/wolfSSL DH key. */
  7410. key = (DhKey*)dh->internal;
  7411. /* Clear out data from internal DH key. */
  7412. wc_FreeDhKey(key);
  7413. /* Re-initialize internal DH key. */
  7414. if (wc_InitDhKey(key) != 0) {
  7415. ret = 0;
  7416. }
  7417. }
  7418. if (ret == 1) {
  7419. /* Generate parameters into internal DH key. */
  7420. if (wc_DhGenerateParams(rng, prime_len, key) != 0) {
  7421. WOLFSSL_ERROR_MSG("wc_DhGenerateParams error");
  7422. ret = 0;
  7423. }
  7424. }
  7425. /* Free local random number generator if created. */
  7426. if (localRng) {
  7427. wc_FreeRng(rng);
  7428. #ifdef WOLFSSL_SMALL_STACK
  7429. XFREE(rng, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7430. #endif
  7431. }
  7432. if (ret == 1) {
  7433. /* Internal parameters set by generation. */
  7434. dh->inSet = 1;
  7435. WOLFSSL_MSG("wolfSSL does not support using a custom generator.");
  7436. /* Synchronize the external to the internal parameters. */
  7437. if (SetDhExternal(dh) != 1) {
  7438. WOLFSSL_ERROR_MSG("SetDhExternal error");
  7439. ret = 0;
  7440. }
  7441. }
  7442. return ret;
  7443. }
  7444. #endif /* WOLFSSL_KEY_GEN && !HAVE_SELFTEST */
  7445. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (HAVE_STUNNEL || WOLFSSL_NGINX ||
  7446. * HAVE_LIGHTY || WOLFSSL_HAPROXY || WOLFSSL_OPENSSH ||
  7447. * HAVE_SBLIM_SFCB)) */
  7448. #ifdef OPENSSL_EXTRA
  7449. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  7450. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  7451. /* Generate a public/private key pair base on parameters.
  7452. *
  7453. * @param [in, out] dh DH key to generate keys into.
  7454. * @return 1 on success.
  7455. * @return 0 on error.
  7456. */
  7457. int wolfSSL_DH_generate_key(WOLFSSL_DH* dh)
  7458. {
  7459. int ret = 1;
  7460. word32 pubSz = 0;
  7461. word32 privSz = 0;
  7462. int localRng = 0;
  7463. WC_RNG* rng = NULL;
  7464. #ifdef WOLFSSL_SMALL_STACK
  7465. WC_RNG* tmpRng = NULL;
  7466. #else
  7467. WC_RNG tmpRng[1];
  7468. #endif
  7469. unsigned char* pub = NULL;
  7470. unsigned char* priv = NULL;
  7471. WOLFSSL_ENTER("wolfSSL_DH_generate_key");
  7472. /* Validate parameters. */
  7473. if ((dh == NULL) || (dh->p == NULL) || (dh->g == NULL)) {
  7474. WOLFSSL_ERROR_MSG("Bad function arguments");
  7475. ret = 0;
  7476. }
  7477. /* Synchronize the external and internal parameters. */
  7478. if ((ret == 1) && (dh->inSet == 0) && (SetDhInternal(dh) != 1)) {
  7479. WOLFSSL_ERROR_MSG("Bad DH set internal");
  7480. ret = 0;
  7481. }
  7482. if (ret == 1) {
  7483. /* Make a new RNG or use global. */
  7484. rng = wolfssl_make_rng(tmpRng, &localRng);
  7485. /* Check we have a random number generator. */
  7486. if (rng == NULL) {
  7487. ret = 0;
  7488. }
  7489. }
  7490. if (ret == 1) {
  7491. /* Get the size of the prime in bytes. */
  7492. pubSz = (word32)wolfSSL_BN_num_bytes(dh->p);
  7493. if (pubSz == 0) {
  7494. WOLFSSL_ERROR_MSG("Prime parameter invalid");
  7495. ret = 0;
  7496. }
  7497. }
  7498. if (ret == 1) {
  7499. /* Private key size can be as much as the size of the prime. */
  7500. if (dh->length) {
  7501. privSz = (word32)(dh->length / 8); /* to bytes */
  7502. }
  7503. else {
  7504. privSz = pubSz;
  7505. }
  7506. /* Allocate public and private key arrays. */
  7507. pub = (unsigned char*)XMALLOC(pubSz, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  7508. priv = (unsigned char*)XMALLOC(privSz, NULL, DYNAMIC_TYPE_PRIVATE_KEY);
  7509. if (pub == NULL || priv == NULL) {
  7510. WOLFSSL_ERROR_MSG("Unable to malloc memory");
  7511. ret = 0;
  7512. }
  7513. }
  7514. if (ret == 1) {
  7515. /* Dispose of old public and private keys. */
  7516. wolfSSL_BN_free(dh->pub_key);
  7517. wolfSSL_BN_free(dh->priv_key);
  7518. /* Allocate new public and private keys. */
  7519. dh->pub_key = wolfSSL_BN_new();
  7520. dh->priv_key = wolfSSL_BN_new();
  7521. if (dh->pub_key == NULL) {
  7522. WOLFSSL_ERROR_MSG("Bad DH new pub");
  7523. ret = 0;
  7524. }
  7525. if (dh->priv_key == NULL) {
  7526. WOLFSSL_ERROR_MSG("Bad DH new priv");
  7527. ret = 0;
  7528. }
  7529. }
  7530. PRIVATE_KEY_UNLOCK();
  7531. /* Generate public and private keys into arrays. */
  7532. if ((ret == 1) && (wc_DhGenerateKeyPair((DhKey*)dh->internal, rng, priv,
  7533. &privSz, pub, &pubSz) < 0)) {
  7534. WOLFSSL_ERROR_MSG("Bad wc_DhGenerateKeyPair");
  7535. ret = 0;
  7536. }
  7537. /* Set public key from array. */
  7538. if ((ret == 1) && (wolfSSL_BN_bin2bn(pub, (int)pubSz, dh->pub_key) ==
  7539. NULL)) {
  7540. WOLFSSL_ERROR_MSG("Bad DH bn2bin error pub");
  7541. ret = 0;
  7542. }
  7543. /* Set private key from array. */
  7544. if ((ret == 1) && (wolfSSL_BN_bin2bn(priv, (int)privSz, dh->priv_key) ==
  7545. NULL)) {
  7546. WOLFSSL_ERROR_MSG("Bad DH bn2bin error priv");
  7547. ret = 0;
  7548. }
  7549. PRIVATE_KEY_LOCK();
  7550. if (localRng) {
  7551. /* Free an initialized local random number generator. */
  7552. wc_FreeRng(rng);
  7553. #ifdef WOLFSSL_SMALL_STACK
  7554. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  7555. #endif
  7556. }
  7557. /* Dispose of allocated data. */
  7558. XFREE(pub, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  7559. XFREE(priv, NULL, DYNAMIC_TYPE_PRIVATE_KEY);
  7560. return ret;
  7561. }
  7562. /* Compute the shared key from the private key and peer's public key.
  7563. *
  7564. * Return code compliant with OpenSSL.
  7565. * OpenSSL returns 0 when number of bits in p are smaller than minimum
  7566. * supported.
  7567. *
  7568. * @param [out] key Buffer to place shared key.
  7569. * @param [in] otherPub Peer's public key.
  7570. * @param [in] dh DH key containing private key.
  7571. * @return -1 on error.
  7572. * @return Size of shared secret in bytes on success.
  7573. */
  7574. int wolfSSL_DH_compute_key(unsigned char* key, const WOLFSSL_BIGNUM* otherPub,
  7575. WOLFSSL_DH* dh)
  7576. {
  7577. int ret = 0;
  7578. word32 keySz = 0;
  7579. int pubSz = MAX_DHKEY_SZ;
  7580. int privSz = MAX_DHKEY_SZ;
  7581. int sz;
  7582. #ifdef WOLFSSL_SMALL_STACK
  7583. unsigned char* pub = NULL;
  7584. unsigned char* priv = NULL;
  7585. #else
  7586. unsigned char pub [MAX_DHKEY_SZ];
  7587. unsigned char priv[MAX_DHKEY_SZ];
  7588. #endif
  7589. WOLFSSL_ENTER("wolfSSL_DH_compute_key");
  7590. /* Validate parameters. */
  7591. if ((dh == NULL) || (dh->priv_key == NULL) || (otherPub == NULL)) {
  7592. WOLFSSL_ERROR_MSG("Bad function arguments");
  7593. ret = -1;
  7594. }
  7595. /* Get the maximum size of computed DH key. */
  7596. if ((ret == 0) && ((keySz = (word32)DH_size(dh)) == 0)) {
  7597. WOLFSSL_ERROR_MSG("Bad DH_size");
  7598. ret = -1;
  7599. }
  7600. if (ret == 0) {
  7601. /* Validate the size of the private key. */
  7602. sz = wolfSSL_BN_num_bytes(dh->priv_key);
  7603. if (sz > (int)privSz) {
  7604. WOLFSSL_ERROR_MSG("Bad priv internal size");
  7605. ret = -1;
  7606. }
  7607. }
  7608. if (ret == 0) {
  7609. #ifdef WOLFSSL_SMALL_STACK
  7610. /* Keep real private key size to minimize amount allocated. */
  7611. privSz = sz;
  7612. #endif
  7613. /* Validate the size of the public key. */
  7614. sz = wolfSSL_BN_num_bytes(otherPub);
  7615. if (sz > pubSz) {
  7616. WOLFSSL_ERROR_MSG("Bad otherPub size");
  7617. ret = -1;
  7618. }
  7619. }
  7620. if (ret == 0) {
  7621. #ifdef WOLFSSL_SMALL_STACK
  7622. /* Allocate memory for the public key array. */
  7623. pub = (unsigned char*)XMALLOC((size_t)sz, NULL,
  7624. DYNAMIC_TYPE_PUBLIC_KEY);
  7625. if (pub == NULL)
  7626. ret = -1;
  7627. }
  7628. if (ret == 0) {
  7629. /* Allocate memory for the private key array. */
  7630. priv = (unsigned char*)XMALLOC((size_t)privSz, NULL,
  7631. DYNAMIC_TYPE_PRIVATE_KEY);
  7632. if (priv == NULL) {
  7633. ret = -1;
  7634. }
  7635. }
  7636. if (ret == 0) {
  7637. #endif
  7638. /* Get the private key into the array. */
  7639. privSz = wolfSSL_BN_bn2bin(dh->priv_key, priv);
  7640. if (privSz <= 0) {
  7641. ret = -1;
  7642. }
  7643. }
  7644. if (ret == 0) {
  7645. /* Get the public key into the array. */
  7646. pubSz = wolfSSL_BN_bn2bin(otherPub, pub);
  7647. if (privSz <= 0) {
  7648. ret = -1;
  7649. }
  7650. }
  7651. /* Synchronize the external into the internal parameters. */
  7652. if ((ret == 0) && ((dh->inSet == 0) && (SetDhInternal(dh) != 1))) {
  7653. WOLFSSL_ERROR_MSG("Bad DH set internal");
  7654. ret = -1;
  7655. }
  7656. PRIVATE_KEY_UNLOCK();
  7657. /* Calculate shared secret from private and public keys. */
  7658. if ((ret == 0) && (wc_DhAgree((DhKey*)dh->internal, key, &keySz, priv,
  7659. (word32)privSz, pub, (word32)pubSz) < 0)) {
  7660. WOLFSSL_ERROR_MSG("wc_DhAgree failed");
  7661. ret = -1;
  7662. }
  7663. if (ret == 0) {
  7664. /* Return actual length. */
  7665. ret = (int)keySz;
  7666. }
  7667. PRIVATE_KEY_LOCK();
  7668. #ifdef WOLFSSL_SMALL_STACK
  7669. if (priv != NULL)
  7670. #endif
  7671. {
  7672. /* Zeroize sensitive data. */
  7673. ForceZero(priv, (word32)privSz);
  7674. }
  7675. #ifdef WOLFSSL_SMALL_STACK
  7676. XFREE(pub, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  7677. XFREE(priv, NULL, DYNAMIC_TYPE_PRIVATE_KEY);
  7678. #endif
  7679. WOLFSSL_LEAVE("wolfSSL_DH_compute_key", ret);
  7680. return ret;
  7681. }
  7682. #endif /* !HAVE_FIPS || (HAVE_FIPS && !WOLFSSL_DH_EXTRA) ||
  7683. * HAVE_FIPS_VERSION > 2 */
  7684. #endif /* OPENSSL_EXTRA */
  7685. #endif /* NO_DH */
  7686. /*******************************************************************************
  7687. * END OF DH API
  7688. ******************************************************************************/
  7689. /*******************************************************************************
  7690. * START OF EC API
  7691. ******************************************************************************/
  7692. #ifdef HAVE_ECC
  7693. #if defined(OPENSSL_EXTRA)
  7694. /* Start EC_curve */
  7695. /* Get the NIST name for the numeric ID.
  7696. *
  7697. * @param [in] nid Numeric ID of an EC curve.
  7698. * @return String representing NIST name of EC curve on success.
  7699. * @return NULL on error.
  7700. */
  7701. const char* wolfSSL_EC_curve_nid2nist(int nid)
  7702. {
  7703. const char* name = NULL;
  7704. const WOLF_EC_NIST_NAME* nist_name;
  7705. /* Attempt to find the curve info matching the NID passed in. */
  7706. for (nist_name = kNistCurves; nist_name->name != NULL; nist_name++) {
  7707. if (nist_name->nid == nid) {
  7708. /* NID found - return name. */
  7709. name = nist_name->name;
  7710. break;
  7711. }
  7712. }
  7713. return name;
  7714. }
  7715. /* Get the numeric ID for the NIST name.
  7716. *
  7717. * @param [in] name NIST name of EC curve.
  7718. * @return NID matching NIST name on success.
  7719. * @return 0 on error.
  7720. */
  7721. int wolfSSL_EC_curve_nist2nid(const char* name)
  7722. {
  7723. int nid = 0;
  7724. const WOLF_EC_NIST_NAME* nist_name;
  7725. /* Attempt to find the curve info matching the NIST name passed in. */
  7726. for (nist_name = kNistCurves; nist_name->name != NULL; nist_name++) {
  7727. if (XSTRCMP(nist_name->name, name) == 0) {
  7728. /* Name found - return NID. */
  7729. nid = nist_name->nid;
  7730. break;
  7731. }
  7732. }
  7733. return nid;
  7734. }
  7735. #endif /* OPENSSL_EXTRA */
  7736. /* End EC_curve */
  7737. /* Start EC_METHOD */
  7738. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  7739. /* Get the EC method of the EC group object.
  7740. *
  7741. * wolfSSL doesn't use method tables. Implementation used is dependent upon
  7742. * the NID.
  7743. *
  7744. * @param [in] group EC group object.
  7745. * @return EC method.
  7746. */
  7747. const WOLFSSL_EC_METHOD* wolfSSL_EC_GROUP_method_of(
  7748. const WOLFSSL_EC_GROUP *group)
  7749. {
  7750. /* No method table used so just return the same object. */
  7751. return group;
  7752. }
  7753. /* Get field type for method.
  7754. *
  7755. * Only prime fields are supported.
  7756. *
  7757. * @param [in] meth EC method.
  7758. * @return X9.63 prime field NID on success.
  7759. * @return 0 on error.
  7760. */
  7761. int wolfSSL_EC_METHOD_get_field_type(const WOLFSSL_EC_METHOD *meth)
  7762. {
  7763. int nid = 0;
  7764. if (meth != NULL) {
  7765. /* Only field type supported by code base. */
  7766. nid = NID_X9_62_prime_field;
  7767. }
  7768. return nid;
  7769. }
  7770. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  7771. /* End EC_METHOD */
  7772. /* Start EC_GROUP */
  7773. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  7774. /* Converts ECC curve enum values in ecc_curve_id to the associated OpenSSL NID
  7775. * value.
  7776. *
  7777. * @param [in] n ECC curve id.
  7778. * @return ECC curve NID (OpenSSL compatible value).
  7779. */
  7780. int EccEnumToNID(int n)
  7781. {
  7782. WOLFSSL_ENTER("EccEnumToNID");
  7783. switch(n) {
  7784. case ECC_SECP192R1:
  7785. return NID_X9_62_prime192v1;
  7786. case ECC_PRIME192V2:
  7787. return NID_X9_62_prime192v2;
  7788. case ECC_PRIME192V3:
  7789. return NID_X9_62_prime192v3;
  7790. case ECC_PRIME239V1:
  7791. return NID_X9_62_prime239v1;
  7792. case ECC_PRIME239V2:
  7793. return NID_X9_62_prime239v2;
  7794. case ECC_PRIME239V3:
  7795. return NID_X9_62_prime239v3;
  7796. case ECC_SECP256R1:
  7797. return NID_X9_62_prime256v1;
  7798. case ECC_SECP112R1:
  7799. return NID_secp112r1;
  7800. case ECC_SECP112R2:
  7801. return NID_secp112r2;
  7802. case ECC_SECP128R1:
  7803. return NID_secp128r1;
  7804. case ECC_SECP128R2:
  7805. return NID_secp128r2;
  7806. case ECC_SECP160R1:
  7807. return NID_secp160r1;
  7808. case ECC_SECP160R2:
  7809. return NID_secp160r2;
  7810. case ECC_SECP224R1:
  7811. return NID_secp224r1;
  7812. case ECC_SECP384R1:
  7813. return NID_secp384r1;
  7814. case ECC_SECP521R1:
  7815. return NID_secp521r1;
  7816. case ECC_SECP160K1:
  7817. return NID_secp160k1;
  7818. case ECC_SECP192K1:
  7819. return NID_secp192k1;
  7820. case ECC_SECP224K1:
  7821. return NID_secp224k1;
  7822. case ECC_SECP256K1:
  7823. return NID_secp256k1;
  7824. case ECC_BRAINPOOLP160R1:
  7825. return NID_brainpoolP160r1;
  7826. case ECC_BRAINPOOLP192R1:
  7827. return NID_brainpoolP192r1;
  7828. case ECC_BRAINPOOLP224R1:
  7829. return NID_brainpoolP224r1;
  7830. case ECC_BRAINPOOLP256R1:
  7831. return NID_brainpoolP256r1;
  7832. case ECC_BRAINPOOLP320R1:
  7833. return NID_brainpoolP320r1;
  7834. case ECC_BRAINPOOLP384R1:
  7835. return NID_brainpoolP384r1;
  7836. case ECC_BRAINPOOLP512R1:
  7837. return NID_brainpoolP512r1;
  7838. #ifdef WOLFSSL_SM2
  7839. case ECC_SM2P256V1:
  7840. return NID_sm2;
  7841. #endif
  7842. default:
  7843. WOLFSSL_MSG("NID not found");
  7844. return -1;
  7845. }
  7846. }
  7847. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  7848. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  7849. /* Converts OpenSSL NID of EC curve to the enum value in ecc_curve_id
  7850. *
  7851. * Used by ecc_sets[].
  7852. *
  7853. * @param [in] n OpenSSL NID of EC curve.
  7854. * @return wolfCrypt EC curve id.
  7855. * @return -1 on error.
  7856. */
  7857. int NIDToEccEnum(int nid)
  7858. {
  7859. /* -1 on error. */
  7860. int id = -1;
  7861. WOLFSSL_ENTER("NIDToEccEnum");
  7862. switch (nid) {
  7863. case NID_X9_62_prime192v1:
  7864. id = ECC_SECP192R1;
  7865. break;
  7866. case NID_X9_62_prime192v2:
  7867. id = ECC_PRIME192V2;
  7868. break;
  7869. case NID_X9_62_prime192v3:
  7870. id = ECC_PRIME192V3;
  7871. break;
  7872. case NID_X9_62_prime239v1:
  7873. id = ECC_PRIME239V1;
  7874. break;
  7875. case NID_X9_62_prime239v2:
  7876. id = ECC_PRIME239V2;
  7877. break;
  7878. case NID_X9_62_prime239v3:
  7879. id = ECC_PRIME239V3;
  7880. break;
  7881. case NID_X9_62_prime256v1:
  7882. id = ECC_SECP256R1;
  7883. break;
  7884. case NID_secp112r1:
  7885. id = ECC_SECP112R1;
  7886. break;
  7887. case NID_secp112r2:
  7888. id = ECC_SECP112R2;
  7889. break;
  7890. case NID_secp128r1:
  7891. id = ECC_SECP128R1;
  7892. break;
  7893. case NID_secp128r2:
  7894. id = ECC_SECP128R2;
  7895. break;
  7896. case NID_secp160r1:
  7897. id = ECC_SECP160R1;
  7898. break;
  7899. case NID_secp160r2:
  7900. id = ECC_SECP160R2;
  7901. break;
  7902. case NID_secp224r1:
  7903. id = ECC_SECP224R1;
  7904. break;
  7905. case NID_secp384r1:
  7906. id = ECC_SECP384R1;
  7907. break;
  7908. case NID_secp521r1:
  7909. id = ECC_SECP521R1;
  7910. break;
  7911. case NID_secp160k1:
  7912. id = ECC_SECP160K1;
  7913. break;
  7914. case NID_secp192k1:
  7915. id = ECC_SECP192K1;
  7916. break;
  7917. case NID_secp224k1:
  7918. id = ECC_SECP224K1;
  7919. break;
  7920. case NID_secp256k1:
  7921. id = ECC_SECP256K1;
  7922. break;
  7923. case NID_brainpoolP160r1:
  7924. id = ECC_BRAINPOOLP160R1;
  7925. break;
  7926. case NID_brainpoolP192r1:
  7927. id = ECC_BRAINPOOLP192R1;
  7928. break;
  7929. case NID_brainpoolP224r1:
  7930. id = ECC_BRAINPOOLP224R1;
  7931. break;
  7932. case NID_brainpoolP256r1:
  7933. id = ECC_BRAINPOOLP256R1;
  7934. break;
  7935. case NID_brainpoolP320r1:
  7936. id = ECC_BRAINPOOLP320R1;
  7937. break;
  7938. case NID_brainpoolP384r1:
  7939. id = ECC_BRAINPOOLP384R1;
  7940. break;
  7941. case NID_brainpoolP512r1:
  7942. id = ECC_BRAINPOOLP512R1;
  7943. break;
  7944. default:
  7945. WOLFSSL_MSG("NID not found");
  7946. }
  7947. return id;
  7948. }
  7949. /* Set the fields of the EC group based on numeric ID.
  7950. *
  7951. * @param [in, out] group EC group.
  7952. * @param [in] nid Numeric ID of an EC curve.
  7953. */
  7954. static void ec_group_set_nid(WOLFSSL_EC_GROUP* group, int nid)
  7955. {
  7956. int eccEnum;
  7957. int realNid;
  7958. /* Convert ecc_curve_id enum to NID. */
  7959. if ((realNid = EccEnumToNID(nid)) != -1) {
  7960. /* ecc_curve_id enum passed in - have real NID value set. */
  7961. eccEnum = nid;
  7962. }
  7963. else {
  7964. /* NID passed in is OpenSSL type. */
  7965. realNid = nid;
  7966. /* Convert NID to ecc_curve_id enum. */
  7967. eccEnum = NIDToEccEnum(nid);
  7968. }
  7969. /* Set the numeric ID of the curve */
  7970. group->curve_nid = realNid;
  7971. /* Initialize index to -1 (i.e. wolfCrypt doesn't support curve). */
  7972. group->curve_idx = -1;
  7973. /* Find index and OID sum for curve if wolfCrypt supports it. */
  7974. if (eccEnum != -1) {
  7975. int i;
  7976. /* Find id and set the internal curve idx and OID sum. */
  7977. for (i = 0; ecc_sets[i].size != 0; i++) {
  7978. if (ecc_sets[i].id == eccEnum) {
  7979. /* Found id in wolfCrypt supported EC curves. */
  7980. group->curve_idx = i;
  7981. group->curve_oid = (int)ecc_sets[i].oidSum;
  7982. break;
  7983. }
  7984. }
  7985. }
  7986. }
  7987. /* Create a new EC group with the numeric ID for an EC curve.
  7988. *
  7989. * @param [in] nid Numeric ID of an EC curve.
  7990. * @return New, allocated EC group on success.
  7991. * @return NULL on error.
  7992. */
  7993. WOLFSSL_EC_GROUP* wolfSSL_EC_GROUP_new_by_curve_name(int nid)
  7994. {
  7995. int err = 0;
  7996. WOLFSSL_EC_GROUP* group;
  7997. WOLFSSL_ENTER("wolfSSL_EC_GROUP_new_by_curve_name");
  7998. /* Allocate EC group. */
  7999. group = (WOLFSSL_EC_GROUP*)XMALLOC(sizeof(WOLFSSL_EC_GROUP), NULL,
  8000. DYNAMIC_TYPE_ECC);
  8001. if (group == NULL) {
  8002. WOLFSSL_MSG("wolfSSL_EC_GROUP_new_by_curve_name malloc failure");
  8003. err = 1;
  8004. }
  8005. if (!err) {
  8006. /* Reset all fields. */
  8007. XMEMSET(group, 0, sizeof(WOLFSSL_EC_GROUP));
  8008. /* Set the fields of group based on the numeric ID. */
  8009. ec_group_set_nid(group, nid);
  8010. }
  8011. return group;
  8012. }
  8013. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  8014. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  8015. /* Dispose of the EC group.
  8016. *
  8017. * Cannot use group after this call.
  8018. *
  8019. * @param [in] group EC group to free.
  8020. */
  8021. void wolfSSL_EC_GROUP_free(WOLFSSL_EC_GROUP *group)
  8022. {
  8023. WOLFSSL_ENTER("wolfSSL_EC_GROUP_free");
  8024. /* Dispose of EC group. */
  8025. XFREE(group, NULL, DYNAMIC_TYPE_ECC);
  8026. }
  8027. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  8028. #ifdef OPENSSL_EXTRA
  8029. #ifndef NO_BIO
  8030. /* Creates an EC group from the DER encoding.
  8031. *
  8032. * Only named curves supported.
  8033. *
  8034. * @param [out] group Reference to EC group object.
  8035. * @param [in] in Buffer holding DER encoding of curve.
  8036. * @param [in] inSz Length of data in buffer.
  8037. * @return EC group on success.
  8038. * @return NULL on error.
  8039. */
  8040. static WOLFSSL_EC_GROUP* wolfssl_ec_group_d2i(WOLFSSL_EC_GROUP** group,
  8041. const unsigned char* in, long inSz)
  8042. {
  8043. int err = 0;
  8044. WOLFSSL_EC_GROUP* ret = NULL;
  8045. word32 idx = 0;
  8046. word32 oid = 0;
  8047. int id = 0;
  8048. /* Use the group passed in. */
  8049. if ((group != NULL) && (*group != NULL)) {
  8050. ret = *group;
  8051. }
  8052. /* Only support named curves. */
  8053. if (in[0] != ASN_OBJECT_ID) {
  8054. WOLFSSL_ERROR_MSG("Invalid or unsupported encoding");
  8055. err = 1;
  8056. }
  8057. /* Decode the OBJECT ID - expecting an EC curve OID. */
  8058. if ((!err) && (GetObjectId(in, &idx, &oid, oidCurveType, (word32)inSz) !=
  8059. 0)) {
  8060. err = 1;
  8061. }
  8062. if (!err) {
  8063. /* Get the internal ID for OID. */
  8064. id = wc_ecc_get_oid(oid, NULL, NULL);
  8065. if (id < 0) {
  8066. err = 1;
  8067. }
  8068. }
  8069. if (!err) {
  8070. /* Get the NID for the internal ID. */
  8071. int nid = EccEnumToNID(id);
  8072. if (ret == NULL) {
  8073. /* Create a new EC group with the numeric ID. */
  8074. ret = wolfSSL_EC_GROUP_new_by_curve_name(nid);
  8075. if (ret == NULL) {
  8076. err = 1;
  8077. }
  8078. }
  8079. else {
  8080. ec_group_set_nid(ret, nid);
  8081. }
  8082. }
  8083. if ((!err) && (group != NULL)) {
  8084. /* Return the EC group through reference. */
  8085. *group = ret;
  8086. }
  8087. if (err) {
  8088. if ((ret != NULL) && (ret != *group)) {
  8089. wolfSSL_EC_GROUP_free(ret);
  8090. }
  8091. ret = NULL;
  8092. }
  8093. return ret;
  8094. }
  8095. /* Creates a new EC group from the PEM encoding in the BIO.
  8096. *
  8097. * @param [in] bio BIO to read PEM encoding from.
  8098. * @param [out] group Reference to EC group object.
  8099. * @param [in] cb Password callback when PEM encrypted.
  8100. * @param [in] pass NUL terminated string for passphrase when PEM encrypted.
  8101. * @return EC group on success.
  8102. * @return NULL on error.
  8103. */
  8104. WOLFSSL_EC_GROUP* wolfSSL_PEM_read_bio_ECPKParameters(WOLFSSL_BIO* bio,
  8105. WOLFSSL_EC_GROUP** group, wc_pem_password_cb* cb, void* pass)
  8106. {
  8107. int err = 0;
  8108. WOLFSSL_EC_GROUP* ret = NULL;
  8109. DerBuffer* der = NULL;
  8110. int keyFormat = 0;
  8111. if (bio == NULL) {
  8112. err = 1;
  8113. }
  8114. /* Read parameters from BIO and convert PEM to DER. */
  8115. if ((!err) && (pem_read_bio_key(bio, cb, pass, ECC_PARAM_TYPE,
  8116. &keyFormat, &der) < 0)) {
  8117. err = 1;
  8118. }
  8119. if (!err) {
  8120. /* Create EC group from DER encoding. */
  8121. ret = wolfssl_ec_group_d2i(group, der->buffer, der->length);
  8122. if (ret == NULL) {
  8123. WOLFSSL_ERROR_MSG("Error loading DER buffer into WOLFSSL_EC_GROUP");
  8124. }
  8125. }
  8126. /* Dispose of any allocated data. */
  8127. FreeDer(&der);
  8128. return ret;
  8129. }
  8130. #endif /* !NO_BIO */
  8131. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  8132. /* Copy an EC group.
  8133. *
  8134. * Only used by wolfSSL_EC_KEY_dup at this time.
  8135. *
  8136. * @param [in, out] dst Destination EC group.
  8137. * @param [in] src Source EC group.
  8138. * @return 0 on success.
  8139. */
  8140. static int wolfssl_ec_group_copy(WOLFSSL_EC_GROUP* dst,
  8141. const WOLFSSL_EC_GROUP* src)
  8142. {
  8143. /* Copy the fields. */
  8144. dst->curve_idx = src->curve_idx;
  8145. dst->curve_nid = src->curve_nid;
  8146. dst->curve_oid = src->curve_oid;
  8147. return 0;
  8148. }
  8149. #endif /* OPENSSL_ALL && !NO_CERTS */
  8150. /* Copies ecc_key into new WOLFSSL_EC_GROUP object
  8151. *
  8152. * @param [in] src EC group to duplicate.
  8153. *
  8154. * @return EC group on success.
  8155. * @return NULL on error.
  8156. */
  8157. WOLFSSL_EC_GROUP* wolfSSL_EC_GROUP_dup(const WOLFSSL_EC_GROUP *src)
  8158. {
  8159. WOLFSSL_EC_GROUP* newGroup = NULL;
  8160. if (src != NULL) {
  8161. /* Create new group base on NID in original EC group. */
  8162. newGroup = wolfSSL_EC_GROUP_new_by_curve_name(src->curve_nid);
  8163. }
  8164. return newGroup;
  8165. }
  8166. /* Compare two EC groups.
  8167. *
  8168. * Return code compliant with OpenSSL.
  8169. *
  8170. * @param [in] a First EC group.
  8171. * @param [in] b Second EC group.
  8172. * @param [in] ctx Big number context to use when comparing fields. Unused.
  8173. *
  8174. * @return 0 if equal.
  8175. * @return 1 if not equal.
  8176. * @return -1 on error.
  8177. */
  8178. int wolfSSL_EC_GROUP_cmp(const WOLFSSL_EC_GROUP *a, const WOLFSSL_EC_GROUP *b,
  8179. WOLFSSL_BN_CTX *ctx)
  8180. {
  8181. int ret;
  8182. /* No BN operations performed. */
  8183. (void)ctx;
  8184. WOLFSSL_ENTER("wolfSSL_EC_GROUP_cmp");
  8185. /* Validate parameters. */
  8186. if ((a == NULL) || (b == NULL)) {
  8187. WOLFSSL_MSG("wolfSSL_EC_GROUP_cmp Bad arguments");
  8188. /* Return error value. */
  8189. ret = -1;
  8190. }
  8191. /* Compare NID and wolfSSL curve index. */
  8192. else {
  8193. /* 0 when same, 1 when not. */
  8194. ret = ((a->curve_nid == b->curve_nid) &&
  8195. (a->curve_idx == b->curve_idx)) ? 0 : 1;
  8196. }
  8197. return ret;
  8198. }
  8199. #ifndef NO_WOLFSSL_STUB
  8200. /* Set the ASN.1 flag that indicate encoding of curve.
  8201. *
  8202. * Stub function - flag not used elsewhere.
  8203. * Always encoded as named curve.
  8204. *
  8205. * @param [in] group EC group to modify.
  8206. * @param [in] flag ASN.1 flag to set. Valid values:
  8207. * OPENSSL_EC_EXPLICIT_CURVE, OPENSSL_EC_NAMED_CURVE
  8208. */
  8209. void wolfSSL_EC_GROUP_set_asn1_flag(WOLFSSL_EC_GROUP *group, int flag)
  8210. {
  8211. (void)group;
  8212. (void)flag;
  8213. WOLFSSL_ENTER("wolfSSL_EC_GROUP_set_asn1_flag");
  8214. WOLFSSL_STUB("EC_GROUP_set_asn1_flag");
  8215. }
  8216. #endif
  8217. /* Get the curve NID of the group.
  8218. *
  8219. * Return code compliant with OpenSSL.
  8220. *
  8221. * @param [in] group EC group.
  8222. * @return Curve NID on success.
  8223. * @return 0 on error.
  8224. */
  8225. int wolfSSL_EC_GROUP_get_curve_name(const WOLFSSL_EC_GROUP *group)
  8226. {
  8227. int nid = 0;
  8228. WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_curve_name");
  8229. if (group == NULL) {
  8230. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_curve_name Bad arguments");
  8231. }
  8232. else {
  8233. nid = group->curve_nid;
  8234. }
  8235. return nid;
  8236. }
  8237. /* Get the degree (curve size in bits) of the EC group.
  8238. *
  8239. * Return code compliant with OpenSSL.
  8240. *
  8241. * @return Degree of the curve on success.
  8242. * @return 0 on error.
  8243. */
  8244. int wolfSSL_EC_GROUP_get_degree(const WOLFSSL_EC_GROUP *group)
  8245. {
  8246. int degree = 0;
  8247. WOLFSSL_ENTER("wolfSSL_EC_GROUP_get_degree");
  8248. if (group == NULL) {
  8249. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_degree Bad arguments");
  8250. }
  8251. else {
  8252. switch (group->curve_nid) {
  8253. case NID_secp112r1:
  8254. case NID_secp112r2:
  8255. degree = 112;
  8256. break;
  8257. case NID_secp128r1:
  8258. case NID_secp128r2:
  8259. degree = 128;
  8260. break;
  8261. case NID_secp160k1:
  8262. case NID_secp160r1:
  8263. case NID_secp160r2:
  8264. case NID_brainpoolP160r1:
  8265. degree = 160;
  8266. break;
  8267. case NID_secp192k1:
  8268. case NID_brainpoolP192r1:
  8269. case NID_X9_62_prime192v1:
  8270. case NID_X9_62_prime192v2:
  8271. case NID_X9_62_prime192v3:
  8272. degree = 192;
  8273. break;
  8274. case NID_secp224k1:
  8275. case NID_secp224r1:
  8276. case NID_brainpoolP224r1:
  8277. degree = 224;
  8278. break;
  8279. case NID_X9_62_prime239v1:
  8280. case NID_X9_62_prime239v2:
  8281. case NID_X9_62_prime239v3:
  8282. degree = 239;
  8283. break;
  8284. case NID_secp256k1:
  8285. case NID_brainpoolP256r1:
  8286. case NID_X9_62_prime256v1:
  8287. degree = 256;
  8288. break;
  8289. case NID_brainpoolP320r1:
  8290. degree = 320;
  8291. break;
  8292. case NID_secp384r1:
  8293. case NID_brainpoolP384r1:
  8294. degree = 384;
  8295. break;
  8296. case NID_brainpoolP512r1:
  8297. degree = 512;
  8298. break;
  8299. case NID_secp521r1:
  8300. degree = 521;
  8301. break;
  8302. }
  8303. }
  8304. return degree;
  8305. }
  8306. #endif /* OPENSSL_EXTRA */
  8307. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)
  8308. /* Get the length of the order in bits of the EC group.
  8309. *
  8310. * TODO: consider switch statement or calculating directly from hex string
  8311. * array instead of using mp_int.
  8312. *
  8313. * @param [in] group EC group.
  8314. * @return Length of order in bits on success.
  8315. * @return 0 on error.
  8316. */
  8317. int wolfSSL_EC_GROUP_order_bits(const WOLFSSL_EC_GROUP *group)
  8318. {
  8319. int ret = 0;
  8320. #ifdef WOLFSSL_SMALL_STACK
  8321. mp_int *order = NULL;
  8322. #else
  8323. mp_int order[1];
  8324. #endif
  8325. /* Validate parameter. */
  8326. if ((group == NULL) || (group->curve_idx < 0)) {
  8327. WOLFSSL_MSG("wolfSSL_EC_GROUP_order_bits NULL error");
  8328. ret = -1;
  8329. }
  8330. #ifdef WOLFSSL_SMALL_STACK
  8331. if (ret == 0) {
  8332. /* Allocate memory for mp_int that will hold order value. */
  8333. order = (mp_int *)XMALLOC(sizeof(*order), NULL,
  8334. DYNAMIC_TYPE_TMP_BUFFER);
  8335. if (order == NULL) {
  8336. ret = -1;
  8337. }
  8338. }
  8339. #endif
  8340. if (ret == 0) {
  8341. /* Initialize mp_int. */
  8342. ret = mp_init(order);
  8343. }
  8344. if (ret == 0) {
  8345. /* Read hex string of order from wolfCrypt array of curves. */
  8346. ret = mp_read_radix(order, ecc_sets[group->curve_idx].order,
  8347. MP_RADIX_HEX);
  8348. if (ret == 0) {
  8349. /* Get bits of order. */
  8350. ret = mp_count_bits(order);
  8351. }
  8352. /* Clear and free mp_int. */
  8353. mp_clear(order);
  8354. }
  8355. #ifdef WOLFSSL_SMALL_STACK
  8356. /* Deallocate order. */
  8357. XFREE(order, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8358. #endif
  8359. /* Convert error code to length of 0. */
  8360. if (ret < 0) {
  8361. ret = 0;
  8362. }
  8363. return ret;
  8364. }
  8365. #endif /* OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL */
  8366. #if defined(OPENSSL_EXTRA)
  8367. /* Get the order of the group as a BN.
  8368. *
  8369. * Return code compliant with OpenSSL.
  8370. *
  8371. * @param [in] group EC group.
  8372. * @param [in, out] order BN to hold order value.
  8373. * @param [in] ctx Context to use for BN operations. Unused.
  8374. * @return 1 on success.
  8375. * @return 0 on error.
  8376. */
  8377. int wolfSSL_EC_GROUP_get_order(const WOLFSSL_EC_GROUP *group,
  8378. WOLFSSL_BIGNUM *order, WOLFSSL_BN_CTX *ctx)
  8379. {
  8380. int ret = 1;
  8381. mp_int* mp = NULL;
  8382. /* No BN operations performed - done with mp_int in BN. */
  8383. (void)ctx;
  8384. /* Validate parameters. */
  8385. if ((group == NULL) || (order == NULL) || (order->internal == NULL)) {
  8386. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order NULL error");
  8387. ret = 0;
  8388. }
  8389. if (ret == 1) {
  8390. mp = (mp_int*)order->internal;
  8391. }
  8392. /* Initialize */
  8393. if ((ret == 1) && (mp_init(mp) != MP_OKAY)) {
  8394. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_init failure");
  8395. ret = 0;
  8396. }
  8397. /* Read hex string of order from wolfCrypt array of curves. */
  8398. if ((ret == 1) && (mp_read_radix(mp, ecc_sets[group->curve_idx].order,
  8399. MP_RADIX_HEX) != MP_OKAY)) {
  8400. WOLFSSL_MSG("wolfSSL_EC_GROUP_get_order mp_read order failure");
  8401. /* Zero out any partial value but don't free. */
  8402. mp_zero(mp);
  8403. ret = 0;
  8404. }
  8405. return ret;
  8406. }
  8407. #endif /* OPENSSL_EXTRA */
  8408. /* End EC_GROUP */
  8409. /* Start EC_POINT */
  8410. #if defined(OPENSSL_EXTRA)
  8411. /* Set data of EC point into internal, wolfCrypt EC point object.
  8412. *
  8413. * EC_POINT Openssl -> WolfSSL
  8414. *
  8415. * @param [in, out] p EC point to update.
  8416. * @return 1 on success.
  8417. * @return -1 on failure.
  8418. */
  8419. static int ec_point_internal_set(WOLFSSL_EC_POINT *p)
  8420. {
  8421. int ret = 1;
  8422. WOLFSSL_ENTER("ec_point_internal_set");
  8423. /* Validate parameter. */
  8424. if ((p == NULL) || (p->internal == NULL)) {
  8425. WOLFSSL_MSG("ECPoint NULL error");
  8426. ret = -1;
  8427. }
  8428. else {
  8429. /* Get internal point as a wolfCrypt EC point. */
  8430. ecc_point* point = (ecc_point*)p->internal;
  8431. /* Set X ordinate if available. */
  8432. if ((p->X != NULL) && (wolfssl_bn_get_value(p->X, point->x) != 1)) {
  8433. WOLFSSL_MSG("ecc point X error");
  8434. ret = -1;
  8435. }
  8436. /* Set Y ordinate if available. */
  8437. if ((ret == 1) && (p->Y != NULL) && (wolfssl_bn_get_value(p->Y,
  8438. point->y) != 1)) {
  8439. WOLFSSL_MSG("ecc point Y error");
  8440. ret = -1;
  8441. }
  8442. /* Set Z ordinate if available. */
  8443. if ((ret == 1) && (p->Z != NULL) && (wolfssl_bn_get_value(p->Z,
  8444. point->z) != 1)) {
  8445. WOLFSSL_MSG("ecc point Z error");
  8446. ret = -1;
  8447. }
  8448. /* Internal values set when operations succeeded. */
  8449. p->inSet = (ret == 1);
  8450. }
  8451. return ret;
  8452. }
  8453. /* Set data of internal, wolfCrypt EC point object into EC point.
  8454. *
  8455. * EC_POINT WolfSSL -> OpenSSL
  8456. *
  8457. * @param [in, out] p EC point to update.
  8458. * @return 1 on success.
  8459. * @return -1 on failure.
  8460. */
  8461. static int ec_point_external_set(WOLFSSL_EC_POINT *p)
  8462. {
  8463. int ret = 1;
  8464. WOLFSSL_ENTER("ec_point_external_set");
  8465. /* Validate parameter. */
  8466. if ((p == NULL) || (p->internal == NULL)) {
  8467. WOLFSSL_MSG("ECPoint NULL error");
  8468. ret = -1;
  8469. }
  8470. else {
  8471. /* Get internal point as a wolfCrypt EC point. */
  8472. ecc_point* point = (ecc_point*)p->internal;
  8473. /* Set X ordinate. */
  8474. if (wolfssl_bn_set_value(&p->X, point->x) != 1) {
  8475. WOLFSSL_MSG("ecc point X error");
  8476. ret = -1;
  8477. }
  8478. /* Set Y ordinate. */
  8479. if ((ret == 1) && (wolfssl_bn_set_value(&p->Y, point->y) != 1)) {
  8480. WOLFSSL_MSG("ecc point Y error");
  8481. ret = -1;
  8482. }
  8483. /* Set Z ordinate. */
  8484. if ((ret == 1) && (wolfssl_bn_set_value(&p->Z, point->z) != 1)) {
  8485. WOLFSSL_MSG("ecc point Z error");
  8486. ret = -1;
  8487. }
  8488. /* External values set when operations succeeded. */
  8489. p->exSet = (ret == 1);
  8490. }
  8491. return ret;
  8492. }
  8493. /* Setup internals of EC point.
  8494. *
  8495. * Assumes point is not NULL.
  8496. *
  8497. * @param [in, out] point EC point to update.
  8498. * @return 1 on success.
  8499. * @return 0 on failure.
  8500. */
  8501. static int ec_point_setup(const WOLFSSL_EC_POINT *point) {
  8502. int ret = 1;
  8503. /* Check if internal values need setting. */
  8504. if (!point->inSet) {
  8505. WOLFSSL_MSG("No ECPoint internal set, do it");
  8506. /* Forcing to non-constant type to update internals. */
  8507. if (ec_point_internal_set((WOLFSSL_EC_POINT *)point) != 1) {
  8508. WOLFSSL_MSG("ec_point_internal_set failed");
  8509. ret = 0;
  8510. }
  8511. }
  8512. return ret;
  8513. }
  8514. /* Create a new EC point from the group.
  8515. *
  8516. * @param [in] group EC group.
  8517. * @return EC point on success.
  8518. * @return NULL on error.
  8519. */
  8520. WOLFSSL_EC_POINT* wolfSSL_EC_POINT_new(const WOLFSSL_EC_GROUP* group)
  8521. {
  8522. int err = 0;
  8523. WOLFSSL_EC_POINT* point = NULL;
  8524. WOLFSSL_ENTER("wolfSSL_EC_POINT_new");
  8525. /* Validate parameter. */
  8526. if (group == NULL) {
  8527. WOLFSSL_MSG("wolfSSL_EC_POINT_new NULL error");
  8528. err = 1;
  8529. }
  8530. if (!err) {
  8531. /* Allocate memory for new EC point. */
  8532. point = (WOLFSSL_EC_POINT*)XMALLOC(sizeof(WOLFSSL_EC_POINT), NULL,
  8533. DYNAMIC_TYPE_ECC);
  8534. if (point == NULL) {
  8535. WOLFSSL_MSG("wolfSSL_EC_POINT_new malloc ecc point failure");
  8536. err = 1;
  8537. }
  8538. }
  8539. if (!err) {
  8540. /* Clear fields of EC point. */
  8541. XMEMSET(point, 0, sizeof(WOLFSSL_EC_POINT));
  8542. /* Allocate internal EC point. */
  8543. point->internal = wc_ecc_new_point();
  8544. if (point->internal == NULL) {
  8545. WOLFSSL_MSG("ecc_new_point failure");
  8546. err = 1;
  8547. }
  8548. }
  8549. if (err) {
  8550. XFREE(point, NULL, DYNAMIC_TYPE_ECC);
  8551. point = NULL;
  8552. }
  8553. return point;
  8554. }
  8555. #endif /* OPENSSL_EXTRA */
  8556. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  8557. /* Dispose of the EC point.
  8558. *
  8559. * Cannot use point after this call.
  8560. *
  8561. * @param [in, out] point EC point to free.
  8562. */
  8563. void wolfSSL_EC_POINT_free(WOLFSSL_EC_POINT *point)
  8564. {
  8565. WOLFSSL_ENTER("wolfSSL_EC_POINT_free");
  8566. if (point != NULL) {
  8567. if (point->internal != NULL) {
  8568. wc_ecc_del_point((ecc_point*)point->internal);
  8569. point->internal = NULL;
  8570. }
  8571. /* Free ordinates. */
  8572. wolfSSL_BN_free(point->X);
  8573. wolfSSL_BN_free(point->Y);
  8574. wolfSSL_BN_free(point->Z);
  8575. /* Clear fields. */
  8576. point->X = NULL;
  8577. point->Y = NULL;
  8578. point->Z = NULL;
  8579. point->inSet = 0;
  8580. point->exSet = 0;
  8581. /* Dispose of EC point. */
  8582. XFREE(point, NULL, DYNAMIC_TYPE_ECC);
  8583. }
  8584. }
  8585. #endif /* OPENSSL_EXTRA || OPENSSL_EXTRA_X509_SMALL */
  8586. #ifdef OPENSSL_EXTRA
  8587. /* Clear and dispose of the EC point.
  8588. *
  8589. * Cannot use point after this call.
  8590. *
  8591. * @param [in, out] point EC point to free.
  8592. */
  8593. void wolfSSL_EC_POINT_clear_free(WOLFSSL_EC_POINT *point)
  8594. {
  8595. WOLFSSL_ENTER("wolfSSL_EC_POINT_clear_free");
  8596. if (point != NULL) {
  8597. if (point->internal != NULL) {
  8598. /* Force internal point to be zeros. */
  8599. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  8600. wc_ecc_forcezero_point((ecc_point*)point->internal);
  8601. #else
  8602. ecc_point* p = (ecc_point*)point->internal;
  8603. mp_forcezero(p->x);
  8604. mp_forcezero(p->y);
  8605. mp_forcezero(p->z);
  8606. #endif
  8607. wc_ecc_del_point((ecc_point*)point->internal);
  8608. point->internal = NULL;
  8609. }
  8610. /* Clear the ordinates before freeing. */
  8611. wolfSSL_BN_clear_free(point->X);
  8612. wolfSSL_BN_clear_free(point->Y);
  8613. wolfSSL_BN_clear_free(point->Z);
  8614. /* Clear fields. */
  8615. point->X = NULL;
  8616. point->Y = NULL;
  8617. point->Z = NULL;
  8618. point->inSet = 0;
  8619. point->exSet = 0;
  8620. /* Dispose of EC point. */
  8621. XFREE(point, NULL, DYNAMIC_TYPE_ECC);
  8622. }
  8623. }
  8624. /* Print out the internals of EC point in debug and when logging callback set.
  8625. *
  8626. * Not an OpenSSL API.
  8627. *
  8628. * TODO: Use WOLFSSL_MSG_EX()?
  8629. *
  8630. * @param [in] msg Message to prepend.
  8631. * @param [in] point EC point to print.
  8632. */
  8633. void wolfSSL_EC_POINT_dump(const char *msg, const WOLFSSL_EC_POINT *point)
  8634. {
  8635. #if defined(DEBUG_WOLFSSL)
  8636. char *num;
  8637. WOLFSSL_ENTER("wolfSSL_EC_POINT_dump");
  8638. /* Only print when debugging on and logging callback set. */
  8639. if (WOLFSSL_IS_DEBUG_ON() && (wolfSSL_GetLoggingCb() == NULL)) {
  8640. if (point == NULL) {
  8641. /* No point passed in so just put out "NULL". */
  8642. XFPRINTF(stderr, "%s = NULL\n", msg);
  8643. }
  8644. else {
  8645. /* Put out message and status of internal/external data set. */
  8646. XFPRINTF(stderr, "%s:\n\tinSet=%d, exSet=%d\n", msg, point->inSet,
  8647. point->exSet);
  8648. /* Get x-ordinate as a hex string and print. */
  8649. num = wolfSSL_BN_bn2hex(point->X);
  8650. XFPRINTF(stderr, "\tX = %s\n", num);
  8651. XFREE(num, NULL, DYNAMIC_TYPE_OPENSSL);
  8652. /* Get x-ordinate as a hex string and print. */
  8653. num = wolfSSL_BN_bn2hex(point->Y);
  8654. XFPRINTF(stderr, "\tY = %s\n", num);
  8655. XFREE(num, NULL, DYNAMIC_TYPE_OPENSSL);
  8656. /* Get z-ordinate as a hex string and print. */
  8657. num = wolfSSL_BN_bn2hex(point->Z);
  8658. XFPRINTF(stderr, "\tZ = %s\n", num);
  8659. XFREE(num, NULL, DYNAMIC_TYPE_OPENSSL);
  8660. }
  8661. }
  8662. #else
  8663. (void)msg;
  8664. (void)point;
  8665. #endif
  8666. }
  8667. #ifndef HAVE_SELFTEST
  8668. /* Convert EC point to hex string that as either uncompressed or compressed.
  8669. *
  8670. * ECC point compression types were not included in selftest ecc.h
  8671. *
  8672. * @param [in] group EC group for point.
  8673. * @param [in] point EC point to encode.
  8674. * @param [in] form Format of encoding. Valid values:
  8675. * POINT_CONVERSION_UNCOMPRESSED, POINT_CONVERSION_COMPRESSED
  8676. * @param [in] ctx Context to use for BN operations. Unused.
  8677. * @return Allocated hex string on success.
  8678. * @return NULL on error.
  8679. */
  8680. char* wolfSSL_EC_POINT_point2hex(const WOLFSSL_EC_GROUP* group,
  8681. const WOLFSSL_EC_POINT* point, int form, WOLFSSL_BN_CTX* ctx)
  8682. {
  8683. static const char* hexDigit = "0123456789ABCDEF";
  8684. char* hex = NULL;
  8685. int i;
  8686. int sz = 0;
  8687. int len = 0;
  8688. int err = 0;
  8689. /* No BN operations performed. */
  8690. (void)ctx;
  8691. /* Validate parameters. */
  8692. if ((group == NULL) || (point == NULL)) {
  8693. err = 1;
  8694. }
  8695. /* Get curve id expects a positive index. */
  8696. if ((!err) && (group->curve_idx < 0)) {
  8697. err = 1;
  8698. }
  8699. if (!err) {
  8700. /* Get curve id to look up ordinate size. */
  8701. int id = wc_ecc_get_curve_id(group->curve_idx);
  8702. /* Get size of ordinate. */
  8703. if ((sz = wc_ecc_get_curve_size_from_id(id)) < 0) {
  8704. err = 1;
  8705. }
  8706. }
  8707. if (!err) {
  8708. /* <format byte> <x-ordinate> [<y-ordinate>] */
  8709. len = sz + 1;
  8710. if (form == POINT_CONVERSION_UNCOMPRESSED) {
  8711. /* Include y ordinate when uncompressed. */
  8712. len += sz;
  8713. }
  8714. /* Hex string: allocate 2 bytes to represent each byte plus 1 for '\0'.
  8715. */
  8716. hex = (char*)XMALLOC((size_t)(2 * len + 1), NULL, DYNAMIC_TYPE_ECC);
  8717. if (hex == NULL) {
  8718. err = 1;
  8719. }
  8720. }
  8721. if (!err) {
  8722. /* Make bytes all zeros to allow for ordinate values less than max size.
  8723. */
  8724. XMEMSET(hex, 0, (size_t)(2 * len + 1));
  8725. /* Calculate offset as leading zeros not encoded. */
  8726. i = sz - mp_unsigned_bin_size((mp_int*)point->X->internal) + 1;
  8727. /* Put in x-ordinate after format byte. */
  8728. if (mp_to_unsigned_bin((mp_int*)point->X->internal, (byte*)(hex + i)) <
  8729. 0) {
  8730. err = 1;
  8731. }
  8732. }
  8733. if (!err) {
  8734. if (form == POINT_CONVERSION_COMPRESSED) {
  8735. /* Compressed format byte value dependent on whether y-ordinate is
  8736. * odd.
  8737. */
  8738. hex[0] = mp_isodd((mp_int*)point->Y->internal) ?
  8739. ECC_POINT_COMP_ODD : ECC_POINT_COMP_EVEN;
  8740. /* No y-ordinate. */
  8741. }
  8742. else {
  8743. /* Put in uncompressed format byte. */
  8744. hex[0] = ECC_POINT_UNCOMP;
  8745. /* Calculate offset as leading zeros not encoded. */
  8746. i = 1 + 2 * sz - mp_unsigned_bin_size((mp_int*)point->Y->internal);
  8747. /* Put in y-ordinate after x-ordinate. */
  8748. if (mp_to_unsigned_bin((mp_int*)point->Y->internal,
  8749. (byte*)(hex + i)) < 0) {
  8750. err = 1;
  8751. }
  8752. }
  8753. }
  8754. if (!err) {
  8755. /* Convert binary encoding to hex string. */
  8756. /* Start at end so as not to overwrite. */
  8757. for (i = len-1; i >= 0; i--) {
  8758. /* Get byte value and store has hex string. */
  8759. byte b = (byte)hex[i];
  8760. hex[i * 2 + 1] = hexDigit[b & 0xf];
  8761. hex[i * 2 ] = hexDigit[b >> 4];
  8762. }
  8763. /* Memset put trailing zero or '\0' on end of string. */
  8764. }
  8765. if (err && (hex != NULL)) {
  8766. /* Dispose of allocated data not being returned. */
  8767. XFREE(hex, NULL, DYNAMIC_TYPE_ECC);
  8768. hex = NULL;
  8769. }
  8770. /* Return hex string encoding. */
  8771. return hex;
  8772. }
  8773. #endif /* HAVE_SELFTEST */
  8774. /* Encode the EC point as an uncompressed point in DER.
  8775. *
  8776. * Return code compliant with OpenSSL.
  8777. * Not OpenSSL API.
  8778. *
  8779. * @param [in] group EC group point belongs to.
  8780. * @param [in] point EC point to encode.
  8781. * @param [out] out Buffer to encode into. May be NULL.
  8782. * @param [in, out] len On in, length of buffer in bytes.
  8783. * On out, length of encoding in bytes.
  8784. * @return 1 on success.
  8785. * @return 0 on error.
  8786. */
  8787. int wolfSSL_ECPoint_i2d(const WOLFSSL_EC_GROUP *group,
  8788. const WOLFSSL_EC_POINT *point, unsigned char *out, unsigned int *len)
  8789. {
  8790. int res = 1;
  8791. WOLFSSL_ENTER("wolfSSL_ECPoint_i2d");
  8792. /* Validate parameters. */
  8793. if ((group == NULL) || (point == NULL) || (len == NULL)) {
  8794. WOLFSSL_MSG("wolfSSL_ECPoint_i2d NULL error");
  8795. res = 0;
  8796. }
  8797. /* Ensure points internals are set up. */
  8798. if ((res == 1) && (ec_point_setup(point) != 1)) {
  8799. res = 0;
  8800. }
  8801. /* Dump the point if encoding. */
  8802. if ((res == 1) && (out != NULL)) {
  8803. wolfSSL_EC_POINT_dump("i2d p", point);
  8804. }
  8805. if (res == 1) {
  8806. /* DER encode point in uncompressed format. */
  8807. int ret = wc_ecc_export_point_der(group->curve_idx,
  8808. (ecc_point*)point->internal, out, len);
  8809. /* Check return. When out is NULL, return will be length only error. */
  8810. if ((ret != MP_OKAY) && ((out != NULL) || (ret != LENGTH_ONLY_E))) {
  8811. WOLFSSL_MSG("wolfSSL_ECPoint_i2d wc_ecc_export_point_der failed");
  8812. res = 0;
  8813. }
  8814. }
  8815. return res;
  8816. }
  8817. /* Decode the uncompressed point in DER into EC point.
  8818. *
  8819. * Return code compliant with OpenSSL.
  8820. * Not OpenSSL API.
  8821. *
  8822. * @param [in] in Buffer containing DER encoded point.
  8823. * @param [in] len Length of data in bytes.
  8824. * @param [in] group EC group associated with point.
  8825. * @param [in, out] point EC point to set data into.
  8826. * @return 1 on success.
  8827. * @return 0 on error.
  8828. */
  8829. int wolfSSL_ECPoint_d2i(const unsigned char *in, unsigned int len,
  8830. const WOLFSSL_EC_GROUP *group, WOLFSSL_EC_POINT *point)
  8831. {
  8832. int ret = 1;
  8833. WOLFSSL_ENTER("wolfSSL_ECPoint_d2i");
  8834. /* Validate parameters. */
  8835. if ((in == NULL) || (group == NULL) || (point == NULL) ||
  8836. (point->internal == NULL)) {
  8837. WOLFSSL_MSG("wolfSSL_ECPoint_d2i NULL error");
  8838. ret = 0;
  8839. }
  8840. if (ret == 1) {
  8841. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  8842. /* Import point into internal EC point. */
  8843. if (wc_ecc_import_point_der_ex(in, len, group->curve_idx,
  8844. (ecc_point*)point->internal, 0) != MP_OKAY) {
  8845. WOLFSSL_MSG("wc_ecc_import_point_der_ex failed");
  8846. ret = 0;
  8847. }
  8848. #else
  8849. /* ECC_POINT_UNCOMP is not defined CAVP self test so use magic number */
  8850. if (in[0] == 0x04) {
  8851. /* Import point into internal EC point. */
  8852. if (wc_ecc_import_point_der((unsigned char *)in, len,
  8853. group->curve_idx, (ecc_point*)point->internal) != MP_OKAY) {
  8854. WOLFSSL_MSG("wc_ecc_import_point_der failed");
  8855. ret = 0;
  8856. }
  8857. }
  8858. else {
  8859. WOLFSSL_MSG("Only uncompressed points supported with "
  8860. "HAVE_SELFTEST");
  8861. ret = 0;
  8862. }
  8863. #endif
  8864. }
  8865. /* Set new external point. */
  8866. if ((ret == 1) && (ec_point_external_set(point) != 1)) {
  8867. WOLFSSL_MSG("ec_point_external_set failed");
  8868. ret = 0;
  8869. }
  8870. if (ret == 1) {
  8871. /* Dump new point. */
  8872. wolfSSL_EC_POINT_dump("d2i p", point);
  8873. }
  8874. return ret;
  8875. }
  8876. /* Encode point as octet string.
  8877. *
  8878. * HYBRID not supported.
  8879. *
  8880. * @param [in] group EC group that point belongs to.
  8881. * @param [in] point EC point to encode.
  8882. * @param [in] form Format of encoding. Valid values:
  8883. * POINT_CONVERSION_UNCOMPRESSED,POINT_CONVERSION_COMPRESSED
  8884. * @param [out] buf Buffer to write encoding into.
  8885. * @param [in] len Length of buffer.
  8886. * @param [in] ctx Context to use for BN operations. Unused.
  8887. * @return Length of encoded data on success.
  8888. * @return 0 on error.
  8889. */
  8890. size_t wolfSSL_EC_POINT_point2oct(const WOLFSSL_EC_GROUP *group,
  8891. const WOLFSSL_EC_POINT *point, int form, byte *buf, size_t len,
  8892. WOLFSSL_BN_CTX *ctx)
  8893. {
  8894. int err = 0;
  8895. word32 enc_len = (word32)len;
  8896. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  8897. int compressed = ((form == POINT_CONVERSION_COMPRESSED) ? 1 : 0);
  8898. #endif /* !HAVE_SELFTEST */
  8899. WOLFSSL_ENTER("wolfSSL_EC_POINT_point2oct");
  8900. /* No BN operations performed. */
  8901. (void)ctx;
  8902. /* Validate parameters. */
  8903. if ((group == NULL) || (point == NULL)) {
  8904. err = 1;
  8905. }
  8906. /* Ensure points internals are set up. */
  8907. if ((!err) && (ec_point_setup(point) != 1)) {
  8908. err = 1;
  8909. }
  8910. /* Special case when point is infinity. */
  8911. if ((!err) && wolfSSL_EC_POINT_is_at_infinity(group, point)) {
  8912. /* Encoding is a single octet: 0x00. */
  8913. enc_len = 1;
  8914. if (buf != NULL) {
  8915. /* Check whether buffer has space. */
  8916. if (len < 1) {
  8917. ECerr(EC_F_EC_GFP_SIMPLE_POINT2OCT, EC_R_BUFFER_TOO_SMALL);
  8918. err = 1;
  8919. }
  8920. else {
  8921. /* Put in encoding of infinity. */
  8922. buf[0] = 0x00;
  8923. }
  8924. }
  8925. }
  8926. /* Not infinity. */
  8927. else if (!err) {
  8928. /* Validate format. */
  8929. if (form != POINT_CONVERSION_UNCOMPRESSED
  8930. #ifndef HAVE_SELFTEST
  8931. && form != POINT_CONVERSION_COMPRESSED
  8932. #endif /* !HAVE_SELFTEST */
  8933. ) {
  8934. WOLFSSL_MSG("Unsupported point form");
  8935. err = 1;
  8936. }
  8937. if (!err) {
  8938. int ret;
  8939. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  8940. /* Encode as compressed or uncompressed. */
  8941. ret = wc_ecc_export_point_der_ex(group->curve_idx,
  8942. (ecc_point*)point->internal, buf, &enc_len, compressed);
  8943. #else
  8944. /* Encode uncompressed point in DER format. */
  8945. ret = wc_ecc_export_point_der(group->curve_idx,
  8946. (ecc_point*)point->internal, buf, &enc_len);
  8947. #endif /* !HAVE_SELFTEST */
  8948. /* Check return. When buf is NULL, return will be length only
  8949. * error.
  8950. */
  8951. if (ret != ((buf != NULL) ? MP_OKAY : LENGTH_ONLY_E)) {
  8952. err = 1;
  8953. }
  8954. }
  8955. }
  8956. /* On error, return encoding length of 0. */
  8957. if (err) {
  8958. enc_len = 0;
  8959. }
  8960. return (size_t)enc_len;
  8961. }
  8962. /* Convert octet string to EC point.
  8963. *
  8964. * @param [in] group EC group.
  8965. * @param [in, out] point EC point to set data into.
  8966. * @param [in] buf Buffer holding octet string.
  8967. * @param [in] len Length of data in buffer in bytes.
  8968. * @param [in] ctx Context to use for BN operations. Unused.
  8969. */
  8970. int wolfSSL_EC_POINT_oct2point(const WOLFSSL_EC_GROUP *group,
  8971. WOLFSSL_EC_POINT *point, const unsigned char *buf, size_t len,
  8972. WOLFSSL_BN_CTX *ctx)
  8973. {
  8974. int ret;
  8975. WOLFSSL_ENTER("wolfSSL_EC_POINT_oct2point");
  8976. /* No BN operations performed. */
  8977. (void)ctx;
  8978. /* Validate parameters. */
  8979. if ((group == NULL) || (point == NULL)) {
  8980. ret = 0;
  8981. }
  8982. else {
  8983. /* Decode DER encoding into EC point. */
  8984. ret = wolfSSL_ECPoint_d2i((unsigned char*)buf, (unsigned int)len, group,
  8985. point);
  8986. }
  8987. return ret;
  8988. }
  8989. /* Convert an EC point to a single BN.
  8990. *
  8991. * @param [in] group EC group.
  8992. * @param [in] point EC point.
  8993. * @param [in] form Format of encoding. Valid values:
  8994. * POINT_CONVERSION_UNCOMPRESSED,
  8995. * POINT_CONVERSION_COMPRESSED.
  8996. * @param [in, out] bn BN to hold point value.
  8997. * When NULL a new BN is allocated otherwise this is
  8998. * returned on success.
  8999. * @param [in] ctx Context to use for BN operations. Unused.
  9000. * @return BN object with point as a value on success.
  9001. * @return NULL on error.
  9002. */
  9003. WOLFSSL_BIGNUM *wolfSSL_EC_POINT_point2bn(const WOLFSSL_EC_GROUP* group,
  9004. const WOLFSSL_EC_POINT* point, int form, WOLFSSL_BIGNUM* bn,
  9005. WOLFSSL_BN_CTX* ctx)
  9006. {
  9007. int err = 0;
  9008. size_t len = 0;
  9009. byte *buf = NULL;
  9010. WOLFSSL_BIGNUM *ret = NULL;
  9011. WOLFSSL_ENTER("wolfSSL_EC_POINT_oct2point");
  9012. /* Validate parameters. */
  9013. if ((group == NULL) || (point == NULL)) {
  9014. err = 1;
  9015. }
  9016. /* Calculate length of octet encoding. */
  9017. if ((!err) && ((len = wolfSSL_EC_POINT_point2oct(group, point, form, NULL,
  9018. 0, ctx)) == 0)) {
  9019. err = 1;
  9020. }
  9021. /* Allocate buffer to hold octet encoding. */
  9022. if ((!err) && ((buf = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_TMP_BUFFER)) ==
  9023. NULL)) {
  9024. WOLFSSL_MSG("malloc failed");
  9025. err = 1;
  9026. }
  9027. /* Encode EC point as an octet string. */
  9028. if ((!err) && (wolfSSL_EC_POINT_point2oct(group, point, form, buf, len,
  9029. ctx) != len)) {
  9030. err = 1;
  9031. }
  9032. /* Load BN with octet string data. */
  9033. if (!err) {
  9034. ret = wolfSSL_BN_bin2bn(buf, (int)len, bn);
  9035. }
  9036. /* Dispose of any allocated data. */
  9037. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9038. return ret;
  9039. }
  9040. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  9041. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  9042. /* Check if EC point is on the the curve defined by the EC group.
  9043. *
  9044. * @param [in] group EC group defining curve.
  9045. * @param [in] point EC point to check.
  9046. * @param [in] ctx Context to use for BN operations. Unused.
  9047. * @return 1 when point is on curve.
  9048. * @return 0 when point is not on curve or error.
  9049. */
  9050. int wolfSSL_EC_POINT_is_on_curve(const WOLFSSL_EC_GROUP *group,
  9051. const WOLFSSL_EC_POINT *point, WOLFSSL_BN_CTX *ctx)
  9052. {
  9053. int err = 0;
  9054. WOLFSSL_ENTER("wolfSSL_EC_POINT_is_on_curve");
  9055. /* No BN operations performed. */
  9056. (void)ctx;
  9057. /* Validate parameters. */
  9058. if ((group == NULL) || (point == NULL)) {
  9059. WOLFSSL_MSG("Invalid arguments");
  9060. err = 1;
  9061. }
  9062. /* Ensure internal EC point set. */
  9063. if ((!err) && (!point->inSet) && ec_point_internal_set(
  9064. (WOLFSSL_EC_POINT*)point) != 1) {
  9065. WOLFSSL_MSG("ec_point_internal_set error");
  9066. err = 1;
  9067. }
  9068. /* Check point is on curve from group. */
  9069. if ((!err) && (wc_ecc_point_is_on_curve((ecc_point*)point->internal,
  9070. group->curve_idx) != MP_OKAY)) {
  9071. err = 1;
  9072. }
  9073. /* Return boolean of on curve. No error means on curve. */
  9074. return !err;
  9075. }
  9076. #endif /* USE_ECC_B_PARAM && !HAVE_SELFTEST && !(FIPS_VERSION <= 2) */
  9077. #if !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  9078. /* Convert Jacobian ordinates to affine.
  9079. *
  9080. * @param [in] group EC group.
  9081. * @param [in] point EC point to get co-ordinates from.
  9082. * @return 1 on success.
  9083. * @return 0 on error.
  9084. */
  9085. static int ec_point_convert_to_affine(const WOLFSSL_EC_GROUP *group,
  9086. WOLFSSL_EC_POINT *point)
  9087. {
  9088. int err = 0;
  9089. mp_digit mp;
  9090. #ifdef WOLFSSL_SMALL_STACK
  9091. mp_int* modulus;
  9092. #else
  9093. mp_int modulus[1];
  9094. #endif
  9095. #ifdef WOLFSSL_SMALL_STACK
  9096. /* Allocate memory for curve's prime modulus. */
  9097. modulus = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9098. if (modulus == NULL) {
  9099. err = 1;
  9100. }
  9101. #endif
  9102. /* Initialize the MP integer. */
  9103. if ((!err) && (mp_init(modulus) != MP_OKAY)) {
  9104. WOLFSSL_MSG("mp_init failed");
  9105. err = 1;
  9106. }
  9107. if (!err) {
  9108. /* Get the modulus from the hex string in the EC curve set. */
  9109. if (mp_read_radix(modulus, ecc_sets[group->curve_idx].prime,
  9110. MP_RADIX_HEX) != MP_OKAY) {
  9111. WOLFSSL_MSG("mp_read_radix failed");
  9112. err = 1;
  9113. }
  9114. /* Get Montgomery multiplier for the modulus as ordinates in
  9115. * Montgomery form.
  9116. */
  9117. if ((!err) && (mp_montgomery_setup(modulus, &mp) != MP_OKAY)) {
  9118. WOLFSSL_MSG("mp_montgomery_setup failed");
  9119. err = 1;
  9120. }
  9121. /* Map internal EC point from Jacobian to affine. */
  9122. if ((!err) && (ecc_map((ecc_point*)point->internal, modulus, mp) !=
  9123. MP_OKAY)) {
  9124. WOLFSSL_MSG("ecc_map failed");
  9125. err = 1;
  9126. }
  9127. /* Set new ordinates into external EC point. */
  9128. if ((!err) && (ec_point_external_set((WOLFSSL_EC_POINT *)point) != 1)) {
  9129. WOLFSSL_MSG("ec_point_external_set failed");
  9130. err = 1;
  9131. }
  9132. point->exSet = !err;
  9133. mp_clear(modulus);
  9134. }
  9135. #ifdef WOLFSSL_SMALL_STACK
  9136. XFREE(modulus, NULL, DYNAMIC_TYPE_BIGINT);
  9137. #endif
  9138. return err;
  9139. }
  9140. /* Get the affine co-ordinates of the EC point on a Prime curve.
  9141. *
  9142. * When z-ordinate is not one then co-ordinates are Jacobian and need to be
  9143. * converted to affine before storing in BNs.
  9144. *
  9145. * Return code compliant with OpenSSL.
  9146. *
  9147. * TODO: OpenSSL doesn't change point when Jacobian. Do the same?
  9148. *
  9149. * @param [in] group EC group.
  9150. * @param [in] point EC point to get co-ordinates from.
  9151. * @param [in, out] x BN to hold x-ordinate.
  9152. * @param [in, out] y BN to hold y-ordinate.
  9153. * @param [in] ctx Context to use for BN operations. Unused.
  9154. * @return 1 on success.
  9155. * @return 0 on error.
  9156. */
  9157. int wolfSSL_EC_POINT_get_affine_coordinates_GFp(const WOLFSSL_EC_GROUP* group,
  9158. const WOLFSSL_EC_POINT* point, WOLFSSL_BIGNUM* x, WOLFSSL_BIGNUM* y,
  9159. WOLFSSL_BN_CTX* ctx)
  9160. {
  9161. int ret = 1;
  9162. /* BN operations don't need context. */
  9163. (void)ctx;
  9164. WOLFSSL_ENTER("wolfSSL_EC_POINT_get_affine_coordinates_GFp");
  9165. /* Validate parameters. */
  9166. if ((group == NULL) || (point == NULL) || (point->internal == NULL) ||
  9167. (x == NULL) || (y == NULL)) {
  9168. WOLFSSL_MSG("wolfSSL_EC_POINT_get_affine_coordinates_GFp NULL error");
  9169. ret = 0;
  9170. }
  9171. /* Don't return point at infinity. */
  9172. if ((ret == 1) && wolfSSL_EC_POINT_is_at_infinity(group, point)) {
  9173. ret = 0;
  9174. }
  9175. /* Ensure internal EC point has values of external EC point. */
  9176. if ((ret == 1) && (ec_point_setup(point) != 1)) {
  9177. ret = 0;
  9178. }
  9179. /* Check whether ordinates are in Jacobian form. */
  9180. if ((ret == 1) && (!wolfSSL_BN_is_one(point->Z))) {
  9181. /* Convert from Jacobian to affine. */
  9182. if (ec_point_convert_to_affine(group, (WOLFSSL_EC_POINT*)point) == 1) {
  9183. ret = 0;
  9184. }
  9185. }
  9186. /* Copy the externally set x and y ordinates. */
  9187. if ((ret == 1) && (BN_copy(x, point->X) == NULL)) {
  9188. ret = 0;
  9189. }
  9190. if ((ret == 1) && (BN_copy(y, point->Y) == NULL)) {
  9191. ret = 0;
  9192. }
  9193. return ret;
  9194. }
  9195. #endif /* !WOLFSSL_SP_MATH && !WOLF_CRYPTO_CB_ONLY_ECC */
  9196. /* Sets the affine co-ordinates that belong on a prime curve.
  9197. *
  9198. * @param [in] group EC group.
  9199. * @param [in, out] point EC point to set co-ordinates into.
  9200. * @param [in] x BN holding x-ordinate.
  9201. * @param [in] y BN holding y-ordinate.
  9202. * @param [in] ctx Context to use for BN operations. Unused.
  9203. * @return 1 on success.
  9204. * @return 0 on error.
  9205. */
  9206. int wolfSSL_EC_POINT_set_affine_coordinates_GFp(const WOLFSSL_EC_GROUP* group,
  9207. WOLFSSL_EC_POINT* point, const WOLFSSL_BIGNUM* x, const WOLFSSL_BIGNUM* y,
  9208. WOLFSSL_BN_CTX* ctx)
  9209. {
  9210. int ret = 1;
  9211. /* BN operations don't need context. */
  9212. (void)ctx;
  9213. WOLFSSL_ENTER("wolfSSL_EC_POINT_set_affine_coordinates_GFp");
  9214. /* Validate parameters. */
  9215. if ((group == NULL) || (point == NULL) || (point->internal == NULL) ||
  9216. (x == NULL) || (y == NULL)) {
  9217. WOLFSSL_MSG("wolfSSL_EC_POINT_set_affine_coordinates_GFp NULL error");
  9218. ret = 0;
  9219. }
  9220. /* Ensure we have a object for x-ordinate. */
  9221. if ((ret == 1) && (point->X == NULL) &&
  9222. ((point->X = wolfSSL_BN_new()) == NULL)) {
  9223. WOLFSSL_MSG("wolfSSL_BN_new failed");
  9224. ret = 0;
  9225. }
  9226. /* Ensure we have a object for y-ordinate. */
  9227. if ((ret == 1) && (point->Y == NULL) &&
  9228. ((point->Y = wolfSSL_BN_new()) == NULL)) {
  9229. WOLFSSL_MSG("wolfSSL_BN_new failed");
  9230. ret = 0;
  9231. }
  9232. /* Ensure we have a object for z-ordinate. */
  9233. if ((ret == 1) && (point->Z == NULL) &&
  9234. ((point->Z = wolfSSL_BN_new()) == NULL)) {
  9235. WOLFSSL_MSG("wolfSSL_BN_new failed");
  9236. ret = 0;
  9237. }
  9238. /* Copy the x-ordinate. */
  9239. if ((ret == 1) && ((wolfSSL_BN_copy(point->X, x)) == NULL)) {
  9240. WOLFSSL_MSG("wolfSSL_BN_copy failed");
  9241. ret = 0;
  9242. }
  9243. /* Copy the y-ordinate. */
  9244. if ((ret == 1) && ((wolfSSL_BN_copy(point->Y, y)) == NULL)) {
  9245. WOLFSSL_MSG("wolfSSL_BN_copy failed");
  9246. ret = 0;
  9247. }
  9248. /* z-ordinate is one for affine co-ordinates. */
  9249. if ((ret == 1) && ((wolfSSL_BN_one(point->Z)) == 0)) {
  9250. WOLFSSL_MSG("wolfSSL_BN_one failed");
  9251. ret = 0;
  9252. }
  9253. /* Copy the new point data to internal object. */
  9254. if ((ret == 1) && (ec_point_internal_set((WOLFSSL_EC_POINT *)point) != 1)) {
  9255. WOLFSSL_MSG("ec_point_internal_set failed");
  9256. ret = 0;
  9257. }
  9258. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  9259. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  9260. /* Check that the point is valid. */
  9261. if ((ret == 1) && (wolfSSL_EC_POINT_is_on_curve(group,
  9262. (WOLFSSL_EC_POINT *)point, ctx) != 1)) {
  9263. WOLFSSL_MSG("EC_POINT_is_on_curve failed");
  9264. ret = 0;
  9265. }
  9266. #endif
  9267. return ret;
  9268. }
  9269. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  9270. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  9271. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  9272. /* Add two points on the same together.
  9273. *
  9274. * @param [in] curveIdx Index of curve in ecc_set.
  9275. * @oaram [out] r Result point.
  9276. * @param [in] p1 First point to add.
  9277. * @param [in] p2 Second point to add.
  9278. * @return 1 on success.
  9279. * @return 0 on error.
  9280. */
  9281. static int wolfssl_ec_point_add(int curveIdx, ecc_point* r, ecc_point* p1,
  9282. ecc_point* p2)
  9283. {
  9284. int ret = 1;
  9285. #ifdef WOLFSSL_SMALL_STACK
  9286. mp_int* a = NULL;
  9287. mp_int* prime = NULL;
  9288. mp_int* mu = NULL;
  9289. #else
  9290. mp_int a[1];
  9291. mp_int prime[1];
  9292. mp_int mu[1];
  9293. #endif
  9294. mp_digit mp = 0;
  9295. ecc_point* montP1 = NULL;
  9296. ecc_point* montP2 = NULL;
  9297. #ifdef WOLFSSL_SMALL_STACK
  9298. if (ret == 1) {
  9299. /* Allocate memory for curve parameter: a. */
  9300. a = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9301. if (a == NULL) {
  9302. WOLFSSL_MSG("Failed to allocate memory for mp_int a");
  9303. ret = 0;
  9304. }
  9305. }
  9306. if (ret == 1) {
  9307. /* Allocate memory for curve parameter: prime. */
  9308. prime = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9309. if (prime == NULL) {
  9310. WOLFSSL_MSG("Failed to allocate memory for mp_int prime");
  9311. ret = 0;
  9312. }
  9313. }
  9314. if (ret == 1) {
  9315. /* Allocate memory for mu (Montgomery normalizer). */
  9316. mu = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9317. if (mu == NULL) {
  9318. WOLFSSL_MSG("Failed to allocate memory for mp_int mu");
  9319. ret = 0;
  9320. }
  9321. }
  9322. if (ret == 1) {
  9323. /* Zero out all MP int data in case initialization fails. */
  9324. XMEMSET(a, 0, sizeof(mp_int));
  9325. XMEMSET(prime, 0, sizeof(mp_int));
  9326. XMEMSET(mu, 0, sizeof(mp_int));
  9327. }
  9328. #endif
  9329. /* Initialize the MP ints. */
  9330. if ((ret == 1) && (mp_init_multi(prime, a, mu, NULL, NULL, NULL) !=
  9331. MP_OKAY)) {
  9332. WOLFSSL_MSG("mp_init_multi error");
  9333. ret = 0;
  9334. }
  9335. /* Read the curve parameter: a. */
  9336. if ((ret == 1) && (mp_read_radix(a, ecc_sets[curveIdx].Af, MP_RADIX_HEX) !=
  9337. MP_OKAY)) {
  9338. WOLFSSL_MSG("mp_read_radix a error");
  9339. ret = 0;
  9340. }
  9341. /* Read the curve parameter: prime. */
  9342. if ((ret == 1) && (mp_read_radix(prime, ecc_sets[curveIdx].prime,
  9343. MP_RADIX_HEX) != MP_OKAY)) {
  9344. WOLFSSL_MSG("mp_read_radix prime error");
  9345. ret = 0;
  9346. }
  9347. /* Calculate the Montgomery product. */
  9348. if ((ret == 1) && (mp_montgomery_setup(prime, &mp) != MP_OKAY)) {
  9349. WOLFSSL_MSG("mp_montgomery_setup nqm error");
  9350. ret = 0;
  9351. }
  9352. /* TODO: use the heap filed of one of the points? */
  9353. /* Allocate new points to hold the Montgomery form values. */
  9354. if ((ret == 1) && (((montP1 = wc_ecc_new_point_h(NULL)) == NULL) ||
  9355. ((montP2 = wc_ecc_new_point_h(NULL)) == NULL))) {
  9356. WOLFSSL_MSG("wc_ecc_new_point_h nqm error");
  9357. ret = 0;
  9358. }
  9359. /* Calculate the Montgomery normalizer. */
  9360. if ((ret == 1) && (mp_montgomery_calc_normalization(mu, prime) !=
  9361. MP_OKAY)) {
  9362. WOLFSSL_MSG("mp_montgomery_calc_normalization error");
  9363. ret = 0;
  9364. }
  9365. /* Convert to Montgomery form. */
  9366. if ((ret == 1) && (mp_cmp_d(mu, 1) == MP_EQ)) {
  9367. /* Copy the points if the normalizer is 1. */
  9368. if ((wc_ecc_copy_point(p1, montP1) != MP_OKAY) ||
  9369. (wc_ecc_copy_point(p2, montP2) != MP_OKAY)) {
  9370. WOLFSSL_MSG("wc_ecc_copy_point error");
  9371. ret = 0;
  9372. }
  9373. }
  9374. else if (ret == 1) {
  9375. /* Multiply each ordinate by the Montgomery normalizer. */
  9376. if ((mp_mulmod(p1->x, mu, prime, montP1->x) != MP_OKAY) ||
  9377. (mp_mulmod(p1->y, mu, prime, montP1->y) != MP_OKAY) ||
  9378. (mp_mulmod(p1->z, mu, prime, montP1->z) != MP_OKAY)) {
  9379. WOLFSSL_MSG("mp_mulmod error");
  9380. ret = 0;
  9381. }
  9382. /* Multiply each ordinate by the Montgomery normalizer. */
  9383. if ((mp_mulmod(p2->x, mu, prime, montP2->x) != MP_OKAY) ||
  9384. (mp_mulmod(p2->y, mu, prime, montP2->y) != MP_OKAY) ||
  9385. (mp_mulmod(p2->z, mu, prime, montP2->z) != MP_OKAY)) {
  9386. WOLFSSL_MSG("mp_mulmod error");
  9387. ret = 0;
  9388. }
  9389. }
  9390. /* Perform point addition with internal EC point objects - Jacobian form
  9391. * result.
  9392. */
  9393. if ((ret == 1) && (ecc_projective_add_point(montP1, montP2, r, a, prime,
  9394. mp) != MP_OKAY)) {
  9395. WOLFSSL_MSG("ecc_projective_add_point error");
  9396. ret = 0;
  9397. }
  9398. /* Map point back to affine co-ordinates. Converts from Montogomery form. */
  9399. if ((ret == 1) && (ecc_map(r, prime, mp) != MP_OKAY)) {
  9400. WOLFSSL_MSG("ecc_map error");
  9401. ret = 0;
  9402. }
  9403. /* Dispose of allocated memory. */
  9404. mp_clear(a);
  9405. mp_clear(prime);
  9406. mp_clear(mu);
  9407. wc_ecc_del_point_h(montP1, NULL);
  9408. wc_ecc_del_point_h(montP2, NULL);
  9409. #ifdef WOLFSSL_SMALL_STACK
  9410. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  9411. XFREE(prime, NULL, DYNAMIC_TYPE_BIGINT);
  9412. XFREE(mu, NULL, DYNAMIC_TYPE_BIGINT);
  9413. #endif
  9414. return ret;
  9415. }
  9416. /* Add two points on the same curve together.
  9417. *
  9418. * @param [in] group EC group.
  9419. * @param [out] r EC point that is result of point addition.
  9420. * @param [in] p1 First EC point to add.
  9421. * @param [in] p2 Second EC point to add.
  9422. * @param [in] ctx Context to use for BN operations. Unused.
  9423. * @return 1 on success.
  9424. * @return 0 on error.
  9425. */
  9426. int wolfSSL_EC_POINT_add(const WOLFSSL_EC_GROUP* group, WOLFSSL_EC_POINT* r,
  9427. const WOLFSSL_EC_POINT* p1, const WOLFSSL_EC_POINT* p2, WOLFSSL_BN_CTX* ctx)
  9428. {
  9429. int ret = 1;
  9430. /* No BN operations performed. */
  9431. (void)ctx;
  9432. /* Validate parameters. */
  9433. if ((group == NULL) || (r == NULL) || (p1 == NULL) || (p2 == NULL)) {
  9434. WOLFSSL_MSG("wolfSSL_EC_POINT_add error");
  9435. ret = 0;
  9436. }
  9437. /* Ensure the internal objects of the EC points are setup. */
  9438. if ((ret == 1) && ((ec_point_setup(r) != 1) || (ec_point_setup(p1) != 1) ||
  9439. (ec_point_setup(p2) != 1))) {
  9440. WOLFSSL_MSG("ec_point_setup error");
  9441. ret = 0;
  9442. }
  9443. if (ret == 1) {
  9444. /* Add points using wolfCrypt objects. */
  9445. ret = wolfssl_ec_point_add(group->curve_idx, (ecc_point*)r->internal,
  9446. (ecc_point*)p1->internal, (ecc_point*)p2->internal);
  9447. }
  9448. /* Copy internal EC point values out to external EC point. */
  9449. if ((ret == 1) && (ec_point_external_set(r) != 1)) {
  9450. WOLFSSL_MSG("ec_point_external_set error");
  9451. ret = 0;
  9452. }
  9453. return ret;
  9454. }
  9455. /* Sum the scalar multiplications of the base point and n, and q and m.
  9456. *
  9457. * r = base point * n + q * m
  9458. *
  9459. * @param [out] r EC point that is result of operation.
  9460. * @param [in] b Base point of curve.
  9461. * @param [in] n Scalar to multiply by base point.
  9462. * @param [in] q EC point to be scalar multiplied.
  9463. * @param [in] m Scalar to multiply q by.
  9464. * @param [in] a Parameter A of curve.
  9465. * @param [in] prime Prime (modulus) of curve.
  9466. * @return 1 on success.
  9467. * @return 0 on error.
  9468. */
  9469. static int ec_mul2add(ecc_point* r, ecc_point* b, mp_int* n, ecc_point* q,
  9470. mp_int* m, mp_int* a, mp_int* prime)
  9471. {
  9472. int ret = 1;
  9473. #if defined(ECC_SHAMIR) && !defined(WOLFSSL_KCAPI_ECC)
  9474. if (ecc_mul2add(b, n, q, m, r, a, prime, NULL) != MP_OKAY) {
  9475. WOLFSSL_MSG("ecc_mul2add error");
  9476. ret = 0;
  9477. }
  9478. #else
  9479. ecc_point* tmp = NULL;
  9480. mp_digit mp = 0;
  9481. /* Calculate Montgomery product. */
  9482. if (mp_montgomery_setup(prime, &mp) != MP_OKAY) {
  9483. WOLFSSL_MSG("mp_montgomery_setup nqm error");
  9484. ret = 0;
  9485. }
  9486. /* Create temporary point to hold: q * m */
  9487. if ((ret == 1) && ((tmp = wc_ecc_new_point()) == NULL)) {
  9488. WOLFSSL_MSG("wolfSSL_EC_POINT_new nqm error");
  9489. ret = 0;
  9490. }
  9491. /* r = base point * n */
  9492. if ((ret == 1) && (wc_ecc_mulmod(n, b, r, a, prime, 0) !=
  9493. MP_OKAY)) {
  9494. WOLFSSL_MSG("wc_ecc_mulmod nqm error");
  9495. ret = 0;
  9496. }
  9497. /* tmp = q * m */
  9498. if ((ret == 1) && (wc_ecc_mulmod(m, q, tmp, a, prime, 0) != MP_OKAY)) {
  9499. WOLFSSL_MSG("wc_ecc_mulmod nqm error");
  9500. ret = 0;
  9501. }
  9502. /* r = r + tmp */
  9503. if ((ret == 1) && (ecc_projective_add_point(tmp, r, r, a, prime, mp) !=
  9504. MP_OKAY)) {
  9505. WOLFSSL_MSG("wc_ecc_mulmod nqm error");
  9506. ret = 0;
  9507. }
  9508. /* Map point back to affine co-ordinates. Converts from Montogomery
  9509. * form. */
  9510. if ((ret == 1) && (ecc_map(r, prime, mp) != MP_OKAY)) {
  9511. WOLFSSL_MSG("ecc_map nqm error");
  9512. ret = 0;
  9513. }
  9514. /* Dispose of allocated temporary point. */
  9515. wc_ecc_del_point(tmp);
  9516. #endif
  9517. return ret;
  9518. }
  9519. /* Sum the scalar multiplications of the base point and n, and q and m.
  9520. *
  9521. * r = base point * n + q * m
  9522. *
  9523. * @param [in] curveIdx Index of curve in ecc_set.
  9524. * @param [out] r EC point that is result of operation.
  9525. * @param [in] n Scalar to multiply by base point. May be NULL.
  9526. * @param [in] q EC point to be scalar multiplied. May be NULL.
  9527. * @param [in] m Scalar to multiply q by. May be NULL.
  9528. * @return 1 on success.
  9529. * @return 0 on error.
  9530. */
  9531. static int wolfssl_ec_point_mul(int curveIdx, ecc_point* r, mp_int* n,
  9532. ecc_point* q, mp_int* m)
  9533. {
  9534. int ret = 1;
  9535. #ifdef WOLFSSL_SMALL_STACK
  9536. mp_int* a = NULL;
  9537. mp_int* prime = NULL;
  9538. #else
  9539. mp_int a[1], prime[1];
  9540. #endif
  9541. #ifdef WOLFSSL_SMALL_STACK
  9542. /* Allocate MP integer for curve parameter: a. */
  9543. a = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9544. if (a == NULL) {
  9545. ret = 0;
  9546. }
  9547. if (ret == 1) {
  9548. /* Allocate MP integer for curve parameter: prime. */
  9549. prime = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9550. if (prime == NULL) {
  9551. ret = 0;
  9552. }
  9553. }
  9554. #endif
  9555. /* Initialize the MP ints. */
  9556. if ((ret == 1) && (mp_init_multi(prime, a, NULL, NULL, NULL, NULL) !=
  9557. MP_OKAY)) {
  9558. WOLFSSL_MSG("mp_init_multi error");
  9559. ret = 0;
  9560. }
  9561. /* Read the curve parameter: prime. */
  9562. if ((ret == 1) && (mp_read_radix(prime, ecc_sets[curveIdx].prime,
  9563. MP_RADIX_HEX) != MP_OKAY)) {
  9564. WOLFSSL_MSG("mp_read_radix prime error");
  9565. ret = 0;
  9566. }
  9567. /* Read the curve parameter: a. */
  9568. if ((ret == 1) && (mp_read_radix(a, ecc_sets[curveIdx].Af,
  9569. MP_RADIX_HEX) != MP_OKAY)) {
  9570. WOLFSSL_MSG("mp_read_radix a error");
  9571. ret = 0;
  9572. }
  9573. if ((ret == 1) && (n != NULL)) {
  9574. /* Get generator - base point. */
  9575. #if !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  9576. if ((ret == 1) && (wc_ecc_get_generator(r, curveIdx) != MP_OKAY)) {
  9577. WOLFSSL_MSG("wc_ecc_get_generator error");
  9578. ret = 0;
  9579. }
  9580. #else
  9581. /* wc_ecc_get_generator is not defined in the FIPS v2 module. */
  9582. /* Read generator (base point) x-ordinate. */
  9583. if ((ret == 1) && (mp_read_radix(r->x, ecc_sets[curveIdx].Gx,
  9584. MP_RADIX_HEX) != MP_OKAY)) {
  9585. WOLFSSL_MSG("mp_read_radix Gx error");
  9586. ret = 0;
  9587. }
  9588. /* Read generator (base point) y-ordinate. */
  9589. if ((ret == 1) && (mp_read_radix(r->y, ecc_sets[curveIdx].Gy,
  9590. MP_RADIX_HEX) != MP_OKAY)) {
  9591. WOLFSSL_MSG("mp_read_radix Gy error");
  9592. ret = 0;
  9593. }
  9594. /* z-ordinate is one as point is affine. */
  9595. if ((ret == 1) && (mp_set(r->z, 1) != MP_OKAY)) {
  9596. WOLFSSL_MSG("mp_set Gz error");
  9597. ret = 0;
  9598. }
  9599. #endif /* NOPT_FIPS_VERSION == 2 */
  9600. }
  9601. if ((ret == 1) && (n != NULL) && (q != NULL) && (m != NULL)) {
  9602. /* r = base point * n + q * m */
  9603. ec_mul2add(r, r, m, q, n, a, prime);
  9604. }
  9605. /* Not all values present, see if we are only doing base point * n. */
  9606. else if ((ret == 1) && (n != NULL)) {
  9607. /* r = base point * n */
  9608. if (wc_ecc_mulmod(n, r, r, a, prime, 1) != MP_OKAY) {
  9609. WOLFSSL_MSG("wc_ecc_mulmod gn error");
  9610. ret = 0;
  9611. }
  9612. }
  9613. /* Not all values present, see if we are only doing q * m. */
  9614. else if ((ret == 1) && (q != NULL) && (m != NULL)) {
  9615. /* r = q * m */
  9616. if (wc_ecc_mulmod(m, q, r, a, prime, 1) != MP_OKAY) {
  9617. WOLFSSL_MSG("wc_ecc_mulmod qm error");
  9618. ret = 0;
  9619. }
  9620. }
  9621. /* No values to use. */
  9622. else if (ret == 1) {
  9623. /* Set result to infinity as no values passed in. */
  9624. mp_zero(r->x);
  9625. mp_zero(r->y);
  9626. mp_zero(r->z);
  9627. }
  9628. mp_clear(a);
  9629. mp_clear(prime);
  9630. #ifdef WOLFSSL_SMALL_STACK
  9631. XFREE(a, NULL, DYNAMIC_TYPE_BIGINT);
  9632. XFREE(prime, NULL, DYNAMIC_TYPE_BIGINT);
  9633. #endif
  9634. return ret;
  9635. }
  9636. /* Sum the scalar multiplications of the base point and n, and q and m.
  9637. *
  9638. * r = base point * n + q * m
  9639. *
  9640. * Return code compliant with OpenSSL.
  9641. *
  9642. * @param [in] group EC group.
  9643. * @param [out] r EC point that is result of operation.
  9644. * @param [in] n Scalar to multiply by base point. May be NULL.
  9645. * @param [in] q EC point to be scalar multiplied. May be NULL.
  9646. * @param [in] m Scalar to multiply q by. May be NULL.
  9647. * @param [in] ctx Context to use for BN operations. Unused.
  9648. * @return 1 on success.
  9649. * @return 0 on error.
  9650. */
  9651. int wolfSSL_EC_POINT_mul(const WOLFSSL_EC_GROUP *group, WOLFSSL_EC_POINT *r,
  9652. const WOLFSSL_BIGNUM *n, const WOLFSSL_EC_POINT *q, const WOLFSSL_BIGNUM *m,
  9653. WOLFSSL_BN_CTX *ctx)
  9654. {
  9655. int ret = 1;
  9656. /* No BN operations performed. */
  9657. (void)ctx;
  9658. WOLFSSL_ENTER("wolfSSL_EC_POINT_mul");
  9659. /* Validate parameters. */
  9660. if ((group == NULL) || (r == NULL)) {
  9661. WOLFSSL_MSG("wolfSSL_EC_POINT_mul NULL error");
  9662. ret = 0;
  9663. }
  9664. /* Ensure the internal representation of the EC point q is setup. */
  9665. if ((ret == 1) && (q != NULL) && (ec_point_setup(q) != 1)) {
  9666. WOLFSSL_MSG("ec_point_setup error");
  9667. ret = 0;
  9668. }
  9669. if (ret == 1) {
  9670. mp_int* ni = (n != NULL) ? (mp_int*)n->internal : NULL;
  9671. ecc_point* qi = (q != NULL) ? (ecc_point*)q->internal : NULL;
  9672. mp_int* mi = (m != NULL) ? (mp_int*)m->internal : NULL;
  9673. /* Perform multiplication with wolfCrypt objects. */
  9674. ret = wolfssl_ec_point_mul(group->curve_idx, (ecc_point*)r->internal,
  9675. ni, qi, mi);
  9676. }
  9677. /* Only on success is the internal point guaranteed to be set. */
  9678. if (r != NULL) {
  9679. r->inSet = (ret == 1);
  9680. }
  9681. /* Copy internal EC point values out to external EC point. */
  9682. if ((ret == 1) && (ec_point_external_set(r) != 1)) {
  9683. WOLFSSL_MSG("ec_point_external_set error");
  9684. ret = 0;
  9685. }
  9686. return ret;
  9687. }
  9688. #endif /* !WOLFSSL_ATECC508A && !WOLFSSL_ATECC608A && !HAVE_SELFTEST &&
  9689. * !WOLFSSL_SP_MATH */
  9690. /* Invert the point on the curve.
  9691. * (x, y) -> (x, -y) = (x, (prime - y) % prime)
  9692. *
  9693. * @param [in] curveIdx Index of curve in ecc_set.
  9694. * @param [in, out] point EC point to invert.
  9695. * @return 1 on success.
  9696. * @return 0 on error.
  9697. */
  9698. static int wolfssl_ec_point_invert(int curveIdx, ecc_point* point)
  9699. {
  9700. int ret = 1;
  9701. #ifdef WOLFSSL_SMALL_STACK
  9702. mp_int* prime = NULL;
  9703. #else
  9704. mp_int prime[1];
  9705. #endif
  9706. #ifdef WOLFSSL_SMALL_STACK
  9707. /* Allocate memory for an MP int to hold the prime of the curve. */
  9708. prime = (mp_int*)XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  9709. if (prime == NULL) {
  9710. ret = 0;
  9711. }
  9712. #endif
  9713. /* Initialize MP int. */
  9714. if ((ret == 1) && (mp_init(prime) != MP_OKAY)) {
  9715. WOLFSSL_MSG("mp_init_multi error");
  9716. ret = 0;
  9717. }
  9718. /* Read the curve parameter: prime. */
  9719. if ((ret == 1) && (mp_read_radix(prime, ecc_sets[curveIdx].prime,
  9720. MP_RADIX_HEX) != MP_OKAY)) {
  9721. WOLFSSL_MSG("mp_read_radix prime error");
  9722. ret = 0;
  9723. }
  9724. /* y = (prime - y) mod prime. */
  9725. if ((ret == 1) && (!mp_iszero(point->y)) && (mp_sub(prime, point->y,
  9726. point->y) != MP_OKAY)) {
  9727. WOLFSSL_MSG("mp_sub error");
  9728. ret = 0;
  9729. }
  9730. /* Dispose of memory associated with MP. */
  9731. mp_free(prime);
  9732. #ifdef WOLFSSL_SMALL_STACK
  9733. /* Dispose of dynamically allocated temporaries. */
  9734. XFREE(prime, NULL, DYNAMIC_TYPE_BIGINT);
  9735. #endif
  9736. return ret;
  9737. }
  9738. /* Invert the point on the curve.
  9739. * (x, y) -> (x, -y) = (x, (prime - y) % prime)
  9740. *
  9741. * @param [in] group EC group.
  9742. * @param [in, out] point EC point to invert.
  9743. * @param [in] ctx Context to use for BN operations. Unused.
  9744. * @return 1 on success.
  9745. * @return 0 on error.
  9746. */
  9747. int wolfSSL_EC_POINT_invert(const WOLFSSL_EC_GROUP *group,
  9748. WOLFSSL_EC_POINT *point, WOLFSSL_BN_CTX *ctx)
  9749. {
  9750. int ret = 1;
  9751. /* No BN operations performed. */
  9752. (void)ctx;
  9753. WOLFSSL_ENTER("wolfSSL_EC_POINT_invert");
  9754. /* Validate parameters. */
  9755. if ((group == NULL) || (point == NULL) || (point->internal == NULL)) {
  9756. ret = 0;
  9757. }
  9758. /* Ensure internal representation of point is setup. */
  9759. if ((ret == 1) && (ec_point_setup(point) != 1)) {
  9760. ret = 0;
  9761. }
  9762. if (ret == 1) {
  9763. /* Perform inversion using wolfCrypt objects. */
  9764. ret = wolfssl_ec_point_invert(group->curve_idx,
  9765. (ecc_point*)point->internal);
  9766. }
  9767. /* Set the external EC point representation based on internal. */
  9768. if ((ret == 1) && (ec_point_external_set(point) != 1)) {
  9769. WOLFSSL_MSG("ec_point_external_set error");
  9770. ret = 0;
  9771. }
  9772. return ret;
  9773. }
  9774. #ifdef WOLFSSL_EC_POINT_CMP_JACOBIAN
  9775. /* Compare two points on a the same curve.
  9776. *
  9777. * (Ax, Ay, Az) => (Ax / (Az ^ 2), Ay / (Az ^ 3))
  9778. * (Bx, By, Bz) => (Bx / (Bz ^ 2), By / (Bz ^ 3))
  9779. * When equal:
  9780. * (Ax / (Az ^ 2), Ay / (Az ^ 3)) = (Bx / (Bz ^ 2), By / (Bz ^ 3))
  9781. * => (Ax * (Bz ^ 2), Ay * (Bz ^ 3)) = (Bx * (Az ^ 2), By * (Az ^ 3))
  9782. *
  9783. * @param [in] group EC group.
  9784. * @param [in] a EC point to compare.
  9785. * @param [in] b EC point to compare.
  9786. * @return 0 when equal.
  9787. * @return 1 when different.
  9788. * @return -1 on error.
  9789. */
  9790. static int ec_point_cmp_jacobian(const WOLFSSL_EC_GROUP* group,
  9791. const WOLFSSL_EC_POINT *a, const WOLFSSL_EC_POINT *b, WOLFSSL_BN_CTX *ctx)
  9792. {
  9793. int ret = 0;
  9794. BIGNUM* at = BN_new();
  9795. BIGNUM* bt = BN_new();
  9796. BIGNUM* az = BN_new();
  9797. BIGNUM* bz = BN_new();
  9798. BIGNUM* mod = BN_new();
  9799. /* Check that the big numbers were allocated. */
  9800. if ((at == NULL) || (bt == NULL) || (az == NULL) || (bz == NULL) ||
  9801. (mod == NULL)) {
  9802. ret = -1;
  9803. }
  9804. /* Get the modulus for the curve. */
  9805. if ((ret == 0) &&
  9806. (BN_hex2bn(&mod, ecc_sets[group->curve_idx].prime) != 1)) {
  9807. ret = -1;
  9808. }
  9809. if (ret == 0) {
  9810. /* bt = Bx * (Az ^ 2). When Az is one then just copy. */
  9811. if (BN_is_one(a->Z)) {
  9812. if (BN_copy(bt, b->X) == NULL) {
  9813. ret = -1;
  9814. }
  9815. }
  9816. /* az = Az ^ 2 */
  9817. else if ((BN_mod_mul(az, a->Z, a->Z, mod, ctx) != 1)) {
  9818. ret = -1;
  9819. }
  9820. /* bt = Bx * az = Bx * (Az ^ 2) */
  9821. else if (BN_mod_mul(bt, b->X, az, mod, ctx) != 1) {
  9822. ret = -1;
  9823. }
  9824. }
  9825. if (ret == 0) {
  9826. /* at = Ax * (Bz ^ 2). When Bz is one then just copy. */
  9827. if (BN_is_one(b->Z)) {
  9828. if (BN_copy(at, a->X) == NULL) {
  9829. ret = -1;
  9830. }
  9831. }
  9832. /* bz = Bz ^ 2 */
  9833. else if (BN_mod_mul(bz, b->Z, b->Z, mod, ctx) != 1) {
  9834. ret = -1;
  9835. }
  9836. /* at = Ax * bz = Ax * (Bz ^ 2) */
  9837. else if (BN_mod_mul(at, a->X, bz, mod, ctx) != 1) {
  9838. ret = -1;
  9839. }
  9840. }
  9841. /* Compare x-ordinates. */
  9842. if ((ret == 0) && (BN_cmp(at, bt) != 0)) {
  9843. ret = 1;
  9844. }
  9845. if (ret == 0) {
  9846. /* bt = By * (Az ^ 3). When Az is one then just copy. */
  9847. if (BN_is_one(a->Z)) {
  9848. if (BN_copy(bt, b->Y) == NULL) {
  9849. ret = -1;
  9850. }
  9851. }
  9852. /* az = az * Az = Az ^ 3 */
  9853. else if ((BN_mod_mul(az, az, a->Z, mod, ctx) != 1)) {
  9854. ret = -1;
  9855. }
  9856. /* bt = By * az = By * (Az ^ 3) */
  9857. else if (BN_mod_mul(bt, b->Y, az, mod, ctx) != 1) {
  9858. ret = -1;
  9859. }
  9860. }
  9861. if (ret == 0) {
  9862. /* at = Ay * (Bz ^ 3). When Bz is one then just copy. */
  9863. if (BN_is_one(b->Z)) {
  9864. if (BN_copy(at, a->Y) == NULL) {
  9865. ret = -1;
  9866. }
  9867. }
  9868. /* bz = bz * Bz = Bz ^ 3 */
  9869. else if (BN_mod_mul(bz, bz, b->Z, mod, ctx) != 1) {
  9870. ret = -1;
  9871. }
  9872. /* at = Ay * bz = Ay * (Bz ^ 3) */
  9873. else if (BN_mod_mul(at, a->Y, bz, mod, ctx) != 1) {
  9874. ret = -1;
  9875. }
  9876. }
  9877. /* Compare y-ordinates. */
  9878. if ((ret == 0) && (BN_cmp(at, bt) != 0)) {
  9879. ret = 1;
  9880. }
  9881. BN_free(mod);
  9882. BN_free(bz);
  9883. BN_free(az);
  9884. BN_free(bt);
  9885. BN_free(at);
  9886. return ret;
  9887. }
  9888. #endif
  9889. /* Compare two points on a the same curve.
  9890. *
  9891. * Return code compliant with OpenSSL.
  9892. *
  9893. * @param [in] group EC group.
  9894. * @param [in] a EC point to compare.
  9895. * @param [in] b EC point to compare.
  9896. * @param [in] ctx Context to use for BN operations. Unused.
  9897. * @return 0 when equal.
  9898. * @return 1 when different.
  9899. * @return -1 on error.
  9900. */
  9901. int wolfSSL_EC_POINT_cmp(const WOLFSSL_EC_GROUP *group,
  9902. const WOLFSSL_EC_POINT *a, const WOLFSSL_EC_POINT *b, WOLFSSL_BN_CTX *ctx)
  9903. {
  9904. int ret = 0;
  9905. WOLFSSL_ENTER("wolfSSL_EC_POINT_cmp");
  9906. /* Validate parameters. */
  9907. if ((group == NULL) || (a == NULL) || (a->internal == NULL) ||
  9908. (b == NULL) || (b->internal == NULL)) {
  9909. WOLFSSL_MSG("wolfSSL_EC_POINT_cmp Bad arguments");
  9910. ret = -1;
  9911. }
  9912. if (ret != -1) {
  9913. #ifdef WOLFSSL_EC_POINT_CMP_JACOBIAN
  9914. /* If same Z ordinate then no need to convert to affine. */
  9915. if (BN_cmp(a->Z, b->Z) == 0) {
  9916. /* Compare */
  9917. ret = ((BN_cmp(a->X, b->X) != 0) || (BN_cmp(a->Y, b->Y) != 0));
  9918. }
  9919. else {
  9920. ret = ec_point_cmp_jacobian(group, a, b, ctx);
  9921. }
  9922. #else
  9923. /* No BN operations performed. */
  9924. (void)ctx;
  9925. ret = (wc_ecc_cmp_point((ecc_point*)a->internal,
  9926. (ecc_point*)b->internal) != MP_EQ);
  9927. #endif
  9928. }
  9929. return ret;
  9930. }
  9931. /* Copy EC point.
  9932. *
  9933. * @param [out] dest EC point to copy into.
  9934. * @param [in] src EC point to copy.
  9935. * @return 1 on success.
  9936. * @return 0 on error.
  9937. */
  9938. int wolfSSL_EC_POINT_copy(WOLFSSL_EC_POINT *dest, const WOLFSSL_EC_POINT *src)
  9939. {
  9940. int ret = 1;
  9941. WOLFSSL_ENTER("wolfSSL_EC_POINT_copy");
  9942. /* Validate parameters. */
  9943. if ((dest == NULL) || (src == NULL)) {
  9944. ret = 0;
  9945. }
  9946. /* Ensure internal EC point of src is setup. */
  9947. if ((ret == 1) && (ec_point_setup(src) != 1)) {
  9948. ret = 0;
  9949. }
  9950. /* Copy internal EC points. */
  9951. if ((ret == 1) && (wc_ecc_copy_point((ecc_point*)src->internal,
  9952. (ecc_point*)dest->internal) != MP_OKAY)) {
  9953. ret = 0;
  9954. }
  9955. if (ret == 1) {
  9956. /* Destinatation internal point is set. */
  9957. dest->inSet = 1;
  9958. /* Set the external EC point of dest based on internal. */
  9959. if (ec_point_external_set(dest) != 1) {
  9960. ret = 0;
  9961. }
  9962. }
  9963. return ret;
  9964. }
  9965. /* Checks whether point is at infinity.
  9966. *
  9967. * Return code compliant with OpenSSL.
  9968. *
  9969. * @param [in] group EC group.
  9970. * @param [in] point EC point to check.
  9971. * @return 1 when at infinity.
  9972. * @return 0 when not at infinity.
  9973. */
  9974. int wolfSSL_EC_POINT_is_at_infinity(const WOLFSSL_EC_GROUP *group,
  9975. const WOLFSSL_EC_POINT *point)
  9976. {
  9977. int ret = 1;
  9978. WOLFSSL_ENTER("wolfSSL_EC_POINT_is_at_infinity");
  9979. /* Validate parameters. */
  9980. if ((group == NULL) || (point == NULL) || (point->internal == NULL)) {
  9981. WOLFSSL_MSG("wolfSSL_EC_POINT_is_at_infinity NULL error");
  9982. ret = 0;
  9983. }
  9984. /* Ensure internal EC point is setup. */
  9985. if ((ret == 1) && (ec_point_setup(point) != 1)) {
  9986. ret = 0;
  9987. }
  9988. if (ret == 1) {
  9989. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  9990. /* Check for infinity. */
  9991. ret = wc_ecc_point_is_at_infinity((ecc_point*)point->internal);
  9992. if (ret < 0) {
  9993. WOLFSSL_MSG("ecc_point_is_at_infinity failure");
  9994. /* Error return is 0 by OpenSSL. */
  9995. ret = 0;
  9996. }
  9997. #else
  9998. WOLFSSL_MSG("ecc_point_is_at_infinitiy compiled out");
  9999. ret = 0;
  10000. #endif
  10001. }
  10002. return ret;
  10003. }
  10004. #endif /* OPENSSL_EXTRA */
  10005. /* End EC_POINT */
  10006. /* Start EC_KEY */
  10007. #ifdef OPENSSL_EXTRA
  10008. /*
  10009. * EC key constructor/deconstructor APIs
  10010. */
  10011. /* Allocate a new EC key.
  10012. *
  10013. * Not OpenSSL API.
  10014. *
  10015. * @param [in] heap Heap hint for dynamic memory allocation.
  10016. * @param [in] devId Device identifier value.
  10017. * @return New, allocated EC key on success.
  10018. * @return NULL on error.
  10019. */
  10020. WOLFSSL_EC_KEY *wolfSSL_EC_KEY_new_ex(void* heap, int devId)
  10021. {
  10022. WOLFSSL_EC_KEY *key = NULL;
  10023. int err = 0;
  10024. WOLFSSL_ENTER("wolfSSL_EC_KEY_new");
  10025. /* Allocate memory for EC key. */
  10026. key = (WOLFSSL_EC_KEY*)XMALLOC(sizeof(WOLFSSL_EC_KEY), heap,
  10027. DYNAMIC_TYPE_ECC);
  10028. if (key == NULL) {
  10029. WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_KEY failure");
  10030. err = 1;
  10031. }
  10032. if (!err) {
  10033. /* Reset all fields to 0. */
  10034. XMEMSET(key, 0, sizeof(WOLFSSL_EC_KEY));
  10035. /* Cache heap hint. */
  10036. key->heap = heap;
  10037. /* Initialize fields to defaults. */
  10038. key->form = POINT_CONVERSION_UNCOMPRESSED;
  10039. /* Initialize reference count. */
  10040. wolfSSL_RefInit(&key->ref, &err);
  10041. #ifdef WOLFSSL_REFCNT_ERROR_RETURN
  10042. }
  10043. if (!err) {
  10044. #endif
  10045. /* Allocate memory for internal EC key representation. */
  10046. key->internal = (ecc_key*)XMALLOC(sizeof(ecc_key), heap,
  10047. DYNAMIC_TYPE_ECC);
  10048. if (key->internal == NULL) {
  10049. WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc ecc key failure");
  10050. err = 1;
  10051. }
  10052. }
  10053. if (!err) {
  10054. /* Initialize wolfCrypt EC key. */
  10055. if (wc_ecc_init_ex((ecc_key*)key->internal, heap, devId) != 0) {
  10056. WOLFSSL_MSG("wolfSSL_EC_KEY_new init ecc key failure");
  10057. err = 1;
  10058. }
  10059. }
  10060. if (!err) {
  10061. /* Group unknown at creation */
  10062. key->group = wolfSSL_EC_GROUP_new_by_curve_name(NID_undef);
  10063. if (key->group == NULL) {
  10064. WOLFSSL_MSG("wolfSSL_EC_KEY_new malloc WOLFSSL_EC_GROUP failure");
  10065. err = 1;
  10066. }
  10067. }
  10068. if (!err) {
  10069. /* Allocate a point as public key. */
  10070. key->pub_key = wolfSSL_EC_POINT_new(key->group);
  10071. if (key->pub_key == NULL) {
  10072. WOLFSSL_MSG("wolfSSL_EC_POINT_new failure");
  10073. err = 1;
  10074. }
  10075. }
  10076. if (!err) {
  10077. /* Allocate a BN as private key. */
  10078. key->priv_key = wolfSSL_BN_new();
  10079. if (key->priv_key == NULL) {
  10080. WOLFSSL_MSG("wolfSSL_BN_new failure");
  10081. err = 1;
  10082. }
  10083. }
  10084. if (err) {
  10085. /* Dispose of EC key on error. */
  10086. wolfSSL_EC_KEY_free(key);
  10087. key = NULL;
  10088. }
  10089. /* Return new EC key object. */
  10090. return key;
  10091. }
  10092. /* Allocate a new EC key.
  10093. *
  10094. * @return New, allocated EC key on success.
  10095. * @return NULL on error.
  10096. */
  10097. WOLFSSL_EC_KEY *wolfSSL_EC_KEY_new(void)
  10098. {
  10099. return wolfSSL_EC_KEY_new_ex(NULL, INVALID_DEVID);
  10100. }
  10101. /* Create new EC key with the group having the specified numeric ID.
  10102. *
  10103. * @param [in] nid Numeric ID.
  10104. * @return New, allocated EC key on success.
  10105. * @return NULL on error.
  10106. */
  10107. WOLFSSL_EC_KEY *wolfSSL_EC_KEY_new_by_curve_name(int nid)
  10108. {
  10109. WOLFSSL_EC_KEY *key;
  10110. int err = 0;
  10111. WOLFSSL_ENTER("wolfSSL_EC_KEY_new_by_curve_name");
  10112. /* Allocate empty, EC key. */
  10113. key = wolfSSL_EC_KEY_new();
  10114. if (key == NULL) {
  10115. WOLFSSL_MSG("wolfSSL_EC_KEY_new failure");
  10116. err = 1;
  10117. }
  10118. if (!err) {
  10119. /* Set group to be nid. */
  10120. ec_group_set_nid(key->group, nid);
  10121. if (key->group->curve_idx == -1) {
  10122. wolfSSL_EC_KEY_free(key);
  10123. key = NULL;
  10124. }
  10125. }
  10126. /* Return the new EC key object. */
  10127. return key;
  10128. }
  10129. /* Dispose of the EC key and allocated data.
  10130. *
  10131. * Cannot use key after this call.
  10132. *
  10133. * @param [in] key EC key to free.
  10134. */
  10135. void wolfSSL_EC_KEY_free(WOLFSSL_EC_KEY *key)
  10136. {
  10137. int doFree = 0;
  10138. int err;
  10139. (void)err;
  10140. WOLFSSL_ENTER("wolfSSL_EC_KEY_free");
  10141. if (key != NULL) {
  10142. void* heap = key->heap;
  10143. /* Decrement reference count. */
  10144. wolfSSL_RefDec(&key->ref, &doFree, &err);
  10145. if (doFree) {
  10146. /* Dispose of allocated reference counting data. */
  10147. wolfSSL_RefFree(&key->ref);
  10148. /* Dispose of private key. */
  10149. wolfSSL_BN_free(key->priv_key);
  10150. wolfSSL_EC_POINT_free(key->pub_key);
  10151. wolfSSL_EC_GROUP_free(key->group);
  10152. if (key->internal != NULL) {
  10153. /* Dispose of wolfCrypt representation of EC key. */
  10154. wc_ecc_free((ecc_key*)key->internal);
  10155. XFREE(key->internal, heap, DYNAMIC_TYPE_ECC);
  10156. }
  10157. /* Set back to NULLs for safety. */
  10158. ForceZero(key, sizeof(*key));
  10159. /* Dispose of the memory associated with the EC key. */
  10160. XFREE(key, heap, DYNAMIC_TYPE_ECC);
  10161. (void)heap;
  10162. }
  10163. }
  10164. }
  10165. /* Increments ref count of EC key.
  10166. *
  10167. * @param [in, out] key EC key.
  10168. * @return 1 on success
  10169. * @return 0 on error
  10170. */
  10171. int wolfSSL_EC_KEY_up_ref(WOLFSSL_EC_KEY* key)
  10172. {
  10173. int err = 1;
  10174. if (key != NULL) {
  10175. wolfSSL_RefInc(&key->ref, &err);
  10176. }
  10177. return !err;
  10178. }
  10179. #ifndef NO_CERTS
  10180. #if defined(OPENSSL_ALL)
  10181. /* Copy the internal, wolfCrypt EC key.
  10182. *
  10183. * @param [in, out] dst Destination wolfCrypt EC key.
  10184. * @param [in] src Source wolfCrypt EC key.
  10185. * @return 0 on success.
  10186. * @return Negative on error.
  10187. */
  10188. static int wolfssl_ec_key_int_copy(ecc_key* dst, const ecc_key* src)
  10189. {
  10190. int ret;
  10191. /* Copy public key. */
  10192. #if !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  10193. ret = wc_ecc_copy_point(&src->pubkey, &dst->pubkey);
  10194. #else
  10195. ret = wc_ecc_copy_point((ecc_point*)&src->pubkey, &dst->pubkey);
  10196. #endif
  10197. if (ret != MP_OKAY) {
  10198. WOLFSSL_MSG("wc_ecc_copy_point error");
  10199. }
  10200. if (ret == 0) {
  10201. /* Copy private key. */
  10202. ret = mp_copy(wc_ecc_key_get_priv(src), wc_ecc_key_get_priv(dst));
  10203. if (ret != MP_OKAY) {
  10204. WOLFSSL_MSG("mp_copy error");
  10205. }
  10206. }
  10207. if (ret == 0) {
  10208. /* Copy domain parameters. */
  10209. if (src->dp) {
  10210. ret = wc_ecc_set_curve(dst, 0, src->dp->id);
  10211. if (ret != 0) {
  10212. WOLFSSL_MSG("wc_ecc_set_curve error");
  10213. }
  10214. }
  10215. }
  10216. if (ret == 0) {
  10217. /* Copy the other components. */
  10218. dst->type = src->type;
  10219. dst->idx = src->idx;
  10220. dst->state = src->state;
  10221. dst->flags = src->flags;
  10222. }
  10223. return ret;
  10224. }
  10225. /* Copies ecc_key into new WOLFSSL_EC_KEY object
  10226. *
  10227. * Copies the internal representation as well.
  10228. *
  10229. * @param [in] src EC key to duplicate.
  10230. *
  10231. * @return EC key on success.
  10232. * @return NULL on error.
  10233. */
  10234. WOLFSSL_EC_KEY *wolfSSL_EC_KEY_dup(const WOLFSSL_EC_KEY *src)
  10235. {
  10236. int err = 0;
  10237. WOLFSSL_EC_KEY* newKey = NULL;
  10238. WOLFSSL_ENTER("wolfSSL_EC_KEY_dup");
  10239. /* Validate EC key. */
  10240. if ((src == NULL) || (src->internal == NULL) || (src->group == NULL) ||
  10241. (src->pub_key == NULL) || (src->priv_key == NULL)) {
  10242. WOLFSSL_MSG("src NULL error");
  10243. err = 1;
  10244. }
  10245. if (!err) {
  10246. /* Create a new, empty key. */
  10247. newKey = wolfSSL_EC_KEY_new();
  10248. if (newKey == NULL) {
  10249. WOLFSSL_MSG("wolfSSL_EC_KEY_new error");
  10250. err = 1;
  10251. }
  10252. }
  10253. if (!err) {
  10254. /* Copy internal EC key. */
  10255. if (wolfssl_ec_key_int_copy((ecc_key*)newKey->internal,
  10256. (ecc_key*)src->internal) != 0) {
  10257. WOLFSSL_MSG("Copying internal EC key error");
  10258. err = 1;
  10259. }
  10260. }
  10261. if (!err) {
  10262. /* Internal key set. */
  10263. newKey->inSet = 1;
  10264. /* Copy group */
  10265. err = wolfssl_ec_group_copy(newKey->group, src->group);
  10266. }
  10267. /* Copy public key. */
  10268. if ((!err) && (wolfSSL_EC_POINT_copy(newKey->pub_key, src->pub_key) != 1)) {
  10269. WOLFSSL_MSG("Copying EC public key error");
  10270. err = 1;
  10271. }
  10272. if (!err) {
  10273. /* Set header size of private key in PKCS#8 format.*/
  10274. newKey->pkcs8HeaderSz = src->pkcs8HeaderSz;
  10275. /* Copy private key. */
  10276. if (wolfSSL_BN_copy(newKey->priv_key, src->priv_key) == NULL) {
  10277. WOLFSSL_MSG("Copying EC private key error");
  10278. err = 1;
  10279. }
  10280. }
  10281. if (err) {
  10282. /* Dispose of EC key on error. */
  10283. wolfSSL_EC_KEY_free(newKey);
  10284. newKey = NULL;
  10285. }
  10286. /* Return the new EC key. */
  10287. return newKey;
  10288. }
  10289. #endif /* OPENSSL_ALL */
  10290. #endif /* !NO_CERTS */
  10291. /*
  10292. * EC key to/from bin/octet APIs
  10293. */
  10294. /* Create an EC key from the octet encoded public key.
  10295. *
  10296. * Behaviour checked against OpenSSL.
  10297. *
  10298. * @param [out] key Reference to EC key. Must pass in a valid object with
  10299. * group set.
  10300. * @param [in, out] in On in, reference to buffer that contains data.
  10301. * On out, reference to buffer after public key data.
  10302. * @param [in] len Length of data in the buffer. Must be length of the
  10303. * encoded public key.
  10304. * @return Allocated EC key on success.
  10305. * @return NULL on error.
  10306. */
  10307. WOLFSSL_EC_KEY *wolfSSL_o2i_ECPublicKey(WOLFSSL_EC_KEY **key,
  10308. const unsigned char **in, long len)
  10309. {
  10310. int err = 0;
  10311. WOLFSSL_EC_KEY* ret = NULL;
  10312. WOLFSSL_ENTER("wolfSSL_o2i_ECPublicKey");
  10313. /* Validate parameters: EC group needed to perform import. */
  10314. if ((key == NULL) || (*key == NULL) || ((*key)->group == NULL) ||
  10315. (in == NULL) || (*in == NULL) || (len <= 0)) {
  10316. WOLFSSL_MSG("wolfSSL_o2i_ECPublicKey Bad arguments");
  10317. err = 1;
  10318. }
  10319. if (!err) {
  10320. /* Return the EC key object passed in. */
  10321. ret = *key;
  10322. /* Import point into public key field. */
  10323. if (wolfSSL_EC_POINT_oct2point(ret->group, ret->pub_key, *in,
  10324. (size_t)len, NULL) != 1) {
  10325. WOLFSSL_MSG("wolfSSL_EC_POINT_oct2point error");
  10326. ret = NULL;
  10327. err = 1;
  10328. }
  10329. }
  10330. if (!err) {
  10331. /* Assumed length passed in is all the data. */
  10332. *in += len;
  10333. }
  10334. return ret;
  10335. }
  10336. /* Puts the encoded public key into out.
  10337. *
  10338. * Passing in NULL for out returns length only.
  10339. * Passing in NULL for *out has buffer allocated, encoded into and passed back.
  10340. * Passing non-NULL for *out has it encoded into and pointer moved past.
  10341. *
  10342. * @param [in] key EC key to encode.
  10343. * @param [in, out] out Reference to buffer to encode into. May be NULL or
  10344. * point to NULL.
  10345. * @return Length of encoding in bytes on success.
  10346. * @return 0 on error.
  10347. */
  10348. int wolfSSL_i2o_ECPublicKey(const WOLFSSL_EC_KEY *key, unsigned char **out)
  10349. {
  10350. int ret = 1;
  10351. size_t len = 0;
  10352. int form = POINT_CONVERSION_UNCOMPRESSED;
  10353. WOLFSSL_ENTER("wolfSSL_i2o_ECPublicKey");
  10354. /* Validate parameters. */
  10355. if (key == NULL) {
  10356. WOLFSSL_MSG("wolfSSL_i2o_ECPublicKey Bad arguments");
  10357. ret = 0;
  10358. }
  10359. /* Ensure the external key data is set from the internal EC key. */
  10360. if ((ret == 1) && (!key->exSet) && (SetECKeyExternal((WOLFSSL_EC_KEY*)
  10361. key) != 1)) {
  10362. WOLFSSL_MSG("SetECKeyExternal failure");
  10363. ret = 0;
  10364. }
  10365. if (ret == 1) {
  10366. #ifdef HAVE_COMP_KEY
  10367. /* Default to compressed form if not set */
  10368. form = (key->form != POINT_CONVERSION_UNCOMPRESSED) ?
  10369. POINT_CONVERSION_UNCOMPRESSED :
  10370. POINT_CONVERSION_COMPRESSED;
  10371. #endif
  10372. /* Calculate length of point encoding. */
  10373. len = wolfSSL_EC_POINT_point2oct(key->group, key->pub_key, form, NULL,
  10374. 0, NULL);
  10375. }
  10376. /* Encode if length calculated and pointer supplied to update. */
  10377. if ((ret == 1) && (len != 0) && (out != NULL)) {
  10378. unsigned char *tmp = NULL;
  10379. /* Allocate buffer for encoding if no buffer supplied. */
  10380. if (*out == NULL) {
  10381. tmp = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_OPENSSL);
  10382. if (tmp == NULL) {
  10383. WOLFSSL_MSG("malloc failed");
  10384. ret = 0;
  10385. }
  10386. }
  10387. else {
  10388. /* Get buffer to encode into. */
  10389. tmp = *out;
  10390. }
  10391. /* Encode public key into buffer. */
  10392. if ((ret == 1) && (wolfSSL_EC_POINT_point2oct(key->group, key->pub_key,
  10393. form, tmp, len, NULL) == 0)) {
  10394. ret = 0;
  10395. }
  10396. if (ret == 1) {
  10397. /* Return buffer if allocated. */
  10398. if (*out == NULL) {
  10399. *out = tmp;
  10400. }
  10401. else {
  10402. /* Step over encoded data if not allocated. */
  10403. *out += len;
  10404. }
  10405. }
  10406. else if (*out == NULL) {
  10407. /* Dispose of allocated buffer. */
  10408. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  10409. }
  10410. }
  10411. if (ret == 1) {
  10412. /* Return length on success. */
  10413. ret = (int)len;
  10414. }
  10415. return ret;
  10416. }
  10417. #ifdef HAVE_ECC_KEY_IMPORT
  10418. /* Create a EC key from the DER encoded private key.
  10419. *
  10420. * @param [out] key Reference to EC key.
  10421. * @param [in, out] in On in, reference to buffer that contains DER data.
  10422. * On out, reference to buffer after private key data.
  10423. * @param [in] long Length of data in the buffer. May be larger than the
  10424. * length of the encoded private key.
  10425. * @return Allocated EC key on success.
  10426. * @return NULL on error.
  10427. */
  10428. WOLFSSL_EC_KEY* wolfSSL_d2i_ECPrivateKey(WOLFSSL_EC_KEY** key,
  10429. const unsigned char** in, long len)
  10430. {
  10431. int err = 0;
  10432. word32 idx = 0;
  10433. WOLFSSL_EC_KEY* ret = NULL;
  10434. WOLFSSL_ENTER("wolfSSL_d2i_ECPrivateKey");
  10435. /* Validate parameters. */
  10436. if ((in == NULL) || (*in == NULL) || (len <= 0)) {
  10437. WOLFSSL_MSG("wolfSSL_d2i_ECPrivateKey Bad arguments");
  10438. err = 1;
  10439. }
  10440. /* Create a new, empty EC key. */
  10441. if ((!err) && ((ret = wolfSSL_EC_KEY_new()) == NULL)) {
  10442. WOLFSSL_MSG("wolfSSL_EC_KEY_new error");
  10443. err = 1;
  10444. }
  10445. /* Decode the private key DER data into internal EC key. */
  10446. if ((!err) && (wc_EccPrivateKeyDecode(*in, &idx, (ecc_key*)ret->internal,
  10447. (word32)len) != 0)) {
  10448. WOLFSSL_MSG("wc_EccPrivateKeyDecode error");
  10449. err = 1;
  10450. }
  10451. if (!err) {
  10452. /* Internal EC key setup. */
  10453. ret->inSet = 1;
  10454. /* Set the EC key from the internal values. */
  10455. if (SetECKeyExternal(ret) != 1) {
  10456. WOLFSSL_MSG("SetECKeyExternal error");
  10457. err = 1;
  10458. }
  10459. }
  10460. if (!err) {
  10461. /* Move buffer on to next byte after data used. */
  10462. *in += idx;
  10463. if (key) {
  10464. /* Return new EC key through reference. */
  10465. *key = ret;
  10466. }
  10467. }
  10468. if (err && (ret != NULL)) {
  10469. /* Dispose of allocated EC key. */
  10470. wolfSSL_EC_KEY_free(ret);
  10471. ret = NULL;
  10472. }
  10473. return ret;
  10474. }
  10475. #endif /* HAVE_ECC_KEY_IMPORT */
  10476. /* Enecode the private key of the EC key into the buffer as DER.
  10477. *
  10478. * @param [in] key EC key to encode.
  10479. * @param [in, out] out On in, reference to buffer to place DER encoding into.
  10480. * On out, reference to buffer adter the encoding.
  10481. * May be NULL.
  10482. * @return Length of DER encoding on success.
  10483. * @return 0 on error.
  10484. */
  10485. int wolfSSL_i2d_ECPrivateKey(const WOLFSSL_EC_KEY *key, unsigned char **out)
  10486. {
  10487. int err = 0;
  10488. word32 len = 0;
  10489. WOLFSSL_ENTER("wolfSSL_i2d_ECPrivateKey");
  10490. /* Validate parameters. */
  10491. if (key == NULL) {
  10492. WOLFSSL_MSG("wolfSSL_i2d_ECPrivateKey Bad arguments");
  10493. err = 1;
  10494. }
  10495. /* Update the internal EC key if not set. */
  10496. if ((!err) && (!key->inSet) && (SetECKeyInternal((WOLFSSL_EC_KEY*)key) !=
  10497. 1)) {
  10498. WOLFSSL_MSG("SetECKeyInternal error");
  10499. err = 1;
  10500. }
  10501. /* Calculate the length of the private key DER encoding using internal EC
  10502. * key. */
  10503. if ((!err) && ((int)(len = (word32)wc_EccKeyDerSize((ecc_key*)key->internal,
  10504. 0)) <= 0)) {
  10505. WOLFSSL_MSG("wc_EccKeyDerSize error");
  10506. err = 1;
  10507. }
  10508. /* Only return length when out is NULL. */
  10509. if ((!err) && (out != NULL)) {
  10510. unsigned char* buf = NULL;
  10511. /* Must have a buffer to encode into. */
  10512. if (*out == NULL) {
  10513. /* Allocate a new buffer of appropriate length. */
  10514. buf = (byte*)XMALLOC(len, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10515. if (buf == NULL) {
  10516. /* Error and return 0. */
  10517. err = 1;
  10518. len = 0;
  10519. }
  10520. else {
  10521. /* Return the allocated buffer. */
  10522. *out = buf;
  10523. }
  10524. }
  10525. /* Encode the internal EC key as a private key in DER format. */
  10526. if ((!err) && wc_EccPrivateKeyToDer((ecc_key*)key->internal, *out,
  10527. len) < 0) {
  10528. WOLFSSL_MSG("wc_EccPrivateKeyToDer error");
  10529. err = 1;
  10530. }
  10531. else if (buf != *out) {
  10532. /* Move the reference to byte past encoded private key. */
  10533. *out += len;
  10534. }
  10535. /* Dispose of any allocated buffer on error. */
  10536. if (err && (*out == buf)) {
  10537. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  10538. *out = NULL;
  10539. }
  10540. }
  10541. return (int)len;
  10542. }
  10543. /* Load private key into EC key from DER encoding.
  10544. *
  10545. * Not an OpenSSL compatibility API.
  10546. *
  10547. * @param [in, out] key EC key to put private key values into.
  10548. * @param [in] derBuf Buffer holding DER encoding.
  10549. * @param [in] derSz Size of DER encoding in bytes.
  10550. * @return 1 on success.
  10551. * @return -1 on error.
  10552. */
  10553. int wolfSSL_EC_KEY_LoadDer(WOLFSSL_EC_KEY* key, const unsigned char* derBuf,
  10554. int derSz)
  10555. {
  10556. return wolfSSL_EC_KEY_LoadDer_ex(key, derBuf, derSz,
  10557. WOLFSSL_EC_KEY_LOAD_PRIVATE);
  10558. }
  10559. /* Load private/public key into EC key from DER encoding.
  10560. *
  10561. * Not an OpenSSL compatibility API.
  10562. *
  10563. * @param [in, out] key EC key to put private/public key values into.
  10564. * @param [in] derBuf Buffer holding DER encoding.
  10565. * @param [in] derSz Size of DER encoding in bytes.
  10566. * @param [in] opt Key type option. Valid values:
  10567. * WOLFSSL_EC_KEY_LOAD_PRIVATE,
  10568. * WOLFSSL_EC_KEY_LOAD_PUBLIC.
  10569. * @return 1 on success.
  10570. * @return -1 on error.
  10571. */
  10572. int wolfSSL_EC_KEY_LoadDer_ex(WOLFSSL_EC_KEY* key, const unsigned char* derBuf,
  10573. int derSz, int opt)
  10574. {
  10575. int res = 1;
  10576. int ret;
  10577. word32 idx = 0;
  10578. word32 algId;
  10579. WOLFSSL_ENTER("wolfSSL_EC_KEY_LoadDer");
  10580. /* Validate parameters. */
  10581. if ((key == NULL) || (key->internal == NULL) || (derBuf == NULL) ||
  10582. (derSz <= 0)) {
  10583. WOLFSSL_MSG("Bad function arguments");
  10584. res = -1;
  10585. }
  10586. if ((res == 1) && (opt != WOLFSSL_EC_KEY_LOAD_PRIVATE) &&
  10587. (opt != WOLFSSL_EC_KEY_LOAD_PUBLIC)) {
  10588. res = -1;
  10589. }
  10590. if (res == 1) {
  10591. /* Assume no PKCS#8 header. */
  10592. key->pkcs8HeaderSz = 0;
  10593. /* Check if input buffer has PKCS8 header. In the case that it does not
  10594. * have a PKCS8 header then do not error out.
  10595. */
  10596. if ((ret = ToTraditionalInline_ex((const byte*)derBuf, &idx,
  10597. (word32)derSz, &algId)) > 0) {
  10598. WOLFSSL_MSG("Found PKCS8 header");
  10599. key->pkcs8HeaderSz = (word16)idx;
  10600. res = 1;
  10601. }
  10602. /* Error out on parsing error. */
  10603. else if (ret != ASN_PARSE_E) {
  10604. WOLFSSL_MSG("Unexpected error with trying to remove PKCS8 header");
  10605. res = -1;
  10606. }
  10607. }
  10608. if (res == 1) {
  10609. /* Load into internal EC key based on key type option. */
  10610. if (opt == WOLFSSL_EC_KEY_LOAD_PRIVATE) {
  10611. ret = wc_EccPrivateKeyDecode(derBuf, &idx, (ecc_key*)key->internal,
  10612. (word32)derSz);
  10613. }
  10614. else {
  10615. ret = wc_EccPublicKeyDecode(derBuf, &idx, (ecc_key*)key->internal,
  10616. (word32)derSz);
  10617. if (ret < 0) {
  10618. ecc_key *tmp = (ecc_key*)XMALLOC(sizeof(ecc_key),
  10619. ((ecc_key*)key->internal)->heap, DYNAMIC_TYPE_ECC);
  10620. if (tmp == NULL) {
  10621. ret = -1;
  10622. }
  10623. else {
  10624. /* We now try again as x.963 [point type][x][opt y]. */
  10625. ret = wc_ecc_init_ex(tmp, ((ecc_key*)key->internal)->heap,
  10626. INVALID_DEVID);
  10627. if (ret == 0) {
  10628. ret = wc_ecc_import_x963(derBuf, (word32)derSz, tmp);
  10629. if (ret == 0) {
  10630. /* Take ownership of new key - set tmp to the old
  10631. * key which will then be freed below. */
  10632. ecc_key *old = (ecc_key *)key->internal;
  10633. key->internal = tmp;
  10634. tmp = old;
  10635. idx = (word32)derSz;
  10636. }
  10637. wc_ecc_free(tmp);
  10638. }
  10639. XFREE(tmp, ((ecc_key*)key->internal)->heap,
  10640. DYNAMIC_TYPE_ECC);
  10641. }
  10642. }
  10643. }
  10644. if (ret < 0) {
  10645. /* Error returned from wolfSSL. */
  10646. if (opt == WOLFSSL_EC_KEY_LOAD_PRIVATE) {
  10647. WOLFSSL_MSG("wc_EccPrivateKeyDecode failed");
  10648. }
  10649. else {
  10650. WOLFSSL_MSG("wc_EccPublicKeyDecode failed");
  10651. }
  10652. res = -1;
  10653. }
  10654. /* Internal key updated - update whether it is a valid key. */
  10655. key->inSet = (res == 1);
  10656. }
  10657. /* Set the external EC key based on value in internal. */
  10658. if ((res == 1) && (SetECKeyExternal(key) != 1)) {
  10659. WOLFSSL_MSG("SetECKeyExternal failed");
  10660. res = -1;
  10661. }
  10662. return res;
  10663. }
  10664. /*
  10665. * EC key PEM APIs
  10666. */
  10667. #if (defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM)) || \
  10668. (!defined(NO_BIO) && (defined(WOLFSSL_KEY_GEN) || \
  10669. defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT)))
  10670. /* Encode the EC public key as DER.
  10671. *
  10672. * Also used by pem_write_pubkey().
  10673. *
  10674. * @param [in] key EC key to encode.
  10675. * @param [out] der Pointer through which buffer is returned.
  10676. * @param [in] heap Heap hint.
  10677. * @return Size of encoding on success.
  10678. * @return 0 on error.
  10679. */
  10680. static int wolfssl_ec_key_to_pubkey_der(WOLFSSL_EC_KEY* key,
  10681. unsigned char** der, void* heap)
  10682. {
  10683. int sz;
  10684. unsigned char* buf = NULL;
  10685. (void)heap;
  10686. /* Calculate encoded size to allocate. */
  10687. sz = wc_EccPublicKeyDerSize((ecc_key*)key->internal, 1);
  10688. if (sz <= 0) {
  10689. WOLFSSL_MSG("wc_EccPublicKeyDerSize failed");
  10690. sz = 0;
  10691. }
  10692. if (sz > 0) {
  10693. /* Allocate memory to hold encoding. */
  10694. buf = (byte*)XMALLOC((size_t)sz, heap, DYNAMIC_TYPE_TMP_BUFFER);
  10695. if (buf == NULL) {
  10696. WOLFSSL_MSG("malloc failed");
  10697. sz = 0;
  10698. }
  10699. }
  10700. if (sz > 0) {
  10701. /* Encode public key to DER using wolfSSL. */
  10702. sz = wc_EccPublicKeyToDer((ecc_key*)key->internal, buf, (word32)sz, 1);
  10703. if (sz < 0) {
  10704. WOLFSSL_MSG("wc_EccPublicKeyToDer failed");
  10705. sz = 0;
  10706. }
  10707. }
  10708. /* Return buffer on success. */
  10709. if (sz > 0) {
  10710. *der = buf;
  10711. }
  10712. else {
  10713. /* Dispose of any dynamically allocated data not returned. */
  10714. XFREE(buf, heap, DYNAMIC_TYPE_TMP_BUFFER);
  10715. }
  10716. return sz;
  10717. }
  10718. #endif
  10719. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_KEY_GEN)
  10720. /*
  10721. * Return code compliant with OpenSSL.
  10722. *
  10723. * @param [in] fp File pointer to write PEM encoding to.
  10724. * @param [in] key EC key to encode and write.
  10725. * @return 1 on success.
  10726. * @return 0 on error.
  10727. */
  10728. int wolfSSL_PEM_write_EC_PUBKEY(XFILE fp, WOLFSSL_EC_KEY* key)
  10729. {
  10730. int ret = 1;
  10731. unsigned char* derBuf = NULL;
  10732. int derSz = 0;
  10733. WOLFSSL_ENTER("wolfSSL_PEM_write_EC_PUBKEY");
  10734. /* Validate parameters. */
  10735. if ((fp == XBADFILE) || (key == NULL)) {
  10736. WOLFSSL_MSG("Bad argument.");
  10737. return 0;
  10738. }
  10739. /* Encode public key in EC key as DER. */
  10740. derSz = wolfssl_ec_key_to_pubkey_der(key, &derBuf, key->heap);
  10741. if (derSz == 0) {
  10742. ret = 0;
  10743. }
  10744. /* Write out to file the PEM encoding of the DER. */
  10745. if ((ret == 1) && (der_write_to_file_as_pem(derBuf, derSz, fp,
  10746. ECC_PUBLICKEY_TYPE, key->heap) != 1)) {
  10747. ret = 0;
  10748. }
  10749. /* Dispose of any dynamically allocated data. */
  10750. XFREE(derBuf, key->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10751. WOLFSSL_LEAVE("wolfSSL_PEM_write_EC_PUBKEY", ret);
  10752. return ret;
  10753. }
  10754. #endif
  10755. #ifndef NO_BIO
  10756. /* Read a PEM encoded EC public key from a BIO.
  10757. *
  10758. * @param [in] bio BIO to read EC public key from.
  10759. * @param [out] out Pointer to return EC key object through. May be NULL.
  10760. * @param [in] cb Password callback when PEM encrypted.
  10761. * @param [in] pass NUL terminated string for passphrase when PEM
  10762. * encrypted.
  10763. * @return New EC key object on success.
  10764. * @return NULL on error.
  10765. */
  10766. WOLFSSL_EC_KEY* wolfSSL_PEM_read_bio_EC_PUBKEY(WOLFSSL_BIO* bio,
  10767. WOLFSSL_EC_KEY** out, wc_pem_password_cb* cb, void *pass)
  10768. {
  10769. int err = 0;
  10770. WOLFSSL_EC_KEY* ec = NULL;
  10771. DerBuffer* der = NULL;
  10772. int keyFormat = 0;
  10773. WOLFSSL_ENTER("wolfSSL_PEM_read_bio_EC_PUBKEY");
  10774. /* Validate parameters. */
  10775. if (bio == NULL) {
  10776. err = 1;
  10777. }
  10778. if (!err) {
  10779. /* Create an empty EC key. */
  10780. ec = wolfSSL_EC_KEY_new();
  10781. if (ec == NULL) {
  10782. err = 1;
  10783. }
  10784. }
  10785. /* Read a PEM key in to a new DER buffer. */
  10786. if ((!err) && (pem_read_bio_key(bio, cb, pass, ECC_PUBLICKEY_TYPE,
  10787. &keyFormat, &der) <= 0)) {
  10788. err = 1;
  10789. }
  10790. /* Load the EC key with the public key from the DER encoding. */
  10791. if ((!err) && (wolfSSL_EC_KEY_LoadDer_ex(ec, der->buffer, (int)der->length,
  10792. WOLFSSL_EC_KEY_LOAD_PUBLIC) != 1)) {
  10793. WOLFSSL_ERROR_MSG("Error loading DER buffer into WOLFSSL_EC_KEY");
  10794. err = 1;
  10795. }
  10796. /* Dispose of dynamically allocated data not needed anymore. */
  10797. FreeDer(&der);
  10798. if (err) {
  10799. wolfSSL_EC_KEY_free(ec);
  10800. ec = NULL;
  10801. }
  10802. /* Return EC key through out if required. */
  10803. if ((out != NULL) && (ec != NULL)) {
  10804. *out = ec;
  10805. }
  10806. return ec;
  10807. }
  10808. /* Read a PEM encoded EC private key from a BIO.
  10809. *
  10810. * @param [in] bio BIO to read EC private key from.
  10811. * @param [out] out Pointer to return EC key object through. May be NULL.
  10812. * @param [in] cb Password callback when PEM encrypted.
  10813. * @param [in] pass NUL terminated string for passphrase when PEM
  10814. * encrypted.
  10815. * @return New EC key object on success.
  10816. * @return NULL on error.
  10817. */
  10818. WOLFSSL_EC_KEY* wolfSSL_PEM_read_bio_ECPrivateKey(WOLFSSL_BIO* bio,
  10819. WOLFSSL_EC_KEY** out, wc_pem_password_cb* cb, void *pass)
  10820. {
  10821. int err = 0;
  10822. WOLFSSL_EC_KEY* ec = NULL;
  10823. DerBuffer* der = NULL;
  10824. int keyFormat = 0;
  10825. WOLFSSL_ENTER("wolfSSL_PEM_read_bio_ECPrivateKey");
  10826. /* Validate parameters. */
  10827. if (bio == NULL) {
  10828. err = 1;
  10829. }
  10830. if (!err) {
  10831. /* Create an empty EC key. */
  10832. ec = wolfSSL_EC_KEY_new();
  10833. if (ec == NULL) {
  10834. err = 1;
  10835. }
  10836. }
  10837. /* Read a PEM key in to a new DER buffer.
  10838. * To check ENC EC PRIVATE KEY, it uses PRIVATEKEY_TYPE to call
  10839. * pem_read_bio_key(), and then check key format if it is EC.
  10840. */
  10841. if ((!err) && (pem_read_bio_key(bio, cb, pass, PRIVATEKEY_TYPE,
  10842. &keyFormat, &der) <= 0)) {
  10843. err = 1;
  10844. }
  10845. if (keyFormat != ECDSAk) {
  10846. WOLFSSL_ERROR_MSG("Error not EC key format");
  10847. err = 1;
  10848. }
  10849. /* Load the EC key with the private key from the DER encoding. */
  10850. if ((!err) && (wolfSSL_EC_KEY_LoadDer_ex(ec, der->buffer, (int)der->length,
  10851. WOLFSSL_EC_KEY_LOAD_PRIVATE) != 1)) {
  10852. WOLFSSL_ERROR_MSG("Error loading DER buffer into WOLFSSL_EC_KEY");
  10853. err = 1;
  10854. }
  10855. /* Dispose of dynamically allocated data not needed anymore. */
  10856. FreeDer(&der);
  10857. if (err) {
  10858. wolfSSL_EC_KEY_free(ec);
  10859. ec = NULL;
  10860. }
  10861. /* Return EC key through out if required. */
  10862. if ((out != NULL) && (ec != NULL)) {
  10863. *out = ec;
  10864. }
  10865. return ec;
  10866. }
  10867. #endif /* !NO_BIO */
  10868. #if defined(WOLFSSL_KEY_GEN)
  10869. #ifndef NO_BIO
  10870. /* Write out the EC public key as PEM to the BIO.
  10871. *
  10872. * @param [in] bio BIO to write PEM encoding to.
  10873. * @param [in] ec EC public key to encode.
  10874. * @return 1 on success.
  10875. * @return 0 on error.
  10876. */
  10877. int wolfSSL_PEM_write_bio_EC_PUBKEY(WOLFSSL_BIO* bio, WOLFSSL_EC_KEY* ec)
  10878. {
  10879. int ret = 1;
  10880. unsigned char* derBuf = NULL;
  10881. int derSz = 0;
  10882. WOLFSSL_ENTER("wolfSSL_PEM_write_bio_EC_PUBKEY");
  10883. /* Validate parameters. */
  10884. if ((bio == NULL) || (ec == NULL)) {
  10885. WOLFSSL_MSG("Bad Function Arguments");
  10886. return 0;
  10887. }
  10888. /* Encode public key in EC key as DER. */
  10889. derSz = wolfssl_ec_key_to_pubkey_der(ec, &derBuf, ec->heap);
  10890. if (derSz == 0) {
  10891. ret = 0;
  10892. }
  10893. /* Write out to BIO the PEM encoding of the EC private key. */
  10894. if ((ret == 1) && (der_write_to_bio_as_pem(derBuf, derSz, bio,
  10895. ECC_PUBLICKEY_TYPE) != 1)) {
  10896. ret = 0;
  10897. }
  10898. /* Dispose of any dynamically allocated data. */
  10899. XFREE(derBuf, ec->heap, DYNAMIC_TYPE_TMP_BUFFER);
  10900. return ret;
  10901. }
  10902. /* Write out the EC private key as PEM to the BIO.
  10903. *
  10904. * Return code compliant with OpenSSL.
  10905. *
  10906. * @param [in] bio BIO to write PEM encoding to.
  10907. * @param [in] ec EC private key to encode.
  10908. * @param [in] cipher Cipher to use when PEM encrypted. May be NULL.
  10909. * @param [in] passwd Password string when PEM encrypted. May be NULL.
  10910. * @param [in] passwdSz Length of password string when PEM encrypted.
  10911. * @param [in] cb Password callback when PEM encrypted. Unused.
  10912. * @param [in] pass NUL terminated string for passphrase when PEM
  10913. * encrypted. Unused.
  10914. * @return 1 on success.
  10915. * @return 0 on error.
  10916. */
  10917. int wolfSSL_PEM_write_bio_ECPrivateKey(WOLFSSL_BIO* bio, WOLFSSL_EC_KEY* ec,
  10918. const EVP_CIPHER* cipher, unsigned char* passwd, int passwdSz,
  10919. wc_pem_password_cb* cb, void* arg)
  10920. {
  10921. int ret = 1;
  10922. unsigned char* pem = NULL;
  10923. int pLen = 0;
  10924. (void)cb;
  10925. (void)arg;
  10926. /* Validate parameters. */
  10927. if ((bio == NULL) || (ec == NULL)) {
  10928. ret = 0;
  10929. }
  10930. /* Write EC private key to PEM. */
  10931. if ((ret == 1) && (wolfSSL_PEM_write_mem_ECPrivateKey(ec, cipher, passwd,
  10932. passwdSz, &pem, &pLen) != 1)) {
  10933. ret = 0;
  10934. }
  10935. /* Write PEM to BIO. */
  10936. if ((ret == 1) && (wolfSSL_BIO_write(bio, pem, pLen) != pLen)) {
  10937. WOLFSSL_ERROR_MSG("EC private key BIO write failed");
  10938. ret = 0;
  10939. }
  10940. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  10941. return ret;
  10942. }
  10943. #endif /* !NO_BIO */
  10944. /* Encode the EC private key as PEM into buffer.
  10945. *
  10946. * Return code compliant with OpenSSL.
  10947. * Not an OpenSSL API.
  10948. *
  10949. * @param [in] ec EC private key to encode.
  10950. * @param [in] cipher Cipher to use when PEM encrypted. May be NULL.
  10951. * @param [in] passwd Password string when PEM encrypted. May be NULL.
  10952. * @param [in] passwdSz Length of password string when PEM encrypted.
  10953. * @param [out] pem Newly allocated buffer holding PEM encoding.
  10954. * @param [out] pLen Length of PEM encoding in bytes.
  10955. * @return 1 on success.
  10956. * @return 0 on error.
  10957. */
  10958. int wolfSSL_PEM_write_mem_ECPrivateKey(WOLFSSL_EC_KEY* ec,
  10959. const EVP_CIPHER* cipher, unsigned char* passwd, int passwdSz,
  10960. unsigned char **pem, int *pLen)
  10961. {
  10962. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  10963. int ret = 1;
  10964. byte* derBuf = NULL;
  10965. word32 der_max_len = 0;
  10966. int derSz = 0;
  10967. WOLFSSL_MSG("wolfSSL_PEM_write_mem_ECPrivateKey");
  10968. /* Validate parameters. */
  10969. if ((pem == NULL) || (pLen == NULL) || (ec == NULL) ||
  10970. (ec->internal == NULL)) {
  10971. WOLFSSL_MSG("Bad function arguments");
  10972. ret = 0;
  10973. }
  10974. /* Ensure internal EC key is set from external. */
  10975. if ((ret == 1) && (ec->inSet == 0)) {
  10976. WOLFSSL_MSG("No ECC internal set, do it");
  10977. if (SetECKeyInternal(ec) != 1) {
  10978. WOLFSSL_MSG("SetECKeyInternal failed");
  10979. ret = 0;
  10980. }
  10981. }
  10982. if (ret == 1) {
  10983. /* Calculate maximum size of DER encoding.
  10984. * 4 > size of pub, priv + ASN.1 additional information */
  10985. der_max_len = 4 * (word32)wc_ecc_size((ecc_key*)ec->internal) +
  10986. AES_BLOCK_SIZE;
  10987. /* Allocate buffer big enough to hold encoding. */
  10988. derBuf = (byte*)XMALLOC((size_t)der_max_len, NULL,
  10989. DYNAMIC_TYPE_TMP_BUFFER);
  10990. if (derBuf == NULL) {
  10991. WOLFSSL_MSG("malloc failed");
  10992. ret = 0;
  10993. }
  10994. }
  10995. if (ret == 1) {
  10996. /* Encode EC private key as DER. */
  10997. derSz = wc_EccKeyToDer((ecc_key*)ec->internal, derBuf, der_max_len);
  10998. if (derSz < 0) {
  10999. WOLFSSL_MSG("wc_EccKeyToDer failed");
  11000. XFREE(derBuf, NULL, DYNAMIC_TYPE_DER);
  11001. ret = 0;
  11002. }
  11003. }
  11004. /* Convert DER to PEM - possibly encrypting. */
  11005. if ((ret == 1) && (der_to_enc_pem_alloc(derBuf, derSz, cipher, passwd,
  11006. passwdSz, ECC_PRIVATEKEY_TYPE, NULL, pem, pLen) != 1)) {
  11007. WOLFSSL_ERROR_MSG("der_to_enc_pem_alloc failed");
  11008. ret = 0;
  11009. }
  11010. return ret;
  11011. #else
  11012. (void)ec;
  11013. (void)cipher;
  11014. (void)passwd;
  11015. (void)passwdSz;
  11016. (void)pem;
  11017. (void)pLen;
  11018. return 0;
  11019. #endif /* WOLFSSL_PEM_TO_DER || WOLFSSL_DER_TO_PEM */
  11020. }
  11021. #ifndef NO_FILESYSTEM
  11022. /* Write out the EC private key as PEM to file.
  11023. *
  11024. * Return code compliant with OpenSSL.
  11025. *
  11026. * @param [in] fp File pointer to write PEM encoding to.
  11027. * @param [in] ec EC private key to encode.
  11028. * @param [in] cipher Cipher to use when PEM encrypted. May be NULL.
  11029. * @param [in] passwd Password string when PEM encrypted. May be NULL.
  11030. * @param [in] passwdSz Length of password string when PEM encrypted.
  11031. * @param [in] cb Password callback when PEM encrypted. Unused.
  11032. * @param [in] pass NUL terminated string for passphrase when PEM
  11033. * encrypted. Unused.
  11034. * @return 1 on success.
  11035. * @return 0 on error.
  11036. */
  11037. int wolfSSL_PEM_write_ECPrivateKey(XFILE fp, WOLFSSL_EC_KEY *ec,
  11038. const EVP_CIPHER *cipher, unsigned char *passwd, int passwdSz,
  11039. wc_pem_password_cb *cb, void *pass)
  11040. {
  11041. int ret = 1;
  11042. byte *pem = NULL;
  11043. int pLen = 0;
  11044. (void)cb;
  11045. (void)pass;
  11046. WOLFSSL_MSG("wolfSSL_PEM_write_ECPrivateKey");
  11047. /* Validate parameters. */
  11048. if ((fp == XBADFILE) || (ec == NULL) || (ec->internal == NULL)) {
  11049. WOLFSSL_MSG("Bad function arguments");
  11050. ret = 0;
  11051. }
  11052. /* Write EC private key to PEM. */
  11053. if ((ret == 1) && (wolfSSL_PEM_write_mem_ECPrivateKey(ec, cipher, passwd,
  11054. passwdSz, &pem, &pLen) != 1)) {
  11055. WOLFSSL_MSG("wolfSSL_PEM_write_mem_ECPrivateKey failed");
  11056. ret = 0;
  11057. }
  11058. /* Write out to file the PEM encoding of the EC private key. */
  11059. if ((ret == 1) && ((int)XFWRITE(pem, 1, (size_t)pLen, fp) != pLen)) {
  11060. WOLFSSL_MSG("ECC private key file write failed");
  11061. ret = 0;
  11062. }
  11063. /* Dispose of any dynamically allocated data. */
  11064. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  11065. return ret;
  11066. }
  11067. #endif /* NO_FILESYSTEM */
  11068. #endif /* defined(WOLFSSL_KEY_GEN) */
  11069. /*
  11070. * EC key print APIs
  11071. */
  11072. #ifndef NO_CERTS
  11073. #if defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  11074. !defined(NO_STDIO_FILESYSTEM)
  11075. /* Print the EC key to a file pointer as text.
  11076. *
  11077. * @param [in] fp File pointer.
  11078. * @param [in] key EC key to print.
  11079. * @param [in] indent Number of spaces to place before each line printed.
  11080. * @return 1 on success.
  11081. * @return 0 on failure.
  11082. */
  11083. int wolfSSL_EC_KEY_print_fp(XFILE fp, WOLFSSL_EC_KEY* key, int indent)
  11084. {
  11085. int ret = 1;
  11086. int bits = 0;
  11087. int priv = 0;
  11088. WOLFSSL_ENTER("wolfSSL_EC_KEY_print_fp");
  11089. /* Validate parameters. */
  11090. if ((fp == XBADFILE) || (key == NULL) || (key->group == NULL) ||
  11091. (indent < 0)) {
  11092. ret = 0;
  11093. }
  11094. if (ret == 1) {
  11095. /* Get EC groups order size in bits. */
  11096. bits = wolfSSL_EC_GROUP_order_bits(key->group);
  11097. if (bits <= 0) {
  11098. WOLFSSL_MSG("Failed to get group order bits.");
  11099. ret = 0;
  11100. }
  11101. }
  11102. if (ret == 1) {
  11103. const char* keyType;
  11104. /* Determine whether this is a private or public key. */
  11105. if ((key->priv_key != NULL) && (!wolfSSL_BN_is_zero(key->priv_key))) {
  11106. keyType = "Private-Key";
  11107. priv = 1;
  11108. }
  11109. else {
  11110. keyType = "Public-Key";
  11111. }
  11112. /* Print key header. */
  11113. if (XFPRINTF(fp, "%*s%s: (%d bit)\n", indent, "", keyType, bits) < 0) {
  11114. ret = 0;
  11115. }
  11116. }
  11117. if ((ret == 1) && priv) {
  11118. /* Print the private key BN. */
  11119. ret = pk_bn_field_print_fp(fp, indent, "priv", key->priv_key);
  11120. }
  11121. /* Check for public key data in EC key. */
  11122. if ((ret == 1) && (key->pub_key != NULL) && (key->pub_key->exSet)) {
  11123. /* Get the public key point as one BN. */
  11124. WOLFSSL_BIGNUM* pubBn = wolfSSL_EC_POINT_point2bn(key->group,
  11125. key->pub_key, POINT_CONVERSION_UNCOMPRESSED, NULL, NULL);
  11126. if (pubBn == NULL) {
  11127. WOLFSSL_MSG("wolfSSL_EC_POINT_point2bn failed.");
  11128. ret = 0;
  11129. }
  11130. else {
  11131. /* Print the public key in a BN. */
  11132. ret = pk_bn_field_print_fp(fp, indent, "pub", pubBn);
  11133. wolfSSL_BN_free(pubBn);
  11134. }
  11135. }
  11136. if (ret == 1) {
  11137. /* Get the NID of the group. */
  11138. int nid = wolfSSL_EC_GROUP_get_curve_name(key->group);
  11139. if (nid > 0) {
  11140. /* Convert the NID into a long name and NIST name. */
  11141. const char* curve = wolfSSL_OBJ_nid2ln(nid);
  11142. const char* nistName = wolfSSL_EC_curve_nid2nist(nid);
  11143. /* Print OID name if known. */
  11144. if ((curve != NULL) &&
  11145. (XFPRINTF(fp, "%*sASN1 OID: %s\n", indent, "", curve) < 0)) {
  11146. ret = 0;
  11147. }
  11148. /* Print NIST curve name if known. */
  11149. if ((nistName != NULL) &&
  11150. (XFPRINTF(fp, "%*sNIST CURVE: %s\n", indent, "",
  11151. nistName) < 0)) {
  11152. ret = 0;
  11153. }
  11154. }
  11155. }
  11156. WOLFSSL_LEAVE("wolfSSL_EC_KEY_print_fp", ret);
  11157. return ret;
  11158. }
  11159. #endif /* XFPRINTF && !NO_FILESYSTEM && !NO_STDIO_FILESYSTEM */
  11160. #endif /* !NO_CERTS */
  11161. /*
  11162. * EC_KEY get/set/test APIs
  11163. */
  11164. /* Set data of internal, wolfCrypt EC key object into EC key.
  11165. *
  11166. * EC_KEY wolfSSL -> OpenSSL
  11167. *
  11168. * @param [in, out] p EC key to update.
  11169. * @return 1 on success.
  11170. * @return -1 on failure.
  11171. */
  11172. int SetECKeyExternal(WOLFSSL_EC_KEY* eckey)
  11173. {
  11174. int ret = 1;
  11175. WOLFSSL_ENTER("SetECKeyExternal");
  11176. /* Validate parameter. */
  11177. if ((eckey == NULL) || (eckey->internal == NULL)) {
  11178. WOLFSSL_MSG("ec key NULL error");
  11179. ret = -1;
  11180. }
  11181. else {
  11182. ecc_key* key = (ecc_key*)eckey->internal;
  11183. /* Set group (OID, nid and idx) from wolfCrypt EC key. */
  11184. eckey->group->curve_oid = (int)key->dp->oidSum;
  11185. eckey->group->curve_nid = EccEnumToNID(key->dp->id);
  11186. eckey->group->curve_idx = key->idx;
  11187. if (eckey->pub_key->internal != NULL) {
  11188. /* Copy internal public point from internal key's public point. */
  11189. if (wc_ecc_copy_point(&key->pubkey,
  11190. (ecc_point*)eckey->pub_key->internal) != MP_OKAY) {
  11191. WOLFSSL_MSG("SetECKeyExternal ecc_copy_point failed");
  11192. ret = -1;
  11193. }
  11194. /* Set external public key from internal wolfCrypt, public key. */
  11195. if ((ret == 1) && (ec_point_external_set(eckey->pub_key) != 1)) {
  11196. WOLFSSL_MSG("SetECKeyExternal ec_point_external_set failed");
  11197. ret = -1;
  11198. }
  11199. }
  11200. /* set the external privkey */
  11201. if ((ret == 1) && (key->type == ECC_PRIVATEKEY) &&
  11202. (wolfssl_bn_set_value(&eckey->priv_key,
  11203. wc_ecc_key_get_priv(key)) != 1)) {
  11204. WOLFSSL_MSG("ec priv key error");
  11205. ret = -1;
  11206. }
  11207. /* External values set when operations succeeded. */
  11208. eckey->exSet = (ret == 1);
  11209. }
  11210. return ret;
  11211. }
  11212. /* Set data of EC key into internal, wolfCrypt EC key object.
  11213. *
  11214. * EC_KEY Openssl -> WolfSSL
  11215. *
  11216. * @param [in, out] p EC key to update.
  11217. * @return 1 on success.
  11218. * @return -1 on failure.
  11219. */
  11220. int SetECKeyInternal(WOLFSSL_EC_KEY* eckey)
  11221. {
  11222. int ret = 1;
  11223. WOLFSSL_ENTER("SetECKeyInternal");
  11224. /* Validate parameter. */
  11225. if ((eckey == NULL) || (eckey->internal == NULL) ||
  11226. (eckey->group == NULL)) {
  11227. WOLFSSL_MSG("ec key NULL error");
  11228. ret = -1;
  11229. }
  11230. else {
  11231. ecc_key* key = (ecc_key*)eckey->internal;
  11232. int pubSet = 0;
  11233. /* Validate group. */
  11234. if ((eckey->group->curve_idx < 0) ||
  11235. (wc_ecc_is_valid_idx(eckey->group->curve_idx) == 0)) {
  11236. WOLFSSL_MSG("invalid curve idx");
  11237. ret = -1;
  11238. }
  11239. if (ret == 1) {
  11240. /* Set group (idx of curve and corresponding domain parameters). */
  11241. key->idx = eckey->group->curve_idx;
  11242. key->dp = &ecc_sets[key->idx];
  11243. pubSet = (eckey->pub_key != NULL);
  11244. }
  11245. /* Set public key (point). */
  11246. if ((ret == 1) && pubSet) {
  11247. if (ec_point_internal_set(eckey->pub_key) != 1) {
  11248. WOLFSSL_MSG("ec key pub error");
  11249. ret = -1;
  11250. }
  11251. /* Copy public point to key. */
  11252. if ((ret == 1) && (wc_ecc_copy_point(
  11253. (ecc_point*)eckey->pub_key->internal, &key->pubkey) !=
  11254. MP_OKAY)) {
  11255. WOLFSSL_MSG("wc_ecc_copy_point error");
  11256. ret = -1;
  11257. }
  11258. if (ret == 1) {
  11259. /* Set that the internal key is a public key */
  11260. key->type = ECC_PUBLICKEY;
  11261. }
  11262. }
  11263. /* set privkey */
  11264. if ((ret == 1) && (eckey->priv_key != NULL)) {
  11265. if (wolfssl_bn_get_value(eckey->priv_key,
  11266. wc_ecc_key_get_priv(key)) != 1) {
  11267. WOLFSSL_MSG("ec key priv error");
  11268. ret = -1;
  11269. }
  11270. /* private key */
  11271. if ((ret == 1) && (!mp_iszero(wc_ecc_key_get_priv(key)))) {
  11272. if (pubSet) {
  11273. key->type = ECC_PRIVATEKEY;
  11274. }
  11275. else {
  11276. key->type = ECC_PRIVATEKEY_ONLY;
  11277. }
  11278. }
  11279. }
  11280. /* Internal values set when operations succeeded. */
  11281. eckey->inSet = (ret == 1);
  11282. }
  11283. return ret;
  11284. }
  11285. /* Get point conversion format of EC key.
  11286. *
  11287. * @param [in] key EC key.
  11288. * @return Point conversion format on success.
  11289. * @return -1 on error.
  11290. */
  11291. point_conversion_form_t wolfSSL_EC_KEY_get_conv_form(const WOLFSSL_EC_KEY* key)
  11292. {
  11293. int ret = -1;
  11294. if (key != NULL) {
  11295. ret = key->form;
  11296. }
  11297. return ret;
  11298. }
  11299. /* Set point conversion format into EC key.
  11300. *
  11301. * @param [in, out] key EC key to set format into.
  11302. * @param [in] form Point conversion format. Valid values:
  11303. * POINT_CONVERSION_UNCOMPRESSED,
  11304. * POINT_CONVERSION_COMPRESSED (when HAVE_COMP_KEY)
  11305. */
  11306. void wolfSSL_EC_KEY_set_conv_form(WOLFSSL_EC_KEY *key, int form)
  11307. {
  11308. if (key == NULL) {
  11309. WOLFSSL_MSG("Key passed in NULL");
  11310. }
  11311. else if (form == POINT_CONVERSION_UNCOMPRESSED
  11312. #ifdef HAVE_COMP_KEY
  11313. || form == POINT_CONVERSION_COMPRESSED
  11314. #endif
  11315. ) {
  11316. key->form = (unsigned char)form;
  11317. }
  11318. else {
  11319. WOLFSSL_MSG("Incorrect form or HAVE_COMP_KEY not compiled in");
  11320. }
  11321. }
  11322. /* Get the EC group object that is in EC key.
  11323. *
  11324. * @param [in] key EC key.
  11325. * @return EC group object on success.
  11326. * @return NULL when key is NULL.
  11327. */
  11328. const WOLFSSL_EC_GROUP *wolfSSL_EC_KEY_get0_group(const WOLFSSL_EC_KEY *key)
  11329. {
  11330. WOLFSSL_EC_GROUP* group = NULL;
  11331. WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_group");
  11332. if (key != NULL) {
  11333. group = key->group;
  11334. }
  11335. return group;
  11336. }
  11337. /* Set the group in WOLFSSL_EC_KEY
  11338. *
  11339. * @param [in, out] key EC key to update.
  11340. * @param [in] group EC group to copy.
  11341. * @return 1 on success
  11342. * @return 0 on failure.
  11343. */
  11344. int wolfSSL_EC_KEY_set_group(WOLFSSL_EC_KEY *key, WOLFSSL_EC_GROUP *group)
  11345. {
  11346. int ret = 1;
  11347. WOLFSSL_ENTER("wolfSSL_EC_KEY_set_group");
  11348. /* Validate parameters. */
  11349. if ((key == NULL) || (group == NULL)) {
  11350. ret = 0;
  11351. }
  11352. if (ret == 1) {
  11353. /* Dispose of the current group. */
  11354. if (key->group != NULL) {
  11355. wolfSSL_EC_GROUP_free(key->group);
  11356. }
  11357. /* Duplicate the passed in group into EC key. */
  11358. key->group = wolfSSL_EC_GROUP_dup(group);
  11359. if (key->group == NULL) {
  11360. ret = 0;
  11361. }
  11362. }
  11363. return ret;
  11364. }
  11365. /* Get the BN object that is the private key in the EC key.
  11366. *
  11367. * @param [in] key EC key.
  11368. * @return BN object on success.
  11369. * @return NULL when key is NULL or private key is not set.
  11370. */
  11371. WOLFSSL_BIGNUM *wolfSSL_EC_KEY_get0_private_key(const WOLFSSL_EC_KEY *key)
  11372. {
  11373. WOLFSSL_BIGNUM* priv_key = NULL;
  11374. WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_private_key");
  11375. /* Validate parameter. */
  11376. if (key == NULL) {
  11377. WOLFSSL_MSG("wolfSSL_EC_KEY_get0_private_key Bad arguments");
  11378. }
  11379. /* Only return private key if it is not 0. */
  11380. else if (!wolfSSL_BN_is_zero(key->priv_key)) {
  11381. priv_key = key->priv_key;
  11382. }
  11383. return priv_key;
  11384. }
  11385. /* Sets the private key value into EC key.
  11386. *
  11387. * Return code compliant with OpenSSL.
  11388. *
  11389. * @param [in, out] key EC key to set.
  11390. * @param [in] priv_key Private key value in a BN.
  11391. * @return 1 on success
  11392. * @return 0 on failure.
  11393. */
  11394. int wolfSSL_EC_KEY_set_private_key(WOLFSSL_EC_KEY *key,
  11395. const WOLFSSL_BIGNUM *priv_key)
  11396. {
  11397. int ret = 1;
  11398. WOLFSSL_ENTER("wolfSSL_EC_KEY_set_private_key");
  11399. /* Validate parameters. */
  11400. if ((key == NULL) || (priv_key == NULL)) {
  11401. WOLFSSL_MSG("Bad arguments");
  11402. ret = 0;
  11403. }
  11404. /* Check for obvious invalid values. */
  11405. if (wolfSSL_BN_is_negative(priv_key) || wolfSSL_BN_is_zero(priv_key) ||
  11406. wolfSSL_BN_is_one(priv_key)) {
  11407. WOLFSSL_MSG("Invalid private key value");
  11408. ret = 0;
  11409. }
  11410. if (ret == 1) {
  11411. /* Free key if previously set. */
  11412. if (key->priv_key != NULL) {
  11413. wolfSSL_BN_free(key->priv_key);
  11414. }
  11415. /* Duplicate the BN passed in. */
  11416. key->priv_key = wolfSSL_BN_dup(priv_key);
  11417. if (key->priv_key == NULL) {
  11418. WOLFSSL_MSG("key ecc priv key NULL");
  11419. ret = 0;
  11420. }
  11421. }
  11422. /* Set the external values into internal EC key. */
  11423. if ((ret == 1) && (SetECKeyInternal(key) != 1)) {
  11424. WOLFSSL_MSG("SetECKeyInternal failed");
  11425. /* Dispose of new private key on error. */
  11426. wolfSSL_BN_free(key->priv_key);
  11427. key->priv_key = NULL;
  11428. ret = 0;
  11429. }
  11430. return ret;
  11431. }
  11432. /* Get the public key EC point object that is in EC key.
  11433. *
  11434. * @param [in] key EC key.
  11435. * @return EC point object that is the public key on success.
  11436. * @return NULL when key is NULL.
  11437. */
  11438. WOLFSSL_EC_POINT* wolfSSL_EC_KEY_get0_public_key(const WOLFSSL_EC_KEY *key)
  11439. {
  11440. WOLFSSL_EC_POINT* pub_key = NULL;
  11441. WOLFSSL_ENTER("wolfSSL_EC_KEY_get0_public_key");
  11442. if (key != NULL) {
  11443. pub_key = key->pub_key;
  11444. }
  11445. return pub_key;
  11446. }
  11447. /*
  11448. * Return code compliant with OpenSSL.
  11449. *
  11450. * @param [in, out] key EC key.
  11451. * @param [in] pub Public key as an EC point.
  11452. * @return 1 on success
  11453. * @return 0 on failure.
  11454. */
  11455. int wolfSSL_EC_KEY_set_public_key(WOLFSSL_EC_KEY *key,
  11456. const WOLFSSL_EC_POINT *pub)
  11457. {
  11458. int ret = 1;
  11459. ecc_point *pub_p = NULL;
  11460. ecc_point *key_p = NULL;
  11461. WOLFSSL_ENTER("wolfSSL_EC_KEY_set_public_key");
  11462. /* Validate parameters. */
  11463. if ((key == NULL) || (key->internal == NULL) || (pub == NULL) ||
  11464. (pub->internal == NULL)) {
  11465. WOLFSSL_MSG("wolfSSL_EC_KEY_set_public_key Bad arguments");
  11466. ret = 0;
  11467. }
  11468. /* Ensure the internal EC key is set. */
  11469. if ((ret == 1) && (key->inSet == 0) && (SetECKeyInternal(key) != 1)) {
  11470. WOLFSSL_MSG("SetECKeyInternal failed");
  11471. ret = 0;
  11472. }
  11473. /* Ensure the internal EC point of pub is setup. */
  11474. if ((ret == 1) && (ec_point_setup(pub) != 1)) {
  11475. ret = 0;
  11476. }
  11477. if (ret == 1) {
  11478. /* Get the internal point of pub and the public key in key. */
  11479. pub_p = (ecc_point*)pub->internal;
  11480. key_p = (ecc_point*)key->pub_key->internal;
  11481. /* Create new point if required. */
  11482. if (key_p == NULL) {
  11483. key_p = wc_ecc_new_point();
  11484. key->pub_key->internal = (void*)key_p;
  11485. }
  11486. /* Check point available. */
  11487. if (key_p == NULL) {
  11488. WOLFSSL_MSG("key ecc point NULL");
  11489. ret = 0;
  11490. }
  11491. }
  11492. /* Copy the internal pub point into internal key point. */
  11493. if ((ret == 1) && (wc_ecc_copy_point(pub_p, key_p) != MP_OKAY)) {
  11494. WOLFSSL_MSG("ecc_copy_point failure");
  11495. ret = 0;
  11496. }
  11497. /* Copy the internal point data into external. */
  11498. if ((ret == 1) && (ec_point_external_set(key->pub_key) != 1)) {
  11499. WOLFSSL_MSG("SetECKeyInternal failed");
  11500. ret = 0;
  11501. }
  11502. /* Copy the internal key into external. */
  11503. if ((ret == 1) && (SetECKeyInternal(key) != 1)) {
  11504. WOLFSSL_MSG("SetECKeyInternal failed");
  11505. ret = 0;
  11506. }
  11507. if (ret == 1) {
  11508. /* Dump out the point and the key's public key for debug. */
  11509. wolfSSL_EC_POINT_dump("pub", pub);
  11510. wolfSSL_EC_POINT_dump("key->pub_key", key->pub_key);
  11511. }
  11512. return ret;
  11513. }
  11514. #ifndef NO_WOLFSSL_STUB
  11515. /* Set the ASN.1 encoding flag against the EC key.
  11516. *
  11517. * No implementation as only named curves supported for encoding.
  11518. *
  11519. * @param [in, out] key EC key.
  11520. * @param [in] flag ASN.1 flag to set. Valid values:
  11521. * OPENSSL_EC_EXPLICIT_CURVE, OPENSSL_EC_NAMED_CURVE
  11522. */
  11523. void wolfSSL_EC_KEY_set_asn1_flag(WOLFSSL_EC_KEY *key, int asn1_flag)
  11524. {
  11525. (void)key;
  11526. (void)asn1_flag;
  11527. WOLFSSL_ENTER("wolfSSL_EC_KEY_set_asn1_flag");
  11528. WOLFSSL_STUB("EC_KEY_set_asn1_flag");
  11529. }
  11530. #endif
  11531. /*
  11532. * EC key generate key APIs
  11533. */
  11534. /* Generate an EC key.
  11535. *
  11536. * Uses the internal curve index set in the EC key or the default.
  11537. *
  11538. * @param [in, out] key EC key.
  11539. * @return 1 on success
  11540. * @return 0 on failure.
  11541. */
  11542. int wolfSSL_EC_KEY_generate_key(WOLFSSL_EC_KEY *key)
  11543. {
  11544. int res = 1;
  11545. int initTmpRng = 0;
  11546. WC_RNG* rng = NULL;
  11547. #ifdef WOLFSSL_SMALL_STACK
  11548. WC_RNG* tmpRng = NULL;
  11549. #else
  11550. WC_RNG tmpRng[1];
  11551. #endif
  11552. WOLFSSL_ENTER("wolfSSL_EC_KEY_generate_key");
  11553. /* Validate parameters. */
  11554. if ((key == NULL) || (key->internal == NULL) || (key->group == NULL)) {
  11555. WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key Bad arguments");
  11556. res = 0;
  11557. }
  11558. if (res == 1) {
  11559. /* Check if we know which internal curve index to use. */
  11560. if (key->group->curve_idx < 0) {
  11561. /* Generate key using the default curve. */
  11562. key->group->curve_idx = ECC_CURVE_DEF;
  11563. }
  11564. /* Create a random number generator. */
  11565. rng = wolfssl_make_rng(tmpRng, &initTmpRng);
  11566. if (rng == NULL) {
  11567. WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key failed to set RNG");
  11568. res = 0;
  11569. }
  11570. }
  11571. if (res == 1) {
  11572. /* NIDToEccEnum returns -1 for invalid NID so if key->group->curve_nid
  11573. * is 0 then pass ECC_CURVE_DEF as arg */
  11574. int eccEnum = key->group->curve_nid ?
  11575. NIDToEccEnum(key->group->curve_nid) : ECC_CURVE_DEF;
  11576. /* Get the internal EC key. */
  11577. ecc_key* ecKey = (ecc_key*)key->internal;
  11578. /* Make the key using internal API. */
  11579. int ret = wc_ecc_make_key_ex(rng, 0, ecKey, eccEnum);
  11580. #if defined(WOLFSSL_ASYNC_CRYPT)
  11581. /* Wait on asynchronouse operation. */
  11582. ret = wc_AsyncWait(ret, &ecKey->asyncDev, WC_ASYNC_FLAG_NONE);
  11583. #endif
  11584. if (ret != 0) {
  11585. WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key wc_ecc_make_key failed");
  11586. res = 0;
  11587. }
  11588. }
  11589. /* Dispose of local random number generator if initialized. */
  11590. if (initTmpRng) {
  11591. wc_FreeRng(rng);
  11592. #ifdef WOLFSSL_SMALL_STACK
  11593. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  11594. #endif
  11595. }
  11596. /* Set the external key from new internal key values. */
  11597. if ((res == 1) && (SetECKeyExternal(key) != 1)) {
  11598. WOLFSSL_MSG("wolfSSL_EC_KEY_generate_key SetECKeyExternal failed");
  11599. res = 0;
  11600. }
  11601. return res;
  11602. }
  11603. /*
  11604. * EC key check key APIs
  11605. */
  11606. /* Check that the EC key is valid.
  11607. *
  11608. * @param [in] key EC key.
  11609. * @return 1 on valid.
  11610. * @return 0 on invalid or error.
  11611. */
  11612. int wolfSSL_EC_KEY_check_key(const WOLFSSL_EC_KEY *key)
  11613. {
  11614. int ret = 1;
  11615. WOLFSSL_ENTER("wolfSSL_EC_KEY_check_key");
  11616. /* Validate parameter. */
  11617. if ((key == NULL) || (key->internal == NULL)) {
  11618. WOLFSSL_MSG("Bad parameter");
  11619. ret = 0;
  11620. }
  11621. /* Set the external EC key values into internal if not already. */
  11622. if ((ret == 1) && (key->inSet == 0) && (SetECKeyInternal(
  11623. (WOLFSSL_EC_KEY*)key) != 1)) {
  11624. WOLFSSL_MSG("SetECKeyInternal failed");
  11625. ret = 0;
  11626. }
  11627. if (ret == 1) {
  11628. /* Have internal EC implementation check key. */
  11629. ret = wc_ecc_check_key((ecc_key*)key->internal) == 0;
  11630. }
  11631. return ret;
  11632. }
  11633. /* End EC_KEY */
  11634. #if !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  11635. /* Get the supported, built-in EC curves
  11636. *
  11637. * @param [in, out] curves Pre-allocated list to put supported curves into.
  11638. * @param [in] len Maximum number of items to place in list.
  11639. * @return Number of built-in EC curves when curves is NULL or len is 0.
  11640. * @return Number of items placed in list otherwise.
  11641. */
  11642. size_t wolfSSL_EC_get_builtin_curves(WOLFSSL_EC_BUILTIN_CURVE *curves,
  11643. size_t len)
  11644. {
  11645. size_t i;
  11646. size_t cnt;
  11647. #ifdef HAVE_SELFTEST
  11648. /* Defined in ecc.h when available. */
  11649. size_t ecc_sets_count;
  11650. /* Count the pre-defined curves since global not available. */
  11651. for (i = 0; ecc_sets[i].size != 0 && ecc_sets[i].name != NULL; i++) {
  11652. /* Do nothing. */
  11653. }
  11654. ecc_sets_count = i;
  11655. #endif
  11656. /* Assume we are going to return total count. */
  11657. cnt = ecc_sets_count;
  11658. /* Check we have a list that can hold data. */
  11659. if ((curves != NULL) && (len != 0)) {
  11660. /* Limit count to length of list. */
  11661. if (cnt > len) {
  11662. cnt = len;
  11663. }
  11664. /* Put in built-in EC curve nid and short name. */
  11665. for (i = 0; i < cnt; i++) {
  11666. curves[i].nid = EccEnumToNID(ecc_sets[i].id);
  11667. curves[i].comment = wolfSSL_OBJ_nid2sn(curves[i].nid);
  11668. }
  11669. }
  11670. return cnt;
  11671. }
  11672. #endif /* !HAVE_FIPS || FIPS_VERSION_GT(2,0) */
  11673. /* Start ECDSA_SIG */
  11674. /* Allocate a new ECDSA signature object.
  11675. *
  11676. * @return New, allocated ECDSA signature object on success.
  11677. * @return NULL on error.
  11678. */
  11679. WOLFSSL_ECDSA_SIG *wolfSSL_ECDSA_SIG_new(void)
  11680. {
  11681. int err = 0;
  11682. WOLFSSL_ECDSA_SIG *sig;
  11683. WOLFSSL_ENTER("wolfSSL_ECDSA_SIG_new");
  11684. /* Allocate memory for ECDSA signature object. */
  11685. sig = (WOLFSSL_ECDSA_SIG*)XMALLOC(sizeof(WOLFSSL_ECDSA_SIG), NULL,
  11686. DYNAMIC_TYPE_ECC);
  11687. if (sig == NULL) {
  11688. WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA signature failure");
  11689. err = 1;
  11690. }
  11691. if (!err) {
  11692. /* Set s to NULL in case of error. */
  11693. sig->s = NULL;
  11694. /* Allocate BN into r. */
  11695. sig->r = wolfSSL_BN_new();
  11696. if (sig->r == NULL) {
  11697. WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA r failure");
  11698. err = 1;
  11699. }
  11700. }
  11701. if (!err) {
  11702. /* Allocate BN into s. */
  11703. sig->s = wolfSSL_BN_new();
  11704. if (sig->s == NULL) {
  11705. WOLFSSL_MSG("wolfSSL_ECDSA_SIG_new malloc ECDSA s failure");
  11706. err = 1;
  11707. }
  11708. }
  11709. if (err && (sig != NULL)) {
  11710. /* Dispose of allocated memory. */
  11711. wolfSSL_ECDSA_SIG_free(sig);
  11712. sig = NULL;
  11713. }
  11714. return sig;
  11715. }
  11716. /* Dispose of ECDSA signature object.
  11717. *
  11718. * Cannot use object after this call.
  11719. *
  11720. * @param [in] sig ECDSA signature object to free.
  11721. */
  11722. void wolfSSL_ECDSA_SIG_free(WOLFSSL_ECDSA_SIG *sig)
  11723. {
  11724. WOLFSSL_ENTER("wolfSSL_ECDSA_SIG_free");
  11725. if (sig != NULL) {
  11726. /* Dispose of BNs allocated for r and s. */
  11727. wolfSSL_BN_free(sig->r);
  11728. wolfSSL_BN_free(sig->s);
  11729. /* Dispose of memory associated with ECDSA signature object. */
  11730. XFREE(sig, NULL, DYNAMIC_TYPE_ECC);
  11731. }
  11732. }
  11733. /* Create an ECDSA signature from the DER encoding.
  11734. *
  11735. * @param [in, out] sig Reference to ECDSA signature object. May be NULL.
  11736. * @param [in, out] pp On in, reference to buffer containing DER encoding.
  11737. * On out, reference to buffer after signature data.
  11738. * @param [in] len Length of the data in the buffer. May be more than
  11739. * the length of the signature.
  11740. * @return ECDSA signature object on success.
  11741. * @return NULL on error.
  11742. */
  11743. WOLFSSL_ECDSA_SIG* wolfSSL_d2i_ECDSA_SIG(WOLFSSL_ECDSA_SIG** sig,
  11744. const unsigned char** pp, long len)
  11745. {
  11746. int err = 0;
  11747. /* ECDSA signature object to return. */
  11748. WOLFSSL_ECDSA_SIG *s = NULL;
  11749. /* Validate parameter. */
  11750. if (pp == NULL) {
  11751. err = 1;
  11752. }
  11753. if (!err) {
  11754. if (sig != NULL) {
  11755. /* Use the ECDSA signature object passed in. */
  11756. s = *sig;
  11757. }
  11758. if (s == NULL) {
  11759. /* No ECDSA signature object passed in - create a new one. */
  11760. s = wolfSSL_ECDSA_SIG_new();
  11761. if (s == NULL) {
  11762. err = 1;
  11763. }
  11764. }
  11765. }
  11766. if (!err) {
  11767. /* DecodeECC_DSA_Sig calls mp_init, so free these. */
  11768. mp_free((mp_int*)s->r->internal);
  11769. mp_free((mp_int*)s->s->internal);
  11770. /* Decode the signature into internal r and s fields. */
  11771. if (DecodeECC_DSA_Sig(*pp, (word32)len, (mp_int*)s->r->internal,
  11772. (mp_int*)s->s->internal) != MP_OKAY) {
  11773. err = 1;
  11774. }
  11775. }
  11776. if (!err) {
  11777. /* Move pointer passed signature data successfully decoded. */
  11778. *pp += wolfssl_der_length(*pp, (int)len);
  11779. if (sig != NULL) {
  11780. /* Update reference to ECDSA signature object. */
  11781. *sig = s;
  11782. }
  11783. }
  11784. /* Dispose of newly allocated object on error. */
  11785. if (err) {
  11786. if ((s != NULL) && ((sig == NULL) || (*sig != s))) {
  11787. wolfSSL_ECDSA_SIG_free(s);
  11788. }
  11789. /* Return NULL for object on error. */
  11790. s = NULL;
  11791. }
  11792. return s;
  11793. }
  11794. /* Encode the ECDSA signature as DER.
  11795. *
  11796. * @param [in] sig ECDSA signature object.
  11797. * @param [in, out] pp On in, reference to buffer in which to place encoding.
  11798. * On out, reference to buffer after encoding.
  11799. * May be NULL or point to NULL in which case no encoding
  11800. * is done.
  11801. * @return Length of encoding on success.
  11802. * @return 0 on error.
  11803. */
  11804. int wolfSSL_i2d_ECDSA_SIG(const WOLFSSL_ECDSA_SIG *sig, unsigned char **pp)
  11805. {
  11806. word32 len = 0;
  11807. /* Validate parameter. */
  11808. if (sig != NULL) {
  11809. /* ASN.1: SEQ + INT + INT
  11810. * ASN.1 Integer must be a positive value - prepend zero if number has
  11811. * top bit set.
  11812. */
  11813. /* Get total length of r including any prepended zero. */
  11814. word32 rLen = (word32)(mp_leading_bit((mp_int*)sig->r->internal) +
  11815. mp_unsigned_bin_size((mp_int*)sig->r->internal));
  11816. /* Get total length of s including any prepended zero. */
  11817. word32 sLen = (word32)(mp_leading_bit((mp_int*)sig->s->internal) +
  11818. mp_unsigned_bin_size((mp_int*)sig->s->internal));
  11819. /* Calculate length of data in sequence. */
  11820. len = (word32)1 + ASN_LEN_SIZE(rLen) + rLen +
  11821. (word32)1 + ASN_LEN_SIZE(sLen) + sLen;
  11822. /* Add in the length of the SEQUENCE. */
  11823. len += (word32)1 + ASN_LEN_SIZE(len);
  11824. /* Encode only if there is a buffer to encode into. */
  11825. if ((pp != NULL) && (*pp != NULL)) {
  11826. /* Encode using the internal representations of r and s. */
  11827. if (StoreECC_DSA_Sig(*pp, &len, (mp_int*)sig->r->internal,
  11828. (mp_int*)sig->s->internal) != MP_OKAY) {
  11829. /* No bytes encoded. */
  11830. len = 0;
  11831. }
  11832. else {
  11833. /* Update pointer to after encoding. */
  11834. *pp += len;
  11835. }
  11836. }
  11837. }
  11838. return (int)len;
  11839. }
  11840. /* Get the pointer to the feilds of the ECDSA signature.
  11841. *
  11842. * r and s untouched when sig is NULL.
  11843. *
  11844. * @param [in] sig ECDSA signature object.
  11845. * @param [out] r R field of ECDSA signature as a BN. May be NULL.
  11846. * @param [out] s S field of ECDSA signature as a BN. May be NULL.
  11847. */
  11848. void wolfSSL_ECDSA_SIG_get0(const WOLFSSL_ECDSA_SIG* sig,
  11849. const WOLFSSL_BIGNUM** r, const WOLFSSL_BIGNUM** s)
  11850. {
  11851. /* Validate parameter. */
  11852. if (sig != NULL) {
  11853. /* Return the r BN when pointer to return through. */
  11854. if (r != NULL) {
  11855. *r = sig->r;
  11856. }
  11857. /* Return the s BN when pointer to return through. */
  11858. if (s != NULL) {
  11859. *s = sig->s;
  11860. }
  11861. }
  11862. }
  11863. /* Set the pointers to the fields of the ECDSA signature.
  11864. *
  11865. * @param [in, out] sig ECDSA signature object to update.
  11866. * @param [in] r R field of ECDSA signature as a BN.
  11867. * @param [in] s S field of ECDSA signature as a BN.
  11868. * @return 1 on success.
  11869. * @return 0 on error.
  11870. */
  11871. int wolfSSL_ECDSA_SIG_set0(WOLFSSL_ECDSA_SIG* sig, WOLFSSL_BIGNUM* r,
  11872. WOLFSSL_BIGNUM* s)
  11873. {
  11874. int ret = 1;
  11875. /* Validate parameters. */
  11876. if ((sig == NULL) || (r == NULL) || (s == NULL)) {
  11877. ret = 0;
  11878. }
  11879. if (ret == 1) {
  11880. /* Dispose of old BN objects. */
  11881. wolfSSL_BN_free(sig->r);
  11882. wolfSSL_BN_free(sig->s);
  11883. /* Assign new BN objects. */
  11884. sig->r = r;
  11885. sig->s = s;
  11886. }
  11887. return ret;
  11888. }
  11889. /* End ECDSA_SIG */
  11890. /* Start ECDSA */
  11891. /* Calculate maximum size of the DER encoded ECDSA signature for the curve.
  11892. *
  11893. * @param [in] key EC key.
  11894. * @return Size of DER encoded signature on success.
  11895. * @return 0 on error.
  11896. */
  11897. int wolfSSL_ECDSA_size(const WOLFSSL_EC_KEY *key)
  11898. {
  11899. int err = 0;
  11900. int len = 0;
  11901. const EC_GROUP *group = NULL;
  11902. int bits = 0;
  11903. /* Validate parameter. */
  11904. if (key == NULL) {
  11905. err = 1;
  11906. }
  11907. /* Get group from key to get order bits. */
  11908. if ((!err) && ((group = wolfSSL_EC_KEY_get0_group(key)) == NULL)) {
  11909. err = 1;
  11910. }
  11911. /* Get order bits of group. */
  11912. if ((!err) && ((bits = wolfSSL_EC_GROUP_order_bits(group)) == 0)) {
  11913. /* Group is not set. */
  11914. err = 1;
  11915. }
  11916. if (!err) {
  11917. /* r and s are mod order. */
  11918. int bytes = (bits + 7) / 8; /* Bytes needed to hold bits. */
  11919. len = SIG_HEADER_SZ + /* 2*ASN_TAG + 2*LEN(ENUM) */
  11920. ECC_MAX_PAD_SZ + /* possible leading zeroes in r and s */
  11921. bytes + bytes; /* max r and s in bytes */
  11922. }
  11923. return len;
  11924. }
  11925. /* Create ECDSA signature by signing digest with key.
  11926. *
  11927. * @param [in] dgst Digest to sign.
  11928. * @param [in] dLen Length of digest in bytes.
  11929. * @param [in] key EC key to sign with.
  11930. * @return ECDSA signature object on success.
  11931. * @return NULL on error.
  11932. */
  11933. WOLFSSL_ECDSA_SIG *wolfSSL_ECDSA_do_sign(const unsigned char *dgst, int dLen,
  11934. WOLFSSL_EC_KEY *key)
  11935. {
  11936. int err = 0;
  11937. WOLFSSL_ECDSA_SIG *sig = NULL;
  11938. #ifdef WOLFSSL_SMALL_STACK
  11939. byte* out = NULL;
  11940. #else
  11941. byte out[ECC_BUFSIZE];
  11942. #endif
  11943. unsigned int outLen = ECC_BUFSIZE;
  11944. WOLFSSL_ENTER("wolfSSL_ECDSA_do_sign");
  11945. /* Validate parameters. */
  11946. if ((dgst == NULL) || (key == NULL) || (key->internal == NULL)) {
  11947. WOLFSSL_MSG("wolfSSL_ECDSA_do_sign Bad arguments");
  11948. err = 1;
  11949. }
  11950. /* Ensure internal EC key is set from external. */
  11951. if ((!err) && (key->inSet == 0)) {
  11952. WOLFSSL_MSG("wolfSSL_ECDSA_do_sign No EC key internal set, do it");
  11953. if (SetECKeyInternal(key) != 1) {
  11954. WOLFSSL_MSG("wolfSSL_ECDSA_do_sign SetECKeyInternal failed");
  11955. err = 1;
  11956. }
  11957. }
  11958. #ifdef WOLFSSL_SMALL_STACK
  11959. if (!err) {
  11960. /* Allocate buffer to hold encoded signature. */
  11961. out = (byte*)XMALLOC(outLen, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11962. if (out == NULL) {
  11963. err = 1;
  11964. }
  11965. }
  11966. #endif
  11967. /* Sign the digest with the key to create encoded ECDSA signature. */
  11968. if ((!err) && (wolfSSL_ECDSA_sign(0, dgst, dLen, out, &outLen, key) != 1)) {
  11969. err = 1;
  11970. }
  11971. if (!err) {
  11972. const byte* p = out;
  11973. /* Decode the ECDSA signature into a new object. */
  11974. sig = wolfSSL_d2i_ECDSA_SIG(NULL, &p, outLen);
  11975. }
  11976. #ifdef WOLFSSL_SMALL_STACK
  11977. /* Dispose of any temporary dynamically allocated data. */
  11978. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11979. #endif
  11980. return sig;
  11981. }
  11982. /* Verify ECDSA signature in the object using digest and key.
  11983. *
  11984. * Return code compliant with OpenSSL.
  11985. *
  11986. * @param [in] dgst Digest to verify.
  11987. * @param [in] dLen Length of the digest in bytes.
  11988. * @param [in] sig ECDSA signature object.
  11989. * @param [in] key EC key containing public key.
  11990. * @return 1 when signature is valid.
  11991. * @return 0 when signature is invalid.
  11992. * @return -1 on error.
  11993. */
  11994. int wolfSSL_ECDSA_do_verify(const unsigned char *dgst, int dLen,
  11995. const WOLFSSL_ECDSA_SIG *sig, WOLFSSL_EC_KEY *key)
  11996. {
  11997. int ret = 1;
  11998. int verified = 0;
  11999. #ifdef WOLF_CRYPTO_CB_ONLY_ECC
  12000. byte signature[ECC_MAX_SIG_SIZE];
  12001. int signatureLen;
  12002. byte* p = signature;
  12003. #endif
  12004. WOLFSSL_ENTER("wolfSSL_ECDSA_do_verify");
  12005. /* Validate parameters. */
  12006. if ((dgst == NULL) || (sig == NULL) || (key == NULL) ||
  12007. (key->internal == NULL)) {
  12008. WOLFSSL_MSG("wolfSSL_ECDSA_do_verify Bad arguments");
  12009. ret = -1;
  12010. }
  12011. /* Ensure internal EC key is set from external. */
  12012. if ((ret == 1) && (key->inSet == 0)) {
  12013. WOLFSSL_MSG("No EC key internal set, do it");
  12014. if (SetECKeyInternal(key) != 1) {
  12015. WOLFSSL_MSG("SetECKeyInternal failed");
  12016. ret = -1;
  12017. }
  12018. }
  12019. if (ret == 1) {
  12020. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  12021. /* Verify hash using digest, r and s as MP ints and internal EC key. */
  12022. if (wc_ecc_verify_hash_ex((mp_int*)sig->r->internal,
  12023. (mp_int*)sig->s->internal, dgst, (word32)dLen, &verified,
  12024. (ecc_key *)key->internal) != MP_OKAY) {
  12025. WOLFSSL_MSG("wc_ecc_verify_hash failed");
  12026. ret = -1;
  12027. }
  12028. else if (verified == 0) {
  12029. WOLFSSL_MSG("wc_ecc_verify_hash incorrect signature detected");
  12030. ret = 0;
  12031. }
  12032. #else
  12033. signatureLen = i2d_ECDSA_SIG(sig, &p);
  12034. if (signatureLen > 0) {
  12035. /* verify hash. expects to call wc_CryptoCb_EccVerify internally */
  12036. ret = wc_ecc_verify_hash(signature, signatureLen, dgst,
  12037. (word32)dLen, &verified, (ecc_key*)key->internal);
  12038. if (ret != MP_OKAY) {
  12039. WOLFSSL_MSG("wc_ecc_verify_hash failed");
  12040. ret = -1;
  12041. }
  12042. else if (verified == 0) {
  12043. WOLFSSL_MSG("wc_ecc_verify_hash incorrect signature detected");
  12044. ret = 0;
  12045. }
  12046. }
  12047. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  12048. }
  12049. return ret;
  12050. }
  12051. /* Sign the digest with the key to produce a DER encode signature.
  12052. *
  12053. * @param [in] type Digest algorithm used to create digest. Unused.
  12054. * @param [in] digest Digest of the message to sign.
  12055. * @param [in] digestSz Size of the digest in bytes.
  12056. * @param [out] sig Buffer to hold signature.
  12057. * @param [in, out] sigSz On in, size of buffer in bytes.
  12058. * On out, size of signatre in bytes.
  12059. * @param [in] key EC key containing private key.
  12060. * @return 1 on success.
  12061. * @return 0 on error.
  12062. */
  12063. int wolfSSL_ECDSA_sign(int type, const unsigned char *digest, int digestSz,
  12064. unsigned char *sig, unsigned int *sigSz, WOLFSSL_EC_KEY *key)
  12065. {
  12066. int ret = 1;
  12067. WC_RNG* rng = NULL;
  12068. #ifdef WOLFSSL_SMALL_STACK
  12069. WC_RNG* tmpRng = NULL;
  12070. #else
  12071. WC_RNG tmpRng[1];
  12072. #endif
  12073. int initTmpRng = 0;
  12074. WOLFSSL_ENTER("wolfSSL_ECDSA_sign");
  12075. /* Digest algorithm not used in DER encoding. */
  12076. (void)type;
  12077. /* Validate parameters. */
  12078. if (key == NULL) {
  12079. ret = 0;
  12080. }
  12081. if (ret == 1) {
  12082. /* Make an RNG - create local or get global. */
  12083. rng = wolfssl_make_rng(tmpRng, &initTmpRng);
  12084. if (rng == NULL) {
  12085. ret = 0;
  12086. }
  12087. }
  12088. /* Sign the digest with the key using the RNG and put signature into buffer
  12089. * update sigSz to be actual length.
  12090. */
  12091. if ((ret == 1) && (wc_ecc_sign_hash(digest, (word32)digestSz, sig, sigSz,
  12092. rng, (ecc_key*)key->internal) != 0)) {
  12093. ret = 0;
  12094. }
  12095. if (initTmpRng) {
  12096. wc_FreeRng(rng);
  12097. #ifdef WOLFSSL_SMALL_STACK
  12098. XFREE(rng, NULL, DYNAMIC_TYPE_RNG);
  12099. #endif
  12100. }
  12101. return ret;
  12102. }
  12103. /* Verify the signature with the digest and key.
  12104. *
  12105. * @param [in] type Digest algorithm used to create digest. Unused.
  12106. * @param [in] digest Digest of the message to verify.
  12107. * @param [in] digestSz Size of the digest in bytes.
  12108. * @param [in] sig Buffer holding signature.
  12109. * @param [in] sigSz Size of signature data in bytes.
  12110. * @param [in] key EC key containing public key.
  12111. * @return 1 when signature is valid.
  12112. * @return 0 when signature is invalid or error.
  12113. */
  12114. int wolfSSL_ECDSA_verify(int type, const unsigned char *digest, int digestSz,
  12115. const unsigned char *sig, int sigSz, WOLFSSL_EC_KEY *key)
  12116. {
  12117. int ret = 1;
  12118. int verify = 0;
  12119. WOLFSSL_ENTER("wolfSSL_ECDSA_verify");
  12120. /* Digest algorithm not used in DER encoding. */
  12121. (void)type;
  12122. /* Validate parameters. */
  12123. if (key == NULL) {
  12124. ret = 0;
  12125. }
  12126. /* Verify signature using digest and key. */
  12127. if ((ret == 1) && (wc_ecc_verify_hash(sig, (word32)sigSz, digest,
  12128. (word32)digestSz, &verify, (ecc_key*)key->internal) != 0)) {
  12129. ret = 0;
  12130. }
  12131. /* When no error, verification may still have failed - check now. */
  12132. if ((ret == 1) && (verify != 1)) {
  12133. WOLFSSL_MSG("wolfSSL_ECDSA_verify failed");
  12134. ret = 0;
  12135. }
  12136. return ret;
  12137. }
  12138. /* End ECDSA */
  12139. /* Start ECDH */
  12140. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  12141. /* Compute the shared secret (key) using ECDH.
  12142. *
  12143. * KDF not supported.
  12144. *
  12145. * Return code compliant with OpenSSL.
  12146. *
  12147. * @param [out] out Buffer to hold key.
  12148. * @param [in] outLen Length of buffer in bytes.
  12149. * @param [in] pubKey Public key as an EC point.
  12150. * @param [in] privKey EC key holding a private key.
  12151. * @param [in] kdf Key derivation function to apply to secret.
  12152. * @return Length of computed key on success
  12153. * @return 0 on error.
  12154. */
  12155. int wolfSSL_ECDH_compute_key(void *out, size_t outLen,
  12156. const WOLFSSL_EC_POINT *pubKey, WOLFSSL_EC_KEY *privKey,
  12157. void *(*kdf) (const void *in, size_t inlen, void *out, size_t *outLen))
  12158. {
  12159. int err = 0;
  12160. word32 len = 0;
  12161. ecc_key* key = NULL;
  12162. #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) && \
  12163. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,0))
  12164. int setGlobalRNG = 0;
  12165. #endif
  12166. /* TODO: support using the KDF. */
  12167. (void)kdf;
  12168. WOLFSSL_ENTER("wolfSSL_ECDH_compute_key");
  12169. /* Validate parameters. */
  12170. if ((out == NULL) || (pubKey == NULL) || (pubKey->internal == NULL) ||
  12171. (privKey == NULL) || (privKey->internal == NULL)) {
  12172. WOLFSSL_MSG("Bad function arguments");
  12173. err = 1;
  12174. }
  12175. /* Ensure internal EC key is set from external. */
  12176. if ((!err) && (privKey->inSet == 0)) {
  12177. WOLFSSL_MSG("No EC key internal set, do it");
  12178. if (SetECKeyInternal(privKey) != 1) {
  12179. WOLFSSL_MSG("SetECKeyInternal failed");
  12180. err = 1;
  12181. }
  12182. }
  12183. if (!err) {
  12184. int ret;
  12185. /* Get the internal key. */
  12186. key = (ecc_key*)privKey->internal;
  12187. /* Set length into variable of type suitable for wolfSSL API. */
  12188. len = (word32)outLen;
  12189. #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) && \
  12190. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,0))
  12191. /* An RNG is needed. */
  12192. if (key->rng == NULL) {
  12193. key->rng = wolfssl_make_global_rng();
  12194. /* RNG set and needs to be unset. */
  12195. setGlobalRNG = 1;
  12196. }
  12197. #endif
  12198. PRIVATE_KEY_UNLOCK();
  12199. /* Create secret using wolfSSL. */
  12200. ret = wc_ecc_shared_secret_ex(key, (ecc_point*)pubKey->internal,
  12201. (byte *)out, &len);
  12202. PRIVATE_KEY_LOCK();
  12203. if (ret != MP_OKAY) {
  12204. WOLFSSL_MSG("wc_ecc_shared_secret failed");
  12205. err = 1;
  12206. }
  12207. }
  12208. #if defined(ECC_TIMING_RESISTANT) && !defined(HAVE_SELFTEST) && \
  12209. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5,0))
  12210. /* Remove global from key. */
  12211. if (setGlobalRNG) {
  12212. key->rng = NULL;
  12213. }
  12214. #endif
  12215. if (err) {
  12216. /* Make returned value zero. */
  12217. len = 0;
  12218. }
  12219. return (int)len;
  12220. }
  12221. #endif /* WOLF_CRYPTO_CB_ONLY_ECC */
  12222. /* End ECDH */
  12223. #endif /* OPENSSL_EXTRA */
  12224. #endif /* HAVE_ECC */
  12225. /*******************************************************************************
  12226. * END OF EC API
  12227. ******************************************************************************/
  12228. #endif /* !WOLFSSL_PK_INCLUDED */