ssl_lib.c 158 KB

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