ssl_lib.c 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. * Copyright 2005 Nokia. All rights reserved.
  5. *
  6. * Licensed under the Apache License 2.0 (the "License"). You may not use
  7. * this file except in compliance with the License. You can obtain a copy
  8. * in the file LICENSE in the source distribution or at
  9. * https://www.openssl.org/source/license.html
  10. */
  11. #include <stdio.h>
  12. #include "ssl_local.h"
  13. #include "e_os.h"
  14. #include <openssl/objects.h>
  15. #include <openssl/x509v3.h>
  16. #include <openssl/rand.h>
  17. #include <openssl/rand_drbg.h>
  18. #include <openssl/ocsp.h>
  19. #include <openssl/dh.h>
  20. #include <openssl/engine.h>
  21. #include <openssl/async.h>
  22. #include <openssl/ct.h>
  23. #include <openssl/trace.h>
  24. #include "internal/cryptlib.h"
  25. #include "internal/refcount.h"
  26. #include "internal/ktls.h"
  27. static int ssl_undefined_function_1(SSL *ssl, SSL3_RECORD *r, size_t s, int t)
  28. {
  29. (void)r;
  30. (void)s;
  31. (void)t;
  32. return ssl_undefined_function(ssl);
  33. }
  34. static int ssl_undefined_function_2(SSL *ssl, SSL3_RECORD *r, unsigned char *s,
  35. int t)
  36. {
  37. (void)r;
  38. (void)s;
  39. (void)t;
  40. return ssl_undefined_function(ssl);
  41. }
  42. static int ssl_undefined_function_3(SSL *ssl, unsigned char *r,
  43. unsigned char *s, size_t t, size_t *u)
  44. {
  45. (void)r;
  46. (void)s;
  47. (void)t;
  48. (void)u;
  49. return ssl_undefined_function(ssl);
  50. }
  51. static int ssl_undefined_function_4(SSL *ssl, int r)
  52. {
  53. (void)r;
  54. return ssl_undefined_function(ssl);
  55. }
  56. static size_t ssl_undefined_function_5(SSL *ssl, const char *r, size_t s,
  57. unsigned char *t)
  58. {
  59. (void)r;
  60. (void)s;
  61. (void)t;
  62. return ssl_undefined_function(ssl);
  63. }
  64. static int ssl_undefined_function_6(int r)
  65. {
  66. (void)r;
  67. return ssl_undefined_function(NULL);
  68. }
  69. static int ssl_undefined_function_7(SSL *ssl, unsigned char *r, size_t s,
  70. const char *t, size_t u,
  71. const unsigned char *v, size_t w, int x)
  72. {
  73. (void)r;
  74. (void)s;
  75. (void)t;
  76. (void)u;
  77. (void)v;
  78. (void)w;
  79. (void)x;
  80. return ssl_undefined_function(ssl);
  81. }
  82. SSL3_ENC_METHOD ssl3_undef_enc_method = {
  83. ssl_undefined_function_1,
  84. ssl_undefined_function_2,
  85. ssl_undefined_function,
  86. ssl_undefined_function_3,
  87. ssl_undefined_function_4,
  88. ssl_undefined_function_5,
  89. NULL, /* client_finished_label */
  90. 0, /* client_finished_label_len */
  91. NULL, /* server_finished_label */
  92. 0, /* server_finished_label_len */
  93. ssl_undefined_function_6,
  94. ssl_undefined_function_7,
  95. };
  96. struct ssl_async_args {
  97. SSL *s;
  98. void *buf;
  99. size_t num;
  100. enum { READFUNC, WRITEFUNC, OTHERFUNC } type;
  101. union {
  102. int (*func_read) (SSL *, void *, size_t, size_t *);
  103. int (*func_write) (SSL *, const void *, size_t, size_t *);
  104. int (*func_other) (SSL *);
  105. } f;
  106. };
  107. static const struct {
  108. uint8_t mtype;
  109. uint8_t ord;
  110. int nid;
  111. } dane_mds[] = {
  112. {
  113. DANETLS_MATCHING_FULL, 0, NID_undef
  114. },
  115. {
  116. DANETLS_MATCHING_2256, 1, NID_sha256
  117. },
  118. {
  119. DANETLS_MATCHING_2512, 2, NID_sha512
  120. },
  121. };
  122. static int dane_ctx_enable(struct dane_ctx_st *dctx)
  123. {
  124. const EVP_MD **mdevp;
  125. uint8_t *mdord;
  126. uint8_t mdmax = DANETLS_MATCHING_LAST;
  127. int n = ((int)mdmax) + 1; /* int to handle PrivMatch(255) */
  128. size_t i;
  129. if (dctx->mdevp != NULL)
  130. return 1;
  131. mdevp = OPENSSL_zalloc(n * sizeof(*mdevp));
  132. mdord = OPENSSL_zalloc(n * sizeof(*mdord));
  133. if (mdord == NULL || mdevp == NULL) {
  134. OPENSSL_free(mdord);
  135. OPENSSL_free(mdevp);
  136. SSLerr(SSL_F_DANE_CTX_ENABLE, ERR_R_MALLOC_FAILURE);
  137. return 0;
  138. }
  139. /* Install default entries */
  140. for (i = 0; i < OSSL_NELEM(dane_mds); ++i) {
  141. const EVP_MD *md;
  142. if (dane_mds[i].nid == NID_undef ||
  143. (md = EVP_get_digestbynid(dane_mds[i].nid)) == NULL)
  144. continue;
  145. mdevp[dane_mds[i].mtype] = md;
  146. mdord[dane_mds[i].mtype] = dane_mds[i].ord;
  147. }
  148. dctx->mdevp = mdevp;
  149. dctx->mdord = mdord;
  150. dctx->mdmax = mdmax;
  151. return 1;
  152. }
  153. static void dane_ctx_final(struct dane_ctx_st *dctx)
  154. {
  155. OPENSSL_free(dctx->mdevp);
  156. dctx->mdevp = NULL;
  157. OPENSSL_free(dctx->mdord);
  158. dctx->mdord = NULL;
  159. dctx->mdmax = 0;
  160. }
  161. static void tlsa_free(danetls_record *t)
  162. {
  163. if (t == NULL)
  164. return;
  165. OPENSSL_free(t->data);
  166. EVP_PKEY_free(t->spki);
  167. OPENSSL_free(t);
  168. }
  169. static void dane_final(SSL_DANE *dane)
  170. {
  171. sk_danetls_record_pop_free(dane->trecs, tlsa_free);
  172. dane->trecs = NULL;
  173. sk_X509_pop_free(dane->certs, X509_free);
  174. dane->certs = NULL;
  175. X509_free(dane->mcert);
  176. dane->mcert = NULL;
  177. dane->mtlsa = NULL;
  178. dane->mdpth = -1;
  179. dane->pdpth = -1;
  180. }
  181. /*
  182. * dane_copy - Copy dane configuration, sans verification state.
  183. */
  184. static int ssl_dane_dup(SSL *to, SSL *from)
  185. {
  186. int num;
  187. int i;
  188. if (!DANETLS_ENABLED(&from->dane))
  189. return 1;
  190. num = sk_danetls_record_num(from->dane.trecs);
  191. dane_final(&to->dane);
  192. to->dane.flags = from->dane.flags;
  193. to->dane.dctx = &to->ctx->dane;
  194. to->dane.trecs = sk_danetls_record_new_reserve(NULL, num);
  195. if (to->dane.trecs == NULL) {
  196. SSLerr(SSL_F_SSL_DANE_DUP, ERR_R_MALLOC_FAILURE);
  197. return 0;
  198. }
  199. for (i = 0; i < num; ++i) {
  200. danetls_record *t = sk_danetls_record_value(from->dane.trecs, i);
  201. if (SSL_dane_tlsa_add(to, t->usage, t->selector, t->mtype,
  202. t->data, t->dlen) <= 0)
  203. return 0;
  204. }
  205. return 1;
  206. }
  207. static int dane_mtype_set(struct dane_ctx_st *dctx,
  208. const EVP_MD *md, uint8_t mtype, uint8_t ord)
  209. {
  210. int i;
  211. if (mtype == DANETLS_MATCHING_FULL && md != NULL) {
  212. SSLerr(SSL_F_DANE_MTYPE_SET, SSL_R_DANE_CANNOT_OVERRIDE_MTYPE_FULL);
  213. return 0;
  214. }
  215. if (mtype > dctx->mdmax) {
  216. const EVP_MD **mdevp;
  217. uint8_t *mdord;
  218. int n = ((int)mtype) + 1;
  219. mdevp = OPENSSL_realloc(dctx->mdevp, n * sizeof(*mdevp));
  220. if (mdevp == NULL) {
  221. SSLerr(SSL_F_DANE_MTYPE_SET, ERR_R_MALLOC_FAILURE);
  222. return -1;
  223. }
  224. dctx->mdevp = mdevp;
  225. mdord = OPENSSL_realloc(dctx->mdord, n * sizeof(*mdord));
  226. if (mdord == NULL) {
  227. SSLerr(SSL_F_DANE_MTYPE_SET, ERR_R_MALLOC_FAILURE);
  228. return -1;
  229. }
  230. dctx->mdord = mdord;
  231. /* Zero-fill any gaps */
  232. for (i = dctx->mdmax + 1; i < mtype; ++i) {
  233. mdevp[i] = NULL;
  234. mdord[i] = 0;
  235. }
  236. dctx->mdmax = mtype;
  237. }
  238. dctx->mdevp[mtype] = md;
  239. /* Coerce ordinal of disabled matching types to 0 */
  240. dctx->mdord[mtype] = (md == NULL) ? 0 : ord;
  241. return 1;
  242. }
  243. static const EVP_MD *tlsa_md_get(SSL_DANE *dane, uint8_t mtype)
  244. {
  245. if (mtype > dane->dctx->mdmax)
  246. return NULL;
  247. return dane->dctx->mdevp[mtype];
  248. }
  249. static int dane_tlsa_add(SSL_DANE *dane,
  250. uint8_t usage,
  251. uint8_t selector,
  252. uint8_t mtype, unsigned const char *data, size_t dlen)
  253. {
  254. danetls_record *t;
  255. const EVP_MD *md = NULL;
  256. int ilen = (int)dlen;
  257. int i;
  258. int num;
  259. if (dane->trecs == NULL) {
  260. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_NOT_ENABLED);
  261. return -1;
  262. }
  263. if (ilen < 0 || dlen != (size_t)ilen) {
  264. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_DATA_LENGTH);
  265. return 0;
  266. }
  267. if (usage > DANETLS_USAGE_LAST) {
  268. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE_USAGE);
  269. return 0;
  270. }
  271. if (selector > DANETLS_SELECTOR_LAST) {
  272. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_SELECTOR);
  273. return 0;
  274. }
  275. if (mtype != DANETLS_MATCHING_FULL) {
  276. md = tlsa_md_get(dane, mtype);
  277. if (md == NULL) {
  278. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_MATCHING_TYPE);
  279. return 0;
  280. }
  281. }
  282. if (md != NULL && dlen != (size_t)EVP_MD_size(md)) {
  283. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_DIGEST_LENGTH);
  284. return 0;
  285. }
  286. if (!data) {
  287. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_NULL_DATA);
  288. return 0;
  289. }
  290. if ((t = OPENSSL_zalloc(sizeof(*t))) == NULL) {
  291. SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
  292. return -1;
  293. }
  294. t->usage = usage;
  295. t->selector = selector;
  296. t->mtype = mtype;
  297. t->data = OPENSSL_malloc(dlen);
  298. if (t->data == NULL) {
  299. tlsa_free(t);
  300. SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
  301. return -1;
  302. }
  303. memcpy(t->data, data, dlen);
  304. t->dlen = dlen;
  305. /* Validate and cache full certificate or public key */
  306. if (mtype == DANETLS_MATCHING_FULL) {
  307. const unsigned char *p = data;
  308. X509 *cert = NULL;
  309. EVP_PKEY *pkey = NULL;
  310. switch (selector) {
  311. case DANETLS_SELECTOR_CERT:
  312. if (!d2i_X509(&cert, &p, ilen) || p < data ||
  313. dlen != (size_t)(p - data)) {
  314. tlsa_free(t);
  315. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
  316. return 0;
  317. }
  318. if (X509_get0_pubkey(cert) == NULL) {
  319. tlsa_free(t);
  320. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_CERTIFICATE);
  321. return 0;
  322. }
  323. if ((DANETLS_USAGE_BIT(usage) & DANETLS_TA_MASK) == 0) {
  324. X509_free(cert);
  325. break;
  326. }
  327. /*
  328. * For usage DANE-TA(2), we support authentication via "2 0 0" TLSA
  329. * records that contain full certificates of trust-anchors that are
  330. * not present in the wire chain. For usage PKIX-TA(0), we augment
  331. * the chain with untrusted Full(0) certificates from DNS, in case
  332. * they are missing from the chain.
  333. */
  334. if ((dane->certs == NULL &&
  335. (dane->certs = sk_X509_new_null()) == NULL) ||
  336. !sk_X509_push(dane->certs, cert)) {
  337. SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
  338. X509_free(cert);
  339. tlsa_free(t);
  340. return -1;
  341. }
  342. break;
  343. case DANETLS_SELECTOR_SPKI:
  344. if (!d2i_PUBKEY(&pkey, &p, ilen) || p < data ||
  345. dlen != (size_t)(p - data)) {
  346. tlsa_free(t);
  347. SSLerr(SSL_F_DANE_TLSA_ADD, SSL_R_DANE_TLSA_BAD_PUBLIC_KEY);
  348. return 0;
  349. }
  350. /*
  351. * For usage DANE-TA(2), we support authentication via "2 1 0" TLSA
  352. * records that contain full bare keys of trust-anchors that are
  353. * not present in the wire chain.
  354. */
  355. if (usage == DANETLS_USAGE_DANE_TA)
  356. t->spki = pkey;
  357. else
  358. EVP_PKEY_free(pkey);
  359. break;
  360. }
  361. }
  362. /*-
  363. * Find the right insertion point for the new record.
  364. *
  365. * See crypto/x509/x509_vfy.c. We sort DANE-EE(3) records first, so that
  366. * they can be processed first, as they require no chain building, and no
  367. * expiration or hostname checks. Because DANE-EE(3) is numerically
  368. * largest, this is accomplished via descending sort by "usage".
  369. *
  370. * We also sort in descending order by matching ordinal to simplify
  371. * the implementation of digest agility in the verification code.
  372. *
  373. * The choice of order for the selector is not significant, so we
  374. * use the same descending order for consistency.
  375. */
  376. num = sk_danetls_record_num(dane->trecs);
  377. for (i = 0; i < num; ++i) {
  378. danetls_record *rec = sk_danetls_record_value(dane->trecs, i);
  379. if (rec->usage > usage)
  380. continue;
  381. if (rec->usage < usage)
  382. break;
  383. if (rec->selector > selector)
  384. continue;
  385. if (rec->selector < selector)
  386. break;
  387. if (dane->dctx->mdord[rec->mtype] > dane->dctx->mdord[mtype])
  388. continue;
  389. break;
  390. }
  391. if (!sk_danetls_record_insert(dane->trecs, t, i)) {
  392. tlsa_free(t);
  393. SSLerr(SSL_F_DANE_TLSA_ADD, ERR_R_MALLOC_FAILURE);
  394. return -1;
  395. }
  396. dane->umask |= DANETLS_USAGE_BIT(usage);
  397. return 1;
  398. }
  399. /*
  400. * Return 0 if there is only one version configured and it was disabled
  401. * at configure time. Return 1 otherwise.
  402. */
  403. static int ssl_check_allowed_versions(int min_version, int max_version)
  404. {
  405. int minisdtls = 0, maxisdtls = 0;
  406. /* Figure out if we're doing DTLS versions or TLS versions */
  407. if (min_version == DTLS1_BAD_VER
  408. || min_version >> 8 == DTLS1_VERSION_MAJOR)
  409. minisdtls = 1;
  410. if (max_version == DTLS1_BAD_VER
  411. || max_version >> 8 == DTLS1_VERSION_MAJOR)
  412. maxisdtls = 1;
  413. /* A wildcard version of 0 could be DTLS or TLS. */
  414. if ((minisdtls && !maxisdtls && max_version != 0)
  415. || (maxisdtls && !minisdtls && min_version != 0)) {
  416. /* Mixing DTLS and TLS versions will lead to sadness; deny it. */
  417. return 0;
  418. }
  419. if (minisdtls || maxisdtls) {
  420. /* Do DTLS version checks. */
  421. if (min_version == 0)
  422. /* Ignore DTLS1_BAD_VER */
  423. min_version = DTLS1_VERSION;
  424. if (max_version == 0)
  425. max_version = DTLS1_2_VERSION;
  426. #ifdef OPENSSL_NO_DTLS1_2
  427. if (max_version == DTLS1_2_VERSION)
  428. max_version = DTLS1_VERSION;
  429. #endif
  430. #ifdef OPENSSL_NO_DTLS1
  431. if (min_version == DTLS1_VERSION)
  432. min_version = DTLS1_2_VERSION;
  433. #endif
  434. /* Done massaging versions; do the check. */
  435. if (0
  436. #ifdef OPENSSL_NO_DTLS1
  437. || (DTLS_VERSION_GE(min_version, DTLS1_VERSION)
  438. && DTLS_VERSION_GE(DTLS1_VERSION, max_version))
  439. #endif
  440. #ifdef OPENSSL_NO_DTLS1_2
  441. || (DTLS_VERSION_GE(min_version, DTLS1_2_VERSION)
  442. && DTLS_VERSION_GE(DTLS1_2_VERSION, max_version))
  443. #endif
  444. )
  445. return 0;
  446. } else {
  447. /* Regular TLS version checks. */
  448. if (min_version == 0)
  449. min_version = SSL3_VERSION;
  450. if (max_version == 0)
  451. max_version = TLS1_3_VERSION;
  452. #ifdef OPENSSL_NO_TLS1_3
  453. if (max_version == TLS1_3_VERSION)
  454. max_version = TLS1_2_VERSION;
  455. #endif
  456. #ifdef OPENSSL_NO_TLS1_2
  457. if (max_version == TLS1_2_VERSION)
  458. max_version = TLS1_1_VERSION;
  459. #endif
  460. #ifdef OPENSSL_NO_TLS1_1
  461. if (max_version == TLS1_1_VERSION)
  462. max_version = TLS1_VERSION;
  463. #endif
  464. #ifdef OPENSSL_NO_TLS1
  465. if (max_version == TLS1_VERSION)
  466. max_version = SSL3_VERSION;
  467. #endif
  468. #ifdef OPENSSL_NO_SSL3
  469. if (min_version == SSL3_VERSION)
  470. min_version = TLS1_VERSION;
  471. #endif
  472. #ifdef OPENSSL_NO_TLS1
  473. if (min_version == TLS1_VERSION)
  474. min_version = TLS1_1_VERSION;
  475. #endif
  476. #ifdef OPENSSL_NO_TLS1_1
  477. if (min_version == TLS1_1_VERSION)
  478. min_version = TLS1_2_VERSION;
  479. #endif
  480. #ifdef OPENSSL_NO_TLS1_2
  481. if (min_version == TLS1_2_VERSION)
  482. min_version = TLS1_3_VERSION;
  483. #endif
  484. /* Done massaging versions; do the check. */
  485. if (0
  486. #ifdef OPENSSL_NO_SSL3
  487. || (min_version <= SSL3_VERSION && SSL3_VERSION <= max_version)
  488. #endif
  489. #ifdef OPENSSL_NO_TLS1
  490. || (min_version <= TLS1_VERSION && TLS1_VERSION <= max_version)
  491. #endif
  492. #ifdef OPENSSL_NO_TLS1_1
  493. || (min_version <= TLS1_1_VERSION && TLS1_1_VERSION <= max_version)
  494. #endif
  495. #ifdef OPENSSL_NO_TLS1_2
  496. || (min_version <= TLS1_2_VERSION && TLS1_2_VERSION <= max_version)
  497. #endif
  498. #ifdef OPENSSL_NO_TLS1_3
  499. || (min_version <= TLS1_3_VERSION && TLS1_3_VERSION <= max_version)
  500. #endif
  501. )
  502. return 0;
  503. }
  504. return 1;
  505. }
  506. static void clear_ciphers(SSL *s)
  507. {
  508. /* clear the current cipher */
  509. ssl_clear_cipher_ctx(s);
  510. ssl_clear_hash_ctx(&s->read_hash);
  511. ssl_clear_hash_ctx(&s->write_hash);
  512. }
  513. int SSL_clear(SSL *s)
  514. {
  515. if (s->method == NULL) {
  516. SSLerr(SSL_F_SSL_CLEAR, SSL_R_NO_METHOD_SPECIFIED);
  517. return 0;
  518. }
  519. if (ssl_clear_bad_session(s)) {
  520. SSL_SESSION_free(s->session);
  521. s->session = NULL;
  522. }
  523. SSL_SESSION_free(s->psksession);
  524. s->psksession = NULL;
  525. OPENSSL_free(s->psksession_id);
  526. s->psksession_id = NULL;
  527. s->psksession_id_len = 0;
  528. s->hello_retry_request = 0;
  529. s->sent_tickets = 0;
  530. s->error = 0;
  531. s->hit = 0;
  532. s->shutdown = 0;
  533. if (s->renegotiate) {
  534. SSLerr(SSL_F_SSL_CLEAR, ERR_R_INTERNAL_ERROR);
  535. return 0;
  536. }
  537. ossl_statem_clear(s);
  538. s->version = s->method->version;
  539. s->client_version = s->version;
  540. s->rwstate = SSL_NOTHING;
  541. BUF_MEM_free(s->init_buf);
  542. s->init_buf = NULL;
  543. clear_ciphers(s);
  544. s->first_packet = 0;
  545. s->key_update = SSL_KEY_UPDATE_NONE;
  546. EVP_MD_CTX_free(s->pha_dgst);
  547. s->pha_dgst = NULL;
  548. /* Reset DANE verification result state */
  549. s->dane.mdpth = -1;
  550. s->dane.pdpth = -1;
  551. X509_free(s->dane.mcert);
  552. s->dane.mcert = NULL;
  553. s->dane.mtlsa = NULL;
  554. /* Clear the verification result peername */
  555. X509_VERIFY_PARAM_move_peername(s->param, NULL);
  556. /* Clear any shared connection state */
  557. OPENSSL_free(s->shared_sigalgs);
  558. s->shared_sigalgs = NULL;
  559. s->shared_sigalgslen = 0;
  560. /*
  561. * Check to see if we were changed into a different method, if so, revert
  562. * back.
  563. */
  564. if (s->method != s->ctx->method) {
  565. s->method->ssl_free(s);
  566. s->method = s->ctx->method;
  567. if (!s->method->ssl_new(s))
  568. return 0;
  569. } else {
  570. if (!s->method->ssl_clear(s))
  571. return 0;
  572. }
  573. RECORD_LAYER_clear(&s->rlayer);
  574. return 1;
  575. }
  576. /** Used to change an SSL_CTXs default SSL method type */
  577. int SSL_CTX_set_ssl_version(SSL_CTX *ctx, const SSL_METHOD *meth)
  578. {
  579. STACK_OF(SSL_CIPHER) *sk;
  580. ctx->method = meth;
  581. if (!SSL_CTX_set_ciphersuites(ctx, OSSL_default_ciphersuites())) {
  582. SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
  583. return 0;
  584. }
  585. sk = ssl_create_cipher_list(ctx->method,
  586. ctx->tls13_ciphersuites,
  587. &(ctx->cipher_list),
  588. &(ctx->cipher_list_by_id),
  589. OSSL_default_cipher_list(), ctx->cert);
  590. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= 0)) {
  591. SSLerr(SSL_F_SSL_CTX_SET_SSL_VERSION, SSL_R_SSL_LIBRARY_HAS_NO_CIPHERS);
  592. return 0;
  593. }
  594. return 1;
  595. }
  596. SSL *SSL_new(SSL_CTX *ctx)
  597. {
  598. SSL *s;
  599. if (ctx == NULL) {
  600. SSLerr(SSL_F_SSL_NEW, SSL_R_NULL_SSL_CTX);
  601. return NULL;
  602. }
  603. if (ctx->method == NULL) {
  604. SSLerr(SSL_F_SSL_NEW, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  605. return NULL;
  606. }
  607. s = OPENSSL_zalloc(sizeof(*s));
  608. if (s == NULL)
  609. goto err;
  610. s->references = 1;
  611. s->lock = CRYPTO_THREAD_lock_new();
  612. if (s->lock == NULL) {
  613. OPENSSL_free(s);
  614. s = NULL;
  615. goto err;
  616. }
  617. RECORD_LAYER_init(&s->rlayer, s);
  618. s->options = ctx->options;
  619. s->dane.flags = ctx->dane.flags;
  620. s->min_proto_version = ctx->min_proto_version;
  621. s->max_proto_version = ctx->max_proto_version;
  622. s->mode = ctx->mode;
  623. s->max_cert_list = ctx->max_cert_list;
  624. s->max_early_data = ctx->max_early_data;
  625. s->recv_max_early_data = ctx->recv_max_early_data;
  626. s->num_tickets = ctx->num_tickets;
  627. s->pha_enabled = ctx->pha_enabled;
  628. /* Shallow copy of the ciphersuites stack */
  629. s->tls13_ciphersuites = sk_SSL_CIPHER_dup(ctx->tls13_ciphersuites);
  630. if (s->tls13_ciphersuites == NULL)
  631. goto err;
  632. /*
  633. * Earlier library versions used to copy the pointer to the CERT, not
  634. * its contents; only when setting new parameters for the per-SSL
  635. * copy, ssl_cert_new would be called (and the direct reference to
  636. * the per-SSL_CTX settings would be lost, but those still were
  637. * indirectly accessed for various purposes, and for that reason they
  638. * used to be known as s->ctx->default_cert). Now we don't look at the
  639. * SSL_CTX's CERT after having duplicated it once.
  640. */
  641. s->cert = ssl_cert_dup(ctx->cert);
  642. if (s->cert == NULL)
  643. goto err;
  644. RECORD_LAYER_set_read_ahead(&s->rlayer, ctx->read_ahead);
  645. s->msg_callback = ctx->msg_callback;
  646. s->msg_callback_arg = ctx->msg_callback_arg;
  647. s->verify_mode = ctx->verify_mode;
  648. s->not_resumable_session_cb = ctx->not_resumable_session_cb;
  649. s->record_padding_cb = ctx->record_padding_cb;
  650. s->record_padding_arg = ctx->record_padding_arg;
  651. s->block_padding = ctx->block_padding;
  652. s->sid_ctx_length = ctx->sid_ctx_length;
  653. if (!ossl_assert(s->sid_ctx_length <= sizeof(s->sid_ctx)))
  654. goto err;
  655. memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
  656. s->verify_callback = ctx->default_verify_callback;
  657. s->generate_session_id = ctx->generate_session_id;
  658. s->param = X509_VERIFY_PARAM_new();
  659. if (s->param == NULL)
  660. goto err;
  661. X509_VERIFY_PARAM_inherit(s->param, ctx->param);
  662. s->quiet_shutdown = ctx->quiet_shutdown;
  663. s->ext.max_fragment_len_mode = ctx->ext.max_fragment_len_mode;
  664. s->max_send_fragment = ctx->max_send_fragment;
  665. s->split_send_fragment = ctx->split_send_fragment;
  666. s->max_pipelines = ctx->max_pipelines;
  667. if (s->max_pipelines > 1)
  668. RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
  669. if (ctx->default_read_buf_len > 0)
  670. SSL_set_default_read_buffer_len(s, ctx->default_read_buf_len);
  671. SSL_CTX_up_ref(ctx);
  672. s->ctx = ctx;
  673. s->ext.debug_cb = 0;
  674. s->ext.debug_arg = NULL;
  675. s->ext.ticket_expected = 0;
  676. s->ext.status_type = ctx->ext.status_type;
  677. s->ext.status_expected = 0;
  678. s->ext.ocsp.ids = NULL;
  679. s->ext.ocsp.exts = NULL;
  680. s->ext.ocsp.resp = NULL;
  681. s->ext.ocsp.resp_len = 0;
  682. SSL_CTX_up_ref(ctx);
  683. s->session_ctx = ctx;
  684. #ifndef OPENSSL_NO_EC
  685. if (ctx->ext.ecpointformats) {
  686. s->ext.ecpointformats =
  687. OPENSSL_memdup(ctx->ext.ecpointformats,
  688. ctx->ext.ecpointformats_len);
  689. if (!s->ext.ecpointformats)
  690. goto err;
  691. s->ext.ecpointformats_len =
  692. ctx->ext.ecpointformats_len;
  693. }
  694. #endif
  695. if (ctx->ext.supportedgroups) {
  696. s->ext.supportedgroups =
  697. OPENSSL_memdup(ctx->ext.supportedgroups,
  698. ctx->ext.supportedgroups_len
  699. * sizeof(*ctx->ext.supportedgroups));
  700. if (!s->ext.supportedgroups)
  701. goto err;
  702. s->ext.supportedgroups_len = ctx->ext.supportedgroups_len;
  703. }
  704. #ifndef OPENSSL_NO_NEXTPROTONEG
  705. s->ext.npn = NULL;
  706. #endif
  707. if (s->ctx->ext.alpn) {
  708. s->ext.alpn = OPENSSL_malloc(s->ctx->ext.alpn_len);
  709. if (s->ext.alpn == NULL)
  710. goto err;
  711. memcpy(s->ext.alpn, s->ctx->ext.alpn, s->ctx->ext.alpn_len);
  712. s->ext.alpn_len = s->ctx->ext.alpn_len;
  713. }
  714. s->verified_chain = NULL;
  715. s->verify_result = X509_V_OK;
  716. s->default_passwd_callback = ctx->default_passwd_callback;
  717. s->default_passwd_callback_userdata = ctx->default_passwd_callback_userdata;
  718. s->method = ctx->method;
  719. s->key_update = SSL_KEY_UPDATE_NONE;
  720. s->allow_early_data_cb = ctx->allow_early_data_cb;
  721. s->allow_early_data_cb_data = ctx->allow_early_data_cb_data;
  722. if (!s->method->ssl_new(s))
  723. goto err;
  724. s->server = (ctx->method->ssl_accept == ssl_undefined_function) ? 0 : 1;
  725. if (!SSL_clear(s))
  726. goto err;
  727. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data))
  728. goto err;
  729. #ifndef OPENSSL_NO_PSK
  730. s->psk_client_callback = ctx->psk_client_callback;
  731. s->psk_server_callback = ctx->psk_server_callback;
  732. #endif
  733. s->psk_find_session_cb = ctx->psk_find_session_cb;
  734. s->psk_use_session_cb = ctx->psk_use_session_cb;
  735. s->async_cb = ctx->async_cb;
  736. s->async_cb_arg = ctx->async_cb_arg;
  737. s->job = NULL;
  738. #ifndef OPENSSL_NO_CT
  739. if (!SSL_set_ct_validation_callback(s, ctx->ct_validation_callback,
  740. ctx->ct_validation_callback_arg))
  741. goto err;
  742. #endif
  743. return s;
  744. err:
  745. SSL_free(s);
  746. SSLerr(SSL_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
  747. return NULL;
  748. }
  749. int SSL_is_dtls(const SSL *s)
  750. {
  751. return SSL_IS_DTLS(s) ? 1 : 0;
  752. }
  753. int SSL_up_ref(SSL *s)
  754. {
  755. int i;
  756. if (CRYPTO_UP_REF(&s->references, &i, s->lock) <= 0)
  757. return 0;
  758. REF_PRINT_COUNT("SSL", s);
  759. REF_ASSERT_ISNT(i < 2);
  760. return ((i > 1) ? 1 : 0);
  761. }
  762. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const unsigned char *sid_ctx,
  763. unsigned int sid_ctx_len)
  764. {
  765. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  766. SSLerr(SSL_F_SSL_CTX_SET_SESSION_ID_CONTEXT,
  767. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  768. return 0;
  769. }
  770. ctx->sid_ctx_length = sid_ctx_len;
  771. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  772. return 1;
  773. }
  774. int SSL_set_session_id_context(SSL *ssl, const unsigned char *sid_ctx,
  775. unsigned int sid_ctx_len)
  776. {
  777. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  778. SSLerr(SSL_F_SSL_SET_SESSION_ID_CONTEXT,
  779. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  780. return 0;
  781. }
  782. ssl->sid_ctx_length = sid_ctx_len;
  783. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  784. return 1;
  785. }
  786. int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb)
  787. {
  788. CRYPTO_THREAD_write_lock(ctx->lock);
  789. ctx->generate_session_id = cb;
  790. CRYPTO_THREAD_unlock(ctx->lock);
  791. return 1;
  792. }
  793. int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb)
  794. {
  795. CRYPTO_THREAD_write_lock(ssl->lock);
  796. ssl->generate_session_id = cb;
  797. CRYPTO_THREAD_unlock(ssl->lock);
  798. return 1;
  799. }
  800. int SSL_has_matching_session_id(const SSL *ssl, const unsigned char *id,
  801. unsigned int id_len)
  802. {
  803. /*
  804. * A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how
  805. * we can "construct" a session to give us the desired check - i.e. to
  806. * find if there's a session in the hash table that would conflict with
  807. * any new session built out of this id/id_len and the ssl_version in use
  808. * by this SSL.
  809. */
  810. SSL_SESSION r, *p;
  811. if (id_len > sizeof(r.session_id))
  812. return 0;
  813. r.ssl_version = ssl->version;
  814. r.session_id_length = id_len;
  815. memcpy(r.session_id, id, id_len);
  816. CRYPTO_THREAD_read_lock(ssl->session_ctx->lock);
  817. p = lh_SSL_SESSION_retrieve(ssl->session_ctx->sessions, &r);
  818. CRYPTO_THREAD_unlock(ssl->session_ctx->lock);
  819. return (p != NULL);
  820. }
  821. int SSL_CTX_set_purpose(SSL_CTX *s, int purpose)
  822. {
  823. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  824. }
  825. int SSL_set_purpose(SSL *s, int purpose)
  826. {
  827. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  828. }
  829. int SSL_CTX_set_trust(SSL_CTX *s, int trust)
  830. {
  831. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  832. }
  833. int SSL_set_trust(SSL *s, int trust)
  834. {
  835. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  836. }
  837. int SSL_set1_host(SSL *s, const char *hostname)
  838. {
  839. return X509_VERIFY_PARAM_set1_host(s->param, hostname, 0);
  840. }
  841. int SSL_add1_host(SSL *s, const char *hostname)
  842. {
  843. return X509_VERIFY_PARAM_add1_host(s->param, hostname, 0);
  844. }
  845. void SSL_set_hostflags(SSL *s, unsigned int flags)
  846. {
  847. X509_VERIFY_PARAM_set_hostflags(s->param, flags);
  848. }
  849. const char *SSL_get0_peername(SSL *s)
  850. {
  851. return X509_VERIFY_PARAM_get0_peername(s->param);
  852. }
  853. int SSL_CTX_dane_enable(SSL_CTX *ctx)
  854. {
  855. return dane_ctx_enable(&ctx->dane);
  856. }
  857. unsigned long SSL_CTX_dane_set_flags(SSL_CTX *ctx, unsigned long flags)
  858. {
  859. unsigned long orig = ctx->dane.flags;
  860. ctx->dane.flags |= flags;
  861. return orig;
  862. }
  863. unsigned long SSL_CTX_dane_clear_flags(SSL_CTX *ctx, unsigned long flags)
  864. {
  865. unsigned long orig = ctx->dane.flags;
  866. ctx->dane.flags &= ~flags;
  867. return orig;
  868. }
  869. int SSL_dane_enable(SSL *s, const char *basedomain)
  870. {
  871. SSL_DANE *dane = &s->dane;
  872. if (s->ctx->dane.mdmax == 0) {
  873. SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_CONTEXT_NOT_DANE_ENABLED);
  874. return 0;
  875. }
  876. if (dane->trecs != NULL) {
  877. SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_DANE_ALREADY_ENABLED);
  878. return 0;
  879. }
  880. /*
  881. * Default SNI name. This rejects empty names, while set1_host below
  882. * accepts them and disables host name checks. To avoid side-effects with
  883. * invalid input, set the SNI name first.
  884. */
  885. if (s->ext.hostname == NULL) {
  886. if (!SSL_set_tlsext_host_name(s, basedomain)) {
  887. SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
  888. return -1;
  889. }
  890. }
  891. /* Primary RFC6125 reference identifier */
  892. if (!X509_VERIFY_PARAM_set1_host(s->param, basedomain, 0)) {
  893. SSLerr(SSL_F_SSL_DANE_ENABLE, SSL_R_ERROR_SETTING_TLSA_BASE_DOMAIN);
  894. return -1;
  895. }
  896. dane->mdpth = -1;
  897. dane->pdpth = -1;
  898. dane->dctx = &s->ctx->dane;
  899. dane->trecs = sk_danetls_record_new_null();
  900. if (dane->trecs == NULL) {
  901. SSLerr(SSL_F_SSL_DANE_ENABLE, ERR_R_MALLOC_FAILURE);
  902. return -1;
  903. }
  904. return 1;
  905. }
  906. unsigned long SSL_dane_set_flags(SSL *ssl, unsigned long flags)
  907. {
  908. unsigned long orig = ssl->dane.flags;
  909. ssl->dane.flags |= flags;
  910. return orig;
  911. }
  912. unsigned long SSL_dane_clear_flags(SSL *ssl, unsigned long flags)
  913. {
  914. unsigned long orig = ssl->dane.flags;
  915. ssl->dane.flags &= ~flags;
  916. return orig;
  917. }
  918. int SSL_get0_dane_authority(SSL *s, X509 **mcert, EVP_PKEY **mspki)
  919. {
  920. SSL_DANE *dane = &s->dane;
  921. if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
  922. return -1;
  923. if (dane->mtlsa) {
  924. if (mcert)
  925. *mcert = dane->mcert;
  926. if (mspki)
  927. *mspki = (dane->mcert == NULL) ? dane->mtlsa->spki : NULL;
  928. }
  929. return dane->mdpth;
  930. }
  931. int SSL_get0_dane_tlsa(SSL *s, uint8_t *usage, uint8_t *selector,
  932. uint8_t *mtype, unsigned const char **data, size_t *dlen)
  933. {
  934. SSL_DANE *dane = &s->dane;
  935. if (!DANETLS_ENABLED(dane) || s->verify_result != X509_V_OK)
  936. return -1;
  937. if (dane->mtlsa) {
  938. if (usage)
  939. *usage = dane->mtlsa->usage;
  940. if (selector)
  941. *selector = dane->mtlsa->selector;
  942. if (mtype)
  943. *mtype = dane->mtlsa->mtype;
  944. if (data)
  945. *data = dane->mtlsa->data;
  946. if (dlen)
  947. *dlen = dane->mtlsa->dlen;
  948. }
  949. return dane->mdpth;
  950. }
  951. SSL_DANE *SSL_get0_dane(SSL *s)
  952. {
  953. return &s->dane;
  954. }
  955. int SSL_dane_tlsa_add(SSL *s, uint8_t usage, uint8_t selector,
  956. uint8_t mtype, unsigned const char *data, size_t dlen)
  957. {
  958. return dane_tlsa_add(&s->dane, usage, selector, mtype, data, dlen);
  959. }
  960. int SSL_CTX_dane_mtype_set(SSL_CTX *ctx, const EVP_MD *md, uint8_t mtype,
  961. uint8_t ord)
  962. {
  963. return dane_mtype_set(&ctx->dane, md, mtype, ord);
  964. }
  965. int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm)
  966. {
  967. return X509_VERIFY_PARAM_set1(ctx->param, vpm);
  968. }
  969. int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm)
  970. {
  971. return X509_VERIFY_PARAM_set1(ssl->param, vpm);
  972. }
  973. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx)
  974. {
  975. return ctx->param;
  976. }
  977. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl)
  978. {
  979. return ssl->param;
  980. }
  981. void SSL_certs_clear(SSL *s)
  982. {
  983. ssl_cert_clear_certs(s->cert);
  984. }
  985. void SSL_free(SSL *s)
  986. {
  987. int i;
  988. if (s == NULL)
  989. return;
  990. CRYPTO_DOWN_REF(&s->references, &i, s->lock);
  991. REF_PRINT_COUNT("SSL", s);
  992. if (i > 0)
  993. return;
  994. REF_ASSERT_ISNT(i < 0);
  995. X509_VERIFY_PARAM_free(s->param);
  996. dane_final(&s->dane);
  997. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL, s, &s->ex_data);
  998. RECORD_LAYER_release(&s->rlayer);
  999. /* Ignore return value */
  1000. ssl_free_wbio_buffer(s);
  1001. BIO_free_all(s->wbio);
  1002. s->wbio = NULL;
  1003. BIO_free_all(s->rbio);
  1004. s->rbio = NULL;
  1005. BUF_MEM_free(s->init_buf);
  1006. /* add extra stuff */
  1007. sk_SSL_CIPHER_free(s->cipher_list);
  1008. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  1009. sk_SSL_CIPHER_free(s->tls13_ciphersuites);
  1010. sk_SSL_CIPHER_free(s->peer_ciphers);
  1011. /* Make the next call work :-) */
  1012. if (s->session != NULL) {
  1013. ssl_clear_bad_session(s);
  1014. SSL_SESSION_free(s->session);
  1015. }
  1016. SSL_SESSION_free(s->psksession);
  1017. OPENSSL_free(s->psksession_id);
  1018. clear_ciphers(s);
  1019. ssl_cert_free(s->cert);
  1020. OPENSSL_free(s->shared_sigalgs);
  1021. /* Free up if allocated */
  1022. OPENSSL_free(s->ext.hostname);
  1023. SSL_CTX_free(s->session_ctx);
  1024. #ifndef OPENSSL_NO_EC
  1025. OPENSSL_free(s->ext.ecpointformats);
  1026. OPENSSL_free(s->ext.peer_ecpointformats);
  1027. #endif /* OPENSSL_NO_EC */
  1028. OPENSSL_free(s->ext.supportedgroups);
  1029. OPENSSL_free(s->ext.peer_supportedgroups);
  1030. sk_X509_EXTENSION_pop_free(s->ext.ocsp.exts, X509_EXTENSION_free);
  1031. #ifndef OPENSSL_NO_OCSP
  1032. sk_OCSP_RESPID_pop_free(s->ext.ocsp.ids, OCSP_RESPID_free);
  1033. #endif
  1034. #ifndef OPENSSL_NO_CT
  1035. SCT_LIST_free(s->scts);
  1036. OPENSSL_free(s->ext.scts);
  1037. #endif
  1038. OPENSSL_free(s->ext.ocsp.resp);
  1039. OPENSSL_free(s->ext.alpn);
  1040. OPENSSL_free(s->ext.tls13_cookie);
  1041. OPENSSL_free(s->clienthello);
  1042. OPENSSL_free(s->pha_context);
  1043. EVP_MD_CTX_free(s->pha_dgst);
  1044. sk_X509_NAME_pop_free(s->ca_names, X509_NAME_free);
  1045. sk_X509_NAME_pop_free(s->client_ca_names, X509_NAME_free);
  1046. sk_X509_pop_free(s->verified_chain, X509_free);
  1047. if (s->method != NULL)
  1048. s->method->ssl_free(s);
  1049. SSL_CTX_free(s->ctx);
  1050. ASYNC_WAIT_CTX_free(s->waitctx);
  1051. #if !defined(OPENSSL_NO_NEXTPROTONEG)
  1052. OPENSSL_free(s->ext.npn);
  1053. #endif
  1054. #ifndef OPENSSL_NO_SRTP
  1055. sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
  1056. #endif
  1057. CRYPTO_THREAD_lock_free(s->lock);
  1058. OPENSSL_free(s);
  1059. }
  1060. void SSL_set0_rbio(SSL *s, BIO *rbio)
  1061. {
  1062. BIO_free_all(s->rbio);
  1063. s->rbio = rbio;
  1064. }
  1065. void SSL_set0_wbio(SSL *s, BIO *wbio)
  1066. {
  1067. /*
  1068. * If the output buffering BIO is still in place, remove it
  1069. */
  1070. if (s->bbio != NULL)
  1071. s->wbio = BIO_pop(s->wbio);
  1072. BIO_free_all(s->wbio);
  1073. s->wbio = wbio;
  1074. /* Re-attach |bbio| to the new |wbio|. */
  1075. if (s->bbio != NULL)
  1076. s->wbio = BIO_push(s->bbio, s->wbio);
  1077. }
  1078. void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio)
  1079. {
  1080. /*
  1081. * For historical reasons, this function has many different cases in
  1082. * ownership handling.
  1083. */
  1084. /* If nothing has changed, do nothing */
  1085. if (rbio == SSL_get_rbio(s) && wbio == SSL_get_wbio(s))
  1086. return;
  1087. /*
  1088. * If the two arguments are equal then one fewer reference is granted by the
  1089. * caller than we want to take
  1090. */
  1091. if (rbio != NULL && rbio == wbio)
  1092. BIO_up_ref(rbio);
  1093. /*
  1094. * If only the wbio is changed only adopt one reference.
  1095. */
  1096. if (rbio == SSL_get_rbio(s)) {
  1097. SSL_set0_wbio(s, wbio);
  1098. return;
  1099. }
  1100. /*
  1101. * There is an asymmetry here for historical reasons. If only the rbio is
  1102. * changed AND the rbio and wbio were originally different, then we only
  1103. * adopt one reference.
  1104. */
  1105. if (wbio == SSL_get_wbio(s) && SSL_get_rbio(s) != SSL_get_wbio(s)) {
  1106. SSL_set0_rbio(s, rbio);
  1107. return;
  1108. }
  1109. /* Otherwise, adopt both references. */
  1110. SSL_set0_rbio(s, rbio);
  1111. SSL_set0_wbio(s, wbio);
  1112. }
  1113. BIO *SSL_get_rbio(const SSL *s)
  1114. {
  1115. return s->rbio;
  1116. }
  1117. BIO *SSL_get_wbio(const SSL *s)
  1118. {
  1119. if (s->bbio != NULL) {
  1120. /*
  1121. * If |bbio| is active, the true caller-configured BIO is its
  1122. * |next_bio|.
  1123. */
  1124. return BIO_next(s->bbio);
  1125. }
  1126. return s->wbio;
  1127. }
  1128. int SSL_get_fd(const SSL *s)
  1129. {
  1130. return SSL_get_rfd(s);
  1131. }
  1132. int SSL_get_rfd(const SSL *s)
  1133. {
  1134. int ret = -1;
  1135. BIO *b, *r;
  1136. b = SSL_get_rbio(s);
  1137. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  1138. if (r != NULL)
  1139. BIO_get_fd(r, &ret);
  1140. return ret;
  1141. }
  1142. int SSL_get_wfd(const SSL *s)
  1143. {
  1144. int ret = -1;
  1145. BIO *b, *r;
  1146. b = SSL_get_wbio(s);
  1147. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  1148. if (r != NULL)
  1149. BIO_get_fd(r, &ret);
  1150. return ret;
  1151. }
  1152. #ifndef OPENSSL_NO_SOCK
  1153. int SSL_set_fd(SSL *s, int fd)
  1154. {
  1155. int ret = 0;
  1156. BIO *bio = NULL;
  1157. bio = BIO_new(BIO_s_socket());
  1158. if (bio == NULL) {
  1159. SSLerr(SSL_F_SSL_SET_FD, ERR_R_BUF_LIB);
  1160. goto err;
  1161. }
  1162. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1163. SSL_set_bio(s, bio, bio);
  1164. #ifndef OPENSSL_NO_KTLS
  1165. /*
  1166. * The new socket is created successfully regardless of ktls_enable.
  1167. * ktls_enable doesn't change any functionality of the socket, except
  1168. * changing the setsockopt to enable the processing of ktls_start.
  1169. * Thus, it is not a problem to call it for non-TLS sockets.
  1170. */
  1171. ktls_enable(fd);
  1172. #endif /* OPENSSL_NO_KTLS */
  1173. ret = 1;
  1174. err:
  1175. return ret;
  1176. }
  1177. int SSL_set_wfd(SSL *s, int fd)
  1178. {
  1179. BIO *rbio = SSL_get_rbio(s);
  1180. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET
  1181. || (int)BIO_get_fd(rbio, NULL) != fd) {
  1182. BIO *bio = BIO_new(BIO_s_socket());
  1183. if (bio == NULL) {
  1184. SSLerr(SSL_F_SSL_SET_WFD, ERR_R_BUF_LIB);
  1185. return 0;
  1186. }
  1187. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1188. SSL_set0_wbio(s, bio);
  1189. #ifndef OPENSSL_NO_KTLS
  1190. /*
  1191. * The new socket is created successfully regardless of ktls_enable.
  1192. * ktls_enable doesn't change any functionality of the socket, except
  1193. * changing the setsockopt to enable the processing of ktls_start.
  1194. * Thus, it is not a problem to call it for non-TLS sockets.
  1195. */
  1196. ktls_enable(fd);
  1197. #endif /* OPENSSL_NO_KTLS */
  1198. } else {
  1199. BIO_up_ref(rbio);
  1200. SSL_set0_wbio(s, rbio);
  1201. }
  1202. return 1;
  1203. }
  1204. int SSL_set_rfd(SSL *s, int fd)
  1205. {
  1206. BIO *wbio = SSL_get_wbio(s);
  1207. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET
  1208. || ((int)BIO_get_fd(wbio, NULL) != fd)) {
  1209. BIO *bio = BIO_new(BIO_s_socket());
  1210. if (bio == NULL) {
  1211. SSLerr(SSL_F_SSL_SET_RFD, ERR_R_BUF_LIB);
  1212. return 0;
  1213. }
  1214. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1215. SSL_set0_rbio(s, bio);
  1216. } else {
  1217. BIO_up_ref(wbio);
  1218. SSL_set0_rbio(s, wbio);
  1219. }
  1220. return 1;
  1221. }
  1222. #endif
  1223. /* return length of latest Finished message we sent, copy to 'buf' */
  1224. size_t SSL_get_finished(const SSL *s, void *buf, size_t count)
  1225. {
  1226. size_t ret = 0;
  1227. ret = s->s3.tmp.finish_md_len;
  1228. if (count > ret)
  1229. count = ret;
  1230. memcpy(buf, s->s3.tmp.finish_md, count);
  1231. return ret;
  1232. }
  1233. /* return length of latest Finished message we expected, copy to 'buf' */
  1234. size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count)
  1235. {
  1236. size_t ret = 0;
  1237. ret = s->s3.tmp.peer_finish_md_len;
  1238. if (count > ret)
  1239. count = ret;
  1240. memcpy(buf, s->s3.tmp.peer_finish_md, count);
  1241. return ret;
  1242. }
  1243. int SSL_get_verify_mode(const SSL *s)
  1244. {
  1245. return s->verify_mode;
  1246. }
  1247. int SSL_get_verify_depth(const SSL *s)
  1248. {
  1249. return X509_VERIFY_PARAM_get_depth(s->param);
  1250. }
  1251. int (*SSL_get_verify_callback(const SSL *s)) (int, X509_STORE_CTX *) {
  1252. return s->verify_callback;
  1253. }
  1254. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx)
  1255. {
  1256. return ctx->verify_mode;
  1257. }
  1258. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx)
  1259. {
  1260. return X509_VERIFY_PARAM_get_depth(ctx->param);
  1261. }
  1262. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx)) (int, X509_STORE_CTX *) {
  1263. return ctx->default_verify_callback;
  1264. }
  1265. void SSL_set_verify(SSL *s, int mode,
  1266. int (*callback) (int ok, X509_STORE_CTX *ctx))
  1267. {
  1268. s->verify_mode = mode;
  1269. if (callback != NULL)
  1270. s->verify_callback = callback;
  1271. }
  1272. void SSL_set_verify_depth(SSL *s, int depth)
  1273. {
  1274. X509_VERIFY_PARAM_set_depth(s->param, depth);
  1275. }
  1276. void SSL_set_read_ahead(SSL *s, int yes)
  1277. {
  1278. RECORD_LAYER_set_read_ahead(&s->rlayer, yes);
  1279. }
  1280. int SSL_get_read_ahead(const SSL *s)
  1281. {
  1282. return RECORD_LAYER_get_read_ahead(&s->rlayer);
  1283. }
  1284. int SSL_pending(const SSL *s)
  1285. {
  1286. size_t pending = s->method->ssl_pending(s);
  1287. /*
  1288. * SSL_pending cannot work properly if read-ahead is enabled
  1289. * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
  1290. * impossible to fix since SSL_pending cannot report errors that may be
  1291. * observed while scanning the new data. (Note that SSL_pending() is
  1292. * often used as a boolean value, so we'd better not return -1.)
  1293. *
  1294. * SSL_pending also cannot work properly if the value >INT_MAX. In that case
  1295. * we just return INT_MAX.
  1296. */
  1297. return pending < INT_MAX ? (int)pending : INT_MAX;
  1298. }
  1299. int SSL_has_pending(const SSL *s)
  1300. {
  1301. /*
  1302. * Similar to SSL_pending() but returns a 1 to indicate that we have
  1303. * unprocessed data available or 0 otherwise (as opposed to the number of
  1304. * bytes available). Unlike SSL_pending() this will take into account
  1305. * read_ahead data. A 1 return simply indicates that we have unprocessed
  1306. * data. That data may not result in any application data, or we may fail
  1307. * to parse the records for some reason.
  1308. */
  1309. if (RECORD_LAYER_processed_read_pending(&s->rlayer))
  1310. return 1;
  1311. return RECORD_LAYER_read_pending(&s->rlayer);
  1312. }
  1313. X509 *SSL_get_peer_certificate(const SSL *s)
  1314. {
  1315. X509 *r;
  1316. if ((s == NULL) || (s->session == NULL))
  1317. r = NULL;
  1318. else
  1319. r = s->session->peer;
  1320. if (r == NULL)
  1321. return r;
  1322. X509_up_ref(r);
  1323. return r;
  1324. }
  1325. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *s)
  1326. {
  1327. STACK_OF(X509) *r;
  1328. if ((s == NULL) || (s->session == NULL))
  1329. r = NULL;
  1330. else
  1331. r = s->session->peer_chain;
  1332. /*
  1333. * If we are a client, cert_chain includes the peer's own certificate; if
  1334. * we are a server, it does not.
  1335. */
  1336. return r;
  1337. }
  1338. /*
  1339. * Now in theory, since the calling process own 't' it should be safe to
  1340. * modify. We need to be able to read f without being hassled
  1341. */
  1342. int SSL_copy_session_id(SSL *t, const SSL *f)
  1343. {
  1344. int i;
  1345. /* Do we need to to SSL locking? */
  1346. if (!SSL_set_session(t, SSL_get_session(f))) {
  1347. return 0;
  1348. }
  1349. /*
  1350. * what if we are setup for one protocol version but want to talk another
  1351. */
  1352. if (t->method != f->method) {
  1353. t->method->ssl_free(t);
  1354. t->method = f->method;
  1355. if (t->method->ssl_new(t) == 0)
  1356. return 0;
  1357. }
  1358. CRYPTO_UP_REF(&f->cert->references, &i, f->cert->lock);
  1359. ssl_cert_free(t->cert);
  1360. t->cert = f->cert;
  1361. if (!SSL_set_session_id_context(t, f->sid_ctx, (int)f->sid_ctx_length)) {
  1362. return 0;
  1363. }
  1364. return 1;
  1365. }
  1366. /* Fix this so it checks all the valid key/cert options */
  1367. int SSL_CTX_check_private_key(const SSL_CTX *ctx)
  1368. {
  1369. if ((ctx == NULL) || (ctx->cert->key->x509 == NULL)) {
  1370. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1371. return 0;
  1372. }
  1373. if (ctx->cert->key->privatekey == NULL) {
  1374. SSLerr(SSL_F_SSL_CTX_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1375. return 0;
  1376. }
  1377. return X509_check_private_key
  1378. (ctx->cert->key->x509, ctx->cert->key->privatekey);
  1379. }
  1380. /* Fix this function so that it takes an optional type parameter */
  1381. int SSL_check_private_key(const SSL *ssl)
  1382. {
  1383. if (ssl == NULL) {
  1384. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
  1385. return 0;
  1386. }
  1387. if (ssl->cert->key->x509 == NULL) {
  1388. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_CERTIFICATE_ASSIGNED);
  1389. return 0;
  1390. }
  1391. if (ssl->cert->key->privatekey == NULL) {
  1392. SSLerr(SSL_F_SSL_CHECK_PRIVATE_KEY, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  1393. return 0;
  1394. }
  1395. return X509_check_private_key(ssl->cert->key->x509,
  1396. ssl->cert->key->privatekey);
  1397. }
  1398. int SSL_waiting_for_async(SSL *s)
  1399. {
  1400. if (s->job)
  1401. return 1;
  1402. return 0;
  1403. }
  1404. int SSL_get_all_async_fds(SSL *s, OSSL_ASYNC_FD *fds, size_t *numfds)
  1405. {
  1406. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1407. if (ctx == NULL)
  1408. return 0;
  1409. return ASYNC_WAIT_CTX_get_all_fds(ctx, fds, numfds);
  1410. }
  1411. int SSL_get_changed_async_fds(SSL *s, OSSL_ASYNC_FD *addfd, size_t *numaddfds,
  1412. OSSL_ASYNC_FD *delfd, size_t *numdelfds)
  1413. {
  1414. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1415. if (ctx == NULL)
  1416. return 0;
  1417. return ASYNC_WAIT_CTX_get_changed_fds(ctx, addfd, numaddfds, delfd,
  1418. numdelfds);
  1419. }
  1420. int SSL_CTX_set_async_callback(SSL_CTX *ctx, SSL_async_callback_fn callback)
  1421. {
  1422. ctx->async_cb = callback;
  1423. return 1;
  1424. }
  1425. int SSL_CTX_set_async_callback_arg(SSL_CTX *ctx, void *arg)
  1426. {
  1427. ctx->async_cb_arg = arg;
  1428. return 1;
  1429. }
  1430. int SSL_set_async_callback(SSL *s, SSL_async_callback_fn callback)
  1431. {
  1432. s->async_cb = callback;
  1433. return 1;
  1434. }
  1435. int SSL_set_async_callback_arg(SSL *s, void *arg)
  1436. {
  1437. s->async_cb_arg = arg;
  1438. return 1;
  1439. }
  1440. int SSL_get_async_status(SSL *s, int *status)
  1441. {
  1442. ASYNC_WAIT_CTX *ctx = s->waitctx;
  1443. if (ctx == NULL)
  1444. return 0;
  1445. *status = ASYNC_WAIT_CTX_get_status(ctx);
  1446. return 1;
  1447. }
  1448. int SSL_accept(SSL *s)
  1449. {
  1450. if (s->handshake_func == NULL) {
  1451. /* Not properly initialized yet */
  1452. SSL_set_accept_state(s);
  1453. }
  1454. return SSL_do_handshake(s);
  1455. }
  1456. int SSL_connect(SSL *s)
  1457. {
  1458. if (s->handshake_func == NULL) {
  1459. /* Not properly initialized yet */
  1460. SSL_set_connect_state(s);
  1461. }
  1462. return SSL_do_handshake(s);
  1463. }
  1464. long SSL_get_default_timeout(const SSL *s)
  1465. {
  1466. return s->method->get_timeout();
  1467. }
  1468. static int ssl_async_wait_ctx_cb(void *arg)
  1469. {
  1470. SSL *s = (SSL *)arg;
  1471. return s->async_cb(s, s->async_cb_arg);
  1472. }
  1473. static int ssl_start_async_job(SSL *s, struct ssl_async_args *args,
  1474. int (*func) (void *))
  1475. {
  1476. int ret;
  1477. if (s->waitctx == NULL) {
  1478. s->waitctx = ASYNC_WAIT_CTX_new();
  1479. if (s->waitctx == NULL)
  1480. return -1;
  1481. if (s->async_cb != NULL
  1482. && !ASYNC_WAIT_CTX_set_callback
  1483. (s->waitctx, ssl_async_wait_ctx_cb, s))
  1484. return -1;
  1485. }
  1486. switch (ASYNC_start_job(&s->job, s->waitctx, &ret, func, args,
  1487. sizeof(struct ssl_async_args))) {
  1488. case ASYNC_ERR:
  1489. s->rwstate = SSL_NOTHING;
  1490. SSLerr(SSL_F_SSL_START_ASYNC_JOB, SSL_R_FAILED_TO_INIT_ASYNC);
  1491. return -1;
  1492. case ASYNC_PAUSE:
  1493. s->rwstate = SSL_ASYNC_PAUSED;
  1494. return -1;
  1495. case ASYNC_NO_JOBS:
  1496. s->rwstate = SSL_ASYNC_NO_JOBS;
  1497. return -1;
  1498. case ASYNC_FINISH:
  1499. s->job = NULL;
  1500. return ret;
  1501. default:
  1502. s->rwstate = SSL_NOTHING;
  1503. SSLerr(SSL_F_SSL_START_ASYNC_JOB, ERR_R_INTERNAL_ERROR);
  1504. /* Shouldn't happen */
  1505. return -1;
  1506. }
  1507. }
  1508. static int ssl_io_intern(void *vargs)
  1509. {
  1510. struct ssl_async_args *args;
  1511. SSL *s;
  1512. void *buf;
  1513. size_t num;
  1514. args = (struct ssl_async_args *)vargs;
  1515. s = args->s;
  1516. buf = args->buf;
  1517. num = args->num;
  1518. switch (args->type) {
  1519. case READFUNC:
  1520. return args->f.func_read(s, buf, num, &s->asyncrw);
  1521. case WRITEFUNC:
  1522. return args->f.func_write(s, buf, num, &s->asyncrw);
  1523. case OTHERFUNC:
  1524. return args->f.func_other(s);
  1525. }
  1526. return -1;
  1527. }
  1528. int ssl_read_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
  1529. {
  1530. if (s->handshake_func == NULL) {
  1531. SSLerr(SSL_F_SSL_READ_INTERNAL, SSL_R_UNINITIALIZED);
  1532. return -1;
  1533. }
  1534. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1535. s->rwstate = SSL_NOTHING;
  1536. return 0;
  1537. }
  1538. if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
  1539. || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY) {
  1540. SSLerr(SSL_F_SSL_READ_INTERNAL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1541. return 0;
  1542. }
  1543. /*
  1544. * If we are a client and haven't received the ServerHello etc then we
  1545. * better do that
  1546. */
  1547. ossl_statem_check_finish_init(s, 0);
  1548. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1549. struct ssl_async_args args;
  1550. int ret;
  1551. args.s = s;
  1552. args.buf = buf;
  1553. args.num = num;
  1554. args.type = READFUNC;
  1555. args.f.func_read = s->method->ssl_read;
  1556. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1557. *readbytes = s->asyncrw;
  1558. return ret;
  1559. } else {
  1560. return s->method->ssl_read(s, buf, num, readbytes);
  1561. }
  1562. }
  1563. int SSL_read(SSL *s, void *buf, int num)
  1564. {
  1565. int ret;
  1566. size_t readbytes;
  1567. if (num < 0) {
  1568. SSLerr(SSL_F_SSL_READ, SSL_R_BAD_LENGTH);
  1569. return -1;
  1570. }
  1571. ret = ssl_read_internal(s, buf, (size_t)num, &readbytes);
  1572. /*
  1573. * The cast is safe here because ret should be <= INT_MAX because num is
  1574. * <= INT_MAX
  1575. */
  1576. if (ret > 0)
  1577. ret = (int)readbytes;
  1578. return ret;
  1579. }
  1580. int SSL_read_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
  1581. {
  1582. int ret = ssl_read_internal(s, buf, num, readbytes);
  1583. if (ret < 0)
  1584. ret = 0;
  1585. return ret;
  1586. }
  1587. int SSL_read_early_data(SSL *s, void *buf, size_t num, size_t *readbytes)
  1588. {
  1589. int ret;
  1590. if (!s->server) {
  1591. SSLerr(SSL_F_SSL_READ_EARLY_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1592. return SSL_READ_EARLY_DATA_ERROR;
  1593. }
  1594. switch (s->early_data_state) {
  1595. case SSL_EARLY_DATA_NONE:
  1596. if (!SSL_in_before(s)) {
  1597. SSLerr(SSL_F_SSL_READ_EARLY_DATA,
  1598. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1599. return SSL_READ_EARLY_DATA_ERROR;
  1600. }
  1601. /* fall through */
  1602. case SSL_EARLY_DATA_ACCEPT_RETRY:
  1603. s->early_data_state = SSL_EARLY_DATA_ACCEPTING;
  1604. ret = SSL_accept(s);
  1605. if (ret <= 0) {
  1606. /* NBIO or error */
  1607. s->early_data_state = SSL_EARLY_DATA_ACCEPT_RETRY;
  1608. return SSL_READ_EARLY_DATA_ERROR;
  1609. }
  1610. /* fall through */
  1611. case SSL_EARLY_DATA_READ_RETRY:
  1612. if (s->ext.early_data == SSL_EARLY_DATA_ACCEPTED) {
  1613. s->early_data_state = SSL_EARLY_DATA_READING;
  1614. ret = SSL_read_ex(s, buf, num, readbytes);
  1615. /*
  1616. * State machine will update early_data_state to
  1617. * SSL_EARLY_DATA_FINISHED_READING if we get an EndOfEarlyData
  1618. * message
  1619. */
  1620. if (ret > 0 || (ret <= 0 && s->early_data_state
  1621. != SSL_EARLY_DATA_FINISHED_READING)) {
  1622. s->early_data_state = SSL_EARLY_DATA_READ_RETRY;
  1623. return ret > 0 ? SSL_READ_EARLY_DATA_SUCCESS
  1624. : SSL_READ_EARLY_DATA_ERROR;
  1625. }
  1626. } else {
  1627. s->early_data_state = SSL_EARLY_DATA_FINISHED_READING;
  1628. }
  1629. *readbytes = 0;
  1630. return SSL_READ_EARLY_DATA_FINISH;
  1631. default:
  1632. SSLerr(SSL_F_SSL_READ_EARLY_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1633. return SSL_READ_EARLY_DATA_ERROR;
  1634. }
  1635. }
  1636. int SSL_get_early_data_status(const SSL *s)
  1637. {
  1638. return s->ext.early_data;
  1639. }
  1640. static int ssl_peek_internal(SSL *s, void *buf, size_t num, size_t *readbytes)
  1641. {
  1642. if (s->handshake_func == NULL) {
  1643. SSLerr(SSL_F_SSL_PEEK_INTERNAL, SSL_R_UNINITIALIZED);
  1644. return -1;
  1645. }
  1646. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  1647. return 0;
  1648. }
  1649. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1650. struct ssl_async_args args;
  1651. int ret;
  1652. args.s = s;
  1653. args.buf = buf;
  1654. args.num = num;
  1655. args.type = READFUNC;
  1656. args.f.func_read = s->method->ssl_peek;
  1657. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1658. *readbytes = s->asyncrw;
  1659. return ret;
  1660. } else {
  1661. return s->method->ssl_peek(s, buf, num, readbytes);
  1662. }
  1663. }
  1664. int SSL_peek(SSL *s, void *buf, int num)
  1665. {
  1666. int ret;
  1667. size_t readbytes;
  1668. if (num < 0) {
  1669. SSLerr(SSL_F_SSL_PEEK, SSL_R_BAD_LENGTH);
  1670. return -1;
  1671. }
  1672. ret = ssl_peek_internal(s, buf, (size_t)num, &readbytes);
  1673. /*
  1674. * The cast is safe here because ret should be <= INT_MAX because num is
  1675. * <= INT_MAX
  1676. */
  1677. if (ret > 0)
  1678. ret = (int)readbytes;
  1679. return ret;
  1680. }
  1681. int SSL_peek_ex(SSL *s, void *buf, size_t num, size_t *readbytes)
  1682. {
  1683. int ret = ssl_peek_internal(s, buf, num, readbytes);
  1684. if (ret < 0)
  1685. ret = 0;
  1686. return ret;
  1687. }
  1688. int ssl_write_internal(SSL *s, const void *buf, size_t num, size_t *written)
  1689. {
  1690. if (s->handshake_func == NULL) {
  1691. SSLerr(SSL_F_SSL_WRITE_INTERNAL, SSL_R_UNINITIALIZED);
  1692. return -1;
  1693. }
  1694. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  1695. s->rwstate = SSL_NOTHING;
  1696. SSLerr(SSL_F_SSL_WRITE_INTERNAL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1697. return -1;
  1698. }
  1699. if (s->early_data_state == SSL_EARLY_DATA_CONNECT_RETRY
  1700. || s->early_data_state == SSL_EARLY_DATA_ACCEPT_RETRY
  1701. || s->early_data_state == SSL_EARLY_DATA_READ_RETRY) {
  1702. SSLerr(SSL_F_SSL_WRITE_INTERNAL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1703. return 0;
  1704. }
  1705. /* If we are a client and haven't sent the Finished we better do that */
  1706. ossl_statem_check_finish_init(s, 1);
  1707. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1708. int ret;
  1709. struct ssl_async_args args;
  1710. args.s = s;
  1711. args.buf = (void *)buf;
  1712. args.num = num;
  1713. args.type = WRITEFUNC;
  1714. args.f.func_write = s->method->ssl_write;
  1715. ret = ssl_start_async_job(s, &args, ssl_io_intern);
  1716. *written = s->asyncrw;
  1717. return ret;
  1718. } else {
  1719. return s->method->ssl_write(s, buf, num, written);
  1720. }
  1721. }
  1722. ossl_ssize_t SSL_sendfile(SSL *s, int fd, off_t offset, size_t size, int flags)
  1723. {
  1724. ossl_ssize_t ret;
  1725. if (s->handshake_func == NULL) {
  1726. SSLerr(SSL_F_SSL_SENDFILE, SSL_R_UNINITIALIZED);
  1727. return -1;
  1728. }
  1729. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  1730. s->rwstate = SSL_NOTHING;
  1731. SSLerr(SSL_F_SSL_SENDFILE, SSL_R_PROTOCOL_IS_SHUTDOWN);
  1732. return -1;
  1733. }
  1734. if (!BIO_get_ktls_send(s->wbio)) {
  1735. SSLerr(SSL_F_SSL_SENDFILE, SSL_R_UNINITIALIZED);
  1736. return -1;
  1737. }
  1738. /* If we have an alert to send, lets send it */
  1739. if (s->s3.alert_dispatch) {
  1740. ret = (ossl_ssize_t)s->method->ssl_dispatch_alert(s);
  1741. if (ret <= 0) {
  1742. /* SSLfatal() already called if appropriate */
  1743. return ret;
  1744. }
  1745. /* if it went, fall through and send more stuff */
  1746. }
  1747. s->rwstate = SSL_WRITING;
  1748. if (BIO_flush(s->wbio) <= 0) {
  1749. if (!BIO_should_retry(s->wbio)) {
  1750. s->rwstate = SSL_NOTHING;
  1751. } else {
  1752. #ifdef EAGAIN
  1753. set_sys_error(EAGAIN);
  1754. #endif
  1755. }
  1756. return -1;
  1757. }
  1758. #ifdef OPENSSL_NO_KTLS
  1759. ERR_raise_data(ERR_LIB_SYS, ERR_R_INTERNAL_ERROR, "calling sendfile()");
  1760. return -1;
  1761. #else
  1762. ret = ktls_sendfile(SSL_get_wfd(s), fd, offset, size, flags);
  1763. if (ret < 0) {
  1764. #if defined(EAGAIN) && defined(EINTR) && defined(EBUSY)
  1765. if ((get_last_sys_error() == EAGAIN) ||
  1766. (get_last_sys_error() == EINTR) ||
  1767. (get_last_sys_error() == EBUSY))
  1768. BIO_set_retry_write(s->wbio);
  1769. else
  1770. #endif
  1771. SSLerr(SSL_F_SSL_SENDFILE, SSL_R_UNINITIALIZED);
  1772. return ret;
  1773. }
  1774. s->rwstate = SSL_NOTHING;
  1775. return ret;
  1776. #endif
  1777. }
  1778. int SSL_write(SSL *s, const void *buf, int num)
  1779. {
  1780. int ret;
  1781. size_t written;
  1782. if (num < 0) {
  1783. SSLerr(SSL_F_SSL_WRITE, SSL_R_BAD_LENGTH);
  1784. return -1;
  1785. }
  1786. ret = ssl_write_internal(s, buf, (size_t)num, &written);
  1787. /*
  1788. * The cast is safe here because ret should be <= INT_MAX because num is
  1789. * <= INT_MAX
  1790. */
  1791. if (ret > 0)
  1792. ret = (int)written;
  1793. return ret;
  1794. }
  1795. int SSL_write_ex(SSL *s, const void *buf, size_t num, size_t *written)
  1796. {
  1797. int ret = ssl_write_internal(s, buf, num, written);
  1798. if (ret < 0)
  1799. ret = 0;
  1800. return ret;
  1801. }
  1802. int SSL_write_early_data(SSL *s, const void *buf, size_t num, size_t *written)
  1803. {
  1804. int ret, early_data_state;
  1805. size_t writtmp;
  1806. uint32_t partialwrite;
  1807. switch (s->early_data_state) {
  1808. case SSL_EARLY_DATA_NONE:
  1809. if (s->server
  1810. || !SSL_in_before(s)
  1811. || ((s->session == NULL || s->session->ext.max_early_data == 0)
  1812. && (s->psk_use_session_cb == NULL))) {
  1813. SSLerr(SSL_F_SSL_WRITE_EARLY_DATA,
  1814. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1815. return 0;
  1816. }
  1817. /* fall through */
  1818. case SSL_EARLY_DATA_CONNECT_RETRY:
  1819. s->early_data_state = SSL_EARLY_DATA_CONNECTING;
  1820. ret = SSL_connect(s);
  1821. if (ret <= 0) {
  1822. /* NBIO or error */
  1823. s->early_data_state = SSL_EARLY_DATA_CONNECT_RETRY;
  1824. return 0;
  1825. }
  1826. /* fall through */
  1827. case SSL_EARLY_DATA_WRITE_RETRY:
  1828. s->early_data_state = SSL_EARLY_DATA_WRITING;
  1829. /*
  1830. * We disable partial write for early data because we don't keep track
  1831. * of how many bytes we've written between the SSL_write_ex() call and
  1832. * the flush if the flush needs to be retried)
  1833. */
  1834. partialwrite = s->mode & SSL_MODE_ENABLE_PARTIAL_WRITE;
  1835. s->mode &= ~SSL_MODE_ENABLE_PARTIAL_WRITE;
  1836. ret = SSL_write_ex(s, buf, num, &writtmp);
  1837. s->mode |= partialwrite;
  1838. if (!ret) {
  1839. s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  1840. return ret;
  1841. }
  1842. s->early_data_state = SSL_EARLY_DATA_WRITE_FLUSH;
  1843. /* fall through */
  1844. case SSL_EARLY_DATA_WRITE_FLUSH:
  1845. /* The buffering BIO is still in place so we need to flush it */
  1846. if (statem_flush(s) != 1)
  1847. return 0;
  1848. *written = num;
  1849. s->early_data_state = SSL_EARLY_DATA_WRITE_RETRY;
  1850. return 1;
  1851. case SSL_EARLY_DATA_FINISHED_READING:
  1852. case SSL_EARLY_DATA_READ_RETRY:
  1853. early_data_state = s->early_data_state;
  1854. /* We are a server writing to an unauthenticated client */
  1855. s->early_data_state = SSL_EARLY_DATA_UNAUTH_WRITING;
  1856. ret = SSL_write_ex(s, buf, num, written);
  1857. /* The buffering BIO is still in place */
  1858. if (ret)
  1859. (void)BIO_flush(s->wbio);
  1860. s->early_data_state = early_data_state;
  1861. return ret;
  1862. default:
  1863. SSLerr(SSL_F_SSL_WRITE_EARLY_DATA, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1864. return 0;
  1865. }
  1866. }
  1867. int SSL_shutdown(SSL *s)
  1868. {
  1869. /*
  1870. * Note that this function behaves differently from what one might
  1871. * expect. Return values are 0 for no success (yet), 1 for success; but
  1872. * calling it once is usually not enough, even if blocking I/O is used
  1873. * (see ssl3_shutdown).
  1874. */
  1875. if (s->handshake_func == NULL) {
  1876. SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_UNINITIALIZED);
  1877. return -1;
  1878. }
  1879. if (!SSL_in_init(s)) {
  1880. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  1881. struct ssl_async_args args;
  1882. args.s = s;
  1883. args.type = OTHERFUNC;
  1884. args.f.func_other = s->method->ssl_shutdown;
  1885. return ssl_start_async_job(s, &args, ssl_io_intern);
  1886. } else {
  1887. return s->method->ssl_shutdown(s);
  1888. }
  1889. } else {
  1890. SSLerr(SSL_F_SSL_SHUTDOWN, SSL_R_SHUTDOWN_WHILE_IN_INIT);
  1891. return -1;
  1892. }
  1893. }
  1894. int SSL_key_update(SSL *s, int updatetype)
  1895. {
  1896. /*
  1897. * TODO(TLS1.3): How will applications know whether TLSv1.3 has been
  1898. * negotiated, and that it is appropriate to call SSL_key_update() instead
  1899. * of SSL_renegotiate().
  1900. */
  1901. if (!SSL_IS_TLS13(s)) {
  1902. SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_WRONG_SSL_VERSION);
  1903. return 0;
  1904. }
  1905. if (updatetype != SSL_KEY_UPDATE_NOT_REQUESTED
  1906. && updatetype != SSL_KEY_UPDATE_REQUESTED) {
  1907. SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_INVALID_KEY_UPDATE_TYPE);
  1908. return 0;
  1909. }
  1910. if (!SSL_is_init_finished(s)) {
  1911. SSLerr(SSL_F_SSL_KEY_UPDATE, SSL_R_STILL_IN_INIT);
  1912. return 0;
  1913. }
  1914. ossl_statem_set_in_init(s, 1);
  1915. s->key_update = updatetype;
  1916. return 1;
  1917. }
  1918. int SSL_get_key_update_type(const SSL *s)
  1919. {
  1920. return s->key_update;
  1921. }
  1922. int SSL_renegotiate(SSL *s)
  1923. {
  1924. if (SSL_IS_TLS13(s)) {
  1925. SSLerr(SSL_F_SSL_RENEGOTIATE, SSL_R_WRONG_SSL_VERSION);
  1926. return 0;
  1927. }
  1928. if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
  1929. SSLerr(SSL_F_SSL_RENEGOTIATE, SSL_R_NO_RENEGOTIATION);
  1930. return 0;
  1931. }
  1932. s->renegotiate = 1;
  1933. s->new_session = 1;
  1934. return s->method->ssl_renegotiate(s);
  1935. }
  1936. int SSL_renegotiate_abbreviated(SSL *s)
  1937. {
  1938. if (SSL_IS_TLS13(s)) {
  1939. SSLerr(SSL_F_SSL_RENEGOTIATE_ABBREVIATED, SSL_R_WRONG_SSL_VERSION);
  1940. return 0;
  1941. }
  1942. if ((s->options & SSL_OP_NO_RENEGOTIATION)) {
  1943. SSLerr(SSL_F_SSL_RENEGOTIATE_ABBREVIATED, SSL_R_NO_RENEGOTIATION);
  1944. return 0;
  1945. }
  1946. s->renegotiate = 1;
  1947. s->new_session = 0;
  1948. return s->method->ssl_renegotiate(s);
  1949. }
  1950. int SSL_renegotiate_pending(const SSL *s)
  1951. {
  1952. /*
  1953. * becomes true when negotiation is requested; false again once a
  1954. * handshake has finished
  1955. */
  1956. return (s->renegotiate != 0);
  1957. }
  1958. long SSL_ctrl(SSL *s, int cmd, long larg, void *parg)
  1959. {
  1960. long l;
  1961. switch (cmd) {
  1962. case SSL_CTRL_GET_READ_AHEAD:
  1963. return RECORD_LAYER_get_read_ahead(&s->rlayer);
  1964. case SSL_CTRL_SET_READ_AHEAD:
  1965. l = RECORD_LAYER_get_read_ahead(&s->rlayer);
  1966. RECORD_LAYER_set_read_ahead(&s->rlayer, larg);
  1967. return l;
  1968. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  1969. s->msg_callback_arg = parg;
  1970. return 1;
  1971. case SSL_CTRL_MODE:
  1972. return (s->mode |= larg);
  1973. case SSL_CTRL_CLEAR_MODE:
  1974. return (s->mode &= ~larg);
  1975. case SSL_CTRL_GET_MAX_CERT_LIST:
  1976. return (long)s->max_cert_list;
  1977. case SSL_CTRL_SET_MAX_CERT_LIST:
  1978. if (larg < 0)
  1979. return 0;
  1980. l = (long)s->max_cert_list;
  1981. s->max_cert_list = (size_t)larg;
  1982. return l;
  1983. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  1984. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  1985. return 0;
  1986. #ifndef OPENSSL_NO_KTLS
  1987. if (s->wbio != NULL && BIO_get_ktls_send(s->wbio))
  1988. return 0;
  1989. #endif /* OPENSSL_NO_KTLS */
  1990. s->max_send_fragment = larg;
  1991. if (s->max_send_fragment < s->split_send_fragment)
  1992. s->split_send_fragment = s->max_send_fragment;
  1993. return 1;
  1994. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  1995. if ((size_t)larg > s->max_send_fragment || larg == 0)
  1996. return 0;
  1997. s->split_send_fragment = larg;
  1998. return 1;
  1999. case SSL_CTRL_SET_MAX_PIPELINES:
  2000. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  2001. return 0;
  2002. s->max_pipelines = larg;
  2003. if (larg > 1)
  2004. RECORD_LAYER_set_read_ahead(&s->rlayer, 1);
  2005. return 1;
  2006. case SSL_CTRL_GET_RI_SUPPORT:
  2007. return s->s3.send_connection_binding;
  2008. case SSL_CTRL_CERT_FLAGS:
  2009. return (s->cert->cert_flags |= larg);
  2010. case SSL_CTRL_CLEAR_CERT_FLAGS:
  2011. return (s->cert->cert_flags &= ~larg);
  2012. case SSL_CTRL_GET_RAW_CIPHERLIST:
  2013. if (parg) {
  2014. if (s->s3.tmp.ciphers_raw == NULL)
  2015. return 0;
  2016. *(unsigned char **)parg = s->s3.tmp.ciphers_raw;
  2017. return (int)s->s3.tmp.ciphers_rawlen;
  2018. } else {
  2019. return TLS_CIPHER_LEN;
  2020. }
  2021. case SSL_CTRL_GET_EXTMS_SUPPORT:
  2022. if (!s->session || SSL_in_init(s) || ossl_statem_get_in_handshake(s))
  2023. return -1;
  2024. if (s->session->flags & SSL_SESS_FLAG_EXTMS)
  2025. return 1;
  2026. else
  2027. return 0;
  2028. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  2029. return ssl_check_allowed_versions(larg, s->max_proto_version)
  2030. && ssl_set_version_bound(s->ctx->method->version, (int)larg,
  2031. &s->min_proto_version);
  2032. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  2033. return s->min_proto_version;
  2034. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  2035. return ssl_check_allowed_versions(s->min_proto_version, larg)
  2036. && ssl_set_version_bound(s->ctx->method->version, (int)larg,
  2037. &s->max_proto_version);
  2038. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  2039. return s->max_proto_version;
  2040. default:
  2041. return s->method->ssl_ctrl(s, cmd, larg, parg);
  2042. }
  2043. }
  2044. long SSL_callback_ctrl(SSL *s, int cmd, void (*fp) (void))
  2045. {
  2046. switch (cmd) {
  2047. case SSL_CTRL_SET_MSG_CALLBACK:
  2048. s->msg_callback = (void (*)
  2049. (int write_p, int version, int content_type,
  2050. const void *buf, size_t len, SSL *ssl,
  2051. void *arg))(fp);
  2052. return 1;
  2053. default:
  2054. return s->method->ssl_callback_ctrl(s, cmd, fp);
  2055. }
  2056. }
  2057. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx)
  2058. {
  2059. return ctx->sessions;
  2060. }
  2061. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg)
  2062. {
  2063. long l;
  2064. /* For some cases with ctx == NULL perform syntax checks */
  2065. if (ctx == NULL) {
  2066. switch (cmd) {
  2067. #ifndef OPENSSL_NO_EC
  2068. case SSL_CTRL_SET_GROUPS_LIST:
  2069. return tls1_set_groups_list(NULL, NULL, parg);
  2070. #endif
  2071. case SSL_CTRL_SET_SIGALGS_LIST:
  2072. case SSL_CTRL_SET_CLIENT_SIGALGS_LIST:
  2073. return tls1_set_sigalgs_list(NULL, parg, 0);
  2074. default:
  2075. return 0;
  2076. }
  2077. }
  2078. switch (cmd) {
  2079. case SSL_CTRL_GET_READ_AHEAD:
  2080. return ctx->read_ahead;
  2081. case SSL_CTRL_SET_READ_AHEAD:
  2082. l = ctx->read_ahead;
  2083. ctx->read_ahead = larg;
  2084. return l;
  2085. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  2086. ctx->msg_callback_arg = parg;
  2087. return 1;
  2088. case SSL_CTRL_GET_MAX_CERT_LIST:
  2089. return (long)ctx->max_cert_list;
  2090. case SSL_CTRL_SET_MAX_CERT_LIST:
  2091. if (larg < 0)
  2092. return 0;
  2093. l = (long)ctx->max_cert_list;
  2094. ctx->max_cert_list = (size_t)larg;
  2095. return l;
  2096. case SSL_CTRL_SET_SESS_CACHE_SIZE:
  2097. if (larg < 0)
  2098. return 0;
  2099. l = (long)ctx->session_cache_size;
  2100. ctx->session_cache_size = (size_t)larg;
  2101. return l;
  2102. case SSL_CTRL_GET_SESS_CACHE_SIZE:
  2103. return (long)ctx->session_cache_size;
  2104. case SSL_CTRL_SET_SESS_CACHE_MODE:
  2105. l = ctx->session_cache_mode;
  2106. ctx->session_cache_mode = larg;
  2107. return l;
  2108. case SSL_CTRL_GET_SESS_CACHE_MODE:
  2109. return ctx->session_cache_mode;
  2110. case SSL_CTRL_SESS_NUMBER:
  2111. return lh_SSL_SESSION_num_items(ctx->sessions);
  2112. case SSL_CTRL_SESS_CONNECT:
  2113. return tsan_load(&ctx->stats.sess_connect);
  2114. case SSL_CTRL_SESS_CONNECT_GOOD:
  2115. return tsan_load(&ctx->stats.sess_connect_good);
  2116. case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
  2117. return tsan_load(&ctx->stats.sess_connect_renegotiate);
  2118. case SSL_CTRL_SESS_ACCEPT:
  2119. return tsan_load(&ctx->stats.sess_accept);
  2120. case SSL_CTRL_SESS_ACCEPT_GOOD:
  2121. return tsan_load(&ctx->stats.sess_accept_good);
  2122. case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
  2123. return tsan_load(&ctx->stats.sess_accept_renegotiate);
  2124. case SSL_CTRL_SESS_HIT:
  2125. return tsan_load(&ctx->stats.sess_hit);
  2126. case SSL_CTRL_SESS_CB_HIT:
  2127. return tsan_load(&ctx->stats.sess_cb_hit);
  2128. case SSL_CTRL_SESS_MISSES:
  2129. return tsan_load(&ctx->stats.sess_miss);
  2130. case SSL_CTRL_SESS_TIMEOUTS:
  2131. return tsan_load(&ctx->stats.sess_timeout);
  2132. case SSL_CTRL_SESS_CACHE_FULL:
  2133. return tsan_load(&ctx->stats.sess_cache_full);
  2134. case SSL_CTRL_MODE:
  2135. return (ctx->mode |= larg);
  2136. case SSL_CTRL_CLEAR_MODE:
  2137. return (ctx->mode &= ~larg);
  2138. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  2139. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH)
  2140. return 0;
  2141. ctx->max_send_fragment = larg;
  2142. if (ctx->max_send_fragment < ctx->split_send_fragment)
  2143. ctx->split_send_fragment = ctx->max_send_fragment;
  2144. return 1;
  2145. case SSL_CTRL_SET_SPLIT_SEND_FRAGMENT:
  2146. if ((size_t)larg > ctx->max_send_fragment || larg == 0)
  2147. return 0;
  2148. ctx->split_send_fragment = larg;
  2149. return 1;
  2150. case SSL_CTRL_SET_MAX_PIPELINES:
  2151. if (larg < 1 || larg > SSL_MAX_PIPELINES)
  2152. return 0;
  2153. ctx->max_pipelines = larg;
  2154. return 1;
  2155. case SSL_CTRL_CERT_FLAGS:
  2156. return (ctx->cert->cert_flags |= larg);
  2157. case SSL_CTRL_CLEAR_CERT_FLAGS:
  2158. return (ctx->cert->cert_flags &= ~larg);
  2159. case SSL_CTRL_SET_MIN_PROTO_VERSION:
  2160. return ssl_check_allowed_versions(larg, ctx->max_proto_version)
  2161. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2162. &ctx->min_proto_version);
  2163. case SSL_CTRL_GET_MIN_PROTO_VERSION:
  2164. return ctx->min_proto_version;
  2165. case SSL_CTRL_SET_MAX_PROTO_VERSION:
  2166. return ssl_check_allowed_versions(ctx->min_proto_version, larg)
  2167. && ssl_set_version_bound(ctx->method->version, (int)larg,
  2168. &ctx->max_proto_version);
  2169. case SSL_CTRL_GET_MAX_PROTO_VERSION:
  2170. return ctx->max_proto_version;
  2171. default:
  2172. return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
  2173. }
  2174. }
  2175. long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp) (void))
  2176. {
  2177. switch (cmd) {
  2178. case SSL_CTRL_SET_MSG_CALLBACK:
  2179. ctx->msg_callback = (void (*)
  2180. (int write_p, int version, int content_type,
  2181. const void *buf, size_t len, SSL *ssl,
  2182. void *arg))(fp);
  2183. return 1;
  2184. default:
  2185. return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
  2186. }
  2187. }
  2188. int ssl_cipher_id_cmp(const SSL_CIPHER *a, const SSL_CIPHER *b)
  2189. {
  2190. if (a->id > b->id)
  2191. return 1;
  2192. if (a->id < b->id)
  2193. return -1;
  2194. return 0;
  2195. }
  2196. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER *const *ap,
  2197. const SSL_CIPHER *const *bp)
  2198. {
  2199. if ((*ap)->id > (*bp)->id)
  2200. return 1;
  2201. if ((*ap)->id < (*bp)->id)
  2202. return -1;
  2203. return 0;
  2204. }
  2205. /** return a STACK of the ciphers available for the SSL and in order of
  2206. * preference */
  2207. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *s)
  2208. {
  2209. if (s != NULL) {
  2210. if (s->cipher_list != NULL) {
  2211. return s->cipher_list;
  2212. } else if ((s->ctx != NULL) && (s->ctx->cipher_list != NULL)) {
  2213. return s->ctx->cipher_list;
  2214. }
  2215. }
  2216. return NULL;
  2217. }
  2218. STACK_OF(SSL_CIPHER) *SSL_get_client_ciphers(const SSL *s)
  2219. {
  2220. if ((s == NULL) || !s->server)
  2221. return NULL;
  2222. return s->peer_ciphers;
  2223. }
  2224. STACK_OF(SSL_CIPHER) *SSL_get1_supported_ciphers(SSL *s)
  2225. {
  2226. STACK_OF(SSL_CIPHER) *sk = NULL, *ciphers;
  2227. int i;
  2228. ciphers = SSL_get_ciphers(s);
  2229. if (!ciphers)
  2230. return NULL;
  2231. if (!ssl_set_client_disabled(s))
  2232. return NULL;
  2233. for (i = 0; i < sk_SSL_CIPHER_num(ciphers); i++) {
  2234. const SSL_CIPHER *c = sk_SSL_CIPHER_value(ciphers, i);
  2235. if (!ssl_cipher_disabled(s, c, SSL_SECOP_CIPHER_SUPPORTED, 0)) {
  2236. if (!sk)
  2237. sk = sk_SSL_CIPHER_new_null();
  2238. if (!sk)
  2239. return NULL;
  2240. if (!sk_SSL_CIPHER_push(sk, c)) {
  2241. sk_SSL_CIPHER_free(sk);
  2242. return NULL;
  2243. }
  2244. }
  2245. }
  2246. return sk;
  2247. }
  2248. /** return a STACK of the ciphers available for the SSL and in order of
  2249. * algorithm id */
  2250. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *s)
  2251. {
  2252. if (s != NULL) {
  2253. if (s->cipher_list_by_id != NULL) {
  2254. return s->cipher_list_by_id;
  2255. } else if ((s->ctx != NULL) && (s->ctx->cipher_list_by_id != NULL)) {
  2256. return s->ctx->cipher_list_by_id;
  2257. }
  2258. }
  2259. return NULL;
  2260. }
  2261. /** The old interface to get the same thing as SSL_get_ciphers() */
  2262. const char *SSL_get_cipher_list(const SSL *s, int n)
  2263. {
  2264. const SSL_CIPHER *c;
  2265. STACK_OF(SSL_CIPHER) *sk;
  2266. if (s == NULL)
  2267. return NULL;
  2268. sk = SSL_get_ciphers(s);
  2269. if ((sk == NULL) || (sk_SSL_CIPHER_num(sk) <= n))
  2270. return NULL;
  2271. c = sk_SSL_CIPHER_value(sk, n);
  2272. if (c == NULL)
  2273. return NULL;
  2274. return c->name;
  2275. }
  2276. /** return a STACK of the ciphers available for the SSL_CTX and in order of
  2277. * preference */
  2278. STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx)
  2279. {
  2280. if (ctx != NULL)
  2281. return ctx->cipher_list;
  2282. return NULL;
  2283. }
  2284. /*
  2285. * Distinguish between ciphers controlled by set_ciphersuite() and
  2286. * set_cipher_list() when counting.
  2287. */
  2288. static int cipher_list_tls12_num(STACK_OF(SSL_CIPHER) *sk)
  2289. {
  2290. int i, num = 0;
  2291. const SSL_CIPHER *c;
  2292. if (sk == NULL)
  2293. return 0;
  2294. for (i = 0; i < sk_SSL_CIPHER_num(sk); ++i) {
  2295. c = sk_SSL_CIPHER_value(sk, i);
  2296. if (c->min_tls >= TLS1_3_VERSION)
  2297. continue;
  2298. num++;
  2299. }
  2300. return num;
  2301. }
  2302. /** specify the ciphers to be used by default by the SSL_CTX */
  2303. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str)
  2304. {
  2305. STACK_OF(SSL_CIPHER) *sk;
  2306. sk = ssl_create_cipher_list(ctx->method, ctx->tls13_ciphersuites,
  2307. &ctx->cipher_list, &ctx->cipher_list_by_id, str,
  2308. ctx->cert);
  2309. /*
  2310. * ssl_create_cipher_list may return an empty stack if it was unable to
  2311. * find a cipher matching the given rule string (for example if the rule
  2312. * string specifies a cipher which has been disabled). This is not an
  2313. * error as far as ssl_create_cipher_list is concerned, and hence
  2314. * ctx->cipher_list and ctx->cipher_list_by_id has been updated.
  2315. */
  2316. if (sk == NULL)
  2317. return 0;
  2318. else if (cipher_list_tls12_num(sk) == 0) {
  2319. SSLerr(SSL_F_SSL_CTX_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  2320. return 0;
  2321. }
  2322. return 1;
  2323. }
  2324. /** specify the ciphers to be used by the SSL */
  2325. int SSL_set_cipher_list(SSL *s, const char *str)
  2326. {
  2327. STACK_OF(SSL_CIPHER) *sk;
  2328. sk = ssl_create_cipher_list(s->ctx->method, s->tls13_ciphersuites,
  2329. &s->cipher_list, &s->cipher_list_by_id, str,
  2330. s->cert);
  2331. /* see comment in SSL_CTX_set_cipher_list */
  2332. if (sk == NULL)
  2333. return 0;
  2334. else if (cipher_list_tls12_num(sk) == 0) {
  2335. SSLerr(SSL_F_SSL_SET_CIPHER_LIST, SSL_R_NO_CIPHER_MATCH);
  2336. return 0;
  2337. }
  2338. return 1;
  2339. }
  2340. char *SSL_get_shared_ciphers(const SSL *s, char *buf, int size)
  2341. {
  2342. char *p;
  2343. STACK_OF(SSL_CIPHER) *clntsk, *srvrsk;
  2344. const SSL_CIPHER *c;
  2345. int i;
  2346. if (!s->server
  2347. || s->peer_ciphers == NULL
  2348. || size < 2)
  2349. return NULL;
  2350. p = buf;
  2351. clntsk = s->peer_ciphers;
  2352. srvrsk = SSL_get_ciphers(s);
  2353. if (clntsk == NULL || srvrsk == NULL)
  2354. return NULL;
  2355. if (sk_SSL_CIPHER_num(clntsk) == 0 || sk_SSL_CIPHER_num(srvrsk) == 0)
  2356. return NULL;
  2357. for (i = 0; i < sk_SSL_CIPHER_num(clntsk); i++) {
  2358. int n;
  2359. c = sk_SSL_CIPHER_value(clntsk, i);
  2360. if (sk_SSL_CIPHER_find(srvrsk, c) < 0)
  2361. continue;
  2362. n = strlen(c->name);
  2363. if (n + 1 > size) {
  2364. if (p != buf)
  2365. --p;
  2366. *p = '\0';
  2367. return buf;
  2368. }
  2369. strcpy(p, c->name);
  2370. p += n;
  2371. *(p++) = ':';
  2372. size -= n + 1;
  2373. }
  2374. p[-1] = '\0';
  2375. return buf;
  2376. }
  2377. /** return a servername extension value if provided in Client Hello, or NULL.
  2378. * So far, only host_name types are defined (RFC 3546).
  2379. */
  2380. const char *SSL_get_servername(const SSL *s, const int type)
  2381. {
  2382. if (type != TLSEXT_NAMETYPE_host_name)
  2383. return NULL;
  2384. /*
  2385. * SNI is not negotiated in pre-TLS-1.3 resumption flows, so fake up an
  2386. * SNI value to return if we are resuming/resumed. N.B. that we still
  2387. * call the relevant callbacks for such resumption flows, and callbacks
  2388. * might error out if there is not a SNI value available.
  2389. */
  2390. if (s->hit)
  2391. return s->session->ext.hostname;
  2392. return s->ext.hostname;
  2393. }
  2394. int SSL_get_servername_type(const SSL *s)
  2395. {
  2396. if (s->session
  2397. && (!s->ext.hostname ? s->session->
  2398. ext.hostname : s->ext.hostname))
  2399. return TLSEXT_NAMETYPE_host_name;
  2400. return -1;
  2401. }
  2402. /*
  2403. * SSL_select_next_proto implements the standard protocol selection. It is
  2404. * expected that this function is called from the callback set by
  2405. * SSL_CTX_set_next_proto_select_cb. The protocol data is assumed to be a
  2406. * vector of 8-bit, length prefixed byte strings. The length byte itself is
  2407. * not included in the length. A byte string of length 0 is invalid. No byte
  2408. * string may be truncated. The current, but experimental algorithm for
  2409. * selecting the protocol is: 1) If the server doesn't support NPN then this
  2410. * is indicated to the callback. In this case, the client application has to
  2411. * abort the connection or have a default application level protocol. 2) If
  2412. * the server supports NPN, but advertises an empty list then the client
  2413. * selects the first protocol in its list, but indicates via the API that this
  2414. * fallback case was enacted. 3) Otherwise, the client finds the first
  2415. * protocol in the server's list that it supports and selects this protocol.
  2416. * This is because it's assumed that the server has better information about
  2417. * which protocol a client should use. 4) If the client doesn't support any
  2418. * of the server's advertised protocols, then this is treated the same as
  2419. * case 2. It returns either OPENSSL_NPN_NEGOTIATED if a common protocol was
  2420. * found, or OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  2421. */
  2422. int SSL_select_next_proto(unsigned char **out, unsigned char *outlen,
  2423. const unsigned char *server,
  2424. unsigned int server_len,
  2425. const unsigned char *client, unsigned int client_len)
  2426. {
  2427. unsigned int i, j;
  2428. const unsigned char *result;
  2429. int status = OPENSSL_NPN_UNSUPPORTED;
  2430. /*
  2431. * For each protocol in server preference order, see if we support it.
  2432. */
  2433. for (i = 0; i < server_len;) {
  2434. for (j = 0; j < client_len;) {
  2435. if (server[i] == client[j] &&
  2436. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  2437. /* We found a match */
  2438. result = &server[i];
  2439. status = OPENSSL_NPN_NEGOTIATED;
  2440. goto found;
  2441. }
  2442. j += client[j];
  2443. j++;
  2444. }
  2445. i += server[i];
  2446. i++;
  2447. }
  2448. /* There's no overlap between our protocols and the server's list. */
  2449. result = client;
  2450. status = OPENSSL_NPN_NO_OVERLAP;
  2451. found:
  2452. *out = (unsigned char *)result + 1;
  2453. *outlen = result[0];
  2454. return status;
  2455. }
  2456. #ifndef OPENSSL_NO_NEXTPROTONEG
  2457. /*
  2458. * SSL_get0_next_proto_negotiated sets *data and *len to point to the
  2459. * client's requested protocol for this connection and returns 0. If the
  2460. * client didn't request any protocol, then *data is set to NULL. Note that
  2461. * the client can request any protocol it chooses. The value returned from
  2462. * this function need not be a member of the list of supported protocols
  2463. * provided by the callback.
  2464. */
  2465. void SSL_get0_next_proto_negotiated(const SSL *s, const unsigned char **data,
  2466. unsigned *len)
  2467. {
  2468. *data = s->ext.npn;
  2469. if (*data == NULL) {
  2470. *len = 0;
  2471. } else {
  2472. *len = (unsigned int)s->ext.npn_len;
  2473. }
  2474. }
  2475. /*
  2476. * SSL_CTX_set_npn_advertised_cb sets a callback that is called when
  2477. * a TLS server needs a list of supported protocols for Next Protocol
  2478. * Negotiation. The returned list must be in wire format. The list is
  2479. * returned by setting |out| to point to it and |outlen| to its length. This
  2480. * memory will not be modified, but one should assume that the SSL* keeps a
  2481. * reference to it. The callback should return SSL_TLSEXT_ERR_OK if it
  2482. * wishes to advertise. Otherwise, no such extension will be included in the
  2483. * ServerHello.
  2484. */
  2485. void SSL_CTX_set_npn_advertised_cb(SSL_CTX *ctx,
  2486. SSL_CTX_npn_advertised_cb_func cb,
  2487. void *arg)
  2488. {
  2489. ctx->ext.npn_advertised_cb = cb;
  2490. ctx->ext.npn_advertised_cb_arg = arg;
  2491. }
  2492. /*
  2493. * SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  2494. * client needs to select a protocol from the server's provided list. |out|
  2495. * must be set to point to the selected protocol (which may be within |in|).
  2496. * The length of the protocol name must be written into |outlen|. The
  2497. * server's advertised protocols are provided in |in| and |inlen|. The
  2498. * callback can assume that |in| is syntactically valid. The client must
  2499. * select a protocol. It is fatal to the connection if this callback returns
  2500. * a value other than SSL_TLSEXT_ERR_OK.
  2501. */
  2502. void SSL_CTX_set_npn_select_cb(SSL_CTX *ctx,
  2503. SSL_CTX_npn_select_cb_func cb,
  2504. void *arg)
  2505. {
  2506. ctx->ext.npn_select_cb = cb;
  2507. ctx->ext.npn_select_cb_arg = arg;
  2508. }
  2509. #endif
  2510. /*
  2511. * SSL_CTX_set_alpn_protos sets the ALPN protocol list on |ctx| to |protos|.
  2512. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  2513. * length-prefixed strings). Returns 0 on success.
  2514. */
  2515. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const unsigned char *protos,
  2516. unsigned int protos_len)
  2517. {
  2518. OPENSSL_free(ctx->ext.alpn);
  2519. ctx->ext.alpn = OPENSSL_memdup(protos, protos_len);
  2520. if (ctx->ext.alpn == NULL) {
  2521. SSLerr(SSL_F_SSL_CTX_SET_ALPN_PROTOS, ERR_R_MALLOC_FAILURE);
  2522. return 1;
  2523. }
  2524. ctx->ext.alpn_len = protos_len;
  2525. return 0;
  2526. }
  2527. /*
  2528. * SSL_set_alpn_protos sets the ALPN protocol list on |ssl| to |protos|.
  2529. * |protos| must be in wire-format (i.e. a series of non-empty, 8-bit
  2530. * length-prefixed strings). Returns 0 on success.
  2531. */
  2532. int SSL_set_alpn_protos(SSL *ssl, const unsigned char *protos,
  2533. unsigned int protos_len)
  2534. {
  2535. OPENSSL_free(ssl->ext.alpn);
  2536. ssl->ext.alpn = OPENSSL_memdup(protos, protos_len);
  2537. if (ssl->ext.alpn == NULL) {
  2538. SSLerr(SSL_F_SSL_SET_ALPN_PROTOS, ERR_R_MALLOC_FAILURE);
  2539. return 1;
  2540. }
  2541. ssl->ext.alpn_len = protos_len;
  2542. return 0;
  2543. }
  2544. /*
  2545. * SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is
  2546. * called during ClientHello processing in order to select an ALPN protocol
  2547. * from the client's list of offered protocols.
  2548. */
  2549. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  2550. SSL_CTX_alpn_select_cb_func cb,
  2551. void *arg)
  2552. {
  2553. ctx->ext.alpn_select_cb = cb;
  2554. ctx->ext.alpn_select_cb_arg = arg;
  2555. }
  2556. /*
  2557. * SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
  2558. * On return it sets |*data| to point to |*len| bytes of protocol name
  2559. * (not including the leading length-prefix byte). If the server didn't
  2560. * respond with a negotiated protocol then |*len| will be zero.
  2561. */
  2562. void SSL_get0_alpn_selected(const SSL *ssl, const unsigned char **data,
  2563. unsigned int *len)
  2564. {
  2565. *data = ssl->s3.alpn_selected;
  2566. if (*data == NULL)
  2567. *len = 0;
  2568. else
  2569. *len = (unsigned int)ssl->s3.alpn_selected_len;
  2570. }
  2571. int SSL_export_keying_material(SSL *s, unsigned char *out, size_t olen,
  2572. const char *label, size_t llen,
  2573. const unsigned char *context, size_t contextlen,
  2574. int use_context)
  2575. {
  2576. if (s->version < TLS1_VERSION && s->version != DTLS1_BAD_VER)
  2577. return -1;
  2578. return s->method->ssl3_enc->export_keying_material(s, out, olen, label,
  2579. llen, context,
  2580. contextlen, use_context);
  2581. }
  2582. int SSL_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
  2583. const char *label, size_t llen,
  2584. const unsigned char *context,
  2585. size_t contextlen)
  2586. {
  2587. if (s->version != TLS1_3_VERSION)
  2588. return 0;
  2589. return tls13_export_keying_material_early(s, out, olen, label, llen,
  2590. context, contextlen);
  2591. }
  2592. static unsigned long ssl_session_hash(const SSL_SESSION *a)
  2593. {
  2594. const unsigned char *session_id = a->session_id;
  2595. unsigned long l;
  2596. unsigned char tmp_storage[4];
  2597. if (a->session_id_length < sizeof(tmp_storage)) {
  2598. memset(tmp_storage, 0, sizeof(tmp_storage));
  2599. memcpy(tmp_storage, a->session_id, a->session_id_length);
  2600. session_id = tmp_storage;
  2601. }
  2602. l = (unsigned long)
  2603. ((unsigned long)session_id[0]) |
  2604. ((unsigned long)session_id[1] << 8L) |
  2605. ((unsigned long)session_id[2] << 16L) |
  2606. ((unsigned long)session_id[3] << 24L);
  2607. return l;
  2608. }
  2609. /*
  2610. * NB: If this function (or indeed the hash function which uses a sort of
  2611. * coarser function than this one) is changed, ensure
  2612. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on
  2613. * being able to construct an SSL_SESSION that will collide with any existing
  2614. * session with a matching session ID.
  2615. */
  2616. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b)
  2617. {
  2618. if (a->ssl_version != b->ssl_version)
  2619. return 1;
  2620. if (a->session_id_length != b->session_id_length)
  2621. return 1;
  2622. return memcmp(a->session_id, b->session_id, a->session_id_length);
  2623. }
  2624. /*
  2625. * These wrapper functions should remain rather than redeclaring
  2626. * SSL_SESSION_hash and SSL_SESSION_cmp for void* types and casting each
  2627. * variable. The reason is that the functions aren't static, they're exposed
  2628. * via ssl.h.
  2629. */
  2630. SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth)
  2631. {
  2632. SSL_CTX *ret = NULL;
  2633. if (meth == NULL) {
  2634. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_NULL_SSL_METHOD_PASSED);
  2635. return NULL;
  2636. }
  2637. if (!OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL))
  2638. return NULL;
  2639. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  2640. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  2641. goto err;
  2642. }
  2643. ret = OPENSSL_zalloc(sizeof(*ret));
  2644. if (ret == NULL)
  2645. goto err;
  2646. ret->method = meth;
  2647. ret->min_proto_version = 0;
  2648. ret->max_proto_version = 0;
  2649. ret->mode = SSL_MODE_AUTO_RETRY;
  2650. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  2651. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  2652. /* We take the system default. */
  2653. ret->session_timeout = meth->get_timeout();
  2654. ret->references = 1;
  2655. ret->lock = CRYPTO_THREAD_lock_new();
  2656. if (ret->lock == NULL) {
  2657. SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
  2658. OPENSSL_free(ret);
  2659. return NULL;
  2660. }
  2661. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  2662. ret->verify_mode = SSL_VERIFY_NONE;
  2663. if ((ret->cert = ssl_cert_new()) == NULL)
  2664. goto err;
  2665. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  2666. if (ret->sessions == NULL)
  2667. goto err;
  2668. ret->cert_store = X509_STORE_new();
  2669. if (ret->cert_store == NULL)
  2670. goto err;
  2671. #ifndef OPENSSL_NO_CT
  2672. ret->ctlog_store = CTLOG_STORE_new();
  2673. if (ret->ctlog_store == NULL)
  2674. goto err;
  2675. #endif
  2676. if (!SSL_CTX_set_ciphersuites(ret, OSSL_default_ciphersuites()))
  2677. goto err;
  2678. if (!ssl_create_cipher_list(ret->method,
  2679. ret->tls13_ciphersuites,
  2680. &ret->cipher_list, &ret->cipher_list_by_id,
  2681. OSSL_default_cipher_list(), ret->cert)
  2682. || sk_SSL_CIPHER_num(ret->cipher_list) <= 0) {
  2683. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  2684. goto err2;
  2685. }
  2686. ret->param = X509_VERIFY_PARAM_new();
  2687. if (ret->param == NULL)
  2688. goto err;
  2689. if ((ret->md5 = EVP_get_digestbyname("ssl3-md5")) == NULL) {
  2690. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_MD5_ROUTINES);
  2691. goto err2;
  2692. }
  2693. if ((ret->sha1 = EVP_get_digestbyname("ssl3-sha1")) == NULL) {
  2694. SSLerr(SSL_F_SSL_CTX_NEW, SSL_R_UNABLE_TO_LOAD_SSL3_SHA1_ROUTINES);
  2695. goto err2;
  2696. }
  2697. if ((ret->ca_names = sk_X509_NAME_new_null()) == NULL)
  2698. goto err;
  2699. if ((ret->client_ca_names = sk_X509_NAME_new_null()) == NULL)
  2700. goto err;
  2701. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_SSL_CTX, ret, &ret->ex_data))
  2702. goto err;
  2703. if ((ret->ext.secure = OPENSSL_secure_zalloc(sizeof(*ret->ext.secure))) == NULL)
  2704. goto err;
  2705. /* No compression for DTLS */
  2706. if (!(meth->ssl3_enc->enc_flags & SSL_ENC_FLAG_DTLS))
  2707. ret->comp_methods = SSL_COMP_get_compression_methods();
  2708. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  2709. ret->split_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  2710. /* Setup RFC5077 ticket keys */
  2711. if ((RAND_bytes(ret->ext.tick_key_name,
  2712. sizeof(ret->ext.tick_key_name)) <= 0)
  2713. || (RAND_priv_bytes(ret->ext.secure->tick_hmac_key,
  2714. sizeof(ret->ext.secure->tick_hmac_key)) <= 0)
  2715. || (RAND_priv_bytes(ret->ext.secure->tick_aes_key,
  2716. sizeof(ret->ext.secure->tick_aes_key)) <= 0))
  2717. ret->options |= SSL_OP_NO_TICKET;
  2718. if (RAND_priv_bytes(ret->ext.cookie_hmac_key,
  2719. sizeof(ret->ext.cookie_hmac_key)) <= 0)
  2720. goto err;
  2721. #ifndef OPENSSL_NO_SRP
  2722. if (!SSL_CTX_SRP_CTX_init(ret))
  2723. goto err;
  2724. #endif
  2725. #ifndef OPENSSL_NO_ENGINE
  2726. # ifdef OPENSSL_SSL_CLIENT_ENGINE_AUTO
  2727. # define eng_strx(x) #x
  2728. # define eng_str(x) eng_strx(x)
  2729. /* Use specific client engine automatically... ignore errors */
  2730. {
  2731. ENGINE *eng;
  2732. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2733. if (!eng) {
  2734. ERR_clear_error();
  2735. ENGINE_load_builtin_engines();
  2736. eng = ENGINE_by_id(eng_str(OPENSSL_SSL_CLIENT_ENGINE_AUTO));
  2737. }
  2738. if (!eng || !SSL_CTX_set_client_cert_engine(ret, eng))
  2739. ERR_clear_error();
  2740. }
  2741. # endif
  2742. #endif
  2743. /*
  2744. * Default is to connect to non-RI servers. When RI is more widely
  2745. * deployed might change this.
  2746. */
  2747. ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
  2748. /*
  2749. * Disable compression by default to prevent CRIME. Applications can
  2750. * re-enable compression by configuring
  2751. * SSL_CTX_clear_options(ctx, SSL_OP_NO_COMPRESSION);
  2752. * or by using the SSL_CONF library. Similarly we also enable TLSv1.3
  2753. * middlebox compatibility by default. This may be disabled by default in
  2754. * a later OpenSSL version.
  2755. */
  2756. ret->options |= SSL_OP_NO_COMPRESSION | SSL_OP_ENABLE_MIDDLEBOX_COMPAT;
  2757. ret->ext.status_type = TLSEXT_STATUSTYPE_nothing;
  2758. /*
  2759. * We cannot usefully set a default max_early_data here (which gets
  2760. * propagated in SSL_new(), for the following reason: setting the
  2761. * SSL field causes tls_construct_stoc_early_data() to tell the
  2762. * client that early data will be accepted when constructing a TLS 1.3
  2763. * session ticket, and the client will accordingly send us early data
  2764. * when using that ticket (if the client has early data to send).
  2765. * However, in order for the early data to actually be consumed by
  2766. * the application, the application must also have calls to
  2767. * SSL_read_early_data(); otherwise we'll just skip past the early data
  2768. * and ignore it. So, since the application must add calls to
  2769. * SSL_read_early_data(), we also require them to add
  2770. * calls to SSL_CTX_set_max_early_data() in order to use early data,
  2771. * eliminating the bandwidth-wasting early data in the case described
  2772. * above.
  2773. */
  2774. ret->max_early_data = 0;
  2775. /*
  2776. * Default recv_max_early_data is a fully loaded single record. Could be
  2777. * split across multiple records in practice. We set this differently to
  2778. * max_early_data so that, in the default case, we do not advertise any
  2779. * support for early_data, but if a client were to send us some (e.g.
  2780. * because of an old, stale ticket) then we will tolerate it and skip over
  2781. * it.
  2782. */
  2783. ret->recv_max_early_data = SSL3_RT_MAX_PLAIN_LENGTH;
  2784. /* By default we send two session tickets automatically in TLSv1.3 */
  2785. ret->num_tickets = 2;
  2786. ssl_ctx_system_config(ret);
  2787. return ret;
  2788. err:
  2789. SSLerr(SSL_F_SSL_CTX_NEW, ERR_R_MALLOC_FAILURE);
  2790. err2:
  2791. SSL_CTX_free(ret);
  2792. return NULL;
  2793. }
  2794. int SSL_CTX_up_ref(SSL_CTX *ctx)
  2795. {
  2796. int i;
  2797. if (CRYPTO_UP_REF(&ctx->references, &i, ctx->lock) <= 0)
  2798. return 0;
  2799. REF_PRINT_COUNT("SSL_CTX", ctx);
  2800. REF_ASSERT_ISNT(i < 2);
  2801. return ((i > 1) ? 1 : 0);
  2802. }
  2803. void SSL_CTX_free(SSL_CTX *a)
  2804. {
  2805. int i;
  2806. if (a == NULL)
  2807. return;
  2808. CRYPTO_DOWN_REF(&a->references, &i, a->lock);
  2809. REF_PRINT_COUNT("SSL_CTX", a);
  2810. if (i > 0)
  2811. return;
  2812. REF_ASSERT_ISNT(i < 0);
  2813. X509_VERIFY_PARAM_free(a->param);
  2814. dane_ctx_final(&a->dane);
  2815. /*
  2816. * Free internal session cache. However: the remove_cb() may reference
  2817. * the ex_data of SSL_CTX, thus the ex_data store can only be removed
  2818. * after the sessions were flushed.
  2819. * As the ex_data handling routines might also touch the session cache,
  2820. * the most secure solution seems to be: empty (flush) the cache, then
  2821. * free ex_data, then finally free the cache.
  2822. * (See ticket [openssl.org #212].)
  2823. */
  2824. if (a->sessions != NULL)
  2825. SSL_CTX_flush_sessions(a, 0);
  2826. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_SSL_CTX, a, &a->ex_data);
  2827. lh_SSL_SESSION_free(a->sessions);
  2828. X509_STORE_free(a->cert_store);
  2829. #ifndef OPENSSL_NO_CT
  2830. CTLOG_STORE_free(a->ctlog_store);
  2831. #endif
  2832. sk_SSL_CIPHER_free(a->cipher_list);
  2833. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  2834. sk_SSL_CIPHER_free(a->tls13_ciphersuites);
  2835. ssl_cert_free(a->cert);
  2836. sk_X509_NAME_pop_free(a->ca_names, X509_NAME_free);
  2837. sk_X509_NAME_pop_free(a->client_ca_names, X509_NAME_free);
  2838. sk_X509_pop_free(a->extra_certs, X509_free);
  2839. a->comp_methods = NULL;
  2840. #ifndef OPENSSL_NO_SRTP
  2841. sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
  2842. #endif
  2843. #ifndef OPENSSL_NO_SRP
  2844. SSL_CTX_SRP_CTX_free(a);
  2845. #endif
  2846. #ifndef OPENSSL_NO_ENGINE
  2847. ENGINE_finish(a->client_cert_engine);
  2848. #endif
  2849. #ifndef OPENSSL_NO_EC
  2850. OPENSSL_free(a->ext.ecpointformats);
  2851. #endif
  2852. OPENSSL_free(a->ext.supportedgroups);
  2853. OPENSSL_free(a->ext.alpn);
  2854. OPENSSL_secure_free(a->ext.secure);
  2855. CRYPTO_THREAD_lock_free(a->lock);
  2856. OPENSSL_free(a);
  2857. }
  2858. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb)
  2859. {
  2860. ctx->default_passwd_callback = cb;
  2861. }
  2862. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u)
  2863. {
  2864. ctx->default_passwd_callback_userdata = u;
  2865. }
  2866. pem_password_cb *SSL_CTX_get_default_passwd_cb(SSL_CTX *ctx)
  2867. {
  2868. return ctx->default_passwd_callback;
  2869. }
  2870. void *SSL_CTX_get_default_passwd_cb_userdata(SSL_CTX *ctx)
  2871. {
  2872. return ctx->default_passwd_callback_userdata;
  2873. }
  2874. void SSL_set_default_passwd_cb(SSL *s, pem_password_cb *cb)
  2875. {
  2876. s->default_passwd_callback = cb;
  2877. }
  2878. void SSL_set_default_passwd_cb_userdata(SSL *s, void *u)
  2879. {
  2880. s->default_passwd_callback_userdata = u;
  2881. }
  2882. pem_password_cb *SSL_get_default_passwd_cb(SSL *s)
  2883. {
  2884. return s->default_passwd_callback;
  2885. }
  2886. void *SSL_get_default_passwd_cb_userdata(SSL *s)
  2887. {
  2888. return s->default_passwd_callback_userdata;
  2889. }
  2890. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  2891. int (*cb) (X509_STORE_CTX *, void *),
  2892. void *arg)
  2893. {
  2894. ctx->app_verify_callback = cb;
  2895. ctx->app_verify_arg = arg;
  2896. }
  2897. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  2898. int (*cb) (int, X509_STORE_CTX *))
  2899. {
  2900. ctx->verify_mode = mode;
  2901. ctx->default_verify_callback = cb;
  2902. }
  2903. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth)
  2904. {
  2905. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  2906. }
  2907. void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb) (SSL *ssl, void *arg), void *arg)
  2908. {
  2909. ssl_cert_set_cert_cb(c->cert, cb, arg);
  2910. }
  2911. void SSL_set_cert_cb(SSL *s, int (*cb) (SSL *ssl, void *arg), void *arg)
  2912. {
  2913. ssl_cert_set_cert_cb(s->cert, cb, arg);
  2914. }
  2915. void ssl_set_masks(SSL *s)
  2916. {
  2917. CERT *c = s->cert;
  2918. uint32_t *pvalid = s->s3.tmp.valid_flags;
  2919. int rsa_enc, rsa_sign, dh_tmp, dsa_sign;
  2920. unsigned long mask_k, mask_a;
  2921. #ifndef OPENSSL_NO_EC
  2922. int have_ecc_cert, ecdsa_ok;
  2923. #endif
  2924. if (c == NULL)
  2925. return;
  2926. #ifndef OPENSSL_NO_DH
  2927. dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL || c->dh_tmp_auto);
  2928. #else
  2929. dh_tmp = 0;
  2930. #endif
  2931. rsa_enc = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  2932. rsa_sign = pvalid[SSL_PKEY_RSA] & CERT_PKEY_VALID;
  2933. dsa_sign = pvalid[SSL_PKEY_DSA_SIGN] & CERT_PKEY_VALID;
  2934. #ifndef OPENSSL_NO_EC
  2935. have_ecc_cert = pvalid[SSL_PKEY_ECC] & CERT_PKEY_VALID;
  2936. #endif
  2937. mask_k = 0;
  2938. mask_a = 0;
  2939. OSSL_TRACE4(TLS_CIPHER, "dh_tmp=%d rsa_enc=%d rsa_sign=%d dsa_sign=%d\n",
  2940. dh_tmp, rsa_enc, rsa_sign, dsa_sign);
  2941. #ifndef OPENSSL_NO_GOST
  2942. if (ssl_has_cert(s, SSL_PKEY_GOST12_512)) {
  2943. mask_k |= SSL_kGOST;
  2944. mask_a |= SSL_aGOST12;
  2945. }
  2946. if (ssl_has_cert(s, SSL_PKEY_GOST12_256)) {
  2947. mask_k |= SSL_kGOST;
  2948. mask_a |= SSL_aGOST12;
  2949. }
  2950. if (ssl_has_cert(s, SSL_PKEY_GOST01)) {
  2951. mask_k |= SSL_kGOST;
  2952. mask_a |= SSL_aGOST01;
  2953. }
  2954. #endif
  2955. if (rsa_enc)
  2956. mask_k |= SSL_kRSA;
  2957. if (dh_tmp)
  2958. mask_k |= SSL_kDHE;
  2959. /*
  2960. * If we only have an RSA-PSS certificate allow RSA authentication
  2961. * if TLS 1.2 and peer supports it.
  2962. */
  2963. if (rsa_enc || rsa_sign || (ssl_has_cert(s, SSL_PKEY_RSA_PSS_SIGN)
  2964. && pvalid[SSL_PKEY_RSA_PSS_SIGN] & CERT_PKEY_EXPLICIT_SIGN
  2965. && TLS1_get_version(s) == TLS1_2_VERSION))
  2966. mask_a |= SSL_aRSA;
  2967. if (dsa_sign) {
  2968. mask_a |= SSL_aDSS;
  2969. }
  2970. mask_a |= SSL_aNULL;
  2971. /*
  2972. * An ECC certificate may be usable for ECDH and/or ECDSA cipher suites
  2973. * depending on the key usage extension.
  2974. */
  2975. #ifndef OPENSSL_NO_EC
  2976. if (have_ecc_cert) {
  2977. uint32_t ex_kusage;
  2978. ex_kusage = X509_get_key_usage(c->pkeys[SSL_PKEY_ECC].x509);
  2979. ecdsa_ok = ex_kusage & X509v3_KU_DIGITAL_SIGNATURE;
  2980. if (!(pvalid[SSL_PKEY_ECC] & CERT_PKEY_SIGN))
  2981. ecdsa_ok = 0;
  2982. if (ecdsa_ok)
  2983. mask_a |= SSL_aECDSA;
  2984. }
  2985. /* Allow Ed25519 for TLS 1.2 if peer supports it */
  2986. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED25519)
  2987. && pvalid[SSL_PKEY_ED25519] & CERT_PKEY_EXPLICIT_SIGN
  2988. && TLS1_get_version(s) == TLS1_2_VERSION)
  2989. mask_a |= SSL_aECDSA;
  2990. /* Allow Ed448 for TLS 1.2 if peer supports it */
  2991. if (!(mask_a & SSL_aECDSA) && ssl_has_cert(s, SSL_PKEY_ED448)
  2992. && pvalid[SSL_PKEY_ED448] & CERT_PKEY_EXPLICIT_SIGN
  2993. && TLS1_get_version(s) == TLS1_2_VERSION)
  2994. mask_a |= SSL_aECDSA;
  2995. #endif
  2996. #ifndef OPENSSL_NO_EC
  2997. mask_k |= SSL_kECDHE;
  2998. #endif
  2999. #ifndef OPENSSL_NO_PSK
  3000. mask_k |= SSL_kPSK;
  3001. mask_a |= SSL_aPSK;
  3002. if (mask_k & SSL_kRSA)
  3003. mask_k |= SSL_kRSAPSK;
  3004. if (mask_k & SSL_kDHE)
  3005. mask_k |= SSL_kDHEPSK;
  3006. if (mask_k & SSL_kECDHE)
  3007. mask_k |= SSL_kECDHEPSK;
  3008. #endif
  3009. s->s3.tmp.mask_k = mask_k;
  3010. s->s3.tmp.mask_a = mask_a;
  3011. }
  3012. #ifndef OPENSSL_NO_EC
  3013. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s)
  3014. {
  3015. if (s->s3.tmp.new_cipher->algorithm_auth & SSL_aECDSA) {
  3016. /* key usage, if present, must allow signing */
  3017. if (!(X509_get_key_usage(x) & X509v3_KU_DIGITAL_SIGNATURE)) {
  3018. SSLerr(SSL_F_SSL_CHECK_SRVR_ECC_CERT_AND_ALG,
  3019. SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  3020. return 0;
  3021. }
  3022. }
  3023. return 1; /* all checks are ok */
  3024. }
  3025. #endif
  3026. int ssl_get_server_cert_serverinfo(SSL *s, const unsigned char **serverinfo,
  3027. size_t *serverinfo_length)
  3028. {
  3029. CERT_PKEY *cpk = s->s3.tmp.cert;
  3030. *serverinfo_length = 0;
  3031. if (cpk == NULL || cpk->serverinfo == NULL)
  3032. return 0;
  3033. *serverinfo = cpk->serverinfo;
  3034. *serverinfo_length = cpk->serverinfo_length;
  3035. return 1;
  3036. }
  3037. void ssl_update_cache(SSL *s, int mode)
  3038. {
  3039. int i;
  3040. /*
  3041. * If the session_id_length is 0, we are not supposed to cache it, and it
  3042. * would be rather hard to do anyway :-)
  3043. */
  3044. if (s->session->session_id_length == 0)
  3045. return;
  3046. /*
  3047. * If sid_ctx_length is 0 there is no specific application context
  3048. * associated with this session, so when we try to resume it and
  3049. * SSL_VERIFY_PEER is requested to verify the client identity, we have no
  3050. * indication that this is actually a session for the proper application
  3051. * context, and the *handshake* will fail, not just the resumption attempt.
  3052. * Do not cache (on the server) these sessions that are not resumable
  3053. * (clients can set SSL_VERIFY_PEER without needing a sid_ctx set).
  3054. */
  3055. if (s->server && s->session->sid_ctx_length == 0
  3056. && (s->verify_mode & SSL_VERIFY_PEER) != 0)
  3057. return;
  3058. i = s->session_ctx->session_cache_mode;
  3059. if ((i & mode) != 0
  3060. && (!s->hit || SSL_IS_TLS13(s))) {
  3061. /*
  3062. * Add the session to the internal cache. In server side TLSv1.3 we
  3063. * normally don't do this because by default it's a full stateless ticket
  3064. * with only a dummy session id so there is no reason to cache it,
  3065. * unless:
  3066. * - we are doing early_data, in which case we cache so that we can
  3067. * detect replays
  3068. * - the application has set a remove_session_cb so needs to know about
  3069. * session timeout events
  3070. * - SSL_OP_NO_TICKET is set in which case it is a stateful ticket
  3071. */
  3072. if ((i & SSL_SESS_CACHE_NO_INTERNAL_STORE) == 0
  3073. && (!SSL_IS_TLS13(s)
  3074. || !s->server
  3075. || (s->max_early_data > 0
  3076. && (s->options & SSL_OP_NO_ANTI_REPLAY) == 0)
  3077. || s->session_ctx->remove_session_cb != NULL
  3078. || (s->options & SSL_OP_NO_TICKET) != 0))
  3079. SSL_CTX_add_session(s->session_ctx, s->session);
  3080. /*
  3081. * Add the session to the external cache. We do this even in server side
  3082. * TLSv1.3 without early data because some applications just want to
  3083. * know about the creation of a session and aren't doing a full cache.
  3084. */
  3085. if (s->session_ctx->new_session_cb != NULL) {
  3086. SSL_SESSION_up_ref(s->session);
  3087. if (!s->session_ctx->new_session_cb(s, s->session))
  3088. SSL_SESSION_free(s->session);
  3089. }
  3090. }
  3091. /* auto flush every 255 connections */
  3092. if ((!(i & SSL_SESS_CACHE_NO_AUTO_CLEAR)) && ((i & mode) == mode)) {
  3093. TSAN_QUALIFIER int *stat;
  3094. if (mode & SSL_SESS_CACHE_CLIENT)
  3095. stat = &s->session_ctx->stats.sess_connect_good;
  3096. else
  3097. stat = &s->session_ctx->stats.sess_accept_good;
  3098. if ((tsan_load(stat) & 0xff) == 0xff)
  3099. SSL_CTX_flush_sessions(s->session_ctx, (unsigned long)time(NULL));
  3100. }
  3101. }
  3102. const SSL_METHOD *SSL_CTX_get_ssl_method(const SSL_CTX *ctx)
  3103. {
  3104. return ctx->method;
  3105. }
  3106. const SSL_METHOD *SSL_get_ssl_method(const SSL *s)
  3107. {
  3108. return s->method;
  3109. }
  3110. int SSL_set_ssl_method(SSL *s, const SSL_METHOD *meth)
  3111. {
  3112. int ret = 1;
  3113. if (s->method != meth) {
  3114. const SSL_METHOD *sm = s->method;
  3115. int (*hf) (SSL *) = s->handshake_func;
  3116. if (sm->version == meth->version)
  3117. s->method = meth;
  3118. else {
  3119. sm->ssl_free(s);
  3120. s->method = meth;
  3121. ret = s->method->ssl_new(s);
  3122. }
  3123. if (hf == sm->ssl_connect)
  3124. s->handshake_func = meth->ssl_connect;
  3125. else if (hf == sm->ssl_accept)
  3126. s->handshake_func = meth->ssl_accept;
  3127. }
  3128. return ret;
  3129. }
  3130. int SSL_get_error(const SSL *s, int i)
  3131. {
  3132. int reason;
  3133. unsigned long l;
  3134. BIO *bio;
  3135. if (i > 0)
  3136. return SSL_ERROR_NONE;
  3137. /*
  3138. * Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  3139. * where we do encode the error
  3140. */
  3141. if ((l = ERR_peek_error()) != 0) {
  3142. if (ERR_GET_LIB(l) == ERR_LIB_SYS)
  3143. return SSL_ERROR_SYSCALL;
  3144. else
  3145. return SSL_ERROR_SSL;
  3146. }
  3147. if (SSL_want_read(s)) {
  3148. bio = SSL_get_rbio(s);
  3149. if (BIO_should_read(bio))
  3150. return SSL_ERROR_WANT_READ;
  3151. else if (BIO_should_write(bio))
  3152. /*
  3153. * This one doesn't make too much sense ... We never try to write
  3154. * to the rbio, and an application program where rbio and wbio
  3155. * are separate couldn't even know what it should wait for.
  3156. * However if we ever set s->rwstate incorrectly (so that we have
  3157. * SSL_want_read(s) instead of SSL_want_write(s)) and rbio and
  3158. * wbio *are* the same, this test works around that bug; so it
  3159. * might be safer to keep it.
  3160. */
  3161. return SSL_ERROR_WANT_WRITE;
  3162. else if (BIO_should_io_special(bio)) {
  3163. reason = BIO_get_retry_reason(bio);
  3164. if (reason == BIO_RR_CONNECT)
  3165. return SSL_ERROR_WANT_CONNECT;
  3166. else if (reason == BIO_RR_ACCEPT)
  3167. return SSL_ERROR_WANT_ACCEPT;
  3168. else
  3169. return SSL_ERROR_SYSCALL; /* unknown */
  3170. }
  3171. }
  3172. if (SSL_want_write(s)) {
  3173. /* Access wbio directly - in order to use the buffered bio if present */
  3174. bio = s->wbio;
  3175. if (BIO_should_write(bio))
  3176. return SSL_ERROR_WANT_WRITE;
  3177. else if (BIO_should_read(bio))
  3178. /*
  3179. * See above (SSL_want_read(s) with BIO_should_write(bio))
  3180. */
  3181. return SSL_ERROR_WANT_READ;
  3182. else if (BIO_should_io_special(bio)) {
  3183. reason = BIO_get_retry_reason(bio);
  3184. if (reason == BIO_RR_CONNECT)
  3185. return SSL_ERROR_WANT_CONNECT;
  3186. else if (reason == BIO_RR_ACCEPT)
  3187. return SSL_ERROR_WANT_ACCEPT;
  3188. else
  3189. return SSL_ERROR_SYSCALL;
  3190. }
  3191. }
  3192. if (SSL_want_x509_lookup(s))
  3193. return SSL_ERROR_WANT_X509_LOOKUP;
  3194. if (SSL_want_async(s))
  3195. return SSL_ERROR_WANT_ASYNC;
  3196. if (SSL_want_async_job(s))
  3197. return SSL_ERROR_WANT_ASYNC_JOB;
  3198. if (SSL_want_client_hello_cb(s))
  3199. return SSL_ERROR_WANT_CLIENT_HELLO_CB;
  3200. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  3201. (s->s3.warn_alert == SSL_AD_CLOSE_NOTIFY))
  3202. return SSL_ERROR_ZERO_RETURN;
  3203. return SSL_ERROR_SYSCALL;
  3204. }
  3205. static int ssl_do_handshake_intern(void *vargs)
  3206. {
  3207. struct ssl_async_args *args;
  3208. SSL *s;
  3209. args = (struct ssl_async_args *)vargs;
  3210. s = args->s;
  3211. return s->handshake_func(s);
  3212. }
  3213. int SSL_do_handshake(SSL *s)
  3214. {
  3215. int ret = 1;
  3216. if (s->handshake_func == NULL) {
  3217. SSLerr(SSL_F_SSL_DO_HANDSHAKE, SSL_R_CONNECTION_TYPE_NOT_SET);
  3218. return -1;
  3219. }
  3220. ossl_statem_check_finish_init(s, -1);
  3221. s->method->ssl_renegotiate_check(s, 0);
  3222. if (SSL_in_init(s) || SSL_in_before(s)) {
  3223. if ((s->mode & SSL_MODE_ASYNC) && ASYNC_get_current_job() == NULL) {
  3224. struct ssl_async_args args;
  3225. args.s = s;
  3226. ret = ssl_start_async_job(s, &args, ssl_do_handshake_intern);
  3227. } else {
  3228. ret = s->handshake_func(s);
  3229. }
  3230. }
  3231. return ret;
  3232. }
  3233. void SSL_set_accept_state(SSL *s)
  3234. {
  3235. s->server = 1;
  3236. s->shutdown = 0;
  3237. ossl_statem_clear(s);
  3238. s->handshake_func = s->method->ssl_accept;
  3239. clear_ciphers(s);
  3240. }
  3241. void SSL_set_connect_state(SSL *s)
  3242. {
  3243. s->server = 0;
  3244. s->shutdown = 0;
  3245. ossl_statem_clear(s);
  3246. s->handshake_func = s->method->ssl_connect;
  3247. clear_ciphers(s);
  3248. }
  3249. int ssl_undefined_function(SSL *s)
  3250. {
  3251. SSLerr(SSL_F_SSL_UNDEFINED_FUNCTION, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3252. return 0;
  3253. }
  3254. int ssl_undefined_void_function(void)
  3255. {
  3256. SSLerr(SSL_F_SSL_UNDEFINED_VOID_FUNCTION,
  3257. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3258. return 0;
  3259. }
  3260. int ssl_undefined_const_function(const SSL *s)
  3261. {
  3262. return 0;
  3263. }
  3264. const SSL_METHOD *ssl_bad_method(int ver)
  3265. {
  3266. SSLerr(SSL_F_SSL_BAD_METHOD, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  3267. return NULL;
  3268. }
  3269. const char *ssl_protocol_to_string(int version)
  3270. {
  3271. switch(version)
  3272. {
  3273. case TLS1_3_VERSION:
  3274. return "TLSv1.3";
  3275. case TLS1_2_VERSION:
  3276. return "TLSv1.2";
  3277. case TLS1_1_VERSION:
  3278. return "TLSv1.1";
  3279. case TLS1_VERSION:
  3280. return "TLSv1";
  3281. case SSL3_VERSION:
  3282. return "SSLv3";
  3283. case DTLS1_BAD_VER:
  3284. return "DTLSv0.9";
  3285. case DTLS1_VERSION:
  3286. return "DTLSv1";
  3287. case DTLS1_2_VERSION:
  3288. return "DTLSv1.2";
  3289. default:
  3290. return "unknown";
  3291. }
  3292. }
  3293. const char *SSL_get_version(const SSL *s)
  3294. {
  3295. return ssl_protocol_to_string(s->version);
  3296. }
  3297. static int dup_ca_names(STACK_OF(X509_NAME) **dst, STACK_OF(X509_NAME) *src)
  3298. {
  3299. STACK_OF(X509_NAME) *sk;
  3300. X509_NAME *xn;
  3301. int i;
  3302. if (src == NULL) {
  3303. *dst = NULL;
  3304. return 1;
  3305. }
  3306. if ((sk = sk_X509_NAME_new_null()) == NULL)
  3307. return 0;
  3308. for (i = 0; i < sk_X509_NAME_num(src); i++) {
  3309. xn = X509_NAME_dup(sk_X509_NAME_value(src, i));
  3310. if (xn == NULL) {
  3311. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  3312. return 0;
  3313. }
  3314. if (sk_X509_NAME_insert(sk, xn, i) == 0) {
  3315. X509_NAME_free(xn);
  3316. sk_X509_NAME_pop_free(sk, X509_NAME_free);
  3317. return 0;
  3318. }
  3319. }
  3320. *dst = sk;
  3321. return 1;
  3322. }
  3323. SSL *SSL_dup(SSL *s)
  3324. {
  3325. SSL *ret;
  3326. int i;
  3327. /* If we're not quiescent, just up_ref! */
  3328. if (!SSL_in_init(s) || !SSL_in_before(s)) {
  3329. CRYPTO_UP_REF(&s->references, &i, s->lock);
  3330. return s;
  3331. }
  3332. /*
  3333. * Otherwise, copy configuration state, and session if set.
  3334. */
  3335. if ((ret = SSL_new(SSL_get_SSL_CTX(s))) == NULL)
  3336. return NULL;
  3337. if (s->session != NULL) {
  3338. /*
  3339. * Arranges to share the same session via up_ref. This "copies"
  3340. * session-id, SSL_METHOD, sid_ctx, and 'cert'
  3341. */
  3342. if (!SSL_copy_session_id(ret, s))
  3343. goto err;
  3344. } else {
  3345. /*
  3346. * No session has been established yet, so we have to expect that
  3347. * s->cert or ret->cert will be changed later -- they should not both
  3348. * point to the same object, and thus we can't use
  3349. * SSL_copy_session_id.
  3350. */
  3351. if (!SSL_set_ssl_method(ret, s->method))
  3352. goto err;
  3353. if (s->cert != NULL) {
  3354. ssl_cert_free(ret->cert);
  3355. ret->cert = ssl_cert_dup(s->cert);
  3356. if (ret->cert == NULL)
  3357. goto err;
  3358. }
  3359. if (!SSL_set_session_id_context(ret, s->sid_ctx,
  3360. (int)s->sid_ctx_length))
  3361. goto err;
  3362. }
  3363. if (!ssl_dane_dup(ret, s))
  3364. goto err;
  3365. ret->version = s->version;
  3366. ret->options = s->options;
  3367. ret->mode = s->mode;
  3368. SSL_set_max_cert_list(ret, SSL_get_max_cert_list(s));
  3369. SSL_set_read_ahead(ret, SSL_get_read_ahead(s));
  3370. ret->msg_callback = s->msg_callback;
  3371. ret->msg_callback_arg = s->msg_callback_arg;
  3372. SSL_set_verify(ret, SSL_get_verify_mode(s), SSL_get_verify_callback(s));
  3373. SSL_set_verify_depth(ret, SSL_get_verify_depth(s));
  3374. ret->generate_session_id = s->generate_session_id;
  3375. SSL_set_info_callback(ret, SSL_get_info_callback(s));
  3376. /* copy app data, a little dangerous perhaps */
  3377. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_SSL, &ret->ex_data, &s->ex_data))
  3378. goto err;
  3379. /* setup rbio, and wbio */
  3380. if (s->rbio != NULL) {
  3381. if (!BIO_dup_state(s->rbio, (char *)&ret->rbio))
  3382. goto err;
  3383. }
  3384. if (s->wbio != NULL) {
  3385. if (s->wbio != s->rbio) {
  3386. if (!BIO_dup_state(s->wbio, (char *)&ret->wbio))
  3387. goto err;
  3388. } else {
  3389. BIO_up_ref(ret->rbio);
  3390. ret->wbio = ret->rbio;
  3391. }
  3392. }
  3393. ret->server = s->server;
  3394. if (s->handshake_func) {
  3395. if (s->server)
  3396. SSL_set_accept_state(ret);
  3397. else
  3398. SSL_set_connect_state(ret);
  3399. }
  3400. ret->shutdown = s->shutdown;
  3401. ret->hit = s->hit;
  3402. ret->default_passwd_callback = s->default_passwd_callback;
  3403. ret->default_passwd_callback_userdata = s->default_passwd_callback_userdata;
  3404. X509_VERIFY_PARAM_inherit(ret->param, s->param);
  3405. /* dup the cipher_list and cipher_list_by_id stacks */
  3406. if (s->cipher_list != NULL) {
  3407. if ((ret->cipher_list = sk_SSL_CIPHER_dup(s->cipher_list)) == NULL)
  3408. goto err;
  3409. }
  3410. if (s->cipher_list_by_id != NULL)
  3411. if ((ret->cipher_list_by_id = sk_SSL_CIPHER_dup(s->cipher_list_by_id))
  3412. == NULL)
  3413. goto err;
  3414. /* Dup the client_CA list */
  3415. if (!dup_ca_names(&ret->ca_names, s->ca_names)
  3416. || !dup_ca_names(&ret->client_ca_names, s->client_ca_names))
  3417. goto err;
  3418. return ret;
  3419. err:
  3420. SSL_free(ret);
  3421. return NULL;
  3422. }
  3423. void ssl_clear_cipher_ctx(SSL *s)
  3424. {
  3425. if (s->enc_read_ctx != NULL) {
  3426. EVP_CIPHER_CTX_free(s->enc_read_ctx);
  3427. s->enc_read_ctx = NULL;
  3428. }
  3429. if (s->enc_write_ctx != NULL) {
  3430. EVP_CIPHER_CTX_free(s->enc_write_ctx);
  3431. s->enc_write_ctx = NULL;
  3432. }
  3433. #ifndef OPENSSL_NO_COMP
  3434. COMP_CTX_free(s->expand);
  3435. s->expand = NULL;
  3436. COMP_CTX_free(s->compress);
  3437. s->compress = NULL;
  3438. #endif
  3439. }
  3440. X509 *SSL_get_certificate(const SSL *s)
  3441. {
  3442. if (s->cert != NULL)
  3443. return s->cert->key->x509;
  3444. else
  3445. return NULL;
  3446. }
  3447. EVP_PKEY *SSL_get_privatekey(const SSL *s)
  3448. {
  3449. if (s->cert != NULL)
  3450. return s->cert->key->privatekey;
  3451. else
  3452. return NULL;
  3453. }
  3454. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx)
  3455. {
  3456. if (ctx->cert != NULL)
  3457. return ctx->cert->key->x509;
  3458. else
  3459. return NULL;
  3460. }
  3461. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx)
  3462. {
  3463. if (ctx->cert != NULL)
  3464. return ctx->cert->key->privatekey;
  3465. else
  3466. return NULL;
  3467. }
  3468. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s)
  3469. {
  3470. if ((s->session != NULL) && (s->session->cipher != NULL))
  3471. return s->session->cipher;
  3472. return NULL;
  3473. }
  3474. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *s)
  3475. {
  3476. return s->s3.tmp.new_cipher;
  3477. }
  3478. const COMP_METHOD *SSL_get_current_compression(const SSL *s)
  3479. {
  3480. #ifndef OPENSSL_NO_COMP
  3481. return s->compress ? COMP_CTX_get_method(s->compress) : NULL;
  3482. #else
  3483. return NULL;
  3484. #endif
  3485. }
  3486. const COMP_METHOD *SSL_get_current_expansion(const SSL *s)
  3487. {
  3488. #ifndef OPENSSL_NO_COMP
  3489. return s->expand ? COMP_CTX_get_method(s->expand) : NULL;
  3490. #else
  3491. return NULL;
  3492. #endif
  3493. }
  3494. int ssl_init_wbio_buffer(SSL *s)
  3495. {
  3496. BIO *bbio;
  3497. if (s->bbio != NULL) {
  3498. /* Already buffered. */
  3499. return 1;
  3500. }
  3501. bbio = BIO_new(BIO_f_buffer());
  3502. if (bbio == NULL || !BIO_set_read_buffer_size(bbio, 1)) {
  3503. BIO_free(bbio);
  3504. SSLerr(SSL_F_SSL_INIT_WBIO_BUFFER, ERR_R_BUF_LIB);
  3505. return 0;
  3506. }
  3507. s->bbio = bbio;
  3508. s->wbio = BIO_push(bbio, s->wbio);
  3509. return 1;
  3510. }
  3511. int ssl_free_wbio_buffer(SSL *s)
  3512. {
  3513. /* callers ensure s is never null */
  3514. if (s->bbio == NULL)
  3515. return 1;
  3516. s->wbio = BIO_pop(s->wbio);
  3517. BIO_free(s->bbio);
  3518. s->bbio = NULL;
  3519. return 1;
  3520. }
  3521. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode)
  3522. {
  3523. ctx->quiet_shutdown = mode;
  3524. }
  3525. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx)
  3526. {
  3527. return ctx->quiet_shutdown;
  3528. }
  3529. void SSL_set_quiet_shutdown(SSL *s, int mode)
  3530. {
  3531. s->quiet_shutdown = mode;
  3532. }
  3533. int SSL_get_quiet_shutdown(const SSL *s)
  3534. {
  3535. return s->quiet_shutdown;
  3536. }
  3537. void SSL_set_shutdown(SSL *s, int mode)
  3538. {
  3539. s->shutdown = mode;
  3540. }
  3541. int SSL_get_shutdown(const SSL *s)
  3542. {
  3543. return s->shutdown;
  3544. }
  3545. int SSL_version(const SSL *s)
  3546. {
  3547. return s->version;
  3548. }
  3549. int SSL_client_version(const SSL *s)
  3550. {
  3551. return s->client_version;
  3552. }
  3553. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl)
  3554. {
  3555. return ssl->ctx;
  3556. }
  3557. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx)
  3558. {
  3559. CERT *new_cert;
  3560. if (ssl->ctx == ctx)
  3561. return ssl->ctx;
  3562. if (ctx == NULL)
  3563. ctx = ssl->session_ctx;
  3564. new_cert = ssl_cert_dup(ctx->cert);
  3565. if (new_cert == NULL) {
  3566. return NULL;
  3567. }
  3568. if (!custom_exts_copy_flags(&new_cert->custext, &ssl->cert->custext)) {
  3569. ssl_cert_free(new_cert);
  3570. return NULL;
  3571. }
  3572. ssl_cert_free(ssl->cert);
  3573. ssl->cert = new_cert;
  3574. /*
  3575. * Program invariant: |sid_ctx| has fixed size (SSL_MAX_SID_CTX_LENGTH),
  3576. * so setter APIs must prevent invalid lengths from entering the system.
  3577. */
  3578. if (!ossl_assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx)))
  3579. return NULL;
  3580. /*
  3581. * If the session ID context matches that of the parent SSL_CTX,
  3582. * inherit it from the new SSL_CTX as well. If however the context does
  3583. * not match (i.e., it was set per-ssl with SSL_set_session_id_context),
  3584. * leave it unchanged.
  3585. */
  3586. if ((ssl->ctx != NULL) &&
  3587. (ssl->sid_ctx_length == ssl->ctx->sid_ctx_length) &&
  3588. (memcmp(ssl->sid_ctx, ssl->ctx->sid_ctx, ssl->sid_ctx_length) == 0)) {
  3589. ssl->sid_ctx_length = ctx->sid_ctx_length;
  3590. memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
  3591. }
  3592. SSL_CTX_up_ref(ctx);
  3593. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  3594. ssl->ctx = ctx;
  3595. return ssl->ctx;
  3596. }
  3597. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx)
  3598. {
  3599. return X509_STORE_set_default_paths(ctx->cert_store);
  3600. }
  3601. int SSL_CTX_set_default_verify_dir(SSL_CTX *ctx)
  3602. {
  3603. X509_LOOKUP *lookup;
  3604. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_hash_dir());
  3605. if (lookup == NULL)
  3606. return 0;
  3607. /* We ignore errors, in case the directory doesn't exist */
  3608. ERR_set_mark();
  3609. X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
  3610. ERR_pop_to_mark();
  3611. return 1;
  3612. }
  3613. int SSL_CTX_set_default_verify_file(SSL_CTX *ctx)
  3614. {
  3615. X509_LOOKUP *lookup;
  3616. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_file());
  3617. if (lookup == NULL)
  3618. return 0;
  3619. /* We ignore errors, in case the directory doesn't exist */
  3620. ERR_set_mark();
  3621. X509_LOOKUP_load_file(lookup, NULL, X509_FILETYPE_DEFAULT);
  3622. ERR_pop_to_mark();
  3623. return 1;
  3624. }
  3625. int SSL_CTX_set_default_verify_store(SSL_CTX *ctx)
  3626. {
  3627. X509_LOOKUP *lookup;
  3628. lookup = X509_STORE_add_lookup(ctx->cert_store, X509_LOOKUP_store());
  3629. if (lookup == NULL)
  3630. return 0;
  3631. /* We ignore errors, in case the directory doesn't exist */
  3632. ERR_set_mark();
  3633. X509_LOOKUP_add_store(lookup, NULL);
  3634. ERR_pop_to_mark();
  3635. return 1;
  3636. }
  3637. int SSL_CTX_load_verify_file(SSL_CTX *ctx, const char *CAfile)
  3638. {
  3639. return X509_STORE_load_file(ctx->cert_store, CAfile);
  3640. }
  3641. int SSL_CTX_load_verify_dir(SSL_CTX *ctx, const char *CApath)
  3642. {
  3643. return X509_STORE_load_path(ctx->cert_store, CApath);
  3644. }
  3645. int SSL_CTX_load_verify_store(SSL_CTX *ctx, const char *CAstore)
  3646. {
  3647. return X509_STORE_load_store(ctx->cert_store, CAstore);
  3648. }
  3649. #ifndef OPENSSL_NO_DEPRECATED_3_0
  3650. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  3651. const char *CApath)
  3652. {
  3653. if (CAfile == NULL && CApath == NULL)
  3654. return 0;
  3655. if (CAfile != NULL && !SSL_CTX_load_verify_file(ctx, CAfile))
  3656. return 0;
  3657. if (CApath != NULL && !SSL_CTX_load_verify_dir(ctx, CApath))
  3658. return 0;
  3659. return 1;
  3660. }
  3661. #endif
  3662. void SSL_set_info_callback(SSL *ssl,
  3663. void (*cb) (const SSL *ssl, int type, int val))
  3664. {
  3665. ssl->info_callback = cb;
  3666. }
  3667. /*
  3668. * One compiler (Diab DCC) doesn't like argument names in returned function
  3669. * pointer.
  3670. */
  3671. void (*SSL_get_info_callback(const SSL *ssl)) (const SSL * /* ssl */ ,
  3672. int /* type */ ,
  3673. int /* val */ ) {
  3674. return ssl->info_callback;
  3675. }
  3676. void SSL_set_verify_result(SSL *ssl, long arg)
  3677. {
  3678. ssl->verify_result = arg;
  3679. }
  3680. long SSL_get_verify_result(const SSL *ssl)
  3681. {
  3682. return ssl->verify_result;
  3683. }
  3684. size_t SSL_get_client_random(const SSL *ssl, unsigned char *out, size_t outlen)
  3685. {
  3686. if (outlen == 0)
  3687. return sizeof(ssl->s3.client_random);
  3688. if (outlen > sizeof(ssl->s3.client_random))
  3689. outlen = sizeof(ssl->s3.client_random);
  3690. memcpy(out, ssl->s3.client_random, outlen);
  3691. return outlen;
  3692. }
  3693. size_t SSL_get_server_random(const SSL *ssl, unsigned char *out, size_t outlen)
  3694. {
  3695. if (outlen == 0)
  3696. return sizeof(ssl->s3.server_random);
  3697. if (outlen > sizeof(ssl->s3.server_random))
  3698. outlen = sizeof(ssl->s3.server_random);
  3699. memcpy(out, ssl->s3.server_random, outlen);
  3700. return outlen;
  3701. }
  3702. size_t SSL_SESSION_get_master_key(const SSL_SESSION *session,
  3703. unsigned char *out, size_t outlen)
  3704. {
  3705. if (outlen == 0)
  3706. return session->master_key_length;
  3707. if (outlen > session->master_key_length)
  3708. outlen = session->master_key_length;
  3709. memcpy(out, session->master_key, outlen);
  3710. return outlen;
  3711. }
  3712. int SSL_SESSION_set1_master_key(SSL_SESSION *sess, const unsigned char *in,
  3713. size_t len)
  3714. {
  3715. if (len > sizeof(sess->master_key))
  3716. return 0;
  3717. memcpy(sess->master_key, in, len);
  3718. sess->master_key_length = len;
  3719. return 1;
  3720. }
  3721. int SSL_set_ex_data(SSL *s, int idx, void *arg)
  3722. {
  3723. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  3724. }
  3725. void *SSL_get_ex_data(const SSL *s, int idx)
  3726. {
  3727. return CRYPTO_get_ex_data(&s->ex_data, idx);
  3728. }
  3729. int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg)
  3730. {
  3731. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  3732. }
  3733. void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx)
  3734. {
  3735. return CRYPTO_get_ex_data(&s->ex_data, idx);
  3736. }
  3737. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx)
  3738. {
  3739. return ctx->cert_store;
  3740. }
  3741. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store)
  3742. {
  3743. X509_STORE_free(ctx->cert_store);
  3744. ctx->cert_store = store;
  3745. }
  3746. void SSL_CTX_set1_cert_store(SSL_CTX *ctx, X509_STORE *store)
  3747. {
  3748. if (store != NULL)
  3749. X509_STORE_up_ref(store);
  3750. SSL_CTX_set_cert_store(ctx, store);
  3751. }
  3752. int SSL_want(const SSL *s)
  3753. {
  3754. return s->rwstate;
  3755. }
  3756. /**
  3757. * \brief Set the callback for generating temporary DH keys.
  3758. * \param ctx the SSL context.
  3759. * \param dh the callback
  3760. */
  3761. #ifndef OPENSSL_NO_DH
  3762. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  3763. DH *(*dh) (SSL *ssl, int is_export,
  3764. int keylength))
  3765. {
  3766. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  3767. }
  3768. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh) (SSL *ssl, int is_export,
  3769. int keylength))
  3770. {
  3771. SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  3772. }
  3773. #endif
  3774. #ifndef OPENSSL_NO_PSK
  3775. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint)
  3776. {
  3777. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3778. SSLerr(SSL_F_SSL_CTX_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
  3779. return 0;
  3780. }
  3781. OPENSSL_free(ctx->cert->psk_identity_hint);
  3782. if (identity_hint != NULL) {
  3783. ctx->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3784. if (ctx->cert->psk_identity_hint == NULL)
  3785. return 0;
  3786. } else
  3787. ctx->cert->psk_identity_hint = NULL;
  3788. return 1;
  3789. }
  3790. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint)
  3791. {
  3792. if (s == NULL)
  3793. return 0;
  3794. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  3795. SSLerr(SSL_F_SSL_USE_PSK_IDENTITY_HINT, SSL_R_DATA_LENGTH_TOO_LONG);
  3796. return 0;
  3797. }
  3798. OPENSSL_free(s->cert->psk_identity_hint);
  3799. if (identity_hint != NULL) {
  3800. s->cert->psk_identity_hint = OPENSSL_strdup(identity_hint);
  3801. if (s->cert->psk_identity_hint == NULL)
  3802. return 0;
  3803. } else
  3804. s->cert->psk_identity_hint = NULL;
  3805. return 1;
  3806. }
  3807. const char *SSL_get_psk_identity_hint(const SSL *s)
  3808. {
  3809. if (s == NULL || s->session == NULL)
  3810. return NULL;
  3811. return s->session->psk_identity_hint;
  3812. }
  3813. const char *SSL_get_psk_identity(const SSL *s)
  3814. {
  3815. if (s == NULL || s->session == NULL)
  3816. return NULL;
  3817. return s->session->psk_identity;
  3818. }
  3819. void SSL_set_psk_client_callback(SSL *s, SSL_psk_client_cb_func cb)
  3820. {
  3821. s->psk_client_callback = cb;
  3822. }
  3823. void SSL_CTX_set_psk_client_callback(SSL_CTX *ctx, SSL_psk_client_cb_func cb)
  3824. {
  3825. ctx->psk_client_callback = cb;
  3826. }
  3827. void SSL_set_psk_server_callback(SSL *s, SSL_psk_server_cb_func cb)
  3828. {
  3829. s->psk_server_callback = cb;
  3830. }
  3831. void SSL_CTX_set_psk_server_callback(SSL_CTX *ctx, SSL_psk_server_cb_func cb)
  3832. {
  3833. ctx->psk_server_callback = cb;
  3834. }
  3835. #endif
  3836. void SSL_set_psk_find_session_callback(SSL *s, SSL_psk_find_session_cb_func cb)
  3837. {
  3838. s->psk_find_session_cb = cb;
  3839. }
  3840. void SSL_CTX_set_psk_find_session_callback(SSL_CTX *ctx,
  3841. SSL_psk_find_session_cb_func cb)
  3842. {
  3843. ctx->psk_find_session_cb = cb;
  3844. }
  3845. void SSL_set_psk_use_session_callback(SSL *s, SSL_psk_use_session_cb_func cb)
  3846. {
  3847. s->psk_use_session_cb = cb;
  3848. }
  3849. void SSL_CTX_set_psk_use_session_callback(SSL_CTX *ctx,
  3850. SSL_psk_use_session_cb_func cb)
  3851. {
  3852. ctx->psk_use_session_cb = cb;
  3853. }
  3854. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  3855. void (*cb) (int write_p, int version,
  3856. int content_type, const void *buf,
  3857. size_t len, SSL *ssl, void *arg))
  3858. {
  3859. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  3860. }
  3861. void SSL_set_msg_callback(SSL *ssl,
  3862. void (*cb) (int write_p, int version,
  3863. int content_type, const void *buf,
  3864. size_t len, SSL *ssl, void *arg))
  3865. {
  3866. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  3867. }
  3868. void SSL_CTX_set_not_resumable_session_callback(SSL_CTX *ctx,
  3869. int (*cb) (SSL *ssl,
  3870. int
  3871. is_forward_secure))
  3872. {
  3873. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  3874. (void (*)(void))cb);
  3875. }
  3876. void SSL_set_not_resumable_session_callback(SSL *ssl,
  3877. int (*cb) (SSL *ssl,
  3878. int is_forward_secure))
  3879. {
  3880. SSL_callback_ctrl(ssl, SSL_CTRL_SET_NOT_RESUMABLE_SESS_CB,
  3881. (void (*)(void))cb);
  3882. }
  3883. void SSL_CTX_set_record_padding_callback(SSL_CTX *ctx,
  3884. size_t (*cb) (SSL *ssl, int type,
  3885. size_t len, void *arg))
  3886. {
  3887. ctx->record_padding_cb = cb;
  3888. }
  3889. void SSL_CTX_set_record_padding_callback_arg(SSL_CTX *ctx, void *arg)
  3890. {
  3891. ctx->record_padding_arg = arg;
  3892. }
  3893. void *SSL_CTX_get_record_padding_callback_arg(const SSL_CTX *ctx)
  3894. {
  3895. return ctx->record_padding_arg;
  3896. }
  3897. int SSL_CTX_set_block_padding(SSL_CTX *ctx, size_t block_size)
  3898. {
  3899. /* block size of 0 or 1 is basically no padding */
  3900. if (block_size == 1)
  3901. ctx->block_padding = 0;
  3902. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  3903. ctx->block_padding = block_size;
  3904. else
  3905. return 0;
  3906. return 1;
  3907. }
  3908. void SSL_set_record_padding_callback(SSL *ssl,
  3909. size_t (*cb) (SSL *ssl, int type,
  3910. size_t len, void *arg))
  3911. {
  3912. ssl->record_padding_cb = cb;
  3913. }
  3914. void SSL_set_record_padding_callback_arg(SSL *ssl, void *arg)
  3915. {
  3916. ssl->record_padding_arg = arg;
  3917. }
  3918. void *SSL_get_record_padding_callback_arg(const SSL *ssl)
  3919. {
  3920. return ssl->record_padding_arg;
  3921. }
  3922. int SSL_set_block_padding(SSL *ssl, size_t block_size)
  3923. {
  3924. /* block size of 0 or 1 is basically no padding */
  3925. if (block_size == 1)
  3926. ssl->block_padding = 0;
  3927. else if (block_size <= SSL3_RT_MAX_PLAIN_LENGTH)
  3928. ssl->block_padding = block_size;
  3929. else
  3930. return 0;
  3931. return 1;
  3932. }
  3933. int SSL_set_num_tickets(SSL *s, size_t num_tickets)
  3934. {
  3935. s->num_tickets = num_tickets;
  3936. return 1;
  3937. }
  3938. size_t SSL_get_num_tickets(const SSL *s)
  3939. {
  3940. return s->num_tickets;
  3941. }
  3942. int SSL_CTX_set_num_tickets(SSL_CTX *ctx, size_t num_tickets)
  3943. {
  3944. ctx->num_tickets = num_tickets;
  3945. return 1;
  3946. }
  3947. size_t SSL_CTX_get_num_tickets(const SSL_CTX *ctx)
  3948. {
  3949. return ctx->num_tickets;
  3950. }
  3951. /*
  3952. * Allocates new EVP_MD_CTX and sets pointer to it into given pointer
  3953. * variable, freeing EVP_MD_CTX previously stored in that variable, if any.
  3954. * If EVP_MD pointer is passed, initializes ctx with this |md|.
  3955. * Returns the newly allocated ctx;
  3956. */
  3957. EVP_MD_CTX *ssl_replace_hash(EVP_MD_CTX **hash, const EVP_MD *md)
  3958. {
  3959. ssl_clear_hash_ctx(hash);
  3960. *hash = EVP_MD_CTX_new();
  3961. if (*hash == NULL || (md && EVP_DigestInit_ex(*hash, md, NULL) <= 0)) {
  3962. EVP_MD_CTX_free(*hash);
  3963. *hash = NULL;
  3964. return NULL;
  3965. }
  3966. return *hash;
  3967. }
  3968. void ssl_clear_hash_ctx(EVP_MD_CTX **hash)
  3969. {
  3970. EVP_MD_CTX_free(*hash);
  3971. *hash = NULL;
  3972. }
  3973. /* Retrieve handshake hashes */
  3974. int ssl_handshake_hash(SSL *s, unsigned char *out, size_t outlen,
  3975. size_t *hashlen)
  3976. {
  3977. EVP_MD_CTX *ctx = NULL;
  3978. EVP_MD_CTX *hdgst = s->s3.handshake_dgst;
  3979. int hashleni = EVP_MD_CTX_size(hdgst);
  3980. int ret = 0;
  3981. if (hashleni < 0 || (size_t)hashleni > outlen) {
  3982. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_HANDSHAKE_HASH,
  3983. ERR_R_INTERNAL_ERROR);
  3984. goto err;
  3985. }
  3986. ctx = EVP_MD_CTX_new();
  3987. if (ctx == NULL)
  3988. goto err;
  3989. if (!EVP_MD_CTX_copy_ex(ctx, hdgst)
  3990. || EVP_DigestFinal_ex(ctx, out, NULL) <= 0) {
  3991. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_HANDSHAKE_HASH,
  3992. ERR_R_INTERNAL_ERROR);
  3993. goto err;
  3994. }
  3995. *hashlen = hashleni;
  3996. ret = 1;
  3997. err:
  3998. EVP_MD_CTX_free(ctx);
  3999. return ret;
  4000. }
  4001. int SSL_session_reused(const SSL *s)
  4002. {
  4003. return s->hit;
  4004. }
  4005. int SSL_is_server(const SSL *s)
  4006. {
  4007. return s->server;
  4008. }
  4009. #ifndef OPENSSL_NO_DEPRECATED_1_1_0
  4010. void SSL_set_debug(SSL *s, int debug)
  4011. {
  4012. /* Old function was do-nothing anyway... */
  4013. (void)s;
  4014. (void)debug;
  4015. }
  4016. #endif
  4017. void SSL_set_security_level(SSL *s, int level)
  4018. {
  4019. s->cert->sec_level = level;
  4020. }
  4021. int SSL_get_security_level(const SSL *s)
  4022. {
  4023. return s->cert->sec_level;
  4024. }
  4025. void SSL_set_security_callback(SSL *s,
  4026. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  4027. int op, int bits, int nid,
  4028. void *other, void *ex))
  4029. {
  4030. s->cert->sec_cb = cb;
  4031. }
  4032. int (*SSL_get_security_callback(const SSL *s)) (const SSL *s,
  4033. const SSL_CTX *ctx, int op,
  4034. int bits, int nid, void *other,
  4035. void *ex) {
  4036. return s->cert->sec_cb;
  4037. }
  4038. void SSL_set0_security_ex_data(SSL *s, void *ex)
  4039. {
  4040. s->cert->sec_ex = ex;
  4041. }
  4042. void *SSL_get0_security_ex_data(const SSL *s)
  4043. {
  4044. return s->cert->sec_ex;
  4045. }
  4046. void SSL_CTX_set_security_level(SSL_CTX *ctx, int level)
  4047. {
  4048. ctx->cert->sec_level = level;
  4049. }
  4050. int SSL_CTX_get_security_level(const SSL_CTX *ctx)
  4051. {
  4052. return ctx->cert->sec_level;
  4053. }
  4054. void SSL_CTX_set_security_callback(SSL_CTX *ctx,
  4055. int (*cb) (const SSL *s, const SSL_CTX *ctx,
  4056. int op, int bits, int nid,
  4057. void *other, void *ex))
  4058. {
  4059. ctx->cert->sec_cb = cb;
  4060. }
  4061. int (*SSL_CTX_get_security_callback(const SSL_CTX *ctx)) (const SSL *s,
  4062. const SSL_CTX *ctx,
  4063. int op, int bits,
  4064. int nid,
  4065. void *other,
  4066. void *ex) {
  4067. return ctx->cert->sec_cb;
  4068. }
  4069. void SSL_CTX_set0_security_ex_data(SSL_CTX *ctx, void *ex)
  4070. {
  4071. ctx->cert->sec_ex = ex;
  4072. }
  4073. void *SSL_CTX_get0_security_ex_data(const SSL_CTX *ctx)
  4074. {
  4075. return ctx->cert->sec_ex;
  4076. }
  4077. /*
  4078. * Get/Set/Clear options in SSL_CTX or SSL, formerly macros, now functions that
  4079. * can return unsigned long, instead of the generic long return value from the
  4080. * control interface.
  4081. */
  4082. unsigned long SSL_CTX_get_options(const SSL_CTX *ctx)
  4083. {
  4084. return ctx->options;
  4085. }
  4086. unsigned long SSL_get_options(const SSL *s)
  4087. {
  4088. return s->options;
  4089. }
  4090. unsigned long SSL_CTX_set_options(SSL_CTX *ctx, unsigned long op)
  4091. {
  4092. return ctx->options |= op;
  4093. }
  4094. unsigned long SSL_set_options(SSL *s, unsigned long op)
  4095. {
  4096. return s->options |= op;
  4097. }
  4098. unsigned long SSL_CTX_clear_options(SSL_CTX *ctx, unsigned long op)
  4099. {
  4100. return ctx->options &= ~op;
  4101. }
  4102. unsigned long SSL_clear_options(SSL *s, unsigned long op)
  4103. {
  4104. return s->options &= ~op;
  4105. }
  4106. STACK_OF(X509) *SSL_get0_verified_chain(const SSL *s)
  4107. {
  4108. return s->verified_chain;
  4109. }
  4110. IMPLEMENT_OBJ_BSEARCH_GLOBAL_CMP_FN(SSL_CIPHER, SSL_CIPHER, ssl_cipher_id);
  4111. #ifndef OPENSSL_NO_CT
  4112. /*
  4113. * Moves SCTs from the |src| stack to the |dst| stack.
  4114. * The source of each SCT will be set to |origin|.
  4115. * If |dst| points to a NULL pointer, a new stack will be created and owned by
  4116. * the caller.
  4117. * Returns the number of SCTs moved, or a negative integer if an error occurs.
  4118. */
  4119. static int ct_move_scts(STACK_OF(SCT) **dst, STACK_OF(SCT) *src,
  4120. sct_source_t origin)
  4121. {
  4122. int scts_moved = 0;
  4123. SCT *sct = NULL;
  4124. if (*dst == NULL) {
  4125. *dst = sk_SCT_new_null();
  4126. if (*dst == NULL) {
  4127. SSLerr(SSL_F_CT_MOVE_SCTS, ERR_R_MALLOC_FAILURE);
  4128. goto err;
  4129. }
  4130. }
  4131. while ((sct = sk_SCT_pop(src)) != NULL) {
  4132. if (SCT_set_source(sct, origin) != 1)
  4133. goto err;
  4134. if (sk_SCT_push(*dst, sct) <= 0)
  4135. goto err;
  4136. scts_moved += 1;
  4137. }
  4138. return scts_moved;
  4139. err:
  4140. if (sct != NULL)
  4141. sk_SCT_push(src, sct); /* Put the SCT back */
  4142. return -1;
  4143. }
  4144. /*
  4145. * Look for data collected during ServerHello and parse if found.
  4146. * Returns the number of SCTs extracted.
  4147. */
  4148. static int ct_extract_tls_extension_scts(SSL *s)
  4149. {
  4150. int scts_extracted = 0;
  4151. if (s->ext.scts != NULL) {
  4152. const unsigned char *p = s->ext.scts;
  4153. STACK_OF(SCT) *scts = o2i_SCT_LIST(NULL, &p, s->ext.scts_len);
  4154. scts_extracted = ct_move_scts(&s->scts, scts, SCT_SOURCE_TLS_EXTENSION);
  4155. SCT_LIST_free(scts);
  4156. }
  4157. return scts_extracted;
  4158. }
  4159. /*
  4160. * Checks for an OCSP response and then attempts to extract any SCTs found if it
  4161. * contains an SCT X509 extension. They will be stored in |s->scts|.
  4162. * Returns:
  4163. * - The number of SCTs extracted, assuming an OCSP response exists.
  4164. * - 0 if no OCSP response exists or it contains no SCTs.
  4165. * - A negative integer if an error occurs.
  4166. */
  4167. static int ct_extract_ocsp_response_scts(SSL *s)
  4168. {
  4169. # ifndef OPENSSL_NO_OCSP
  4170. int scts_extracted = 0;
  4171. const unsigned char *p;
  4172. OCSP_BASICRESP *br = NULL;
  4173. OCSP_RESPONSE *rsp = NULL;
  4174. STACK_OF(SCT) *scts = NULL;
  4175. int i;
  4176. if (s->ext.ocsp.resp == NULL || s->ext.ocsp.resp_len == 0)
  4177. goto err;
  4178. p = s->ext.ocsp.resp;
  4179. rsp = d2i_OCSP_RESPONSE(NULL, &p, (int)s->ext.ocsp.resp_len);
  4180. if (rsp == NULL)
  4181. goto err;
  4182. br = OCSP_response_get1_basic(rsp);
  4183. if (br == NULL)
  4184. goto err;
  4185. for (i = 0; i < OCSP_resp_count(br); ++i) {
  4186. OCSP_SINGLERESP *single = OCSP_resp_get0(br, i);
  4187. if (single == NULL)
  4188. continue;
  4189. scts =
  4190. OCSP_SINGLERESP_get1_ext_d2i(single, NID_ct_cert_scts, NULL, NULL);
  4191. scts_extracted =
  4192. ct_move_scts(&s->scts, scts, SCT_SOURCE_OCSP_STAPLED_RESPONSE);
  4193. if (scts_extracted < 0)
  4194. goto err;
  4195. }
  4196. err:
  4197. SCT_LIST_free(scts);
  4198. OCSP_BASICRESP_free(br);
  4199. OCSP_RESPONSE_free(rsp);
  4200. return scts_extracted;
  4201. # else
  4202. /* Behave as if no OCSP response exists */
  4203. return 0;
  4204. # endif
  4205. }
  4206. /*
  4207. * Attempts to extract SCTs from the peer certificate.
  4208. * Return the number of SCTs extracted, or a negative integer if an error
  4209. * occurs.
  4210. */
  4211. static int ct_extract_x509v3_extension_scts(SSL *s)
  4212. {
  4213. int scts_extracted = 0;
  4214. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  4215. if (cert != NULL) {
  4216. STACK_OF(SCT) *scts =
  4217. X509_get_ext_d2i(cert, NID_ct_precert_scts, NULL, NULL);
  4218. scts_extracted =
  4219. ct_move_scts(&s->scts, scts, SCT_SOURCE_X509V3_EXTENSION);
  4220. SCT_LIST_free(scts);
  4221. }
  4222. return scts_extracted;
  4223. }
  4224. /*
  4225. * Attempts to find all received SCTs by checking TLS extensions, the OCSP
  4226. * response (if it exists) and X509v3 extensions in the certificate.
  4227. * Returns NULL if an error occurs.
  4228. */
  4229. const STACK_OF(SCT) *SSL_get0_peer_scts(SSL *s)
  4230. {
  4231. if (!s->scts_parsed) {
  4232. if (ct_extract_tls_extension_scts(s) < 0 ||
  4233. ct_extract_ocsp_response_scts(s) < 0 ||
  4234. ct_extract_x509v3_extension_scts(s) < 0)
  4235. goto err;
  4236. s->scts_parsed = 1;
  4237. }
  4238. return s->scts;
  4239. err:
  4240. return NULL;
  4241. }
  4242. static int ct_permissive(const CT_POLICY_EVAL_CTX * ctx,
  4243. const STACK_OF(SCT) *scts, void *unused_arg)
  4244. {
  4245. return 1;
  4246. }
  4247. static int ct_strict(const CT_POLICY_EVAL_CTX * ctx,
  4248. const STACK_OF(SCT) *scts, void *unused_arg)
  4249. {
  4250. int count = scts != NULL ? sk_SCT_num(scts) : 0;
  4251. int i;
  4252. for (i = 0; i < count; ++i) {
  4253. SCT *sct = sk_SCT_value(scts, i);
  4254. int status = SCT_get_validation_status(sct);
  4255. if (status == SCT_VALIDATION_STATUS_VALID)
  4256. return 1;
  4257. }
  4258. SSLerr(SSL_F_CT_STRICT, SSL_R_NO_VALID_SCTS);
  4259. return 0;
  4260. }
  4261. int SSL_set_ct_validation_callback(SSL *s, ssl_ct_validation_cb callback,
  4262. void *arg)
  4263. {
  4264. /*
  4265. * Since code exists that uses the custom extension handler for CT, look
  4266. * for this and throw an error if they have already registered to use CT.
  4267. */
  4268. if (callback != NULL && SSL_CTX_has_client_custom_ext(s->ctx,
  4269. TLSEXT_TYPE_signed_certificate_timestamp))
  4270. {
  4271. SSLerr(SSL_F_SSL_SET_CT_VALIDATION_CALLBACK,
  4272. SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  4273. return 0;
  4274. }
  4275. if (callback != NULL) {
  4276. /*
  4277. * If we are validating CT, then we MUST accept SCTs served via OCSP
  4278. */
  4279. if (!SSL_set_tlsext_status_type(s, TLSEXT_STATUSTYPE_ocsp))
  4280. return 0;
  4281. }
  4282. s->ct_validation_callback = callback;
  4283. s->ct_validation_callback_arg = arg;
  4284. return 1;
  4285. }
  4286. int SSL_CTX_set_ct_validation_callback(SSL_CTX *ctx,
  4287. ssl_ct_validation_cb callback, void *arg)
  4288. {
  4289. /*
  4290. * Since code exists that uses the custom extension handler for CT, look for
  4291. * this and throw an error if they have already registered to use CT.
  4292. */
  4293. if (callback != NULL && SSL_CTX_has_client_custom_ext(ctx,
  4294. TLSEXT_TYPE_signed_certificate_timestamp))
  4295. {
  4296. SSLerr(SSL_F_SSL_CTX_SET_CT_VALIDATION_CALLBACK,
  4297. SSL_R_CUSTOM_EXT_HANDLER_ALREADY_INSTALLED);
  4298. return 0;
  4299. }
  4300. ctx->ct_validation_callback = callback;
  4301. ctx->ct_validation_callback_arg = arg;
  4302. return 1;
  4303. }
  4304. int SSL_ct_is_enabled(const SSL *s)
  4305. {
  4306. return s->ct_validation_callback != NULL;
  4307. }
  4308. int SSL_CTX_ct_is_enabled(const SSL_CTX *ctx)
  4309. {
  4310. return ctx->ct_validation_callback != NULL;
  4311. }
  4312. int ssl_validate_ct(SSL *s)
  4313. {
  4314. int ret = 0;
  4315. X509 *cert = s->session != NULL ? s->session->peer : NULL;
  4316. X509 *issuer;
  4317. SSL_DANE *dane = &s->dane;
  4318. CT_POLICY_EVAL_CTX *ctx = NULL;
  4319. const STACK_OF(SCT) *scts;
  4320. /*
  4321. * If no callback is set, the peer is anonymous, or its chain is invalid,
  4322. * skip SCT validation - just return success. Applications that continue
  4323. * handshakes without certificates, with unverified chains, or pinned leaf
  4324. * certificates are outside the scope of the WebPKI and CT.
  4325. *
  4326. * The above exclusions notwithstanding the vast majority of peers will
  4327. * have rather ordinary certificate chains validated by typical
  4328. * applications that perform certificate verification and therefore will
  4329. * process SCTs when enabled.
  4330. */
  4331. if (s->ct_validation_callback == NULL || cert == NULL ||
  4332. s->verify_result != X509_V_OK ||
  4333. s->verified_chain == NULL || sk_X509_num(s->verified_chain) <= 1)
  4334. return 1;
  4335. /*
  4336. * CT not applicable for chains validated via DANE-TA(2) or DANE-EE(3)
  4337. * trust-anchors. See https://tools.ietf.org/html/rfc7671#section-4.2
  4338. */
  4339. if (DANETLS_ENABLED(dane) && dane->mtlsa != NULL) {
  4340. switch (dane->mtlsa->usage) {
  4341. case DANETLS_USAGE_DANE_TA:
  4342. case DANETLS_USAGE_DANE_EE:
  4343. return 1;
  4344. }
  4345. }
  4346. ctx = CT_POLICY_EVAL_CTX_new();
  4347. if (ctx == NULL) {
  4348. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_VALIDATE_CT,
  4349. ERR_R_MALLOC_FAILURE);
  4350. goto end;
  4351. }
  4352. issuer = sk_X509_value(s->verified_chain, 1);
  4353. CT_POLICY_EVAL_CTX_set1_cert(ctx, cert);
  4354. CT_POLICY_EVAL_CTX_set1_issuer(ctx, issuer);
  4355. CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(ctx, s->ctx->ctlog_store);
  4356. CT_POLICY_EVAL_CTX_set_time(
  4357. ctx, (uint64_t)SSL_SESSION_get_time(SSL_get0_session(s)) * 1000);
  4358. scts = SSL_get0_peer_scts(s);
  4359. /*
  4360. * This function returns success (> 0) only when all the SCTs are valid, 0
  4361. * when some are invalid, and < 0 on various internal errors (out of
  4362. * memory, etc.). Having some, or even all, invalid SCTs is not sufficient
  4363. * reason to abort the handshake, that decision is up to the callback.
  4364. * Therefore, we error out only in the unexpected case that the return
  4365. * value is negative.
  4366. *
  4367. * XXX: One might well argue that the return value of this function is an
  4368. * unfortunate design choice. Its job is only to determine the validation
  4369. * status of each of the provided SCTs. So long as it correctly separates
  4370. * the wheat from the chaff it should return success. Failure in this case
  4371. * ought to correspond to an inability to carry out its duties.
  4372. */
  4373. if (SCT_LIST_validate(scts, ctx) < 0) {
  4374. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_SSL_VALIDATE_CT,
  4375. SSL_R_SCT_VERIFICATION_FAILED);
  4376. goto end;
  4377. }
  4378. ret = s->ct_validation_callback(ctx, scts, s->ct_validation_callback_arg);
  4379. if (ret < 0)
  4380. ret = 0; /* This function returns 0 on failure */
  4381. if (!ret)
  4382. SSLfatal(s, SSL_AD_HANDSHAKE_FAILURE, SSL_F_SSL_VALIDATE_CT,
  4383. SSL_R_CALLBACK_FAILED);
  4384. end:
  4385. CT_POLICY_EVAL_CTX_free(ctx);
  4386. /*
  4387. * With SSL_VERIFY_NONE the session may be cached and re-used despite a
  4388. * failure return code here. Also the application may wish the complete
  4389. * the handshake, and then disconnect cleanly at a higher layer, after
  4390. * checking the verification status of the completed connection.
  4391. *
  4392. * We therefore force a certificate verification failure which will be
  4393. * visible via SSL_get_verify_result() and cached as part of any resumed
  4394. * session.
  4395. *
  4396. * Note: the permissive callback is for information gathering only, always
  4397. * returns success, and does not affect verification status. Only the
  4398. * strict callback or a custom application-specified callback can trigger
  4399. * connection failure or record a verification error.
  4400. */
  4401. if (ret <= 0)
  4402. s->verify_result = X509_V_ERR_NO_VALID_SCTS;
  4403. return ret;
  4404. }
  4405. int SSL_CTX_enable_ct(SSL_CTX *ctx, int validation_mode)
  4406. {
  4407. switch (validation_mode) {
  4408. default:
  4409. SSLerr(SSL_F_SSL_CTX_ENABLE_CT, SSL_R_INVALID_CT_VALIDATION_TYPE);
  4410. return 0;
  4411. case SSL_CT_VALIDATION_PERMISSIVE:
  4412. return SSL_CTX_set_ct_validation_callback(ctx, ct_permissive, NULL);
  4413. case SSL_CT_VALIDATION_STRICT:
  4414. return SSL_CTX_set_ct_validation_callback(ctx, ct_strict, NULL);
  4415. }
  4416. }
  4417. int SSL_enable_ct(SSL *s, int validation_mode)
  4418. {
  4419. switch (validation_mode) {
  4420. default:
  4421. SSLerr(SSL_F_SSL_ENABLE_CT, SSL_R_INVALID_CT_VALIDATION_TYPE);
  4422. return 0;
  4423. case SSL_CT_VALIDATION_PERMISSIVE:
  4424. return SSL_set_ct_validation_callback(s, ct_permissive, NULL);
  4425. case SSL_CT_VALIDATION_STRICT:
  4426. return SSL_set_ct_validation_callback(s, ct_strict, NULL);
  4427. }
  4428. }
  4429. int SSL_CTX_set_default_ctlog_list_file(SSL_CTX *ctx)
  4430. {
  4431. return CTLOG_STORE_load_default_file(ctx->ctlog_store);
  4432. }
  4433. int SSL_CTX_set_ctlog_list_file(SSL_CTX *ctx, const char *path)
  4434. {
  4435. return CTLOG_STORE_load_file(ctx->ctlog_store, path);
  4436. }
  4437. void SSL_CTX_set0_ctlog_store(SSL_CTX *ctx, CTLOG_STORE * logs)
  4438. {
  4439. CTLOG_STORE_free(ctx->ctlog_store);
  4440. ctx->ctlog_store = logs;
  4441. }
  4442. const CTLOG_STORE *SSL_CTX_get0_ctlog_store(const SSL_CTX *ctx)
  4443. {
  4444. return ctx->ctlog_store;
  4445. }
  4446. #endif /* OPENSSL_NO_CT */
  4447. void SSL_CTX_set_client_hello_cb(SSL_CTX *c, SSL_client_hello_cb_fn cb,
  4448. void *arg)
  4449. {
  4450. c->client_hello_cb = cb;
  4451. c->client_hello_cb_arg = arg;
  4452. }
  4453. int SSL_client_hello_isv2(SSL *s)
  4454. {
  4455. if (s->clienthello == NULL)
  4456. return 0;
  4457. return s->clienthello->isv2;
  4458. }
  4459. unsigned int SSL_client_hello_get0_legacy_version(SSL *s)
  4460. {
  4461. if (s->clienthello == NULL)
  4462. return 0;
  4463. return s->clienthello->legacy_version;
  4464. }
  4465. size_t SSL_client_hello_get0_random(SSL *s, const unsigned char **out)
  4466. {
  4467. if (s->clienthello == NULL)
  4468. return 0;
  4469. if (out != NULL)
  4470. *out = s->clienthello->random;
  4471. return SSL3_RANDOM_SIZE;
  4472. }
  4473. size_t SSL_client_hello_get0_session_id(SSL *s, const unsigned char **out)
  4474. {
  4475. if (s->clienthello == NULL)
  4476. return 0;
  4477. if (out != NULL)
  4478. *out = s->clienthello->session_id;
  4479. return s->clienthello->session_id_len;
  4480. }
  4481. size_t SSL_client_hello_get0_ciphers(SSL *s, const unsigned char **out)
  4482. {
  4483. if (s->clienthello == NULL)
  4484. return 0;
  4485. if (out != NULL)
  4486. *out = PACKET_data(&s->clienthello->ciphersuites);
  4487. return PACKET_remaining(&s->clienthello->ciphersuites);
  4488. }
  4489. size_t SSL_client_hello_get0_compression_methods(SSL *s, const unsigned char **out)
  4490. {
  4491. if (s->clienthello == NULL)
  4492. return 0;
  4493. if (out != NULL)
  4494. *out = s->clienthello->compressions;
  4495. return s->clienthello->compressions_len;
  4496. }
  4497. int SSL_client_hello_get1_extensions_present(SSL *s, int **out, size_t *outlen)
  4498. {
  4499. RAW_EXTENSION *ext;
  4500. int *present;
  4501. size_t num = 0, i;
  4502. if (s->clienthello == NULL || out == NULL || outlen == NULL)
  4503. return 0;
  4504. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4505. ext = s->clienthello->pre_proc_exts + i;
  4506. if (ext->present)
  4507. num++;
  4508. }
  4509. if (num == 0) {
  4510. *out = NULL;
  4511. *outlen = 0;
  4512. return 1;
  4513. }
  4514. if ((present = OPENSSL_malloc(sizeof(*present) * num)) == NULL) {
  4515. SSLerr(SSL_F_SSL_CLIENT_HELLO_GET1_EXTENSIONS_PRESENT,
  4516. ERR_R_MALLOC_FAILURE);
  4517. return 0;
  4518. }
  4519. for (i = 0; i < s->clienthello->pre_proc_exts_len; i++) {
  4520. ext = s->clienthello->pre_proc_exts + i;
  4521. if (ext->present) {
  4522. if (ext->received_order >= num)
  4523. goto err;
  4524. present[ext->received_order] = ext->type;
  4525. }
  4526. }
  4527. *out = present;
  4528. *outlen = num;
  4529. return 1;
  4530. err:
  4531. OPENSSL_free(present);
  4532. return 0;
  4533. }
  4534. int SSL_client_hello_get0_ext(SSL *s, unsigned int type, const unsigned char **out,
  4535. size_t *outlen)
  4536. {
  4537. size_t i;
  4538. RAW_EXTENSION *r;
  4539. if (s->clienthello == NULL)
  4540. return 0;
  4541. for (i = 0; i < s->clienthello->pre_proc_exts_len; ++i) {
  4542. r = s->clienthello->pre_proc_exts + i;
  4543. if (r->present && r->type == type) {
  4544. if (out != NULL)
  4545. *out = PACKET_data(&r->data);
  4546. if (outlen != NULL)
  4547. *outlen = PACKET_remaining(&r->data);
  4548. return 1;
  4549. }
  4550. }
  4551. return 0;
  4552. }
  4553. int SSL_free_buffers(SSL *ssl)
  4554. {
  4555. RECORD_LAYER *rl = &ssl->rlayer;
  4556. if (RECORD_LAYER_read_pending(rl) || RECORD_LAYER_write_pending(rl))
  4557. return 0;
  4558. RECORD_LAYER_release(rl);
  4559. return 1;
  4560. }
  4561. int SSL_alloc_buffers(SSL *ssl)
  4562. {
  4563. return ssl3_setup_buffers(ssl);
  4564. }
  4565. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx, SSL_CTX_keylog_cb_func cb)
  4566. {
  4567. ctx->keylog_callback = cb;
  4568. }
  4569. SSL_CTX_keylog_cb_func SSL_CTX_get_keylog_callback(const SSL_CTX *ctx)
  4570. {
  4571. return ctx->keylog_callback;
  4572. }
  4573. static int nss_keylog_int(const char *prefix,
  4574. SSL *ssl,
  4575. const uint8_t *parameter_1,
  4576. size_t parameter_1_len,
  4577. const uint8_t *parameter_2,
  4578. size_t parameter_2_len)
  4579. {
  4580. char *out = NULL;
  4581. char *cursor = NULL;
  4582. size_t out_len = 0;
  4583. size_t i;
  4584. size_t prefix_len;
  4585. if (ssl->ctx->keylog_callback == NULL)
  4586. return 1;
  4587. /*
  4588. * Our output buffer will contain the following strings, rendered with
  4589. * space characters in between, terminated by a NULL character: first the
  4590. * prefix, then the first parameter, then the second parameter. The
  4591. * meaning of each parameter depends on the specific key material being
  4592. * logged. Note that the first and second parameters are encoded in
  4593. * hexadecimal, so we need a buffer that is twice their lengths.
  4594. */
  4595. prefix_len = strlen(prefix);
  4596. out_len = prefix_len + (2 * parameter_1_len) + (2 * parameter_2_len) + 3;
  4597. if ((out = cursor = OPENSSL_malloc(out_len)) == NULL) {
  4598. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR, SSL_F_NSS_KEYLOG_INT,
  4599. ERR_R_MALLOC_FAILURE);
  4600. return 0;
  4601. }
  4602. strcpy(cursor, prefix);
  4603. cursor += prefix_len;
  4604. *cursor++ = ' ';
  4605. for (i = 0; i < parameter_1_len; i++) {
  4606. sprintf(cursor, "%02x", parameter_1[i]);
  4607. cursor += 2;
  4608. }
  4609. *cursor++ = ' ';
  4610. for (i = 0; i < parameter_2_len; i++) {
  4611. sprintf(cursor, "%02x", parameter_2[i]);
  4612. cursor += 2;
  4613. }
  4614. *cursor = '\0';
  4615. ssl->ctx->keylog_callback(ssl, (const char *)out);
  4616. OPENSSL_clear_free(out, out_len);
  4617. return 1;
  4618. }
  4619. int ssl_log_rsa_client_key_exchange(SSL *ssl,
  4620. const uint8_t *encrypted_premaster,
  4621. size_t encrypted_premaster_len,
  4622. const uint8_t *premaster,
  4623. size_t premaster_len)
  4624. {
  4625. if (encrypted_premaster_len < 8) {
  4626. SSLfatal(ssl, SSL_AD_INTERNAL_ERROR,
  4627. SSL_F_SSL_LOG_RSA_CLIENT_KEY_EXCHANGE, ERR_R_INTERNAL_ERROR);
  4628. return 0;
  4629. }
  4630. /* We only want the first 8 bytes of the encrypted premaster as a tag. */
  4631. return nss_keylog_int("RSA",
  4632. ssl,
  4633. encrypted_premaster,
  4634. 8,
  4635. premaster,
  4636. premaster_len);
  4637. }
  4638. int ssl_log_secret(SSL *ssl,
  4639. const char *label,
  4640. const uint8_t *secret,
  4641. size_t secret_len)
  4642. {
  4643. return nss_keylog_int(label,
  4644. ssl,
  4645. ssl->s3.client_random,
  4646. SSL3_RANDOM_SIZE,
  4647. secret,
  4648. secret_len);
  4649. }
  4650. #define SSLV2_CIPHER_LEN 3
  4651. int ssl_cache_cipherlist(SSL *s, PACKET *cipher_suites, int sslv2format)
  4652. {
  4653. int n;
  4654. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  4655. if (PACKET_remaining(cipher_suites) == 0) {
  4656. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_SSL_CACHE_CIPHERLIST,
  4657. SSL_R_NO_CIPHERS_SPECIFIED);
  4658. return 0;
  4659. }
  4660. if (PACKET_remaining(cipher_suites) % n != 0) {
  4661. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
  4662. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4663. return 0;
  4664. }
  4665. OPENSSL_free(s->s3.tmp.ciphers_raw);
  4666. s->s3.tmp.ciphers_raw = NULL;
  4667. s->s3.tmp.ciphers_rawlen = 0;
  4668. if (sslv2format) {
  4669. size_t numciphers = PACKET_remaining(cipher_suites) / n;
  4670. PACKET sslv2ciphers = *cipher_suites;
  4671. unsigned int leadbyte;
  4672. unsigned char *raw;
  4673. /*
  4674. * We store the raw ciphers list in SSLv3+ format so we need to do some
  4675. * preprocessing to convert the list first. If there are any SSLv2 only
  4676. * ciphersuites with a non-zero leading byte then we are going to
  4677. * slightly over allocate because we won't store those. But that isn't a
  4678. * problem.
  4679. */
  4680. raw = OPENSSL_malloc(numciphers * TLS_CIPHER_LEN);
  4681. s->s3.tmp.ciphers_raw = raw;
  4682. if (raw == NULL) {
  4683. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
  4684. ERR_R_MALLOC_FAILURE);
  4685. return 0;
  4686. }
  4687. for (s->s3.tmp.ciphers_rawlen = 0;
  4688. PACKET_remaining(&sslv2ciphers) > 0;
  4689. raw += TLS_CIPHER_LEN) {
  4690. if (!PACKET_get_1(&sslv2ciphers, &leadbyte)
  4691. || (leadbyte == 0
  4692. && !PACKET_copy_bytes(&sslv2ciphers, raw,
  4693. TLS_CIPHER_LEN))
  4694. || (leadbyte != 0
  4695. && !PACKET_forward(&sslv2ciphers, TLS_CIPHER_LEN))) {
  4696. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
  4697. SSL_R_BAD_PACKET);
  4698. OPENSSL_free(s->s3.tmp.ciphers_raw);
  4699. s->s3.tmp.ciphers_raw = NULL;
  4700. s->s3.tmp.ciphers_rawlen = 0;
  4701. return 0;
  4702. }
  4703. if (leadbyte == 0)
  4704. s->s3.tmp.ciphers_rawlen += TLS_CIPHER_LEN;
  4705. }
  4706. } else if (!PACKET_memdup(cipher_suites, &s->s3.tmp.ciphers_raw,
  4707. &s->s3.tmp.ciphers_rawlen)) {
  4708. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_SSL_CACHE_CIPHERLIST,
  4709. ERR_R_INTERNAL_ERROR);
  4710. return 0;
  4711. }
  4712. return 1;
  4713. }
  4714. int SSL_bytes_to_cipher_list(SSL *s, const unsigned char *bytes, size_t len,
  4715. int isv2format, STACK_OF(SSL_CIPHER) **sk,
  4716. STACK_OF(SSL_CIPHER) **scsvs)
  4717. {
  4718. PACKET pkt;
  4719. if (!PACKET_buf_init(&pkt, bytes, len))
  4720. return 0;
  4721. return bytes_to_cipher_list(s, &pkt, sk, scsvs, isv2format, 0);
  4722. }
  4723. int bytes_to_cipher_list(SSL *s, PACKET *cipher_suites,
  4724. STACK_OF(SSL_CIPHER) **skp,
  4725. STACK_OF(SSL_CIPHER) **scsvs_out,
  4726. int sslv2format, int fatal)
  4727. {
  4728. const SSL_CIPHER *c;
  4729. STACK_OF(SSL_CIPHER) *sk = NULL;
  4730. STACK_OF(SSL_CIPHER) *scsvs = NULL;
  4731. int n;
  4732. /* 3 = SSLV2_CIPHER_LEN > TLS_CIPHER_LEN = 2. */
  4733. unsigned char cipher[SSLV2_CIPHER_LEN];
  4734. n = sslv2format ? SSLV2_CIPHER_LEN : TLS_CIPHER_LEN;
  4735. if (PACKET_remaining(cipher_suites) == 0) {
  4736. if (fatal)
  4737. SSLfatal(s, SSL_AD_ILLEGAL_PARAMETER, SSL_F_BYTES_TO_CIPHER_LIST,
  4738. SSL_R_NO_CIPHERS_SPECIFIED);
  4739. else
  4740. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, SSL_R_NO_CIPHERS_SPECIFIED);
  4741. return 0;
  4742. }
  4743. if (PACKET_remaining(cipher_suites) % n != 0) {
  4744. if (fatal)
  4745. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_BYTES_TO_CIPHER_LIST,
  4746. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4747. else
  4748. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST,
  4749. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  4750. return 0;
  4751. }
  4752. sk = sk_SSL_CIPHER_new_null();
  4753. scsvs = sk_SSL_CIPHER_new_null();
  4754. if (sk == NULL || scsvs == NULL) {
  4755. if (fatal)
  4756. SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_BYTES_TO_CIPHER_LIST,
  4757. ERR_R_MALLOC_FAILURE);
  4758. else
  4759. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  4760. goto err;
  4761. }
  4762. while (PACKET_copy_bytes(cipher_suites, cipher, n)) {
  4763. /*
  4764. * SSLv3 ciphers wrapped in an SSLv2-compatible ClientHello have the
  4765. * first byte set to zero, while true SSLv2 ciphers have a non-zero
  4766. * first byte. We don't support any true SSLv2 ciphers, so skip them.
  4767. */
  4768. if (sslv2format && cipher[0] != '\0')
  4769. continue;
  4770. /* For SSLv2-compat, ignore leading 0-byte. */
  4771. c = ssl_get_cipher_by_char(s, sslv2format ? &cipher[1] : cipher, 1);
  4772. if (c != NULL) {
  4773. if ((c->valid && !sk_SSL_CIPHER_push(sk, c)) ||
  4774. (!c->valid && !sk_SSL_CIPHER_push(scsvs, c))) {
  4775. if (fatal)
  4776. SSLfatal(s, SSL_AD_INTERNAL_ERROR,
  4777. SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  4778. else
  4779. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, ERR_R_MALLOC_FAILURE);
  4780. goto err;
  4781. }
  4782. }
  4783. }
  4784. if (PACKET_remaining(cipher_suites) > 0) {
  4785. if (fatal)
  4786. SSLfatal(s, SSL_AD_DECODE_ERROR, SSL_F_BYTES_TO_CIPHER_LIST,
  4787. SSL_R_BAD_LENGTH);
  4788. else
  4789. SSLerr(SSL_F_BYTES_TO_CIPHER_LIST, SSL_R_BAD_LENGTH);
  4790. goto err;
  4791. }
  4792. if (skp != NULL)
  4793. *skp = sk;
  4794. else
  4795. sk_SSL_CIPHER_free(sk);
  4796. if (scsvs_out != NULL)
  4797. *scsvs_out = scsvs;
  4798. else
  4799. sk_SSL_CIPHER_free(scsvs);
  4800. return 1;
  4801. err:
  4802. sk_SSL_CIPHER_free(sk);
  4803. sk_SSL_CIPHER_free(scsvs);
  4804. return 0;
  4805. }
  4806. int SSL_CTX_set_max_early_data(SSL_CTX *ctx, uint32_t max_early_data)
  4807. {
  4808. ctx->max_early_data = max_early_data;
  4809. return 1;
  4810. }
  4811. uint32_t SSL_CTX_get_max_early_data(const SSL_CTX *ctx)
  4812. {
  4813. return ctx->max_early_data;
  4814. }
  4815. int SSL_set_max_early_data(SSL *s, uint32_t max_early_data)
  4816. {
  4817. s->max_early_data = max_early_data;
  4818. return 1;
  4819. }
  4820. uint32_t SSL_get_max_early_data(const SSL *s)
  4821. {
  4822. return s->max_early_data;
  4823. }
  4824. int SSL_CTX_set_recv_max_early_data(SSL_CTX *ctx, uint32_t recv_max_early_data)
  4825. {
  4826. ctx->recv_max_early_data = recv_max_early_data;
  4827. return 1;
  4828. }
  4829. uint32_t SSL_CTX_get_recv_max_early_data(const SSL_CTX *ctx)
  4830. {
  4831. return ctx->recv_max_early_data;
  4832. }
  4833. int SSL_set_recv_max_early_data(SSL *s, uint32_t recv_max_early_data)
  4834. {
  4835. s->recv_max_early_data = recv_max_early_data;
  4836. return 1;
  4837. }
  4838. uint32_t SSL_get_recv_max_early_data(const SSL *s)
  4839. {
  4840. return s->recv_max_early_data;
  4841. }
  4842. __owur unsigned int ssl_get_max_send_fragment(const SSL *ssl)
  4843. {
  4844. /* Return any active Max Fragment Len extension */
  4845. if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session))
  4846. return GET_MAX_FRAGMENT_LENGTH(ssl->session);
  4847. /* return current SSL connection setting */
  4848. return ssl->max_send_fragment;
  4849. }
  4850. __owur unsigned int ssl_get_split_send_fragment(const SSL *ssl)
  4851. {
  4852. /* Return a value regarding an active Max Fragment Len extension */
  4853. if (ssl->session != NULL && USE_MAX_FRAGMENT_LENGTH_EXT(ssl->session)
  4854. && ssl->split_send_fragment > GET_MAX_FRAGMENT_LENGTH(ssl->session))
  4855. return GET_MAX_FRAGMENT_LENGTH(ssl->session);
  4856. /* else limit |split_send_fragment| to current |max_send_fragment| */
  4857. if (ssl->split_send_fragment > ssl->max_send_fragment)
  4858. return ssl->max_send_fragment;
  4859. /* return current SSL connection setting */
  4860. return ssl->split_send_fragment;
  4861. }
  4862. int SSL_stateless(SSL *s)
  4863. {
  4864. int ret;
  4865. /* Ensure there is no state left over from a previous invocation */
  4866. if (!SSL_clear(s))
  4867. return 0;
  4868. ERR_clear_error();
  4869. s->s3.flags |= TLS1_FLAGS_STATELESS;
  4870. ret = SSL_accept(s);
  4871. s->s3.flags &= ~TLS1_FLAGS_STATELESS;
  4872. if (ret > 0 && s->ext.cookieok)
  4873. return 1;
  4874. if (s->hello_retry_request == SSL_HRR_PENDING && !ossl_statem_in_error(s))
  4875. return 0;
  4876. return -1;
  4877. }
  4878. void SSL_CTX_set_post_handshake_auth(SSL_CTX *ctx, int val)
  4879. {
  4880. ctx->pha_enabled = val;
  4881. }
  4882. void SSL_set_post_handshake_auth(SSL *ssl, int val)
  4883. {
  4884. ssl->pha_enabled = val;
  4885. }
  4886. int SSL_verify_client_post_handshake(SSL *ssl)
  4887. {
  4888. if (!SSL_IS_TLS13(ssl)) {
  4889. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_WRONG_SSL_VERSION);
  4890. return 0;
  4891. }
  4892. if (!ssl->server) {
  4893. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_NOT_SERVER);
  4894. return 0;
  4895. }
  4896. if (!SSL_is_init_finished(ssl)) {
  4897. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_STILL_IN_INIT);
  4898. return 0;
  4899. }
  4900. switch (ssl->post_handshake_auth) {
  4901. case SSL_PHA_NONE:
  4902. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_EXTENSION_NOT_RECEIVED);
  4903. return 0;
  4904. default:
  4905. case SSL_PHA_EXT_SENT:
  4906. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, ERR_R_INTERNAL_ERROR);
  4907. return 0;
  4908. case SSL_PHA_EXT_RECEIVED:
  4909. break;
  4910. case SSL_PHA_REQUEST_PENDING:
  4911. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_REQUEST_PENDING);
  4912. return 0;
  4913. case SSL_PHA_REQUESTED:
  4914. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_REQUEST_SENT);
  4915. return 0;
  4916. }
  4917. ssl->post_handshake_auth = SSL_PHA_REQUEST_PENDING;
  4918. /* checks verify_mode and algorithm_auth */
  4919. if (!send_certificate_request(ssl)) {
  4920. ssl->post_handshake_auth = SSL_PHA_EXT_RECEIVED; /* restore on error */
  4921. SSLerr(SSL_F_SSL_VERIFY_CLIENT_POST_HANDSHAKE, SSL_R_INVALID_CONFIG);
  4922. return 0;
  4923. }
  4924. ossl_statem_set_in_init(ssl, 1);
  4925. return 1;
  4926. }
  4927. int SSL_CTX_set_session_ticket_cb(SSL_CTX *ctx,
  4928. SSL_CTX_generate_session_ticket_fn gen_cb,
  4929. SSL_CTX_decrypt_session_ticket_fn dec_cb,
  4930. void *arg)
  4931. {
  4932. ctx->generate_ticket_cb = gen_cb;
  4933. ctx->decrypt_ticket_cb = dec_cb;
  4934. ctx->ticket_cb_data = arg;
  4935. return 1;
  4936. }
  4937. void SSL_CTX_set_allow_early_data_cb(SSL_CTX *ctx,
  4938. SSL_allow_early_data_cb_fn cb,
  4939. void *arg)
  4940. {
  4941. ctx->allow_early_data_cb = cb;
  4942. ctx->allow_early_data_cb_data = arg;
  4943. }
  4944. void SSL_set_allow_early_data_cb(SSL *s,
  4945. SSL_allow_early_data_cb_fn cb,
  4946. void *arg)
  4947. {
  4948. s->allow_early_data_cb = cb;
  4949. s->allow_early_data_cb_data = arg;
  4950. }