ssl_lib.c 189 KB

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