Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

2931 rader
83 KiB

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