ssl_lib.c 162 KB

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