ssl.c 314 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351
  1. /* ssl.c
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL 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. * CyaSSL 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-1301, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/ctaocrypt/settings.h>
  25. #ifdef HAVE_ERRNO_H
  26. #include <errno.h>
  27. #endif
  28. #include <cyassl/ssl.h>
  29. #include <cyassl/internal.h>
  30. #include <cyassl/error-ssl.h>
  31. #include <cyassl/ctaocrypt/coding.h>
  32. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  33. #include <cyassl/openssl/evp.h>
  34. #endif
  35. #ifdef OPENSSL_EXTRA
  36. /* openssl headers begin */
  37. #include <cyassl/openssl/hmac.h>
  38. #include <cyassl/openssl/crypto.h>
  39. #include <cyassl/openssl/des.h>
  40. #include <cyassl/openssl/bn.h>
  41. #include <cyassl/openssl/dh.h>
  42. #include <cyassl/openssl/rsa.h>
  43. #include <cyassl/openssl/pem.h>
  44. /* openssl headers end, cyassl internal headers next */
  45. #include <cyassl/ctaocrypt/hmac.h>
  46. #include <cyassl/ctaocrypt/random.h>
  47. #include <cyassl/ctaocrypt/des3.h>
  48. #include <cyassl/ctaocrypt/md4.h>
  49. #include <cyassl/ctaocrypt/md5.h>
  50. #include <cyassl/ctaocrypt/arc4.h>
  51. #ifdef CYASSL_SHA512
  52. #include <cyassl/ctaocrypt/sha512.h>
  53. #endif
  54. #endif
  55. #ifndef NO_FILESYSTEM
  56. #if !defined(USE_WINDOWS_API) && !defined(NO_CYASSL_DIR) \
  57. && !defined(EBSNET)
  58. #include <dirent.h>
  59. #include <sys/stat.h>
  60. #endif
  61. #ifdef EBSNET
  62. #include "vfapi.h"
  63. #include "vfile.h"
  64. #endif
  65. #endif /* NO_FILESYSTEM */
  66. #ifndef TRUE
  67. #define TRUE 1
  68. #endif
  69. #ifndef FALSE
  70. #define FALSE 0
  71. #endif
  72. #ifndef min
  73. static INLINE word32 min(word32 a, word32 b)
  74. {
  75. return a > b ? b : a;
  76. }
  77. #endif /* min */
  78. #ifndef max
  79. #ifdef CYASSL_DTLS
  80. static INLINE word32 max(word32 a, word32 b)
  81. {
  82. return a > b ? a : b;
  83. }
  84. #endif
  85. #endif /* min */
  86. #ifndef CYASSL_LEANPSK
  87. char* mystrnstr(const char* s1, const char* s2, unsigned int n)
  88. {
  89. unsigned int s2_len = (unsigned int)XSTRLEN(s2);
  90. if (s2_len == 0)
  91. return (char*)s1;
  92. while (n >= s2_len && s1[0]) {
  93. if (s1[0] == s2[0])
  94. if (XMEMCMP(s1, s2, s2_len) == 0)
  95. return (char*)s1;
  96. s1++;
  97. n--;
  98. }
  99. return NULL;
  100. }
  101. #endif
  102. /* prevent multiple mutex initializations */
  103. static volatile int initRefCount = 0;
  104. static CyaSSL_Mutex count_mutex; /* init ref count mutex */
  105. CYASSL_CTX* CyaSSL_CTX_new(CYASSL_METHOD* method)
  106. {
  107. CYASSL_CTX* ctx = NULL;
  108. CYASSL_ENTER("CYASSL_CTX_new");
  109. if (initRefCount == 0)
  110. CyaSSL_Init(); /* user no longer forced to call Init themselves */
  111. if (method == NULL)
  112. return ctx;
  113. ctx = (CYASSL_CTX*) XMALLOC(sizeof(CYASSL_CTX), 0, DYNAMIC_TYPE_CTX);
  114. if (ctx) {
  115. if (InitSSL_Ctx(ctx, method) < 0) {
  116. CYASSL_MSG("Init CTX failed");
  117. CyaSSL_CTX_free(ctx);
  118. ctx = NULL;
  119. }
  120. }
  121. else {
  122. CYASSL_MSG("Alloc CTX failed, method freed");
  123. XFREE(method, NULL, DYNAMIC_TYPE_METHOD);
  124. }
  125. CYASSL_LEAVE("CYASSL_CTX_new", 0);
  126. return ctx;
  127. }
  128. void CyaSSL_CTX_free(CYASSL_CTX* ctx)
  129. {
  130. CYASSL_ENTER("SSL_CTX_free");
  131. if (ctx)
  132. FreeSSL_Ctx(ctx);
  133. CYASSL_LEAVE("SSL_CTX_free", 0);
  134. }
  135. CYASSL* CyaSSL_new(CYASSL_CTX* ctx)
  136. {
  137. CYASSL* ssl = NULL;
  138. int ret = 0;
  139. (void)ret;
  140. CYASSL_ENTER("SSL_new");
  141. if (ctx == NULL)
  142. return ssl;
  143. ssl = (CYASSL*) XMALLOC(sizeof(CYASSL), ctx->heap,DYNAMIC_TYPE_SSL);
  144. if (ssl)
  145. if ( (ret = InitSSL(ssl, ctx)) < 0) {
  146. FreeSSL(ssl);
  147. ssl = 0;
  148. }
  149. CYASSL_LEAVE("SSL_new", ret);
  150. return ssl;
  151. }
  152. void CyaSSL_free(CYASSL* ssl)
  153. {
  154. CYASSL_ENTER("SSL_free");
  155. if (ssl)
  156. FreeSSL(ssl);
  157. CYASSL_LEAVE("SSL_free", 0);
  158. }
  159. #ifdef HAVE_POLY1305
  160. /* set if to use old poly 1 for yes 0 to use new poly */
  161. int CyaSSL_use_old_poly(CYASSL* ssl, int value)
  162. {
  163. CYASSL_ENTER("SSL_use_old_poly");
  164. ssl->options.oldPoly = value;
  165. CYASSL_LEAVE("SSL_use_old_poly", 0);
  166. return 0;
  167. }
  168. #endif
  169. int CyaSSL_set_fd(CYASSL* ssl, int fd)
  170. {
  171. CYASSL_ENTER("SSL_set_fd");
  172. ssl->rfd = fd; /* not used directly to allow IO callbacks */
  173. ssl->wfd = fd;
  174. ssl->IOCB_ReadCtx = &ssl->rfd;
  175. ssl->IOCB_WriteCtx = &ssl->wfd;
  176. #ifdef CYASSL_DTLS
  177. if (ssl->options.dtls) {
  178. ssl->IOCB_ReadCtx = &ssl->buffers.dtlsCtx;
  179. ssl->IOCB_WriteCtx = &ssl->buffers.dtlsCtx;
  180. ssl->buffers.dtlsCtx.fd = fd;
  181. }
  182. #endif
  183. CYASSL_LEAVE("SSL_set_fd", SSL_SUCCESS);
  184. return SSL_SUCCESS;
  185. }
  186. int CyaSSL_get_ciphers(char* buf, int len)
  187. {
  188. const char* const* ciphers = GetCipherNames();
  189. int totalInc = 0;
  190. int step = 0;
  191. char delim = ':';
  192. int size = GetCipherNamesSize();
  193. int i;
  194. if (buf == NULL || len <= 0)
  195. return BAD_FUNC_ARG;
  196. /* Add each member to the buffer delimitted by a : */
  197. for (i = 0; i < size; i++) {
  198. step = (int)(XSTRLEN(ciphers[i]) + 1); /* delimiter */
  199. totalInc += step;
  200. /* Check to make sure buf is large enough and will not overflow */
  201. if (totalInc < len) {
  202. XSTRNCPY(buf, ciphers[i], XSTRLEN(ciphers[i]));
  203. buf += XSTRLEN(ciphers[i]);
  204. if (i < size - 1)
  205. *buf++ = delim;
  206. }
  207. else
  208. return BUFFER_E;
  209. }
  210. return SSL_SUCCESS;
  211. }
  212. int CyaSSL_get_fd(const CYASSL* ssl)
  213. {
  214. CYASSL_ENTER("SSL_get_fd");
  215. CYASSL_LEAVE("SSL_get_fd", ssl->rfd);
  216. return ssl->rfd;
  217. }
  218. int CyaSSL_get_using_nonblock(CYASSL* ssl)
  219. {
  220. CYASSL_ENTER("CyaSSL_get_using_nonblock");
  221. CYASSL_LEAVE("CyaSSL_get_using_nonblock", ssl->options.usingNonblock);
  222. return ssl->options.usingNonblock;
  223. }
  224. int CyaSSL_dtls(CYASSL* ssl)
  225. {
  226. return ssl->options.dtls;
  227. }
  228. #ifndef CYASSL_LEANPSK
  229. void CyaSSL_set_using_nonblock(CYASSL* ssl, int nonblock)
  230. {
  231. CYASSL_ENTER("CyaSSL_set_using_nonblock");
  232. ssl->options.usingNonblock = (nonblock != 0);
  233. }
  234. int CyaSSL_dtls_set_peer(CYASSL* ssl, void* peer, unsigned int peerSz)
  235. {
  236. #ifdef CYASSL_DTLS
  237. void* sa = (void*)XMALLOC(peerSz, ssl->heap, DYNAMIC_TYPE_SOCKADDR);
  238. if (sa != NULL) {
  239. if (ssl->buffers.dtlsCtx.peer.sa != NULL)
  240. XFREE(ssl->buffers.dtlsCtx.peer.sa,ssl->heap,DYNAMIC_TYPE_SOCKADDR);
  241. XMEMCPY(sa, peer, peerSz);
  242. ssl->buffers.dtlsCtx.peer.sa = sa;
  243. ssl->buffers.dtlsCtx.peer.sz = peerSz;
  244. return SSL_SUCCESS;
  245. }
  246. return SSL_FAILURE;
  247. #else
  248. (void)ssl;
  249. (void)peer;
  250. (void)peerSz;
  251. return SSL_NOT_IMPLEMENTED;
  252. #endif
  253. }
  254. int CyaSSL_dtls_get_peer(CYASSL* ssl, void* peer, unsigned int* peerSz)
  255. {
  256. #ifdef CYASSL_DTLS
  257. if (peer != NULL && peerSz != NULL
  258. && *peerSz >= ssl->buffers.dtlsCtx.peer.sz) {
  259. *peerSz = ssl->buffers.dtlsCtx.peer.sz;
  260. XMEMCPY(peer, ssl->buffers.dtlsCtx.peer.sa, *peerSz);
  261. return SSL_SUCCESS;
  262. }
  263. return SSL_FAILURE;
  264. #else
  265. (void)ssl;
  266. (void)peer;
  267. (void)peerSz;
  268. return SSL_NOT_IMPLEMENTED;
  269. #endif
  270. }
  271. #endif /* CYASSL_LEANPSK */
  272. /* return underlyig connect or accept, SSL_SUCCESS on ok */
  273. int CyaSSL_negotiate(CYASSL* ssl)
  274. {
  275. int err = SSL_FATAL_ERROR;
  276. CYASSL_ENTER("CyaSSL_negotiate");
  277. #ifndef NO_CYASSL_SERVER
  278. if (ssl->options.side == CYASSL_SERVER_END)
  279. err = CyaSSL_accept(ssl);
  280. #endif
  281. #ifndef NO_CYASSL_CLIENT
  282. if (ssl->options.side == CYASSL_CLIENT_END)
  283. err = CyaSSL_connect(ssl);
  284. #endif
  285. CYASSL_LEAVE("CyaSSL_negotiate", err);
  286. return err;
  287. }
  288. #ifndef CYASSL_LEANPSK
  289. /* object size based on build */
  290. int CyaSSL_GetObjectSize(void)
  291. {
  292. #ifdef SHOW_SIZES
  293. printf("sizeof suites = %lu\n", sizeof(Suites));
  294. printf("sizeof ciphers(2) = %lu\n", sizeof(Ciphers));
  295. #ifndef NO_RC4
  296. printf(" sizeof arc4 = %lu\n", sizeof(Arc4));
  297. #endif
  298. printf(" sizeof aes = %lu\n", sizeof(Aes));
  299. #ifndef NO_DES3
  300. printf(" sizeof des3 = %lu\n", sizeof(Des3));
  301. #endif
  302. #ifndef NO_RABBIT
  303. printf(" sizeof rabbit = %lu\n", sizeof(Rabbit));
  304. #endif
  305. #ifdef HAVE_CHACHA
  306. printf(" sizeof chacha = %lu\n", sizeof(Chacha));
  307. #endif
  308. printf("sizeof cipher specs = %lu\n", sizeof(CipherSpecs));
  309. printf("sizeof keys = %lu\n", sizeof(Keys));
  310. printf("sizeof Hashes(2) = %lu\n", sizeof(Hashes));
  311. #ifndef NO_MD5
  312. printf(" sizeof MD5 = %lu\n", sizeof(Md5));
  313. #endif
  314. #ifndef NO_SHA
  315. printf(" sizeof SHA = %lu\n", sizeof(Sha));
  316. #endif
  317. #ifndef NO_SHA256
  318. printf(" sizeof SHA256 = %lu\n", sizeof(Sha256));
  319. #endif
  320. #ifdef CYASSL_SHA384
  321. printf(" sizeof SHA384 = %lu\n", sizeof(Sha384));
  322. #endif
  323. #ifdef CYASSL_SHA384
  324. printf(" sizeof SHA512 = %lu\n", sizeof(Sha512));
  325. #endif
  326. printf("sizeof Buffers = %lu\n", sizeof(Buffers));
  327. printf("sizeof Options = %lu\n", sizeof(Options));
  328. printf("sizeof Arrays = %lu\n", sizeof(Arrays));
  329. #ifndef NO_RSA
  330. printf("sizeof RsaKey = %lu\n", sizeof(RsaKey));
  331. #endif
  332. #ifdef HAVE_ECC
  333. printf("sizeof ecc_key = %lu\n", sizeof(ecc_key));
  334. #endif
  335. printf("sizeof CYASSL_CIPHER = %lu\n", sizeof(CYASSL_CIPHER));
  336. printf("sizeof CYASSL_SESSION = %lu\n", sizeof(CYASSL_SESSION));
  337. printf("sizeof CYASSL = %lu\n", sizeof(CYASSL));
  338. printf("sizeof CYASSL_CTX = %lu\n", sizeof(CYASSL_CTX));
  339. #endif
  340. return sizeof(CYASSL);
  341. }
  342. #endif
  343. #ifndef NO_DH
  344. /* server Diffie-Hellman parameters, SSL_SUCCESS on ok */
  345. int CyaSSL_SetTmpDH(CYASSL* ssl, const unsigned char* p, int pSz,
  346. const unsigned char* g, int gSz)
  347. {
  348. byte havePSK = 0;
  349. byte haveRSA = 1;
  350. CYASSL_ENTER("CyaSSL_SetTmpDH");
  351. if (ssl == NULL || p == NULL || g == NULL) return BAD_FUNC_ARG;
  352. if (ssl->options.side != CYASSL_SERVER_END)
  353. return SIDE_ERROR;
  354. if (ssl->buffers.serverDH_P.buffer && ssl->buffers.weOwnDH)
  355. XFREE(ssl->buffers.serverDH_P.buffer, ssl->ctx->heap, DYNAMIC_TYPE_DH);
  356. if (ssl->buffers.serverDH_G.buffer && ssl->buffers.weOwnDH)
  357. XFREE(ssl->buffers.serverDH_G.buffer, ssl->ctx->heap, DYNAMIC_TYPE_DH);
  358. ssl->buffers.weOwnDH = 1; /* SSL owns now */
  359. ssl->buffers.serverDH_P.buffer = (byte*)XMALLOC(pSz, ssl->ctx->heap,
  360. DYNAMIC_TYPE_DH);
  361. if (ssl->buffers.serverDH_P.buffer == NULL)
  362. return MEMORY_E;
  363. ssl->buffers.serverDH_G.buffer = (byte*)XMALLOC(gSz, ssl->ctx->heap,
  364. DYNAMIC_TYPE_DH);
  365. if (ssl->buffers.serverDH_G.buffer == NULL) {
  366. XFREE(ssl->buffers.serverDH_P.buffer, ssl->ctx->heap, DYNAMIC_TYPE_DH);
  367. return MEMORY_E;
  368. }
  369. ssl->buffers.serverDH_P.length = pSz;
  370. ssl->buffers.serverDH_G.length = gSz;
  371. XMEMCPY(ssl->buffers.serverDH_P.buffer, p, pSz);
  372. XMEMCPY(ssl->buffers.serverDH_G.buffer, g, gSz);
  373. ssl->options.haveDH = 1;
  374. #ifndef NO_PSK
  375. havePSK = ssl->options.havePSK;
  376. #endif
  377. #ifdef NO_RSA
  378. haveRSA = 0;
  379. #endif
  380. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, ssl->options.haveDH,
  381. ssl->options.haveNTRU, ssl->options.haveECDSAsig,
  382. ssl->options.haveStaticECC, ssl->options.side);
  383. CYASSL_LEAVE("CyaSSL_SetTmpDH", 0);
  384. return SSL_SUCCESS;
  385. }
  386. #endif /* !NO_DH */
  387. int CyaSSL_write(CYASSL* ssl, const void* data, int sz)
  388. {
  389. int ret;
  390. CYASSL_ENTER("SSL_write()");
  391. if (ssl == NULL || data == NULL || sz < 0)
  392. return BAD_FUNC_ARG;
  393. #ifdef HAVE_ERRNO_H
  394. errno = 0;
  395. #endif
  396. ret = SendData(ssl, data, sz);
  397. CYASSL_LEAVE("SSL_write()", ret);
  398. if (ret < 0)
  399. return SSL_FATAL_ERROR;
  400. else
  401. return ret;
  402. }
  403. static int CyaSSL_read_internal(CYASSL* ssl, void* data, int sz, int peek)
  404. {
  405. int ret;
  406. CYASSL_ENTER("CyaSSL_read_internal()");
  407. if (ssl == NULL || data == NULL || sz < 0)
  408. return BAD_FUNC_ARG;
  409. #ifdef HAVE_ERRNO_H
  410. errno = 0;
  411. #endif
  412. #ifdef CYASSL_DTLS
  413. if (ssl->options.dtls)
  414. ssl->dtls_expected_rx = max(sz + 100, MAX_MTU);
  415. #endif
  416. #ifdef HAVE_MAX_FRAGMENT
  417. ret = ReceiveData(ssl, (byte*)data,
  418. min(sz, min(ssl->max_fragment, OUTPUT_RECORD_SIZE)), peek);
  419. #else
  420. ret = ReceiveData(ssl, (byte*)data, min(sz, OUTPUT_RECORD_SIZE), peek);
  421. #endif
  422. CYASSL_LEAVE("CyaSSL_read_internal()", ret);
  423. if (ret < 0)
  424. return SSL_FATAL_ERROR;
  425. else
  426. return ret;
  427. }
  428. int CyaSSL_peek(CYASSL* ssl, void* data, int sz)
  429. {
  430. CYASSL_ENTER("CyaSSL_peek()");
  431. return CyaSSL_read_internal(ssl, data, sz, TRUE);
  432. }
  433. int CyaSSL_read(CYASSL* ssl, void* data, int sz)
  434. {
  435. CYASSL_ENTER("CyaSSL_read()");
  436. return CyaSSL_read_internal(ssl, data, sz, FALSE);
  437. }
  438. #ifdef HAVE_CAVIUM
  439. /* let's use cavium, SSL_SUCCESS on ok */
  440. int CyaSSL_UseCavium(CYASSL* ssl, int devId)
  441. {
  442. if (ssl == NULL)
  443. return BAD_FUNC_ARG;
  444. ssl->devId = devId;
  445. return SSL_SUCCESS;
  446. }
  447. /* let's use cavium, SSL_SUCCESS on ok */
  448. int CyaSSL_CTX_UseCavium(CYASSL_CTX* ctx, int devId)
  449. {
  450. if (ctx == NULL)
  451. return BAD_FUNC_ARG;
  452. ctx->devId = devId;
  453. return SSL_SUCCESS;
  454. }
  455. #endif /* HAVE_CAVIUM */
  456. #ifdef HAVE_SNI
  457. int CyaSSL_UseSNI(CYASSL* ssl, byte type, const void* data, word16 size)
  458. {
  459. if (ssl == NULL)
  460. return BAD_FUNC_ARG;
  461. return TLSX_UseSNI(&ssl->extensions, type, data, size);
  462. }
  463. int CyaSSL_CTX_UseSNI(CYASSL_CTX* ctx, byte type, const void* data, word16 size)
  464. {
  465. if (ctx == NULL)
  466. return BAD_FUNC_ARG;
  467. return TLSX_UseSNI(&ctx->extensions, type, data, size);
  468. }
  469. #ifndef NO_CYASSL_SERVER
  470. void CyaSSL_SNI_SetOptions(CYASSL* ssl, byte type, byte options)
  471. {
  472. if (ssl && ssl->extensions)
  473. TLSX_SNI_SetOptions(ssl->extensions, type, options);
  474. }
  475. void CyaSSL_CTX_SNI_SetOptions(CYASSL_CTX* ctx, byte type, byte options)
  476. {
  477. if (ctx && ctx->extensions)
  478. TLSX_SNI_SetOptions(ctx->extensions, type, options);
  479. }
  480. byte CyaSSL_SNI_Status(CYASSL* ssl, byte type)
  481. {
  482. return TLSX_SNI_Status(ssl ? ssl->extensions : NULL, type);
  483. }
  484. word16 CyaSSL_SNI_GetRequest(CYASSL* ssl, byte type, void** data)
  485. {
  486. if (data)
  487. *data = NULL;
  488. if (ssl && ssl->extensions)
  489. return TLSX_SNI_GetRequest(ssl->extensions, type, data);
  490. return 0;
  491. }
  492. int CyaSSL_SNI_GetFromBuffer(const byte* clientHello, word32 helloSz, byte type,
  493. byte* sni, word32* inOutSz)
  494. {
  495. if (clientHello && helloSz > 0 && sni && inOutSz && *inOutSz > 0)
  496. return TLSX_SNI_GetFromBuffer(clientHello, helloSz, type, sni, inOutSz);
  497. return BAD_FUNC_ARG;
  498. }
  499. #endif /* NO_CYASSL_SERVER */
  500. #endif /* HAVE_SNI */
  501. #ifdef HAVE_MAX_FRAGMENT
  502. #ifndef NO_CYASSL_CLIENT
  503. int CyaSSL_UseMaxFragment(CYASSL* ssl, byte mfl)
  504. {
  505. if (ssl == NULL)
  506. return BAD_FUNC_ARG;
  507. return TLSX_UseMaxFragment(&ssl->extensions, mfl);
  508. }
  509. int CyaSSL_CTX_UseMaxFragment(CYASSL_CTX* ctx, byte mfl)
  510. {
  511. if (ctx == NULL)
  512. return BAD_FUNC_ARG;
  513. return TLSX_UseMaxFragment(&ctx->extensions, mfl);
  514. }
  515. #endif /* NO_CYASSL_CLIENT */
  516. #endif /* HAVE_MAX_FRAGMENT */
  517. #ifdef HAVE_TRUNCATED_HMAC
  518. #ifndef NO_CYASSL_CLIENT
  519. int CyaSSL_UseTruncatedHMAC(CYASSL* ssl)
  520. {
  521. if (ssl == NULL)
  522. return BAD_FUNC_ARG;
  523. return TLSX_UseTruncatedHMAC(&ssl->extensions);
  524. }
  525. int CyaSSL_CTX_UseTruncatedHMAC(CYASSL_CTX* ctx)
  526. {
  527. if (ctx == NULL)
  528. return BAD_FUNC_ARG;
  529. return TLSX_UseTruncatedHMAC(&ctx->extensions);
  530. }
  531. #endif /* NO_CYASSL_CLIENT */
  532. #endif /* HAVE_TRUNCATED_HMAC */
  533. /* Elliptic Curves */
  534. #ifdef HAVE_SUPPORTED_CURVES
  535. #ifndef NO_CYASSL_CLIENT
  536. int CyaSSL_UseSupportedCurve(CYASSL* ssl, word16 name)
  537. {
  538. if (ssl == NULL)
  539. return BAD_FUNC_ARG;
  540. switch (name) {
  541. case CYASSL_ECC_SECP160R1:
  542. case CYASSL_ECC_SECP192R1:
  543. case CYASSL_ECC_SECP224R1:
  544. case CYASSL_ECC_SECP256R1:
  545. case CYASSL_ECC_SECP384R1:
  546. case CYASSL_ECC_SECP521R1:
  547. break;
  548. default:
  549. return BAD_FUNC_ARG;
  550. }
  551. return TLSX_UseSupportedCurve(&ssl->extensions, name);
  552. }
  553. int CyaSSL_CTX_UseSupportedCurve(CYASSL_CTX* ctx, word16 name)
  554. {
  555. if (ctx == NULL)
  556. return BAD_FUNC_ARG;
  557. switch (name) {
  558. case CYASSL_ECC_SECP160R1:
  559. case CYASSL_ECC_SECP192R1:
  560. case CYASSL_ECC_SECP224R1:
  561. case CYASSL_ECC_SECP256R1:
  562. case CYASSL_ECC_SECP384R1:
  563. case CYASSL_ECC_SECP521R1:
  564. break;
  565. default:
  566. return BAD_FUNC_ARG;
  567. }
  568. return TLSX_UseSupportedCurve(&ctx->extensions, name);
  569. }
  570. #endif /* NO_CYASSL_CLIENT */
  571. #endif /* HAVE_SUPPORTED_CURVES */
  572. /* Secure Renegotiation */
  573. #ifdef HAVE_SECURE_RENEGOTIATION
  574. /* user is forcing ability to use secure renegotiation, we discourage it */
  575. int CyaSSL_UseSecureRenegotiation(CYASSL* ssl)
  576. {
  577. int ret = BAD_FUNC_ARG;
  578. if (ssl)
  579. ret = TLSX_UseSecureRenegotiation(&ssl->extensions);
  580. if (ret == SSL_SUCCESS) {
  581. TLSX* extension = TLSX_Find(ssl->extensions, SECURE_RENEGOTIATION);
  582. if (extension)
  583. ssl->secure_renegotiation = (SecureRenegotiation*)extension->data;
  584. }
  585. return ret;
  586. }
  587. /* do a secure renegotiation handshake, user forced, we discourage */
  588. int CyaSSL_Rehandshake(CYASSL* ssl)
  589. {
  590. int ret;
  591. if (ssl == NULL)
  592. return BAD_FUNC_ARG;
  593. if (ssl->secure_renegotiation == NULL) {
  594. CYASSL_MSG("Secure Renegotiation not forced on by user");
  595. return SECURE_RENEGOTIATION_E;
  596. }
  597. if (ssl->secure_renegotiation->enabled == 0) {
  598. CYASSL_MSG("Secure Renegotiation not enabled at extension level");
  599. return SECURE_RENEGOTIATION_E;
  600. }
  601. if (ssl->options.handShakeState != HANDSHAKE_DONE) {
  602. CYASSL_MSG("Can't renegotiate until previous handshake complete");
  603. return SECURE_RENEGOTIATION_E;
  604. }
  605. #ifndef NO_FORCE_SCR_SAME_SUITE
  606. /* force same suite */
  607. if (ssl->suites) {
  608. ssl->suites->suiteSz = SUITE_LEN;
  609. ssl->suites->suites[0] = ssl->options.cipherSuite0;
  610. ssl->suites->suites[1] = ssl->options.cipherSuite;
  611. }
  612. #endif
  613. /* reset handshake states */
  614. ssl->options.serverState = NULL_STATE;
  615. ssl->options.clientState = NULL_STATE;
  616. ssl->options.connectState = CONNECT_BEGIN;
  617. ssl->options.acceptState = ACCEPT_BEGIN;
  618. ssl->options.handShakeState = NULL_STATE;
  619. ssl->options.processReply = 0; /* TODO, move states in internal.h */
  620. XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived));
  621. ssl->secure_renegotiation->cache_status = SCR_CACHE_NEEDED;
  622. #ifndef NO_OLD_TLS
  623. #ifndef NO_MD5
  624. InitMd5(&ssl->hashMd5);
  625. #endif
  626. #ifndef NO_SHA
  627. ret = InitSha(&ssl->hashSha);
  628. if (ret !=0)
  629. return ret;
  630. #endif
  631. #endif /* NO_OLD_TLS */
  632. #ifndef NO_SHA256
  633. ret = InitSha256(&ssl->hashSha256);
  634. if (ret !=0)
  635. return ret;
  636. #endif
  637. #ifdef CYASSL_SHA384
  638. ret = InitSha384(&ssl->hashSha384);
  639. if (ret !=0)
  640. return ret;
  641. #endif
  642. ret = CyaSSL_negotiate(ssl);
  643. return ret;
  644. }
  645. #endif /* HAVE_SECURE_RENEGOTIATION */
  646. /* Session Ticket */
  647. #if !defined(NO_CYASSL_CLIENT) && defined(HAVE_SESSION_TICKET)
  648. int CyaSSL_UseSessionTicket(CYASSL* ssl)
  649. {
  650. if (ssl == NULL)
  651. return BAD_FUNC_ARG;
  652. return TLSX_UseSessionTicket(&ssl->extensions, NULL);
  653. }
  654. int CyaSSL_CTX_UseSessionTicket(CYASSL_CTX* ctx)
  655. {
  656. if (ctx == NULL)
  657. return BAD_FUNC_ARG;
  658. return TLSX_UseSessionTicket(&ctx->extensions, NULL);
  659. }
  660. CYASSL_API int CyaSSL_get_SessionTicket(CYASSL* ssl, byte* buf, word32* bufSz)
  661. {
  662. if (ssl == NULL || buf == NULL || bufSz == NULL || *bufSz == 0)
  663. return BAD_FUNC_ARG;
  664. if (ssl->session.ticketLen <= *bufSz) {
  665. XMEMCPY(buf, ssl->session.ticket, ssl->session.ticketLen);
  666. *bufSz = ssl->session.ticketLen;
  667. }
  668. else
  669. *bufSz = 0;
  670. return SSL_SUCCESS;
  671. }
  672. CYASSL_API int CyaSSL_set_SessionTicket(CYASSL* ssl, byte* buf, word32 bufSz)
  673. {
  674. if (ssl == NULL || buf == NULL || bufSz == 0)
  675. return BAD_FUNC_ARG;
  676. XMEMCPY(ssl->session.ticket, buf, bufSz);
  677. ssl->session.ticketLen = bufSz;
  678. return SSL_SUCCESS;
  679. }
  680. CYASSL_API int CyaSSL_set_SessionTicket_cb(CYASSL* ssl,
  681. CallbackSessionTicket cb, void* ctx)
  682. {
  683. if (ssl == NULL)
  684. return BAD_FUNC_ARG;
  685. ssl->session_ticket_cb = cb;
  686. ssl->session_ticket_ctx = ctx;
  687. return SSL_SUCCESS;
  688. }
  689. #endif
  690. #ifndef CYASSL_LEANPSK
  691. int CyaSSL_send(CYASSL* ssl, const void* data, int sz, int flags)
  692. {
  693. int ret;
  694. int oldFlags;
  695. CYASSL_ENTER("CyaSSL_send()");
  696. if (ssl == NULL || data == NULL || sz < 0)
  697. return BAD_FUNC_ARG;
  698. oldFlags = ssl->wflags;
  699. ssl->wflags = flags;
  700. ret = CyaSSL_write(ssl, data, sz);
  701. ssl->wflags = oldFlags;
  702. CYASSL_LEAVE("CyaSSL_send()", ret);
  703. return ret;
  704. }
  705. int CyaSSL_recv(CYASSL* ssl, void* data, int sz, int flags)
  706. {
  707. int ret;
  708. int oldFlags;
  709. CYASSL_ENTER("CyaSSL_recv()");
  710. if (ssl == NULL || data == NULL || sz < 0)
  711. return BAD_FUNC_ARG;
  712. oldFlags = ssl->rflags;
  713. ssl->rflags = flags;
  714. ret = CyaSSL_read(ssl, data, sz);
  715. ssl->rflags = oldFlags;
  716. CYASSL_LEAVE("CyaSSL_recv()", ret);
  717. return ret;
  718. }
  719. #endif
  720. /* SSL_SUCCESS on ok */
  721. int CyaSSL_shutdown(CYASSL* ssl)
  722. {
  723. CYASSL_ENTER("SSL_shutdown()");
  724. if (ssl == NULL)
  725. return SSL_FATAL_ERROR;
  726. if (ssl->options.quietShutdown) {
  727. CYASSL_MSG("quiet shutdown, no close notify sent");
  728. return SSL_SUCCESS;
  729. }
  730. /* try to send close notify, not an error if can't */
  731. if (!ssl->options.isClosed && !ssl->options.connReset &&
  732. !ssl->options.sentNotify) {
  733. ssl->error = SendAlert(ssl, alert_warning, close_notify);
  734. if (ssl->error < 0) {
  735. CYASSL_ERROR(ssl->error);
  736. return SSL_FATAL_ERROR;
  737. }
  738. ssl->options.sentNotify = 1; /* don't send close_notify twice */
  739. }
  740. CYASSL_LEAVE("SSL_shutdown()", ssl->error);
  741. ssl->error = SSL_ERROR_SYSCALL; /* simulate OpenSSL behavior */
  742. return SSL_SUCCESS;
  743. }
  744. int CyaSSL_get_error(CYASSL* ssl, int ret)
  745. {
  746. CYASSL_ENTER("SSL_get_error");
  747. if (ret > 0)
  748. return SSL_ERROR_NONE;
  749. if (ssl == NULL)
  750. return BAD_FUNC_ARG;
  751. CYASSL_LEAVE("SSL_get_error", ssl->error);
  752. /* make sure converted types are handled in SetErrorString() too */
  753. if (ssl->error == WANT_READ)
  754. return SSL_ERROR_WANT_READ; /* convert to OpenSSL type */
  755. else if (ssl->error == WANT_WRITE)
  756. return SSL_ERROR_WANT_WRITE; /* convert to OpenSSL type */
  757. else if (ssl->error == ZERO_RETURN)
  758. return SSL_ERROR_ZERO_RETURN; /* convert to OpenSSL type */
  759. return ssl->error;
  760. }
  761. /* retrive alert history, SSL_SUCCESS on ok */
  762. int CyaSSL_get_alert_history(CYASSL* ssl, CYASSL_ALERT_HISTORY *h)
  763. {
  764. if (ssl && h) {
  765. *h = ssl->alert_history;
  766. }
  767. return SSL_SUCCESS;
  768. }
  769. /* return TRUE if current error is want read */
  770. int CyaSSL_want_read(CYASSL* ssl)
  771. {
  772. CYASSL_ENTER("SSL_want_read");
  773. if (ssl->error == WANT_READ)
  774. return 1;
  775. return 0;
  776. }
  777. /* return TRUE if current error is want write */
  778. int CyaSSL_want_write(CYASSL* ssl)
  779. {
  780. CYASSL_ENTER("SSL_want_write");
  781. if (ssl->error == WANT_WRITE)
  782. return 1;
  783. return 0;
  784. }
  785. char* CyaSSL_ERR_error_string(unsigned long errNumber, char* data)
  786. {
  787. static const char* msg = "Please supply a buffer for error string";
  788. CYASSL_ENTER("ERR_error_string");
  789. if (data) {
  790. SetErrorString((int)errNumber, data);
  791. return data;
  792. }
  793. return (char*)msg;
  794. }
  795. void CyaSSL_ERR_error_string_n(unsigned long e, char* buf, unsigned long len)
  796. {
  797. CYASSL_ENTER("CyaSSL_ERR_error_string_n");
  798. if (len >= CYASSL_MAX_ERROR_SZ)
  799. CyaSSL_ERR_error_string(e, buf);
  800. else {
  801. char tmp[CYASSL_MAX_ERROR_SZ];
  802. CYASSL_MSG("Error buffer too short, truncating");
  803. if (len) {
  804. CyaSSL_ERR_error_string(e, tmp);
  805. XMEMCPY(buf, tmp, len-1);
  806. buf[len-1] = '\0';
  807. }
  808. }
  809. }
  810. /* don't free temporary arrays at end of handshake */
  811. void CyaSSL_KeepArrays(CYASSL* ssl)
  812. {
  813. if (ssl)
  814. ssl->options.saveArrays = 1;
  815. }
  816. /* user doesn't need temporary arrays anymore, Free */
  817. void CyaSSL_FreeArrays(CYASSL* ssl)
  818. {
  819. if (ssl && ssl->options.handShakeState == HANDSHAKE_DONE) {
  820. ssl->options.saveArrays = 0;
  821. FreeArrays(ssl, 1);
  822. }
  823. }
  824. const byte* CyaSSL_GetMacSecret(CYASSL* ssl, int verify)
  825. {
  826. if (ssl == NULL)
  827. return NULL;
  828. if ( (ssl->options.side == CYASSL_CLIENT_END && !verify) ||
  829. (ssl->options.side == CYASSL_SERVER_END && verify) )
  830. return ssl->keys.client_write_MAC_secret;
  831. else
  832. return ssl->keys.server_write_MAC_secret;
  833. }
  834. #ifdef ATOMIC_USER
  835. void CyaSSL_CTX_SetMacEncryptCb(CYASSL_CTX* ctx, CallbackMacEncrypt cb)
  836. {
  837. if (ctx)
  838. ctx->MacEncryptCb = cb;
  839. }
  840. void CyaSSL_SetMacEncryptCtx(CYASSL* ssl, void *ctx)
  841. {
  842. if (ssl)
  843. ssl->MacEncryptCtx = ctx;
  844. }
  845. void* CyaSSL_GetMacEncryptCtx(CYASSL* ssl)
  846. {
  847. if (ssl)
  848. return ssl->MacEncryptCtx;
  849. return NULL;
  850. }
  851. void CyaSSL_CTX_SetDecryptVerifyCb(CYASSL_CTX* ctx, CallbackDecryptVerify cb)
  852. {
  853. if (ctx)
  854. ctx->DecryptVerifyCb = cb;
  855. }
  856. void CyaSSL_SetDecryptVerifyCtx(CYASSL* ssl, void *ctx)
  857. {
  858. if (ssl)
  859. ssl->DecryptVerifyCtx = ctx;
  860. }
  861. void* CyaSSL_GetDecryptVerifyCtx(CYASSL* ssl)
  862. {
  863. if (ssl)
  864. return ssl->DecryptVerifyCtx;
  865. return NULL;
  866. }
  867. const byte* CyaSSL_GetClientWriteKey(CYASSL* ssl)
  868. {
  869. if (ssl)
  870. return ssl->keys.client_write_key;
  871. return NULL;
  872. }
  873. const byte* CyaSSL_GetClientWriteIV(CYASSL* ssl)
  874. {
  875. if (ssl)
  876. return ssl->keys.client_write_IV;
  877. return NULL;
  878. }
  879. const byte* CyaSSL_GetServerWriteKey(CYASSL* ssl)
  880. {
  881. if (ssl)
  882. return ssl->keys.server_write_key;
  883. return NULL;
  884. }
  885. const byte* CyaSSL_GetServerWriteIV(CYASSL* ssl)
  886. {
  887. if (ssl)
  888. return ssl->keys.server_write_IV;
  889. return NULL;
  890. }
  891. int CyaSSL_GetKeySize(CYASSL* ssl)
  892. {
  893. if (ssl)
  894. return ssl->specs.key_size;
  895. return BAD_FUNC_ARG;
  896. }
  897. int CyaSSL_GetIVSize(CYASSL* ssl)
  898. {
  899. if (ssl)
  900. return ssl->specs.iv_size;
  901. return BAD_FUNC_ARG;
  902. }
  903. int CyaSSL_GetBulkCipher(CYASSL* ssl)
  904. {
  905. if (ssl)
  906. return ssl->specs.bulk_cipher_algorithm;
  907. return BAD_FUNC_ARG;
  908. }
  909. int CyaSSL_GetCipherType(CYASSL* ssl)
  910. {
  911. if (ssl == NULL)
  912. return BAD_FUNC_ARG;
  913. if (ssl->specs.cipher_type == block)
  914. return CYASSL_BLOCK_TYPE;
  915. if (ssl->specs.cipher_type == stream)
  916. return CYASSL_STREAM_TYPE;
  917. if (ssl->specs.cipher_type == aead)
  918. return CYASSL_AEAD_TYPE;
  919. return -1;
  920. }
  921. int CyaSSL_GetCipherBlockSize(CYASSL* ssl)
  922. {
  923. if (ssl == NULL)
  924. return BAD_FUNC_ARG;
  925. return ssl->specs.block_size;
  926. }
  927. int CyaSSL_GetAeadMacSize(CYASSL* ssl)
  928. {
  929. if (ssl == NULL)
  930. return BAD_FUNC_ARG;
  931. return ssl->specs.aead_mac_size;
  932. }
  933. int CyaSSL_IsTLSv1_1(CYASSL* ssl)
  934. {
  935. if (ssl == NULL)
  936. return BAD_FUNC_ARG;
  937. if (ssl->options.tls1_1)
  938. return 1;
  939. return 0;
  940. }
  941. int CyaSSL_GetSide(CYASSL* ssl)
  942. {
  943. if (ssl)
  944. return ssl->options.side;
  945. return BAD_FUNC_ARG;
  946. }
  947. int CyaSSL_GetHmacSize(CYASSL* ssl)
  948. {
  949. /* AEAD ciphers don't have HMAC keys */
  950. if (ssl)
  951. return (ssl->specs.cipher_type != aead) ? ssl->specs.hash_size : 0;
  952. return BAD_FUNC_ARG;
  953. }
  954. #endif /* ATOMIC_USER */
  955. #ifndef NO_CERTS
  956. CYASSL_CERT_MANAGER* CyaSSL_CertManagerNew(void)
  957. {
  958. CYASSL_CERT_MANAGER* cm = NULL;
  959. CYASSL_ENTER("CyaSSL_CertManagerNew");
  960. cm = (CYASSL_CERT_MANAGER*) XMALLOC(sizeof(CYASSL_CERT_MANAGER), 0,
  961. DYNAMIC_TYPE_CERT_MANAGER);
  962. if (cm) {
  963. XMEMSET(cm, 0, sizeof(CYASSL_CERT_MANAGER));
  964. if (InitMutex(&cm->caLock) != 0) {
  965. CYASSL_MSG("Bad mutex init");
  966. CyaSSL_CertManagerFree(cm);
  967. return NULL;
  968. }
  969. }
  970. return cm;
  971. }
  972. void CyaSSL_CertManagerFree(CYASSL_CERT_MANAGER* cm)
  973. {
  974. CYASSL_ENTER("CyaSSL_CertManagerFree");
  975. if (cm) {
  976. #ifdef HAVE_CRL
  977. if (cm->crl)
  978. FreeCRL(cm->crl, 1);
  979. #endif
  980. #ifdef HAVE_OCSP
  981. if (cm->ocsp)
  982. FreeOCSP(cm->ocsp, 1);
  983. #endif
  984. FreeSignerTable(cm->caTable, CA_TABLE_SIZE, NULL);
  985. FreeMutex(&cm->caLock);
  986. XFREE(cm, NULL, DYNAMIC_TYPE_CERT_MANAGER);
  987. }
  988. }
  989. /* Unload the CA signer list */
  990. int CyaSSL_CertManagerUnloadCAs(CYASSL_CERT_MANAGER* cm)
  991. {
  992. CYASSL_ENTER("CyaSSL_CertManagerUnloadCAs");
  993. if (cm == NULL)
  994. return BAD_FUNC_ARG;
  995. if (LockMutex(&cm->caLock) != 0)
  996. return BAD_MUTEX_E;
  997. FreeSignerTable(cm->caTable, CA_TABLE_SIZE, NULL);
  998. UnLockMutex(&cm->caLock);
  999. return SSL_SUCCESS;
  1000. }
  1001. /* Return bytes written to buff or < 0 for error */
  1002. int CyaSSL_CertPemToDer(const unsigned char* pem, int pemSz,
  1003. unsigned char* buff, int buffSz,
  1004. int type)
  1005. {
  1006. int eccKey = 0;
  1007. int ret;
  1008. buffer der;
  1009. #ifdef CYASSL_SMALL_STACK
  1010. EncryptedInfo* info = NULL;
  1011. #else
  1012. EncryptedInfo info[1];
  1013. #endif
  1014. CYASSL_ENTER("CyaSSL_CertPemToDer");
  1015. if (pem == NULL || buff == NULL || buffSz <= 0) {
  1016. CYASSL_MSG("Bad pem der args");
  1017. return BAD_FUNC_ARG;
  1018. }
  1019. if (type != CERT_TYPE && type != CA_TYPE && type != CERTREQ_TYPE) {
  1020. CYASSL_MSG("Bad cert type");
  1021. return BAD_FUNC_ARG;
  1022. }
  1023. #ifdef CYASSL_SMALL_STACK
  1024. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  1025. DYNAMIC_TYPE_TMP_BUFFER);
  1026. if (info == NULL)
  1027. return MEMORY_E;
  1028. #endif
  1029. info->set = 0;
  1030. info->ctx = NULL;
  1031. info->consumed = 0;
  1032. der.buffer = NULL;
  1033. ret = PemToDer(pem, pemSz, type, &der, NULL, info, &eccKey);
  1034. #ifdef CYASSL_SMALL_STACK
  1035. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1036. #endif
  1037. if (ret < 0) {
  1038. CYASSL_MSG("Bad Pem To Der");
  1039. }
  1040. else {
  1041. if (der.length <= (word32)buffSz) {
  1042. XMEMCPY(buff, der.buffer, der.length);
  1043. ret = der.length;
  1044. }
  1045. else {
  1046. CYASSL_MSG("Bad der length");
  1047. ret = BAD_FUNC_ARG;
  1048. }
  1049. }
  1050. XFREE(der.buffer, NULL, DYNAMIC_TYPE_KEY);
  1051. return ret;
  1052. }
  1053. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1054. /* our KeyPemToDer password callback, password in userData */
  1055. static INLINE int OurPasswordCb(char* passwd, int sz, int rw, void* userdata)
  1056. {
  1057. (void)rw;
  1058. if (userdata == NULL)
  1059. return 0;
  1060. XSTRNCPY(passwd, (char*)userdata, sz);
  1061. return min((word32)sz, (word32)XSTRLEN((char*)userdata));
  1062. }
  1063. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  1064. /* Return bytes written to buff or < 0 for error */
  1065. int CyaSSL_KeyPemToDer(const unsigned char* pem, int pemSz, unsigned char* buff,
  1066. int buffSz, const char* pass)
  1067. {
  1068. int eccKey = 0;
  1069. int ret;
  1070. buffer der;
  1071. #ifdef CYASSL_SMALL_STACK
  1072. EncryptedInfo* info = NULL;
  1073. #else
  1074. EncryptedInfo info[1];
  1075. #endif
  1076. (void)pass;
  1077. CYASSL_ENTER("CyaSSL_KeyPemToDer");
  1078. if (pem == NULL || buff == NULL || buffSz <= 0) {
  1079. CYASSL_MSG("Bad pem der args");
  1080. return BAD_FUNC_ARG;
  1081. }
  1082. #ifdef CYASSL_SMALL_STACK
  1083. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  1084. DYNAMIC_TYPE_TMP_BUFFER);
  1085. if (info == NULL)
  1086. return MEMORY_E;
  1087. #endif
  1088. info->set = 0;
  1089. info->ctx = NULL;
  1090. info->consumed = 0;
  1091. der.buffer = NULL;
  1092. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1093. if (pass) {
  1094. info->ctx = CyaSSL_CTX_new(CyaSSLv23_client_method());
  1095. if (info->ctx == NULL) {
  1096. #ifdef CYASSL_SMALL_STACK
  1097. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1098. #endif
  1099. return MEMORY_E;
  1100. }
  1101. CyaSSL_CTX_set_default_passwd_cb(info->ctx, OurPasswordCb);
  1102. CyaSSL_CTX_set_default_passwd_cb_userdata(info->ctx, (void*)pass);
  1103. }
  1104. #endif
  1105. ret = PemToDer(pem, pemSz, PRIVATEKEY_TYPE, &der, NULL, info, &eccKey);
  1106. if (info->ctx)
  1107. CyaSSL_CTX_free(info->ctx);
  1108. #ifdef CYASSL_SMALL_STACK
  1109. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1110. #endif
  1111. if (ret < 0) {
  1112. CYASSL_MSG("Bad Pem To Der");
  1113. }
  1114. else {
  1115. if (der.length <= (word32)buffSz) {
  1116. XMEMCPY(buff, der.buffer, der.length);
  1117. ret = der.length;
  1118. }
  1119. else {
  1120. CYASSL_MSG("Bad der length");
  1121. ret = BAD_FUNC_ARG;
  1122. }
  1123. }
  1124. XFREE(der.buffer, NULL, DYNAMIC_TYPE_KEY);
  1125. return ret;
  1126. }
  1127. #endif /* !NO_CERTS */
  1128. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  1129. void CyaSSL_ERR_print_errors_fp(FILE* fp, int err)
  1130. {
  1131. char data[CYASSL_MAX_ERROR_SZ + 1];
  1132. CYASSL_ENTER("CyaSSL_ERR_print_errors_fp");
  1133. SetErrorString(err, data);
  1134. fprintf(fp, "%s", data);
  1135. }
  1136. #endif
  1137. int CyaSSL_pending(CYASSL* ssl)
  1138. {
  1139. CYASSL_ENTER("SSL_pending");
  1140. return ssl->buffers.clearOutputBuffer.length;
  1141. }
  1142. #ifndef CYASSL_LEANPSK
  1143. /* trun on handshake group messages for context */
  1144. int CyaSSL_CTX_set_group_messages(CYASSL_CTX* ctx)
  1145. {
  1146. if (ctx == NULL)
  1147. return BAD_FUNC_ARG;
  1148. ctx->groupMessages = 1;
  1149. return SSL_SUCCESS;
  1150. }
  1151. #endif
  1152. #ifndef NO_CYASSL_CLIENT
  1153. /* connect enough to get peer cert chain */
  1154. int CyaSSL_connect_cert(CYASSL* ssl)
  1155. {
  1156. int ret;
  1157. if (ssl == NULL)
  1158. return SSL_FAILURE;
  1159. ssl->options.certOnly = 1;
  1160. ret = CyaSSL_connect(ssl);
  1161. ssl->options.certOnly = 0;
  1162. return ret;
  1163. }
  1164. #endif
  1165. #ifndef CYASSL_LEANPSK
  1166. /* trun on handshake group messages for ssl object */
  1167. int CyaSSL_set_group_messages(CYASSL* ssl)
  1168. {
  1169. if (ssl == NULL)
  1170. return BAD_FUNC_ARG;
  1171. ssl->options.groupMessages = 1;
  1172. return SSL_SUCCESS;
  1173. }
  1174. /* Set minimum downgrade version allowed, SSL_SUCCESS on ok */
  1175. int CyaSSL_SetMinVersion(CYASSL* ssl, int version)
  1176. {
  1177. CYASSL_ENTER("CyaSSL_SetMinVersion");
  1178. if (ssl == NULL) {
  1179. CYASSL_MSG("Bad function argument");
  1180. return BAD_FUNC_ARG;
  1181. }
  1182. switch (version) {
  1183. #ifndef NO_OLD_TLS
  1184. case CYASSL_SSLV3:
  1185. ssl->options.minDowngrade = SSLv3_MINOR;
  1186. break;
  1187. #endif
  1188. #ifndef NO_TLS
  1189. #ifndef NO_OLD_TLS
  1190. case CYASSL_TLSV1:
  1191. ssl->options.minDowngrade = TLSv1_MINOR;
  1192. break;
  1193. case CYASSL_TLSV1_1:
  1194. ssl->options.minDowngrade = TLSv1_1_MINOR;
  1195. break;
  1196. #endif
  1197. case CYASSL_TLSV1_2:
  1198. ssl->options.minDowngrade = TLSv1_2_MINOR;
  1199. break;
  1200. #endif
  1201. default:
  1202. CYASSL_MSG("Bad function argument");
  1203. return BAD_FUNC_ARG;
  1204. }
  1205. return SSL_SUCCESS;
  1206. }
  1207. int CyaSSL_SetVersion(CYASSL* ssl, int version)
  1208. {
  1209. byte haveRSA = 1;
  1210. byte havePSK = 0;
  1211. CYASSL_ENTER("CyaSSL_SetVersion");
  1212. if (ssl == NULL) {
  1213. CYASSL_MSG("Bad function argument");
  1214. return BAD_FUNC_ARG;
  1215. }
  1216. switch (version) {
  1217. #ifndef NO_OLD_TLS
  1218. case CYASSL_SSLV3:
  1219. ssl->version = MakeSSLv3();
  1220. break;
  1221. #endif
  1222. #ifndef NO_TLS
  1223. #ifndef NO_OLD_TLS
  1224. case CYASSL_TLSV1:
  1225. ssl->version = MakeTLSv1();
  1226. break;
  1227. case CYASSL_TLSV1_1:
  1228. ssl->version = MakeTLSv1_1();
  1229. break;
  1230. #endif
  1231. case CYASSL_TLSV1_2:
  1232. ssl->version = MakeTLSv1_2();
  1233. break;
  1234. #endif
  1235. default:
  1236. CYASSL_MSG("Bad function argument");
  1237. return BAD_FUNC_ARG;
  1238. }
  1239. #ifdef NO_RSA
  1240. haveRSA = 0;
  1241. #endif
  1242. #ifndef NO_PSK
  1243. havePSK = ssl->options.havePSK;
  1244. #endif
  1245. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK, ssl->options.haveDH,
  1246. ssl->options.haveNTRU, ssl->options.haveECDSAsig,
  1247. ssl->options.haveStaticECC, ssl->options.side);
  1248. return SSL_SUCCESS;
  1249. }
  1250. #endif /* !leanpsk */
  1251. #if !defined(NO_CERTS) || !defined(NO_SESSION_CACHE)
  1252. /* Make a work from the front of random hash */
  1253. static INLINE word32 MakeWordFromHash(const byte* hashID)
  1254. {
  1255. return (hashID[0] << 24) | (hashID[1] << 16) | (hashID[2] << 8) |
  1256. hashID[3];
  1257. }
  1258. #endif /* !NO_CERTS || !NO_SESSION_CACHE */
  1259. #ifndef NO_CERTS
  1260. /* hash is the SHA digest of name, just use first 32 bits as hash */
  1261. static INLINE word32 HashSigner(const byte* hash)
  1262. {
  1263. return MakeWordFromHash(hash) % CA_TABLE_SIZE;
  1264. }
  1265. /* does CA already exist on signer list */
  1266. int AlreadySigner(CYASSL_CERT_MANAGER* cm, byte* hash)
  1267. {
  1268. Signer* signers;
  1269. int ret = 0;
  1270. word32 row = HashSigner(hash);
  1271. if (LockMutex(&cm->caLock) != 0)
  1272. return ret;
  1273. signers = cm->caTable[row];
  1274. while (signers) {
  1275. byte* subjectHash;
  1276. #ifndef NO_SKID
  1277. subjectHash = signers->subjectKeyIdHash;
  1278. #else
  1279. subjectHash = signers->subjectNameHash;
  1280. #endif
  1281. if (XMEMCMP(hash, subjectHash, SHA_DIGEST_SIZE) == 0) {
  1282. ret = 1;
  1283. break;
  1284. }
  1285. signers = signers->next;
  1286. }
  1287. UnLockMutex(&cm->caLock);
  1288. return ret;
  1289. }
  1290. /* return CA if found, otherwise NULL */
  1291. Signer* GetCA(void* vp, byte* hash)
  1292. {
  1293. CYASSL_CERT_MANAGER* cm = (CYASSL_CERT_MANAGER*)vp;
  1294. Signer* ret = NULL;
  1295. Signer* signers;
  1296. word32 row = HashSigner(hash);
  1297. if (cm == NULL)
  1298. return NULL;
  1299. if (LockMutex(&cm->caLock) != 0)
  1300. return ret;
  1301. signers = cm->caTable[row];
  1302. while (signers) {
  1303. byte* subjectHash;
  1304. #ifndef NO_SKID
  1305. subjectHash = signers->subjectKeyIdHash;
  1306. #else
  1307. subjectHash = signers->subjectNameHash;
  1308. #endif
  1309. if (XMEMCMP(hash, subjectHash, SHA_DIGEST_SIZE) == 0) {
  1310. ret = signers;
  1311. break;
  1312. }
  1313. signers = signers->next;
  1314. }
  1315. UnLockMutex(&cm->caLock);
  1316. return ret;
  1317. }
  1318. #ifndef NO_SKID
  1319. /* return CA if found, otherwise NULL. Walk through hash table. */
  1320. Signer* GetCAByName(void* vp, byte* hash)
  1321. {
  1322. CYASSL_CERT_MANAGER* cm = (CYASSL_CERT_MANAGER*)vp;
  1323. Signer* ret = NULL;
  1324. Signer* signers;
  1325. word32 row;
  1326. if (cm == NULL)
  1327. return NULL;
  1328. if (LockMutex(&cm->caLock) != 0)
  1329. return ret;
  1330. for (row = 0; row < CA_TABLE_SIZE && ret == NULL; row++) {
  1331. signers = cm->caTable[row];
  1332. while (signers && ret == NULL) {
  1333. if (XMEMCMP(hash, signers->subjectNameHash, SHA_DIGEST_SIZE) == 0) {
  1334. ret = signers;
  1335. }
  1336. signers = signers->next;
  1337. }
  1338. }
  1339. UnLockMutex(&cm->caLock);
  1340. return ret;
  1341. }
  1342. #endif
  1343. /* owns der, internal now uses too */
  1344. /* type flag ids from user or from chain received during verify
  1345. don't allow chain ones to be added w/o isCA extension */
  1346. int AddCA(CYASSL_CERT_MANAGER* cm, buffer der, int type, int verify)
  1347. {
  1348. int ret;
  1349. Signer* signer = 0;
  1350. word32 row;
  1351. byte* subjectHash;
  1352. #ifdef CYASSL_SMALL_STACK
  1353. DecodedCert* cert = NULL;
  1354. #else
  1355. DecodedCert cert[1];
  1356. #endif
  1357. CYASSL_MSG("Adding a CA");
  1358. #ifdef CYASSL_SMALL_STACK
  1359. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  1360. DYNAMIC_TYPE_TMP_BUFFER);
  1361. if (cert == NULL)
  1362. return MEMORY_E;
  1363. #endif
  1364. InitDecodedCert(cert, der.buffer, der.length, cm->heap);
  1365. ret = ParseCert(cert, CA_TYPE, verify, cm);
  1366. CYASSL_MSG(" Parsed new CA");
  1367. #ifndef NO_SKID
  1368. subjectHash = cert->extSubjKeyId;
  1369. #else
  1370. subjectHash = cert->subjectHash;
  1371. #endif
  1372. if (ret == 0 && cert->isCA == 0 && type != CYASSL_USER_CA) {
  1373. CYASSL_MSG(" Can't add as CA if not actually one");
  1374. ret = NOT_CA_ERROR;
  1375. }
  1376. #ifndef ALLOW_INVALID_CERTSIGN
  1377. else if (ret == 0 && cert->isCA == 1 && type != CYASSL_USER_CA &&
  1378. (cert->extKeyUsage & KEYUSE_KEY_CERT_SIGN) == 0) {
  1379. /* Intermediate CA certs are required to have the keyCertSign
  1380. * extension set. User loaded root certs are not. */
  1381. CYASSL_MSG(" Doesn't have key usage certificate signing");
  1382. ret = NOT_CA_ERROR;
  1383. }
  1384. #endif
  1385. else if (ret == 0 && AlreadySigner(cm, subjectHash)) {
  1386. CYASSL_MSG(" Already have this CA, not adding again");
  1387. (void)ret;
  1388. }
  1389. else if (ret == 0) {
  1390. /* take over signer parts */
  1391. signer = MakeSigner(cm->heap);
  1392. if (!signer)
  1393. ret = MEMORY_ERROR;
  1394. else {
  1395. signer->keyOID = cert->keyOID;
  1396. signer->publicKey = cert->publicKey;
  1397. signer->pubKeySize = cert->pubKeySize;
  1398. signer->nameLen = cert->subjectCNLen;
  1399. signer->name = cert->subjectCN;
  1400. #ifndef IGNORE_NAME_CONSTRAINTS
  1401. signer->permittedNames = cert->permittedNames;
  1402. signer->excludedNames = cert->excludedNames;
  1403. #endif
  1404. #ifndef NO_SKID
  1405. XMEMCPY(signer->subjectKeyIdHash, cert->extSubjKeyId,
  1406. SHA_DIGEST_SIZE);
  1407. #endif
  1408. XMEMCPY(signer->subjectNameHash, cert->subjectHash,
  1409. SHA_DIGEST_SIZE);
  1410. signer->keyUsage = cert->extKeyUsageSet ? cert->extKeyUsage
  1411. : 0xFFFF;
  1412. signer->next = NULL; /* If Key Usage not set, all uses valid. */
  1413. cert->publicKey = 0; /* in case lock fails don't free here. */
  1414. cert->subjectCN = 0;
  1415. #ifndef IGNORE_NAME_CONSTRAINTS
  1416. cert->permittedNames = NULL;
  1417. cert->excludedNames = NULL;
  1418. #endif
  1419. #ifndef NO_SKID
  1420. row = HashSigner(signer->subjectKeyIdHash);
  1421. #else
  1422. row = HashSigner(signer->subjectNameHash);
  1423. #endif
  1424. if (LockMutex(&cm->caLock) == 0) {
  1425. signer->next = cm->caTable[row];
  1426. cm->caTable[row] = signer; /* takes ownership */
  1427. UnLockMutex(&cm->caLock);
  1428. if (cm->caCacheCallback)
  1429. cm->caCacheCallback(der.buffer, (int)der.length, type);
  1430. }
  1431. else {
  1432. CYASSL_MSG(" CA Mutex Lock failed");
  1433. ret = BAD_MUTEX_E;
  1434. FreeSigner(signer, cm->heap);
  1435. }
  1436. }
  1437. }
  1438. CYASSL_MSG(" Freeing Parsed CA");
  1439. FreeDecodedCert(cert);
  1440. #ifdef CYASSL_SMALL_STACK
  1441. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1442. #endif
  1443. CYASSL_MSG(" Freeing der CA");
  1444. XFREE(der.buffer, cm->heap, DYNAMIC_TYPE_CA);
  1445. CYASSL_MSG(" OK Freeing der CA");
  1446. CYASSL_LEAVE("AddCA", ret);
  1447. return ret == 0 ? SSL_SUCCESS : ret;
  1448. }
  1449. #endif /* !NO_CERTS */
  1450. #ifndef NO_SESSION_CACHE
  1451. /* basic config gives a cache with 33 sessions, adequate for clients and
  1452. embedded servers
  1453. MEDIUM_SESSION_CACHE allows 1055 sessions, adequate for servers that
  1454. aren't under heavy load, basically allows 200 new sessions per minute
  1455. BIG_SESSION_CACHE yields 20,027 sessions
  1456. HUGE_SESSION_CACHE yields 65,791 sessions, for servers under heavy load,
  1457. allows over 13,000 new sessions per minute or over 200 new sessions per
  1458. second
  1459. SMALL_SESSION_CACHE only stores 6 sessions, good for embedded clients
  1460. or systems where the default of nearly 3kB is too much RAM, this define
  1461. uses less than 500 bytes RAM
  1462. default SESSION_CACHE stores 33 sessions (no XXX_SESSION_CACHE defined)
  1463. */
  1464. #ifdef HUGE_SESSION_CACHE
  1465. #define SESSIONS_PER_ROW 11
  1466. #define SESSION_ROWS 5981
  1467. #elif defined(BIG_SESSION_CACHE)
  1468. #define SESSIONS_PER_ROW 7
  1469. #define SESSION_ROWS 2861
  1470. #elif defined(MEDIUM_SESSION_CACHE)
  1471. #define SESSIONS_PER_ROW 5
  1472. #define SESSION_ROWS 211
  1473. #elif defined(SMALL_SESSION_CACHE)
  1474. #define SESSIONS_PER_ROW 2
  1475. #define SESSION_ROWS 3
  1476. #else
  1477. #define SESSIONS_PER_ROW 3
  1478. #define SESSION_ROWS 11
  1479. #endif
  1480. typedef struct SessionRow {
  1481. int nextIdx; /* where to place next one */
  1482. int totalCount; /* sessions ever on this row */
  1483. CYASSL_SESSION Sessions[SESSIONS_PER_ROW];
  1484. } SessionRow;
  1485. static SessionRow SessionCache[SESSION_ROWS];
  1486. static CyaSSL_Mutex session_mutex; /* SessionCache mutex */
  1487. #ifndef NO_CLIENT_CACHE
  1488. typedef struct ClientSession {
  1489. word16 serverRow; /* SessionCache Row id */
  1490. word16 serverIdx; /* SessionCache Idx (column) */
  1491. } ClientSession;
  1492. typedef struct ClientRow {
  1493. int nextIdx; /* where to place next one */
  1494. int totalCount; /* sessions ever on this row */
  1495. ClientSession Clients[SESSIONS_PER_ROW];
  1496. } ClientRow;
  1497. static ClientRow ClientCache[SESSION_ROWS]; /* Client Cache */
  1498. /* uses session mutex */
  1499. #endif /* NO_CLIENT_CACHE */
  1500. #endif /* NO_SESSION_CACHE */
  1501. int CyaSSL_Init(void)
  1502. {
  1503. int ret = SSL_SUCCESS;
  1504. CYASSL_ENTER("CyaSSL_Init");
  1505. if (initRefCount == 0) {
  1506. #ifndef NO_SESSION_CACHE
  1507. if (InitMutex(&session_mutex) != 0)
  1508. ret = BAD_MUTEX_E;
  1509. #endif
  1510. if (InitMutex(&count_mutex) != 0)
  1511. ret = BAD_MUTEX_E;
  1512. }
  1513. if (ret == SSL_SUCCESS) {
  1514. if (LockMutex(&count_mutex) != 0) {
  1515. CYASSL_MSG("Bad Lock Mutex count");
  1516. return BAD_MUTEX_E;
  1517. }
  1518. initRefCount++;
  1519. UnLockMutex(&count_mutex);
  1520. }
  1521. return ret;
  1522. }
  1523. #ifndef NO_CERTS
  1524. static const char* BEGIN_CERT = "-----BEGIN CERTIFICATE-----";
  1525. static const char* END_CERT = "-----END CERTIFICATE-----";
  1526. static const char* BEGIN_CERT_REQ = "-----BEGIN CERTIFICATE REQUEST-----";
  1527. static const char* END_CERT_REQ = "-----END CERTIFICATE REQUEST-----";
  1528. static const char* BEGIN_DH_PARAM = "-----BEGIN DH PARAMETERS-----";
  1529. static const char* END_DH_PARAM = "-----END DH PARAMETERS-----";
  1530. static const char* BEGIN_X509_CRL = "-----BEGIN X509 CRL-----";
  1531. static const char* END_X509_CRL = "-----END X509 CRL-----";
  1532. static const char* BEGIN_RSA_PRIV = "-----BEGIN RSA PRIVATE KEY-----";
  1533. static const char* END_RSA_PRIV = "-----END RSA PRIVATE KEY-----";
  1534. static const char* BEGIN_PRIV_KEY = "-----BEGIN PRIVATE KEY-----";
  1535. static const char* END_PRIV_KEY = "-----END PRIVATE KEY-----";
  1536. static const char* BEGIN_ENC_PRIV_KEY = "-----BEGIN ENCRYPTED PRIVATE KEY-----";
  1537. static const char* END_ENC_PRIV_KEY = "-----END ENCRYPTED PRIVATE KEY-----";
  1538. static const char* BEGIN_EC_PRIV = "-----BEGIN EC PRIVATE KEY-----";
  1539. static const char* END_EC_PRIV = "-----END EC PRIVATE KEY-----";
  1540. static const char* BEGIN_DSA_PRIV = "-----BEGIN DSA PRIVATE KEY-----";
  1541. static const char* END_DSA_PRIV = "-----END DSA PRIVATE KEY-----";
  1542. /* Remove PEM header/footer, convert to ASN1, store any encrypted data
  1543. info->consumed tracks of PEM bytes consumed in case multiple parts */
  1544. int PemToDer(const unsigned char* buff, long longSz, int type,
  1545. buffer* der, void* heap, EncryptedInfo* info, int* eccKey)
  1546. {
  1547. const char* header = NULL;
  1548. const char* footer = NULL;
  1549. char* headerEnd;
  1550. char* footerEnd;
  1551. char* consumedEnd;
  1552. char* bufferEnd = (char*)(buff + longSz);
  1553. long neededSz;
  1554. int ret = 0;
  1555. int dynamicType = 0;
  1556. int sz = (int)longSz;
  1557. switch (type) {
  1558. case CA_TYPE: /* same as below */
  1559. case CERT_TYPE: header= BEGIN_CERT; footer= END_CERT; break;
  1560. case CRL_TYPE: header= BEGIN_X509_CRL; footer= END_X509_CRL; break;
  1561. case DH_PARAM_TYPE: header= BEGIN_DH_PARAM; footer= END_DH_PARAM; break;
  1562. case CERTREQ_TYPE: header= BEGIN_CERT_REQ; footer= END_CERT_REQ; break;
  1563. default: header= BEGIN_RSA_PRIV; footer= END_RSA_PRIV; break;
  1564. }
  1565. switch (type) {
  1566. case CA_TYPE: dynamicType = DYNAMIC_TYPE_CA; break;
  1567. case CERT_TYPE: dynamicType = DYNAMIC_TYPE_CERT; break;
  1568. case CRL_TYPE: dynamicType = DYNAMIC_TYPE_CRL; break;
  1569. default: dynamicType = DYNAMIC_TYPE_KEY; break;
  1570. }
  1571. /* find header */
  1572. for (;;) {
  1573. headerEnd = XSTRNSTR((char*)buff, header, sz);
  1574. if (headerEnd || type != PRIVATEKEY_TYPE) {
  1575. break;
  1576. } else if (header == BEGIN_RSA_PRIV) {
  1577. header = BEGIN_PRIV_KEY; footer = END_PRIV_KEY;
  1578. } else if (header == BEGIN_PRIV_KEY) {
  1579. header = BEGIN_ENC_PRIV_KEY; footer = END_ENC_PRIV_KEY;
  1580. } else if (header == BEGIN_ENC_PRIV_KEY) {
  1581. header = BEGIN_EC_PRIV; footer = END_EC_PRIV;
  1582. } else if (header == BEGIN_ENC_PRIV_KEY) {
  1583. header = BEGIN_DSA_PRIV; footer = END_DSA_PRIV;
  1584. } else
  1585. break;
  1586. }
  1587. if (!headerEnd) {
  1588. CYASSL_MSG("Couldn't find PEM header");
  1589. return SSL_NO_PEM_HEADER;
  1590. }
  1591. headerEnd += XSTRLEN(header);
  1592. /* eat end of line */
  1593. if (headerEnd[0] == '\n')
  1594. headerEnd++;
  1595. else if (headerEnd[1] == '\n')
  1596. headerEnd += 2;
  1597. else
  1598. return SSL_BAD_FILE;
  1599. if (type == PRIVATEKEY_TYPE) {
  1600. if (eccKey)
  1601. *eccKey = header == BEGIN_EC_PRIV;
  1602. }
  1603. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1604. {
  1605. /* remove encrypted header if there */
  1606. char encHeader[] = "Proc-Type";
  1607. char* line = XSTRNSTR(headerEnd, encHeader, PEM_LINE_LEN);
  1608. if (line) {
  1609. char* newline;
  1610. char* finish;
  1611. char* start = XSTRNSTR(line, "DES", PEM_LINE_LEN);
  1612. if (!start)
  1613. start = XSTRNSTR(line, "AES", PEM_LINE_LEN);
  1614. if (!start) return SSL_BAD_FILE;
  1615. if (!info) return SSL_BAD_FILE;
  1616. finish = XSTRNSTR(start, ",", PEM_LINE_LEN);
  1617. if (start && finish && (start < finish)) {
  1618. newline = XSTRNSTR(finish, "\r", PEM_LINE_LEN);
  1619. XMEMCPY(info->name, start, finish - start);
  1620. info->name[finish - start] = 0;
  1621. XMEMCPY(info->iv, finish + 1, sizeof(info->iv));
  1622. if (!newline) newline = XSTRNSTR(finish, "\n", PEM_LINE_LEN);
  1623. if (newline && (newline > finish)) {
  1624. info->ivSz = (word32)(newline - (finish + 1));
  1625. info->set = 1;
  1626. }
  1627. else
  1628. return SSL_BAD_FILE;
  1629. }
  1630. else
  1631. return SSL_BAD_FILE;
  1632. /* eat blank line */
  1633. while (*newline == '\r' || *newline == '\n')
  1634. newline++;
  1635. headerEnd = newline;
  1636. }
  1637. }
  1638. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  1639. /* find footer */
  1640. footerEnd = XSTRNSTR((char*)buff, footer, sz);
  1641. if (!footerEnd)
  1642. return SSL_BAD_FILE;
  1643. consumedEnd = footerEnd + XSTRLEN(footer);
  1644. if (consumedEnd < bufferEnd) { /* handle no end of line on last line */
  1645. /* eat end of line */
  1646. if (consumedEnd[0] == '\n')
  1647. consumedEnd++;
  1648. else if (consumedEnd[1] == '\n')
  1649. consumedEnd += 2;
  1650. else
  1651. return SSL_BAD_FILE;
  1652. }
  1653. if (info)
  1654. info->consumed = (long)(consumedEnd - (char*)buff);
  1655. /* set up der buffer */
  1656. neededSz = (long)(footerEnd - headerEnd);
  1657. if (neededSz > sz || neededSz < 0)
  1658. return SSL_BAD_FILE;
  1659. der->buffer = (byte*)XMALLOC(neededSz, heap, dynamicType);
  1660. if (!der->buffer)
  1661. return MEMORY_ERROR;
  1662. der->length = (word32)neededSz;
  1663. if (Base64_Decode((byte*)headerEnd, (word32)neededSz, der->buffer,
  1664. &der->length) < 0)
  1665. return SSL_BAD_FILE;
  1666. if (header == BEGIN_PRIV_KEY) {
  1667. /* pkcs8 key, convert and adjust length */
  1668. if ((ret = ToTraditional(der->buffer, der->length)) < 0)
  1669. return ret;
  1670. der->length = ret;
  1671. return 0;
  1672. }
  1673. #if (defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)) && !defined(NO_PWDBASED)
  1674. if (header == BEGIN_ENC_PRIV_KEY) {
  1675. int passwordSz;
  1676. #ifdef CYASSL_SMALL_STACK
  1677. char* password = NULL;
  1678. #else
  1679. char password[80];
  1680. #endif
  1681. if (!info || !info->ctx || !info->ctx->passwd_cb)
  1682. return SSL_BAD_FILE; /* no callback error */
  1683. #ifdef CYASSL_SMALL_STACK
  1684. password = (char*)XMALLOC(80, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1685. if (password == NULL)
  1686. return MEMORY_E;
  1687. #endif
  1688. passwordSz = info->ctx->passwd_cb(password, sizeof(password), 0,
  1689. info->ctx->userdata);
  1690. /* convert and adjust length */
  1691. ret = ToTraditionalEnc(der->buffer, der->length, password, passwordSz);
  1692. #ifdef CYASSL_SMALL_STACK
  1693. XFREE(password, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1694. #endif
  1695. if (ret < 0)
  1696. return ret;
  1697. der->length = ret;
  1698. return 0;
  1699. }
  1700. #endif
  1701. return 0;
  1702. }
  1703. /* process the buffer buff, legnth sz, into ctx of format and type
  1704. used tracks bytes consumed, userChain specifies a user cert chain
  1705. to pass during the handshake */
  1706. static int ProcessBuffer(CYASSL_CTX* ctx, const unsigned char* buff,
  1707. long sz, int format, int type, CYASSL* ssl,
  1708. long* used, int userChain)
  1709. {
  1710. buffer der; /* holds DER or RAW (for NTRU) */
  1711. int ret;
  1712. int dynamicType = 0;
  1713. int eccKey = 0;
  1714. int rsaKey = 0;
  1715. void* heap = ctx ? ctx->heap : NULL;
  1716. #ifdef CYASSL_SMALL_STACK
  1717. EncryptedInfo* info = NULL;
  1718. #else
  1719. EncryptedInfo info[1];
  1720. #endif
  1721. (void)dynamicType;
  1722. (void)rsaKey;
  1723. if (used)
  1724. *used = sz; /* used bytes default to sz, PEM chain may shorten*/
  1725. if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM
  1726. && format != SSL_FILETYPE_RAW)
  1727. return SSL_BAD_FILETYPE;
  1728. if (ctx == NULL && ssl == NULL)
  1729. return BAD_FUNC_ARG;
  1730. if (type == CA_TYPE)
  1731. dynamicType = DYNAMIC_TYPE_CA;
  1732. else if (type == CERT_TYPE)
  1733. dynamicType = DYNAMIC_TYPE_CERT;
  1734. else
  1735. dynamicType = DYNAMIC_TYPE_KEY;
  1736. #ifdef CYASSL_SMALL_STACK
  1737. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  1738. DYNAMIC_TYPE_TMP_BUFFER);
  1739. if (info == NULL)
  1740. return MEMORY_E;
  1741. #endif
  1742. info->set = 0;
  1743. info->ctx = ctx;
  1744. info->consumed = 0;
  1745. der.buffer = 0;
  1746. if (format == SSL_FILETYPE_PEM) {
  1747. ret = PemToDer(buff, sz, type, &der, heap, info, &eccKey);
  1748. if (ret < 0) {
  1749. #ifdef CYASSL_SMALL_STACK
  1750. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1751. #endif
  1752. XFREE(der.buffer, heap, dynamicType);
  1753. return ret;
  1754. }
  1755. if (used)
  1756. *used = info->consumed;
  1757. /* we may have a user cert chain, try to consume */
  1758. if (userChain && type == CERT_TYPE && info->consumed < sz) {
  1759. #ifdef CYASSL_SMALL_STACK
  1760. byte staticBuffer[1]; /* force heap usage */
  1761. #else
  1762. byte staticBuffer[FILE_BUFFER_SIZE]; /* tmp chain buffer */
  1763. #endif
  1764. byte* chainBuffer = staticBuffer;
  1765. byte* shrinked = NULL; /* shrinked to size chainBuffer
  1766. * or staticBuffer */
  1767. int dynamicBuffer = 0;
  1768. word32 bufferSz = sizeof(staticBuffer);
  1769. long consumed = info->consumed;
  1770. word32 idx = 0;
  1771. int gotOne = 0;
  1772. if ( (sz - consumed) > (int)bufferSz) {
  1773. CYASSL_MSG("Growing Tmp Chain Buffer");
  1774. bufferSz = (word32)(sz - consumed);
  1775. /* will shrink to actual size */
  1776. chainBuffer = (byte*)XMALLOC(bufferSz, heap, DYNAMIC_TYPE_FILE);
  1777. if (chainBuffer == NULL) {
  1778. #ifdef CYASSL_SMALL_STACK
  1779. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1780. #endif
  1781. XFREE(der.buffer, heap, dynamicType);
  1782. return MEMORY_E;
  1783. }
  1784. dynamicBuffer = 1;
  1785. }
  1786. CYASSL_MSG("Processing Cert Chain");
  1787. while (consumed < sz) {
  1788. buffer part;
  1789. info->consumed = 0;
  1790. part.buffer = 0;
  1791. ret = PemToDer(buff + consumed, sz - consumed, type, &part,
  1792. heap, info, &eccKey);
  1793. if (ret == 0) {
  1794. gotOne = 1;
  1795. if ( (idx + part.length) > bufferSz) {
  1796. CYASSL_MSG(" Cert Chain bigger than buffer");
  1797. ret = BUFFER_E;
  1798. }
  1799. else {
  1800. c32to24(part.length, &chainBuffer[idx]);
  1801. idx += CERT_HEADER_SZ;
  1802. XMEMCPY(&chainBuffer[idx], part.buffer,part.length);
  1803. idx += part.length;
  1804. consumed += info->consumed;
  1805. if (used)
  1806. *used += info->consumed;
  1807. }
  1808. }
  1809. XFREE(part.buffer, heap, dynamicType);
  1810. if (ret == SSL_NO_PEM_HEADER && gotOne) {
  1811. CYASSL_MSG("We got one good PEM so stuff at end ok");
  1812. break;
  1813. }
  1814. if (ret < 0) {
  1815. CYASSL_MSG(" Error in Cert in Chain");
  1816. if (dynamicBuffer)
  1817. XFREE(chainBuffer, heap, DYNAMIC_TYPE_FILE);
  1818. #ifdef CYASSL_SMALL_STACK
  1819. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1820. #endif
  1821. XFREE(der.buffer, heap, dynamicType);
  1822. return ret;
  1823. }
  1824. CYASSL_MSG(" Consumed another Cert in Chain");
  1825. }
  1826. CYASSL_MSG("Finished Processing Cert Chain");
  1827. /* only retain actual size used */
  1828. shrinked = (byte*)XMALLOC(idx, heap, dynamicType);
  1829. if (shrinked) {
  1830. if (ssl) {
  1831. if (ssl->buffers.certChain.buffer &&
  1832. ssl->buffers.weOwnCertChain) {
  1833. XFREE(ssl->buffers.certChain.buffer, heap,
  1834. dynamicType);
  1835. }
  1836. ssl->buffers.certChain.buffer = shrinked;
  1837. ssl->buffers.certChain.length = idx;
  1838. XMEMCPY(ssl->buffers.certChain.buffer, chainBuffer,idx);
  1839. ssl->buffers.weOwnCertChain = 1;
  1840. } else if (ctx) {
  1841. if (ctx->certChain.buffer)
  1842. XFREE(ctx->certChain.buffer, heap, dynamicType);
  1843. ctx->certChain.buffer = shrinked;
  1844. ctx->certChain.length = idx;
  1845. XMEMCPY(ctx->certChain.buffer, chainBuffer, idx);
  1846. }
  1847. }
  1848. if (dynamicBuffer)
  1849. XFREE(chainBuffer, heap, DYNAMIC_TYPE_FILE);
  1850. if (shrinked == NULL) {
  1851. #ifdef CYASSL_SMALL_STACK
  1852. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1853. #endif
  1854. XFREE(der.buffer, heap, dynamicType);
  1855. return MEMORY_E;
  1856. }
  1857. }
  1858. }
  1859. else { /* ASN1 (DER) or RAW (NTRU) */
  1860. der.buffer = (byte*) XMALLOC(sz, heap, dynamicType);
  1861. if (!der.buffer) {
  1862. #ifdef CYASSL_SMALL_STACK
  1863. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1864. #endif
  1865. return MEMORY_ERROR;
  1866. }
  1867. XMEMCPY(der.buffer, buff, sz);
  1868. der.length = (word32)sz;
  1869. }
  1870. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  1871. if (info->set) {
  1872. /* decrypt */
  1873. int passwordSz;
  1874. #ifdef CYASSL_SMALL_STACK
  1875. char* password = NULL;
  1876. byte* key = NULL;
  1877. byte* iv = NULL;
  1878. #else
  1879. char password[80];
  1880. byte key[AES_256_KEY_SIZE];
  1881. byte iv[AES_IV_SIZE];
  1882. #endif
  1883. #ifdef CYASSL_SMALL_STACK
  1884. password = (char*)XMALLOC(80, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1885. key = (byte*)XMALLOC(AES_256_KEY_SIZE, NULL,
  1886. DYNAMIC_TYPE_TMP_BUFFER);
  1887. iv = (byte*)XMALLOC(AES_IV_SIZE, NULL,
  1888. DYNAMIC_TYPE_TMP_BUFFER);
  1889. if (password == NULL || key == NULL || iv == NULL) {
  1890. XFREE(password, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1891. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1892. XFREE(iv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1893. ret = MEMORY_E;
  1894. }
  1895. else
  1896. #endif
  1897. if (!ctx || !ctx->passwd_cb) {
  1898. ret = NO_PASSWORD;
  1899. }
  1900. else {
  1901. passwordSz = ctx->passwd_cb(password, sizeof(password), 0,
  1902. ctx->userdata);
  1903. /* use file's salt for key derivation, hex decode first */
  1904. if (Base16_Decode(info->iv, info->ivSz, info->iv, &info->ivSz)
  1905. != 0) {
  1906. ret = ASN_INPUT_E;
  1907. }
  1908. else if ((ret = EVP_BytesToKey(info->name, "MD5", info->iv,
  1909. (byte*)password, passwordSz, 1, key, iv)) <= 0) {
  1910. /* empty */
  1911. }
  1912. else if (XSTRNCMP(info->name, "DES-CBC", 7) == 0) {
  1913. ret = Des_CbcDecryptWithKey(der.buffer, der.buffer, der.length,
  1914. key, info->iv);
  1915. }
  1916. else if (XSTRNCMP(info->name, "DES-EDE3-CBC", 13) == 0) {
  1917. ret = Des3_CbcDecryptWithKey(der.buffer, der.buffer, der.length,
  1918. key, info->iv);
  1919. }
  1920. else if (XSTRNCMP(info->name, "AES-128-CBC", 13) == 0) {
  1921. ret = AesCbcDecryptWithKey(der.buffer, der.buffer, der.length,
  1922. key, AES_128_KEY_SIZE, info->iv);
  1923. }
  1924. else if (XSTRNCMP(info->name, "AES-192-CBC", 13) == 0) {
  1925. ret = AesCbcDecryptWithKey(der.buffer, der.buffer, der.length,
  1926. key, AES_192_KEY_SIZE, info->iv);
  1927. }
  1928. else if (XSTRNCMP(info->name, "AES-256-CBC", 13) == 0) {
  1929. ret = AesCbcDecryptWithKey(der.buffer, der.buffer, der.length,
  1930. key, AES_256_KEY_SIZE, info->iv);
  1931. }
  1932. else {
  1933. ret = SSL_BAD_FILE;
  1934. }
  1935. }
  1936. #ifdef CYASSL_SMALL_STACK
  1937. XFREE(password, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1938. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1939. XFREE(iv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1940. #endif
  1941. if (ret != 0) {
  1942. #ifdef CYASSL_SMALL_STACK
  1943. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1944. #endif
  1945. XFREE(der.buffer, heap, dynamicType);
  1946. return ret;
  1947. }
  1948. }
  1949. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  1950. #ifdef CYASSL_SMALL_STACK
  1951. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1952. #endif
  1953. if (type == CA_TYPE) {
  1954. if (ctx == NULL) {
  1955. CYASSL_MSG("Need context for CA load");
  1956. XFREE(der.buffer, heap, dynamicType);
  1957. return BAD_FUNC_ARG;
  1958. }
  1959. return AddCA(ctx->cm, der, CYASSL_USER_CA, ctx->verifyPeer);
  1960. /* takes der over */
  1961. }
  1962. else if (type == CERT_TYPE) {
  1963. if (ssl) {
  1964. if (ssl->buffers.weOwnCert && ssl->buffers.certificate.buffer)
  1965. XFREE(ssl->buffers.certificate.buffer, heap, dynamicType);
  1966. ssl->buffers.certificate = der;
  1967. ssl->buffers.weOwnCert = 1;
  1968. }
  1969. else if (ctx) {
  1970. if (ctx->certificate.buffer)
  1971. XFREE(ctx->certificate.buffer, heap, dynamicType);
  1972. ctx->certificate = der; /* takes der over */
  1973. }
  1974. }
  1975. else if (type == PRIVATEKEY_TYPE) {
  1976. if (ssl) {
  1977. if (ssl->buffers.weOwnKey && ssl->buffers.key.buffer)
  1978. XFREE(ssl->buffers.key.buffer, heap, dynamicType);
  1979. ssl->buffers.key = der;
  1980. ssl->buffers.weOwnKey = 1;
  1981. }
  1982. else if (ctx) {
  1983. if (ctx->privateKey.buffer)
  1984. XFREE(ctx->privateKey.buffer, heap, dynamicType);
  1985. ctx->privateKey = der; /* takes der over */
  1986. }
  1987. }
  1988. else {
  1989. XFREE(der.buffer, heap, dynamicType);
  1990. return SSL_BAD_CERTTYPE;
  1991. }
  1992. if (type == PRIVATEKEY_TYPE && format != SSL_FILETYPE_RAW) {
  1993. #ifndef NO_RSA
  1994. if (!eccKey) {
  1995. /* make sure RSA key can be used */
  1996. word32 idx = 0;
  1997. #ifdef CYASSL_SMALL_STACK
  1998. RsaKey* key = NULL;
  1999. #else
  2000. RsaKey key[1];
  2001. #endif
  2002. #ifdef CYASSL_SMALL_STACK
  2003. key = (RsaKey*)XMALLOC(sizeof(RsaKey), NULL,
  2004. DYNAMIC_TYPE_TMP_BUFFER);
  2005. if (key == NULL)
  2006. return MEMORY_E;
  2007. #endif
  2008. ret = InitRsaKey(key, 0);
  2009. if (ret == 0) {
  2010. if (RsaPrivateKeyDecode(der.buffer, &idx, key, der.length) !=
  2011. 0) {
  2012. #ifdef HAVE_ECC
  2013. /* could have DER ECC (or pkcs8 ecc), no easy way to tell */
  2014. eccKey = 1; /* so try it out */
  2015. #endif
  2016. if (!eccKey)
  2017. ret = SSL_BAD_FILE;
  2018. } else {
  2019. rsaKey = 1;
  2020. (void)rsaKey; /* for no ecc builds */
  2021. }
  2022. }
  2023. FreeRsaKey(key);
  2024. #ifdef CYASSL_SMALL_STACK
  2025. XFREE(key, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2026. #endif
  2027. if (ret != 0)
  2028. return ret;
  2029. }
  2030. #endif
  2031. #ifdef HAVE_ECC
  2032. if (!rsaKey) {
  2033. /* make sure ECC key can be used */
  2034. word32 idx = 0;
  2035. ecc_key key;
  2036. ecc_init(&key);
  2037. if (EccPrivateKeyDecode(der.buffer,&idx,&key,der.length) != 0) {
  2038. ecc_free(&key);
  2039. return SSL_BAD_FILE;
  2040. }
  2041. ecc_free(&key);
  2042. eccKey = 1;
  2043. if (ctx)
  2044. ctx->haveStaticECC = 1;
  2045. if (ssl)
  2046. ssl->options.haveStaticECC = 1;
  2047. }
  2048. #endif /* HAVE_ECC */
  2049. }
  2050. else if (type == CERT_TYPE) {
  2051. #ifdef CYASSL_SMALL_STACK
  2052. DecodedCert* cert = NULL;
  2053. #else
  2054. DecodedCert cert[1];
  2055. #endif
  2056. #ifdef CYASSL_SMALL_STACK
  2057. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  2058. DYNAMIC_TYPE_TMP_BUFFER);
  2059. if (cert == NULL)
  2060. return MEMORY_E;
  2061. #endif
  2062. CYASSL_MSG("Checking cert signature type");
  2063. InitDecodedCert(cert, der.buffer, der.length, heap);
  2064. if (DecodeToKey(cert, 0) < 0) {
  2065. CYASSL_MSG("Decode to key failed");
  2066. #ifdef CYASSL_SMALL_STACK
  2067. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2068. #endif
  2069. return SSL_BAD_FILE;
  2070. }
  2071. switch (cert->signatureOID) {
  2072. case CTC_SHAwECDSA:
  2073. case CTC_SHA256wECDSA:
  2074. case CTC_SHA384wECDSA:
  2075. case CTC_SHA512wECDSA:
  2076. CYASSL_MSG("ECDSA cert signature");
  2077. if (ctx)
  2078. ctx->haveECDSAsig = 1;
  2079. if (ssl)
  2080. ssl->options.haveECDSAsig = 1;
  2081. break;
  2082. default:
  2083. CYASSL_MSG("Not ECDSA cert signature");
  2084. break;
  2085. }
  2086. #ifdef HAVE_ECC
  2087. if (ctx)
  2088. ctx->pkCurveOID = cert->pkCurveOID;
  2089. if (ssl)
  2090. ssl->pkCurveOID = cert->pkCurveOID;
  2091. #endif
  2092. FreeDecodedCert(cert);
  2093. #ifdef CYASSL_SMALL_STACK
  2094. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2095. #endif
  2096. }
  2097. return SSL_SUCCESS;
  2098. }
  2099. /* CA PEM file for verification, may have multiple/chain certs to process */
  2100. static int ProcessChainBuffer(CYASSL_CTX* ctx, const unsigned char* buff,
  2101. long sz, int format, int type, CYASSL* ssl)
  2102. {
  2103. long used = 0;
  2104. int ret = 0;
  2105. int gotOne = 0;
  2106. CYASSL_MSG("Processing CA PEM file");
  2107. while (used < sz) {
  2108. long consumed = 0;
  2109. ret = ProcessBuffer(ctx, buff + used, sz - used, format, type, ssl,
  2110. &consumed, 0);
  2111. if (ret == SSL_NO_PEM_HEADER && gotOne) {
  2112. CYASSL_MSG("We got one good PEM file so stuff at end ok");
  2113. ret = SSL_SUCCESS;
  2114. break;
  2115. }
  2116. if (ret < 0)
  2117. break;
  2118. CYASSL_MSG(" Processed a CA");
  2119. gotOne = 1;
  2120. used += consumed;
  2121. }
  2122. return ret;
  2123. }
  2124. /* Verify the ceritficate, SSL_SUCCESS for ok, < 0 for error */
  2125. int CyaSSL_CertManagerVerifyBuffer(CYASSL_CERT_MANAGER* cm, const byte* buff,
  2126. long sz, int format)
  2127. {
  2128. int ret = 0;
  2129. buffer der;
  2130. #ifdef CYASSL_SMALL_STACK
  2131. DecodedCert* cert = NULL;
  2132. #else
  2133. DecodedCert cert[1];
  2134. #endif
  2135. CYASSL_ENTER("CyaSSL_CertManagerVerifyBuffer");
  2136. #ifdef CYASSL_SMALL_STACK
  2137. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  2138. DYNAMIC_TYPE_TMP_BUFFER);
  2139. if (cert == NULL)
  2140. return MEMORY_E;
  2141. #endif
  2142. der.buffer = NULL;
  2143. der.length = 0;
  2144. if (format == SSL_FILETYPE_PEM) {
  2145. int eccKey = 0; /* not used */
  2146. #ifdef CYASSL_SMALL_STACK
  2147. EncryptedInfo* info = NULL;
  2148. #else
  2149. EncryptedInfo info[1];
  2150. #endif
  2151. #ifdef CYASSL_SMALL_STACK
  2152. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  2153. DYNAMIC_TYPE_TMP_BUFFER);
  2154. if (info == NULL) {
  2155. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2156. return MEMORY_E;
  2157. }
  2158. #endif
  2159. info->set = 0;
  2160. info->ctx = NULL;
  2161. info->consumed = 0;
  2162. ret = PemToDer(buff, sz, CERT_TYPE, &der, cm->heap, info, &eccKey);
  2163. if (ret == 0)
  2164. InitDecodedCert(cert, der.buffer, der.length, cm->heap);
  2165. #ifdef CYASSL_SMALL_STACK
  2166. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2167. #endif
  2168. }
  2169. else
  2170. InitDecodedCert(cert, (byte*)buff, (word32)sz, cm->heap);
  2171. if (ret == 0)
  2172. ret = ParseCertRelative(cert, CERT_TYPE, 1, cm);
  2173. #ifdef HAVE_CRL
  2174. if (ret == 0 && cm->crlEnabled)
  2175. ret = CheckCertCRL(cm->crl, cert);
  2176. #endif
  2177. FreeDecodedCert(cert);
  2178. XFREE(der.buffer, cm->heap, DYNAMIC_TYPE_CERT);
  2179. #ifdef CYASSL_SMALL_STACK
  2180. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2181. #endif
  2182. return ret == 0 ? SSL_SUCCESS : ret;
  2183. }
  2184. /* turn on OCSP if off and compiled in, set options */
  2185. int CyaSSL_CertManagerEnableOCSP(CYASSL_CERT_MANAGER* cm, int options)
  2186. {
  2187. int ret = SSL_SUCCESS;
  2188. (void)options;
  2189. CYASSL_ENTER("CyaSSL_CertManagerEnableOCSP");
  2190. if (cm == NULL)
  2191. return BAD_FUNC_ARG;
  2192. #ifdef HAVE_OCSP
  2193. if (cm->ocsp == NULL) {
  2194. cm->ocsp = (CYASSL_OCSP*)XMALLOC(sizeof(CYASSL_OCSP), cm->heap,
  2195. DYNAMIC_TYPE_OCSP);
  2196. if (cm->ocsp == NULL)
  2197. return MEMORY_E;
  2198. if (InitOCSP(cm->ocsp, cm) != 0) {
  2199. CYASSL_MSG("Init OCSP failed");
  2200. FreeOCSP(cm->ocsp, 1);
  2201. cm->ocsp = NULL;
  2202. return SSL_FAILURE;
  2203. }
  2204. }
  2205. cm->ocspEnabled = 1;
  2206. if (options & CYASSL_OCSP_URL_OVERRIDE)
  2207. cm->ocspUseOverrideURL = 1;
  2208. if (options & CYASSL_OCSP_NO_NONCE)
  2209. cm->ocspSendNonce = 0;
  2210. else
  2211. cm->ocspSendNonce = 1;
  2212. #ifndef CYASSL_USER_IO
  2213. cm->ocspIOCb = EmbedOcspLookup;
  2214. cm->ocspRespFreeCb = EmbedOcspRespFree;
  2215. #endif /* CYASSL_USER_IO */
  2216. #else
  2217. ret = NOT_COMPILED_IN;
  2218. #endif
  2219. return ret;
  2220. }
  2221. int CyaSSL_CertManagerDisableOCSP(CYASSL_CERT_MANAGER* cm)
  2222. {
  2223. CYASSL_ENTER("CyaSSL_CertManagerDisableOCSP");
  2224. if (cm == NULL)
  2225. return BAD_FUNC_ARG;
  2226. cm->ocspEnabled = 0;
  2227. return SSL_SUCCESS;
  2228. }
  2229. #ifdef HAVE_OCSP
  2230. /* check CRL if enabled, SSL_SUCCESS */
  2231. int CyaSSL_CertManagerCheckOCSP(CYASSL_CERT_MANAGER* cm, byte* der, int sz)
  2232. {
  2233. int ret;
  2234. #ifdef CYASSL_SMALL_STACK
  2235. DecodedCert* cert = NULL;
  2236. #else
  2237. DecodedCert cert[1];
  2238. #endif
  2239. CYASSL_ENTER("CyaSSL_CertManagerCheckOCSP");
  2240. if (cm == NULL)
  2241. return BAD_FUNC_ARG;
  2242. if (cm->ocspEnabled == 0)
  2243. return SSL_SUCCESS;
  2244. #ifdef CYASSL_SMALL_STACK
  2245. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  2246. DYNAMIC_TYPE_TMP_BUFFER);
  2247. if (cert == NULL)
  2248. return MEMORY_E;
  2249. #endif
  2250. InitDecodedCert(cert, der, sz, NULL);
  2251. if ((ret = ParseCertRelative(cert, CERT_TYPE, NO_VERIFY, cm)) != 0) {
  2252. CYASSL_MSG("ParseCert failed");
  2253. }
  2254. else if ((ret = CheckCertOCSP(cm->ocsp, cert)) != 0) {
  2255. CYASSL_MSG("CheckCertOCSP failed");
  2256. }
  2257. FreeDecodedCert(cert);
  2258. #ifdef CYASSL_SMALL_STACK
  2259. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2260. #endif
  2261. return ret == 0 ? SSL_SUCCESS : ret;
  2262. }
  2263. int CyaSSL_CertManagerSetOCSPOverrideURL(CYASSL_CERT_MANAGER* cm,
  2264. const char* url)
  2265. {
  2266. CYASSL_ENTER("CyaSSL_CertManagerSetOCSPOverrideURL");
  2267. if (cm == NULL)
  2268. return BAD_FUNC_ARG;
  2269. XFREE(cm->ocspOverrideURL, cm->heap, 0);
  2270. if (url != NULL) {
  2271. int urlSz = (int)XSTRLEN(url) + 1;
  2272. cm->ocspOverrideURL = (char*)XMALLOC(urlSz, cm->heap, 0);
  2273. if (cm->ocspOverrideURL != NULL) {
  2274. XMEMCPY(cm->ocspOverrideURL, url, urlSz);
  2275. }
  2276. else
  2277. return MEMORY_E;
  2278. }
  2279. else
  2280. cm->ocspOverrideURL = NULL;
  2281. return SSL_SUCCESS;
  2282. }
  2283. int CyaSSL_CertManagerSetOCSP_Cb(CYASSL_CERT_MANAGER* cm,
  2284. CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
  2285. {
  2286. CYASSL_ENTER("CyaSSL_CertManagerSetOCSP_Cb");
  2287. if (cm == NULL)
  2288. return BAD_FUNC_ARG;
  2289. cm->ocspIOCb = ioCb;
  2290. cm->ocspRespFreeCb = respFreeCb;
  2291. cm->ocspIOCtx = ioCbCtx;
  2292. return SSL_SUCCESS;
  2293. }
  2294. int CyaSSL_EnableOCSP(CYASSL* ssl, int options)
  2295. {
  2296. CYASSL_ENTER("CyaSSL_EnableOCSP");
  2297. if (ssl)
  2298. return CyaSSL_CertManagerEnableOCSP(ssl->ctx->cm, options);
  2299. else
  2300. return BAD_FUNC_ARG;
  2301. }
  2302. int CyaSSL_DisableOCSP(CYASSL* ssl)
  2303. {
  2304. CYASSL_ENTER("CyaSSL_DisableOCSP");
  2305. if (ssl)
  2306. return CyaSSL_CertManagerDisableOCSP(ssl->ctx->cm);
  2307. else
  2308. return BAD_FUNC_ARG;
  2309. }
  2310. int CyaSSL_SetOCSP_OverrideURL(CYASSL* ssl, const char* url)
  2311. {
  2312. CYASSL_ENTER("CyaSSL_SetOCSP_OverrideURL");
  2313. if (ssl)
  2314. return CyaSSL_CertManagerSetOCSPOverrideURL(ssl->ctx->cm, url);
  2315. else
  2316. return BAD_FUNC_ARG;
  2317. }
  2318. int CyaSSL_SetOCSP_Cb(CYASSL* ssl,
  2319. CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
  2320. {
  2321. CYASSL_ENTER("CyaSSL_SetOCSP_Cb");
  2322. if (ssl)
  2323. return CyaSSL_CertManagerSetOCSP_Cb(ssl->ctx->cm,
  2324. ioCb, respFreeCb, ioCbCtx);
  2325. else
  2326. return BAD_FUNC_ARG;
  2327. }
  2328. int CyaSSL_CTX_EnableOCSP(CYASSL_CTX* ctx, int options)
  2329. {
  2330. CYASSL_ENTER("CyaSSL_CTX_EnableOCSP");
  2331. if (ctx)
  2332. return CyaSSL_CertManagerEnableOCSP(ctx->cm, options);
  2333. else
  2334. return BAD_FUNC_ARG;
  2335. }
  2336. int CyaSSL_CTX_DisableOCSP(CYASSL_CTX* ctx)
  2337. {
  2338. CYASSL_ENTER("CyaSSL_CTX_DisableOCSP");
  2339. if (ctx)
  2340. return CyaSSL_CertManagerDisableOCSP(ctx->cm);
  2341. else
  2342. return BAD_FUNC_ARG;
  2343. }
  2344. int CyaSSL_CTX_SetOCSP_OverrideURL(CYASSL_CTX* ctx, const char* url)
  2345. {
  2346. CYASSL_ENTER("CyaSSL_SetOCSP_OverrideURL");
  2347. if (ctx)
  2348. return CyaSSL_CertManagerSetOCSPOverrideURL(ctx->cm, url);
  2349. else
  2350. return BAD_FUNC_ARG;
  2351. }
  2352. int CyaSSL_CTX_SetOCSP_Cb(CYASSL_CTX* ctx,
  2353. CbOCSPIO ioCb, CbOCSPRespFree respFreeCb, void* ioCbCtx)
  2354. {
  2355. CYASSL_ENTER("CyaSSL_CTX_SetOCSP_Cb");
  2356. if (ctx)
  2357. return CyaSSL_CertManagerSetOCSP_Cb(ctx->cm, ioCb, respFreeCb, ioCbCtx);
  2358. else
  2359. return BAD_FUNC_ARG;
  2360. }
  2361. #endif /* HAVE_OCSP */
  2362. #ifndef NO_FILESYSTEM
  2363. #if defined(CYASSL_MDK_ARM)
  2364. extern FILE * CyaSSL_fopen(const char *name, const char *mode) ;
  2365. #define XFOPEN CyaSSL_fopen
  2366. #else
  2367. #define XFOPEN fopen
  2368. #endif
  2369. /* process a file with name fname into ctx of format and type
  2370. userChain specifies a user certificate chain to pass during handshake */
  2371. int ProcessFile(CYASSL_CTX* ctx, const char* fname, int format, int type,
  2372. CYASSL* ssl, int userChain, CYASSL_CRL* crl)
  2373. {
  2374. #ifdef CYASSL_SMALL_STACK
  2375. byte staticBuffer[1]; /* force heap usage */
  2376. #else
  2377. byte staticBuffer[FILE_BUFFER_SIZE];
  2378. #endif
  2379. byte* myBuffer = staticBuffer;
  2380. int dynamic = 0;
  2381. int ret;
  2382. long sz = 0;
  2383. XFILE file;
  2384. void* heapHint = ctx ? ctx->heap : NULL;
  2385. (void)crl;
  2386. (void)heapHint;
  2387. if (fname == NULL) return SSL_BAD_FILE;
  2388. file = XFOPEN(fname, "rb");
  2389. if (file == XBADFILE) return SSL_BAD_FILE;
  2390. XFSEEK(file, 0, XSEEK_END);
  2391. sz = XFTELL(file);
  2392. XREWIND(file);
  2393. if (sz > (long)sizeof(staticBuffer)) {
  2394. CYASSL_MSG("Getting dynamic buffer");
  2395. myBuffer = (byte*)XMALLOC(sz, heapHint, DYNAMIC_TYPE_FILE);
  2396. if (myBuffer == NULL) {
  2397. XFCLOSE(file);
  2398. return SSL_BAD_FILE;
  2399. }
  2400. dynamic = 1;
  2401. }
  2402. else if (sz < 0) {
  2403. XFCLOSE(file);
  2404. return SSL_BAD_FILE;
  2405. }
  2406. if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
  2407. ret = SSL_BAD_FILE;
  2408. else {
  2409. if (type == CA_TYPE && format == SSL_FILETYPE_PEM)
  2410. ret = ProcessChainBuffer(ctx, myBuffer, sz, format, type, ssl);
  2411. #ifdef HAVE_CRL
  2412. else if (type == CRL_TYPE)
  2413. ret = BufferLoadCRL(crl, myBuffer, sz, format);
  2414. #endif
  2415. else
  2416. ret = ProcessBuffer(ctx, myBuffer, sz, format, type, ssl, NULL,
  2417. userChain);
  2418. }
  2419. XFCLOSE(file);
  2420. if (dynamic)
  2421. XFREE(myBuffer, heapHint, DYNAMIC_TYPE_FILE);
  2422. return ret;
  2423. }
  2424. /* loads file then loads each file in path, no c_rehash */
  2425. int CyaSSL_CTX_load_verify_locations(CYASSL_CTX* ctx, const char* file,
  2426. const char* path)
  2427. {
  2428. int ret = SSL_SUCCESS;
  2429. CYASSL_ENTER("CyaSSL_CTX_load_verify_locations");
  2430. (void)path;
  2431. if (ctx == NULL || (file == NULL && path == NULL) )
  2432. return SSL_FAILURE;
  2433. if (file)
  2434. ret = ProcessFile(ctx, file, SSL_FILETYPE_PEM, CA_TYPE, NULL, 0, NULL);
  2435. if (ret == SSL_SUCCESS && path) {
  2436. /* try to load each regular file in path */
  2437. #ifdef USE_WINDOWS_API
  2438. WIN32_FIND_DATAA FindFileData;
  2439. HANDLE hFind;
  2440. #ifdef CYASSL_SMALL_STACK
  2441. char* name = NULL;
  2442. #else
  2443. char name[MAX_FILENAME_SZ];
  2444. #endif
  2445. #ifdef CYASSL_SMALL_STACK
  2446. name = (char*)XMALLOC(MAX_FILENAME_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2447. if (name == NULL)
  2448. return MEMORY_E;
  2449. #endif
  2450. XMEMSET(name, 0, MAX_FILENAME_SZ);
  2451. XSTRNCPY(name, path, MAX_FILENAME_SZ - 4);
  2452. XSTRNCAT(name, "\\*", 3);
  2453. hFind = FindFirstFileA(name, &FindFileData);
  2454. if (hFind == INVALID_HANDLE_VALUE) {
  2455. CYASSL_MSG("FindFirstFile for path verify locations failed");
  2456. #ifdef CYASSL_SMALL_STACK
  2457. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2458. #endif
  2459. return BAD_PATH_ERROR;
  2460. }
  2461. do {
  2462. if (FindFileData.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY) {
  2463. XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 3);
  2464. XSTRNCAT(name, "\\", 2);
  2465. XSTRNCAT(name, FindFileData.cFileName, MAX_FILENAME_SZ/2);
  2466. ret = ProcessFile(ctx, name, SSL_FILETYPE_PEM, CA_TYPE, NULL,0,
  2467. NULL);
  2468. }
  2469. } while (ret == SSL_SUCCESS && FindNextFileA(hFind, &FindFileData));
  2470. #ifdef CYASSL_SMALL_STACK
  2471. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2472. #endif
  2473. FindClose(hFind);
  2474. #elif !defined(NO_CYASSL_DIR)
  2475. struct dirent* entry;
  2476. DIR* dir = opendir(path);
  2477. #ifdef CYASSL_SMALL_STACK
  2478. char* name = NULL;
  2479. #else
  2480. char name[MAX_FILENAME_SZ];
  2481. #endif
  2482. if (dir == NULL) {
  2483. CYASSL_MSG("opendir path verify locations failed");
  2484. return BAD_PATH_ERROR;
  2485. }
  2486. #ifdef CYASSL_SMALL_STACK
  2487. name = (char*)XMALLOC(MAX_FILENAME_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2488. if (name == NULL)
  2489. return MEMORY_E;
  2490. #endif
  2491. while ( ret == SSL_SUCCESS && (entry = readdir(dir)) != NULL) {
  2492. struct stat s;
  2493. XMEMSET(name, 0, MAX_FILENAME_SZ);
  2494. XSTRNCPY(name, path, MAX_FILENAME_SZ/2 - 2);
  2495. XSTRNCAT(name, "/", 1);
  2496. XSTRNCAT(name, entry->d_name, MAX_FILENAME_SZ/2);
  2497. if (stat(name, &s) != 0) {
  2498. CYASSL_MSG("stat on name failed");
  2499. ret = BAD_PATH_ERROR;
  2500. } else if (s.st_mode & S_IFREG)
  2501. ret = ProcessFile(ctx, name, SSL_FILETYPE_PEM, CA_TYPE, NULL,0,
  2502. NULL);
  2503. }
  2504. #ifdef CYASSL_SMALL_STACK
  2505. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2506. #endif
  2507. closedir(dir);
  2508. #endif
  2509. }
  2510. return ret;
  2511. }
  2512. /* Verify the ceritficate, SSL_SUCCESS for ok, < 0 for error */
  2513. int CyaSSL_CertManagerVerify(CYASSL_CERT_MANAGER* cm, const char* fname,
  2514. int format)
  2515. {
  2516. int ret = SSL_FATAL_ERROR;
  2517. #ifdef CYASSL_SMALL_STACK
  2518. byte staticBuffer[1]; /* force heap usage */
  2519. #else
  2520. byte staticBuffer[FILE_BUFFER_SIZE];
  2521. #endif
  2522. byte* myBuffer = staticBuffer;
  2523. int dynamic = 0;
  2524. long sz = 0;
  2525. XFILE file = XFOPEN(fname, "rb");
  2526. CYASSL_ENTER("CyaSSL_CertManagerVerify");
  2527. if (file == XBADFILE) return SSL_BAD_FILE;
  2528. XFSEEK(file, 0, XSEEK_END);
  2529. sz = XFTELL(file);
  2530. XREWIND(file);
  2531. if (sz > MAX_CYASSL_FILE_SIZE || sz < 0) {
  2532. CYASSL_MSG("CertManagerVerify file bad size");
  2533. XFCLOSE(file);
  2534. return SSL_BAD_FILE;
  2535. }
  2536. if (sz > (long)sizeof(staticBuffer)) {
  2537. CYASSL_MSG("Getting dynamic buffer");
  2538. myBuffer = (byte*) XMALLOC(sz, cm->heap, DYNAMIC_TYPE_FILE);
  2539. if (myBuffer == NULL) {
  2540. XFCLOSE(file);
  2541. return SSL_BAD_FILE;
  2542. }
  2543. dynamic = 1;
  2544. }
  2545. if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
  2546. ret = SSL_BAD_FILE;
  2547. else
  2548. ret = CyaSSL_CertManagerVerifyBuffer(cm, myBuffer, sz, format);
  2549. XFCLOSE(file);
  2550. if (dynamic)
  2551. XFREE(myBuffer, cm->heap, DYNAMIC_TYPE_FILE);
  2552. return ret;
  2553. }
  2554. static INLINE CYASSL_METHOD* cm_pick_method(void)
  2555. {
  2556. #ifndef NO_CYASSL_CLIENT
  2557. #ifdef NO_OLD_TLS
  2558. return CyaTLSv1_2_client_method();
  2559. #else
  2560. return CyaSSLv3_client_method();
  2561. #endif
  2562. #elif !defined(NO_CYASSL_SERVER)
  2563. #ifdef NO_OLD_TLS
  2564. return CyaTLSv1_2_server_method();
  2565. #else
  2566. return CyaSSLv3_server_method();
  2567. #endif
  2568. #else
  2569. return NULL;
  2570. #endif
  2571. }
  2572. /* like load verify locations, 1 for success, < 0 for error */
  2573. int CyaSSL_CertManagerLoadCA(CYASSL_CERT_MANAGER* cm, const char* file,
  2574. const char* path)
  2575. {
  2576. int ret = SSL_FATAL_ERROR;
  2577. CYASSL_CTX* tmp;
  2578. CYASSL_ENTER("CyaSSL_CertManagerLoadCA");
  2579. if (cm == NULL) {
  2580. CYASSL_MSG("No CertManager error");
  2581. return ret;
  2582. }
  2583. tmp = CyaSSL_CTX_new(cm_pick_method());
  2584. if (tmp == NULL) {
  2585. CYASSL_MSG("CTX new failed");
  2586. return ret;
  2587. }
  2588. /* for tmp use */
  2589. CyaSSL_CertManagerFree(tmp->cm);
  2590. tmp->cm = cm;
  2591. ret = CyaSSL_CTX_load_verify_locations(tmp, file, path);
  2592. /* don't loose our good one */
  2593. tmp->cm = NULL;
  2594. CyaSSL_CTX_free(tmp);
  2595. return ret;
  2596. }
  2597. /* turn on CRL if off and compiled in, set options */
  2598. int CyaSSL_CertManagerEnableCRL(CYASSL_CERT_MANAGER* cm, int options)
  2599. {
  2600. int ret = SSL_SUCCESS;
  2601. (void)options;
  2602. CYASSL_ENTER("CyaSSL_CertManagerEnableCRL");
  2603. if (cm == NULL)
  2604. return BAD_FUNC_ARG;
  2605. #ifdef HAVE_CRL
  2606. if (cm->crl == NULL) {
  2607. cm->crl = (CYASSL_CRL*)XMALLOC(sizeof(CYASSL_CRL), cm->heap,
  2608. DYNAMIC_TYPE_CRL);
  2609. if (cm->crl == NULL)
  2610. return MEMORY_E;
  2611. if (InitCRL(cm->crl, cm) != 0) {
  2612. CYASSL_MSG("Init CRL failed");
  2613. FreeCRL(cm->crl, 1);
  2614. cm->crl = NULL;
  2615. return SSL_FAILURE;
  2616. }
  2617. }
  2618. cm->crlEnabled = 1;
  2619. if (options & CYASSL_CRL_CHECKALL)
  2620. cm->crlCheckAll = 1;
  2621. #else
  2622. ret = NOT_COMPILED_IN;
  2623. #endif
  2624. return ret;
  2625. }
  2626. int CyaSSL_CertManagerDisableCRL(CYASSL_CERT_MANAGER* cm)
  2627. {
  2628. CYASSL_ENTER("CyaSSL_CertManagerDisableCRL");
  2629. if (cm == NULL)
  2630. return BAD_FUNC_ARG;
  2631. cm->crlEnabled = 0;
  2632. return SSL_SUCCESS;
  2633. }
  2634. int CyaSSL_CTX_check_private_key(CYASSL_CTX* ctx)
  2635. {
  2636. /* TODO: check private against public for RSA match */
  2637. (void)ctx;
  2638. CYASSL_ENTER("SSL_CTX_check_private_key");
  2639. return SSL_SUCCESS;
  2640. }
  2641. #ifdef HAVE_CRL
  2642. /* check CRL if enabled, SSL_SUCCESS */
  2643. int CyaSSL_CertManagerCheckCRL(CYASSL_CERT_MANAGER* cm, byte* der, int sz)
  2644. {
  2645. int ret = 0;
  2646. #ifdef CYASSL_SMALL_STACK
  2647. DecodedCert* cert = NULL;
  2648. #else
  2649. DecodedCert cert[1];
  2650. #endif
  2651. CYASSL_ENTER("CyaSSL_CertManagerCheckCRL");
  2652. if (cm == NULL)
  2653. return BAD_FUNC_ARG;
  2654. if (cm->crlEnabled == 0)
  2655. return SSL_SUCCESS;
  2656. #ifdef CYASSL_SMALL_STACK
  2657. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  2658. DYNAMIC_TYPE_TMP_BUFFER);
  2659. if (cert == NULL)
  2660. return MEMORY_E;
  2661. #endif
  2662. InitDecodedCert(cert, der, sz, NULL);
  2663. if ((ret = ParseCertRelative(cert, CERT_TYPE, NO_VERIFY, cm)) != 0) {
  2664. CYASSL_MSG("ParseCert failed");
  2665. }
  2666. else if ((ret = CheckCertCRL(cm->crl, cert)) != 0) {
  2667. CYASSL_MSG("CheckCertCRL failed");
  2668. }
  2669. FreeDecodedCert(cert);
  2670. #ifdef CYASSL_SMALL_STACK
  2671. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2672. #endif
  2673. return ret == 0 ? SSL_SUCCESS : ret;
  2674. }
  2675. int CyaSSL_CertManagerSetCRL_Cb(CYASSL_CERT_MANAGER* cm, CbMissingCRL cb)
  2676. {
  2677. CYASSL_ENTER("CyaSSL_CertManagerSetCRL_Cb");
  2678. if (cm == NULL)
  2679. return BAD_FUNC_ARG;
  2680. cm->cbMissingCRL = cb;
  2681. return SSL_SUCCESS;
  2682. }
  2683. int CyaSSL_CertManagerLoadCRL(CYASSL_CERT_MANAGER* cm, const char* path,
  2684. int type, int monitor)
  2685. {
  2686. CYASSL_ENTER("CyaSSL_CertManagerLoadCRL");
  2687. if (cm == NULL)
  2688. return BAD_FUNC_ARG;
  2689. if (cm->crl == NULL) {
  2690. if (CyaSSL_CertManagerEnableCRL(cm, 0) != SSL_SUCCESS) {
  2691. CYASSL_MSG("Enable CRL failed");
  2692. return SSL_FATAL_ERROR;
  2693. }
  2694. }
  2695. return LoadCRL(cm->crl, path, type, monitor);
  2696. }
  2697. int CyaSSL_EnableCRL(CYASSL* ssl, int options)
  2698. {
  2699. CYASSL_ENTER("CyaSSL_EnableCRL");
  2700. if (ssl)
  2701. return CyaSSL_CertManagerEnableCRL(ssl->ctx->cm, options);
  2702. else
  2703. return BAD_FUNC_ARG;
  2704. }
  2705. int CyaSSL_DisableCRL(CYASSL* ssl)
  2706. {
  2707. CYASSL_ENTER("CyaSSL_DisableCRL");
  2708. if (ssl)
  2709. return CyaSSL_CertManagerDisableCRL(ssl->ctx->cm);
  2710. else
  2711. return BAD_FUNC_ARG;
  2712. }
  2713. int CyaSSL_LoadCRL(CYASSL* ssl, const char* path, int type, int monitor)
  2714. {
  2715. CYASSL_ENTER("CyaSSL_LoadCRL");
  2716. if (ssl)
  2717. return CyaSSL_CertManagerLoadCRL(ssl->ctx->cm, path, type, monitor);
  2718. else
  2719. return BAD_FUNC_ARG;
  2720. }
  2721. int CyaSSL_SetCRL_Cb(CYASSL* ssl, CbMissingCRL cb)
  2722. {
  2723. CYASSL_ENTER("CyaSSL_SetCRL_Cb");
  2724. if (ssl)
  2725. return CyaSSL_CertManagerSetCRL_Cb(ssl->ctx->cm, cb);
  2726. else
  2727. return BAD_FUNC_ARG;
  2728. }
  2729. int CyaSSL_CTX_EnableCRL(CYASSL_CTX* ctx, int options)
  2730. {
  2731. CYASSL_ENTER("CyaSSL_CTX_EnableCRL");
  2732. if (ctx)
  2733. return CyaSSL_CertManagerEnableCRL(ctx->cm, options);
  2734. else
  2735. return BAD_FUNC_ARG;
  2736. }
  2737. int CyaSSL_CTX_DisableCRL(CYASSL_CTX* ctx)
  2738. {
  2739. CYASSL_ENTER("CyaSSL_CTX_DisableCRL");
  2740. if (ctx)
  2741. return CyaSSL_CertManagerDisableCRL(ctx->cm);
  2742. else
  2743. return BAD_FUNC_ARG;
  2744. }
  2745. int CyaSSL_CTX_LoadCRL(CYASSL_CTX* ctx, const char* path, int type, int monitor)
  2746. {
  2747. CYASSL_ENTER("CyaSSL_CTX_LoadCRL");
  2748. if (ctx)
  2749. return CyaSSL_CertManagerLoadCRL(ctx->cm, path, type, monitor);
  2750. else
  2751. return BAD_FUNC_ARG;
  2752. }
  2753. int CyaSSL_CTX_SetCRL_Cb(CYASSL_CTX* ctx, CbMissingCRL cb)
  2754. {
  2755. CYASSL_ENTER("CyaSSL_CTX_SetCRL_Cb");
  2756. if (ctx)
  2757. return CyaSSL_CertManagerSetCRL_Cb(ctx->cm, cb);
  2758. else
  2759. return BAD_FUNC_ARG;
  2760. }
  2761. #endif /* HAVE_CRL */
  2762. #ifdef CYASSL_DER_LOAD
  2763. /* Add format parameter to allow DER load of CA files */
  2764. int CyaSSL_CTX_der_load_verify_locations(CYASSL_CTX* ctx, const char* file,
  2765. int format)
  2766. {
  2767. CYASSL_ENTER("CyaSSL_CTX_der_load_verify_locations");
  2768. if (ctx == NULL || file == NULL)
  2769. return SSL_FAILURE;
  2770. if (ProcessFile(ctx, file, format, CA_TYPE, NULL, 0, NULL) == SSL_SUCCESS)
  2771. return SSL_SUCCESS;
  2772. return SSL_FAILURE;
  2773. }
  2774. #endif /* CYASSL_DER_LOAD */
  2775. #ifdef CYASSL_CERT_GEN
  2776. /* load pem cert from file into der buffer, return der size or error */
  2777. int CyaSSL_PemCertToDer(const char* fileName, unsigned char* derBuf, int derSz)
  2778. {
  2779. #ifdef CYASSL_SMALL_STACK
  2780. EncryptedInfo* info = NULL;
  2781. byte staticBuffer[1]; /* force XMALLOC */
  2782. #else
  2783. EncryptedInfo info[1];
  2784. byte staticBuffer[FILE_BUFFER_SIZE];
  2785. #endif
  2786. byte* fileBuf = staticBuffer;
  2787. int dynamic = 0;
  2788. int ret = 0;
  2789. int ecc = 0;
  2790. long sz = 0;
  2791. XFILE file = XFOPEN(fileName, "rb");
  2792. buffer converted;
  2793. CYASSL_ENTER("CyaSSL_PemCertToDer");
  2794. if (file == XBADFILE)
  2795. ret = SSL_BAD_FILE;
  2796. else {
  2797. XFSEEK(file, 0, XSEEK_END);
  2798. sz = XFTELL(file);
  2799. XREWIND(file);
  2800. if (sz < 0) {
  2801. ret = SSL_BAD_FILE;
  2802. }
  2803. else if (sz > (long)sizeof(staticBuffer)) {
  2804. fileBuf = (byte*)XMALLOC(sz, 0, DYNAMIC_TYPE_FILE);
  2805. if (fileBuf == NULL)
  2806. ret = MEMORY_E;
  2807. else
  2808. dynamic = 1;
  2809. }
  2810. converted.buffer = 0;
  2811. if (ret == 0) {
  2812. if ( (ret = (int)XFREAD(fileBuf, sz, 1, file)) < 0)
  2813. ret = SSL_BAD_FILE;
  2814. else {
  2815. #ifdef CYASSL_SMALL_STACK
  2816. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  2817. DYNAMIC_TYPE_TMP_BUFFER);
  2818. if (info == NULL)
  2819. ret = MEMORY_E;
  2820. else
  2821. #endif
  2822. {
  2823. ret = PemToDer(fileBuf, sz, CA_TYPE, &converted, 0, info,
  2824. &ecc);
  2825. #ifdef CYASSL_SMALL_STACK
  2826. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2827. #endif
  2828. }
  2829. }
  2830. if (ret == 0) {
  2831. if (converted.length < (word32)derSz) {
  2832. XMEMCPY(derBuf, converted.buffer, converted.length);
  2833. ret = converted.length;
  2834. }
  2835. else
  2836. ret = BUFFER_E;
  2837. }
  2838. XFREE(converted.buffer, 0, DYNAMIC_TYPE_CA);
  2839. }
  2840. XFCLOSE(file);
  2841. if (dynamic)
  2842. XFREE(fileBuf, 0, DYNAMIC_TYPE_FILE);
  2843. }
  2844. return ret;
  2845. }
  2846. #endif /* CYASSL_CERT_GEN */
  2847. int CyaSSL_CTX_use_certificate_file(CYASSL_CTX* ctx, const char* file,
  2848. int format)
  2849. {
  2850. CYASSL_ENTER("CyaSSL_CTX_use_certificate_file");
  2851. if (ProcessFile(ctx, file, format, CERT_TYPE, NULL, 0, NULL) == SSL_SUCCESS)
  2852. return SSL_SUCCESS;
  2853. return SSL_FAILURE;
  2854. }
  2855. int CyaSSL_CTX_use_PrivateKey_file(CYASSL_CTX* ctx, const char* file,int format)
  2856. {
  2857. CYASSL_ENTER("CyaSSL_CTX_use_PrivateKey_file");
  2858. if (ProcessFile(ctx, file, format, PRIVATEKEY_TYPE, NULL, 0, NULL)
  2859. == SSL_SUCCESS)
  2860. return SSL_SUCCESS;
  2861. return SSL_FAILURE;
  2862. }
  2863. int CyaSSL_CTX_use_certificate_chain_file(CYASSL_CTX* ctx, const char* file)
  2864. {
  2865. /* procces up to MAX_CHAIN_DEPTH plus subject cert */
  2866. CYASSL_ENTER("CyaSSL_CTX_use_certificate_chain_file");
  2867. if (ProcessFile(ctx, file, SSL_FILETYPE_PEM,CERT_TYPE,NULL,1, NULL)
  2868. == SSL_SUCCESS)
  2869. return SSL_SUCCESS;
  2870. return SSL_FAILURE;
  2871. }
  2872. #ifndef NO_DH
  2873. /* server wrapper for ctx or ssl Diffie-Hellman parameters */
  2874. static int CyaSSL_SetTmpDH_buffer_wrapper(CYASSL_CTX* ctx, CYASSL* ssl,
  2875. const unsigned char* buf, long sz, int format)
  2876. {
  2877. buffer der;
  2878. int ret = 0;
  2879. int weOwnDer = 0;
  2880. word32 pSz = MAX_DH_SIZE;
  2881. word32 gSz = MAX_DH_SIZE;
  2882. #ifdef CYASSL_SMALL_STACK
  2883. byte* p = NULL;
  2884. byte* g = NULL;
  2885. #else
  2886. byte p[MAX_DH_SIZE];
  2887. byte g[MAX_DH_SIZE];
  2888. #endif
  2889. der.buffer = (byte*)buf;
  2890. der.length = (word32)sz;
  2891. #ifdef CYASSL_SMALL_STACK
  2892. p = (byte*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2893. g = (byte*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2894. if (p == NULL || g == NULL) {
  2895. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2896. XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2897. return MEMORY_E;
  2898. }
  2899. #endif
  2900. if (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM)
  2901. ret = SSL_BAD_FILETYPE;
  2902. else {
  2903. if (format == SSL_FILETYPE_PEM) {
  2904. der.buffer = NULL;
  2905. ret = PemToDer(buf, sz, DH_PARAM_TYPE, &der, ctx->heap, NULL,NULL);
  2906. weOwnDer = 1;
  2907. }
  2908. if (ret == 0) {
  2909. if (DhParamsLoad(der.buffer, der.length, p, &pSz, g, &gSz) < 0)
  2910. ret = SSL_BAD_FILETYPE;
  2911. else if (ssl)
  2912. ret = CyaSSL_SetTmpDH(ssl, p, pSz, g, gSz);
  2913. else
  2914. ret = CyaSSL_CTX_SetTmpDH(ctx, p, pSz, g, gSz);
  2915. }
  2916. }
  2917. if (weOwnDer)
  2918. XFREE(der.buffer, ctx->heap, DYNAMIC_TYPE_KEY);
  2919. #ifdef CYASSL_SMALL_STACK
  2920. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2921. XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  2922. #endif
  2923. return ret;
  2924. }
  2925. /* server Diffie-Hellman parameters, SSL_SUCCESS on ok */
  2926. int CyaSSL_SetTmpDH_buffer(CYASSL* ssl, const unsigned char* buf, long sz,
  2927. int format)
  2928. {
  2929. return CyaSSL_SetTmpDH_buffer_wrapper(ssl->ctx, ssl, buf, sz, format);
  2930. }
  2931. /* server ctx Diffie-Hellman parameters, SSL_SUCCESS on ok */
  2932. int CyaSSL_CTX_SetTmpDH_buffer(CYASSL_CTX* ctx, const unsigned char* buf,
  2933. long sz, int format)
  2934. {
  2935. return CyaSSL_SetTmpDH_buffer_wrapper(ctx, NULL, buf, sz, format);
  2936. }
  2937. /* server Diffie-Hellman parameters */
  2938. static int CyaSSL_SetTmpDH_file_wrapper(CYASSL_CTX* ctx, CYASSL* ssl,
  2939. const char* fname, int format)
  2940. {
  2941. #ifdef CYASSL_SMALL_STACK
  2942. byte staticBuffer[1]; /* force heap usage */
  2943. #else
  2944. byte staticBuffer[FILE_BUFFER_SIZE];
  2945. #endif
  2946. byte* myBuffer = staticBuffer;
  2947. int dynamic = 0;
  2948. int ret;
  2949. long sz = 0;
  2950. XFILE file = XFOPEN(fname, "rb");
  2951. if (file == XBADFILE) return SSL_BAD_FILE;
  2952. XFSEEK(file, 0, XSEEK_END);
  2953. sz = XFTELL(file);
  2954. XREWIND(file);
  2955. if (sz > (long)sizeof(staticBuffer)) {
  2956. CYASSL_MSG("Getting dynamic buffer");
  2957. myBuffer = (byte*) XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
  2958. if (myBuffer == NULL) {
  2959. XFCLOSE(file);
  2960. return SSL_BAD_FILE;
  2961. }
  2962. dynamic = 1;
  2963. }
  2964. else if (sz < 0) {
  2965. XFCLOSE(file);
  2966. return SSL_BAD_FILE;
  2967. }
  2968. if ( (ret = (int)XFREAD(myBuffer, sz, 1, file)) < 0)
  2969. ret = SSL_BAD_FILE;
  2970. else {
  2971. if (ssl)
  2972. ret = CyaSSL_SetTmpDH_buffer(ssl, myBuffer, sz, format);
  2973. else
  2974. ret = CyaSSL_CTX_SetTmpDH_buffer(ctx, myBuffer, sz, format);
  2975. }
  2976. XFCLOSE(file);
  2977. if (dynamic)
  2978. XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
  2979. return ret;
  2980. }
  2981. /* server Diffie-Hellman parameters */
  2982. int CyaSSL_SetTmpDH_file(CYASSL* ssl, const char* fname, int format)
  2983. {
  2984. return CyaSSL_SetTmpDH_file_wrapper(ssl->ctx, ssl, fname, format);
  2985. }
  2986. /* server Diffie-Hellman parameters */
  2987. int CyaSSL_CTX_SetTmpDH_file(CYASSL_CTX* ctx, const char* fname, int format)
  2988. {
  2989. return CyaSSL_SetTmpDH_file_wrapper(ctx, NULL, fname, format);
  2990. }
  2991. /* server ctx Diffie-Hellman parameters, SSL_SUCCESS on ok */
  2992. int CyaSSL_CTX_SetTmpDH(CYASSL_CTX* ctx, const unsigned char* p, int pSz,
  2993. const unsigned char* g, int gSz)
  2994. {
  2995. CYASSL_ENTER("CyaSSL_CTX_SetTmpDH");
  2996. if (ctx == NULL || p == NULL || g == NULL) return BAD_FUNC_ARG;
  2997. XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  2998. XFREE(ctx->serverDH_G.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  2999. ctx->serverDH_P.buffer = (byte*)XMALLOC(pSz, ctx->heap,DYNAMIC_TYPE_DH);
  3000. if (ctx->serverDH_P.buffer == NULL)
  3001. return MEMORY_E;
  3002. ctx->serverDH_G.buffer = (byte*)XMALLOC(gSz, ctx->heap,DYNAMIC_TYPE_DH);
  3003. if (ctx->serverDH_G.buffer == NULL) {
  3004. XFREE(ctx->serverDH_P.buffer, ctx->heap, DYNAMIC_TYPE_DH);
  3005. return MEMORY_E;
  3006. }
  3007. ctx->serverDH_P.length = pSz;
  3008. ctx->serverDH_G.length = gSz;
  3009. XMEMCPY(ctx->serverDH_P.buffer, p, pSz);
  3010. XMEMCPY(ctx->serverDH_G.buffer, g, gSz);
  3011. ctx->haveDH = 1;
  3012. CYASSL_LEAVE("CyaSSL_CTX_SetTmpDH", 0);
  3013. return SSL_SUCCESS;
  3014. }
  3015. #endif /* NO_DH */
  3016. #ifdef OPENSSL_EXTRA
  3017. /* put SSL type in extra for now, not very common */
  3018. int CyaSSL_use_certificate_file(CYASSL* ssl, const char* file, int format)
  3019. {
  3020. CYASSL_ENTER("CyaSSL_use_certificate_file");
  3021. if (ProcessFile(ssl->ctx, file, format, CERT_TYPE, ssl, 0, NULL)
  3022. == SSL_SUCCESS)
  3023. return SSL_SUCCESS;
  3024. return SSL_FAILURE;
  3025. }
  3026. int CyaSSL_use_PrivateKey_file(CYASSL* ssl, const char* file, int format)
  3027. {
  3028. CYASSL_ENTER("CyaSSL_use_PrivateKey_file");
  3029. if (ProcessFile(ssl->ctx, file, format, PRIVATEKEY_TYPE, ssl, 0, NULL)
  3030. == SSL_SUCCESS)
  3031. return SSL_SUCCESS;
  3032. return SSL_FAILURE;
  3033. }
  3034. int CyaSSL_use_certificate_chain_file(CYASSL* ssl, const char* file)
  3035. {
  3036. /* procces up to MAX_CHAIN_DEPTH plus subject cert */
  3037. CYASSL_ENTER("CyaSSL_use_certificate_chain_file");
  3038. if (ProcessFile(ssl->ctx, file, SSL_FILETYPE_PEM, CERT_TYPE, ssl, 1, NULL)
  3039. == SSL_SUCCESS)
  3040. return SSL_SUCCESS;
  3041. return SSL_FAILURE;
  3042. }
  3043. #ifdef HAVE_ECC
  3044. /* Set Temp CTX EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
  3045. int CyaSSL_CTX_SetTmpEC_DHE_Sz(CYASSL_CTX* ctx, word16 sz)
  3046. {
  3047. if (ctx == NULL || sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
  3048. return BAD_FUNC_ARG;
  3049. ctx->eccTempKeySz = sz;
  3050. return SSL_SUCCESS;
  3051. }
  3052. /* Set Temp SSL EC-DHE size in octets, should be 20 - 66 for 160 - 521 bit */
  3053. int CyaSSL_SetTmpEC_DHE_Sz(CYASSL* ssl, word16 sz)
  3054. {
  3055. if (ssl == NULL || sz < ECC_MINSIZE || sz > ECC_MAXSIZE)
  3056. return BAD_FUNC_ARG;
  3057. ssl->eccTempKeySz = sz;
  3058. return SSL_SUCCESS;
  3059. }
  3060. #endif /* HAVE_ECC */
  3061. int CyaSSL_CTX_use_RSAPrivateKey_file(CYASSL_CTX* ctx,const char* file,
  3062. int format)
  3063. {
  3064. CYASSL_ENTER("SSL_CTX_use_RSAPrivateKey_file");
  3065. return CyaSSL_CTX_use_PrivateKey_file(ctx, file, format);
  3066. }
  3067. int CyaSSL_use_RSAPrivateKey_file(CYASSL* ssl, const char* file, int format)
  3068. {
  3069. CYASSL_ENTER("CyaSSL_use_RSAPrivateKey_file");
  3070. return CyaSSL_use_PrivateKey_file(ssl, file, format);
  3071. }
  3072. #endif /* OPENSSL_EXTRA */
  3073. #ifdef HAVE_NTRU
  3074. int CyaSSL_CTX_use_NTRUPrivateKey_file(CYASSL_CTX* ctx, const char* file)
  3075. {
  3076. CYASSL_ENTER("CyaSSL_CTX_use_NTRUPrivateKey_file");
  3077. if (ctx == NULL)
  3078. return SSL_FAILURE;
  3079. if (ProcessFile(ctx, file, SSL_FILETYPE_RAW, PRIVATEKEY_TYPE, NULL, 0, NULL)
  3080. == SSL_SUCCESS) {
  3081. ctx->haveNTRU = 1;
  3082. return SSL_SUCCESS;
  3083. }
  3084. return SSL_FAILURE;
  3085. }
  3086. #endif /* HAVE_NTRU */
  3087. #endif /* NO_FILESYSTEM */
  3088. void CyaSSL_CTX_set_verify(CYASSL_CTX* ctx, int mode, VerifyCallback vc)
  3089. {
  3090. CYASSL_ENTER("CyaSSL_CTX_set_verify");
  3091. if (mode & SSL_VERIFY_PEER) {
  3092. ctx->verifyPeer = 1;
  3093. ctx->verifyNone = 0; /* in case perviously set */
  3094. }
  3095. if (mode == SSL_VERIFY_NONE) {
  3096. ctx->verifyNone = 1;
  3097. ctx->verifyPeer = 0; /* in case previously set */
  3098. }
  3099. if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3100. ctx->failNoCert = 1;
  3101. ctx->verifyCallback = vc;
  3102. }
  3103. void CyaSSL_set_verify(CYASSL* ssl, int mode, VerifyCallback vc)
  3104. {
  3105. CYASSL_ENTER("CyaSSL_set_verify");
  3106. if (mode & SSL_VERIFY_PEER) {
  3107. ssl->options.verifyPeer = 1;
  3108. ssl->options.verifyNone = 0; /* in case perviously set */
  3109. }
  3110. if (mode == SSL_VERIFY_NONE) {
  3111. ssl->options.verifyNone = 1;
  3112. ssl->options.verifyPeer = 0; /* in case previously set */
  3113. }
  3114. if (mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)
  3115. ssl->options.failNoCert = 1;
  3116. ssl->verifyCallback = vc;
  3117. }
  3118. /* store user ctx for verify callback */
  3119. void CyaSSL_SetCertCbCtx(CYASSL* ssl, void* ctx)
  3120. {
  3121. CYASSL_ENTER("CyaSSL_SetCertCbCtx");
  3122. if (ssl)
  3123. ssl->verifyCbCtx = ctx;
  3124. }
  3125. /* store context CA Cache addition callback */
  3126. void CyaSSL_CTX_SetCACb(CYASSL_CTX* ctx, CallbackCACache cb)
  3127. {
  3128. if (ctx && ctx->cm)
  3129. ctx->cm->caCacheCallback = cb;
  3130. }
  3131. #if defined(PERSIST_CERT_CACHE)
  3132. #if !defined(NO_FILESYSTEM)
  3133. /* Persist cert cache to file */
  3134. int CyaSSL_CTX_save_cert_cache(CYASSL_CTX* ctx, const char* fname)
  3135. {
  3136. CYASSL_ENTER("CyaSSL_CTX_save_cert_cache");
  3137. if (ctx == NULL || fname == NULL)
  3138. return BAD_FUNC_ARG;
  3139. return CM_SaveCertCache(ctx->cm, fname);
  3140. }
  3141. /* Persist cert cache from file */
  3142. int CyaSSL_CTX_restore_cert_cache(CYASSL_CTX* ctx, const char* fname)
  3143. {
  3144. CYASSL_ENTER("CyaSSL_CTX_restore_cert_cache");
  3145. if (ctx == NULL || fname == NULL)
  3146. return BAD_FUNC_ARG;
  3147. return CM_RestoreCertCache(ctx->cm, fname);
  3148. }
  3149. #endif /* NO_FILESYSTEM */
  3150. /* Persist cert cache to memory */
  3151. int CyaSSL_CTX_memsave_cert_cache(CYASSL_CTX* ctx, void* mem, int sz, int* used)
  3152. {
  3153. CYASSL_ENTER("CyaSSL_CTX_memsave_cert_cache");
  3154. if (ctx == NULL || mem == NULL || used == NULL || sz <= 0)
  3155. return BAD_FUNC_ARG;
  3156. return CM_MemSaveCertCache(ctx->cm, mem, sz, used);
  3157. }
  3158. /* Restore cert cache from memory */
  3159. int CyaSSL_CTX_memrestore_cert_cache(CYASSL_CTX* ctx, const void* mem, int sz)
  3160. {
  3161. CYASSL_ENTER("CyaSSL_CTX_memrestore_cert_cache");
  3162. if (ctx == NULL || mem == NULL || sz <= 0)
  3163. return BAD_FUNC_ARG;
  3164. return CM_MemRestoreCertCache(ctx->cm, mem, sz);
  3165. }
  3166. /* get how big the the cert cache save buffer needs to be */
  3167. int CyaSSL_CTX_get_cert_cache_memsize(CYASSL_CTX* ctx)
  3168. {
  3169. CYASSL_ENTER("CyaSSL_CTX_get_cert_cache_memsize");
  3170. if (ctx == NULL)
  3171. return BAD_FUNC_ARG;
  3172. return CM_GetCertCacheMemSize(ctx->cm);
  3173. }
  3174. #endif /* PERSISTE_CERT_CACHE */
  3175. #endif /* !NO_CERTS */
  3176. #ifndef NO_SESSION_CACHE
  3177. CYASSL_SESSION* CyaSSL_get_session(CYASSL* ssl)
  3178. {
  3179. CYASSL_ENTER("SSL_get_session");
  3180. if (ssl)
  3181. return GetSession(ssl, 0);
  3182. return NULL;
  3183. }
  3184. int CyaSSL_set_session(CYASSL* ssl, CYASSL_SESSION* session)
  3185. {
  3186. CYASSL_ENTER("SSL_set_session");
  3187. if (session)
  3188. return SetSession(ssl, session);
  3189. return SSL_FAILURE;
  3190. }
  3191. #ifndef NO_CLIENT_CACHE
  3192. /* Associate client session with serverID, find existing or store for saving
  3193. if newSession flag on, don't reuse existing session
  3194. SSL_SUCCESS on ok */
  3195. int CyaSSL_SetServerID(CYASSL* ssl, const byte* id, int len, int newSession)
  3196. {
  3197. CYASSL_SESSION* session = NULL;
  3198. CYASSL_ENTER("CyaSSL_SetServerID");
  3199. if (ssl == NULL || id == NULL || len <= 0)
  3200. return BAD_FUNC_ARG;
  3201. if (newSession == 0) {
  3202. session = GetSessionClient(ssl, id, len);
  3203. if (session) {
  3204. if (SetSession(ssl, session) != SSL_SUCCESS) {
  3205. CYASSL_MSG("SetSession failed");
  3206. session = NULL;
  3207. }
  3208. }
  3209. }
  3210. if (session == NULL) {
  3211. CYASSL_MSG("Valid ServerID not cached already");
  3212. ssl->session.idLen = (word16)min(SERVER_ID_LEN, (word32)len);
  3213. XMEMCPY(ssl->session.serverID, id, ssl->session.idLen);
  3214. }
  3215. return SSL_SUCCESS;
  3216. }
  3217. #endif /* NO_CLIENT_CACHE */
  3218. #if defined(PERSIST_SESSION_CACHE)
  3219. /* for persistance, if changes to layout need to increment and modify
  3220. save_session_cache() and restore_session_cache and memory versions too */
  3221. #define CYASSL_CACHE_VERSION 2
  3222. /* Session Cache Header information */
  3223. typedef struct {
  3224. int version; /* cache layout version id */
  3225. int rows; /* session rows */
  3226. int columns; /* session columns */
  3227. int sessionSz; /* sizeof CYASSL_SESSION */
  3228. } cache_header_t;
  3229. /* current persistence layout is:
  3230. 1) cache_header_t
  3231. 2) SessionCache
  3232. 3) ClientCache
  3233. update CYASSL_CACHE_VERSION if change layout for the following
  3234. PERSISTENT_SESSION_CACHE functions
  3235. */
  3236. /* get how big the the session cache save buffer needs to be */
  3237. int CyaSSL_get_session_cache_memsize(void)
  3238. {
  3239. int sz = (int)(sizeof(SessionCache) + sizeof(cache_header_t));
  3240. #ifndef NO_CLIENT_CACHE
  3241. sz += (int)(sizeof(ClientCache));
  3242. #endif
  3243. return sz;
  3244. }
  3245. /* Persist session cache to memory */
  3246. int CyaSSL_memsave_session_cache(void* mem, int sz)
  3247. {
  3248. int i;
  3249. cache_header_t cache_header;
  3250. SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
  3251. #ifndef NO_CLIENT_CACHE
  3252. ClientRow* clRow;
  3253. #endif
  3254. CYASSL_ENTER("CyaSSL_memsave_session_cache");
  3255. if (sz < CyaSSL_get_session_cache_memsize()) {
  3256. CYASSL_MSG("Memory buffer too small");
  3257. return BUFFER_E;
  3258. }
  3259. cache_header.version = CYASSL_CACHE_VERSION;
  3260. cache_header.rows = SESSION_ROWS;
  3261. cache_header.columns = SESSIONS_PER_ROW;
  3262. cache_header.sessionSz = (int)sizeof(CYASSL_SESSION);
  3263. XMEMCPY(mem, &cache_header, sizeof(cache_header));
  3264. if (LockMutex(&session_mutex) != 0) {
  3265. CYASSL_MSG("Session cache mutex lock failed");
  3266. return BAD_MUTEX_E;
  3267. }
  3268. for (i = 0; i < cache_header.rows; ++i)
  3269. XMEMCPY(row++, SessionCache + i, sizeof(SessionRow));
  3270. #ifndef NO_CLIENT_CACHE
  3271. clRow = (ClientRow*)row;
  3272. for (i = 0; i < cache_header.rows; ++i)
  3273. XMEMCPY(clRow++, ClientCache + i, sizeof(ClientRow));
  3274. #endif
  3275. UnLockMutex(&session_mutex);
  3276. CYASSL_LEAVE("CyaSSL_memsave_session_cache", SSL_SUCCESS);
  3277. return SSL_SUCCESS;
  3278. }
  3279. /* Restore the persistant session cache from memory */
  3280. int CyaSSL_memrestore_session_cache(const void* mem, int sz)
  3281. {
  3282. int i;
  3283. cache_header_t cache_header;
  3284. SessionRow* row = (SessionRow*)((byte*)mem + sizeof(cache_header));
  3285. #ifndef NO_CLIENT_CACHE
  3286. ClientRow* clRow;
  3287. #endif
  3288. CYASSL_ENTER("CyaSSL_memrestore_session_cache");
  3289. if (sz < CyaSSL_get_session_cache_memsize()) {
  3290. CYASSL_MSG("Memory buffer too small");
  3291. return BUFFER_E;
  3292. }
  3293. XMEMCPY(&cache_header, mem, sizeof(cache_header));
  3294. if (cache_header.version != CYASSL_CACHE_VERSION ||
  3295. cache_header.rows != SESSION_ROWS ||
  3296. cache_header.columns != SESSIONS_PER_ROW ||
  3297. cache_header.sessionSz != (int)sizeof(CYASSL_SESSION)) {
  3298. CYASSL_MSG("Session cache header match failed");
  3299. return CACHE_MATCH_ERROR;
  3300. }
  3301. if (LockMutex(&session_mutex) != 0) {
  3302. CYASSL_MSG("Session cache mutex lock failed");
  3303. return BAD_MUTEX_E;
  3304. }
  3305. for (i = 0; i < cache_header.rows; ++i)
  3306. XMEMCPY(SessionCache + i, row++, sizeof(SessionRow));
  3307. #ifndef NO_CLIENT_CACHE
  3308. clRow = (ClientRow*)row;
  3309. for (i = 0; i < cache_header.rows; ++i)
  3310. XMEMCPY(ClientCache + i, clRow++, sizeof(ClientRow));
  3311. #endif
  3312. UnLockMutex(&session_mutex);
  3313. CYASSL_LEAVE("CyaSSL_memrestore_session_cache", SSL_SUCCESS);
  3314. return SSL_SUCCESS;
  3315. }
  3316. #if !defined(NO_FILESYSTEM)
  3317. /* Persist session cache to file */
  3318. /* doesn't use memsave because of additional memory use */
  3319. int CyaSSL_save_session_cache(const char *fname)
  3320. {
  3321. XFILE file;
  3322. int ret;
  3323. int rc = SSL_SUCCESS;
  3324. int i;
  3325. cache_header_t cache_header;
  3326. CYASSL_ENTER("CyaSSL_save_session_cache");
  3327. file = XFOPEN(fname, "w+b");
  3328. if (file == XBADFILE) {
  3329. CYASSL_MSG("Couldn't open session cache save file");
  3330. return SSL_BAD_FILE;
  3331. }
  3332. cache_header.version = CYASSL_CACHE_VERSION;
  3333. cache_header.rows = SESSION_ROWS;
  3334. cache_header.columns = SESSIONS_PER_ROW;
  3335. cache_header.sessionSz = (int)sizeof(CYASSL_SESSION);
  3336. /* cache header */
  3337. ret = (int)XFWRITE(&cache_header, sizeof cache_header, 1, file);
  3338. if (ret != 1) {
  3339. CYASSL_MSG("Session cache header file write failed");
  3340. XFCLOSE(file);
  3341. return FWRITE_ERROR;
  3342. }
  3343. if (LockMutex(&session_mutex) != 0) {
  3344. CYASSL_MSG("Session cache mutex lock failed");
  3345. XFCLOSE(file);
  3346. return BAD_MUTEX_E;
  3347. }
  3348. /* session cache */
  3349. for (i = 0; i < cache_header.rows; ++i) {
  3350. ret = (int)XFWRITE(SessionCache + i, sizeof(SessionRow), 1, file);
  3351. if (ret != 1) {
  3352. CYASSL_MSG("Session cache member file write failed");
  3353. rc = FWRITE_ERROR;
  3354. break;
  3355. }
  3356. }
  3357. #ifndef NO_CLIENT_CACHE
  3358. /* client cache */
  3359. for (i = 0; i < cache_header.rows; ++i) {
  3360. ret = (int)XFWRITE(ClientCache + i, sizeof(ClientRow), 1, file);
  3361. if (ret != 1) {
  3362. CYASSL_MSG("Client cache member file write failed");
  3363. rc = FWRITE_ERROR;
  3364. break;
  3365. }
  3366. }
  3367. #endif /* NO_CLIENT_CACHE */
  3368. UnLockMutex(&session_mutex);
  3369. XFCLOSE(file);
  3370. CYASSL_LEAVE("CyaSSL_save_session_cache", rc);
  3371. return rc;
  3372. }
  3373. /* Restore the persistant session cache from file */
  3374. /* doesn't use memstore because of additional memory use */
  3375. int CyaSSL_restore_session_cache(const char *fname)
  3376. {
  3377. XFILE file;
  3378. int rc = SSL_SUCCESS;
  3379. int ret;
  3380. int i;
  3381. cache_header_t cache_header;
  3382. CYASSL_ENTER("CyaSSL_restore_session_cache");
  3383. file = XFOPEN(fname, "rb");
  3384. if (file == XBADFILE) {
  3385. CYASSL_MSG("Couldn't open session cache save file");
  3386. return SSL_BAD_FILE;
  3387. }
  3388. /* cache header */
  3389. ret = (int)XFREAD(&cache_header, sizeof cache_header, 1, file);
  3390. if (ret != 1) {
  3391. CYASSL_MSG("Session cache header file read failed");
  3392. XFCLOSE(file);
  3393. return FREAD_ERROR;
  3394. }
  3395. if (cache_header.version != CYASSL_CACHE_VERSION ||
  3396. cache_header.rows != SESSION_ROWS ||
  3397. cache_header.columns != SESSIONS_PER_ROW ||
  3398. cache_header.sessionSz != (int)sizeof(CYASSL_SESSION)) {
  3399. CYASSL_MSG("Session cache header match failed");
  3400. XFCLOSE(file);
  3401. return CACHE_MATCH_ERROR;
  3402. }
  3403. if (LockMutex(&session_mutex) != 0) {
  3404. CYASSL_MSG("Session cache mutex lock failed");
  3405. XFCLOSE(file);
  3406. return BAD_MUTEX_E;
  3407. }
  3408. /* session cache */
  3409. for (i = 0; i < cache_header.rows; ++i) {
  3410. ret = (int)XFREAD(SessionCache + i, sizeof(SessionRow), 1, file);
  3411. if (ret != 1) {
  3412. CYASSL_MSG("Session cache member file read failed");
  3413. XMEMSET(SessionCache, 0, sizeof SessionCache);
  3414. rc = FREAD_ERROR;
  3415. break;
  3416. }
  3417. }
  3418. #ifndef NO_CLIENT_CACHE
  3419. /* client cache */
  3420. for (i = 0; i < cache_header.rows; ++i) {
  3421. ret = (int)XFREAD(ClientCache + i, sizeof(ClientRow), 1, file);
  3422. if (ret != 1) {
  3423. CYASSL_MSG("Client cache member file read failed");
  3424. XMEMSET(ClientCache, 0, sizeof ClientCache);
  3425. rc = FREAD_ERROR;
  3426. break;
  3427. }
  3428. }
  3429. #endif /* NO_CLIENT_CACHE */
  3430. UnLockMutex(&session_mutex);
  3431. XFCLOSE(file);
  3432. CYASSL_LEAVE("CyaSSL_restore_session_cache", rc);
  3433. return rc;
  3434. }
  3435. #endif /* !NO_FILESYSTEM */
  3436. #endif /* PERSIST_SESSION_CACHE */
  3437. #endif /* NO_SESSION_CACHE */
  3438. void CyaSSL_load_error_strings(void) /* compatibility only */
  3439. {}
  3440. int CyaSSL_library_init(void)
  3441. {
  3442. CYASSL_ENTER("SSL_library_init");
  3443. if (CyaSSL_Init() == SSL_SUCCESS)
  3444. return SSL_SUCCESS;
  3445. else
  3446. return SSL_FATAL_ERROR;
  3447. }
  3448. #ifdef HAVE_SECRET_CALLBACK
  3449. int CyaSSL_set_session_secret_cb(CYASSL* ssl, SessionSecretCb cb, void* ctx)
  3450. {
  3451. CYASSL_ENTER("CyaSSL_set_session_secret_cb");
  3452. if (ssl == NULL)
  3453. return SSL_FATAL_ERROR;
  3454. ssl->sessionSecretCb = cb;
  3455. ssl->sessionSecretCtx = ctx;
  3456. /* If using a pre-set key, assume session resumption. */
  3457. ssl->session.sessionIDSz = 0;
  3458. ssl->options.resuming = 1;
  3459. return SSL_SUCCESS;
  3460. }
  3461. #endif
  3462. #ifndef NO_SESSION_CACHE
  3463. /* on by default if built in but allow user to turn off */
  3464. long CyaSSL_CTX_set_session_cache_mode(CYASSL_CTX* ctx, long mode)
  3465. {
  3466. CYASSL_ENTER("SSL_CTX_set_session_cache_mode");
  3467. if (mode == SSL_SESS_CACHE_OFF)
  3468. ctx->sessionCacheOff = 1;
  3469. if (mode == SSL_SESS_CACHE_NO_AUTO_CLEAR)
  3470. ctx->sessionCacheFlushOff = 1;
  3471. return SSL_SUCCESS;
  3472. }
  3473. #endif /* NO_SESSION_CACHE */
  3474. #if !defined(NO_CERTS)
  3475. #if defined(PERSIST_CERT_CACHE)
  3476. #define CYASSL_CACHE_CERT_VERSION 1
  3477. typedef struct {
  3478. int version; /* cache cert layout version id */
  3479. int rows; /* hash table rows, CA_TABLE_SIZE */
  3480. int columns[CA_TABLE_SIZE]; /* columns per row on list */
  3481. int signerSz; /* sizeof Signer object */
  3482. } CertCacheHeader;
  3483. /* current cert persistance layout is:
  3484. 1) CertCacheHeader
  3485. 2) caTable
  3486. update CYASSL_CERT_CACHE_VERSION if change layout for the following
  3487. PERSIST_CERT_CACHE functions
  3488. */
  3489. /* Return memory needed to persist this signer, have lock */
  3490. static INLINE int GetSignerMemory(Signer* signer)
  3491. {
  3492. int sz = sizeof(signer->pubKeySize) + sizeof(signer->keyOID)
  3493. + sizeof(signer->nameLen) + sizeof(signer->subjectNameHash);
  3494. #if !defined(NO_SKID)
  3495. sz += (int)sizeof(signer->subjectKeyIdHash);
  3496. #endif
  3497. /* add dynamic bytes needed */
  3498. sz += signer->pubKeySize;
  3499. sz += signer->nameLen;
  3500. return sz;
  3501. }
  3502. /* Return memory needed to persist this row, have lock */
  3503. static INLINE int GetCertCacheRowMemory(Signer* row)
  3504. {
  3505. int sz = 0;
  3506. while (row) {
  3507. sz += GetSignerMemory(row);
  3508. row = row->next;
  3509. }
  3510. return sz;
  3511. }
  3512. /* get the size of persist cert cache, have lock */
  3513. static INLINE int GetCertCacheMemSize(CYASSL_CERT_MANAGER* cm)
  3514. {
  3515. int sz;
  3516. int i;
  3517. sz = sizeof(CertCacheHeader);
  3518. for (i = 0; i < CA_TABLE_SIZE; i++)
  3519. sz += GetCertCacheRowMemory(cm->caTable[i]);
  3520. return sz;
  3521. }
  3522. /* Store cert cache header columns with number of items per list, have lock */
  3523. static INLINE void SetCertHeaderColumns(CYASSL_CERT_MANAGER* cm, int* columns)
  3524. {
  3525. int i;
  3526. Signer* row;
  3527. for (i = 0; i < CA_TABLE_SIZE; i++) {
  3528. int count = 0;
  3529. row = cm->caTable[i];
  3530. while (row) {
  3531. ++count;
  3532. row = row->next;
  3533. }
  3534. columns[i] = count;
  3535. }
  3536. }
  3537. /* Restore whole cert row from memory, have lock, return bytes consumed,
  3538. < 0 on error, have lock */
  3539. static INLINE int RestoreCertRow(CYASSL_CERT_MANAGER* cm, byte* current,
  3540. int row, int listSz, const byte* end)
  3541. {
  3542. int idx = 0;
  3543. if (listSz < 0) {
  3544. CYASSL_MSG("Row header corrupted, negative value");
  3545. return PARSE_ERROR;
  3546. }
  3547. while (listSz) {
  3548. Signer* signer;
  3549. byte* start = current + idx; /* for end checks on this signer */
  3550. int minSz = sizeof(signer->pubKeySize) + sizeof(signer->keyOID) +
  3551. sizeof(signer->nameLen) + sizeof(signer->subjectNameHash);
  3552. #ifndef NO_SKID
  3553. minSz += (int)sizeof(signer->subjectKeyIdHash);
  3554. #endif
  3555. if (start + minSz > end) {
  3556. CYASSL_MSG("Would overread restore buffer");
  3557. return BUFFER_E;
  3558. }
  3559. signer = MakeSigner(cm->heap);
  3560. if (signer == NULL)
  3561. return MEMORY_E;
  3562. /* pubKeySize */
  3563. XMEMCPY(&signer->pubKeySize, current + idx, sizeof(signer->pubKeySize));
  3564. idx += (int)sizeof(signer->pubKeySize);
  3565. /* keyOID */
  3566. XMEMCPY(&signer->keyOID, current + idx, sizeof(signer->keyOID));
  3567. idx += (int)sizeof(signer->keyOID);
  3568. /* pulicKey */
  3569. if (start + minSz + signer->pubKeySize > end) {
  3570. CYASSL_MSG("Would overread restore buffer");
  3571. FreeSigner(signer, cm->heap);
  3572. return BUFFER_E;
  3573. }
  3574. signer->publicKey = (byte*)XMALLOC(signer->pubKeySize, cm->heap,
  3575. DYNAMIC_TYPE_KEY);
  3576. if (signer->publicKey == NULL) {
  3577. FreeSigner(signer, cm->heap);
  3578. return MEMORY_E;
  3579. }
  3580. XMEMCPY(signer->publicKey, current + idx, signer->pubKeySize);
  3581. idx += signer->pubKeySize;
  3582. /* nameLen */
  3583. XMEMCPY(&signer->nameLen, current + idx, sizeof(signer->nameLen));
  3584. idx += (int)sizeof(signer->nameLen);
  3585. /* name */
  3586. if (start + minSz + signer->pubKeySize + signer->nameLen > end) {
  3587. CYASSL_MSG("Would overread restore buffer");
  3588. FreeSigner(signer, cm->heap);
  3589. return BUFFER_E;
  3590. }
  3591. signer->name = (char*)XMALLOC(signer->nameLen, cm->heap,
  3592. DYNAMIC_TYPE_SUBJECT_CN);
  3593. if (signer->name == NULL) {
  3594. FreeSigner(signer, cm->heap);
  3595. return MEMORY_E;
  3596. }
  3597. XMEMCPY(signer->name, current + idx, signer->nameLen);
  3598. idx += signer->nameLen;
  3599. /* subjectNameHash */
  3600. XMEMCPY(signer->subjectNameHash, current + idx, SIGNER_DIGEST_SIZE);
  3601. idx += SIGNER_DIGEST_SIZE;
  3602. #ifndef NO_SKID
  3603. /* subjectKeyIdHash */
  3604. XMEMCPY(signer->subjectKeyIdHash, current + idx,SIGNER_DIGEST_SIZE);
  3605. idx += SIGNER_DIGEST_SIZE;
  3606. #endif
  3607. signer->next = cm->caTable[row];
  3608. cm->caTable[row] = signer;
  3609. --listSz;
  3610. }
  3611. return idx;
  3612. }
  3613. /* Store whole cert row into memory, have lock, return bytes added */
  3614. static INLINE int StoreCertRow(CYASSL_CERT_MANAGER* cm, byte* current, int row)
  3615. {
  3616. int added = 0;
  3617. Signer* list = cm->caTable[row];
  3618. while (list) {
  3619. XMEMCPY(current + added, &list->pubKeySize, sizeof(list->pubKeySize));
  3620. added += (int)sizeof(list->pubKeySize);
  3621. XMEMCPY(current + added, &list->keyOID, sizeof(list->keyOID));
  3622. added += (int)sizeof(list->keyOID);
  3623. XMEMCPY(current + added, list->publicKey, list->pubKeySize);
  3624. added += list->pubKeySize;
  3625. XMEMCPY(current + added, &list->nameLen, sizeof(list->nameLen));
  3626. added += (int)sizeof(list->nameLen);
  3627. XMEMCPY(current + added, list->name, list->nameLen);
  3628. added += list->nameLen;
  3629. XMEMCPY(current + added, list->subjectNameHash, SIGNER_DIGEST_SIZE);
  3630. added += SIGNER_DIGEST_SIZE;
  3631. #ifndef NO_SKID
  3632. XMEMCPY(current + added, list->subjectKeyIdHash,SIGNER_DIGEST_SIZE);
  3633. added += SIGNER_DIGEST_SIZE;
  3634. #endif
  3635. list = list->next;
  3636. }
  3637. return added;
  3638. }
  3639. /* Persist cert cache to memory, have lock */
  3640. static INLINE int DoMemSaveCertCache(CYASSL_CERT_MANAGER* cm, void* mem, int sz)
  3641. {
  3642. int realSz;
  3643. int ret = SSL_SUCCESS;
  3644. int i;
  3645. CYASSL_ENTER("DoMemSaveCertCache");
  3646. realSz = GetCertCacheMemSize(cm);
  3647. if (realSz > sz) {
  3648. CYASSL_MSG("Mem output buffer too small");
  3649. ret = BUFFER_E;
  3650. }
  3651. else {
  3652. byte* current;
  3653. CertCacheHeader hdr;
  3654. hdr.version = CYASSL_CACHE_CERT_VERSION;
  3655. hdr.rows = CA_TABLE_SIZE;
  3656. SetCertHeaderColumns(cm, hdr.columns);
  3657. hdr.signerSz = (int)sizeof(Signer);
  3658. XMEMCPY(mem, &hdr, sizeof(CertCacheHeader));
  3659. current = (byte*)mem + sizeof(CertCacheHeader);
  3660. for (i = 0; i < CA_TABLE_SIZE; ++i)
  3661. current += StoreCertRow(cm, current, i);
  3662. }
  3663. return ret;
  3664. }
  3665. #if !defined(NO_FILESYSTEM)
  3666. /* Persist cert cache to file */
  3667. int CM_SaveCertCache(CYASSL_CERT_MANAGER* cm, const char* fname)
  3668. {
  3669. XFILE file;
  3670. int rc = SSL_SUCCESS;
  3671. int memSz;
  3672. byte* mem;
  3673. CYASSL_ENTER("CM_SaveCertCache");
  3674. file = XFOPEN(fname, "w+b");
  3675. if (file == XBADFILE) {
  3676. CYASSL_MSG("Couldn't open cert cache save file");
  3677. return SSL_BAD_FILE;
  3678. }
  3679. if (LockMutex(&cm->caLock) != 0) {
  3680. CYASSL_MSG("LockMutex on caLock failed");
  3681. XFCLOSE(file);
  3682. return BAD_MUTEX_E;
  3683. }
  3684. memSz = GetCertCacheMemSize(cm);
  3685. mem = (byte*)XMALLOC(memSz, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
  3686. if (mem == NULL) {
  3687. CYASSL_MSG("Alloc for tmp buffer failed");
  3688. rc = MEMORY_E;
  3689. } else {
  3690. rc = DoMemSaveCertCache(cm, mem, memSz);
  3691. if (rc == SSL_SUCCESS) {
  3692. int ret = (int)XFWRITE(mem, memSz, 1, file);
  3693. if (ret != 1) {
  3694. CYASSL_MSG("Cert cache file write failed");
  3695. rc = FWRITE_ERROR;
  3696. }
  3697. }
  3698. XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
  3699. }
  3700. UnLockMutex(&cm->caLock);
  3701. XFCLOSE(file);
  3702. return rc;
  3703. }
  3704. /* Restore cert cache from file */
  3705. int CM_RestoreCertCache(CYASSL_CERT_MANAGER* cm, const char* fname)
  3706. {
  3707. XFILE file;
  3708. int rc = SSL_SUCCESS;
  3709. int ret;
  3710. int memSz;
  3711. byte* mem;
  3712. CYASSL_ENTER("CM_RestoreCertCache");
  3713. file = XFOPEN(fname, "rb");
  3714. if (file == XBADFILE) {
  3715. CYASSL_MSG("Couldn't open cert cache save file");
  3716. return SSL_BAD_FILE;
  3717. }
  3718. XFSEEK(file, 0, XSEEK_END);
  3719. memSz = (int)XFTELL(file);
  3720. XREWIND(file);
  3721. if (memSz <= 0) {
  3722. CYASSL_MSG("Bad file size");
  3723. XFCLOSE(file);
  3724. return SSL_BAD_FILE;
  3725. }
  3726. mem = (byte*)XMALLOC(memSz, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
  3727. if (mem == NULL) {
  3728. CYASSL_MSG("Alloc for tmp buffer failed");
  3729. XFCLOSE(file);
  3730. return MEMORY_E;
  3731. }
  3732. ret = (int)XFREAD(mem, memSz, 1, file);
  3733. if (ret != 1) {
  3734. CYASSL_MSG("Cert file read error");
  3735. rc = FREAD_ERROR;
  3736. } else {
  3737. rc = CM_MemRestoreCertCache(cm, mem, memSz);
  3738. if (rc != SSL_SUCCESS) {
  3739. CYASSL_MSG("Mem restore cert cache failed");
  3740. }
  3741. }
  3742. XFREE(mem, cm->heap, DYNAMIC_TYPE_TMP_BUFFER);
  3743. XFCLOSE(file);
  3744. return rc;
  3745. }
  3746. #endif /* NO_FILESYSTEM */
  3747. /* Persist cert cache to memory */
  3748. int CM_MemSaveCertCache(CYASSL_CERT_MANAGER* cm, void* mem, int sz, int* used)
  3749. {
  3750. int ret = SSL_SUCCESS;
  3751. CYASSL_ENTER("CM_MemSaveCertCache");
  3752. if (LockMutex(&cm->caLock) != 0) {
  3753. CYASSL_MSG("LockMutex on caLock failed");
  3754. return BAD_MUTEX_E;
  3755. }
  3756. ret = DoMemSaveCertCache(cm, mem, sz);
  3757. if (ret == SSL_SUCCESS)
  3758. *used = GetCertCacheMemSize(cm);
  3759. UnLockMutex(&cm->caLock);
  3760. return ret;
  3761. }
  3762. /* Restore cert cache from memory */
  3763. int CM_MemRestoreCertCache(CYASSL_CERT_MANAGER* cm, const void* mem, int sz)
  3764. {
  3765. int ret = SSL_SUCCESS;
  3766. int i;
  3767. CertCacheHeader* hdr = (CertCacheHeader*)mem;
  3768. byte* current = (byte*)mem + sizeof(CertCacheHeader);
  3769. byte* end = (byte*)mem + sz; /* don't go over */
  3770. CYASSL_ENTER("CM_MemRestoreCertCache");
  3771. if (current > end) {
  3772. CYASSL_MSG("Cert Cache Memory buffer too small");
  3773. return BUFFER_E;
  3774. }
  3775. if (hdr->version != CYASSL_CACHE_CERT_VERSION ||
  3776. hdr->rows != CA_TABLE_SIZE ||
  3777. hdr->signerSz != (int)sizeof(Signer)) {
  3778. CYASSL_MSG("Cert Cache Memory header mismatch");
  3779. return CACHE_MATCH_ERROR;
  3780. }
  3781. if (LockMutex(&cm->caLock) != 0) {
  3782. CYASSL_MSG("LockMutex on caLock failed");
  3783. return BAD_MUTEX_E;
  3784. }
  3785. FreeSignerTable(cm->caTable, CA_TABLE_SIZE, cm->heap);
  3786. for (i = 0; i < CA_TABLE_SIZE; ++i) {
  3787. int added = RestoreCertRow(cm, current, i, hdr->columns[i], end);
  3788. if (added < 0) {
  3789. CYASSL_MSG("RestoreCertRow error");
  3790. ret = added;
  3791. break;
  3792. }
  3793. current += added;
  3794. }
  3795. UnLockMutex(&cm->caLock);
  3796. return ret;
  3797. }
  3798. /* get how big the the cert cache save buffer needs to be */
  3799. int CM_GetCertCacheMemSize(CYASSL_CERT_MANAGER* cm)
  3800. {
  3801. int sz;
  3802. CYASSL_ENTER("CM_GetCertCacheMemSize");
  3803. if (LockMutex(&cm->caLock) != 0) {
  3804. CYASSL_MSG("LockMutex on caLock failed");
  3805. return BAD_MUTEX_E;
  3806. }
  3807. sz = GetCertCacheMemSize(cm);
  3808. UnLockMutex(&cm->caLock);
  3809. return sz;
  3810. }
  3811. #endif /* PERSIST_CERT_CACHE */
  3812. #endif /* NO_CERTS */
  3813. int CyaSSL_CTX_set_cipher_list(CYASSL_CTX* ctx, const char* list)
  3814. {
  3815. CYASSL_ENTER("CyaSSL_CTX_set_cipher_list");
  3816. return (SetCipherList(&ctx->suites, list)) ? SSL_SUCCESS : SSL_FAILURE;
  3817. }
  3818. int CyaSSL_set_cipher_list(CYASSL* ssl, const char* list)
  3819. {
  3820. CYASSL_ENTER("CyaSSL_set_cipher_list");
  3821. return (SetCipherList(ssl->suites, list)) ? SSL_SUCCESS : SSL_FAILURE;
  3822. }
  3823. #ifndef CYASSL_LEANPSK
  3824. #ifdef CYASSL_DTLS
  3825. int CyaSSL_dtls_get_current_timeout(CYASSL* ssl)
  3826. {
  3827. (void)ssl;
  3828. return ssl->dtls_timeout;
  3829. }
  3830. /* user may need to alter init dtls recv timeout, SSL_SUCCESS on ok */
  3831. int CyaSSL_dtls_set_timeout_init(CYASSL* ssl, int timeout)
  3832. {
  3833. if (ssl == NULL || timeout < 0)
  3834. return BAD_FUNC_ARG;
  3835. if (timeout > ssl->dtls_timeout_max) {
  3836. CYASSL_MSG("Can't set dtls timeout init greater than dtls timeout max");
  3837. return BAD_FUNC_ARG;
  3838. }
  3839. ssl->dtls_timeout_init = timeout;
  3840. ssl->dtls_timeout = timeout;
  3841. return SSL_SUCCESS;
  3842. }
  3843. /* user may need to alter max dtls recv timeout, SSL_SUCCESS on ok */
  3844. int CyaSSL_dtls_set_timeout_max(CYASSL* ssl, int timeout)
  3845. {
  3846. if (ssl == NULL || timeout < 0)
  3847. return BAD_FUNC_ARG;
  3848. if (timeout < ssl->dtls_timeout_init) {
  3849. CYASSL_MSG("Can't set dtls timeout max less than dtls timeout init");
  3850. return BAD_FUNC_ARG;
  3851. }
  3852. ssl->dtls_timeout_max = timeout;
  3853. return SSL_SUCCESS;
  3854. }
  3855. int CyaSSL_dtls_got_timeout(CYASSL* ssl)
  3856. {
  3857. int result = SSL_SUCCESS;
  3858. DtlsMsgListDelete(ssl->dtls_msg_list, ssl->heap);
  3859. ssl->dtls_msg_list = NULL;
  3860. if (DtlsPoolTimeout(ssl) < 0 || DtlsPoolSend(ssl) < 0) {
  3861. result = SSL_FATAL_ERROR;
  3862. }
  3863. return result;
  3864. }
  3865. #endif /* DTLS */
  3866. #endif /* LEANPSK */
  3867. /* client only parts */
  3868. #ifndef NO_CYASSL_CLIENT
  3869. #ifndef NO_OLD_TLS
  3870. CYASSL_METHOD* CyaSSLv3_client_method(void)
  3871. {
  3872. CYASSL_METHOD* method =
  3873. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  3874. DYNAMIC_TYPE_METHOD);
  3875. CYASSL_ENTER("SSLv3_client_method");
  3876. if (method)
  3877. InitSSL_Method(method, MakeSSLv3());
  3878. return method;
  3879. }
  3880. #endif
  3881. #ifdef CYASSL_DTLS
  3882. CYASSL_METHOD* CyaDTLSv1_client_method(void)
  3883. {
  3884. CYASSL_METHOD* method =
  3885. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  3886. DYNAMIC_TYPE_METHOD);
  3887. CYASSL_ENTER("DTLSv1_client_method");
  3888. if (method)
  3889. InitSSL_Method(method, MakeDTLSv1());
  3890. return method;
  3891. }
  3892. CYASSL_METHOD* CyaDTLSv1_2_client_method(void)
  3893. {
  3894. CYASSL_METHOD* method =
  3895. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  3896. DYNAMIC_TYPE_METHOD);
  3897. CYASSL_ENTER("DTLSv1_2_client_method");
  3898. if (method)
  3899. InitSSL_Method(method, MakeDTLSv1_2());
  3900. return method;
  3901. }
  3902. #endif
  3903. /* please see note at top of README if you get an error from connect */
  3904. int CyaSSL_connect(CYASSL* ssl)
  3905. {
  3906. int neededState;
  3907. CYASSL_ENTER("SSL_connect()");
  3908. #ifdef HAVE_ERRNO_H
  3909. errno = 0;
  3910. #endif
  3911. if (ssl->options.side != CYASSL_CLIENT_END) {
  3912. CYASSL_ERROR(ssl->error = SIDE_ERROR);
  3913. return SSL_FATAL_ERROR;
  3914. }
  3915. #ifdef CYASSL_DTLS
  3916. if (ssl->version.major == DTLS_MAJOR) {
  3917. ssl->options.dtls = 1;
  3918. ssl->options.tls = 1;
  3919. ssl->options.tls1_1 = 1;
  3920. if (DtlsPoolInit(ssl) != 0) {
  3921. ssl->error = MEMORY_ERROR;
  3922. CYASSL_ERROR(ssl->error);
  3923. return SSL_FATAL_ERROR;
  3924. }
  3925. }
  3926. #endif
  3927. if (ssl->buffers.outputBuffer.length > 0) {
  3928. if ( (ssl->error = SendBuffered(ssl)) == 0) {
  3929. ssl->options.connectState++;
  3930. CYASSL_MSG("connect state: Advanced from buffered send");
  3931. }
  3932. else {
  3933. CYASSL_ERROR(ssl->error);
  3934. return SSL_FATAL_ERROR;
  3935. }
  3936. }
  3937. switch (ssl->options.connectState) {
  3938. case CONNECT_BEGIN :
  3939. /* always send client hello first */
  3940. if ( (ssl->error = SendClientHello(ssl)) != 0) {
  3941. CYASSL_ERROR(ssl->error);
  3942. return SSL_FATAL_ERROR;
  3943. }
  3944. ssl->options.connectState = CLIENT_HELLO_SENT;
  3945. CYASSL_MSG("connect state: CLIENT_HELLO_SENT");
  3946. case CLIENT_HELLO_SENT :
  3947. neededState = ssl->options.resuming ? SERVER_FINISHED_COMPLETE :
  3948. SERVER_HELLODONE_COMPLETE;
  3949. #ifdef CYASSL_DTLS
  3950. /* In DTLS, when resuming, we can go straight to FINISHED,
  3951. * or do a cookie exchange and then skip to FINISHED, assume
  3952. * we need the cookie exchange first. */
  3953. if (ssl->options.dtls)
  3954. neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
  3955. #endif
  3956. /* get response */
  3957. while (ssl->options.serverState < neededState) {
  3958. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  3959. CYASSL_ERROR(ssl->error);
  3960. return SSL_FATAL_ERROR;
  3961. }
  3962. /* if resumption failed, reset needed state */
  3963. else if (neededState == SERVER_FINISHED_COMPLETE)
  3964. if (!ssl->options.resuming) {
  3965. if (!ssl->options.dtls)
  3966. neededState = SERVER_HELLODONE_COMPLETE;
  3967. else
  3968. neededState = SERVER_HELLOVERIFYREQUEST_COMPLETE;
  3969. }
  3970. }
  3971. ssl->options.connectState = HELLO_AGAIN;
  3972. CYASSL_MSG("connect state: HELLO_AGAIN");
  3973. case HELLO_AGAIN :
  3974. if (ssl->options.certOnly)
  3975. return SSL_SUCCESS;
  3976. #ifdef CYASSL_DTLS
  3977. if (ssl->options.dtls) {
  3978. /* re-init hashes, exclude first hello and verify request */
  3979. #ifndef NO_OLD_TLS
  3980. InitMd5(&ssl->hashMd5);
  3981. if ( (ssl->error = InitSha(&ssl->hashSha)) != 0) {
  3982. CYASSL_ERROR(ssl->error);
  3983. return SSL_FATAL_ERROR;
  3984. }
  3985. #endif
  3986. if (IsAtLeastTLSv1_2(ssl)) {
  3987. #ifndef NO_SHA256
  3988. if ( (ssl->error =
  3989. InitSha256(&ssl->hashSha256)) != 0) {
  3990. CYASSL_ERROR(ssl->error);
  3991. return SSL_FATAL_ERROR;
  3992. }
  3993. #endif
  3994. #ifdef CYASSL_SHA384
  3995. if ( (ssl->error =
  3996. InitSha384(&ssl->hashSha384)) != 0) {
  3997. CYASSL_ERROR(ssl->error);
  3998. return SSL_FATAL_ERROR;
  3999. }
  4000. #endif
  4001. }
  4002. if ( (ssl->error = SendClientHello(ssl)) != 0) {
  4003. CYASSL_ERROR(ssl->error);
  4004. return SSL_FATAL_ERROR;
  4005. }
  4006. }
  4007. #endif
  4008. ssl->options.connectState = HELLO_AGAIN_REPLY;
  4009. CYASSL_MSG("connect state: HELLO_AGAIN_REPLY");
  4010. case HELLO_AGAIN_REPLY :
  4011. #ifdef CYASSL_DTLS
  4012. if (ssl->options.dtls) {
  4013. neededState = ssl->options.resuming ?
  4014. SERVER_FINISHED_COMPLETE : SERVER_HELLODONE_COMPLETE;
  4015. /* get response */
  4016. while (ssl->options.serverState < neededState) {
  4017. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4018. CYASSL_ERROR(ssl->error);
  4019. return SSL_FATAL_ERROR;
  4020. }
  4021. /* if resumption failed, reset needed state */
  4022. else if (neededState == SERVER_FINISHED_COMPLETE)
  4023. if (!ssl->options.resuming)
  4024. neededState = SERVER_HELLODONE_COMPLETE;
  4025. }
  4026. }
  4027. #endif
  4028. ssl->options.connectState = FIRST_REPLY_DONE;
  4029. CYASSL_MSG("connect state: FIRST_REPLY_DONE");
  4030. case FIRST_REPLY_DONE :
  4031. #ifndef NO_CERTS
  4032. if (ssl->options.sendVerify) {
  4033. if ( (ssl->error = SendCertificate(ssl)) != 0) {
  4034. CYASSL_ERROR(ssl->error);
  4035. return SSL_FATAL_ERROR;
  4036. }
  4037. CYASSL_MSG("sent: certificate");
  4038. }
  4039. #endif
  4040. ssl->options.connectState = FIRST_REPLY_FIRST;
  4041. CYASSL_MSG("connect state: FIRST_REPLY_FIRST");
  4042. case FIRST_REPLY_FIRST :
  4043. if (!ssl->options.resuming) {
  4044. if ( (ssl->error = SendClientKeyExchange(ssl)) != 0) {
  4045. CYASSL_ERROR(ssl->error);
  4046. return SSL_FATAL_ERROR;
  4047. }
  4048. CYASSL_MSG("sent: client key exchange");
  4049. }
  4050. ssl->options.connectState = FIRST_REPLY_SECOND;
  4051. CYASSL_MSG("connect state: FIRST_REPLY_SECOND");
  4052. case FIRST_REPLY_SECOND :
  4053. #ifndef NO_CERTS
  4054. if (ssl->options.sendVerify) {
  4055. if ( (ssl->error = SendCertificateVerify(ssl)) != 0) {
  4056. CYASSL_ERROR(ssl->error);
  4057. return SSL_FATAL_ERROR;
  4058. }
  4059. CYASSL_MSG("sent: certificate verify");
  4060. }
  4061. #endif
  4062. ssl->options.connectState = FIRST_REPLY_THIRD;
  4063. CYASSL_MSG("connect state: FIRST_REPLY_THIRD");
  4064. case FIRST_REPLY_THIRD :
  4065. if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
  4066. CYASSL_ERROR(ssl->error);
  4067. return SSL_FATAL_ERROR;
  4068. }
  4069. CYASSL_MSG("sent: change cipher spec");
  4070. ssl->options.connectState = FIRST_REPLY_FOURTH;
  4071. CYASSL_MSG("connect state: FIRST_REPLY_FOURTH");
  4072. case FIRST_REPLY_FOURTH :
  4073. if ( (ssl->error = SendFinished(ssl)) != 0) {
  4074. CYASSL_ERROR(ssl->error);
  4075. return SSL_FATAL_ERROR;
  4076. }
  4077. CYASSL_MSG("sent: finished");
  4078. ssl->options.connectState = FINISHED_DONE;
  4079. CYASSL_MSG("connect state: FINISHED_DONE");
  4080. case FINISHED_DONE :
  4081. /* get response */
  4082. while (ssl->options.serverState < SERVER_FINISHED_COMPLETE)
  4083. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4084. CYASSL_ERROR(ssl->error);
  4085. return SSL_FATAL_ERROR;
  4086. }
  4087. ssl->options.connectState = SECOND_REPLY_DONE;
  4088. CYASSL_MSG("connect state: SECOND_REPLY_DONE");
  4089. case SECOND_REPLY_DONE:
  4090. FreeHandshakeResources(ssl);
  4091. CYASSL_LEAVE("SSL_connect()", SSL_SUCCESS);
  4092. return SSL_SUCCESS;
  4093. default:
  4094. CYASSL_MSG("Unknown connect state ERROR");
  4095. return SSL_FATAL_ERROR; /* unknown connect state */
  4096. }
  4097. }
  4098. #endif /* NO_CYASSL_CLIENT */
  4099. /* server only parts */
  4100. #ifndef NO_CYASSL_SERVER
  4101. #ifndef NO_OLD_TLS
  4102. CYASSL_METHOD* CyaSSLv3_server_method(void)
  4103. {
  4104. CYASSL_METHOD* method =
  4105. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  4106. DYNAMIC_TYPE_METHOD);
  4107. CYASSL_ENTER("SSLv3_server_method");
  4108. if (method) {
  4109. InitSSL_Method(method, MakeSSLv3());
  4110. method->side = CYASSL_SERVER_END;
  4111. }
  4112. return method;
  4113. }
  4114. #endif
  4115. #ifdef CYASSL_DTLS
  4116. CYASSL_METHOD* CyaDTLSv1_server_method(void)
  4117. {
  4118. CYASSL_METHOD* method =
  4119. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  4120. DYNAMIC_TYPE_METHOD);
  4121. CYASSL_ENTER("DTLSv1_server_method");
  4122. if (method) {
  4123. InitSSL_Method(method, MakeDTLSv1());
  4124. method->side = CYASSL_SERVER_END;
  4125. }
  4126. return method;
  4127. }
  4128. CYASSL_METHOD* CyaDTLSv1_2_server_method(void)
  4129. {
  4130. CYASSL_METHOD* method =
  4131. (CYASSL_METHOD*) XMALLOC(sizeof(CYASSL_METHOD), 0,
  4132. DYNAMIC_TYPE_METHOD);
  4133. CYASSL_ENTER("DTLSv1_2_server_method");
  4134. if (method) {
  4135. InitSSL_Method(method, MakeDTLSv1_2());
  4136. method->side = CYASSL_SERVER_END;
  4137. }
  4138. return method;
  4139. }
  4140. #endif
  4141. int CyaSSL_accept(CYASSL* ssl)
  4142. {
  4143. byte havePSK = 0;
  4144. CYASSL_ENTER("SSL_accept()");
  4145. #ifdef HAVE_ERRNO_H
  4146. errno = 0;
  4147. #endif
  4148. #ifndef NO_PSK
  4149. havePSK = ssl->options.havePSK;
  4150. #endif
  4151. (void)havePSK;
  4152. if (ssl->options.side != CYASSL_SERVER_END) {
  4153. CYASSL_ERROR(ssl->error = SIDE_ERROR);
  4154. return SSL_FATAL_ERROR;
  4155. }
  4156. #ifndef NO_CERTS
  4157. /* in case used set_accept_state after init */
  4158. if (!havePSK && (ssl->buffers.certificate.buffer == NULL ||
  4159. ssl->buffers.key.buffer == NULL)) {
  4160. CYASSL_MSG("accept error: don't have server cert and key");
  4161. ssl->error = NO_PRIVATE_KEY;
  4162. CYASSL_ERROR(ssl->error);
  4163. return SSL_FATAL_ERROR;
  4164. }
  4165. #endif
  4166. #ifdef CYASSL_DTLS
  4167. if (ssl->version.major == DTLS_MAJOR) {
  4168. ssl->options.dtls = 1;
  4169. ssl->options.tls = 1;
  4170. ssl->options.tls1_1 = 1;
  4171. if (DtlsPoolInit(ssl) != 0) {
  4172. ssl->error = MEMORY_ERROR;
  4173. CYASSL_ERROR(ssl->error);
  4174. return SSL_FATAL_ERROR;
  4175. }
  4176. }
  4177. #endif
  4178. if (ssl->buffers.outputBuffer.length > 0) {
  4179. if ( (ssl->error = SendBuffered(ssl)) == 0) {
  4180. ssl->options.acceptState++;
  4181. CYASSL_MSG("accept state: Advanced from buffered send");
  4182. }
  4183. else {
  4184. CYASSL_ERROR(ssl->error);
  4185. return SSL_FATAL_ERROR;
  4186. }
  4187. }
  4188. switch (ssl->options.acceptState) {
  4189. case ACCEPT_BEGIN :
  4190. /* get response */
  4191. while (ssl->options.clientState < CLIENT_HELLO_COMPLETE)
  4192. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4193. CYASSL_ERROR(ssl->error);
  4194. return SSL_FATAL_ERROR;
  4195. }
  4196. ssl->options.acceptState = ACCEPT_CLIENT_HELLO_DONE;
  4197. CYASSL_MSG("accept state ACCEPT_CLIENT_HELLO_DONE");
  4198. case ACCEPT_CLIENT_HELLO_DONE :
  4199. #ifdef CYASSL_DTLS
  4200. if (ssl->options.dtls)
  4201. if ( (ssl->error = SendHelloVerifyRequest(ssl)) != 0) {
  4202. CYASSL_ERROR(ssl->error);
  4203. return SSL_FATAL_ERROR;
  4204. }
  4205. #endif
  4206. ssl->options.acceptState = HELLO_VERIFY_SENT;
  4207. CYASSL_MSG("accept state HELLO_VERIFY_SENT");
  4208. case HELLO_VERIFY_SENT:
  4209. #ifdef CYASSL_DTLS
  4210. if (ssl->options.dtls) {
  4211. ssl->options.clientState = NULL_STATE; /* get again */
  4212. /* reset messages received */
  4213. XMEMSET(&ssl->msgsReceived, 0, sizeof(ssl->msgsReceived));
  4214. /* re-init hashes, exclude first hello and verify request */
  4215. #ifndef NO_OLD_TLS
  4216. InitMd5(&ssl->hashMd5);
  4217. if ( (ssl->error = InitSha(&ssl->hashSha)) != 0) {
  4218. CYASSL_ERROR(ssl->error);
  4219. return SSL_FATAL_ERROR;
  4220. }
  4221. #endif
  4222. if (IsAtLeastTLSv1_2(ssl)) {
  4223. #ifndef NO_SHA256
  4224. if ( (ssl->error =
  4225. InitSha256(&ssl->hashSha256)) != 0) {
  4226. CYASSL_ERROR(ssl->error);
  4227. return SSL_FATAL_ERROR;
  4228. }
  4229. #endif
  4230. #ifdef CYASSL_SHA384
  4231. if ( (ssl->error =
  4232. InitSha384(&ssl->hashSha384)) != 0) {
  4233. CYASSL_ERROR(ssl->error);
  4234. return SSL_FATAL_ERROR;
  4235. }
  4236. #endif
  4237. }
  4238. while (ssl->options.clientState < CLIENT_HELLO_COMPLETE)
  4239. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4240. CYASSL_ERROR(ssl->error);
  4241. return SSL_FATAL_ERROR;
  4242. }
  4243. }
  4244. #endif
  4245. ssl->options.acceptState = ACCEPT_FIRST_REPLY_DONE;
  4246. CYASSL_MSG("accept state ACCEPT_FIRST_REPLY_DONE");
  4247. case ACCEPT_FIRST_REPLY_DONE :
  4248. if ( (ssl->error = SendServerHello(ssl)) != 0) {
  4249. CYASSL_ERROR(ssl->error);
  4250. return SSL_FATAL_ERROR;
  4251. }
  4252. ssl->options.acceptState = SERVER_HELLO_SENT;
  4253. CYASSL_MSG("accept state SERVER_HELLO_SENT");
  4254. case SERVER_HELLO_SENT :
  4255. #ifndef NO_CERTS
  4256. if (!ssl->options.resuming)
  4257. if ( (ssl->error = SendCertificate(ssl)) != 0) {
  4258. CYASSL_ERROR(ssl->error);
  4259. return SSL_FATAL_ERROR;
  4260. }
  4261. #endif
  4262. ssl->options.acceptState = CERT_SENT;
  4263. CYASSL_MSG("accept state CERT_SENT");
  4264. case CERT_SENT :
  4265. if (!ssl->options.resuming)
  4266. if ( (ssl->error = SendServerKeyExchange(ssl)) != 0) {
  4267. CYASSL_ERROR(ssl->error);
  4268. return SSL_FATAL_ERROR;
  4269. }
  4270. ssl->options.acceptState = KEY_EXCHANGE_SENT;
  4271. CYASSL_MSG("accept state KEY_EXCHANGE_SENT");
  4272. case KEY_EXCHANGE_SENT :
  4273. #ifndef NO_CERTS
  4274. if (!ssl->options.resuming)
  4275. if (ssl->options.verifyPeer)
  4276. if ( (ssl->error = SendCertificateRequest(ssl)) != 0) {
  4277. CYASSL_ERROR(ssl->error);
  4278. return SSL_FATAL_ERROR;
  4279. }
  4280. #endif
  4281. ssl->options.acceptState = CERT_REQ_SENT;
  4282. CYASSL_MSG("accept state CERT_REQ_SENT");
  4283. case CERT_REQ_SENT :
  4284. if (!ssl->options.resuming)
  4285. if ( (ssl->error = SendServerHelloDone(ssl)) != 0) {
  4286. CYASSL_ERROR(ssl->error);
  4287. return SSL_FATAL_ERROR;
  4288. }
  4289. ssl->options.acceptState = SERVER_HELLO_DONE;
  4290. CYASSL_MSG("accept state SERVER_HELLO_DONE");
  4291. case SERVER_HELLO_DONE :
  4292. if (!ssl->options.resuming) {
  4293. while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
  4294. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4295. CYASSL_ERROR(ssl->error);
  4296. return SSL_FATAL_ERROR;
  4297. }
  4298. }
  4299. ssl->options.acceptState = ACCEPT_SECOND_REPLY_DONE;
  4300. CYASSL_MSG("accept state ACCEPT_SECOND_REPLY_DONE");
  4301. case ACCEPT_SECOND_REPLY_DONE :
  4302. if ( (ssl->error = SendChangeCipher(ssl)) != 0) {
  4303. CYASSL_ERROR(ssl->error);
  4304. return SSL_FATAL_ERROR;
  4305. }
  4306. ssl->options.acceptState = CHANGE_CIPHER_SENT;
  4307. CYASSL_MSG("accept state CHANGE_CIPHER_SENT");
  4308. case CHANGE_CIPHER_SENT :
  4309. if ( (ssl->error = SendFinished(ssl)) != 0) {
  4310. CYASSL_ERROR(ssl->error);
  4311. return SSL_FATAL_ERROR;
  4312. }
  4313. ssl->options.acceptState = ACCEPT_FINISHED_DONE;
  4314. CYASSL_MSG("accept state ACCEPT_FINISHED_DONE");
  4315. case ACCEPT_FINISHED_DONE :
  4316. if (ssl->options.resuming)
  4317. while (ssl->options.clientState < CLIENT_FINISHED_COMPLETE)
  4318. if ( (ssl->error = ProcessReply(ssl)) < 0) {
  4319. CYASSL_ERROR(ssl->error);
  4320. return SSL_FATAL_ERROR;
  4321. }
  4322. ssl->options.acceptState = ACCEPT_THIRD_REPLY_DONE;
  4323. CYASSL_MSG("accept state ACCEPT_THIRD_REPLY_DONE");
  4324. case ACCEPT_THIRD_REPLY_DONE :
  4325. FreeHandshakeResources(ssl);
  4326. CYASSL_LEAVE("SSL_accept()", SSL_SUCCESS);
  4327. return SSL_SUCCESS;
  4328. default :
  4329. CYASSL_MSG("Unknown accept state ERROR");
  4330. return SSL_FATAL_ERROR;
  4331. }
  4332. }
  4333. #endif /* NO_CYASSL_SERVER */
  4334. int CyaSSL_Cleanup(void)
  4335. {
  4336. int ret = SSL_SUCCESS;
  4337. int release = 0;
  4338. CYASSL_ENTER("CyaSSL_Cleanup");
  4339. if (initRefCount == 0)
  4340. return ret; /* possibly no init yet, but not failure either way */
  4341. if (LockMutex(&count_mutex) != 0) {
  4342. CYASSL_MSG("Bad Lock Mutex count");
  4343. return BAD_MUTEX_E;
  4344. }
  4345. release = initRefCount-- == 1;
  4346. if (initRefCount < 0)
  4347. initRefCount = 0;
  4348. UnLockMutex(&count_mutex);
  4349. if (!release)
  4350. return ret;
  4351. #ifndef NO_SESSION_CACHE
  4352. if (FreeMutex(&session_mutex) != 0)
  4353. ret = BAD_MUTEX_E;
  4354. #endif
  4355. if (FreeMutex(&count_mutex) != 0)
  4356. ret = BAD_MUTEX_E;
  4357. #if defined(HAVE_ECC) && defined(FP_ECC)
  4358. ecc_fp_free();
  4359. #endif
  4360. return ret;
  4361. }
  4362. #ifndef NO_SESSION_CACHE
  4363. /* some session IDs aren't random afterall, let's make them random */
  4364. static INLINE word32 HashSession(const byte* sessionID, word32 len, int* error)
  4365. {
  4366. byte digest[MD5_DIGEST_SIZE];
  4367. #ifndef NO_MD5
  4368. *error = Md5Hash(sessionID, len, digest);
  4369. #elif !defined(NO_SHA)
  4370. *error = ShaHash(sessionID, len, digest);
  4371. #elif !defined(NO_SHA256)
  4372. *error = Sha256Hash(sessionID, len, digest);
  4373. #else
  4374. #error "We need a digest to hash the session IDs"
  4375. #endif
  4376. return *error == 0 ? MakeWordFromHash(digest) : 0; /* 0 on failure */
  4377. }
  4378. void CyaSSL_flush_sessions(CYASSL_CTX* ctx, long tm)
  4379. {
  4380. /* static table now, no flusing needed */
  4381. (void)ctx;
  4382. (void)tm;
  4383. }
  4384. /* set ssl session timeout in seconds */
  4385. int CyaSSL_set_timeout(CYASSL* ssl, unsigned int to)
  4386. {
  4387. if (ssl == NULL)
  4388. return BAD_FUNC_ARG;
  4389. ssl->timeout = to;
  4390. return SSL_SUCCESS;
  4391. }
  4392. /* set ctx session timeout in seconds */
  4393. int CyaSSL_CTX_set_timeout(CYASSL_CTX* ctx, unsigned int to)
  4394. {
  4395. if (ctx == NULL)
  4396. return BAD_FUNC_ARG;
  4397. ctx->timeout = to;
  4398. return SSL_SUCCESS;
  4399. }
  4400. #ifndef NO_CLIENT_CACHE
  4401. /* Get Session from Client cache based on id/len, return NULL on failure */
  4402. CYASSL_SESSION* GetSessionClient(CYASSL* ssl, const byte* id, int len)
  4403. {
  4404. CYASSL_SESSION* ret = NULL;
  4405. word32 row;
  4406. int idx;
  4407. int count;
  4408. int error = 0;
  4409. CYASSL_ENTER("GetSessionClient");
  4410. if (ssl->options.side == CYASSL_SERVER_END)
  4411. return NULL;
  4412. len = min(SERVER_ID_LEN, (word32)len);
  4413. row = HashSession(id, len, &error) % SESSION_ROWS;
  4414. if (error != 0) {
  4415. CYASSL_MSG("Hash session failed");
  4416. return NULL;
  4417. }
  4418. if (LockMutex(&session_mutex) != 0) {
  4419. CYASSL_MSG("Lock session mutex failed");
  4420. return NULL;
  4421. }
  4422. /* start from most recently used */
  4423. count = min((word32)ClientCache[row].totalCount, SESSIONS_PER_ROW);
  4424. idx = ClientCache[row].nextIdx - 1;
  4425. if (idx < 0)
  4426. idx = SESSIONS_PER_ROW - 1; /* if back to front, the previous was end */
  4427. for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
  4428. CYASSL_SESSION* current;
  4429. ClientSession clSess;
  4430. if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
  4431. CYASSL_MSG("Bad idx");
  4432. break;
  4433. }
  4434. clSess = ClientCache[row].Clients[idx];
  4435. current = &SessionCache[clSess.serverRow].Sessions[clSess.serverIdx];
  4436. if (XMEMCMP(current->serverID, id, len) == 0) {
  4437. CYASSL_MSG("Found a serverid match for client");
  4438. if (LowResTimer() < (current->bornOn + current->timeout)) {
  4439. CYASSL_MSG("Session valid");
  4440. ret = current;
  4441. break;
  4442. } else {
  4443. CYASSL_MSG("Session timed out"); /* could have more for id */
  4444. }
  4445. } else {
  4446. CYASSL_MSG("ServerID not a match from client table");
  4447. }
  4448. }
  4449. UnLockMutex(&session_mutex);
  4450. return ret;
  4451. }
  4452. #endif /* NO_CLIENT_CACHE */
  4453. CYASSL_SESSION* GetSession(CYASSL* ssl, byte* masterSecret)
  4454. {
  4455. CYASSL_SESSION* ret = 0;
  4456. const byte* id = NULL;
  4457. word32 row;
  4458. int idx;
  4459. int count;
  4460. int error = 0;
  4461. if (ssl->options.sessionCacheOff)
  4462. return NULL;
  4463. if (ssl->options.haveSessionId == 0)
  4464. return NULL;
  4465. if (ssl->arrays)
  4466. id = ssl->arrays->sessionID;
  4467. else
  4468. id = ssl->session.sessionID;
  4469. row = HashSession(id, ID_LEN, &error) % SESSION_ROWS;
  4470. if (error != 0) {
  4471. CYASSL_MSG("Hash session failed");
  4472. return NULL;
  4473. }
  4474. if (LockMutex(&session_mutex) != 0)
  4475. return 0;
  4476. /* start from most recently used */
  4477. count = min((word32)SessionCache[row].totalCount, SESSIONS_PER_ROW);
  4478. idx = SessionCache[row].nextIdx - 1;
  4479. if (idx < 0)
  4480. idx = SESSIONS_PER_ROW - 1; /* if back to front, the previous was end */
  4481. for (; count > 0; --count, idx = idx ? idx - 1 : SESSIONS_PER_ROW - 1) {
  4482. CYASSL_SESSION* current;
  4483. if (idx >= SESSIONS_PER_ROW || idx < 0) { /* sanity check */
  4484. CYASSL_MSG("Bad idx");
  4485. break;
  4486. }
  4487. current = &SessionCache[row].Sessions[idx];
  4488. if (XMEMCMP(current->sessionID, id, ID_LEN) == 0) {
  4489. CYASSL_MSG("Found a session match");
  4490. if (LowResTimer() < (current->bornOn + current->timeout)) {
  4491. CYASSL_MSG("Session valid");
  4492. ret = current;
  4493. if (masterSecret)
  4494. XMEMCPY(masterSecret, current->masterSecret, SECRET_LEN);
  4495. } else {
  4496. CYASSL_MSG("Session timed out");
  4497. }
  4498. break; /* no more sessionIDs whether valid or not that match */
  4499. } else {
  4500. CYASSL_MSG("SessionID not a match at this idx");
  4501. }
  4502. }
  4503. UnLockMutex(&session_mutex);
  4504. return ret;
  4505. }
  4506. int SetSession(CYASSL* ssl, CYASSL_SESSION* session)
  4507. {
  4508. if (ssl->options.sessionCacheOff)
  4509. return SSL_FAILURE;
  4510. if (LowResTimer() < (session->bornOn + session->timeout)) {
  4511. ssl->session = *session;
  4512. ssl->options.resuming = 1;
  4513. #ifdef SESSION_CERTS
  4514. ssl->version = session->version;
  4515. ssl->options.cipherSuite0 = session->cipherSuite0;
  4516. ssl->options.cipherSuite = session->cipherSuite;
  4517. #endif
  4518. return SSL_SUCCESS;
  4519. }
  4520. return SSL_FAILURE; /* session timed out */
  4521. }
  4522. int AddSession(CYASSL* ssl)
  4523. {
  4524. word32 row, idx;
  4525. int error = 0;
  4526. if (ssl->options.sessionCacheOff)
  4527. return 0;
  4528. if (ssl->options.haveSessionId == 0)
  4529. return 0;
  4530. row = HashSession(ssl->arrays->sessionID, ID_LEN, &error) % SESSION_ROWS;
  4531. if (error != 0) {
  4532. CYASSL_MSG("Hash session failed");
  4533. return error;
  4534. }
  4535. if (LockMutex(&session_mutex) != 0)
  4536. return BAD_MUTEX_E;
  4537. idx = SessionCache[row].nextIdx++;
  4538. #ifdef SESSION_INDEX
  4539. ssl->sessionIndex = (row << SESSIDX_ROW_SHIFT) | idx;
  4540. #endif
  4541. XMEMCPY(SessionCache[row].Sessions[idx].masterSecret,
  4542. ssl->arrays->masterSecret, SECRET_LEN);
  4543. XMEMCPY(SessionCache[row].Sessions[idx].sessionID, ssl->arrays->sessionID,
  4544. ID_LEN);
  4545. SessionCache[row].Sessions[idx].sessionIDSz = ssl->arrays->sessionIDSz;
  4546. SessionCache[row].Sessions[idx].timeout = ssl->timeout;
  4547. SessionCache[row].Sessions[idx].bornOn = LowResTimer();
  4548. #ifdef HAVE_SESSION_TICKET
  4549. SessionCache[row].Sessions[idx].ticketLen = ssl->session.ticketLen;
  4550. XMEMCPY(SessionCache[row].Sessions[idx].ticket,
  4551. ssl->session.ticket, ssl->session.ticketLen);
  4552. #endif
  4553. #ifdef SESSION_CERTS
  4554. SessionCache[row].Sessions[idx].chain.count = ssl->session.chain.count;
  4555. XMEMCPY(SessionCache[row].Sessions[idx].chain.certs,
  4556. ssl->session.chain.certs, sizeof(x509_buffer) * MAX_CHAIN_DEPTH);
  4557. SessionCache[row].Sessions[idx].version = ssl->version;
  4558. SessionCache[row].Sessions[idx].cipherSuite0 = ssl->options.cipherSuite0;
  4559. SessionCache[row].Sessions[idx].cipherSuite = ssl->options.cipherSuite;
  4560. #endif /* SESSION_CERTS */
  4561. SessionCache[row].totalCount++;
  4562. if (SessionCache[row].nextIdx == SESSIONS_PER_ROW)
  4563. SessionCache[row].nextIdx = 0;
  4564. #ifndef NO_CLIENT_CACHE
  4565. if (ssl->options.side == CYASSL_CLIENT_END && ssl->session.idLen) {
  4566. word32 clientRow, clientIdx;
  4567. CYASSL_MSG("Adding client cache entry");
  4568. SessionCache[row].Sessions[idx].idLen = ssl->session.idLen;
  4569. XMEMCPY(SessionCache[row].Sessions[idx].serverID, ssl->session.serverID,
  4570. ssl->session.idLen);
  4571. clientRow = HashSession(ssl->session.serverID, ssl->session.idLen,
  4572. &error) % SESSION_ROWS;
  4573. if (error != 0) {
  4574. CYASSL_MSG("Hash session failed");
  4575. return error;
  4576. }
  4577. clientIdx = ClientCache[clientRow].nextIdx++;
  4578. ClientCache[clientRow].Clients[clientIdx].serverRow = (word16)row;
  4579. ClientCache[clientRow].Clients[clientIdx].serverIdx = (word16)idx;
  4580. ClientCache[clientRow].totalCount++;
  4581. if (ClientCache[clientRow].nextIdx == SESSIONS_PER_ROW)
  4582. ClientCache[clientRow].nextIdx = 0;
  4583. }
  4584. else
  4585. SessionCache[row].Sessions[idx].idLen = 0;
  4586. #endif /* NO_CLIENT_CACHE */
  4587. if (UnLockMutex(&session_mutex) != 0)
  4588. return BAD_MUTEX_E;
  4589. return 0;
  4590. }
  4591. #ifdef SESSION_INDEX
  4592. int CyaSSL_GetSessionIndex(CYASSL* ssl)
  4593. {
  4594. CYASSL_ENTER("CyaSSL_GetSessionIndex");
  4595. CYASSL_LEAVE("CyaSSL_GetSessionIndex", ssl->sessionIndex);
  4596. return ssl->sessionIndex;
  4597. }
  4598. int CyaSSL_GetSessionAtIndex(int idx, CYASSL_SESSION* session)
  4599. {
  4600. int row, col, result = SSL_FAILURE;
  4601. CYASSL_ENTER("CyaSSL_GetSessionAtIndex");
  4602. row = idx >> SESSIDX_ROW_SHIFT;
  4603. col = idx & SESSIDX_IDX_MASK;
  4604. if (LockMutex(&session_mutex) != 0) {
  4605. return BAD_MUTEX_E;
  4606. }
  4607. if (row < SESSION_ROWS &&
  4608. col < (int)min(SessionCache[row].totalCount, SESSIONS_PER_ROW)) {
  4609. XMEMCPY(session,
  4610. &SessionCache[row].Sessions[col], sizeof(CYASSL_SESSION));
  4611. result = SSL_SUCCESS;
  4612. }
  4613. if (UnLockMutex(&session_mutex) != 0)
  4614. result = BAD_MUTEX_E;
  4615. CYASSL_LEAVE("CyaSSL_GetSessionAtIndex", result);
  4616. return result;
  4617. }
  4618. #endif /* SESSION_INDEX */
  4619. #if defined(SESSION_INDEX) && defined(SESSION_CERTS)
  4620. CYASSL_X509_CHAIN* CyaSSL_SESSION_get_peer_chain(CYASSL_SESSION* session)
  4621. {
  4622. CYASSL_X509_CHAIN* chain = NULL;
  4623. CYASSL_ENTER("CyaSSL_SESSION_get_peer_chain");
  4624. if (session)
  4625. chain = &session->chain;
  4626. CYASSL_LEAVE("CyaSSL_SESSION_get_peer_chain", chain ? 1 : 0);
  4627. return chain;
  4628. }
  4629. #endif /* SESSION_INDEX && SESSION_CERTS */
  4630. #ifdef SESSION_STATS
  4631. CYASSL_API
  4632. void PrintSessionStats(void)
  4633. {
  4634. word32 totalSessionsSeen = 0;
  4635. word32 totalSessionsNow = 0;
  4636. word32 rowNow;
  4637. int i;
  4638. double E; /* expected freq */
  4639. double chiSquare = 0;
  4640. for (i = 0; i < SESSION_ROWS; i++) {
  4641. totalSessionsSeen += SessionCache[i].totalCount;
  4642. if (SessionCache[i].totalCount >= SESSIONS_PER_ROW)
  4643. rowNow = SESSIONS_PER_ROW;
  4644. else if (SessionCache[i].nextIdx == 0)
  4645. rowNow = 0;
  4646. else
  4647. rowNow = SessionCache[i].nextIdx;
  4648. totalSessionsNow += rowNow;
  4649. }
  4650. printf("Total Sessions Seen = %d\n", totalSessionsSeen);
  4651. printf("Total Sessions Now = %d\n", totalSessionsNow);
  4652. E = (double)totalSessionsSeen / SESSION_ROWS;
  4653. for (i = 0; i < SESSION_ROWS; i++) {
  4654. double diff = SessionCache[i].totalCount - E;
  4655. diff *= diff; /* square */
  4656. diff /= E; /* normalize */
  4657. chiSquare += diff;
  4658. }
  4659. printf(" chi-square = %5.1f, d.f. = %d\n", chiSquare,
  4660. SESSION_ROWS - 1);
  4661. if (SESSION_ROWS == 11)
  4662. printf(" .05 p value = 18.3, chi-square should be less\n");
  4663. else if (SESSION_ROWS == 211)
  4664. printf(".05 p value = 244.8, chi-square should be less\n");
  4665. else if (SESSION_ROWS == 5981)
  4666. printf(".05 p value = 6161.0, chi-square should be less\n");
  4667. else if (SESSION_ROWS == 3)
  4668. printf(".05 p value = 6.0, chi-square should be less\n");
  4669. else if (SESSION_ROWS == 2861)
  4670. printf(".05 p value = 2985.5, chi-square should be less\n");
  4671. printf("\n");
  4672. }
  4673. #endif /* SESSION_STATS */
  4674. #else /* NO_SESSION_CACHE */
  4675. /* No session cache version */
  4676. CYASSL_SESSION* GetSession(CYASSL* ssl, byte* masterSecret)
  4677. {
  4678. (void)ssl;
  4679. (void)masterSecret;
  4680. return NULL;
  4681. }
  4682. #endif /* NO_SESSION_CACHE */
  4683. /* call before SSL_connect, if verifying will add name check to
  4684. date check and signature check */
  4685. int CyaSSL_check_domain_name(CYASSL* ssl, const char* dn)
  4686. {
  4687. CYASSL_ENTER("CyaSSL_check_domain_name");
  4688. if (ssl->buffers.domainName.buffer)
  4689. XFREE(ssl->buffers.domainName.buffer, ssl->heap, DYNAMIC_TYPE_DOMAIN);
  4690. ssl->buffers.domainName.length = (word32)XSTRLEN(dn) + 1;
  4691. ssl->buffers.domainName.buffer = (byte*) XMALLOC(
  4692. ssl->buffers.domainName.length, ssl->heap, DYNAMIC_TYPE_DOMAIN);
  4693. if (ssl->buffers.domainName.buffer) {
  4694. XSTRNCPY((char*)ssl->buffers.domainName.buffer, dn,
  4695. ssl->buffers.domainName.length);
  4696. return SSL_SUCCESS;
  4697. }
  4698. else {
  4699. ssl->error = MEMORY_ERROR;
  4700. return SSL_FAILURE;
  4701. }
  4702. }
  4703. /* turn on CyaSSL zlib compression
  4704. returns SSL_SUCCESS for success, else error (not built in)
  4705. */
  4706. int CyaSSL_set_compression(CYASSL* ssl)
  4707. {
  4708. CYASSL_ENTER("CyaSSL_set_compression");
  4709. (void)ssl;
  4710. #ifdef HAVE_LIBZ
  4711. ssl->options.usingCompression = 1;
  4712. return SSL_SUCCESS;
  4713. #else
  4714. return NOT_COMPILED_IN;
  4715. #endif
  4716. }
  4717. #ifndef USE_WINDOWS_API
  4718. #ifndef NO_WRITEV
  4719. /* simulate writev semantics, doesn't actually do block at a time though
  4720. because of SSL_write behavior and because front adds may be small */
  4721. int CyaSSL_writev(CYASSL* ssl, const struct iovec* iov, int iovcnt)
  4722. {
  4723. #ifdef CYASSL_SMALL_STACK
  4724. byte staticBuffer[1]; /* force heap usage */
  4725. #else
  4726. byte staticBuffer[FILE_BUFFER_SIZE];
  4727. #endif
  4728. byte* myBuffer = staticBuffer;
  4729. int dynamic = 0;
  4730. int sending = 0;
  4731. int idx = 0;
  4732. int i;
  4733. int ret;
  4734. CYASSL_ENTER("CyaSSL_writev");
  4735. for (i = 0; i < iovcnt; i++)
  4736. sending += (int)iov[i].iov_len;
  4737. if (sending > (int)sizeof(staticBuffer)) {
  4738. myBuffer = (byte*)XMALLOC(sending, ssl->heap,
  4739. DYNAMIC_TYPE_WRITEV);
  4740. if (!myBuffer)
  4741. return MEMORY_ERROR;
  4742. dynamic = 1;
  4743. }
  4744. for (i = 0; i < iovcnt; i++) {
  4745. XMEMCPY(&myBuffer[idx], iov[i].iov_base, iov[i].iov_len);
  4746. idx += (int)iov[i].iov_len;
  4747. }
  4748. ret = CyaSSL_write(ssl, myBuffer, sending);
  4749. if (dynamic)
  4750. XFREE(myBuffer, ssl->heap, DYNAMIC_TYPE_WRITEV);
  4751. return ret;
  4752. }
  4753. #endif
  4754. #endif
  4755. #ifdef CYASSL_CALLBACKS
  4756. typedef struct itimerval Itimerval;
  4757. /* don't keep calling simple functions while setting up timer and singals
  4758. if no inlining these are the next best */
  4759. #define AddTimes(a, b, c) \
  4760. do { \
  4761. c.tv_sec = a.tv_sec + b.tv_sec; \
  4762. c.tv_usec = a.tv_usec + b.tv_usec; \
  4763. if (c.tv_usec >= 1000000) { \
  4764. c.tv_sec++; \
  4765. c.tv_usec -= 1000000; \
  4766. } \
  4767. } while (0)
  4768. #define SubtractTimes(a, b, c) \
  4769. do { \
  4770. c.tv_sec = a.tv_sec - b.tv_sec; \
  4771. c.tv_usec = a.tv_usec - b.tv_usec; \
  4772. if (c.tv_usec < 0) { \
  4773. c.tv_sec--; \
  4774. c.tv_usec += 1000000; \
  4775. } \
  4776. } while (0)
  4777. #define CmpTimes(a, b, cmp) \
  4778. ((a.tv_sec == b.tv_sec) ? \
  4779. (a.tv_usec cmp b.tv_usec) : \
  4780. (a.tv_sec cmp b.tv_sec)) \
  4781. /* do nothing handler */
  4782. static void myHandler(int signo)
  4783. {
  4784. (void)signo;
  4785. return;
  4786. }
  4787. static int CyaSSL_ex_wrapper(CYASSL* ssl, HandShakeCallBack hsCb,
  4788. TimeoutCallBack toCb, Timeval timeout)
  4789. {
  4790. int ret = SSL_FATAL_ERROR;
  4791. int oldTimerOn = 0; /* was timer already on */
  4792. Timeval startTime;
  4793. Timeval endTime;
  4794. Timeval totalTime;
  4795. Itimerval myTimeout;
  4796. Itimerval oldTimeout; /* if old timer adjust from total time to reset */
  4797. struct sigaction act, oact;
  4798. #define ERR_OUT(x) { ssl->hsInfoOn = 0; ssl->toInfoOn = 0; return x; }
  4799. if (hsCb) {
  4800. ssl->hsInfoOn = 1;
  4801. InitHandShakeInfo(&ssl->handShakeInfo);
  4802. }
  4803. if (toCb) {
  4804. ssl->toInfoOn = 1;
  4805. InitTimeoutInfo(&ssl->timeoutInfo);
  4806. if (gettimeofday(&startTime, 0) < 0)
  4807. ERR_OUT(GETTIME_ERROR);
  4808. /* use setitimer to simulate getitimer, init 0 myTimeout */
  4809. myTimeout.it_interval.tv_sec = 0;
  4810. myTimeout.it_interval.tv_usec = 0;
  4811. myTimeout.it_value.tv_sec = 0;
  4812. myTimeout.it_value.tv_usec = 0;
  4813. if (setitimer(ITIMER_REAL, &myTimeout, &oldTimeout) < 0)
  4814. ERR_OUT(SETITIMER_ERROR);
  4815. if (oldTimeout.it_value.tv_sec || oldTimeout.it_value.tv_usec) {
  4816. oldTimerOn = 1;
  4817. /* is old timer going to expire before ours */
  4818. if (CmpTimes(oldTimeout.it_value, timeout, <)) {
  4819. timeout.tv_sec = oldTimeout.it_value.tv_sec;
  4820. timeout.tv_usec = oldTimeout.it_value.tv_usec;
  4821. }
  4822. }
  4823. myTimeout.it_value.tv_sec = timeout.tv_sec;
  4824. myTimeout.it_value.tv_usec = timeout.tv_usec;
  4825. /* set up signal handler, don't restart socket send/recv */
  4826. act.sa_handler = myHandler;
  4827. sigemptyset(&act.sa_mask);
  4828. act.sa_flags = 0;
  4829. #ifdef SA_INTERRUPT
  4830. act.sa_flags |= SA_INTERRUPT;
  4831. #endif
  4832. if (sigaction(SIGALRM, &act, &oact) < 0)
  4833. ERR_OUT(SIGACT_ERROR);
  4834. if (setitimer(ITIMER_REAL, &myTimeout, 0) < 0)
  4835. ERR_OUT(SETITIMER_ERROR);
  4836. }
  4837. /* do main work */
  4838. #ifndef NO_CYASSL_CLIENT
  4839. if (ssl->options.side == CYASSL_CLIENT_END)
  4840. ret = CyaSSL_connect(ssl);
  4841. #endif
  4842. #ifndef NO_CYASSL_SERVER
  4843. if (ssl->options.side == CYASSL_SERVER_END)
  4844. ret = CyaSSL_accept(ssl);
  4845. #endif
  4846. /* do callbacks */
  4847. if (toCb) {
  4848. if (oldTimerOn) {
  4849. gettimeofday(&endTime, 0);
  4850. SubtractTimes(endTime, startTime, totalTime);
  4851. /* adjust old timer for elapsed time */
  4852. if (CmpTimes(totalTime, oldTimeout.it_value, <))
  4853. SubtractTimes(oldTimeout.it_value, totalTime,
  4854. oldTimeout.it_value);
  4855. else {
  4856. /* reset value to interval, may be off */
  4857. oldTimeout.it_value.tv_sec = oldTimeout.it_interval.tv_sec;
  4858. oldTimeout.it_value.tv_usec =oldTimeout.it_interval.tv_usec;
  4859. }
  4860. /* keep iter the same whether there or not */
  4861. }
  4862. /* restore old handler */
  4863. if (sigaction(SIGALRM, &oact, 0) < 0)
  4864. ret = SIGACT_ERROR; /* more pressing error, stomp */
  4865. else
  4866. /* use old settings which may turn off (expired or not there) */
  4867. if (setitimer(ITIMER_REAL, &oldTimeout, 0) < 0)
  4868. ret = SETITIMER_ERROR;
  4869. /* if we had a timeout call callback */
  4870. if (ssl->timeoutInfo.timeoutName[0]) {
  4871. ssl->timeoutInfo.timeoutValue.tv_sec = timeout.tv_sec;
  4872. ssl->timeoutInfo.timeoutValue.tv_usec = timeout.tv_usec;
  4873. (toCb)(&ssl->timeoutInfo);
  4874. }
  4875. /* clean up */
  4876. FreeTimeoutInfo(&ssl->timeoutInfo, ssl->heap);
  4877. ssl->toInfoOn = 0;
  4878. }
  4879. if (hsCb) {
  4880. FinishHandShakeInfo(&ssl->handShakeInfo, ssl);
  4881. (hsCb)(&ssl->handShakeInfo);
  4882. ssl->hsInfoOn = 0;
  4883. }
  4884. return ret;
  4885. }
  4886. #ifndef NO_CYASSL_CLIENT
  4887. int CyaSSL_connect_ex(CYASSL* ssl, HandShakeCallBack hsCb,
  4888. TimeoutCallBack toCb, Timeval timeout)
  4889. {
  4890. CYASSL_ENTER("CyaSSL_connect_ex");
  4891. return CyaSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
  4892. }
  4893. #endif
  4894. #ifndef NO_CYASSL_SERVER
  4895. int CyaSSL_accept_ex(CYASSL* ssl, HandShakeCallBack hsCb,
  4896. TimeoutCallBack toCb,Timeval timeout)
  4897. {
  4898. CYASSL_ENTER("CyaSSL_accept_ex");
  4899. return CyaSSL_ex_wrapper(ssl, hsCb, toCb, timeout);
  4900. }
  4901. #endif
  4902. #endif /* CYASSL_CALLBACKS */
  4903. #ifndef NO_PSK
  4904. void CyaSSL_CTX_set_psk_client_callback(CYASSL_CTX* ctx,
  4905. psk_client_callback cb)
  4906. {
  4907. CYASSL_ENTER("SSL_CTX_set_psk_client_callback");
  4908. ctx->havePSK = 1;
  4909. ctx->client_psk_cb = cb;
  4910. }
  4911. void CyaSSL_set_psk_client_callback(CYASSL* ssl, psk_client_callback cb)
  4912. {
  4913. byte haveRSA = 1;
  4914. CYASSL_ENTER("SSL_set_psk_client_callback");
  4915. ssl->options.havePSK = 1;
  4916. ssl->options.client_psk_cb = cb;
  4917. #ifdef NO_RSA
  4918. haveRSA = 0;
  4919. #endif
  4920. InitSuites(ssl->suites, ssl->version, haveRSA, TRUE,
  4921. ssl->options.haveDH, ssl->options.haveNTRU,
  4922. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  4923. ssl->options.side);
  4924. }
  4925. void CyaSSL_CTX_set_psk_server_callback(CYASSL_CTX* ctx,
  4926. psk_server_callback cb)
  4927. {
  4928. CYASSL_ENTER("SSL_CTX_set_psk_server_callback");
  4929. ctx->havePSK = 1;
  4930. ctx->server_psk_cb = cb;
  4931. }
  4932. void CyaSSL_set_psk_server_callback(CYASSL* ssl, psk_server_callback cb)
  4933. {
  4934. byte haveRSA = 1;
  4935. CYASSL_ENTER("SSL_set_psk_server_callback");
  4936. ssl->options.havePSK = 1;
  4937. ssl->options.server_psk_cb = cb;
  4938. #ifdef NO_RSA
  4939. haveRSA = 0;
  4940. #endif
  4941. InitSuites(ssl->suites, ssl->version, haveRSA, TRUE,
  4942. ssl->options.haveDH, ssl->options.haveNTRU,
  4943. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  4944. ssl->options.side);
  4945. }
  4946. const char* CyaSSL_get_psk_identity_hint(const CYASSL* ssl)
  4947. {
  4948. CYASSL_ENTER("SSL_get_psk_identity_hint");
  4949. if (ssl == NULL || ssl->arrays == NULL)
  4950. return NULL;
  4951. return ssl->arrays->server_hint;
  4952. }
  4953. const char* CyaSSL_get_psk_identity(const CYASSL* ssl)
  4954. {
  4955. CYASSL_ENTER("SSL_get_psk_identity");
  4956. if (ssl == NULL || ssl->arrays == NULL)
  4957. return NULL;
  4958. return ssl->arrays->client_identity;
  4959. }
  4960. int CyaSSL_CTX_use_psk_identity_hint(CYASSL_CTX* ctx, const char* hint)
  4961. {
  4962. CYASSL_ENTER("SSL_CTX_use_psk_identity_hint");
  4963. if (hint == 0)
  4964. ctx->server_hint[0] = 0;
  4965. else {
  4966. XSTRNCPY(ctx->server_hint, hint, MAX_PSK_ID_LEN);
  4967. ctx->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
  4968. }
  4969. return SSL_SUCCESS;
  4970. }
  4971. int CyaSSL_use_psk_identity_hint(CYASSL* ssl, const char* hint)
  4972. {
  4973. CYASSL_ENTER("SSL_use_psk_identity_hint");
  4974. if (ssl == NULL || ssl->arrays == NULL)
  4975. return SSL_FAILURE;
  4976. if (hint == 0)
  4977. ssl->arrays->server_hint[0] = 0;
  4978. else {
  4979. XSTRNCPY(ssl->arrays->server_hint, hint, MAX_PSK_ID_LEN);
  4980. ssl->arrays->server_hint[MAX_PSK_ID_LEN - 1] = '\0';
  4981. }
  4982. return SSL_SUCCESS;
  4983. }
  4984. #endif /* NO_PSK */
  4985. #ifndef NO_CERTS
  4986. /* used to be defined on NO_FILESYSTEM only, but are generally useful */
  4987. /* CyaSSL extension allows DER files to be loaded from buffers as well */
  4988. int CyaSSL_CTX_load_verify_buffer(CYASSL_CTX* ctx, const unsigned char* in,
  4989. long sz, int format)
  4990. {
  4991. CYASSL_ENTER("CyaSSL_CTX_load_verify_buffer");
  4992. if (format == SSL_FILETYPE_PEM)
  4993. return ProcessChainBuffer(ctx, in, sz, format, CA_TYPE, NULL);
  4994. else
  4995. return ProcessBuffer(ctx, in, sz, format, CA_TYPE, NULL,NULL,0);
  4996. }
  4997. int CyaSSL_CTX_use_certificate_buffer(CYASSL_CTX* ctx,
  4998. const unsigned char* in, long sz, int format)
  4999. {
  5000. CYASSL_ENTER("CyaSSL_CTX_use_certificate_buffer");
  5001. return ProcessBuffer(ctx, in, sz, format, CERT_TYPE, NULL, NULL, 0);
  5002. }
  5003. int CyaSSL_CTX_use_PrivateKey_buffer(CYASSL_CTX* ctx,
  5004. const unsigned char* in, long sz, int format)
  5005. {
  5006. CYASSL_ENTER("CyaSSL_CTX_use_PrivateKey_buffer");
  5007. return ProcessBuffer(ctx, in, sz, format, PRIVATEKEY_TYPE, NULL,NULL,0);
  5008. }
  5009. int CyaSSL_CTX_use_certificate_chain_buffer(CYASSL_CTX* ctx,
  5010. const unsigned char* in, long sz)
  5011. {
  5012. CYASSL_ENTER("CyaSSL_CTX_use_certificate_chain_buffer");
  5013. return ProcessBuffer(ctx, in, sz, SSL_FILETYPE_PEM, CERT_TYPE, NULL,
  5014. NULL, 1);
  5015. }
  5016. int CyaSSL_use_certificate_buffer(CYASSL* ssl,
  5017. const unsigned char* in, long sz, int format)
  5018. {
  5019. CYASSL_ENTER("CyaSSL_use_certificate_buffer");
  5020. return ProcessBuffer(ssl->ctx, in, sz, format,CERT_TYPE,ssl,NULL,0);
  5021. }
  5022. int CyaSSL_use_PrivateKey_buffer(CYASSL* ssl,
  5023. const unsigned char* in, long sz, int format)
  5024. {
  5025. CYASSL_ENTER("CyaSSL_use_PrivateKey_buffer");
  5026. return ProcessBuffer(ssl->ctx, in, sz, format, PRIVATEKEY_TYPE,
  5027. ssl, NULL, 0);
  5028. }
  5029. int CyaSSL_use_certificate_chain_buffer(CYASSL* ssl,
  5030. const unsigned char* in, long sz)
  5031. {
  5032. CYASSL_ENTER("CyaSSL_use_certificate_chain_buffer");
  5033. return ProcessBuffer(ssl->ctx, in, sz, SSL_FILETYPE_PEM, CERT_TYPE,
  5034. ssl, NULL, 1);
  5035. }
  5036. /* unload any certs or keys that SSL owns, leave CTX as is
  5037. SSL_SUCCESS on ok */
  5038. int CyaSSL_UnloadCertsKeys(CYASSL* ssl)
  5039. {
  5040. if (ssl == NULL) {
  5041. CYASSL_MSG("Null function arg");
  5042. return BAD_FUNC_ARG;
  5043. }
  5044. if (ssl->buffers.weOwnCert) {
  5045. CYASSL_MSG("Unloading cert");
  5046. XFREE(ssl->buffers.certificate.buffer, ssl->heap,DYNAMIC_TYPE_CERT);
  5047. ssl->buffers.weOwnCert = 0;
  5048. ssl->buffers.certificate.length = 0;
  5049. ssl->buffers.certificate.buffer = NULL;
  5050. }
  5051. if (ssl->buffers.weOwnCertChain) {
  5052. CYASSL_MSG("Unloading cert chain");
  5053. XFREE(ssl->buffers.certChain.buffer, ssl->heap,DYNAMIC_TYPE_CERT);
  5054. ssl->buffers.weOwnCertChain = 0;
  5055. ssl->buffers.certChain.length = 0;
  5056. ssl->buffers.certChain.buffer = NULL;
  5057. }
  5058. if (ssl->buffers.weOwnKey) {
  5059. CYASSL_MSG("Unloading key");
  5060. XFREE(ssl->buffers.key.buffer, ssl->heap, DYNAMIC_TYPE_KEY);
  5061. ssl->buffers.weOwnKey = 0;
  5062. ssl->buffers.key.length = 0;
  5063. ssl->buffers.key.buffer = NULL;
  5064. }
  5065. return SSL_SUCCESS;
  5066. }
  5067. int CyaSSL_CTX_UnloadCAs(CYASSL_CTX* ctx)
  5068. {
  5069. CYASSL_ENTER("CyaSSL_CTX_UnloadCAs");
  5070. if (ctx == NULL)
  5071. return BAD_FUNC_ARG;
  5072. return CyaSSL_CertManagerUnloadCAs(ctx->cm);
  5073. }
  5074. /* old NO_FILESYSTEM end */
  5075. #endif /* !NO_CERTS */
  5076. #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
  5077. int CyaSSL_add_all_algorithms(void)
  5078. {
  5079. CYASSL_ENTER("CyaSSL_add_all_algorithms");
  5080. CyaSSL_Init();
  5081. return SSL_SUCCESS;
  5082. }
  5083. long CyaSSL_CTX_sess_set_cache_size(CYASSL_CTX* ctx, long sz)
  5084. {
  5085. /* cache size fixed at compile time in CyaSSL */
  5086. (void)ctx;
  5087. (void)sz;
  5088. return 0;
  5089. }
  5090. void CyaSSL_CTX_set_quiet_shutdown(CYASSL_CTX* ctx, int mode)
  5091. {
  5092. CYASSL_ENTER("CyaSSL_CTX_set_quiet_shutdown");
  5093. if (mode)
  5094. ctx->quietShutdown = 1;
  5095. }
  5096. void CyaSSL_set_quiet_shutdown(CYASSL* ssl, int mode)
  5097. {
  5098. CYASSL_ENTER("CyaSSL_CTX_set_quiet_shutdown");
  5099. if (mode)
  5100. ssl->options.quietShutdown = 1;
  5101. }
  5102. void CyaSSL_set_bio(CYASSL* ssl, CYASSL_BIO* rd, CYASSL_BIO* wr)
  5103. {
  5104. CYASSL_ENTER("SSL_set_bio");
  5105. CyaSSL_set_rfd(ssl, rd->fd);
  5106. CyaSSL_set_wfd(ssl, wr->fd);
  5107. ssl->biord = rd;
  5108. ssl->biowr = wr;
  5109. }
  5110. void CyaSSL_CTX_set_client_CA_list(CYASSL_CTX* ctx,
  5111. STACK_OF(CYASSL_X509_NAME)* names)
  5112. {
  5113. (void)ctx;
  5114. (void)names;
  5115. }
  5116. STACK_OF(CYASSL_X509_NAME)* CyaSSL_load_client_CA_file(const char* fname)
  5117. {
  5118. (void)fname;
  5119. return 0;
  5120. }
  5121. int CyaSSL_CTX_set_default_verify_paths(CYASSL_CTX* ctx)
  5122. {
  5123. /* TODO:, not needed in goahead */
  5124. (void)ctx;
  5125. return SSL_NOT_IMPLEMENTED;
  5126. }
  5127. /* keyblock size in bytes or -1 */
  5128. int CyaSSL_get_keyblock_size(CYASSL* ssl)
  5129. {
  5130. if (ssl == NULL)
  5131. return SSL_FATAL_ERROR;
  5132. return 2 * (ssl->specs.key_size + ssl->specs.iv_size +
  5133. ssl->specs.hash_size);
  5134. }
  5135. /* store keys returns SSL_SUCCESS or -1 on error */
  5136. int CyaSSL_get_keys(CYASSL* ssl, unsigned char** ms, unsigned int* msLen,
  5137. unsigned char** sr, unsigned int* srLen,
  5138. unsigned char** cr, unsigned int* crLen)
  5139. {
  5140. if (ssl == NULL || ssl->arrays == NULL)
  5141. return SSL_FATAL_ERROR;
  5142. *ms = ssl->arrays->masterSecret;
  5143. *sr = ssl->arrays->serverRandom;
  5144. *cr = ssl->arrays->clientRandom;
  5145. *msLen = SECRET_LEN;
  5146. *srLen = RAN_LEN;
  5147. *crLen = RAN_LEN;
  5148. return SSL_SUCCESS;
  5149. }
  5150. void CyaSSL_set_accept_state(CYASSL* ssl)
  5151. {
  5152. byte haveRSA = 1;
  5153. byte havePSK = 0;
  5154. CYASSL_ENTER("SSL_set_accept_state");
  5155. ssl->options.side = CYASSL_SERVER_END;
  5156. /* reset suites in case user switched */
  5157. #ifdef NO_RSA
  5158. haveRSA = 0;
  5159. #endif
  5160. #ifndef NO_PSK
  5161. havePSK = ssl->options.havePSK;
  5162. #endif
  5163. InitSuites(ssl->suites, ssl->version, haveRSA, havePSK,
  5164. ssl->options.haveDH, ssl->options.haveNTRU,
  5165. ssl->options.haveECDSAsig, ssl->options.haveStaticECC,
  5166. ssl->options.side);
  5167. }
  5168. #endif
  5169. /* return true if connection established */
  5170. int CyaSSL_is_init_finished(CYASSL* ssl)
  5171. {
  5172. if (ssl == NULL)
  5173. return 0;
  5174. if (ssl->options.handShakeState == HANDSHAKE_DONE)
  5175. return 1;
  5176. return 0;
  5177. }
  5178. #if defined(OPENSSL_EXTRA) || defined(GOAHEAD_WS)
  5179. void CyaSSL_CTX_set_tmp_rsa_callback(CYASSL_CTX* ctx,
  5180. CYASSL_RSA*(*f)(CYASSL*, int, int))
  5181. {
  5182. /* CyaSSL verifies all these internally */
  5183. (void)ctx;
  5184. (void)f;
  5185. }
  5186. void CyaSSL_set_shutdown(CYASSL* ssl, int opt)
  5187. {
  5188. (void)ssl;
  5189. (void)opt;
  5190. }
  5191. long CyaSSL_CTX_set_options(CYASSL_CTX* ctx, long opt)
  5192. {
  5193. /* goahead calls with 0, do nothing */
  5194. CYASSL_ENTER("SSL_CTX_set_options");
  5195. (void)ctx;
  5196. return opt;
  5197. }
  5198. int CyaSSL_set_rfd(CYASSL* ssl, int rfd)
  5199. {
  5200. CYASSL_ENTER("SSL_set_rfd");
  5201. ssl->rfd = rfd; /* not used directly to allow IO callbacks */
  5202. ssl->IOCB_ReadCtx = &ssl->rfd;
  5203. return SSL_SUCCESS;
  5204. }
  5205. int CyaSSL_set_wfd(CYASSL* ssl, int wfd)
  5206. {
  5207. CYASSL_ENTER("SSL_set_wfd");
  5208. ssl->wfd = wfd; /* not used directly to allow IO callbacks */
  5209. ssl->IOCB_WriteCtx = &ssl->wfd;
  5210. return SSL_SUCCESS;
  5211. }
  5212. CYASSL_RSA* CyaSSL_RSA_generate_key(int len, unsigned long bits,
  5213. void(*f)(int, int, void*), void* data)
  5214. {
  5215. /* no tmp key needed, actual generation not supported */
  5216. CYASSL_ENTER("RSA_generate_key");
  5217. (void)len;
  5218. (void)bits;
  5219. (void)f;
  5220. (void)data;
  5221. return NULL;
  5222. }
  5223. CYASSL_X509* CyaSSL_X509_STORE_CTX_get_current_cert(
  5224. CYASSL_X509_STORE_CTX* ctx)
  5225. {
  5226. (void)ctx;
  5227. return 0;
  5228. }
  5229. int CyaSSL_X509_STORE_CTX_get_error(CYASSL_X509_STORE_CTX* ctx)
  5230. {
  5231. if (ctx != NULL)
  5232. return ctx->error;
  5233. return 0;
  5234. }
  5235. int CyaSSL_X509_STORE_CTX_get_error_depth(CYASSL_X509_STORE_CTX* ctx)
  5236. {
  5237. (void)ctx;
  5238. return 0;
  5239. }
  5240. CYASSL_BIO_METHOD* CyaSSL_BIO_f_buffer(void)
  5241. {
  5242. static CYASSL_BIO_METHOD meth;
  5243. CYASSL_ENTER("BIO_f_buffer");
  5244. meth.type = BIO_BUFFER;
  5245. return &meth;
  5246. }
  5247. long CyaSSL_BIO_set_write_buffer_size(CYASSL_BIO* bio, long size)
  5248. {
  5249. /* CyaSSL has internal buffer, compatibility only */
  5250. CYASSL_ENTER("BIO_set_write_buffer_size");
  5251. (void)bio;
  5252. return size;
  5253. }
  5254. CYASSL_BIO_METHOD* CyaSSL_BIO_f_ssl(void)
  5255. {
  5256. static CYASSL_BIO_METHOD meth;
  5257. CYASSL_ENTER("BIO_f_ssl");
  5258. meth.type = BIO_SSL;
  5259. return &meth;
  5260. }
  5261. CYASSL_BIO* CyaSSL_BIO_new_socket(int sfd, int closeF)
  5262. {
  5263. CYASSL_BIO* bio = (CYASSL_BIO*) XMALLOC(sizeof(CYASSL_BIO), 0,
  5264. DYNAMIC_TYPE_OPENSSL);
  5265. CYASSL_ENTER("BIO_new_socket");
  5266. if (bio) {
  5267. bio->type = BIO_SOCKET;
  5268. bio->close = (byte)closeF;
  5269. bio->eof = 0;
  5270. bio->ssl = 0;
  5271. bio->fd = sfd;
  5272. bio->prev = 0;
  5273. bio->next = 0;
  5274. bio->mem = NULL;
  5275. bio->memLen = 0;
  5276. }
  5277. return bio;
  5278. }
  5279. int CyaSSL_BIO_eof(CYASSL_BIO* b)
  5280. {
  5281. CYASSL_ENTER("BIO_eof");
  5282. if (b->eof)
  5283. return 1;
  5284. return 0;
  5285. }
  5286. long CyaSSL_BIO_set_ssl(CYASSL_BIO* b, CYASSL* ssl, int closeF)
  5287. {
  5288. CYASSL_ENTER("BIO_set_ssl");
  5289. b->ssl = ssl;
  5290. b->close = (byte)closeF;
  5291. /* add to ssl for bio free if SSL_free called before/instead of free_all? */
  5292. return 0;
  5293. }
  5294. CYASSL_BIO* CyaSSL_BIO_new(CYASSL_BIO_METHOD* method)
  5295. {
  5296. CYASSL_BIO* bio = (CYASSL_BIO*) XMALLOC(sizeof(CYASSL_BIO), 0,
  5297. DYNAMIC_TYPE_OPENSSL);
  5298. CYASSL_ENTER("BIO_new");
  5299. if (bio) {
  5300. bio->type = method->type;
  5301. bio->close = 0;
  5302. bio->eof = 0;
  5303. bio->ssl = NULL;
  5304. bio->mem = NULL;
  5305. bio->memLen = 0;
  5306. bio->fd = 0;
  5307. bio->prev = NULL;
  5308. bio->next = NULL;
  5309. }
  5310. return bio;
  5311. }
  5312. int CyaSSL_BIO_get_mem_data(CYASSL_BIO* bio, const byte** p)
  5313. {
  5314. if (bio == NULL || p == NULL)
  5315. return SSL_FATAL_ERROR;
  5316. *p = bio->mem;
  5317. return bio->memLen;
  5318. }
  5319. CYASSL_BIO* CyaSSL_BIO_new_mem_buf(void* buf, int len)
  5320. {
  5321. CYASSL_BIO* bio = NULL;
  5322. if (buf == NULL)
  5323. return bio;
  5324. bio = CyaSSL_BIO_new(CyaSSL_BIO_s_mem());
  5325. if (bio == NULL)
  5326. return bio;
  5327. bio->memLen = len;
  5328. bio->mem = (byte*)XMALLOC(len, 0, DYNAMIC_TYPE_OPENSSL);
  5329. if (bio->mem == NULL) {
  5330. XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL);
  5331. return NULL;
  5332. }
  5333. XMEMCPY(bio->mem, buf, len);
  5334. return bio;
  5335. }
  5336. #ifdef USE_WINDOWS_API
  5337. #define CloseSocket(s) closesocket(s)
  5338. #elif defined(CYASSL_MDK_ARM)
  5339. #define CloseSocket(s) closesocket(s)
  5340. extern int closesocket(int) ;
  5341. #else
  5342. #define CloseSocket(s) close(s)
  5343. #endif
  5344. int CyaSSL_BIO_free(CYASSL_BIO* bio)
  5345. {
  5346. /* unchain?, doesn't matter in goahead since from free all */
  5347. CYASSL_ENTER("BIO_free");
  5348. if (bio) {
  5349. if (bio->close) {
  5350. if (bio->ssl)
  5351. CyaSSL_free(bio->ssl);
  5352. if (bio->fd)
  5353. CloseSocket(bio->fd);
  5354. }
  5355. if (bio->mem)
  5356. XFREE(bio->mem, 0, DYNAMIC_TYPE_OPENSSL);
  5357. XFREE(bio, 0, DYNAMIC_TYPE_OPENSSL);
  5358. }
  5359. return 0;
  5360. }
  5361. int CyaSSL_BIO_free_all(CYASSL_BIO* bio)
  5362. {
  5363. CYASSL_ENTER("BIO_free_all");
  5364. while (bio) {
  5365. CYASSL_BIO* next = bio->next;
  5366. CyaSSL_BIO_free(bio);
  5367. bio = next;
  5368. }
  5369. return 0;
  5370. }
  5371. int CyaSSL_BIO_read(CYASSL_BIO* bio, void* buf, int len)
  5372. {
  5373. int ret;
  5374. CYASSL* ssl = 0;
  5375. CYASSL_BIO* front = bio;
  5376. CYASSL_ENTER("BIO_read");
  5377. /* already got eof, again is error */
  5378. if (front->eof)
  5379. return SSL_FATAL_ERROR;
  5380. while(bio && ((ssl = bio->ssl) == 0) )
  5381. bio = bio->next;
  5382. if (ssl == 0) return BAD_FUNC_ARG;
  5383. ret = CyaSSL_read(ssl, buf, len);
  5384. if (ret == 0)
  5385. front->eof = 1;
  5386. else if (ret < 0) {
  5387. int err = CyaSSL_get_error(ssl, 0);
  5388. if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) )
  5389. front->eof = 1;
  5390. }
  5391. return ret;
  5392. }
  5393. int CyaSSL_BIO_write(CYASSL_BIO* bio, const void* data, int len)
  5394. {
  5395. int ret;
  5396. CYASSL* ssl = 0;
  5397. CYASSL_BIO* front = bio;
  5398. CYASSL_ENTER("BIO_write");
  5399. /* already got eof, again is error */
  5400. if (front->eof)
  5401. return SSL_FATAL_ERROR;
  5402. while(bio && ((ssl = bio->ssl) == 0) )
  5403. bio = bio->next;
  5404. if (ssl == 0) return BAD_FUNC_ARG;
  5405. ret = CyaSSL_write(ssl, data, len);
  5406. if (ret == 0)
  5407. front->eof = 1;
  5408. else if (ret < 0) {
  5409. int err = CyaSSL_get_error(ssl, 0);
  5410. if ( !(err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) )
  5411. front->eof = 1;
  5412. }
  5413. return ret;
  5414. }
  5415. CYASSL_BIO* CyaSSL_BIO_push(CYASSL_BIO* top, CYASSL_BIO* append)
  5416. {
  5417. CYASSL_ENTER("BIO_push");
  5418. top->next = append;
  5419. append->prev = top;
  5420. return top;
  5421. }
  5422. int CyaSSL_BIO_flush(CYASSL_BIO* bio)
  5423. {
  5424. /* for CyaSSL no flushing needed */
  5425. CYASSL_ENTER("BIO_flush");
  5426. (void)bio;
  5427. return 1;
  5428. }
  5429. #endif /* OPENSSL_EXTRA || GOAHEAD_WS */
  5430. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  5431. void CyaSSL_CTX_set_default_passwd_cb_userdata(CYASSL_CTX* ctx,
  5432. void* userdata)
  5433. {
  5434. CYASSL_ENTER("SSL_CTX_set_default_passwd_cb_userdata");
  5435. ctx->userdata = userdata;
  5436. }
  5437. void CyaSSL_CTX_set_default_passwd_cb(CYASSL_CTX* ctx, pem_password_cb cb)
  5438. {
  5439. CYASSL_ENTER("SSL_CTX_set_default_passwd_cb");
  5440. ctx->passwd_cb = cb;
  5441. }
  5442. int CyaSSL_num_locks(void)
  5443. {
  5444. return 0;
  5445. }
  5446. void CyaSSL_set_locking_callback(void (*f)(int, int, const char*, int))
  5447. {
  5448. (void)f;
  5449. }
  5450. void CyaSSL_set_id_callback(unsigned long (*f)(void))
  5451. {
  5452. (void)f;
  5453. }
  5454. unsigned long CyaSSL_ERR_get_error(void)
  5455. {
  5456. /* TODO: */
  5457. return 0;
  5458. }
  5459. int CyaSSL_EVP_BytesToKey(const CYASSL_EVP_CIPHER* type,
  5460. const CYASSL_EVP_MD* md, const byte* salt,
  5461. const byte* data, int sz, int count, byte* key, byte* iv)
  5462. {
  5463. int keyLen = 0;
  5464. int ivLen = 0;
  5465. int j;
  5466. int keyLeft;
  5467. int ivLeft;
  5468. int keyOutput = 0;
  5469. byte digest[MD5_DIGEST_SIZE];
  5470. #ifdef CYASSL_SMALL_STACK
  5471. Md5* md5 = NULL;
  5472. #else
  5473. Md5 md5[1];
  5474. #endif
  5475. #ifdef CYASSL_SMALL_STACK
  5476. md5 = (Md5*)XMALLOC(sizeof(Md5), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5477. if (md5 == NULL)
  5478. return 0;
  5479. #endif
  5480. CYASSL_ENTER("EVP_BytesToKey");
  5481. InitMd5(md5);
  5482. /* only support MD5 for now */
  5483. if (XSTRNCMP(md, "MD5", 3) != 0) return 0;
  5484. /* only support CBC DES and AES for now */
  5485. if (XSTRNCMP(type, "DES-CBC", 7) == 0) {
  5486. keyLen = DES_KEY_SIZE;
  5487. ivLen = DES_IV_SIZE;
  5488. }
  5489. else if (XSTRNCMP(type, "DES-EDE3-CBC", 12) == 0) {
  5490. keyLen = DES3_KEY_SIZE;
  5491. ivLen = DES_IV_SIZE;
  5492. }
  5493. else if (XSTRNCMP(type, "AES-128-CBC", 11) == 0) {
  5494. keyLen = AES_128_KEY_SIZE;
  5495. ivLen = AES_IV_SIZE;
  5496. }
  5497. else if (XSTRNCMP(type, "AES-192-CBC", 11) == 0) {
  5498. keyLen = AES_192_KEY_SIZE;
  5499. ivLen = AES_IV_SIZE;
  5500. }
  5501. else if (XSTRNCMP(type, "AES-256-CBC", 11) == 0) {
  5502. keyLen = AES_256_KEY_SIZE;
  5503. ivLen = AES_IV_SIZE;
  5504. }
  5505. else {
  5506. #ifdef CYASSL_SMALL_STACK
  5507. XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5508. #endif
  5509. return 0;
  5510. }
  5511. keyLeft = keyLen;
  5512. ivLeft = ivLen;
  5513. while (keyOutput < (keyLen + ivLen)) {
  5514. int digestLeft = MD5_DIGEST_SIZE;
  5515. /* D_(i - 1) */
  5516. if (keyOutput) /* first time D_0 is empty */
  5517. Md5Update(md5, digest, MD5_DIGEST_SIZE);
  5518. /* data */
  5519. Md5Update(md5, data, sz);
  5520. /* salt */
  5521. if (salt)
  5522. Md5Update(md5, salt, EVP_SALT_SIZE);
  5523. Md5Final(md5, digest);
  5524. /* count */
  5525. for (j = 1; j < count; j++) {
  5526. Md5Update(md5, digest, MD5_DIGEST_SIZE);
  5527. Md5Final(md5, digest);
  5528. }
  5529. if (keyLeft) {
  5530. int store = min(keyLeft, MD5_DIGEST_SIZE);
  5531. XMEMCPY(&key[keyLen - keyLeft], digest, store);
  5532. keyOutput += store;
  5533. keyLeft -= store;
  5534. digestLeft -= store;
  5535. }
  5536. if (ivLeft && digestLeft) {
  5537. int store = min(ivLeft, digestLeft);
  5538. XMEMCPY(&iv[ivLen - ivLeft], &digest[MD5_DIGEST_SIZE -
  5539. digestLeft], store);
  5540. keyOutput += store;
  5541. ivLeft -= store;
  5542. }
  5543. }
  5544. #ifdef CYASSL_SMALL_STACK
  5545. XFREE(md5, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  5546. #endif
  5547. return keyOutput == (keyLen + ivLen) ? keyOutput : 0;
  5548. }
  5549. #endif /* OPENSSL_EXTRA || HAVE_WEBSERVER */
  5550. #ifdef OPENSSL_EXTRA
  5551. unsigned long CyaSSLeay(void)
  5552. {
  5553. return SSLEAY_VERSION_NUMBER;
  5554. }
  5555. const char* CyaSSLeay_version(int type)
  5556. {
  5557. static const char* version = "SSLeay CyaSSL compatibility";
  5558. (void)type;
  5559. return version;
  5560. }
  5561. void CyaSSL_MD5_Init(CYASSL_MD5_CTX* md5)
  5562. {
  5563. typedef char md5_test[sizeof(MD5_CTX) >= sizeof(Md5) ? 1 : -1];
  5564. (void)sizeof(md5_test);
  5565. CYASSL_ENTER("MD5_Init");
  5566. InitMd5((Md5*)md5);
  5567. }
  5568. void CyaSSL_MD5_Update(CYASSL_MD5_CTX* md5, const void* input,
  5569. unsigned long sz)
  5570. {
  5571. CYASSL_ENTER("CyaSSL_MD5_Update");
  5572. Md5Update((Md5*)md5, (const byte*)input, (word32)sz);
  5573. }
  5574. void CyaSSL_MD5_Final(byte* input, CYASSL_MD5_CTX* md5)
  5575. {
  5576. CYASSL_ENTER("MD5_Final");
  5577. Md5Final((Md5*)md5, input);
  5578. }
  5579. void CyaSSL_SHA_Init(CYASSL_SHA_CTX* sha)
  5580. {
  5581. typedef char sha_test[sizeof(SHA_CTX) >= sizeof(Sha) ? 1 : -1];
  5582. (void)sizeof(sha_test);
  5583. CYASSL_ENTER("SHA_Init");
  5584. InitSha((Sha*)sha); /* OpenSSL compat, no ret */
  5585. }
  5586. void CyaSSL_SHA_Update(CYASSL_SHA_CTX* sha, const void* input,
  5587. unsigned long sz)
  5588. {
  5589. CYASSL_ENTER("SHA_Update");
  5590. ShaUpdate((Sha*)sha, (const byte*)input, (word32)sz);
  5591. }
  5592. void CyaSSL_SHA_Final(byte* input, CYASSL_SHA_CTX* sha)
  5593. {
  5594. CYASSL_ENTER("SHA_Final");
  5595. ShaFinal((Sha*)sha, input);
  5596. }
  5597. void CyaSSL_SHA1_Init(CYASSL_SHA_CTX* sha)
  5598. {
  5599. CYASSL_ENTER("SHA1_Init");
  5600. SHA_Init(sha);
  5601. }
  5602. void CyaSSL_SHA1_Update(CYASSL_SHA_CTX* sha, const void* input,
  5603. unsigned long sz)
  5604. {
  5605. CYASSL_ENTER("SHA1_Update");
  5606. SHA_Update(sha, input, sz);
  5607. }
  5608. void CyaSSL_SHA1_Final(byte* input, CYASSL_SHA_CTX* sha)
  5609. {
  5610. CYASSL_ENTER("SHA1_Final");
  5611. SHA_Final(input, sha);
  5612. }
  5613. void CyaSSL_SHA256_Init(CYASSL_SHA256_CTX* sha256)
  5614. {
  5615. typedef char sha_test[sizeof(SHA256_CTX) >= sizeof(Sha256) ? 1 : -1];
  5616. (void)sizeof(sha_test);
  5617. CYASSL_ENTER("SHA256_Init");
  5618. InitSha256((Sha256*)sha256); /* OpenSSL compat, no error */
  5619. }
  5620. void CyaSSL_SHA256_Update(CYASSL_SHA256_CTX* sha, const void* input,
  5621. unsigned long sz)
  5622. {
  5623. CYASSL_ENTER("SHA256_Update");
  5624. Sha256Update((Sha256*)sha, (const byte*)input, (word32)sz);
  5625. /* OpenSSL compat, no error */
  5626. }
  5627. void CyaSSL_SHA256_Final(byte* input, CYASSL_SHA256_CTX* sha)
  5628. {
  5629. CYASSL_ENTER("SHA256_Final");
  5630. Sha256Final((Sha256*)sha, input);
  5631. /* OpenSSL compat, no error */
  5632. }
  5633. #ifdef CYASSL_SHA384
  5634. void CyaSSL_SHA384_Init(CYASSL_SHA384_CTX* sha)
  5635. {
  5636. typedef char sha_test[sizeof(SHA384_CTX) >= sizeof(Sha384) ? 1 : -1];
  5637. (void)sizeof(sha_test);
  5638. CYASSL_ENTER("SHA384_Init");
  5639. InitSha384((Sha384*)sha); /* OpenSSL compat, no error */
  5640. }
  5641. void CyaSSL_SHA384_Update(CYASSL_SHA384_CTX* sha, const void* input,
  5642. unsigned long sz)
  5643. {
  5644. CYASSL_ENTER("SHA384_Update");
  5645. Sha384Update((Sha384*)sha, (const byte*)input, (word32)sz);
  5646. /* OpenSSL compat, no error */
  5647. }
  5648. void CyaSSL_SHA384_Final(byte* input, CYASSL_SHA384_CTX* sha)
  5649. {
  5650. CYASSL_ENTER("SHA384_Final");
  5651. Sha384Final((Sha384*)sha, input);
  5652. /* OpenSSL compat, no error */
  5653. }
  5654. #endif /* CYASSL_SHA384 */
  5655. #ifdef CYASSL_SHA512
  5656. void CyaSSL_SHA512_Init(CYASSL_SHA512_CTX* sha)
  5657. {
  5658. typedef char sha_test[sizeof(SHA512_CTX) >= sizeof(Sha512) ? 1 : -1];
  5659. (void)sizeof(sha_test);
  5660. CYASSL_ENTER("SHA512_Init");
  5661. InitSha512((Sha512*)sha); /* OpenSSL compat, no error */
  5662. }
  5663. void CyaSSL_SHA512_Update(CYASSL_SHA512_CTX* sha, const void* input,
  5664. unsigned long sz)
  5665. {
  5666. CYASSL_ENTER("SHA512_Update");
  5667. Sha512Update((Sha512*)sha, (const byte*)input, (word32)sz);
  5668. /* OpenSSL compat, no error */
  5669. }
  5670. void CyaSSL_SHA512_Final(byte* input, CYASSL_SHA512_CTX* sha)
  5671. {
  5672. CYASSL_ENTER("SHA512_Final");
  5673. Sha512Final((Sha512*)sha, input);
  5674. /* OpenSSL compat, no error */
  5675. }
  5676. #endif /* CYASSL_SHA512 */
  5677. const CYASSL_EVP_MD* CyaSSL_EVP_md5(void)
  5678. {
  5679. static const char* type = "MD5";
  5680. CYASSL_ENTER("EVP_md5");
  5681. return type;
  5682. }
  5683. const CYASSL_EVP_MD* CyaSSL_EVP_sha1(void)
  5684. {
  5685. static const char* type = "SHA";
  5686. CYASSL_ENTER("EVP_sha1");
  5687. return type;
  5688. }
  5689. const CYASSL_EVP_MD* CyaSSL_EVP_sha256(void)
  5690. {
  5691. static const char* type = "SHA256";
  5692. CYASSL_ENTER("EVP_sha256");
  5693. return type;
  5694. }
  5695. #ifdef CYASSL_SHA384
  5696. const CYASSL_EVP_MD* CyaSSL_EVP_sha384(void)
  5697. {
  5698. static const char* type = "SHA384";
  5699. CYASSL_ENTER("EVP_sha384");
  5700. return type;
  5701. }
  5702. #endif /* CYASSL_SHA384 */
  5703. #ifdef CYASSL_SHA512
  5704. const CYASSL_EVP_MD* CyaSSL_EVP_sha512(void)
  5705. {
  5706. static const char* type = "SHA512";
  5707. CYASSL_ENTER("EVP_sha512");
  5708. return type;
  5709. }
  5710. #endif /* CYASSL_SHA512 */
  5711. void CyaSSL_EVP_MD_CTX_init(CYASSL_EVP_MD_CTX* ctx)
  5712. {
  5713. CYASSL_ENTER("EVP_CIPHER_MD_CTX_init");
  5714. (void)ctx;
  5715. /* do nothing */
  5716. }
  5717. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_128_cbc(void)
  5718. {
  5719. static const char* type = "AES128-CBC";
  5720. CYASSL_ENTER("CyaSSL_EVP_aes_128_cbc");
  5721. return type;
  5722. }
  5723. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_192_cbc(void)
  5724. {
  5725. static const char* type = "AES192-CBC";
  5726. CYASSL_ENTER("CyaSSL_EVP_aes_192_cbc");
  5727. return type;
  5728. }
  5729. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_256_cbc(void)
  5730. {
  5731. static const char* type = "AES256-CBC";
  5732. CYASSL_ENTER("CyaSSL_EVP_aes_256_cbc");
  5733. return type;
  5734. }
  5735. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_128_ctr(void)
  5736. {
  5737. static const char* type = "AES128-CTR";
  5738. CYASSL_ENTER("CyaSSL_EVP_aes_128_ctr");
  5739. return type;
  5740. }
  5741. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_192_ctr(void)
  5742. {
  5743. static const char* type = "AES192-CTR";
  5744. CYASSL_ENTER("CyaSSL_EVP_aes_192_ctr");
  5745. return type;
  5746. }
  5747. const CYASSL_EVP_CIPHER* CyaSSL_EVP_aes_256_ctr(void)
  5748. {
  5749. static const char* type = "AES256-CTR";
  5750. CYASSL_ENTER("CyaSSL_EVP_aes_256_ctr");
  5751. return type;
  5752. }
  5753. const CYASSL_EVP_CIPHER* CyaSSL_EVP_des_cbc(void)
  5754. {
  5755. static const char* type = "DES-CBC";
  5756. CYASSL_ENTER("CyaSSL_EVP_des_cbc");
  5757. return type;
  5758. }
  5759. const CYASSL_EVP_CIPHER* CyaSSL_EVP_des_ede3_cbc(void)
  5760. {
  5761. static const char* type = "DES-EDE3-CBC";
  5762. CYASSL_ENTER("CyaSSL_EVP_des_ede3_cbc");
  5763. return type;
  5764. }
  5765. const CYASSL_EVP_CIPHER* CyaSSL_EVP_rc4(void)
  5766. {
  5767. static const char* type = "ARC4";
  5768. CYASSL_ENTER("CyaSSL_EVP_rc4");
  5769. return type;
  5770. }
  5771. const CYASSL_EVP_CIPHER* CyaSSL_EVP_enc_null(void)
  5772. {
  5773. static const char* type = "NULL";
  5774. CYASSL_ENTER("CyaSSL_EVP_enc_null");
  5775. return type;
  5776. }
  5777. int CyaSSL_EVP_MD_CTX_cleanup(CYASSL_EVP_MD_CTX* ctx)
  5778. {
  5779. CYASSL_ENTER("EVP_MD_CTX_cleanup");
  5780. (void)ctx;
  5781. return 0;
  5782. }
  5783. void CyaSSL_EVP_CIPHER_CTX_init(CYASSL_EVP_CIPHER_CTX* ctx)
  5784. {
  5785. CYASSL_ENTER("EVP_CIPHER_CTX_init");
  5786. if (ctx) {
  5787. ctx->cipherType = 0xff; /* no init */
  5788. ctx->keyLen = 0;
  5789. ctx->enc = 1; /* start in encrypt mode */
  5790. }
  5791. }
  5792. /* SSL_SUCCESS on ok */
  5793. int CyaSSL_EVP_CIPHER_CTX_cleanup(CYASSL_EVP_CIPHER_CTX* ctx)
  5794. {
  5795. CYASSL_ENTER("EVP_CIPHER_CTX_cleanup");
  5796. if (ctx) {
  5797. ctx->cipherType = 0xff; /* no more init */
  5798. ctx->keyLen = 0;
  5799. }
  5800. return SSL_SUCCESS;
  5801. }
  5802. /* SSL_SUCCESS on ok */
  5803. int CyaSSL_EVP_CipherInit(CYASSL_EVP_CIPHER_CTX* ctx,
  5804. const CYASSL_EVP_CIPHER* type, byte* key,
  5805. byte* iv, int enc)
  5806. {
  5807. int ret = 0;
  5808. CYASSL_ENTER("CyaSSL_EVP_CipherInit");
  5809. if (ctx == NULL) {
  5810. CYASSL_MSG("no ctx");
  5811. return 0; /* failure */
  5812. }
  5813. if (type == NULL && ctx->cipherType == 0xff) {
  5814. CYASSL_MSG("no type set");
  5815. return 0; /* failure */
  5816. }
  5817. if (ctx->cipherType == AES_128_CBC_TYPE || (type &&
  5818. XSTRNCMP(type, "AES128-CBC", 10) == 0)) {
  5819. CYASSL_MSG("AES-128-CBC");
  5820. ctx->cipherType = AES_128_CBC_TYPE;
  5821. ctx->keyLen = 16;
  5822. if (enc == 0 || enc == 1)
  5823. ctx->enc = enc ? 1 : 0;
  5824. if (key) {
  5825. ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5826. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
  5827. if (ret != 0)
  5828. return ret;
  5829. }
  5830. if (iv && key == NULL) {
  5831. ret = AesSetIV(&ctx->cipher.aes, iv);
  5832. if (ret != 0)
  5833. return ret;
  5834. }
  5835. }
  5836. else if (ctx->cipherType == AES_192_CBC_TYPE || (type &&
  5837. XSTRNCMP(type, "AES192-CBC", 10) == 0)) {
  5838. CYASSL_MSG("AES-192-CBC");
  5839. ctx->cipherType = AES_192_CBC_TYPE;
  5840. ctx->keyLen = 24;
  5841. if (enc == 0 || enc == 1)
  5842. ctx->enc = enc ? 1 : 0;
  5843. if (key) {
  5844. ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5845. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
  5846. if (ret != 0)
  5847. return ret;
  5848. }
  5849. if (iv && key == NULL) {
  5850. ret = AesSetIV(&ctx->cipher.aes, iv);
  5851. if (ret != 0)
  5852. return ret;
  5853. }
  5854. }
  5855. else if (ctx->cipherType == AES_256_CBC_TYPE || (type &&
  5856. XSTRNCMP(type, "AES256-CBC", 10) == 0)) {
  5857. CYASSL_MSG("AES-256-CBC");
  5858. ctx->cipherType = AES_256_CBC_TYPE;
  5859. ctx->keyLen = 32;
  5860. if (enc == 0 || enc == 1)
  5861. ctx->enc = enc ? 1 : 0;
  5862. if (key) {
  5863. ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5864. ctx->enc ? AES_ENCRYPTION : AES_DECRYPTION);
  5865. if (ret != 0)
  5866. return ret;
  5867. }
  5868. if (iv && key == NULL) {
  5869. ret = AesSetIV(&ctx->cipher.aes, iv);
  5870. if (ret != 0)
  5871. return ret;
  5872. }
  5873. }
  5874. #ifdef CYASSL_AES_COUNTER
  5875. else if (ctx->cipherType == AES_128_CTR_TYPE || (type &&
  5876. XSTRNCMP(type, "AES128-CTR", 10) == 0)) {
  5877. CYASSL_MSG("AES-128-CTR");
  5878. ctx->cipherType = AES_128_CTR_TYPE;
  5879. ctx->keyLen = 16;
  5880. if (enc == 0 || enc == 1)
  5881. ctx->enc = enc ? 1 : 0;
  5882. if (key) {
  5883. ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5884. AES_ENCRYPTION);
  5885. if (ret != 0)
  5886. return ret;
  5887. }
  5888. if (iv && key == NULL) {
  5889. ret = AesSetIV(&ctx->cipher.aes, iv);
  5890. if (ret != 0)
  5891. return ret;
  5892. }
  5893. }
  5894. else if (ctx->cipherType == AES_192_CTR_TYPE || (type &&
  5895. XSTRNCMP(type, "AES192-CTR", 10) == 0)) {
  5896. CYASSL_MSG("AES-192-CTR");
  5897. ctx->cipherType = AES_192_CTR_TYPE;
  5898. ctx->keyLen = 24;
  5899. if (enc == 0 || enc == 1)
  5900. ctx->enc = enc ? 1 : 0;
  5901. if (key) {
  5902. ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5903. AES_ENCRYPTION);
  5904. if (ret != 0)
  5905. return ret;
  5906. }
  5907. if (iv && key == NULL) {
  5908. ret = AesSetIV(&ctx->cipher.aes, iv);
  5909. if (ret != 0)
  5910. return ret;
  5911. }
  5912. }
  5913. else if (ctx->cipherType == AES_256_CTR_TYPE || (type &&
  5914. XSTRNCMP(type, "AES256-CTR", 10) == 0)) {
  5915. CYASSL_MSG("AES-256-CTR");
  5916. ctx->cipherType = AES_256_CTR_TYPE;
  5917. ctx->keyLen = 32;
  5918. if (enc == 0 || enc == 1)
  5919. ctx->enc = enc ? 1 : 0;
  5920. if (key) {
  5921. ret = AesSetKey(&ctx->cipher.aes, key, ctx->keyLen, iv,
  5922. AES_ENCRYPTION);
  5923. if (ret != 0)
  5924. return ret;
  5925. }
  5926. if (iv && key == NULL) {
  5927. ret = AesSetIV(&ctx->cipher.aes, iv);
  5928. if (ret != 0)
  5929. return ret;
  5930. }
  5931. }
  5932. #endif /* CYASSL_AES_CTR */
  5933. else if (ctx->cipherType == DES_CBC_TYPE || (type &&
  5934. XSTRNCMP(type, "DES-CBC", 7) == 0)) {
  5935. CYASSL_MSG("DES-CBC");
  5936. ctx->cipherType = DES_CBC_TYPE;
  5937. ctx->keyLen = 8;
  5938. if (enc == 0 || enc == 1)
  5939. ctx->enc = enc ? 1 : 0;
  5940. if (key) {
  5941. ret = Des_SetKey(&ctx->cipher.des, key, iv,
  5942. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  5943. if (ret != 0)
  5944. return ret;
  5945. }
  5946. if (iv && key == NULL)
  5947. Des_SetIV(&ctx->cipher.des, iv);
  5948. }
  5949. else if (ctx->cipherType == DES_EDE3_CBC_TYPE || (type &&
  5950. XSTRNCMP(type, "DES-EDE3-CBC", 11) == 0)) {
  5951. CYASSL_MSG("DES-EDE3-CBC");
  5952. ctx->cipherType = DES_EDE3_CBC_TYPE;
  5953. ctx->keyLen = 24;
  5954. if (enc == 0 || enc == 1)
  5955. ctx->enc = enc ? 1 : 0;
  5956. if (key) {
  5957. ret = Des3_SetKey(&ctx->cipher.des3, key, iv,
  5958. ctx->enc ? DES_ENCRYPTION : DES_DECRYPTION);
  5959. if (ret != 0)
  5960. return ret;
  5961. }
  5962. if (iv && key == NULL) {
  5963. ret = Des3_SetIV(&ctx->cipher.des3, iv);
  5964. if (ret != 0)
  5965. return ret;
  5966. }
  5967. }
  5968. else if (ctx->cipherType == ARC4_TYPE || (type &&
  5969. XSTRNCMP(type, "ARC4", 4) == 0)) {
  5970. CYASSL_MSG("ARC4");
  5971. ctx->cipherType = ARC4_TYPE;
  5972. if (ctx->keyLen == 0) /* user may have already set */
  5973. ctx->keyLen = 16; /* default to 128 */
  5974. if (key)
  5975. Arc4SetKey(&ctx->cipher.arc4, key, ctx->keyLen);
  5976. }
  5977. else if (ctx->cipherType == NULL_CIPHER_TYPE || (type &&
  5978. XSTRNCMP(type, "NULL", 4) == 0)) {
  5979. CYASSL_MSG("NULL cipher");
  5980. ctx->cipherType = NULL_CIPHER_TYPE;
  5981. ctx->keyLen = 0;
  5982. }
  5983. else
  5984. return 0; /* failure */
  5985. return SSL_SUCCESS;
  5986. }
  5987. /* SSL_SUCCESS on ok */
  5988. int CyaSSL_EVP_CIPHER_CTX_key_length(CYASSL_EVP_CIPHER_CTX* ctx)
  5989. {
  5990. CYASSL_ENTER("CyaSSL_EVP_CIPHER_CTX_key_length");
  5991. if (ctx)
  5992. return ctx->keyLen;
  5993. return 0; /* failure */
  5994. }
  5995. /* SSL_SUCCESS on ok */
  5996. int CyaSSL_EVP_CIPHER_CTX_set_key_length(CYASSL_EVP_CIPHER_CTX* ctx,
  5997. int keylen)
  5998. {
  5999. CYASSL_ENTER("CyaSSL_EVP_CIPHER_CTX_set_key_length");
  6000. if (ctx)
  6001. ctx->keyLen = keylen;
  6002. else
  6003. return 0; /* failure */
  6004. return SSL_SUCCESS;
  6005. }
  6006. /* SSL_SUCCESS on ok */
  6007. int CyaSSL_EVP_Cipher(CYASSL_EVP_CIPHER_CTX* ctx, byte* dst, byte* src,
  6008. word32 len)
  6009. {
  6010. int ret = 0;
  6011. CYASSL_ENTER("CyaSSL_EVP_Cipher");
  6012. if (ctx == NULL || dst == NULL || src == NULL) {
  6013. CYASSL_MSG("Bad function argument");
  6014. return 0; /* failure */
  6015. }
  6016. if (ctx->cipherType == 0xff) {
  6017. CYASSL_MSG("no init");
  6018. return 0; /* failure */
  6019. }
  6020. switch (ctx->cipherType) {
  6021. case AES_128_CBC_TYPE :
  6022. case AES_192_CBC_TYPE :
  6023. case AES_256_CBC_TYPE :
  6024. CYASSL_MSG("AES CBC");
  6025. if (ctx->enc)
  6026. ret = AesCbcEncrypt(&ctx->cipher.aes, dst, src, len);
  6027. else
  6028. ret = AesCbcDecrypt(&ctx->cipher.aes, dst, src, len);
  6029. break;
  6030. #ifdef CYASSL_AES_COUNTER
  6031. case AES_128_CTR_TYPE :
  6032. case AES_192_CTR_TYPE :
  6033. case AES_256_CTR_TYPE :
  6034. CYASSL_MSG("AES CTR");
  6035. AesCtrEncrypt(&ctx->cipher.aes, dst, src, len);
  6036. break;
  6037. #endif
  6038. case DES_CBC_TYPE :
  6039. if (ctx->enc)
  6040. Des_CbcEncrypt(&ctx->cipher.des, dst, src, len);
  6041. else
  6042. Des_CbcDecrypt(&ctx->cipher.des, dst, src, len);
  6043. break;
  6044. case DES_EDE3_CBC_TYPE :
  6045. if (ctx->enc)
  6046. ret = Des3_CbcEncrypt(&ctx->cipher.des3, dst, src, len);
  6047. else
  6048. ret = Des3_CbcDecrypt(&ctx->cipher.des3, dst, src, len);
  6049. break;
  6050. case ARC4_TYPE :
  6051. Arc4Process(&ctx->cipher.arc4, dst, src, len);
  6052. break;
  6053. case NULL_CIPHER_TYPE :
  6054. XMEMCPY(dst, src, len);
  6055. break;
  6056. default: {
  6057. CYASSL_MSG("bad type");
  6058. return 0; /* failure */
  6059. }
  6060. }
  6061. if (ret != 0) {
  6062. CYASSL_MSG("CyaSSL_EVP_Cipher failure");
  6063. return 0; /* failuer */
  6064. }
  6065. CYASSL_MSG("CyaSSL_EVP_Cipher success");
  6066. return SSL_SUCCESS; /* success */
  6067. }
  6068. /* store for external read of iv, SSL_SUCCESS on success */
  6069. int CyaSSL_StoreExternalIV(CYASSL_EVP_CIPHER_CTX* ctx)
  6070. {
  6071. CYASSL_ENTER("CyaSSL_StoreExternalIV");
  6072. if (ctx == NULL) {
  6073. CYASSL_MSG("Bad function argument");
  6074. return SSL_FATAL_ERROR;
  6075. }
  6076. switch (ctx->cipherType) {
  6077. case AES_128_CBC_TYPE :
  6078. case AES_192_CBC_TYPE :
  6079. case AES_256_CBC_TYPE :
  6080. CYASSL_MSG("AES CBC");
  6081. memcpy(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  6082. break;
  6083. #ifdef CYASSL_AES_COUNTER
  6084. case AES_128_CTR_TYPE :
  6085. case AES_192_CTR_TYPE :
  6086. case AES_256_CTR_TYPE :
  6087. CYASSL_MSG("AES CTR");
  6088. memcpy(ctx->iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  6089. break;
  6090. #endif
  6091. case DES_CBC_TYPE :
  6092. CYASSL_MSG("DES CBC");
  6093. memcpy(ctx->iv, &ctx->cipher.des.reg, DES_BLOCK_SIZE);
  6094. break;
  6095. case DES_EDE3_CBC_TYPE :
  6096. CYASSL_MSG("DES EDE3 CBC");
  6097. memcpy(ctx->iv, &ctx->cipher.des.reg, DES_BLOCK_SIZE);
  6098. break;
  6099. case ARC4_TYPE :
  6100. CYASSL_MSG("ARC4");
  6101. break;
  6102. case NULL_CIPHER_TYPE :
  6103. CYASSL_MSG("NULL");
  6104. break;
  6105. default: {
  6106. CYASSL_MSG("bad type");
  6107. return SSL_FATAL_ERROR;
  6108. }
  6109. }
  6110. return SSL_SUCCESS;
  6111. }
  6112. /* set internal IV from external, SSL_SUCCESS on success */
  6113. int CyaSSL_SetInternalIV(CYASSL_EVP_CIPHER_CTX* ctx)
  6114. {
  6115. CYASSL_ENTER("CyaSSL_SetInternalIV");
  6116. if (ctx == NULL) {
  6117. CYASSL_MSG("Bad function argument");
  6118. return SSL_FATAL_ERROR;
  6119. }
  6120. switch (ctx->cipherType) {
  6121. case AES_128_CBC_TYPE :
  6122. case AES_192_CBC_TYPE :
  6123. case AES_256_CBC_TYPE :
  6124. CYASSL_MSG("AES CBC");
  6125. memcpy(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
  6126. break;
  6127. #ifdef CYASSL_AES_COUNTER
  6128. case AES_128_CTR_TYPE :
  6129. case AES_192_CTR_TYPE :
  6130. case AES_256_CTR_TYPE :
  6131. CYASSL_MSG("AES CTR");
  6132. memcpy(&ctx->cipher.aes.reg, ctx->iv, AES_BLOCK_SIZE);
  6133. break;
  6134. #endif
  6135. case DES_CBC_TYPE :
  6136. CYASSL_MSG("DES CBC");
  6137. memcpy(&ctx->cipher.des.reg, ctx->iv, DES_BLOCK_SIZE);
  6138. break;
  6139. case DES_EDE3_CBC_TYPE :
  6140. CYASSL_MSG("DES EDE3 CBC");
  6141. memcpy(&ctx->cipher.des.reg, ctx->iv, DES_BLOCK_SIZE);
  6142. break;
  6143. case ARC4_TYPE :
  6144. CYASSL_MSG("ARC4");
  6145. break;
  6146. case NULL_CIPHER_TYPE :
  6147. CYASSL_MSG("NULL");
  6148. break;
  6149. default: {
  6150. CYASSL_MSG("bad type");
  6151. return SSL_FATAL_ERROR;
  6152. }
  6153. }
  6154. return SSL_SUCCESS;
  6155. }
  6156. /* SSL_SUCCESS on ok */
  6157. int CyaSSL_EVP_DigestInit(CYASSL_EVP_MD_CTX* ctx, const CYASSL_EVP_MD* type)
  6158. {
  6159. CYASSL_ENTER("EVP_DigestInit");
  6160. if (XSTRNCMP(type, "MD5", 3) == 0) {
  6161. ctx->macType = MD5;
  6162. CyaSSL_MD5_Init((MD5_CTX*)&ctx->hash);
  6163. }
  6164. else if (XSTRNCMP(type, "SHA256", 6) == 0) {
  6165. ctx->macType = SHA256;
  6166. CyaSSL_SHA256_Init((SHA256_CTX*)&ctx->hash);
  6167. }
  6168. #ifdef CYASSL_SHA384
  6169. else if (XSTRNCMP(type, "SHA384", 6) == 0) {
  6170. ctx->macType = SHA384;
  6171. CyaSSL_SHA384_Init((SHA384_CTX*)&ctx->hash);
  6172. }
  6173. #endif
  6174. #ifdef CYASSL_SHA512
  6175. else if (XSTRNCMP(type, "SHA512", 6) == 0) {
  6176. ctx->macType = SHA512;
  6177. CyaSSL_SHA512_Init((SHA512_CTX*)&ctx->hash);
  6178. }
  6179. #endif
  6180. /* has to be last since would pick or 256, 384, or 512 too */
  6181. else if (XSTRNCMP(type, "SHA", 3) == 0) {
  6182. ctx->macType = SHA;
  6183. CyaSSL_SHA_Init((SHA_CTX*)&ctx->hash);
  6184. }
  6185. else
  6186. return BAD_FUNC_ARG;
  6187. return SSL_SUCCESS;
  6188. }
  6189. /* SSL_SUCCESS on ok */
  6190. int CyaSSL_EVP_DigestUpdate(CYASSL_EVP_MD_CTX* ctx, const void* data,
  6191. unsigned long sz)
  6192. {
  6193. CYASSL_ENTER("EVP_DigestUpdate");
  6194. if (ctx->macType == MD5)
  6195. CyaSSL_MD5_Update((MD5_CTX*)&ctx->hash, data, (unsigned long)sz);
  6196. else if (ctx->macType == SHA)
  6197. CyaSSL_SHA_Update((SHA_CTX*)&ctx->hash, data, (unsigned long)sz);
  6198. else if (ctx->macType == SHA256)
  6199. CyaSSL_SHA256_Update((SHA256_CTX*)&ctx->hash, data,
  6200. (unsigned long)sz);
  6201. #ifdef CYASSL_SHA384
  6202. else if (ctx->macType == SHA384)
  6203. CyaSSL_SHA384_Update((SHA384_CTX*)&ctx->hash, data,
  6204. (unsigned long)sz);
  6205. #endif
  6206. #ifdef CYASSL_SHA512
  6207. else if (ctx->macType == SHA512)
  6208. CyaSSL_SHA512_Update((SHA512_CTX*)&ctx->hash, data,
  6209. (unsigned long)sz);
  6210. #endif
  6211. else
  6212. return BAD_FUNC_ARG;
  6213. return SSL_SUCCESS;
  6214. }
  6215. /* SSL_SUCCESS on ok */
  6216. int CyaSSL_EVP_DigestFinal(CYASSL_EVP_MD_CTX* ctx, unsigned char* md,
  6217. unsigned int* s)
  6218. {
  6219. CYASSL_ENTER("EVP_DigestFinal");
  6220. if (ctx->macType == MD5) {
  6221. CyaSSL_MD5_Final(md, (MD5_CTX*)&ctx->hash);
  6222. if (s) *s = MD5_DIGEST_SIZE;
  6223. }
  6224. else if (ctx->macType == SHA) {
  6225. CyaSSL_SHA_Final(md, (SHA_CTX*)&ctx->hash);
  6226. if (s) *s = SHA_DIGEST_SIZE;
  6227. }
  6228. else if (ctx->macType == SHA256) {
  6229. CyaSSL_SHA256_Final(md, (SHA256_CTX*)&ctx->hash);
  6230. if (s) *s = SHA256_DIGEST_SIZE;
  6231. }
  6232. #ifdef CYASSL_SHA384
  6233. else if (ctx->macType == SHA384) {
  6234. CyaSSL_SHA384_Final(md, (SHA384_CTX*)&ctx->hash);
  6235. if (s) *s = SHA384_DIGEST_SIZE;
  6236. }
  6237. #endif
  6238. #ifdef CYASSL_SHA512
  6239. else if (ctx->macType == SHA512) {
  6240. CyaSSL_SHA512_Final(md, (SHA512_CTX*)&ctx->hash);
  6241. if (s) *s = SHA512_DIGEST_SIZE;
  6242. }
  6243. #endif
  6244. else
  6245. return BAD_FUNC_ARG;
  6246. return SSL_SUCCESS;
  6247. }
  6248. /* SSL_SUCCESS on ok */
  6249. int CyaSSL_EVP_DigestFinal_ex(CYASSL_EVP_MD_CTX* ctx, unsigned char* md,
  6250. unsigned int* s)
  6251. {
  6252. CYASSL_ENTER("EVP_DigestFinal_ex");
  6253. return EVP_DigestFinal(ctx, md, s);
  6254. }
  6255. unsigned char* CyaSSL_HMAC(const CYASSL_EVP_MD* evp_md, const void* key,
  6256. int key_len, const unsigned char* d, int n,
  6257. unsigned char* md, unsigned int* md_len)
  6258. {
  6259. int type;
  6260. unsigned char* ret = NULL;
  6261. #ifdef CYASSL_SMALL_STACK
  6262. Hmac* hmac = NULL;
  6263. #else
  6264. Hmac hmac[1];
  6265. #endif
  6266. CYASSL_ENTER("HMAC");
  6267. if (!md)
  6268. return NULL; /* no static buffer support */
  6269. if (XSTRNCMP(evp_md, "MD5", 3) == 0)
  6270. type = MD5;
  6271. else if (XSTRNCMP(evp_md, "SHA", 3) == 0)
  6272. type = MD5;
  6273. else
  6274. return NULL;
  6275. #ifdef CYASSL_SMALL_STACK
  6276. hmac = (Hmac*)XMALLOC(sizeof(Hmac), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6277. if (hmac == NULL)
  6278. return NULL;
  6279. #endif
  6280. if (HmacSetKey(hmac, type, (const byte*)key, key_len) == 0)
  6281. if (HmacUpdate(hmac, d, n) == 0)
  6282. if (HmacFinal(hmac, md) == 0) {
  6283. if (md_len)
  6284. *md_len = type == MD5 ? MD5_DIGEST_SIZE
  6285. : SHA_DIGEST_SIZE;
  6286. ret = md;
  6287. }
  6288. #ifdef CYASSL_SMALL_STACK
  6289. XFREE(hmac, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6290. #endif
  6291. return ret;
  6292. }
  6293. void CyaSSL_ERR_clear_error(void)
  6294. {
  6295. /* TODO: */
  6296. }
  6297. int CyaSSL_RAND_status(void)
  6298. {
  6299. return SSL_SUCCESS; /* CTaoCrypt provides enough seed internally */
  6300. }
  6301. void CyaSSL_RAND_add(const void* add, int len, double entropy)
  6302. {
  6303. (void)add;
  6304. (void)len;
  6305. (void)entropy;
  6306. /* CyaSSL seeds/adds internally, use explicit RNG if you want
  6307. to take control */
  6308. }
  6309. /* SSL_SUCCESS on ok */
  6310. int CyaSSL_DES_key_sched(CYASSL_const_DES_cblock* key,
  6311. CYASSL_DES_key_schedule* schedule)
  6312. {
  6313. CYASSL_ENTER("DES_key_sched");
  6314. XMEMCPY(schedule, key, sizeof(const_DES_cblock));
  6315. return SSL_SUCCESS;
  6316. }
  6317. void CyaSSL_DES_cbc_encrypt(const unsigned char* input,
  6318. unsigned char* output, long length,
  6319. CYASSL_DES_key_schedule* schedule, CYASSL_DES_cblock* ivec,
  6320. int enc)
  6321. {
  6322. Des myDes;
  6323. CYASSL_ENTER("DES_cbc_encrypt");
  6324. /* OpenSSL compat, no ret */
  6325. Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
  6326. if (enc)
  6327. Des_CbcEncrypt(&myDes, output, input, (word32)length);
  6328. else
  6329. Des_CbcDecrypt(&myDes, output, input, (word32)length);
  6330. }
  6331. /* correctly sets ivec for next call */
  6332. void CyaSSL_DES_ncbc_encrypt(const unsigned char* input,
  6333. unsigned char* output, long length,
  6334. CYASSL_DES_key_schedule* schedule, CYASSL_DES_cblock* ivec,
  6335. int enc)
  6336. {
  6337. Des myDes;
  6338. CYASSL_ENTER("DES_ncbc_encrypt");
  6339. /* OpenSSL compat, no ret */
  6340. Des_SetKey(&myDes, (const byte*)schedule, (const byte*)ivec, !enc);
  6341. if (enc)
  6342. Des_CbcEncrypt(&myDes, output, input, (word32)length);
  6343. else
  6344. Des_CbcDecrypt(&myDes, output, input, (word32)length);
  6345. XMEMCPY(ivec, output + length - sizeof(DES_cblock), sizeof(DES_cblock));
  6346. }
  6347. void CyaSSL_ERR_free_strings(void)
  6348. {
  6349. /* handled internally */
  6350. }
  6351. void CyaSSL_ERR_remove_state(unsigned long state)
  6352. {
  6353. /* TODO: GetErrors().Remove(); */
  6354. (void)state;
  6355. }
  6356. void CyaSSL_EVP_cleanup(void)
  6357. {
  6358. /* nothing to do here */
  6359. }
  6360. void CyaSSL_cleanup_all_ex_data(void)
  6361. {
  6362. /* nothing to do here */
  6363. }
  6364. long CyaSSL_CTX_set_mode(CYASSL_CTX* ctx, long mode)
  6365. {
  6366. /* SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER is CyaSSL default mode */
  6367. CYASSL_ENTER("SSL_CTX_set_mode");
  6368. if (mode == SSL_MODE_ENABLE_PARTIAL_WRITE)
  6369. ctx->partialWrite = 1;
  6370. return mode;
  6371. }
  6372. long CyaSSL_CTX_get_mode(CYASSL_CTX* ctx)
  6373. {
  6374. /* TODO: */
  6375. (void)ctx;
  6376. return 0;
  6377. }
  6378. void CyaSSL_CTX_set_default_read_ahead(CYASSL_CTX* ctx, int m)
  6379. {
  6380. /* TODO: maybe? */
  6381. (void)ctx;
  6382. (void)m;
  6383. }
  6384. int CyaSSL_CTX_set_session_id_context(CYASSL_CTX* ctx,
  6385. const unsigned char* sid_ctx,
  6386. unsigned int sid_ctx_len)
  6387. {
  6388. /* No application specific context needed for cyaSSL */
  6389. (void)ctx;
  6390. (void)sid_ctx;
  6391. (void)sid_ctx_len;
  6392. return SSL_SUCCESS;
  6393. }
  6394. long CyaSSL_CTX_sess_get_cache_size(CYASSL_CTX* ctx)
  6395. {
  6396. /* TODO: maybe? */
  6397. (void)ctx;
  6398. return (~0);
  6399. }
  6400. unsigned long CyaSSL_ERR_get_error_line_data(const char** file, int* line,
  6401. const char** data, int *flags)
  6402. {
  6403. /* Not implemented */
  6404. (void)file;
  6405. (void)line;
  6406. (void)data;
  6407. (void)flags;
  6408. return 0;
  6409. }
  6410. #endif /* OPENSSL_EXTRA */
  6411. #if defined(KEEP_PEER_CERT)
  6412. CYASSL_X509* CyaSSL_get_peer_certificate(CYASSL* ssl)
  6413. {
  6414. CYASSL_ENTER("SSL_get_peer_certificate");
  6415. if (ssl->peerCert.issuer.sz)
  6416. return &ssl->peerCert;
  6417. else
  6418. return 0;
  6419. }
  6420. #endif /* KEEP_PEER_CERT */
  6421. #if defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)
  6422. void CyaSSL_FreeX509(CYASSL_X509* x509)
  6423. {
  6424. CYASSL_ENTER("CyaSSL_FreeX509");
  6425. FreeX509(x509);
  6426. }
  6427. /* return the next, if any, altname from the peer cert */
  6428. char* CyaSSL_X509_get_next_altname(CYASSL_X509* cert)
  6429. {
  6430. char* ret = NULL;
  6431. CYASSL_ENTER("CyaSSL_X509_get_next_altname");
  6432. /* don't have any to work with */
  6433. if (cert == NULL || cert->altNames == NULL)
  6434. return NULL;
  6435. /* already went through them */
  6436. if (cert->altNamesNext == NULL)
  6437. return NULL;
  6438. ret = cert->altNamesNext->name;
  6439. cert->altNamesNext = cert->altNamesNext->next;
  6440. return ret;
  6441. }
  6442. CYASSL_X509_NAME* CyaSSL_X509_get_issuer_name(CYASSL_X509* cert)
  6443. {
  6444. CYASSL_ENTER("X509_get_issuer_name");
  6445. return &cert->issuer;
  6446. }
  6447. CYASSL_X509_NAME* CyaSSL_X509_get_subject_name(CYASSL_X509* cert)
  6448. {
  6449. CYASSL_ENTER("X509_get_subject_name");
  6450. return &cert->subject;
  6451. }
  6452. int CyaSSL_X509_get_isCA(CYASSL_X509* x509)
  6453. {
  6454. int isCA = 0;
  6455. CYASSL_ENTER("CyaSSL_X509_get_isCA");
  6456. if (x509 != NULL)
  6457. isCA = x509->isCa;
  6458. CYASSL_LEAVE("CyaSSL_X509_get_isCA", isCA);
  6459. return isCA;
  6460. }
  6461. #ifdef OPENSSL_EXTRA
  6462. int CyaSSL_X509_ext_isSet_by_NID(CYASSL_X509* x509, int nid)
  6463. {
  6464. int isSet = 0;
  6465. CYASSL_ENTER("CyaSSL_X509_ext_isSet_by_NID");
  6466. if (x509 != NULL) {
  6467. switch (nid) {
  6468. case BASIC_CA_OID: isSet = x509->basicConstSet; break;
  6469. case ALT_NAMES_OID: isSet = x509->subjAltNameSet; break;
  6470. case AUTH_KEY_OID: isSet = x509->authKeyIdSet; break;
  6471. case SUBJ_KEY_OID: isSet = x509->subjKeyIdSet; break;
  6472. case KEY_USAGE_OID: isSet = x509->keyUsageSet; break;
  6473. #ifdef CYASSL_SEP
  6474. case CERT_POLICY_OID: isSet = x509->certPolicySet; break;
  6475. #endif /* CYASSL_SEP */
  6476. }
  6477. }
  6478. CYASSL_LEAVE("CyaSSL_X509_ext_isSet_by_NID", isSet);
  6479. return isSet;
  6480. }
  6481. int CyaSSL_X509_ext_get_critical_by_NID(CYASSL_X509* x509, int nid)
  6482. {
  6483. int crit = 0;
  6484. CYASSL_ENTER("CyaSSL_X509_ext_get_critical_by_NID");
  6485. if (x509 != NULL) {
  6486. switch (nid) {
  6487. case BASIC_CA_OID: crit = x509->basicConstCrit; break;
  6488. case ALT_NAMES_OID: crit = x509->subjAltNameCrit; break;
  6489. case AUTH_KEY_OID: crit = x509->authKeyIdCrit; break;
  6490. case SUBJ_KEY_OID: crit = x509->subjKeyIdCrit; break;
  6491. case KEY_USAGE_OID: crit = x509->keyUsageCrit; break;
  6492. #ifdef CYASSL_SEP
  6493. case CERT_POLICY_OID: crit = x509->certPolicyCrit; break;
  6494. #endif /* CYASSL_SEP */
  6495. }
  6496. }
  6497. CYASSL_LEAVE("CyaSSL_X509_ext_get_critical_by_NID", crit);
  6498. return crit;
  6499. }
  6500. int CyaSSL_X509_get_isSet_pathLength(CYASSL_X509* x509)
  6501. {
  6502. int isSet = 0;
  6503. CYASSL_ENTER("CyaSSL_X509_get_isSet_pathLength");
  6504. if (x509 != NULL)
  6505. isSet = x509->basicConstPlSet;
  6506. CYASSL_LEAVE("CyaSSL_X509_get_isSet_pathLength", isSet);
  6507. return isSet;
  6508. }
  6509. word32 CyaSSL_X509_get_pathLength(CYASSL_X509* x509)
  6510. {
  6511. word32 pathLength = 0;
  6512. CYASSL_ENTER("CyaSSL_X509_get_pathLength");
  6513. if (x509 != NULL)
  6514. pathLength = x509->pathLength;
  6515. CYASSL_LEAVE("CyaSSL_X509_get_pathLength", pathLength);
  6516. return pathLength;
  6517. }
  6518. unsigned int CyaSSL_X509_get_keyUsage(CYASSL_X509* x509)
  6519. {
  6520. word16 usage = 0;
  6521. CYASSL_ENTER("CyaSSL_X509_get_keyUsage");
  6522. if (x509 != NULL)
  6523. usage = x509->keyUsage;
  6524. CYASSL_LEAVE("CyaSSL_X509_get_keyUsage", usage);
  6525. return usage;
  6526. }
  6527. byte* CyaSSL_X509_get_authorityKeyID(
  6528. CYASSL_X509* x509, byte* dst, int* dstLen)
  6529. {
  6530. byte *id = NULL;
  6531. int copySz = 0;
  6532. CYASSL_ENTER("CyaSSL_X509_get_authorityKeyID");
  6533. if (x509 != NULL) {
  6534. if (x509->authKeyIdSet) {
  6535. copySz = min(dstLen != NULL ? *dstLen : 0,
  6536. (int)x509->authKeyIdSz);
  6537. id = x509->authKeyId;
  6538. }
  6539. if (dst != NULL && dstLen != NULL && id != NULL && copySz > 0) {
  6540. XMEMCPY(dst, id, copySz);
  6541. id = dst;
  6542. *dstLen = copySz;
  6543. }
  6544. }
  6545. CYASSL_LEAVE("CyaSSL_X509_get_authorityKeyID", copySz);
  6546. return id;
  6547. }
  6548. byte* CyaSSL_X509_get_subjectKeyID(
  6549. CYASSL_X509* x509, byte* dst, int* dstLen)
  6550. {
  6551. byte *id = NULL;
  6552. int copySz = 0;
  6553. CYASSL_ENTER("CyaSSL_X509_get_subjectKeyID");
  6554. if (x509 != NULL) {
  6555. if (x509->subjKeyIdSet) {
  6556. copySz = min(dstLen != NULL ? *dstLen : 0,
  6557. (int)x509->subjKeyIdSz);
  6558. id = x509->subjKeyId;
  6559. }
  6560. if (dst != NULL && dstLen != NULL && id != NULL && copySz > 0) {
  6561. XMEMCPY(dst, id, copySz);
  6562. id = dst;
  6563. *dstLen = copySz;
  6564. }
  6565. }
  6566. CYASSL_LEAVE("CyaSSL_X509_get_subjectKeyID", copySz);
  6567. return id;
  6568. }
  6569. int CyaSSL_X509_NAME_entry_count(CYASSL_X509_NAME* name)
  6570. {
  6571. int count = 0;
  6572. CYASSL_ENTER("CyaSSL_X509_NAME_entry_count");
  6573. if (name != NULL)
  6574. count = name->fullName.entryCount;
  6575. CYASSL_LEAVE("CyaSSL_X509_NAME_entry_count", count);
  6576. return count;
  6577. }
  6578. int CyaSSL_X509_NAME_get_text_by_NID(CYASSL_X509_NAME* name,
  6579. int nid, char* buf, int len)
  6580. {
  6581. char *text = NULL;
  6582. int textSz = 0;
  6583. CYASSL_ENTER("CyaSSL_X509_NAME_get_text_by_NID");
  6584. switch (nid) {
  6585. case ASN_COMMON_NAME:
  6586. text = name->fullName.fullName + name->fullName.cnIdx;
  6587. textSz = name->fullName.cnLen;
  6588. break;
  6589. case ASN_SUR_NAME:
  6590. text = name->fullName.fullName + name->fullName.snIdx;
  6591. textSz = name->fullName.snLen;
  6592. break;
  6593. case ASN_SERIAL_NUMBER:
  6594. text = name->fullName.fullName + name->fullName.serialIdx;
  6595. textSz = name->fullName.serialLen;
  6596. break;
  6597. case ASN_COUNTRY_NAME:
  6598. text = name->fullName.fullName + name->fullName.cIdx;
  6599. textSz = name->fullName.cLen;
  6600. break;
  6601. case ASN_LOCALITY_NAME:
  6602. text = name->fullName.fullName + name->fullName.lIdx;
  6603. textSz = name->fullName.lLen;
  6604. break;
  6605. case ASN_STATE_NAME:
  6606. text = name->fullName.fullName + name->fullName.stIdx;
  6607. textSz = name->fullName.stLen;
  6608. break;
  6609. case ASN_ORG_NAME:
  6610. text = name->fullName.fullName + name->fullName.oIdx;
  6611. textSz = name->fullName.oLen;
  6612. break;
  6613. case ASN_ORGUNIT_NAME:
  6614. text = name->fullName.fullName + name->fullName.ouIdx;
  6615. textSz = name->fullName.ouLen;
  6616. break;
  6617. default:
  6618. break;
  6619. }
  6620. if (buf != NULL && text != NULL) {
  6621. textSz = min(textSz, len);
  6622. XMEMCPY(buf, text, textSz);
  6623. buf[textSz] = '\0';
  6624. }
  6625. CYASSL_LEAVE("CyaSSL_X509_NAME_get_text_by_NID", textSz);
  6626. return textSz;
  6627. }
  6628. #endif
  6629. /* copy name into in buffer, at most sz bytes, if buffer is null will
  6630. malloc buffer, call responsible for freeing */
  6631. char* CyaSSL_X509_NAME_oneline(CYASSL_X509_NAME* name, char* in, int sz)
  6632. {
  6633. int copySz = min(sz, name->sz);
  6634. CYASSL_ENTER("CyaSSL_X509_NAME_oneline");
  6635. if (!name->sz) return in;
  6636. if (!in) {
  6637. in = (char*)XMALLOC(name->sz, 0, DYNAMIC_TYPE_OPENSSL);
  6638. if (!in ) return in;
  6639. copySz = name->sz;
  6640. }
  6641. if (copySz == 0)
  6642. return in;
  6643. XMEMCPY(in, name->name, copySz - 1);
  6644. in[copySz - 1] = 0;
  6645. return in;
  6646. }
  6647. int CyaSSL_X509_get_signature_type(CYASSL_X509* x509)
  6648. {
  6649. int type = 0;
  6650. CYASSL_ENTER("CyaSSL_X509_get_signature_type");
  6651. if (x509 != NULL)
  6652. type = x509->sigOID;
  6653. return type;
  6654. }
  6655. int CyaSSL_X509_get_signature(CYASSL_X509* x509,
  6656. unsigned char* buf, int* bufSz)
  6657. {
  6658. CYASSL_ENTER("CyaSSL_X509_get_signature");
  6659. if (x509 == NULL || bufSz == NULL || *bufSz < (int)x509->sig.length)
  6660. return SSL_FATAL_ERROR;
  6661. if (buf != NULL)
  6662. XMEMCPY(buf, x509->sig.buffer, x509->sig.length);
  6663. *bufSz = x509->sig.length;
  6664. return SSL_SUCCESS;
  6665. }
  6666. /* write X509 serial number in unsigned binary to buffer
  6667. buffer needs to be at least EXTERNAL_SERIAL_SIZE (32) for all cases
  6668. return SSL_SUCCESS on success */
  6669. int CyaSSL_X509_get_serial_number(CYASSL_X509* x509, byte* in, int* inOutSz)
  6670. {
  6671. CYASSL_ENTER("CyaSSL_X509_get_serial_number");
  6672. if (x509 == NULL || in == NULL ||
  6673. inOutSz == NULL || *inOutSz < x509->serialSz)
  6674. return BAD_FUNC_ARG;
  6675. XMEMCPY(in, x509->serial, x509->serialSz);
  6676. *inOutSz = x509->serialSz;
  6677. return SSL_SUCCESS;
  6678. }
  6679. const byte* CyaSSL_X509_get_der(CYASSL_X509* x509, int* outSz)
  6680. {
  6681. CYASSL_ENTER("CyaSSL_X509_get_der");
  6682. if (x509 == NULL || outSz == NULL)
  6683. return NULL;
  6684. *outSz = (int)x509->derCert.length;
  6685. return x509->derCert.buffer;
  6686. }
  6687. int CyaSSL_X509_version(CYASSL_X509* x509)
  6688. {
  6689. CYASSL_ENTER("CyaSSL_X509_version");
  6690. if (x509 == NULL)
  6691. return 0;
  6692. return x509->version;
  6693. }
  6694. const byte* CyaSSL_X509_notBefore(CYASSL_X509* x509)
  6695. {
  6696. CYASSL_ENTER("CyaSSL_X509_notBefore");
  6697. if (x509 == NULL)
  6698. return NULL;
  6699. return x509->notBefore;
  6700. }
  6701. const byte* CyaSSL_X509_notAfter(CYASSL_X509* x509)
  6702. {
  6703. CYASSL_ENTER("CyaSSL_X509_notAfter");
  6704. if (x509 == NULL)
  6705. return NULL;
  6706. return x509->notAfter;
  6707. }
  6708. #ifdef CYASSL_SEP
  6709. /* copy oid into in buffer, at most *inOutSz bytes, if buffer is null will
  6710. malloc buffer, call responsible for freeing. Actual size returned in
  6711. *inOutSz. Requires inOutSz be non-null */
  6712. byte* CyaSSL_X509_get_device_type(CYASSL_X509* x509, byte* in, int *inOutSz)
  6713. {
  6714. int copySz;
  6715. CYASSL_ENTER("CyaSSL_X509_get_dev_type");
  6716. if (inOutSz == NULL) return NULL;
  6717. if (!x509->deviceTypeSz) return in;
  6718. copySz = min(*inOutSz, x509->deviceTypeSz);
  6719. if (!in) {
  6720. in = (byte*)XMALLOC(x509->deviceTypeSz, 0, DYNAMIC_TYPE_OPENSSL);
  6721. if (!in) return in;
  6722. copySz = x509->deviceTypeSz;
  6723. }
  6724. XMEMCPY(in, x509->deviceType, copySz);
  6725. *inOutSz = copySz;
  6726. return in;
  6727. }
  6728. byte* CyaSSL_X509_get_hw_type(CYASSL_X509* x509, byte* in, int* inOutSz)
  6729. {
  6730. int copySz;
  6731. CYASSL_ENTER("CyaSSL_X509_get_hw_type");
  6732. if (inOutSz == NULL) return NULL;
  6733. if (!x509->hwTypeSz) return in;
  6734. copySz = min(*inOutSz, x509->hwTypeSz);
  6735. if (!in) {
  6736. in = (byte*)XMALLOC(x509->hwTypeSz, 0, DYNAMIC_TYPE_OPENSSL);
  6737. if (!in) return in;
  6738. copySz = x509->hwTypeSz;
  6739. }
  6740. XMEMCPY(in, x509->hwType, copySz);
  6741. *inOutSz = copySz;
  6742. return in;
  6743. }
  6744. byte* CyaSSL_X509_get_hw_serial_number(CYASSL_X509* x509,byte* in,int* inOutSz)
  6745. {
  6746. int copySz;
  6747. CYASSL_ENTER("CyaSSL_X509_get_hw_serial_number");
  6748. if (inOutSz == NULL) return NULL;
  6749. if (!x509->hwTypeSz) return in;
  6750. copySz = min(*inOutSz, x509->hwSerialNumSz);
  6751. if (!in) {
  6752. in = (byte*)XMALLOC(x509->hwSerialNumSz, 0, DYNAMIC_TYPE_OPENSSL);
  6753. if (!in) return in;
  6754. copySz = x509->hwSerialNumSz;
  6755. }
  6756. XMEMCPY(in, x509->hwSerialNum, copySz);
  6757. *inOutSz = copySz;
  6758. return in;
  6759. }
  6760. #endif /* CYASSL_SEP */
  6761. CYASSL_X509* CyaSSL_X509_d2i(CYASSL_X509** x509, const byte* in, int len)
  6762. {
  6763. CYASSL_X509 *newX509 = NULL;
  6764. CYASSL_ENTER("CyaSSL_X509_d2i");
  6765. if (in != NULL && len != 0) {
  6766. #ifdef CYASSL_SMALL_STACK
  6767. DecodedCert* cert = NULL;
  6768. #else
  6769. DecodedCert cert[1];
  6770. #endif
  6771. #ifdef CYASSL_SMALL_STACK
  6772. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  6773. DYNAMIC_TYPE_TMP_BUFFER);
  6774. if (cert == NULL)
  6775. return NULL;
  6776. #endif
  6777. InitDecodedCert(cert, (byte*)in, len, NULL);
  6778. if (ParseCertRelative(cert, CERT_TYPE, 0, NULL) == 0) {
  6779. newX509 = (CYASSL_X509*)XMALLOC(sizeof(CYASSL_X509),
  6780. NULL, DYNAMIC_TYPE_X509);
  6781. if (newX509 != NULL) {
  6782. InitX509(newX509, 1);
  6783. if (CopyDecodedToX509(newX509, cert) != 0) {
  6784. XFREE(newX509, NULL, DYNAMIC_TYPE_X509);
  6785. newX509 = NULL;
  6786. }
  6787. }
  6788. }
  6789. FreeDecodedCert(cert);
  6790. #ifdef CYASSL_SMALL_STACK
  6791. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6792. #endif
  6793. }
  6794. if (x509 != NULL)
  6795. *x509 = newX509;
  6796. return newX509;
  6797. }
  6798. #ifndef NO_FILESYSTEM
  6799. #ifndef NO_STDIO_FILESYSTEM
  6800. CYASSL_X509* CyaSSL_X509_d2i_fp(CYASSL_X509** x509, XFILE file)
  6801. {
  6802. CYASSL_X509* newX509 = NULL;
  6803. CYASSL_ENTER("CyaSSL_X509_d2i_fp");
  6804. if (file != XBADFILE) {
  6805. byte* fileBuffer = NULL;
  6806. long sz = 0;
  6807. XFSEEK(file, 0, XSEEK_END);
  6808. sz = XFTELL(file);
  6809. XREWIND(file);
  6810. fileBuffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
  6811. if (fileBuffer != NULL) {
  6812. if ((int)XFREAD(fileBuffer, sz, 1, file) > 0) {
  6813. newX509 = CyaSSL_X509_d2i(NULL, fileBuffer, (int)sz);
  6814. }
  6815. XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
  6816. }
  6817. }
  6818. if (x509 != NULL)
  6819. *x509 = newX509;
  6820. return newX509;
  6821. }
  6822. #endif /* NO_STDIO_FILESYSTEM */
  6823. CYASSL_X509* CyaSSL_X509_load_certificate_file(const char* fname, int format)
  6824. {
  6825. #ifdef CYASSL_SMALL_STACK
  6826. byte staticBuffer[1]; /* force heap usage */
  6827. #else
  6828. byte staticBuffer[FILE_BUFFER_SIZE];
  6829. #endif
  6830. byte* fileBuffer = staticBuffer;
  6831. int dynamic = 0;
  6832. long sz = 0;
  6833. XFILE file;
  6834. CYASSL_X509* x509 = NULL;
  6835. buffer der;
  6836. CYASSL_ENTER("CyaSSL_X509_load_certificate");
  6837. /* Check the inputs */
  6838. if ((fname == NULL) ||
  6839. (format != SSL_FILETYPE_ASN1 && format != SSL_FILETYPE_PEM))
  6840. return NULL;
  6841. file = XFOPEN(fname, "rb");
  6842. if (file == XBADFILE)
  6843. return NULL;
  6844. XFSEEK(file, 0, XSEEK_END);
  6845. sz = XFTELL(file);
  6846. XREWIND(file);
  6847. if (sz > (long)sizeof(staticBuffer)) {
  6848. fileBuffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE);
  6849. if (fileBuffer == NULL) {
  6850. XFCLOSE(file);
  6851. return NULL;
  6852. }
  6853. dynamic = 1;
  6854. }
  6855. if ((int)XFREAD(fileBuffer, sz, 1, file) < 0) {
  6856. XFCLOSE(file);
  6857. if (dynamic)
  6858. XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
  6859. return NULL;
  6860. }
  6861. XFCLOSE(file);
  6862. der.buffer = NULL;
  6863. der.length = 0;
  6864. if (format == SSL_FILETYPE_PEM) {
  6865. int ecc = 0;
  6866. #ifdef CYASSL_SMALL_STACK
  6867. EncryptedInfo* info = NULL;
  6868. #else
  6869. EncryptedInfo info[1];
  6870. #endif
  6871. #ifdef CYASSL_SMALL_STACK
  6872. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  6873. DYNAMIC_TYPE_TMP_BUFFER);
  6874. if (info == NULL) {
  6875. if (dynamic)
  6876. XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
  6877. return NULL;
  6878. }
  6879. #endif
  6880. info->set = 0;
  6881. info->ctx = NULL;
  6882. info->consumed = 0;
  6883. if (PemToDer(fileBuffer, sz, CERT_TYPE, &der, NULL, info, &ecc) != 0)
  6884. {
  6885. /* Only time this should fail, and leave `der` with a buffer
  6886. is when the Base64 Decode fails. Release `der.buffer` in
  6887. that case. */
  6888. if (der.buffer != NULL) {
  6889. XFREE(der.buffer, NULL, DYNAMIC_TYPE_CERT);
  6890. der.buffer = NULL;
  6891. }
  6892. }
  6893. #ifdef CYASSL_SMALL_STACK
  6894. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6895. #endif
  6896. }
  6897. else {
  6898. der.buffer = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_CERT);
  6899. if (der.buffer != NULL) {
  6900. XMEMCPY(der.buffer, fileBuffer, sz);
  6901. der.length = (word32)sz;
  6902. }
  6903. }
  6904. if (dynamic)
  6905. XFREE(fileBuffer, NULL, DYNAMIC_TYPE_FILE);
  6906. /* At this point we want `der` to have the certificate in DER format */
  6907. /* ready to be decoded. */
  6908. if (der.buffer != NULL) {
  6909. #ifdef CYASSL_SMALL_STACK
  6910. DecodedCert* cert = NULL;
  6911. #else
  6912. DecodedCert cert[1];
  6913. #endif
  6914. #ifdef CYASSL_SMALL_STACK
  6915. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  6916. DYNAMIC_TYPE_TMP_BUFFER);
  6917. if (cert != NULL)
  6918. #endif
  6919. {
  6920. InitDecodedCert(cert, der.buffer, der.length, NULL);
  6921. if (ParseCertRelative(cert, CERT_TYPE, 0, NULL) == 0) {
  6922. x509 = (CYASSL_X509*)XMALLOC(sizeof(CYASSL_X509), NULL,
  6923. DYNAMIC_TYPE_X509);
  6924. if (x509 != NULL) {
  6925. InitX509(x509, 1);
  6926. if (CopyDecodedToX509(x509, cert) != 0) {
  6927. XFREE(x509, NULL, DYNAMIC_TYPE_X509);
  6928. x509 = NULL;
  6929. }
  6930. }
  6931. }
  6932. FreeDecodedCert(cert);
  6933. #ifdef CYASSL_SMALL_STACK
  6934. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  6935. #endif
  6936. }
  6937. XFREE(der.buffer, NULL, DYNAMIC_TYPE_CERT);
  6938. }
  6939. return x509;
  6940. }
  6941. #endif /* NO_FILESYSTEM */
  6942. #endif /* KEEP_PEER_CERT || SESSION_CERTS */
  6943. #ifdef OPENSSL_EXTRA
  6944. int CyaSSL_set_ex_data(CYASSL* ssl, int idx, void* data)
  6945. {
  6946. #ifdef FORTRESS
  6947. if (ssl != NULL && idx < MAX_EX_DATA)
  6948. {
  6949. ssl->ex_data[idx] = data;
  6950. return SSL_SUCCESS;
  6951. }
  6952. #else
  6953. (void)ssl;
  6954. (void)idx;
  6955. (void)data;
  6956. #endif
  6957. return SSL_FAILURE;
  6958. }
  6959. int CyaSSL_set_session_id_context(CYASSL* ssl, const unsigned char* id,
  6960. unsigned int len)
  6961. {
  6962. (void)ssl;
  6963. (void)id;
  6964. (void)len;
  6965. return 0;
  6966. }
  6967. void CyaSSL_set_connect_state(CYASSL* ssl)
  6968. {
  6969. (void)ssl;
  6970. /* client by default */
  6971. }
  6972. #endif
  6973. int CyaSSL_get_shutdown(const CYASSL* ssl)
  6974. {
  6975. return (ssl->options.isClosed ||
  6976. ssl->options.connReset ||
  6977. ssl->options.sentNotify);
  6978. }
  6979. int CyaSSL_session_reused(CYASSL* ssl)
  6980. {
  6981. return ssl->options.resuming;
  6982. }
  6983. #ifdef OPENSSL_EXTRA
  6984. void CyaSSL_SESSION_free(CYASSL_SESSION* session)
  6985. {
  6986. (void)session;
  6987. }
  6988. #endif
  6989. const char* CyaSSL_get_version(CYASSL* ssl)
  6990. {
  6991. CYASSL_ENTER("SSL_get_version");
  6992. if (ssl->version.major == SSLv3_MAJOR) {
  6993. switch (ssl->version.minor) {
  6994. case SSLv3_MINOR :
  6995. return "SSLv3";
  6996. case TLSv1_MINOR :
  6997. return "TLSv1";
  6998. case TLSv1_1_MINOR :
  6999. return "TLSv1.1";
  7000. case TLSv1_2_MINOR :
  7001. return "TLSv1.2";
  7002. default:
  7003. return "unknown";
  7004. }
  7005. }
  7006. else if (ssl->version.major == DTLS_MAJOR) {
  7007. switch (ssl->version.minor) {
  7008. case DTLS_MINOR :
  7009. return "DTLS";
  7010. case DTLSv1_2_MINOR :
  7011. return "DTLSv1.2";
  7012. default:
  7013. return "unknown";
  7014. }
  7015. }
  7016. return "unknown";
  7017. }
  7018. int CyaSSL_get_current_cipher_suite(CYASSL* ssl)
  7019. {
  7020. CYASSL_ENTER("SSL_get_current_cipher_suite");
  7021. if (ssl)
  7022. return (ssl->options.cipherSuite0 << 8) | ssl->options.cipherSuite;
  7023. return 0;
  7024. }
  7025. CYASSL_CIPHER* CyaSSL_get_current_cipher(CYASSL* ssl)
  7026. {
  7027. CYASSL_ENTER("SSL_get_current_cipher");
  7028. if (ssl)
  7029. return &ssl->cipher;
  7030. else
  7031. return NULL;
  7032. }
  7033. const char* CyaSSL_CIPHER_get_name(const CYASSL_CIPHER* cipher)
  7034. {
  7035. (void)cipher;
  7036. CYASSL_ENTER("SSL_CIPHER_get_name");
  7037. #ifndef NO_ERROR_STRINGS
  7038. if (cipher) {
  7039. #if defined(HAVE_CHACHA)
  7040. if (cipher->ssl->options.cipherSuite0 == CHACHA_BYTE) {
  7041. /* ChaCha suites */
  7042. switch (cipher->ssl->options.cipherSuite) {
  7043. #ifdef HAVE_CHACHA
  7044. #ifndef NO_RSA
  7045. case TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  7046. return "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256";
  7047. case TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 :
  7048. return "TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256";
  7049. #endif
  7050. case TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 :
  7051. return "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256";
  7052. #endif
  7053. }
  7054. }
  7055. #endif
  7056. #if defined(HAVE_ECC) || defined(HAVE_AESCCM)
  7057. /* Awkwardly, the ECC cipher suites use the ECC_BYTE as expected,
  7058. * but the AES-CCM cipher suites also use it, even the ones that
  7059. * aren't ECC. */
  7060. if (cipher->ssl->options.cipherSuite0 == ECC_BYTE) {
  7061. /* ECC suites */
  7062. switch (cipher->ssl->options.cipherSuite) {
  7063. #ifdef HAVE_ECC
  7064. #ifndef NO_RSA
  7065. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 :
  7066. return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256";
  7067. #endif
  7068. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 :
  7069. return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256";
  7070. #ifndef NO_RSA
  7071. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 :
  7072. return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256";
  7073. #endif
  7074. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 :
  7075. return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256";
  7076. #ifndef NO_RSA
  7077. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 :
  7078. return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384";
  7079. #endif
  7080. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384 :
  7081. return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384";
  7082. #ifndef NO_RSA
  7083. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 :
  7084. return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384";
  7085. #endif
  7086. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 :
  7087. return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384";
  7088. #ifndef NO_SHA
  7089. #ifndef NO_RSA
  7090. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA :
  7091. return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA";
  7092. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA :
  7093. return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA";
  7094. #endif
  7095. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA :
  7096. return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA";
  7097. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA :
  7098. return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA";
  7099. #ifndef NO_RC4
  7100. #ifndef NO_RSA
  7101. case TLS_ECDHE_RSA_WITH_RC4_128_SHA :
  7102. return "TLS_ECDHE_RSA_WITH_RC4_128_SHA";
  7103. #endif
  7104. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA :
  7105. return "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA";
  7106. #endif
  7107. #ifndef NO_DES3
  7108. #ifndef NO_RSA
  7109. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA :
  7110. return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA";
  7111. #endif
  7112. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA :
  7113. return "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA";
  7114. #endif
  7115. #ifndef NO_RSA
  7116. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA :
  7117. return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA";
  7118. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA :
  7119. return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA";
  7120. #endif
  7121. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA :
  7122. return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA";
  7123. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA :
  7124. return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA";
  7125. #ifndef NO_RC4
  7126. #ifndef NO_RSA
  7127. case TLS_ECDH_RSA_WITH_RC4_128_SHA :
  7128. return "TLS_ECDH_RSA_WITH_RC4_128_SHA";
  7129. #endif
  7130. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA :
  7131. return "TLS_ECDH_ECDSA_WITH_RC4_128_SHA";
  7132. #endif
  7133. #ifndef NO_DES3
  7134. #ifndef NO_RSA
  7135. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA :
  7136. return "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA";
  7137. #endif
  7138. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA :
  7139. return "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA";
  7140. #endif
  7141. #endif /* NO_SHA */
  7142. #ifdef HAVE_AESGCM
  7143. #ifndef NO_RSA
  7144. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 :
  7145. return "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256";
  7146. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 :
  7147. return "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
  7148. #endif
  7149. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 :
  7150. return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256";
  7151. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 :
  7152. return "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384";
  7153. #ifndef NO_RSA
  7154. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 :
  7155. return "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256";
  7156. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 :
  7157. return "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384";
  7158. #endif
  7159. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 :
  7160. return "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256";
  7161. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 :
  7162. return "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384";
  7163. #endif
  7164. #endif /* HAVE_ECC */
  7165. #ifdef HAVE_AESCCM
  7166. #ifndef NO_RSA
  7167. case TLS_RSA_WITH_AES_128_CCM_8 :
  7168. return "TLS_RSA_WITH_AES_128_CCM_8";
  7169. case TLS_RSA_WITH_AES_256_CCM_8 :
  7170. return "TLS_RSA_WITH_AES_256_CCM_8";
  7171. #endif
  7172. #ifndef NO_PSK
  7173. case TLS_PSK_WITH_AES_128_CCM_8 :
  7174. return "TLS_PSK_WITH_AES_128_CCM_8";
  7175. case TLS_PSK_WITH_AES_256_CCM_8 :
  7176. return "TLS_PSK_WITH_AES_256_CCM_8";
  7177. case TLS_PSK_WITH_AES_128_CCM :
  7178. return "TLS_PSK_WITH_AES_128_CCM";
  7179. case TLS_PSK_WITH_AES_256_CCM :
  7180. return "TLS_PSK_WITH_AES_256_CCM";
  7181. case TLS_DHE_PSK_WITH_AES_128_CCM :
  7182. return "TLS_DHE_PSK_WITH_AES_128_CCM";
  7183. case TLS_DHE_PSK_WITH_AES_256_CCM :
  7184. return "TLS_DHE_PSK_WITH_AES_256_CCM";
  7185. #endif
  7186. #ifdef HAVE_ECC
  7187. case TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8:
  7188. return "TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8";
  7189. case TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 :
  7190. return "TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8";
  7191. #endif
  7192. #endif
  7193. default:
  7194. return "NONE";
  7195. }
  7196. }
  7197. #endif /* ECC */
  7198. if (cipher->ssl->options.cipherSuite0 != ECC_BYTE &&
  7199. cipher->ssl->options.cipherSuite0 != CHACHA_BYTE) {
  7200. /* normal suites */
  7201. switch (cipher->ssl->options.cipherSuite) {
  7202. #ifndef NO_RSA
  7203. #ifndef NO_RC4
  7204. #ifndef NO_SHA
  7205. case SSL_RSA_WITH_RC4_128_SHA :
  7206. return "SSL_RSA_WITH_RC4_128_SHA";
  7207. #endif
  7208. #ifndef NO_MD5
  7209. case SSL_RSA_WITH_RC4_128_MD5 :
  7210. return "SSL_RSA_WITH_RC4_128_MD5";
  7211. #endif
  7212. #endif
  7213. #ifndef NO_SHA
  7214. #ifndef NO_DES3
  7215. case SSL_RSA_WITH_3DES_EDE_CBC_SHA :
  7216. return "SSL_RSA_WITH_3DES_EDE_CBC_SHA";
  7217. #endif
  7218. case TLS_RSA_WITH_AES_128_CBC_SHA :
  7219. return "TLS_RSA_WITH_AES_128_CBC_SHA";
  7220. case TLS_RSA_WITH_AES_256_CBC_SHA :
  7221. return "TLS_RSA_WITH_AES_256_CBC_SHA";
  7222. #endif
  7223. case TLS_RSA_WITH_AES_128_CBC_SHA256 :
  7224. return "TLS_RSA_WITH_AES_128_CBC_SHA256";
  7225. case TLS_RSA_WITH_AES_256_CBC_SHA256 :
  7226. return "TLS_RSA_WITH_AES_256_CBC_SHA256";
  7227. #ifdef HAVE_BLAKE2
  7228. case TLS_RSA_WITH_AES_128_CBC_B2B256:
  7229. return "TLS_RSA_WITH_AES_128_CBC_B2B256";
  7230. case TLS_RSA_WITH_AES_256_CBC_B2B256:
  7231. return "TLS_RSA_WITH_AES_256_CBC_B2B256";
  7232. #endif
  7233. #ifndef NO_SHA
  7234. case TLS_RSA_WITH_NULL_SHA :
  7235. return "TLS_RSA_WITH_NULL_SHA";
  7236. #endif
  7237. case TLS_RSA_WITH_NULL_SHA256 :
  7238. return "TLS_RSA_WITH_NULL_SHA256";
  7239. #endif /* NO_RSA */
  7240. #ifndef NO_PSK
  7241. #ifndef NO_SHA
  7242. case TLS_PSK_WITH_AES_128_CBC_SHA :
  7243. return "TLS_PSK_WITH_AES_128_CBC_SHA";
  7244. case TLS_PSK_WITH_AES_256_CBC_SHA :
  7245. return "TLS_PSK_WITH_AES_256_CBC_SHA";
  7246. #endif
  7247. #ifndef NO_SHA256
  7248. case TLS_PSK_WITH_AES_128_CBC_SHA256 :
  7249. return "TLS_PSK_WITH_AES_128_CBC_SHA256";
  7250. case TLS_PSK_WITH_NULL_SHA256 :
  7251. return "TLS_PSK_WITH_NULL_SHA256";
  7252. case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 :
  7253. return "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256";
  7254. case TLS_DHE_PSK_WITH_NULL_SHA256 :
  7255. return "TLS_DHE_PSK_WITH_NULL_SHA256";
  7256. #ifdef HAVE_AESGCM
  7257. case TLS_PSK_WITH_AES_128_GCM_SHA256 :
  7258. return "TLS_PSK_WITH_AES_128_GCM_SHA256";
  7259. case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 :
  7260. return "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256";
  7261. #endif
  7262. #endif
  7263. #ifdef CYASSL_SHA384
  7264. case TLS_PSK_WITH_AES_256_CBC_SHA384 :
  7265. return "TLS_PSK_WITH_AES_256_CBC_SHA384";
  7266. case TLS_PSK_WITH_NULL_SHA384 :
  7267. return "TLS_PSK_WITH_NULL_SHA384";
  7268. case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 :
  7269. return "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384";
  7270. case TLS_DHE_PSK_WITH_NULL_SHA384 :
  7271. return "TLS_DHE_PSK_WITH_NULL_SHA384";
  7272. #ifdef HAVE_AESGCM
  7273. case TLS_PSK_WITH_AES_256_GCM_SHA384 :
  7274. return "TLS_PSK_WITH_AES_256_GCM_SHA384";
  7275. case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 :
  7276. return "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384";
  7277. #endif
  7278. #endif
  7279. #ifndef NO_SHA
  7280. case TLS_PSK_WITH_NULL_SHA :
  7281. return "TLS_PSK_WITH_NULL_SHA";
  7282. #endif
  7283. #endif /* NO_PSK */
  7284. #ifndef NO_RSA
  7285. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 :
  7286. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256";
  7287. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 :
  7288. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256";
  7289. #ifndef NO_SHA
  7290. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA :
  7291. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA";
  7292. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA :
  7293. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA";
  7294. #endif
  7295. #ifndef NO_HC128
  7296. #ifndef NO_MD5
  7297. case TLS_RSA_WITH_HC_128_MD5 :
  7298. return "TLS_RSA_WITH_HC_128_MD5";
  7299. #endif
  7300. #ifndef NO_SHA
  7301. case TLS_RSA_WITH_HC_128_SHA :
  7302. return "TLS_RSA_WITH_HC_128_SHA";
  7303. #endif
  7304. #ifdef HAVE_BLAKE2
  7305. case TLS_RSA_WITH_HC_128_B2B256:
  7306. return "TLS_RSA_WITH_HC_128_B2B256";
  7307. #endif
  7308. #endif /* NO_HC128 */
  7309. #ifndef NO_SHA
  7310. #ifndef NO_RABBIT
  7311. case TLS_RSA_WITH_RABBIT_SHA :
  7312. return "TLS_RSA_WITH_RABBIT_SHA";
  7313. #endif
  7314. #ifdef HAVE_NTRU
  7315. #ifndef NO_RC4
  7316. case TLS_NTRU_RSA_WITH_RC4_128_SHA :
  7317. return "TLS_NTRU_RSA_WITH_RC4_128_SHA";
  7318. #endif
  7319. #ifndef NO_DES3
  7320. case TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA :
  7321. return "TLS_NTRU_RSA_WITH_3DES_EDE_CBC_SHA";
  7322. #endif
  7323. case TLS_NTRU_RSA_WITH_AES_128_CBC_SHA :
  7324. return "TLS_NTRU_RSA_WITH_AES_128_CBC_SHA";
  7325. case TLS_NTRU_RSA_WITH_AES_256_CBC_SHA :
  7326. return "TLS_NTRU_RSA_WITH_AES_256_CBC_SHA";
  7327. #endif /* HAVE_NTRU */
  7328. #endif /* NO_SHA */
  7329. case TLS_RSA_WITH_AES_128_GCM_SHA256 :
  7330. return "TLS_RSA_WITH_AES_128_GCM_SHA256";
  7331. case TLS_RSA_WITH_AES_256_GCM_SHA384 :
  7332. return "TLS_RSA_WITH_AES_256_GCM_SHA384";
  7333. case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 :
  7334. return "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256";
  7335. case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 :
  7336. return "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384";
  7337. #ifndef NO_SHA
  7338. case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA :
  7339. return "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA";
  7340. case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA :
  7341. return "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA";
  7342. #endif
  7343. case TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
  7344. return "TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256";
  7345. case TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
  7346. return "TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256";
  7347. #ifndef NO_SHA
  7348. case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA :
  7349. return "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA";
  7350. case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA :
  7351. return "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA";
  7352. #endif
  7353. case TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 :
  7354. return "TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256";
  7355. case TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 :
  7356. return "TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256";
  7357. #endif /* NO_RSA */
  7358. default:
  7359. return "NONE";
  7360. } /* switch */
  7361. } /* normal / ECC */
  7362. }
  7363. #endif /* NO_ERROR_STRINGS */
  7364. return "NONE";
  7365. }
  7366. const char* CyaSSL_get_cipher(CYASSL* ssl)
  7367. {
  7368. CYASSL_ENTER("CyaSSL_get_cipher");
  7369. return CyaSSL_CIPHER_get_name(CyaSSL_get_current_cipher(ssl));
  7370. }
  7371. #ifdef OPENSSL_EXTRA
  7372. char* CyaSSL_CIPHER_description(CYASSL_CIPHER* cipher, char* in, int len)
  7373. {
  7374. (void)cipher;
  7375. (void)in;
  7376. (void)len;
  7377. return 0;
  7378. }
  7379. CYASSL_SESSION* CyaSSL_get1_session(CYASSL* ssl) /* what's ref count */
  7380. {
  7381. (void)ssl;
  7382. return 0;
  7383. }
  7384. void CyaSSL_X509_free(CYASSL_X509* buf)
  7385. {
  7386. (void)buf;
  7387. }
  7388. /* was do nothing */
  7389. /*
  7390. void OPENSSL_free(void* buf)
  7391. {
  7392. (void)buf;
  7393. }
  7394. */
  7395. int CyaSSL_OCSP_parse_url(char* url, char** host, char** port, char** path,
  7396. int* ssl)
  7397. {
  7398. (void)url;
  7399. (void)host;
  7400. (void)port;
  7401. (void)path;
  7402. (void)ssl;
  7403. return 0;
  7404. }
  7405. CYASSL_METHOD* CyaSSLv2_client_method(void)
  7406. {
  7407. return 0;
  7408. }
  7409. CYASSL_METHOD* CyaSSLv2_server_method(void)
  7410. {
  7411. return 0;
  7412. }
  7413. #ifndef NO_MD4
  7414. void CyaSSL_MD4_Init(CYASSL_MD4_CTX* md4)
  7415. {
  7416. /* make sure we have a big enough buffer */
  7417. typedef char ok[sizeof(md4->buffer) >= sizeof(Md4) ? 1 : -1];
  7418. (void) sizeof(ok);
  7419. CYASSL_ENTER("MD4_Init");
  7420. InitMd4((Md4*)md4);
  7421. }
  7422. void CyaSSL_MD4_Update(CYASSL_MD4_CTX* md4, const void* data,
  7423. unsigned long len)
  7424. {
  7425. CYASSL_ENTER("MD4_Update");
  7426. Md4Update((Md4*)md4, (const byte*)data, (word32)len);
  7427. }
  7428. void CyaSSL_MD4_Final(unsigned char* digest, CYASSL_MD4_CTX* md4)
  7429. {
  7430. CYASSL_ENTER("MD4_Final");
  7431. Md4Final((Md4*)md4, digest);
  7432. }
  7433. #endif /* NO_MD4 */
  7434. CYASSL_BIO* CyaSSL_BIO_pop(CYASSL_BIO* top)
  7435. {
  7436. (void)top;
  7437. return 0;
  7438. }
  7439. int CyaSSL_BIO_pending(CYASSL_BIO* bio)
  7440. {
  7441. (void)bio;
  7442. return 0;
  7443. }
  7444. CYASSL_BIO_METHOD* CyaSSL_BIO_s_mem(void)
  7445. {
  7446. static CYASSL_BIO_METHOD meth;
  7447. CYASSL_ENTER("BIO_s_mem");
  7448. meth.type = BIO_MEMORY;
  7449. return &meth;
  7450. }
  7451. CYASSL_BIO_METHOD* CyaSSL_BIO_f_base64(void)
  7452. {
  7453. return 0;
  7454. }
  7455. void CyaSSL_BIO_set_flags(CYASSL_BIO* bio, int flags)
  7456. {
  7457. (void)bio;
  7458. (void)flags;
  7459. }
  7460. void CyaSSL_RAND_screen(void)
  7461. {
  7462. }
  7463. const char* CyaSSL_RAND_file_name(char* fname, unsigned long len)
  7464. {
  7465. (void)fname;
  7466. (void)len;
  7467. return 0;
  7468. }
  7469. int CyaSSL_RAND_write_file(const char* fname)
  7470. {
  7471. (void)fname;
  7472. return 0;
  7473. }
  7474. int CyaSSL_RAND_load_file(const char* fname, long len)
  7475. {
  7476. (void)fname;
  7477. /* CTaoCrypt provides enough entropy internally or will report error */
  7478. if (len == -1)
  7479. return 1024;
  7480. else
  7481. return (int)len;
  7482. }
  7483. int CyaSSL_RAND_egd(const char* path)
  7484. {
  7485. (void)path;
  7486. return 0;
  7487. }
  7488. CYASSL_COMP_METHOD* CyaSSL_COMP_zlib(void)
  7489. {
  7490. return 0;
  7491. }
  7492. CYASSL_COMP_METHOD* CyaSSL_COMP_rle(void)
  7493. {
  7494. return 0;
  7495. }
  7496. int CyaSSL_COMP_add_compression_method(int method, void* data)
  7497. {
  7498. (void)method;
  7499. (void)data;
  7500. return 0;
  7501. }
  7502. int CyaSSL_get_ex_new_index(long idx, void* data, void* cb1, void* cb2,
  7503. void* cb3)
  7504. {
  7505. (void)idx;
  7506. (void)data;
  7507. (void)cb1;
  7508. (void)cb2;
  7509. (void)cb3;
  7510. return 0;
  7511. }
  7512. void CyaSSL_set_dynlock_create_callback(CYASSL_dynlock_value* (*f)(
  7513. const char*, int))
  7514. {
  7515. (void)f;
  7516. }
  7517. void CyaSSL_set_dynlock_lock_callback(
  7518. void (*f)(int, CYASSL_dynlock_value*, const char*, int))
  7519. {
  7520. (void)f;
  7521. }
  7522. void CyaSSL_set_dynlock_destroy_callback(
  7523. void (*f)(CYASSL_dynlock_value*, const char*, int))
  7524. {
  7525. (void)f;
  7526. }
  7527. const char* CyaSSL_X509_verify_cert_error_string(long err)
  7528. {
  7529. (void)err;
  7530. return 0;
  7531. }
  7532. int CyaSSL_X509_LOOKUP_add_dir(CYASSL_X509_LOOKUP* lookup, const char* dir,
  7533. long len)
  7534. {
  7535. (void)lookup;
  7536. (void)dir;
  7537. (void)len;
  7538. return 0;
  7539. }
  7540. int CyaSSL_X509_LOOKUP_load_file(CYASSL_X509_LOOKUP* lookup,
  7541. const char* file, long len)
  7542. {
  7543. (void)lookup;
  7544. (void)file;
  7545. (void)len;
  7546. return 0;
  7547. }
  7548. CYASSL_X509_LOOKUP_METHOD* CyaSSL_X509_LOOKUP_hash_dir(void)
  7549. {
  7550. return 0;
  7551. }
  7552. CYASSL_X509_LOOKUP_METHOD* CyaSSL_X509_LOOKUP_file(void)
  7553. {
  7554. return 0;
  7555. }
  7556. CYASSL_X509_LOOKUP* CyaSSL_X509_STORE_add_lookup(CYASSL_X509_STORE* store,
  7557. CYASSL_X509_LOOKUP_METHOD* m)
  7558. {
  7559. (void)store;
  7560. (void)m;
  7561. return 0;
  7562. }
  7563. int CyaSSL_X509_STORE_add_cert(CYASSL_X509_STORE* store, CYASSL_X509* x509)
  7564. {
  7565. int result = SSL_FATAL_ERROR;
  7566. CYASSL_ENTER("CyaSSL_X509_STORE_add_cert");
  7567. if (store != NULL && store->cm != NULL && x509 != NULL) {
  7568. buffer derCert;
  7569. derCert.buffer = (byte*)XMALLOC(x509->derCert.length,
  7570. NULL, DYNAMIC_TYPE_CERT);
  7571. if (derCert.buffer != NULL) {
  7572. derCert.length = x509->derCert.length;
  7573. /* AddCA() frees the buffer. */
  7574. XMEMCPY(derCert.buffer,
  7575. x509->derCert.buffer, x509->derCert.length);
  7576. result = AddCA(store->cm, derCert, CYASSL_USER_CA, 1);
  7577. if (result != SSL_SUCCESS) result = SSL_FATAL_ERROR;
  7578. }
  7579. }
  7580. CYASSL_LEAVE("CyaSSL_X509_STORE_add_cert", result);
  7581. return result;
  7582. }
  7583. CYASSL_X509_STORE* CyaSSL_X509_STORE_new(void)
  7584. {
  7585. CYASSL_X509_STORE* store = NULL;
  7586. store = (CYASSL_X509_STORE*)XMALLOC(sizeof(CYASSL_X509_STORE), NULL, 0);
  7587. if (store != NULL) {
  7588. store->cm = CyaSSL_CertManagerNew();
  7589. if (store->cm == NULL) {
  7590. XFREE(store, NULL, 0);
  7591. store = NULL;
  7592. }
  7593. }
  7594. return store;
  7595. }
  7596. void CyaSSL_X509_STORE_free(CYASSL_X509_STORE* store)
  7597. {
  7598. if (store != NULL) {
  7599. if (store->cm != NULL)
  7600. CyaSSL_CertManagerFree(store->cm);
  7601. XFREE(store, NULL, 0);
  7602. }
  7603. }
  7604. int CyaSSL_X509_STORE_set_default_paths(CYASSL_X509_STORE* store)
  7605. {
  7606. (void)store;
  7607. return SSL_SUCCESS;
  7608. }
  7609. int CyaSSL_X509_STORE_get_by_subject(CYASSL_X509_STORE_CTX* ctx, int idx,
  7610. CYASSL_X509_NAME* name, CYASSL_X509_OBJECT* obj)
  7611. {
  7612. (void)ctx;
  7613. (void)idx;
  7614. (void)name;
  7615. (void)obj;
  7616. return 0;
  7617. }
  7618. CYASSL_X509_STORE_CTX* CyaSSL_X509_STORE_CTX_new(void)
  7619. {
  7620. CYASSL_X509_STORE_CTX* ctx = (CYASSL_X509_STORE_CTX*)XMALLOC(
  7621. sizeof(CYASSL_X509_STORE_CTX), NULL, 0);
  7622. if (ctx != NULL)
  7623. CyaSSL_X509_STORE_CTX_init(ctx, NULL, NULL, NULL);
  7624. return ctx;
  7625. }
  7626. int CyaSSL_X509_STORE_CTX_init(CYASSL_X509_STORE_CTX* ctx,
  7627. CYASSL_X509_STORE* store, CYASSL_X509* x509, STACK_OF(CYASSL_X509)* sk)
  7628. {
  7629. (void)sk;
  7630. if (ctx != NULL) {
  7631. ctx->store = store;
  7632. ctx->current_cert = x509;
  7633. ctx->domain = NULL;
  7634. ctx->ex_data = NULL;
  7635. ctx->userCtx = NULL;
  7636. ctx->error = 0;
  7637. ctx->error_depth = 0;
  7638. ctx->discardSessionCerts = 0;
  7639. return SSL_SUCCESS;
  7640. }
  7641. return SSL_FATAL_ERROR;
  7642. }
  7643. void CyaSSL_X509_STORE_CTX_free(CYASSL_X509_STORE_CTX* ctx)
  7644. {
  7645. if (ctx != NULL) {
  7646. if (ctx->store != NULL)
  7647. CyaSSL_X509_STORE_free(ctx->store);
  7648. if (ctx->current_cert != NULL)
  7649. CyaSSL_FreeX509(ctx->current_cert);
  7650. XFREE(ctx, NULL, 0);
  7651. }
  7652. }
  7653. void CyaSSL_X509_STORE_CTX_cleanup(CYASSL_X509_STORE_CTX* ctx)
  7654. {
  7655. (void)ctx;
  7656. }
  7657. int CyaSSL_X509_verify_cert(CYASSL_X509_STORE_CTX* ctx)
  7658. {
  7659. if (ctx != NULL && ctx->store != NULL && ctx->store->cm != NULL
  7660. && ctx->current_cert != NULL) {
  7661. return CyaSSL_CertManagerVerifyBuffer(ctx->store->cm,
  7662. ctx->current_cert->derCert.buffer,
  7663. ctx->current_cert->derCert.length,
  7664. SSL_FILETYPE_ASN1);
  7665. }
  7666. return SSL_FATAL_ERROR;
  7667. }
  7668. CYASSL_ASN1_TIME* CyaSSL_X509_CRL_get_lastUpdate(CYASSL_X509_CRL* crl)
  7669. {
  7670. (void)crl;
  7671. return 0;
  7672. }
  7673. CYASSL_ASN1_TIME* CyaSSL_X509_CRL_get_nextUpdate(CYASSL_X509_CRL* crl)
  7674. {
  7675. (void)crl;
  7676. return 0;
  7677. }
  7678. CYASSL_EVP_PKEY* CyaSSL_X509_get_pubkey(CYASSL_X509* x509)
  7679. {
  7680. CYASSL_EVP_PKEY* key = NULL;
  7681. if (x509 != NULL) {
  7682. key = (CYASSL_EVP_PKEY*)XMALLOC(
  7683. sizeof(CYASSL_EVP_PKEY), NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  7684. if (key != NULL) {
  7685. key->type = x509->pubKeyOID;
  7686. key->save_type = 0;
  7687. key->pkey.ptr = (char*)XMALLOC(
  7688. x509->pubKey.length, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  7689. if (key->pkey.ptr == NULL) {
  7690. XFREE(key, NULL, DYNAMIC_TYPE_PUBLIC_KEY);
  7691. return NULL;
  7692. }
  7693. XMEMCPY(key->pkey.ptr,
  7694. x509->pubKey.buffer, x509->pubKey.length);
  7695. key->pkey_sz = x509->pubKey.length;
  7696. #ifdef HAVE_ECC
  7697. key->pkey_curve = (int)x509->pkCurveOID;
  7698. #endif /* HAVE_ECC */
  7699. }
  7700. }
  7701. return key;
  7702. }
  7703. int CyaSSL_X509_CRL_verify(CYASSL_X509_CRL* crl, CYASSL_EVP_PKEY* key)
  7704. {
  7705. (void)crl;
  7706. (void)key;
  7707. return 0;
  7708. }
  7709. void CyaSSL_X509_STORE_CTX_set_error(CYASSL_X509_STORE_CTX* ctx, int err)
  7710. {
  7711. (void)ctx;
  7712. (void)err;
  7713. }
  7714. void CyaSSL_X509_OBJECT_free_contents(CYASSL_X509_OBJECT* obj)
  7715. {
  7716. (void)obj;
  7717. }
  7718. void CyaSSL_EVP_PKEY_free(CYASSL_EVP_PKEY* key)
  7719. {
  7720. if (key != NULL) {
  7721. if (key->pkey.ptr != NULL)
  7722. XFREE(key->pkey.ptr, NULL, 0);
  7723. XFREE(key, NULL, 0);
  7724. }
  7725. }
  7726. int CyaSSL_X509_cmp_current_time(const CYASSL_ASN1_TIME* asnTime)
  7727. {
  7728. (void)asnTime;
  7729. return 0;
  7730. }
  7731. int CyaSSL_sk_X509_REVOKED_num(CYASSL_X509_REVOKED* revoked)
  7732. {
  7733. (void)revoked;
  7734. return 0;
  7735. }
  7736. CYASSL_X509_REVOKED* CyaSSL_X509_CRL_get_REVOKED(CYASSL_X509_CRL* crl)
  7737. {
  7738. (void)crl;
  7739. return 0;
  7740. }
  7741. CYASSL_X509_REVOKED* CyaSSL_sk_X509_REVOKED_value(
  7742. CYASSL_X509_REVOKED* revoked, int value)
  7743. {
  7744. (void)revoked;
  7745. (void)value;
  7746. return 0;
  7747. }
  7748. CYASSL_ASN1_INTEGER* CyaSSL_X509_get_serialNumber(CYASSL_X509* x509)
  7749. {
  7750. (void)x509;
  7751. return 0;
  7752. }
  7753. int CyaSSL_ASN1_TIME_print(CYASSL_BIO* bio, const CYASSL_ASN1_TIME* asnTime)
  7754. {
  7755. (void)bio;
  7756. (void)asnTime;
  7757. return 0;
  7758. }
  7759. int CyaSSL_ASN1_INTEGER_cmp(const CYASSL_ASN1_INTEGER* a,
  7760. const CYASSL_ASN1_INTEGER* b)
  7761. {
  7762. (void)a;
  7763. (void)b;
  7764. return 0;
  7765. }
  7766. long CyaSSL_ASN1_INTEGER_get(const CYASSL_ASN1_INTEGER* i)
  7767. {
  7768. (void)i;
  7769. return 0;
  7770. }
  7771. void* CyaSSL_X509_STORE_CTX_get_ex_data(CYASSL_X509_STORE_CTX* ctx, int idx)
  7772. {
  7773. #ifdef FORTRESS
  7774. if (ctx != NULL && idx == 0)
  7775. return ctx->ex_data;
  7776. #else
  7777. (void)ctx;
  7778. (void)idx;
  7779. #endif
  7780. return 0;
  7781. }
  7782. int CyaSSL_get_ex_data_X509_STORE_CTX_idx(void)
  7783. {
  7784. return 0;
  7785. }
  7786. void* CyaSSL_get_ex_data(const CYASSL* ssl, int idx)
  7787. {
  7788. #ifdef FORTRESS
  7789. if (ssl != NULL && idx < MAX_EX_DATA)
  7790. return ssl->ex_data[idx];
  7791. #else
  7792. (void)ssl;
  7793. (void)idx;
  7794. #endif
  7795. return 0;
  7796. }
  7797. void CyaSSL_CTX_set_info_callback(CYASSL_CTX* ctx, void (*f)(void))
  7798. {
  7799. (void)ctx;
  7800. (void)f;
  7801. }
  7802. unsigned long CyaSSL_ERR_peek_error(void)
  7803. {
  7804. return 0;
  7805. }
  7806. int CyaSSL_ERR_GET_REASON(int err)
  7807. {
  7808. (void)err;
  7809. return 0;
  7810. }
  7811. char* CyaSSL_alert_type_string_long(int alertID)
  7812. {
  7813. (void)alertID;
  7814. return 0;
  7815. }
  7816. char* CyaSSL_alert_desc_string_long(int alertID)
  7817. {
  7818. (void)alertID;
  7819. return 0;
  7820. }
  7821. char* CyaSSL_state_string_long(CYASSL* ssl)
  7822. {
  7823. (void)ssl;
  7824. return 0;
  7825. }
  7826. int CyaSSL_PEM_def_callback(char* name, int num, int w, void* key)
  7827. {
  7828. (void)name;
  7829. (void)num;
  7830. (void)w;
  7831. (void)key;
  7832. return 0;
  7833. }
  7834. long CyaSSL_CTX_sess_accept(CYASSL_CTX* ctx)
  7835. {
  7836. (void)ctx;
  7837. return 0;
  7838. }
  7839. long CyaSSL_CTX_sess_connect(CYASSL_CTX* ctx)
  7840. {
  7841. (void)ctx;
  7842. return 0;
  7843. }
  7844. long CyaSSL_CTX_sess_accept_good(CYASSL_CTX* ctx)
  7845. {
  7846. (void)ctx;
  7847. return 0;
  7848. }
  7849. long CyaSSL_CTX_sess_connect_good(CYASSL_CTX* ctx)
  7850. {
  7851. (void)ctx;
  7852. return 0;
  7853. }
  7854. long CyaSSL_CTX_sess_accept_renegotiate(CYASSL_CTX* ctx)
  7855. {
  7856. (void)ctx;
  7857. return 0;
  7858. }
  7859. long CyaSSL_CTX_sess_connect_renegotiate(CYASSL_CTX* ctx)
  7860. {
  7861. (void)ctx;
  7862. return 0;
  7863. }
  7864. long CyaSSL_CTX_sess_hits(CYASSL_CTX* ctx)
  7865. {
  7866. (void)ctx;
  7867. return 0;
  7868. }
  7869. long CyaSSL_CTX_sess_cb_hits(CYASSL_CTX* ctx)
  7870. {
  7871. (void)ctx;
  7872. return 0;
  7873. }
  7874. long CyaSSL_CTX_sess_cache_full(CYASSL_CTX* ctx)
  7875. {
  7876. (void)ctx;
  7877. return 0;
  7878. }
  7879. long CyaSSL_CTX_sess_misses(CYASSL_CTX* ctx)
  7880. {
  7881. (void)ctx;
  7882. return 0;
  7883. }
  7884. long CyaSSL_CTX_sess_timeouts(CYASSL_CTX* ctx)
  7885. {
  7886. (void)ctx;
  7887. return 0;
  7888. }
  7889. long CyaSSL_CTX_sess_number(CYASSL_CTX* ctx)
  7890. {
  7891. (void)ctx;
  7892. return 0;
  7893. }
  7894. void CyaSSL_DES_set_key_unchecked(CYASSL_const_DES_cblock* myDes,
  7895. CYASSL_DES_key_schedule* key)
  7896. {
  7897. (void)myDes;
  7898. (void)key;
  7899. }
  7900. void CyaSSL_DES_set_odd_parity(CYASSL_DES_cblock* myDes)
  7901. {
  7902. (void)myDes;
  7903. }
  7904. void CyaSSL_DES_ecb_encrypt(CYASSL_DES_cblock* desa,
  7905. CYASSL_DES_cblock* desb, CYASSL_DES_key_schedule* key, int len)
  7906. {
  7907. (void)desa;
  7908. (void)desb;
  7909. (void)key;
  7910. (void)len;
  7911. }
  7912. int CyaSSL_BIO_printf(CYASSL_BIO* bio, const char* format, ...)
  7913. {
  7914. (void)bio;
  7915. (void)format;
  7916. return 0;
  7917. }
  7918. int CyaSSL_ASN1_UTCTIME_print(CYASSL_BIO* bio, const CYASSL_ASN1_UTCTIME* a)
  7919. {
  7920. (void)bio;
  7921. (void)a;
  7922. return 0;
  7923. }
  7924. int CyaSSL_sk_num(CYASSL_X509_REVOKED* rev)
  7925. {
  7926. (void)rev;
  7927. return 0;
  7928. }
  7929. void* CyaSSL_sk_value(CYASSL_X509_REVOKED* rev, int i)
  7930. {
  7931. (void)rev;
  7932. (void)i;
  7933. return 0;
  7934. }
  7935. /* stunnel 4.28 needs */
  7936. void* CyaSSL_CTX_get_ex_data(const CYASSL_CTX* ctx, int d)
  7937. {
  7938. (void)ctx;
  7939. (void)d;
  7940. return 0;
  7941. }
  7942. int CyaSSL_CTX_set_ex_data(CYASSL_CTX* ctx, int d, void* p)
  7943. {
  7944. (void)ctx;
  7945. (void)d;
  7946. (void)p;
  7947. return SSL_SUCCESS;
  7948. }
  7949. void CyaSSL_CTX_sess_set_get_cb(CYASSL_CTX* ctx,
  7950. CYASSL_SESSION*(*f)(CYASSL*, unsigned char*, int, int*))
  7951. {
  7952. (void)ctx;
  7953. (void)f;
  7954. }
  7955. void CyaSSL_CTX_sess_set_new_cb(CYASSL_CTX* ctx,
  7956. int (*f)(CYASSL*, CYASSL_SESSION*))
  7957. {
  7958. (void)ctx;
  7959. (void)f;
  7960. }
  7961. void CyaSSL_CTX_sess_set_remove_cb(CYASSL_CTX* ctx, void (*f)(CYASSL_CTX*,
  7962. CYASSL_SESSION*))
  7963. {
  7964. (void)ctx;
  7965. (void)f;
  7966. }
  7967. int CyaSSL_i2d_SSL_SESSION(CYASSL_SESSION* sess, unsigned char** p)
  7968. {
  7969. (void)sess;
  7970. (void)p;
  7971. return sizeof(CYASSL_SESSION);
  7972. }
  7973. CYASSL_SESSION* CyaSSL_d2i_SSL_SESSION(CYASSL_SESSION** sess,
  7974. const unsigned char** p, long i)
  7975. {
  7976. (void)p;
  7977. (void)i;
  7978. if (sess)
  7979. return *sess;
  7980. return NULL;
  7981. }
  7982. long CyaSSL_SESSION_get_timeout(const CYASSL_SESSION* sess)
  7983. {
  7984. CYASSL_ENTER("CyaSSL_SESSION_get_timeout");
  7985. return sess->timeout;
  7986. }
  7987. long CyaSSL_SESSION_get_time(const CYASSL_SESSION* sess)
  7988. {
  7989. CYASSL_ENTER("CyaSSL_SESSION_get_time");
  7990. return sess->bornOn;
  7991. }
  7992. int CyaSSL_CTX_get_ex_new_index(long idx, void* arg, void* a, void* b,
  7993. void* c)
  7994. {
  7995. (void)idx;
  7996. (void)arg;
  7997. (void)a;
  7998. (void)b;
  7999. (void)c;
  8000. return 0;
  8001. }
  8002. #endif /* OPENSSL_EXTRA */
  8003. #ifdef KEEP_PEER_CERT
  8004. char* CyaSSL_X509_get_subjectCN(CYASSL_X509* x509)
  8005. {
  8006. if (x509 == NULL)
  8007. return NULL;
  8008. return x509->subjectCN;
  8009. }
  8010. #endif /* KEEP_PEER_CERT */
  8011. #ifdef OPENSSL_EXTRA
  8012. #ifdef FORTRESS
  8013. int CyaSSL_cmp_peer_cert_to_file(CYASSL* ssl, const char *fname)
  8014. {
  8015. int ret = SSL_FATAL_ERROR;
  8016. CYASSL_ENTER("CyaSSL_cmp_peer_cert_to_file");
  8017. if (ssl != NULL && fname != NULL)
  8018. {
  8019. #ifdef CYASSL_SMALL_STACK
  8020. EncryptedInfo* info = NULL;
  8021. byte staticBuffer[1]; /* force heap usage */
  8022. #else
  8023. EncryptedInfo info[1];
  8024. byte staticBuffer[FILE_BUFFER_SIZE];
  8025. #endif
  8026. byte* myBuffer = staticBuffer;
  8027. int dynamic = 0;
  8028. XFILE file = XBADFILE;
  8029. long sz = 0;
  8030. int eccKey = 0;
  8031. CYASSL_CTX* ctx = ssl->ctx;
  8032. CYASSL_X509* peer_cert = &ssl->peerCert;
  8033. buffer fileDer;
  8034. file = XFOPEN(fname, "rb");
  8035. if (file == XBADFILE)
  8036. return SSL_BAD_FILE;
  8037. XFSEEK(file, 0, XSEEK_END);
  8038. sz = XFTELL(file);
  8039. XREWIND(file);
  8040. if (sz > (long)sizeof(staticBuffer)) {
  8041. CYASSL_MSG("Getting dynamic buffer");
  8042. myBuffer = (byte*)XMALLOC(sz, ctx->heap, DYNAMIC_TYPE_FILE);
  8043. dynamic = 1;
  8044. }
  8045. #ifdef CYASSL_SMALL_STACK
  8046. info = (EncryptedInfo*)XMALLOC(sizeof(EncryptedInfo), NULL,
  8047. DYNAMIC_TYPE_TMP_BUFFER);
  8048. if (info == NULL)
  8049. ret = MEMORY_E;
  8050. else
  8051. #endif
  8052. {
  8053. info->set = 0;
  8054. info->ctx = ctx;
  8055. info->consumed = 0;
  8056. fileDer.buffer = 0;
  8057. if ((myBuffer != NULL) &&
  8058. (sz > 0) &&
  8059. (XFREAD(myBuffer, sz, 1, file) > 0) &&
  8060. (PemToDer(myBuffer, sz, CERT_TYPE,
  8061. &fileDer, ctx->heap, info, &eccKey) == 0) &&
  8062. (fileDer.length != 0) &&
  8063. (fileDer.length == peer_cert->derCert.length) &&
  8064. (XMEMCMP(peer_cert->derCert.buffer, fileDer.buffer,
  8065. fileDer.length) == 0))
  8066. {
  8067. ret = 0;
  8068. }
  8069. #ifdef CYASSL_SMALL_STACK
  8070. XFREE(info, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8071. #endif
  8072. }
  8073. XFREE(fileDer.buffer, ctx->heap, DYNAMIC_TYPE_CERT);
  8074. if (dynamic)
  8075. XFREE(myBuffer, ctx->heap, DYNAMIC_TYPE_FILE);
  8076. XFCLOSE(file);
  8077. }
  8078. return ret;
  8079. }
  8080. #endif
  8081. static RNG globalRNG;
  8082. static int initGlobalRNG = 0;
  8083. /* SSL_SUCCESS on ok */
  8084. int CyaSSL_RAND_seed(const void* seed, int len)
  8085. {
  8086. CYASSL_MSG("CyaSSL_RAND_seed");
  8087. (void)seed;
  8088. (void)len;
  8089. if (initGlobalRNG == 0) {
  8090. if (InitRng(&globalRNG) < 0) {
  8091. CYASSL_MSG("CyaSSL Init Global RNG failed");
  8092. return 0;
  8093. }
  8094. initGlobalRNG = 1;
  8095. }
  8096. return SSL_SUCCESS;
  8097. }
  8098. /* SSL_SUCCESS on ok */
  8099. int CyaSSL_RAND_bytes(unsigned char* buf, int num)
  8100. {
  8101. int ret = 0;
  8102. RNG* rng = NULL;
  8103. #ifdef CYASSL_SMALL_STACK
  8104. RNG* tmpRNG = NULL;
  8105. #else
  8106. RNG tmpRNG[1];
  8107. #endif
  8108. CYASSL_ENTER("RAND_bytes");
  8109. #ifdef CYASSL_SMALL_STACK
  8110. tmpRNG = (RNG*)XMALLOC(sizeof(RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8111. if (tmpRNG == NULL)
  8112. return ret;
  8113. #endif
  8114. if (InitRng(tmpRNG) == 0)
  8115. rng = tmpRNG;
  8116. else if (initGlobalRNG)
  8117. rng = &globalRNG;
  8118. if (rng) {
  8119. if (RNG_GenerateBlock(rng, buf, num) != 0)
  8120. CYASSL_MSG("Bad RNG_GenerateBlock");
  8121. else
  8122. ret = SSL_SUCCESS;
  8123. }
  8124. #ifdef CYASSL_SMALL_STACK
  8125. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8126. #endif
  8127. return ret;
  8128. }
  8129. CYASSL_BN_CTX* CyaSSL_BN_CTX_new(void)
  8130. {
  8131. static int ctx; /* ctaocrypt doesn't now need ctx */
  8132. CYASSL_MSG("CyaSSL_BN_CTX_new");
  8133. return (CYASSL_BN_CTX*)&ctx;
  8134. }
  8135. void CyaSSL_BN_CTX_init(CYASSL_BN_CTX* ctx)
  8136. {
  8137. (void)ctx;
  8138. CYASSL_MSG("CyaSSL_BN_CTX_init");
  8139. }
  8140. void CyaSSL_BN_CTX_free(CYASSL_BN_CTX* ctx)
  8141. {
  8142. (void)ctx;
  8143. CYASSL_MSG("CyaSSL_BN_CTX_free");
  8144. /* do free since static ctx that does nothing */
  8145. }
  8146. static void InitCyaSSL_BigNum(CYASSL_BIGNUM* bn)
  8147. {
  8148. CYASSL_MSG("InitCyaSSL_BigNum");
  8149. if (bn) {
  8150. bn->neg = 0;
  8151. bn->internal = NULL;
  8152. }
  8153. }
  8154. CYASSL_BIGNUM* CyaSSL_BN_new(void)
  8155. {
  8156. CYASSL_BIGNUM* external;
  8157. mp_int* mpi;
  8158. CYASSL_MSG("CyaSSL_BN_new");
  8159. mpi = (mp_int*) XMALLOC(sizeof(mp_int), NULL, DYNAMIC_TYPE_BIGINT);
  8160. if (mpi == NULL) {
  8161. CYASSL_MSG("CyaSSL_BN_new malloc mpi failure");
  8162. return NULL;
  8163. }
  8164. external = (CYASSL_BIGNUM*) XMALLOC(sizeof(CYASSL_BIGNUM), NULL,
  8165. DYNAMIC_TYPE_BIGINT);
  8166. if (external == NULL) {
  8167. CYASSL_MSG("CyaSSL_BN_new malloc CYASSL_BIGNUM failure");
  8168. XFREE(mpi, NULL, DYNAMIC_TYPE_BIGINT);
  8169. return NULL;
  8170. }
  8171. InitCyaSSL_BigNum(external);
  8172. external->internal = mpi;
  8173. if (mp_init(mpi) != MP_OKAY) {
  8174. CyaSSL_BN_free(external);
  8175. return NULL;
  8176. }
  8177. return external;
  8178. }
  8179. void CyaSSL_BN_free(CYASSL_BIGNUM* bn)
  8180. {
  8181. CYASSL_MSG("CyaSSL_BN_free");
  8182. if (bn) {
  8183. if (bn->internal) {
  8184. mp_clear((mp_int*)bn->internal);
  8185. XFREE(bn->internal, NULL, DYNAMIC_TYPE_BIGINT);
  8186. bn->internal = NULL;
  8187. }
  8188. XFREE(bn, NULL, DYNAMIC_TYPE_BIGINT);
  8189. }
  8190. }
  8191. void CyaSSL_BN_clear_free(CYASSL_BIGNUM* bn)
  8192. {
  8193. CYASSL_MSG("CyaSSL_BN_clear_free");
  8194. CyaSSL_BN_free(bn);
  8195. }
  8196. /* SSL_SUCCESS on ok */
  8197. int CyaSSL_BN_sub(CYASSL_BIGNUM* r, const CYASSL_BIGNUM* a,
  8198. const CYASSL_BIGNUM* b)
  8199. {
  8200. CYASSL_MSG("CyaSSL_BN_sub");
  8201. if (r == NULL || a == NULL || b == NULL)
  8202. return 0;
  8203. if (mp_sub((mp_int*)a->internal,(mp_int*)b->internal,
  8204. (mp_int*)r->internal) == MP_OKAY)
  8205. return SSL_SUCCESS;
  8206. CYASSL_MSG("CyaSSL_BN_sub mp_sub failed");
  8207. return 0;
  8208. }
  8209. /* SSL_SUCCESS on ok */
  8210. int CyaSSL_BN_mod(CYASSL_BIGNUM* r, const CYASSL_BIGNUM* a,
  8211. const CYASSL_BIGNUM* b, const CYASSL_BN_CTX* c)
  8212. {
  8213. (void)c;
  8214. CYASSL_MSG("CyaSSL_BN_mod");
  8215. if (r == NULL || a == NULL || b == NULL)
  8216. return 0;
  8217. if (mp_mod((mp_int*)a->internal,(mp_int*)b->internal,
  8218. (mp_int*)r->internal) == MP_OKAY)
  8219. return SSL_SUCCESS;
  8220. CYASSL_MSG("CyaSSL_BN_mod mp_mod failed");
  8221. return 0;
  8222. }
  8223. const CYASSL_BIGNUM* CyaSSL_BN_value_one(void)
  8224. {
  8225. static CYASSL_BIGNUM* bn_one = NULL;
  8226. CYASSL_MSG("CyaSSL_BN_value_one");
  8227. if (bn_one == NULL) {
  8228. bn_one = CyaSSL_BN_new();
  8229. if (bn_one)
  8230. mp_set_int((mp_int*)bn_one->internal, 1);
  8231. }
  8232. return bn_one;
  8233. }
  8234. int CyaSSL_BN_num_bytes(const CYASSL_BIGNUM* bn)
  8235. {
  8236. CYASSL_MSG("CyaSSL_BN_num_bytes");
  8237. if (bn == NULL || bn->internal == NULL)
  8238. return 0;
  8239. return mp_unsigned_bin_size((mp_int*)bn->internal);
  8240. }
  8241. int CyaSSL_BN_num_bits(const CYASSL_BIGNUM* bn)
  8242. {
  8243. CYASSL_MSG("CyaSSL_BN_num_bits");
  8244. if (bn == NULL || bn->internal == NULL)
  8245. return 0;
  8246. return mp_count_bits((mp_int*)bn->internal);
  8247. }
  8248. int CyaSSL_BN_is_zero(const CYASSL_BIGNUM* bn)
  8249. {
  8250. CYASSL_MSG("CyaSSL_BN_is_zero");
  8251. if (bn == NULL || bn->internal == NULL)
  8252. return 0;
  8253. return mp_iszero((mp_int*)bn->internal);
  8254. }
  8255. int CyaSSL_BN_is_one(const CYASSL_BIGNUM* bn)
  8256. {
  8257. CYASSL_MSG("CyaSSL_BN_is_one");
  8258. if (bn == NULL || bn->internal == NULL)
  8259. return 0;
  8260. if (mp_cmp_d((mp_int*)bn->internal, 1) == 0)
  8261. return 1;
  8262. return 0;
  8263. }
  8264. int CyaSSL_BN_is_odd(const CYASSL_BIGNUM* bn)
  8265. {
  8266. CYASSL_MSG("CyaSSL_BN_is_odd");
  8267. if (bn == NULL || bn->internal == NULL)
  8268. return 0;
  8269. return mp_isodd((mp_int*)bn->internal);
  8270. }
  8271. int CyaSSL_BN_cmp(const CYASSL_BIGNUM* a, const CYASSL_BIGNUM* b)
  8272. {
  8273. CYASSL_MSG("CyaSSL_BN_cmp");
  8274. if (a == NULL || a->internal == NULL || b == NULL || b->internal ==NULL)
  8275. return 0;
  8276. return mp_cmp((mp_int*)a->internal, (mp_int*)b->internal);
  8277. }
  8278. int CyaSSL_BN_bn2bin(const CYASSL_BIGNUM* bn, unsigned char* r)
  8279. {
  8280. CYASSL_MSG("CyaSSL_BN_bn2bin");
  8281. if (bn == NULL || bn->internal == NULL) {
  8282. CYASSL_MSG("NULL bn error");
  8283. return SSL_FATAL_ERROR;
  8284. }
  8285. if (r == NULL)
  8286. return mp_unsigned_bin_size((mp_int*)bn->internal);
  8287. if (mp_to_unsigned_bin((mp_int*)bn->internal, r) != MP_OKAY) {
  8288. CYASSL_MSG("mp_to_unsigned_bin error");
  8289. return SSL_FATAL_ERROR;
  8290. }
  8291. return mp_unsigned_bin_size((mp_int*)bn->internal);
  8292. }
  8293. CYASSL_BIGNUM* CyaSSL_BN_bin2bn(const unsigned char* str, int len,
  8294. CYASSL_BIGNUM* ret)
  8295. {
  8296. CYASSL_MSG("CyaSSL_BN_bin2bn");
  8297. if (ret && ret->internal) {
  8298. if (mp_read_unsigned_bin((mp_int*)ret->internal, str, len) != 0) {
  8299. CYASSL_MSG("mp_read_unsigned_bin failure");
  8300. return NULL;
  8301. }
  8302. }
  8303. else {
  8304. CYASSL_MSG("CyaSSL_BN_bin2bn wants return bignum");
  8305. }
  8306. return ret;
  8307. }
  8308. int CyaSSL_mask_bits(CYASSL_BIGNUM* bn, int n)
  8309. {
  8310. (void)bn;
  8311. (void)n;
  8312. CYASSL_MSG("CyaSSL_BN_mask_bits");
  8313. return SSL_FATAL_ERROR;
  8314. }
  8315. /* SSL_SUCCESS on ok */
  8316. int CyaSSL_BN_rand(CYASSL_BIGNUM* bn, int bits, int top, int bottom)
  8317. {
  8318. int ret = 0;
  8319. int len = bits / 8;
  8320. RNG* rng = NULL;
  8321. #ifdef CYASSL_SMALL_STACK
  8322. RNG* tmpRNG = NULL;
  8323. byte* buff = NULL;
  8324. #else
  8325. RNG tmpRNG[1];
  8326. byte buff[1024];
  8327. #endif
  8328. (void)top;
  8329. (void)bottom;
  8330. CYASSL_MSG("CyaSSL_BN_rand");
  8331. if (bits % 8)
  8332. len++;
  8333. #ifdef CYASSL_SMALL_STACK
  8334. buff = (byte*)XMALLOC(1024, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8335. tmpRNG = (RNG*) XMALLOC(sizeof(RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8336. if (buff == NULL || tmpRNG == NULL) {
  8337. XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8338. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8339. return ret;
  8340. }
  8341. #endif
  8342. if (bn == NULL || bn->internal == NULL)
  8343. CYASSL_MSG("Bad function arguments");
  8344. else if (InitRng(tmpRNG) == 0)
  8345. rng = tmpRNG;
  8346. else if (initGlobalRNG)
  8347. rng = &globalRNG;
  8348. if (rng) {
  8349. if (RNG_GenerateBlock(rng, buff, len) != 0)
  8350. CYASSL_MSG("Bad RNG_GenerateBlock");
  8351. else {
  8352. buff[0] |= 0x80 | 0x40;
  8353. buff[len-1] |= 0x01;
  8354. if (mp_read_unsigned_bin((mp_int*)bn->internal,buff,len) != MP_OKAY)
  8355. CYASSL_MSG("mp read bin failed");
  8356. else
  8357. ret = SSL_SUCCESS;
  8358. }
  8359. }
  8360. #ifdef CYASSL_SMALL_STACK
  8361. XFREE(buff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8362. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8363. #endif
  8364. return ret;
  8365. }
  8366. int CyaSSL_BN_is_bit_set(const CYASSL_BIGNUM* bn, int n)
  8367. {
  8368. (void)bn;
  8369. (void)n;
  8370. CYASSL_MSG("CyaSSL_BN_is_bit_set");
  8371. return 0;
  8372. }
  8373. /* SSL_SUCCESS on ok */
  8374. int CyaSSL_BN_hex2bn(CYASSL_BIGNUM** bn, const char* str)
  8375. {
  8376. int ret = 0;
  8377. word32 decSz = 1024;
  8378. #ifdef CYASSL_SMALL_STACK
  8379. byte* decoded = NULL;
  8380. #else
  8381. byte decoded[1024];
  8382. #endif
  8383. CYASSL_MSG("CyaSSL_BN_hex2bn");
  8384. #ifdef CYASSL_SMALL_STACK
  8385. decoded = (byte*)XMALLOC(decSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8386. if (decoded == NULL)
  8387. return ret;
  8388. #endif
  8389. if (str == NULL)
  8390. CYASSL_MSG("Bad function argument");
  8391. else if (Base16_Decode((byte*)str, (int)XSTRLEN(str), decoded, &decSz) < 0)
  8392. CYASSL_MSG("Bad Base16_Decode error");
  8393. else if (bn == NULL)
  8394. ret = decSz;
  8395. else {
  8396. if (*bn == NULL)
  8397. *bn = CyaSSL_BN_new();
  8398. if (*bn == NULL)
  8399. CYASSL_MSG("BN new failed");
  8400. else if (CyaSSL_BN_bin2bn(decoded, decSz, *bn) == NULL)
  8401. CYASSL_MSG("Bad bin2bn error");
  8402. else
  8403. ret = SSL_SUCCESS;
  8404. }
  8405. #ifdef CYASSL_SMALL_STACK
  8406. XFREE(decoded, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8407. #endif
  8408. return ret;
  8409. }
  8410. CYASSL_BIGNUM* CyaSSL_BN_dup(const CYASSL_BIGNUM* bn)
  8411. {
  8412. CYASSL_BIGNUM* ret;
  8413. CYASSL_MSG("CyaSSL_BN_dup");
  8414. if (bn == NULL || bn->internal == NULL) {
  8415. CYASSL_MSG("bn NULL error");
  8416. return NULL;
  8417. }
  8418. ret = CyaSSL_BN_new();
  8419. if (ret == NULL) {
  8420. CYASSL_MSG("bn new error");
  8421. return NULL;
  8422. }
  8423. if (mp_copy((mp_int*)bn->internal, (mp_int*)ret->internal) != MP_OKAY) {
  8424. CYASSL_MSG("mp_copy error");
  8425. CyaSSL_BN_free(ret);
  8426. return NULL;
  8427. }
  8428. return ret;
  8429. }
  8430. CYASSL_BIGNUM* CyaSSL_BN_copy(CYASSL_BIGNUM* r, const CYASSL_BIGNUM* bn)
  8431. {
  8432. (void)r;
  8433. (void)bn;
  8434. CYASSL_MSG("CyaSSL_BN_copy");
  8435. return NULL;
  8436. }
  8437. int CyaSSL_BN_set_word(CYASSL_BIGNUM* bn, unsigned long w)
  8438. {
  8439. (void)bn;
  8440. (void)w;
  8441. CYASSL_MSG("CyaSSL_BN_set_word");
  8442. return SSL_FATAL_ERROR;
  8443. }
  8444. int CyaSSL_BN_dec2bn(CYASSL_BIGNUM** bn, const char* str)
  8445. {
  8446. (void)bn;
  8447. (void)str;
  8448. CYASSL_MSG("CyaSSL_BN_dec2bn");
  8449. return SSL_FATAL_ERROR;
  8450. }
  8451. char* CyaSSL_BN_bn2dec(const CYASSL_BIGNUM* bn)
  8452. {
  8453. (void)bn;
  8454. CYASSL_MSG("CyaSSL_BN_bn2dec");
  8455. return NULL;
  8456. }
  8457. #ifndef NO_DH
  8458. static void InitCyaSSL_DH(CYASSL_DH* dh)
  8459. {
  8460. if (dh) {
  8461. dh->p = NULL;
  8462. dh->g = NULL;
  8463. dh->pub_key = NULL;
  8464. dh->priv_key = NULL;
  8465. dh->internal = NULL;
  8466. dh->inSet = 0;
  8467. dh->exSet = 0;
  8468. }
  8469. }
  8470. CYASSL_DH* CyaSSL_DH_new(void)
  8471. {
  8472. CYASSL_DH* external;
  8473. DhKey* key;
  8474. CYASSL_MSG("CyaSSL_DH_new");
  8475. key = (DhKey*) XMALLOC(sizeof(DhKey), NULL, DYNAMIC_TYPE_DH);
  8476. if (key == NULL) {
  8477. CYASSL_MSG("CyaSSL_DH_new malloc DhKey failure");
  8478. return NULL;
  8479. }
  8480. external = (CYASSL_DH*) XMALLOC(sizeof(CYASSL_DH), NULL,
  8481. DYNAMIC_TYPE_DH);
  8482. if (external == NULL) {
  8483. CYASSL_MSG("CyaSSL_DH_new malloc CYASSL_DH failure");
  8484. XFREE(key, NULL, DYNAMIC_TYPE_DH);
  8485. return NULL;
  8486. }
  8487. InitCyaSSL_DH(external);
  8488. InitDhKey(key);
  8489. external->internal = key;
  8490. return external;
  8491. }
  8492. void CyaSSL_DH_free(CYASSL_DH* dh)
  8493. {
  8494. CYASSL_MSG("CyaSSL_DH_free");
  8495. if (dh) {
  8496. if (dh->internal) {
  8497. FreeDhKey((DhKey*)dh->internal);
  8498. XFREE(dh->internal, NULL, DYNAMIC_TYPE_DH);
  8499. dh->internal = NULL;
  8500. }
  8501. CyaSSL_BN_free(dh->priv_key);
  8502. CyaSSL_BN_free(dh->pub_key);
  8503. CyaSSL_BN_free(dh->g);
  8504. CyaSSL_BN_free(dh->p);
  8505. InitCyaSSL_DH(dh); /* set back to NULLs for safety */
  8506. XFREE(dh, NULL, DYNAMIC_TYPE_DH);
  8507. }
  8508. }
  8509. static int SetDhInternal(CYASSL_DH* dh)
  8510. {
  8511. int ret = SSL_FATAL_ERROR;
  8512. int pSz = 1024;
  8513. int gSz = 1024;
  8514. #ifdef CYASSL_SMALL_STACK
  8515. unsigned char* p = NULL;
  8516. unsigned char* g = NULL;
  8517. #else
  8518. unsigned char p[1024];
  8519. unsigned char g[1024];
  8520. #endif
  8521. CYASSL_ENTER("SetDhInternal");
  8522. if (dh == NULL || dh->p == NULL || dh->g == NULL)
  8523. CYASSL_MSG("Bad function arguments");
  8524. else if (CyaSSL_BN_bn2bin(dh->p, NULL) > pSz)
  8525. CYASSL_MSG("Bad p internal size");
  8526. else if (CyaSSL_BN_bn2bin(dh->g, NULL) > gSz)
  8527. CYASSL_MSG("Bad g internal size");
  8528. else {
  8529. #ifdef CYASSL_SMALL_STACK
  8530. p = (unsigned char*)XMALLOC(pSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8531. g = (unsigned char*)XMALLOC(gSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8532. if (p == NULL || g == NULL) {
  8533. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8534. XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8535. return ret;
  8536. }
  8537. #endif
  8538. pSz = CyaSSL_BN_bn2bin(dh->p, p);
  8539. gSz = CyaSSL_BN_bn2bin(dh->g, g);
  8540. if (pSz <= 0 || gSz <= 0)
  8541. CYASSL_MSG("Bad BN2bin set");
  8542. else if (DhSetKey((DhKey*)dh->internal, p, pSz, g, gSz) < 0)
  8543. CYASSL_MSG("Bad DH SetKey");
  8544. else {
  8545. dh->inSet = 1;
  8546. ret = 0;
  8547. }
  8548. #ifdef CYASSL_SMALL_STACK
  8549. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8550. XFREE(g, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8551. #endif
  8552. }
  8553. return ret;
  8554. }
  8555. int CyaSSL_DH_size(CYASSL_DH* dh)
  8556. {
  8557. CYASSL_MSG("CyaSSL_DH_size");
  8558. if (dh == NULL)
  8559. return 0;
  8560. return CyaSSL_BN_num_bytes(dh->p);
  8561. }
  8562. /* return SSL_SUCCESS on ok, else 0 */
  8563. int CyaSSL_DH_generate_key(CYASSL_DH* dh)
  8564. {
  8565. int ret = 0;
  8566. word32 pubSz = 768;
  8567. word32 privSz = 768;
  8568. RNG* rng = NULL;
  8569. #ifdef CYASSL_SMALL_STACK
  8570. unsigned char* pub = NULL;
  8571. unsigned char* priv = NULL;
  8572. RNG* tmpRNG = NULL;
  8573. #else
  8574. unsigned char pub [768];
  8575. unsigned char priv[768];
  8576. RNG tmpRNG[1];
  8577. #endif
  8578. CYASSL_MSG("CyaSSL_DH_generate_key");
  8579. #ifdef CYASSL_SMALL_STACK
  8580. tmpRNG = (RNG*)XMALLOC(sizeof(RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8581. pub = (unsigned char*)XMALLOC(pubSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8582. priv = (unsigned char*)XMALLOC(privSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8583. if (tmpRNG == NULL || pub == NULL || priv == NULL) {
  8584. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8585. XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8586. XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8587. return ret;
  8588. }
  8589. #endif
  8590. if (dh == NULL || dh->p == NULL || dh->g == NULL)
  8591. CYASSL_MSG("Bad function arguments");
  8592. else if (dh->inSet == 0 && SetDhInternal(dh) < 0)
  8593. CYASSL_MSG("Bad DH set internal");
  8594. else if (InitRng(tmpRNG) == 0)
  8595. rng = tmpRNG;
  8596. else {
  8597. CYASSL_MSG("Bad RNG Init, trying global");
  8598. if (initGlobalRNG == 0)
  8599. CYASSL_MSG("Global RNG no Init");
  8600. else
  8601. rng = &globalRNG;
  8602. }
  8603. if (rng) {
  8604. if (DhGenerateKeyPair((DhKey*)dh->internal, rng, priv, &privSz,
  8605. pub, &pubSz) < 0)
  8606. CYASSL_MSG("Bad DhGenerateKeyPair");
  8607. else {
  8608. if (dh->pub_key)
  8609. CyaSSL_BN_free(dh->pub_key);
  8610. dh->pub_key = CyaSSL_BN_new();
  8611. if (dh->pub_key == NULL) {
  8612. CYASSL_MSG("Bad DH new pub");
  8613. }
  8614. if (dh->priv_key)
  8615. CyaSSL_BN_free(dh->priv_key);
  8616. dh->priv_key = CyaSSL_BN_new();
  8617. if (dh->priv_key == NULL) {
  8618. CYASSL_MSG("Bad DH new priv");
  8619. }
  8620. if (dh->pub_key && dh->priv_key) {
  8621. if (CyaSSL_BN_bin2bn(pub, pubSz, dh->pub_key) == NULL)
  8622. CYASSL_MSG("Bad DH bn2bin error pub");
  8623. else if (CyaSSL_BN_bin2bn(priv, privSz, dh->priv_key) == NULL)
  8624. CYASSL_MSG("Bad DH bn2bin error priv");
  8625. else
  8626. ret = SSL_SUCCESS;
  8627. }
  8628. }
  8629. }
  8630. #ifdef CYASSL_SMALL_STACK
  8631. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8632. XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8633. XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8634. #endif
  8635. return ret;
  8636. }
  8637. /* return key size on ok, 0 otherwise */
  8638. int CyaSSL_DH_compute_key(unsigned char* key, CYASSL_BIGNUM* otherPub,
  8639. CYASSL_DH* dh)
  8640. {
  8641. int ret = 0;
  8642. word32 keySz = 0;
  8643. word32 pubSz = 1024;
  8644. word32 privSz = 1024;
  8645. #ifdef CYASSL_SMALL_STACK
  8646. unsigned char* pub = NULL;
  8647. unsigned char* priv = NULL;
  8648. #else
  8649. unsigned char pub [1024];
  8650. unsigned char priv[1024];
  8651. #endif
  8652. CYASSL_MSG("CyaSSL_DH_compute_key");
  8653. #ifdef CYASSL_SMALL_STACK
  8654. pub = (unsigned char*)XMALLOC(pubSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8655. if (pub == NULL)
  8656. return ret;
  8657. priv = (unsigned char*)XMALLOC(privSz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8658. if (priv == NULL) {
  8659. XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8660. return 0;
  8661. }
  8662. #endif
  8663. if (dh == NULL || dh->priv_key == NULL || otherPub == NULL)
  8664. CYASSL_MSG("Bad function arguments");
  8665. else if ((keySz = (word32)DH_size(dh)) == 0)
  8666. CYASSL_MSG("Bad DH_size");
  8667. else if (CyaSSL_BN_bn2bin(dh->priv_key, NULL) > (int)privSz)
  8668. CYASSL_MSG("Bad priv internal size");
  8669. else if (CyaSSL_BN_bn2bin(otherPub, NULL) > (int)pubSz)
  8670. CYASSL_MSG("Bad otherPub size");
  8671. else {
  8672. privSz = CyaSSL_BN_bn2bin(dh->priv_key, priv);
  8673. pubSz = CyaSSL_BN_bn2bin(otherPub, pub);
  8674. if (privSz <= 0 || pubSz <= 0)
  8675. CYASSL_MSG("Bad BN2bin set");
  8676. else if (DhAgree((DhKey*)dh->internal, key, &keySz, priv, privSz, pub,
  8677. pubSz) < 0)
  8678. CYASSL_MSG("DhAgree failed");
  8679. else
  8680. ret = (int)keySz;
  8681. }
  8682. #ifdef CYASSL_SMALL_STACK
  8683. XFREE(pub, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8684. XFREE(priv, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8685. #endif
  8686. return ret;
  8687. }
  8688. #endif /* NO_DH */
  8689. #ifndef NO_DSA
  8690. static void InitCyaSSL_DSA(CYASSL_DSA* dsa)
  8691. {
  8692. if (dsa) {
  8693. dsa->p = NULL;
  8694. dsa->q = NULL;
  8695. dsa->g = NULL;
  8696. dsa->pub_key = NULL;
  8697. dsa->priv_key = NULL;
  8698. dsa->internal = NULL;
  8699. dsa->inSet = 0;
  8700. dsa->exSet = 0;
  8701. }
  8702. }
  8703. CYASSL_DSA* CyaSSL_DSA_new(void)
  8704. {
  8705. CYASSL_DSA* external;
  8706. DsaKey* key;
  8707. CYASSL_MSG("CyaSSL_DSA_new");
  8708. key = (DsaKey*) XMALLOC(sizeof(DsaKey), NULL, DYNAMIC_TYPE_DSA);
  8709. if (key == NULL) {
  8710. CYASSL_MSG("CyaSSL_DSA_new malloc DsaKey failure");
  8711. return NULL;
  8712. }
  8713. external = (CYASSL_DSA*) XMALLOC(sizeof(CYASSL_DSA), NULL,
  8714. DYNAMIC_TYPE_DSA);
  8715. if (external == NULL) {
  8716. CYASSL_MSG("CyaSSL_DSA_new malloc CYASSL_DSA failure");
  8717. XFREE(key, NULL, DYNAMIC_TYPE_DSA);
  8718. return NULL;
  8719. }
  8720. InitCyaSSL_DSA(external);
  8721. InitDsaKey(key);
  8722. external->internal = key;
  8723. return external;
  8724. }
  8725. void CyaSSL_DSA_free(CYASSL_DSA* dsa)
  8726. {
  8727. CYASSL_MSG("CyaSSL_DSA_free");
  8728. if (dsa) {
  8729. if (dsa->internal) {
  8730. FreeDsaKey((DsaKey*)dsa->internal);
  8731. XFREE(dsa->internal, NULL, DYNAMIC_TYPE_DSA);
  8732. dsa->internal = NULL;
  8733. }
  8734. CyaSSL_BN_free(dsa->priv_key);
  8735. CyaSSL_BN_free(dsa->pub_key);
  8736. CyaSSL_BN_free(dsa->g);
  8737. CyaSSL_BN_free(dsa->q);
  8738. CyaSSL_BN_free(dsa->p);
  8739. InitCyaSSL_DSA(dsa); /* set back to NULLs for safety */
  8740. XFREE(dsa, NULL, DYNAMIC_TYPE_DSA);
  8741. }
  8742. }
  8743. int CyaSSL_DSA_generate_key(CYASSL_DSA* dsa)
  8744. {
  8745. (void)dsa;
  8746. CYASSL_MSG("CyaSSL_DSA_generate_key");
  8747. return 0; /* key gen not needed by server */
  8748. }
  8749. int CyaSSL_DSA_generate_parameters_ex(CYASSL_DSA* dsa, int bits,
  8750. unsigned char* seed, int seedLen, int* counterRet,
  8751. unsigned long* hRet, void* cb)
  8752. {
  8753. (void)dsa;
  8754. (void)bits;
  8755. (void)seed;
  8756. (void)seedLen;
  8757. (void)counterRet;
  8758. (void)hRet;
  8759. (void)cb;
  8760. CYASSL_MSG("CyaSSL_DSA_generate_parameters_ex");
  8761. return 0; /* key gen not needed by server */
  8762. }
  8763. #endif /* NO_DSA */
  8764. #ifndef NO_RSA
  8765. static void InitCyaSSL_Rsa(CYASSL_RSA* rsa)
  8766. {
  8767. if (rsa) {
  8768. rsa->n = NULL;
  8769. rsa->e = NULL;
  8770. rsa->d = NULL;
  8771. rsa->p = NULL;
  8772. rsa->q = NULL;
  8773. rsa->dmp1 = NULL;
  8774. rsa->dmq1 = NULL;
  8775. rsa->iqmp = NULL;
  8776. rsa->internal = NULL;
  8777. rsa->inSet = 0;
  8778. rsa->exSet = 0;
  8779. }
  8780. }
  8781. CYASSL_RSA* CyaSSL_RSA_new(void)
  8782. {
  8783. CYASSL_RSA* external;
  8784. RsaKey* key;
  8785. CYASSL_MSG("CyaSSL_RSA_new");
  8786. key = (RsaKey*) XMALLOC(sizeof(RsaKey), NULL, DYNAMIC_TYPE_RSA);
  8787. if (key == NULL) {
  8788. CYASSL_MSG("CyaSSL_RSA_new malloc RsaKey failure");
  8789. return NULL;
  8790. }
  8791. external = (CYASSL_RSA*) XMALLOC(sizeof(CYASSL_RSA), NULL,
  8792. DYNAMIC_TYPE_RSA);
  8793. if (external == NULL) {
  8794. CYASSL_MSG("CyaSSL_RSA_new malloc CYASSL_RSA failure");
  8795. XFREE(key, NULL, DYNAMIC_TYPE_RSA);
  8796. return NULL;
  8797. }
  8798. InitCyaSSL_Rsa(external);
  8799. if (InitRsaKey(key, NULL) != 0) {
  8800. CYASSL_MSG("InitRsaKey CYASSL_RSA failure");
  8801. XFREE(external, NULL, DYNAMIC_TYPE_RSA);
  8802. XFREE(key, NULL, DYNAMIC_TYPE_RSA);
  8803. return NULL;
  8804. }
  8805. external->internal = key;
  8806. return external;
  8807. }
  8808. void CyaSSL_RSA_free(CYASSL_RSA* rsa)
  8809. {
  8810. CYASSL_MSG("CyaSSL_RSA_free");
  8811. if (rsa) {
  8812. if (rsa->internal) {
  8813. FreeRsaKey((RsaKey*)rsa->internal);
  8814. XFREE(rsa->internal, NULL, DYNAMIC_TYPE_RSA);
  8815. rsa->internal = NULL;
  8816. }
  8817. CyaSSL_BN_free(rsa->iqmp);
  8818. CyaSSL_BN_free(rsa->dmq1);
  8819. CyaSSL_BN_free(rsa->dmp1);
  8820. CyaSSL_BN_free(rsa->q);
  8821. CyaSSL_BN_free(rsa->p);
  8822. CyaSSL_BN_free(rsa->d);
  8823. CyaSSL_BN_free(rsa->e);
  8824. CyaSSL_BN_free(rsa->n);
  8825. InitCyaSSL_Rsa(rsa); /* set back to NULLs for safety */
  8826. XFREE(rsa, NULL, DYNAMIC_TYPE_RSA);
  8827. }
  8828. }
  8829. #endif /* NO_RSA */
  8830. #if !defined(NO_RSA) || !defined(NO_DSA)
  8831. static int SetIndividualExternal(CYASSL_BIGNUM** bn, mp_int* mpi)
  8832. {
  8833. CYASSL_MSG("Entering SetIndividualExternal");
  8834. if (mpi == NULL) {
  8835. CYASSL_MSG("mpi NULL error");
  8836. return SSL_FATAL_ERROR;
  8837. }
  8838. if (*bn == NULL) {
  8839. *bn = CyaSSL_BN_new();
  8840. if (*bn == NULL) {
  8841. CYASSL_MSG("SetIndividualExternal alloc failed");
  8842. return SSL_FATAL_ERROR;
  8843. }
  8844. }
  8845. if (mp_copy(mpi, (mp_int*)((*bn)->internal)) != MP_OKAY) {
  8846. CYASSL_MSG("mp_copy error");
  8847. return SSL_FATAL_ERROR;
  8848. }
  8849. return 0;
  8850. }
  8851. #endif /* !NO_RSA && !NO_DSA */
  8852. #ifndef NO_DSA
  8853. static int SetDsaExternal(CYASSL_DSA* dsa)
  8854. {
  8855. DsaKey* key;
  8856. CYASSL_MSG("Entering SetDsaExternal");
  8857. if (dsa == NULL || dsa->internal == NULL) {
  8858. CYASSL_MSG("dsa key NULL error");
  8859. return SSL_FATAL_ERROR;
  8860. }
  8861. key = (DsaKey*)dsa->internal;
  8862. if (SetIndividualExternal(&dsa->p, &key->p) < 0) {
  8863. CYASSL_MSG("dsa p key error");
  8864. return SSL_FATAL_ERROR;
  8865. }
  8866. if (SetIndividualExternal(&dsa->q, &key->q) < 0) {
  8867. CYASSL_MSG("dsa q key error");
  8868. return SSL_FATAL_ERROR;
  8869. }
  8870. if (SetIndividualExternal(&dsa->g, &key->g) < 0) {
  8871. CYASSL_MSG("dsa g key error");
  8872. return SSL_FATAL_ERROR;
  8873. }
  8874. if (SetIndividualExternal(&dsa->pub_key, &key->y) < 0) {
  8875. CYASSL_MSG("dsa y key error");
  8876. return SSL_FATAL_ERROR;
  8877. }
  8878. if (SetIndividualExternal(&dsa->priv_key, &key->x) < 0) {
  8879. CYASSL_MSG("dsa x key error");
  8880. return SSL_FATAL_ERROR;
  8881. }
  8882. dsa->exSet = 1;
  8883. return 0;
  8884. }
  8885. #endif /* NO_DSA */
  8886. #ifndef NO_RSA
  8887. static int SetRsaExternal(CYASSL_RSA* rsa)
  8888. {
  8889. RsaKey* key;
  8890. CYASSL_MSG("Entering SetRsaExternal");
  8891. if (rsa == NULL || rsa->internal == NULL) {
  8892. CYASSL_MSG("rsa key NULL error");
  8893. return SSL_FATAL_ERROR;
  8894. }
  8895. key = (RsaKey*)rsa->internal;
  8896. if (SetIndividualExternal(&rsa->n, &key->n) < 0) {
  8897. CYASSL_MSG("rsa n key error");
  8898. return SSL_FATAL_ERROR;
  8899. }
  8900. if (SetIndividualExternal(&rsa->e, &key->e) < 0) {
  8901. CYASSL_MSG("rsa e key error");
  8902. return SSL_FATAL_ERROR;
  8903. }
  8904. if (SetIndividualExternal(&rsa->d, &key->d) < 0) {
  8905. CYASSL_MSG("rsa d key error");
  8906. return SSL_FATAL_ERROR;
  8907. }
  8908. if (SetIndividualExternal(&rsa->p, &key->p) < 0) {
  8909. CYASSL_MSG("rsa p key error");
  8910. return SSL_FATAL_ERROR;
  8911. }
  8912. if (SetIndividualExternal(&rsa->q, &key->q) < 0) {
  8913. CYASSL_MSG("rsa q key error");
  8914. return SSL_FATAL_ERROR;
  8915. }
  8916. if (SetIndividualExternal(&rsa->dmp1, &key->dP) < 0) {
  8917. CYASSL_MSG("rsa dP key error");
  8918. return SSL_FATAL_ERROR;
  8919. }
  8920. if (SetIndividualExternal(&rsa->dmq1, &key->dQ) < 0) {
  8921. CYASSL_MSG("rsa dQ key error");
  8922. return SSL_FATAL_ERROR;
  8923. }
  8924. if (SetIndividualExternal(&rsa->iqmp, &key->u) < 0) {
  8925. CYASSL_MSG("rsa u key error");
  8926. return SSL_FATAL_ERROR;
  8927. }
  8928. rsa->exSet = 1;
  8929. return 0;
  8930. }
  8931. /* SSL_SUCCESS on ok */
  8932. int CyaSSL_RSA_generate_key_ex(CYASSL_RSA* rsa, int bits, CYASSL_BIGNUM* bn,
  8933. void* cb)
  8934. {
  8935. int ret = SSL_FATAL_ERROR;
  8936. CYASSL_MSG("CyaSSL_RSA_generate_key_ex");
  8937. (void)rsa;
  8938. (void)bits;
  8939. (void)cb;
  8940. (void)bn;
  8941. #ifdef CYASSL_KEY_GEN
  8942. {
  8943. #ifdef CYASSL_SMALL_STACK
  8944. RNG* rng = NULL;
  8945. #else
  8946. RNG rng[1];
  8947. #endif
  8948. #ifdef CYASSL_SMALL_STACK
  8949. rng = (RNG*)XMALLOC(sizeof(RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8950. if (rng == NULL)
  8951. return SSL_FATAL_ERROR;
  8952. #endif
  8953. if (InitRng(rng) < 0)
  8954. CYASSL_MSG("RNG init failed");
  8955. else if (MakeRsaKey((RsaKey*)rsa->internal, bits, 65537, rng) < 0)
  8956. CYASSL_MSG("MakeRsaKey failed");
  8957. else if (SetRsaExternal(rsa) < 0)
  8958. CYASSL_MSG("SetRsaExternal failed");
  8959. else {
  8960. rsa->inSet = 1;
  8961. ret = SSL_SUCCESS;
  8962. }
  8963. #ifdef CYASSL_SMALL_STACK
  8964. XFREE(rng, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  8965. #endif
  8966. }
  8967. #else
  8968. CYASSL_MSG("No Key Gen built in");
  8969. #endif
  8970. return ret;
  8971. }
  8972. /* SSL_SUCCESS on ok */
  8973. int CyaSSL_RSA_blinding_on(CYASSL_RSA* rsa, CYASSL_BN_CTX* bn)
  8974. {
  8975. (void)rsa;
  8976. (void)bn;
  8977. CYASSL_MSG("CyaSSL_RSA_blinding_on");
  8978. return SSL_SUCCESS; /* on by default */
  8979. }
  8980. int CyaSSL_RSA_public_encrypt(int len, unsigned char* fr,
  8981. unsigned char* to, CYASSL_RSA* rsa, int padding)
  8982. {
  8983. (void)len;
  8984. (void)fr;
  8985. (void)to;
  8986. (void)rsa;
  8987. (void)padding;
  8988. CYASSL_MSG("CyaSSL_RSA_public_encrypt");
  8989. return SSL_FATAL_ERROR;
  8990. }
  8991. int CyaSSL_RSA_private_decrypt(int len, unsigned char* fr,
  8992. unsigned char* to, CYASSL_RSA* rsa, int padding)
  8993. {
  8994. (void)len;
  8995. (void)fr;
  8996. (void)to;
  8997. (void)rsa;
  8998. (void)padding;
  8999. CYASSL_MSG("CyaSSL_RSA_private_decrypt");
  9000. return SSL_FATAL_ERROR;
  9001. }
  9002. int CyaSSL_RSA_size(const CYASSL_RSA* rsa)
  9003. {
  9004. CYASSL_MSG("CyaSSL_RSA_size");
  9005. if (rsa == NULL)
  9006. return 0;
  9007. return CyaSSL_BN_num_bytes(rsa->n);
  9008. }
  9009. #endif /* NO_RSA */
  9010. #ifndef NO_DSA
  9011. /* return SSL_SUCCESS on success, < 0 otherwise */
  9012. int CyaSSL_DSA_do_sign(const unsigned char* d, unsigned char* sigRet,
  9013. CYASSL_DSA* dsa)
  9014. {
  9015. int ret = SSL_FATAL_ERROR;
  9016. RNG* rng = NULL;
  9017. #ifdef CYASSL_SMALL_STACK
  9018. RNG* tmpRNG = NULL;
  9019. #else
  9020. RNG tmpRNG[1];
  9021. #endif
  9022. CYASSL_MSG("CyaSSL_DSA_do_sign");
  9023. if (d == NULL || sigRet == NULL || dsa == NULL)
  9024. CYASSL_MSG("Bad function arguments");
  9025. else if (dsa->inSet == 0)
  9026. CYASSL_MSG("No DSA internal set");
  9027. else {
  9028. #ifdef CYASSL_SMALL_STACK
  9029. tmpRNG = (RNG*)XMALLOC(sizeof(RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9030. if (tmpRNG == NULL)
  9031. return SSL_FATAL_ERROR;
  9032. #endif
  9033. if (InitRng(tmpRNG) == 0)
  9034. rng = tmpRNG;
  9035. else {
  9036. CYASSL_MSG("Bad RNG Init, trying global");
  9037. if (initGlobalRNG == 0)
  9038. CYASSL_MSG("Global RNG no Init");
  9039. else
  9040. rng = &globalRNG;
  9041. }
  9042. if (rng) {
  9043. if (DsaSign(d, sigRet, (DsaKey*)dsa->internal, rng) < 0)
  9044. CYASSL_MSG("DsaSign failed");
  9045. else
  9046. ret = SSL_SUCCESS;
  9047. }
  9048. #ifdef CYASSL_SMALL_STACK
  9049. XFREE(RNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9050. #endif
  9051. }
  9052. return ret;
  9053. }
  9054. #endif /* NO_DSA */
  9055. #ifndef NO_RSA
  9056. /* return SSL_SUCCES on ok, 0 otherwise */
  9057. int CyaSSL_RSA_sign(int type, const unsigned char* m,
  9058. unsigned int mLen, unsigned char* sigRet,
  9059. unsigned int* sigLen, CYASSL_RSA* rsa)
  9060. {
  9061. word32 outLen;
  9062. word32 signSz;
  9063. RNG* rng = NULL;
  9064. int ret = 0;
  9065. #ifdef CYASSL_SMALL_STACK
  9066. RNG* tmpRNG = NULL;
  9067. byte* encodedSig = NULL;
  9068. #else
  9069. RNG tmpRNG[1];
  9070. byte encodedSig[MAX_ENCODED_SIG_SZ];
  9071. #endif
  9072. CYASSL_MSG("CyaSSL_RSA_sign");
  9073. if (m == NULL || sigRet == NULL || sigLen == NULL || rsa == NULL)
  9074. CYASSL_MSG("Bad function arguments");
  9075. else if (rsa->inSet == 0)
  9076. CYASSL_MSG("No RSA internal set");
  9077. else if (type != NID_md5 && type != NID_sha1)
  9078. CYASSL_MSG("Bad md type");
  9079. else {
  9080. outLen = (word32)CyaSSL_BN_num_bytes(rsa->n);
  9081. #ifdef CYASSL_SMALL_STACK
  9082. tmpRNG = (RNG*)XMALLOC(sizeof(RNG), NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9083. if (tmpRNG == NULL)
  9084. return 0;
  9085. encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, NULL,
  9086. DYNAMIC_TYPE_TMP_BUFFER);
  9087. if (encodedSig == NULL) {
  9088. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9089. return 0;
  9090. }
  9091. #endif
  9092. if (outLen == 0)
  9093. CYASSL_MSG("Bad RSA size");
  9094. else if (InitRng(tmpRNG) == 0)
  9095. rng = tmpRNG;
  9096. else {
  9097. CYASSL_MSG("Bad RNG Init, trying global");
  9098. if (initGlobalRNG == 0)
  9099. CYASSL_MSG("Global RNG no Init");
  9100. else
  9101. rng = &globalRNG;
  9102. }
  9103. }
  9104. if (rng) {
  9105. type = (type == NID_md5) ? MD5h : SHAh;
  9106. signSz = EncodeSignature(encodedSig, m, mLen, type);
  9107. if (signSz == 0) {
  9108. CYASSL_MSG("Bad Encode Signature");
  9109. }
  9110. else {
  9111. *sigLen = RsaSSL_Sign(encodedSig, signSz, sigRet, outLen,
  9112. (RsaKey*)rsa->internal, rng);
  9113. if (*sigLen <= 0)
  9114. CYASSL_MSG("Bad Rsa Sign");
  9115. else
  9116. ret = SSL_SUCCESS;
  9117. }
  9118. }
  9119. #ifdef CYASSL_SMALL_STACK
  9120. XFREE(tmpRNG, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9121. XFREE(encodedSig, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9122. #endif
  9123. CYASSL_MSG("CyaSSL_RSA_sign success");
  9124. return ret;
  9125. }
  9126. int CyaSSL_RSA_public_decrypt(int flen, unsigned char* from,
  9127. unsigned char* to, CYASSL_RSA* rsa, int padding)
  9128. {
  9129. (void)flen;
  9130. (void)from;
  9131. (void)to;
  9132. (void)rsa;
  9133. (void)padding;
  9134. CYASSL_MSG("CyaSSL_RSA_public_decrypt");
  9135. return SSL_FATAL_ERROR;
  9136. }
  9137. /* generate p-1 and q-1, SSL_SUCCESS on ok */
  9138. int CyaSSL_RSA_GenAdd(CYASSL_RSA* rsa)
  9139. {
  9140. int err;
  9141. mp_int tmp;
  9142. CYASSL_MSG("CyaSSL_RsaGenAdd");
  9143. if (rsa == NULL || rsa->p == NULL || rsa->q == NULL || rsa->d == NULL ||
  9144. rsa->dmp1 == NULL || rsa->dmq1 == NULL) {
  9145. CYASSL_MSG("rsa no init error");
  9146. return SSL_FATAL_ERROR;
  9147. }
  9148. if (mp_init(&tmp) != MP_OKAY) {
  9149. CYASSL_MSG("mp_init error");
  9150. return SSL_FATAL_ERROR;
  9151. }
  9152. err = mp_sub_d((mp_int*)rsa->p->internal, 1, &tmp);
  9153. if (err != MP_OKAY) {
  9154. CYASSL_MSG("mp_sub_d error");
  9155. }
  9156. else
  9157. err = mp_mod((mp_int*)rsa->d->internal, &tmp,
  9158. (mp_int*)rsa->dmp1->internal);
  9159. if (err != MP_OKAY) {
  9160. CYASSL_MSG("mp_mod error");
  9161. }
  9162. else
  9163. err = mp_sub_d((mp_int*)rsa->q->internal, 1, &tmp);
  9164. if (err != MP_OKAY) {
  9165. CYASSL_MSG("mp_sub_d error");
  9166. }
  9167. else
  9168. err = mp_mod((mp_int*)rsa->d->internal, &tmp,
  9169. (mp_int*)rsa->dmq1->internal);
  9170. mp_clear(&tmp);
  9171. if (err == MP_OKAY)
  9172. return SSL_SUCCESS;
  9173. else
  9174. return SSL_FATAL_ERROR;
  9175. }
  9176. #endif /* NO_RSA */
  9177. void CyaSSL_HMAC_Init(CYASSL_HMAC_CTX* ctx, const void* key, int keylen,
  9178. const EVP_MD* type)
  9179. {
  9180. CYASSL_MSG("CyaSSL_HMAC_Init");
  9181. if (ctx == NULL) {
  9182. CYASSL_MSG("no ctx on init");
  9183. return;
  9184. }
  9185. if (type) {
  9186. CYASSL_MSG("init has type");
  9187. if (XSTRNCMP(type, "MD5", 3) == 0) {
  9188. CYASSL_MSG("md5 hmac");
  9189. ctx->type = MD5;
  9190. }
  9191. else if (XSTRNCMP(type, "SHA256", 6) == 0) {
  9192. CYASSL_MSG("sha256 hmac");
  9193. ctx->type = SHA256;
  9194. }
  9195. /* has to be last since would pick or 256, 384, or 512 too */
  9196. else if (XSTRNCMP(type, "SHA", 3) == 0) {
  9197. CYASSL_MSG("sha hmac");
  9198. ctx->type = SHA;
  9199. }
  9200. else {
  9201. CYASSL_MSG("bad init type");
  9202. }
  9203. }
  9204. if (key && keylen) {
  9205. CYASSL_MSG("keying hmac");
  9206. HmacSetKey(&ctx->hmac, ctx->type, (const byte*)key, (word32)keylen);
  9207. /* OpenSSL compat, no error */
  9208. }
  9209. }
  9210. void CyaSSL_HMAC_Update(CYASSL_HMAC_CTX* ctx, const unsigned char* data,
  9211. int len)
  9212. {
  9213. CYASSL_MSG("CyaSSL_HMAC_Update");
  9214. if (ctx && data) {
  9215. CYASSL_MSG("updating hmac");
  9216. HmacUpdate(&ctx->hmac, data, (word32)len);
  9217. /* OpenSSL compat, no error */
  9218. }
  9219. }
  9220. void CyaSSL_HMAC_Final(CYASSL_HMAC_CTX* ctx, unsigned char* hash,
  9221. unsigned int* len)
  9222. {
  9223. CYASSL_MSG("CyaSSL_HMAC_Final");
  9224. if (ctx && hash) {
  9225. CYASSL_MSG("final hmac");
  9226. HmacFinal(&ctx->hmac, hash);
  9227. /* OpenSSL compat, no error */
  9228. if (len) {
  9229. CYASSL_MSG("setting output len");
  9230. switch (ctx->type) {
  9231. case MD5:
  9232. *len = MD5_DIGEST_SIZE;
  9233. break;
  9234. case SHA:
  9235. *len = SHA_DIGEST_SIZE;
  9236. break;
  9237. case SHA256:
  9238. *len = SHA256_DIGEST_SIZE;
  9239. break;
  9240. default:
  9241. CYASSL_MSG("bad hmac type");
  9242. }
  9243. }
  9244. }
  9245. }
  9246. void CyaSSL_HMAC_cleanup(CYASSL_HMAC_CTX* ctx)
  9247. {
  9248. (void)ctx;
  9249. CYASSL_MSG("CyaSSL_HMAC_cleanup");
  9250. }
  9251. const CYASSL_EVP_MD* CyaSSL_EVP_get_digestbynid(int id)
  9252. {
  9253. CYASSL_MSG("CyaSSL_get_digestbynid");
  9254. switch(id) {
  9255. case NID_md5:
  9256. return CyaSSL_EVP_md5();
  9257. case NID_sha1:
  9258. return CyaSSL_EVP_sha1();
  9259. default:
  9260. CYASSL_MSG("Bad digest id value");
  9261. }
  9262. return NULL;
  9263. }
  9264. CYASSL_RSA* CyaSSL_EVP_PKEY_get1_RSA(CYASSL_EVP_PKEY* key)
  9265. {
  9266. (void)key;
  9267. CYASSL_MSG("CyaSSL_EVP_PKEY_get1_RSA");
  9268. return NULL;
  9269. }
  9270. CYASSL_DSA* CyaSSL_EVP_PKEY_get1_DSA(CYASSL_EVP_PKEY* key)
  9271. {
  9272. (void)key;
  9273. CYASSL_MSG("CyaSSL_EVP_PKEY_get1_DSA");
  9274. return NULL;
  9275. }
  9276. void* CyaSSL_EVP_X_STATE(const CYASSL_EVP_CIPHER_CTX* ctx)
  9277. {
  9278. CYASSL_MSG("CyaSSL_EVP_X_STATE");
  9279. if (ctx) {
  9280. switch (ctx->cipherType) {
  9281. case ARC4_TYPE:
  9282. CYASSL_MSG("returning arc4 state");
  9283. return (void*)&ctx->cipher.arc4.x;
  9284. default:
  9285. CYASSL_MSG("bad x state type");
  9286. return 0;
  9287. }
  9288. }
  9289. return NULL;
  9290. }
  9291. int CyaSSL_EVP_X_STATE_LEN(const CYASSL_EVP_CIPHER_CTX* ctx)
  9292. {
  9293. CYASSL_MSG("CyaSSL_EVP_X_STATE_LEN");
  9294. if (ctx) {
  9295. switch (ctx->cipherType) {
  9296. case ARC4_TYPE:
  9297. CYASSL_MSG("returning arc4 state size");
  9298. return sizeof(Arc4);
  9299. default:
  9300. CYASSL_MSG("bad x state type");
  9301. return 0;
  9302. }
  9303. }
  9304. return 0;
  9305. }
  9306. void CyaSSL_3des_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
  9307. unsigned char* iv, int len)
  9308. {
  9309. (void)len;
  9310. CYASSL_MSG("CyaSSL_3des_iv");
  9311. if (ctx == NULL || iv == NULL) {
  9312. CYASSL_MSG("Bad function argument");
  9313. return;
  9314. }
  9315. if (doset)
  9316. Des3_SetIV(&ctx->cipher.des3, iv); /* OpenSSL compat, no ret */
  9317. else
  9318. memcpy(iv, &ctx->cipher.des3.reg, DES_BLOCK_SIZE);
  9319. }
  9320. void CyaSSL_aes_ctr_iv(CYASSL_EVP_CIPHER_CTX* ctx, int doset,
  9321. unsigned char* iv, int len)
  9322. {
  9323. (void)len;
  9324. CYASSL_MSG("CyaSSL_aes_ctr_iv");
  9325. if (ctx == NULL || iv == NULL) {
  9326. CYASSL_MSG("Bad function argument");
  9327. return;
  9328. }
  9329. if (doset)
  9330. AesSetIV(&ctx->cipher.aes, iv); /* OpenSSL compat, no ret */
  9331. else
  9332. memcpy(iv, &ctx->cipher.aes.reg, AES_BLOCK_SIZE);
  9333. }
  9334. const CYASSL_EVP_MD* CyaSSL_EVP_ripemd160(void)
  9335. {
  9336. CYASSL_MSG("CyaSSL_ripemd160");
  9337. return NULL;
  9338. }
  9339. int CyaSSL_EVP_MD_size(const CYASSL_EVP_MD* type)
  9340. {
  9341. CYASSL_MSG("CyaSSL_EVP_MD_size");
  9342. if (type == NULL) {
  9343. CYASSL_MSG("No md type arg");
  9344. return BAD_FUNC_ARG;
  9345. }
  9346. if (XSTRNCMP(type, "MD5", 3) == 0) {
  9347. return MD5_DIGEST_SIZE;
  9348. }
  9349. else if (XSTRNCMP(type, "SHA256", 6) == 0) {
  9350. return SHA256_DIGEST_SIZE;
  9351. }
  9352. #ifdef CYASSL_SHA384
  9353. else if (XSTRNCMP(type, "SHA384", 6) == 0) {
  9354. return SHA384_DIGEST_SIZE;
  9355. }
  9356. #endif
  9357. #ifdef CYASSL_SHA512
  9358. else if (XSTRNCMP(type, "SHA512", 6) == 0) {
  9359. return SHA512_DIGEST_SIZE;
  9360. }
  9361. #endif
  9362. /* has to be last since would pick or 256, 384, or 512 too */
  9363. else if (XSTRNCMP(type, "SHA", 3) == 0) {
  9364. return SHA_DIGEST_SIZE;
  9365. }
  9366. return BAD_FUNC_ARG;
  9367. }
  9368. int CyaSSL_EVP_CIPHER_CTX_iv_length(const CYASSL_EVP_CIPHER_CTX* ctx)
  9369. {
  9370. CYASSL_MSG("CyaSSL_EVP_CIPHER_CTX_iv_length");
  9371. switch (ctx->cipherType) {
  9372. case AES_128_CBC_TYPE :
  9373. case AES_192_CBC_TYPE :
  9374. case AES_256_CBC_TYPE :
  9375. CYASSL_MSG("AES CBC");
  9376. return AES_BLOCK_SIZE;
  9377. #ifdef CYASSL_AES_COUNTER
  9378. case AES_128_CTR_TYPE :
  9379. case AES_192_CTR_TYPE :
  9380. case AES_256_CTR_TYPE :
  9381. CYASSL_MSG("AES CTR");
  9382. return AES_BLOCK_SIZE;
  9383. #endif
  9384. case DES_CBC_TYPE :
  9385. CYASSL_MSG("DES CBC");
  9386. return DES_BLOCK_SIZE;
  9387. case DES_EDE3_CBC_TYPE :
  9388. CYASSL_MSG("DES EDE3 CBC");
  9389. return DES_BLOCK_SIZE;
  9390. case ARC4_TYPE :
  9391. CYASSL_MSG("ARC4");
  9392. return 0;
  9393. case NULL_CIPHER_TYPE :
  9394. CYASSL_MSG("NULL");
  9395. return 0;
  9396. default: {
  9397. CYASSL_MSG("bad type");
  9398. }
  9399. }
  9400. return 0;
  9401. }
  9402. void CyaSSL_OPENSSL_free(void* p)
  9403. {
  9404. CYASSL_MSG("CyaSSL_OPENSSL_free");
  9405. XFREE(p, NULL, 0);
  9406. }
  9407. int CyaSSL_PEM_write_bio_RSAPrivateKey(CYASSL_BIO* bio, RSA* rsa,
  9408. const EVP_CIPHER* cipher,
  9409. unsigned char* passwd, int len,
  9410. pem_password_cb cb, void* arg)
  9411. {
  9412. (void)bio;
  9413. (void)rsa;
  9414. (void)cipher;
  9415. (void)passwd;
  9416. (void)len;
  9417. (void)cb;
  9418. (void)arg;
  9419. CYASSL_MSG("CyaSSL_PEM_write_bio_RSAPrivateKey");
  9420. return SSL_FATAL_ERROR;
  9421. }
  9422. int CyaSSL_PEM_write_bio_DSAPrivateKey(CYASSL_BIO* bio, DSA* rsa,
  9423. const EVP_CIPHER* cipher,
  9424. unsigned char* passwd, int len,
  9425. pem_password_cb cb, void* arg)
  9426. {
  9427. (void)bio;
  9428. (void)rsa;
  9429. (void)cipher;
  9430. (void)passwd;
  9431. (void)len;
  9432. (void)cb;
  9433. (void)arg;
  9434. CYASSL_MSG("CyaSSL_PEM_write_bio_DSAPrivateKey");
  9435. return SSL_FATAL_ERROR;
  9436. }
  9437. CYASSL_EVP_PKEY* CyaSSL_PEM_read_bio_PrivateKey(CYASSL_BIO* bio,
  9438. CYASSL_EVP_PKEY** key, pem_password_cb cb, void* arg)
  9439. {
  9440. (void)bio;
  9441. (void)key;
  9442. (void)cb;
  9443. (void)arg;
  9444. CYASSL_MSG("CyaSSL_PEM_read_bio_PrivateKey");
  9445. return NULL;
  9446. }
  9447. #ifndef NO_RSA
  9448. /* Load RSA from Der, SSL_SUCCESS on success < 0 on error */
  9449. int CyaSSL_RSA_LoadDer(CYASSL_RSA* rsa, const unsigned char* der, int derSz)
  9450. {
  9451. word32 idx = 0;
  9452. int ret;
  9453. CYASSL_ENTER("CyaSSL_RSA_LoadDer");
  9454. if (rsa == NULL || rsa->internal == NULL || der == NULL || derSz <= 0) {
  9455. CYASSL_MSG("Bad function arguments");
  9456. return BAD_FUNC_ARG;
  9457. }
  9458. ret = RsaPrivateKeyDecode(der, &idx, (RsaKey*)rsa->internal, derSz);
  9459. if (ret < 0) {
  9460. CYASSL_MSG("RsaPrivateKeyDecode failed");
  9461. return ret;
  9462. }
  9463. if (SetRsaExternal(rsa) < 0) {
  9464. CYASSL_MSG("SetRsaExternal failed");
  9465. return SSL_FATAL_ERROR;
  9466. }
  9467. rsa->inSet = 1;
  9468. return SSL_SUCCESS;
  9469. }
  9470. #endif /* NO_RSA */
  9471. #ifndef NO_DSA
  9472. /* Load DSA from Der, SSL_SUCCESS on success < 0 on error */
  9473. int CyaSSL_DSA_LoadDer(CYASSL_DSA* dsa, const unsigned char* der, int derSz)
  9474. {
  9475. word32 idx = 0;
  9476. int ret;
  9477. CYASSL_ENTER("CyaSSL_DSA_LoadDer");
  9478. if (dsa == NULL || dsa->internal == NULL || der == NULL || derSz <= 0) {
  9479. CYASSL_MSG("Bad function arguments");
  9480. return BAD_FUNC_ARG;
  9481. }
  9482. ret = DsaPrivateKeyDecode(der, &idx, (DsaKey*)dsa->internal, derSz);
  9483. if (ret < 0) {
  9484. CYASSL_MSG("DsaPrivateKeyDecode failed");
  9485. return ret;
  9486. }
  9487. if (SetDsaExternal(dsa) < 0) {
  9488. CYASSL_MSG("SetDsaExternal failed");
  9489. return SSL_FATAL_ERROR;
  9490. }
  9491. dsa->inSet = 1;
  9492. return SSL_SUCCESS;
  9493. }
  9494. #endif /* NO_DSA */
  9495. #endif /* OPENSSL_EXTRA */
  9496. #ifdef SESSION_CERTS
  9497. /* Get peer's certificate chain */
  9498. CYASSL_X509_CHAIN* CyaSSL_get_peer_chain(CYASSL* ssl)
  9499. {
  9500. CYASSL_ENTER("CyaSSL_get_peer_chain");
  9501. if (ssl)
  9502. return &ssl->session.chain;
  9503. return 0;
  9504. }
  9505. /* Get peer's certificate chain total count */
  9506. int CyaSSL_get_chain_count(CYASSL_X509_CHAIN* chain)
  9507. {
  9508. CYASSL_ENTER("CyaSSL_get_chain_count");
  9509. if (chain)
  9510. return chain->count;
  9511. return 0;
  9512. }
  9513. /* Get peer's ASN.1 DER ceritifcate at index (idx) length in bytes */
  9514. int CyaSSL_get_chain_length(CYASSL_X509_CHAIN* chain, int idx)
  9515. {
  9516. CYASSL_ENTER("CyaSSL_get_chain_length");
  9517. if (chain)
  9518. return chain->certs[idx].length;
  9519. return 0;
  9520. }
  9521. /* Get peer's ASN.1 DER ceritifcate at index (idx) */
  9522. byte* CyaSSL_get_chain_cert(CYASSL_X509_CHAIN* chain, int idx)
  9523. {
  9524. CYASSL_ENTER("CyaSSL_get_chain_cert");
  9525. if (chain)
  9526. return chain->certs[idx].buffer;
  9527. return 0;
  9528. }
  9529. /* Get peer's CyaSSL X509 ceritifcate at index (idx) */
  9530. CYASSL_X509* CyaSSL_get_chain_X509(CYASSL_X509_CHAIN* chain, int idx)
  9531. {
  9532. int ret;
  9533. CYASSL_X509* x509 = NULL;
  9534. #ifdef CYASSL_SMALL_STACK
  9535. DecodedCert* cert = NULL;
  9536. #else
  9537. DecodedCert cert[1];
  9538. #endif
  9539. CYASSL_ENTER("CyaSSL_get_chain_X509");
  9540. if (chain != NULL) {
  9541. #ifdef CYASSL_SMALL_STACK
  9542. cert = (DecodedCert*)XMALLOC(sizeof(DecodedCert), NULL,
  9543. DYNAMIC_TYPE_TMP_BUFFER);
  9544. if (cert != NULL)
  9545. #endif
  9546. {
  9547. InitDecodedCert(cert, chain->certs[idx].buffer,
  9548. chain->certs[idx].length, NULL);
  9549. if ((ret = ParseCertRelative(cert, CERT_TYPE, 0, NULL)) != 0)
  9550. CYASSL_MSG("Failed to parse cert");
  9551. else {
  9552. x509 = (CYASSL_X509*)XMALLOC(sizeof(CYASSL_X509), NULL,
  9553. DYNAMIC_TYPE_X509);
  9554. if (x509 == NULL) {
  9555. CYASSL_MSG("Failed alloc X509");
  9556. }
  9557. else {
  9558. InitX509(x509, 1);
  9559. if ((ret = CopyDecodedToX509(x509, cert)) != 0) {
  9560. CYASSL_MSG("Failed to copy decoded");
  9561. XFREE(x509, NULL, DYNAMIC_TYPE_X509);
  9562. x509 = NULL;
  9563. }
  9564. }
  9565. }
  9566. FreeDecodedCert(cert);
  9567. #ifdef CYASSL_SMALL_STACK
  9568. XFREE(cert, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  9569. #endif
  9570. }
  9571. }
  9572. return x509;
  9573. }
  9574. /* Get peer's PEM ceritifcate at index (idx), output to buffer if inLen big
  9575. enough else return error (-1), output length is in *outLen
  9576. SSL_SUCCESS on ok */
  9577. int CyaSSL_get_chain_cert_pem(CYASSL_X509_CHAIN* chain, int idx,
  9578. unsigned char* buf, int inLen, int* outLen)
  9579. {
  9580. const char header[] = "-----BEGIN CERTIFICATE-----\n";
  9581. const char footer[] = "-----END CERTIFICATE-----\n";
  9582. int headerLen = sizeof(header) - 1;
  9583. int footerLen = sizeof(footer) - 1;
  9584. int i;
  9585. int err;
  9586. CYASSL_ENTER("CyaSSL_get_chain_cert_pem");
  9587. if (!chain || !outLen || !buf)
  9588. return BAD_FUNC_ARG;
  9589. /* don't even try if inLen too short */
  9590. if (inLen < headerLen + footerLen + chain->certs[idx].length)
  9591. return BAD_FUNC_ARG;
  9592. /* header */
  9593. XMEMCPY(buf, header, headerLen);
  9594. i = headerLen;
  9595. /* body */
  9596. *outLen = inLen; /* input to Base64_Encode */
  9597. if ( (err = Base64_Encode(chain->certs[idx].buffer,
  9598. chain->certs[idx].length, buf + i, (word32*)outLen)) < 0)
  9599. return err;
  9600. i += *outLen;
  9601. /* footer */
  9602. if ( (i + footerLen) > inLen)
  9603. return BAD_FUNC_ARG;
  9604. XMEMCPY(buf + i, footer, footerLen);
  9605. *outLen += headerLen + footerLen;
  9606. return SSL_SUCCESS;
  9607. }
  9608. /* get session ID */
  9609. const byte* CyaSSL_get_sessionID(const CYASSL_SESSION* session)
  9610. {
  9611. CYASSL_ENTER("CyaSSL_get_sessionID");
  9612. if (session)
  9613. return session->sessionID;
  9614. return NULL;
  9615. }
  9616. #endif /* SESSION_CERTS */
  9617. #ifdef HAVE_FUZZER
  9618. void CyaSSL_SetFuzzerCb(CYASSL* ssl, CallbackFuzzer cbf, void* fCtx)
  9619. {
  9620. if (ssl) {
  9621. ssl->fuzzerCb = cbf;
  9622. ssl->fuzzerCtx = fCtx;
  9623. }
  9624. }
  9625. #endif
  9626. #ifndef NO_CERTS
  9627. #ifdef HAVE_PK_CALLBACKS
  9628. #ifdef HAVE_ECC
  9629. void CyaSSL_CTX_SetEccSignCb(CYASSL_CTX* ctx, CallbackEccSign cb)
  9630. {
  9631. if (ctx)
  9632. ctx->EccSignCb = cb;
  9633. }
  9634. void CyaSSL_SetEccSignCtx(CYASSL* ssl, void *ctx)
  9635. {
  9636. if (ssl)
  9637. ssl->EccSignCtx = ctx;
  9638. }
  9639. void* CyaSSL_GetEccSignCtx(CYASSL* ssl)
  9640. {
  9641. if (ssl)
  9642. return ssl->EccSignCtx;
  9643. return NULL;
  9644. }
  9645. void CyaSSL_CTX_SetEccVerifyCb(CYASSL_CTX* ctx, CallbackEccVerify cb)
  9646. {
  9647. if (ctx)
  9648. ctx->EccVerifyCb = cb;
  9649. }
  9650. void CyaSSL_SetEccVerifyCtx(CYASSL* ssl, void *ctx)
  9651. {
  9652. if (ssl)
  9653. ssl->EccVerifyCtx = ctx;
  9654. }
  9655. void* CyaSSL_GetEccVerifyCtx(CYASSL* ssl)
  9656. {
  9657. if (ssl)
  9658. return ssl->EccVerifyCtx;
  9659. return NULL;
  9660. }
  9661. #endif /* HAVE_ECC */
  9662. #ifndef NO_RSA
  9663. void CyaSSL_CTX_SetRsaSignCb(CYASSL_CTX* ctx, CallbackRsaSign cb)
  9664. {
  9665. if (ctx)
  9666. ctx->RsaSignCb = cb;
  9667. }
  9668. void CyaSSL_SetRsaSignCtx(CYASSL* ssl, void *ctx)
  9669. {
  9670. if (ssl)
  9671. ssl->RsaSignCtx = ctx;
  9672. }
  9673. void* CyaSSL_GetRsaSignCtx(CYASSL* ssl)
  9674. {
  9675. if (ssl)
  9676. return ssl->RsaSignCtx;
  9677. return NULL;
  9678. }
  9679. void CyaSSL_CTX_SetRsaVerifyCb(CYASSL_CTX* ctx, CallbackRsaVerify cb)
  9680. {
  9681. if (ctx)
  9682. ctx->RsaVerifyCb = cb;
  9683. }
  9684. void CyaSSL_SetRsaVerifyCtx(CYASSL* ssl, void *ctx)
  9685. {
  9686. if (ssl)
  9687. ssl->RsaVerifyCtx = ctx;
  9688. }
  9689. void* CyaSSL_GetRsaVerifyCtx(CYASSL* ssl)
  9690. {
  9691. if (ssl)
  9692. return ssl->RsaVerifyCtx;
  9693. return NULL;
  9694. }
  9695. void CyaSSL_CTX_SetRsaEncCb(CYASSL_CTX* ctx, CallbackRsaEnc cb)
  9696. {
  9697. if (ctx)
  9698. ctx->RsaEncCb = cb;
  9699. }
  9700. void CyaSSL_SetRsaEncCtx(CYASSL* ssl, void *ctx)
  9701. {
  9702. if (ssl)
  9703. ssl->RsaEncCtx = ctx;
  9704. }
  9705. void* CyaSSL_GetRsaEncCtx(CYASSL* ssl)
  9706. {
  9707. if (ssl)
  9708. return ssl->RsaEncCtx;
  9709. return NULL;
  9710. }
  9711. void CyaSSL_CTX_SetRsaDecCb(CYASSL_CTX* ctx, CallbackRsaDec cb)
  9712. {
  9713. if (ctx)
  9714. ctx->RsaDecCb = cb;
  9715. }
  9716. void CyaSSL_SetRsaDecCtx(CYASSL* ssl, void *ctx)
  9717. {
  9718. if (ssl)
  9719. ssl->RsaDecCtx = ctx;
  9720. }
  9721. void* CyaSSL_GetRsaDecCtx(CYASSL* ssl)
  9722. {
  9723. if (ssl)
  9724. return ssl->RsaDecCtx;
  9725. return NULL;
  9726. }
  9727. #endif /* NO_RSA */
  9728. #endif /* HAVE_PK_CALLBACKS */
  9729. #endif /* NO_CERTS */
  9730. #ifdef CYASSL_HAVE_WOLFSCEP
  9731. /* Used by autoconf to see if wolfSCEP is available */
  9732. void CyaSSL_wolfSCEP(void) {}
  9733. #endif
  9734. #ifdef CYASSL_HAVE_CERT_SERVICE
  9735. /* Used by autoconf to see if cert service is available */
  9736. void CyaSSL_cert_service(void) {}
  9737. #endif