ssl_client.c 33 KB

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