You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ssl_lib.c 86 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.]
  56. */
  57. /* ====================================================================
  58. * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved.
  59. *
  60. * Redistribution and use in source and binary forms, with or without
  61. * modification, are permitted provided that the following conditions
  62. * are met:
  63. *
  64. * 1. Redistributions of source code must retain the above copyright
  65. * notice, this list of conditions and the following disclaimer.
  66. *
  67. * 2. Redistributions in binary form must reproduce the above copyright
  68. * notice, this list of conditions and the following disclaimer in
  69. * the documentation and/or other materials provided with the
  70. * distribution.
  71. *
  72. * 3. All advertising materials mentioning features or use of this
  73. * software must display the following acknowledgment:
  74. * "This product includes software developed by the OpenSSL Project
  75. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  76. *
  77. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  78. * endorse or promote products derived from this software without
  79. * prior written permission. For written permission, please contact
  80. * openssl-core@openssl.org.
  81. *
  82. * 5. Products derived from this software may not be called "OpenSSL"
  83. * nor may "OpenSSL" appear in their names without prior written
  84. * permission of the OpenSSL Project.
  85. *
  86. * 6. Redistributions of any form whatsoever must retain the following
  87. * acknowledgment:
  88. * "This product includes software developed by the OpenSSL Project
  89. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  90. *
  91. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  92. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  93. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  94. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  95. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  96. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  97. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  98. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  99. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  100. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  101. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  102. * OF THE POSSIBILITY OF SUCH DAMAGE.
  103. * ====================================================================
  104. *
  105. * This product includes cryptographic software written by Eric Young
  106. * (eay@cryptsoft.com). This product includes software written by Tim
  107. * Hudson (tjh@cryptsoft.com).
  108. *
  109. */
  110. /* ====================================================================
  111. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  112. * ECC cipher suite support in OpenSSL originally developed by
  113. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  114. */
  115. /* ====================================================================
  116. * Copyright 2005 Nokia. All rights reserved.
  117. *
  118. * The portions of the attached software ("Contribution") is developed by
  119. * Nokia Corporation and is licensed pursuant to the OpenSSL open source
  120. * license.
  121. *
  122. * The Contribution, originally written by Mika Kousa and Pasi Eronen of
  123. * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites
  124. * support (see RFC 4279) to OpenSSL.
  125. *
  126. * No patent licenses or other rights except those expressly stated in
  127. * the OpenSSL open source license shall be deemed granted or received
  128. * expressly, by implication, estoppel, or otherwise.
  129. *
  130. * No assurances are provided by Nokia that the Contribution does not
  131. * infringe the patent or other intellectual property rights of any third
  132. * party or that the license provides you with all the necessary rights
  133. * to make use of the Contribution.
  134. *
  135. * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN
  136. * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA
  137. * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY
  138. * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR
  139. * OTHERWISE. */
  140. #include <assert.h>
  141. #include <stdio.h>
  142. #include <string.h>
  143. #include <openssl/bytestring.h>
  144. #include <openssl/dh.h>
  145. #include <openssl/err.h>
  146. #include <openssl/lhash.h>
  147. #include <openssl/mem.h>
  148. #include <openssl/obj.h>
  149. #include <openssl/rand.h>
  150. #include <openssl/x509v3.h>
  151. #include "internal.h"
  152. #include "../crypto/internal.h"
  153. /* Some error codes are special. Ensure the make_errors.go script never
  154. * regresses this. */
  155. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  156. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  157. ssl_alert_reason_code_mismatch);
  158. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl = CRYPTO_EX_DATA_CLASS_INIT;
  159. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx = CRYPTO_EX_DATA_CLASS_INIT;
  160. int SSL_clear(SSL *s) {
  161. if (s->method == NULL) {
  162. OPENSSL_PUT_ERROR(SSL, SSL_clear, SSL_R_NO_METHOD_SPECIFIED);
  163. return 0;
  164. }
  165. if (ssl_clear_bad_session(s)) {
  166. SSL_SESSION_free(s->session);
  167. s->session = NULL;
  168. }
  169. s->hit = 0;
  170. s->shutdown = 0;
  171. if (s->renegotiate) {
  172. OPENSSL_PUT_ERROR(SSL, SSL_clear, ERR_R_INTERNAL_ERROR);
  173. return 0;
  174. }
  175. /* SSL_clear may be called before or after the |s| is initialized in either
  176. * accept or connect state. In the latter case, SSL_clear should preserve the
  177. * half and reset |s->state| accordingly. */
  178. if (s->handshake_func != NULL) {
  179. if (s->server) {
  180. SSL_set_accept_state(s);
  181. } else {
  182. SSL_set_connect_state(s);
  183. }
  184. } else {
  185. assert(s->state == 0);
  186. }
  187. /* TODO(davidben): Some state on |s| is reset both in |SSL_new| and
  188. * |SSL_clear| because it is per-connection state rather than configuration
  189. * state. Per-connection state should be on |s->s3| and |s->d1| so it is
  190. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  191. * |ssl3_new|. */
  192. s->rwstate = SSL_NOTHING;
  193. s->rstate = SSL_ST_READ_HEADER;
  194. if (s->init_buf != NULL) {
  195. BUF_MEM_free(s->init_buf);
  196. s->init_buf = NULL;
  197. }
  198. s->packet = NULL;
  199. s->packet_length = 0;
  200. ssl_clear_cipher_ctx(s);
  201. if (s->next_proto_negotiated) {
  202. OPENSSL_free(s->next_proto_negotiated);
  203. s->next_proto_negotiated = NULL;
  204. s->next_proto_negotiated_len = 0;
  205. }
  206. /* The s->d1->mtu is simultaneously configuration (preserved across
  207. * clear) and connection-specific state (gets reset).
  208. *
  209. * TODO(davidben): Avoid this. */
  210. unsigned mtu = 0;
  211. if (s->d1 != NULL) {
  212. mtu = s->d1->mtu;
  213. }
  214. s->method->ssl_free(s);
  215. if (!s->method->ssl_new(s)) {
  216. return 0;
  217. }
  218. s->enc_method = ssl3_get_enc_method(s->version);
  219. assert(s->enc_method != NULL);
  220. if (SSL_IS_DTLS(s) && (SSL_get_options(s) & SSL_OP_NO_QUERY_MTU)) {
  221. s->d1->mtu = mtu;
  222. }
  223. s->client_version = s->version;
  224. return 1;
  225. }
  226. SSL *SSL_new(SSL_CTX *ctx) {
  227. SSL *s;
  228. if (ctx == NULL) {
  229. OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_NULL_SSL_CTX);
  230. return NULL;
  231. }
  232. if (ctx->method == NULL) {
  233. OPENSSL_PUT_ERROR(SSL, SSL_new, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  234. return NULL;
  235. }
  236. s = (SSL *)OPENSSL_malloc(sizeof(SSL));
  237. if (s == NULL) {
  238. goto err;
  239. }
  240. memset(s, 0, sizeof(SSL));
  241. s->min_version = ctx->min_version;
  242. s->max_version = ctx->max_version;
  243. s->options = ctx->options;
  244. s->mode = ctx->mode;
  245. s->max_cert_list = ctx->max_cert_list;
  246. s->cert = ssl_cert_dup(ctx->cert);
  247. if (s->cert == NULL) {
  248. goto err;
  249. }
  250. s->read_ahead = ctx->read_ahead;
  251. s->msg_callback = ctx->msg_callback;
  252. s->msg_callback_arg = ctx->msg_callback_arg;
  253. s->verify_mode = ctx->verify_mode;
  254. s->sid_ctx_length = ctx->sid_ctx_length;
  255. assert(s->sid_ctx_length <= sizeof s->sid_ctx);
  256. memcpy(&s->sid_ctx, &ctx->sid_ctx, sizeof(s->sid_ctx));
  257. s->verify_callback = ctx->default_verify_callback;
  258. s->generate_session_id = ctx->generate_session_id;
  259. s->param = X509_VERIFY_PARAM_new();
  260. if (!s->param) {
  261. goto err;
  262. }
  263. X509_VERIFY_PARAM_inherit(s->param, ctx->param);
  264. s->quiet_shutdown = ctx->quiet_shutdown;
  265. s->max_send_fragment = ctx->max_send_fragment;
  266. CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
  267. s->ctx = ctx;
  268. s->tlsext_debug_cb = 0;
  269. s->tlsext_debug_arg = NULL;
  270. s->tlsext_ticket_expected = 0;
  271. CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
  272. s->initial_ctx = ctx;
  273. if (ctx->tlsext_ecpointformatlist) {
  274. s->tlsext_ecpointformatlist = BUF_memdup(
  275. ctx->tlsext_ecpointformatlist, ctx->tlsext_ecpointformatlist_length);
  276. if (!s->tlsext_ecpointformatlist) {
  277. goto err;
  278. }
  279. s->tlsext_ecpointformatlist_length = ctx->tlsext_ecpointformatlist_length;
  280. }
  281. if (ctx->tlsext_ellipticcurvelist) {
  282. s->tlsext_ellipticcurvelist =
  283. BUF_memdup(ctx->tlsext_ellipticcurvelist,
  284. ctx->tlsext_ellipticcurvelist_length * 2);
  285. if (!s->tlsext_ellipticcurvelist) {
  286. goto err;
  287. }
  288. s->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
  289. }
  290. s->next_proto_negotiated = NULL;
  291. if (s->ctx->alpn_client_proto_list) {
  292. s->alpn_client_proto_list = BUF_memdup(s->ctx->alpn_client_proto_list,
  293. s->ctx->alpn_client_proto_list_len);
  294. if (s->alpn_client_proto_list == NULL) {
  295. goto err;
  296. }
  297. s->alpn_client_proto_list_len = s->ctx->alpn_client_proto_list_len;
  298. }
  299. s->verify_result = X509_V_OK;
  300. s->method = ctx->method;
  301. if (!s->method->ssl_new(s)) {
  302. goto err;
  303. }
  304. s->enc_method = ssl3_get_enc_method(s->version);
  305. assert(s->enc_method != NULL);
  306. s->rwstate = SSL_NOTHING;
  307. s->rstate = SSL_ST_READ_HEADER;
  308. CRYPTO_new_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
  309. s->psk_identity_hint = NULL;
  310. if (ctx->psk_identity_hint) {
  311. s->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  312. if (s->psk_identity_hint == NULL) {
  313. goto err;
  314. }
  315. }
  316. s->psk_client_callback = ctx->psk_client_callback;
  317. s->psk_server_callback = ctx->psk_server_callback;
  318. s->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  319. if (ctx->tlsext_channel_id_private) {
  320. s->tlsext_channel_id_private = EVP_PKEY_dup(ctx->tlsext_channel_id_private);
  321. }
  322. s->signed_cert_timestamps_enabled = s->ctx->signed_cert_timestamps_enabled;
  323. s->ocsp_stapling_enabled = s->ctx->ocsp_stapling_enabled;
  324. return s;
  325. err:
  326. if (s != NULL) {
  327. SSL_free(s);
  328. }
  329. OPENSSL_PUT_ERROR(SSL, SSL_new, ERR_R_MALLOC_FAILURE);
  330. return NULL;
  331. }
  332. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  333. unsigned int sid_ctx_len) {
  334. if (sid_ctx_len > sizeof ctx->sid_ctx) {
  335. OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_session_id_context,
  336. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  337. return 0;
  338. }
  339. ctx->sid_ctx_length = sid_ctx_len;
  340. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  341. return 1;
  342. }
  343. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  344. unsigned int sid_ctx_len) {
  345. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  346. OPENSSL_PUT_ERROR(SSL, SSL_set_session_id_context,
  347. SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  348. return 0;
  349. }
  350. ssl->sid_ctx_length = sid_ctx_len;
  351. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  352. return 1;
  353. }
  354. int SSL_CTX_set_generate_session_id(SSL_CTX *ctx, GEN_SESSION_CB cb) {
  355. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  356. ctx->generate_session_id = cb;
  357. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  358. return 1;
  359. }
  360. int SSL_set_generate_session_id(SSL *ssl, GEN_SESSION_CB cb) {
  361. ssl->generate_session_id = cb;
  362. return 1;
  363. }
  364. int SSL_has_matching_session_id(const SSL *ssl, const uint8_t *id,
  365. unsigned int id_len) {
  366. /* A quick examination of SSL_SESSION_hash and SSL_SESSION_cmp shows how we
  367. * can "construct" a session to give us the desired check - ie. to find if
  368. * there's a session in the hash table that would conflict with any new
  369. * session built out of this id/id_len and the ssl_version in use by this
  370. * SSL. */
  371. SSL_SESSION r, *p;
  372. if (id_len > sizeof r.session_id) {
  373. return 0;
  374. }
  375. r.ssl_version = ssl->version;
  376. r.session_id_length = id_len;
  377. memcpy(r.session_id, id, id_len);
  378. CRYPTO_r_lock(CRYPTO_LOCK_SSL_CTX);
  379. p = lh_SSL_SESSION_retrieve(ssl->ctx->sessions, &r);
  380. CRYPTO_r_unlock(CRYPTO_LOCK_SSL_CTX);
  381. return p != NULL;
  382. }
  383. int SSL_CTX_set_purpose(SSL_CTX *s, int purpose) {
  384. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  385. }
  386. int SSL_set_purpose(SSL *s, int purpose) {
  387. return X509_VERIFY_PARAM_set_purpose(s->param, purpose);
  388. }
  389. int SSL_CTX_set_trust(SSL_CTX *s, int trust) {
  390. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  391. }
  392. int SSL_set_trust(SSL *s, int trust) {
  393. return X509_VERIFY_PARAM_set_trust(s->param, trust);
  394. }
  395. int SSL_CTX_set1_param(SSL_CTX *ctx, X509_VERIFY_PARAM *vpm) {
  396. return X509_VERIFY_PARAM_set1(ctx->param, vpm);
  397. }
  398. int SSL_set1_param(SSL *ssl, X509_VERIFY_PARAM *vpm) {
  399. return X509_VERIFY_PARAM_set1(ssl->param, vpm);
  400. }
  401. void ssl_cipher_preference_list_free(
  402. struct ssl_cipher_preference_list_st *cipher_list) {
  403. sk_SSL_CIPHER_free(cipher_list->ciphers);
  404. OPENSSL_free(cipher_list->in_group_flags);
  405. OPENSSL_free(cipher_list);
  406. }
  407. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_dup(
  408. struct ssl_cipher_preference_list_st *cipher_list) {
  409. struct ssl_cipher_preference_list_st *ret = NULL;
  410. size_t n = sk_SSL_CIPHER_num(cipher_list->ciphers);
  411. ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  412. if (!ret) {
  413. goto err;
  414. }
  415. ret->ciphers = NULL;
  416. ret->in_group_flags = NULL;
  417. ret->ciphers = sk_SSL_CIPHER_dup(cipher_list->ciphers);
  418. if (!ret->ciphers) {
  419. goto err;
  420. }
  421. ret->in_group_flags = BUF_memdup(cipher_list->in_group_flags, n);
  422. if (!ret->in_group_flags) {
  423. goto err;
  424. }
  425. return ret;
  426. err:
  427. if (ret && ret->ciphers) {
  428. sk_SSL_CIPHER_free(ret->ciphers);
  429. }
  430. if (ret) {
  431. OPENSSL_free(ret);
  432. }
  433. return NULL;
  434. }
  435. struct ssl_cipher_preference_list_st *ssl_cipher_preference_list_from_ciphers(
  436. STACK_OF(SSL_CIPHER) * ciphers) {
  437. struct ssl_cipher_preference_list_st *ret = NULL;
  438. size_t n = sk_SSL_CIPHER_num(ciphers);
  439. ret = OPENSSL_malloc(sizeof(struct ssl_cipher_preference_list_st));
  440. if (!ret) {
  441. goto err;
  442. }
  443. ret->ciphers = NULL;
  444. ret->in_group_flags = NULL;
  445. ret->ciphers = sk_SSL_CIPHER_dup(ciphers);
  446. if (!ret->ciphers) {
  447. goto err;
  448. }
  449. ret->in_group_flags = OPENSSL_malloc(n);
  450. if (!ret->in_group_flags) {
  451. goto err;
  452. }
  453. memset(ret->in_group_flags, 0, n);
  454. return ret;
  455. err:
  456. if (ret && ret->ciphers) {
  457. sk_SSL_CIPHER_free(ret->ciphers);
  458. }
  459. if (ret) {
  460. OPENSSL_free(ret);
  461. }
  462. return NULL;
  463. }
  464. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
  465. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
  466. void SSL_certs_clear(SSL *s) { ssl_cert_clear_certs(s->cert); }
  467. void SSL_free(SSL *s) {
  468. if (s == NULL) {
  469. return;
  470. }
  471. if (s->param) {
  472. X509_VERIFY_PARAM_free(s->param);
  473. }
  474. CRYPTO_free_ex_data(&g_ex_data_class_ssl, s, &s->ex_data);
  475. if (s->bbio != NULL) {
  476. /* If the buffering BIO is in place, pop it off */
  477. if (s->bbio == s->wbio) {
  478. s->wbio = BIO_pop(s->wbio);
  479. }
  480. BIO_free(s->bbio);
  481. s->bbio = NULL;
  482. }
  483. if (s->rbio != NULL) {
  484. BIO_free_all(s->rbio);
  485. }
  486. if (s->wbio != NULL && s->wbio != s->rbio) {
  487. BIO_free_all(s->wbio);
  488. }
  489. if (s->init_buf != NULL) {
  490. BUF_MEM_free(s->init_buf);
  491. }
  492. /* add extra stuff */
  493. if (s->cipher_list != NULL) {
  494. ssl_cipher_preference_list_free(s->cipher_list);
  495. }
  496. if (s->cipher_list_by_id != NULL) {
  497. sk_SSL_CIPHER_free(s->cipher_list_by_id);
  498. }
  499. if (s->session != NULL) {
  500. ssl_clear_bad_session(s);
  501. SSL_SESSION_free(s->session);
  502. }
  503. ssl_clear_cipher_ctx(s);
  504. if (s->cert != NULL) {
  505. ssl_cert_free(s->cert);
  506. }
  507. if (s->tlsext_hostname) {
  508. OPENSSL_free(s->tlsext_hostname);
  509. }
  510. if (s->initial_ctx) {
  511. SSL_CTX_free(s->initial_ctx);
  512. }
  513. if (s->tlsext_ecpointformatlist) {
  514. OPENSSL_free(s->tlsext_ecpointformatlist);
  515. }
  516. if (s->tlsext_ellipticcurvelist) {
  517. OPENSSL_free(s->tlsext_ellipticcurvelist);
  518. }
  519. if (s->alpn_client_proto_list) {
  520. OPENSSL_free(s->alpn_client_proto_list);
  521. }
  522. if (s->tlsext_channel_id_private) {
  523. EVP_PKEY_free(s->tlsext_channel_id_private);
  524. }
  525. if (s->psk_identity_hint) {
  526. OPENSSL_free(s->psk_identity_hint);
  527. }
  528. if (s->client_CA != NULL) {
  529. sk_X509_NAME_pop_free(s->client_CA, X509_NAME_free);
  530. }
  531. if (s->next_proto_negotiated) {
  532. OPENSSL_free(s->next_proto_negotiated);
  533. }
  534. if (s->srtp_profiles) {
  535. sk_SRTP_PROTECTION_PROFILE_free(s->srtp_profiles);
  536. }
  537. if (s->method != NULL) {
  538. s->method->ssl_free(s);
  539. }
  540. if (s->ctx) {
  541. SSL_CTX_free(s->ctx);
  542. }
  543. OPENSSL_free(s);
  544. }
  545. void SSL_set_bio(SSL *s, BIO *rbio, BIO *wbio) {
  546. /* If the output buffering BIO is still in place, remove it. */
  547. if (s->bbio != NULL) {
  548. if (s->wbio == s->bbio) {
  549. s->wbio = s->wbio->next_bio;
  550. s->bbio->next_bio = NULL;
  551. }
  552. }
  553. if (s->rbio != NULL && s->rbio != rbio) {
  554. BIO_free_all(s->rbio);
  555. }
  556. if (s->wbio != NULL && s->wbio != wbio && s->rbio != s->wbio) {
  557. BIO_free_all(s->wbio);
  558. }
  559. s->rbio = rbio;
  560. s->wbio = wbio;
  561. }
  562. BIO *SSL_get_rbio(const SSL *s) { return s->rbio; }
  563. BIO *SSL_get_wbio(const SSL *s) { return s->wbio; }
  564. int SSL_get_fd(const SSL *s) { return SSL_get_rfd(s); }
  565. int SSL_get_rfd(const SSL *s) {
  566. int ret = -1;
  567. BIO *b, *r;
  568. b = SSL_get_rbio(s);
  569. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  570. if (r != NULL) {
  571. BIO_get_fd(r, &ret);
  572. }
  573. return ret;
  574. }
  575. int SSL_get_wfd(const SSL *s) {
  576. int ret = -1;
  577. BIO *b, *r;
  578. b = SSL_get_wbio(s);
  579. r = BIO_find_type(b, BIO_TYPE_DESCRIPTOR);
  580. if (r != NULL) {
  581. BIO_get_fd(r, &ret);
  582. }
  583. return ret;
  584. }
  585. int SSL_set_fd(SSL *s, int fd) {
  586. int ret = 0;
  587. BIO *bio = NULL;
  588. bio = BIO_new(BIO_s_fd());
  589. if (bio == NULL) {
  590. OPENSSL_PUT_ERROR(SSL, SSL_set_fd, ERR_R_BUF_LIB);
  591. goto err;
  592. }
  593. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  594. SSL_set_bio(s, bio, bio);
  595. ret = 1;
  596. err:
  597. return ret;
  598. }
  599. int SSL_set_wfd(SSL *s, int fd) {
  600. int ret = 0;
  601. BIO *bio = NULL;
  602. if (s->rbio == NULL || BIO_method_type(s->rbio) != BIO_TYPE_FD ||
  603. (int)BIO_get_fd(s->rbio, NULL) != fd) {
  604. bio = BIO_new(BIO_s_fd());
  605. if (bio == NULL) {
  606. OPENSSL_PUT_ERROR(SSL, SSL_set_wfd, ERR_R_BUF_LIB);
  607. goto err;
  608. }
  609. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  610. SSL_set_bio(s, SSL_get_rbio(s), bio);
  611. } else {
  612. SSL_set_bio(s, SSL_get_rbio(s), SSL_get_rbio(s));
  613. }
  614. ret = 1;
  615. err:
  616. return ret;
  617. }
  618. int SSL_set_rfd(SSL *s, int fd) {
  619. int ret = 0;
  620. BIO *bio = NULL;
  621. if (s->wbio == NULL || BIO_method_type(s->wbio) != BIO_TYPE_FD ||
  622. (int)BIO_get_fd(s->wbio, NULL) != fd) {
  623. bio = BIO_new(BIO_s_fd());
  624. if (bio == NULL) {
  625. OPENSSL_PUT_ERROR(SSL, SSL_set_rfd, ERR_R_BUF_LIB);
  626. goto err;
  627. }
  628. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  629. SSL_set_bio(s, bio, SSL_get_wbio(s));
  630. } else {
  631. SSL_set_bio(s, SSL_get_wbio(s), SSL_get_wbio(s));
  632. }
  633. ret = 1;
  634. err:
  635. return ret;
  636. }
  637. /* return length of latest Finished message we sent, copy to 'buf' */
  638. size_t SSL_get_finished(const SSL *s, void *buf, size_t count) {
  639. size_t ret = 0;
  640. if (s->s3 != NULL) {
  641. ret = s->s3->tmp.finish_md_len;
  642. if (count > ret) {
  643. count = ret;
  644. }
  645. memcpy(buf, s->s3->tmp.finish_md, count);
  646. }
  647. return ret;
  648. }
  649. /* return length of latest Finished message we expected, copy to 'buf' */
  650. size_t SSL_get_peer_finished(const SSL *s, void *buf, size_t count) {
  651. size_t ret = 0;
  652. if (s->s3 != NULL) {
  653. ret = s->s3->tmp.peer_finish_md_len;
  654. if (count > ret) {
  655. count = ret;
  656. }
  657. memcpy(buf, s->s3->tmp.peer_finish_md, count);
  658. }
  659. return ret;
  660. }
  661. int SSL_get_verify_mode(const SSL *s) { return s->verify_mode; }
  662. int SSL_get_verify_depth(const SSL *s) {
  663. return X509_VERIFY_PARAM_get_depth(s->param);
  664. }
  665. int (*SSL_get_verify_callback(const SSL *s))(int, X509_STORE_CTX *) {
  666. return s->verify_callback;
  667. }
  668. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
  669. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  670. return X509_VERIFY_PARAM_get_depth(ctx->param);
  671. }
  672. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(int, X509_STORE_CTX *) {
  673. return ctx->default_verify_callback;
  674. }
  675. void SSL_set_verify(SSL *s, int mode,
  676. int (*callback)(int ok, X509_STORE_CTX *ctx)) {
  677. s->verify_mode = mode;
  678. if (callback != NULL) {
  679. s->verify_callback = callback;
  680. }
  681. }
  682. void SSL_set_verify_depth(SSL *s, int depth) {
  683. X509_VERIFY_PARAM_set_depth(s->param, depth);
  684. }
  685. void SSL_set_read_ahead(SSL *s, int yes) { s->read_ahead = yes; }
  686. int SSL_get_read_ahead(const SSL *s) { return s->read_ahead; }
  687. int SSL_pending(const SSL *s) {
  688. /* SSL_pending cannot work properly if read-ahead is enabled
  689. * (SSL_[CTX_]ctrl(..., SSL_CTRL_SET_READ_AHEAD, 1, NULL)), and it is
  690. * impossible to fix since SSL_pending cannot report errors that may be
  691. * observed while scanning the new data. (Note that SSL_pending() is often
  692. * used as a boolean value, so we'd better not return -1.). */
  693. return s->method->ssl_pending(s);
  694. }
  695. X509 *SSL_get_peer_certificate(const SSL *s) {
  696. X509 *r;
  697. if (s == NULL || s->session == NULL) {
  698. r = NULL;
  699. } else {
  700. r = s->session->peer;
  701. }
  702. if (r == NULL) {
  703. return NULL;
  704. }
  705. return X509_up_ref(r);
  706. }
  707. STACK_OF(X509) * SSL_get_peer_cert_chain(const SSL *s) {
  708. STACK_OF(X509) * r;
  709. if (s == NULL || s->session == NULL || s->session->sess_cert == NULL) {
  710. r = NULL;
  711. } else {
  712. r = s->session->sess_cert->cert_chain;
  713. }
  714. /* If we are a client, cert_chain includes the peer's own certificate; if we
  715. * are a server, it does not. */
  716. return r;
  717. }
  718. /* Fix this so it checks all the valid key/cert options */
  719. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  720. if (ctx == NULL || ctx->cert == NULL || ctx->cert->key->x509 == NULL) {
  721. OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
  722. SSL_R_NO_CERTIFICATE_ASSIGNED);
  723. return 0;
  724. }
  725. if (ctx->cert->key->privatekey == NULL) {
  726. OPENSSL_PUT_ERROR(SSL, SSL_CTX_check_private_key,
  727. SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  728. return 0;
  729. }
  730. return X509_check_private_key(ctx->cert->key->x509,
  731. ctx->cert->key->privatekey);
  732. }
  733. /* Fix this function so that it takes an optional type parameter */
  734. int SSL_check_private_key(const SSL *ssl) {
  735. if (ssl == NULL) {
  736. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key, ERR_R_PASSED_NULL_PARAMETER);
  737. return 0;
  738. }
  739. if (ssl->cert == NULL) {
  740. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
  741. SSL_R_NO_CERTIFICATE_ASSIGNED);
  742. return 0;
  743. }
  744. if (ssl->cert->key->x509 == NULL) {
  745. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
  746. SSL_R_NO_CERTIFICATE_ASSIGNED);
  747. return 0;
  748. }
  749. if (ssl->cert->key->privatekey == NULL) {
  750. OPENSSL_PUT_ERROR(SSL, SSL_check_private_key,
  751. SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  752. return 0;
  753. }
  754. return X509_check_private_key(ssl->cert->key->x509,
  755. ssl->cert->key->privatekey);
  756. }
  757. int SSL_accept(SSL *s) {
  758. if (s->handshake_func == 0) {
  759. /* Not properly initialized yet */
  760. SSL_set_accept_state(s);
  761. }
  762. if (s->handshake_func != s->method->ssl_accept) {
  763. OPENSSL_PUT_ERROR(SSL, SSL_accept, ERR_R_INTERNAL_ERROR);
  764. return -1;
  765. }
  766. return s->handshake_func(s);
  767. }
  768. int SSL_connect(SSL *s) {
  769. if (s->handshake_func == 0) {
  770. /* Not properly initialized yet */
  771. SSL_set_connect_state(s);
  772. }
  773. if (s->handshake_func != s->method->ssl_connect) {
  774. OPENSSL_PUT_ERROR(SSL, SSL_connect, ERR_R_INTERNAL_ERROR);
  775. return -1;
  776. }
  777. return s->handshake_func(s);
  778. }
  779. long SSL_get_default_timeout(const SSL *s) {
  780. return SSL_DEFAULT_SESSION_TIMEOUT;
  781. }
  782. int SSL_read(SSL *s, void *buf, int num) {
  783. if (s->handshake_func == 0) {
  784. OPENSSL_PUT_ERROR(SSL, SSL_read, SSL_R_UNINITIALIZED);
  785. return -1;
  786. }
  787. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  788. s->rwstate = SSL_NOTHING;
  789. return 0;
  790. }
  791. return s->method->ssl_read(s, buf, num);
  792. }
  793. int SSL_peek(SSL *s, void *buf, int num) {
  794. if (s->handshake_func == 0) {
  795. OPENSSL_PUT_ERROR(SSL, SSL_peek, SSL_R_UNINITIALIZED);
  796. return -1;
  797. }
  798. if (s->shutdown & SSL_RECEIVED_SHUTDOWN) {
  799. return 0;
  800. }
  801. return s->method->ssl_peek(s, buf, num);
  802. }
  803. int SSL_write(SSL *s, const void *buf, int num) {
  804. if (s->handshake_func == 0) {
  805. OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_UNINITIALIZED);
  806. return -1;
  807. }
  808. if (s->shutdown & SSL_SENT_SHUTDOWN) {
  809. s->rwstate = SSL_NOTHING;
  810. OPENSSL_PUT_ERROR(SSL, SSL_write, SSL_R_PROTOCOL_IS_SHUTDOWN);
  811. return -1;
  812. }
  813. return s->method->ssl_write(s, buf, num);
  814. }
  815. int SSL_shutdown(SSL *s) {
  816. /* Note that this function behaves differently from what one might expect.
  817. * Return values are 0 for no success (yet), 1 for success; but calling it
  818. * once is usually not enough, even if blocking I/O is used (see
  819. * ssl3_shutdown). */
  820. if (s->handshake_func == 0) {
  821. OPENSSL_PUT_ERROR(SSL, SSL_shutdown, SSL_R_UNINITIALIZED);
  822. return -1;
  823. }
  824. if (!SSL_in_init(s)) {
  825. return s->method->ssl_shutdown(s);
  826. }
  827. return 1;
  828. }
  829. int SSL_renegotiate(SSL *s) {
  830. if (SSL_IS_DTLS(s)) {
  831. /* Renegotiation is not supported for DTLS. */
  832. OPENSSL_PUT_ERROR(SSL, SSL_renegotiate, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  833. return 0;
  834. }
  835. if (s->renegotiate == 0) {
  836. s->renegotiate = 1;
  837. }
  838. s->new_session = 1;
  839. return s->method->ssl_renegotiate(s);
  840. }
  841. int SSL_renegotiate_pending(SSL *s) {
  842. /* becomes true when negotiation is requested; false again once a handshake
  843. * has finished */
  844. return s->renegotiate != 0;
  845. }
  846. long SSL_ctrl(SSL *s, int cmd, long larg, void *parg) {
  847. long l;
  848. switch (cmd) {
  849. case SSL_CTRL_GET_READ_AHEAD:
  850. return s->read_ahead;
  851. case SSL_CTRL_SET_READ_AHEAD:
  852. l = s->read_ahead;
  853. s->read_ahead = larg;
  854. return l;
  855. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  856. s->msg_callback_arg = parg;
  857. return 1;
  858. case SSL_CTRL_OPTIONS:
  859. return s->options |= larg;
  860. case SSL_CTRL_CLEAR_OPTIONS:
  861. return s->options &= ~larg;
  862. case SSL_CTRL_MODE:
  863. return s->mode |= larg;
  864. case SSL_CTRL_CLEAR_MODE:
  865. return s->mode &= ~larg;
  866. case SSL_CTRL_GET_MAX_CERT_LIST:
  867. return s->max_cert_list;
  868. case SSL_CTRL_SET_MAX_CERT_LIST:
  869. l = s->max_cert_list;
  870. s->max_cert_list = larg;
  871. return l;
  872. case SSL_CTRL_SET_MTU:
  873. if (larg < (long)dtls1_min_mtu()) {
  874. return 0;
  875. }
  876. if (SSL_IS_DTLS(s)) {
  877. s->d1->mtu = larg;
  878. return larg;
  879. }
  880. return 0;
  881. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  882. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
  883. return 0;
  884. }
  885. s->max_send_fragment = larg;
  886. return 1;
  887. case SSL_CTRL_GET_RI_SUPPORT:
  888. if (s->s3) {
  889. return s->s3->send_connection_binding;
  890. }
  891. return 0;
  892. case SSL_CTRL_CERT_FLAGS:
  893. return s->cert->cert_flags |= larg;
  894. case SSL_CTRL_CLEAR_CERT_FLAGS:
  895. return s->cert->cert_flags &= ~larg;
  896. case SSL_CTRL_GET_RAW_CIPHERLIST:
  897. if (parg) {
  898. if (s->cert->ciphers_raw == NULL) {
  899. return 0;
  900. }
  901. *(uint8_t **)parg = s->cert->ciphers_raw;
  902. return (int)s->cert->ciphers_rawlen;
  903. }
  904. /* Passing a NULL |parg| returns the size of a single
  905. * cipher suite value. */
  906. return 2;
  907. default:
  908. return s->method->ssl_ctrl(s, cmd, larg, parg);
  909. }
  910. }
  911. long SSL_callback_ctrl(SSL *s, int cmd, void (*fp)(void)) {
  912. switch (cmd) {
  913. case SSL_CTRL_SET_MSG_CALLBACK:
  914. s->msg_callback =
  915. (void (*)(int write_p, int version, int content_type, const void *buf,
  916. size_t len, SSL *ssl, void *arg))(fp);
  917. return 1;
  918. default:
  919. return s->method->ssl_callback_ctrl(s, cmd, fp);
  920. }
  921. }
  922. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  923. long SSL_CTX_ctrl(SSL_CTX *ctx, int cmd, long larg, void *parg) {
  924. long l;
  925. switch (cmd) {
  926. case SSL_CTRL_GET_READ_AHEAD:
  927. return ctx->read_ahead;
  928. case SSL_CTRL_SET_READ_AHEAD:
  929. l = ctx->read_ahead;
  930. ctx->read_ahead = larg;
  931. return l;
  932. case SSL_CTRL_SET_MSG_CALLBACK_ARG:
  933. ctx->msg_callback_arg = parg;
  934. return 1;
  935. case SSL_CTRL_GET_MAX_CERT_LIST:
  936. return ctx->max_cert_list;
  937. case SSL_CTRL_SET_MAX_CERT_LIST:
  938. l = ctx->max_cert_list;
  939. ctx->max_cert_list = larg;
  940. return l;
  941. case SSL_CTRL_SET_SESS_CACHE_SIZE:
  942. l = ctx->session_cache_size;
  943. ctx->session_cache_size = larg;
  944. return l;
  945. case SSL_CTRL_GET_SESS_CACHE_SIZE:
  946. return ctx->session_cache_size;
  947. case SSL_CTRL_SET_SESS_CACHE_MODE:
  948. l = ctx->session_cache_mode;
  949. ctx->session_cache_mode = larg;
  950. return l;
  951. case SSL_CTRL_GET_SESS_CACHE_MODE:
  952. return ctx->session_cache_mode;
  953. case SSL_CTRL_SESS_NUMBER:
  954. return lh_SSL_SESSION_num_items(ctx->sessions);
  955. case SSL_CTRL_SESS_CONNECT:
  956. case SSL_CTRL_SESS_CONNECT_GOOD:
  957. case SSL_CTRL_SESS_CONNECT_RENEGOTIATE:
  958. case SSL_CTRL_SESS_ACCEPT:
  959. case SSL_CTRL_SESS_ACCEPT_GOOD:
  960. case SSL_CTRL_SESS_ACCEPT_RENEGOTIATE:
  961. case SSL_CTRL_SESS_HIT:
  962. case SSL_CTRL_SESS_CB_HIT:
  963. case SSL_CTRL_SESS_MISSES:
  964. case SSL_CTRL_SESS_TIMEOUTS:
  965. case SSL_CTRL_SESS_CACHE_FULL:
  966. /* Statistics are no longer supported.
  967. * TODO(davidben): Try to remove the accessors altogether. */
  968. return 0;
  969. case SSL_CTRL_OPTIONS:
  970. return ctx->options |= larg;
  971. case SSL_CTRL_CLEAR_OPTIONS:
  972. return ctx->options &= ~larg;
  973. case SSL_CTRL_MODE:
  974. return ctx->mode |= larg;
  975. case SSL_CTRL_CLEAR_MODE:
  976. return ctx->mode &= ~larg;
  977. case SSL_CTRL_SET_MAX_SEND_FRAGMENT:
  978. if (larg < 512 || larg > SSL3_RT_MAX_PLAIN_LENGTH) {
  979. return 0;
  980. }
  981. ctx->max_send_fragment = larg;
  982. return 1;
  983. case SSL_CTRL_CERT_FLAGS:
  984. return ctx->cert->cert_flags |= larg;
  985. case SSL_CTRL_CLEAR_CERT_FLAGS:
  986. return ctx->cert->cert_flags &= ~larg;
  987. default:
  988. return ctx->method->ssl_ctx_ctrl(ctx, cmd, larg, parg);
  989. }
  990. }
  991. long SSL_CTX_callback_ctrl(SSL_CTX *ctx, int cmd, void (*fp)(void)) {
  992. switch (cmd) {
  993. case SSL_CTRL_SET_MSG_CALLBACK:
  994. ctx->msg_callback =
  995. (void (*)(int write_p, int version, int content_type, const void *buf,
  996. size_t len, SSL *ssl, void *arg))(fp);
  997. return 1;
  998. default:
  999. return ctx->method->ssl_ctx_callback_ctrl(ctx, cmd, fp);
  1000. }
  1001. }
  1002. int ssl_cipher_id_cmp(const void *in_a, const void *in_b) {
  1003. long l;
  1004. const SSL_CIPHER *a = in_a;
  1005. const SSL_CIPHER *b = in_b;
  1006. const long a_id = a->id;
  1007. const long b_id = b->id;
  1008. l = a_id - b_id;
  1009. if (l == 0L) {
  1010. return 0;
  1011. } else {
  1012. return (l > 0) ? 1 : -1;
  1013. }
  1014. }
  1015. int ssl_cipher_ptr_id_cmp(const SSL_CIPHER **ap, const SSL_CIPHER **bp) {
  1016. long l;
  1017. const long a_id = (*ap)->id;
  1018. const long b_id = (*bp)->id;
  1019. l = a_id - b_id;
  1020. if (l == 0) {
  1021. return 0;
  1022. } else {
  1023. return (l > 0) ? 1 : -1;
  1024. }
  1025. }
  1026. /* return a STACK of the ciphers available for the SSL and in order of
  1027. * preference */
  1028. STACK_OF(SSL_CIPHER) * SSL_get_ciphers(const SSL *s) {
  1029. if (s == NULL) {
  1030. return NULL;
  1031. }
  1032. if (s->cipher_list != NULL) {
  1033. return s->cipher_list->ciphers;
  1034. }
  1035. if (s->version >= TLS1_1_VERSION && s->ctx != NULL &&
  1036. s->ctx->cipher_list_tls11 != NULL) {
  1037. return s->ctx->cipher_list_tls11->ciphers;
  1038. }
  1039. if (s->ctx != NULL && s->ctx->cipher_list != NULL) {
  1040. return s->ctx->cipher_list->ciphers;
  1041. }
  1042. return NULL;
  1043. }
  1044. /* return a STACK of the ciphers available for the SSL and in order of
  1045. * algorithm id */
  1046. STACK_OF(SSL_CIPHER) * ssl_get_ciphers_by_id(SSL *s) {
  1047. if (s == NULL) {
  1048. return NULL;
  1049. }
  1050. if (s->cipher_list_by_id != NULL) {
  1051. return s->cipher_list_by_id;
  1052. }
  1053. if (s->ctx != NULL && s->ctx->cipher_list_by_id != NULL) {
  1054. return s->ctx->cipher_list_by_id;
  1055. }
  1056. return NULL;
  1057. }
  1058. /* The old interface to get the same thing as SSL_get_ciphers() */
  1059. const char *SSL_get_cipher_list(const SSL *s, int n) {
  1060. const SSL_CIPHER *c;
  1061. STACK_OF(SSL_CIPHER) * sk;
  1062. if (s == NULL) {
  1063. return NULL;
  1064. }
  1065. sk = SSL_get_ciphers(s);
  1066. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1067. return NULL;
  1068. }
  1069. c = sk_SSL_CIPHER_value(sk, n);
  1070. if (c == NULL) {
  1071. return NULL;
  1072. }
  1073. return c->name;
  1074. }
  1075. /* specify the ciphers to be used by default by the SSL_CTX */
  1076. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1077. STACK_OF(SSL_CIPHER) *sk;
  1078. sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list,
  1079. &ctx->cipher_list_by_id, str);
  1080. /* ssl_create_cipher_list may return an empty stack if it was unable to find
  1081. * a cipher matching the given rule string (for example if the rule string
  1082. * specifies a cipher which has been disabled). This is not an error as far
  1083. * as ssl_create_cipher_list is concerned, and hence ctx->cipher_list and
  1084. * ctx->cipher_list_by_id has been updated. */
  1085. if (sk == NULL) {
  1086. return 0;
  1087. } else if (sk_SSL_CIPHER_num(sk) == 0) {
  1088. OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
  1089. return 0;
  1090. }
  1091. return 1;
  1092. }
  1093. int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
  1094. STACK_OF(SSL_CIPHER) *sk;
  1095. sk = ssl_create_cipher_list(ctx->method, &ctx->cipher_list_tls11, NULL, str);
  1096. if (sk == NULL) {
  1097. return 0;
  1098. } else if (sk_SSL_CIPHER_num(sk) == 0) {
  1099. OPENSSL_PUT_ERROR(SSL, SSL_CTX_set_cipher_list_tls11,
  1100. SSL_R_NO_CIPHER_MATCH);
  1101. return 0;
  1102. }
  1103. return 1;
  1104. }
  1105. /* specify the ciphers to be used by the SSL */
  1106. int SSL_set_cipher_list(SSL *s, const char *str) {
  1107. STACK_OF(SSL_CIPHER) *sk;
  1108. sk = ssl_create_cipher_list(s->ctx->method, &s->cipher_list,
  1109. &s->cipher_list_by_id, str);
  1110. /* see comment in SSL_CTX_set_cipher_list */
  1111. if (sk == NULL) {
  1112. return 0;
  1113. } else if (sk_SSL_CIPHER_num(sk) == 0) {
  1114. OPENSSL_PUT_ERROR(SSL, SSL_set_cipher_list, SSL_R_NO_CIPHER_MATCH);
  1115. return 0;
  1116. }
  1117. return 1;
  1118. }
  1119. int ssl_cipher_list_to_bytes(SSL *s, STACK_OF(SSL_CIPHER) *sk, uint8_t *p) {
  1120. size_t i;
  1121. const SSL_CIPHER *c;
  1122. CERT *ct = s->cert;
  1123. uint8_t *q;
  1124. /* Set disabled masks for this session */
  1125. ssl_set_client_disabled(s);
  1126. if (sk == NULL) {
  1127. return 0;
  1128. }
  1129. q = p;
  1130. for (i = 0; i < sk_SSL_CIPHER_num(sk); i++) {
  1131. c = sk_SSL_CIPHER_value(sk, i);
  1132. /* Skip disabled ciphers */
  1133. if (c->algorithm_ssl & ct->mask_ssl ||
  1134. c->algorithm_mkey & ct->mask_k ||
  1135. c->algorithm_auth & ct->mask_a) {
  1136. continue;
  1137. }
  1138. s2n(ssl3_get_cipher_value(c), p);
  1139. }
  1140. /* If all ciphers were disabled, return the error to the caller. */
  1141. if (p == q) {
  1142. return 0;
  1143. }
  1144. /* Add SCSVs. */
  1145. if (!s->renegotiate) {
  1146. s2n(SSL3_CK_SCSV & 0xffff, p);
  1147. }
  1148. if (s->mode & SSL_MODE_SEND_FALLBACK_SCSV) {
  1149. s2n(SSL3_CK_FALLBACK_SCSV & 0xffff, p);
  1150. }
  1151. return p - q;
  1152. }
  1153. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *s, const CBS *cbs) {
  1154. CBS cipher_suites = *cbs;
  1155. const SSL_CIPHER *c;
  1156. STACK_OF(SSL_CIPHER) * sk;
  1157. if (s->s3) {
  1158. s->s3->send_connection_binding = 0;
  1159. }
  1160. if (CBS_len(&cipher_suites) % 2 != 0) {
  1161. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
  1162. SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  1163. return NULL;
  1164. }
  1165. sk = sk_SSL_CIPHER_new_null();
  1166. if (sk == NULL) {
  1167. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
  1168. goto err;
  1169. }
  1170. if (!CBS_stow(&cipher_suites, &s->cert->ciphers_raw,
  1171. &s->cert->ciphers_rawlen)) {
  1172. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
  1173. goto err;
  1174. }
  1175. while (CBS_len(&cipher_suites) > 0) {
  1176. uint16_t cipher_suite;
  1177. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  1178. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_INTERNAL_ERROR);
  1179. goto err;
  1180. }
  1181. /* Check for SCSV. */
  1182. if (s->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
  1183. /* SCSV is fatal if renegotiating. */
  1184. if (s->renegotiate) {
  1185. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
  1186. SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1187. ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1188. goto err;
  1189. }
  1190. s->s3->send_connection_binding = 1;
  1191. continue;
  1192. }
  1193. /* Check for FALLBACK_SCSV. */
  1194. if (s->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  1195. uint16_t max_version = ssl3_get_max_server_version(s);
  1196. if (SSL_IS_DTLS(s) ? (uint16_t)s->version > max_version
  1197. : (uint16_t)s->version < max_version) {
  1198. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list,
  1199. SSL_R_INAPPROPRIATE_FALLBACK);
  1200. ssl3_send_alert(s, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
  1201. goto err;
  1202. }
  1203. continue;
  1204. }
  1205. c = ssl3_get_cipher_by_value(cipher_suite);
  1206. if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
  1207. OPENSSL_PUT_ERROR(SSL, ssl_bytes_to_cipher_list, ERR_R_MALLOC_FAILURE);
  1208. goto err;
  1209. }
  1210. }
  1211. return sk;
  1212. err:
  1213. if (sk != NULL) {
  1214. sk_SSL_CIPHER_free(sk);
  1215. }
  1216. return NULL;
  1217. }
  1218. /* return a servername extension value if provided in Client Hello, or NULL. So
  1219. * far, only host_name types are defined (RFC 3546). */
  1220. const char *SSL_get_servername(const SSL *s, const int type) {
  1221. if (type != TLSEXT_NAMETYPE_host_name) {
  1222. return NULL;
  1223. }
  1224. return s->session && !s->tlsext_hostname ? s->session->tlsext_hostname
  1225. : s->tlsext_hostname;
  1226. }
  1227. int SSL_get_servername_type(const SSL *s) {
  1228. if (s->session &&
  1229. (!s->tlsext_hostname ? s->session->tlsext_hostname : s->tlsext_hostname)) {
  1230. return TLSEXT_NAMETYPE_host_name;
  1231. }
  1232. return -1;
  1233. }
  1234. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1235. ctx->signed_cert_timestamps_enabled = 1;
  1236. }
  1237. int SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1238. ssl->signed_cert_timestamps_enabled = 1;
  1239. return 1;
  1240. }
  1241. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1242. ctx->ocsp_stapling_enabled = 1;
  1243. }
  1244. int SSL_enable_ocsp_stapling(SSL *ssl) {
  1245. ssl->ocsp_stapling_enabled = 1;
  1246. return 1;
  1247. }
  1248. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1249. size_t *out_len) {
  1250. SSL_SESSION *session = ssl->session;
  1251. *out_len = 0;
  1252. *out = NULL;
  1253. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1254. return;
  1255. }
  1256. *out = session->tlsext_signed_cert_timestamp_list;
  1257. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1258. }
  1259. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1260. size_t *out_len) {
  1261. SSL_SESSION *session = ssl->session;
  1262. *out_len = 0;
  1263. *out = NULL;
  1264. if (ssl->server || !session || !session->ocsp_response) {
  1265. return;
  1266. }
  1267. *out = session->ocsp_response;
  1268. *out_len = session->ocsp_response_length;
  1269. }
  1270. /* SSL_select_next_proto implements the standard protocol selection. It is
  1271. * expected that this function is called from the callback set by
  1272. * SSL_CTX_set_next_proto_select_cb.
  1273. *
  1274. * The protocol data is assumed to be a vector of 8-bit, length prefixed byte
  1275. * strings. The length byte itself is not included in the length. A byte
  1276. * string of length 0 is invalid. No byte string may be truncated.
  1277. *
  1278. * The current, but experimental algorithm for selecting the protocol is:
  1279. *
  1280. * 1) If the server doesn't support NPN then this is indicated to the
  1281. * callback. In this case, the client application has to abort the connection
  1282. * or have a default application level protocol.
  1283. *
  1284. * 2) If the server supports NPN, but advertises an empty list then the
  1285. * client selects the first protcol in its list, but indicates via the
  1286. * API that this fallback case was enacted.
  1287. *
  1288. * 3) Otherwise, the client finds the first protocol in the server's list
  1289. * that it supports and selects this protocol. This is because it's
  1290. * assumed that the server has better information about which protocol
  1291. * a client should use.
  1292. *
  1293. * 4) If the client doesn't support any of the server's advertised
  1294. * protocols, then this is treated the same as case 2.
  1295. *
  1296. * It returns either
  1297. * OPENSSL_NPN_NEGOTIATED if a common protocol was found, or
  1298. * OPENSSL_NPN_NO_OVERLAP if the fallback case was reached.
  1299. */
  1300. int SSL_select_next_proto(uint8_t **out, uint8_t *outlen, const uint8_t *server,
  1301. unsigned int server_len, const uint8_t *client,
  1302. unsigned int client_len) {
  1303. unsigned int i, j;
  1304. const uint8_t *result;
  1305. int status = OPENSSL_NPN_UNSUPPORTED;
  1306. /* For each protocol in server preference order, see if we support it. */
  1307. for (i = 0; i < server_len;) {
  1308. for (j = 0; j < client_len;) {
  1309. if (server[i] == client[j] &&
  1310. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1311. /* We found a match */
  1312. result = &server[i];
  1313. status = OPENSSL_NPN_NEGOTIATED;
  1314. goto found;
  1315. }
  1316. j += client[j];
  1317. j++;
  1318. }
  1319. i += server[i];
  1320. i++;
  1321. }
  1322. /* There's no overlap between our protocols and the server's list. */
  1323. result = client;
  1324. status = OPENSSL_NPN_NO_OVERLAP;
  1325. found:
  1326. *out = (uint8_t *)result + 1;
  1327. *outlen = result[0];
  1328. return status;
  1329. }
  1330. /* SSL_get0_next_proto_negotiated sets *data and *len to point to the client's
  1331. * requested protocol for this connection and returns 0. If the client didn't
  1332. * request any protocol, then *data is set to NULL.
  1333. *
  1334. * Note that the client can request any protocol it chooses. The value returned
  1335. * from this function need not be a member of the list of supported protocols
  1336. * provided by the callback. */
  1337. void SSL_get0_next_proto_negotiated(const SSL *s, const uint8_t **data,
  1338. unsigned *len) {
  1339. *data = s->next_proto_negotiated;
  1340. if (!*data) {
  1341. *len = 0;
  1342. } else {
  1343. *len = s->next_proto_negotiated_len;
  1344. }
  1345. }
  1346. /* SSL_CTX_set_next_protos_advertised_cb sets a callback that is called when a
  1347. * TLS server needs a list of supported protocols for Next Protocol
  1348. * Negotiation. The returned list must be in wire format. The list is returned
  1349. * by setting |out| to point to it and |outlen| to its length. This memory will
  1350. * not be modified, but one should assume that the SSL* keeps a reference to
  1351. * it.
  1352. *
  1353. * The callback should return SSL_TLSEXT_ERR_OK if it wishes to advertise.
  1354. * Otherwise, no such extension will be included in the ServerHello. */
  1355. void SSL_CTX_set_next_protos_advertised_cb(
  1356. SSL_CTX *ctx,
  1357. int (*cb)(SSL *ssl, const uint8_t **out, unsigned int *outlen, void *arg),
  1358. void *arg) {
  1359. ctx->next_protos_advertised_cb = cb;
  1360. ctx->next_protos_advertised_cb_arg = arg;
  1361. }
  1362. /* SSL_CTX_set_next_proto_select_cb sets a callback that is called when a
  1363. * client needs to select a protocol from the server's provided list. |out|
  1364. * must be set to point to the selected protocol (which may be within |in|).
  1365. * The length of the protocol name must be written into |outlen|. The server's
  1366. * advertised protocols are provided in |in| and |inlen|. The callback can
  1367. * assume that |in| is syntactically valid.
  1368. *
  1369. * The client must select a protocol. It is fatal to the connection if this
  1370. * callback returns a value other than SSL_TLSEXT_ERR_OK.
  1371. */
  1372. void SSL_CTX_set_next_proto_select_cb(
  1373. SSL_CTX *ctx, int (*cb)(SSL *s, uint8_t **out, uint8_t *outlen,
  1374. const uint8_t *in, unsigned int inlen, void *arg),
  1375. void *arg) {
  1376. ctx->next_proto_select_cb = cb;
  1377. ctx->next_proto_select_cb_arg = arg;
  1378. }
  1379. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1380. unsigned protos_len) {
  1381. if (ctx->alpn_client_proto_list) {
  1382. OPENSSL_free(ctx->alpn_client_proto_list);
  1383. }
  1384. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1385. if (!ctx->alpn_client_proto_list) {
  1386. return 1;
  1387. }
  1388. ctx->alpn_client_proto_list_len = protos_len;
  1389. return 0;
  1390. }
  1391. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1392. if (ssl->alpn_client_proto_list) {
  1393. OPENSSL_free(ssl->alpn_client_proto_list);
  1394. }
  1395. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1396. if (!ssl->alpn_client_proto_list) {
  1397. return 1;
  1398. }
  1399. ssl->alpn_client_proto_list_len = protos_len;
  1400. return 0;
  1401. }
  1402. /* SSL_CTX_set_alpn_select_cb sets a callback function on |ctx| that is called
  1403. * during ClientHello processing in order to select an ALPN protocol from the
  1404. * client's list of offered protocols. */
  1405. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1406. int (*cb)(SSL *ssl, const uint8_t **out,
  1407. uint8_t *outlen, const uint8_t *in,
  1408. unsigned int inlen, void *arg),
  1409. void *arg) {
  1410. ctx->alpn_select_cb = cb;
  1411. ctx->alpn_select_cb_arg = arg;
  1412. }
  1413. /* SSL_get0_alpn_selected gets the selected ALPN protocol (if any) from |ssl|.
  1414. * On return it sets |*data| to point to |*len| bytes of protocol name (not
  1415. * including the leading length-prefix byte). If the server didn't respond with
  1416. * a negotiated protocol then |*len| will be zero. */
  1417. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **data,
  1418. unsigned *len) {
  1419. *data = NULL;
  1420. if (ssl->s3) {
  1421. *data = ssl->s3->alpn_selected;
  1422. }
  1423. if (*data == NULL) {
  1424. *len = 0;
  1425. } else {
  1426. *len = ssl->s3->alpn_selected_len;
  1427. }
  1428. }
  1429. int SSL_export_keying_material(SSL *s, uint8_t *out, size_t out_len,
  1430. const char *label, size_t label_len,
  1431. const uint8_t *context, size_t context_len,
  1432. int use_context) {
  1433. if (s->version < TLS1_VERSION) {
  1434. return 0;
  1435. }
  1436. return s->enc_method->export_keying_material(
  1437. s, out, out_len, label, label_len, context, context_len, use_context);
  1438. }
  1439. static uint32_t ssl_session_hash(const SSL_SESSION *a) {
  1440. uint32_t hash =
  1441. ((uint32_t)a->session_id[0]) ||
  1442. ((uint32_t)a->session_id[1] << 8) ||
  1443. ((uint32_t)a->session_id[2] << 16) ||
  1444. ((uint32_t)a->session_id[3] << 24);
  1445. return hash;
  1446. }
  1447. /* NB: If this function (or indeed the hash function which uses a sort of
  1448. * coarser function than this one) is changed, ensure
  1449. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  1450. * able to construct an SSL_SESSION that will collide with any existing session
  1451. * with a matching session ID. */
  1452. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  1453. if (a->ssl_version != b->ssl_version) {
  1454. return 1;
  1455. }
  1456. if (a->session_id_length != b->session_id_length) {
  1457. return 1;
  1458. }
  1459. return memcmp(a->session_id, b->session_id, a->session_id_length);
  1460. }
  1461. SSL_CTX *SSL_CTX_new(const SSL_METHOD *meth) {
  1462. SSL_CTX *ret = NULL;
  1463. if (meth == NULL) {
  1464. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_NULL_SSL_METHOD_PASSED);
  1465. return NULL;
  1466. }
  1467. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  1468. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  1469. goto err;
  1470. }
  1471. ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
  1472. if (ret == NULL) {
  1473. goto err;
  1474. }
  1475. memset(ret, 0, sizeof(SSL_CTX));
  1476. ret->method = meth->method;
  1477. ret->cert_store = NULL;
  1478. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  1479. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  1480. ret->session_cache_head = NULL;
  1481. ret->session_cache_tail = NULL;
  1482. /* We take the system default */
  1483. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  1484. ret->new_session_cb = 0;
  1485. ret->remove_session_cb = 0;
  1486. ret->get_session_cb = 0;
  1487. ret->generate_session_id = 0;
  1488. ret->references = 1;
  1489. ret->quiet_shutdown = 0;
  1490. ret->info_callback = NULL;
  1491. ret->app_verify_callback = 0;
  1492. ret->app_verify_arg = NULL;
  1493. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  1494. ret->read_ahead = 0;
  1495. ret->msg_callback = 0;
  1496. ret->msg_callback_arg = NULL;
  1497. ret->verify_mode = SSL_VERIFY_NONE;
  1498. ret->sid_ctx_length = 0;
  1499. ret->default_verify_callback = NULL;
  1500. ret->cert = ssl_cert_new();
  1501. if (ret->cert == NULL) {
  1502. goto err;
  1503. }
  1504. ret->default_passwd_callback = 0;
  1505. ret->default_passwd_callback_userdata = NULL;
  1506. ret->client_cert_cb = 0;
  1507. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  1508. if (ret->sessions == NULL) {
  1509. goto err;
  1510. }
  1511. ret->cert_store = X509_STORE_new();
  1512. if (ret->cert_store == NULL) {
  1513. goto err;
  1514. }
  1515. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  1516. &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
  1517. if (ret->cipher_list == NULL ||
  1518. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  1519. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  1520. goto err2;
  1521. }
  1522. ret->param = X509_VERIFY_PARAM_new();
  1523. if (!ret->param) {
  1524. goto err;
  1525. }
  1526. ret->client_CA = sk_X509_NAME_new_null();
  1527. if (ret->client_CA == NULL) {
  1528. goto err;
  1529. }
  1530. CRYPTO_new_ex_data(&g_ex_data_class_ssl_ctx, ret, &ret->ex_data);
  1531. ret->extra_certs = NULL;
  1532. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1533. ret->tlsext_servername_callback = 0;
  1534. ret->tlsext_servername_arg = NULL;
  1535. /* Setup RFC4507 ticket keys */
  1536. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  1537. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  1538. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  1539. ret->options |= SSL_OP_NO_TICKET;
  1540. }
  1541. ret->tlsext_status_cb = 0;
  1542. ret->tlsext_status_arg = NULL;
  1543. ret->next_protos_advertised_cb = 0;
  1544. ret->next_proto_select_cb = 0;
  1545. ret->psk_identity_hint = NULL;
  1546. ret->psk_client_callback = NULL;
  1547. ret->psk_server_callback = NULL;
  1548. /* Default is to connect to non-RI servers. When RI is more widely deployed
  1549. * might change this. */
  1550. ret->options |= SSL_OP_LEGACY_SERVER_CONNECT;
  1551. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  1552. * uses of SSL_METHOD. */
  1553. if (meth->version != 0) {
  1554. SSL_CTX_set_max_version(ret, meth->version);
  1555. SSL_CTX_set_min_version(ret, meth->version);
  1556. }
  1557. return ret;
  1558. err:
  1559. OPENSSL_PUT_ERROR(SSL, SSL_CTX_new, ERR_R_MALLOC_FAILURE);
  1560. err2:
  1561. if (ret != NULL) {
  1562. SSL_CTX_free(ret);
  1563. }
  1564. return NULL;
  1565. }
  1566. void SSL_CTX_free(SSL_CTX *a) {
  1567. int i;
  1568. if (a == NULL) {
  1569. return;
  1570. }
  1571. i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_SSL_CTX);
  1572. if (i > 0) {
  1573. return;
  1574. }
  1575. if (a->param) {
  1576. X509_VERIFY_PARAM_free(a->param);
  1577. }
  1578. /* Free internal session cache. However: the remove_cb() may reference the
  1579. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  1580. * sessions were flushed. As the ex_data handling routines might also touch
  1581. * the session cache, the most secure solution seems to be: empty (flush) the
  1582. * cache, then free ex_data, then finally free the cache. (See ticket
  1583. * [openssl.org #212].) */
  1584. if (a->sessions != NULL) {
  1585. SSL_CTX_flush_sessions(a, 0);
  1586. }
  1587. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, a, &a->ex_data);
  1588. if (a->sessions != NULL) {
  1589. lh_SSL_SESSION_free(a->sessions);
  1590. }
  1591. if (a->cert_store != NULL) {
  1592. X509_STORE_free(a->cert_store);
  1593. }
  1594. if (a->cipher_list != NULL) {
  1595. ssl_cipher_preference_list_free(a->cipher_list);
  1596. }
  1597. if (a->cipher_list_by_id != NULL) {
  1598. sk_SSL_CIPHER_free(a->cipher_list_by_id);
  1599. }
  1600. if (a->cipher_list_tls11 != NULL) {
  1601. ssl_cipher_preference_list_free(a->cipher_list_tls11);
  1602. }
  1603. if (a->cert != NULL) {
  1604. ssl_cert_free(a->cert);
  1605. }
  1606. if (a->client_CA != NULL) {
  1607. sk_X509_NAME_pop_free(a->client_CA, X509_NAME_free);
  1608. }
  1609. if (a->extra_certs != NULL) {
  1610. sk_X509_pop_free(a->extra_certs, X509_free);
  1611. }
  1612. if (a->srtp_profiles) {
  1613. sk_SRTP_PROTECTION_PROFILE_free(a->srtp_profiles);
  1614. }
  1615. if (a->psk_identity_hint) {
  1616. OPENSSL_free(a->psk_identity_hint);
  1617. }
  1618. if (a->tlsext_ecpointformatlist) {
  1619. OPENSSL_free(a->tlsext_ecpointformatlist);
  1620. }
  1621. if (a->tlsext_ellipticcurvelist) {
  1622. OPENSSL_free(a->tlsext_ellipticcurvelist);
  1623. }
  1624. if (a->alpn_client_proto_list != NULL) {
  1625. OPENSSL_free(a->alpn_client_proto_list);
  1626. }
  1627. if (a->tlsext_channel_id_private) {
  1628. EVP_PKEY_free(a->tlsext_channel_id_private);
  1629. }
  1630. if (a->keylog_bio) {
  1631. BIO_free(a->keylog_bio);
  1632. }
  1633. OPENSSL_free(a);
  1634. }
  1635. void SSL_CTX_set_default_passwd_cb(SSL_CTX *ctx, pem_password_cb *cb) {
  1636. ctx->default_passwd_callback = cb;
  1637. }
  1638. void SSL_CTX_set_default_passwd_cb_userdata(SSL_CTX *ctx, void *u) {
  1639. ctx->default_passwd_callback_userdata = u;
  1640. }
  1641. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  1642. int (*cb)(X509_STORE_CTX *, void *),
  1643. void *arg) {
  1644. ctx->app_verify_callback = cb;
  1645. ctx->app_verify_arg = arg;
  1646. }
  1647. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  1648. int (*cb)(int, X509_STORE_CTX *)) {
  1649. ctx->verify_mode = mode;
  1650. ctx->default_verify_callback = cb;
  1651. }
  1652. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  1653. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1654. }
  1655. void SSL_CTX_set_cert_cb(SSL_CTX *c, int (*cb)(SSL *ssl, void *arg),
  1656. void *arg) {
  1657. ssl_cert_set_cert_cb(c->cert, cb, arg);
  1658. }
  1659. void SSL_set_cert_cb(SSL *s, int (*cb)(SSL *ssl, void *arg), void *arg) {
  1660. ssl_cert_set_cert_cb(s->cert, cb, arg);
  1661. }
  1662. static int ssl_has_key(SSL *s, size_t idx) {
  1663. CERT_PKEY *cpk = &s->cert->pkeys[idx];
  1664. return cpk->x509 && cpk->privatekey;
  1665. }
  1666. void ssl_get_compatible_server_ciphers(SSL *s, uint32_t *out_mask_k,
  1667. uint32_t *out_mask_a) {
  1668. CERT *c = s->cert;
  1669. int rsa_enc, rsa_sign, dh_tmp;
  1670. uint32_t mask_k, mask_a;
  1671. int have_ecc_cert, ecdsa_ok;
  1672. X509 *x;
  1673. if (c == NULL) {
  1674. /* TODO(davidben): Is this codepath possible? */
  1675. *out_mask_k = 0;
  1676. *out_mask_a = 0;
  1677. return;
  1678. }
  1679. dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
  1680. rsa_enc = ssl_has_key(s, SSL_PKEY_RSA_ENC);
  1681. rsa_sign = ssl_has_key(s, SSL_PKEY_RSA_SIGN);
  1682. have_ecc_cert = ssl_has_key(s, SSL_PKEY_ECC);
  1683. mask_k = 0;
  1684. mask_a = 0;
  1685. if (rsa_enc) {
  1686. mask_k |= SSL_kRSA;
  1687. }
  1688. if (dh_tmp) {
  1689. mask_k |= SSL_kDHE;
  1690. }
  1691. if (rsa_enc || rsa_sign) {
  1692. mask_a |= SSL_aRSA;
  1693. }
  1694. /* An ECC certificate may be usable for ECDSA cipher suites depending on the
  1695. * key usage extension and on the client's curve preferences. */
  1696. if (have_ecc_cert) {
  1697. x = c->pkeys[SSL_PKEY_ECC].x509;
  1698. /* This call populates extension flags (ex_flags). */
  1699. X509_check_purpose(x, -1, 0);
  1700. ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
  1701. ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
  1702. : 1;
  1703. if (!tls1_check_ec_cert(s, x)) {
  1704. ecdsa_ok = 0;
  1705. }
  1706. if (ecdsa_ok) {
  1707. mask_a |= SSL_aECDSA;
  1708. }
  1709. }
  1710. /* If we are considering an ECC cipher suite that uses an ephemeral EC
  1711. * key, check it. */
  1712. if (tls1_check_ec_tmp_key(s)) {
  1713. mask_k |= SSL_kECDHE;
  1714. }
  1715. /* PSK requires a server callback. */
  1716. if (s->psk_server_callback != NULL) {
  1717. mask_k |= SSL_kPSK;
  1718. mask_a |= SSL_aPSK;
  1719. }
  1720. *out_mask_k = mask_k;
  1721. *out_mask_a = mask_a;
  1722. }
  1723. /* This handy macro borrowed from crypto/x509v3/v3_purp.c */
  1724. #define ku_reject(x, usage) \
  1725. (((x)->ex_flags & EXFLAG_KUSAGE) && !((x)->ex_kusage & (usage)))
  1726. int ssl_check_srvr_ecc_cert_and_alg(X509 *x, SSL *s) {
  1727. const SSL_CIPHER *cs = s->s3->tmp.new_cipher;
  1728. uint32_t alg_a = cs->algorithm_auth;
  1729. int signature_nid = 0, md_nid = 0, pk_nid = 0;
  1730. /* This call populates the ex_flags field correctly */
  1731. X509_check_purpose(x, -1, 0);
  1732. if (x->sig_alg && x->sig_alg->algorithm) {
  1733. signature_nid = OBJ_obj2nid(x->sig_alg->algorithm);
  1734. OBJ_find_sigid_algs(signature_nid, &md_nid, &pk_nid);
  1735. }
  1736. if (alg_a & SSL_aECDSA) {
  1737. /* key usage, if present, must allow signing */
  1738. if (ku_reject(x, X509v3_KU_DIGITAL_SIGNATURE)) {
  1739. OPENSSL_PUT_ERROR(SSL, ssl_check_srvr_ecc_cert_and_alg,
  1740. SSL_R_ECC_CERT_NOT_FOR_SIGNING);
  1741. return 0;
  1742. }
  1743. }
  1744. return 1; /* all checks are ok */
  1745. }
  1746. static int ssl_get_server_cert_index(const SSL *s) {
  1747. int idx;
  1748. idx = ssl_cipher_get_cert_index(s->s3->tmp.new_cipher);
  1749. if (idx == SSL_PKEY_RSA_ENC && !s->cert->pkeys[SSL_PKEY_RSA_ENC].x509) {
  1750. idx = SSL_PKEY_RSA_SIGN;
  1751. }
  1752. if (idx == -1) {
  1753. OPENSSL_PUT_ERROR(SSL, ssl_get_server_cert_index, ERR_R_INTERNAL_ERROR);
  1754. }
  1755. return idx;
  1756. }
  1757. CERT_PKEY *ssl_get_server_send_pkey(const SSL *s) {
  1758. int i = ssl_get_server_cert_index(s);
  1759. /* This may or may not be an error. */
  1760. if (i < 0) {
  1761. return NULL;
  1762. }
  1763. /* May be NULL. */
  1764. return &s->cert->pkeys[i];
  1765. }
  1766. EVP_PKEY *ssl_get_sign_pkey(SSL *s, const SSL_CIPHER *cipher) {
  1767. uint32_t alg_a = cipher->algorithm_auth;
  1768. CERT *c = s->cert;
  1769. int idx = -1;
  1770. if (alg_a & SSL_aRSA) {
  1771. if (c->pkeys[SSL_PKEY_RSA_SIGN].privatekey != NULL) {
  1772. idx = SSL_PKEY_RSA_SIGN;
  1773. } else if (c->pkeys[SSL_PKEY_RSA_ENC].privatekey != NULL) {
  1774. idx = SSL_PKEY_RSA_ENC;
  1775. }
  1776. } else if ((alg_a & SSL_aECDSA) &&
  1777. (c->pkeys[SSL_PKEY_ECC].privatekey != NULL)) {
  1778. idx = SSL_PKEY_ECC;
  1779. }
  1780. if (idx == -1) {
  1781. OPENSSL_PUT_ERROR(SSL, ssl_get_sign_pkey, ERR_R_INTERNAL_ERROR);
  1782. return NULL;
  1783. }
  1784. return c->pkeys[idx].privatekey;
  1785. }
  1786. void ssl_update_cache(SSL *s, int mode) {
  1787. /* Never cache sessions with empty session IDs. */
  1788. if (s->session->session_id_length == 0) {
  1789. return;
  1790. }
  1791. SSL_CTX *ctx = s->initial_ctx;
  1792. if ((ctx->session_cache_mode & mode) == mode && !s->hit &&
  1793. ((ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) ||
  1794. SSL_CTX_add_session(ctx, s->session)) &&
  1795. ctx->new_session_cb != NULL) {
  1796. /* Note: |new_session_cb| is called whether the internal session cache is
  1797. * used or not. */
  1798. if (!ctx->new_session_cb(s, SSL_SESSION_up_ref(s->session))) {
  1799. SSL_SESSION_free(s->session);
  1800. }
  1801. }
  1802. if (!(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR) &&
  1803. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_INTERNAL_STORE) &&
  1804. (ctx->session_cache_mode & mode) == mode) {
  1805. /* Automatically flush the internal session cache every 255 connections. */
  1806. int flush_cache = 0;
  1807. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  1808. ctx->handshakes_since_cache_flush++;
  1809. if (ctx->handshakes_since_cache_flush >= 255) {
  1810. flush_cache = 1;
  1811. ctx->handshakes_since_cache_flush = 0;
  1812. }
  1813. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  1814. if (flush_cache) {
  1815. SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
  1816. }
  1817. }
  1818. }
  1819. int SSL_get_error(const SSL *s, int ret_code) {
  1820. int reason;
  1821. uint32_t err;
  1822. BIO *bio;
  1823. if (ret_code > 0) {
  1824. return SSL_ERROR_NONE;
  1825. }
  1826. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  1827. * where we do encode the error */
  1828. err = ERR_peek_error();
  1829. if (err != 0) {
  1830. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  1831. return SSL_ERROR_SYSCALL;
  1832. }
  1833. return SSL_ERROR_SSL;
  1834. }
  1835. if (ret_code == 0) {
  1836. if ((s->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  1837. (s->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
  1838. /* The socket was cleanly shut down with a close_notify. */
  1839. return SSL_ERROR_ZERO_RETURN;
  1840. }
  1841. /* An EOF was observed which violates the protocol, and the underlying
  1842. * transport does not participate in the error queue. Bubble up to the
  1843. * caller. */
  1844. return SSL_ERROR_SYSCALL;
  1845. }
  1846. if (SSL_want_session(s)) {
  1847. return SSL_ERROR_PENDING_SESSION;
  1848. }
  1849. if (SSL_want_certificate(s)) {
  1850. return SSL_ERROR_PENDING_CERTIFICATE;
  1851. }
  1852. if (SSL_want_read(s)) {
  1853. bio = SSL_get_rbio(s);
  1854. if (BIO_should_read(bio)) {
  1855. return SSL_ERROR_WANT_READ;
  1856. }
  1857. if (BIO_should_write(bio)) {
  1858. /* This one doesn't make too much sense ... We never try to write to the
  1859. * rbio, and an application program where rbio and wbio are separate
  1860. * couldn't even know what it should wait for. However if we ever set
  1861. * s->rwstate incorrectly (so that we have SSL_want_read(s) instead of
  1862. * SSL_want_write(s)) and rbio and wbio *are* the same, this test works
  1863. * around that bug; so it might be safer to keep it. */
  1864. return SSL_ERROR_WANT_WRITE;
  1865. }
  1866. if (BIO_should_io_special(bio)) {
  1867. reason = BIO_get_retry_reason(bio);
  1868. if (reason == BIO_RR_CONNECT) {
  1869. return SSL_ERROR_WANT_CONNECT;
  1870. }
  1871. if (reason == BIO_RR_ACCEPT) {
  1872. return SSL_ERROR_WANT_ACCEPT;
  1873. }
  1874. return SSL_ERROR_SYSCALL; /* unknown */
  1875. }
  1876. }
  1877. if (SSL_want_write(s)) {
  1878. bio = SSL_get_wbio(s);
  1879. if (BIO_should_write(bio)) {
  1880. return SSL_ERROR_WANT_WRITE;
  1881. }
  1882. if (BIO_should_read(bio)) {
  1883. /* See above (SSL_want_read(s) with BIO_should_write(bio)) */
  1884. return SSL_ERROR_WANT_READ;
  1885. }
  1886. if (BIO_should_io_special(bio)) {
  1887. reason = BIO_get_retry_reason(bio);
  1888. if (reason == BIO_RR_CONNECT) {
  1889. return SSL_ERROR_WANT_CONNECT;
  1890. }
  1891. if (reason == BIO_RR_ACCEPT) {
  1892. return SSL_ERROR_WANT_ACCEPT;
  1893. }
  1894. return SSL_ERROR_SYSCALL;
  1895. }
  1896. }
  1897. if (SSL_want_x509_lookup(s)) {
  1898. return SSL_ERROR_WANT_X509_LOOKUP;
  1899. }
  1900. if (SSL_want_channel_id_lookup(s)) {
  1901. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  1902. }
  1903. return SSL_ERROR_SYSCALL;
  1904. }
  1905. int SSL_do_handshake(SSL *s) {
  1906. int ret = 1;
  1907. if (s->handshake_func == NULL) {
  1908. OPENSSL_PUT_ERROR(SSL, SSL_do_handshake, SSL_R_CONNECTION_TYPE_NOT_SET);
  1909. return -1;
  1910. }
  1911. s->method->ssl_renegotiate_check(s);
  1912. if (SSL_in_init(s)) {
  1913. ret = s->handshake_func(s);
  1914. }
  1915. return ret;
  1916. }
  1917. void SSL_set_accept_state(SSL *s) {
  1918. s->server = 1;
  1919. s->shutdown = 0;
  1920. s->state = SSL_ST_ACCEPT | SSL_ST_BEFORE;
  1921. s->handshake_func = s->method->ssl_accept;
  1922. /* clear the current cipher */
  1923. ssl_clear_cipher_ctx(s);
  1924. }
  1925. void SSL_set_connect_state(SSL *s) {
  1926. s->server = 0;
  1927. s->shutdown = 0;
  1928. s->state = SSL_ST_CONNECT | SSL_ST_BEFORE;
  1929. s->handshake_func = s->method->ssl_connect;
  1930. /* clear the current cipher */
  1931. ssl_clear_cipher_ctx(s);
  1932. }
  1933. int ssl_undefined_function(SSL *s) {
  1934. OPENSSL_PUT_ERROR(SSL, ssl_undefined_function,
  1935. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1936. return 0;
  1937. }
  1938. int ssl_undefined_void_function(void) {
  1939. OPENSSL_PUT_ERROR(SSL, ssl_undefined_void_function,
  1940. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1941. return 0;
  1942. }
  1943. int ssl_undefined_const_function(const SSL *s) {
  1944. OPENSSL_PUT_ERROR(SSL, ssl_undefined_const_function,
  1945. ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1946. return 0;
  1947. }
  1948. static const char *ssl_get_version(int version) {
  1949. switch (version) {
  1950. case TLS1_2_VERSION:
  1951. return "TLSv1.2";
  1952. case TLS1_1_VERSION:
  1953. return "TLSv1.1";
  1954. case TLS1_VERSION:
  1955. return "TLSv1";
  1956. case SSL3_VERSION:
  1957. return "SSLv3";
  1958. case DTLS1_VERSION:
  1959. return "DTLSv1";
  1960. case DTLS1_2_VERSION:
  1961. return "DTLSv1.2";
  1962. default:
  1963. return "unknown";
  1964. }
  1965. }
  1966. const char *SSL_get_version(const SSL *s) {
  1967. return ssl_get_version(s->version);
  1968. }
  1969. const char *SSL_SESSION_get_version(const SSL_SESSION *sess) {
  1970. return ssl_get_version(sess->ssl_version);
  1971. }
  1972. void ssl_clear_cipher_ctx(SSL *s) {
  1973. if (s->aead_read_ctx != NULL) {
  1974. EVP_AEAD_CTX_cleanup(&s->aead_read_ctx->ctx);
  1975. OPENSSL_free(s->aead_read_ctx);
  1976. s->aead_read_ctx = NULL;
  1977. }
  1978. if (s->aead_write_ctx != NULL) {
  1979. EVP_AEAD_CTX_cleanup(&s->aead_write_ctx->ctx);
  1980. OPENSSL_free(s->aead_write_ctx);
  1981. s->aead_write_ctx = NULL;
  1982. }
  1983. }
  1984. X509 *SSL_get_certificate(const SSL *s) {
  1985. if (s->cert != NULL) {
  1986. return s->cert->key->x509;
  1987. }
  1988. return NULL;
  1989. }
  1990. EVP_PKEY *SSL_get_privatekey(const SSL *s) {
  1991. if (s->cert != NULL) {
  1992. return s->cert->key->privatekey;
  1993. }
  1994. return NULL;
  1995. }
  1996. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  1997. if (ctx->cert != NULL) {
  1998. return ctx->cert->key->x509;
  1999. }
  2000. return NULL;
  2001. }
  2002. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  2003. if (ctx->cert != NULL) {
  2004. return ctx->cert->key->privatekey;
  2005. }
  2006. return NULL;
  2007. }
  2008. const SSL_CIPHER *SSL_get_current_cipher(const SSL *s) {
  2009. if (s->session != NULL && s->session->cipher != NULL) {
  2010. return s->session->cipher;
  2011. }
  2012. return NULL;
  2013. }
  2014. const void *SSL_get_current_compression(SSL *s) { return NULL; }
  2015. const void *SSL_get_current_expansion(SSL *s) { return NULL; }
  2016. int ssl_init_wbio_buffer(SSL *s, int push) {
  2017. BIO *bbio;
  2018. if (s->bbio == NULL) {
  2019. bbio = BIO_new(BIO_f_buffer());
  2020. if (bbio == NULL) {
  2021. return 0;
  2022. }
  2023. s->bbio = bbio;
  2024. } else {
  2025. bbio = s->bbio;
  2026. if (s->bbio == s->wbio) {
  2027. s->wbio = BIO_pop(s->wbio);
  2028. }
  2029. }
  2030. BIO_reset(bbio);
  2031. if (!BIO_set_read_buffer_size(bbio, 1)) {
  2032. OPENSSL_PUT_ERROR(SSL, ssl_init_wbio_buffer, ERR_R_BUF_LIB);
  2033. return 0;
  2034. }
  2035. if (push) {
  2036. if (s->wbio != bbio) {
  2037. s->wbio = BIO_push(bbio, s->wbio);
  2038. }
  2039. } else {
  2040. if (s->wbio == bbio) {
  2041. s->wbio = BIO_pop(bbio);
  2042. }
  2043. }
  2044. return 1;
  2045. }
  2046. void ssl_free_wbio_buffer(SSL *s) {
  2047. if (s->bbio == NULL) {
  2048. return;
  2049. }
  2050. if (s->bbio == s->wbio) {
  2051. /* remove buffering */
  2052. s->wbio = BIO_pop(s->wbio);
  2053. }
  2054. BIO_free(s->bbio);
  2055. s->bbio = NULL;
  2056. }
  2057. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  2058. ctx->quiet_shutdown = mode;
  2059. }
  2060. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  2061. return ctx->quiet_shutdown;
  2062. }
  2063. void SSL_set_quiet_shutdown(SSL *s, int mode) { s->quiet_shutdown = mode; }
  2064. int SSL_get_quiet_shutdown(const SSL *s) { return s->quiet_shutdown; }
  2065. void SSL_set_shutdown(SSL *s, int mode) { s->shutdown = mode; }
  2066. int SSL_get_shutdown(const SSL *s) { return s->shutdown; }
  2067. int SSL_version(const SSL *s) { return s->version; }
  2068. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  2069. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  2070. if (ssl->ctx == ctx) {
  2071. return ssl->ctx;
  2072. }
  2073. if (ctx == NULL) {
  2074. ctx = ssl->initial_ctx;
  2075. }
  2076. if (ssl->cert != NULL) {
  2077. ssl_cert_free(ssl->cert);
  2078. }
  2079. ssl->cert = ssl_cert_dup(ctx->cert);
  2080. CRYPTO_add(&ctx->references, 1, CRYPTO_LOCK_SSL_CTX);
  2081. if (ssl->ctx != NULL) {
  2082. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  2083. }
  2084. ssl->ctx = ctx;
  2085. ssl->sid_ctx_length = ctx->sid_ctx_length;
  2086. assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
  2087. memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
  2088. return ssl->ctx;
  2089. }
  2090. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  2091. return X509_STORE_set_default_paths(ctx->cert_store);
  2092. }
  2093. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *CAfile,
  2094. const char *CApath) {
  2095. return X509_STORE_load_locations(ctx->cert_store, CAfile, CApath);
  2096. }
  2097. void SSL_set_info_callback(SSL *ssl,
  2098. void (*cb)(const SSL *ssl, int type, int val)) {
  2099. ssl->info_callback = cb;
  2100. }
  2101. void (*SSL_get_info_callback(const SSL *ssl))(const SSL * /*ssl*/, int /*type*/,
  2102. int /*val*/) {
  2103. return ssl->info_callback;
  2104. }
  2105. int SSL_state(const SSL *ssl) { return ssl->state; }
  2106. void SSL_set_state(SSL *ssl, int state) { ssl->state = state; }
  2107. void SSL_set_verify_result(SSL *ssl, long arg) { ssl->verify_result = arg; }
  2108. long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
  2109. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  2110. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  2111. int index;
  2112. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  2113. new_func, dup_func, free_func)) {
  2114. return -1;
  2115. }
  2116. return index;
  2117. }
  2118. int SSL_set_ex_data(SSL *s, int idx, void *arg) {
  2119. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  2120. }
  2121. void *SSL_get_ex_data(const SSL *s, int idx) {
  2122. return CRYPTO_get_ex_data(&s->ex_data, idx);
  2123. }
  2124. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  2125. CRYPTO_EX_dup *dup_func,
  2126. CRYPTO_EX_free *free_func) {
  2127. int index;
  2128. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  2129. new_func, dup_func, free_func)) {
  2130. return -1;
  2131. }
  2132. return index;
  2133. }
  2134. int SSL_CTX_set_ex_data(SSL_CTX *s, int idx, void *arg) {
  2135. return CRYPTO_set_ex_data(&s->ex_data, idx, arg);
  2136. }
  2137. void *SSL_CTX_get_ex_data(const SSL_CTX *s, int idx) {
  2138. return CRYPTO_get_ex_data(&s->ex_data, idx);
  2139. }
  2140. int ssl_ok(SSL *s) { return 1; }
  2141. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  2142. return ctx->cert_store;
  2143. }
  2144. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  2145. if (ctx->cert_store != NULL) {
  2146. X509_STORE_free(ctx->cert_store);
  2147. }
  2148. ctx->cert_store = store;
  2149. }
  2150. int SSL_want(const SSL *s) { return s->rwstate; }
  2151. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  2152. RSA *(*cb)(SSL *ssl, int is_export,
  2153. int keylength)) {
  2154. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
  2155. }
  2156. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  2157. int keylength)) {
  2158. SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_RSA_CB, (void (*)(void))cb);
  2159. }
  2160. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  2161. DH *(*dh)(SSL *ssl, int is_export,
  2162. int keylength)) {
  2163. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  2164. }
  2165. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*dh)(SSL *ssl, int is_export,
  2166. int keylength)) {
  2167. SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_DH_CB, (void (*)(void))dh);
  2168. }
  2169. void SSL_CTX_set_tmp_ecdh_callback(SSL_CTX *ctx,
  2170. EC_KEY *(*callback)(SSL *ssl, int is_export,
  2171. int keylength)) {
  2172. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH_CB,
  2173. (void (*)(void))callback);
  2174. }
  2175. void SSL_set_tmp_ecdh_callback(SSL *ssl,
  2176. EC_KEY *(*callback)(SSL *ssl, int is_export,
  2177. int keylength)) {
  2178. SSL_callback_ctrl(ssl, SSL_CTRL_SET_TMP_ECDH_CB, (void (*)(void))callback);
  2179. }
  2180. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  2181. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  2182. OPENSSL_PUT_ERROR(SSL, SSL_CTX_use_psk_identity_hint,
  2183. SSL_R_DATA_LENGTH_TOO_LONG);
  2184. return 0;
  2185. }
  2186. if (ctx->psk_identity_hint != NULL) {
  2187. OPENSSL_free(ctx->psk_identity_hint);
  2188. }
  2189. if (identity_hint != NULL) {
  2190. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  2191. if (ctx->psk_identity_hint == NULL) {
  2192. return 0;
  2193. }
  2194. } else {
  2195. ctx->psk_identity_hint = NULL;
  2196. }
  2197. return 1;
  2198. }
  2199. int SSL_use_psk_identity_hint(SSL *s, const char *identity_hint) {
  2200. if (s == NULL) {
  2201. return 0;
  2202. }
  2203. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  2204. OPENSSL_PUT_ERROR(SSL, SSL_use_psk_identity_hint,
  2205. SSL_R_DATA_LENGTH_TOO_LONG);
  2206. return 0;
  2207. }
  2208. /* Clear currently configured hint, if any. */
  2209. if (s->psk_identity_hint != NULL) {
  2210. OPENSSL_free(s->psk_identity_hint);
  2211. s->psk_identity_hint = NULL;
  2212. }
  2213. if (identity_hint != NULL) {
  2214. s->psk_identity_hint = BUF_strdup(identity_hint);
  2215. if (s->psk_identity_hint == NULL) {
  2216. return 0;
  2217. }
  2218. }
  2219. return 1;
  2220. }
  2221. const char *SSL_get_psk_identity_hint(const SSL *s) {
  2222. if (s == NULL) {
  2223. return NULL;
  2224. }
  2225. return s->psk_identity_hint;
  2226. }
  2227. const char *SSL_get_psk_identity(const SSL *s) {
  2228. if (s == NULL || s->session == NULL) {
  2229. return NULL;
  2230. }
  2231. return s->session->psk_identity;
  2232. }
  2233. void SSL_set_psk_client_callback(
  2234. SSL *s, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
  2235. unsigned int max_identity_len, uint8_t *psk,
  2236. unsigned int max_psk_len)) {
  2237. s->psk_client_callback = cb;
  2238. }
  2239. void SSL_CTX_set_psk_client_callback(
  2240. SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *hint, char *identity,
  2241. unsigned int max_identity_len,
  2242. uint8_t *psk, unsigned int max_psk_len)) {
  2243. ctx->psk_client_callback = cb;
  2244. }
  2245. void SSL_set_psk_server_callback(
  2246. SSL *s, unsigned int (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  2247. unsigned int max_psk_len)) {
  2248. s->psk_server_callback = cb;
  2249. }
  2250. void SSL_CTX_set_psk_server_callback(
  2251. SSL_CTX *ctx, unsigned int (*cb)(SSL *ssl, const char *identity,
  2252. uint8_t *psk, unsigned int max_psk_len)) {
  2253. ctx->psk_server_callback = cb;
  2254. }
  2255. void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  2256. ctx->min_version = version;
  2257. }
  2258. void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  2259. ctx->max_version = version;
  2260. }
  2261. void SSL_set_min_version(SSL *ssl, uint16_t version) {
  2262. ssl->min_version = version;
  2263. }
  2264. void SSL_set_max_version(SSL *ssl, uint16_t version) {
  2265. ssl->max_version = version;
  2266. }
  2267. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  2268. void (*cb)(int write_p, int version,
  2269. int content_type, const void *buf,
  2270. size_t len, SSL *ssl, void *arg)) {
  2271. SSL_CTX_callback_ctrl(ctx, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  2272. }
  2273. void SSL_set_msg_callback(SSL *ssl,
  2274. void (*cb)(int write_p, int version, int content_type,
  2275. const void *buf, size_t len, SSL *ssl,
  2276. void *arg)) {
  2277. SSL_callback_ctrl(ssl, SSL_CTRL_SET_MSG_CALLBACK, (void (*)(void))cb);
  2278. }
  2279. void SSL_CTX_set_keylog_bio(SSL_CTX *ctx, BIO *keylog_bio) {
  2280. if (ctx->keylog_bio != NULL) {
  2281. BIO_free(ctx->keylog_bio);
  2282. }
  2283. ctx->keylog_bio = keylog_bio;
  2284. }
  2285. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  2286. static const char hextable[] = "0123456789abcdef";
  2287. uint8_t *out;
  2288. size_t i;
  2289. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  2290. return 0;
  2291. }
  2292. for (i = 0; i < in_len; i++) {
  2293. *(out++) = (uint8_t)hextable[in[i] >> 4];
  2294. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  2295. }
  2296. return 1;
  2297. }
  2298. int ssl_ctx_log_rsa_client_key_exchange(SSL_CTX *ctx,
  2299. const uint8_t *encrypted_premaster,
  2300. size_t encrypted_premaster_len,
  2301. const uint8_t *premaster,
  2302. size_t premaster_len) {
  2303. BIO *bio = ctx->keylog_bio;
  2304. CBB cbb;
  2305. uint8_t *out;
  2306. size_t out_len;
  2307. int ret;
  2308. if (bio == NULL) {
  2309. return 1;
  2310. }
  2311. if (encrypted_premaster_len < 8) {
  2312. OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_rsa_client_key_exchange,
  2313. ERR_R_INTERNAL_ERROR);
  2314. return 0;
  2315. }
  2316. if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1)) {
  2317. return 0;
  2318. }
  2319. if (!CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
  2320. /* Only the first 8 bytes of the encrypted premaster secret are
  2321. * logged. */
  2322. !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
  2323. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2324. !cbb_add_hex(&cbb, premaster, premaster_len) ||
  2325. !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
  2326. !CBB_finish(&cbb, &out, &out_len)) {
  2327. CBB_cleanup(&cbb);
  2328. return 0;
  2329. }
  2330. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  2331. ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
  2332. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  2333. OPENSSL_free(out);
  2334. return ret;
  2335. }
  2336. int ssl_ctx_log_master_secret(SSL_CTX *ctx, const uint8_t *client_random,
  2337. size_t client_random_len, const uint8_t *master,
  2338. size_t master_len) {
  2339. BIO *bio = ctx->keylog_bio;
  2340. CBB cbb;
  2341. uint8_t *out;
  2342. size_t out_len;
  2343. int ret;
  2344. if (bio == NULL) {
  2345. return 1;
  2346. }
  2347. if (client_random_len != 32) {
  2348. OPENSSL_PUT_ERROR(SSL, ssl_ctx_log_master_secret, ERR_R_INTERNAL_ERROR);
  2349. return 0;
  2350. }
  2351. if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1)) {
  2352. return 0;
  2353. }
  2354. if (!CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
  2355. !cbb_add_hex(&cbb, client_random, 32) ||
  2356. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  2357. !cbb_add_hex(&cbb, master, master_len) ||
  2358. !CBB_add_bytes(&cbb, (const uint8_t *)"\n", 1) ||
  2359. !CBB_finish(&cbb, &out, &out_len)) {
  2360. CBB_cleanup(&cbb);
  2361. return 0;
  2362. }
  2363. CRYPTO_w_lock(CRYPTO_LOCK_SSL_CTX);
  2364. ret = BIO_write(bio, out, out_len) >= 0 && BIO_flush(bio);
  2365. CRYPTO_w_unlock(CRYPTO_LOCK_SSL_CTX);
  2366. OPENSSL_free(out);
  2367. return ret;
  2368. }
  2369. int SSL_in_false_start(const SSL *s) {
  2370. return s->s3->tmp.in_false_start;
  2371. }
  2372. int SSL_cutthrough_complete(const SSL *s) {
  2373. return SSL_in_false_start(s);
  2374. }
  2375. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  2376. size_t *ssl_session_size) {
  2377. *ssl_size = sizeof(SSL);
  2378. *ssl_ctx_size = sizeof(SSL_CTX);
  2379. *ssl_session_size = sizeof(SSL_SESSION);
  2380. }
  2381. int ssl3_can_false_start(const SSL *s) {
  2382. const SSL_CIPHER *const cipher = SSL_get_current_cipher(s);
  2383. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  2384. return !SSL_IS_DTLS(s) &&
  2385. SSL_version(s) >= TLS1_2_VERSION &&
  2386. (s->s3->alpn_selected || s->s3->next_proto_neg_seen) &&
  2387. cipher != NULL &&
  2388. cipher->algorithm_mkey == SSL_kECDHE &&
  2389. (cipher->algorithm_enc == SSL_AES128GCM ||
  2390. cipher->algorithm_enc == SSL_AES256GCM ||
  2391. cipher->algorithm_enc == SSL_CHACHA20POLY1305);
  2392. }
  2393. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
  2394. switch (version) {
  2395. case SSL3_VERSION:
  2396. return &SSLv3_enc_data;
  2397. case TLS1_VERSION:
  2398. return &TLSv1_enc_data;
  2399. case DTLS1_VERSION:
  2400. case TLS1_1_VERSION:
  2401. return &TLSv1_1_enc_data;
  2402. case DTLS1_2_VERSION:
  2403. case TLS1_2_VERSION:
  2404. return &TLSv1_2_enc_data;
  2405. default:
  2406. return NULL;
  2407. }
  2408. }
  2409. uint16_t ssl3_get_max_server_version(const SSL *s) {
  2410. uint16_t max_version;
  2411. if (SSL_IS_DTLS(s)) {
  2412. max_version = (s->max_version != 0) ? s->max_version : DTLS1_2_VERSION;
  2413. if (!(s->options & SSL_OP_NO_DTLSv1_2) && DTLS1_2_VERSION >= max_version) {
  2414. return DTLS1_2_VERSION;
  2415. }
  2416. if (!(s->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
  2417. return DTLS1_VERSION;
  2418. }
  2419. return 0;
  2420. }
  2421. max_version = (s->max_version != 0) ? s->max_version : TLS1_2_VERSION;
  2422. if (!(s->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
  2423. return TLS1_2_VERSION;
  2424. }
  2425. if (!(s->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
  2426. return TLS1_1_VERSION;
  2427. }
  2428. if (!(s->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
  2429. return TLS1_VERSION;
  2430. }
  2431. if (!(s->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
  2432. return SSL3_VERSION;
  2433. }
  2434. return 0;
  2435. }
  2436. uint16_t ssl3_get_mutual_version(SSL *s, uint16_t client_version) {
  2437. uint16_t version = 0;
  2438. if (SSL_IS_DTLS(s)) {
  2439. /* Clamp client_version to max_version. */
  2440. if (s->max_version != 0 && client_version < s->max_version) {
  2441. client_version = s->max_version;
  2442. }
  2443. if (client_version <= DTLS1_2_VERSION && !(s->options & SSL_OP_NO_DTLSv1_2)) {
  2444. version = DTLS1_2_VERSION;
  2445. } else if (client_version <= DTLS1_VERSION &&
  2446. !(s->options & SSL_OP_NO_DTLSv1)) {
  2447. version = DTLS1_VERSION;
  2448. }
  2449. /* Check against min_version. */
  2450. if (version != 0 && s->min_version != 0 && version > s->min_version) {
  2451. return 0;
  2452. }
  2453. return version;
  2454. } else {
  2455. /* Clamp client_version to max_version. */
  2456. if (s->max_version != 0 && client_version > s->max_version) {
  2457. client_version = s->max_version;
  2458. }
  2459. if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) {
  2460. version = TLS1_2_VERSION;
  2461. } else if (client_version >= TLS1_1_VERSION &&
  2462. !(s->options & SSL_OP_NO_TLSv1_1)) {
  2463. version = TLS1_1_VERSION;
  2464. } else if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) {
  2465. version = TLS1_VERSION;
  2466. } else if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) {
  2467. version = SSL3_VERSION;
  2468. }
  2469. /* Check against min_version. */
  2470. if (version != 0 && s->min_version != 0 && version < s->min_version) {
  2471. return 0;
  2472. }
  2473. return version;
  2474. }
  2475. }
  2476. uint16_t ssl3_get_max_client_version(SSL *s) {
  2477. unsigned long options = s->options;
  2478. uint16_t version = 0;
  2479. /* OpenSSL's API for controlling versions entails blacklisting individual
  2480. * protocols. This has two problems. First, on the client, the protocol can
  2481. * only express a contiguous range of versions. Second, a library consumer
  2482. * trying to set a maximum version cannot disable protocol versions that get
  2483. * added in a future version of the library.
  2484. *
  2485. * To account for both of these, OpenSSL interprets the client-side bitmask
  2486. * as a min/max range by picking the lowest contiguous non-empty range of
  2487. * enabled protocols. Note that this means it is impossible to set a maximum
  2488. * version of TLS 1.2 in a future-proof way.
  2489. *
  2490. * By this scheme, the maximum version is the lowest version V such that V is
  2491. * enabled and V+1 is disabled or unimplemented. */
  2492. if (SSL_IS_DTLS(s)) {
  2493. if (!(options & SSL_OP_NO_DTLSv1_2)) {
  2494. version = DTLS1_2_VERSION;
  2495. }
  2496. if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
  2497. version = DTLS1_VERSION;
  2498. }
  2499. if (s->max_version != 0 && version < s->max_version) {
  2500. version = s->max_version;
  2501. }
  2502. } else {
  2503. if (!(options & SSL_OP_NO_TLSv1_2)) {
  2504. version = TLS1_2_VERSION;
  2505. }
  2506. if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
  2507. version = TLS1_1_VERSION;
  2508. }
  2509. if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
  2510. version = TLS1_VERSION;
  2511. }
  2512. if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
  2513. version = SSL3_VERSION;
  2514. }
  2515. if (s->max_version != 0 && version > s->max_version) {
  2516. version = s->max_version;
  2517. }
  2518. }
  2519. return version;
  2520. }
  2521. int ssl3_is_version_enabled(SSL *s, uint16_t version) {
  2522. if (SSL_IS_DTLS(s)) {
  2523. if (s->max_version != 0 && version < s->max_version) {
  2524. return 0;
  2525. }
  2526. if (s->min_version != 0 && version > s->min_version) {
  2527. return 0;
  2528. }
  2529. switch (version) {
  2530. case DTLS1_VERSION:
  2531. return !(s->options & SSL_OP_NO_DTLSv1);
  2532. case DTLS1_2_VERSION:
  2533. return !(s->options & SSL_OP_NO_DTLSv1_2);
  2534. default:
  2535. return 0;
  2536. }
  2537. } else {
  2538. if (s->max_version != 0 && version > s->max_version) {
  2539. return 0;
  2540. }
  2541. if (s->min_version != 0 && version < s->min_version) {
  2542. return 0;
  2543. }
  2544. switch (version) {
  2545. case SSL3_VERSION:
  2546. return !(s->options & SSL_OP_NO_SSLv3);
  2547. case TLS1_VERSION:
  2548. return !(s->options & SSL_OP_NO_TLSv1);
  2549. case TLS1_1_VERSION:
  2550. return !(s->options & SSL_OP_NO_TLSv1_1);
  2551. case TLS1_2_VERSION:
  2552. return !(s->options & SSL_OP_NO_TLSv1_2);
  2553. default:
  2554. return 0;
  2555. }
  2556. }
  2557. }
  2558. uint16_t ssl3_version_from_wire(SSL *s, uint16_t wire_version) {
  2559. if (!SSL_IS_DTLS(s)) {
  2560. return wire_version;
  2561. }
  2562. uint16_t tls_version = ~wire_version;
  2563. uint16_t version = tls_version + 0x0201;
  2564. /* If either component overflowed, clamp it so comparisons still work. */
  2565. if ((version >> 8) < (tls_version >> 8)) {
  2566. version = 0xff00 | (version & 0xff);
  2567. }
  2568. if ((version & 0xff) < (tls_version & 0xff)) {
  2569. version = (version & 0xff00) | 0xff;
  2570. }
  2571. /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
  2572. if (version == TLS1_VERSION) {
  2573. version = TLS1_1_VERSION;
  2574. }
  2575. return version;
  2576. }
  2577. int SSL_cache_hit(SSL *s) { return s->hit; }
  2578. int SSL_is_server(SSL *s) { return s->server; }
  2579. void SSL_CTX_set_dos_protection_cb(
  2580. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2581. ctx->dos_protection_cb = cb;
  2582. }
  2583. void SSL_enable_fastradio_padding(SSL *s, char on_off) {
  2584. s->fastradio_padding = on_off;
  2585. }
  2586. void SSL_set_reject_peer_renegotiations(SSL *s, int reject) {
  2587. s->reject_peer_renegotiations = !!reject;
  2588. }
  2589. const SSL_CIPHER *SSL_get_cipher_by_value(uint16_t value) {
  2590. return ssl3_get_cipher_by_value(value);
  2591. }
  2592. int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
  2593. const RC4_KEY **write_key) {
  2594. if (ssl->aead_read_ctx == NULL || ssl->aead_write_ctx == NULL) {
  2595. return 0;
  2596. }
  2597. return EVP_AEAD_CTX_get_rc4_state(&ssl->aead_read_ctx->ctx, read_key) &&
  2598. EVP_AEAD_CTX_get_rc4_state(&ssl->aead_write_ctx->ctx, write_key);
  2599. }