Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

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