Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

3574 lignes
104 KiB

  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. #include <openssl/ssl.h>
  109. #include <assert.h>
  110. #include <limits.h>
  111. #include <stdlib.h>
  112. #include <string.h>
  113. #include <openssl/bytestring.h>
  114. #include <openssl/digest.h>
  115. #include <openssl/err.h>
  116. #include <openssl/evp.h>
  117. #include <openssl/hmac.h>
  118. #include <openssl/mem.h>
  119. #include <openssl/nid.h>
  120. #include <openssl/rand.h>
  121. #include "internal.h"
  122. #include "../crypto/internal.h"
  123. namespace bssl {
  124. static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs);
  125. static int compare_uint16_t(const void *p1, const void *p2) {
  126. uint16_t u1 = *((const uint16_t *)p1);
  127. uint16_t u2 = *((const uint16_t *)p2);
  128. if (u1 < u2) {
  129. return -1;
  130. } else if (u1 > u2) {
  131. return 1;
  132. } else {
  133. return 0;
  134. }
  135. }
  136. // Per http://tools.ietf.org/html/rfc5246#section-7.4.1.4, there may not be
  137. // more than one extension of the same type in a ClientHello or ServerHello.
  138. // This function does an initial scan over the extensions block to filter those
  139. // out.
  140. static int tls1_check_duplicate_extensions(const CBS *cbs) {
  141. CBS extensions = *cbs;
  142. size_t num_extensions = 0, i = 0;
  143. uint16_t *extension_types = NULL;
  144. int ret = 0;
  145. // First pass: count the extensions.
  146. while (CBS_len(&extensions) > 0) {
  147. uint16_t type;
  148. CBS extension;
  149. if (!CBS_get_u16(&extensions, &type) ||
  150. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  151. goto done;
  152. }
  153. num_extensions++;
  154. }
  155. if (num_extensions == 0) {
  156. return 1;
  157. }
  158. extension_types =
  159. (uint16_t *)OPENSSL_malloc(sizeof(uint16_t) * num_extensions);
  160. if (extension_types == NULL) {
  161. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  162. goto done;
  163. }
  164. // Second pass: gather the extension types.
  165. extensions = *cbs;
  166. for (i = 0; i < num_extensions; i++) {
  167. CBS extension;
  168. if (!CBS_get_u16(&extensions, &extension_types[i]) ||
  169. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  170. // This should not happen.
  171. goto done;
  172. }
  173. }
  174. assert(CBS_len(&extensions) == 0);
  175. // Sort the extensions and make sure there are no duplicates.
  176. qsort(extension_types, num_extensions, sizeof(uint16_t), compare_uint16_t);
  177. for (i = 1; i < num_extensions; i++) {
  178. if (extension_types[i - 1] == extension_types[i]) {
  179. goto done;
  180. }
  181. }
  182. ret = 1;
  183. done:
  184. OPENSSL_free(extension_types);
  185. return ret;
  186. }
  187. int ssl_client_hello_init(SSL *ssl, SSL_CLIENT_HELLO *out,
  188. const SSLMessage &msg) {
  189. OPENSSL_memset(out, 0, sizeof(*out));
  190. out->ssl = ssl;
  191. out->client_hello = CBS_data(&msg.body);
  192. out->client_hello_len = CBS_len(&msg.body);
  193. CBS client_hello, random, session_id;
  194. CBS_init(&client_hello, out->client_hello, out->client_hello_len);
  195. if (!CBS_get_u16(&client_hello, &out->version) ||
  196. !CBS_get_bytes(&client_hello, &random, SSL3_RANDOM_SIZE) ||
  197. !CBS_get_u8_length_prefixed(&client_hello, &session_id) ||
  198. CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  199. return 0;
  200. }
  201. out->random = CBS_data(&random);
  202. out->random_len = CBS_len(&random);
  203. out->session_id = CBS_data(&session_id);
  204. out->session_id_len = CBS_len(&session_id);
  205. // Skip past DTLS cookie
  206. if (SSL_is_dtls(out->ssl)) {
  207. CBS cookie;
  208. if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) ||
  209. CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) {
  210. return 0;
  211. }
  212. }
  213. CBS cipher_suites, compression_methods;
  214. if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) ||
  215. CBS_len(&cipher_suites) < 2 || (CBS_len(&cipher_suites) & 1) != 0 ||
  216. !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) ||
  217. CBS_len(&compression_methods) < 1) {
  218. return 0;
  219. }
  220. out->cipher_suites = CBS_data(&cipher_suites);
  221. out->cipher_suites_len = CBS_len(&cipher_suites);
  222. out->compression_methods = CBS_data(&compression_methods);
  223. out->compression_methods_len = CBS_len(&compression_methods);
  224. // If the ClientHello ends here then it's valid, but doesn't have any
  225. // extensions. (E.g. SSLv3.)
  226. if (CBS_len(&client_hello) == 0) {
  227. out->extensions = NULL;
  228. out->extensions_len = 0;
  229. return 1;
  230. }
  231. // Extract extensions and check it is valid.
  232. CBS extensions;
  233. if (!CBS_get_u16_length_prefixed(&client_hello, &extensions) ||
  234. !tls1_check_duplicate_extensions(&extensions) ||
  235. CBS_len(&client_hello) != 0) {
  236. return 0;
  237. }
  238. out->extensions = CBS_data(&extensions);
  239. out->extensions_len = CBS_len(&extensions);
  240. return 1;
  241. }
  242. int ssl_client_hello_get_extension(const SSL_CLIENT_HELLO *client_hello,
  243. CBS *out, uint16_t extension_type) {
  244. CBS extensions;
  245. CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
  246. while (CBS_len(&extensions) != 0) {
  247. // Decode the next extension.
  248. uint16_t type;
  249. CBS extension;
  250. if (!CBS_get_u16(&extensions, &type) ||
  251. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  252. return 0;
  253. }
  254. if (type == extension_type) {
  255. *out = extension;
  256. return 1;
  257. }
  258. }
  259. return 0;
  260. }
  261. static const uint16_t kDefaultGroups[] = {
  262. SSL_CURVE_X25519,
  263. SSL_CURVE_SECP256R1,
  264. SSL_CURVE_SECP384R1,
  265. };
  266. void tls1_get_grouplist(SSL *ssl, const uint16_t **out_group_ids,
  267. size_t *out_group_ids_len) {
  268. *out_group_ids = ssl->supported_group_list;
  269. *out_group_ids_len = ssl->supported_group_list_len;
  270. if (!*out_group_ids) {
  271. *out_group_ids = kDefaultGroups;
  272. *out_group_ids_len = OPENSSL_ARRAY_SIZE(kDefaultGroups);
  273. }
  274. }
  275. int tls1_get_shared_group(SSL_HANDSHAKE *hs, uint16_t *out_group_id) {
  276. SSL *const ssl = hs->ssl;
  277. assert(ssl->server);
  278. const uint16_t *groups, *pref, *supp;
  279. size_t groups_len, pref_len, supp_len;
  280. tls1_get_grouplist(ssl, &groups, &groups_len);
  281. // Clients are not required to send a supported_groups extension. In this
  282. // case, the server is free to pick any group it likes. See RFC 4492,
  283. // section 4, paragraph 3.
  284. //
  285. // However, in the interests of compatibility, we will skip ECDH if the
  286. // client didn't send an extension because we can't be sure that they'll
  287. // support our favoured group. Thus we do not special-case an emtpy
  288. // |peer_supported_group_list|.
  289. if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  290. pref = groups;
  291. pref_len = groups_len;
  292. supp = hs->peer_supported_group_list;
  293. supp_len = hs->peer_supported_group_list_len;
  294. } else {
  295. pref = hs->peer_supported_group_list;
  296. pref_len = hs->peer_supported_group_list_len;
  297. supp = groups;
  298. supp_len = groups_len;
  299. }
  300. for (size_t i = 0; i < pref_len; i++) {
  301. for (size_t j = 0; j < supp_len; j++) {
  302. if (pref[i] == supp[j]) {
  303. *out_group_id = pref[i];
  304. return 1;
  305. }
  306. }
  307. }
  308. return 0;
  309. }
  310. int tls1_set_curves(uint16_t **out_group_ids, size_t *out_group_ids_len,
  311. const int *curves, size_t ncurves) {
  312. uint16_t *group_ids = (uint16_t *)OPENSSL_malloc(ncurves * sizeof(uint16_t));
  313. if (group_ids == NULL) {
  314. return 0;
  315. }
  316. for (size_t i = 0; i < ncurves; i++) {
  317. if (!ssl_nid_to_group_id(&group_ids[i], curves[i])) {
  318. OPENSSL_free(group_ids);
  319. return 0;
  320. }
  321. }
  322. OPENSSL_free(*out_group_ids);
  323. *out_group_ids = group_ids;
  324. *out_group_ids_len = ncurves;
  325. return 1;
  326. }
  327. int tls1_set_curves_list(uint16_t **out_group_ids, size_t *out_group_ids_len,
  328. const char *curves) {
  329. uint16_t *group_ids = NULL;
  330. size_t ncurves = 0;
  331. const char *col;
  332. const char *ptr = curves;
  333. do {
  334. col = strchr(ptr, ':');
  335. uint16_t group_id;
  336. if (!ssl_name_to_group_id(&group_id, ptr,
  337. col ? (size_t)(col - ptr) : strlen(ptr))) {
  338. goto err;
  339. }
  340. uint16_t *new_group_ids = (uint16_t *)OPENSSL_realloc(
  341. group_ids, (ncurves + 1) * sizeof(uint16_t));
  342. if (new_group_ids == NULL) {
  343. goto err;
  344. }
  345. group_ids = new_group_ids;
  346. group_ids[ncurves] = group_id;
  347. ncurves++;
  348. if (col) {
  349. ptr = col + 1;
  350. }
  351. } while (col);
  352. OPENSSL_free(*out_group_ids);
  353. *out_group_ids = group_ids;
  354. *out_group_ids_len = ncurves;
  355. return 1;
  356. err:
  357. OPENSSL_free(group_ids);
  358. return 0;
  359. }
  360. int tls1_check_group_id(SSL *ssl, uint16_t group_id) {
  361. const uint16_t *groups;
  362. size_t groups_len;
  363. tls1_get_grouplist(ssl, &groups, &groups_len);
  364. for (size_t i = 0; i < groups_len; i++) {
  365. if (groups[i] == group_id) {
  366. return 1;
  367. }
  368. }
  369. return 0;
  370. }
  371. // kVerifySignatureAlgorithms is the default list of accepted signature
  372. // algorithms for verifying.
  373. //
  374. // For now, RSA-PSS signature algorithms are not enabled on Android's system
  375. // BoringSSL. Once the change in Chrome has stuck and the values are finalized,
  376. // restore them.
  377. static const uint16_t kVerifySignatureAlgorithms[] = {
  378. // List our preferred algorithms first.
  379. SSL_SIGN_ED25519,
  380. SSL_SIGN_ECDSA_SECP256R1_SHA256,
  381. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  382. SSL_SIGN_RSA_PSS_SHA256,
  383. #endif
  384. SSL_SIGN_RSA_PKCS1_SHA256,
  385. // Larger hashes are acceptable.
  386. SSL_SIGN_ECDSA_SECP384R1_SHA384,
  387. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  388. SSL_SIGN_RSA_PSS_SHA384,
  389. #endif
  390. SSL_SIGN_RSA_PKCS1_SHA384,
  391. // TODO(davidben): Remove this.
  392. #if defined(BORINGSSL_ANDROID_SYSTEM)
  393. SSL_SIGN_ECDSA_SECP521R1_SHA512,
  394. #endif
  395. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  396. SSL_SIGN_RSA_PSS_SHA512,
  397. #endif
  398. SSL_SIGN_RSA_PKCS1_SHA512,
  399. // For now, SHA-1 is still accepted but least preferable.
  400. SSL_SIGN_RSA_PKCS1_SHA1,
  401. };
  402. // kSignSignatureAlgorithms is the default list of supported signature
  403. // algorithms for signing.
  404. //
  405. // For now, RSA-PSS signature algorithms are not enabled on Android's system
  406. // BoringSSL. Once the change in Chrome has stuck and the values are finalized,
  407. // restore them.
  408. static const uint16_t kSignSignatureAlgorithms[] = {
  409. // List our preferred algorithms first.
  410. SSL_SIGN_ED25519,
  411. SSL_SIGN_ECDSA_SECP256R1_SHA256,
  412. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  413. SSL_SIGN_RSA_PSS_SHA256,
  414. #endif
  415. SSL_SIGN_RSA_PKCS1_SHA256,
  416. // If needed, sign larger hashes.
  417. //
  418. // TODO(davidben): Determine which of these may be pruned.
  419. SSL_SIGN_ECDSA_SECP384R1_SHA384,
  420. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  421. SSL_SIGN_RSA_PSS_SHA384,
  422. #endif
  423. SSL_SIGN_RSA_PKCS1_SHA384,
  424. SSL_SIGN_ECDSA_SECP521R1_SHA512,
  425. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  426. SSL_SIGN_RSA_PSS_SHA512,
  427. #endif
  428. SSL_SIGN_RSA_PKCS1_SHA512,
  429. // If the peer supports nothing else, sign with SHA-1.
  430. SSL_SIGN_ECDSA_SHA1,
  431. SSL_SIGN_RSA_PKCS1_SHA1,
  432. };
  433. int tls12_add_verify_sigalgs(const SSL *ssl, CBB *out) {
  434. const uint16_t *sigalgs = kVerifySignatureAlgorithms;
  435. size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
  436. if (ssl->ctx->num_verify_sigalgs != 0) {
  437. sigalgs = ssl->ctx->verify_sigalgs;
  438. num_sigalgs = ssl->ctx->num_verify_sigalgs;
  439. }
  440. for (size_t i = 0; i < num_sigalgs; i++) {
  441. if (sigalgs == kVerifySignatureAlgorithms &&
  442. sigalgs[i] == SSL_SIGN_ED25519 &&
  443. !ssl->ctx->ed25519_enabled) {
  444. continue;
  445. }
  446. if (!CBB_add_u16(out, sigalgs[i])) {
  447. return 0;
  448. }
  449. }
  450. return 1;
  451. }
  452. int tls12_check_peer_sigalg(SSL *ssl, uint8_t *out_alert, uint16_t sigalg) {
  453. const uint16_t *sigalgs = kVerifySignatureAlgorithms;
  454. size_t num_sigalgs = OPENSSL_ARRAY_SIZE(kVerifySignatureAlgorithms);
  455. if (ssl->ctx->num_verify_sigalgs != 0) {
  456. sigalgs = ssl->ctx->verify_sigalgs;
  457. num_sigalgs = ssl->ctx->num_verify_sigalgs;
  458. }
  459. for (size_t i = 0; i < num_sigalgs; i++) {
  460. if (sigalgs == kVerifySignatureAlgorithms &&
  461. sigalgs[i] == SSL_SIGN_ED25519 &&
  462. !ssl->ctx->ed25519_enabled) {
  463. continue;
  464. }
  465. if (sigalg == sigalgs[i]) {
  466. return 1;
  467. }
  468. }
  469. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  470. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  471. return 0;
  472. }
  473. // tls_extension represents a TLS extension that is handled internally. The
  474. // |init| function is called for each handshake, before any other functions of
  475. // the extension. Then the add and parse callbacks are called as needed.
  476. //
  477. // The parse callbacks receive a |CBS| that contains the contents of the
  478. // extension (i.e. not including the type and length bytes). If an extension is
  479. // not received then the parse callbacks will be called with a NULL CBS so that
  480. // they can do any processing needed to handle the absence of an extension.
  481. //
  482. // The add callbacks receive a |CBB| to which the extension can be appended but
  483. // the function is responsible for appending the type and length bytes too.
  484. //
  485. // All callbacks return one for success and zero for error. If a parse function
  486. // returns zero then a fatal alert with value |*out_alert| will be sent. If
  487. // |*out_alert| isn't set, then a |decode_error| alert will be sent.
  488. struct tls_extension {
  489. uint16_t value;
  490. void (*init)(SSL_HANDSHAKE *hs);
  491. int (*add_clienthello)(SSL_HANDSHAKE *hs, CBB *out);
  492. int (*parse_serverhello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  493. CBS *contents);
  494. int (*parse_clienthello)(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  495. CBS *contents);
  496. int (*add_serverhello)(SSL_HANDSHAKE *hs, CBB *out);
  497. };
  498. static int forbid_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  499. CBS *contents) {
  500. if (contents != NULL) {
  501. // Servers MUST NOT send this extension.
  502. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  503. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  504. return 0;
  505. }
  506. return 1;
  507. }
  508. static int ignore_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  509. CBS *contents) {
  510. // This extension from the client is handled elsewhere.
  511. return 1;
  512. }
  513. static int dont_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  514. return 1;
  515. }
  516. // Server name indication (SNI).
  517. //
  518. // https://tools.ietf.org/html/rfc6066#section-3.
  519. static int ext_sni_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  520. SSL *const ssl = hs->ssl;
  521. if (ssl->tlsext_hostname == NULL) {
  522. return 1;
  523. }
  524. CBB contents, server_name_list, name;
  525. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  526. !CBB_add_u16_length_prefixed(out, &contents) ||
  527. !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
  528. !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
  529. !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
  530. !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
  531. strlen(ssl->tlsext_hostname)) ||
  532. !CBB_flush(out)) {
  533. return 0;
  534. }
  535. return 1;
  536. }
  537. static int ext_sni_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  538. CBS *contents) {
  539. SSL *const ssl = hs->ssl;
  540. if (contents == NULL) {
  541. return 1;
  542. }
  543. if (CBS_len(contents) != 0) {
  544. return 0;
  545. }
  546. assert(ssl->tlsext_hostname != NULL);
  547. if (ssl->session == NULL) {
  548. OPENSSL_free(hs->new_session->tlsext_hostname);
  549. hs->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
  550. if (!hs->new_session->tlsext_hostname) {
  551. *out_alert = SSL_AD_INTERNAL_ERROR;
  552. return 0;
  553. }
  554. }
  555. return 1;
  556. }
  557. static int ext_sni_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  558. CBS *contents) {
  559. if (contents == NULL) {
  560. return 1;
  561. }
  562. CBS server_name_list, host_name;
  563. uint8_t name_type;
  564. if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
  565. !CBS_get_u8(&server_name_list, &name_type) ||
  566. // Although the server_name extension was intended to be extensible to
  567. // new name types and multiple names, OpenSSL 1.0.x had a bug which meant
  568. // different name types will cause an error. Further, RFC 4366 originally
  569. // defined syntax inextensibly. RFC 6066 corrected this mistake, but
  570. // adding new name types is no longer feasible.
  571. //
  572. // Act as if the extensibility does not exist to simplify parsing.
  573. !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
  574. CBS_len(&server_name_list) != 0 ||
  575. CBS_len(contents) != 0) {
  576. return 0;
  577. }
  578. if (name_type != TLSEXT_NAMETYPE_host_name ||
  579. CBS_len(&host_name) == 0 ||
  580. CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
  581. CBS_contains_zero_byte(&host_name)) {
  582. *out_alert = SSL_AD_UNRECOGNIZED_NAME;
  583. return 0;
  584. }
  585. // Copy the hostname as a string.
  586. char *hostname_raw = nullptr;
  587. if (!CBS_strdup(&host_name, &hostname_raw)) {
  588. *out_alert = SSL_AD_INTERNAL_ERROR;
  589. return 0;
  590. }
  591. hs->hostname.reset(hostname_raw);
  592. hs->should_ack_sni = 1;
  593. return 1;
  594. }
  595. static int ext_sni_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  596. if (hs->ssl->s3->session_reused ||
  597. !hs->should_ack_sni) {
  598. return 1;
  599. }
  600. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  601. !CBB_add_u16(out, 0 /* length */)) {
  602. return 0;
  603. }
  604. return 1;
  605. }
  606. // Renegotiation indication.
  607. //
  608. // https://tools.ietf.org/html/rfc5746
  609. static int ext_ri_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  610. SSL *const ssl = hs->ssl;
  611. // Renegotiation indication is not necessary in TLS 1.3.
  612. if (hs->min_version >= TLS1_3_VERSION) {
  613. return 1;
  614. }
  615. assert(ssl->s3->initial_handshake_complete ==
  616. (ssl->s3->previous_client_finished_len != 0));
  617. CBB contents, prev_finished;
  618. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  619. !CBB_add_u16_length_prefixed(out, &contents) ||
  620. !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
  621. !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
  622. ssl->s3->previous_client_finished_len) ||
  623. !CBB_flush(out)) {
  624. return 0;
  625. }
  626. return 1;
  627. }
  628. static int ext_ri_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  629. CBS *contents) {
  630. SSL *const ssl = hs->ssl;
  631. if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  632. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  633. return 0;
  634. }
  635. // Servers may not switch between omitting the extension and supporting it.
  636. // See RFC 5746, sections 3.5 and 4.2.
  637. if (ssl->s3->initial_handshake_complete &&
  638. (contents != NULL) != ssl->s3->send_connection_binding) {
  639. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  640. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  641. return 0;
  642. }
  643. if (contents == NULL) {
  644. // Strictly speaking, if we want to avoid an attack we should *always* see
  645. // RI even on initial ServerHello because the client doesn't see any
  646. // renegotiation during an attack. However this would mean we could not
  647. // connect to any server which doesn't support RI.
  648. //
  649. // OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
  650. // practical terms every client sets it so it's just assumed here.
  651. return 1;
  652. }
  653. const size_t expected_len = ssl->s3->previous_client_finished_len +
  654. ssl->s3->previous_server_finished_len;
  655. // Check for logic errors
  656. assert(!expected_len || ssl->s3->previous_client_finished_len);
  657. assert(!expected_len || ssl->s3->previous_server_finished_len);
  658. assert(ssl->s3->initial_handshake_complete ==
  659. (ssl->s3->previous_client_finished_len != 0));
  660. assert(ssl->s3->initial_handshake_complete ==
  661. (ssl->s3->previous_server_finished_len != 0));
  662. // Parse out the extension contents.
  663. CBS renegotiated_connection;
  664. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  665. CBS_len(contents) != 0) {
  666. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  667. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  668. return 0;
  669. }
  670. // Check that the extension matches.
  671. if (CBS_len(&renegotiated_connection) != expected_len) {
  672. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  673. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  674. return 0;
  675. }
  676. const uint8_t *d = CBS_data(&renegotiated_connection);
  677. int ok = CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
  678. ssl->s3->previous_client_finished_len) == 0;
  679. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  680. ok = 1;
  681. #endif
  682. if (!ok) {
  683. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  684. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  685. return 0;
  686. }
  687. d += ssl->s3->previous_client_finished_len;
  688. ok = CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
  689. ssl->s3->previous_server_finished_len) == 0;
  690. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  691. ok = 1;
  692. #endif
  693. if (!ok) {
  694. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  695. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  696. return 0;
  697. }
  698. ssl->s3->send_connection_binding = 1;
  699. return 1;
  700. }
  701. static int ext_ri_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  702. CBS *contents) {
  703. SSL *const ssl = hs->ssl;
  704. // Renegotiation isn't supported as a server so this function should never be
  705. // called after the initial handshake.
  706. assert(!ssl->s3->initial_handshake_complete);
  707. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  708. return 1;
  709. }
  710. if (contents == NULL) {
  711. return 1;
  712. }
  713. CBS renegotiated_connection;
  714. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  715. CBS_len(contents) != 0) {
  716. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  717. return 0;
  718. }
  719. // Check that the extension matches. We do not support renegotiation as a
  720. // server, so this must be empty.
  721. if (CBS_len(&renegotiated_connection) != 0) {
  722. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  723. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  724. return 0;
  725. }
  726. ssl->s3->send_connection_binding = 1;
  727. return 1;
  728. }
  729. static int ext_ri_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  730. SSL *const ssl = hs->ssl;
  731. // Renegotiation isn't supported as a server so this function should never be
  732. // called after the initial handshake.
  733. assert(!ssl->s3->initial_handshake_complete);
  734. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  735. return 1;
  736. }
  737. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  738. !CBB_add_u16(out, 1 /* length */) ||
  739. !CBB_add_u8(out, 0 /* empty renegotiation info */)) {
  740. return 0;
  741. }
  742. return 1;
  743. }
  744. // Extended Master Secret.
  745. //
  746. // https://tools.ietf.org/html/rfc7627
  747. static int ext_ems_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  748. // Extended master secret is not necessary in TLS 1.3.
  749. if (hs->min_version >= TLS1_3_VERSION || hs->max_version <= SSL3_VERSION) {
  750. return 1;
  751. }
  752. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  753. !CBB_add_u16(out, 0 /* length */)) {
  754. return 0;
  755. }
  756. return 1;
  757. }
  758. static int ext_ems_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  759. CBS *contents) {
  760. SSL *const ssl = hs->ssl;
  761. if (contents != NULL) {
  762. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  763. ssl->version == SSL3_VERSION ||
  764. CBS_len(contents) != 0) {
  765. return 0;
  766. }
  767. hs->extended_master_secret = 1;
  768. }
  769. // Whether EMS is negotiated may not change on renegotiation.
  770. if (ssl->s3->established_session != NULL &&
  771. hs->extended_master_secret !=
  772. ssl->s3->established_session->extended_master_secret) {
  773. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
  774. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  775. return 0;
  776. }
  777. return 1;
  778. }
  779. static int ext_ems_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  780. CBS *contents) {
  781. uint16_t version = ssl3_protocol_version(hs->ssl);
  782. if (version >= TLS1_3_VERSION ||
  783. version == SSL3_VERSION) {
  784. return 1;
  785. }
  786. if (contents == NULL) {
  787. return 1;
  788. }
  789. if (CBS_len(contents) != 0) {
  790. return 0;
  791. }
  792. hs->extended_master_secret = 1;
  793. return 1;
  794. }
  795. static int ext_ems_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  796. if (!hs->extended_master_secret) {
  797. return 1;
  798. }
  799. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  800. !CBB_add_u16(out, 0 /* length */)) {
  801. return 0;
  802. }
  803. return 1;
  804. }
  805. // Session tickets.
  806. //
  807. // https://tools.ietf.org/html/rfc5077
  808. static int ext_ticket_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  809. SSL *const ssl = hs->ssl;
  810. // TLS 1.3 uses a different ticket extension.
  811. if (hs->min_version >= TLS1_3_VERSION ||
  812. SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
  813. return 1;
  814. }
  815. const uint8_t *ticket_data = NULL;
  816. int ticket_len = 0;
  817. // Renegotiation does not participate in session resumption. However, still
  818. // advertise the extension to avoid potentially breaking servers which carry
  819. // over the state from the previous handshake, such as OpenSSL servers
  820. // without upstream's 3c3f0259238594d77264a78944d409f2127642c4.
  821. if (!ssl->s3->initial_handshake_complete &&
  822. ssl->session != NULL &&
  823. ssl->session->tlsext_tick != NULL &&
  824. // Don't send TLS 1.3 session tickets in the ticket extension.
  825. SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
  826. ticket_data = ssl->session->tlsext_tick;
  827. ticket_len = ssl->session->tlsext_ticklen;
  828. }
  829. CBB ticket;
  830. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  831. !CBB_add_u16_length_prefixed(out, &ticket) ||
  832. !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
  833. !CBB_flush(out)) {
  834. return 0;
  835. }
  836. return 1;
  837. }
  838. static int ext_ticket_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  839. CBS *contents) {
  840. SSL *const ssl = hs->ssl;
  841. if (contents == NULL) {
  842. return 1;
  843. }
  844. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  845. return 0;
  846. }
  847. // If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
  848. // this function should never be called, even if the server tries to send the
  849. // extension.
  850. assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
  851. if (CBS_len(contents) != 0) {
  852. return 0;
  853. }
  854. hs->ticket_expected = 1;
  855. return 1;
  856. }
  857. static int ext_ticket_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  858. if (!hs->ticket_expected) {
  859. return 1;
  860. }
  861. // If |SSL_OP_NO_TICKET| is set, |ticket_expected| should never be true.
  862. assert((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) == 0);
  863. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  864. !CBB_add_u16(out, 0 /* length */)) {
  865. return 0;
  866. }
  867. return 1;
  868. }
  869. // Signature Algorithms.
  870. //
  871. // https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  872. static int ext_sigalgs_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  873. SSL *const ssl = hs->ssl;
  874. if (hs->max_version < TLS1_2_VERSION) {
  875. return 1;
  876. }
  877. CBB contents, sigalgs_cbb;
  878. if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
  879. !CBB_add_u16_length_prefixed(out, &contents) ||
  880. !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb) ||
  881. !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb) ||
  882. !CBB_flush(out)) {
  883. return 0;
  884. }
  885. return 1;
  886. }
  887. static int ext_sigalgs_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  888. CBS *contents) {
  889. OPENSSL_free(hs->peer_sigalgs);
  890. hs->peer_sigalgs = NULL;
  891. hs->num_peer_sigalgs = 0;
  892. if (contents == NULL) {
  893. return 1;
  894. }
  895. CBS supported_signature_algorithms;
  896. if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
  897. CBS_len(contents) != 0 ||
  898. CBS_len(&supported_signature_algorithms) == 0 ||
  899. !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) {
  900. return 0;
  901. }
  902. return 1;
  903. }
  904. // OCSP Stapling.
  905. //
  906. // https://tools.ietf.org/html/rfc6066#section-8
  907. static int ext_ocsp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  908. SSL *const ssl = hs->ssl;
  909. if (!ssl->ocsp_stapling_enabled) {
  910. return 1;
  911. }
  912. CBB contents;
  913. if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
  914. !CBB_add_u16_length_prefixed(out, &contents) ||
  915. !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
  916. !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
  917. !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
  918. !CBB_flush(out)) {
  919. return 0;
  920. }
  921. return 1;
  922. }
  923. static int ext_ocsp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  924. CBS *contents) {
  925. SSL *const ssl = hs->ssl;
  926. if (contents == NULL) {
  927. return 1;
  928. }
  929. // TLS 1.3 OCSP responses are included in the Certificate extensions.
  930. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  931. return 0;
  932. }
  933. // OCSP stapling is forbidden on non-certificate ciphers.
  934. if (CBS_len(contents) != 0 ||
  935. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  936. return 0;
  937. }
  938. // Note this does not check for resumption in TLS 1.2. Sending
  939. // status_request here does not make sense, but OpenSSL does so and the
  940. // specification does not say anything. Tolerate it but ignore it.
  941. hs->certificate_status_expected = 1;
  942. return 1;
  943. }
  944. static int ext_ocsp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  945. CBS *contents) {
  946. if (contents == NULL) {
  947. return 1;
  948. }
  949. uint8_t status_type;
  950. if (!CBS_get_u8(contents, &status_type)) {
  951. return 0;
  952. }
  953. // We cannot decide whether OCSP stapling will occur yet because the correct
  954. // SSL_CTX might not have been selected.
  955. hs->ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
  956. return 1;
  957. }
  958. static int ext_ocsp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  959. SSL *const ssl = hs->ssl;
  960. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  961. !hs->ocsp_stapling_requested ||
  962. ssl->cert->ocsp_response == NULL ||
  963. ssl->s3->session_reused ||
  964. !ssl_cipher_uses_certificate_auth(hs->new_cipher)) {
  965. return 1;
  966. }
  967. hs->certificate_status_expected = 1;
  968. return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
  969. CBB_add_u16(out, 0 /* length */);
  970. }
  971. // Next protocol negotiation.
  972. //
  973. // https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html
  974. static int ext_npn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  975. SSL *const ssl = hs->ssl;
  976. if (ssl->s3->initial_handshake_complete ||
  977. ssl->ctx->next_proto_select_cb == NULL ||
  978. SSL_is_dtls(ssl)) {
  979. return 1;
  980. }
  981. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  982. !CBB_add_u16(out, 0 /* length */)) {
  983. return 0;
  984. }
  985. return 1;
  986. }
  987. static int ext_npn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  988. CBS *contents) {
  989. SSL *const ssl = hs->ssl;
  990. if (contents == NULL) {
  991. return 1;
  992. }
  993. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  994. return 0;
  995. }
  996. // If any of these are false then we should never have sent the NPN
  997. // extension in the ClientHello and thus this function should never have been
  998. // called.
  999. assert(!ssl->s3->initial_handshake_complete);
  1000. assert(!SSL_is_dtls(ssl));
  1001. assert(ssl->ctx->next_proto_select_cb != NULL);
  1002. if (ssl->s3->alpn_selected != NULL) {
  1003. // NPN and ALPN may not be negotiated in the same connection.
  1004. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1005. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  1006. return 0;
  1007. }
  1008. const uint8_t *const orig_contents = CBS_data(contents);
  1009. const size_t orig_len = CBS_len(contents);
  1010. while (CBS_len(contents) != 0) {
  1011. CBS proto;
  1012. if (!CBS_get_u8_length_prefixed(contents, &proto) ||
  1013. CBS_len(&proto) == 0) {
  1014. return 0;
  1015. }
  1016. }
  1017. uint8_t *selected;
  1018. uint8_t selected_len;
  1019. if (ssl->ctx->next_proto_select_cb(
  1020. ssl, &selected, &selected_len, orig_contents, orig_len,
  1021. ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
  1022. *out_alert = SSL_AD_INTERNAL_ERROR;
  1023. return 0;
  1024. }
  1025. OPENSSL_free(ssl->s3->next_proto_negotiated);
  1026. ssl->s3->next_proto_negotiated =
  1027. (uint8_t *)BUF_memdup(selected, selected_len);
  1028. if (ssl->s3->next_proto_negotiated == NULL) {
  1029. *out_alert = SSL_AD_INTERNAL_ERROR;
  1030. return 0;
  1031. }
  1032. ssl->s3->next_proto_negotiated_len = selected_len;
  1033. hs->next_proto_neg_seen = 1;
  1034. return 1;
  1035. }
  1036. static int ext_npn_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1037. CBS *contents) {
  1038. SSL *const ssl = hs->ssl;
  1039. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1040. return 1;
  1041. }
  1042. if (contents != NULL && CBS_len(contents) != 0) {
  1043. return 0;
  1044. }
  1045. if (contents == NULL ||
  1046. ssl->s3->initial_handshake_complete ||
  1047. ssl->ctx->next_protos_advertised_cb == NULL ||
  1048. SSL_is_dtls(ssl)) {
  1049. return 1;
  1050. }
  1051. hs->next_proto_neg_seen = 1;
  1052. return 1;
  1053. }
  1054. static int ext_npn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1055. SSL *const ssl = hs->ssl;
  1056. // |next_proto_neg_seen| might have been cleared when an ALPN extension was
  1057. // parsed.
  1058. if (!hs->next_proto_neg_seen) {
  1059. return 1;
  1060. }
  1061. const uint8_t *npa;
  1062. unsigned npa_len;
  1063. if (ssl->ctx->next_protos_advertised_cb(
  1064. ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
  1065. SSL_TLSEXT_ERR_OK) {
  1066. hs->next_proto_neg_seen = 0;
  1067. return 1;
  1068. }
  1069. CBB contents;
  1070. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  1071. !CBB_add_u16_length_prefixed(out, &contents) ||
  1072. !CBB_add_bytes(&contents, npa, npa_len) ||
  1073. !CBB_flush(out)) {
  1074. return 0;
  1075. }
  1076. return 1;
  1077. }
  1078. // Signed certificate timestamps.
  1079. //
  1080. // https://tools.ietf.org/html/rfc6962#section-3.3.1
  1081. static int ext_sct_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1082. SSL *const ssl = hs->ssl;
  1083. if (!ssl->signed_cert_timestamps_enabled) {
  1084. return 1;
  1085. }
  1086. if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
  1087. !CBB_add_u16(out, 0 /* length */)) {
  1088. return 0;
  1089. }
  1090. return 1;
  1091. }
  1092. static int ext_sct_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1093. CBS *contents) {
  1094. SSL *const ssl = hs->ssl;
  1095. if (contents == NULL) {
  1096. return 1;
  1097. }
  1098. // TLS 1.3 SCTs are included in the Certificate extensions.
  1099. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1100. *out_alert = SSL_AD_DECODE_ERROR;
  1101. return 0;
  1102. }
  1103. // If this is false then we should never have sent the SCT extension in the
  1104. // ClientHello and thus this function should never have been called.
  1105. assert(ssl->signed_cert_timestamps_enabled);
  1106. if (!ssl_is_sct_list_valid(contents)) {
  1107. *out_alert = SSL_AD_DECODE_ERROR;
  1108. return 0;
  1109. }
  1110. // Session resumption uses the original session information. The extension
  1111. // should not be sent on resumption, but RFC 6962 did not make it a
  1112. // requirement, so tolerate this.
  1113. //
  1114. // TODO(davidben): Enforce this anyway.
  1115. if (!ssl->s3->session_reused) {
  1116. CRYPTO_BUFFER_free(hs->new_session->signed_cert_timestamp_list);
  1117. hs->new_session->signed_cert_timestamp_list =
  1118. CRYPTO_BUFFER_new_from_CBS(contents, ssl->ctx->pool);
  1119. if (hs->new_session->signed_cert_timestamp_list == nullptr) {
  1120. *out_alert = SSL_AD_INTERNAL_ERROR;
  1121. return 0;
  1122. }
  1123. }
  1124. return 1;
  1125. }
  1126. static int ext_sct_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1127. CBS *contents) {
  1128. if (contents == NULL) {
  1129. return 1;
  1130. }
  1131. if (CBS_len(contents) != 0) {
  1132. return 0;
  1133. }
  1134. hs->scts_requested = 1;
  1135. return 1;
  1136. }
  1137. static int ext_sct_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1138. SSL *const ssl = hs->ssl;
  1139. // The extension shouldn't be sent when resuming sessions.
  1140. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  1141. ssl->s3->session_reused ||
  1142. ssl->cert->signed_cert_timestamp_list == NULL) {
  1143. return 1;
  1144. }
  1145. CBB contents;
  1146. return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
  1147. CBB_add_u16_length_prefixed(out, &contents) &&
  1148. CBB_add_bytes(
  1149. &contents,
  1150. CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
  1151. CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) &&
  1152. CBB_flush(out);
  1153. }
  1154. // Application-level Protocol Negotiation.
  1155. //
  1156. // https://tools.ietf.org/html/rfc7301
  1157. static int ext_alpn_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1158. SSL *const ssl = hs->ssl;
  1159. if (ssl->alpn_client_proto_list == NULL ||
  1160. ssl->s3->initial_handshake_complete) {
  1161. return 1;
  1162. }
  1163. CBB contents, proto_list;
  1164. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1165. !CBB_add_u16_length_prefixed(out, &contents) ||
  1166. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1167. !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
  1168. ssl->alpn_client_proto_list_len) ||
  1169. !CBB_flush(out)) {
  1170. return 0;
  1171. }
  1172. return 1;
  1173. }
  1174. static int ext_alpn_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1175. CBS *contents) {
  1176. SSL *const ssl = hs->ssl;
  1177. if (contents == NULL) {
  1178. return 1;
  1179. }
  1180. assert(!ssl->s3->initial_handshake_complete);
  1181. assert(ssl->alpn_client_proto_list != NULL);
  1182. if (hs->next_proto_neg_seen) {
  1183. // NPN and ALPN may not be negotiated in the same connection.
  1184. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1185. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  1186. return 0;
  1187. }
  1188. // The extension data consists of a ProtocolNameList which must have
  1189. // exactly one ProtocolName. Each of these is length-prefixed.
  1190. CBS protocol_name_list, protocol_name;
  1191. if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
  1192. CBS_len(contents) != 0 ||
  1193. !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
  1194. // Empty protocol names are forbidden.
  1195. CBS_len(&protocol_name) == 0 ||
  1196. CBS_len(&protocol_name_list) != 0) {
  1197. return 0;
  1198. }
  1199. if (!ssl->ctx->allow_unknown_alpn_protos) {
  1200. // Check that the protocol name is one of the ones we advertised.
  1201. int protocol_ok = 0;
  1202. CBS client_protocol_name_list, client_protocol_name;
  1203. CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
  1204. ssl->alpn_client_proto_list_len);
  1205. while (CBS_len(&client_protocol_name_list) > 0) {
  1206. if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
  1207. &client_protocol_name)) {
  1208. *out_alert = SSL_AD_INTERNAL_ERROR;
  1209. return 0;
  1210. }
  1211. if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
  1212. OPENSSL_memcmp(CBS_data(&client_protocol_name),
  1213. CBS_data(&protocol_name),
  1214. CBS_len(&protocol_name)) == 0) {
  1215. protocol_ok = 1;
  1216. break;
  1217. }
  1218. }
  1219. if (!protocol_ok) {
  1220. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
  1221. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1222. return 0;
  1223. }
  1224. }
  1225. if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
  1226. &ssl->s3->alpn_selected_len)) {
  1227. *out_alert = SSL_AD_INTERNAL_ERROR;
  1228. return 0;
  1229. }
  1230. return 1;
  1231. }
  1232. int ssl_negotiate_alpn(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1233. const SSL_CLIENT_HELLO *client_hello) {
  1234. SSL *const ssl = hs->ssl;
  1235. CBS contents;
  1236. if (ssl->ctx->alpn_select_cb == NULL ||
  1237. !ssl_client_hello_get_extension(
  1238. client_hello, &contents,
  1239. TLSEXT_TYPE_application_layer_protocol_negotiation)) {
  1240. // Ignore ALPN if not configured or no extension was supplied.
  1241. return 1;
  1242. }
  1243. // ALPN takes precedence over NPN.
  1244. hs->next_proto_neg_seen = 0;
  1245. CBS protocol_name_list;
  1246. if (!CBS_get_u16_length_prefixed(&contents, &protocol_name_list) ||
  1247. CBS_len(&contents) != 0 ||
  1248. CBS_len(&protocol_name_list) < 2) {
  1249. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  1250. *out_alert = SSL_AD_DECODE_ERROR;
  1251. return 0;
  1252. }
  1253. // Validate the protocol list.
  1254. CBS protocol_name_list_copy = protocol_name_list;
  1255. while (CBS_len(&protocol_name_list_copy) > 0) {
  1256. CBS protocol_name;
  1257. if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
  1258. // Empty protocol names are forbidden.
  1259. CBS_len(&protocol_name) == 0) {
  1260. OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT);
  1261. *out_alert = SSL_AD_DECODE_ERROR;
  1262. return 0;
  1263. }
  1264. }
  1265. const uint8_t *selected;
  1266. uint8_t selected_len;
  1267. if (ssl->ctx->alpn_select_cb(
  1268. ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
  1269. CBS_len(&protocol_name_list),
  1270. ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
  1271. OPENSSL_free(ssl->s3->alpn_selected);
  1272. ssl->s3->alpn_selected = (uint8_t *)BUF_memdup(selected, selected_len);
  1273. if (ssl->s3->alpn_selected == NULL) {
  1274. *out_alert = SSL_AD_INTERNAL_ERROR;
  1275. return 0;
  1276. }
  1277. ssl->s3->alpn_selected_len = selected_len;
  1278. }
  1279. return 1;
  1280. }
  1281. static int ext_alpn_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1282. SSL *const ssl = hs->ssl;
  1283. if (ssl->s3->alpn_selected == NULL) {
  1284. return 1;
  1285. }
  1286. CBB contents, proto_list, proto;
  1287. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1288. !CBB_add_u16_length_prefixed(out, &contents) ||
  1289. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1290. !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
  1291. !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
  1292. ssl->s3->alpn_selected_len) ||
  1293. !CBB_flush(out)) {
  1294. return 0;
  1295. }
  1296. return 1;
  1297. }
  1298. // Channel ID.
  1299. //
  1300. // https://tools.ietf.org/html/draft-balfanz-tls-channelid-01
  1301. static void ext_channel_id_init(SSL_HANDSHAKE *hs) {
  1302. hs->ssl->s3->tlsext_channel_id_valid = 0;
  1303. }
  1304. static int ext_channel_id_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1305. SSL *const ssl = hs->ssl;
  1306. if (!ssl->tlsext_channel_id_enabled ||
  1307. SSL_is_dtls(ssl)) {
  1308. return 1;
  1309. }
  1310. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1311. !CBB_add_u16(out, 0 /* length */)) {
  1312. return 0;
  1313. }
  1314. return 1;
  1315. }
  1316. static int ext_channel_id_parse_serverhello(SSL_HANDSHAKE *hs,
  1317. uint8_t *out_alert, CBS *contents) {
  1318. SSL *const ssl = hs->ssl;
  1319. if (contents == NULL) {
  1320. return 1;
  1321. }
  1322. assert(!SSL_is_dtls(ssl));
  1323. assert(ssl->tlsext_channel_id_enabled);
  1324. if (CBS_len(contents) != 0) {
  1325. return 0;
  1326. }
  1327. ssl->s3->tlsext_channel_id_valid = 1;
  1328. return 1;
  1329. }
  1330. static int ext_channel_id_parse_clienthello(SSL_HANDSHAKE *hs,
  1331. uint8_t *out_alert, CBS *contents) {
  1332. SSL *const ssl = hs->ssl;
  1333. if (contents == NULL ||
  1334. !ssl->tlsext_channel_id_enabled ||
  1335. SSL_is_dtls(ssl)) {
  1336. return 1;
  1337. }
  1338. if (CBS_len(contents) != 0) {
  1339. return 0;
  1340. }
  1341. ssl->s3->tlsext_channel_id_valid = 1;
  1342. return 1;
  1343. }
  1344. static int ext_channel_id_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1345. SSL *const ssl = hs->ssl;
  1346. if (!ssl->s3->tlsext_channel_id_valid) {
  1347. return 1;
  1348. }
  1349. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1350. !CBB_add_u16(out, 0 /* length */)) {
  1351. return 0;
  1352. }
  1353. return 1;
  1354. }
  1355. // Secure Real-time Transport Protocol (SRTP) extension.
  1356. //
  1357. // https://tools.ietf.org/html/rfc5764
  1358. static void ext_srtp_init(SSL_HANDSHAKE *hs) {
  1359. hs->ssl->srtp_profile = NULL;
  1360. }
  1361. static int ext_srtp_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1362. SSL *const ssl = hs->ssl;
  1363. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1364. if (profiles == NULL ||
  1365. sk_SRTP_PROTECTION_PROFILE_num(profiles) == 0) {
  1366. return 1;
  1367. }
  1368. CBB contents, profile_ids;
  1369. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1370. !CBB_add_u16_length_prefixed(out, &contents) ||
  1371. !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
  1372. return 0;
  1373. }
  1374. for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
  1375. if (!CBB_add_u16(&profile_ids, profile->id)) {
  1376. return 0;
  1377. }
  1378. }
  1379. if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
  1380. !CBB_flush(out)) {
  1381. return 0;
  1382. }
  1383. return 1;
  1384. }
  1385. static int ext_srtp_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1386. CBS *contents) {
  1387. SSL *const ssl = hs->ssl;
  1388. if (contents == NULL) {
  1389. return 1;
  1390. }
  1391. // The extension consists of a u16-prefixed profile ID list containing a
  1392. // single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
  1393. //
  1394. // See https://tools.ietf.org/html/rfc5764#section-4.1.1
  1395. CBS profile_ids, srtp_mki;
  1396. uint16_t profile_id;
  1397. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1398. !CBS_get_u16(&profile_ids, &profile_id) ||
  1399. CBS_len(&profile_ids) != 0 ||
  1400. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1401. CBS_len(contents) != 0) {
  1402. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1403. return 0;
  1404. }
  1405. if (CBS_len(&srtp_mki) != 0) {
  1406. // Must be no MKI, since we never offer one.
  1407. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
  1408. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1409. return 0;
  1410. }
  1411. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1412. // Check to see if the server gave us something we support (and presumably
  1413. // offered).
  1414. for (const SRTP_PROTECTION_PROFILE *profile : profiles) {
  1415. if (profile->id == profile_id) {
  1416. ssl->srtp_profile = profile;
  1417. return 1;
  1418. }
  1419. }
  1420. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1421. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1422. return 0;
  1423. }
  1424. static int ext_srtp_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1425. CBS *contents) {
  1426. SSL *const ssl = hs->ssl;
  1427. if (contents == NULL) {
  1428. return 1;
  1429. }
  1430. CBS profile_ids, srtp_mki;
  1431. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1432. CBS_len(&profile_ids) < 2 ||
  1433. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1434. CBS_len(contents) != 0) {
  1435. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1436. return 0;
  1437. }
  1438. // Discard the MKI value for now.
  1439. const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
  1440. SSL_get_srtp_profiles(ssl);
  1441. // Pick the server's most preferred profile.
  1442. for (const SRTP_PROTECTION_PROFILE *server_profile : server_profiles) {
  1443. CBS profile_ids_tmp;
  1444. CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
  1445. while (CBS_len(&profile_ids_tmp) > 0) {
  1446. uint16_t profile_id;
  1447. if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
  1448. return 0;
  1449. }
  1450. if (server_profile->id == profile_id) {
  1451. ssl->srtp_profile = server_profile;
  1452. return 1;
  1453. }
  1454. }
  1455. }
  1456. return 1;
  1457. }
  1458. static int ext_srtp_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1459. SSL *const ssl = hs->ssl;
  1460. if (ssl->srtp_profile == NULL) {
  1461. return 1;
  1462. }
  1463. CBB contents, profile_ids;
  1464. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1465. !CBB_add_u16_length_prefixed(out, &contents) ||
  1466. !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
  1467. !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
  1468. !CBB_add_u8(&contents, 0 /* empty MKI */) ||
  1469. !CBB_flush(out)) {
  1470. return 0;
  1471. }
  1472. return 1;
  1473. }
  1474. // EC point formats.
  1475. //
  1476. // https://tools.ietf.org/html/rfc4492#section-5.1.2
  1477. static int ext_ec_point_add_extension(SSL_HANDSHAKE *hs, CBB *out) {
  1478. CBB contents, formats;
  1479. if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
  1480. !CBB_add_u16_length_prefixed(out, &contents) ||
  1481. !CBB_add_u8_length_prefixed(&contents, &formats) ||
  1482. !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
  1483. !CBB_flush(out)) {
  1484. return 0;
  1485. }
  1486. return 1;
  1487. }
  1488. static int ext_ec_point_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1489. // The point format extension is unneccessary in TLS 1.3.
  1490. if (hs->min_version >= TLS1_3_VERSION) {
  1491. return 1;
  1492. }
  1493. return ext_ec_point_add_extension(hs, out);
  1494. }
  1495. static int ext_ec_point_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1496. CBS *contents) {
  1497. if (contents == NULL) {
  1498. return 1;
  1499. }
  1500. if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
  1501. return 0;
  1502. }
  1503. CBS ec_point_format_list;
  1504. if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
  1505. CBS_len(contents) != 0) {
  1506. return 0;
  1507. }
  1508. // Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
  1509. // point format.
  1510. if (OPENSSL_memchr(CBS_data(&ec_point_format_list),
  1511. TLSEXT_ECPOINTFORMAT_uncompressed,
  1512. CBS_len(&ec_point_format_list)) == NULL) {
  1513. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1514. return 0;
  1515. }
  1516. return 1;
  1517. }
  1518. static int ext_ec_point_parse_clienthello(SSL_HANDSHAKE *hs, uint8_t *out_alert,
  1519. CBS *contents) {
  1520. if (ssl3_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
  1521. return 1;
  1522. }
  1523. return ext_ec_point_parse_serverhello(hs, out_alert, contents);
  1524. }
  1525. static int ext_ec_point_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1526. SSL *const ssl = hs->ssl;
  1527. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1528. return 1;
  1529. }
  1530. const uint32_t alg_k = hs->new_cipher->algorithm_mkey;
  1531. const uint32_t alg_a = hs->new_cipher->algorithm_auth;
  1532. const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
  1533. if (!using_ecc) {
  1534. return 1;
  1535. }
  1536. return ext_ec_point_add_extension(hs, out);
  1537. }
  1538. // Pre Shared Key
  1539. //
  1540. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.6
  1541. static size_t ext_pre_shared_key_clienthello_length(SSL_HANDSHAKE *hs) {
  1542. SSL *const ssl = hs->ssl;
  1543. if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
  1544. SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
  1545. return 0;
  1546. }
  1547. size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
  1548. return 15 + ssl->session->tlsext_ticklen + binder_len;
  1549. }
  1550. static int ext_pre_shared_key_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1551. SSL *const ssl = hs->ssl;
  1552. if (hs->max_version < TLS1_3_VERSION || ssl->session == NULL ||
  1553. SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION) {
  1554. return 1;
  1555. }
  1556. struct OPENSSL_timeval now;
  1557. ssl_get_current_time(ssl, &now);
  1558. uint32_t ticket_age = 1000 * (now.tv_sec - ssl->session->time);
  1559. uint32_t obfuscated_ticket_age = ticket_age + ssl->session->ticket_age_add;
  1560. // Fill in a placeholder zero binder of the appropriate length. It will be
  1561. // computed and filled in later after length prefixes are computed.
  1562. uint8_t zero_binder[EVP_MAX_MD_SIZE] = {0};
  1563. size_t binder_len = EVP_MD_size(SSL_SESSION_get_digest(ssl->session));
  1564. CBB contents, identity, ticket, binders, binder;
  1565. if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
  1566. !CBB_add_u16_length_prefixed(out, &contents) ||
  1567. !CBB_add_u16_length_prefixed(&contents, &identity) ||
  1568. !CBB_add_u16_length_prefixed(&identity, &ticket) ||
  1569. !CBB_add_bytes(&ticket, ssl->session->tlsext_tick,
  1570. ssl->session->tlsext_ticklen) ||
  1571. !CBB_add_u32(&identity, obfuscated_ticket_age) ||
  1572. !CBB_add_u16_length_prefixed(&contents, &binders) ||
  1573. !CBB_add_u8_length_prefixed(&binders, &binder) ||
  1574. !CBB_add_bytes(&binder, zero_binder, binder_len)) {
  1575. return 0;
  1576. }
  1577. hs->needs_psk_binder = 1;
  1578. return CBB_flush(out);
  1579. }
  1580. int ssl_ext_pre_shared_key_parse_serverhello(SSL_HANDSHAKE *hs,
  1581. uint8_t *out_alert,
  1582. CBS *contents) {
  1583. uint16_t psk_id;
  1584. if (!CBS_get_u16(contents, &psk_id) ||
  1585. CBS_len(contents) != 0) {
  1586. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1587. *out_alert = SSL_AD_DECODE_ERROR;
  1588. return 0;
  1589. }
  1590. // We only advertise one PSK identity, so the only legal index is zero.
  1591. if (psk_id != 0) {
  1592. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND);
  1593. *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
  1594. return 0;
  1595. }
  1596. return 1;
  1597. }
  1598. int ssl_ext_pre_shared_key_parse_clienthello(
  1599. SSL_HANDSHAKE *hs, CBS *out_ticket, CBS *out_binders,
  1600. uint32_t *out_obfuscated_ticket_age, uint8_t *out_alert, CBS *contents) {
  1601. // We only process the first PSK identity since we don't support pure PSK.
  1602. CBS identities, binders;
  1603. if (!CBS_get_u16_length_prefixed(contents, &identities) ||
  1604. !CBS_get_u16_length_prefixed(&identities, out_ticket) ||
  1605. !CBS_get_u32(&identities, out_obfuscated_ticket_age) ||
  1606. !CBS_get_u16_length_prefixed(contents, &binders) ||
  1607. CBS_len(&binders) == 0 ||
  1608. CBS_len(contents) != 0) {
  1609. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1610. *out_alert = SSL_AD_DECODE_ERROR;
  1611. return 0;
  1612. }
  1613. *out_binders = binders;
  1614. // Check the syntax of the remaining identities, but do not process them.
  1615. size_t num_identities = 1;
  1616. while (CBS_len(&identities) != 0) {
  1617. CBS unused_ticket;
  1618. uint32_t unused_obfuscated_ticket_age;
  1619. if (!CBS_get_u16_length_prefixed(&identities, &unused_ticket) ||
  1620. !CBS_get_u32(&identities, &unused_obfuscated_ticket_age)) {
  1621. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1622. *out_alert = SSL_AD_DECODE_ERROR;
  1623. return 0;
  1624. }
  1625. num_identities++;
  1626. }
  1627. // Check the syntax of the binders. The value will be checked later if
  1628. // resuming.
  1629. size_t num_binders = 0;
  1630. while (CBS_len(&binders) != 0) {
  1631. CBS binder;
  1632. if (!CBS_get_u8_length_prefixed(&binders, &binder)) {
  1633. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1634. *out_alert = SSL_AD_DECODE_ERROR;
  1635. return 0;
  1636. }
  1637. num_binders++;
  1638. }
  1639. if (num_identities != num_binders) {
  1640. OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_BINDER_COUNT_MISMATCH);
  1641. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1642. return 0;
  1643. }
  1644. return 1;
  1645. }
  1646. int ssl_ext_pre_shared_key_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1647. if (!hs->ssl->s3->session_reused) {
  1648. return 1;
  1649. }
  1650. CBB contents;
  1651. if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
  1652. !CBB_add_u16_length_prefixed(out, &contents) ||
  1653. // We only consider the first identity for resumption
  1654. !CBB_add_u16(&contents, 0) ||
  1655. !CBB_flush(out)) {
  1656. return 0;
  1657. }
  1658. return 1;
  1659. }
  1660. // Pre-Shared Key Exchange Modes
  1661. //
  1662. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.7
  1663. static int ext_psk_key_exchange_modes_add_clienthello(SSL_HANDSHAKE *hs,
  1664. CBB *out) {
  1665. if (hs->max_version < TLS1_3_VERSION) {
  1666. return 1;
  1667. }
  1668. CBB contents, ke_modes;
  1669. if (!CBB_add_u16(out, TLSEXT_TYPE_psk_key_exchange_modes) ||
  1670. !CBB_add_u16_length_prefixed(out, &contents) ||
  1671. !CBB_add_u8_length_prefixed(&contents, &ke_modes) ||
  1672. !CBB_add_u8(&ke_modes, SSL_PSK_DHE_KE)) {
  1673. return 0;
  1674. }
  1675. return CBB_flush(out);
  1676. }
  1677. static int ext_psk_key_exchange_modes_parse_clienthello(SSL_HANDSHAKE *hs,
  1678. uint8_t *out_alert,
  1679. CBS *contents) {
  1680. if (contents == NULL) {
  1681. return 1;
  1682. }
  1683. CBS ke_modes;
  1684. if (!CBS_get_u8_length_prefixed(contents, &ke_modes) ||
  1685. CBS_len(&ke_modes) == 0 ||
  1686. CBS_len(contents) != 0) {
  1687. *out_alert = SSL_AD_DECODE_ERROR;
  1688. return 0;
  1689. }
  1690. // We only support tickets with PSK_DHE_KE.
  1691. hs->accept_psk_mode = OPENSSL_memchr(CBS_data(&ke_modes), SSL_PSK_DHE_KE,
  1692. CBS_len(&ke_modes)) != NULL;
  1693. return 1;
  1694. }
  1695. // Early Data Indication
  1696. //
  1697. // https://tools.ietf.org/html/draft-ietf-tls-tls13-18#section-4.2.8
  1698. static int ext_early_data_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1699. SSL *const ssl = hs->ssl;
  1700. if (ssl->session == NULL ||
  1701. SSL_SESSION_protocol_version(ssl->session) < TLS1_3_VERSION ||
  1702. ssl->session->ticket_max_early_data == 0 ||
  1703. hs->received_hello_retry_request ||
  1704. !ssl->cert->enable_early_data) {
  1705. return 1;
  1706. }
  1707. hs->early_data_offered = 1;
  1708. if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
  1709. !CBB_add_u16(out, 0) ||
  1710. !CBB_flush(out)) {
  1711. return 0;
  1712. }
  1713. return 1;
  1714. }
  1715. static int ext_early_data_parse_serverhello(SSL_HANDSHAKE *hs,
  1716. uint8_t *out_alert, CBS *contents) {
  1717. SSL *const ssl = hs->ssl;
  1718. if (contents == NULL) {
  1719. return 1;
  1720. }
  1721. if (CBS_len(contents) != 0) {
  1722. *out_alert = SSL_AD_DECODE_ERROR;
  1723. return 0;
  1724. }
  1725. if (!ssl->s3->session_reused) {
  1726. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1727. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  1728. return 0;
  1729. }
  1730. ssl->early_data_accepted = 1;
  1731. return 1;
  1732. }
  1733. static int ext_early_data_parse_clienthello(SSL_HANDSHAKE *hs,
  1734. uint8_t *out_alert, CBS *contents) {
  1735. SSL *const ssl = hs->ssl;
  1736. if (contents == NULL ||
  1737. ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  1738. return 1;
  1739. }
  1740. if (CBS_len(contents) != 0) {
  1741. *out_alert = SSL_AD_DECODE_ERROR;
  1742. return 0;
  1743. }
  1744. hs->early_data_offered = 1;
  1745. return 1;
  1746. }
  1747. static int ext_early_data_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1748. if (!hs->ssl->early_data_accepted) {
  1749. return 1;
  1750. }
  1751. if (!CBB_add_u16(out, TLSEXT_TYPE_early_data) ||
  1752. !CBB_add_u16(out, 0) ||
  1753. !CBB_flush(out)) {
  1754. return 0;
  1755. }
  1756. return 1;
  1757. }
  1758. // Key Share
  1759. //
  1760. // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.5
  1761. static int ext_key_share_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1762. SSL *const ssl = hs->ssl;
  1763. if (hs->max_version < TLS1_3_VERSION) {
  1764. return 1;
  1765. }
  1766. CBB contents, kse_bytes;
  1767. if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
  1768. !CBB_add_u16_length_prefixed(out, &contents) ||
  1769. !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
  1770. return 0;
  1771. }
  1772. uint16_t group_id = hs->retry_group;
  1773. if (hs->received_hello_retry_request) {
  1774. // We received a HelloRetryRequest without a new curve, so there is no new
  1775. // share to append. Leave |hs->key_share| as-is.
  1776. if (group_id == 0 &&
  1777. !CBB_add_bytes(&kse_bytes, hs->key_share_bytes,
  1778. hs->key_share_bytes_len)) {
  1779. return 0;
  1780. }
  1781. OPENSSL_free(hs->key_share_bytes);
  1782. hs->key_share_bytes = NULL;
  1783. hs->key_share_bytes_len = 0;
  1784. if (group_id == 0) {
  1785. return CBB_flush(out);
  1786. }
  1787. } else {
  1788. // Add a fake group. See draft-davidben-tls-grease-01.
  1789. if (ssl->ctx->grease_enabled &&
  1790. (!CBB_add_u16(&kse_bytes,
  1791. ssl_get_grease_value(ssl, ssl_grease_group)) ||
  1792. !CBB_add_u16(&kse_bytes, 1 /* length */) ||
  1793. !CBB_add_u8(&kse_bytes, 0 /* one byte key share */))) {
  1794. return 0;
  1795. }
  1796. // Predict the most preferred group.
  1797. const uint16_t *groups;
  1798. size_t groups_len;
  1799. tls1_get_grouplist(ssl, &groups, &groups_len);
  1800. if (groups_len == 0) {
  1801. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_GROUPS_SPECIFIED);
  1802. return 0;
  1803. }
  1804. group_id = groups[0];
  1805. }
  1806. hs->key_share = SSLKeyShare::Create(group_id);
  1807. CBB key_exchange;
  1808. if (!hs->key_share ||
  1809. !CBB_add_u16(&kse_bytes, group_id) ||
  1810. !CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
  1811. !hs->key_share->Offer(&key_exchange) ||
  1812. !CBB_flush(&kse_bytes)) {
  1813. return 0;
  1814. }
  1815. if (!hs->received_hello_retry_request) {
  1816. // Save the contents of the extension to repeat it in the second
  1817. // ClientHello.
  1818. hs->key_share_bytes_len = CBB_len(&kse_bytes);
  1819. hs->key_share_bytes =
  1820. (uint8_t *)BUF_memdup(CBB_data(&kse_bytes), CBB_len(&kse_bytes));
  1821. if (hs->key_share_bytes == NULL) {
  1822. return 0;
  1823. }
  1824. }
  1825. return CBB_flush(out);
  1826. }
  1827. int ssl_ext_key_share_parse_serverhello(SSL_HANDSHAKE *hs, uint8_t **out_secret,
  1828. size_t *out_secret_len,
  1829. uint8_t *out_alert, CBS *contents) {
  1830. CBS peer_key;
  1831. uint16_t group_id;
  1832. if (!CBS_get_u16(contents, &group_id) ||
  1833. !CBS_get_u16_length_prefixed(contents, &peer_key) ||
  1834. CBS_len(contents) != 0) {
  1835. *out_alert = SSL_AD_DECODE_ERROR;
  1836. return 0;
  1837. }
  1838. if (hs->key_share->GroupID() != group_id) {
  1839. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1840. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  1841. return 0;
  1842. }
  1843. if (!hs->key_share->Finish(out_secret, out_secret_len, out_alert,
  1844. CBS_data(&peer_key), CBS_len(&peer_key))) {
  1845. *out_alert = SSL_AD_INTERNAL_ERROR;
  1846. return 0;
  1847. }
  1848. hs->new_session->group_id = group_id;
  1849. hs->key_share.reset();
  1850. return 1;
  1851. }
  1852. int ssl_ext_key_share_parse_clienthello(SSL_HANDSHAKE *hs, int *out_found,
  1853. uint8_t **out_secret,
  1854. size_t *out_secret_len,
  1855. uint8_t *out_alert, CBS *contents) {
  1856. uint16_t group_id;
  1857. CBS key_shares;
  1858. if (!tls1_get_shared_group(hs, &group_id)) {
  1859. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_GROUP);
  1860. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  1861. return 0;
  1862. }
  1863. if (!CBS_get_u16_length_prefixed(contents, &key_shares) ||
  1864. CBS_len(contents) != 0) {
  1865. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1866. return 0;
  1867. }
  1868. // Find the corresponding key share.
  1869. int found = 0;
  1870. CBS peer_key;
  1871. while (CBS_len(&key_shares) > 0) {
  1872. uint16_t id;
  1873. CBS peer_key_tmp;
  1874. if (!CBS_get_u16(&key_shares, &id) ||
  1875. !CBS_get_u16_length_prefixed(&key_shares, &peer_key_tmp)) {
  1876. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  1877. return 0;
  1878. }
  1879. if (id == group_id) {
  1880. if (found) {
  1881. OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_KEY_SHARE);
  1882. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1883. return 0;
  1884. }
  1885. found = 1;
  1886. peer_key = peer_key_tmp;
  1887. // Continue parsing the structure to keep peers honest.
  1888. }
  1889. }
  1890. if (!found) {
  1891. *out_found = 0;
  1892. *out_secret = NULL;
  1893. *out_secret_len = 0;
  1894. return 1;
  1895. }
  1896. // Compute the DH secret.
  1897. uint8_t *secret = NULL;
  1898. size_t secret_len;
  1899. ScopedCBB public_key;
  1900. UniquePtr<SSLKeyShare> key_share = SSLKeyShare::Create(group_id);
  1901. if (!key_share ||
  1902. !CBB_init(public_key.get(), 32) ||
  1903. !key_share->Accept(public_key.get(), &secret, &secret_len, out_alert,
  1904. CBS_data(&peer_key), CBS_len(&peer_key)) ||
  1905. !CBB_finish(public_key.get(), &hs->ecdh_public_key,
  1906. &hs->ecdh_public_key_len)) {
  1907. OPENSSL_free(secret);
  1908. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1909. return 0;
  1910. }
  1911. *out_secret = secret;
  1912. *out_secret_len = secret_len;
  1913. *out_found = 1;
  1914. return 1;
  1915. }
  1916. int ssl_ext_key_share_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  1917. uint16_t group_id;
  1918. CBB kse_bytes, public_key;
  1919. if (!tls1_get_shared_group(hs, &group_id) ||
  1920. !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
  1921. !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
  1922. !CBB_add_u16(&kse_bytes, group_id) ||
  1923. !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
  1924. !CBB_add_bytes(&public_key, hs->ecdh_public_key,
  1925. hs->ecdh_public_key_len) ||
  1926. !CBB_flush(out)) {
  1927. return 0;
  1928. }
  1929. OPENSSL_free(hs->ecdh_public_key);
  1930. hs->ecdh_public_key = NULL;
  1931. hs->ecdh_public_key_len = 0;
  1932. hs->new_session->group_id = group_id;
  1933. return 1;
  1934. }
  1935. // Supported Versions
  1936. //
  1937. // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.1
  1938. static int ext_supported_versions_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1939. SSL *const ssl = hs->ssl;
  1940. if (hs->max_version <= TLS1_2_VERSION) {
  1941. return 1;
  1942. }
  1943. CBB contents, versions;
  1944. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_versions) ||
  1945. !CBB_add_u16_length_prefixed(out, &contents) ||
  1946. !CBB_add_u8_length_prefixed(&contents, &versions)) {
  1947. return 0;
  1948. }
  1949. // Add a fake version. See draft-davidben-tls-grease-01.
  1950. if (ssl->ctx->grease_enabled &&
  1951. !CBB_add_u16(&versions, ssl_get_grease_value(ssl, ssl_grease_version))) {
  1952. return 0;
  1953. }
  1954. if (!ssl_add_supported_versions(hs, &versions) ||
  1955. !CBB_flush(out)) {
  1956. return 0;
  1957. }
  1958. return 1;
  1959. }
  1960. // Cookie
  1961. //
  1962. // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.2
  1963. static int ext_cookie_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1964. if (hs->cookie == NULL) {
  1965. return 1;
  1966. }
  1967. CBB contents, cookie;
  1968. if (!CBB_add_u16(out, TLSEXT_TYPE_cookie) ||
  1969. !CBB_add_u16_length_prefixed(out, &contents) ||
  1970. !CBB_add_u16_length_prefixed(&contents, &cookie) ||
  1971. !CBB_add_bytes(&cookie, hs->cookie, hs->cookie_len) ||
  1972. !CBB_flush(out)) {
  1973. return 0;
  1974. }
  1975. // The cookie is no longer needed in memory.
  1976. OPENSSL_free(hs->cookie);
  1977. hs->cookie = NULL;
  1978. hs->cookie_len = 0;
  1979. return 1;
  1980. }
  1981. // Negotiated Groups
  1982. //
  1983. // https://tools.ietf.org/html/rfc4492#section-5.1.2
  1984. // https://tools.ietf.org/html/draft-ietf-tls-tls13-16#section-4.2.4
  1985. static int ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1986. SSL *const ssl = hs->ssl;
  1987. CBB contents, groups_bytes;
  1988. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
  1989. !CBB_add_u16_length_prefixed(out, &contents) ||
  1990. !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
  1991. return 0;
  1992. }
  1993. // Add a fake group. See draft-davidben-tls-grease-01.
  1994. if (ssl->ctx->grease_enabled &&
  1995. !CBB_add_u16(&groups_bytes,
  1996. ssl_get_grease_value(ssl, ssl_grease_group))) {
  1997. return 0;
  1998. }
  1999. const uint16_t *groups;
  2000. size_t groups_len;
  2001. tls1_get_grouplist(ssl, &groups, &groups_len);
  2002. for (size_t i = 0; i < groups_len; i++) {
  2003. if (!CBB_add_u16(&groups_bytes, groups[i])) {
  2004. return 0;
  2005. }
  2006. }
  2007. return CBB_flush(out);
  2008. }
  2009. static int ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
  2010. uint8_t *out_alert,
  2011. CBS *contents) {
  2012. // This extension is not expected to be echoed by servers in TLS 1.2, but some
  2013. // BigIP servers send it nonetheless, so do not enforce this.
  2014. return 1;
  2015. }
  2016. static int ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
  2017. uint8_t *out_alert,
  2018. CBS *contents) {
  2019. if (contents == NULL) {
  2020. return 1;
  2021. }
  2022. CBS supported_group_list;
  2023. if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
  2024. CBS_len(&supported_group_list) == 0 ||
  2025. (CBS_len(&supported_group_list) & 1) != 0 ||
  2026. CBS_len(contents) != 0) {
  2027. return 0;
  2028. }
  2029. hs->peer_supported_group_list =
  2030. (uint16_t *)OPENSSL_malloc(CBS_len(&supported_group_list));
  2031. if (hs->peer_supported_group_list == NULL) {
  2032. *out_alert = SSL_AD_INTERNAL_ERROR;
  2033. return 0;
  2034. }
  2035. const size_t num_groups = CBS_len(&supported_group_list) / 2;
  2036. for (size_t i = 0; i < num_groups; i++) {
  2037. if (!CBS_get_u16(&supported_group_list,
  2038. &hs->peer_supported_group_list[i])) {
  2039. goto err;
  2040. }
  2041. }
  2042. assert(CBS_len(&supported_group_list) == 0);
  2043. hs->peer_supported_group_list_len = num_groups;
  2044. return 1;
  2045. err:
  2046. OPENSSL_free(hs->peer_supported_group_list);
  2047. hs->peer_supported_group_list = NULL;
  2048. *out_alert = SSL_AD_INTERNAL_ERROR;
  2049. return 0;
  2050. }
  2051. static int ext_supported_groups_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  2052. // Servers don't echo this extension.
  2053. return 1;
  2054. }
  2055. // kExtensions contains all the supported extensions.
  2056. static const struct tls_extension kExtensions[] = {
  2057. {
  2058. TLSEXT_TYPE_renegotiate,
  2059. NULL,
  2060. ext_ri_add_clienthello,
  2061. ext_ri_parse_serverhello,
  2062. ext_ri_parse_clienthello,
  2063. ext_ri_add_serverhello,
  2064. },
  2065. {
  2066. TLSEXT_TYPE_server_name,
  2067. NULL,
  2068. ext_sni_add_clienthello,
  2069. ext_sni_parse_serverhello,
  2070. ext_sni_parse_clienthello,
  2071. ext_sni_add_serverhello,
  2072. },
  2073. {
  2074. TLSEXT_TYPE_extended_master_secret,
  2075. NULL,
  2076. ext_ems_add_clienthello,
  2077. ext_ems_parse_serverhello,
  2078. ext_ems_parse_clienthello,
  2079. ext_ems_add_serverhello,
  2080. },
  2081. {
  2082. TLSEXT_TYPE_session_ticket,
  2083. NULL,
  2084. ext_ticket_add_clienthello,
  2085. ext_ticket_parse_serverhello,
  2086. // Ticket extension client parsing is handled in ssl_session.c
  2087. ignore_parse_clienthello,
  2088. ext_ticket_add_serverhello,
  2089. },
  2090. {
  2091. TLSEXT_TYPE_signature_algorithms,
  2092. NULL,
  2093. ext_sigalgs_add_clienthello,
  2094. forbid_parse_serverhello,
  2095. ext_sigalgs_parse_clienthello,
  2096. dont_add_serverhello,
  2097. },
  2098. {
  2099. TLSEXT_TYPE_status_request,
  2100. NULL,
  2101. ext_ocsp_add_clienthello,
  2102. ext_ocsp_parse_serverhello,
  2103. ext_ocsp_parse_clienthello,
  2104. ext_ocsp_add_serverhello,
  2105. },
  2106. {
  2107. TLSEXT_TYPE_next_proto_neg,
  2108. NULL,
  2109. ext_npn_add_clienthello,
  2110. ext_npn_parse_serverhello,
  2111. ext_npn_parse_clienthello,
  2112. ext_npn_add_serverhello,
  2113. },
  2114. {
  2115. TLSEXT_TYPE_certificate_timestamp,
  2116. NULL,
  2117. ext_sct_add_clienthello,
  2118. ext_sct_parse_serverhello,
  2119. ext_sct_parse_clienthello,
  2120. ext_sct_add_serverhello,
  2121. },
  2122. {
  2123. TLSEXT_TYPE_application_layer_protocol_negotiation,
  2124. NULL,
  2125. ext_alpn_add_clienthello,
  2126. ext_alpn_parse_serverhello,
  2127. // ALPN is negotiated late in |ssl_negotiate_alpn|.
  2128. ignore_parse_clienthello,
  2129. ext_alpn_add_serverhello,
  2130. },
  2131. {
  2132. TLSEXT_TYPE_channel_id,
  2133. ext_channel_id_init,
  2134. ext_channel_id_add_clienthello,
  2135. ext_channel_id_parse_serverhello,
  2136. ext_channel_id_parse_clienthello,
  2137. ext_channel_id_add_serverhello,
  2138. },
  2139. {
  2140. TLSEXT_TYPE_srtp,
  2141. ext_srtp_init,
  2142. ext_srtp_add_clienthello,
  2143. ext_srtp_parse_serverhello,
  2144. ext_srtp_parse_clienthello,
  2145. ext_srtp_add_serverhello,
  2146. },
  2147. {
  2148. TLSEXT_TYPE_ec_point_formats,
  2149. NULL,
  2150. ext_ec_point_add_clienthello,
  2151. ext_ec_point_parse_serverhello,
  2152. ext_ec_point_parse_clienthello,
  2153. ext_ec_point_add_serverhello,
  2154. },
  2155. {
  2156. TLSEXT_TYPE_key_share,
  2157. NULL,
  2158. ext_key_share_add_clienthello,
  2159. forbid_parse_serverhello,
  2160. ignore_parse_clienthello,
  2161. dont_add_serverhello,
  2162. },
  2163. {
  2164. TLSEXT_TYPE_psk_key_exchange_modes,
  2165. NULL,
  2166. ext_psk_key_exchange_modes_add_clienthello,
  2167. forbid_parse_serverhello,
  2168. ext_psk_key_exchange_modes_parse_clienthello,
  2169. dont_add_serverhello,
  2170. },
  2171. {
  2172. TLSEXT_TYPE_early_data,
  2173. NULL,
  2174. ext_early_data_add_clienthello,
  2175. ext_early_data_parse_serverhello,
  2176. ext_early_data_parse_clienthello,
  2177. ext_early_data_add_serverhello,
  2178. },
  2179. {
  2180. TLSEXT_TYPE_supported_versions,
  2181. NULL,
  2182. ext_supported_versions_add_clienthello,
  2183. forbid_parse_serverhello,
  2184. ignore_parse_clienthello,
  2185. dont_add_serverhello,
  2186. },
  2187. {
  2188. TLSEXT_TYPE_cookie,
  2189. NULL,
  2190. ext_cookie_add_clienthello,
  2191. forbid_parse_serverhello,
  2192. ignore_parse_clienthello,
  2193. dont_add_serverhello,
  2194. },
  2195. // The final extension must be non-empty. WebSphere Application Server 7.0 is
  2196. // intolerant to the last extension being zero-length. See
  2197. // https://crbug.com/363583.
  2198. {
  2199. TLSEXT_TYPE_supported_groups,
  2200. NULL,
  2201. ext_supported_groups_add_clienthello,
  2202. ext_supported_groups_parse_serverhello,
  2203. ext_supported_groups_parse_clienthello,
  2204. ext_supported_groups_add_serverhello,
  2205. },
  2206. };
  2207. #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
  2208. static_assert(kNumExtensions <=
  2209. sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
  2210. "too many extensions for sent bitset");
  2211. static_assert(kNumExtensions <=
  2212. sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
  2213. "too many extensions for received bitset");
  2214. static const struct tls_extension *tls_extension_find(uint32_t *out_index,
  2215. uint16_t value) {
  2216. unsigned i;
  2217. for (i = 0; i < kNumExtensions; i++) {
  2218. if (kExtensions[i].value == value) {
  2219. *out_index = i;
  2220. return &kExtensions[i];
  2221. }
  2222. }
  2223. return NULL;
  2224. }
  2225. int ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out, size_t header_len) {
  2226. SSL *const ssl = hs->ssl;
  2227. // Don't add extensions for SSLv3 unless doing secure renegotiation.
  2228. if (hs->client_version == SSL3_VERSION &&
  2229. !ssl->s3->send_connection_binding) {
  2230. return 1;
  2231. }
  2232. CBB extensions;
  2233. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2234. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2235. return 0;
  2236. }
  2237. hs->extensions.sent = 0;
  2238. hs->custom_extensions.sent = 0;
  2239. for (size_t i = 0; i < kNumExtensions; i++) {
  2240. if (kExtensions[i].init != NULL) {
  2241. kExtensions[i].init(hs);
  2242. }
  2243. }
  2244. uint16_t grease_ext1 = 0;
  2245. if (ssl->ctx->grease_enabled) {
  2246. // Add a fake empty extension. See draft-davidben-tls-grease-01.
  2247. grease_ext1 = ssl_get_grease_value(ssl, ssl_grease_extension1);
  2248. if (!CBB_add_u16(&extensions, grease_ext1) ||
  2249. !CBB_add_u16(&extensions, 0 /* zero length */)) {
  2250. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2251. return 0;
  2252. }
  2253. }
  2254. for (size_t i = 0; i < kNumExtensions; i++) {
  2255. const size_t len_before = CBB_len(&extensions);
  2256. if (!kExtensions[i].add_clienthello(hs, &extensions)) {
  2257. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2258. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2259. return 0;
  2260. }
  2261. if (CBB_len(&extensions) != len_before) {
  2262. hs->extensions.sent |= (1u << i);
  2263. }
  2264. }
  2265. if (!custom_ext_add_clienthello(hs, &extensions)) {
  2266. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2267. return 0;
  2268. }
  2269. if (ssl->ctx->grease_enabled) {
  2270. // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
  2271. uint16_t grease_ext2 = ssl_get_grease_value(ssl, ssl_grease_extension2);
  2272. // The two fake extensions must not have the same value. GREASE values are
  2273. // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
  2274. // one.
  2275. if (grease_ext1 == grease_ext2) {
  2276. grease_ext2 ^= 0x1010;
  2277. }
  2278. if (!CBB_add_u16(&extensions, grease_ext2) ||
  2279. !CBB_add_u16(&extensions, 1 /* one byte length */) ||
  2280. !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
  2281. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2282. return 0;
  2283. }
  2284. }
  2285. if (!SSL_is_dtls(ssl)) {
  2286. size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
  2287. header_len += 2 + CBB_len(&extensions) + psk_extension_len;
  2288. if (header_len > 0xff && header_len < 0x200) {
  2289. // Add padding to workaround bugs in F5 terminators. See RFC 7685.
  2290. //
  2291. // NB: because this code works out the length of all existing extensions
  2292. // it MUST always appear last.
  2293. size_t padding_len = 0x200 - header_len;
  2294. // Extensions take at least four bytes to encode. Always include at least
  2295. // one byte of data if including the extension. WebSphere Application
  2296. // Server 7.0 is intolerant to the last extension being zero-length. See
  2297. // https://crbug.com/363583.
  2298. if (padding_len >= 4 + 1) {
  2299. padding_len -= 4;
  2300. } else {
  2301. padding_len = 1;
  2302. }
  2303. uint8_t *padding_bytes;
  2304. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
  2305. !CBB_add_u16(&extensions, padding_len) ||
  2306. !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
  2307. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2308. return 0;
  2309. }
  2310. OPENSSL_memset(padding_bytes, 0, padding_len);
  2311. }
  2312. }
  2313. // The PSK extension must be last, including after the padding.
  2314. if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
  2315. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2316. return 0;
  2317. }
  2318. // Discard empty extensions blocks.
  2319. if (CBB_len(&extensions) == 0) {
  2320. CBB_discard_child(out);
  2321. }
  2322. return CBB_flush(out);
  2323. }
  2324. int ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
  2325. SSL *const ssl = hs->ssl;
  2326. CBB extensions;
  2327. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2328. goto err;
  2329. }
  2330. for (unsigned i = 0; i < kNumExtensions; i++) {
  2331. if (!(hs->extensions.received & (1u << i))) {
  2332. // Don't send extensions that were not received.
  2333. continue;
  2334. }
  2335. if (!kExtensions[i].add_serverhello(hs, &extensions)) {
  2336. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2337. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2338. goto err;
  2339. }
  2340. }
  2341. if (!custom_ext_add_serverhello(hs, &extensions)) {
  2342. goto err;
  2343. }
  2344. // Discard empty extensions blocks before TLS 1.3.
  2345. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
  2346. CBB_len(&extensions) == 0) {
  2347. CBB_discard_child(out);
  2348. }
  2349. return CBB_flush(out);
  2350. err:
  2351. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2352. return 0;
  2353. }
  2354. static int ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2355. const SSL_CLIENT_HELLO *client_hello,
  2356. int *out_alert) {
  2357. SSL *const ssl = hs->ssl;
  2358. for (size_t i = 0; i < kNumExtensions; i++) {
  2359. if (kExtensions[i].init != NULL) {
  2360. kExtensions[i].init(hs);
  2361. }
  2362. }
  2363. hs->extensions.received = 0;
  2364. hs->custom_extensions.received = 0;
  2365. CBS extensions;
  2366. CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
  2367. while (CBS_len(&extensions) != 0) {
  2368. uint16_t type;
  2369. CBS extension;
  2370. // Decode the next extension.
  2371. if (!CBS_get_u16(&extensions, &type) ||
  2372. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2373. *out_alert = SSL_AD_DECODE_ERROR;
  2374. return 0;
  2375. }
  2376. // RFC 5746 made the existence of extensions in SSL 3.0 somewhat
  2377. // ambiguous. Ignore all but the renegotiation_info extension.
  2378. if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
  2379. continue;
  2380. }
  2381. unsigned ext_index;
  2382. const struct tls_extension *const ext =
  2383. tls_extension_find(&ext_index, type);
  2384. if (ext == NULL) {
  2385. if (!custom_ext_parse_clienthello(hs, out_alert, type, &extension)) {
  2386. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2387. return 0;
  2388. }
  2389. continue;
  2390. }
  2391. hs->extensions.received |= (1u << ext_index);
  2392. uint8_t alert = SSL_AD_DECODE_ERROR;
  2393. if (!ext->parse_clienthello(hs, &alert, &extension)) {
  2394. *out_alert = alert;
  2395. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2396. ERR_add_error_dataf("extension %u", (unsigned)type);
  2397. return 0;
  2398. }
  2399. }
  2400. for (size_t i = 0; i < kNumExtensions; i++) {
  2401. if (hs->extensions.received & (1u << i)) {
  2402. continue;
  2403. }
  2404. CBS *contents = NULL, fake_contents;
  2405. static const uint8_t kFakeRenegotiateExtension[] = {0};
  2406. if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
  2407. ssl_client_cipher_list_contains_cipher(client_hello,
  2408. SSL3_CK_SCSV & 0xffff)) {
  2409. // The renegotiation SCSV was received so pretend that we received a
  2410. // renegotiation extension.
  2411. CBS_init(&fake_contents, kFakeRenegotiateExtension,
  2412. sizeof(kFakeRenegotiateExtension));
  2413. contents = &fake_contents;
  2414. hs->extensions.received |= (1u << i);
  2415. }
  2416. // Extension wasn't observed so call the callback with a NULL
  2417. // parameter.
  2418. uint8_t alert = SSL_AD_DECODE_ERROR;
  2419. if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
  2420. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2421. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2422. *out_alert = alert;
  2423. return 0;
  2424. }
  2425. }
  2426. return 1;
  2427. }
  2428. int ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2429. const SSL_CLIENT_HELLO *client_hello) {
  2430. SSL *const ssl = hs->ssl;
  2431. int alert = SSL_AD_DECODE_ERROR;
  2432. if (ssl_scan_clienthello_tlsext(hs, client_hello, &alert) <= 0) {
  2433. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  2434. return 0;
  2435. }
  2436. if (ssl_check_clienthello_tlsext(hs) <= 0) {
  2437. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
  2438. return 0;
  2439. }
  2440. return 1;
  2441. }
  2442. static int ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
  2443. int *out_alert) {
  2444. SSL *const ssl = hs->ssl;
  2445. // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
  2446. if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  2447. return 1;
  2448. }
  2449. // Decode the extensions block and check it is valid.
  2450. CBS extensions;
  2451. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  2452. !tls1_check_duplicate_extensions(&extensions)) {
  2453. *out_alert = SSL_AD_DECODE_ERROR;
  2454. return 0;
  2455. }
  2456. uint32_t received = 0;
  2457. while (CBS_len(&extensions) != 0) {
  2458. uint16_t type;
  2459. CBS extension;
  2460. // Decode the next extension.
  2461. if (!CBS_get_u16(&extensions, &type) ||
  2462. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2463. *out_alert = SSL_AD_DECODE_ERROR;
  2464. return 0;
  2465. }
  2466. unsigned ext_index;
  2467. const struct tls_extension *const ext =
  2468. tls_extension_find(&ext_index, type);
  2469. if (ext == NULL) {
  2470. hs->received_custom_extension = 1;
  2471. if (!custom_ext_parse_serverhello(hs, out_alert, type, &extension)) {
  2472. return 0;
  2473. }
  2474. continue;
  2475. }
  2476. static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
  2477. "too many bits");
  2478. if (!(hs->extensions.sent & (1u << ext_index)) &&
  2479. type != TLSEXT_TYPE_renegotiate) {
  2480. // If the extension was never sent then it is illegal, except for the
  2481. // renegotiation extension which, in SSL 3.0, is signaled via SCSV.
  2482. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  2483. ERR_add_error_dataf("extension :%u", (unsigned)type);
  2484. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  2485. return 0;
  2486. }
  2487. received |= (1u << ext_index);
  2488. uint8_t alert = SSL_AD_DECODE_ERROR;
  2489. if (!ext->parse_serverhello(hs, &alert, &extension)) {
  2490. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2491. ERR_add_error_dataf("extension %u", (unsigned)type);
  2492. *out_alert = alert;
  2493. return 0;
  2494. }
  2495. }
  2496. for (size_t i = 0; i < kNumExtensions; i++) {
  2497. if (!(received & (1u << i))) {
  2498. // Extension wasn't observed so call the callback with a NULL
  2499. // parameter.
  2500. uint8_t alert = SSL_AD_DECODE_ERROR;
  2501. if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
  2502. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2503. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2504. *out_alert = alert;
  2505. return 0;
  2506. }
  2507. }
  2508. }
  2509. return 1;
  2510. }
  2511. static int ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
  2512. SSL *const ssl = hs->ssl;
  2513. int ret = SSL_TLSEXT_ERR_NOACK;
  2514. int al = SSL_AD_UNRECOGNIZED_NAME;
  2515. if (ssl->ctx->tlsext_servername_callback != 0) {
  2516. ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
  2517. ssl->ctx->tlsext_servername_arg);
  2518. } else if (ssl->session_ctx->tlsext_servername_callback != 0) {
  2519. ret = ssl->session_ctx->tlsext_servername_callback(
  2520. ssl, &al, ssl->session_ctx->tlsext_servername_arg);
  2521. }
  2522. switch (ret) {
  2523. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2524. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  2525. return -1;
  2526. case SSL_TLSEXT_ERR_NOACK:
  2527. hs->should_ack_sni = 0;
  2528. return 1;
  2529. default:
  2530. return 1;
  2531. }
  2532. }
  2533. int ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
  2534. SSL *const ssl = hs->ssl;
  2535. int alert = SSL_AD_DECODE_ERROR;
  2536. if (ssl_scan_serverhello_tlsext(hs, cbs, &alert) <= 0) {
  2537. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  2538. return 0;
  2539. }
  2540. return 1;
  2541. }
  2542. static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
  2543. uint8_t **out, size_t *out_len, EVP_CIPHER_CTX *cipher_ctx,
  2544. HMAC_CTX *hmac_ctx, const uint8_t *ticket, size_t ticket_len) {
  2545. size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
  2546. // Check the MAC at the end of the ticket.
  2547. uint8_t mac[EVP_MAX_MD_SIZE];
  2548. size_t mac_len = HMAC_size(hmac_ctx);
  2549. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
  2550. // The ticket must be large enough for key name, IV, data, and MAC.
  2551. return ssl_ticket_aead_ignore_ticket;
  2552. }
  2553. HMAC_Update(hmac_ctx, ticket, ticket_len - mac_len);
  2554. HMAC_Final(hmac_ctx, mac, NULL);
  2555. int mac_ok =
  2556. CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) == 0;
  2557. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2558. mac_ok = 1;
  2559. #endif
  2560. if (!mac_ok) {
  2561. return ssl_ticket_aead_ignore_ticket;
  2562. }
  2563. // Decrypt the session data.
  2564. const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
  2565. size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
  2566. mac_len;
  2567. UniquePtr<uint8_t> plaintext((uint8_t *)OPENSSL_malloc(ciphertext_len));
  2568. if (!plaintext) {
  2569. return ssl_ticket_aead_error;
  2570. }
  2571. size_t plaintext_len;
  2572. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2573. OPENSSL_memcpy(plaintext.get(), ciphertext, ciphertext_len);
  2574. plaintext_len = ciphertext_len;
  2575. #else
  2576. if (ciphertext_len >= INT_MAX) {
  2577. return ssl_ticket_aead_ignore_ticket;
  2578. }
  2579. int len1, len2;
  2580. if (!EVP_DecryptUpdate(cipher_ctx, plaintext.get(), &len1, ciphertext,
  2581. (int)ciphertext_len) ||
  2582. !EVP_DecryptFinal_ex(cipher_ctx, plaintext.get() + len1, &len2)) {
  2583. ERR_clear_error();
  2584. return ssl_ticket_aead_ignore_ticket;
  2585. }
  2586. plaintext_len = (size_t)(len1) + len2;
  2587. #endif
  2588. *out = plaintext.release();
  2589. *out_len = plaintext_len;
  2590. return ssl_ticket_aead_success;
  2591. }
  2592. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
  2593. SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
  2594. const uint8_t *ticket, size_t ticket_len) {
  2595. assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
  2596. ScopedEVP_CIPHER_CTX cipher_ctx;
  2597. ScopedHMAC_CTX hmac_ctx;
  2598. const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
  2599. int cb_ret = ssl->session_ctx->tlsext_ticket_key_cb(
  2600. ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, cipher_ctx.get(),
  2601. hmac_ctx.get(), 0 /* decrypt */);
  2602. if (cb_ret < 0) {
  2603. return ssl_ticket_aead_error;
  2604. } else if (cb_ret == 0) {
  2605. return ssl_ticket_aead_ignore_ticket;
  2606. } else if (cb_ret == 2) {
  2607. *out_renew_ticket = 1;
  2608. } else {
  2609. assert(cb_ret == 1);
  2610. }
  2611. return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
  2612. hmac_ctx.get(), ticket, ticket_len);
  2613. }
  2614. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
  2615. SSL *ssl, uint8_t **out, size_t *out_len, const uint8_t *ticket,
  2616. size_t ticket_len) {
  2617. assert(ticket_len >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
  2618. SSL_CTX *ctx = ssl->session_ctx;
  2619. // Rotate the ticket key if necessary.
  2620. if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
  2621. return ssl_ticket_aead_error;
  2622. }
  2623. // Pick the matching ticket key and decrypt.
  2624. ScopedEVP_CIPHER_CTX cipher_ctx;
  2625. ScopedHMAC_CTX hmac_ctx;
  2626. {
  2627. MutexReadLock lock(&ctx->lock);
  2628. const tlsext_ticket_key *key;
  2629. if (ctx->tlsext_ticket_key_current &&
  2630. !OPENSSL_memcmp(ctx->tlsext_ticket_key_current->name, ticket,
  2631. SSL_TICKET_KEY_NAME_LEN)) {
  2632. key = ctx->tlsext_ticket_key_current;
  2633. } else if (ctx->tlsext_ticket_key_prev &&
  2634. !OPENSSL_memcmp(ctx->tlsext_ticket_key_prev->name, ticket,
  2635. SSL_TICKET_KEY_NAME_LEN)) {
  2636. key = ctx->tlsext_ticket_key_prev;
  2637. } else {
  2638. return ssl_ticket_aead_ignore_ticket;
  2639. }
  2640. const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
  2641. if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
  2642. tlsext_tick_md(), NULL) ||
  2643. !EVP_DecryptInit_ex(cipher_ctx.get(), EVP_aes_128_cbc(), NULL,
  2644. key->aes_key, iv)) {
  2645. return ssl_ticket_aead_error;
  2646. }
  2647. }
  2648. return decrypt_ticket_with_cipher_ctx(out, out_len, cipher_ctx.get(),
  2649. hmac_ctx.get(), ticket, ticket_len);
  2650. }
  2651. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
  2652. SSL *ssl, uint8_t **out, size_t *out_len, int *out_renew_ticket,
  2653. const uint8_t *ticket, size_t ticket_len) {
  2654. uint8_t *plaintext = (uint8_t *)OPENSSL_malloc(ticket_len);
  2655. if (plaintext == NULL) {
  2656. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  2657. return ssl_ticket_aead_error;
  2658. }
  2659. size_t plaintext_len;
  2660. const enum ssl_ticket_aead_result_t result =
  2661. ssl->session_ctx->ticket_aead_method->open(
  2662. ssl, plaintext, &plaintext_len, ticket_len, ticket, ticket_len);
  2663. if (result == ssl_ticket_aead_success) {
  2664. *out = plaintext;
  2665. plaintext = NULL;
  2666. *out_len = plaintext_len;
  2667. }
  2668. OPENSSL_free(plaintext);
  2669. return result;
  2670. }
  2671. enum ssl_ticket_aead_result_t ssl_process_ticket(
  2672. SSL *ssl, UniquePtr<SSL_SESSION> *out_session, int *out_renew_ticket,
  2673. const uint8_t *ticket, size_t ticket_len, const uint8_t *session_id,
  2674. size_t session_id_len) {
  2675. *out_renew_ticket = 0;
  2676. out_session->reset();
  2677. if ((SSL_get_options(ssl) & SSL_OP_NO_TICKET) ||
  2678. session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  2679. return ssl_ticket_aead_ignore_ticket;
  2680. }
  2681. uint8_t *plaintext = NULL;
  2682. size_t plaintext_len;
  2683. enum ssl_ticket_aead_result_t result;
  2684. if (ssl->session_ctx->ticket_aead_method != NULL) {
  2685. result = ssl_decrypt_ticket_with_method(
  2686. ssl, &plaintext, &plaintext_len, out_renew_ticket, ticket, ticket_len);
  2687. } else {
  2688. // Ensure there is room for the key name and the largest IV
  2689. // |tlsext_ticket_key_cb| may try to consume. The real limit may be lower,
  2690. // but the maximum IV length should be well under the minimum size for the
  2691. // session material and HMAC.
  2692. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
  2693. return ssl_ticket_aead_ignore_ticket;
  2694. }
  2695. if (ssl->session_ctx->tlsext_ticket_key_cb != NULL) {
  2696. result = ssl_decrypt_ticket_with_cb(ssl, &plaintext, &plaintext_len,
  2697. out_renew_ticket, ticket, ticket_len);
  2698. } else {
  2699. result = ssl_decrypt_ticket_with_ticket_keys(
  2700. ssl, &plaintext, &plaintext_len, ticket, ticket_len);
  2701. }
  2702. }
  2703. if (result != ssl_ticket_aead_success) {
  2704. return result;
  2705. }
  2706. // Decode the session.
  2707. UniquePtr<SSL_SESSION> session(
  2708. SSL_SESSION_from_bytes(plaintext, plaintext_len, ssl->ctx));
  2709. OPENSSL_free(plaintext);
  2710. if (!session) {
  2711. ERR_clear_error(); // Don't leave an error on the queue.
  2712. return ssl_ticket_aead_ignore_ticket;
  2713. }
  2714. // Copy the client's session ID into the new session, to denote the ticket has
  2715. // been accepted.
  2716. OPENSSL_memcpy(session->session_id, session_id, session_id_len);
  2717. session->session_id_length = session_id_len;
  2718. *out_session = std::move(session);
  2719. return ssl_ticket_aead_success;
  2720. }
  2721. int tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
  2722. // Extension ignored for inappropriate versions
  2723. if (ssl3_protocol_version(hs->ssl) < TLS1_2_VERSION) {
  2724. return 1;
  2725. }
  2726. OPENSSL_free(hs->peer_sigalgs);
  2727. hs->peer_sigalgs = NULL;
  2728. hs->num_peer_sigalgs = 0;
  2729. size_t num_sigalgs = CBS_len(in_sigalgs);
  2730. if (num_sigalgs % 2 != 0) {
  2731. return 0;
  2732. }
  2733. num_sigalgs /= 2;
  2734. // supported_signature_algorithms in the certificate request is
  2735. // allowed to be empty.
  2736. if (num_sigalgs == 0) {
  2737. return 1;
  2738. }
  2739. // This multiplication doesn't overflow because sizeof(uint16_t) is two
  2740. // and we just divided |num_sigalgs| by two.
  2741. hs->peer_sigalgs = (uint16_t *)OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
  2742. if (hs->peer_sigalgs == NULL) {
  2743. return 0;
  2744. }
  2745. hs->num_peer_sigalgs = num_sigalgs;
  2746. CBS sigalgs;
  2747. CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
  2748. for (size_t i = 0; i < num_sigalgs; i++) {
  2749. if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
  2750. return 0;
  2751. }
  2752. }
  2753. return 1;
  2754. }
  2755. int tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
  2756. switch (EVP_PKEY_id(pkey)) {
  2757. case EVP_PKEY_RSA:
  2758. *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
  2759. return 1;
  2760. case EVP_PKEY_EC:
  2761. *out = SSL_SIGN_ECDSA_SHA1;
  2762. return 1;
  2763. default:
  2764. return 0;
  2765. }
  2766. }
  2767. int tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
  2768. SSL *const ssl = hs->ssl;
  2769. CERT *cert = ssl->cert;
  2770. // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
  2771. // handshake.
  2772. if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
  2773. if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
  2774. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  2775. return 0;
  2776. }
  2777. return 1;
  2778. }
  2779. const uint16_t *sigalgs = cert->sigalgs;
  2780. size_t num_sigalgs = cert->num_sigalgs;
  2781. if (sigalgs == NULL) {
  2782. sigalgs = kSignSignatureAlgorithms;
  2783. num_sigalgs = OPENSSL_ARRAY_SIZE(kSignSignatureAlgorithms);
  2784. }
  2785. const uint16_t *peer_sigalgs = hs->peer_sigalgs;
  2786. size_t num_peer_sigalgs = hs->num_peer_sigalgs;
  2787. if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  2788. // If the client didn't specify any signature_algorithms extension then
  2789. // we can assume that it supports SHA1. See
  2790. // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  2791. static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
  2792. SSL_SIGN_ECDSA_SHA1};
  2793. peer_sigalgs = kDefaultPeerAlgorithms;
  2794. num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
  2795. }
  2796. for (size_t i = 0; i < num_sigalgs; i++) {
  2797. uint16_t sigalg = sigalgs[i];
  2798. // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
  2799. // negotiated.
  2800. if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
  2801. !ssl_private_key_supports_signature_algorithm(hs, sigalgs[i])) {
  2802. continue;
  2803. }
  2804. for (size_t j = 0; j < num_peer_sigalgs; j++) {
  2805. if (sigalg == peer_sigalgs[j]) {
  2806. *out = sigalg;
  2807. return 1;
  2808. }
  2809. }
  2810. }
  2811. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  2812. return 0;
  2813. }
  2814. int tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
  2815. SSL *const ssl = hs->ssl;
  2816. // A Channel ID handshake message is structured to contain multiple
  2817. // extensions, but the only one that can be present is Channel ID.
  2818. uint16_t extension_type;
  2819. CBS channel_id = msg.body, extension;
  2820. if (!CBS_get_u16(&channel_id, &extension_type) ||
  2821. !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
  2822. CBS_len(&channel_id) != 0 ||
  2823. extension_type != TLSEXT_TYPE_channel_id ||
  2824. CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
  2825. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  2826. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  2827. return 0;
  2828. }
  2829. UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  2830. if (!p256) {
  2831. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
  2832. return 0;
  2833. }
  2834. UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
  2835. UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
  2836. if (!sig || !x || !y) {
  2837. return 0;
  2838. }
  2839. const uint8_t *p = CBS_data(&extension);
  2840. if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
  2841. BN_bin2bn(p + 32, 32, y.get()) == NULL ||
  2842. BN_bin2bn(p + 64, 32, sig->r) == NULL ||
  2843. BN_bin2bn(p + 96, 32, sig->s) == NULL) {
  2844. return 0;
  2845. }
  2846. UniquePtr<EC_KEY> key(EC_KEY_new());
  2847. UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
  2848. if (!key || !point ||
  2849. !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
  2850. y.get(), nullptr) ||
  2851. !EC_KEY_set_group(key.get(), p256.get()) ||
  2852. !EC_KEY_set_public_key(key.get(), point.get())) {
  2853. return 0;
  2854. }
  2855. uint8_t digest[EVP_MAX_MD_SIZE];
  2856. size_t digest_len;
  2857. if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
  2858. return 0;
  2859. }
  2860. int sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
  2861. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2862. sig_ok = 1;
  2863. #endif
  2864. if (!sig_ok) {
  2865. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
  2866. ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  2867. ssl->s3->tlsext_channel_id_valid = 0;
  2868. return 0;
  2869. }
  2870. OPENSSL_memcpy(ssl->s3->tlsext_channel_id, p, 64);
  2871. return 1;
  2872. }
  2873. int tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
  2874. SSL *const ssl = hs->ssl;
  2875. uint8_t digest[EVP_MAX_MD_SIZE];
  2876. size_t digest_len;
  2877. if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
  2878. return 0;
  2879. }
  2880. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(ssl->tlsext_channel_id_private);
  2881. if (ec_key == NULL) {
  2882. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2883. return 0;
  2884. }
  2885. int ret = 0;
  2886. BIGNUM *x = BN_new();
  2887. BIGNUM *y = BN_new();
  2888. ECDSA_SIG *sig = NULL;
  2889. if (x == NULL || y == NULL ||
  2890. !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
  2891. EC_KEY_get0_public_key(ec_key),
  2892. x, y, NULL)) {
  2893. goto err;
  2894. }
  2895. sig = ECDSA_do_sign(digest, digest_len, ec_key);
  2896. if (sig == NULL) {
  2897. goto err;
  2898. }
  2899. CBB child;
  2900. if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
  2901. !CBB_add_u16_length_prefixed(cbb, &child) ||
  2902. !BN_bn2cbb_padded(&child, 32, x) ||
  2903. !BN_bn2cbb_padded(&child, 32, y) ||
  2904. !BN_bn2cbb_padded(&child, 32, sig->r) ||
  2905. !BN_bn2cbb_padded(&child, 32, sig->s) ||
  2906. !CBB_flush(cbb)) {
  2907. goto err;
  2908. }
  2909. ret = 1;
  2910. err:
  2911. BN_free(x);
  2912. BN_free(y);
  2913. ECDSA_SIG_free(sig);
  2914. return ret;
  2915. }
  2916. int tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
  2917. SSL *const ssl = hs->ssl;
  2918. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  2919. uint8_t *msg;
  2920. size_t msg_len;
  2921. if (!tls13_get_cert_verify_signature_input(hs, &msg, &msg_len,
  2922. ssl_cert_verify_channel_id)) {
  2923. return 0;
  2924. }
  2925. SHA256(msg, msg_len, out);
  2926. *out_len = SHA256_DIGEST_LENGTH;
  2927. OPENSSL_free(msg);
  2928. return 1;
  2929. }
  2930. SHA256_CTX ctx;
  2931. SHA256_Init(&ctx);
  2932. static const char kClientIDMagic[] = "TLS Channel ID signature";
  2933. SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
  2934. if (ssl->session != NULL) {
  2935. static const char kResumptionMagic[] = "Resumption";
  2936. SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
  2937. if (ssl->session->original_handshake_hash_len == 0) {
  2938. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2939. return 0;
  2940. }
  2941. SHA256_Update(&ctx, ssl->session->original_handshake_hash,
  2942. ssl->session->original_handshake_hash_len);
  2943. }
  2944. uint8_t hs_hash[EVP_MAX_MD_SIZE];
  2945. size_t hs_hash_len;
  2946. if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
  2947. return 0;
  2948. }
  2949. SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
  2950. SHA256_Final(out, &ctx);
  2951. *out_len = SHA256_DIGEST_LENGTH;
  2952. return 1;
  2953. }
  2954. // tls1_record_handshake_hashes_for_channel_id records the current handshake
  2955. // hashes in |hs->new_session| so that Channel ID resumptions can sign that
  2956. // data.
  2957. int tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
  2958. SSL *const ssl = hs->ssl;
  2959. // This function should never be called for a resumed session because the
  2960. // handshake hashes that we wish to record are for the original, full
  2961. // handshake.
  2962. if (ssl->session != NULL) {
  2963. return 0;
  2964. }
  2965. static_assert(
  2966. sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
  2967. "original_handshake_hash is too small");
  2968. size_t digest_len;
  2969. if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
  2970. &digest_len)) {
  2971. return 0;
  2972. }
  2973. static_assert(EVP_MAX_MD_SIZE <= 0xff,
  2974. "EVP_MAX_MD_SIZE does not fit in uint8_t");
  2975. hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
  2976. return 1;
  2977. }
  2978. int ssl_do_channel_id_callback(SSL *ssl) {
  2979. if (ssl->tlsext_channel_id_private != NULL ||
  2980. ssl->ctx->channel_id_cb == NULL) {
  2981. return 1;
  2982. }
  2983. EVP_PKEY *key = NULL;
  2984. ssl->ctx->channel_id_cb(ssl, &key);
  2985. if (key == NULL) {
  2986. // The caller should try again later.
  2987. return 1;
  2988. }
  2989. int ret = SSL_set1_tls_channel_id(ssl, key);
  2990. EVP_PKEY_free(key);
  2991. return ret;
  2992. }
  2993. int ssl_is_sct_list_valid(const CBS *contents) {
  2994. // Shallow parse the SCT list for sanity. By the RFC
  2995. // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
  2996. // of the SCTs may be empty.
  2997. CBS copy = *contents;
  2998. CBS sct_list;
  2999. if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
  3000. CBS_len(&copy) != 0 ||
  3001. CBS_len(&sct_list) == 0) {
  3002. return 0;
  3003. }
  3004. while (CBS_len(&sct_list) > 0) {
  3005. CBS sct;
  3006. if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
  3007. CBS_len(&sct) == 0) {
  3008. return 0;
  3009. }
  3010. }
  3011. return 1;
  3012. }
  3013. } // namespace bssl
  3014. using namespace bssl;
  3015. int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
  3016. uint16_t extension_type,
  3017. const uint8_t **out_data,
  3018. size_t *out_len) {
  3019. CBS cbs;
  3020. if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
  3021. return 0;
  3022. }
  3023. *out_data = CBS_data(&cbs);
  3024. *out_len = CBS_len(&cbs);
  3025. return 1;
  3026. }
  3027. void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
  3028. ctx->ed25519_enabled = !!enabled;
  3029. }
  3030. int SSL_extension_supported(unsigned extension_value) {
  3031. uint32_t index;
  3032. return extension_value == TLSEXT_TYPE_padding ||
  3033. tls_extension_find(&index, extension_value) != NULL;
  3034. }