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.
 
 
 
 
 
 

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