ssl_lib.c 153 KB

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