You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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