ssl_lib.c 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  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 <openssl/ssl.h>
  141. #include <assert.h>
  142. #include <stdio.h>
  143. #include <string.h>
  144. #include <openssl/bytestring.h>
  145. #include <openssl/crypto.h>
  146. #include <openssl/dh.h>
  147. #include <openssl/err.h>
  148. #include <openssl/lhash.h>
  149. #include <openssl/mem.h>
  150. #include <openssl/obj.h>
  151. #include <openssl/rand.h>
  152. #include <openssl/x509v3.h>
  153. #include "internal.h"
  154. #include "../crypto/internal.h"
  155. /* |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
  156. * to avoid downstream churn. */
  157. OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
  158. /* Some error codes are special. Ensure the make_errors.go script never
  159. * regresses this. */
  160. OPENSSL_COMPILE_ASSERT(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  161. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  162. ssl_alert_reason_code_mismatch);
  163. /* kMaxHandshakeSize is the maximum size, in bytes, of a handshake message. */
  164. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  165. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
  166. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  167. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
  168. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  169. int SSL_library_init(void) {
  170. CRYPTO_library_init();
  171. return 1;
  172. }
  173. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  174. const uint8_t *session_id = sess->session_id;
  175. uint8_t tmp_storage[sizeof(uint32_t)];
  176. if (sess->session_id_length < sizeof(tmp_storage)) {
  177. memset(tmp_storage, 0, sizeof(tmp_storage));
  178. memcpy(tmp_storage, sess->session_id, sess->session_id_length);
  179. session_id = tmp_storage;
  180. }
  181. uint32_t hash =
  182. ((uint32_t)session_id[0]) |
  183. ((uint32_t)session_id[1] << 8) |
  184. ((uint32_t)session_id[2] << 16) |
  185. ((uint32_t)session_id[3] << 24);
  186. return hash;
  187. }
  188. /* NB: If this function (or indeed the hash function which uses a sort of
  189. * coarser function than this one) is changed, ensure
  190. * SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  191. * able to construct an SSL_SESSION that will collide with any existing session
  192. * with a matching session ID. */
  193. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  194. if (a->ssl_version != b->ssl_version) {
  195. return 1;
  196. }
  197. if (a->session_id_length != b->session_id_length) {
  198. return 1;
  199. }
  200. return memcmp(a->session_id, b->session_id, a->session_id_length);
  201. }
  202. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  203. SSL_CTX *ret = NULL;
  204. if (method == NULL) {
  205. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  206. return NULL;
  207. }
  208. if (SSL_get_ex_data_X509_STORE_CTX_idx() < 0) {
  209. OPENSSL_PUT_ERROR(SSL, SSL_R_X509_VERIFICATION_SETUP_PROBLEMS);
  210. goto err;
  211. }
  212. ret = OPENSSL_malloc(sizeof(SSL_CTX));
  213. if (ret == NULL) {
  214. goto err;
  215. }
  216. memset(ret, 0, sizeof(SSL_CTX));
  217. ret->method = method->method;
  218. CRYPTO_MUTEX_init(&ret->lock);
  219. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  220. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  221. /* We take the system default */
  222. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  223. ret->references = 1;
  224. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  225. ret->verify_mode = SSL_VERIFY_NONE;
  226. ret->cert = ssl_cert_new();
  227. if (ret->cert == NULL) {
  228. goto err;
  229. }
  230. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  231. if (ret->sessions == NULL) {
  232. goto err;
  233. }
  234. ret->cert_store = X509_STORE_new();
  235. if (ret->cert_store == NULL) {
  236. goto err;
  237. }
  238. ssl_create_cipher_list(ret->method, &ret->cipher_list,
  239. &ret->cipher_list_by_id, SSL_DEFAULT_CIPHER_LIST);
  240. if (ret->cipher_list == NULL ||
  241. sk_SSL_CIPHER_num(ret->cipher_list->ciphers) <= 0) {
  242. OPENSSL_PUT_ERROR(SSL, SSL_R_LIBRARY_HAS_NO_CIPHERS);
  243. goto err2;
  244. }
  245. ret->param = X509_VERIFY_PARAM_new();
  246. if (!ret->param) {
  247. goto err;
  248. }
  249. ret->client_CA = sk_X509_NAME_new_null();
  250. if (ret->client_CA == NULL) {
  251. goto err;
  252. }
  253. CRYPTO_new_ex_data(&ret->ex_data);
  254. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  255. /* Setup RFC4507 ticket keys */
  256. if (!RAND_bytes(ret->tlsext_tick_key_name, 16) ||
  257. !RAND_bytes(ret->tlsext_tick_hmac_key, 16) ||
  258. !RAND_bytes(ret->tlsext_tick_aes_key, 16)) {
  259. ret->options |= SSL_OP_NO_TICKET;
  260. }
  261. /* Lock the SSL_CTX to the specified version, for compatibility with legacy
  262. * uses of SSL_METHOD. */
  263. if (method->version != 0) {
  264. SSL_CTX_set_max_version(ret, method->version);
  265. SSL_CTX_set_min_version(ret, method->version);
  266. }
  267. return ret;
  268. err:
  269. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  270. err2:
  271. SSL_CTX_free(ret);
  272. return NULL;
  273. }
  274. void SSL_CTX_free(SSL_CTX *ctx) {
  275. if (ctx == NULL ||
  276. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  277. return;
  278. }
  279. X509_VERIFY_PARAM_free(ctx->param);
  280. /* Free internal session cache. However: the remove_cb() may reference the
  281. * ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  282. * sessions were flushed. As the ex_data handling routines might also touch
  283. * the session cache, the most secure solution seems to be: empty (flush) the
  284. * cache, then free ex_data, then finally free the cache. (See ticket
  285. * [openssl.org #212].) */
  286. SSL_CTX_flush_sessions(ctx, 0);
  287. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  288. CRYPTO_MUTEX_cleanup(&ctx->lock);
  289. lh_SSL_SESSION_free(ctx->sessions);
  290. X509_STORE_free(ctx->cert_store);
  291. ssl_cipher_preference_list_free(ctx->cipher_list);
  292. sk_SSL_CIPHER_free(ctx->cipher_list_by_id);
  293. ssl_cipher_preference_list_free(ctx->cipher_list_tls10);
  294. ssl_cipher_preference_list_free(ctx->cipher_list_tls11);
  295. ssl_cert_free(ctx->cert);
  296. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
  297. SSL_CUSTOM_EXTENSION_free);
  298. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
  299. SSL_CUSTOM_EXTENSION_free);
  300. sk_X509_NAME_pop_free(ctx->client_CA, X509_NAME_free);
  301. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  302. OPENSSL_free(ctx->psk_identity_hint);
  303. OPENSSL_free(ctx->tlsext_ellipticcurvelist);
  304. OPENSSL_free(ctx->alpn_client_proto_list);
  305. OPENSSL_free(ctx->ocsp_response);
  306. OPENSSL_free(ctx->signed_cert_timestamp_list);
  307. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  308. OPENSSL_free(ctx);
  309. }
  310. SSL *SSL_new(SSL_CTX *ctx) {
  311. if (ctx == NULL) {
  312. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  313. return NULL;
  314. }
  315. if (ctx->method == NULL) {
  316. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  317. return NULL;
  318. }
  319. SSL *ssl = OPENSSL_malloc(sizeof(SSL));
  320. if (ssl == NULL) {
  321. goto err;
  322. }
  323. memset(ssl, 0, sizeof(SSL));
  324. ssl->min_version = ctx->min_version;
  325. ssl->max_version = ctx->max_version;
  326. ssl->options = ctx->options;
  327. ssl->mode = ctx->mode;
  328. ssl->max_cert_list = ctx->max_cert_list;
  329. ssl->cert = ssl_cert_dup(ctx->cert);
  330. if (ssl->cert == NULL) {
  331. goto err;
  332. }
  333. ssl->msg_callback = ctx->msg_callback;
  334. ssl->msg_callback_arg = ctx->msg_callback_arg;
  335. ssl->verify_mode = ctx->verify_mode;
  336. ssl->sid_ctx_length = ctx->sid_ctx_length;
  337. assert(ssl->sid_ctx_length <= sizeof ssl->sid_ctx);
  338. memcpy(&ssl->sid_ctx, &ctx->sid_ctx, sizeof(ssl->sid_ctx));
  339. ssl->verify_callback = ctx->default_verify_callback;
  340. ssl->param = X509_VERIFY_PARAM_new();
  341. if (!ssl->param) {
  342. goto err;
  343. }
  344. X509_VERIFY_PARAM_inherit(ssl->param, ctx->param);
  345. ssl->quiet_shutdown = ctx->quiet_shutdown;
  346. ssl->max_send_fragment = ctx->max_send_fragment;
  347. CRYPTO_refcount_inc(&ctx->references);
  348. ssl->ctx = ctx;
  349. CRYPTO_refcount_inc(&ctx->references);
  350. ssl->initial_ctx = ctx;
  351. if (ctx->tlsext_ellipticcurvelist) {
  352. ssl->tlsext_ellipticcurvelist =
  353. BUF_memdup(ctx->tlsext_ellipticcurvelist,
  354. ctx->tlsext_ellipticcurvelist_length * 2);
  355. if (!ssl->tlsext_ellipticcurvelist) {
  356. goto err;
  357. }
  358. ssl->tlsext_ellipticcurvelist_length = ctx->tlsext_ellipticcurvelist_length;
  359. }
  360. if (ssl->ctx->alpn_client_proto_list) {
  361. ssl->alpn_client_proto_list = BUF_memdup(
  362. ssl->ctx->alpn_client_proto_list, ssl->ctx->alpn_client_proto_list_len);
  363. if (ssl->alpn_client_proto_list == NULL) {
  364. goto err;
  365. }
  366. ssl->alpn_client_proto_list_len = ssl->ctx->alpn_client_proto_list_len;
  367. }
  368. ssl->verify_result = X509_V_OK;
  369. ssl->method = ctx->method;
  370. if (!ssl->method->ssl_new(ssl)) {
  371. goto err;
  372. }
  373. ssl->rwstate = SSL_NOTHING;
  374. CRYPTO_new_ex_data(&ssl->ex_data);
  375. ssl->psk_identity_hint = NULL;
  376. if (ctx->psk_identity_hint) {
  377. ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  378. if (ssl->psk_identity_hint == NULL) {
  379. goto err;
  380. }
  381. }
  382. ssl->psk_client_callback = ctx->psk_client_callback;
  383. ssl->psk_server_callback = ctx->psk_server_callback;
  384. ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  385. if (ctx->tlsext_channel_id_private) {
  386. ssl->tlsext_channel_id_private =
  387. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  388. }
  389. ssl->signed_cert_timestamps_enabled =
  390. ssl->ctx->signed_cert_timestamps_enabled;
  391. ssl->ocsp_stapling_enabled = ssl->ctx->ocsp_stapling_enabled;
  392. return ssl;
  393. err:
  394. SSL_free(ssl);
  395. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  396. return NULL;
  397. }
  398. void SSL_free(SSL *ssl) {
  399. if (ssl == NULL) {
  400. return;
  401. }
  402. X509_VERIFY_PARAM_free(ssl->param);
  403. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  404. if (ssl->bbio != NULL) {
  405. /* If the buffering BIO is in place, pop it off */
  406. if (ssl->bbio == ssl->wbio) {
  407. ssl->wbio = BIO_pop(ssl->wbio);
  408. }
  409. BIO_free(ssl->bbio);
  410. ssl->bbio = NULL;
  411. }
  412. int free_wbio = ssl->wbio != ssl->rbio;
  413. BIO_free_all(ssl->rbio);
  414. if (free_wbio) {
  415. BIO_free_all(ssl->wbio);
  416. }
  417. BUF_MEM_free(ssl->init_buf);
  418. /* add extra stuff */
  419. ssl_cipher_preference_list_free(ssl->cipher_list);
  420. sk_SSL_CIPHER_free(ssl->cipher_list_by_id);
  421. ssl_clear_bad_session(ssl);
  422. SSL_SESSION_free(ssl->session);
  423. ssl_cert_free(ssl->cert);
  424. OPENSSL_free(ssl->tlsext_hostname);
  425. SSL_CTX_free(ssl->initial_ctx);
  426. OPENSSL_free(ssl->tlsext_ellipticcurvelist);
  427. OPENSSL_free(ssl->alpn_client_proto_list);
  428. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  429. OPENSSL_free(ssl->psk_identity_hint);
  430. sk_X509_NAME_pop_free(ssl->client_CA, X509_NAME_free);
  431. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  432. if (ssl->method != NULL) {
  433. ssl->method->ssl_free(ssl);
  434. }
  435. SSL_CTX_free(ssl->ctx);
  436. OPENSSL_free(ssl);
  437. }
  438. void SSL_set_connect_state(SSL *ssl) {
  439. ssl->server = 0;
  440. ssl->shutdown = 0;
  441. ssl->state = SSL_ST_CONNECT;
  442. ssl->handshake_func = ssl->method->ssl_connect;
  443. }
  444. void SSL_set_accept_state(SSL *ssl) {
  445. ssl->server = 1;
  446. ssl->shutdown = 0;
  447. ssl->state = SSL_ST_ACCEPT;
  448. ssl->handshake_func = ssl->method->ssl_accept;
  449. }
  450. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  451. /* If the output buffering BIO is still in place, remove it. */
  452. if (ssl->bbio != NULL) {
  453. if (ssl->wbio == ssl->bbio) {
  454. ssl->wbio = ssl->wbio->next_bio;
  455. ssl->bbio->next_bio = NULL;
  456. }
  457. }
  458. if (ssl->rbio != rbio) {
  459. BIO_free_all(ssl->rbio);
  460. }
  461. if (ssl->wbio != wbio && ssl->rbio != ssl->wbio) {
  462. BIO_free_all(ssl->wbio);
  463. }
  464. ssl->rbio = rbio;
  465. ssl->wbio = wbio;
  466. }
  467. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  468. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
  469. int SSL_do_handshake(SSL *ssl) {
  470. if (ssl->handshake_func == NULL) {
  471. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  472. return -1;
  473. }
  474. if (!SSL_in_init(ssl)) {
  475. return 1;
  476. }
  477. return ssl->handshake_func(ssl);
  478. }
  479. int SSL_connect(SSL *ssl) {
  480. if (ssl->handshake_func == 0) {
  481. /* Not properly initialized yet */
  482. SSL_set_connect_state(ssl);
  483. }
  484. if (ssl->handshake_func != ssl->method->ssl_connect) {
  485. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  486. return -1;
  487. }
  488. return ssl->handshake_func(ssl);
  489. }
  490. int SSL_accept(SSL *ssl) {
  491. if (ssl->handshake_func == 0) {
  492. /* Not properly initialized yet */
  493. SSL_set_accept_state(ssl);
  494. }
  495. if (ssl->handshake_func != ssl->method->ssl_accept) {
  496. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  497. return -1;
  498. }
  499. return ssl->handshake_func(ssl);
  500. }
  501. int SSL_read(SSL *ssl, void *buf, int num) {
  502. if (ssl->handshake_func == 0) {
  503. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  504. return -1;
  505. }
  506. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
  507. ssl->rwstate = SSL_NOTHING;
  508. return 0;
  509. }
  510. ERR_clear_system_error();
  511. return ssl->method->ssl_read_app_data(ssl, buf, num, 0);
  512. }
  513. int SSL_peek(SSL *ssl, void *buf, int num) {
  514. if (ssl->handshake_func == 0) {
  515. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  516. return -1;
  517. }
  518. if (ssl->shutdown & SSL_RECEIVED_SHUTDOWN) {
  519. return 0;
  520. }
  521. ERR_clear_system_error();
  522. return ssl->method->ssl_read_app_data(ssl, buf, num, 1);
  523. }
  524. int SSL_write(SSL *ssl, const void *buf, int num) {
  525. if (ssl->handshake_func == 0) {
  526. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  527. return -1;
  528. }
  529. if (ssl->shutdown & SSL_SENT_SHUTDOWN) {
  530. ssl->rwstate = SSL_NOTHING;
  531. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  532. return -1;
  533. }
  534. ERR_clear_system_error();
  535. return ssl->method->ssl_write_app_data(ssl, buf, num);
  536. }
  537. int SSL_shutdown(SSL *ssl) {
  538. /* Note that this function behaves differently from what one might expect.
  539. * Return values are 0 for no success (yet), 1 for success; but calling it
  540. * once is usually not enough, even if blocking I/O is used (see
  541. * ssl3_shutdown). */
  542. if (ssl->handshake_func == 0) {
  543. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  544. return -1;
  545. }
  546. if (SSL_in_init(ssl)) {
  547. return 1;
  548. }
  549. /* Do nothing if configured not to send a close_notify. */
  550. if (ssl->quiet_shutdown) {
  551. ssl->shutdown = SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN;
  552. return 1;
  553. }
  554. if (!(ssl->shutdown & SSL_SENT_SHUTDOWN)) {
  555. ssl->shutdown |= SSL_SENT_SHUTDOWN;
  556. ssl3_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY);
  557. /* our shutdown alert has been sent now, and if it still needs to be
  558. * written, ssl->s3->alert_dispatch will be true */
  559. if (ssl->s3->alert_dispatch) {
  560. return -1; /* return WANT_WRITE */
  561. }
  562. } else if (ssl->s3->alert_dispatch) {
  563. /* resend it if not sent */
  564. int ret = ssl->method->ssl_dispatch_alert(ssl);
  565. if (ret == -1) {
  566. /* we only get to return -1 here the 2nd/Nth invocation, we must have
  567. * already signalled return 0 upon a previous invoation, return
  568. * WANT_WRITE */
  569. return ret;
  570. }
  571. } else if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
  572. /* If we are waiting for a close from our peer, we are closed */
  573. ssl->method->ssl_read_close_notify(ssl);
  574. if (!(ssl->shutdown & SSL_RECEIVED_SHUTDOWN)) {
  575. return -1; /* return WANT_READ */
  576. }
  577. }
  578. if (ssl->shutdown == (SSL_SENT_SHUTDOWN | SSL_RECEIVED_SHUTDOWN) &&
  579. !ssl->s3->alert_dispatch) {
  580. return 1;
  581. } else {
  582. return 0;
  583. }
  584. }
  585. int SSL_get_error(const SSL *ssl, int ret_code) {
  586. int reason;
  587. uint32_t err;
  588. BIO *bio;
  589. if (ret_code > 0) {
  590. return SSL_ERROR_NONE;
  591. }
  592. /* Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  593. * where we do encode the error */
  594. err = ERR_peek_error();
  595. if (err != 0) {
  596. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  597. return SSL_ERROR_SYSCALL;
  598. }
  599. return SSL_ERROR_SSL;
  600. }
  601. if (ret_code == 0) {
  602. if ((ssl->shutdown & SSL_RECEIVED_SHUTDOWN) &&
  603. (ssl->s3->warn_alert == SSL_AD_CLOSE_NOTIFY)) {
  604. /* The socket was cleanly shut down with a close_notify. */
  605. return SSL_ERROR_ZERO_RETURN;
  606. }
  607. /* An EOF was observed which violates the protocol, and the underlying
  608. * transport does not participate in the error queue. Bubble up to the
  609. * caller. */
  610. return SSL_ERROR_SYSCALL;
  611. }
  612. if (SSL_want_session(ssl)) {
  613. return SSL_ERROR_PENDING_SESSION;
  614. }
  615. if (SSL_want_certificate(ssl)) {
  616. return SSL_ERROR_PENDING_CERTIFICATE;
  617. }
  618. if (SSL_want_read(ssl)) {
  619. bio = SSL_get_rbio(ssl);
  620. if (BIO_should_read(bio)) {
  621. return SSL_ERROR_WANT_READ;
  622. }
  623. if (BIO_should_write(bio)) {
  624. /* This one doesn't make too much sense ... We never try to write to the
  625. * rbio, and an application program where rbio and wbio are separate
  626. * couldn't even know what it should wait for. However if we ever set
  627. * ssl->rwstate incorrectly (so that we have SSL_want_read(ssl) instead of
  628. * SSL_want_write(ssl)) and rbio and wbio *are* the same, this test works
  629. * around that bug; so it might be safer to keep it. */
  630. return SSL_ERROR_WANT_WRITE;
  631. }
  632. if (BIO_should_io_special(bio)) {
  633. reason = BIO_get_retry_reason(bio);
  634. if (reason == BIO_RR_CONNECT) {
  635. return SSL_ERROR_WANT_CONNECT;
  636. }
  637. if (reason == BIO_RR_ACCEPT) {
  638. return SSL_ERROR_WANT_ACCEPT;
  639. }
  640. return SSL_ERROR_SYSCALL; /* unknown */
  641. }
  642. }
  643. if (SSL_want_write(ssl)) {
  644. bio = SSL_get_wbio(ssl);
  645. if (BIO_should_write(bio)) {
  646. return SSL_ERROR_WANT_WRITE;
  647. }
  648. if (BIO_should_read(bio)) {
  649. /* See above (SSL_want_read(ssl) with BIO_should_write(bio)) */
  650. return SSL_ERROR_WANT_READ;
  651. }
  652. if (BIO_should_io_special(bio)) {
  653. reason = BIO_get_retry_reason(bio);
  654. if (reason == BIO_RR_CONNECT) {
  655. return SSL_ERROR_WANT_CONNECT;
  656. }
  657. if (reason == BIO_RR_ACCEPT) {
  658. return SSL_ERROR_WANT_ACCEPT;
  659. }
  660. return SSL_ERROR_SYSCALL;
  661. }
  662. }
  663. if (SSL_want_x509_lookup(ssl)) {
  664. return SSL_ERROR_WANT_X509_LOOKUP;
  665. }
  666. if (SSL_want_channel_id_lookup(ssl)) {
  667. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  668. }
  669. if (SSL_want_private_key_operation(ssl)) {
  670. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  671. }
  672. return SSL_ERROR_SYSCALL;
  673. }
  674. void SSL_CTX_set_min_version(SSL_CTX *ctx, uint16_t version) {
  675. ctx->min_version = version;
  676. }
  677. void SSL_CTX_set_max_version(SSL_CTX *ctx, uint16_t version) {
  678. ctx->max_version = version;
  679. }
  680. void SSL_set_min_version(SSL *ssl, uint16_t version) {
  681. ssl->min_version = version;
  682. }
  683. void SSL_set_max_version(SSL *ssl, uint16_t version) {
  684. ssl->max_version = version;
  685. }
  686. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  687. ctx->options |= options;
  688. return ctx->options;
  689. }
  690. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  691. ctx->options &= ~options;
  692. return ctx->options;
  693. }
  694. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  695. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  696. ssl->options |= options;
  697. return ssl->options;
  698. }
  699. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  700. ssl->options &= ~options;
  701. return ssl->options;
  702. }
  703. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  704. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  705. ctx->mode |= mode;
  706. return ctx->mode;
  707. }
  708. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  709. ctx->mode &= ~mode;
  710. return ctx->mode;
  711. }
  712. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  713. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  714. ssl->mode |= mode;
  715. return ssl->mode;
  716. }
  717. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  718. ssl->mode &= ~mode;
  719. return ssl->mode;
  720. }
  721. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  722. X509 *SSL_get_peer_certificate(const SSL *ssl) {
  723. if (ssl == NULL || ssl->session == NULL || ssl->session->peer == NULL) {
  724. return NULL;
  725. }
  726. return X509_up_ref(ssl->session->peer);
  727. }
  728. STACK_OF(X509) *SSL_get_peer_cert_chain(const SSL *ssl) {
  729. if (ssl == NULL || ssl->session == NULL) {
  730. return NULL;
  731. }
  732. return ssl->session->cert_chain;
  733. }
  734. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  735. size_t max_out) {
  736. /* The tls-unique value is the first Finished message in the handshake, which
  737. * is the client's in a full handshake and the server's for a resumption. See
  738. * https://tools.ietf.org/html/rfc5929#section-3.1. */
  739. const uint8_t *finished = ssl->s3->previous_client_finished;
  740. size_t finished_len = ssl->s3->previous_client_finished_len;
  741. if (ssl->hit) {
  742. /* tls-unique is broken for resumed sessions unless EMS is used. */
  743. if (!ssl->session->extended_master_secret) {
  744. goto err;
  745. }
  746. finished = ssl->s3->previous_server_finished;
  747. finished_len = ssl->s3->previous_server_finished_len;
  748. }
  749. if (!ssl->s3->initial_handshake_complete ||
  750. ssl->version < TLS1_VERSION) {
  751. goto err;
  752. }
  753. *out_len = finished_len;
  754. if (finished_len > max_out) {
  755. *out_len = max_out;
  756. }
  757. memcpy(out, finished, *out_len);
  758. return 1;
  759. err:
  760. *out_len = 0;
  761. memset(out, 0, max_out);
  762. return 0;
  763. }
  764. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  765. unsigned sid_ctx_len) {
  766. if (sid_ctx_len > sizeof(ctx->sid_ctx)) {
  767. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  768. return 0;
  769. }
  770. ctx->sid_ctx_length = sid_ctx_len;
  771. memcpy(ctx->sid_ctx, sid_ctx, sid_ctx_len);
  772. return 1;
  773. }
  774. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  775. unsigned sid_ctx_len) {
  776. if (sid_ctx_len > SSL_MAX_SID_CTX_LENGTH) {
  777. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  778. return 0;
  779. }
  780. ssl->sid_ctx_length = sid_ctx_len;
  781. memcpy(ssl->sid_ctx, sid_ctx, sid_ctx_len);
  782. return 1;
  783. }
  784. int SSL_CTX_set_purpose(SSL_CTX *ctx, int purpose) {
  785. return X509_VERIFY_PARAM_set_purpose(ctx->param, purpose);
  786. }
  787. int SSL_set_purpose(SSL *ssl, int purpose) {
  788. return X509_VERIFY_PARAM_set_purpose(ssl->param, purpose);
  789. }
  790. int SSL_CTX_set_trust(SSL_CTX *ctx, int trust) {
  791. return X509_VERIFY_PARAM_set_trust(ctx->param, trust);
  792. }
  793. int SSL_set_trust(SSL *ssl, int trust) {
  794. return X509_VERIFY_PARAM_set_trust(ssl->param, trust);
  795. }
  796. int SSL_CTX_set1_param(SSL_CTX *ctx, const X509_VERIFY_PARAM *param) {
  797. return X509_VERIFY_PARAM_set1(ctx->param, param);
  798. }
  799. int SSL_set1_param(SSL *ssl, const X509_VERIFY_PARAM *param) {
  800. return X509_VERIFY_PARAM_set1(ssl->param, param);
  801. }
  802. void ssl_cipher_preference_list_free(
  803. struct ssl_cipher_preference_list_st *cipher_list) {
  804. if (cipher_list == NULL) {
  805. return;
  806. }
  807. sk_SSL_CIPHER_free(cipher_list->ciphers);
  808. OPENSSL_free(cipher_list->in_group_flags);
  809. OPENSSL_free(cipher_list);
  810. }
  811. X509_VERIFY_PARAM *SSL_CTX_get0_param(SSL_CTX *ctx) { return ctx->param; }
  812. X509_VERIFY_PARAM *SSL_get0_param(SSL *ssl) { return ssl->param; }
  813. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  814. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  815. int SSL_get_rfd(const SSL *ssl) {
  816. int ret = -1;
  817. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  818. if (b != NULL) {
  819. BIO_get_fd(b, &ret);
  820. }
  821. return ret;
  822. }
  823. int SSL_get_wfd(const SSL *ssl) {
  824. int ret = -1;
  825. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  826. if (b != NULL) {
  827. BIO_get_fd(b, &ret);
  828. }
  829. return ret;
  830. }
  831. int SSL_set_fd(SSL *ssl, int fd) {
  832. BIO *bio = BIO_new(BIO_s_fd());
  833. if (bio == NULL) {
  834. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  835. return 0;
  836. }
  837. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  838. SSL_set_bio(ssl, bio, bio);
  839. return 1;
  840. }
  841. int SSL_set_wfd(SSL *ssl, int fd) {
  842. if (ssl->rbio == NULL ||
  843. BIO_method_type(ssl->rbio) != BIO_TYPE_FD ||
  844. BIO_get_fd(ssl->rbio, NULL) != fd) {
  845. BIO *bio = BIO_new(BIO_s_fd());
  846. if (bio == NULL) {
  847. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  848. return 0;
  849. }
  850. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  851. SSL_set_bio(ssl, SSL_get_rbio(ssl), bio);
  852. } else {
  853. SSL_set_bio(ssl, SSL_get_rbio(ssl), SSL_get_rbio(ssl));
  854. }
  855. return 1;
  856. }
  857. int SSL_set_rfd(SSL *ssl, int fd) {
  858. if (ssl->wbio == NULL || BIO_method_type(ssl->wbio) != BIO_TYPE_FD ||
  859. BIO_get_fd(ssl->wbio, NULL) != fd) {
  860. BIO *bio = BIO_new(BIO_s_fd());
  861. if (bio == NULL) {
  862. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  863. return 0;
  864. }
  865. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  866. SSL_set_bio(ssl, bio, SSL_get_wbio(ssl));
  867. } else {
  868. SSL_set_bio(ssl, SSL_get_wbio(ssl), SSL_get_wbio(ssl));
  869. }
  870. return 1;
  871. }
  872. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  873. size_t ret = 0;
  874. if (ssl->s3 != NULL) {
  875. ret = ssl->s3->tmp.finish_md_len;
  876. if (count > ret) {
  877. count = ret;
  878. }
  879. memcpy(buf, ssl->s3->tmp.finish_md, count);
  880. }
  881. return ret;
  882. }
  883. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  884. size_t ret = 0;
  885. if (ssl->s3 != NULL) {
  886. ret = ssl->s3->tmp.peer_finish_md_len;
  887. if (count > ret) {
  888. count = ret;
  889. }
  890. memcpy(buf, ssl->s3->tmp.peer_finish_md, count);
  891. }
  892. return ret;
  893. }
  894. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  895. int SSL_get_verify_depth(const SSL *ssl) {
  896. return X509_VERIFY_PARAM_get_depth(ssl->param);
  897. }
  898. int SSL_get_extms_support(const SSL *ssl) {
  899. return ssl->s3->tmp.extended_master_secret == 1;
  900. }
  901. int (*SSL_get_verify_callback(const SSL *ssl))(int, X509_STORE_CTX *) {
  902. return ssl->verify_callback;
  903. }
  904. int SSL_CTX_get_verify_mode(const SSL_CTX *ctx) { return ctx->verify_mode; }
  905. int SSL_CTX_get_verify_depth(const SSL_CTX *ctx) {
  906. return X509_VERIFY_PARAM_get_depth(ctx->param);
  907. }
  908. int (*SSL_CTX_get_verify_callback(const SSL_CTX *ctx))(
  909. int ok, X509_STORE_CTX *store_ctx) {
  910. return ctx->default_verify_callback;
  911. }
  912. void SSL_set_verify(SSL *ssl, int mode,
  913. int (*callback)(int ok, X509_STORE_CTX *store_ctx)) {
  914. ssl->verify_mode = mode;
  915. if (callback != NULL) {
  916. ssl->verify_callback = callback;
  917. }
  918. }
  919. void SSL_set_verify_depth(SSL *ssl, int depth) {
  920. X509_VERIFY_PARAM_set_depth(ssl->param, depth);
  921. }
  922. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  923. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  924. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  925. void SSL_set_read_ahead(SSL *ssl, int yes) { }
  926. int SSL_pending(const SSL *ssl) {
  927. if (ssl->s3->rrec.type != SSL3_RT_APPLICATION_DATA) {
  928. return 0;
  929. }
  930. return ssl->s3->rrec.length;
  931. }
  932. /* Fix this so it checks all the valid key/cert options */
  933. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  934. if (ctx->cert->x509 == NULL) {
  935. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  936. return 0;
  937. }
  938. if (ctx->cert->privatekey == NULL) {
  939. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  940. return 0;
  941. }
  942. return X509_check_private_key(ctx->cert->x509, ctx->cert->privatekey);
  943. }
  944. /* Fix this function so that it takes an optional type parameter */
  945. int SSL_check_private_key(const SSL *ssl) {
  946. if (ssl->cert->x509 == NULL) {
  947. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_ASSIGNED);
  948. return 0;
  949. }
  950. if (ssl->cert->privatekey == NULL) {
  951. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_PRIVATE_KEY_ASSIGNED);
  952. return 0;
  953. }
  954. return X509_check_private_key(ssl->cert->x509, ssl->cert->privatekey);
  955. }
  956. long SSL_get_default_timeout(const SSL *ssl) {
  957. return SSL_DEFAULT_SESSION_TIMEOUT;
  958. }
  959. int SSL_renegotiate(SSL *ssl) {
  960. /* Caller-initiated renegotiation is not supported. */
  961. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  962. return 0;
  963. }
  964. int SSL_renegotiate_pending(SSL *ssl) {
  965. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  966. }
  967. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  968. return ctx->max_cert_list;
  969. }
  970. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  971. if (max_cert_list > kMaxHandshakeSize) {
  972. max_cert_list = kMaxHandshakeSize;
  973. }
  974. ctx->max_cert_list = (uint32_t)max_cert_list;
  975. }
  976. size_t SSL_get_max_cert_list(const SSL *ssl) {
  977. return ssl->max_cert_list;
  978. }
  979. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  980. if (max_cert_list > kMaxHandshakeSize) {
  981. max_cert_list = kMaxHandshakeSize;
  982. }
  983. ssl->max_cert_list = (uint32_t)max_cert_list;
  984. }
  985. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  986. if (max_send_fragment < 512) {
  987. max_send_fragment = 512;
  988. }
  989. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  990. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  991. }
  992. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  993. return 1;
  994. }
  995. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  996. if (max_send_fragment < 512) {
  997. max_send_fragment = 512;
  998. }
  999. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1000. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1001. }
  1002. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1003. return 1;
  1004. }
  1005. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1006. if (!SSL_IS_DTLS(ssl) || mtu < dtls1_min_mtu()) {
  1007. return 0;
  1008. }
  1009. ssl->d1->mtu = mtu;
  1010. return 1;
  1011. }
  1012. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1013. return ssl->s3->send_connection_binding;
  1014. }
  1015. LHASH_OF(SSL_SESSION) *SSL_CTX_sessions(SSL_CTX *ctx) { return ctx->sessions; }
  1016. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1017. return lh_SSL_SESSION_num_items(ctx->sessions);
  1018. }
  1019. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1020. unsigned long ret = ctx->session_cache_size;
  1021. ctx->session_cache_size = size;
  1022. return ret;
  1023. }
  1024. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1025. return ctx->session_cache_size;
  1026. }
  1027. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1028. int ret = ctx->session_cache_mode;
  1029. ctx->session_cache_mode = mode;
  1030. return ret;
  1031. }
  1032. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1033. return ctx->session_cache_mode;
  1034. }
  1035. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1036. if (ssl == NULL) {
  1037. return NULL;
  1038. }
  1039. if (ssl->cipher_list != NULL) {
  1040. return ssl->cipher_list->ciphers;
  1041. }
  1042. if (ssl->version >= TLS1_1_VERSION && ssl->ctx != NULL &&
  1043. ssl->ctx->cipher_list_tls11 != NULL) {
  1044. return ssl->ctx->cipher_list_tls11->ciphers;
  1045. }
  1046. if (ssl->version >= TLS1_VERSION && ssl->ctx != NULL &&
  1047. ssl->ctx->cipher_list_tls10 != NULL) {
  1048. return ssl->ctx->cipher_list_tls10->ciphers;
  1049. }
  1050. if (ssl->ctx != NULL && ssl->ctx->cipher_list != NULL) {
  1051. return ssl->ctx->cipher_list->ciphers;
  1052. }
  1053. return NULL;
  1054. }
  1055. /* return a STACK of the ciphers available for the SSL and in order of
  1056. * algorithm id */
  1057. STACK_OF(SSL_CIPHER) *ssl_get_ciphers_by_id(SSL *ssl) {
  1058. if (ssl == NULL) {
  1059. return NULL;
  1060. }
  1061. if (ssl->cipher_list_by_id != NULL) {
  1062. return ssl->cipher_list_by_id;
  1063. }
  1064. if (ssl->ctx != NULL && ssl->ctx->cipher_list_by_id != NULL) {
  1065. return ssl->ctx->cipher_list_by_id;
  1066. }
  1067. return NULL;
  1068. }
  1069. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1070. const SSL_CIPHER *c;
  1071. STACK_OF(SSL_CIPHER) *sk;
  1072. if (ssl == NULL) {
  1073. return NULL;
  1074. }
  1075. sk = SSL_get_ciphers(ssl);
  1076. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1077. return NULL;
  1078. }
  1079. c = sk_SSL_CIPHER_value(sk, n);
  1080. if (c == NULL) {
  1081. return NULL;
  1082. }
  1083. return c->name;
  1084. }
  1085. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1086. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1087. ctx->method, &ctx->cipher_list, &ctx->cipher_list_by_id, str);
  1088. if (cipher_list == NULL) {
  1089. return 0;
  1090. }
  1091. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1092. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1093. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1094. return 0;
  1095. }
  1096. return 1;
  1097. }
  1098. int SSL_CTX_set_cipher_list_tls10(SSL_CTX *ctx, const char *str) {
  1099. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1100. ctx->method, &ctx->cipher_list_tls10, NULL, str);
  1101. if (cipher_list == NULL) {
  1102. return 0;
  1103. }
  1104. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1105. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1106. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1107. return 0;
  1108. }
  1109. return 1;
  1110. }
  1111. int SSL_CTX_set_cipher_list_tls11(SSL_CTX *ctx, const char *str) {
  1112. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1113. ctx->method, &ctx->cipher_list_tls11, NULL, str);
  1114. if (cipher_list == NULL) {
  1115. return 0;
  1116. }
  1117. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1118. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1119. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1120. return 0;
  1121. }
  1122. return 1;
  1123. }
  1124. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1125. STACK_OF(SSL_CIPHER) *cipher_list = ssl_create_cipher_list(
  1126. ssl->ctx->method, &ssl->cipher_list, &ssl->cipher_list_by_id, str);
  1127. if (cipher_list == NULL) {
  1128. return 0;
  1129. }
  1130. /* |ssl_create_cipher_list| may succeed but return an empty cipher list. */
  1131. if (sk_SSL_CIPHER_num(cipher_list) == 0) {
  1132. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHER_MATCH);
  1133. return 0;
  1134. }
  1135. return 1;
  1136. }
  1137. STACK_OF(SSL_CIPHER) *ssl_bytes_to_cipher_list(SSL *ssl, const CBS *cbs) {
  1138. CBS cipher_suites = *cbs;
  1139. const SSL_CIPHER *c;
  1140. STACK_OF(SSL_CIPHER) *sk;
  1141. if (ssl->s3) {
  1142. ssl->s3->send_connection_binding = 0;
  1143. }
  1144. if (CBS_len(&cipher_suites) % 2 != 0) {
  1145. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST);
  1146. return NULL;
  1147. }
  1148. sk = sk_SSL_CIPHER_new_null();
  1149. if (sk == NULL) {
  1150. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1151. goto err;
  1152. }
  1153. while (CBS_len(&cipher_suites) > 0) {
  1154. uint16_t cipher_suite;
  1155. if (!CBS_get_u16(&cipher_suites, &cipher_suite)) {
  1156. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1157. goto err;
  1158. }
  1159. /* Check for SCSV. */
  1160. if (ssl->s3 && cipher_suite == (SSL3_CK_SCSV & 0xffff)) {
  1161. /* SCSV is fatal if renegotiating. */
  1162. if (ssl->s3->initial_handshake_complete) {
  1163. OPENSSL_PUT_ERROR(SSL, SSL_R_SCSV_RECEIVED_WHEN_RENEGOTIATING);
  1164. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE);
  1165. goto err;
  1166. }
  1167. ssl->s3->send_connection_binding = 1;
  1168. continue;
  1169. }
  1170. /* Check for FALLBACK_SCSV. */
  1171. if (ssl->s3 && cipher_suite == (SSL3_CK_FALLBACK_SCSV & 0xffff)) {
  1172. uint16_t max_version = ssl3_get_max_server_version(ssl);
  1173. if (SSL_IS_DTLS(ssl) ? (uint16_t)ssl->version > max_version
  1174. : (uint16_t)ssl->version < max_version) {
  1175. OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK);
  1176. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL3_AD_INAPPROPRIATE_FALLBACK);
  1177. goto err;
  1178. }
  1179. continue;
  1180. }
  1181. c = SSL_get_cipher_by_value(cipher_suite);
  1182. if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) {
  1183. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1184. goto err;
  1185. }
  1186. }
  1187. return sk;
  1188. err:
  1189. sk_SSL_CIPHER_free(sk);
  1190. return NULL;
  1191. }
  1192. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1193. if (type != TLSEXT_NAMETYPE_host_name) {
  1194. return NULL;
  1195. }
  1196. /* Historically, |SSL_get_servername| was also the configuration getter
  1197. * corresponding to |SSL_set_tlsext_host_name|. */
  1198. if (ssl->tlsext_hostname != NULL) {
  1199. return ssl->tlsext_hostname;
  1200. }
  1201. if (ssl->session == NULL) {
  1202. return NULL;
  1203. }
  1204. return ssl->session->tlsext_hostname;
  1205. }
  1206. int SSL_get_servername_type(const SSL *ssl) {
  1207. if (ssl->session != NULL && ssl->session->tlsext_hostname != NULL) {
  1208. return TLSEXT_NAMETYPE_host_name;
  1209. }
  1210. return -1;
  1211. }
  1212. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1213. ctx->signed_cert_timestamps_enabled = 1;
  1214. }
  1215. int SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1216. ssl->signed_cert_timestamps_enabled = 1;
  1217. return 1;
  1218. }
  1219. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1220. ctx->ocsp_stapling_enabled = 1;
  1221. }
  1222. int SSL_enable_ocsp_stapling(SSL *ssl) {
  1223. ssl->ocsp_stapling_enabled = 1;
  1224. return 1;
  1225. }
  1226. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1227. size_t *out_len) {
  1228. SSL_SESSION *session = ssl->session;
  1229. *out_len = 0;
  1230. *out = NULL;
  1231. if (ssl->server || !session || !session->tlsext_signed_cert_timestamp_list) {
  1232. return;
  1233. }
  1234. *out = session->tlsext_signed_cert_timestamp_list;
  1235. *out_len = session->tlsext_signed_cert_timestamp_list_length;
  1236. }
  1237. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1238. size_t *out_len) {
  1239. SSL_SESSION *session = ssl->session;
  1240. *out_len = 0;
  1241. *out = NULL;
  1242. if (ssl->server || !session || !session->ocsp_response) {
  1243. return;
  1244. }
  1245. *out = session->ocsp_response;
  1246. *out_len = session->ocsp_response_length;
  1247. }
  1248. int SSL_CTX_set_signed_cert_timestamp_list(SSL_CTX *ctx, const uint8_t *list,
  1249. size_t list_len) {
  1250. OPENSSL_free(ctx->signed_cert_timestamp_list);
  1251. ctx->signed_cert_timestamp_list_length = 0;
  1252. ctx->signed_cert_timestamp_list = BUF_memdup(list, list_len);
  1253. if (ctx->signed_cert_timestamp_list == NULL) {
  1254. return 0;
  1255. }
  1256. ctx->signed_cert_timestamp_list_length = list_len;
  1257. return 1;
  1258. }
  1259. int SSL_CTX_set_ocsp_response(SSL_CTX *ctx, const uint8_t *response,
  1260. size_t response_len) {
  1261. OPENSSL_free(ctx->ocsp_response);
  1262. ctx->ocsp_response_length = 0;
  1263. ctx->ocsp_response = BUF_memdup(response, response_len);
  1264. if (ctx->ocsp_response == NULL) {
  1265. return 0;
  1266. }
  1267. ctx->ocsp_response_length = response_len;
  1268. return 1;
  1269. }
  1270. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len,
  1271. const uint8_t *server, unsigned server_len,
  1272. const uint8_t *client, unsigned client_len) {
  1273. unsigned int i, j;
  1274. const uint8_t *result;
  1275. int status = OPENSSL_NPN_UNSUPPORTED;
  1276. /* For each protocol in server preference order, see if we support it. */
  1277. for (i = 0; i < server_len;) {
  1278. for (j = 0; j < client_len;) {
  1279. if (server[i] == client[j] &&
  1280. memcmp(&server[i + 1], &client[j + 1], server[i]) == 0) {
  1281. /* We found a match */
  1282. result = &server[i];
  1283. status = OPENSSL_NPN_NEGOTIATED;
  1284. goto found;
  1285. }
  1286. j += client[j];
  1287. j++;
  1288. }
  1289. i += server[i];
  1290. i++;
  1291. }
  1292. /* There's no overlap between our protocols and the server's list. */
  1293. result = client;
  1294. status = OPENSSL_NPN_NO_OVERLAP;
  1295. found:
  1296. *out = (uint8_t *)result + 1;
  1297. *out_len = result[0];
  1298. return status;
  1299. }
  1300. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1301. unsigned *out_len) {
  1302. *out_data = ssl->s3->next_proto_negotiated;
  1303. if (*out_data == NULL) {
  1304. *out_len = 0;
  1305. } else {
  1306. *out_len = ssl->s3->next_proto_negotiated_len;
  1307. }
  1308. }
  1309. void SSL_CTX_set_next_protos_advertised_cb(
  1310. SSL_CTX *ctx,
  1311. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1312. void *arg) {
  1313. ctx->next_protos_advertised_cb = cb;
  1314. ctx->next_protos_advertised_cb_arg = arg;
  1315. }
  1316. void SSL_CTX_set_next_proto_select_cb(
  1317. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1318. const uint8_t *in, unsigned in_len, void *arg),
  1319. void *arg) {
  1320. ctx->next_proto_select_cb = cb;
  1321. ctx->next_proto_select_cb_arg = arg;
  1322. }
  1323. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1324. unsigned protos_len) {
  1325. OPENSSL_free(ctx->alpn_client_proto_list);
  1326. ctx->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1327. if (!ctx->alpn_client_proto_list) {
  1328. return 1;
  1329. }
  1330. ctx->alpn_client_proto_list_len = protos_len;
  1331. return 0;
  1332. }
  1333. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1334. OPENSSL_free(ssl->alpn_client_proto_list);
  1335. ssl->alpn_client_proto_list = BUF_memdup(protos, protos_len);
  1336. if (!ssl->alpn_client_proto_list) {
  1337. return 1;
  1338. }
  1339. ssl->alpn_client_proto_list_len = protos_len;
  1340. return 0;
  1341. }
  1342. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1343. int (*cb)(SSL *ssl, const uint8_t **out,
  1344. uint8_t *out_len, const uint8_t *in,
  1345. unsigned in_len, void *arg),
  1346. void *arg) {
  1347. ctx->alpn_select_cb = cb;
  1348. ctx->alpn_select_cb_arg = arg;
  1349. }
  1350. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1351. unsigned *out_len) {
  1352. *out_data = NULL;
  1353. if (ssl->s3) {
  1354. *out_data = ssl->s3->alpn_selected;
  1355. }
  1356. if (*out_data == NULL) {
  1357. *out_len = 0;
  1358. } else {
  1359. *out_len = ssl->s3->alpn_selected_len;
  1360. }
  1361. }
  1362. void SSL_CTX_set_cert_verify_callback(SSL_CTX *ctx,
  1363. int (*cb)(X509_STORE_CTX *store_ctx,
  1364. void *arg),
  1365. void *arg) {
  1366. ctx->app_verify_callback = cb;
  1367. ctx->app_verify_arg = arg;
  1368. }
  1369. void SSL_CTX_set_verify(SSL_CTX *ctx, int mode,
  1370. int (*cb)(int, X509_STORE_CTX *)) {
  1371. ctx->verify_mode = mode;
  1372. ctx->default_verify_callback = cb;
  1373. }
  1374. void SSL_CTX_set_verify_depth(SSL_CTX *ctx, int depth) {
  1375. X509_VERIFY_PARAM_set_depth(ctx->param, depth);
  1376. }
  1377. void SSL_CTX_set_cert_cb(SSL_CTX *ctx, int (*cb)(SSL *ssl, void *arg),
  1378. void *arg) {
  1379. ssl_cert_set_cert_cb(ctx->cert, cb, arg);
  1380. }
  1381. void SSL_set_cert_cb(SSL *ssl, int (*cb)(SSL *ssl, void *arg), void *arg) {
  1382. ssl_cert_set_cert_cb(ssl->cert, cb, arg);
  1383. }
  1384. void ssl_get_compatible_server_ciphers(SSL *ssl, uint32_t *out_mask_k,
  1385. uint32_t *out_mask_a) {
  1386. CERT *c = ssl->cert;
  1387. int have_rsa_cert = 0, dh_tmp;
  1388. uint32_t mask_k, mask_a;
  1389. int have_ecc_cert = 0, ecdsa_ok;
  1390. X509 *x;
  1391. dh_tmp = (c->dh_tmp != NULL || c->dh_tmp_cb != NULL);
  1392. if (ssl->cert->x509 != NULL && ssl_has_private_key(ssl)) {
  1393. if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) {
  1394. have_rsa_cert = 1;
  1395. } else if (ssl_private_key_type(ssl) == EVP_PKEY_EC) {
  1396. have_ecc_cert = 1;
  1397. }
  1398. }
  1399. mask_k = 0;
  1400. mask_a = 0;
  1401. if (dh_tmp) {
  1402. mask_k |= SSL_kDHE;
  1403. }
  1404. if (have_rsa_cert) {
  1405. mask_k |= SSL_kRSA;
  1406. mask_a |= SSL_aRSA;
  1407. }
  1408. /* An ECC certificate may be usable for ECDSA cipher suites depending on the
  1409. * key usage extension and on the client's curve preferences. */
  1410. if (have_ecc_cert) {
  1411. x = c->x509;
  1412. /* This call populates extension flags (ex_flags). */
  1413. X509_check_purpose(x, -1, 0);
  1414. ecdsa_ok = (x->ex_flags & EXFLAG_KUSAGE)
  1415. ? (x->ex_kusage & X509v3_KU_DIGITAL_SIGNATURE)
  1416. : 1;
  1417. if (!tls1_check_ec_cert(ssl, x)) {
  1418. ecdsa_ok = 0;
  1419. }
  1420. if (ecdsa_ok) {
  1421. mask_a |= SSL_aECDSA;
  1422. }
  1423. }
  1424. /* If we are considering an ECC cipher suite that uses an ephemeral EC
  1425. * key, check for a shared curve. */
  1426. uint16_t unused;
  1427. if (tls1_get_shared_curve(ssl, &unused)) {
  1428. mask_k |= SSL_kECDHE;
  1429. }
  1430. /* PSK requires a server callback. */
  1431. if (ssl->psk_server_callback != NULL) {
  1432. mask_k |= SSL_kPSK;
  1433. mask_a |= SSL_aPSK;
  1434. }
  1435. *out_mask_k = mask_k;
  1436. *out_mask_a = mask_a;
  1437. }
  1438. void ssl_update_cache(SSL *ssl, int mode) {
  1439. SSL_CTX *ctx = ssl->initial_ctx;
  1440. /* Never cache sessions with empty session IDs. */
  1441. if (ssl->session->session_id_length == 0 ||
  1442. (ctx->session_cache_mode & mode) != mode) {
  1443. return;
  1444. }
  1445. /* Clients never use the internal session cache. */
  1446. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  1447. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1448. /* A client may see new sessions on abbreviated handshakes if the server
  1449. * decides to renew the ticket. Once the handshake is completed, it should be
  1450. * inserted into the cache. */
  1451. if (!ssl->hit || (!ssl->server && ssl->tlsext_ticket_expected)) {
  1452. if (use_internal_cache) {
  1453. SSL_CTX_add_session(ctx, ssl->session);
  1454. }
  1455. if (ctx->new_session_cb != NULL &&
  1456. !ctx->new_session_cb(ssl, SSL_SESSION_up_ref(ssl->session))) {
  1457. /* |new_session_cb|'s return value signals whether it took ownership. */
  1458. SSL_SESSION_free(ssl->session);
  1459. }
  1460. }
  1461. if (use_internal_cache &&
  1462. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  1463. /* Automatically flush the internal session cache every 255 connections. */
  1464. int flush_cache = 0;
  1465. CRYPTO_MUTEX_lock_write(&ctx->lock);
  1466. ctx->handshakes_since_cache_flush++;
  1467. if (ctx->handshakes_since_cache_flush >= 255) {
  1468. flush_cache = 1;
  1469. ctx->handshakes_since_cache_flush = 0;
  1470. }
  1471. CRYPTO_MUTEX_unlock(&ctx->lock);
  1472. if (flush_cache) {
  1473. SSL_CTX_flush_sessions(ctx, (unsigned long)time(NULL));
  1474. }
  1475. }
  1476. }
  1477. static const char *ssl_get_version(int version) {
  1478. switch (version) {
  1479. case TLS1_2_VERSION:
  1480. return "TLSv1.2";
  1481. case TLS1_1_VERSION:
  1482. return "TLSv1.1";
  1483. case TLS1_VERSION:
  1484. return "TLSv1";
  1485. case SSL3_VERSION:
  1486. return "SSLv3";
  1487. case DTLS1_VERSION:
  1488. return "DTLSv1";
  1489. case DTLS1_2_VERSION:
  1490. return "DTLSv1.2";
  1491. default:
  1492. return "unknown";
  1493. }
  1494. }
  1495. const char *SSL_get_version(const SSL *ssl) {
  1496. return ssl_get_version(ssl->version);
  1497. }
  1498. const char *SSL_SESSION_get_version(const SSL_SESSION *session) {
  1499. return ssl_get_version(session->ssl_version);
  1500. }
  1501. X509 *SSL_get_certificate(const SSL *ssl) {
  1502. if (ssl->cert != NULL) {
  1503. return ssl->cert->x509;
  1504. }
  1505. return NULL;
  1506. }
  1507. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1508. if (ssl->cert != NULL) {
  1509. return ssl->cert->privatekey;
  1510. }
  1511. return NULL;
  1512. }
  1513. X509 *SSL_CTX_get0_certificate(const SSL_CTX *ctx) {
  1514. if (ctx->cert != NULL) {
  1515. return ctx->cert->x509;
  1516. }
  1517. return NULL;
  1518. }
  1519. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1520. if (ctx->cert != NULL) {
  1521. return ctx->cert->privatekey;
  1522. }
  1523. return NULL;
  1524. }
  1525. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1526. if (ssl->s3->aead_write_ctx == NULL) {
  1527. return NULL;
  1528. }
  1529. return ssl->s3->aead_write_ctx->cipher;
  1530. }
  1531. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  1532. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  1533. int ssl_init_wbio_buffer(SSL *ssl, int push) {
  1534. BIO *bbio;
  1535. if (ssl->bbio == NULL) {
  1536. bbio = BIO_new(BIO_f_buffer());
  1537. if (bbio == NULL) {
  1538. return 0;
  1539. }
  1540. ssl->bbio = bbio;
  1541. } else {
  1542. bbio = ssl->bbio;
  1543. if (ssl->bbio == ssl->wbio) {
  1544. ssl->wbio = BIO_pop(ssl->wbio);
  1545. }
  1546. }
  1547. BIO_reset(bbio);
  1548. if (!BIO_set_read_buffer_size(bbio, 1)) {
  1549. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1550. return 0;
  1551. }
  1552. if (push) {
  1553. if (ssl->wbio != bbio) {
  1554. ssl->wbio = BIO_push(bbio, ssl->wbio);
  1555. }
  1556. } else {
  1557. if (ssl->wbio == bbio) {
  1558. ssl->wbio = BIO_pop(bbio);
  1559. }
  1560. }
  1561. return 1;
  1562. }
  1563. void ssl_free_wbio_buffer(SSL *ssl) {
  1564. if (ssl->bbio == NULL) {
  1565. return;
  1566. }
  1567. if (ssl->bbio == ssl->wbio) {
  1568. /* remove buffering */
  1569. ssl->wbio = BIO_pop(ssl->wbio);
  1570. }
  1571. BIO_free(ssl->bbio);
  1572. ssl->bbio = NULL;
  1573. }
  1574. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1575. ctx->quiet_shutdown = (mode != 0);
  1576. }
  1577. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1578. return ctx->quiet_shutdown;
  1579. }
  1580. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1581. ssl->quiet_shutdown = (mode != 0);
  1582. }
  1583. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1584. void SSL_set_shutdown(SSL *ssl, int mode) {
  1585. /* It is an error to clear any bits that have already been set. (We can't try
  1586. * to get a second close_notify or send two.) */
  1587. assert((ssl->shutdown & mode) == ssl->shutdown);
  1588. ssl->shutdown |= mode;
  1589. }
  1590. int SSL_get_shutdown(const SSL *ssl) { return ssl->shutdown; }
  1591. int SSL_version(const SSL *ssl) { return ssl->version; }
  1592. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1593. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1594. if (ssl->ctx == ctx) {
  1595. return ssl->ctx;
  1596. }
  1597. if (ctx == NULL) {
  1598. ctx = ssl->initial_ctx;
  1599. }
  1600. ssl_cert_free(ssl->cert);
  1601. ssl->cert = ssl_cert_dup(ctx->cert);
  1602. CRYPTO_refcount_inc(&ctx->references);
  1603. SSL_CTX_free(ssl->ctx); /* decrement reference count */
  1604. ssl->ctx = ctx;
  1605. ssl->sid_ctx_length = ctx->sid_ctx_length;
  1606. assert(ssl->sid_ctx_length <= sizeof(ssl->sid_ctx));
  1607. memcpy(ssl->sid_ctx, ctx->sid_ctx, sizeof(ssl->sid_ctx));
  1608. return ssl->ctx;
  1609. }
  1610. int SSL_CTX_set_default_verify_paths(SSL_CTX *ctx) {
  1611. return X509_STORE_set_default_paths(ctx->cert_store);
  1612. }
  1613. int SSL_CTX_load_verify_locations(SSL_CTX *ctx, const char *ca_file,
  1614. const char *ca_dir) {
  1615. return X509_STORE_load_locations(ctx->cert_store, ca_file, ca_dir);
  1616. }
  1617. void SSL_set_info_callback(SSL *ssl,
  1618. void (*cb)(const SSL *ssl, int type, int value)) {
  1619. ssl->info_callback = cb;
  1620. }
  1621. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1622. int value) {
  1623. return ssl->info_callback;
  1624. }
  1625. int SSL_state(const SSL *ssl) { return ssl->state; }
  1626. void SSL_set_state(SSL *ssl, int state) { }
  1627. void SSL_set_verify_result(SSL *ssl, long result) {
  1628. ssl->verify_result = result;
  1629. }
  1630. long SSL_get_verify_result(const SSL *ssl) { return ssl->verify_result; }
  1631. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1632. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  1633. int index;
  1634. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1635. dup_func, free_func)) {
  1636. return -1;
  1637. }
  1638. return index;
  1639. }
  1640. int SSL_set_ex_data(SSL *ssl, int idx, void *arg) {
  1641. return CRYPTO_set_ex_data(&ssl->ex_data, idx, arg);
  1642. }
  1643. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1644. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1645. }
  1646. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1647. CRYPTO_EX_dup *dup_func,
  1648. CRYPTO_EX_free *free_func) {
  1649. int index;
  1650. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1651. dup_func, free_func)) {
  1652. return -1;
  1653. }
  1654. return index;
  1655. }
  1656. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *arg) {
  1657. return CRYPTO_set_ex_data(&ctx->ex_data, idx, arg);
  1658. }
  1659. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1660. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1661. }
  1662. X509_STORE *SSL_CTX_get_cert_store(const SSL_CTX *ctx) {
  1663. return ctx->cert_store;
  1664. }
  1665. void SSL_CTX_set_cert_store(SSL_CTX *ctx, X509_STORE *store) {
  1666. X509_STORE_free(ctx->cert_store);
  1667. ctx->cert_store = store;
  1668. }
  1669. int SSL_want(const SSL *ssl) { return ssl->rwstate; }
  1670. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1671. RSA *(*cb)(SSL *ssl, int is_export,
  1672. int keylength)) {
  1673. }
  1674. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1675. int keylength)) {
  1676. }
  1677. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1678. DH *(*callback)(SSL *ssl, int is_export,
  1679. int keylength)) {
  1680. ctx->cert->dh_tmp_cb = callback;
  1681. }
  1682. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*callback)(SSL *ssl, int is_export,
  1683. int keylength)) {
  1684. ssl->cert->dh_tmp_cb = callback;
  1685. }
  1686. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1687. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1688. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1689. return 0;
  1690. }
  1691. OPENSSL_free(ctx->psk_identity_hint);
  1692. if (identity_hint != NULL) {
  1693. ctx->psk_identity_hint = BUF_strdup(identity_hint);
  1694. if (ctx->psk_identity_hint == NULL) {
  1695. return 0;
  1696. }
  1697. } else {
  1698. ctx->psk_identity_hint = NULL;
  1699. }
  1700. return 1;
  1701. }
  1702. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  1703. if (ssl == NULL) {
  1704. return 0;
  1705. }
  1706. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1707. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1708. return 0;
  1709. }
  1710. /* Clear currently configured hint, if any. */
  1711. OPENSSL_free(ssl->psk_identity_hint);
  1712. ssl->psk_identity_hint = NULL;
  1713. if (identity_hint != NULL) {
  1714. ssl->psk_identity_hint = BUF_strdup(identity_hint);
  1715. if (ssl->psk_identity_hint == NULL) {
  1716. return 0;
  1717. }
  1718. }
  1719. return 1;
  1720. }
  1721. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  1722. if (ssl == NULL) {
  1723. return NULL;
  1724. }
  1725. return ssl->psk_identity_hint;
  1726. }
  1727. const char *SSL_get_psk_identity(const SSL *ssl) {
  1728. if (ssl == NULL || ssl->session == NULL) {
  1729. return NULL;
  1730. }
  1731. return ssl->session->psk_identity;
  1732. }
  1733. void SSL_set_psk_client_callback(
  1734. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1735. unsigned max_identity_len, uint8_t *psk,
  1736. unsigned max_psk_len)) {
  1737. ssl->psk_client_callback = cb;
  1738. }
  1739. void SSL_CTX_set_psk_client_callback(
  1740. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1741. unsigned max_identity_len, uint8_t *psk,
  1742. unsigned max_psk_len)) {
  1743. ctx->psk_client_callback = cb;
  1744. }
  1745. void SSL_set_psk_server_callback(
  1746. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  1747. unsigned max_psk_len)) {
  1748. ssl->psk_server_callback = cb;
  1749. }
  1750. void SSL_CTX_set_psk_server_callback(
  1751. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  1752. uint8_t *psk, unsigned max_psk_len)) {
  1753. ctx->psk_server_callback = cb;
  1754. }
  1755. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  1756. void (*cb)(int write_p, int version,
  1757. int content_type, const void *buf,
  1758. size_t len, SSL *ssl, void *arg)) {
  1759. ctx->msg_callback = cb;
  1760. }
  1761. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  1762. ctx->msg_callback_arg = arg;
  1763. }
  1764. void SSL_set_msg_callback(SSL *ssl,
  1765. void (*cb)(int write_p, int version, int content_type,
  1766. const void *buf, size_t len, SSL *ssl,
  1767. void *arg)) {
  1768. ssl->msg_callback = cb;
  1769. }
  1770. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  1771. ssl->msg_callback_arg = arg;
  1772. }
  1773. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  1774. void (*cb)(const SSL *ssl, const char *line)) {
  1775. ctx->keylog_callback = cb;
  1776. }
  1777. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  1778. static const char hextable[] = "0123456789abcdef";
  1779. uint8_t *out;
  1780. size_t i;
  1781. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  1782. return 0;
  1783. }
  1784. for (i = 0; i < in_len; i++) {
  1785. *(out++) = (uint8_t)hextable[in[i] >> 4];
  1786. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  1787. }
  1788. return 1;
  1789. }
  1790. int ssl_log_rsa_client_key_exchange(const SSL *ssl,
  1791. const uint8_t *encrypted_premaster,
  1792. size_t encrypted_premaster_len,
  1793. const uint8_t *premaster,
  1794. size_t premaster_len) {
  1795. if (ssl->ctx->keylog_callback == NULL) {
  1796. return 1;
  1797. }
  1798. if (encrypted_premaster_len < 8) {
  1799. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1800. return 0;
  1801. }
  1802. CBB cbb;
  1803. uint8_t *out;
  1804. size_t out_len;
  1805. if (!CBB_init(&cbb, 4 + 16 + 1 + premaster_len * 2 + 1) ||
  1806. !CBB_add_bytes(&cbb, (const uint8_t *)"RSA ", 4) ||
  1807. /* Only the first 8 bytes of the encrypted premaster secret are
  1808. * logged. */
  1809. !cbb_add_hex(&cbb, encrypted_premaster, 8) ||
  1810. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1811. !cbb_add_hex(&cbb, premaster, premaster_len) ||
  1812. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  1813. !CBB_finish(&cbb, &out, &out_len)) {
  1814. CBB_cleanup(&cbb);
  1815. return 0;
  1816. }
  1817. ssl->ctx->keylog_callback(ssl, (const char *)out);
  1818. OPENSSL_free(out);
  1819. return 1;
  1820. }
  1821. int ssl_log_master_secret(const SSL *ssl, const uint8_t *client_random,
  1822. size_t client_random_len, const uint8_t *master,
  1823. size_t master_len) {
  1824. if (ssl->ctx->keylog_callback == NULL) {
  1825. return 1;
  1826. }
  1827. if (client_random_len != 32) {
  1828. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  1829. return 0;
  1830. }
  1831. CBB cbb;
  1832. uint8_t *out;
  1833. size_t out_len;
  1834. if (!CBB_init(&cbb, 14 + 64 + 1 + master_len * 2 + 1) ||
  1835. !CBB_add_bytes(&cbb, (const uint8_t *)"CLIENT_RANDOM ", 14) ||
  1836. !cbb_add_hex(&cbb, client_random, 32) ||
  1837. !CBB_add_bytes(&cbb, (const uint8_t *)" ", 1) ||
  1838. !cbb_add_hex(&cbb, master, master_len) ||
  1839. !CBB_add_u8(&cbb, 0 /* NUL */) ||
  1840. !CBB_finish(&cbb, &out, &out_len)) {
  1841. CBB_cleanup(&cbb);
  1842. return 0;
  1843. }
  1844. ssl->ctx->keylog_callback(ssl, (const char *)out);
  1845. OPENSSL_free(out);
  1846. return 1;
  1847. }
  1848. int SSL_is_init_finished(const SSL *ssl) {
  1849. return ssl->state == SSL_ST_OK;
  1850. }
  1851. int SSL_in_init(const SSL *ssl) {
  1852. return (ssl->state & SSL_ST_INIT) != 0;
  1853. }
  1854. int SSL_in_false_start(const SSL *ssl) {
  1855. return ssl->s3->tmp.in_false_start;
  1856. }
  1857. int SSL_cutthrough_complete(const SSL *ssl) {
  1858. return SSL_in_false_start(ssl);
  1859. }
  1860. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  1861. size_t *ssl_session_size) {
  1862. *ssl_size = sizeof(SSL);
  1863. *ssl_ctx_size = sizeof(SSL_CTX);
  1864. *ssl_session_size = sizeof(SSL_SESSION);
  1865. }
  1866. int ssl3_can_false_start(const SSL *ssl) {
  1867. const SSL_CIPHER *const cipher = SSL_get_current_cipher(ssl);
  1868. /* False Start only for TLS 1.2 with an ECDHE+AEAD cipher and ALPN or NPN. */
  1869. return !SSL_IS_DTLS(ssl) &&
  1870. SSL_version(ssl) >= TLS1_2_VERSION &&
  1871. (ssl->s3->alpn_selected || ssl->s3->next_proto_neg_seen) &&
  1872. cipher != NULL &&
  1873. cipher->algorithm_mkey == SSL_kECDHE &&
  1874. cipher->algorithm_mac == SSL_AEAD;
  1875. }
  1876. const SSL3_ENC_METHOD *ssl3_get_enc_method(uint16_t version) {
  1877. switch (version) {
  1878. case SSL3_VERSION:
  1879. return &SSLv3_enc_data;
  1880. case TLS1_VERSION:
  1881. case TLS1_1_VERSION:
  1882. case TLS1_2_VERSION:
  1883. case DTLS1_VERSION:
  1884. case DTLS1_2_VERSION:
  1885. return &TLSv1_enc_data;
  1886. default:
  1887. return NULL;
  1888. }
  1889. }
  1890. uint16_t ssl3_get_max_server_version(const SSL *ssl) {
  1891. uint16_t max_version;
  1892. if (SSL_IS_DTLS(ssl)) {
  1893. max_version = (ssl->max_version != 0) ? ssl->max_version : DTLS1_2_VERSION;
  1894. if (!(ssl->options & SSL_OP_NO_DTLSv1_2) &&
  1895. DTLS1_2_VERSION >= max_version) {
  1896. return DTLS1_2_VERSION;
  1897. }
  1898. if (!(ssl->options & SSL_OP_NO_DTLSv1) && DTLS1_VERSION >= max_version) {
  1899. return DTLS1_VERSION;
  1900. }
  1901. return 0;
  1902. }
  1903. max_version = (ssl->max_version != 0) ? ssl->max_version : TLS1_2_VERSION;
  1904. if (!(ssl->options & SSL_OP_NO_TLSv1_2) && TLS1_2_VERSION <= max_version) {
  1905. return TLS1_2_VERSION;
  1906. }
  1907. if (!(ssl->options & SSL_OP_NO_TLSv1_1) && TLS1_1_VERSION <= max_version) {
  1908. return TLS1_1_VERSION;
  1909. }
  1910. if (!(ssl->options & SSL_OP_NO_TLSv1) && TLS1_VERSION <= max_version) {
  1911. return TLS1_VERSION;
  1912. }
  1913. if (!(ssl->options & SSL_OP_NO_SSLv3) && SSL3_VERSION <= max_version) {
  1914. return SSL3_VERSION;
  1915. }
  1916. return 0;
  1917. }
  1918. uint16_t ssl3_get_mutual_version(SSL *ssl, uint16_t client_version) {
  1919. uint16_t version = 0;
  1920. if (SSL_IS_DTLS(ssl)) {
  1921. /* Clamp client_version to max_version. */
  1922. if (ssl->max_version != 0 && client_version < ssl->max_version) {
  1923. client_version = ssl->max_version;
  1924. }
  1925. if (client_version <= DTLS1_2_VERSION &&
  1926. !(ssl->options & SSL_OP_NO_DTLSv1_2)) {
  1927. version = DTLS1_2_VERSION;
  1928. } else if (client_version <= DTLS1_VERSION &&
  1929. !(ssl->options & SSL_OP_NO_DTLSv1)) {
  1930. version = DTLS1_VERSION;
  1931. }
  1932. /* Check against min_version. */
  1933. if (version != 0 && ssl->min_version != 0 && version > ssl->min_version) {
  1934. return 0;
  1935. }
  1936. return version;
  1937. } else {
  1938. /* Clamp client_version to max_version. */
  1939. if (ssl->max_version != 0 && client_version > ssl->max_version) {
  1940. client_version = ssl->max_version;
  1941. }
  1942. if (client_version >= TLS1_2_VERSION &&
  1943. !(ssl->options & SSL_OP_NO_TLSv1_2)) {
  1944. version = TLS1_2_VERSION;
  1945. } else if (client_version >= TLS1_1_VERSION &&
  1946. !(ssl->options & SSL_OP_NO_TLSv1_1)) {
  1947. version = TLS1_1_VERSION;
  1948. } else if (client_version >= TLS1_VERSION &&
  1949. !(ssl->options & SSL_OP_NO_TLSv1)) {
  1950. version = TLS1_VERSION;
  1951. } else if (client_version >= SSL3_VERSION &&
  1952. !(ssl->options & SSL_OP_NO_SSLv3)) {
  1953. version = SSL3_VERSION;
  1954. }
  1955. /* Check against min_version. */
  1956. if (version != 0 && ssl->min_version != 0 && version < ssl->min_version) {
  1957. return 0;
  1958. }
  1959. return version;
  1960. }
  1961. }
  1962. uint16_t ssl3_get_max_client_version(SSL *ssl) {
  1963. uint32_t options = ssl->options;
  1964. uint16_t version = 0;
  1965. /* OpenSSL's API for controlling versions entails blacklisting individual
  1966. * protocols. This has two problems. First, on the client, the protocol can
  1967. * only express a contiguous range of versions. Second, a library consumer
  1968. * trying to set a maximum version cannot disable protocol versions that get
  1969. * added in a future version of the library.
  1970. *
  1971. * To account for both of these, OpenSSL interprets the client-side bitmask
  1972. * as a min/max range by picking the lowest contiguous non-empty range of
  1973. * enabled protocols. Note that this means it is impossible to set a maximum
  1974. * version of TLS 1.2 in a future-proof way.
  1975. *
  1976. * By this scheme, the maximum version is the lowest version V such that V is
  1977. * enabled and V+1 is disabled or unimplemented. */
  1978. if (SSL_IS_DTLS(ssl)) {
  1979. if (!(options & SSL_OP_NO_DTLSv1_2)) {
  1980. version = DTLS1_2_VERSION;
  1981. }
  1982. if (!(options & SSL_OP_NO_DTLSv1) && (options & SSL_OP_NO_DTLSv1_2)) {
  1983. version = DTLS1_VERSION;
  1984. }
  1985. if (ssl->max_version != 0 && version < ssl->max_version) {
  1986. version = ssl->max_version;
  1987. }
  1988. } else {
  1989. if (!(options & SSL_OP_NO_TLSv1_2)) {
  1990. version = TLS1_2_VERSION;
  1991. }
  1992. if (!(options & SSL_OP_NO_TLSv1_1) && (options & SSL_OP_NO_TLSv1_2)) {
  1993. version = TLS1_1_VERSION;
  1994. }
  1995. if (!(options & SSL_OP_NO_TLSv1) && (options & SSL_OP_NO_TLSv1_1)) {
  1996. version = TLS1_VERSION;
  1997. }
  1998. if (!(options & SSL_OP_NO_SSLv3) && (options & SSL_OP_NO_TLSv1)) {
  1999. version = SSL3_VERSION;
  2000. }
  2001. if (ssl->max_version != 0 && version > ssl->max_version) {
  2002. version = ssl->max_version;
  2003. }
  2004. }
  2005. return version;
  2006. }
  2007. int ssl3_is_version_enabled(SSL *ssl, uint16_t version) {
  2008. if (SSL_IS_DTLS(ssl)) {
  2009. if (ssl->max_version != 0 && version < ssl->max_version) {
  2010. return 0;
  2011. }
  2012. if (ssl->min_version != 0 && version > ssl->min_version) {
  2013. return 0;
  2014. }
  2015. switch (version) {
  2016. case DTLS1_VERSION:
  2017. return !(ssl->options & SSL_OP_NO_DTLSv1);
  2018. case DTLS1_2_VERSION:
  2019. return !(ssl->options & SSL_OP_NO_DTLSv1_2);
  2020. default:
  2021. return 0;
  2022. }
  2023. } else {
  2024. if (ssl->max_version != 0 && version > ssl->max_version) {
  2025. return 0;
  2026. }
  2027. if (ssl->min_version != 0 && version < ssl->min_version) {
  2028. return 0;
  2029. }
  2030. switch (version) {
  2031. case SSL3_VERSION:
  2032. return !(ssl->options & SSL_OP_NO_SSLv3);
  2033. case TLS1_VERSION:
  2034. return !(ssl->options & SSL_OP_NO_TLSv1);
  2035. case TLS1_1_VERSION:
  2036. return !(ssl->options & SSL_OP_NO_TLSv1_1);
  2037. case TLS1_2_VERSION:
  2038. return !(ssl->options & SSL_OP_NO_TLSv1_2);
  2039. default:
  2040. return 0;
  2041. }
  2042. }
  2043. }
  2044. uint16_t ssl3_version_from_wire(const SSL *ssl, uint16_t wire_version) {
  2045. if (!SSL_IS_DTLS(ssl)) {
  2046. return wire_version;
  2047. }
  2048. uint16_t tls_version = ~wire_version;
  2049. uint16_t version = tls_version + 0x0201;
  2050. /* If either component overflowed, clamp it so comparisons still work. */
  2051. if ((version >> 8) < (tls_version >> 8)) {
  2052. version = 0xff00 | (version & 0xff);
  2053. }
  2054. if ((version & 0xff) < (tls_version & 0xff)) {
  2055. version = (version & 0xff00) | 0xff;
  2056. }
  2057. /* DTLS 1.0 maps to TLS 1.1, not TLS 1.0. */
  2058. if (version == TLS1_VERSION) {
  2059. version = TLS1_1_VERSION;
  2060. }
  2061. return version;
  2062. }
  2063. uint16_t ssl3_protocol_version(const SSL *ssl) {
  2064. assert(ssl->s3->have_version);
  2065. return ssl3_version_from_wire(ssl, ssl->version);
  2066. }
  2067. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2068. int SSL_is_server(SSL *ssl) { return ssl->server; }
  2069. void SSL_CTX_set_select_certificate_cb(
  2070. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2071. ctx->select_certificate_cb = cb;
  2072. }
  2073. void SSL_CTX_set_dos_protection_cb(
  2074. SSL_CTX *ctx, int (*cb)(const struct ssl_early_callback_ctx *)) {
  2075. ctx->dos_protection_cb = cb;
  2076. }
  2077. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2078. ssl->renegotiate_mode = mode;
  2079. }
  2080. void SSL_set_reject_peer_renegotiations(SSL *ssl, int reject) {
  2081. SSL_set_renegotiate_mode(
  2082. ssl, reject ? ssl_renegotiate_never : ssl_renegotiate_freely);
  2083. }
  2084. int SSL_get_rc4_state(const SSL *ssl, const RC4_KEY **read_key,
  2085. const RC4_KEY **write_key) {
  2086. if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
  2087. return 0;
  2088. }
  2089. return EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_read_ctx->ctx, read_key) &&
  2090. EVP_AEAD_CTX_get_rc4_state(&ssl->s3->aead_write_ctx->ctx, write_key);
  2091. }
  2092. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2093. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2094. if (ssl->s3->aead_read_ctx == NULL || ssl->s3->aead_write_ctx == NULL) {
  2095. return 0;
  2096. }
  2097. size_t write_iv_len;
  2098. if (!EVP_AEAD_CTX_get_iv(&ssl->s3->aead_read_ctx->ctx, out_read_iv,
  2099. out_iv_len) ||
  2100. !EVP_AEAD_CTX_get_iv(&ssl->s3->aead_write_ctx->ctx, out_write_iv,
  2101. &write_iv_len) ||
  2102. *out_iv_len != write_iv_len) {
  2103. return 0;
  2104. }
  2105. return 1;
  2106. }
  2107. static uint64_t be_to_u64(const uint8_t in[8]) {
  2108. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2109. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2110. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2111. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2112. }
  2113. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2114. /* TODO(davidben): Internally represent sequence numbers as uint64_t. */
  2115. if (SSL_IS_DTLS(ssl)) {
  2116. /* max_seq_num already includes the epoch. */
  2117. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2118. return ssl->d1->bitmap.max_seq_num;
  2119. }
  2120. return be_to_u64(ssl->s3->read_sequence);
  2121. }
  2122. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2123. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2124. if (SSL_IS_DTLS(ssl)) {
  2125. assert((ret >> 48) == 0);
  2126. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2127. }
  2128. return ret;
  2129. }
  2130. uint8_t SSL_get_server_key_exchange_hash(const SSL *ssl) {
  2131. return ssl->s3->tmp.server_key_exchange_hash;
  2132. }
  2133. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2134. if (max_out == 0) {
  2135. return sizeof(ssl->s3->client_random);
  2136. }
  2137. if (max_out > sizeof(ssl->s3->client_random)) {
  2138. max_out = sizeof(ssl->s3->client_random);
  2139. }
  2140. memcpy(out, ssl->s3->client_random, max_out);
  2141. return max_out;
  2142. }
  2143. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2144. if (max_out == 0) {
  2145. return sizeof(ssl->s3->server_random);
  2146. }
  2147. if (max_out > sizeof(ssl->s3->server_random)) {
  2148. max_out = sizeof(ssl->s3->server_random);
  2149. }
  2150. memcpy(out, ssl->s3->server_random, max_out);
  2151. return max_out;
  2152. }
  2153. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2154. if (!SSL_in_init(ssl)) {
  2155. return NULL;
  2156. }
  2157. return ssl->s3->tmp.new_cipher;
  2158. }
  2159. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2160. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2161. }
  2162. int SSL_clear(SSL *ssl) {
  2163. if (ssl->method == NULL) {
  2164. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_METHOD_SPECIFIED);
  2165. return 0;
  2166. }
  2167. if (ssl_clear_bad_session(ssl)) {
  2168. SSL_SESSION_free(ssl->session);
  2169. ssl->session = NULL;
  2170. }
  2171. ssl->hit = 0;
  2172. ssl->shutdown = 0;
  2173. /* SSL_clear may be called before or after the |ssl| is initialized in either
  2174. * accept or connect state. In the latter case, SSL_clear should preserve the
  2175. * half and reset |ssl->state| accordingly. */
  2176. if (ssl->handshake_func != NULL) {
  2177. if (ssl->server) {
  2178. SSL_set_accept_state(ssl);
  2179. } else {
  2180. SSL_set_connect_state(ssl);
  2181. }
  2182. } else {
  2183. assert(ssl->state == 0);
  2184. }
  2185. /* TODO(davidben): Some state on |ssl| is reset both in |SSL_new| and
  2186. * |SSL_clear| because it is per-connection state rather than configuration
  2187. * state. Per-connection state should be on |ssl->s3| and |ssl->d1| so it is
  2188. * naturally reset at the right points between |SSL_new|, |SSL_clear|, and
  2189. * |ssl3_new|. */
  2190. ssl->rwstate = SSL_NOTHING;
  2191. BUF_MEM_free(ssl->init_buf);
  2192. ssl->init_buf = NULL;
  2193. /* The ssl->d1->mtu is simultaneously configuration (preserved across
  2194. * clear) and connection-specific state (gets reset).
  2195. *
  2196. * TODO(davidben): Avoid this. */
  2197. unsigned mtu = 0;
  2198. if (ssl->d1 != NULL) {
  2199. mtu = ssl->d1->mtu;
  2200. }
  2201. ssl->method->ssl_free(ssl);
  2202. if (!ssl->method->ssl_new(ssl)) {
  2203. return 0;
  2204. }
  2205. if (SSL_IS_DTLS(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2206. ssl->d1->mtu = mtu;
  2207. }
  2208. ssl->client_version = ssl->version;
  2209. return 1;
  2210. }
  2211. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2212. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2213. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2214. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2215. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2216. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2217. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2218. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2219. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2220. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2221. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2222. void ERR_load_SSL_strings(void) {}
  2223. void SSL_load_error_strings(void) {}