25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

3576 satır
103 KiB

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