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

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