ssl.c 304 KB

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