ssl_client.c 34 KB

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