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

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