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.
 
 
 
 
 
 

3864 linhas
115 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. BSSL_NAMESPACE_BEGIN
  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->config->verify_sigalgs.empty()) {
  435. ret.list = ssl->config->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/rfc8446#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/rfc8446#section-4.2.11
  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 RFC 8446 section 4.1.4, skip offering the session if the selected
  1567. // cipher in HelloRetryRequest does not match. This avoids performing the
  1568. // 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/rfc8446#section-4.2.9
  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/rfc8446#section-4.2.10
  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/rfc8446#section-4.2.8
  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/rfc8446#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/rfc8446#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. // Supported Groups
  1986. //
  1987. // https://tools.ietf.org/html/rfc4492#section-5.1.1
  1988. // https://tools.ietf.org/html/rfc8446#section-4.2.7
  1989. static bool ext_supported_groups_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  1990. SSL *const ssl = hs->ssl;
  1991. CBB contents, groups_bytes;
  1992. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
  1993. !CBB_add_u16_length_prefixed(out, &contents) ||
  1994. !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
  1995. return false;
  1996. }
  1997. // Add a fake group. See draft-davidben-tls-grease-01.
  1998. if (ssl->ctx->grease_enabled &&
  1999. !CBB_add_u16(&groups_bytes,
  2000. ssl_get_grease_value(hs, ssl_grease_group))) {
  2001. return false;
  2002. }
  2003. for (uint16_t group : tls1_get_grouplist(hs)) {
  2004. if (!CBB_add_u16(&groups_bytes, group)) {
  2005. return false;
  2006. }
  2007. }
  2008. return CBB_flush(out);
  2009. }
  2010. static bool ext_supported_groups_parse_serverhello(SSL_HANDSHAKE *hs,
  2011. uint8_t *out_alert,
  2012. CBS *contents) {
  2013. // This extension is not expected to be echoed by servers in TLS 1.2, but some
  2014. // BigIP servers send it nonetheless, so do not enforce this.
  2015. return true;
  2016. }
  2017. static bool parse_u16_array(const CBS *cbs, Array<uint16_t> *out) {
  2018. CBS copy = *cbs;
  2019. if ((CBS_len(&copy) & 1) != 0) {
  2020. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  2021. return false;
  2022. }
  2023. Array<uint16_t> ret;
  2024. if (!ret.Init(CBS_len(&copy) / 2)) {
  2025. return false;
  2026. }
  2027. for (size_t i = 0; i < ret.size(); i++) {
  2028. if (!CBS_get_u16(&copy, &ret[i])) {
  2029. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2030. return false;
  2031. }
  2032. }
  2033. assert(CBS_len(&copy) == 0);
  2034. *out = std::move(ret);
  2035. return 1;
  2036. }
  2037. static bool ext_supported_groups_parse_clienthello(SSL_HANDSHAKE *hs,
  2038. uint8_t *out_alert,
  2039. CBS *contents) {
  2040. if (contents == NULL) {
  2041. return true;
  2042. }
  2043. CBS supported_group_list;
  2044. if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
  2045. CBS_len(&supported_group_list) == 0 ||
  2046. CBS_len(contents) != 0 ||
  2047. !parse_u16_array(&supported_group_list, &hs->peer_supported_group_list)) {
  2048. return false;
  2049. }
  2050. return true;
  2051. }
  2052. // Token Binding
  2053. //
  2054. // https://tools.ietf.org/html/draft-ietf-tokbind-negotiation-10
  2055. // The Token Binding version number currently matches the draft number of
  2056. // draft-ietf-tokbind-protocol, and when published as an RFC it will be 0x0100.
  2057. // Since there are no wire changes to the protocol from draft 13 through the
  2058. // current draft (16), this implementation supports all versions in that range.
  2059. static uint16_t kTokenBindingMaxVersion = 16;
  2060. static uint16_t kTokenBindingMinVersion = 13;
  2061. static bool ext_token_binding_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  2062. SSL *const ssl = hs->ssl;
  2063. if (hs->config->token_binding_params.empty() || SSL_is_dtls(ssl)) {
  2064. return true;
  2065. }
  2066. CBB contents, params;
  2067. if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
  2068. !CBB_add_u16_length_prefixed(out, &contents) ||
  2069. !CBB_add_u16(&contents, kTokenBindingMaxVersion) ||
  2070. !CBB_add_u8_length_prefixed(&contents, &params) ||
  2071. !CBB_add_bytes(&params, hs->config->token_binding_params.data(),
  2072. hs->config->token_binding_params.size()) ||
  2073. !CBB_flush(out)) {
  2074. return false;
  2075. }
  2076. return true;
  2077. }
  2078. static bool ext_token_binding_parse_serverhello(SSL_HANDSHAKE *hs,
  2079. uint8_t *out_alert,
  2080. CBS *contents) {
  2081. SSL *const ssl = hs->ssl;
  2082. if (contents == nullptr) {
  2083. return true;
  2084. }
  2085. CBS params_list;
  2086. uint16_t version;
  2087. uint8_t param;
  2088. if (!CBS_get_u16(contents, &version) ||
  2089. !CBS_get_u8_length_prefixed(contents, &params_list) ||
  2090. !CBS_get_u8(&params_list, &param) ||
  2091. CBS_len(&params_list) > 0 ||
  2092. CBS_len(contents) > 0) {
  2093. *out_alert = SSL_AD_DECODE_ERROR;
  2094. return false;
  2095. }
  2096. // The server-negotiated version must be less than or equal to our version.
  2097. if (version > kTokenBindingMaxVersion) {
  2098. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  2099. return false;
  2100. }
  2101. // If the server-selected version is less than what we support, then Token
  2102. // Binding wasn't negotiated (but the extension was parsed successfully).
  2103. if (version < kTokenBindingMinVersion) {
  2104. return true;
  2105. }
  2106. for (uint8_t config_param : hs->config->token_binding_params) {
  2107. if (param == config_param) {
  2108. ssl->s3->negotiated_token_binding_param = param;
  2109. ssl->s3->token_binding_negotiated = true;
  2110. return true;
  2111. }
  2112. }
  2113. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  2114. return false;
  2115. }
  2116. // select_tb_param looks for the first token binding param in
  2117. // |hs->ssl->token_binding_params| that is also in |params| and puts it in
  2118. // |hs->ssl->negotiated_token_binding_param|. It returns true if a token binding
  2119. // param is found, and false otherwise.
  2120. static bool select_tb_param(SSL_HANDSHAKE *hs,
  2121. Span<const uint8_t> peer_params) {
  2122. for (uint8_t tb_param : hs->config->token_binding_params) {
  2123. for (uint8_t peer_param : peer_params) {
  2124. if (tb_param == peer_param) {
  2125. hs->ssl->s3->negotiated_token_binding_param = tb_param;
  2126. return true;
  2127. }
  2128. }
  2129. }
  2130. return false;
  2131. }
  2132. static bool ext_token_binding_parse_clienthello(SSL_HANDSHAKE *hs,
  2133. uint8_t *out_alert,
  2134. CBS *contents) {
  2135. SSL *const ssl = hs->ssl;
  2136. if (contents == nullptr || hs->config->token_binding_params.empty()) {
  2137. return true;
  2138. }
  2139. CBS params;
  2140. uint16_t version;
  2141. if (!CBS_get_u16(contents, &version) ||
  2142. !CBS_get_u8_length_prefixed(contents, &params) ||
  2143. CBS_len(&params) == 0 ||
  2144. CBS_len(contents) > 0) {
  2145. *out_alert = SSL_AD_DECODE_ERROR;
  2146. return false;
  2147. }
  2148. // If the client-selected version is less than what we support, then Token
  2149. // Binding wasn't negotiated (but the extension was parsed successfully).
  2150. if (version < kTokenBindingMinVersion) {
  2151. return true;
  2152. }
  2153. // If the client-selected version is higher than we support, use our max
  2154. // version. Otherwise, use the client's version.
  2155. hs->negotiated_token_binding_version =
  2156. std::min(version, kTokenBindingMaxVersion);
  2157. if (!select_tb_param(hs, params)) {
  2158. return true;
  2159. }
  2160. ssl->s3->token_binding_negotiated = true;
  2161. return true;
  2162. }
  2163. static bool ext_token_binding_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  2164. SSL *const ssl = hs->ssl;
  2165. if (!ssl->s3->token_binding_negotiated) {
  2166. return true;
  2167. }
  2168. CBB contents, params;
  2169. if (!CBB_add_u16(out, TLSEXT_TYPE_token_binding) ||
  2170. !CBB_add_u16_length_prefixed(out, &contents) ||
  2171. !CBB_add_u16(&contents, hs->negotiated_token_binding_version) ||
  2172. !CBB_add_u8_length_prefixed(&contents, &params) ||
  2173. !CBB_add_u8(&params, ssl->s3->negotiated_token_binding_param) ||
  2174. !CBB_flush(out)) {
  2175. return false;
  2176. }
  2177. return true;
  2178. }
  2179. // QUIC Transport Parameters
  2180. static bool ext_quic_transport_params_add_clienthello(SSL_HANDSHAKE *hs,
  2181. CBB *out) {
  2182. if (hs->config->quic_transport_params.empty() ||
  2183. hs->max_version <= TLS1_2_VERSION) {
  2184. return true;
  2185. }
  2186. CBB contents;
  2187. if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
  2188. !CBB_add_u16_length_prefixed(out, &contents) ||
  2189. !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
  2190. hs->config->quic_transport_params.size()) ||
  2191. !CBB_flush(out)) {
  2192. return false;
  2193. }
  2194. return true;
  2195. }
  2196. static bool ext_quic_transport_params_parse_serverhello(SSL_HANDSHAKE *hs,
  2197. uint8_t *out_alert,
  2198. CBS *contents) {
  2199. SSL *const ssl = hs->ssl;
  2200. if (contents == nullptr) {
  2201. return true;
  2202. }
  2203. // QUIC requires TLS 1.3.
  2204. if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  2205. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  2206. return false;
  2207. }
  2208. return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
  2209. }
  2210. static bool ext_quic_transport_params_parse_clienthello(SSL_HANDSHAKE *hs,
  2211. uint8_t *out_alert,
  2212. CBS *contents) {
  2213. SSL *const ssl = hs->ssl;
  2214. if (!contents || hs->config->quic_transport_params.empty()) {
  2215. return true;
  2216. }
  2217. // Ignore the extension before TLS 1.3.
  2218. if (ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  2219. return true;
  2220. }
  2221. return ssl->s3->peer_quic_transport_params.CopyFrom(*contents);
  2222. }
  2223. static bool ext_quic_transport_params_add_serverhello(SSL_HANDSHAKE *hs,
  2224. CBB *out) {
  2225. if (hs->config->quic_transport_params.empty()) {
  2226. return true;
  2227. }
  2228. CBB contents;
  2229. if (!CBB_add_u16(out, TLSEXT_TYPE_quic_transport_parameters) ||
  2230. !CBB_add_u16_length_prefixed(out, &contents) ||
  2231. !CBB_add_bytes(&contents, hs->config->quic_transport_params.data(),
  2232. hs->config->quic_transport_params.size()) ||
  2233. !CBB_flush(out)) {
  2234. return false;
  2235. }
  2236. return true;
  2237. }
  2238. // Certificate compression
  2239. static bool cert_compression_add_clienthello(SSL_HANDSHAKE *hs, CBB *out) {
  2240. bool first = true;
  2241. CBB contents, algs;
  2242. for (const auto *alg : hs->ssl->ctx->cert_compression_algs.get()) {
  2243. if (alg->decompress == nullptr) {
  2244. continue;
  2245. }
  2246. if (first && (!CBB_add_u16(out, TLSEXT_TYPE_cert_compression) ||
  2247. !CBB_add_u16_length_prefixed(out, &contents) ||
  2248. !CBB_add_u8_length_prefixed(&contents, &algs))) {
  2249. return false;
  2250. }
  2251. first = false;
  2252. if (!CBB_add_u16(&algs, alg->alg_id)) {
  2253. return false;
  2254. }
  2255. }
  2256. return first || CBB_flush(out);
  2257. }
  2258. static bool cert_compression_parse_serverhello(SSL_HANDSHAKE *hs,
  2259. uint8_t *out_alert,
  2260. CBS *contents) {
  2261. if (contents == nullptr) {
  2262. return true;
  2263. }
  2264. // The server may not echo this extension. Any server to client negotiation is
  2265. // advertised in the CertificateRequest message.
  2266. return false;
  2267. }
  2268. static bool cert_compression_parse_clienthello(SSL_HANDSHAKE *hs,
  2269. uint8_t *out_alert,
  2270. CBS *contents) {
  2271. if (contents == nullptr) {
  2272. return true;
  2273. }
  2274. const size_t num_algs =
  2275. sk_CertCompressionAlg_num(hs->ssl->ctx->cert_compression_algs.get());
  2276. CBS alg_ids;
  2277. if (!CBS_get_u8_length_prefixed(contents, &alg_ids) ||
  2278. CBS_len(contents) != 0 ||
  2279. CBS_len(&alg_ids) == 0 ||
  2280. CBS_len(&alg_ids) % 2 == 1) {
  2281. return false;
  2282. }
  2283. const size_t num_given_alg_ids = CBS_len(&alg_ids) / 2;
  2284. Array<uint16_t> given_alg_ids;
  2285. if (!given_alg_ids.Init(num_given_alg_ids)) {
  2286. return false;
  2287. }
  2288. size_t best_index = num_algs;
  2289. size_t given_alg_idx = 0;
  2290. while (CBS_len(&alg_ids) > 0) {
  2291. uint16_t alg_id;
  2292. if (!CBS_get_u16(&alg_ids, &alg_id)) {
  2293. return false;
  2294. }
  2295. given_alg_ids[given_alg_idx++] = alg_id;
  2296. for (size_t i = 0; i < num_algs; i++) {
  2297. const auto *alg = sk_CertCompressionAlg_value(
  2298. hs->ssl->ctx->cert_compression_algs.get(), i);
  2299. if (alg->alg_id == alg_id && alg->compress != nullptr) {
  2300. if (i < best_index) {
  2301. best_index = i;
  2302. }
  2303. break;
  2304. }
  2305. }
  2306. }
  2307. qsort(given_alg_ids.data(), given_alg_ids.size(), sizeof(uint16_t),
  2308. compare_uint16_t);
  2309. for (size_t i = 1; i < num_given_alg_ids; i++) {
  2310. if (given_alg_ids[i - 1] == given_alg_ids[i]) {
  2311. return false;
  2312. }
  2313. }
  2314. if (best_index < num_algs &&
  2315. ssl_protocol_version(hs->ssl) >= TLS1_3_VERSION) {
  2316. hs->cert_compression_negotiated = true;
  2317. hs->cert_compression_alg_id =
  2318. sk_CertCompressionAlg_value(hs->ssl->ctx->cert_compression_algs.get(),
  2319. best_index)
  2320. ->alg_id;
  2321. }
  2322. return true;
  2323. }
  2324. static bool cert_compression_add_serverhello(SSL_HANDSHAKE *hs, CBB *out) {
  2325. return true;
  2326. }
  2327. // kExtensions contains all the supported extensions.
  2328. static const struct tls_extension kExtensions[] = {
  2329. {
  2330. TLSEXT_TYPE_server_name,
  2331. NULL,
  2332. ext_sni_add_clienthello,
  2333. ext_sni_parse_serverhello,
  2334. ext_sni_parse_clienthello,
  2335. ext_sni_add_serverhello,
  2336. },
  2337. {
  2338. TLSEXT_TYPE_extended_master_secret,
  2339. NULL,
  2340. ext_ems_add_clienthello,
  2341. ext_ems_parse_serverhello,
  2342. ext_ems_parse_clienthello,
  2343. ext_ems_add_serverhello,
  2344. },
  2345. {
  2346. TLSEXT_TYPE_renegotiate,
  2347. NULL,
  2348. ext_ri_add_clienthello,
  2349. ext_ri_parse_serverhello,
  2350. ext_ri_parse_clienthello,
  2351. ext_ri_add_serverhello,
  2352. },
  2353. {
  2354. TLSEXT_TYPE_supported_groups,
  2355. NULL,
  2356. ext_supported_groups_add_clienthello,
  2357. ext_supported_groups_parse_serverhello,
  2358. ext_supported_groups_parse_clienthello,
  2359. dont_add_serverhello,
  2360. },
  2361. {
  2362. TLSEXT_TYPE_ec_point_formats,
  2363. NULL,
  2364. ext_ec_point_add_clienthello,
  2365. ext_ec_point_parse_serverhello,
  2366. ext_ec_point_parse_clienthello,
  2367. ext_ec_point_add_serverhello,
  2368. },
  2369. {
  2370. TLSEXT_TYPE_session_ticket,
  2371. NULL,
  2372. ext_ticket_add_clienthello,
  2373. ext_ticket_parse_serverhello,
  2374. // Ticket extension client parsing is handled in ssl_session.c
  2375. ignore_parse_clienthello,
  2376. ext_ticket_add_serverhello,
  2377. },
  2378. {
  2379. TLSEXT_TYPE_application_layer_protocol_negotiation,
  2380. NULL,
  2381. ext_alpn_add_clienthello,
  2382. ext_alpn_parse_serverhello,
  2383. // ALPN is negotiated late in |ssl_negotiate_alpn|.
  2384. ignore_parse_clienthello,
  2385. ext_alpn_add_serverhello,
  2386. },
  2387. {
  2388. TLSEXT_TYPE_status_request,
  2389. NULL,
  2390. ext_ocsp_add_clienthello,
  2391. ext_ocsp_parse_serverhello,
  2392. ext_ocsp_parse_clienthello,
  2393. ext_ocsp_add_serverhello,
  2394. },
  2395. {
  2396. TLSEXT_TYPE_signature_algorithms,
  2397. NULL,
  2398. ext_sigalgs_add_clienthello,
  2399. forbid_parse_serverhello,
  2400. ext_sigalgs_parse_clienthello,
  2401. dont_add_serverhello,
  2402. },
  2403. {
  2404. TLSEXT_TYPE_signature_algorithms_cert,
  2405. NULL,
  2406. ext_sigalgs_cert_add_clienthello,
  2407. forbid_parse_serverhello,
  2408. ignore_parse_clienthello,
  2409. dont_add_serverhello,
  2410. },
  2411. {
  2412. TLSEXT_TYPE_next_proto_neg,
  2413. NULL,
  2414. ext_npn_add_clienthello,
  2415. ext_npn_parse_serverhello,
  2416. ext_npn_parse_clienthello,
  2417. ext_npn_add_serverhello,
  2418. },
  2419. {
  2420. TLSEXT_TYPE_certificate_timestamp,
  2421. NULL,
  2422. ext_sct_add_clienthello,
  2423. ext_sct_parse_serverhello,
  2424. ext_sct_parse_clienthello,
  2425. ext_sct_add_serverhello,
  2426. },
  2427. {
  2428. TLSEXT_TYPE_channel_id,
  2429. ext_channel_id_init,
  2430. ext_channel_id_add_clienthello,
  2431. ext_channel_id_parse_serverhello,
  2432. ext_channel_id_parse_clienthello,
  2433. ext_channel_id_add_serverhello,
  2434. },
  2435. {
  2436. TLSEXT_TYPE_srtp,
  2437. ext_srtp_init,
  2438. ext_srtp_add_clienthello,
  2439. ext_srtp_parse_serverhello,
  2440. ext_srtp_parse_clienthello,
  2441. ext_srtp_add_serverhello,
  2442. },
  2443. {
  2444. TLSEXT_TYPE_key_share,
  2445. NULL,
  2446. ext_key_share_add_clienthello,
  2447. forbid_parse_serverhello,
  2448. ignore_parse_clienthello,
  2449. dont_add_serverhello,
  2450. },
  2451. {
  2452. TLSEXT_TYPE_psk_key_exchange_modes,
  2453. NULL,
  2454. ext_psk_key_exchange_modes_add_clienthello,
  2455. forbid_parse_serverhello,
  2456. ext_psk_key_exchange_modes_parse_clienthello,
  2457. dont_add_serverhello,
  2458. },
  2459. {
  2460. TLSEXT_TYPE_early_data,
  2461. NULL,
  2462. ext_early_data_add_clienthello,
  2463. ext_early_data_parse_serverhello,
  2464. ext_early_data_parse_clienthello,
  2465. ext_early_data_add_serverhello,
  2466. },
  2467. {
  2468. TLSEXT_TYPE_supported_versions,
  2469. NULL,
  2470. ext_supported_versions_add_clienthello,
  2471. forbid_parse_serverhello,
  2472. ignore_parse_clienthello,
  2473. dont_add_serverhello,
  2474. },
  2475. {
  2476. TLSEXT_TYPE_cookie,
  2477. NULL,
  2478. ext_cookie_add_clienthello,
  2479. forbid_parse_serverhello,
  2480. ignore_parse_clienthello,
  2481. dont_add_serverhello,
  2482. },
  2483. {
  2484. TLSEXT_TYPE_quic_transport_parameters,
  2485. NULL,
  2486. ext_quic_transport_params_add_clienthello,
  2487. ext_quic_transport_params_parse_serverhello,
  2488. ext_quic_transport_params_parse_clienthello,
  2489. ext_quic_transport_params_add_serverhello,
  2490. },
  2491. {
  2492. TLSEXT_TYPE_token_binding,
  2493. NULL,
  2494. ext_token_binding_add_clienthello,
  2495. ext_token_binding_parse_serverhello,
  2496. ext_token_binding_parse_clienthello,
  2497. ext_token_binding_add_serverhello,
  2498. },
  2499. {
  2500. TLSEXT_TYPE_cert_compression,
  2501. NULL,
  2502. cert_compression_add_clienthello,
  2503. cert_compression_parse_serverhello,
  2504. cert_compression_parse_clienthello,
  2505. cert_compression_add_serverhello,
  2506. },
  2507. };
  2508. #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
  2509. static_assert(kNumExtensions <=
  2510. sizeof(((SSL_HANDSHAKE *)NULL)->extensions.sent) * 8,
  2511. "too many extensions for sent bitset");
  2512. static_assert(kNumExtensions <=
  2513. sizeof(((SSL_HANDSHAKE *)NULL)->extensions.received) * 8,
  2514. "too many extensions for received bitset");
  2515. static const struct tls_extension *tls_extension_find(uint32_t *out_index,
  2516. uint16_t value) {
  2517. unsigned i;
  2518. for (i = 0; i < kNumExtensions; i++) {
  2519. if (kExtensions[i].value == value) {
  2520. *out_index = i;
  2521. return &kExtensions[i];
  2522. }
  2523. }
  2524. return NULL;
  2525. }
  2526. bool ssl_add_clienthello_tlsext(SSL_HANDSHAKE *hs, CBB *out,
  2527. size_t header_len) {
  2528. SSL *const ssl = hs->ssl;
  2529. CBB extensions;
  2530. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2531. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2532. return false;
  2533. }
  2534. hs->extensions.sent = 0;
  2535. for (size_t i = 0; i < kNumExtensions; i++) {
  2536. if (kExtensions[i].init != NULL) {
  2537. kExtensions[i].init(hs);
  2538. }
  2539. }
  2540. uint16_t grease_ext1 = 0;
  2541. if (ssl->ctx->grease_enabled) {
  2542. // Add a fake empty extension. See draft-davidben-tls-grease-01.
  2543. grease_ext1 = ssl_get_grease_value(hs, ssl_grease_extension1);
  2544. if (!CBB_add_u16(&extensions, grease_ext1) ||
  2545. !CBB_add_u16(&extensions, 0 /* zero length */)) {
  2546. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2547. return false;
  2548. }
  2549. }
  2550. bool last_was_empty = false;
  2551. for (size_t i = 0; i < kNumExtensions; i++) {
  2552. const size_t len_before = CBB_len(&extensions);
  2553. if (!kExtensions[i].add_clienthello(hs, &extensions)) {
  2554. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2555. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2556. return false;
  2557. }
  2558. const size_t bytes_written = CBB_len(&extensions) - len_before;
  2559. if (bytes_written != 0) {
  2560. hs->extensions.sent |= (1u << i);
  2561. }
  2562. // If the difference in lengths is only four bytes then the extension had
  2563. // an empty body.
  2564. last_was_empty = (bytes_written == 4);
  2565. }
  2566. if (ssl->ctx->grease_enabled) {
  2567. // Add a fake non-empty extension. See draft-davidben-tls-grease-01.
  2568. uint16_t grease_ext2 = ssl_get_grease_value(hs, ssl_grease_extension2);
  2569. // The two fake extensions must not have the same value. GREASE values are
  2570. // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different
  2571. // one.
  2572. if (grease_ext1 == grease_ext2) {
  2573. grease_ext2 ^= 0x1010;
  2574. }
  2575. if (!CBB_add_u16(&extensions, grease_ext2) ||
  2576. !CBB_add_u16(&extensions, 1 /* one byte length */) ||
  2577. !CBB_add_u8(&extensions, 0 /* single zero byte as contents */)) {
  2578. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2579. return false;
  2580. }
  2581. last_was_empty = false;
  2582. }
  2583. if (!SSL_is_dtls(ssl)) {
  2584. size_t psk_extension_len = ext_pre_shared_key_clienthello_length(hs);
  2585. header_len += 2 + CBB_len(&extensions) + psk_extension_len;
  2586. size_t padding_len = 0;
  2587. // The final extension must be non-empty. WebSphere Application
  2588. // Server 7.0 is intolerant to the last extension being zero-length. See
  2589. // https://crbug.com/363583.
  2590. if (last_was_empty && psk_extension_len == 0) {
  2591. padding_len = 1;
  2592. // The addition of the padding extension may push us into the F5 bug.
  2593. header_len += 4 + padding_len;
  2594. }
  2595. // Add padding to workaround bugs in F5 terminators. See RFC 7685.
  2596. //
  2597. // NB: because this code works out the length of all existing extensions
  2598. // it MUST always appear last (save for any PSK extension).
  2599. if (header_len > 0xff && header_len < 0x200) {
  2600. // If our calculations already included a padding extension, remove that
  2601. // factor because we're about to change its length.
  2602. if (padding_len != 0) {
  2603. header_len -= 4 + padding_len;
  2604. }
  2605. padding_len = 0x200 - header_len;
  2606. // Extensions take at least four bytes to encode. Always include at least
  2607. // one byte of data if including the extension. WebSphere Application
  2608. // Server 7.0 is intolerant to the last extension being zero-length. See
  2609. // https://crbug.com/363583.
  2610. if (padding_len >= 4 + 1) {
  2611. padding_len -= 4;
  2612. } else {
  2613. padding_len = 1;
  2614. }
  2615. }
  2616. if (padding_len != 0) {
  2617. uint8_t *padding_bytes;
  2618. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
  2619. !CBB_add_u16(&extensions, padding_len) ||
  2620. !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
  2621. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2622. return false;
  2623. }
  2624. OPENSSL_memset(padding_bytes, 0, padding_len);
  2625. }
  2626. }
  2627. // The PSK extension must be last, including after the padding.
  2628. if (!ext_pre_shared_key_add_clienthello(hs, &extensions)) {
  2629. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2630. return false;
  2631. }
  2632. // Discard empty extensions blocks.
  2633. if (CBB_len(&extensions) == 0) {
  2634. CBB_discard_child(out);
  2635. }
  2636. return CBB_flush(out);
  2637. }
  2638. bool ssl_add_serverhello_tlsext(SSL_HANDSHAKE *hs, CBB *out) {
  2639. SSL *const ssl = hs->ssl;
  2640. CBB extensions;
  2641. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2642. goto err;
  2643. }
  2644. for (unsigned i = 0; i < kNumExtensions; i++) {
  2645. if (!(hs->extensions.received & (1u << i))) {
  2646. // Don't send extensions that were not received.
  2647. continue;
  2648. }
  2649. if (!kExtensions[i].add_serverhello(hs, &extensions)) {
  2650. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2651. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2652. goto err;
  2653. }
  2654. }
  2655. // Discard empty extensions blocks before TLS 1.3.
  2656. if (ssl_protocol_version(ssl) < TLS1_3_VERSION &&
  2657. CBB_len(&extensions) == 0) {
  2658. CBB_discard_child(out);
  2659. }
  2660. return CBB_flush(out);
  2661. err:
  2662. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2663. return false;
  2664. }
  2665. static bool ssl_scan_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2666. const SSL_CLIENT_HELLO *client_hello,
  2667. int *out_alert) {
  2668. for (size_t i = 0; i < kNumExtensions; i++) {
  2669. if (kExtensions[i].init != NULL) {
  2670. kExtensions[i].init(hs);
  2671. }
  2672. }
  2673. hs->extensions.received = 0;
  2674. CBS extensions;
  2675. CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
  2676. while (CBS_len(&extensions) != 0) {
  2677. uint16_t type;
  2678. CBS extension;
  2679. // Decode the next extension.
  2680. if (!CBS_get_u16(&extensions, &type) ||
  2681. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2682. *out_alert = SSL_AD_DECODE_ERROR;
  2683. return false;
  2684. }
  2685. unsigned ext_index;
  2686. const struct tls_extension *const ext =
  2687. tls_extension_find(&ext_index, type);
  2688. if (ext == NULL) {
  2689. continue;
  2690. }
  2691. hs->extensions.received |= (1u << ext_index);
  2692. uint8_t alert = SSL_AD_DECODE_ERROR;
  2693. if (!ext->parse_clienthello(hs, &alert, &extension)) {
  2694. *out_alert = alert;
  2695. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2696. ERR_add_error_dataf("extension %u", (unsigned)type);
  2697. return false;
  2698. }
  2699. }
  2700. for (size_t i = 0; i < kNumExtensions; i++) {
  2701. if (hs->extensions.received & (1u << i)) {
  2702. continue;
  2703. }
  2704. CBS *contents = NULL, fake_contents;
  2705. static const uint8_t kFakeRenegotiateExtension[] = {0};
  2706. if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
  2707. ssl_client_cipher_list_contains_cipher(client_hello,
  2708. SSL3_CK_SCSV & 0xffff)) {
  2709. // The renegotiation SCSV was received so pretend that we received a
  2710. // renegotiation extension.
  2711. CBS_init(&fake_contents, kFakeRenegotiateExtension,
  2712. sizeof(kFakeRenegotiateExtension));
  2713. contents = &fake_contents;
  2714. hs->extensions.received |= (1u << i);
  2715. }
  2716. // Extension wasn't observed so call the callback with a NULL
  2717. // parameter.
  2718. uint8_t alert = SSL_AD_DECODE_ERROR;
  2719. if (!kExtensions[i].parse_clienthello(hs, &alert, contents)) {
  2720. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2721. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2722. *out_alert = alert;
  2723. return false;
  2724. }
  2725. }
  2726. return true;
  2727. }
  2728. bool ssl_parse_clienthello_tlsext(SSL_HANDSHAKE *hs,
  2729. const SSL_CLIENT_HELLO *client_hello) {
  2730. SSL *const ssl = hs->ssl;
  2731. int alert = SSL_AD_DECODE_ERROR;
  2732. if (!ssl_scan_clienthello_tlsext(hs, client_hello, &alert)) {
  2733. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  2734. return false;
  2735. }
  2736. if (!ssl_check_clienthello_tlsext(hs)) {
  2737. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
  2738. return false;
  2739. }
  2740. return true;
  2741. }
  2742. static bool ssl_scan_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs,
  2743. int *out_alert) {
  2744. SSL *const ssl = hs->ssl;
  2745. // Before TLS 1.3, ServerHello extensions blocks may be omitted if empty.
  2746. if (CBS_len(cbs) == 0 && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  2747. return true;
  2748. }
  2749. // Decode the extensions block and check it is valid.
  2750. CBS extensions;
  2751. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  2752. !tls1_check_duplicate_extensions(&extensions)) {
  2753. *out_alert = SSL_AD_DECODE_ERROR;
  2754. return false;
  2755. }
  2756. uint32_t received = 0;
  2757. while (CBS_len(&extensions) != 0) {
  2758. uint16_t type;
  2759. CBS extension;
  2760. // Decode the next extension.
  2761. if (!CBS_get_u16(&extensions, &type) ||
  2762. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2763. *out_alert = SSL_AD_DECODE_ERROR;
  2764. return false;
  2765. }
  2766. unsigned ext_index;
  2767. const struct tls_extension *const ext =
  2768. tls_extension_find(&ext_index, type);
  2769. if (ext == NULL) {
  2770. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  2771. ERR_add_error_dataf("extension %u", (unsigned)type);
  2772. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  2773. return false;
  2774. }
  2775. static_assert(kNumExtensions <= sizeof(hs->extensions.sent) * 8,
  2776. "too many bits");
  2777. if (!(hs->extensions.sent & (1u << ext_index))) {
  2778. // If the extension was never sent then it is illegal.
  2779. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  2780. ERR_add_error_dataf("extension :%u", (unsigned)type);
  2781. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  2782. return false;
  2783. }
  2784. received |= (1u << ext_index);
  2785. uint8_t alert = SSL_AD_DECODE_ERROR;
  2786. if (!ext->parse_serverhello(hs, &alert, &extension)) {
  2787. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2788. ERR_add_error_dataf("extension %u", (unsigned)type);
  2789. *out_alert = alert;
  2790. return false;
  2791. }
  2792. }
  2793. for (size_t i = 0; i < kNumExtensions; i++) {
  2794. if (!(received & (1u << i))) {
  2795. // Extension wasn't observed so call the callback with a NULL
  2796. // parameter.
  2797. uint8_t alert = SSL_AD_DECODE_ERROR;
  2798. if (!kExtensions[i].parse_serverhello(hs, &alert, NULL)) {
  2799. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2800. ERR_add_error_dataf("extension %u", (unsigned)kExtensions[i].value);
  2801. *out_alert = alert;
  2802. return false;
  2803. }
  2804. }
  2805. }
  2806. return true;
  2807. }
  2808. static bool ssl_check_clienthello_tlsext(SSL_HANDSHAKE *hs) {
  2809. SSL *const ssl = hs->ssl;
  2810. if (ssl->s3->token_binding_negotiated &&
  2811. !(SSL_get_secure_renegotiation_support(ssl) &&
  2812. SSL_get_extms_support(ssl))) {
  2813. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_TB_WITHOUT_EMS_OR_RI);
  2814. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
  2815. return false;
  2816. }
  2817. int ret = SSL_TLSEXT_ERR_NOACK;
  2818. int al = SSL_AD_UNRECOGNIZED_NAME;
  2819. if (ssl->ctx->servername_callback != 0) {
  2820. ret = ssl->ctx->servername_callback(ssl, &al, ssl->ctx->servername_arg);
  2821. } else if (ssl->session_ctx->servername_callback != 0) {
  2822. ret = ssl->session_ctx->servername_callback(
  2823. ssl, &al, ssl->session_ctx->servername_arg);
  2824. }
  2825. switch (ret) {
  2826. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2827. ssl_send_alert(ssl, SSL3_AL_FATAL, al);
  2828. return false;
  2829. case SSL_TLSEXT_ERR_NOACK:
  2830. hs->should_ack_sni = false;
  2831. return true;
  2832. default:
  2833. return true;
  2834. }
  2835. }
  2836. bool ssl_parse_serverhello_tlsext(SSL_HANDSHAKE *hs, CBS *cbs) {
  2837. SSL *const ssl = hs->ssl;
  2838. int alert = SSL_AD_DECODE_ERROR;
  2839. if (!ssl_scan_serverhello_tlsext(hs, cbs, &alert)) {
  2840. ssl_send_alert(ssl, SSL3_AL_FATAL, alert);
  2841. return false;
  2842. }
  2843. return true;
  2844. }
  2845. static enum ssl_ticket_aead_result_t decrypt_ticket_with_cipher_ctx(
  2846. Array<uint8_t> *out, EVP_CIPHER_CTX *cipher_ctx, HMAC_CTX *hmac_ctx,
  2847. Span<const uint8_t> ticket) {
  2848. size_t iv_len = EVP_CIPHER_CTX_iv_length(cipher_ctx);
  2849. // Check the MAC at the end of the ticket.
  2850. uint8_t mac[EVP_MAX_MD_SIZE];
  2851. size_t mac_len = HMAC_size(hmac_ctx);
  2852. if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
  2853. // The ticket must be large enough for key name, IV, data, and MAC.
  2854. return ssl_ticket_aead_ignore_ticket;
  2855. }
  2856. // Split the ticket into the ticket and the MAC.
  2857. auto ticket_mac = ticket.subspan(ticket.size() - mac_len);
  2858. ticket = ticket.subspan(0, ticket.size() - mac_len);
  2859. HMAC_Update(hmac_ctx, ticket.data(), ticket.size());
  2860. HMAC_Final(hmac_ctx, mac, NULL);
  2861. assert(mac_len == ticket_mac.size());
  2862. bool mac_ok = CRYPTO_memcmp(mac, ticket_mac.data(), mac_len) == 0;
  2863. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2864. mac_ok = true;
  2865. #endif
  2866. if (!mac_ok) {
  2867. return ssl_ticket_aead_ignore_ticket;
  2868. }
  2869. // Decrypt the session data.
  2870. auto ciphertext = ticket.subspan(SSL_TICKET_KEY_NAME_LEN + iv_len);
  2871. Array<uint8_t> plaintext;
  2872. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  2873. if (!plaintext.CopyFrom(ciphertext)) {
  2874. return ssl_ticket_aead_error;
  2875. }
  2876. #else
  2877. if (ciphertext.size() >= INT_MAX) {
  2878. return ssl_ticket_aead_ignore_ticket;
  2879. }
  2880. if (!plaintext.Init(ciphertext.size())) {
  2881. return ssl_ticket_aead_error;
  2882. }
  2883. int len1, len2;
  2884. if (!EVP_DecryptUpdate(cipher_ctx, plaintext.data(), &len1, ciphertext.data(),
  2885. (int)ciphertext.size()) ||
  2886. !EVP_DecryptFinal_ex(cipher_ctx, plaintext.data() + len1, &len2)) {
  2887. ERR_clear_error();
  2888. return ssl_ticket_aead_ignore_ticket;
  2889. }
  2890. plaintext.Shrink(static_cast<size_t>(len1) + len2);
  2891. #endif
  2892. *out = std::move(plaintext);
  2893. return ssl_ticket_aead_success;
  2894. }
  2895. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_cb(
  2896. SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
  2897. Span<const uint8_t> ticket) {
  2898. assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
  2899. ScopedEVP_CIPHER_CTX cipher_ctx;
  2900. ScopedHMAC_CTX hmac_ctx;
  2901. auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
  2902. // The actual IV is shorter, but the length is determined by the callback's
  2903. // chosen cipher. Instead we pass in |EVP_MAX_IV_LENGTH| worth of IV to ensure
  2904. // the callback has enough.
  2905. auto iv = ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_MAX_IV_LENGTH);
  2906. int cb_ret = hs->ssl->session_ctx->ticket_key_cb(
  2907. hs->ssl, const_cast<uint8_t *>(name.data()),
  2908. const_cast<uint8_t *>(iv.data()), cipher_ctx.get(), hmac_ctx.get(),
  2909. 0 /* decrypt */);
  2910. if (cb_ret < 0) {
  2911. return ssl_ticket_aead_error;
  2912. } else if (cb_ret == 0) {
  2913. return ssl_ticket_aead_ignore_ticket;
  2914. } else if (cb_ret == 2) {
  2915. *out_renew_ticket = true;
  2916. } else {
  2917. assert(cb_ret == 1);
  2918. }
  2919. return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
  2920. ticket);
  2921. }
  2922. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_ticket_keys(
  2923. SSL_HANDSHAKE *hs, Array<uint8_t> *out, Span<const uint8_t> ticket) {
  2924. assert(ticket.size() >= SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH);
  2925. SSL_CTX *ctx = hs->ssl->session_ctx.get();
  2926. // Rotate the ticket key if necessary.
  2927. if (!ssl_ctx_rotate_ticket_encryption_key(ctx)) {
  2928. return ssl_ticket_aead_error;
  2929. }
  2930. const EVP_CIPHER *cipher = EVP_aes_128_cbc();
  2931. auto name = ticket.subspan(0, SSL_TICKET_KEY_NAME_LEN);
  2932. auto iv =
  2933. ticket.subspan(SSL_TICKET_KEY_NAME_LEN, EVP_CIPHER_iv_length(cipher));
  2934. // Pick the matching ticket key and decrypt.
  2935. ScopedEVP_CIPHER_CTX cipher_ctx;
  2936. ScopedHMAC_CTX hmac_ctx;
  2937. {
  2938. MutexReadLock lock(&ctx->lock);
  2939. const TicketKey *key;
  2940. if (ctx->ticket_key_current && name == ctx->ticket_key_current->name) {
  2941. key = ctx->ticket_key_current.get();
  2942. } else if (ctx->ticket_key_prev && name == ctx->ticket_key_prev->name) {
  2943. key = ctx->ticket_key_prev.get();
  2944. } else {
  2945. return ssl_ticket_aead_ignore_ticket;
  2946. }
  2947. if (!HMAC_Init_ex(hmac_ctx.get(), key->hmac_key, sizeof(key->hmac_key),
  2948. tlsext_tick_md(), NULL) ||
  2949. !EVP_DecryptInit_ex(cipher_ctx.get(), cipher, NULL,
  2950. key->aes_key, iv.data())) {
  2951. return ssl_ticket_aead_error;
  2952. }
  2953. }
  2954. return decrypt_ticket_with_cipher_ctx(out, cipher_ctx.get(), hmac_ctx.get(),
  2955. ticket);
  2956. }
  2957. static enum ssl_ticket_aead_result_t ssl_decrypt_ticket_with_method(
  2958. SSL_HANDSHAKE *hs, Array<uint8_t> *out, bool *out_renew_ticket,
  2959. Span<const uint8_t> ticket) {
  2960. Array<uint8_t> plaintext;
  2961. if (!plaintext.Init(ticket.size())) {
  2962. OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
  2963. return ssl_ticket_aead_error;
  2964. }
  2965. size_t plaintext_len;
  2966. const enum ssl_ticket_aead_result_t result =
  2967. hs->ssl->session_ctx->ticket_aead_method->open(
  2968. hs->ssl, plaintext.data(), &plaintext_len, ticket.size(),
  2969. ticket.data(), ticket.size());
  2970. if (result != ssl_ticket_aead_success) {
  2971. return result;
  2972. }
  2973. plaintext.Shrink(plaintext_len);
  2974. *out = std::move(plaintext);
  2975. return ssl_ticket_aead_success;
  2976. }
  2977. enum ssl_ticket_aead_result_t ssl_process_ticket(
  2978. SSL_HANDSHAKE *hs, UniquePtr<SSL_SESSION> *out_session,
  2979. bool *out_renew_ticket, Span<const uint8_t> ticket,
  2980. Span<const uint8_t> session_id) {
  2981. *out_renew_ticket = false;
  2982. out_session->reset();
  2983. if ((SSL_get_options(hs->ssl) & SSL_OP_NO_TICKET) ||
  2984. session_id.size() > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  2985. return ssl_ticket_aead_ignore_ticket;
  2986. }
  2987. Array<uint8_t> plaintext;
  2988. enum ssl_ticket_aead_result_t result;
  2989. if (hs->ssl->session_ctx->ticket_aead_method != NULL) {
  2990. result = ssl_decrypt_ticket_with_method(hs, &plaintext, out_renew_ticket,
  2991. ticket);
  2992. } else {
  2993. // Ensure there is room for the key name and the largest IV |ticket_key_cb|
  2994. // may try to consume. The real limit may be lower, but the maximum IV
  2995. // length should be well under the minimum size for the session material and
  2996. // HMAC.
  2997. if (ticket.size() < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
  2998. return ssl_ticket_aead_ignore_ticket;
  2999. }
  3000. if (hs->ssl->session_ctx->ticket_key_cb != NULL) {
  3001. result =
  3002. ssl_decrypt_ticket_with_cb(hs, &plaintext, out_renew_ticket, ticket);
  3003. } else {
  3004. result = ssl_decrypt_ticket_with_ticket_keys(hs, &plaintext, ticket);
  3005. }
  3006. }
  3007. if (result != ssl_ticket_aead_success) {
  3008. return result;
  3009. }
  3010. // Decode the session.
  3011. UniquePtr<SSL_SESSION> session(SSL_SESSION_from_bytes(
  3012. plaintext.data(), plaintext.size(), hs->ssl->ctx.get()));
  3013. if (!session) {
  3014. ERR_clear_error(); // Don't leave an error on the queue.
  3015. return ssl_ticket_aead_ignore_ticket;
  3016. }
  3017. // Copy the client's session ID into the new session, to denote the ticket has
  3018. // been accepted.
  3019. OPENSSL_memcpy(session->session_id, session_id.data(), session_id.size());
  3020. session->session_id_length = session_id.size();
  3021. *out_session = std::move(session);
  3022. return ssl_ticket_aead_success;
  3023. }
  3024. bool tls1_parse_peer_sigalgs(SSL_HANDSHAKE *hs, const CBS *in_sigalgs) {
  3025. // Extension ignored for inappropriate versions
  3026. if (ssl_protocol_version(hs->ssl) < TLS1_2_VERSION) {
  3027. return true;
  3028. }
  3029. return parse_u16_array(in_sigalgs, &hs->peer_sigalgs);
  3030. }
  3031. bool tls1_get_legacy_signature_algorithm(uint16_t *out, const EVP_PKEY *pkey) {
  3032. switch (EVP_PKEY_id(pkey)) {
  3033. case EVP_PKEY_RSA:
  3034. *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
  3035. return true;
  3036. case EVP_PKEY_EC:
  3037. *out = SSL_SIGN_ECDSA_SHA1;
  3038. return true;
  3039. default:
  3040. return false;
  3041. }
  3042. }
  3043. bool tls1_choose_signature_algorithm(SSL_HANDSHAKE *hs, uint16_t *out) {
  3044. SSL *const ssl = hs->ssl;
  3045. CERT *cert = hs->config->cert.get();
  3046. // Before TLS 1.2, the signature algorithm isn't negotiated as part of the
  3047. // handshake.
  3048. if (ssl_protocol_version(ssl) < TLS1_2_VERSION) {
  3049. if (!tls1_get_legacy_signature_algorithm(out, hs->local_pubkey.get())) {
  3050. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  3051. return false;
  3052. }
  3053. return true;
  3054. }
  3055. Span<const uint16_t> sigalgs = kSignSignatureAlgorithms;
  3056. if (!cert->sigalgs.empty()) {
  3057. sigalgs = cert->sigalgs;
  3058. }
  3059. Span<const uint16_t> peer_sigalgs = hs->peer_sigalgs;
  3060. if (peer_sigalgs.empty() && ssl_protocol_version(ssl) < TLS1_3_VERSION) {
  3061. // If the client didn't specify any signature_algorithms extension then
  3062. // we can assume that it supports SHA1. See
  3063. // http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  3064. static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
  3065. SSL_SIGN_ECDSA_SHA1};
  3066. peer_sigalgs = kDefaultPeerAlgorithms;
  3067. }
  3068. for (uint16_t sigalg : sigalgs) {
  3069. // SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
  3070. // negotiated.
  3071. if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
  3072. !ssl_private_key_supports_signature_algorithm(hs, sigalg)) {
  3073. continue;
  3074. }
  3075. for (uint16_t peer_sigalg : peer_sigalgs) {
  3076. if (sigalg == peer_sigalg) {
  3077. *out = sigalg;
  3078. return true;
  3079. }
  3080. }
  3081. }
  3082. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  3083. return false;
  3084. }
  3085. bool tls1_verify_channel_id(SSL_HANDSHAKE *hs, const SSLMessage &msg) {
  3086. SSL *const ssl = hs->ssl;
  3087. // A Channel ID handshake message is structured to contain multiple
  3088. // extensions, but the only one that can be present is Channel ID.
  3089. uint16_t extension_type;
  3090. CBS channel_id = msg.body, extension;
  3091. if (!CBS_get_u16(&channel_id, &extension_type) ||
  3092. !CBS_get_u16_length_prefixed(&channel_id, &extension) ||
  3093. CBS_len(&channel_id) != 0 ||
  3094. extension_type != TLSEXT_TYPE_channel_id ||
  3095. CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) {
  3096. OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
  3097. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
  3098. return false;
  3099. }
  3100. UniquePtr<EC_GROUP> p256(EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  3101. if (!p256) {
  3102. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT);
  3103. return false;
  3104. }
  3105. UniquePtr<ECDSA_SIG> sig(ECDSA_SIG_new());
  3106. UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
  3107. if (!sig || !x || !y) {
  3108. return false;
  3109. }
  3110. const uint8_t *p = CBS_data(&extension);
  3111. if (BN_bin2bn(p + 0, 32, x.get()) == NULL ||
  3112. BN_bin2bn(p + 32, 32, y.get()) == NULL ||
  3113. BN_bin2bn(p + 64, 32, sig->r) == NULL ||
  3114. BN_bin2bn(p + 96, 32, sig->s) == NULL) {
  3115. return false;
  3116. }
  3117. UniquePtr<EC_KEY> key(EC_KEY_new());
  3118. UniquePtr<EC_POINT> point(EC_POINT_new(p256.get()));
  3119. if (!key || !point ||
  3120. !EC_POINT_set_affine_coordinates_GFp(p256.get(), point.get(), x.get(),
  3121. y.get(), nullptr) ||
  3122. !EC_KEY_set_group(key.get(), p256.get()) ||
  3123. !EC_KEY_set_public_key(key.get(), point.get())) {
  3124. return false;
  3125. }
  3126. uint8_t digest[EVP_MAX_MD_SIZE];
  3127. size_t digest_len;
  3128. if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
  3129. return false;
  3130. }
  3131. bool sig_ok = ECDSA_do_verify(digest, digest_len, sig.get(), key.get());
  3132. #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
  3133. sig_ok = true;
  3134. ERR_clear_error();
  3135. #endif
  3136. if (!sig_ok) {
  3137. OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID);
  3138. ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
  3139. ssl->s3->channel_id_valid = false;
  3140. return false;
  3141. }
  3142. OPENSSL_memcpy(ssl->s3->channel_id, p, 64);
  3143. return true;
  3144. }
  3145. bool tls1_write_channel_id(SSL_HANDSHAKE *hs, CBB *cbb) {
  3146. uint8_t digest[EVP_MAX_MD_SIZE];
  3147. size_t digest_len;
  3148. if (!tls1_channel_id_hash(hs, digest, &digest_len)) {
  3149. return false;
  3150. }
  3151. EC_KEY *ec_key = EVP_PKEY_get0_EC_KEY(hs->config->channel_id_private.get());
  3152. if (ec_key == nullptr) {
  3153. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  3154. return false;
  3155. }
  3156. UniquePtr<BIGNUM> x(BN_new()), y(BN_new());
  3157. if (!x || !y ||
  3158. !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(ec_key),
  3159. EC_KEY_get0_public_key(ec_key),
  3160. x.get(), y.get(), nullptr)) {
  3161. return false;
  3162. }
  3163. UniquePtr<ECDSA_SIG> sig(ECDSA_do_sign(digest, digest_len, ec_key));
  3164. if (!sig) {
  3165. return false;
  3166. }
  3167. CBB child;
  3168. if (!CBB_add_u16(cbb, TLSEXT_TYPE_channel_id) ||
  3169. !CBB_add_u16_length_prefixed(cbb, &child) ||
  3170. !BN_bn2cbb_padded(&child, 32, x.get()) ||
  3171. !BN_bn2cbb_padded(&child, 32, y.get()) ||
  3172. !BN_bn2cbb_padded(&child, 32, sig->r) ||
  3173. !BN_bn2cbb_padded(&child, 32, sig->s) ||
  3174. !CBB_flush(cbb)) {
  3175. return false;
  3176. }
  3177. return true;
  3178. }
  3179. bool tls1_channel_id_hash(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len) {
  3180. SSL *const ssl = hs->ssl;
  3181. if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) {
  3182. Array<uint8_t> msg;
  3183. if (!tls13_get_cert_verify_signature_input(hs, &msg,
  3184. ssl_cert_verify_channel_id)) {
  3185. return false;
  3186. }
  3187. SHA256(msg.data(), msg.size(), out);
  3188. *out_len = SHA256_DIGEST_LENGTH;
  3189. return true;
  3190. }
  3191. SHA256_CTX ctx;
  3192. SHA256_Init(&ctx);
  3193. static const char kClientIDMagic[] = "TLS Channel ID signature";
  3194. SHA256_Update(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
  3195. if (ssl->session != NULL) {
  3196. static const char kResumptionMagic[] = "Resumption";
  3197. SHA256_Update(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
  3198. if (ssl->session->original_handshake_hash_len == 0) {
  3199. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  3200. return false;
  3201. }
  3202. SHA256_Update(&ctx, ssl->session->original_handshake_hash,
  3203. ssl->session->original_handshake_hash_len);
  3204. }
  3205. uint8_t hs_hash[EVP_MAX_MD_SIZE];
  3206. size_t hs_hash_len;
  3207. if (!hs->transcript.GetHash(hs_hash, &hs_hash_len)) {
  3208. return false;
  3209. }
  3210. SHA256_Update(&ctx, hs_hash, (size_t)hs_hash_len);
  3211. SHA256_Final(out, &ctx);
  3212. *out_len = SHA256_DIGEST_LENGTH;
  3213. return true;
  3214. }
  3215. bool tls1_record_handshake_hashes_for_channel_id(SSL_HANDSHAKE *hs) {
  3216. SSL *const ssl = hs->ssl;
  3217. // This function should never be called for a resumed session because the
  3218. // handshake hashes that we wish to record are for the original, full
  3219. // handshake.
  3220. if (ssl->session != NULL) {
  3221. return false;
  3222. }
  3223. static_assert(
  3224. sizeof(hs->new_session->original_handshake_hash) == EVP_MAX_MD_SIZE,
  3225. "original_handshake_hash is too small");
  3226. size_t digest_len;
  3227. if (!hs->transcript.GetHash(hs->new_session->original_handshake_hash,
  3228. &digest_len)) {
  3229. return false;
  3230. }
  3231. static_assert(EVP_MAX_MD_SIZE <= 0xff,
  3232. "EVP_MAX_MD_SIZE does not fit in uint8_t");
  3233. hs->new_session->original_handshake_hash_len = (uint8_t)digest_len;
  3234. return true;
  3235. }
  3236. bool ssl_do_channel_id_callback(SSL_HANDSHAKE *hs) {
  3237. if (hs->config->channel_id_private != NULL ||
  3238. hs->ssl->ctx->channel_id_cb == NULL) {
  3239. return true;
  3240. }
  3241. EVP_PKEY *key = NULL;
  3242. hs->ssl->ctx->channel_id_cb(hs->ssl, &key);
  3243. if (key == NULL) {
  3244. // The caller should try again later.
  3245. return true;
  3246. }
  3247. UniquePtr<EVP_PKEY> free_key(key);
  3248. return SSL_set1_tls_channel_id(hs->ssl, key);
  3249. }
  3250. bool ssl_is_sct_list_valid(const CBS *contents) {
  3251. // Shallow parse the SCT list for sanity. By the RFC
  3252. // (https://tools.ietf.org/html/rfc6962#section-3.3) neither the list nor any
  3253. // of the SCTs may be empty.
  3254. CBS copy = *contents;
  3255. CBS sct_list;
  3256. if (!CBS_get_u16_length_prefixed(&copy, &sct_list) ||
  3257. CBS_len(&copy) != 0 ||
  3258. CBS_len(&sct_list) == 0) {
  3259. return false;
  3260. }
  3261. while (CBS_len(&sct_list) > 0) {
  3262. CBS sct;
  3263. if (!CBS_get_u16_length_prefixed(&sct_list, &sct) ||
  3264. CBS_len(&sct) == 0) {
  3265. return false;
  3266. }
  3267. }
  3268. return true;
  3269. }
  3270. BSSL_NAMESPACE_END
  3271. using namespace bssl;
  3272. int SSL_early_callback_ctx_extension_get(const SSL_CLIENT_HELLO *client_hello,
  3273. uint16_t extension_type,
  3274. const uint8_t **out_data,
  3275. size_t *out_len) {
  3276. CBS cbs;
  3277. if (!ssl_client_hello_get_extension(client_hello, &cbs, extension_type)) {
  3278. return 0;
  3279. }
  3280. *out_data = CBS_data(&cbs);
  3281. *out_len = CBS_len(&cbs);
  3282. return 1;
  3283. }
  3284. void SSL_CTX_set_ed25519_enabled(SSL_CTX *ctx, int enabled) {
  3285. ctx->ed25519_enabled = !!enabled;
  3286. }
  3287. void SSL_CTX_set_rsa_pss_rsae_certs_enabled(SSL_CTX *ctx, int enabled) {
  3288. ctx->rsa_pss_rsae_certs_enabled = !!enabled;
  3289. }