2
0

ssl_lib.cc 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650
  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 <stdlib.h>
  143. #include <string.h>
  144. #include <openssl/bytestring.h>
  145. #include <openssl/crypto.h>
  146. #include <openssl/err.h>
  147. #include <openssl/lhash.h>
  148. #include <openssl/mem.h>
  149. #include <openssl/rand.h>
  150. #include "internal.h"
  151. #include "../crypto/internal.h"
  152. #if defined(OPENSSL_WINDOWS)
  153. #include <sys/timeb.h>
  154. #else
  155. #include <sys/socket.h>
  156. #include <sys/time.h>
  157. #endif
  158. namespace bssl {
  159. // |SSL_R_UNKNOWN_PROTOCOL| is no longer emitted, but continue to define it
  160. // to avoid downstream churn.
  161. OPENSSL_DECLARE_ERROR_REASON(SSL, UNKNOWN_PROTOCOL)
  162. // The following errors are no longer emitted, but are used in nginx without
  163. // #ifdefs.
  164. OPENSSL_DECLARE_ERROR_REASON(SSL, BLOCK_CIPHER_PAD_IS_WRONG)
  165. OPENSSL_DECLARE_ERROR_REASON(SSL, NO_CIPHERS_SPECIFIED)
  166. // Some error codes are special. Ensure the make_errors.go script never
  167. // regresses this.
  168. static_assert(SSL_R_TLSV1_ALERT_NO_RENEGOTIATION ==
  169. SSL_AD_NO_RENEGOTIATION + SSL_AD_REASON_OFFSET,
  170. "alert reason code mismatch");
  171. // kMaxHandshakeSize is the maximum size, in bytes, of a handshake message.
  172. static const size_t kMaxHandshakeSize = (1u << 24) - 1;
  173. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl =
  174. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  175. static CRYPTO_EX_DATA_CLASS g_ex_data_class_ssl_ctx =
  176. CRYPTO_EX_DATA_CLASS_INIT_WITH_APP_DATA;
  177. bool CBBFinishArray(CBB *cbb, Array<uint8_t> *out) {
  178. uint8_t *ptr;
  179. size_t len;
  180. if (!CBB_finish(cbb, &ptr, &len)) {
  181. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  182. return false;
  183. }
  184. out->Reset(ptr, len);
  185. return true;
  186. }
  187. void ssl_reset_error_state(SSL *ssl) {
  188. // Functions which use |SSL_get_error| must reset I/O and error state on
  189. // entry.
  190. ssl->s3->rwstate = SSL_NOTHING;
  191. ERR_clear_error();
  192. ERR_clear_system_error();
  193. }
  194. void ssl_set_read_error(SSL* ssl) {
  195. ssl->s3->read_shutdown = ssl_shutdown_error;
  196. ssl->s3->read_error.reset(ERR_save_state());
  197. }
  198. static bool check_read_error(const SSL *ssl) {
  199. if (ssl->s3->read_shutdown == ssl_shutdown_error) {
  200. ERR_restore_state(ssl->s3->read_error.get());
  201. return false;
  202. }
  203. return true;
  204. }
  205. int ssl_can_write(const SSL *ssl) {
  206. return !SSL_in_init(ssl) || ssl->s3->hs->can_early_write;
  207. }
  208. int ssl_can_read(const SSL *ssl) {
  209. return !SSL_in_init(ssl) || ssl->s3->hs->can_early_read;
  210. }
  211. ssl_open_record_t ssl_open_handshake(SSL *ssl, size_t *out_consumed,
  212. uint8_t *out_alert, Span<uint8_t> in) {
  213. *out_consumed = 0;
  214. if (!check_read_error(ssl)) {
  215. *out_alert = 0;
  216. return ssl_open_record_error;
  217. }
  218. auto ret = ssl->method->open_handshake(ssl, out_consumed, out_alert, in);
  219. if (ret == ssl_open_record_error) {
  220. ssl_set_read_error(ssl);
  221. }
  222. return ret;
  223. }
  224. ssl_open_record_t ssl_open_change_cipher_spec(SSL *ssl, size_t *out_consumed,
  225. uint8_t *out_alert,
  226. Span<uint8_t> in) {
  227. *out_consumed = 0;
  228. if (!check_read_error(ssl)) {
  229. *out_alert = 0;
  230. return ssl_open_record_error;
  231. }
  232. auto ret =
  233. ssl->method->open_change_cipher_spec(ssl, out_consumed, out_alert, in);
  234. if (ret == ssl_open_record_error) {
  235. ssl_set_read_error(ssl);
  236. }
  237. return ret;
  238. }
  239. ssl_open_record_t ssl_open_app_data(SSL *ssl, Span<uint8_t> *out,
  240. size_t *out_consumed, uint8_t *out_alert,
  241. Span<uint8_t> in) {
  242. *out_consumed = 0;
  243. if (!check_read_error(ssl)) {
  244. *out_alert = 0;
  245. return ssl_open_record_error;
  246. }
  247. auto ret = ssl->method->open_app_data(ssl, out, out_consumed, out_alert, in);
  248. if (ret == ssl_open_record_error) {
  249. ssl_set_read_error(ssl);
  250. }
  251. return ret;
  252. }
  253. void ssl_cipher_preference_list_free(
  254. struct ssl_cipher_preference_list_st *cipher_list) {
  255. if (cipher_list == NULL) {
  256. return;
  257. }
  258. sk_SSL_CIPHER_free(cipher_list->ciphers);
  259. OPENSSL_free(cipher_list->in_group_flags);
  260. OPENSSL_free(cipher_list);
  261. }
  262. void ssl_update_cache(SSL_HANDSHAKE *hs, int mode) {
  263. SSL *const ssl = hs->ssl;
  264. SSL_CTX *ctx = ssl->session_ctx;
  265. // Never cache sessions with empty session IDs.
  266. if (ssl->s3->established_session->session_id_length == 0 ||
  267. ssl->s3->established_session->not_resumable ||
  268. (ctx->session_cache_mode & mode) != mode) {
  269. return;
  270. }
  271. // Clients never use the internal session cache.
  272. int use_internal_cache = ssl->server && !(ctx->session_cache_mode &
  273. SSL_SESS_CACHE_NO_INTERNAL_STORE);
  274. // A client may see new sessions on abbreviated handshakes if the server
  275. // decides to renew the ticket. Once the handshake is completed, it should be
  276. // inserted into the cache.
  277. if (ssl->s3->established_session.get() != ssl->session ||
  278. (!ssl->server && hs->ticket_expected)) {
  279. if (use_internal_cache) {
  280. SSL_CTX_add_session(ctx, ssl->s3->established_session.get());
  281. }
  282. if (ctx->new_session_cb != NULL) {
  283. SSL_SESSION_up_ref(ssl->s3->established_session.get());
  284. if (!ctx->new_session_cb(ssl, ssl->s3->established_session.get())) {
  285. // |new_session_cb|'s return value signals whether it took ownership.
  286. SSL_SESSION_free(ssl->s3->established_session.get());
  287. }
  288. }
  289. }
  290. if (use_internal_cache &&
  291. !(ctx->session_cache_mode & SSL_SESS_CACHE_NO_AUTO_CLEAR)) {
  292. // Automatically flush the internal session cache every 255 connections.
  293. int flush_cache = 0;
  294. CRYPTO_MUTEX_lock_write(&ctx->lock);
  295. ctx->handshakes_since_cache_flush++;
  296. if (ctx->handshakes_since_cache_flush >= 255) {
  297. flush_cache = 1;
  298. ctx->handshakes_since_cache_flush = 0;
  299. }
  300. CRYPTO_MUTEX_unlock_write(&ctx->lock);
  301. if (flush_cache) {
  302. struct OPENSSL_timeval now;
  303. ssl_get_current_time(ssl, &now);
  304. SSL_CTX_flush_sessions(ctx, now.tv_sec);
  305. }
  306. }
  307. }
  308. static int cbb_add_hex(CBB *cbb, const uint8_t *in, size_t in_len) {
  309. static const char hextable[] = "0123456789abcdef";
  310. uint8_t *out;
  311. if (!CBB_add_space(cbb, &out, in_len * 2)) {
  312. return 0;
  313. }
  314. for (size_t i = 0; i < in_len; i++) {
  315. *(out++) = (uint8_t)hextable[in[i] >> 4];
  316. *(out++) = (uint8_t)hextable[in[i] & 0xf];
  317. }
  318. return 1;
  319. }
  320. int ssl_log_secret(const SSL *ssl, const char *label, const uint8_t *secret,
  321. size_t secret_len) {
  322. if (ssl->ctx->keylog_callback == NULL) {
  323. return 1;
  324. }
  325. ScopedCBB cbb;
  326. uint8_t *out;
  327. size_t out_len;
  328. if (!CBB_init(cbb.get(), strlen(label) + 1 + SSL3_RANDOM_SIZE * 2 + 1 +
  329. secret_len * 2 + 1) ||
  330. !CBB_add_bytes(cbb.get(), (const uint8_t *)label, strlen(label)) ||
  331. !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
  332. !cbb_add_hex(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) ||
  333. !CBB_add_bytes(cbb.get(), (const uint8_t *)" ", 1) ||
  334. !cbb_add_hex(cbb.get(), secret, secret_len) ||
  335. !CBB_add_u8(cbb.get(), 0 /* NUL */) ||
  336. !CBB_finish(cbb.get(), &out, &out_len)) {
  337. return 0;
  338. }
  339. ssl->ctx->keylog_callback(ssl, (const char *)out);
  340. OPENSSL_free(out);
  341. return 1;
  342. }
  343. void ssl_do_info_callback(const SSL *ssl, int type, int value) {
  344. void (*cb)(const SSL *ssl, int type, int value) = NULL;
  345. if (ssl->info_callback != NULL) {
  346. cb = ssl->info_callback;
  347. } else if (ssl->ctx->info_callback != NULL) {
  348. cb = ssl->ctx->info_callback;
  349. }
  350. if (cb != NULL) {
  351. cb(ssl, type, value);
  352. }
  353. }
  354. void ssl_do_msg_callback(SSL *ssl, int is_write, int content_type,
  355. Span<const uint8_t> in) {
  356. if (ssl->msg_callback == NULL) {
  357. return;
  358. }
  359. // |version| is zero when calling for |SSL3_RT_HEADER| and |SSL2_VERSION| for
  360. // a V2ClientHello.
  361. int version;
  362. switch (content_type) {
  363. case 0:
  364. // V2ClientHello
  365. version = SSL2_VERSION;
  366. break;
  367. case SSL3_RT_HEADER:
  368. version = 0;
  369. break;
  370. default:
  371. version = SSL_version(ssl);
  372. }
  373. ssl->msg_callback(is_write, version, content_type, in.data(), in.size(), ssl,
  374. ssl->msg_callback_arg);
  375. }
  376. void ssl_get_current_time(const SSL *ssl, struct OPENSSL_timeval *out_clock) {
  377. // TODO(martinkr): Change callers to |ssl_ctx_get_current_time| and drop the
  378. // |ssl| arg from |current_time_cb| if possible.
  379. ssl_ctx_get_current_time(ssl->ctx, out_clock);
  380. }
  381. void ssl_ctx_get_current_time(const SSL_CTX *ctx,
  382. struct OPENSSL_timeval *out_clock) {
  383. if (ctx->current_time_cb != NULL) {
  384. // TODO(davidben): Update current_time_cb to use OPENSSL_timeval. See
  385. // https://crbug.com/boringssl/155.
  386. struct timeval clock;
  387. ctx->current_time_cb(nullptr /* ssl */, &clock);
  388. if (clock.tv_sec < 0) {
  389. assert(0);
  390. out_clock->tv_sec = 0;
  391. out_clock->tv_usec = 0;
  392. } else {
  393. out_clock->tv_sec = (uint64_t)clock.tv_sec;
  394. out_clock->tv_usec = (uint32_t)clock.tv_usec;
  395. }
  396. return;
  397. }
  398. #if defined(BORINGSSL_UNSAFE_DETERMINISTIC_MODE)
  399. out_clock->tv_sec = 1234;
  400. out_clock->tv_usec = 1234;
  401. #elif defined(OPENSSL_WINDOWS)
  402. struct _timeb time;
  403. _ftime(&time);
  404. if (time.time < 0) {
  405. assert(0);
  406. out_clock->tv_sec = 0;
  407. out_clock->tv_usec = 0;
  408. } else {
  409. out_clock->tv_sec = time.time;
  410. out_clock->tv_usec = time.millitm * 1000;
  411. }
  412. #else
  413. struct timeval clock;
  414. gettimeofday(&clock, NULL);
  415. if (clock.tv_sec < 0) {
  416. assert(0);
  417. out_clock->tv_sec = 0;
  418. out_clock->tv_usec = 0;
  419. } else {
  420. out_clock->tv_sec = (uint64_t)clock.tv_sec;
  421. out_clock->tv_usec = (uint32_t)clock.tv_usec;
  422. }
  423. #endif
  424. }
  425. } // namespace bssl
  426. using namespace bssl;
  427. int SSL_library_init(void) {
  428. CRYPTO_library_init();
  429. return 1;
  430. }
  431. int OPENSSL_init_ssl(uint64_t opts, const OPENSSL_INIT_SETTINGS *settings) {
  432. CRYPTO_library_init();
  433. return 1;
  434. }
  435. static uint32_t ssl_session_hash(const SSL_SESSION *sess) {
  436. const uint8_t *session_id = sess->session_id;
  437. uint8_t tmp_storage[sizeof(uint32_t)];
  438. if (sess->session_id_length < sizeof(tmp_storage)) {
  439. OPENSSL_memset(tmp_storage, 0, sizeof(tmp_storage));
  440. OPENSSL_memcpy(tmp_storage, sess->session_id, sess->session_id_length);
  441. session_id = tmp_storage;
  442. }
  443. uint32_t hash =
  444. ((uint32_t)session_id[0]) |
  445. ((uint32_t)session_id[1] << 8) |
  446. ((uint32_t)session_id[2] << 16) |
  447. ((uint32_t)session_id[3] << 24);
  448. return hash;
  449. }
  450. // NB: If this function (or indeed the hash function which uses a sort of
  451. // coarser function than this one) is changed, ensure
  452. // SSL_CTX_has_matching_session_id() is checked accordingly. It relies on being
  453. // able to construct an SSL_SESSION that will collide with any existing session
  454. // with a matching session ID.
  455. static int ssl_session_cmp(const SSL_SESSION *a, const SSL_SESSION *b) {
  456. if (a->ssl_version != b->ssl_version) {
  457. return 1;
  458. }
  459. if (a->session_id_length != b->session_id_length) {
  460. return 1;
  461. }
  462. return OPENSSL_memcmp(a->session_id, b->session_id, a->session_id_length);
  463. }
  464. SSL_CTX *SSL_CTX_new(const SSL_METHOD *method) {
  465. SSL_CTX *ret = NULL;
  466. if (method == NULL) {
  467. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_METHOD_PASSED);
  468. return NULL;
  469. }
  470. ret = (SSL_CTX *)OPENSSL_malloc(sizeof(SSL_CTX));
  471. if (ret == NULL) {
  472. goto err;
  473. }
  474. OPENSSL_memset(ret, 0, sizeof(SSL_CTX));
  475. ret->method = method->method;
  476. ret->x509_method = method->x509_method;
  477. CRYPTO_MUTEX_init(&ret->lock);
  478. ret->session_cache_mode = SSL_SESS_CACHE_SERVER;
  479. ret->session_cache_size = SSL_SESSION_CACHE_MAX_SIZE_DEFAULT;
  480. ret->session_timeout = SSL_DEFAULT_SESSION_TIMEOUT;
  481. ret->session_psk_dhe_timeout = SSL_DEFAULT_SESSION_PSK_DHE_TIMEOUT;
  482. ret->references = 1;
  483. ret->max_cert_list = SSL_MAX_CERT_LIST_DEFAULT;
  484. ret->verify_mode = SSL_VERIFY_NONE;
  485. ret->cert = ssl_cert_new(method->x509_method);
  486. if (ret->cert == NULL) {
  487. goto err;
  488. }
  489. ret->sessions = lh_SSL_SESSION_new(ssl_session_hash, ssl_session_cmp);
  490. if (ret->sessions == NULL) {
  491. goto err;
  492. }
  493. if (!ret->x509_method->ssl_ctx_new(ret)) {
  494. goto err;
  495. }
  496. if (!SSL_CTX_set_strict_cipher_list(ret, SSL_DEFAULT_CIPHER_LIST)) {
  497. goto err2;
  498. }
  499. ret->client_CA = sk_CRYPTO_BUFFER_new_null();
  500. if (ret->client_CA == NULL) {
  501. goto err;
  502. }
  503. CRYPTO_new_ex_data(&ret->ex_data);
  504. ret->max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  505. // Disable the auto-chaining feature by default. Once this has stuck without
  506. // problems, the feature will be removed entirely.
  507. ret->mode = SSL_MODE_NO_AUTO_CHAIN;
  508. // Lock the SSL_CTX to the specified version, for compatibility with legacy
  509. // uses of SSL_METHOD, but we do not set the minimum version for
  510. // |SSLv3_method|.
  511. if (!SSL_CTX_set_max_proto_version(ret, method->version) ||
  512. !SSL_CTX_set_min_proto_version(ret, method->version == SSL3_VERSION
  513. ? 0 // default
  514. : method->version)) {
  515. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  516. goto err2;
  517. }
  518. return ret;
  519. err:
  520. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  521. err2:
  522. SSL_CTX_free(ret);
  523. return NULL;
  524. }
  525. int SSL_CTX_up_ref(SSL_CTX *ctx) {
  526. CRYPTO_refcount_inc(&ctx->references);
  527. return 1;
  528. }
  529. void SSL_CTX_free(SSL_CTX *ctx) {
  530. if (ctx == NULL ||
  531. !CRYPTO_refcount_dec_and_test_zero(&ctx->references)) {
  532. return;
  533. }
  534. // Free internal session cache. However: the remove_cb() may reference the
  535. // ex_data of SSL_CTX, thus the ex_data store can only be removed after the
  536. // sessions were flushed. As the ex_data handling routines might also touch
  537. // the session cache, the most secure solution seems to be: empty (flush) the
  538. // cache, then free ex_data, then finally free the cache. (See ticket
  539. // [openssl.org #212].)
  540. SSL_CTX_flush_sessions(ctx, 0);
  541. CRYPTO_free_ex_data(&g_ex_data_class_ssl_ctx, ctx, &ctx->ex_data);
  542. CRYPTO_MUTEX_cleanup(&ctx->lock);
  543. lh_SSL_SESSION_free(ctx->sessions);
  544. ssl_cipher_preference_list_free(ctx->cipher_list);
  545. ssl_cert_free(ctx->cert);
  546. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->client_custom_extensions,
  547. SSL_CUSTOM_EXTENSION_free);
  548. sk_SSL_CUSTOM_EXTENSION_pop_free(ctx->server_custom_extensions,
  549. SSL_CUSTOM_EXTENSION_free);
  550. sk_CRYPTO_BUFFER_pop_free(ctx->client_CA, CRYPTO_BUFFER_free);
  551. ctx->x509_method->ssl_ctx_free(ctx);
  552. sk_SRTP_PROTECTION_PROFILE_free(ctx->srtp_profiles);
  553. OPENSSL_free(ctx->psk_identity_hint);
  554. OPENSSL_free(ctx->supported_group_list);
  555. OPENSSL_free(ctx->alpn_client_proto_list);
  556. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  557. OPENSSL_free(ctx->verify_sigalgs);
  558. OPENSSL_free(ctx->tlsext_ticket_key_current);
  559. OPENSSL_free(ctx->tlsext_ticket_key_prev);
  560. OPENSSL_free(ctx);
  561. }
  562. SSL *SSL_new(SSL_CTX *ctx) {
  563. if (ctx == NULL) {
  564. OPENSSL_PUT_ERROR(SSL, SSL_R_NULL_SSL_CTX);
  565. return NULL;
  566. }
  567. if (ctx->method == NULL) {
  568. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_CTX_HAS_NO_DEFAULT_SSL_VERSION);
  569. return NULL;
  570. }
  571. SSL *ssl = (SSL *)OPENSSL_malloc(sizeof(SSL));
  572. if (ssl == NULL) {
  573. goto err;
  574. }
  575. OPENSSL_memset(ssl, 0, sizeof(SSL));
  576. ssl->conf_min_version = ctx->conf_min_version;
  577. ssl->conf_max_version = ctx->conf_max_version;
  578. ssl->tls13_variant = ctx->tls13_variant;
  579. // RFC 6347 states that implementations SHOULD use an initial timer value of
  580. // 1 second.
  581. ssl->initial_timeout_duration_ms = 1000;
  582. ssl->options = ctx->options;
  583. ssl->mode = ctx->mode;
  584. ssl->max_cert_list = ctx->max_cert_list;
  585. ssl->cert = ssl_cert_dup(ctx->cert);
  586. if (ssl->cert == NULL) {
  587. goto err;
  588. }
  589. ssl->msg_callback = ctx->msg_callback;
  590. ssl->msg_callback_arg = ctx->msg_callback_arg;
  591. ssl->verify_mode = ctx->verify_mode;
  592. ssl->verify_callback = ctx->default_verify_callback;
  593. ssl->custom_verify_callback = ctx->custom_verify_callback;
  594. ssl->retain_only_sha256_of_client_certs =
  595. ctx->retain_only_sha256_of_client_certs;
  596. ssl->quiet_shutdown = ctx->quiet_shutdown;
  597. ssl->max_send_fragment = ctx->max_send_fragment;
  598. SSL_CTX_up_ref(ctx);
  599. ssl->ctx = ctx;
  600. SSL_CTX_up_ref(ctx);
  601. ssl->session_ctx = ctx;
  602. if (!ssl->ctx->x509_method->ssl_new(ssl)) {
  603. goto err;
  604. }
  605. if (ctx->supported_group_list) {
  606. ssl->supported_group_list = (uint16_t *)BUF_memdup(
  607. ctx->supported_group_list, ctx->supported_group_list_len * 2);
  608. if (!ssl->supported_group_list) {
  609. goto err;
  610. }
  611. ssl->supported_group_list_len = ctx->supported_group_list_len;
  612. }
  613. if (ctx->alpn_client_proto_list) {
  614. ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(
  615. ctx->alpn_client_proto_list, ctx->alpn_client_proto_list_len);
  616. if (ssl->alpn_client_proto_list == NULL) {
  617. goto err;
  618. }
  619. ssl->alpn_client_proto_list_len = ctx->alpn_client_proto_list_len;
  620. }
  621. ssl->method = ctx->method;
  622. if (!ssl->method->ssl_new(ssl)) {
  623. goto err;
  624. }
  625. CRYPTO_new_ex_data(&ssl->ex_data);
  626. ssl->psk_identity_hint = NULL;
  627. if (ctx->psk_identity_hint) {
  628. ssl->psk_identity_hint = BUF_strdup(ctx->psk_identity_hint);
  629. if (ssl->psk_identity_hint == NULL) {
  630. goto err;
  631. }
  632. }
  633. ssl->psk_client_callback = ctx->psk_client_callback;
  634. ssl->psk_server_callback = ctx->psk_server_callback;
  635. ssl->tlsext_channel_id_enabled = ctx->tlsext_channel_id_enabled;
  636. if (ctx->tlsext_channel_id_private) {
  637. EVP_PKEY_up_ref(ctx->tlsext_channel_id_private);
  638. ssl->tlsext_channel_id_private = ctx->tlsext_channel_id_private;
  639. }
  640. ssl->signed_cert_timestamps_enabled = ctx->signed_cert_timestamps_enabled;
  641. ssl->ocsp_stapling_enabled = ctx->ocsp_stapling_enabled;
  642. return ssl;
  643. err:
  644. SSL_free(ssl);
  645. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  646. return NULL;
  647. }
  648. void SSL_free(SSL *ssl) {
  649. if (ssl == NULL) {
  650. return;
  651. }
  652. if (ssl->ctx != NULL) {
  653. ssl->ctx->x509_method->ssl_free(ssl);
  654. }
  655. CRYPTO_free_ex_data(&g_ex_data_class_ssl, ssl, &ssl->ex_data);
  656. BIO_free_all(ssl->rbio);
  657. BIO_free_all(ssl->wbio);
  658. // add extra stuff
  659. ssl_cipher_preference_list_free(ssl->cipher_list);
  660. SSL_SESSION_free(ssl->session);
  661. ssl_cert_free(ssl->cert);
  662. OPENSSL_free(ssl->tlsext_hostname);
  663. SSL_CTX_free(ssl->session_ctx);
  664. OPENSSL_free(ssl->supported_group_list);
  665. OPENSSL_free(ssl->alpn_client_proto_list);
  666. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  667. OPENSSL_free(ssl->psk_identity_hint);
  668. sk_CRYPTO_BUFFER_pop_free(ssl->client_CA, CRYPTO_BUFFER_free);
  669. sk_SRTP_PROTECTION_PROFILE_free(ssl->srtp_profiles);
  670. if (ssl->method != NULL) {
  671. ssl->method->ssl_free(ssl);
  672. }
  673. SSL_CTX_free(ssl->ctx);
  674. OPENSSL_free(ssl);
  675. }
  676. void SSL_set_connect_state(SSL *ssl) {
  677. ssl->server = false;
  678. ssl->do_handshake = ssl_client_handshake;
  679. }
  680. void SSL_set_accept_state(SSL *ssl) {
  681. ssl->server = true;
  682. ssl->do_handshake = ssl_server_handshake;
  683. }
  684. void SSL_set0_rbio(SSL *ssl, BIO *rbio) {
  685. BIO_free_all(ssl->rbio);
  686. ssl->rbio = rbio;
  687. }
  688. void SSL_set0_wbio(SSL *ssl, BIO *wbio) {
  689. BIO_free_all(ssl->wbio);
  690. ssl->wbio = wbio;
  691. }
  692. void SSL_set_bio(SSL *ssl, BIO *rbio, BIO *wbio) {
  693. // For historical reasons, this function has many different cases in ownership
  694. // handling.
  695. // If nothing has changed, do nothing
  696. if (rbio == SSL_get_rbio(ssl) && wbio == SSL_get_wbio(ssl)) {
  697. return;
  698. }
  699. // If the two arguments are equal, one fewer reference is granted than
  700. // taken.
  701. if (rbio != NULL && rbio == wbio) {
  702. BIO_up_ref(rbio);
  703. }
  704. // If only the wbio is changed, adopt only one reference.
  705. if (rbio == SSL_get_rbio(ssl)) {
  706. SSL_set0_wbio(ssl, wbio);
  707. return;
  708. }
  709. // There is an asymmetry here for historical reasons. If only the rbio is
  710. // changed AND the rbio and wbio were originally different, then we only adopt
  711. // one reference.
  712. if (wbio == SSL_get_wbio(ssl) && SSL_get_rbio(ssl) != SSL_get_wbio(ssl)) {
  713. SSL_set0_rbio(ssl, rbio);
  714. return;
  715. }
  716. // Otherwise, adopt both references.
  717. SSL_set0_rbio(ssl, rbio);
  718. SSL_set0_wbio(ssl, wbio);
  719. }
  720. BIO *SSL_get_rbio(const SSL *ssl) { return ssl->rbio; }
  721. BIO *SSL_get_wbio(const SSL *ssl) { return ssl->wbio; }
  722. int SSL_do_handshake(SSL *ssl) {
  723. ssl_reset_error_state(ssl);
  724. if (ssl->do_handshake == NULL) {
  725. OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_TYPE_NOT_SET);
  726. return -1;
  727. }
  728. if (!SSL_in_init(ssl)) {
  729. return 1;
  730. }
  731. // Run the handshake.
  732. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  733. bool early_return = false;
  734. int ret = ssl_run_handshake(hs, &early_return);
  735. ssl_do_info_callback(
  736. ssl, ssl->server ? SSL_CB_ACCEPT_EXIT : SSL_CB_CONNECT_EXIT, ret);
  737. if (ret <= 0) {
  738. return ret;
  739. }
  740. // Destroy the handshake object if the handshake has completely finished.
  741. if (!early_return) {
  742. ssl->s3->hs.reset();
  743. }
  744. return 1;
  745. }
  746. int SSL_connect(SSL *ssl) {
  747. if (ssl->do_handshake == NULL) {
  748. // Not properly initialized yet
  749. SSL_set_connect_state(ssl);
  750. }
  751. return SSL_do_handshake(ssl);
  752. }
  753. int SSL_accept(SSL *ssl) {
  754. if (ssl->do_handshake == NULL) {
  755. // Not properly initialized yet
  756. SSL_set_accept_state(ssl);
  757. }
  758. return SSL_do_handshake(ssl);
  759. }
  760. static int ssl_do_post_handshake(SSL *ssl, const SSLMessage &msg) {
  761. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  762. return tls13_post_handshake(ssl, msg);
  763. }
  764. // We do not accept renegotiations as a server or SSL 3.0. SSL 3.0 will be
  765. // removed entirely in the future and requires retaining more data for
  766. // renegotiation_info.
  767. if (ssl->server || ssl->version == SSL3_VERSION) {
  768. goto no_renegotiation;
  769. }
  770. if (msg.type != SSL3_MT_HELLO_REQUEST || CBS_len(&msg.body) != 0) {
  771. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  772. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_HELLO_REQUEST);
  773. return 0;
  774. }
  775. switch (ssl->renegotiate_mode) {
  776. case ssl_renegotiate_ignore:
  777. // Ignore the HelloRequest.
  778. return 1;
  779. case ssl_renegotiate_once:
  780. if (ssl->s3->total_renegotiations != 0) {
  781. goto no_renegotiation;
  782. }
  783. break;
  784. case ssl_renegotiate_never:
  785. goto no_renegotiation;
  786. case ssl_renegotiate_freely:
  787. break;
  788. }
  789. // Renegotiation is only supported at quiescent points in the application
  790. // protocol, namely in HTTPS, just before reading the HTTP response. Require
  791. // the record-layer be idle and avoid complexities of sending a handshake
  792. // record while an application_data record is being written.
  793. if (!ssl->s3->write_buffer.empty() ||
  794. ssl->s3->write_shutdown != ssl_shutdown_none) {
  795. goto no_renegotiation;
  796. }
  797. // Begin a new handshake.
  798. if (ssl->s3->hs != nullptr) {
  799. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  800. return 0;
  801. }
  802. ssl->s3->hs = ssl_handshake_new(ssl);
  803. if (ssl->s3->hs == nullptr) {
  804. return 0;
  805. }
  806. ssl->s3->total_renegotiations++;
  807. return 1;
  808. no_renegotiation:
  809. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_RENEGOTIATION);
  810. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_NO_RENEGOTIATION);
  811. return 0;
  812. }
  813. static int ssl_read_impl(SSL *ssl) {
  814. ssl_reset_error_state(ssl);
  815. if (ssl->do_handshake == NULL) {
  816. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  817. return -1;
  818. }
  819. // Replay post-handshake message errors.
  820. if (!check_read_error(ssl)) {
  821. return -1;
  822. }
  823. while (ssl->s3->pending_app_data.empty()) {
  824. // Complete the current handshake, if any. False Start will cause
  825. // |SSL_do_handshake| to return mid-handshake, so this may require multiple
  826. // iterations.
  827. while (!ssl_can_read(ssl)) {
  828. int ret = SSL_do_handshake(ssl);
  829. if (ret < 0) {
  830. return ret;
  831. }
  832. if (ret == 0) {
  833. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  834. return -1;
  835. }
  836. }
  837. // Process any buffered post-handshake messages.
  838. SSLMessage msg;
  839. if (ssl->method->get_message(ssl, &msg)) {
  840. // If we received an interrupt in early read (EndOfEarlyData), loop again
  841. // for the handshake to process it.
  842. if (SSL_in_init(ssl)) {
  843. ssl->s3->hs->can_early_read = false;
  844. continue;
  845. }
  846. // Handle the post-handshake message and try again.
  847. if (!ssl_do_post_handshake(ssl, msg)) {
  848. ssl_set_read_error(ssl);
  849. return -1;
  850. }
  851. ssl->method->next_message(ssl);
  852. continue; // Loop again. We may have begun a new handshake.
  853. }
  854. uint8_t alert = SSL_AD_DECODE_ERROR;
  855. size_t consumed = 0;
  856. auto ret = ssl_open_app_data(ssl, &ssl->s3->pending_app_data, &consumed,
  857. &alert, ssl->s3->read_buffer.span());
  858. bool retry;
  859. int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert);
  860. if (bio_ret <= 0) {
  861. return bio_ret;
  862. }
  863. if (!retry) {
  864. assert(!ssl->s3->pending_app_data.empty());
  865. ssl->s3->key_update_count = 0;
  866. }
  867. }
  868. return 1;
  869. }
  870. int SSL_read(SSL *ssl, void *buf, int num) {
  871. int ret = SSL_peek(ssl, buf, num);
  872. if (ret <= 0) {
  873. return ret;
  874. }
  875. // TODO(davidben): In DTLS, should the rest of the record be discarded? DTLS
  876. // is not a stream. See https://crbug.com/boringssl/65.
  877. ssl->s3->pending_app_data =
  878. ssl->s3->pending_app_data.subspan(static_cast<size_t>(ret));
  879. if (ssl->s3->pending_app_data.empty()) {
  880. ssl->s3->read_buffer.DiscardConsumed();
  881. }
  882. return ret;
  883. }
  884. int SSL_peek(SSL *ssl, void *buf, int num) {
  885. int ret = ssl_read_impl(ssl);
  886. if (ret <= 0) {
  887. return ret;
  888. }
  889. if (num <= 0) {
  890. return num;
  891. }
  892. size_t todo =
  893. std::min(ssl->s3->pending_app_data.size(), static_cast<size_t>(num));
  894. OPENSSL_memcpy(buf, ssl->s3->pending_app_data.data(), todo);
  895. return static_cast<int>(todo);
  896. }
  897. int SSL_write(SSL *ssl, const void *buf, int num) {
  898. ssl_reset_error_state(ssl);
  899. if (ssl->do_handshake == NULL) {
  900. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  901. return -1;
  902. }
  903. if (ssl->s3->write_shutdown != ssl_shutdown_none) {
  904. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  905. return -1;
  906. }
  907. int ret = 0;
  908. bool needs_handshake = false;
  909. do {
  910. // If necessary, complete the handshake implicitly.
  911. if (!ssl_can_write(ssl)) {
  912. ret = SSL_do_handshake(ssl);
  913. if (ret < 0) {
  914. return ret;
  915. }
  916. if (ret == 0) {
  917. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
  918. return -1;
  919. }
  920. }
  921. ret = ssl->method->write_app_data(ssl, &needs_handshake,
  922. (const uint8_t *)buf, num);
  923. } while (needs_handshake);
  924. return ret;
  925. }
  926. int SSL_shutdown(SSL *ssl) {
  927. ssl_reset_error_state(ssl);
  928. if (ssl->do_handshake == NULL) {
  929. OPENSSL_PUT_ERROR(SSL, SSL_R_UNINITIALIZED);
  930. return -1;
  931. }
  932. // If we are in the middle of a handshake, silently succeed. Consumers often
  933. // call this function before |SSL_free|, whether the handshake succeeded or
  934. // not. We assume the caller has already handled failed handshakes.
  935. if (SSL_in_init(ssl)) {
  936. return 1;
  937. }
  938. if (ssl->quiet_shutdown) {
  939. // Do nothing if configured not to send a close_notify.
  940. ssl->s3->write_shutdown = ssl_shutdown_close_notify;
  941. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  942. return 1;
  943. }
  944. // This function completes in two stages. It sends a close_notify and then it
  945. // waits for a close_notify to come in. Perform exactly one action and return
  946. // whether or not it succeeds.
  947. if (ssl->s3->write_shutdown != ssl_shutdown_close_notify) {
  948. // Send a close_notify.
  949. if (ssl_send_alert(ssl, SSL3_AL_WARNING, SSL_AD_CLOSE_NOTIFY) <= 0) {
  950. return -1;
  951. }
  952. } else if (ssl->s3->alert_dispatch) {
  953. // Finish sending the close_notify.
  954. if (ssl->method->dispatch_alert(ssl) <= 0) {
  955. return -1;
  956. }
  957. } else if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
  958. if (SSL_is_dtls(ssl)) {
  959. // Bidirectional shutdown doesn't make sense for an unordered
  960. // transport. DTLS alerts also aren't delivered reliably, so we may even
  961. // time out because the peer never received our close_notify. Report to
  962. // the caller that the channel has fully shut down.
  963. if (ssl->s3->read_shutdown == ssl_shutdown_error) {
  964. ERR_restore_state(ssl->s3->read_error.get());
  965. return -1;
  966. }
  967. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  968. } else {
  969. // Keep discarding data until we see a close_notify.
  970. for (;;) {
  971. ssl->s3->pending_app_data = Span<uint8_t>();
  972. int ret = ssl_read_impl(ssl);
  973. if (ret <= 0) {
  974. break;
  975. }
  976. }
  977. if (ssl->s3->read_shutdown != ssl_shutdown_close_notify) {
  978. return -1;
  979. }
  980. }
  981. }
  982. // Return 0 for unidirectional shutdown and 1 for bidirectional shutdown.
  983. return ssl->s3->read_shutdown == ssl_shutdown_close_notify;
  984. }
  985. int SSL_send_fatal_alert(SSL *ssl, uint8_t alert) {
  986. if (ssl->s3->alert_dispatch) {
  987. if (ssl->s3->send_alert[0] != SSL3_AL_FATAL ||
  988. ssl->s3->send_alert[1] != alert) {
  989. // We are already attempting to write a different alert.
  990. OPENSSL_PUT_ERROR(SSL, SSL_R_PROTOCOL_IS_SHUTDOWN);
  991. return -1;
  992. }
  993. return ssl->method->dispatch_alert(ssl);
  994. }
  995. return ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  996. }
  997. void SSL_CTX_set_early_data_enabled(SSL_CTX *ctx, int enabled) {
  998. ctx->cert->enable_early_data = !!enabled;
  999. }
  1000. void SSL_CTX_set_tls13_variant(SSL_CTX *ctx, enum tls13_variant_t variant) {
  1001. ctx->tls13_variant = variant;
  1002. }
  1003. void SSL_set_tls13_variant(SSL *ssl, enum tls13_variant_t variant) {
  1004. ssl->tls13_variant = variant;
  1005. }
  1006. void SSL_set_early_data_enabled(SSL *ssl, int enabled) {
  1007. ssl->cert->enable_early_data = !!enabled;
  1008. }
  1009. int SSL_in_early_data(const SSL *ssl) {
  1010. if (ssl->s3->hs == NULL) {
  1011. return 0;
  1012. }
  1013. return ssl->s3->hs->in_early_data;
  1014. }
  1015. int SSL_early_data_accepted(const SSL *ssl) {
  1016. return ssl->early_data_accepted;
  1017. }
  1018. void SSL_reset_early_data_reject(SSL *ssl) {
  1019. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  1020. if (hs == NULL ||
  1021. hs->wait != ssl_hs_early_data_rejected) {
  1022. abort();
  1023. }
  1024. hs->wait = ssl_hs_ok;
  1025. hs->in_early_data = false;
  1026. hs->early_session.reset();
  1027. // Discard any unfinished writes from the perspective of |SSL_write|'s
  1028. // retry. The handshake will transparently flush out the pending record
  1029. // (discarded by the server) to keep the framing correct.
  1030. ssl->s3->wpend_pending = false;
  1031. }
  1032. static int bio_retry_reason_to_error(int reason) {
  1033. switch (reason) {
  1034. case BIO_RR_CONNECT:
  1035. return SSL_ERROR_WANT_CONNECT;
  1036. case BIO_RR_ACCEPT:
  1037. return SSL_ERROR_WANT_ACCEPT;
  1038. default:
  1039. return SSL_ERROR_SYSCALL;
  1040. }
  1041. }
  1042. int SSL_get_error(const SSL *ssl, int ret_code) {
  1043. if (ret_code > 0) {
  1044. return SSL_ERROR_NONE;
  1045. }
  1046. // Make things return SSL_ERROR_SYSCALL when doing SSL_do_handshake etc,
  1047. // where we do encode the error
  1048. uint32_t err = ERR_peek_error();
  1049. if (err != 0) {
  1050. if (ERR_GET_LIB(err) == ERR_LIB_SYS) {
  1051. return SSL_ERROR_SYSCALL;
  1052. }
  1053. return SSL_ERROR_SSL;
  1054. }
  1055. if (ret_code == 0) {
  1056. if (ssl->s3->read_shutdown == ssl_shutdown_close_notify) {
  1057. return SSL_ERROR_ZERO_RETURN;
  1058. }
  1059. // An EOF was observed which violates the protocol, and the underlying
  1060. // transport does not participate in the error queue. Bubble up to the
  1061. // caller.
  1062. return SSL_ERROR_SYSCALL;
  1063. }
  1064. switch (ssl->s3->rwstate) {
  1065. case SSL_PENDING_SESSION:
  1066. return SSL_ERROR_PENDING_SESSION;
  1067. case SSL_CERTIFICATE_SELECTION_PENDING:
  1068. return SSL_ERROR_PENDING_CERTIFICATE;
  1069. case SSL_READING: {
  1070. BIO *bio = SSL_get_rbio(ssl);
  1071. if (BIO_should_read(bio)) {
  1072. return SSL_ERROR_WANT_READ;
  1073. }
  1074. if (BIO_should_write(bio)) {
  1075. // TODO(davidben): OpenSSL historically checked for writes on the read
  1076. // BIO. Can this be removed?
  1077. return SSL_ERROR_WANT_WRITE;
  1078. }
  1079. if (BIO_should_io_special(bio)) {
  1080. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  1081. }
  1082. break;
  1083. }
  1084. case SSL_WRITING: {
  1085. BIO *bio = SSL_get_wbio(ssl);
  1086. if (BIO_should_write(bio)) {
  1087. return SSL_ERROR_WANT_WRITE;
  1088. }
  1089. if (BIO_should_read(bio)) {
  1090. // TODO(davidben): OpenSSL historically checked for reads on the write
  1091. // BIO. Can this be removed?
  1092. return SSL_ERROR_WANT_READ;
  1093. }
  1094. if (BIO_should_io_special(bio)) {
  1095. return bio_retry_reason_to_error(BIO_get_retry_reason(bio));
  1096. }
  1097. break;
  1098. }
  1099. case SSL_X509_LOOKUP:
  1100. return SSL_ERROR_WANT_X509_LOOKUP;
  1101. case SSL_CHANNEL_ID_LOOKUP:
  1102. return SSL_ERROR_WANT_CHANNEL_ID_LOOKUP;
  1103. case SSL_PRIVATE_KEY_OPERATION:
  1104. return SSL_ERROR_WANT_PRIVATE_KEY_OPERATION;
  1105. case SSL_PENDING_TICKET:
  1106. return SSL_ERROR_PENDING_TICKET;
  1107. case SSL_EARLY_DATA_REJECTED:
  1108. return SSL_ERROR_EARLY_DATA_REJECTED;
  1109. case SSL_CERTIFICATE_VERIFY:
  1110. return SSL_ERROR_WANT_CERTIFICATE_VERIFY;
  1111. }
  1112. return SSL_ERROR_SYSCALL;
  1113. }
  1114. uint32_t SSL_CTX_set_options(SSL_CTX *ctx, uint32_t options) {
  1115. ctx->options |= options;
  1116. return ctx->options;
  1117. }
  1118. uint32_t SSL_CTX_clear_options(SSL_CTX *ctx, uint32_t options) {
  1119. ctx->options &= ~options;
  1120. return ctx->options;
  1121. }
  1122. uint32_t SSL_CTX_get_options(const SSL_CTX *ctx) { return ctx->options; }
  1123. uint32_t SSL_set_options(SSL *ssl, uint32_t options) {
  1124. ssl->options |= options;
  1125. return ssl->options;
  1126. }
  1127. uint32_t SSL_clear_options(SSL *ssl, uint32_t options) {
  1128. ssl->options &= ~options;
  1129. return ssl->options;
  1130. }
  1131. uint32_t SSL_get_options(const SSL *ssl) { return ssl->options; }
  1132. uint32_t SSL_CTX_set_mode(SSL_CTX *ctx, uint32_t mode) {
  1133. ctx->mode |= mode;
  1134. return ctx->mode;
  1135. }
  1136. uint32_t SSL_CTX_clear_mode(SSL_CTX *ctx, uint32_t mode) {
  1137. ctx->mode &= ~mode;
  1138. return ctx->mode;
  1139. }
  1140. uint32_t SSL_CTX_get_mode(const SSL_CTX *ctx) { return ctx->mode; }
  1141. uint32_t SSL_set_mode(SSL *ssl, uint32_t mode) {
  1142. ssl->mode |= mode;
  1143. return ssl->mode;
  1144. }
  1145. uint32_t SSL_clear_mode(SSL *ssl, uint32_t mode) {
  1146. ssl->mode &= ~mode;
  1147. return ssl->mode;
  1148. }
  1149. uint32_t SSL_get_mode(const SSL *ssl) { return ssl->mode; }
  1150. void SSL_CTX_set0_buffer_pool(SSL_CTX *ctx, CRYPTO_BUFFER_POOL *pool) {
  1151. ctx->pool = pool;
  1152. }
  1153. int SSL_get_tls_unique(const SSL *ssl, uint8_t *out, size_t *out_len,
  1154. size_t max_out) {
  1155. *out_len = 0;
  1156. OPENSSL_memset(out, 0, max_out);
  1157. // tls-unique is not defined for SSL 3.0 or TLS 1.3.
  1158. if (!ssl->s3->initial_handshake_complete ||
  1159. ssl_protocol_version(ssl) < TLS1_VERSION ||
  1160. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1161. return 0;
  1162. }
  1163. // The tls-unique value is the first Finished message in the handshake, which
  1164. // is the client's in a full handshake and the server's for a resumption. See
  1165. // https://tools.ietf.org/html/rfc5929#section-3.1.
  1166. const uint8_t *finished = ssl->s3->previous_client_finished;
  1167. size_t finished_len = ssl->s3->previous_client_finished_len;
  1168. if (ssl->session != NULL) {
  1169. // tls-unique is broken for resumed sessions unless EMS is used.
  1170. if (!ssl->session->extended_master_secret) {
  1171. return 0;
  1172. }
  1173. finished = ssl->s3->previous_server_finished;
  1174. finished_len = ssl->s3->previous_server_finished_len;
  1175. }
  1176. *out_len = finished_len;
  1177. if (finished_len > max_out) {
  1178. *out_len = max_out;
  1179. }
  1180. OPENSSL_memcpy(out, finished, *out_len);
  1181. return 1;
  1182. }
  1183. static int set_session_id_context(CERT *cert, const uint8_t *sid_ctx,
  1184. size_t sid_ctx_len) {
  1185. if (sid_ctx_len > sizeof(cert->sid_ctx)) {
  1186. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_SESSION_ID_CONTEXT_TOO_LONG);
  1187. return 0;
  1188. }
  1189. static_assert(sizeof(cert->sid_ctx) < 256, "sid_ctx too large");
  1190. cert->sid_ctx_length = (uint8_t)sid_ctx_len;
  1191. OPENSSL_memcpy(cert->sid_ctx, sid_ctx, sid_ctx_len);
  1192. return 1;
  1193. }
  1194. int SSL_CTX_set_session_id_context(SSL_CTX *ctx, const uint8_t *sid_ctx,
  1195. size_t sid_ctx_len) {
  1196. return set_session_id_context(ctx->cert, sid_ctx, sid_ctx_len);
  1197. }
  1198. int SSL_set_session_id_context(SSL *ssl, const uint8_t *sid_ctx,
  1199. size_t sid_ctx_len) {
  1200. return set_session_id_context(ssl->cert, sid_ctx, sid_ctx_len);
  1201. }
  1202. const uint8_t *SSL_get0_session_id_context(const SSL *ssl, size_t *out_len) {
  1203. *out_len = ssl->cert->sid_ctx_length;
  1204. return ssl->cert->sid_ctx;
  1205. }
  1206. void SSL_certs_clear(SSL *ssl) { ssl_cert_clear_certs(ssl->cert); }
  1207. int SSL_get_fd(const SSL *ssl) { return SSL_get_rfd(ssl); }
  1208. int SSL_get_rfd(const SSL *ssl) {
  1209. int ret = -1;
  1210. BIO *b = BIO_find_type(SSL_get_rbio(ssl), BIO_TYPE_DESCRIPTOR);
  1211. if (b != NULL) {
  1212. BIO_get_fd(b, &ret);
  1213. }
  1214. return ret;
  1215. }
  1216. int SSL_get_wfd(const SSL *ssl) {
  1217. int ret = -1;
  1218. BIO *b = BIO_find_type(SSL_get_wbio(ssl), BIO_TYPE_DESCRIPTOR);
  1219. if (b != NULL) {
  1220. BIO_get_fd(b, &ret);
  1221. }
  1222. return ret;
  1223. }
  1224. int SSL_set_fd(SSL *ssl, int fd) {
  1225. BIO *bio = BIO_new(BIO_s_socket());
  1226. if (bio == NULL) {
  1227. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1228. return 0;
  1229. }
  1230. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1231. SSL_set_bio(ssl, bio, bio);
  1232. return 1;
  1233. }
  1234. int SSL_set_wfd(SSL *ssl, int fd) {
  1235. BIO *rbio = SSL_get_rbio(ssl);
  1236. if (rbio == NULL || BIO_method_type(rbio) != BIO_TYPE_SOCKET ||
  1237. BIO_get_fd(rbio, NULL) != fd) {
  1238. BIO *bio = BIO_new(BIO_s_socket());
  1239. if (bio == NULL) {
  1240. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1241. return 0;
  1242. }
  1243. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1244. SSL_set0_wbio(ssl, bio);
  1245. } else {
  1246. // Copy the rbio over to the wbio.
  1247. BIO_up_ref(rbio);
  1248. SSL_set0_wbio(ssl, rbio);
  1249. }
  1250. return 1;
  1251. }
  1252. int SSL_set_rfd(SSL *ssl, int fd) {
  1253. BIO *wbio = SSL_get_wbio(ssl);
  1254. if (wbio == NULL || BIO_method_type(wbio) != BIO_TYPE_SOCKET ||
  1255. BIO_get_fd(wbio, NULL) != fd) {
  1256. BIO *bio = BIO_new(BIO_s_socket());
  1257. if (bio == NULL) {
  1258. OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB);
  1259. return 0;
  1260. }
  1261. BIO_set_fd(bio, fd, BIO_NOCLOSE);
  1262. SSL_set0_rbio(ssl, bio);
  1263. } else {
  1264. // Copy the wbio over to the rbio.
  1265. BIO_up_ref(wbio);
  1266. SSL_set0_rbio(ssl, wbio);
  1267. }
  1268. return 1;
  1269. }
  1270. static size_t copy_finished(void *out, size_t out_len, const uint8_t *in,
  1271. size_t in_len) {
  1272. if (out_len > in_len) {
  1273. out_len = in_len;
  1274. }
  1275. OPENSSL_memcpy(out, in, out_len);
  1276. return in_len;
  1277. }
  1278. size_t SSL_get_finished(const SSL *ssl, void *buf, size_t count) {
  1279. if (!ssl->s3->initial_handshake_complete ||
  1280. ssl_protocol_version(ssl) < TLS1_VERSION ||
  1281. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1282. return 0;
  1283. }
  1284. if (ssl->server) {
  1285. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1286. ssl->s3->previous_server_finished_len);
  1287. }
  1288. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1289. ssl->s3->previous_client_finished_len);
  1290. }
  1291. size_t SSL_get_peer_finished(const SSL *ssl, void *buf, size_t count) {
  1292. if (!ssl->s3->initial_handshake_complete ||
  1293. ssl_protocol_version(ssl) < TLS1_VERSION ||
  1294. ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1295. return 0;
  1296. }
  1297. if (ssl->server) {
  1298. return copy_finished(buf, count, ssl->s3->previous_client_finished,
  1299. ssl->s3->previous_client_finished_len);
  1300. }
  1301. return copy_finished(buf, count, ssl->s3->previous_server_finished,
  1302. ssl->s3->previous_server_finished_len);
  1303. }
  1304. int SSL_get_verify_mode(const SSL *ssl) { return ssl->verify_mode; }
  1305. int SSL_get_extms_support(const SSL *ssl) {
  1306. // TLS 1.3 does not require extended master secret and always reports as
  1307. // supporting it.
  1308. if (!ssl->s3->have_version) {
  1309. return 0;
  1310. }
  1311. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  1312. return 1;
  1313. }
  1314. // If the initial handshake completed, query the established session.
  1315. if (ssl->s3->established_session != NULL) {
  1316. return ssl->s3->established_session->extended_master_secret;
  1317. }
  1318. // Otherwise, query the in-progress handshake.
  1319. if (ssl->s3->hs != NULL) {
  1320. return ssl->s3->hs->extended_master_secret;
  1321. }
  1322. assert(0);
  1323. return 0;
  1324. }
  1325. int SSL_CTX_get_read_ahead(const SSL_CTX *ctx) { return 0; }
  1326. int SSL_get_read_ahead(const SSL *ssl) { return 0; }
  1327. void SSL_CTX_set_read_ahead(SSL_CTX *ctx, int yes) { }
  1328. void SSL_set_read_ahead(SSL *ssl, int yes) { }
  1329. int SSL_pending(const SSL *ssl) {
  1330. return static_cast<int>(ssl->s3->pending_app_data.size());
  1331. }
  1332. // Fix this so it checks all the valid key/cert options
  1333. int SSL_CTX_check_private_key(const SSL_CTX *ctx) {
  1334. return ssl_cert_check_private_key(ctx->cert, ctx->cert->privatekey);
  1335. }
  1336. // Fix this function so that it takes an optional type parameter
  1337. int SSL_check_private_key(const SSL *ssl) {
  1338. return ssl_cert_check_private_key(ssl->cert, ssl->cert->privatekey);
  1339. }
  1340. long SSL_get_default_timeout(const SSL *ssl) {
  1341. return SSL_DEFAULT_SESSION_TIMEOUT;
  1342. }
  1343. int SSL_renegotiate(SSL *ssl) {
  1344. // Caller-initiated renegotiation is not supported.
  1345. OPENSSL_PUT_ERROR(SSL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
  1346. return 0;
  1347. }
  1348. int SSL_renegotiate_pending(SSL *ssl) {
  1349. return SSL_in_init(ssl) && ssl->s3->initial_handshake_complete;
  1350. }
  1351. int SSL_total_renegotiations(const SSL *ssl) {
  1352. return ssl->s3->total_renegotiations;
  1353. }
  1354. size_t SSL_CTX_get_max_cert_list(const SSL_CTX *ctx) {
  1355. return ctx->max_cert_list;
  1356. }
  1357. void SSL_CTX_set_max_cert_list(SSL_CTX *ctx, size_t max_cert_list) {
  1358. if (max_cert_list > kMaxHandshakeSize) {
  1359. max_cert_list = kMaxHandshakeSize;
  1360. }
  1361. ctx->max_cert_list = (uint32_t)max_cert_list;
  1362. }
  1363. size_t SSL_get_max_cert_list(const SSL *ssl) {
  1364. return ssl->max_cert_list;
  1365. }
  1366. void SSL_set_max_cert_list(SSL *ssl, size_t max_cert_list) {
  1367. if (max_cert_list > kMaxHandshakeSize) {
  1368. max_cert_list = kMaxHandshakeSize;
  1369. }
  1370. ssl->max_cert_list = (uint32_t)max_cert_list;
  1371. }
  1372. int SSL_CTX_set_max_send_fragment(SSL_CTX *ctx, size_t max_send_fragment) {
  1373. if (max_send_fragment < 512) {
  1374. max_send_fragment = 512;
  1375. }
  1376. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1377. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1378. }
  1379. ctx->max_send_fragment = (uint16_t)max_send_fragment;
  1380. return 1;
  1381. }
  1382. int SSL_set_max_send_fragment(SSL *ssl, size_t max_send_fragment) {
  1383. if (max_send_fragment < 512) {
  1384. max_send_fragment = 512;
  1385. }
  1386. if (max_send_fragment > SSL3_RT_MAX_PLAIN_LENGTH) {
  1387. max_send_fragment = SSL3_RT_MAX_PLAIN_LENGTH;
  1388. }
  1389. ssl->max_send_fragment = (uint16_t)max_send_fragment;
  1390. return 1;
  1391. }
  1392. int SSL_set_mtu(SSL *ssl, unsigned mtu) {
  1393. if (!SSL_is_dtls(ssl) || mtu < dtls1_min_mtu()) {
  1394. return 0;
  1395. }
  1396. ssl->d1->mtu = mtu;
  1397. return 1;
  1398. }
  1399. int SSL_get_secure_renegotiation_support(const SSL *ssl) {
  1400. if (!ssl->s3->have_version) {
  1401. return 0;
  1402. }
  1403. return ssl_protocol_version(ssl) >= TLS1_3_VERSION ||
  1404. ssl->s3->send_connection_binding;
  1405. }
  1406. size_t SSL_CTX_sess_number(const SSL_CTX *ctx) {
  1407. MutexReadLock lock(const_cast<CRYPTO_MUTEX *>(&ctx->lock));
  1408. return lh_SSL_SESSION_num_items(ctx->sessions);
  1409. }
  1410. unsigned long SSL_CTX_sess_set_cache_size(SSL_CTX *ctx, unsigned long size) {
  1411. unsigned long ret = ctx->session_cache_size;
  1412. ctx->session_cache_size = size;
  1413. return ret;
  1414. }
  1415. unsigned long SSL_CTX_sess_get_cache_size(const SSL_CTX *ctx) {
  1416. return ctx->session_cache_size;
  1417. }
  1418. int SSL_CTX_set_session_cache_mode(SSL_CTX *ctx, int mode) {
  1419. int ret = ctx->session_cache_mode;
  1420. ctx->session_cache_mode = mode;
  1421. return ret;
  1422. }
  1423. int SSL_CTX_get_session_cache_mode(const SSL_CTX *ctx) {
  1424. return ctx->session_cache_mode;
  1425. }
  1426. int SSL_CTX_get_tlsext_ticket_keys(SSL_CTX *ctx, void *out, size_t len) {
  1427. if (out == NULL) {
  1428. return 48;
  1429. }
  1430. if (len != 48) {
  1431. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1432. return 0;
  1433. }
  1434. // The default ticket keys are initialized lazily. Trigger a key
  1435. // rotation to initialize them.
  1436. if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
  1437. return 0;
  1438. }
  1439. uint8_t *out_bytes = reinterpret_cast<uint8_t *>(out);
  1440. MutexReadLock lock(&ctx->lock);
  1441. OPENSSL_memcpy(out_bytes, ctx->tlsext_ticket_key_current->name, 16);
  1442. OPENSSL_memcpy(out_bytes + 16, ctx->tlsext_ticket_key_current->hmac_key, 16);
  1443. OPENSSL_memcpy(out_bytes + 32, ctx->tlsext_ticket_key_current->aes_key, 16);
  1444. return 1;
  1445. }
  1446. int SSL_CTX_set_tlsext_ticket_keys(SSL_CTX *ctx, const void *in, size_t len) {
  1447. if (in == NULL) {
  1448. return 48;
  1449. }
  1450. if (len != 48) {
  1451. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_TICKET_KEYS_LENGTH);
  1452. return 0;
  1453. }
  1454. if (!ctx->tlsext_ticket_key_current) {
  1455. ctx->tlsext_ticket_key_current =
  1456. (tlsext_ticket_key *)OPENSSL_malloc(sizeof(tlsext_ticket_key));
  1457. if (!ctx->tlsext_ticket_key_current) {
  1458. return 0;
  1459. }
  1460. }
  1461. OPENSSL_memset(ctx->tlsext_ticket_key_current, 0, sizeof(tlsext_ticket_key));
  1462. const uint8_t *in_bytes = reinterpret_cast<const uint8_t *>(in);
  1463. OPENSSL_memcpy(ctx->tlsext_ticket_key_current->name, in_bytes, 16);
  1464. OPENSSL_memcpy(ctx->tlsext_ticket_key_current->hmac_key, in_bytes + 16, 16);
  1465. OPENSSL_memcpy(ctx->tlsext_ticket_key_current->aes_key, in_bytes + 32, 16);
  1466. OPENSSL_free(ctx->tlsext_ticket_key_prev);
  1467. ctx->tlsext_ticket_key_prev = nullptr;
  1468. // Disable automatic key rotation.
  1469. ctx->tlsext_ticket_key_current->next_rotation_tv_sec = 0;
  1470. return 1;
  1471. }
  1472. int SSL_CTX_set_tlsext_ticket_key_cb(
  1473. SSL_CTX *ctx, int (*callback)(SSL *ssl, uint8_t *key_name, uint8_t *iv,
  1474. EVP_CIPHER_CTX *ctx, HMAC_CTX *hmac_ctx,
  1475. int encrypt)) {
  1476. ctx->tlsext_ticket_key_cb = callback;
  1477. return 1;
  1478. }
  1479. int SSL_CTX_set1_curves(SSL_CTX *ctx, const int *curves, size_t curves_len) {
  1480. return tls1_set_curves(&ctx->supported_group_list,
  1481. &ctx->supported_group_list_len, curves,
  1482. curves_len);
  1483. }
  1484. int SSL_set1_curves(SSL *ssl, const int *curves, size_t curves_len) {
  1485. return tls1_set_curves(&ssl->supported_group_list,
  1486. &ssl->supported_group_list_len, curves,
  1487. curves_len);
  1488. }
  1489. int SSL_CTX_set1_curves_list(SSL_CTX *ctx, const char *curves) {
  1490. return tls1_set_curves_list(&ctx->supported_group_list,
  1491. &ctx->supported_group_list_len, curves);
  1492. }
  1493. int SSL_set1_curves_list(SSL *ssl, const char *curves) {
  1494. return tls1_set_curves_list(&ssl->supported_group_list,
  1495. &ssl->supported_group_list_len, curves);
  1496. }
  1497. uint16_t SSL_get_curve_id(const SSL *ssl) {
  1498. // TODO(davidben): This checks the wrong session if there is a renegotiation
  1499. // in progress.
  1500. SSL_SESSION *session = SSL_get_session(ssl);
  1501. if (session == NULL) {
  1502. return 0;
  1503. }
  1504. return session->group_id;
  1505. }
  1506. int SSL_CTX_set_tmp_dh(SSL_CTX *ctx, const DH *dh) {
  1507. return 1;
  1508. }
  1509. int SSL_set_tmp_dh(SSL *ssl, const DH *dh) {
  1510. return 1;
  1511. }
  1512. STACK_OF(SSL_CIPHER) *SSL_CTX_get_ciphers(const SSL_CTX *ctx) {
  1513. return ctx->cipher_list->ciphers;
  1514. }
  1515. int SSL_CTX_cipher_in_group(const SSL_CTX *ctx, size_t i) {
  1516. if (i >= sk_SSL_CIPHER_num(ctx->cipher_list->ciphers)) {
  1517. return 0;
  1518. }
  1519. return ctx->cipher_list->in_group_flags[i];
  1520. }
  1521. STACK_OF(SSL_CIPHER) *SSL_get_ciphers(const SSL *ssl) {
  1522. if (ssl == NULL) {
  1523. return NULL;
  1524. }
  1525. const struct ssl_cipher_preference_list_st *prefs =
  1526. ssl_get_cipher_preferences(ssl);
  1527. if (prefs == NULL) {
  1528. return NULL;
  1529. }
  1530. return prefs->ciphers;
  1531. }
  1532. const char *SSL_get_cipher_list(const SSL *ssl, int n) {
  1533. if (ssl == NULL) {
  1534. return NULL;
  1535. }
  1536. STACK_OF(SSL_CIPHER) *sk = SSL_get_ciphers(ssl);
  1537. if (sk == NULL || n < 0 || (size_t)n >= sk_SSL_CIPHER_num(sk)) {
  1538. return NULL;
  1539. }
  1540. const SSL_CIPHER *c = sk_SSL_CIPHER_value(sk, n);
  1541. if (c == NULL) {
  1542. return NULL;
  1543. }
  1544. return c->name;
  1545. }
  1546. int SSL_CTX_set_cipher_list(SSL_CTX *ctx, const char *str) {
  1547. return ssl_create_cipher_list(&ctx->cipher_list, str, false /* not strict */);
  1548. }
  1549. int SSL_CTX_set_strict_cipher_list(SSL_CTX *ctx, const char *str) {
  1550. return ssl_create_cipher_list(&ctx->cipher_list, str, true /* strict */);
  1551. }
  1552. int SSL_set_cipher_list(SSL *ssl, const char *str) {
  1553. return ssl_create_cipher_list(&ssl->cipher_list, str, false /* not strict */);
  1554. }
  1555. int SSL_set_strict_cipher_list(SSL *ssl, const char *str) {
  1556. return ssl_create_cipher_list(&ssl->cipher_list, str, true /* strict */);
  1557. }
  1558. const char *SSL_get_servername(const SSL *ssl, const int type) {
  1559. if (type != TLSEXT_NAMETYPE_host_name) {
  1560. return NULL;
  1561. }
  1562. // Historically, |SSL_get_servername| was also the configuration getter
  1563. // corresponding to |SSL_set_tlsext_host_name|.
  1564. if (ssl->tlsext_hostname != NULL) {
  1565. return ssl->tlsext_hostname;
  1566. }
  1567. return ssl->s3->hostname.get();
  1568. }
  1569. int SSL_get_servername_type(const SSL *ssl) {
  1570. if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) == NULL) {
  1571. return -1;
  1572. }
  1573. return TLSEXT_NAMETYPE_host_name;
  1574. }
  1575. void SSL_CTX_set_custom_verify(
  1576. SSL_CTX *ctx, int mode,
  1577. enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
  1578. ctx->verify_mode = mode;
  1579. ctx->custom_verify_callback = callback;
  1580. }
  1581. void SSL_set_custom_verify(
  1582. SSL *ssl, int mode,
  1583. enum ssl_verify_result_t (*callback)(SSL *ssl, uint8_t *out_alert)) {
  1584. ssl->verify_mode = mode;
  1585. ssl->custom_verify_callback = callback;
  1586. }
  1587. void SSL_CTX_enable_signed_cert_timestamps(SSL_CTX *ctx) {
  1588. ctx->signed_cert_timestamps_enabled = true;
  1589. }
  1590. void SSL_enable_signed_cert_timestamps(SSL *ssl) {
  1591. ssl->signed_cert_timestamps_enabled = true;
  1592. }
  1593. void SSL_CTX_enable_ocsp_stapling(SSL_CTX *ctx) {
  1594. ctx->ocsp_stapling_enabled = true;
  1595. }
  1596. void SSL_enable_ocsp_stapling(SSL *ssl) {
  1597. ssl->ocsp_stapling_enabled = true;
  1598. }
  1599. void SSL_get0_signed_cert_timestamp_list(const SSL *ssl, const uint8_t **out,
  1600. size_t *out_len) {
  1601. SSL_SESSION *session = SSL_get_session(ssl);
  1602. if (ssl->server || !session || !session->signed_cert_timestamp_list) {
  1603. *out_len = 0;
  1604. *out = NULL;
  1605. return;
  1606. }
  1607. *out = CRYPTO_BUFFER_data(session->signed_cert_timestamp_list);
  1608. *out_len = CRYPTO_BUFFER_len(session->signed_cert_timestamp_list);
  1609. }
  1610. void SSL_get0_ocsp_response(const SSL *ssl, const uint8_t **out,
  1611. size_t *out_len) {
  1612. SSL_SESSION *session = SSL_get_session(ssl);
  1613. if (ssl->server || !session || !session->ocsp_response) {
  1614. *out_len = 0;
  1615. *out = NULL;
  1616. return;
  1617. }
  1618. *out = CRYPTO_BUFFER_data(session->ocsp_response);
  1619. *out_len = CRYPTO_BUFFER_len(session->ocsp_response);
  1620. }
  1621. int SSL_set_tlsext_host_name(SSL *ssl, const char *name) {
  1622. OPENSSL_free(ssl->tlsext_hostname);
  1623. ssl->tlsext_hostname = NULL;
  1624. if (name == NULL) {
  1625. return 1;
  1626. }
  1627. size_t len = strlen(name);
  1628. if (len == 0 || len > TLSEXT_MAXLEN_host_name) {
  1629. OPENSSL_PUT_ERROR(SSL, SSL_R_SSL3_EXT_INVALID_SERVERNAME);
  1630. return 0;
  1631. }
  1632. ssl->tlsext_hostname = BUF_strdup(name);
  1633. if (ssl->tlsext_hostname == NULL) {
  1634. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  1635. return 0;
  1636. }
  1637. return 1;
  1638. }
  1639. int SSL_CTX_set_tlsext_servername_callback(
  1640. SSL_CTX *ctx, int (*callback)(SSL *ssl, int *out_alert, void *arg)) {
  1641. ctx->tlsext_servername_callback = callback;
  1642. return 1;
  1643. }
  1644. int SSL_CTX_set_tlsext_servername_arg(SSL_CTX *ctx, void *arg) {
  1645. ctx->tlsext_servername_arg = arg;
  1646. return 1;
  1647. }
  1648. int SSL_select_next_proto(uint8_t **out, uint8_t *out_len, const uint8_t *peer,
  1649. unsigned peer_len, const uint8_t *supported,
  1650. unsigned supported_len) {
  1651. const uint8_t *result;
  1652. int status;
  1653. // For each protocol in peer preference order, see if we support it.
  1654. for (unsigned i = 0; i < peer_len;) {
  1655. for (unsigned j = 0; j < supported_len;) {
  1656. if (peer[i] == supported[j] &&
  1657. OPENSSL_memcmp(&peer[i + 1], &supported[j + 1], peer[i]) == 0) {
  1658. // We found a match
  1659. result = &peer[i];
  1660. status = OPENSSL_NPN_NEGOTIATED;
  1661. goto found;
  1662. }
  1663. j += supported[j];
  1664. j++;
  1665. }
  1666. i += peer[i];
  1667. i++;
  1668. }
  1669. // There's no overlap between our protocols and the peer's list.
  1670. result = supported;
  1671. status = OPENSSL_NPN_NO_OVERLAP;
  1672. found:
  1673. *out = (uint8_t *)result + 1;
  1674. *out_len = result[0];
  1675. return status;
  1676. }
  1677. void SSL_get0_next_proto_negotiated(const SSL *ssl, const uint8_t **out_data,
  1678. unsigned *out_len) {
  1679. *out_data = ssl->s3->next_proto_negotiated.data();
  1680. *out_len = ssl->s3->next_proto_negotiated.size();
  1681. }
  1682. void SSL_CTX_set_next_protos_advertised_cb(
  1683. SSL_CTX *ctx,
  1684. int (*cb)(SSL *ssl, const uint8_t **out, unsigned *out_len, void *arg),
  1685. void *arg) {
  1686. ctx->next_protos_advertised_cb = cb;
  1687. ctx->next_protos_advertised_cb_arg = arg;
  1688. }
  1689. void SSL_CTX_set_next_proto_select_cb(
  1690. SSL_CTX *ctx, int (*cb)(SSL *ssl, uint8_t **out, uint8_t *out_len,
  1691. const uint8_t *in, unsigned in_len, void *arg),
  1692. void *arg) {
  1693. ctx->next_proto_select_cb = cb;
  1694. ctx->next_proto_select_cb_arg = arg;
  1695. }
  1696. int SSL_CTX_set_alpn_protos(SSL_CTX *ctx, const uint8_t *protos,
  1697. unsigned protos_len) {
  1698. OPENSSL_free(ctx->alpn_client_proto_list);
  1699. ctx->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
  1700. if (!ctx->alpn_client_proto_list) {
  1701. return 1;
  1702. }
  1703. ctx->alpn_client_proto_list_len = protos_len;
  1704. return 0;
  1705. }
  1706. int SSL_set_alpn_protos(SSL *ssl, const uint8_t *protos, unsigned protos_len) {
  1707. OPENSSL_free(ssl->alpn_client_proto_list);
  1708. ssl->alpn_client_proto_list = (uint8_t *)BUF_memdup(protos, protos_len);
  1709. if (!ssl->alpn_client_proto_list) {
  1710. return 1;
  1711. }
  1712. ssl->alpn_client_proto_list_len = protos_len;
  1713. return 0;
  1714. }
  1715. void SSL_CTX_set_alpn_select_cb(SSL_CTX *ctx,
  1716. int (*cb)(SSL *ssl, const uint8_t **out,
  1717. uint8_t *out_len, const uint8_t *in,
  1718. unsigned in_len, void *arg),
  1719. void *arg) {
  1720. ctx->alpn_select_cb = cb;
  1721. ctx->alpn_select_cb_arg = arg;
  1722. }
  1723. void SSL_get0_alpn_selected(const SSL *ssl, const uint8_t **out_data,
  1724. unsigned *out_len) {
  1725. if (SSL_in_early_data(ssl) && !ssl->server) {
  1726. *out_data = ssl->s3->hs->early_session->early_alpn;
  1727. *out_len = ssl->s3->hs->early_session->early_alpn_len;
  1728. } else {
  1729. *out_data = ssl->s3->alpn_selected.data();
  1730. *out_len = ssl->s3->alpn_selected.size();
  1731. }
  1732. }
  1733. void SSL_CTX_set_allow_unknown_alpn_protos(SSL_CTX *ctx, int enabled) {
  1734. ctx->allow_unknown_alpn_protos = !!enabled;
  1735. }
  1736. void SSL_CTX_set_tls_channel_id_enabled(SSL_CTX *ctx, int enabled) {
  1737. ctx->tlsext_channel_id_enabled = !!enabled;
  1738. }
  1739. int SSL_CTX_enable_tls_channel_id(SSL_CTX *ctx) {
  1740. SSL_CTX_set_tls_channel_id_enabled(ctx, 1);
  1741. return 1;
  1742. }
  1743. void SSL_set_tls_channel_id_enabled(SSL *ssl, int enabled) {
  1744. ssl->tlsext_channel_id_enabled = !!enabled;
  1745. }
  1746. int SSL_enable_tls_channel_id(SSL *ssl) {
  1747. SSL_set_tls_channel_id_enabled(ssl, 1);
  1748. return 1;
  1749. }
  1750. static int is_p256_key(EVP_PKEY *private_key) {
  1751. const EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(private_key);
  1752. return ec_key != NULL &&
  1753. EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key)) ==
  1754. NID_X9_62_prime256v1;
  1755. }
  1756. int SSL_CTX_set1_tls_channel_id(SSL_CTX *ctx, EVP_PKEY *private_key) {
  1757. if (!is_p256_key(private_key)) {
  1758. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1759. return 0;
  1760. }
  1761. EVP_PKEY_free(ctx->tlsext_channel_id_private);
  1762. EVP_PKEY_up_ref(private_key);
  1763. ctx->tlsext_channel_id_private = private_key;
  1764. ctx->tlsext_channel_id_enabled = true;
  1765. return 1;
  1766. }
  1767. int SSL_set1_tls_channel_id(SSL *ssl, EVP_PKEY *private_key) {
  1768. if (!is_p256_key(private_key)) {
  1769. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_NOT_P256);
  1770. return 0;
  1771. }
  1772. EVP_PKEY_free(ssl->tlsext_channel_id_private);
  1773. EVP_PKEY_up_ref(private_key);
  1774. ssl->tlsext_channel_id_private = private_key;
  1775. ssl->tlsext_channel_id_enabled = true;
  1776. return 1;
  1777. }
  1778. size_t SSL_get_tls_channel_id(SSL *ssl, uint8_t *out, size_t max_out) {
  1779. if (!ssl->s3->tlsext_channel_id_valid) {
  1780. return 0;
  1781. }
  1782. OPENSSL_memcpy(out, ssl->s3->tlsext_channel_id,
  1783. (max_out < 64) ? max_out : 64);
  1784. return 64;
  1785. }
  1786. size_t SSL_get0_certificate_types(SSL *ssl, const uint8_t **out_types) {
  1787. if (ssl->server || ssl->s3->hs == NULL) {
  1788. *out_types = NULL;
  1789. return 0;
  1790. }
  1791. *out_types = ssl->s3->hs->certificate_types.data();
  1792. return ssl->s3->hs->certificate_types.size();
  1793. }
  1794. EVP_PKEY *SSL_get_privatekey(const SSL *ssl) {
  1795. if (ssl->cert != NULL) {
  1796. return ssl->cert->privatekey;
  1797. }
  1798. return NULL;
  1799. }
  1800. EVP_PKEY *SSL_CTX_get0_privatekey(const SSL_CTX *ctx) {
  1801. if (ctx->cert != NULL) {
  1802. return ctx->cert->privatekey;
  1803. }
  1804. return NULL;
  1805. }
  1806. const SSL_CIPHER *SSL_get_current_cipher(const SSL *ssl) {
  1807. return ssl->s3->aead_write_ctx->cipher();
  1808. }
  1809. int SSL_session_reused(const SSL *ssl) {
  1810. return ssl->s3->session_reused || SSL_in_early_data(ssl);
  1811. }
  1812. const COMP_METHOD *SSL_get_current_compression(SSL *ssl) { return NULL; }
  1813. const COMP_METHOD *SSL_get_current_expansion(SSL *ssl) { return NULL; }
  1814. int *SSL_get_server_tmp_key(SSL *ssl, EVP_PKEY **out_key) { return 0; }
  1815. void SSL_CTX_set_quiet_shutdown(SSL_CTX *ctx, int mode) {
  1816. ctx->quiet_shutdown = (mode != 0);
  1817. }
  1818. int SSL_CTX_get_quiet_shutdown(const SSL_CTX *ctx) {
  1819. return ctx->quiet_shutdown;
  1820. }
  1821. void SSL_set_quiet_shutdown(SSL *ssl, int mode) {
  1822. ssl->quiet_shutdown = (mode != 0);
  1823. }
  1824. int SSL_get_quiet_shutdown(const SSL *ssl) { return ssl->quiet_shutdown; }
  1825. void SSL_set_shutdown(SSL *ssl, int mode) {
  1826. // It is an error to clear any bits that have already been set. (We can't try
  1827. // to get a second close_notify or send two.)
  1828. assert((SSL_get_shutdown(ssl) & mode) == SSL_get_shutdown(ssl));
  1829. if (mode & SSL_RECEIVED_SHUTDOWN &&
  1830. ssl->s3->read_shutdown == ssl_shutdown_none) {
  1831. ssl->s3->read_shutdown = ssl_shutdown_close_notify;
  1832. }
  1833. if (mode & SSL_SENT_SHUTDOWN &&
  1834. ssl->s3->write_shutdown == ssl_shutdown_none) {
  1835. ssl->s3->write_shutdown = ssl_shutdown_close_notify;
  1836. }
  1837. }
  1838. int SSL_get_shutdown(const SSL *ssl) {
  1839. int ret = 0;
  1840. if (ssl->s3->read_shutdown != ssl_shutdown_none) {
  1841. // Historically, OpenSSL set |SSL_RECEIVED_SHUTDOWN| on both close_notify
  1842. // and fatal alert.
  1843. ret |= SSL_RECEIVED_SHUTDOWN;
  1844. }
  1845. if (ssl->s3->write_shutdown == ssl_shutdown_close_notify) {
  1846. // Historically, OpenSSL set |SSL_SENT_SHUTDOWN| on only close_notify.
  1847. ret |= SSL_SENT_SHUTDOWN;
  1848. }
  1849. return ret;
  1850. }
  1851. SSL_CTX *SSL_get_SSL_CTX(const SSL *ssl) { return ssl->ctx; }
  1852. SSL_CTX *SSL_set_SSL_CTX(SSL *ssl, SSL_CTX *ctx) {
  1853. if (ssl->ctx == ctx) {
  1854. return ssl->ctx;
  1855. }
  1856. // One cannot change the X.509 callbacks during a connection.
  1857. if (ssl->ctx->x509_method != ctx->x509_method) {
  1858. assert(0);
  1859. return NULL;
  1860. }
  1861. if (ctx == NULL) {
  1862. ctx = ssl->session_ctx;
  1863. }
  1864. ssl_cert_free(ssl->cert);
  1865. ssl->cert = ssl_cert_dup(ctx->cert);
  1866. SSL_CTX_up_ref(ctx);
  1867. SSL_CTX_free(ssl->ctx);
  1868. ssl->ctx = ctx;
  1869. return ssl->ctx;
  1870. }
  1871. void SSL_set_info_callback(SSL *ssl,
  1872. void (*cb)(const SSL *ssl, int type, int value)) {
  1873. ssl->info_callback = cb;
  1874. }
  1875. void (*SSL_get_info_callback(const SSL *ssl))(const SSL *ssl, int type,
  1876. int value) {
  1877. return ssl->info_callback;
  1878. }
  1879. int SSL_state(const SSL *ssl) {
  1880. return SSL_in_init(ssl) ? SSL_ST_INIT : SSL_ST_OK;
  1881. }
  1882. void SSL_set_state(SSL *ssl, int state) { }
  1883. char *SSL_get_shared_ciphers(const SSL *ssl, char *buf, int len) {
  1884. if (len <= 0) {
  1885. return NULL;
  1886. }
  1887. buf[0] = '\0';
  1888. return buf;
  1889. }
  1890. int SSL_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1891. CRYPTO_EX_dup *dup_unused, CRYPTO_EX_free *free_func) {
  1892. int index;
  1893. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl, &index, argl, argp,
  1894. free_func)) {
  1895. return -1;
  1896. }
  1897. return index;
  1898. }
  1899. int SSL_set_ex_data(SSL *ssl, int idx, void *data) {
  1900. return CRYPTO_set_ex_data(&ssl->ex_data, idx, data);
  1901. }
  1902. void *SSL_get_ex_data(const SSL *ssl, int idx) {
  1903. return CRYPTO_get_ex_data(&ssl->ex_data, idx);
  1904. }
  1905. int SSL_CTX_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  1906. CRYPTO_EX_dup *dup_unused,
  1907. CRYPTO_EX_free *free_func) {
  1908. int index;
  1909. if (!CRYPTO_get_ex_new_index(&g_ex_data_class_ssl_ctx, &index, argl, argp,
  1910. free_func)) {
  1911. return -1;
  1912. }
  1913. return index;
  1914. }
  1915. int SSL_CTX_set_ex_data(SSL_CTX *ctx, int idx, void *data) {
  1916. return CRYPTO_set_ex_data(&ctx->ex_data, idx, data);
  1917. }
  1918. void *SSL_CTX_get_ex_data(const SSL_CTX *ctx, int idx) {
  1919. return CRYPTO_get_ex_data(&ctx->ex_data, idx);
  1920. }
  1921. int SSL_want(const SSL *ssl) { return ssl->s3->rwstate; }
  1922. void SSL_CTX_set_tmp_rsa_callback(SSL_CTX *ctx,
  1923. RSA *(*cb)(SSL *ssl, int is_export,
  1924. int keylength)) {}
  1925. void SSL_set_tmp_rsa_callback(SSL *ssl, RSA *(*cb)(SSL *ssl, int is_export,
  1926. int keylength)) {}
  1927. void SSL_CTX_set_tmp_dh_callback(SSL_CTX *ctx,
  1928. DH *(*cb)(SSL *ssl, int is_export,
  1929. int keylength)) {}
  1930. void SSL_set_tmp_dh_callback(SSL *ssl, DH *(*cb)(SSL *ssl, int is_export,
  1931. int keylength)) {}
  1932. static int use_psk_identity_hint(char **out, const char *identity_hint) {
  1933. if (identity_hint != NULL && strlen(identity_hint) > PSK_MAX_IDENTITY_LEN) {
  1934. OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG);
  1935. return 0;
  1936. }
  1937. // Clear currently configured hint, if any.
  1938. OPENSSL_free(*out);
  1939. *out = NULL;
  1940. // Treat the empty hint as not supplying one. Plain PSK makes it possible to
  1941. // send either no hint (omit ServerKeyExchange) or an empty hint, while
  1942. // ECDHE_PSK can only spell empty hint. Having different capabilities is odd,
  1943. // so we interpret empty and missing as identical.
  1944. if (identity_hint != NULL && identity_hint[0] != '\0') {
  1945. *out = BUF_strdup(identity_hint);
  1946. if (*out == NULL) {
  1947. return 0;
  1948. }
  1949. }
  1950. return 1;
  1951. }
  1952. int SSL_CTX_use_psk_identity_hint(SSL_CTX *ctx, const char *identity_hint) {
  1953. return use_psk_identity_hint(&ctx->psk_identity_hint, identity_hint);
  1954. }
  1955. int SSL_use_psk_identity_hint(SSL *ssl, const char *identity_hint) {
  1956. return use_psk_identity_hint(&ssl->psk_identity_hint, identity_hint);
  1957. }
  1958. const char *SSL_get_psk_identity_hint(const SSL *ssl) {
  1959. if (ssl == NULL) {
  1960. return NULL;
  1961. }
  1962. return ssl->psk_identity_hint;
  1963. }
  1964. const char *SSL_get_psk_identity(const SSL *ssl) {
  1965. if (ssl == NULL) {
  1966. return NULL;
  1967. }
  1968. SSL_SESSION *session = SSL_get_session(ssl);
  1969. if (session == NULL) {
  1970. return NULL;
  1971. }
  1972. return session->psk_identity;
  1973. }
  1974. void SSL_set_psk_client_callback(
  1975. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1976. unsigned max_identity_len, uint8_t *psk,
  1977. unsigned max_psk_len)) {
  1978. ssl->psk_client_callback = cb;
  1979. }
  1980. void SSL_CTX_set_psk_client_callback(
  1981. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *hint, char *identity,
  1982. unsigned max_identity_len, uint8_t *psk,
  1983. unsigned max_psk_len)) {
  1984. ctx->psk_client_callback = cb;
  1985. }
  1986. void SSL_set_psk_server_callback(
  1987. SSL *ssl, unsigned (*cb)(SSL *ssl, const char *identity, uint8_t *psk,
  1988. unsigned max_psk_len)) {
  1989. ssl->psk_server_callback = cb;
  1990. }
  1991. void SSL_CTX_set_psk_server_callback(
  1992. SSL_CTX *ctx, unsigned (*cb)(SSL *ssl, const char *identity,
  1993. uint8_t *psk, unsigned max_psk_len)) {
  1994. ctx->psk_server_callback = cb;
  1995. }
  1996. void SSL_CTX_set_msg_callback(SSL_CTX *ctx,
  1997. void (*cb)(int write_p, int version,
  1998. int content_type, const void *buf,
  1999. size_t len, SSL *ssl, void *arg)) {
  2000. ctx->msg_callback = cb;
  2001. }
  2002. void SSL_CTX_set_msg_callback_arg(SSL_CTX *ctx, void *arg) {
  2003. ctx->msg_callback_arg = arg;
  2004. }
  2005. void SSL_set_msg_callback(SSL *ssl,
  2006. void (*cb)(int write_p, int version, int content_type,
  2007. const void *buf, size_t len, SSL *ssl,
  2008. void *arg)) {
  2009. ssl->msg_callback = cb;
  2010. }
  2011. void SSL_set_msg_callback_arg(SSL *ssl, void *arg) {
  2012. ssl->msg_callback_arg = arg;
  2013. }
  2014. void SSL_CTX_set_keylog_callback(SSL_CTX *ctx,
  2015. void (*cb)(const SSL *ssl, const char *line)) {
  2016. ctx->keylog_callback = cb;
  2017. }
  2018. void (*SSL_CTX_get_keylog_callback(const SSL_CTX *ctx))(const SSL *ssl,
  2019. const char *line) {
  2020. return ctx->keylog_callback;
  2021. }
  2022. void SSL_CTX_set_current_time_cb(SSL_CTX *ctx,
  2023. void (*cb)(const SSL *ssl,
  2024. struct timeval *out_clock)) {
  2025. ctx->current_time_cb = cb;
  2026. }
  2027. int SSL_is_init_finished(const SSL *ssl) {
  2028. return !SSL_in_init(ssl);
  2029. }
  2030. int SSL_in_init(const SSL *ssl) {
  2031. // This returns false once all the handshake state has been finalized, to
  2032. // allow callbacks and getters based on SSL_in_init to return the correct
  2033. // values.
  2034. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2035. return hs != nullptr && !hs->handshake_finalized;
  2036. }
  2037. int SSL_in_false_start(const SSL *ssl) {
  2038. if (ssl->s3->hs == NULL) {
  2039. return 0;
  2040. }
  2041. return ssl->s3->hs->in_false_start;
  2042. }
  2043. int SSL_cutthrough_complete(const SSL *ssl) {
  2044. return SSL_in_false_start(ssl);
  2045. }
  2046. void SSL_get_structure_sizes(size_t *ssl_size, size_t *ssl_ctx_size,
  2047. size_t *ssl_session_size) {
  2048. *ssl_size = sizeof(SSL);
  2049. *ssl_ctx_size = sizeof(SSL_CTX);
  2050. *ssl_session_size = sizeof(SSL_SESSION);
  2051. }
  2052. int SSL_is_server(const SSL *ssl) { return ssl->server; }
  2053. int SSL_is_dtls(const SSL *ssl) { return ssl->method->is_dtls; }
  2054. void SSL_CTX_set_select_certificate_cb(
  2055. SSL_CTX *ctx,
  2056. enum ssl_select_cert_result_t (*cb)(const SSL_CLIENT_HELLO *)) {
  2057. ctx->select_certificate_cb = cb;
  2058. }
  2059. void SSL_CTX_set_dos_protection_cb(SSL_CTX *ctx,
  2060. int (*cb)(const SSL_CLIENT_HELLO *)) {
  2061. ctx->dos_protection_cb = cb;
  2062. }
  2063. void SSL_set_renegotiate_mode(SSL *ssl, enum ssl_renegotiate_mode_t mode) {
  2064. ssl->renegotiate_mode = mode;
  2065. }
  2066. int SSL_get_ivs(const SSL *ssl, const uint8_t **out_read_iv,
  2067. const uint8_t **out_write_iv, size_t *out_iv_len) {
  2068. size_t write_iv_len;
  2069. if (!ssl->s3->aead_read_ctx->GetIV(out_read_iv, out_iv_len) ||
  2070. !ssl->s3->aead_write_ctx->GetIV(out_write_iv, &write_iv_len) ||
  2071. *out_iv_len != write_iv_len) {
  2072. return 0;
  2073. }
  2074. return 1;
  2075. }
  2076. static uint64_t be_to_u64(const uint8_t in[8]) {
  2077. return (((uint64_t)in[0]) << 56) | (((uint64_t)in[1]) << 48) |
  2078. (((uint64_t)in[2]) << 40) | (((uint64_t)in[3]) << 32) |
  2079. (((uint64_t)in[4]) << 24) | (((uint64_t)in[5]) << 16) |
  2080. (((uint64_t)in[6]) << 8) | ((uint64_t)in[7]);
  2081. }
  2082. uint64_t SSL_get_read_sequence(const SSL *ssl) {
  2083. // TODO(davidben): Internally represent sequence numbers as uint64_t.
  2084. if (SSL_is_dtls(ssl)) {
  2085. // max_seq_num already includes the epoch.
  2086. assert(ssl->d1->r_epoch == (ssl->d1->bitmap.max_seq_num >> 48));
  2087. return ssl->d1->bitmap.max_seq_num;
  2088. }
  2089. return be_to_u64(ssl->s3->read_sequence);
  2090. }
  2091. uint64_t SSL_get_write_sequence(const SSL *ssl) {
  2092. uint64_t ret = be_to_u64(ssl->s3->write_sequence);
  2093. if (SSL_is_dtls(ssl)) {
  2094. assert((ret >> 48) == 0);
  2095. ret |= ((uint64_t)ssl->d1->w_epoch) << 48;
  2096. }
  2097. return ret;
  2098. }
  2099. uint16_t SSL_get_peer_signature_algorithm(const SSL *ssl) {
  2100. // TODO(davidben): This checks the wrong session if there is a renegotiation
  2101. // in progress.
  2102. SSL_SESSION *session = SSL_get_session(ssl);
  2103. if (session == NULL) {
  2104. return 0;
  2105. }
  2106. return session->peer_signature_algorithm;
  2107. }
  2108. size_t SSL_get_client_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2109. if (max_out == 0) {
  2110. return sizeof(ssl->s3->client_random);
  2111. }
  2112. if (max_out > sizeof(ssl->s3->client_random)) {
  2113. max_out = sizeof(ssl->s3->client_random);
  2114. }
  2115. OPENSSL_memcpy(out, ssl->s3->client_random, max_out);
  2116. return max_out;
  2117. }
  2118. size_t SSL_get_server_random(const SSL *ssl, uint8_t *out, size_t max_out) {
  2119. if (max_out == 0) {
  2120. return sizeof(ssl->s3->server_random);
  2121. }
  2122. if (max_out > sizeof(ssl->s3->server_random)) {
  2123. max_out = sizeof(ssl->s3->server_random);
  2124. }
  2125. OPENSSL_memcpy(out, ssl->s3->server_random, max_out);
  2126. return max_out;
  2127. }
  2128. const SSL_CIPHER *SSL_get_pending_cipher(const SSL *ssl) {
  2129. SSL_HANDSHAKE *hs = ssl->s3->hs.get();
  2130. if (hs == NULL) {
  2131. return NULL;
  2132. }
  2133. return hs->new_cipher;
  2134. }
  2135. void SSL_set_retain_only_sha256_of_client_certs(SSL *ssl, int enabled) {
  2136. ssl->retain_only_sha256_of_client_certs = !!enabled;
  2137. }
  2138. void SSL_CTX_set_retain_only_sha256_of_client_certs(SSL_CTX *ctx, int enabled) {
  2139. ctx->retain_only_sha256_of_client_certs = !!enabled;
  2140. }
  2141. void SSL_CTX_set_grease_enabled(SSL_CTX *ctx, int enabled) {
  2142. ctx->grease_enabled = !!enabled;
  2143. }
  2144. int32_t SSL_get_ticket_age_skew(const SSL *ssl) {
  2145. return ssl->s3->ticket_age_skew;
  2146. }
  2147. int SSL_clear(SSL *ssl) {
  2148. // In OpenSSL, reusing a client |SSL| with |SSL_clear| causes the previously
  2149. // established session to be offered the next time around. wpa_supplicant
  2150. // depends on this behavior, so emulate it.
  2151. UniquePtr<SSL_SESSION> session;
  2152. if (!ssl->server && ssl->s3->established_session != NULL) {
  2153. session.reset(ssl->s3->established_session.get());
  2154. SSL_SESSION_up_ref(session.get());
  2155. }
  2156. // The ssl->d1->mtu is simultaneously configuration (preserved across
  2157. // clear) and connection-specific state (gets reset).
  2158. //
  2159. // TODO(davidben): Avoid this.
  2160. unsigned mtu = 0;
  2161. if (ssl->d1 != NULL) {
  2162. mtu = ssl->d1->mtu;
  2163. }
  2164. ssl->method->ssl_free(ssl);
  2165. if (!ssl->method->ssl_new(ssl)) {
  2166. return 0;
  2167. }
  2168. if (SSL_is_dtls(ssl) && (SSL_get_options(ssl) & SSL_OP_NO_QUERY_MTU)) {
  2169. ssl->d1->mtu = mtu;
  2170. }
  2171. if (session != nullptr) {
  2172. SSL_set_session(ssl, session.get());
  2173. }
  2174. return 1;
  2175. }
  2176. int SSL_CTX_sess_connect(const SSL_CTX *ctx) { return 0; }
  2177. int SSL_CTX_sess_connect_good(const SSL_CTX *ctx) { return 0; }
  2178. int SSL_CTX_sess_connect_renegotiate(const SSL_CTX *ctx) { return 0; }
  2179. int SSL_CTX_sess_accept(const SSL_CTX *ctx) { return 0; }
  2180. int SSL_CTX_sess_accept_renegotiate(const SSL_CTX *ctx) { return 0; }
  2181. int SSL_CTX_sess_accept_good(const SSL_CTX *ctx) { return 0; }
  2182. int SSL_CTX_sess_hits(const SSL_CTX *ctx) { return 0; }
  2183. int SSL_CTX_sess_cb_hits(const SSL_CTX *ctx) { return 0; }
  2184. int SSL_CTX_sess_misses(const SSL_CTX *ctx) { return 0; }
  2185. int SSL_CTX_sess_timeouts(const SSL_CTX *ctx) { return 0; }
  2186. int SSL_CTX_sess_cache_full(const SSL_CTX *ctx) { return 0; }
  2187. int SSL_num_renegotiations(const SSL *ssl) {
  2188. return SSL_total_renegotiations(ssl);
  2189. }
  2190. int SSL_CTX_need_tmp_RSA(const SSL_CTX *ctx) { return 0; }
  2191. int SSL_need_tmp_RSA(const SSL *ssl) { return 0; }
  2192. int SSL_CTX_set_tmp_rsa(SSL_CTX *ctx, const RSA *rsa) { return 1; }
  2193. int SSL_set_tmp_rsa(SSL *ssl, const RSA *rsa) { return 1; }
  2194. void ERR_load_SSL_strings(void) {}
  2195. void SSL_load_error_strings(void) {}
  2196. int SSL_cache_hit(SSL *ssl) { return SSL_session_reused(ssl); }
  2197. int SSL_CTX_set_tmp_ecdh(SSL_CTX *ctx, const EC_KEY *ec_key) {
  2198. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2199. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2200. return 0;
  2201. }
  2202. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2203. return SSL_CTX_set1_curves(ctx, &nid, 1);
  2204. }
  2205. int SSL_set_tmp_ecdh(SSL *ssl, const EC_KEY *ec_key) {
  2206. if (ec_key == NULL || EC_KEY_get0_group(ec_key) == NULL) {
  2207. OPENSSL_PUT_ERROR(SSL, ERR_R_PASSED_NULL_PARAMETER);
  2208. return 0;
  2209. }
  2210. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec_key));
  2211. return SSL_set1_curves(ssl, &nid, 1);
  2212. }
  2213. void SSL_CTX_set_ticket_aead_method(SSL_CTX *ctx,
  2214. const SSL_TICKET_AEAD_METHOD *aead_method) {
  2215. ctx->ticket_aead_method = aead_method;
  2216. }