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

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