ssl_lib.c 165 KB

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