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

2637 行
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_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);
  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. /* header_len is the length of the ClientHello header written so far, used to
  692. * compute padding. It does not include the record header. Pass 0 if no padding
  693. * is to be done. */
  694. uint8_t *ssl_add_clienthello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit,
  695. size_t header_len) {
  696. int extdatalen = 0;
  697. uint8_t *ret = buf;
  698. uint8_t *orig = buf;
  699. /* See if we support any ECC ciphersuites */
  700. int using_ecc = 0;
  701. if (s->version >= TLS1_VERSION || SSL_IS_DTLS(s)) {
  702. size_t i;
  703. uint32_t alg_k, alg_a;
  704. STACK_OF(SSL_CIPHER) *cipher_stack = SSL_get_ciphers(s);
  705. for (i = 0; i < sk_SSL_CIPHER_num(cipher_stack); i++) {
  706. const SSL_CIPHER *c = sk_SSL_CIPHER_value(cipher_stack, i);
  707. alg_k = c->algorithm_mkey;
  708. alg_a = c->algorithm_auth;
  709. if ((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) {
  710. using_ecc = 1;
  711. break;
  712. }
  713. }
  714. }
  715. /* don't add extensions for SSLv3 unless doing secure renegotiation */
  716. if (s->client_version == SSL3_VERSION && !s->s3->send_connection_binding) {
  717. return orig;
  718. }
  719. ret += 2;
  720. if (ret >= limit) {
  721. return NULL; /* should never occur. */
  722. }
  723. if (s->tlsext_hostname != NULL) {
  724. /* Add TLS extension servername to the Client Hello message */
  725. unsigned long size_str;
  726. long lenmax;
  727. /* check for enough space.
  728. 4 for the servername type and entension length
  729. 2 for servernamelist length
  730. 1 for the hostname type
  731. 2 for hostname length
  732. + hostname length */
  733. lenmax = limit - ret - 9;
  734. size_str = strlen(s->tlsext_hostname);
  735. if (lenmax < 0 || size_str > (unsigned long)lenmax) {
  736. return NULL;
  737. }
  738. /* extension type and length */
  739. s2n(TLSEXT_TYPE_server_name, ret);
  740. s2n(size_str + 5, ret);
  741. /* length of servername list */
  742. s2n(size_str + 3, ret);
  743. /* hostname type, length and hostname */
  744. *(ret++) = (uint8_t)TLSEXT_NAMETYPE_host_name;
  745. s2n(size_str, ret);
  746. memcpy(ret, s->tlsext_hostname, size_str);
  747. ret += size_str;
  748. }
  749. /* Add RI if renegotiating */
  750. if (s->s3->initial_handshake_complete) {
  751. int el;
  752. if (!ssl_add_clienthello_renegotiate_ext(s, 0, &el, 0)) {
  753. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  754. return NULL;
  755. }
  756. if ((limit - ret - 4 - el) < 0) {
  757. return NULL;
  758. }
  759. s2n(TLSEXT_TYPE_renegotiate, ret);
  760. s2n(el, ret);
  761. if (!ssl_add_clienthello_renegotiate_ext(s, ret, &el, el)) {
  762. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  763. return NULL;
  764. }
  765. ret += el;
  766. }
  767. /* Add extended master secret. */
  768. if (s->version != SSL3_VERSION) {
  769. if (limit - ret - 4 < 0) {
  770. return NULL;
  771. }
  772. s2n(TLSEXT_TYPE_extended_master_secret, ret);
  773. s2n(0, ret);
  774. }
  775. if (!(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
  776. int ticklen = 0;
  777. /* Renegotiation does not participate in session resumption. However, still
  778. * advertise the extension to avoid potentially breaking servers which carry
  779. * over the state from the previous handshake, such as OpenSSL servers
  780. * without upstream's 3c3f0259238594d77264a78944d409f2127642c4. */
  781. if (!s->s3->initial_handshake_complete && s->session != NULL &&
  782. s->session->tlsext_tick != NULL) {
  783. ticklen = s->session->tlsext_ticklen;
  784. }
  785. /* Check for enough room 2 for extension type, 2 for len rest for
  786. * ticket. */
  787. if ((long)(limit - ret - 4 - ticklen) < 0) {
  788. return NULL;
  789. }
  790. s2n(TLSEXT_TYPE_session_ticket, ret);
  791. s2n(ticklen, ret);
  792. if (ticklen) {
  793. memcpy(ret, s->session->tlsext_tick, ticklen);
  794. ret += ticklen;
  795. }
  796. }
  797. if (ssl3_version_from_wire(s, s->client_version) >= TLS1_2_VERSION) {
  798. size_t salglen;
  799. const uint8_t *salg;
  800. salglen = tls12_get_psigalgs(s, &salg);
  801. if ((size_t)(limit - ret) < salglen + 6) {
  802. return NULL;
  803. }
  804. s2n(TLSEXT_TYPE_signature_algorithms, ret);
  805. s2n(salglen + 2, ret);
  806. s2n(salglen, ret);
  807. memcpy(ret, salg, salglen);
  808. ret += salglen;
  809. }
  810. if (s->ocsp_stapling_enabled) {
  811. /* The status_request extension is excessively extensible at every layer.
  812. * On the client, only support requesting OCSP responses with an empty
  813. * responder_id_list and no extensions. */
  814. if (limit - ret - 4 - 1 - 2 - 2 < 0) {
  815. return NULL;
  816. }
  817. s2n(TLSEXT_TYPE_status_request, ret);
  818. s2n(1 + 2 + 2, ret);
  819. /* status_type */
  820. *(ret++) = TLSEXT_STATUSTYPE_ocsp;
  821. /* responder_id_list - empty */
  822. s2n(0, ret);
  823. /* request_extensions - empty */
  824. s2n(0, ret);
  825. }
  826. if (s->ctx->next_proto_select_cb && !s->s3->initial_handshake_complete &&
  827. !SSL_IS_DTLS(s)) {
  828. /* The client advertises an emtpy extension to indicate its support for
  829. * Next Protocol Negotiation */
  830. if (limit - ret - 4 < 0) {
  831. return NULL;
  832. }
  833. s2n(TLSEXT_TYPE_next_proto_neg, ret);
  834. s2n(0, ret);
  835. }
  836. if (s->signed_cert_timestamps_enabled) {
  837. /* The client advertises an empty extension to indicate its support for
  838. * certificate timestamps. */
  839. if (limit - ret - 4 < 0) {
  840. return NULL;
  841. }
  842. s2n(TLSEXT_TYPE_certificate_timestamp, ret);
  843. s2n(0, ret);
  844. }
  845. if (s->alpn_client_proto_list && !s->s3->initial_handshake_complete) {
  846. if ((size_t)(limit - ret) < 6 + s->alpn_client_proto_list_len) {
  847. return NULL;
  848. }
  849. s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
  850. s2n(2 + s->alpn_client_proto_list_len, ret);
  851. s2n(s->alpn_client_proto_list_len, ret);
  852. memcpy(ret, s->alpn_client_proto_list, s->alpn_client_proto_list_len);
  853. ret += s->alpn_client_proto_list_len;
  854. }
  855. if (s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
  856. /* The client advertises an emtpy extension to indicate its support for
  857. * Channel ID. */
  858. if (limit - ret - 4 < 0) {
  859. return NULL;
  860. }
  861. if (s->ctx->tlsext_channel_id_enabled_new) {
  862. s2n(TLSEXT_TYPE_channel_id_new, ret);
  863. } else {
  864. s2n(TLSEXT_TYPE_channel_id, ret);
  865. }
  866. s2n(0, ret);
  867. }
  868. if (SSL_get_srtp_profiles(s)) {
  869. int el;
  870. ssl_add_clienthello_use_srtp_ext(s, 0, &el, 0);
  871. if ((limit - ret - 4 - el) < 0) {
  872. return NULL;
  873. }
  874. s2n(TLSEXT_TYPE_use_srtp, ret);
  875. s2n(el, ret);
  876. if (!ssl_add_clienthello_use_srtp_ext(s, ret, &el, el)) {
  877. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  878. return NULL;
  879. }
  880. ret += el;
  881. }
  882. if (using_ecc) {
  883. /* Add TLS extension ECPointFormats to the ClientHello message */
  884. long lenmax;
  885. const uint8_t *formats;
  886. const uint16_t *curves;
  887. size_t formats_len, curves_len, i;
  888. tls1_get_formatlist(s, &formats, &formats_len);
  889. lenmax = limit - ret - 5;
  890. if (lenmax < 0) {
  891. return NULL;
  892. }
  893. if (formats_len > (size_t)lenmax) {
  894. return NULL;
  895. }
  896. if (formats_len > 255) {
  897. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  898. return NULL;
  899. }
  900. s2n(TLSEXT_TYPE_ec_point_formats, ret);
  901. s2n(formats_len + 1, ret);
  902. *(ret++) = (uint8_t)formats_len;
  903. memcpy(ret, formats, formats_len);
  904. ret += formats_len;
  905. /* Add TLS extension EllipticCurves to the ClientHello message */
  906. tls1_get_curvelist(s, 0, &curves, &curves_len);
  907. lenmax = limit - ret - 6;
  908. if (lenmax < 0) {
  909. return NULL;
  910. }
  911. if (curves_len * 2 > (size_t)lenmax) {
  912. return NULL;
  913. }
  914. if (curves_len * 2 > 65532) {
  915. OPENSSL_PUT_ERROR(SSL, ssl_add_clienthello_tlsext, ERR_R_INTERNAL_ERROR);
  916. return NULL;
  917. }
  918. s2n(TLSEXT_TYPE_elliptic_curves, ret);
  919. s2n((curves_len * 2) + 2, ret);
  920. s2n(curves_len * 2, ret);
  921. for (i = 0; i < curves_len; i++) {
  922. s2n(curves[i], ret);
  923. }
  924. }
  925. if (header_len > 0) {
  926. size_t clienthello_minsize = 0;
  927. header_len += ret - orig;
  928. if (header_len > 0xff && header_len < 0x200) {
  929. /* Add padding to workaround bugs in F5 terminators. See
  930. * https://tools.ietf.org/html/draft-agl-tls-padding-03
  931. *
  932. * NB: because this code works out the length of all existing extensions
  933. * it MUST always appear last. */
  934. clienthello_minsize = 0x200;
  935. }
  936. if (s->fastradio_padding) {
  937. /* Pad the ClientHello record to 1024 bytes to fast forward the radio
  938. * into DCH (high data rate) state in 3G networks. Note that when
  939. * fastradio_padding is enabled, even if the header_len is less than 255
  940. * bytes, the padding will be applied regardless. This is slightly
  941. * different from the TLS padding extension suggested in
  942. * https://tools.ietf.org/html/draft-agl-tls-padding-03 */
  943. clienthello_minsize = 0x400;
  944. }
  945. if (header_len < clienthello_minsize) {
  946. size_t padding_len = clienthello_minsize - header_len;
  947. /* Extensions take at least four bytes to encode. Always include least
  948. * one byte of data if including the extension. WebSphere Application
  949. * Server 7.0 is intolerant to the last extension being zero-length. */
  950. if (padding_len >= 4 + 1) {
  951. padding_len -= 4;
  952. } else {
  953. padding_len = 1;
  954. }
  955. if (limit - ret - 4 - (long)padding_len < 0) {
  956. return NULL;
  957. }
  958. s2n(TLSEXT_TYPE_padding, ret);
  959. s2n(padding_len, ret);
  960. memset(ret, 0, padding_len);
  961. ret += padding_len;
  962. }
  963. }
  964. extdatalen = ret - orig - 2;
  965. if (extdatalen == 0) {
  966. return orig;
  967. }
  968. s2n(extdatalen, orig);
  969. return ret;
  970. }
  971. uint8_t *ssl_add_serverhello_tlsext(SSL *s, uint8_t *buf, uint8_t *limit) {
  972. int extdatalen = 0;
  973. uint8_t *orig = buf;
  974. uint8_t *ret = buf;
  975. int next_proto_neg_seen;
  976. uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  977. uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  978. int using_ecc = (alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA);
  979. using_ecc = using_ecc && (s->s3->tmp.peer_ecpointformatlist != NULL);
  980. /* don't add extensions for SSLv3, unless doing secure renegotiation */
  981. if (s->version == SSL3_VERSION && !s->s3->send_connection_binding) {
  982. return orig;
  983. }
  984. ret += 2;
  985. if (ret >= limit) {
  986. return NULL; /* should never happen. */
  987. }
  988. if (!s->hit && s->should_ack_sni && s->session->tlsext_hostname != NULL) {
  989. if ((long)(limit - ret - 4) < 0) {
  990. return NULL;
  991. }
  992. s2n(TLSEXT_TYPE_server_name, ret);
  993. s2n(0, ret);
  994. }
  995. if (s->s3->send_connection_binding) {
  996. int el;
  997. if (!ssl_add_serverhello_renegotiate_ext(s, 0, &el, 0)) {
  998. OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
  999. return NULL;
  1000. }
  1001. if ((limit - ret - 4 - el) < 0) {
  1002. return NULL;
  1003. }
  1004. s2n(TLSEXT_TYPE_renegotiate, ret);
  1005. s2n(el, ret);
  1006. if (!ssl_add_serverhello_renegotiate_ext(s, ret, &el, el)) {
  1007. OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
  1008. return NULL;
  1009. }
  1010. ret += el;
  1011. }
  1012. if (s->s3->tmp.extended_master_secret) {
  1013. if ((long)(limit - ret - 4) < 0) {
  1014. return NULL;
  1015. }
  1016. s2n(TLSEXT_TYPE_extended_master_secret, ret);
  1017. s2n(0, ret);
  1018. }
  1019. if (using_ecc) {
  1020. const uint8_t *plist;
  1021. size_t plistlen;
  1022. /* Add TLS extension ECPointFormats to the ServerHello message */
  1023. long lenmax;
  1024. tls1_get_formatlist(s, &plist, &plistlen);
  1025. lenmax = limit - ret - 5;
  1026. if (lenmax < 0) {
  1027. return NULL;
  1028. }
  1029. if (plistlen > (size_t)lenmax) {
  1030. return NULL;
  1031. }
  1032. if (plistlen > 255) {
  1033. OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
  1034. return NULL;
  1035. }
  1036. s2n(TLSEXT_TYPE_ec_point_formats, ret);
  1037. s2n(plistlen + 1, ret);
  1038. *(ret++) = (uint8_t)plistlen;
  1039. memcpy(ret, plist, plistlen);
  1040. ret += plistlen;
  1041. }
  1042. /* Currently the server should not respond with a SupportedCurves extension */
  1043. if (s->tlsext_ticket_expected && !(SSL_get_options(s) & SSL_OP_NO_TICKET)) {
  1044. if ((long)(limit - ret - 4) < 0) {
  1045. return NULL;
  1046. }
  1047. s2n(TLSEXT_TYPE_session_ticket, ret);
  1048. s2n(0, ret);
  1049. }
  1050. if (s->s3->tmp.certificate_status_expected) {
  1051. if ((long)(limit - ret - 4) < 0) {
  1052. return NULL;
  1053. }
  1054. s2n(TLSEXT_TYPE_status_request, ret);
  1055. s2n(0, ret);
  1056. }
  1057. if (s->srtp_profile) {
  1058. int el;
  1059. ssl_add_serverhello_use_srtp_ext(s, 0, &el, 0);
  1060. if ((limit - ret - 4 - el) < 0) {
  1061. return NULL;
  1062. }
  1063. s2n(TLSEXT_TYPE_use_srtp, ret);
  1064. s2n(el, ret);
  1065. if (!ssl_add_serverhello_use_srtp_ext(s, ret, &el, el)) {
  1066. OPENSSL_PUT_ERROR(SSL, ssl_add_serverhello_tlsext, ERR_R_INTERNAL_ERROR);
  1067. return NULL;
  1068. }
  1069. ret += el;
  1070. }
  1071. next_proto_neg_seen = s->s3->next_proto_neg_seen;
  1072. s->s3->next_proto_neg_seen = 0;
  1073. if (next_proto_neg_seen && s->ctx->next_protos_advertised_cb) {
  1074. const uint8_t *npa;
  1075. unsigned int npalen;
  1076. int r;
  1077. r = s->ctx->next_protos_advertised_cb(
  1078. s, &npa, &npalen, s->ctx->next_protos_advertised_cb_arg);
  1079. if (r == SSL_TLSEXT_ERR_OK) {
  1080. if ((long)(limit - ret - 4 - npalen) < 0) {
  1081. return NULL;
  1082. }
  1083. s2n(TLSEXT_TYPE_next_proto_neg, ret);
  1084. s2n(npalen, ret);
  1085. memcpy(ret, npa, npalen);
  1086. ret += npalen;
  1087. s->s3->next_proto_neg_seen = 1;
  1088. }
  1089. }
  1090. if (s->s3->alpn_selected) {
  1091. const uint8_t *selected = s->s3->alpn_selected;
  1092. size_t len = s->s3->alpn_selected_len;
  1093. if ((long)(limit - ret - 4 - 2 - 1 - len) < 0) {
  1094. return NULL;
  1095. }
  1096. s2n(TLSEXT_TYPE_application_layer_protocol_negotiation, ret);
  1097. s2n(3 + len, ret);
  1098. s2n(1 + len, ret);
  1099. *ret++ = len;
  1100. memcpy(ret, selected, len);
  1101. ret += len;
  1102. }
  1103. /* If the client advertised support for Channel ID, and we have it
  1104. * enabled, then we want to echo it back. */
  1105. if (s->s3->tlsext_channel_id_valid) {
  1106. if (limit - ret - 4 < 0) {
  1107. return NULL;
  1108. }
  1109. if (s->s3->tlsext_channel_id_new) {
  1110. s2n(TLSEXT_TYPE_channel_id_new, ret);
  1111. } else {
  1112. s2n(TLSEXT_TYPE_channel_id, ret);
  1113. }
  1114. s2n(0, ret);
  1115. }
  1116. extdatalen = ret - orig - 2;
  1117. if (extdatalen == 0) {
  1118. return orig;
  1119. }
  1120. s2n(extdatalen, orig);
  1121. return ret;
  1122. }
  1123. /* tls1_alpn_handle_client_hello is called to process the ALPN extension in a
  1124. * ClientHello.
  1125. * cbs: the contents of the extension, not including the type and length.
  1126. * out_alert: a pointer to the alert value to send in the event of a zero
  1127. * return.
  1128. *
  1129. * returns: 1 on success. */
  1130. static int tls1_alpn_handle_client_hello(SSL *s, CBS *cbs, int *out_alert) {
  1131. CBS protocol_name_list, protocol_name_list_copy;
  1132. const uint8_t *selected;
  1133. uint8_t selected_len;
  1134. int r;
  1135. if (s->ctx->alpn_select_cb == NULL) {
  1136. return 1;
  1137. }
  1138. if (!CBS_get_u16_length_prefixed(cbs, &protocol_name_list) ||
  1139. CBS_len(cbs) != 0 || CBS_len(&protocol_name_list) < 2) {
  1140. goto parse_error;
  1141. }
  1142. /* Validate the protocol list. */
  1143. protocol_name_list_copy = protocol_name_list;
  1144. while (CBS_len(&protocol_name_list_copy) > 0) {
  1145. CBS protocol_name;
  1146. if (!CBS_get_u8_length_prefixed(&protocol_name_list_copy, &protocol_name)) {
  1147. goto parse_error;
  1148. }
  1149. }
  1150. r = s->ctx->alpn_select_cb(
  1151. s, &selected, &selected_len, CBS_data(&protocol_name_list),
  1152. CBS_len(&protocol_name_list), s->ctx->alpn_select_cb_arg);
  1153. if (r == SSL_TLSEXT_ERR_OK) {
  1154. OPENSSL_free(s->s3->alpn_selected);
  1155. s->s3->alpn_selected = BUF_memdup(selected, selected_len);
  1156. if (!s->s3->alpn_selected) {
  1157. *out_alert = SSL_AD_INTERNAL_ERROR;
  1158. return 0;
  1159. }
  1160. s->s3->alpn_selected_len = selected_len;
  1161. }
  1162. return 1;
  1163. parse_error:
  1164. *out_alert = SSL_AD_DECODE_ERROR;
  1165. return 0;
  1166. }
  1167. static int ssl_scan_clienthello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
  1168. int renegotiate_seen = 0;
  1169. CBS extensions;
  1170. s->should_ack_sni = 0;
  1171. s->srtp_profile = NULL;
  1172. s->s3->next_proto_neg_seen = 0;
  1173. s->s3->tmp.certificate_status_expected = 0;
  1174. s->s3->tmp.extended_master_secret = 0;
  1175. OPENSSL_free(s->s3->alpn_selected);
  1176. s->s3->alpn_selected = NULL;
  1177. /* Clear any signature algorithms extension received */
  1178. OPENSSL_free(s->cert->peer_sigalgs);
  1179. s->cert->peer_sigalgs = NULL;
  1180. s->cert->peer_sigalgslen = 0;
  1181. /* Clear any shared signature algorithms */
  1182. OPENSSL_free(s->cert->shared_sigalgs);
  1183. s->cert->shared_sigalgs = NULL;
  1184. s->cert->shared_sigalgslen = 0;
  1185. /* Clear ECC extensions */
  1186. OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
  1187. s->s3->tmp.peer_ecpointformatlist = NULL;
  1188. s->s3->tmp.peer_ecpointformatlist_length = 0;
  1189. OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
  1190. s->s3->tmp.peer_ellipticcurvelist = NULL;
  1191. s->s3->tmp.peer_ellipticcurvelist_length = 0;
  1192. /* There may be no extensions. */
  1193. if (CBS_len(cbs) == 0) {
  1194. goto ri_check;
  1195. }
  1196. /* Decode the extensions block and check it is valid. */
  1197. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  1198. !tls1_check_duplicate_extensions(&extensions)) {
  1199. *out_alert = SSL_AD_DECODE_ERROR;
  1200. return 0;
  1201. }
  1202. while (CBS_len(&extensions) != 0) {
  1203. uint16_t type;
  1204. CBS extension;
  1205. /* Decode the next extension. */
  1206. if (!CBS_get_u16(&extensions, &type) ||
  1207. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  1208. *out_alert = SSL_AD_DECODE_ERROR;
  1209. return 0;
  1210. }
  1211. /* The servername extension is treated as follows:
  1212. - Only the hostname type is supported with a maximum length of 255.
  1213. - The servername is rejected if too long or if it contains zeros, in
  1214. which case an fatal alert is generated.
  1215. - The servername field is maintained together with the session cache.
  1216. - When a session is resumed, the servername call back invoked in order
  1217. to allow the application to position itself to the right context.
  1218. - The servername is acknowledged if it is new for a session or when
  1219. it is identical to a previously used for the same session.
  1220. Applications can control the behaviour. They can at any time
  1221. set a 'desirable' servername for a new SSL object. This can be the
  1222. case for example with HTTPS when a Host: header field is received and
  1223. a renegotiation is requested. In this case, a possible servername
  1224. presented in the new client hello is only acknowledged if it matches
  1225. the value of the Host: field.
  1226. - Applications must use SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION
  1227. if they provide for changing an explicit servername context for the
  1228. session,
  1229. i.e. when the session has been established with a servername extension.
  1230. - On session reconnect, the servername extension may be absent. */
  1231. if (type == TLSEXT_TYPE_server_name) {
  1232. CBS server_name_list;
  1233. char have_seen_host_name = 0;
  1234. if (!CBS_get_u16_length_prefixed(&extension, &server_name_list) ||
  1235. CBS_len(&server_name_list) < 1 || CBS_len(&extension) != 0) {
  1236. *out_alert = SSL_AD_DECODE_ERROR;
  1237. return 0;
  1238. }
  1239. /* Decode each ServerName in the extension. */
  1240. while (CBS_len(&server_name_list) > 0) {
  1241. uint8_t name_type;
  1242. CBS host_name;
  1243. /* Decode the NameType. */
  1244. if (!CBS_get_u8(&server_name_list, &name_type)) {
  1245. *out_alert = SSL_AD_DECODE_ERROR;
  1246. return 0;
  1247. }
  1248. /* Only host_name is supported. */
  1249. if (name_type != TLSEXT_NAMETYPE_host_name) {
  1250. continue;
  1251. }
  1252. if (have_seen_host_name) {
  1253. /* The ServerNameList MUST NOT contain more than one name of the same
  1254. * name_type. */
  1255. *out_alert = SSL_AD_DECODE_ERROR;
  1256. return 0;
  1257. }
  1258. have_seen_host_name = 1;
  1259. if (!CBS_get_u16_length_prefixed(&server_name_list, &host_name) ||
  1260. CBS_len(&host_name) < 1) {
  1261. *out_alert = SSL_AD_DECODE_ERROR;
  1262. return 0;
  1263. }
  1264. if (CBS_len(&host_name) > TLSEXT_MAXLEN_host_name ||
  1265. CBS_contains_zero_byte(&host_name)) {
  1266. *out_alert = SSL_AD_UNRECOGNIZED_NAME;
  1267. return 0;
  1268. }
  1269. if (!s->hit) {
  1270. assert(s->session->tlsext_hostname == NULL);
  1271. if (s->session->tlsext_hostname) {
  1272. /* This should be impossible. */
  1273. *out_alert = SSL_AD_DECODE_ERROR;
  1274. return 0;
  1275. }
  1276. /* Copy the hostname as a string. */
  1277. if (!CBS_strdup(&host_name, &s->session->tlsext_hostname)) {
  1278. *out_alert = SSL_AD_INTERNAL_ERROR;
  1279. return 0;
  1280. }
  1281. s->should_ack_sni = 1;
  1282. }
  1283. }
  1284. } else if (type == TLSEXT_TYPE_ec_point_formats) {
  1285. CBS ec_point_format_list;
  1286. if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
  1287. CBS_len(&extension) != 0) {
  1288. *out_alert = SSL_AD_DECODE_ERROR;
  1289. return 0;
  1290. }
  1291. if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
  1292. &s->s3->tmp.peer_ecpointformatlist_length)) {
  1293. *out_alert = SSL_AD_INTERNAL_ERROR;
  1294. return 0;
  1295. }
  1296. } else if (type == TLSEXT_TYPE_elliptic_curves) {
  1297. CBS elliptic_curve_list;
  1298. size_t i, num_curves;
  1299. if (!CBS_get_u16_length_prefixed(&extension, &elliptic_curve_list) ||
  1300. CBS_len(&elliptic_curve_list) == 0 ||
  1301. (CBS_len(&elliptic_curve_list) & 1) != 0 ||
  1302. CBS_len(&extension) != 0) {
  1303. *out_alert = SSL_AD_DECODE_ERROR;
  1304. return 0;
  1305. }
  1306. OPENSSL_free(s->s3->tmp.peer_ellipticcurvelist);
  1307. s->s3->tmp.peer_ellipticcurvelist_length = 0;
  1308. s->s3->tmp.peer_ellipticcurvelist =
  1309. (uint16_t *)OPENSSL_malloc(CBS_len(&elliptic_curve_list));
  1310. if (s->s3->tmp.peer_ellipticcurvelist == NULL) {
  1311. *out_alert = SSL_AD_INTERNAL_ERROR;
  1312. return 0;
  1313. }
  1314. num_curves = CBS_len(&elliptic_curve_list) / 2;
  1315. for (i = 0; i < num_curves; i++) {
  1316. if (!CBS_get_u16(&elliptic_curve_list,
  1317. &s->s3->tmp.peer_ellipticcurvelist[i])) {
  1318. *out_alert = SSL_AD_INTERNAL_ERROR;
  1319. return 0;
  1320. }
  1321. }
  1322. if (CBS_len(&elliptic_curve_list) != 0) {
  1323. *out_alert = SSL_AD_INTERNAL_ERROR;
  1324. return 0;
  1325. }
  1326. s->s3->tmp.peer_ellipticcurvelist_length = num_curves;
  1327. } else if (type == TLSEXT_TYPE_renegotiate) {
  1328. if (!ssl_parse_clienthello_renegotiate_ext(s, &extension, out_alert)) {
  1329. return 0;
  1330. }
  1331. renegotiate_seen = 1;
  1332. } else if (type == TLSEXT_TYPE_signature_algorithms) {
  1333. CBS supported_signature_algorithms;
  1334. if (!CBS_get_u16_length_prefixed(&extension,
  1335. &supported_signature_algorithms) ||
  1336. CBS_len(&extension) != 0) {
  1337. *out_alert = SSL_AD_DECODE_ERROR;
  1338. return 0;
  1339. }
  1340. /* Ensure the signature algorithms are non-empty. It contains a list of
  1341. * SignatureAndHashAlgorithms which are two bytes each. */
  1342. if (CBS_len(&supported_signature_algorithms) == 0 ||
  1343. (CBS_len(&supported_signature_algorithms) % 2) != 0) {
  1344. *out_alert = SSL_AD_DECODE_ERROR;
  1345. return 0;
  1346. }
  1347. if (!tls1_process_sigalgs(s, &supported_signature_algorithms)) {
  1348. *out_alert = SSL_AD_DECODE_ERROR;
  1349. return 0;
  1350. }
  1351. /* If sigalgs received and no shared algorithms fatal error. */
  1352. if (s->cert->peer_sigalgs && !s->cert->shared_sigalgs) {
  1353. OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
  1354. SSL_R_NO_SHARED_SIGATURE_ALGORITHMS);
  1355. *out_alert = SSL_AD_ILLEGAL_PARAMETER;
  1356. return 0;
  1357. }
  1358. } else if (type == TLSEXT_TYPE_next_proto_neg &&
  1359. !s->s3->initial_handshake_complete &&
  1360. s->s3->alpn_selected == NULL && !SSL_IS_DTLS(s)) {
  1361. /* The extension must be empty. */
  1362. if (CBS_len(&extension) != 0) {
  1363. *out_alert = SSL_AD_DECODE_ERROR;
  1364. return 0;
  1365. }
  1366. s->s3->next_proto_neg_seen = 1;
  1367. } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
  1368. s->ctx->alpn_select_cb && !s->s3->initial_handshake_complete) {
  1369. if (!tls1_alpn_handle_client_hello(s, &extension, out_alert)) {
  1370. return 0;
  1371. }
  1372. /* ALPN takes precedence over NPN. */
  1373. s->s3->next_proto_neg_seen = 0;
  1374. } else if (type == TLSEXT_TYPE_channel_id && s->tlsext_channel_id_enabled &&
  1375. !SSL_IS_DTLS(s)) {
  1376. /* The extension must be empty. */
  1377. if (CBS_len(&extension) != 0) {
  1378. *out_alert = SSL_AD_DECODE_ERROR;
  1379. return 0;
  1380. }
  1381. s->s3->tlsext_channel_id_valid = 1;
  1382. } else if (type == TLSEXT_TYPE_channel_id_new &&
  1383. s->tlsext_channel_id_enabled && !SSL_IS_DTLS(s)) {
  1384. /* The extension must be empty. */
  1385. if (CBS_len(&extension) != 0) {
  1386. *out_alert = SSL_AD_DECODE_ERROR;
  1387. return 0;
  1388. }
  1389. s->s3->tlsext_channel_id_valid = 1;
  1390. s->s3->tlsext_channel_id_new = 1;
  1391. } else if (type == TLSEXT_TYPE_use_srtp) {
  1392. if (!ssl_parse_clienthello_use_srtp_ext(s, &extension, out_alert)) {
  1393. return 0;
  1394. }
  1395. } else if (type == TLSEXT_TYPE_extended_master_secret &&
  1396. s->version != SSL3_VERSION) {
  1397. if (CBS_len(&extension) != 0) {
  1398. *out_alert = SSL_AD_DECODE_ERROR;
  1399. return 0;
  1400. }
  1401. s->s3->tmp.extended_master_secret = 1;
  1402. }
  1403. }
  1404. ri_check:
  1405. /* Need RI if renegotiating */
  1406. if (!renegotiate_seen && s->s3->initial_handshake_complete &&
  1407. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
  1408. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  1409. OPENSSL_PUT_ERROR(SSL, ssl_scan_clienthello_tlsext,
  1410. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  1411. return 0;
  1412. }
  1413. return 1;
  1414. }
  1415. int ssl_parse_clienthello_tlsext(SSL *s, CBS *cbs) {
  1416. int alert = -1;
  1417. if (ssl_scan_clienthello_tlsext(s, cbs, &alert) <= 0) {
  1418. ssl3_send_alert(s, SSL3_AL_FATAL, alert);
  1419. return 0;
  1420. }
  1421. if (ssl_check_clienthello_tlsext(s) <= 0) {
  1422. OPENSSL_PUT_ERROR(SSL, ssl_parse_clienthello_tlsext,
  1423. SSL_R_CLIENTHELLO_TLSEXT);
  1424. return 0;
  1425. }
  1426. return 1;
  1427. }
  1428. /* ssl_next_proto_validate validates a Next Protocol Negotiation block. No
  1429. * elements of zero length are allowed and the set of elements must exactly
  1430. * fill the length of the block. */
  1431. static char ssl_next_proto_validate(const CBS *cbs) {
  1432. CBS copy = *cbs;
  1433. while (CBS_len(&copy) != 0) {
  1434. CBS proto;
  1435. if (!CBS_get_u8_length_prefixed(&copy, &proto) || CBS_len(&proto) == 0) {
  1436. return 0;
  1437. }
  1438. }
  1439. return 1;
  1440. }
  1441. static int ssl_scan_serverhello_tlsext(SSL *s, CBS *cbs, int *out_alert) {
  1442. int tlsext_servername = 0;
  1443. int renegotiate_seen = 0;
  1444. CBS extensions;
  1445. /* TODO(davidben): Move all of these to some per-handshake state that gets
  1446. * systematically reset on a new handshake; perhaps allocate it fresh each
  1447. * time so it's not even kept around post-handshake. */
  1448. s->s3->next_proto_neg_seen = 0;
  1449. s->tlsext_ticket_expected = 0;
  1450. s->s3->tmp.certificate_status_expected = 0;
  1451. s->s3->tmp.extended_master_secret = 0;
  1452. s->srtp_profile = NULL;
  1453. OPENSSL_free(s->s3->alpn_selected);
  1454. s->s3->alpn_selected = NULL;
  1455. /* Clear ECC extensions */
  1456. OPENSSL_free(s->s3->tmp.peer_ecpointformatlist);
  1457. s->s3->tmp.peer_ecpointformatlist = NULL;
  1458. s->s3->tmp.peer_ecpointformatlist_length = 0;
  1459. /* There may be no extensions. */
  1460. if (CBS_len(cbs) == 0) {
  1461. goto ri_check;
  1462. }
  1463. /* Decode the extensions block and check it is valid. */
  1464. if (!CBS_get_u16_length_prefixed(cbs, &extensions) ||
  1465. !tls1_check_duplicate_extensions(&extensions)) {
  1466. *out_alert = SSL_AD_DECODE_ERROR;
  1467. return 0;
  1468. }
  1469. while (CBS_len(&extensions) != 0) {
  1470. uint16_t type;
  1471. CBS extension;
  1472. /* Decode the next extension. */
  1473. if (!CBS_get_u16(&extensions, &type) ||
  1474. !CBS_get_u16_length_prefixed(&extensions, &extension)) {
  1475. *out_alert = SSL_AD_DECODE_ERROR;
  1476. return 0;
  1477. }
  1478. if (type == TLSEXT_TYPE_server_name) {
  1479. /* The extension must be empty. */
  1480. if (CBS_len(&extension) != 0) {
  1481. *out_alert = SSL_AD_DECODE_ERROR;
  1482. return 0;
  1483. }
  1484. /* We must have sent it in ClientHello. */
  1485. if (s->tlsext_hostname == NULL) {
  1486. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1487. return 0;
  1488. }
  1489. tlsext_servername = 1;
  1490. } else if (type == TLSEXT_TYPE_ec_point_formats) {
  1491. CBS ec_point_format_list;
  1492. if (!CBS_get_u8_length_prefixed(&extension, &ec_point_format_list) ||
  1493. CBS_len(&extension) != 0) {
  1494. *out_alert = SSL_AD_DECODE_ERROR;
  1495. return 0;
  1496. }
  1497. if (!CBS_stow(&ec_point_format_list, &s->s3->tmp.peer_ecpointformatlist,
  1498. &s->s3->tmp.peer_ecpointformatlist_length)) {
  1499. *out_alert = SSL_AD_INTERNAL_ERROR;
  1500. return 0;
  1501. }
  1502. } else if (type == TLSEXT_TYPE_session_ticket) {
  1503. if ((SSL_get_options(s) & SSL_OP_NO_TICKET) || CBS_len(&extension) > 0) {
  1504. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1505. return 0;
  1506. }
  1507. s->tlsext_ticket_expected = 1;
  1508. } else if (type == TLSEXT_TYPE_status_request) {
  1509. /* The extension MUST be empty and may only sent if we've requested a
  1510. * status request message. */
  1511. if (CBS_len(&extension) != 0) {
  1512. *out_alert = SSL_AD_DECODE_ERROR;
  1513. return 0;
  1514. }
  1515. if (!s->ocsp_stapling_enabled) {
  1516. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1517. return 0;
  1518. }
  1519. /* Set a flag to expect a CertificateStatus message */
  1520. s->s3->tmp.certificate_status_expected = 1;
  1521. } else if (type == TLSEXT_TYPE_next_proto_neg &&
  1522. !s->s3->initial_handshake_complete && !SSL_IS_DTLS(s)) {
  1523. uint8_t *selected;
  1524. uint8_t selected_len;
  1525. /* We must have requested it. */
  1526. if (s->ctx->next_proto_select_cb == NULL) {
  1527. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1528. return 0;
  1529. }
  1530. /* The data must be valid. */
  1531. if (!ssl_next_proto_validate(&extension)) {
  1532. *out_alert = SSL_AD_DECODE_ERROR;
  1533. return 0;
  1534. }
  1535. if (s->ctx->next_proto_select_cb(
  1536. s, &selected, &selected_len, CBS_data(&extension),
  1537. CBS_len(&extension),
  1538. s->ctx->next_proto_select_cb_arg) != SSL_TLSEXT_ERR_OK) {
  1539. *out_alert = SSL_AD_INTERNAL_ERROR;
  1540. return 0;
  1541. }
  1542. s->next_proto_negotiated = BUF_memdup(selected, selected_len);
  1543. if (s->next_proto_negotiated == NULL) {
  1544. *out_alert = SSL_AD_INTERNAL_ERROR;
  1545. return 0;
  1546. }
  1547. s->next_proto_negotiated_len = selected_len;
  1548. s->s3->next_proto_neg_seen = 1;
  1549. } else if (type == TLSEXT_TYPE_application_layer_protocol_negotiation &&
  1550. !s->s3->initial_handshake_complete) {
  1551. CBS protocol_name_list, protocol_name;
  1552. /* We must have requested it. */
  1553. if (s->alpn_client_proto_list == NULL) {
  1554. *out_alert = SSL_AD_UNSUPPORTED_EXTENSION;
  1555. return 0;
  1556. }
  1557. /* The extension data consists of a ProtocolNameList which must have
  1558. * exactly one ProtocolName. Each of these is length-prefixed. */
  1559. if (!CBS_get_u16_length_prefixed(&extension, &protocol_name_list) ||
  1560. CBS_len(&extension) != 0 ||
  1561. !CBS_get_u8_length_prefixed(&protocol_name_list, &protocol_name) ||
  1562. CBS_len(&protocol_name_list) != 0) {
  1563. *out_alert = SSL_AD_DECODE_ERROR;
  1564. return 0;
  1565. }
  1566. if (!CBS_stow(&protocol_name, &s->s3->alpn_selected,
  1567. &s->s3->alpn_selected_len)) {
  1568. *out_alert = SSL_AD_INTERNAL_ERROR;
  1569. return 0;
  1570. }
  1571. } else if (type == TLSEXT_TYPE_channel_id && !SSL_IS_DTLS(s)) {
  1572. if (CBS_len(&extension) != 0) {
  1573. *out_alert = SSL_AD_DECODE_ERROR;
  1574. return 0;
  1575. }
  1576. s->s3->tlsext_channel_id_valid = 1;
  1577. } else if (type == TLSEXT_TYPE_channel_id_new && !SSL_IS_DTLS(s)) {
  1578. if (CBS_len(&extension) != 0) {
  1579. *out_alert = SSL_AD_DECODE_ERROR;
  1580. return 0;
  1581. }
  1582. s->s3->tlsext_channel_id_valid = 1;
  1583. s->s3->tlsext_channel_id_new = 1;
  1584. } else if (type == TLSEXT_TYPE_certificate_timestamp) {
  1585. if (CBS_len(&extension) == 0) {
  1586. *out_alert = SSL_AD_DECODE_ERROR;
  1587. return 0;
  1588. }
  1589. /* Session resumption uses the original session information. */
  1590. if (!s->hit &&
  1591. !CBS_stow(&extension, &s->session->tlsext_signed_cert_timestamp_list,
  1592. &s->session->tlsext_signed_cert_timestamp_list_length)) {
  1593. *out_alert = SSL_AD_INTERNAL_ERROR;
  1594. return 0;
  1595. }
  1596. } else if (type == TLSEXT_TYPE_renegotiate) {
  1597. if (!ssl_parse_serverhello_renegotiate_ext(s, &extension, out_alert)) {
  1598. return 0;
  1599. }
  1600. renegotiate_seen = 1;
  1601. } else if (type == TLSEXT_TYPE_use_srtp) {
  1602. if (!ssl_parse_serverhello_use_srtp_ext(s, &extension, out_alert)) {
  1603. return 0;
  1604. }
  1605. } else if (type == TLSEXT_TYPE_extended_master_secret) {
  1606. if (/* It is invalid for the server to select EMS and
  1607. SSLv3. */
  1608. s->version == SSL3_VERSION || CBS_len(&extension) != 0) {
  1609. *out_alert = SSL_AD_DECODE_ERROR;
  1610. return 0;
  1611. }
  1612. s->s3->tmp.extended_master_secret = 1;
  1613. }
  1614. }
  1615. if (!s->hit && tlsext_servername == 1 && s->tlsext_hostname) {
  1616. if (s->session->tlsext_hostname == NULL) {
  1617. s->session->tlsext_hostname = BUF_strdup(s->tlsext_hostname);
  1618. if (!s->session->tlsext_hostname) {
  1619. *out_alert = SSL_AD_UNRECOGNIZED_NAME;
  1620. return 0;
  1621. }
  1622. } else {
  1623. *out_alert = SSL_AD_DECODE_ERROR;
  1624. return 0;
  1625. }
  1626. }
  1627. ri_check:
  1628. /* Determine if we need to see RI. Strictly speaking if we want to avoid an
  1629. * attack we should *always* see RI even on initial server hello because the
  1630. * client doesn't see any renegotiation during an attack. However this would
  1631. * mean we could not connect to any server which doesn't support RI so for
  1632. * the immediate future tolerate RI absence on initial connect only. */
  1633. if (!renegotiate_seen && !(s->options & SSL_OP_LEGACY_SERVER_CONNECT) &&
  1634. !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) {
  1635. *out_alert = SSL_AD_HANDSHAKE_FAILURE;
  1636. OPENSSL_PUT_ERROR(SSL, ssl_scan_serverhello_tlsext,
  1637. SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED);
  1638. return 0;
  1639. }
  1640. return 1;
  1641. }
  1642. int ssl_prepare_clienthello_tlsext(SSL *s) { return 1; }
  1643. int ssl_prepare_serverhello_tlsext(SSL *s) { return 1; }
  1644. static int ssl_check_clienthello_tlsext(SSL *s) {
  1645. int ret = SSL_TLSEXT_ERR_NOACK;
  1646. int al = SSL_AD_UNRECOGNIZED_NAME;
  1647. /* The handling of the ECPointFormats extension is done elsewhere, namely in
  1648. * ssl3_choose_cipher in s3_lib.c. */
  1649. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
  1650. ret = s->ctx->tlsext_servername_callback(s, &al,
  1651. s->ctx->tlsext_servername_arg);
  1652. } else if (s->initial_ctx != NULL &&
  1653. s->initial_ctx->tlsext_servername_callback != 0) {
  1654. ret = s->initial_ctx->tlsext_servername_callback(
  1655. s, &al, s->initial_ctx->tlsext_servername_arg);
  1656. }
  1657. switch (ret) {
  1658. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1659. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1660. return -1;
  1661. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1662. ssl3_send_alert(s, SSL3_AL_WARNING, al);
  1663. return 1;
  1664. case SSL_TLSEXT_ERR_NOACK:
  1665. s->should_ack_sni = 0;
  1666. return 1;
  1667. default:
  1668. return 1;
  1669. }
  1670. }
  1671. static int ssl_check_serverhello_tlsext(SSL *s) {
  1672. int ret = SSL_TLSEXT_ERR_NOACK;
  1673. int al = SSL_AD_UNRECOGNIZED_NAME;
  1674. /* If we are client and using an elliptic curve cryptography cipher suite,
  1675. * then if server returns an EC point formats lists extension it must contain
  1676. * uncompressed. */
  1677. uint32_t alg_k = s->s3->tmp.new_cipher->algorithm_mkey;
  1678. uint32_t alg_a = s->s3->tmp.new_cipher->algorithm_auth;
  1679. if (((alg_k & SSL_kECDHE) || (alg_a & SSL_aECDSA)) &&
  1680. !tls1_check_point_format(s, TLSEXT_ECPOINTFORMAT_uncompressed)) {
  1681. OPENSSL_PUT_ERROR(SSL, ssl_check_serverhello_tlsext,
  1682. SSL_R_TLS_INVALID_ECPOINTFORMAT_LIST);
  1683. return -1;
  1684. }
  1685. ret = SSL_TLSEXT_ERR_OK;
  1686. if (s->ctx != NULL && s->ctx->tlsext_servername_callback != 0) {
  1687. ret = s->ctx->tlsext_servername_callback(s, &al,
  1688. s->ctx->tlsext_servername_arg);
  1689. } else if (s->initial_ctx != NULL &&
  1690. s->initial_ctx->tlsext_servername_callback != 0) {
  1691. ret = s->initial_ctx->tlsext_servername_callback(
  1692. s, &al, s->initial_ctx->tlsext_servername_arg);
  1693. }
  1694. switch (ret) {
  1695. case SSL_TLSEXT_ERR_ALERT_FATAL:
  1696. ssl3_send_alert(s, SSL3_AL_FATAL, al);
  1697. return -1;
  1698. case SSL_TLSEXT_ERR_ALERT_WARNING:
  1699. ssl3_send_alert(s, SSL3_AL_WARNING, al);
  1700. return 1;
  1701. default:
  1702. return 1;
  1703. }
  1704. }
  1705. int ssl_parse_serverhello_tlsext(SSL *s, CBS *cbs) {
  1706. int alert = -1;
  1707. if (s->version < SSL3_VERSION) {
  1708. return 1;
  1709. }
  1710. if (ssl_scan_serverhello_tlsext(s, cbs, &alert) <= 0) {
  1711. ssl3_send_alert(s, SSL3_AL_FATAL, alert);
  1712. return 0;
  1713. }
  1714. if (ssl_check_serverhello_tlsext(s) <= 0) {
  1715. OPENSSL_PUT_ERROR(SSL, ssl_parse_serverhello_tlsext,
  1716. SSL_R_SERVERHELLO_TLSEXT);
  1717. return 0;
  1718. }
  1719. return 1;
  1720. }
  1721. /* Since the server cache lookup is done early on in the processing of the
  1722. * ClientHello, and other operations depend on the result, we need to handle
  1723. * any TLS session ticket extension at the same time.
  1724. *
  1725. * ctx: contains the early callback context, which is the result of a
  1726. * shallow parse of the ClientHello.
  1727. * ret: (output) on return, if a ticket was decrypted, then this is set to
  1728. * point to the resulting session.
  1729. *
  1730. * Returns:
  1731. * -1: fatal error, either from parsing or decrypting the ticket.
  1732. * 0: no ticket was found (or was ignored, based on settings).
  1733. * 1: a zero length extension was found, indicating that the client supports
  1734. * session tickets but doesn't currently have one to offer.
  1735. * 2: a ticket was offered but couldn't be decrypted because of a non-fatal
  1736. * error.
  1737. * 3: a ticket was successfully decrypted and *ret was set.
  1738. *
  1739. * Side effects:
  1740. * Sets s->tlsext_ticket_expected to 1 if the server will have to issue
  1741. * a new session ticket to the client because the client indicated support
  1742. * but the client either doesn't have a session ticket or we couldn't use
  1743. * the one it gave us, or if s->ctx->tlsext_ticket_key_cb asked to renew
  1744. * the client's ticket. Otherwise, s->tlsext_ticket_expected is set to 0.
  1745. */
  1746. int tls1_process_ticket(SSL *s, const struct ssl_early_callback_ctx *ctx,
  1747. SSL_SESSION **ret) {
  1748. *ret = NULL;
  1749. s->tlsext_ticket_expected = 0;
  1750. const uint8_t *data;
  1751. size_t len;
  1752. int r;
  1753. /* If tickets disabled behave as if no ticket present to permit stateful
  1754. * resumption. */
  1755. if ((SSL_get_options(s) & SSL_OP_NO_TICKET) ||
  1756. (s->version <= SSL3_VERSION && !ctx->extensions) ||
  1757. !SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_session_ticket,
  1758. &data, &len)) {
  1759. return 0;
  1760. }
  1761. if (len == 0) {
  1762. /* The client will accept a ticket but doesn't currently have one. */
  1763. s->tlsext_ticket_expected = 1;
  1764. return 1;
  1765. }
  1766. r = tls_decrypt_ticket(s, data, len, ctx->session_id, ctx->session_id_len,
  1767. ret);
  1768. switch (r) {
  1769. case 2: /* ticket couldn't be decrypted */
  1770. s->tlsext_ticket_expected = 1;
  1771. return 2;
  1772. case 3: /* ticket was decrypted */
  1773. return r;
  1774. case 4: /* ticket decrypted but need to renew */
  1775. s->tlsext_ticket_expected = 1;
  1776. return 3;
  1777. default: /* fatal error */
  1778. return -1;
  1779. }
  1780. }
  1781. /* tls_decrypt_ticket attempts to decrypt a session ticket.
  1782. *
  1783. * etick: points to the body of the session ticket extension.
  1784. * eticklen: the length of the session tickets extenion.
  1785. * sess_id: points at the session ID.
  1786. * sesslen: the length of the session ID.
  1787. * psess: (output) on return, if a ticket was decrypted, then this is set to
  1788. * point to the resulting session.
  1789. *
  1790. * Returns:
  1791. * -1: fatal error, either from parsing or decrypting the ticket.
  1792. * 2: the ticket couldn't be decrypted.
  1793. * 3: a ticket was successfully decrypted and *psess was set.
  1794. * 4: same as 3, but the ticket needs to be renewed. */
  1795. static int tls_decrypt_ticket(SSL *s, const uint8_t *etick, int eticklen,
  1796. const uint8_t *sess_id, int sesslen,
  1797. SSL_SESSION **psess) {
  1798. SSL_SESSION *sess;
  1799. uint8_t *sdec;
  1800. const uint8_t *p;
  1801. int slen, mlen, renew_ticket = 0;
  1802. uint8_t tick_hmac[EVP_MAX_MD_SIZE];
  1803. HMAC_CTX hctx;
  1804. EVP_CIPHER_CTX ctx;
  1805. SSL_CTX *tctx = s->initial_ctx;
  1806. /* Ensure there is room for the key name and the largest IV
  1807. * |tlsext_ticket_key_cb| may try to consume. The real limit may be lower, but
  1808. * the maximum IV length should be well under the minimum size for the
  1809. * session material and HMAC. */
  1810. if (eticklen < 16 + EVP_MAX_IV_LENGTH) {
  1811. return 2;
  1812. }
  1813. /* Initialize session ticket encryption and HMAC contexts */
  1814. HMAC_CTX_init(&hctx);
  1815. EVP_CIPHER_CTX_init(&ctx);
  1816. if (tctx->tlsext_ticket_key_cb) {
  1817. uint8_t *nctick = (uint8_t *)etick;
  1818. int rv = tctx->tlsext_ticket_key_cb(s, nctick, nctick + 16, &ctx, &hctx,
  1819. 0 /* decrypt */);
  1820. if (rv < 0) {
  1821. return -1;
  1822. }
  1823. if (rv == 0) {
  1824. return 2;
  1825. }
  1826. if (rv == 2) {
  1827. renew_ticket = 1;
  1828. }
  1829. } else {
  1830. /* Check key name matches */
  1831. if (memcmp(etick, tctx->tlsext_tick_key_name, 16)) {
  1832. return 2;
  1833. }
  1834. if (!HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(),
  1835. NULL) ||
  1836. !EVP_DecryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL,
  1837. tctx->tlsext_tick_aes_key, etick + 16)) {
  1838. HMAC_CTX_cleanup(&hctx);
  1839. EVP_CIPHER_CTX_cleanup(&ctx);
  1840. return -1;
  1841. }
  1842. }
  1843. /* First, check the MAC. The MAC is at the end of the ticket. */
  1844. mlen = HMAC_size(&hctx);
  1845. if ((size_t) eticklen < 16 + EVP_CIPHER_CTX_iv_length(&ctx) + 1 + mlen) {
  1846. /* The ticket must be large enough for key name, IV, data, and MAC. */
  1847. HMAC_CTX_cleanup(&hctx);
  1848. EVP_CIPHER_CTX_cleanup(&ctx);
  1849. return 2;
  1850. }
  1851. eticklen -= mlen;
  1852. /* Check HMAC of encrypted ticket */
  1853. HMAC_Update(&hctx, etick, eticklen);
  1854. HMAC_Final(&hctx, tick_hmac, NULL);
  1855. HMAC_CTX_cleanup(&hctx);
  1856. if (CRYPTO_memcmp(tick_hmac, etick + eticklen, mlen)) {
  1857. EVP_CIPHER_CTX_cleanup(&ctx);
  1858. return 2;
  1859. }
  1860. /* Attempt to decrypt session data */
  1861. /* Move p after IV to start of encrypted ticket, update length */
  1862. p = etick + 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  1863. eticklen -= 16 + EVP_CIPHER_CTX_iv_length(&ctx);
  1864. sdec = OPENSSL_malloc(eticklen);
  1865. if (!sdec) {
  1866. EVP_CIPHER_CTX_cleanup(&ctx);
  1867. return -1;
  1868. }
  1869. EVP_DecryptUpdate(&ctx, sdec, &slen, p, eticklen);
  1870. if (EVP_DecryptFinal_ex(&ctx, sdec + slen, &mlen) <= 0) {
  1871. EVP_CIPHER_CTX_cleanup(&ctx);
  1872. OPENSSL_free(sdec);
  1873. return 2;
  1874. }
  1875. slen += mlen;
  1876. EVP_CIPHER_CTX_cleanup(&ctx);
  1877. p = sdec;
  1878. sess = d2i_SSL_SESSION(NULL, &p, slen);
  1879. OPENSSL_free(sdec);
  1880. if (sess) {
  1881. /* The session ID, if non-empty, is used by some clients to detect that the
  1882. * ticket has been accepted. So we copy it to the session structure. If it
  1883. * is empty set length to zero as required by standard. */
  1884. if (sesslen) {
  1885. memcpy(sess->session_id, sess_id, sesslen);
  1886. }
  1887. sess->session_id_length = sesslen;
  1888. *psess = sess;
  1889. if (renew_ticket) {
  1890. return 4;
  1891. }
  1892. return 3;
  1893. }
  1894. ERR_clear_error();
  1895. /* For session parse failure, indicate that we need to send a new ticket. */
  1896. return 2;
  1897. }
  1898. /* Tables to translate from NIDs to TLS v1.2 ids */
  1899. typedef struct {
  1900. int nid;
  1901. int id;
  1902. } tls12_lookup;
  1903. static const tls12_lookup tls12_md[] = {{NID_md5, TLSEXT_hash_md5},
  1904. {NID_sha1, TLSEXT_hash_sha1},
  1905. {NID_sha224, TLSEXT_hash_sha224},
  1906. {NID_sha256, TLSEXT_hash_sha256},
  1907. {NID_sha384, TLSEXT_hash_sha384},
  1908. {NID_sha512, TLSEXT_hash_sha512}};
  1909. static const tls12_lookup tls12_sig[] = {{EVP_PKEY_RSA, TLSEXT_signature_rsa},
  1910. {EVP_PKEY_EC, TLSEXT_signature_ecdsa}};
  1911. static int tls12_find_id(int nid, const tls12_lookup *table, size_t tlen) {
  1912. size_t i;
  1913. for (i = 0; i < tlen; i++) {
  1914. if (table[i].nid == nid) {
  1915. return table[i].id;
  1916. }
  1917. }
  1918. return -1;
  1919. }
  1920. static int tls12_find_nid(int id, const tls12_lookup *table, size_t tlen) {
  1921. size_t i;
  1922. for (i = 0; i < tlen; i++) {
  1923. if (table[i].id == id) {
  1924. return table[i].nid;
  1925. }
  1926. }
  1927. return NID_undef;
  1928. }
  1929. int tls12_get_sigandhash(uint8_t *p, const EVP_PKEY *pk, const EVP_MD *md) {
  1930. int sig_id, md_id;
  1931. if (!md) {
  1932. return 0;
  1933. }
  1934. md_id = tls12_find_id(EVP_MD_type(md), tls12_md,
  1935. sizeof(tls12_md) / sizeof(tls12_lookup));
  1936. if (md_id == -1) {
  1937. return 0;
  1938. }
  1939. sig_id = tls12_get_sigid(pk);
  1940. if (sig_id == -1) {
  1941. return 0;
  1942. }
  1943. p[0] = (uint8_t)md_id;
  1944. p[1] = (uint8_t)sig_id;
  1945. return 1;
  1946. }
  1947. int tls12_get_sigid(const EVP_PKEY *pk) {
  1948. return tls12_find_id(pk->type, tls12_sig,
  1949. sizeof(tls12_sig) / sizeof(tls12_lookup));
  1950. }
  1951. const EVP_MD *tls12_get_hash(uint8_t hash_alg) {
  1952. switch (hash_alg) {
  1953. case TLSEXT_hash_md5:
  1954. return EVP_md5();
  1955. case TLSEXT_hash_sha1:
  1956. return EVP_sha1();
  1957. case TLSEXT_hash_sha224:
  1958. return EVP_sha224();
  1959. case TLSEXT_hash_sha256:
  1960. return EVP_sha256();
  1961. case TLSEXT_hash_sha384:
  1962. return EVP_sha384();
  1963. case TLSEXT_hash_sha512:
  1964. return EVP_sha512();
  1965. default:
  1966. return NULL;
  1967. }
  1968. }
  1969. /* tls12_get_pkey_type returns the EVP_PKEY type corresponding to TLS signature
  1970. * algorithm |sig_alg|. It returns -1 if the type is unknown. */
  1971. static int tls12_get_pkey_type(uint8_t sig_alg) {
  1972. switch (sig_alg) {
  1973. case TLSEXT_signature_rsa:
  1974. return EVP_PKEY_RSA;
  1975. case TLSEXT_signature_ecdsa:
  1976. return EVP_PKEY_EC;
  1977. default:
  1978. return -1;
  1979. }
  1980. }
  1981. /* Convert TLS 1.2 signature algorithm extension values into NIDs */
  1982. static void tls1_lookup_sigalg(int *phash_nid, int *psign_nid,
  1983. int *psignhash_nid, const uint8_t *data) {
  1984. int sign_nid = 0, hash_nid = 0;
  1985. if (!phash_nid && !psign_nid && !psignhash_nid) {
  1986. return;
  1987. }
  1988. if (phash_nid || psignhash_nid) {
  1989. hash_nid = tls12_find_nid(data[0], tls12_md,
  1990. sizeof(tls12_md) / sizeof(tls12_lookup));
  1991. if (phash_nid) {
  1992. *phash_nid = hash_nid;
  1993. }
  1994. }
  1995. if (psign_nid || psignhash_nid) {
  1996. sign_nid = tls12_find_nid(data[1], tls12_sig,
  1997. sizeof(tls12_sig) / sizeof(tls12_lookup));
  1998. if (psign_nid) {
  1999. *psign_nid = sign_nid;
  2000. }
  2001. }
  2002. if (psignhash_nid) {
  2003. if (sign_nid && hash_nid) {
  2004. OBJ_find_sigid_by_algs(psignhash_nid, hash_nid, sign_nid);
  2005. } else {
  2006. *psignhash_nid = NID_undef;
  2007. }
  2008. }
  2009. }
  2010. /* Given preference and allowed sigalgs set shared sigalgs */
  2011. static int tls12_do_shared_sigalgs(TLS_SIGALGS *shsig, const uint8_t *pref,
  2012. size_t preflen, const uint8_t *allow,
  2013. size_t allowlen) {
  2014. const uint8_t *ptmp, *atmp;
  2015. size_t i, j, nmatch = 0;
  2016. for (i = 0, ptmp = pref; i < preflen; i += 2, ptmp += 2) {
  2017. /* Skip disabled hashes or signature algorithms */
  2018. if (tls12_get_hash(ptmp[0]) == NULL ||
  2019. tls12_get_pkey_type(ptmp[1]) == -1) {
  2020. continue;
  2021. }
  2022. for (j = 0, atmp = allow; j < allowlen; j += 2, atmp += 2) {
  2023. if (ptmp[0] == atmp[0] && ptmp[1] == atmp[1]) {
  2024. nmatch++;
  2025. if (shsig) {
  2026. shsig->rhash = ptmp[0];
  2027. shsig->rsign = ptmp[1];
  2028. tls1_lookup_sigalg(&shsig->hash_nid, &shsig->sign_nid,
  2029. &shsig->signandhash_nid, ptmp);
  2030. shsig++;
  2031. }
  2032. break;
  2033. }
  2034. }
  2035. }
  2036. return nmatch;
  2037. }
  2038. /* Set shared signature algorithms for SSL structures */
  2039. static int tls1_set_shared_sigalgs(SSL *s) {
  2040. const uint8_t *pref, *allow, *conf;
  2041. size_t preflen, allowlen, conflen;
  2042. size_t nmatch;
  2043. TLS_SIGALGS *salgs = NULL;
  2044. CERT *c = s->cert;
  2045. OPENSSL_free(c->shared_sigalgs);
  2046. c->shared_sigalgs = NULL;
  2047. c->shared_sigalgslen = 0;
  2048. /* If client use client signature algorithms if not NULL */
  2049. if (!s->server && c->client_sigalgs) {
  2050. conf = c->client_sigalgs;
  2051. conflen = c->client_sigalgslen;
  2052. } else if (c->conf_sigalgs) {
  2053. conf = c->conf_sigalgs;
  2054. conflen = c->conf_sigalgslen;
  2055. } else {
  2056. conflen = tls12_get_psigalgs(s, &conf);
  2057. }
  2058. if (s->options & SSL_OP_CIPHER_SERVER_PREFERENCE) {
  2059. pref = conf;
  2060. preflen = conflen;
  2061. allow = c->peer_sigalgs;
  2062. allowlen = c->peer_sigalgslen;
  2063. } else {
  2064. allow = conf;
  2065. allowlen = conflen;
  2066. pref = c->peer_sigalgs;
  2067. preflen = c->peer_sigalgslen;
  2068. }
  2069. nmatch = tls12_do_shared_sigalgs(NULL, pref, preflen, allow, allowlen);
  2070. if (!nmatch) {
  2071. return 1;
  2072. }
  2073. salgs = OPENSSL_malloc(nmatch * sizeof(TLS_SIGALGS));
  2074. if (!salgs) {
  2075. return 0;
  2076. }
  2077. nmatch = tls12_do_shared_sigalgs(salgs, pref, preflen, allow, allowlen);
  2078. c->shared_sigalgs = salgs;
  2079. c->shared_sigalgslen = nmatch;
  2080. return 1;
  2081. }
  2082. /* Set preferred digest for each key type */
  2083. int tls1_process_sigalgs(SSL *s, const CBS *sigalgs) {
  2084. CERT *c = s->cert;
  2085. /* Extension ignored for inappropriate versions */
  2086. if (!SSL_USE_SIGALGS(s)) {
  2087. return 1;
  2088. }
  2089. if (CBS_len(sigalgs) % 2 != 0 ||
  2090. !CBS_stow(sigalgs, &c->peer_sigalgs, &c->peer_sigalgslen) ||
  2091. !tls1_set_shared_sigalgs(s)) {
  2092. return 0;
  2093. }
  2094. return 1;
  2095. }
  2096. const EVP_MD *tls1_choose_signing_digest(SSL *s, EVP_PKEY *pkey) {
  2097. CERT *c = s->cert;
  2098. int type = EVP_PKEY_id(pkey);
  2099. size_t i;
  2100. /* Select the first shared digest supported by our key. */
  2101. for (i = 0; i < c->shared_sigalgslen; i++) {
  2102. const EVP_MD *md = tls12_get_hash(c->shared_sigalgs[i].rhash);
  2103. if (md == NULL ||
  2104. tls12_get_pkey_type(c->shared_sigalgs[i].rsign) != type ||
  2105. !EVP_PKEY_supports_digest(pkey, md)) {
  2106. continue;
  2107. }
  2108. return md;
  2109. }
  2110. /* If no suitable digest may be found, default to SHA-1. */
  2111. return EVP_sha1();
  2112. }
  2113. int SSL_get_sigalgs(SSL *s, int idx, int *psign, int *phash, int *psignhash,
  2114. uint8_t *rsig, uint8_t *rhash) {
  2115. const uint8_t *psig = s->cert->peer_sigalgs;
  2116. if (psig == NULL) {
  2117. return 0;
  2118. }
  2119. if (idx >= 0) {
  2120. idx <<= 1;
  2121. if (idx >= (int)s->cert->peer_sigalgslen) {
  2122. return 0;
  2123. }
  2124. psig += idx;
  2125. if (rhash) {
  2126. *rhash = psig[0];
  2127. }
  2128. if (rsig) {
  2129. *rsig = psig[1];
  2130. }
  2131. tls1_lookup_sigalg(phash, psign, psignhash, psig);
  2132. }
  2133. return s->cert->peer_sigalgslen / 2;
  2134. }
  2135. int SSL_get_shared_sigalgs(SSL *s, int idx, int *psign, int *phash,
  2136. int *psignhash, uint8_t *rsig, uint8_t *rhash) {
  2137. TLS_SIGALGS *shsigalgs = s->cert->shared_sigalgs;
  2138. if (!shsigalgs || idx >= (int)s->cert->shared_sigalgslen) {
  2139. return 0;
  2140. }
  2141. shsigalgs += idx;
  2142. if (phash) {
  2143. *phash = shsigalgs->hash_nid;
  2144. }
  2145. if (psign) {
  2146. *psign = shsigalgs->sign_nid;
  2147. }
  2148. if (psignhash) {
  2149. *psignhash = shsigalgs->signandhash_nid;
  2150. }
  2151. if (rsig) {
  2152. *rsig = shsigalgs->rsign;
  2153. }
  2154. if (rhash) {
  2155. *rhash = shsigalgs->rhash;
  2156. }
  2157. return s->cert->shared_sigalgslen;
  2158. }
  2159. /* tls1_channel_id_hash calculates the signed data for a Channel ID on the
  2160. * given SSL connection and writes it to |md|. */
  2161. int tls1_channel_id_hash(EVP_MD_CTX *md, SSL *s) {
  2162. EVP_MD_CTX ctx;
  2163. uint8_t temp_digest[EVP_MAX_MD_SIZE];
  2164. unsigned temp_digest_len;
  2165. int i;
  2166. static const char kClientIDMagic[] = "TLS Channel ID signature";
  2167. if (s->s3->handshake_buffer &&
  2168. !ssl3_digest_cached_records(s, free_handshake_buffer)) {
  2169. return 0;
  2170. }
  2171. EVP_DigestUpdate(md, kClientIDMagic, sizeof(kClientIDMagic));
  2172. if (s->hit && s->s3->tlsext_channel_id_new) {
  2173. static const char kResumptionMagic[] = "Resumption";
  2174. EVP_DigestUpdate(md, kResumptionMagic, sizeof(kResumptionMagic));
  2175. if (s->session->original_handshake_hash_len == 0) {
  2176. return 0;
  2177. }
  2178. EVP_DigestUpdate(md, s->session->original_handshake_hash,
  2179. s->session->original_handshake_hash_len);
  2180. }
  2181. EVP_MD_CTX_init(&ctx);
  2182. for (i = 0; i < SSL_MAX_DIGEST; i++) {
  2183. if (s->s3->handshake_dgst[i] == NULL) {
  2184. continue;
  2185. }
  2186. if (!EVP_MD_CTX_copy_ex(&ctx, s->s3->handshake_dgst[i])) {
  2187. EVP_MD_CTX_cleanup(&ctx);
  2188. return 0;
  2189. }
  2190. EVP_DigestFinal_ex(&ctx, temp_digest, &temp_digest_len);
  2191. EVP_DigestUpdate(md, temp_digest, temp_digest_len);
  2192. }
  2193. EVP_MD_CTX_cleanup(&ctx);
  2194. return 1;
  2195. }
  2196. /* tls1_record_handshake_hashes_for_channel_id records the current handshake
  2197. * hashes in |s->session| so that Channel ID resumptions can sign that data. */
  2198. int tls1_record_handshake_hashes_for_channel_id(SSL *s) {
  2199. int digest_len;
  2200. /* This function should never be called for a resumed session because the
  2201. * handshake hashes that we wish to record are for the original, full
  2202. * handshake. */
  2203. if (s->hit) {
  2204. return -1;
  2205. }
  2206. /* It only makes sense to call this function if Channel IDs have been
  2207. * negotiated. */
  2208. if (!s->s3->tlsext_channel_id_new) {
  2209. return -1;
  2210. }
  2211. digest_len =
  2212. tls1_handshake_digest(s, s->session->original_handshake_hash,
  2213. sizeof(s->session->original_handshake_hash));
  2214. if (digest_len < 0) {
  2215. return -1;
  2216. }
  2217. s->session->original_handshake_hash_len = digest_len;
  2218. return 1;
  2219. }
  2220. int tls1_set_sigalgs(CERT *c, const int *psig_nids, size_t salglen,
  2221. int client) {
  2222. uint8_t *sigalgs, *sptr;
  2223. int rhash, rsign;
  2224. size_t i;
  2225. if (salglen & 1) {
  2226. return 0;
  2227. }
  2228. sigalgs = OPENSSL_malloc(salglen);
  2229. if (sigalgs == NULL) {
  2230. return 0;
  2231. }
  2232. for (i = 0, sptr = sigalgs; i < salglen; i += 2) {
  2233. rhash = tls12_find_id(*psig_nids++, tls12_md,
  2234. sizeof(tls12_md) / sizeof(tls12_lookup));
  2235. rsign = tls12_find_id(*psig_nids++, tls12_sig,
  2236. sizeof(tls12_sig) / sizeof(tls12_lookup));
  2237. if (rhash == -1 || rsign == -1) {
  2238. goto err;
  2239. }
  2240. *sptr++ = rhash;
  2241. *sptr++ = rsign;
  2242. }
  2243. if (client) {
  2244. OPENSSL_free(c->client_sigalgs);
  2245. c->client_sigalgs = sigalgs;
  2246. c->client_sigalgslen = salglen;
  2247. } else {
  2248. OPENSSL_free(c->conf_sigalgs);
  2249. c->conf_sigalgs = sigalgs;
  2250. c->conf_sigalgslen = salglen;
  2251. }
  2252. return 1;
  2253. err:
  2254. OPENSSL_free(sigalgs);
  2255. return 0;
  2256. }