No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

3170 líneas
90 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. /* For now, do not ship RSA-PSS signature algorithms on Android's system
  450. * BoringSSL. Once TLS 1.3 is finalized and the change in Chrome has stuck,
  451. * restore them. */
  452. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  453. SSL_SIGN_RSA_PSS_SHA512,
  454. #endif
  455. SSL_SIGN_RSA_PKCS1_SHA512,
  456. SSL_SIGN_ECDSA_SECP521R1_SHA512,
  457. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  458. SSL_SIGN_RSA_PSS_SHA384,
  459. #endif
  460. SSL_SIGN_RSA_PKCS1_SHA384,
  461. SSL_SIGN_ECDSA_SECP384R1_SHA384,
  462. #if !defined(BORINGSSL_ANDROID_SYSTEM)
  463. SSL_SIGN_RSA_PSS_SHA256,
  464. #endif
  465. SSL_SIGN_RSA_PKCS1_SHA256,
  466. SSL_SIGN_ECDSA_SECP256R1_SHA256,
  467. SSL_SIGN_RSA_PKCS1_SHA1,
  468. SSL_SIGN_ECDSA_SHA1,
  469. };
  470. size_t tls12_get_psigalgs(SSL *ssl, const uint16_t **psigs) {
  471. *psigs = kDefaultSignatureAlgorithms;
  472. return OPENSSL_ARRAY_SIZE(kDefaultSignatureAlgorithms);
  473. }
  474. int tls12_check_peer_sigalg(SSL *ssl, int *out_alert, uint16_t sigalg) {
  475. const uint16_t *sent_sigs;
  476. size_t sent_sigslen, i;
  477. /* Check signature matches a type we sent */
  478. sent_sigslen = tls12_get_psigalgs(ssl, &sent_sigs);
  479. for (i = 0; i < sent_sigslen; i++) {
  480. if (sigalg == sent_sigs[i]) {
  481. break;
  482. }
  483. }
  484. if (i == sent_sigslen) {
  485. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_SIGNATURE_TYPE);
  486. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  487. return 0;
  488. }
  489. return 1;
  490. }
  491. /* Get a mask of disabled algorithms: an algorithm is disabled if it isn't
  492. * supported or doesn't appear in supported signature algorithms. Unlike
  493. * ssl_cipher_get_disabled this applies to a specific session and not global
  494. * settings. */
  495. void ssl_set_client_disabled(SSL *ssl) {
  496. CERT *c = ssl->cert;
  497. int have_rsa = 0, have_ecdsa = 0;
  498. c->mask_a = 0;
  499. c->mask_k = 0;
  500. /* Now go through all signature algorithms seeing if we support any for RSA,
  501. * DSA, ECDSA. Do this for all versions not just TLS 1.2. */
  502. const uint16_t *sigalgs;
  503. size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
  504. for (size_t i = 0; i < num_sigalgs; i++) {
  505. switch (sigalgs[i]) {
  506. case SSL_SIGN_RSA_PSS_SHA512:
  507. case SSL_SIGN_RSA_PSS_SHA384:
  508. case SSL_SIGN_RSA_PSS_SHA256:
  509. case SSL_SIGN_RSA_PKCS1_SHA512:
  510. case SSL_SIGN_RSA_PKCS1_SHA384:
  511. case SSL_SIGN_RSA_PKCS1_SHA256:
  512. case SSL_SIGN_RSA_PKCS1_SHA1:
  513. have_rsa = 1;
  514. break;
  515. case SSL_SIGN_ECDSA_SECP521R1_SHA512:
  516. case SSL_SIGN_ECDSA_SECP384R1_SHA384:
  517. case SSL_SIGN_ECDSA_SECP256R1_SHA256:
  518. case SSL_SIGN_ECDSA_SHA1:
  519. have_ecdsa = 1;
  520. break;
  521. }
  522. }
  523. /* Disable auth if we don't include any appropriate signature algorithms. */
  524. if (!have_rsa) {
  525. c->mask_a |= SSL_aRSA;
  526. }
  527. if (!have_ecdsa) {
  528. c->mask_a |= SSL_aECDSA;
  529. }
  530. /* with PSK there must be client callback set */
  531. if (!ssl->psk_client_callback) {
  532. c->mask_a |= SSL_aPSK;
  533. c->mask_k |= SSL_kPSK;
  534. }
  535. }
  536. /* tls_extension represents a TLS extension that is handled internally. The
  537. * |init| function is called for each handshake, before any other functions of
  538. * the extension. Then the add and parse callbacks are called as needed.
  539. *
  540. * The parse callbacks receive a |CBS| that contains the contents of the
  541. * extension (i.e. not including the type and length bytes). If an extension is
  542. * not received then the parse callbacks will be called with a NULL CBS so that
  543. * they can do any processing needed to handle the absence of an extension.
  544. *
  545. * The add callbacks receive a |CBB| to which the extension can be appended but
  546. * the function is responsible for appending the type and length bytes too.
  547. *
  548. * All callbacks return one for success and zero for error. If a parse function
  549. * returns zero then a fatal alert with value |*out_alert| will be sent. If
  550. * |*out_alert| isn't set, then a |decode_error| alert will be sent. */
  551. struct tls_extension {
  552. uint16_t value;
  553. void (*init)(SSL *ssl);
  554. int (*add_clienthello)(SSL *ssl, CBB *out);
  555. int (*parse_serverhello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
  556. int (*parse_clienthello)(SSL *ssl, uint8_t *out_alert, CBS *contents);
  557. int (*add_serverhello)(SSL *ssl, CBB *out);
  558. };
  559. static int forbid_parse_serverhello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
  560. if (contents != NULL) {
  561. /* Servers MUST NOT send this extension. */
  562. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  563. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  564. return 0;
  565. }
  566. return 1;
  567. }
  568. static int ignore_parse_clienthello(SSL *ssl, uint8_t *out_alert, CBS *contents) {
  569. /* This extension from the client is handled elsewhere. */
  570. return 1;
  571. }
  572. static int dont_add_serverhello(SSL *ssl, CBB *out) {
  573. return 1;
  574. }
  575. /* Server name indication (SNI).
  576. *
  577. * https://tools.ietf.org/html/rfc6066#section-3. */
  578. static void ext_sni_init(SSL *ssl) {
  579. ssl->s3->tmp.should_ack_sni = 0;
  580. }
  581. static int ext_sni_add_clienthello(SSL *ssl, CBB *out) {
  582. if (ssl->tlsext_hostname == NULL) {
  583. return 1;
  584. }
  585. CBB contents, server_name_list, name;
  586. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  587. !CBB_add_u16_length_prefixed(out, &contents) ||
  588. !CBB_add_u16_length_prefixed(&contents, &server_name_list) ||
  589. !CBB_add_u8(&server_name_list, TLSEXT_NAMETYPE_host_name) ||
  590. !CBB_add_u16_length_prefixed(&server_name_list, &name) ||
  591. !CBB_add_bytes(&name, (const uint8_t *)ssl->tlsext_hostname,
  592. strlen(ssl->tlsext_hostname)) ||
  593. !CBB_flush(out)) {
  594. return 0;
  595. }
  596. return 1;
  597. }
  598. static int ext_sni_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  599. CBS *contents) {
  600. if (contents == NULL) {
  601. return 1;
  602. }
  603. if (CBS_len(contents) != 0) {
  604. return 0;
  605. }
  606. assert(ssl->tlsext_hostname != NULL);
  607. if (ssl->session == NULL) {
  608. assert(ssl->s3->new_session->tlsext_hostname == NULL);
  609. ssl->s3->new_session->tlsext_hostname = BUF_strdup(ssl->tlsext_hostname);
  610. if (!ssl->s3->new_session->tlsext_hostname) {
  611. *out_alert = SSL_AD_INTERNAL_ERROR;
  612. return 0;
  613. }
  614. }
  615. return 1;
  616. }
  617. static int ext_sni_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  618. CBS *contents) {
  619. if (contents == NULL) {
  620. return 1;
  621. }
  622. CBS server_name_list, host_name;
  623. uint8_t name_type;
  624. if (!CBS_get_u16_length_prefixed(contents, &server_name_list) ||
  625. !CBS_get_u8(&server_name_list, &name_type) ||
  626. /* Although the server_name extension was intended to be extensible to
  627. * new name types and multiple names, OpenSSL 1.0.x had a bug which meant
  628. * different name types will cause an error. Further, RFC 4366 originally
  629. * defined syntax inextensibly. RFC 6066 corrected this mistake, but
  630. * adding new name types is no longer feasible.
  631. *
  632. * Act as if the extensibility does not exist to simplify parsing. */
  633. !CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
  634. CBS_len(&server_name_list) != 0 ||
  635. CBS_len(contents) != 0) {
  636. return 0;
  637. }
  638. if (name_type != TLSEXT_NAMETYPE_host_name ||
  639. CBS_len(&host_name) == 0 ||
  640. CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
  641. CBS_contains_zero_byte(&host_name)) {
  642. *out_alert = SSL_AD_UNRECOGNIZED_NAME;
  643. return 0;
  644. }
  645. /* TODO(davidben): SNI should be resolved before resumption. We have the
  646. * early callback as a replacement, but we should fix the current callback
  647. * and avoid the need for |SSL_CTX_set_session_id_context|. */
  648. if (ssl->session == NULL) {
  649. assert(ssl->s3->new_session->tlsext_hostname == NULL);
  650. /* Copy the hostname as a string. */
  651. if (!CBS_strdup(&host_name, &ssl->s3->new_session->tlsext_hostname)) {
  652. *out_alert = SSL_AD_INTERNAL_ERROR;
  653. return 0;
  654. }
  655. ssl->s3->tmp.should_ack_sni = 1;
  656. }
  657. return 1;
  658. }
  659. static int ext_sni_add_serverhello(SSL *ssl, CBB *out) {
  660. if (ssl->session != NULL ||
  661. !ssl->s3->tmp.should_ack_sni ||
  662. ssl->s3->new_session->tlsext_hostname == NULL) {
  663. return 1;
  664. }
  665. if (!CBB_add_u16(out, TLSEXT_TYPE_server_name) ||
  666. !CBB_add_u16(out, 0 /* length */)) {
  667. return 0;
  668. }
  669. return 1;
  670. }
  671. /* Renegotiation indication.
  672. *
  673. * https://tools.ietf.org/html/rfc5746 */
  674. static int ext_ri_add_clienthello(SSL *ssl, CBB *out) {
  675. uint16_t min_version, max_version;
  676. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  677. return 0;
  678. }
  679. /* Renegotiation indication is not necessary in TLS 1.3. */
  680. if (min_version >= TLS1_3_VERSION) {
  681. return 1;
  682. }
  683. CBB contents, prev_finished;
  684. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  685. !CBB_add_u16_length_prefixed(out, &contents) ||
  686. !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
  687. !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
  688. ssl->s3->previous_client_finished_len) ||
  689. !CBB_flush(out)) {
  690. return 0;
  691. }
  692. return 1;
  693. }
  694. static int ext_ri_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  695. CBS *contents) {
  696. if (contents != NULL && ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  697. return 0;
  698. }
  699. /* Servers may not switch between omitting the extension and supporting it.
  700. * See RFC 5746, sections 3.5 and 4.2. */
  701. if (ssl->s3->initial_handshake_complete &&
  702. (contents != NULL) != ssl->s3->send_connection_binding) {
  703. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  704. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  705. return 0;
  706. }
  707. if (contents == NULL) {
  708. /* Strictly speaking, if we want to avoid an attack we should *always* see
  709. * RI even on initial ServerHello because the client doesn't see any
  710. * renegotiation during an attack. However this would mean we could not
  711. * connect to any server which doesn't support RI.
  712. *
  713. * OpenSSL has |SSL_OP_LEGACY_SERVER_CONNECT| to control this, but in
  714. * practical terms every client sets it so it's just assumed here. */
  715. return 1;
  716. }
  717. const size_t expected_len = ssl->s3->previous_client_finished_len +
  718. ssl->s3->previous_server_finished_len;
  719. /* Check for logic errors */
  720. assert(!expected_len || ssl->s3->previous_client_finished_len);
  721. assert(!expected_len || ssl->s3->previous_server_finished_len);
  722. /* Parse out the extension contents. */
  723. CBS renegotiated_connection;
  724. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  725. CBS_len(contents) != 0) {
  726. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  727. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  728. return 0;
  729. }
  730. /* Check that the extension matches. */
  731. if (CBS_len(&renegotiated_connection) != expected_len) {
  732. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  733. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  734. return 0;
  735. }
  736. const uint8_t *d = CBS_data(&renegotiated_connection);
  737. if (CRYPTO_memcmp(d, ssl->s3->previous_client_finished,
  738. ssl->s3->previous_client_finished_len)) {
  739. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  740. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  741. return 0;
  742. }
  743. d += ssl->s3->previous_client_finished_len;
  744. if (CRYPTO_memcmp(d, ssl->s3->previous_server_finished,
  745. ssl->s3->previous_server_finished_len)) {
  746. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  747. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  748. return 0;
  749. }
  750. ssl->s3->send_connection_binding = 1;
  751. return 1;
  752. }
  753. static int ext_ri_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  754. CBS *contents) {
  755. /* Renegotiation isn't supported as a server so this function should never be
  756. * called after the initial handshake. */
  757. assert(!ssl->s3->initial_handshake_complete);
  758. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  759. return 1;
  760. }
  761. if (contents == NULL) {
  762. return 1;
  763. }
  764. CBS renegotiated_connection;
  765. if (!CBS_get_u8_length_prefixed(contents, &renegotiated_connection) ||
  766. CBS_len(contents) != 0) {
  767. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_ENCODING_ERR);
  768. return 0;
  769. }
  770. /* Check that the extension matches */
  771. if (!CBS_mem_equal(&renegotiated_connection,
  772. ssl->s3->previous_client_finished,
  773. ssl->s3->previous_client_finished_len)) {
  774. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_MISMATCH);
  775. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  776. return 0;
  777. }
  778. ssl->s3->send_connection_binding = 1;
  779. return 1;
  780. }
  781. static int ext_ri_add_serverhello(SSL *ssl, CBB *out) {
  782. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  783. return 1;
  784. }
  785. CBB contents, prev_finished;
  786. if (!CBB_add_u16(out, TLSEXT_TYPE_renegotiate) ||
  787. !CBB_add_u16_length_prefixed(out, &contents) ||
  788. !CBB_add_u8_length_prefixed(&contents, &prev_finished) ||
  789. !CBB_add_bytes(&prev_finished, ssl->s3->previous_client_finished,
  790. ssl->s3->previous_client_finished_len) ||
  791. !CBB_add_bytes(&prev_finished, ssl->s3->previous_server_finished,
  792. ssl->s3->previous_server_finished_len) ||
  793. !CBB_flush(out)) {
  794. return 0;
  795. }
  796. return 1;
  797. }
  798. /* Extended Master Secret.
  799. *
  800. * https://tools.ietf.org/html/rfc7627 */
  801. static int ext_ems_add_clienthello(SSL *ssl, CBB *out) {
  802. uint16_t min_version, max_version;
  803. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  804. return 0;
  805. }
  806. /* Extended master secret is not necessary in TLS 1.3. */
  807. if (min_version >= TLS1_3_VERSION || max_version <= SSL3_VERSION) {
  808. return 1;
  809. }
  810. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  811. !CBB_add_u16(out, 0 /* length */)) {
  812. return 0;
  813. }
  814. return 1;
  815. }
  816. static int ext_ems_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  817. CBS *contents) {
  818. /* Whether EMS is negotiated may not change on renegotation. */
  819. if (ssl->s3->initial_handshake_complete) {
  820. if ((contents != NULL) != ssl->s3->tmp.extended_master_secret) {
  821. OPENSSL_PUT_ERROR(SSL, SSL_R_RENEGOTIATION_EMS_MISMATCH);
  822. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  823. return 0;
  824. }
  825. return 1;
  826. }
  827. if (contents == NULL) {
  828. return 1;
  829. }
  830. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  831. ssl->version == SSL3_VERSION) {
  832. return 0;
  833. }
  834. if (CBS_len(contents) != 0) {
  835. return 0;
  836. }
  837. ssl->s3->tmp.extended_master_secret = 1;
  838. return 1;
  839. }
  840. static int ext_ems_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  841. CBS *contents) {
  842. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION ||
  843. ssl->version == SSL3_VERSION) {
  844. return 1;
  845. }
  846. if (contents == NULL) {
  847. return 1;
  848. }
  849. if (CBS_len(contents) != 0) {
  850. return 0;
  851. }
  852. ssl->s3->tmp.extended_master_secret = 1;
  853. return 1;
  854. }
  855. static int ext_ems_add_serverhello(SSL *ssl, CBB *out) {
  856. if (!ssl->s3->tmp.extended_master_secret) {
  857. return 1;
  858. }
  859. if (!CBB_add_u16(out, TLSEXT_TYPE_extended_master_secret) ||
  860. !CBB_add_u16(out, 0 /* length */)) {
  861. return 0;
  862. }
  863. return 1;
  864. }
  865. /* Session tickets.
  866. *
  867. * https://tools.ietf.org/html/rfc5077 */
  868. static int ext_ticket_add_clienthello(SSL *ssl, CBB *out) {
  869. uint16_t min_version, max_version;
  870. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  871. return 0;
  872. }
  873. /* TLS 1.3 uses a different ticket extension. */
  874. if (min_version >= TLS1_3_VERSION ||
  875. SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
  876. return 1;
  877. }
  878. const uint8_t *ticket_data = NULL;
  879. int ticket_len = 0;
  880. /* Renegotiation does not participate in session resumption. However, still
  881. * advertise the extension to avoid potentially breaking servers which carry
  882. * over the state from the previous handshake, such as OpenSSL servers
  883. * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
  884. if (!ssl->s3->initial_handshake_complete &&
  885. ssl->session != NULL &&
  886. ssl->session->tlsext_tick != NULL &&
  887. /* Don't send TLS 1.3 session tickets in the ticket extension. */
  888. ssl->method->version_from_wire(ssl->session->ssl_version) <
  889. TLS1_3_VERSION) {
  890. ticket_data = ssl->session->tlsext_tick;
  891. ticket_len = ssl->session->tlsext_ticklen;
  892. }
  893. CBB ticket;
  894. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  895. !CBB_add_u16_length_prefixed(out, &ticket) ||
  896. !CBB_add_bytes(&ticket, ticket_data, ticket_len) ||
  897. !CBB_flush(out)) {
  898. return 0;
  899. }
  900. return 1;
  901. }
  902. static int ext_ticket_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  903. CBS *contents) {
  904. ssl->tlsext_ticket_expected = 0;
  905. if (contents == NULL) {
  906. return 1;
  907. }
  908. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  909. return 0;
  910. }
  911. /* If |SSL_OP_NO_TICKET| is set then no extension will have been sent and
  912. * this function should never be called, even if the server tries to send the
  913. * extension. */
  914. assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
  915. if (CBS_len(contents) != 0) {
  916. return 0;
  917. }
  918. ssl->tlsext_ticket_expected = 1;
  919. return 1;
  920. }
  921. static int ext_ticket_add_serverhello(SSL *ssl, CBB *out) {
  922. if (!ssl->tlsext_ticket_expected) {
  923. return 1;
  924. }
  925. /* If |SSL_OP_NO_TICKET| is set, |tlsext_ticket_expected| should never be
  926. * true. */
  927. assert((SSL_get_options(ssl) & SSL_OP_NO_TICKET) == 0);
  928. if (!CBB_add_u16(out, TLSEXT_TYPE_session_ticket) ||
  929. !CBB_add_u16(out, 0 /* length */)) {
  930. return 0;
  931. }
  932. return 1;
  933. }
  934. /* Signature Algorithms.
  935. *
  936. * https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
  937. static int ext_sigalgs_add_clienthello(SSL *ssl, CBB *out) {
  938. if (ssl->method->version_from_wire(ssl->client_version) < TLS1_2_VERSION) {
  939. return 1;
  940. }
  941. const uint16_t *sigalgs;
  942. const size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
  943. CBB contents, sigalgs_cbb;
  944. if (!CBB_add_u16(out, TLSEXT_TYPE_signature_algorithms) ||
  945. !CBB_add_u16_length_prefixed(out, &contents) ||
  946. !CBB_add_u16_length_prefixed(&contents, &sigalgs_cbb)) {
  947. return 0;
  948. }
  949. for (size_t i = 0; i < num_sigalgs; i++) {
  950. if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) {
  951. return 0;
  952. }
  953. }
  954. if (!CBB_flush(out)) {
  955. return 0;
  956. }
  957. return 1;
  958. }
  959. static int ext_sigalgs_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  960. CBS *contents) {
  961. OPENSSL_free(ssl->s3->hs->peer_sigalgs);
  962. ssl->s3->hs->peer_sigalgs = NULL;
  963. ssl->s3->hs->num_peer_sigalgs = 0;
  964. if (contents == NULL) {
  965. return 1;
  966. }
  967. CBS supported_signature_algorithms;
  968. if (!CBS_get_u16_length_prefixed(contents, &supported_signature_algorithms) ||
  969. CBS_len(contents) != 0 ||
  970. CBS_len(&supported_signature_algorithms) == 0 ||
  971. !tls1_parse_peer_sigalgs(ssl, &supported_signature_algorithms)) {
  972. return 0;
  973. }
  974. return 1;
  975. }
  976. /* OCSP Stapling.
  977. *
  978. * https://tools.ietf.org/html/rfc6066#section-8 */
  979. static void ext_ocsp_init(SSL *ssl) {
  980. ssl->s3->tmp.certificate_status_expected = 0;
  981. ssl->tlsext_status_type = -1;
  982. }
  983. static int ext_ocsp_add_clienthello(SSL *ssl, CBB *out) {
  984. if (!ssl->ocsp_stapling_enabled) {
  985. return 1;
  986. }
  987. CBB contents;
  988. if (!CBB_add_u16(out, TLSEXT_TYPE_status_request) ||
  989. !CBB_add_u16_length_prefixed(out, &contents) ||
  990. !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
  991. !CBB_add_u16(&contents, 0 /* empty responder ID list */) ||
  992. !CBB_add_u16(&contents, 0 /* empty request extensions */) ||
  993. !CBB_flush(out)) {
  994. return 0;
  995. }
  996. ssl->tlsext_status_type = TLSEXT_STATUSTYPE_ocsp;
  997. return 1;
  998. }
  999. static int ext_ocsp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1000. CBS *contents) {
  1001. if (contents == NULL) {
  1002. return 1;
  1003. }
  1004. /* OCSP stapling is forbidden on a non-certificate cipher. */
  1005. if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
  1006. return 0;
  1007. }
  1008. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  1009. if (CBS_len(contents) != 0) {
  1010. return 0;
  1011. }
  1012. ssl->s3->tmp.certificate_status_expected = 1;
  1013. return 1;
  1014. }
  1015. uint8_t status_type;
  1016. CBS ocsp_response;
  1017. if (!CBS_get_u8(contents, &status_type) ||
  1018. status_type != TLSEXT_STATUSTYPE_ocsp ||
  1019. !CBS_get_u24_length_prefixed(contents, &ocsp_response) ||
  1020. CBS_len(&ocsp_response) == 0 ||
  1021. CBS_len(contents) != 0) {
  1022. return 0;
  1023. }
  1024. if (!CBS_stow(&ocsp_response, &ssl->s3->new_session->ocsp_response,
  1025. &ssl->s3->new_session->ocsp_response_length)) {
  1026. *out_alert = SSL_AD_INTERNAL_ERROR;
  1027. return 0;
  1028. }
  1029. return 1;
  1030. }
  1031. static int ext_ocsp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1032. CBS *contents) {
  1033. if (contents == NULL) {
  1034. return 1;
  1035. }
  1036. uint8_t status_type;
  1037. if (!CBS_get_u8(contents, &status_type)) {
  1038. return 0;
  1039. }
  1040. /* We cannot decide whether OCSP stapling will occur yet because the correct
  1041. * SSL_CTX might not have been selected. */
  1042. ssl->s3->tmp.ocsp_stapling_requested = status_type == TLSEXT_STATUSTYPE_ocsp;
  1043. return 1;
  1044. }
  1045. static int ext_ocsp_add_serverhello(SSL *ssl, CBB *out) {
  1046. if (!ssl->s3->tmp.ocsp_stapling_requested ||
  1047. ssl->ctx->ocsp_response_length == 0 ||
  1048. !ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) {
  1049. return 1;
  1050. }
  1051. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  1052. /* The extension shouldn't be sent when resuming sessions. */
  1053. if (ssl->session != NULL) {
  1054. return 1;
  1055. }
  1056. ssl->s3->tmp.certificate_status_expected = 1;
  1057. return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
  1058. CBB_add_u16(out, 0 /* length */);
  1059. }
  1060. CBB body, ocsp_response;
  1061. return CBB_add_u16(out, TLSEXT_TYPE_status_request) &&
  1062. CBB_add_u16_length_prefixed(out, &body) &&
  1063. CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) &&
  1064. CBB_add_u24_length_prefixed(&body, &ocsp_response) &&
  1065. CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response,
  1066. ssl->ctx->ocsp_response_length) &&
  1067. CBB_flush(out);
  1068. }
  1069. /* Next protocol negotiation.
  1070. *
  1071. * https://htmlpreview.github.io/?https://github.com/agl/technotes/blob/master/nextprotoneg.html */
  1072. static void ext_npn_init(SSL *ssl) {
  1073. ssl->s3->next_proto_neg_seen = 0;
  1074. }
  1075. static int ext_npn_add_clienthello(SSL *ssl, CBB *out) {
  1076. if (ssl->s3->initial_handshake_complete ||
  1077. ssl->ctx->next_proto_select_cb == NULL ||
  1078. (ssl->options & SSL_OP_DISABLE_NPN) ||
  1079. SSL_is_dtls(ssl)) {
  1080. return 1;
  1081. }
  1082. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  1083. !CBB_add_u16(out, 0 /* length */)) {
  1084. return 0;
  1085. }
  1086. return 1;
  1087. }
  1088. static int ext_npn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1089. CBS *contents) {
  1090. if (contents == NULL) {
  1091. return 1;
  1092. }
  1093. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1094. return 0;
  1095. }
  1096. /* If any of these are false then we should never have sent the NPN
  1097. * extension in the ClientHello and thus this function should never have been
  1098. * called. */
  1099. assert(!ssl->s3->initial_handshake_complete);
  1100. assert(!SSL_is_dtls(ssl));
  1101. assert(ssl->ctx->next_proto_select_cb != NULL);
  1102. assert(!(ssl->options & SSL_OP_DISABLE_NPN));
  1103. if (ssl->s3->alpn_selected != NULL) {
  1104. /* NPN and ALPN may not be negotiated in the same connection. */
  1105. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1106. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  1107. return 0;
  1108. }
  1109. const uint8_t *const orig_contents = CBS_data(contents);
  1110. const size_t orig_len = CBS_len(contents);
  1111. while (CBS_len(contents) != 0) {
  1112. CBS proto;
  1113. if (!CBS_get_u8_length_prefixed(contents, &proto) ||
  1114. CBS_len(&proto) == 0) {
  1115. return 0;
  1116. }
  1117. }
  1118. uint8_t *selected;
  1119. uint8_t selected_len;
  1120. if (ssl->ctx->next_proto_select_cb(
  1121. ssl, &selected, &selected_len, orig_contents, orig_len,
  1122. ssl->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
  1123. *out_alert = SSL_AD_INTERNAL_ERROR;
  1124. return 0;
  1125. }
  1126. OPENSSL_free(ssl->s3->next_proto_negotiated);
  1127. ssl->s3->next_proto_negotiated = BUF_memdup(selected, selected_len);
  1128. if (ssl->s3->next_proto_negotiated == NULL) {
  1129. *out_alert = SSL_AD_INTERNAL_ERROR;
  1130. return 0;
  1131. }
  1132. ssl->s3->next_proto_negotiated_len = selected_len;
  1133. ssl->s3->next_proto_neg_seen = 1;
  1134. return 1;
  1135. }
  1136. static int ext_npn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1137. CBS *contents) {
  1138. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1139. return 1;
  1140. }
  1141. if (contents != NULL && CBS_len(contents) != 0) {
  1142. return 0;
  1143. }
  1144. if (contents == NULL ||
  1145. ssl->s3->initial_handshake_complete ||
  1146. /* If the ALPN extension is seen before NPN, ignore it. (If ALPN is seen
  1147. * afterwards, parsing the ALPN extension will clear
  1148. * |next_proto_neg_seen|. */
  1149. ssl->s3->alpn_selected != NULL ||
  1150. ssl->ctx->next_protos_advertised_cb == NULL ||
  1151. SSL_is_dtls(ssl)) {
  1152. return 1;
  1153. }
  1154. ssl->s3->next_proto_neg_seen = 1;
  1155. return 1;
  1156. }
  1157. static int ext_npn_add_serverhello(SSL *ssl, CBB *out) {
  1158. /* |next_proto_neg_seen| might have been cleared when an ALPN extension was
  1159. * parsed. */
  1160. if (!ssl->s3->next_proto_neg_seen) {
  1161. return 1;
  1162. }
  1163. const uint8_t *npa;
  1164. unsigned npa_len;
  1165. if (ssl->ctx->next_protos_advertised_cb(
  1166. ssl, &npa, &npa_len, ssl->ctx->next_protos_advertised_cb_arg) !=
  1167. SSL_TLSEXT_ERR_OK) {
  1168. ssl->s3->next_proto_neg_seen = 0;
  1169. return 1;
  1170. }
  1171. CBB contents;
  1172. if (!CBB_add_u16(out, TLSEXT_TYPE_next_proto_neg) ||
  1173. !CBB_add_u16_length_prefixed(out, &contents) ||
  1174. !CBB_add_bytes(&contents, npa, npa_len) ||
  1175. !CBB_flush(out)) {
  1176. return 0;
  1177. }
  1178. return 1;
  1179. }
  1180. /* Signed certificate timestamps.
  1181. *
  1182. * https://tools.ietf.org/html/rfc6962#section-3.3.1 */
  1183. static int ext_sct_add_clienthello(SSL *ssl, CBB *out) {
  1184. if (!ssl->signed_cert_timestamps_enabled) {
  1185. return 1;
  1186. }
  1187. if (!CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) ||
  1188. !CBB_add_u16(out, 0 /* length */)) {
  1189. return 0;
  1190. }
  1191. return 1;
  1192. }
  1193. static int ext_sct_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1194. CBS *contents) {
  1195. if (contents == NULL) {
  1196. return 1;
  1197. }
  1198. /* If this is false then we should never have sent the SCT extension in the
  1199. * ClientHello and thus this function should never have been called. */
  1200. assert(ssl->signed_cert_timestamps_enabled);
  1201. if (CBS_len(contents) == 0) {
  1202. *out_alert = SSL_AD_DECODE_ERROR;
  1203. return 0;
  1204. }
  1205. /* Session resumption uses the original session information. */
  1206. if (!ssl->s3->session_reused &&
  1207. !CBS_stow(
  1208. contents,
  1209. &ssl->s3->new_session->tlsext_signed_cert_timestamp_list,
  1210. &ssl->s3->new_session->tlsext_signed_cert_timestamp_list_length)) {
  1211. *out_alert = SSL_AD_INTERNAL_ERROR;
  1212. return 0;
  1213. }
  1214. return 1;
  1215. }
  1216. static int ext_sct_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1217. CBS *contents) {
  1218. return contents == NULL || CBS_len(contents) == 0;
  1219. }
  1220. static int ext_sct_add_serverhello(SSL *ssl, CBB *out) {
  1221. /* The extension shouldn't be sent when resuming sessions. */
  1222. if (ssl->s3->session_reused ||
  1223. ssl->ctx->signed_cert_timestamp_list_length == 0) {
  1224. return 1;
  1225. }
  1226. CBB contents;
  1227. return CBB_add_u16(out, TLSEXT_TYPE_certificate_timestamp) &&
  1228. CBB_add_u16_length_prefixed(out, &contents) &&
  1229. CBB_add_bytes(&contents, ssl->ctx->signed_cert_timestamp_list,
  1230. ssl->ctx->signed_cert_timestamp_list_length) &&
  1231. CBB_flush(out);
  1232. }
  1233. /* Application-level Protocol Negotiation.
  1234. *
  1235. * https://tools.ietf.org/html/rfc7301 */
  1236. static void ext_alpn_init(SSL *ssl) {
  1237. OPENSSL_free(ssl->s3->alpn_selected);
  1238. ssl->s3->alpn_selected = NULL;
  1239. }
  1240. static int ext_alpn_add_clienthello(SSL *ssl, CBB *out) {
  1241. if (ssl->alpn_client_proto_list == NULL ||
  1242. ssl->s3->initial_handshake_complete) {
  1243. return 1;
  1244. }
  1245. CBB contents, proto_list;
  1246. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1247. !CBB_add_u16_length_prefixed(out, &contents) ||
  1248. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1249. !CBB_add_bytes(&proto_list, ssl->alpn_client_proto_list,
  1250. ssl->alpn_client_proto_list_len) ||
  1251. !CBB_flush(out)) {
  1252. return 0;
  1253. }
  1254. return 1;
  1255. }
  1256. static int ext_alpn_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1257. CBS *contents) {
  1258. if (contents == NULL) {
  1259. return 1;
  1260. }
  1261. assert(!ssl->s3->initial_handshake_complete);
  1262. assert(ssl->alpn_client_proto_list != NULL);
  1263. if (ssl->s3->next_proto_neg_seen) {
  1264. /* NPN and ALPN may not be negotiated in the same connection. */
  1265. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1266. OPENSSL_PUT_ERROR(SSL, SSL_R_NEGOTIATED_BOTH_NPN_AND_ALPN);
  1267. return 0;
  1268. }
  1269. /* The extension data consists of a ProtocolNameList which must have
  1270. * exactly one ProtocolName. Each of these is length-prefixed. */
  1271. CBS protocol_name_list, protocol_name;
  1272. if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
  1273. CBS_len(contents) != 0 ||
  1274. !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
  1275. /* Empty protocol names are forbidden. */
  1276. CBS_len(&protocol_name) == 0 ||
  1277. CBS_len(&protocol_name_list) != 0) {
  1278. return 0;
  1279. }
  1280. /* Check that the protcol name is one of the ones we advertised. */
  1281. int protocol_ok = 0;
  1282. CBS client_protocol_name_list, client_protocol_name;
  1283. CBS_init(&client_protocol_name_list, ssl->alpn_client_proto_list,
  1284. ssl->alpn_client_proto_list_len);
  1285. while (CBS_len(&client_protocol_name_list) > 0) {
  1286. if (!CBS_get_u8_length_prefixed(&client_protocol_name_list,
  1287. &client_protocol_name)) {
  1288. *out_alert = SSL_AD_INTERNAL_ERROR;
  1289. return 0;
  1290. }
  1291. if (CBS_len(&client_protocol_name) == CBS_len(&protocol_name) &&
  1292. memcmp(CBS_data(&client_protocol_name), CBS_data(&protocol_name),
  1293. CBS_len(&protocol_name)) == 0) {
  1294. protocol_ok = 1;
  1295. break;
  1296. }
  1297. }
  1298. if (!protocol_ok) {
  1299. OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_ALPN_PROTOCOL);
  1300. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1301. return 0;
  1302. }
  1303. if (!CBS_stow(&protocol_name, &ssl->s3->alpn_selected,
  1304. &ssl->s3->alpn_selected_len)) {
  1305. *out_alert = SSL_AD_INTERNAL_ERROR;
  1306. return 0;
  1307. }
  1308. return 1;
  1309. }
  1310. static int ext_alpn_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1311. CBS *contents) {
  1312. if (contents == NULL) {
  1313. return 1;
  1314. }
  1315. if (ssl->ctx->alpn_select_cb == NULL ||
  1316. ssl->s3->initial_handshake_complete) {
  1317. return 1;
  1318. }
  1319. /* ALPN takes precedence over NPN. */
  1320. ssl->s3->next_proto_neg_seen = 0;
  1321. CBS protocol_name_list;
  1322. if (!CBS_get_u16_length_prefixed(contents, &protocol_name_list) ||
  1323. CBS_len(contents) != 0 ||
  1324. CBS_len(&protocol_name_list) < 2) {
  1325. return 0;
  1326. }
  1327. /* Validate the protocol list. */
  1328. CBS protocol_name_list_copy = protocol_name_list;
  1329. while (CBS_len(&protocol_name_list_copy) > 0) {
  1330. CBS protocol_name;
  1331. if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name) ||
  1332. /* Empty protocol names are forbidden. */
  1333. CBS_len(&protocol_name) == 0) {
  1334. return 0;
  1335. }
  1336. }
  1337. const uint8_t *selected;
  1338. uint8_t selected_len;
  1339. if (ssl->ctx->alpn_select_cb(
  1340. ssl, &selected, &selected_len, CBS_data(&protocol_name_list),
  1341. CBS_len(&protocol_name_list),
  1342. ssl->ctx->alpn_select_cb_arg) == SSL_TLSEXT_ERR_OK) {
  1343. OPENSSL_free(ssl->s3->alpn_selected);
  1344. ssl->s3->alpn_selected = BUF_memdup(selected, selected_len);
  1345. if (ssl->s3->alpn_selected == NULL) {
  1346. *out_alert = SSL_AD_INTERNAL_ERROR;
  1347. return 0;
  1348. }
  1349. ssl->s3->alpn_selected_len = selected_len;
  1350. }
  1351. return 1;
  1352. }
  1353. static int ext_alpn_add_serverhello(SSL *ssl, CBB *out) {
  1354. if (ssl->s3->alpn_selected == NULL) {
  1355. return 1;
  1356. }
  1357. CBB contents, proto_list, proto;
  1358. if (!CBB_add_u16(out, TLSEXT_TYPE_application_layer_protocol_negotiation) ||
  1359. !CBB_add_u16_length_prefixed(out, &contents) ||
  1360. !CBB_add_u16_length_prefixed(&contents, &proto_list) ||
  1361. !CBB_add_u8_length_prefixed(&proto_list, &proto) ||
  1362. !CBB_add_bytes(&proto, ssl->s3->alpn_selected,
  1363. ssl->s3->alpn_selected_len) ||
  1364. !CBB_flush(out)) {
  1365. return 0;
  1366. }
  1367. return 1;
  1368. }
  1369. /* Channel ID.
  1370. *
  1371. * https://tools.ietf.org/html/draft-balfanz-tls-channelid-01 */
  1372. static void ext_channel_id_init(SSL *ssl) {
  1373. ssl->s3->tlsext_channel_id_valid = 0;
  1374. }
  1375. static int ext_channel_id_add_clienthello(SSL *ssl, CBB *out) {
  1376. if (!ssl->tlsext_channel_id_enabled ||
  1377. SSL_is_dtls(ssl)) {
  1378. return 1;
  1379. }
  1380. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1381. !CBB_add_u16(out, 0 /* length */)) {
  1382. return 0;
  1383. }
  1384. return 1;
  1385. }
  1386. static int ext_channel_id_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1387. CBS *contents) {
  1388. if (contents == NULL) {
  1389. return 1;
  1390. }
  1391. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1392. return 0;
  1393. }
  1394. assert(!SSL_is_dtls(ssl));
  1395. assert(ssl->tlsext_channel_id_enabled);
  1396. if (CBS_len(contents) != 0) {
  1397. return 0;
  1398. }
  1399. ssl->s3->tlsext_channel_id_valid = 1;
  1400. return 1;
  1401. }
  1402. static int ext_channel_id_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1403. CBS *contents) {
  1404. if (contents == NULL ||
  1405. !ssl->tlsext_channel_id_enabled ||
  1406. SSL_is_dtls(ssl)) {
  1407. return 1;
  1408. }
  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_add_serverhello(SSL *ssl, CBB *out) {
  1416. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1417. return 1;
  1418. }
  1419. if (!ssl->s3->tlsext_channel_id_valid) {
  1420. return 1;
  1421. }
  1422. if (!CBB_add_u16(out, TLSEXT_TYPE_channel_id) ||
  1423. !CBB_add_u16(out, 0 /* length */)) {
  1424. return 0;
  1425. }
  1426. return 1;
  1427. }
  1428. /* Secure Real-time Transport Protocol (SRTP) extension.
  1429. *
  1430. * https://tools.ietf.org/html/rfc5764 */
  1431. static void ext_srtp_init(SSL *ssl) {
  1432. ssl->srtp_profile = NULL;
  1433. }
  1434. static int ext_srtp_add_clienthello(SSL *ssl, CBB *out) {
  1435. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1436. if (profiles == NULL) {
  1437. return 1;
  1438. }
  1439. const size_t num_profiles = sk_SRTP_PROTECTION_PROFILE_num(profiles);
  1440. if (num_profiles == 0) {
  1441. return 1;
  1442. }
  1443. CBB contents, profile_ids;
  1444. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1445. !CBB_add_u16_length_prefixed(out, &contents) ||
  1446. !CBB_add_u16_length_prefixed(&contents, &profile_ids)) {
  1447. return 0;
  1448. }
  1449. size_t i;
  1450. for (i = 0; i < num_profiles; i++) {
  1451. if (!CBB_add_u16(&profile_ids,
  1452. sk_SRTP_PROTECTION_PROFILE_value(profiles, i)->id)) {
  1453. return 0;
  1454. }
  1455. }
  1456. if (!CBB_add_u8(&contents, 0 /* empty use_mki value */) ||
  1457. !CBB_flush(out)) {
  1458. return 0;
  1459. }
  1460. return 1;
  1461. }
  1462. static int ext_srtp_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1463. CBS *contents) {
  1464. if (contents == NULL) {
  1465. return 1;
  1466. }
  1467. /* The extension consists of a u16-prefixed profile ID list containing a
  1468. * single uint16_t profile ID, then followed by a u8-prefixed srtp_mki field.
  1469. *
  1470. * See https://tools.ietf.org/html/rfc5764#section-4.1.1 */
  1471. CBS profile_ids, srtp_mki;
  1472. uint16_t profile_id;
  1473. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1474. !CBS_get_u16(&profile_ids, &profile_id) ||
  1475. CBS_len(&profile_ids) != 0 ||
  1476. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1477. CBS_len(contents) != 0) {
  1478. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1479. return 0;
  1480. }
  1481. if (CBS_len(&srtp_mki) != 0) {
  1482. /* Must be no MKI, since we never offer one. */
  1483. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_MKI_VALUE);
  1484. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1485. return 0;
  1486. }
  1487. STACK_OF(SRTP_PROTECTION_PROFILE) *profiles = SSL_get_srtp_profiles(ssl);
  1488. /* Check to see if the server gave us something we support (and presumably
  1489. * offered). */
  1490. size_t i;
  1491. for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(profiles); i++) {
  1492. const SRTP_PROTECTION_PROFILE *profile =
  1493. sk_SRTP_PROTECTION_PROFILE_value(profiles, i);
  1494. if (profile->id == profile_id) {
  1495. ssl->srtp_profile = profile;
  1496. return 1;
  1497. }
  1498. }
  1499. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1500. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1501. return 0;
  1502. }
  1503. static int ext_srtp_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1504. CBS *contents) {
  1505. if (contents == NULL) {
  1506. return 1;
  1507. }
  1508. CBS profile_ids, srtp_mki;
  1509. if (!CBS_get_u16_length_prefixed(contents, &profile_ids) ||
  1510. CBS_len(&profile_ids) < 2 ||
  1511. !CBS_get_u8_length_prefixed(contents, &srtp_mki) ||
  1512. CBS_len(contents) != 0) {
  1513. OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SRTP_PROTECTION_PROFILE_LIST);
  1514. return 0;
  1515. }
  1516. /* Discard the MKI value for now. */
  1517. const STACK_OF(SRTP_PROTECTION_PROFILE) *server_profiles =
  1518. SSL_get_srtp_profiles(ssl);
  1519. /* Pick the server's most preferred profile. */
  1520. size_t i;
  1521. for (i = 0; i < sk_SRTP_PROTECTION_PROFILE_num(server_profiles); i++) {
  1522. const SRTP_PROTECTION_PROFILE *server_profile =
  1523. sk_SRTP_PROTECTION_PROFILE_value(server_profiles, i);
  1524. CBS profile_ids_tmp;
  1525. CBS_init(&profile_ids_tmp, CBS_data(&profile_ids), CBS_len(&profile_ids));
  1526. while (CBS_len(&profile_ids_tmp) > 0) {
  1527. uint16_t profile_id;
  1528. if (!CBS_get_u16(&profile_ids_tmp, &profile_id)) {
  1529. return 0;
  1530. }
  1531. if (server_profile->id == profile_id) {
  1532. ssl->srtp_profile = server_profile;
  1533. return 1;
  1534. }
  1535. }
  1536. }
  1537. return 1;
  1538. }
  1539. static int ext_srtp_add_serverhello(SSL *ssl, CBB *out) {
  1540. if (ssl->srtp_profile == NULL) {
  1541. return 1;
  1542. }
  1543. CBB contents, profile_ids;
  1544. if (!CBB_add_u16(out, TLSEXT_TYPE_srtp) ||
  1545. !CBB_add_u16_length_prefixed(out, &contents) ||
  1546. !CBB_add_u16_length_prefixed(&contents, &profile_ids) ||
  1547. !CBB_add_u16(&profile_ids, ssl->srtp_profile->id) ||
  1548. !CBB_add_u8(&contents, 0 /* empty MKI */) ||
  1549. !CBB_flush(out)) {
  1550. return 0;
  1551. }
  1552. return 1;
  1553. }
  1554. /* EC point formats.
  1555. *
  1556. * https://tools.ietf.org/html/rfc4492#section-5.1.2 */
  1557. static int ssl_any_ec_cipher_suites_enabled(const SSL *ssl) {
  1558. if (ssl->version < TLS1_VERSION && !SSL_is_dtls(ssl)) {
  1559. return 0;
  1560. }
  1561. const STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(ssl);
  1562. size_t i;
  1563. for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
  1564. const SSL_CIPHER *cipher = sk_SSL_CIPHER_value(cipher_stack, i);
  1565. const uint32_t alg_k = cipher->algorithm_mkey;
  1566. const uint32_t alg_a = cipher->algorithm_auth;
  1567. if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
  1568. return 1;
  1569. }
  1570. }
  1571. return 0;
  1572. }
  1573. static int ext_ec_point_add_extension(SSL *ssl, CBB *out) {
  1574. CBB contents, formats;
  1575. if (!CBB_add_u16(out, TLSEXT_TYPE_ec_point_formats) ||
  1576. !CBB_add_u16_length_prefixed(out, &contents) ||
  1577. !CBB_add_u8_length_prefixed(&contents, &formats) ||
  1578. !CBB_add_u8(&formats, TLSEXT_ECPOINTFORMAT_uncompressed) ||
  1579. !CBB_flush(out)) {
  1580. return 0;
  1581. }
  1582. return 1;
  1583. }
  1584. static int ext_ec_point_add_clienthello(SSL *ssl, CBB *out) {
  1585. if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
  1586. return 1;
  1587. }
  1588. return ext_ec_point_add_extension(ssl, out);
  1589. }
  1590. static int ext_ec_point_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1591. CBS *contents) {
  1592. if (contents == NULL) {
  1593. return 1;
  1594. }
  1595. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1596. return 0;
  1597. }
  1598. CBS ec_point_format_list;
  1599. if (!CBS_get_u8_length_prefixed(contents, &ec_point_format_list) ||
  1600. CBS_len(contents) != 0) {
  1601. return 0;
  1602. }
  1603. /* Per RFC 4492, section 5.1.2, implementations MUST support the uncompressed
  1604. * point format. */
  1605. if (memchr(CBS_data(&ec_point_format_list), TLSEXT_ECPOINTFORMAT_uncompressed,
  1606. CBS_len(&ec_point_format_list)) == NULL) {
  1607. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1608. return 0;
  1609. }
  1610. return 1;
  1611. }
  1612. static int ext_ec_point_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1613. CBS *contents) {
  1614. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1615. return 1;
  1616. }
  1617. return ext_ec_point_parse_serverhello(ssl, out_alert, contents);
  1618. }
  1619. static int ext_ec_point_add_serverhello(SSL *ssl, CBB *out) {
  1620. if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) {
  1621. return 1;
  1622. }
  1623. const uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey;
  1624. const uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth;
  1625. const int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
  1626. if (!using_ecc) {
  1627. return 1;
  1628. }
  1629. return ext_ec_point_add_extension(ssl, out);
  1630. }
  1631. /* Draft Version Extension */
  1632. static int ext_draft_version_add_clienthello(SSL *ssl, CBB *out) {
  1633. uint16_t min_version, max_version;
  1634. if (!ssl_get_version_range(ssl, &min_version, &max_version) ||
  1635. max_version < TLS1_3_VERSION) {
  1636. return 1;
  1637. }
  1638. CBB contents;
  1639. if (!CBB_add_u16(out, TLSEXT_TYPE_draft_version) ||
  1640. !CBB_add_u16_length_prefixed(out, &contents) ||
  1641. !CBB_add_u16(&contents, TLS1_3_DRAFT_VERSION)) {
  1642. return 0;
  1643. }
  1644. return CBB_flush(out);
  1645. }
  1646. /* Pre Shared Key
  1647. *
  1648. * https://tools.ietf.org/html/draft-ietf-tls-tls13-14 */
  1649. static int ext_pre_shared_key_add_clienthello(SSL *ssl, CBB *out) {
  1650. uint16_t min_version, max_version;
  1651. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  1652. return 0;
  1653. }
  1654. if (max_version < TLS1_3_VERSION || ssl->session == NULL ||
  1655. ssl->method->version_from_wire(ssl->session->ssl_version) <
  1656. TLS1_3_VERSION) {
  1657. return 1;
  1658. }
  1659. CBB contents, identities, identity;
  1660. if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
  1661. !CBB_add_u16_length_prefixed(out, &contents) ||
  1662. !CBB_add_u16_length_prefixed(&contents, &identities) ||
  1663. !CBB_add_u16_length_prefixed(&identities, &identity) ||
  1664. !CBB_add_bytes(&identity, ssl->session->tlsext_tick,
  1665. ssl->session->tlsext_ticklen)) {
  1666. return 0;
  1667. }
  1668. return CBB_flush(out);
  1669. }
  1670. int ssl_ext_pre_shared_key_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1671. CBS *contents) {
  1672. uint16_t psk_id;
  1673. if (!CBS_get_u16(contents, &psk_id) ||
  1674. CBS_len(contents) != 0) {
  1675. *out_alert = SSL_AD_DECODE_ERROR;
  1676. return 0;
  1677. }
  1678. if (psk_id != 0) {
  1679. *out_alert = SSL_AD_UNKNOWN_PSK_IDENTITY;
  1680. return 0;
  1681. }
  1682. return 1;
  1683. }
  1684. int ssl_ext_pre_shared_key_parse_clienthello(SSL *ssl,
  1685. SSL_SESSION **out_session,
  1686. uint8_t *out_alert,
  1687. CBS *contents) {
  1688. CBS identities, identity;
  1689. if (!CBS_get_u16_length_prefixed(contents, &identities) ||
  1690. !CBS_get_u16_length_prefixed(&identities, &identity) ||
  1691. CBS_len(contents) != 0) {
  1692. *out_alert = SSL_AD_DECODE_ERROR;
  1693. return 0;
  1694. }
  1695. /* TLS 1.3 session tickets are renewed separately as part of the
  1696. * NewSessionTicket. */
  1697. int renew;
  1698. return tls_process_ticket(ssl, out_session, &renew, CBS_data(&identity),
  1699. CBS_len(&identity), NULL, 0);
  1700. }
  1701. int ssl_ext_pre_shared_key_add_serverhello(SSL *ssl, CBB *out) {
  1702. if (!ssl->s3->session_reused) {
  1703. return 1;
  1704. }
  1705. CBB contents;
  1706. if (!CBB_add_u16(out, TLSEXT_TYPE_pre_shared_key) ||
  1707. !CBB_add_u16_length_prefixed(out, &contents) ||
  1708. /* We only consider the first identity for resumption */
  1709. !CBB_add_u16(&contents, 0) ||
  1710. !CBB_flush(out)) {
  1711. return 0;
  1712. }
  1713. return 1;
  1714. }
  1715. /* Key Share
  1716. *
  1717. * https://tools.ietf.org/html/draft-ietf-tls-tls13-12 */
  1718. static int ext_key_share_add_clienthello(SSL *ssl, CBB *out) {
  1719. uint16_t min_version, max_version;
  1720. if (!ssl_get_version_range(ssl, &min_version, &max_version)) {
  1721. return 0;
  1722. }
  1723. if (max_version < TLS1_3_VERSION || !ssl_any_ec_cipher_suites_enabled(ssl)) {
  1724. return 1;
  1725. }
  1726. CBB contents, kse_bytes;
  1727. if (!CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
  1728. !CBB_add_u16_length_prefixed(out, &contents) ||
  1729. !CBB_add_u16_length_prefixed(&contents, &kse_bytes)) {
  1730. return 0;
  1731. }
  1732. const uint16_t *groups;
  1733. size_t groups_len;
  1734. if (ssl->s3->hs->retry_group) {
  1735. /* Append the new key share to the old list. */
  1736. if (!CBB_add_bytes(&kse_bytes, ssl->s3->hs->key_share_bytes,
  1737. ssl->s3->hs->key_share_bytes_len)) {
  1738. return 0;
  1739. }
  1740. OPENSSL_free(ssl->s3->hs->key_share_bytes);
  1741. ssl->s3->hs->key_share_bytes = NULL;
  1742. groups = &ssl->s3->hs->retry_group;
  1743. groups_len = 1;
  1744. } else {
  1745. tls1_get_grouplist(ssl, 0 /* local groups */, &groups, &groups_len);
  1746. /* Only send the top two preferred key shares. */
  1747. if (groups_len > 2) {
  1748. groups_len = 2;
  1749. }
  1750. }
  1751. ssl->s3->hs->groups = OPENSSL_malloc(groups_len * sizeof(SSL_ECDH_CTX));
  1752. if (ssl->s3->hs->groups == NULL) {
  1753. return 0;
  1754. }
  1755. memset(ssl->s3->hs->groups, 0, groups_len * sizeof(SSL_ECDH_CTX));
  1756. ssl->s3->hs->groups_len = groups_len;
  1757. for (size_t i = 0; i < groups_len; i++) {
  1758. if (!CBB_add_u16(&kse_bytes, groups[i])) {
  1759. return 0;
  1760. }
  1761. CBB key_exchange;
  1762. if (!CBB_add_u16_length_prefixed(&kse_bytes, &key_exchange) ||
  1763. !SSL_ECDH_CTX_init(&ssl->s3->hs->groups[i], groups[i]) ||
  1764. !SSL_ECDH_CTX_offer(&ssl->s3->hs->groups[i], &key_exchange) ||
  1765. !CBB_flush(&kse_bytes)) {
  1766. return 0;
  1767. }
  1768. }
  1769. if (!ssl->s3->hs->retry_group) {
  1770. /* Save the contents of the extension to repeat it in the second
  1771. * ClientHello. */
  1772. ssl->s3->hs->key_share_bytes_len = CBB_len(&kse_bytes);
  1773. ssl->s3->hs->key_share_bytes = BUF_memdup(CBB_data(&kse_bytes),
  1774. CBB_len(&kse_bytes));
  1775. if (ssl->s3->hs->key_share_bytes == NULL) {
  1776. return 0;
  1777. }
  1778. }
  1779. return CBB_flush(out);
  1780. }
  1781. int ssl_ext_key_share_parse_serverhello(SSL *ssl, uint8_t **out_secret,
  1782. size_t *out_secret_len,
  1783. uint8_t *out_alert, CBS *contents) {
  1784. CBS peer_key;
  1785. uint16_t group_id;
  1786. if (!CBS_get_u16(contents, &group_id) ||
  1787. !CBS_get_u16_length_prefixed(contents, &peer_key) ||
  1788. CBS_len(contents) != 0) {
  1789. *out_alert = SSL_AD_DECODE_ERROR;
  1790. return 0;
  1791. }
  1792. SSL_ECDH_CTX *group_ctx = NULL;
  1793. for (size_t i = 0; i < ssl->s3->hs->groups_len; i++) {
  1794. if (SSL_ECDH_CTX_get_id(&ssl->s3->hs->groups[i]) == group_id) {
  1795. group_ctx = &ssl->s3->hs->groups[i];
  1796. break;
  1797. }
  1798. }
  1799. if (group_ctx == NULL) {
  1800. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1801. OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE);
  1802. return 0;
  1803. }
  1804. if (!SSL_ECDH_CTX_finish(group_ctx, out_secret, out_secret_len, out_alert,
  1805. CBS_data(&peer_key), CBS_len(&peer_key))) {
  1806. *out_alert = SSL_AD_INTERNAL_ERROR;
  1807. return 0;
  1808. }
  1809. ssl->s3->new_session->key_exchange_info = group_id;
  1810. ssl_handshake_clear_groups(ssl->s3->hs);
  1811. return 1;
  1812. }
  1813. int ssl_ext_key_share_parse_clienthello(SSL *ssl, int *out_found,
  1814. uint8_t **out_secret,
  1815. size_t *out_secret_len,
  1816. uint8_t *out_alert, CBS *contents) {
  1817. uint16_t group_id;
  1818. CBS key_shares;
  1819. if (!tls1_get_shared_group(ssl, &group_id) ||
  1820. !CBS_get_u16_length_prefixed(contents, &key_shares) ||
  1821. CBS_len(contents) != 0) {
  1822. return 0;
  1823. }
  1824. *out_found = 0;
  1825. while (CBS_len(&key_shares) > 0) {
  1826. uint16_t id;
  1827. CBS peer_key;
  1828. if (!CBS_get_u16(&key_shares, &id) ||
  1829. !CBS_get_u16_length_prefixed(&key_shares, &peer_key)) {
  1830. return 0;
  1831. }
  1832. if (id != group_id || *out_found) {
  1833. continue;
  1834. }
  1835. SSL_ECDH_CTX group;
  1836. memset(&group, 0, sizeof(SSL_ECDH_CTX));
  1837. CBB public_key;
  1838. if (!CBB_init(&public_key, 0) ||
  1839. !SSL_ECDH_CTX_init(&group, group_id) ||
  1840. !SSL_ECDH_CTX_accept(&group, &public_key, out_secret, out_secret_len,
  1841. out_alert, CBS_data(&peer_key),
  1842. CBS_len(&peer_key)) ||
  1843. !CBB_finish(&public_key, &ssl->s3->hs->public_key,
  1844. &ssl->s3->hs->public_key_len)) {
  1845. SSL_ECDH_CTX_cleanup(&group);
  1846. CBB_cleanup(&public_key);
  1847. return 0;
  1848. }
  1849. SSL_ECDH_CTX_cleanup(&group);
  1850. *out_found = 1;
  1851. }
  1852. return 1;
  1853. }
  1854. int ssl_ext_key_share_add_serverhello(SSL *ssl, CBB *out) {
  1855. if (ssl->s3->tmp.new_cipher->algorithm_mkey != SSL_kECDHE) {
  1856. return 1;
  1857. }
  1858. uint16_t group_id;
  1859. CBB kse_bytes, public_key;
  1860. if (!tls1_get_shared_group(ssl, &group_id) ||
  1861. !CBB_add_u16(out, TLSEXT_TYPE_key_share) ||
  1862. !CBB_add_u16_length_prefixed(out, &kse_bytes) ||
  1863. !CBB_add_u16(&kse_bytes, group_id) ||
  1864. !CBB_add_u16_length_prefixed(&kse_bytes, &public_key) ||
  1865. !CBB_add_bytes(&public_key, ssl->s3->hs->public_key,
  1866. ssl->s3->hs->public_key_len) ||
  1867. !CBB_flush(out)) {
  1868. return 0;
  1869. }
  1870. ssl->s3->new_session->key_exchange_info = group_id;
  1871. return 1;
  1872. }
  1873. /* Negotiated Groups
  1874. *
  1875. * https://tools.ietf.org/html/rfc4492#section-5.1.2
  1876. * https://tools.ietf.org/html/draft-ietf-tls-tls13-12#section-6.3.2.2 */
  1877. static void ext_supported_groups_init(SSL *ssl) {
  1878. OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
  1879. ssl->s3->tmp.peer_supported_group_list = NULL;
  1880. ssl->s3->tmp.peer_supported_group_list_len = 0;
  1881. }
  1882. static int ext_supported_groups_add_clienthello(SSL *ssl, CBB *out) {
  1883. if (!ssl_any_ec_cipher_suites_enabled(ssl)) {
  1884. return 1;
  1885. }
  1886. CBB contents, groups_bytes;
  1887. if (!CBB_add_u16(out, TLSEXT_TYPE_supported_groups) ||
  1888. !CBB_add_u16_length_prefixed(out, &contents) ||
  1889. !CBB_add_u16_length_prefixed(&contents, &groups_bytes)) {
  1890. return 0;
  1891. }
  1892. const uint16_t *groups;
  1893. size_t groups_len;
  1894. tls1_get_grouplist(ssl, 0, &groups, &groups_len);
  1895. size_t i;
  1896. for (i = 0; i < groups_len; i++) {
  1897. if (!CBB_add_u16(&groups_bytes, groups[i])) {
  1898. return 0;
  1899. }
  1900. }
  1901. return CBB_flush(out);
  1902. }
  1903. static int ext_supported_groups_parse_serverhello(SSL *ssl, uint8_t *out_alert,
  1904. CBS *contents) {
  1905. /* This extension is not expected to be echoed by servers in TLS 1.2, but some
  1906. * BigIP servers send it nonetheless, so do not enforce this. */
  1907. return 1;
  1908. }
  1909. static int ext_supported_groups_parse_clienthello(SSL *ssl, uint8_t *out_alert,
  1910. CBS *contents) {
  1911. if (contents == NULL) {
  1912. return 1;
  1913. }
  1914. CBS supported_group_list;
  1915. if (!CBS_get_u16_length_prefixed(contents, &supported_group_list) ||
  1916. CBS_len(&supported_group_list) == 0 ||
  1917. (CBS_len(&supported_group_list) & 1) != 0 ||
  1918. CBS_len(contents) != 0) {
  1919. return 0;
  1920. }
  1921. ssl->s3->tmp.peer_supported_group_list = OPENSSL_malloc(
  1922. CBS_len(&supported_group_list));
  1923. if (ssl->s3->tmp.peer_supported_group_list == NULL) {
  1924. *out_alert = SSL_AD_INTERNAL_ERROR;
  1925. return 0;
  1926. }
  1927. const size_t num_groups = CBS_len(&supported_group_list) / 2;
  1928. size_t i;
  1929. for (i = 0; i < num_groups; i++) {
  1930. if (!CBS_get_u16(&supported_group_list,
  1931. &ssl->s3->tmp.peer_supported_group_list[i])) {
  1932. goto err;
  1933. }
  1934. }
  1935. assert(CBS_len(&supported_group_list) == 0);
  1936. ssl->s3->tmp.peer_supported_group_list_len = num_groups;
  1937. return 1;
  1938. err:
  1939. OPENSSL_free(ssl->s3->tmp.peer_supported_group_list);
  1940. ssl->s3->tmp.peer_supported_group_list = NULL;
  1941. *out_alert = SSL_AD_INTERNAL_ERROR;
  1942. return 0;
  1943. }
  1944. static int ext_supported_groups_add_serverhello(SSL *ssl, CBB *out) {
  1945. /* Servers don't echo this extension. */
  1946. return 1;
  1947. }
  1948. /* kExtensions contains all the supported extensions. */
  1949. static const struct tls_extension kExtensions[] = {
  1950. {
  1951. TLSEXT_TYPE_renegotiate,
  1952. NULL,
  1953. ext_ri_add_clienthello,
  1954. ext_ri_parse_serverhello,
  1955. ext_ri_parse_clienthello,
  1956. ext_ri_add_serverhello,
  1957. },
  1958. {
  1959. TLSEXT_TYPE_server_name,
  1960. ext_sni_init,
  1961. ext_sni_add_clienthello,
  1962. ext_sni_parse_serverhello,
  1963. ext_sni_parse_clienthello,
  1964. ext_sni_add_serverhello,
  1965. },
  1966. {
  1967. TLSEXT_TYPE_extended_master_secret,
  1968. NULL,
  1969. ext_ems_add_clienthello,
  1970. ext_ems_parse_serverhello,
  1971. ext_ems_parse_clienthello,
  1972. ext_ems_add_serverhello,
  1973. },
  1974. {
  1975. TLSEXT_TYPE_session_ticket,
  1976. NULL,
  1977. ext_ticket_add_clienthello,
  1978. ext_ticket_parse_serverhello,
  1979. /* Ticket extension client parsing is handled in ssl_session.c */
  1980. ignore_parse_clienthello,
  1981. ext_ticket_add_serverhello,
  1982. },
  1983. {
  1984. TLSEXT_TYPE_signature_algorithms,
  1985. NULL,
  1986. ext_sigalgs_add_clienthello,
  1987. forbid_parse_serverhello,
  1988. ext_sigalgs_parse_clienthello,
  1989. dont_add_serverhello,
  1990. },
  1991. {
  1992. TLSEXT_TYPE_status_request,
  1993. ext_ocsp_init,
  1994. ext_ocsp_add_clienthello,
  1995. ext_ocsp_parse_serverhello,
  1996. ext_ocsp_parse_clienthello,
  1997. ext_ocsp_add_serverhello,
  1998. },
  1999. {
  2000. TLSEXT_TYPE_next_proto_neg,
  2001. ext_npn_init,
  2002. ext_npn_add_clienthello,
  2003. ext_npn_parse_serverhello,
  2004. ext_npn_parse_clienthello,
  2005. ext_npn_add_serverhello,
  2006. },
  2007. {
  2008. TLSEXT_TYPE_certificate_timestamp,
  2009. NULL,
  2010. ext_sct_add_clienthello,
  2011. ext_sct_parse_serverhello,
  2012. ext_sct_parse_clienthello,
  2013. ext_sct_add_serverhello,
  2014. },
  2015. {
  2016. TLSEXT_TYPE_application_layer_protocol_negotiation,
  2017. ext_alpn_init,
  2018. ext_alpn_add_clienthello,
  2019. ext_alpn_parse_serverhello,
  2020. ext_alpn_parse_clienthello,
  2021. ext_alpn_add_serverhello,
  2022. },
  2023. {
  2024. TLSEXT_TYPE_channel_id,
  2025. ext_channel_id_init,
  2026. ext_channel_id_add_clienthello,
  2027. ext_channel_id_parse_serverhello,
  2028. ext_channel_id_parse_clienthello,
  2029. ext_channel_id_add_serverhello,
  2030. },
  2031. {
  2032. TLSEXT_TYPE_srtp,
  2033. ext_srtp_init,
  2034. ext_srtp_add_clienthello,
  2035. ext_srtp_parse_serverhello,
  2036. ext_srtp_parse_clienthello,
  2037. ext_srtp_add_serverhello,
  2038. },
  2039. {
  2040. TLSEXT_TYPE_ec_point_formats,
  2041. NULL,
  2042. ext_ec_point_add_clienthello,
  2043. ext_ec_point_parse_serverhello,
  2044. ext_ec_point_parse_clienthello,
  2045. ext_ec_point_add_serverhello,
  2046. },
  2047. {
  2048. TLSEXT_TYPE_draft_version,
  2049. NULL,
  2050. ext_draft_version_add_clienthello,
  2051. forbid_parse_serverhello,
  2052. ignore_parse_clienthello,
  2053. dont_add_serverhello,
  2054. },
  2055. {
  2056. TLSEXT_TYPE_key_share,
  2057. NULL,
  2058. ext_key_share_add_clienthello,
  2059. forbid_parse_serverhello,
  2060. ignore_parse_clienthello,
  2061. dont_add_serverhello,
  2062. },
  2063. {
  2064. TLSEXT_TYPE_pre_shared_key,
  2065. NULL,
  2066. ext_pre_shared_key_add_clienthello,
  2067. forbid_parse_serverhello,
  2068. ignore_parse_clienthello,
  2069. dont_add_serverhello,
  2070. },
  2071. /* The final extension must be non-empty. WebSphere Application Server 7.0 is
  2072. * intolerant to the last extension being zero-length. See
  2073. * https://crbug.com/363583. */
  2074. {
  2075. TLSEXT_TYPE_supported_groups,
  2076. ext_supported_groups_init,
  2077. ext_supported_groups_add_clienthello,
  2078. ext_supported_groups_parse_serverhello,
  2079. ext_supported_groups_parse_clienthello,
  2080. ext_supported_groups_add_serverhello,
  2081. },
  2082. };
  2083. #define kNumExtensions (sizeof(kExtensions) / sizeof(struct tls_extension))
  2084. OPENSSL_COMPILE_ASSERT(kNumExtensions <=
  2085. sizeof(((SSL *)NULL)->s3->tmp.extensions.sent) * 8,
  2086. too_many_extensions_for_sent_bitset);
  2087. OPENSSL_COMPILE_ASSERT(kNumExtensions <=
  2088. sizeof(((SSL *)NULL)->s3->tmp.extensions.received) *
  2089. 8,
  2090. too_many_extensions_for_received_bitset);
  2091. static const struct tls_extension *tls_extension_find(uint32_t *out_index,
  2092. uint16_t value) {
  2093. unsigned i;
  2094. for (i = 0; i < kNumExtensions; i++) {
  2095. if (kExtensions[i].value == value) {
  2096. *out_index = i;
  2097. return &kExtensions[i];
  2098. }
  2099. }
  2100. return NULL;
  2101. }
  2102. int SSL_extension_supported(unsigned extension_value) {
  2103. uint32_t index;
  2104. return extension_value == TLSEXT_TYPE_padding ||
  2105. tls_extension_find(&index, extension_value) != NULL;
  2106. }
  2107. int ssl_add_clienthello_tlsext(SSL *ssl, CBB *out, size_t header_len) {
  2108. /* don't add extensions for SSLv3 unless doing secure renegotiation */
  2109. if (ssl->client_version == SSL3_VERSION &&
  2110. !ssl->s3->send_connection_binding) {
  2111. return 1;
  2112. }
  2113. CBB extensions;
  2114. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2115. goto err;
  2116. }
  2117. ssl->s3->tmp.extensions.sent = 0;
  2118. ssl->s3->tmp.custom_extensions.sent = 0;
  2119. size_t i;
  2120. for (i = 0; i < kNumExtensions; i++) {
  2121. if (kExtensions[i].init != NULL) {
  2122. kExtensions[i].init(ssl);
  2123. }
  2124. }
  2125. for (i = 0; i < kNumExtensions; i++) {
  2126. const size_t len_before = CBB_len(&extensions);
  2127. if (!kExtensions[i].add_clienthello(ssl, &extensions)) {
  2128. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2129. ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
  2130. goto err;
  2131. }
  2132. if (CBB_len(&extensions) != len_before) {
  2133. ssl->s3->tmp.extensions.sent |= (1u << i);
  2134. }
  2135. }
  2136. if (!custom_ext_add_clienthello(ssl, &extensions)) {
  2137. goto err;
  2138. }
  2139. if (!SSL_is_dtls(ssl)) {
  2140. header_len += 2 + CBB_len(&extensions);
  2141. if (header_len > 0xff && header_len < 0x200) {
  2142. /* Add padding to workaround bugs in F5 terminators. See RFC 7685.
  2143. *
  2144. * NB: because this code works out the length of all existing extensions
  2145. * it MUST always appear last. */
  2146. size_t padding_len = 0x200 - header_len;
  2147. /* Extensions take at least four bytes to encode. Always include at least
  2148. * one byte of data if including the extension. WebSphere Application
  2149. * Server 7.0 is intolerant to the last extension being zero-length. See
  2150. * https://crbug.com/363583. */
  2151. if (padding_len >= 4 + 1) {
  2152. padding_len -= 4;
  2153. } else {
  2154. padding_len = 1;
  2155. }
  2156. uint8_t *padding_bytes;
  2157. if (!CBB_add_u16(&extensions, TLSEXT_TYPE_padding) ||
  2158. !CBB_add_u16(&extensions, padding_len) ||
  2159. !CBB_add_space(&extensions, &padding_bytes, padding_len)) {
  2160. goto err;
  2161. }
  2162. memset(padding_bytes, 0, padding_len);
  2163. }
  2164. }
  2165. /* Discard empty extensions blocks. */
  2166. if (CBB_len(&extensions) == 0) {
  2167. CBB_discard_child(out);
  2168. }
  2169. return CBB_flush(out);
  2170. err:
  2171. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2172. return 0;
  2173. }
  2174. int ssl_add_serverhello_tlsext(SSL *ssl, CBB *out) {
  2175. CBB extensions;
  2176. if (!CBB_add_u16_length_prefixed(out, &extensions)) {
  2177. goto err;
  2178. }
  2179. unsigned i;
  2180. for (i = 0; i < kNumExtensions; i++) {
  2181. if (!(ssl->s3->tmp.extensions.received & (1u << i))) {
  2182. /* Don't send extensions that were not received. */
  2183. continue;
  2184. }
  2185. if (!kExtensions[i].add_serverhello(ssl, &extensions)) {
  2186. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_ADDING_EXTENSION);
  2187. ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
  2188. goto err;
  2189. }
  2190. }
  2191. if (!custom_ext_add_serverhello(ssl, &extensions)) {
  2192. goto err;
  2193. }
  2194. /* Discard empty extensions blocks before TLS 1.3. */
  2195. if (ssl3_protocol_version(ssl) < TLS1_3_VERSION &&
  2196. CBB_len(&extensions) == 0) {
  2197. CBB_discard_child(out);
  2198. }
  2199. return CBB_flush(out);
  2200. err:
  2201. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2202. return 0;
  2203. }
  2204. static int ssl_scan_clienthello_tlsext(
  2205. SSL *ssl, const struct ssl_early_callback_ctx *client_hello,
  2206. int *out_alert) {
  2207. for (size_t i = 0; i < kNumExtensions; i++) {
  2208. if (kExtensions[i].init != NULL) {
  2209. kExtensions[i].init(ssl);
  2210. }
  2211. }
  2212. ssl->s3->tmp.extensions.received = 0;
  2213. ssl->s3->tmp.custom_extensions.received = 0;
  2214. CBS extensions;
  2215. CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len);
  2216. while (CBS_len(&extensions) != 0) {
  2217. uint16_t type;
  2218. CBS extension;
  2219. /* Decode the next extension. */
  2220. if (!CBS_get_u16(&extensions, &type) ||
  2221. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2222. *out_alert = SSL_AD_DECODE_ERROR;
  2223. return 0;
  2224. }
  2225. /* RFC 5746 made the existence of extensions in SSL 3.0 somewhat
  2226. * ambiguous. Ignore all but the renegotiation_info extension. */
  2227. if (ssl->version == SSL3_VERSION && type != TLSEXT_TYPE_renegotiate) {
  2228. continue;
  2229. }
  2230. unsigned ext_index;
  2231. const struct tls_extension *const ext =
  2232. tls_extension_find(&ext_index, type);
  2233. if (ext == NULL) {
  2234. if (!custom_ext_parse_clienthello(ssl, out_alert, type, &extension)) {
  2235. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2236. return 0;
  2237. }
  2238. continue;
  2239. }
  2240. ssl->s3->tmp.extensions.received |= (1u << ext_index);
  2241. uint8_t alert = SSL_AD_DECODE_ERROR;
  2242. if (!ext->parse_clienthello(ssl, &alert, &extension)) {
  2243. *out_alert = alert;
  2244. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2245. ERR_add_error_dataf("extension: %u", (unsigned)type);
  2246. return 0;
  2247. }
  2248. }
  2249. for (size_t i = 0; i < kNumExtensions; i++) {
  2250. if (ssl->s3->tmp.extensions.received & (1u << i)) {
  2251. continue;
  2252. }
  2253. CBS *contents = NULL, fake_contents;
  2254. static const uint8_t kFakeRenegotiateExtension[] = {0};
  2255. if (kExtensions[i].value == TLSEXT_TYPE_renegotiate &&
  2256. ssl_client_cipher_list_contains_cipher(client_hello,
  2257. SSL3_CK_SCSV & 0xffff)) {
  2258. /* The renegotiation SCSV was received so pretend that we received a
  2259. * renegotiation extension. */
  2260. CBS_init(&fake_contents, kFakeRenegotiateExtension,
  2261. sizeof(kFakeRenegotiateExtension));
  2262. contents = &fake_contents;
  2263. ssl->s3->tmp.extensions.received |= (1u << i);
  2264. }
  2265. /* Extension wasn't observed so call the callback with a NULL
  2266. * parameter. */
  2267. uint8_t alert = SSL_AD_DECODE_ERROR;
  2268. if (!kExtensions[i].parse_clienthello(ssl, &alert, contents)) {
  2269. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2270. ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
  2271. *out_alert = alert;
  2272. return 0;
  2273. }
  2274. }
  2275. return 1;
  2276. }
  2277. int ssl_parse_clienthello_tlsext(
  2278. SSL *ssl, const struct ssl_early_callback_ctx *client_hello) {
  2279. int alert = -1;
  2280. if (ssl_scan_clienthello_tlsext(ssl, client_hello, &alert) <= 0) {
  2281. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  2282. return 0;
  2283. }
  2284. if (ssl_check_clienthello_tlsext(ssl) <= 0) {
  2285. OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_TLSEXT);
  2286. return 0;
  2287. }
  2288. return 1;
  2289. }
  2290. OPENSSL_COMPILE_ASSERT(kNumExtensions <= sizeof(uint32_t) * 8, too_many_bits);
  2291. static int ssl_scan_serverhello_tlsext(SSL *ssl, CBS *cbs, int *out_alert) {
  2292. /* Before TLS 1.3, ServerHello extensions blocks may be omitted if empty. */
  2293. if (CBS_len(cbs) == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  2294. return 1;
  2295. }
  2296. /* Decode the extensions block and check it is valid. */
  2297. CBS extensions;
  2298. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  2299. !tls1_check_duplicate_extensions(&extensions)) {
  2300. *out_alert = SSL_AD_DECODE_ERROR;
  2301. return 0;
  2302. }
  2303. uint32_t received = 0;
  2304. while (CBS_len(&extensions) != 0) {
  2305. uint16_t type;
  2306. CBS extension;
  2307. /* Decode the next extension. */
  2308. if (!CBS_get_u16(&extensions, &type) ||
  2309. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  2310. *out_alert = SSL_AD_DECODE_ERROR;
  2311. return 0;
  2312. }
  2313. unsigned ext_index;
  2314. const struct tls_extension *const ext =
  2315. tls_extension_find(&ext_index, type);
  2316. if (ext == NULL) {
  2317. if (!custom_ext_parse_serverhello(ssl, out_alert, type, &extension)) {
  2318. return 0;
  2319. }
  2320. continue;
  2321. }
  2322. if (!(ssl->s3->tmp.extensions.sent & (1u << ext_index)) &&
  2323. type != TLSEXT_TYPE_renegotiate) {
  2324. /* If the extension was never sent then it is illegal, except for the
  2325. * renegotiation extension which, in SSL 3.0, is signaled via SCSV. */
  2326. OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
  2327. ERR_add_error_dataf("extension :%u", (unsigned)type);
  2328. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  2329. return 0;
  2330. }
  2331. received |= (1u << ext_index);
  2332. uint8_t alert = SSL_AD_DECODE_ERROR;
  2333. if (!ext->parse_serverhello(ssl, &alert, &extension)) {
  2334. OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
  2335. ERR_add_error_dataf("extension: %u", (unsigned)type);
  2336. *out_alert = alert;
  2337. return 0;
  2338. }
  2339. }
  2340. size_t i;
  2341. for (i = 0; i < kNumExtensions; i++) {
  2342. if (!(received & (1u << i))) {
  2343. /* Extension wasn't observed so call the callback with a NULL
  2344. * parameter. */
  2345. uint8_t alert = SSL_AD_DECODE_ERROR;
  2346. if (!kExtensions[i].parse_serverhello(ssl, &alert, NULL)) {
  2347. OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_EXTENSION);
  2348. ERR_add_error_dataf("extension: %u", (unsigned)kExtensions[i].value);
  2349. *out_alert = alert;
  2350. return 0;
  2351. }
  2352. }
  2353. }
  2354. return 1;
  2355. }
  2356. static int ssl_check_clienthello_tlsext(SSL *ssl) {
  2357. int ret = SSL_TLSEXT_ERR_NOACK;
  2358. int al = SSL_AD_UNRECOGNIZED_NAME;
  2359. if (ssl->ctx->tlsext_servername_callback != 0) {
  2360. ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
  2361. ssl->ctx->tlsext_servername_arg);
  2362. } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
  2363. ret = ssl->initial_ctx->tlsext_servername_callback(
  2364. ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
  2365. }
  2366. switch (ret) {
  2367. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2368. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  2369. return -1;
  2370. case SSL_TLSEXT_ERR_ALERT_WARNING:
  2371. ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
  2372. return 1;
  2373. case SSL_TLSEXT_ERR_NOACK:
  2374. ssl->s3->tmp.should_ack_sni = 0;
  2375. return 1;
  2376. default:
  2377. return 1;
  2378. }
  2379. }
  2380. static int ssl_check_serverhello_tlsext(SSL *ssl) {
  2381. int ret = SSL_TLSEXT_ERR_OK;
  2382. int al = SSL_AD_UNRECOGNIZED_NAME;
  2383. if (ssl->ctx->tlsext_servername_callback != 0) {
  2384. ret = ssl->ctx->tlsext_servername_callback(ssl, &al,
  2385. ssl->ctx->tlsext_servername_arg);
  2386. } else if (ssl->initial_ctx->tlsext_servername_callback != 0) {
  2387. ret = ssl->initial_ctx->tlsext_servername_callback(
  2388. ssl, &al, ssl->initial_ctx->tlsext_servername_arg);
  2389. }
  2390. switch (ret) {
  2391. case SSL_TLSEXT_ERR_ALERT_FATAL:
  2392. ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
  2393. return -1;
  2394. case SSL_TLSEXT_ERR_ALERT_WARNING:
  2395. ssl3_send_alert(ssl, SSL3_AL_WARNING, al);
  2396. return 1;
  2397. default:
  2398. return 1;
  2399. }
  2400. }
  2401. int ssl_parse_serverhello_tlsext(SSL *ssl, CBS *cbs) {
  2402. int alert = -1;
  2403. if (ssl_scan_serverhello_tlsext(ssl, cbs, &alert) <= 0) {
  2404. ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
  2405. return 0;
  2406. }
  2407. if (ssl_check_serverhello_tlsext(ssl) <= 0) {
  2408. OPENSSL_PUT_ERROR(SSL, SSL_R_SERVERHELLO_TLSEXT);
  2409. return 0;
  2410. }
  2411. return 1;
  2412. }
  2413. int tls_process_ticket(SSL *ssl, SSL_SESSION **out_session,
  2414. int *out_renew_ticket, const uint8_t *ticket,
  2415. size_t ticket_len, const uint8_t *session_id,
  2416. size_t session_id_len) {
  2417. int ret = 1; /* Most errors are non-fatal. */
  2418. SSL_CTX *ssl_ctx = ssl->initial_ctx;
  2419. uint8_t *plaintext = NULL;
  2420. HMAC_CTX hmac_ctx;
  2421. HMAC_CTX_init(&hmac_ctx);
  2422. EVP_CIPHER_CTX cipher_ctx;
  2423. EVP_CIPHER_CTX_init(&cipher_ctx);
  2424. *out_renew_ticket = 0;
  2425. *out_session = NULL;
  2426. if (SSL_get_options(ssl) & SSL_OP_NO_TICKET) {
  2427. goto done;
  2428. }
  2429. if (session_id_len > SSL_MAX_SSL_SESSION_ID_LENGTH) {
  2430. goto done;
  2431. }
  2432. /* Ensure there is room for the key name and the largest IV
  2433. * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
  2434. * the maximum IV length should be well under the minimum size for the
  2435. * session material and HMAC. */
  2436. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + EVP_MAX_IV_LENGTH) {
  2437. goto done;
  2438. }
  2439. const uint8_t *iv = ticket + SSL_TICKET_KEY_NAME_LEN;
  2440. if (ssl_ctx->tlsext_ticket_key_cb != NULL) {
  2441. int cb_ret = ssl_ctx->tlsext_ticket_key_cb(
  2442. ssl, (uint8_t *)ticket /* name */, (uint8_t *)iv, &cipher_ctx,
  2443. &hmac_ctx, 0 /* decrypt */);
  2444. if (cb_ret < 0) {
  2445. ret = 0;
  2446. goto done;
  2447. }
  2448. if (cb_ret == 0) {
  2449. goto done;
  2450. }
  2451. if (cb_ret == 2) {
  2452. *out_renew_ticket = 1;
  2453. }
  2454. } else {
  2455. /* Check the key name matches. */
  2456. if (memcmp(ticket, ssl_ctx->tlsext_tick_key_name,
  2457. SSL_TICKET_KEY_NAME_LEN) != 0) {
  2458. goto done;
  2459. }
  2460. if (!HMAC_Init_ex(&hmac_ctx, ssl_ctx->tlsext_tick_hmac_key,
  2461. sizeof(ssl_ctx->tlsext_tick_hmac_key), tlsext_tick_md(),
  2462. NULL) ||
  2463. !EVP_DecryptInit_ex(&cipher_ctx, EVP_aes_128_cbc(), NULL,
  2464. ssl_ctx->tlsext_tick_aes_key, iv)) {
  2465. ret = 0;
  2466. goto done;
  2467. }
  2468. }
  2469. size_t iv_len = EVP_CIPHER_CTX_iv_length(&cipher_ctx);
  2470. /* Check the MAC at the end of the ticket. */
  2471. uint8_t mac[EVP_MAX_MD_SIZE];
  2472. size_t mac_len = HMAC_size(&hmac_ctx);
  2473. if (ticket_len < SSL_TICKET_KEY_NAME_LEN + iv_len + 1 + mac_len) {
  2474. /* The ticket must be large enough for key name, IV, data, and MAC. */
  2475. goto done;
  2476. }
  2477. HMAC_Update(&hmac_ctx, ticket, ticket_len - mac_len);
  2478. HMAC_Final(&hmac_ctx, mac, NULL);
  2479. if (CRYPTO_memcmp(mac, ticket + (ticket_len - mac_len), mac_len) != 0) {
  2480. goto done;
  2481. }
  2482. /* Decrypt the session data. */
  2483. const uint8_t *ciphertext = ticket + SSL_TICKET_KEY_NAME_LEN + iv_len;
  2484. size_t ciphertext_len = ticket_len - SSL_TICKET_KEY_NAME_LEN - iv_len -
  2485. mac_len;
  2486. plaintext = OPENSSL_malloc(ciphertext_len);
  2487. if (plaintext == NULL) {
  2488. ret = 0;
  2489. goto done;
  2490. }
  2491. if (ciphertext_len >= INT_MAX) {
  2492. goto done;
  2493. }
  2494. int len1, len2;
  2495. if (!EVP_DecryptUpdate(&cipher_ctx, plaintext, &len1, ciphertext,
  2496. (int)ciphertext_len) ||
  2497. !EVP_DecryptFinal_ex(&cipher_ctx, plaintext + len1, &len2)) {
  2498. ERR_clear_error(); /* Don't leave an error on the queue. */
  2499. goto done;
  2500. }
  2501. /* Decode the session. */
  2502. SSL_SESSION *session = SSL_SESSION_from_bytes(plaintext, len1 + len2);
  2503. if (session == NULL) {
  2504. ERR_clear_error(); /* Don't leave an error on the queue. */
  2505. goto done;
  2506. }
  2507. /* Copy the client's session ID into the new session, to denote the ticket has
  2508. * been accepted. */
  2509. memcpy(session->session_id, session_id, session_id_len);
  2510. session->session_id_length = session_id_len;
  2511. if (!ssl_session_is_context_valid(ssl, session) ||
  2512. !ssl_session_is_time_valid(ssl, session)) {
  2513. SSL_SESSION_free(session);
  2514. session = NULL;
  2515. }
  2516. *out_session = session;
  2517. done:
  2518. OPENSSL_free(plaintext);
  2519. HMAC_CTX_cleanup(&hmac_ctx);
  2520. EVP_CIPHER_CTX_cleanup(&cipher_ctx);
  2521. return ret;
  2522. }
  2523. int tls1_parse_peer_sigalgs(SSL *ssl, const CBS *in_sigalgs) {
  2524. /* Extension ignored for inappropriate versions */
  2525. if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
  2526. return 1;
  2527. }
  2528. SSL_HANDSHAKE *hs = ssl->s3->hs;
  2529. OPENSSL_free(hs->peer_sigalgs);
  2530. hs->peer_sigalgs = NULL;
  2531. hs->num_peer_sigalgs = 0;
  2532. size_t num_sigalgs = CBS_len(in_sigalgs);
  2533. if (num_sigalgs % 2 != 0) {
  2534. return 0;
  2535. }
  2536. num_sigalgs /= 2;
  2537. /* supported_signature_algorithms in the certificate request is
  2538. * allowed to be empty. */
  2539. if (num_sigalgs == 0) {
  2540. return 1;
  2541. }
  2542. /* This multiplication doesn't overflow because sizeof(uint16_t) is two
  2543. * and we just divided |num_sigalgs| by two. */
  2544. hs->peer_sigalgs = OPENSSL_malloc(num_sigalgs * sizeof(uint16_t));
  2545. if (hs->peer_sigalgs == NULL) {
  2546. return 0;
  2547. }
  2548. hs->num_peer_sigalgs = num_sigalgs;
  2549. CBS sigalgs;
  2550. CBS_init(&sigalgs, CBS_data(in_sigalgs), CBS_len(in_sigalgs));
  2551. for (size_t i = 0; i < num_sigalgs; i++) {
  2552. if (!CBS_get_u16(&sigalgs, &hs->peer_sigalgs[i])) {
  2553. return 0;
  2554. }
  2555. }
  2556. return 1;
  2557. }
  2558. int tls1_choose_signature_algorithm(SSL *ssl, uint16_t *out) {
  2559. CERT *cert = ssl->cert;
  2560. SSL_HANDSHAKE *hs = ssl->s3->hs;
  2561. /* Before TLS 1.2, the signature algorithm isn't negotiated as part of the
  2562. * handshake. It is fixed at MD5-SHA1 for RSA and SHA1 for ECDSA. */
  2563. if (ssl3_protocol_version(ssl) < TLS1_2_VERSION) {
  2564. int type = ssl_private_key_type(ssl);
  2565. if (type == NID_rsaEncryption) {
  2566. *out = SSL_SIGN_RSA_PKCS1_MD5_SHA1;
  2567. return 1;
  2568. }
  2569. if (ssl_is_ecdsa_key_type(type)) {
  2570. *out = SSL_SIGN_ECDSA_SHA1;
  2571. return 1;
  2572. }
  2573. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  2574. return 0;
  2575. }
  2576. const uint16_t *sigalgs;
  2577. size_t num_sigalgs = tls12_get_psigalgs(ssl, &sigalgs);
  2578. if (cert->sigalgs != NULL) {
  2579. sigalgs = cert->sigalgs;
  2580. num_sigalgs = cert->num_sigalgs;
  2581. }
  2582. const uint16_t *peer_sigalgs = hs->peer_sigalgs;
  2583. size_t num_peer_sigalgs = hs->num_peer_sigalgs;
  2584. if (num_peer_sigalgs == 0 && ssl3_protocol_version(ssl) < TLS1_3_VERSION) {
  2585. /* If the client didn't specify any signature_algorithms extension then
  2586. * we can assume that it supports SHA1. See
  2587. * http://tools.ietf.org/html/rfc5246#section-7.4.1.4.1 */
  2588. static const uint16_t kDefaultPeerAlgorithms[] = {SSL_SIGN_RSA_PKCS1_SHA1,
  2589. SSL_SIGN_ECDSA_SHA1};
  2590. peer_sigalgs = kDefaultPeerAlgorithms;
  2591. num_peer_sigalgs = OPENSSL_ARRAY_SIZE(kDefaultPeerAlgorithms);
  2592. }
  2593. for (size_t i = 0; i < num_sigalgs; i++) {
  2594. uint16_t sigalg = sigalgs[i];
  2595. /* SSL_SIGN_RSA_PKCS1_MD5_SHA1 is an internal value and should never be
  2596. * negotiated. */
  2597. if (sigalg == SSL_SIGN_RSA_PKCS1_MD5_SHA1 ||
  2598. !ssl_private_key_supports_signature_algorithm(ssl, sigalgs[i])) {
  2599. continue;
  2600. }
  2601. for (size_t j = 0; j < num_peer_sigalgs; j++) {
  2602. if (sigalg == peer_sigalgs[j]) {
  2603. *out = sigalg;
  2604. return 1;
  2605. }
  2606. }
  2607. }
  2608. OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMMON_SIGNATURE_ALGORITHMS);
  2609. return 0;
  2610. }
  2611. int tls1_channel_id_hash(SSL *ssl, uint8_t *out, size_t *out_len) {
  2612. int ret = 0;
  2613. EVP_MD_CTX ctx;
  2614. EVP_MD_CTX_init(&ctx);
  2615. if (!EVP_DigestInit_ex(&ctx, EVP_sha256(), NULL)) {
  2616. goto err;
  2617. }
  2618. static const char kClientIDMagic[] = "TLS Channel ID signature";
  2619. EVP_DigestUpdate(&ctx, kClientIDMagic, sizeof(kClientIDMagic));
  2620. if (ssl->session != NULL) {
  2621. static const char kResumptionMagic[] = "Resumption";
  2622. EVP_DigestUpdate(&ctx, kResumptionMagic, sizeof(kResumptionMagic));
  2623. if (ssl->session->original_handshake_hash_len == 0) {
  2624. OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
  2625. goto err;
  2626. }
  2627. EVP_DigestUpdate(&ctx, ssl->session->original_handshake_hash,
  2628. ssl->session->original_handshake_hash_len);
  2629. }
  2630. uint8_t handshake_hash[EVP_MAX_MD_SIZE];
  2631. int handshake_hash_len = tls1_handshake_digest(ssl, handshake_hash,
  2632. sizeof(handshake_hash));
  2633. if (handshake_hash_len < 0) {
  2634. goto err;
  2635. }
  2636. EVP_DigestUpdate(&ctx, handshake_hash, (size_t)handshake_hash_len);
  2637. unsigned len_u;
  2638. EVP_DigestFinal_ex(&ctx, out, &len_u);
  2639. *out_len = len_u;
  2640. ret = 1;
  2641. err:
  2642. EVP_MD_CTX_cleanup(&ctx);
  2643. return ret;
  2644. }
  2645. /* tls1_record_handshake_hashes_for_channel_id records the current handshake
  2646. * hashes in |ssl->s3->new_session| so that Channel ID resumptions can sign that
  2647. * data. */
  2648. int tls1_record_handshake_hashes_for_channel_id(SSL *ssl) {
  2649. int digest_len;
  2650. /* This function should never be called for a resumed session because the
  2651. * handshake hashes that we wish to record are for the original, full
  2652. * handshake. */
  2653. if (ssl->session != NULL) {
  2654. return -1;
  2655. }
  2656. digest_len =
  2657. tls1_handshake_digest(
  2658. ssl, ssl->s3->new_session->original_handshake_hash,
  2659. sizeof(ssl->s3->new_session->original_handshake_hash));
  2660. if (digest_len < 0) {
  2661. return -1;
  2662. }
  2663. ssl->s3->new_session->original_handshake_hash_len = digest_len;
  2664. return 1;
  2665. }