ssl_client.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * TLS 1.2 and 1.3 client-side functions
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. #include "common.h"
  8. #if defined(MBEDTLS_SSL_CLI_C)
  9. #if defined(MBEDTLS_SSL_PROTO_TLS1_3) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
  10. #include <string.h>
  11. #include "debug_internal.h"
  12. #include "mbedtls/error.h"
  13. #include "mbedtls/platform.h"
  14. #include "ssl_client.h"
  15. #include "ssl_misc.h"
  16. #include "ssl_tls13_keys.h"
  17. #include "ssl_debug_helpers.h"
  18. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  19. MBEDTLS_CHECK_RETURN_CRITICAL
  20. static int ssl_write_hostname_ext(mbedtls_ssl_context *ssl,
  21. unsigned char *buf,
  22. const unsigned char *end,
  23. size_t *olen)
  24. {
  25. unsigned char *p = buf;
  26. size_t hostname_len;
  27. *olen = 0;
  28. if (ssl->hostname == NULL) {
  29. return 0;
  30. }
  31. MBEDTLS_SSL_DEBUG_MSG(3,
  32. ("client hello, adding server name extension: %s",
  33. ssl->hostname));
  34. hostname_len = strlen(ssl->hostname);
  35. MBEDTLS_SSL_CHK_BUF_PTR(p, end, hostname_len + 9);
  36. /*
  37. * Sect. 3, RFC 6066 (TLS Extensions Definitions)
  38. *
  39. * In order to provide any of the server names, clients MAY include an
  40. * extension of type "server_name" in the (extended) client hello. The
  41. * "extension_data" field of this extension SHALL contain
  42. * "ServerNameList" where:
  43. *
  44. * struct {
  45. * NameType name_type;
  46. * select (name_type) {
  47. * case host_name: HostName;
  48. * } name;
  49. * } ServerName;
  50. *
  51. * enum {
  52. * host_name(0), (255)
  53. * } NameType;
  54. *
  55. * opaque HostName<1..2^16-1>;
  56. *
  57. * struct {
  58. * ServerName server_name_list<1..2^16-1>
  59. * } ServerNameList;
  60. *
  61. */
  62. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SERVERNAME, p, 0);
  63. p += 2;
  64. MBEDTLS_PUT_UINT16_BE(hostname_len + 5, p, 0);
  65. p += 2;
  66. MBEDTLS_PUT_UINT16_BE(hostname_len + 3, p, 0);
  67. p += 2;
  68. *p++ = MBEDTLS_BYTE_0(MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME);
  69. MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
  70. p += 2;
  71. memcpy(p, ssl->hostname, hostname_len);
  72. *olen = hostname_len + 9;
  73. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  74. mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SERVERNAME);
  75. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
  76. return 0;
  77. }
  78. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  79. #if defined(MBEDTLS_SSL_ALPN)
  80. /*
  81. * ssl_write_alpn_ext()
  82. *
  83. * Structure of the application_layer_protocol_negotiation extension in
  84. * ClientHello:
  85. *
  86. * opaque ProtocolName<1..2^8-1>;
  87. *
  88. * struct {
  89. * ProtocolName protocol_name_list<2..2^16-1>
  90. * } ProtocolNameList;
  91. *
  92. */
  93. MBEDTLS_CHECK_RETURN_CRITICAL
  94. static int ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
  95. unsigned char *buf,
  96. const unsigned char *end,
  97. size_t *out_len)
  98. {
  99. unsigned char *p = buf;
  100. *out_len = 0;
  101. if (ssl->conf->alpn_list == NULL) {
  102. return 0;
  103. }
  104. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding alpn extension"));
  105. /* Check we have enough space for the extension type (2 bytes), the
  106. * extension length (2 bytes) and the protocol_name_list length (2 bytes).
  107. */
  108. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
  109. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
  110. /* Skip writing extension and list length for now */
  111. p += 6;
  112. /*
  113. * opaque ProtocolName<1..2^8-1>;
  114. *
  115. * struct {
  116. * ProtocolName protocol_name_list<2..2^16-1>
  117. * } ProtocolNameList;
  118. */
  119. for (const char **cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
  120. /*
  121. * mbedtls_ssl_conf_set_alpn_protocols() checked that the length of
  122. * protocol names is less than 255.
  123. */
  124. size_t protocol_name_len = strlen(*cur);
  125. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 1 + protocol_name_len);
  126. *p++ = (unsigned char) protocol_name_len;
  127. memcpy(p, *cur, protocol_name_len);
  128. p += protocol_name_len;
  129. }
  130. *out_len = (size_t) (p - buf);
  131. /* List length = *out_len - 2 (ext_type) - 2 (ext_len) - 2 (list_len) */
  132. MBEDTLS_PUT_UINT16_BE(*out_len - 6, buf, 4);
  133. /* Extension length = *out_len - 2 (ext_type) - 2 (ext_len) */
  134. MBEDTLS_PUT_UINT16_BE(*out_len - 4, buf, 2);
  135. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  136. mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
  137. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
  138. return 0;
  139. }
  140. #endif /* MBEDTLS_SSL_ALPN */
  141. #if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
  142. defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  143. /*
  144. * Function for writing a supported groups (TLS 1.3) or supported elliptic
  145. * curves (TLS 1.2) extension.
  146. *
  147. * The "extension_data" field of a supported groups extension contains a
  148. * "NamedGroupList" value (TLS 1.3 RFC8446):
  149. * enum {
  150. * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
  151. * x25519(0x001D), x448(0x001E),
  152. * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
  153. * ffdhe6144(0x0103), ffdhe8192(0x0104),
  154. * ffdhe_private_use(0x01FC..0x01FF),
  155. * ecdhe_private_use(0xFE00..0xFEFF),
  156. * (0xFFFF)
  157. * } NamedGroup;
  158. * struct {
  159. * NamedGroup named_group_list<2..2^16-1>;
  160. * } NamedGroupList;
  161. *
  162. * The "extension_data" field of a supported elliptic curves extension contains
  163. * a "NamedCurveList" value (TLS 1.2 RFC 8422):
  164. * enum {
  165. * deprecated(1..22),
  166. * secp256r1 (23), secp384r1 (24), secp521r1 (25),
  167. * x25519(29), x448(30),
  168. * reserved (0xFE00..0xFEFF),
  169. * deprecated(0xFF01..0xFF02),
  170. * (0xFFFF)
  171. * } NamedCurve;
  172. * struct {
  173. * NamedCurve named_curve_list<2..2^16-1>
  174. * } NamedCurveList;
  175. *
  176. * The TLS 1.3 supported groups extension was defined to be a compatible
  177. * generalization of the TLS 1.2 supported elliptic curves extension. They both
  178. * share the same extension identifier.
  179. *
  180. */
  181. #define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG 1
  182. #define SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG 2
  183. MBEDTLS_CHECK_RETURN_CRITICAL
  184. static int ssl_write_supported_groups_ext(mbedtls_ssl_context *ssl,
  185. unsigned char *buf,
  186. const unsigned char *end,
  187. int flags,
  188. size_t *out_len)
  189. {
  190. unsigned char *p = buf;
  191. unsigned char *named_group_list; /* Start of named_group_list */
  192. size_t named_group_list_len; /* Length of named_group_list */
  193. const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
  194. *out_len = 0;
  195. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, adding supported_groups extension"));
  196. /* Check if we have space for header and length fields:
  197. * - extension_type (2 bytes)
  198. * - extension_data_length (2 bytes)
  199. * - named_group_list_length (2 bytes)
  200. */
  201. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
  202. p += 6;
  203. named_group_list = p;
  204. if (group_list == NULL) {
  205. return MBEDTLS_ERR_SSL_BAD_CONFIG;
  206. }
  207. for (; *group_list != 0; group_list++) {
  208. int propose_group = 0;
  209. MBEDTLS_SSL_DEBUG_MSG(3, ("got supported group(%04x)", *group_list));
  210. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  211. if (flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG) {
  212. #if defined(PSA_WANT_ALG_ECDH)
  213. if (mbedtls_ssl_tls13_named_group_is_ecdhe(*group_list) &&
  214. (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
  215. MBEDTLS_ECP_DP_NONE)) {
  216. propose_group = 1;
  217. }
  218. #endif
  219. #if defined(PSA_WANT_ALG_FFDH)
  220. if (mbedtls_ssl_tls13_named_group_is_ffdh(*group_list)) {
  221. propose_group = 1;
  222. }
  223. #endif
  224. }
  225. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
  226. #if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
  227. if ((flags & SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG) &&
  228. mbedtls_ssl_tls12_named_group_is_ecdhe(*group_list) &&
  229. (mbedtls_ssl_get_ecp_group_id_from_tls_id(*group_list) !=
  230. MBEDTLS_ECP_DP_NONE)) {
  231. propose_group = 1;
  232. }
  233. #endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC */
  234. if (propose_group) {
  235. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  236. MBEDTLS_PUT_UINT16_BE(*group_list, p, 0);
  237. p += 2;
  238. MBEDTLS_SSL_DEBUG_MSG(3, ("NamedGroup: %s ( %x )",
  239. mbedtls_ssl_named_group_to_str(*group_list),
  240. *group_list));
  241. }
  242. }
  243. /* Length of named_group_list */
  244. named_group_list_len = (size_t) (p - named_group_list);
  245. if (named_group_list_len == 0) {
  246. MBEDTLS_SSL_DEBUG_MSG(1, ("No group available."));
  247. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  248. }
  249. /* Write extension_type */
  250. MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0);
  251. /* Write extension_data_length */
  252. MBEDTLS_PUT_UINT16_BE(named_group_list_len + 2, buf, 2);
  253. /* Write length of named_group_list */
  254. MBEDTLS_PUT_UINT16_BE(named_group_list_len, buf, 4);
  255. MBEDTLS_SSL_DEBUG_BUF(3, "Supported groups extension",
  256. buf + 4, named_group_list_len + 2);
  257. *out_len = (size_t) (p - buf);
  258. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  259. mbedtls_ssl_tls13_set_hs_sent_ext_mask(
  260. ssl, MBEDTLS_TLS_EXT_SUPPORTED_GROUPS);
  261. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
  262. return 0;
  263. }
  264. #endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
  265. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
  266. MBEDTLS_CHECK_RETURN_CRITICAL
  267. static int ssl_write_client_hello_cipher_suites(
  268. mbedtls_ssl_context *ssl,
  269. unsigned char *buf,
  270. unsigned char *end,
  271. int *tls12_uses_ec,
  272. size_t *out_len)
  273. {
  274. unsigned char *p = buf;
  275. const int *ciphersuite_list;
  276. unsigned char *cipher_suites; /* Start of the cipher_suites list */
  277. size_t cipher_suites_len;
  278. *tls12_uses_ec = 0;
  279. *out_len = 0;
  280. /*
  281. * Ciphersuite list
  282. *
  283. * This is a list of the symmetric cipher options supported by
  284. * the client, specifically the record protection algorithm
  285. * ( including secret key length ) and a hash to be used with
  286. * HKDF, in descending order of client preference.
  287. */
  288. ciphersuite_list = ssl->conf->ciphersuite_list;
  289. /* Check there is space for the cipher suite list length (2 bytes). */
  290. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  291. p += 2;
  292. /* Write cipher_suites
  293. * CipherSuite cipher_suites<2..2^16-2>;
  294. */
  295. cipher_suites = p;
  296. for (size_t i = 0; ciphersuite_list[i] != 0; i++) {
  297. int cipher_suite = ciphersuite_list[i];
  298. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  299. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(cipher_suite);
  300. if (mbedtls_ssl_validate_ciphersuite(ssl, ciphersuite_info,
  301. ssl->handshake->min_tls_version,
  302. ssl->tls_version) != 0) {
  303. continue;
  304. }
  305. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  306. (defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDH_OR_ECDHE_1_2_ENABLED) || \
  307. defined(MBEDTLS_KEY_EXCHANGE_ECDSA_CERT_REQ_ALLOWED_ENABLED) || \
  308. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED))
  309. *tls12_uses_ec |= mbedtls_ssl_ciphersuite_uses_ec(ciphersuite_info);
  310. #endif
  311. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, add ciphersuite: %04x, %s",
  312. (unsigned int) cipher_suite,
  313. ciphersuite_info->name));
  314. /* Check there is space for the cipher suite identifier (2 bytes). */
  315. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  316. MBEDTLS_PUT_UINT16_BE(cipher_suite, p, 0);
  317. p += 2;
  318. }
  319. /*
  320. * Add TLS_EMPTY_RENEGOTIATION_INFO_SCSV
  321. */
  322. int renegotiating = 0;
  323. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  324. renegotiating = (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE);
  325. #endif
  326. if (!renegotiating) {
  327. MBEDTLS_SSL_DEBUG_MSG(3, ("adding EMPTY_RENEGOTIATION_INFO_SCSV"));
  328. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  329. MBEDTLS_PUT_UINT16_BE(MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO, p, 0);
  330. p += 2;
  331. }
  332. /* Write the cipher_suites length in number of bytes */
  333. cipher_suites_len = (size_t) (p - cipher_suites);
  334. MBEDTLS_PUT_UINT16_BE(cipher_suites_len, buf, 0);
  335. MBEDTLS_SSL_DEBUG_MSG(3,
  336. ("client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
  337. cipher_suites_len/2));
  338. /* Output the total length of cipher_suites field. */
  339. *out_len = (size_t) (p - buf);
  340. return 0;
  341. }
  342. /*
  343. * Structure of the TLS 1.3 ClientHello message:
  344. *
  345. * struct {
  346. * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
  347. * Random random;
  348. * opaque legacy_session_id<0..32>;
  349. * CipherSuite cipher_suites<2..2^16-2>;
  350. * opaque legacy_compression_methods<1..2^8-1>;
  351. * Extension extensions<8..2^16-1>;
  352. * } ClientHello;
  353. *
  354. * Structure of the (D)TLS 1.2 ClientHello message:
  355. *
  356. * struct {
  357. * ProtocolVersion client_version;
  358. * Random random;
  359. * SessionID session_id;
  360. * opaque cookie<0..2^8-1>; // DTLS 1.2 ONLY
  361. * CipherSuite cipher_suites<2..2^16-2>;
  362. * CompressionMethod compression_methods<1..2^8-1>;
  363. * select (extensions_present) {
  364. * case false:
  365. * struct {};
  366. * case true:
  367. * Extension extensions<0..2^16-1>;
  368. * };
  369. * } ClientHello;
  370. */
  371. MBEDTLS_CHECK_RETURN_CRITICAL
  372. static int ssl_write_client_hello_body(mbedtls_ssl_context *ssl,
  373. unsigned char *buf,
  374. unsigned char *end,
  375. size_t *out_len,
  376. size_t *binders_len)
  377. {
  378. int ret;
  379. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  380. unsigned char *p = buf;
  381. unsigned char *p_extensions_len; /* Pointer to extensions length */
  382. size_t output_len; /* Length of buffer used by function */
  383. size_t extensions_len; /* Length of the list of extensions*/
  384. int tls12_uses_ec = 0;
  385. *out_len = 0;
  386. *binders_len = 0;
  387. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  388. unsigned char propose_tls12 =
  389. (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_2)
  390. &&
  391. (MBEDTLS_SSL_VERSION_TLS1_2 <= ssl->tls_version);
  392. #endif
  393. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  394. unsigned char propose_tls13 =
  395. (handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3)
  396. &&
  397. (MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version);
  398. #endif
  399. /*
  400. * Write client_version (TLS 1.2) or legacy_version (TLS 1.3)
  401. *
  402. * In all cases this is the TLS 1.2 version.
  403. */
  404. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  405. mbedtls_ssl_write_version(p, ssl->conf->transport,
  406. MBEDTLS_SSL_VERSION_TLS1_2);
  407. p += 2;
  408. /* ...
  409. * Random random;
  410. * ...
  411. *
  412. * The random bytes have been prepared by ssl_prepare_client_hello() into
  413. * the handshake->randbytes buffer and are copied here into the output
  414. * buffer.
  415. */
  416. MBEDTLS_SSL_CHK_BUF_PTR(p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
  417. memcpy(p, handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
  418. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, random bytes",
  419. p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN);
  420. p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
  421. /* TLS 1.2:
  422. * ...
  423. * SessionID session_id;
  424. * ...
  425. * with
  426. * opaque SessionID<0..32>;
  427. *
  428. * TLS 1.3:
  429. * ...
  430. * opaque legacy_session_id<0..32>;
  431. * ...
  432. *
  433. * The (legacy) session identifier bytes have been prepared by
  434. * ssl_prepare_client_hello() into the ssl->session_negotiate->id buffer
  435. * and are copied here into the output buffer.
  436. */
  437. MBEDTLS_SSL_CHK_BUF_PTR(p, end, ssl->session_negotiate->id_len + 1);
  438. *p++ = (unsigned char) ssl->session_negotiate->id_len;
  439. memcpy(p, ssl->session_negotiate->id, ssl->session_negotiate->id_len);
  440. p += ssl->session_negotiate->id_len;
  441. MBEDTLS_SSL_DEBUG_BUF(3, "session id", ssl->session_negotiate->id,
  442. ssl->session_negotiate->id_len);
  443. /* DTLS 1.2 ONLY
  444. * ...
  445. * opaque cookie<0..2^8-1>;
  446. * ...
  447. */
  448. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
  449. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  450. #if !defined(MBEDTLS_SSL_PROTO_TLS1_3)
  451. uint8_t cookie_len = 0;
  452. #else
  453. uint16_t cookie_len = 0;
  454. #endif /* !MBEDTLS_SSL_PROTO_TLS1_3 */
  455. if (handshake->cookie != NULL) {
  456. MBEDTLS_SSL_DEBUG_BUF(3, "client hello, cookie",
  457. handshake->cookie,
  458. handshake->cookie_len);
  459. cookie_len = handshake->cookie_len;
  460. }
  461. MBEDTLS_SSL_CHK_BUF_PTR(p, end, cookie_len + 1);
  462. *p++ = (unsigned char) cookie_len;
  463. if (cookie_len > 0) {
  464. memcpy(p, handshake->cookie, cookie_len);
  465. p += cookie_len;
  466. }
  467. }
  468. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
  469. /* Write cipher_suites */
  470. ret = ssl_write_client_hello_cipher_suites(ssl, p, end,
  471. &tls12_uses_ec,
  472. &output_len);
  473. if (ret != 0) {
  474. return ret;
  475. }
  476. p += output_len;
  477. /* Write legacy_compression_methods (TLS 1.3) or
  478. * compression_methods (TLS 1.2)
  479. *
  480. * For every TLS 1.3 ClientHello, this vector MUST contain exactly
  481. * one byte set to zero, which corresponds to the 'null' compression
  482. * method in prior versions of TLS.
  483. *
  484. * For TLS 1.2 ClientHello, for security reasons we do not support
  485. * compression anymore, thus also just the 'null' compression method.
  486. */
  487. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  488. *p++ = 1;
  489. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  490. /* Write extensions */
  491. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  492. /* Keeping track of the included extensions */
  493. handshake->sent_extensions = MBEDTLS_SSL_EXT_MASK_NONE;
  494. #endif
  495. /* First write extensions, then the total length */
  496. MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
  497. p_extensions_len = p;
  498. p += 2;
  499. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  500. /* Write server name extension */
  501. ret = ssl_write_hostname_ext(ssl, p, end, &output_len);
  502. if (ret != 0) {
  503. return ret;
  504. }
  505. p += output_len;
  506. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  507. #if defined(MBEDTLS_SSL_ALPN)
  508. ret = ssl_write_alpn_ext(ssl, p, end, &output_len);
  509. if (ret != 0) {
  510. return ret;
  511. }
  512. p += output_len;
  513. #endif /* MBEDTLS_SSL_ALPN */
  514. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  515. if (propose_tls13) {
  516. ret = mbedtls_ssl_tls13_write_client_hello_exts(ssl, p, end,
  517. &output_len);
  518. if (ret != 0) {
  519. return ret;
  520. }
  521. p += output_len;
  522. }
  523. #endif
  524. #if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC) || \
  525. defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  526. {
  527. int ssl_write_supported_groups_ext_flags = 0;
  528. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED)
  529. if (propose_tls13 && mbedtls_ssl_conf_tls13_is_some_ephemeral_enabled(ssl)) {
  530. ssl_write_supported_groups_ext_flags |=
  531. SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_3_FLAG;
  532. }
  533. #endif
  534. #if defined(MBEDTLS_SSL_TLS1_2_SOME_ECC)
  535. if (propose_tls12 && tls12_uses_ec) {
  536. ssl_write_supported_groups_ext_flags |=
  537. SSL_WRITE_SUPPORTED_GROUPS_EXT_TLS1_2_FLAG;
  538. }
  539. #endif
  540. if (ssl_write_supported_groups_ext_flags != 0) {
  541. ret = ssl_write_supported_groups_ext(ssl, p, end,
  542. ssl_write_supported_groups_ext_flags,
  543. &output_len);
  544. if (ret != 0) {
  545. return ret;
  546. }
  547. p += output_len;
  548. }
  549. }
  550. #endif /* MBEDTLS_SSL_TLS1_2_SOME_ECC ||
  551. MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_EPHEMERAL_ENABLED */
  552. #if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
  553. int write_sig_alg_ext = 0;
  554. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  555. write_sig_alg_ext = write_sig_alg_ext ||
  556. (propose_tls13 && mbedtls_ssl_conf_tls13_is_ephemeral_enabled(ssl));
  557. #endif
  558. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  559. write_sig_alg_ext = write_sig_alg_ext || propose_tls12;
  560. #endif
  561. if (write_sig_alg_ext) {
  562. ret = mbedtls_ssl_write_sig_alg_ext(ssl, p, end, &output_len);
  563. if (ret != 0) {
  564. return ret;
  565. }
  566. p += output_len;
  567. }
  568. #endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
  569. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  570. if (propose_tls12) {
  571. ret = mbedtls_ssl_tls12_write_client_hello_exts(ssl, p, end,
  572. tls12_uses_ec,
  573. &output_len);
  574. if (ret != 0) {
  575. return ret;
  576. }
  577. p += output_len;
  578. }
  579. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  580. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  581. /* The "pre_shared_key" extension (RFC 8446 Section 4.2.11)
  582. * MUST be the last extension in the ClientHello.
  583. */
  584. if (propose_tls13 && mbedtls_ssl_conf_tls13_is_some_psk_enabled(ssl)) {
  585. ret = mbedtls_ssl_tls13_write_identities_of_pre_shared_key_ext(
  586. ssl, p, end, &output_len, binders_len);
  587. if (ret != 0) {
  588. return ret;
  589. }
  590. p += output_len;
  591. }
  592. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  593. /* Write the length of the list of extensions. */
  594. extensions_len = (size_t) (p - p_extensions_len) - 2;
  595. if (extensions_len == 0) {
  596. p = p_extensions_len;
  597. } else {
  598. MBEDTLS_PUT_UINT16_BE(extensions_len, p_extensions_len, 0);
  599. MBEDTLS_SSL_DEBUG_MSG(3, ("client hello, total extension length: %" \
  600. MBEDTLS_PRINTF_SIZET, extensions_len));
  601. MBEDTLS_SSL_DEBUG_BUF(3, "client hello extensions",
  602. p_extensions_len, extensions_len);
  603. }
  604. *out_len = (size_t) (p - buf);
  605. return 0;
  606. }
  607. MBEDTLS_CHECK_RETURN_CRITICAL
  608. static int ssl_generate_random(mbedtls_ssl_context *ssl)
  609. {
  610. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  611. unsigned char *randbytes = ssl->handshake->randbytes;
  612. size_t gmt_unix_time_len = 0;
  613. /*
  614. * Generate the random bytes
  615. *
  616. * TLS 1.2 case:
  617. * struct {
  618. * uint32 gmt_unix_time;
  619. * opaque random_bytes[28];
  620. * } Random;
  621. *
  622. * TLS 1.3 case:
  623. * opaque Random[32];
  624. */
  625. if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
  626. #if defined(MBEDTLS_HAVE_TIME)
  627. mbedtls_time_t gmt_unix_time = mbedtls_time(NULL);
  628. MBEDTLS_PUT_UINT32_BE(gmt_unix_time, randbytes, 0);
  629. gmt_unix_time_len = 4;
  630. MBEDTLS_SSL_DEBUG_MSG(3,
  631. ("client hello, current time: %" MBEDTLS_PRINTF_LONGLONG,
  632. (long long) gmt_unix_time));
  633. #endif /* MBEDTLS_HAVE_TIME */
  634. }
  635. ret = ssl->conf->f_rng(ssl->conf->p_rng,
  636. randbytes + gmt_unix_time_len,
  637. MBEDTLS_CLIENT_HELLO_RANDOM_LEN - gmt_unix_time_len);
  638. return ret;
  639. }
  640. MBEDTLS_CHECK_RETURN_CRITICAL
  641. static int ssl_prepare_client_hello(mbedtls_ssl_context *ssl)
  642. {
  643. int ret;
  644. size_t session_id_len;
  645. mbedtls_ssl_session *session_negotiate = ssl->session_negotiate;
  646. if (session_negotiate == NULL) {
  647. return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  648. }
  649. #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
  650. defined(MBEDTLS_SSL_SESSION_TICKETS) && \
  651. defined(MBEDTLS_HAVE_TIME)
  652. /* Check if a tls13 ticket has been configured. */
  653. if (ssl->handshake->resume != 0 &&
  654. session_negotiate->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
  655. session_negotiate->ticket != NULL) {
  656. mbedtls_ms_time_t now = mbedtls_ms_time();
  657. mbedtls_ms_time_t age = now - session_negotiate->ticket_reception_time;
  658. if (age < 0 ||
  659. age > (mbedtls_ms_time_t) session_negotiate->ticket_lifetime * 1000) {
  660. /* Without valid ticket, disable session resumption.*/
  661. MBEDTLS_SSL_DEBUG_MSG(
  662. 3, ("Ticket expired, disable session resumption"));
  663. ssl->handshake->resume = 0;
  664. }
  665. }
  666. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
  667. MBEDTLS_SSL_SESSION_TICKETS &&
  668. MBEDTLS_HAVE_TIME */
  669. /* Bet on the highest configured version if we are not in a TLS 1.2
  670. * renegotiation or session resumption.
  671. */
  672. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  673. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  674. ssl->handshake->min_tls_version = ssl->tls_version;
  675. } else
  676. #endif
  677. {
  678. if (ssl->handshake->resume) {
  679. ssl->tls_version = session_negotiate->tls_version;
  680. ssl->handshake->min_tls_version = ssl->tls_version;
  681. } else {
  682. ssl->handshake->min_tls_version = ssl->conf->min_tls_version;
  683. }
  684. }
  685. /*
  686. * Generate the random bytes, except when responding to a verify request
  687. * where we MUST reuse the previously generated random bytes
  688. * (RFC 6347 4.2.1).
  689. */
  690. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  691. if ((ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) ||
  692. (ssl->handshake->cookie == NULL))
  693. #endif
  694. {
  695. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  696. if (!ssl->handshake->hello_retry_request_flag)
  697. #endif
  698. {
  699. ret = ssl_generate_random(ssl);
  700. if (ret != 0) {
  701. MBEDTLS_SSL_DEBUG_RET(1, "Random bytes generation failed", ret);
  702. return ret;
  703. }
  704. }
  705. }
  706. /*
  707. * Prepare session identifier. At that point, the length of the session
  708. * identifier in the SSL context `ssl->session_negotiate->id_len` is equal
  709. * to zero, except in the case of a TLS 1.2 session renegotiation or
  710. * session resumption.
  711. */
  712. session_id_len = session_negotiate->id_len;
  713. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  714. if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
  715. if (session_id_len < 16 || session_id_len > 32 ||
  716. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  717. ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE ||
  718. #endif
  719. ssl->handshake->resume == 0) {
  720. session_id_len = 0;
  721. }
  722. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  723. /*
  724. * RFC 5077 section 3.4: "When presenting a ticket, the client MAY
  725. * generate and include a Session ID in the TLS ClientHello."
  726. */
  727. int renegotiating = 0;
  728. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  729. if (ssl->renego_status != MBEDTLS_SSL_INITIAL_HANDSHAKE) {
  730. renegotiating = 1;
  731. }
  732. #endif
  733. if (!renegotiating) {
  734. if ((session_negotiate->ticket != NULL) &&
  735. (session_negotiate->ticket_len != 0)) {
  736. session_id_len = 32;
  737. }
  738. }
  739. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  740. }
  741. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  742. #if defined(MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE)
  743. if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
  744. /*
  745. * Create a legacy session identifier for the purpose of middlebox
  746. * compatibility only if one has not been created already, which is
  747. * the case if we are here for the TLS 1.3 second ClientHello.
  748. *
  749. * Versions of TLS before TLS 1.3 supported a "session resumption"
  750. * feature which has been merged with pre-shared keys in TLS 1.3
  751. * version. A client which has a cached session ID set by a pre-TLS 1.3
  752. * server SHOULD set this field to that value. In compatibility mode,
  753. * this field MUST be non-empty, so a client not offering a pre-TLS 1.3
  754. * session MUST generate a new 32-byte value. This value need not be
  755. * random but SHOULD be unpredictable to avoid implementations fixating
  756. * on a specific value (also known as ossification). Otherwise, it MUST
  757. * be set as a zero-length vector ( i.e., a zero-valued single byte
  758. * length field ).
  759. */
  760. session_id_len = 32;
  761. }
  762. #endif /* MBEDTLS_SSL_TLS1_3_COMPATIBILITY_MODE */
  763. if (session_id_len != session_negotiate->id_len) {
  764. session_negotiate->id_len = session_id_len;
  765. if (session_id_len > 0) {
  766. ret = ssl->conf->f_rng(ssl->conf->p_rng,
  767. session_negotiate->id,
  768. session_id_len);
  769. if (ret != 0) {
  770. MBEDTLS_SSL_DEBUG_RET(1, "creating session id failed", ret);
  771. return ret;
  772. }
  773. }
  774. }
  775. #if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
  776. defined(MBEDTLS_SSL_SESSION_TICKETS) && \
  777. defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  778. if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
  779. ssl->handshake->resume) {
  780. int hostname_mismatch = ssl->hostname != NULL ||
  781. session_negotiate->hostname != NULL;
  782. if (ssl->hostname != NULL && session_negotiate->hostname != NULL) {
  783. hostname_mismatch = strcmp(
  784. ssl->hostname, session_negotiate->hostname) != 0;
  785. }
  786. if (hostname_mismatch) {
  787. MBEDTLS_SSL_DEBUG_MSG(
  788. 1, ("Hostname mismatch the session ticket, "
  789. "disable session resumption."));
  790. return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
  791. }
  792. } else {
  793. return mbedtls_ssl_session_set_hostname(session_negotiate,
  794. ssl->hostname);
  795. }
  796. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
  797. MBEDTLS_SSL_SESSION_TICKETS &&
  798. MBEDTLS_SSL_SERVER_NAME_INDICATION */
  799. return 0;
  800. }
  801. /*
  802. * Write ClientHello handshake message.
  803. * Handler for MBEDTLS_SSL_CLIENT_HELLO
  804. */
  805. int mbedtls_ssl_write_client_hello(mbedtls_ssl_context *ssl)
  806. {
  807. int ret = 0;
  808. unsigned char *buf;
  809. size_t buf_len, msg_len, binders_len;
  810. MBEDTLS_SSL_DEBUG_MSG(2, ("=> write client hello"));
  811. MBEDTLS_SSL_PROC_CHK(ssl_prepare_client_hello(ssl));
  812. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_start_handshake_msg(
  813. ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
  814. &buf, &buf_len));
  815. MBEDTLS_SSL_PROC_CHK(ssl_write_client_hello_body(ssl, buf,
  816. buf + buf_len,
  817. &msg_len,
  818. &binders_len));
  819. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_DTLS)
  820. if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
  821. ssl->out_msglen = msg_len + 4;
  822. mbedtls_ssl_send_flight_completed(ssl);
  823. /*
  824. * The two functions below may try to send data on the network and
  825. * can return with the MBEDTLS_ERR_SSL_WANT_READ error code when they
  826. * fail to do so and the transmission has to be retried later. In that
  827. * case as in fatal error cases, we return immediately. But we must have
  828. * set the handshake state to the next state at that point to ensure
  829. * that we will not write and send again a ClientHello when we
  830. * eventually succeed in sending the pending data.
  831. */
  832. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
  833. if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
  834. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
  835. return ret;
  836. }
  837. if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
  838. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
  839. return ret;
  840. }
  841. } else
  842. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 && MBEDTLS_SSL_PROTO_DTLS */
  843. {
  844. ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl,
  845. MBEDTLS_SSL_HS_CLIENT_HELLO,
  846. msg_len);
  847. if (ret != 0) {
  848. MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_add_hs_hdr_to_checksum", ret);
  849. return ret;
  850. }
  851. ret = ssl->handshake->update_checksum(ssl, buf, msg_len - binders_len);
  852. if (ret != 0) {
  853. MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
  854. return ret;
  855. }
  856. #if defined(MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED)
  857. if (binders_len > 0) {
  858. MBEDTLS_SSL_PROC_CHK(
  859. mbedtls_ssl_tls13_write_binders_of_pre_shared_key_ext(
  860. ssl, buf + msg_len - binders_len, buf + msg_len));
  861. ret = ssl->handshake->update_checksum(ssl, buf + msg_len - binders_len,
  862. binders_len);
  863. if (ret != 0) {
  864. MBEDTLS_SSL_DEBUG_RET(1, "update_checksum", ret);
  865. return ret;
  866. }
  867. }
  868. #endif /* MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_SOME_PSK_ENABLED */
  869. MBEDTLS_SSL_PROC_CHK(mbedtls_ssl_finish_handshake_msg(ssl,
  870. buf_len,
  871. msg_len));
  872. /*
  873. * Set next state. Note that if TLS 1.3 is proposed, this may be
  874. * overwritten by mbedtls_ssl_tls13_finalize_client_hello().
  875. */
  876. mbedtls_ssl_handshake_set_state(ssl, MBEDTLS_SSL_SERVER_HELLO);
  877. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  878. if (ssl->handshake->min_tls_version <= MBEDTLS_SSL_VERSION_TLS1_3 &&
  879. MBEDTLS_SSL_VERSION_TLS1_3 <= ssl->tls_version) {
  880. ret = mbedtls_ssl_tls13_finalize_client_hello(ssl);
  881. }
  882. #endif
  883. }
  884. #if defined(MBEDTLS_SSL_PROTO_TLS1_3)
  885. MBEDTLS_SSL_PRINT_EXTS(
  886. 3, MBEDTLS_SSL_HS_CLIENT_HELLO, ssl->handshake->sent_extensions);
  887. #endif
  888. cleanup:
  889. MBEDTLS_SSL_DEBUG_MSG(2, ("<= write client hello"));
  890. return ret;
  891. }
  892. #endif /* MBEDTLS_SSL_PROTO_TLS1_3 || MBEDTLS_SSL_PROTO_TLS1_2 */
  893. #endif /* MBEDTLS_SSL_CLI_C */