Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

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