25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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