Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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