25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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