1
0

ssl_lib.c 157 KB

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