Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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