ssl_lib.c 210 KB

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