選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

3192 行
91 KiB

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