ssl_tls13_client.c 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726
  1. /*
  2. * TLS 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_PROTO_TLS1_3_EXPERIMENTAL)
  23. #if defined(MBEDTLS_SSL_CLI_C)
  24. #include <string.h>
  25. #include "mbedtls/debug.h"
  26. #include "mbedtls/error.h"
  27. #include "mbedtls/platform.h"
  28. #include "ssl_misc.h"
  29. #include "ecdh_misc.h"
  30. #include "ssl_tls13_keys.h"
  31. /* Write extensions */
  32. /*
  33. * ssl_tls13_write_supported_versions_ext():
  34. *
  35. * struct {
  36. * ProtocolVersion versions<2..254>;
  37. * } SupportedVersions;
  38. */
  39. static int ssl_tls13_write_supported_versions_ext( mbedtls_ssl_context *ssl,
  40. unsigned char *buf,
  41. unsigned char *end,
  42. size_t *olen )
  43. {
  44. unsigned char *p = buf;
  45. *olen = 0;
  46. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported versions extension" ) );
  47. /* Check if we have space to write the extension:
  48. * - extension_type (2 bytes)
  49. * - extension_data_length (2 bytes)
  50. * - versions_length (1 byte )
  51. * - versions (2 bytes)
  52. */
  53. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 7 );
  54. /* Write extension_type */
  55. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS, p, 0 );
  56. /* Write extension_data_length */
  57. MBEDTLS_PUT_UINT16_BE( 3, p, 2 );
  58. p += 4;
  59. /* Length of versions */
  60. *p++ = 0x2;
  61. /* Write values of supported versions.
  62. *
  63. * They are defined by the configuration.
  64. *
  65. * Currently, only one version is advertised.
  66. */
  67. mbedtls_ssl_write_version( ssl->conf->max_major_ver,
  68. ssl->conf->max_minor_ver,
  69. ssl->conf->transport, p );
  70. MBEDTLS_SSL_DEBUG_MSG( 3, ( "supported version: [%d:%d]",
  71. ssl->conf->max_major_ver,
  72. ssl->conf->max_minor_ver ) );
  73. *olen = 7;
  74. return( 0 );
  75. }
  76. static int ssl_tls13_parse_supported_versions_ext( mbedtls_ssl_context *ssl,
  77. const unsigned char *buf,
  78. const unsigned char *end )
  79. {
  80. ((void) ssl);
  81. MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2);
  82. if( buf[0] != MBEDTLS_SSL_MAJOR_VERSION_3 ||
  83. buf[1] != MBEDTLS_SSL_MINOR_VERSION_4 )
  84. {
  85. MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected version" ) );
  86. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  87. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  88. return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  89. }
  90. return( 0 );
  91. }
  92. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  93. /*
  94. * Functions for writing supported_groups extension.
  95. *
  96. * Stucture of supported_groups:
  97. * enum {
  98. * secp256r1(0x0017), secp384r1(0x0018), secp521r1(0x0019),
  99. * x25519(0x001D), x448(0x001E),
  100. * ffdhe2048(0x0100), ffdhe3072(0x0101), ffdhe4096(0x0102),
  101. * ffdhe6144(0x0103), ffdhe8192(0x0104),
  102. * ffdhe_private_use(0x01FC..0x01FF),
  103. * ecdhe_private_use(0xFE00..0xFEFF),
  104. * (0xFFFF)
  105. * } NamedGroup;
  106. * struct {
  107. * NamedGroup named_group_list<2..2^16-1>;
  108. * } NamedGroupList;
  109. */
  110. #if defined(MBEDTLS_ECDH_C)
  111. /*
  112. * In versions of TLS prior to TLS 1.3, this extension was named
  113. * 'elliptic_curves' and only contained elliptic curve groups.
  114. */
  115. static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl,
  116. unsigned char *buf,
  117. unsigned char *end,
  118. size_t *olen )
  119. {
  120. unsigned char *p = buf;
  121. *olen = 0;
  122. const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
  123. if( group_list == NULL )
  124. return( MBEDTLS_ERR_SSL_BAD_CONFIG );
  125. for ( ; *group_list != 0; group_list++ )
  126. {
  127. const mbedtls_ecp_curve_info *info;
  128. info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
  129. if( info == NULL )
  130. continue;
  131. if( !mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
  132. continue;
  133. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2);
  134. MBEDTLS_PUT_UINT16_BE( *group_list, p, 0 );
  135. p += 2;
  136. MBEDTLS_SSL_DEBUG_MSG( 3, ( "NamedGroup: %s ( %x )",
  137. info->name, *group_list ) );
  138. }
  139. *olen = p - buf;
  140. return( 0 );
  141. }
  142. #else
  143. static int ssl_tls13_write_named_group_list_ecdhe( mbedtls_ssl_context *ssl,
  144. unsigned char *buf,
  145. unsigned char *end,
  146. size_t *olen )
  147. {
  148. ((void) ssl);
  149. ((void) buf);
  150. ((void) end);
  151. *olen = 0;
  152. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  153. }
  154. #endif /* MBEDTLS_ECDH_C */
  155. static int ssl_tls13_write_named_group_list_dhe( mbedtls_ssl_context *ssl,
  156. unsigned char *buf,
  157. unsigned char *end,
  158. size_t *olen )
  159. {
  160. ((void) ssl);
  161. ((void) buf);
  162. ((void) end);
  163. *olen = 0;
  164. MBEDTLS_SSL_DEBUG_MSG( 3, ( "write_named_group_dhe is not implemented" ) );
  165. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  166. }
  167. static int ssl_tls13_write_supported_groups_ext( mbedtls_ssl_context *ssl,
  168. unsigned char *buf,
  169. unsigned char *end,
  170. size_t *olen )
  171. {
  172. unsigned char *p = buf ;
  173. unsigned char *named_group_list_ptr; /* Start of named_group_list */
  174. size_t named_group_list_len; /* Length of named_group_list */
  175. size_t output_len = 0;
  176. int ret_ecdhe, ret_dhe;
  177. *olen = 0;
  178. if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
  179. return( 0 );
  180. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, adding supported_groups extension" ) );
  181. /* Check if we have space for header and length fields:
  182. * - extension_type (2 bytes)
  183. * - extension_data_length (2 bytes)
  184. * - named_group_list_length (2 bytes)
  185. */
  186. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
  187. p += 6;
  188. named_group_list_ptr = p;
  189. ret_ecdhe = ssl_tls13_write_named_group_list_ecdhe( ssl, p, end, &output_len );
  190. if( ret_ecdhe != 0 )
  191. {
  192. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_ecdhe", ret_ecdhe );
  193. }
  194. p += output_len;
  195. ret_dhe = ssl_tls13_write_named_group_list_dhe( ssl, p, end, &output_len );
  196. if( ret_dhe != 0 )
  197. {
  198. MBEDTLS_SSL_DEBUG_RET( 1, "ssl_tls13_write_named_group_list_dhe", ret_dhe );
  199. }
  200. p += output_len;
  201. /* Both ECDHE and DHE failed. */
  202. if( ret_ecdhe != 0 && ret_dhe != 0 )
  203. {
  204. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Both ECDHE and DHE groups are fail. " ) );
  205. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  206. }
  207. /* Length of named_group_list*/
  208. named_group_list_len = p - named_group_list_ptr;
  209. if( named_group_list_len == 0 )
  210. {
  211. MBEDTLS_SSL_DEBUG_MSG( 1, ( "No group available." ) );
  212. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  213. }
  214. /* Write extension_type */
  215. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SUPPORTED_GROUPS, buf, 0 );
  216. /* Write extension_data_length */
  217. MBEDTLS_PUT_UINT16_BE( named_group_list_len + 2, buf, 2 );
  218. /* Write length of named_group_list */
  219. MBEDTLS_PUT_UINT16_BE( named_group_list_len, buf, 4 );
  220. MBEDTLS_SSL_DEBUG_BUF( 3, "Supported groups extension", buf + 4, named_group_list_len + 2 );
  221. *olen = p - buf;
  222. ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SUPPORTED_GROUPS;
  223. return( 0 );
  224. }
  225. /*
  226. * Functions for writing key_share extension.
  227. */
  228. #if defined(MBEDTLS_ECDH_C)
  229. static int ssl_tls13_generate_and_write_ecdh_key_exchange(
  230. mbedtls_ssl_context *ssl,
  231. uint16_t named_group,
  232. unsigned char *buf,
  233. unsigned char *end,
  234. size_t *olen )
  235. {
  236. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  237. const mbedtls_ecp_curve_info *curve_info =
  238. mbedtls_ecp_curve_info_from_tls_id( named_group );
  239. if( curve_info == NULL )
  240. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  241. MBEDTLS_SSL_DEBUG_MSG( 3, ( "offer curve %s", curve_info->name ) );
  242. if( ( ret = mbedtls_ecdh_setup_no_everest( &ssl->handshake->ecdh_ctx,
  243. curve_info->grp_id ) ) != 0 )
  244. {
  245. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_setup_no_everest", ret );
  246. return( ret );
  247. }
  248. ret = mbedtls_ecdh_tls13_make_params( &ssl->handshake->ecdh_ctx, olen,
  249. buf, end - buf,
  250. ssl->conf->f_rng, ssl->conf->p_rng );
  251. if( ret != 0 )
  252. {
  253. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_tls13_make_params", ret );
  254. return( ret );
  255. }
  256. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  257. MBEDTLS_DEBUG_ECDH_Q );
  258. return( 0 );
  259. }
  260. #endif /* MBEDTLS_ECDH_C */
  261. static int ssl_tls13_get_default_group_id( mbedtls_ssl_context *ssl,
  262. uint16_t *group_id )
  263. {
  264. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  265. #if defined(MBEDTLS_ECDH_C)
  266. const uint16_t *group_list = mbedtls_ssl_get_groups( ssl );
  267. /* Pick first available ECDHE group compatible with TLS 1.3 */
  268. if( group_list == NULL )
  269. return( MBEDTLS_ERR_SSL_BAD_CONFIG );
  270. for ( ; *group_list != 0; group_list++ )
  271. {
  272. const mbedtls_ecp_curve_info *info;
  273. info = mbedtls_ecp_curve_info_from_tls_id( *group_list );
  274. if( info != NULL &&
  275. mbedtls_ssl_tls13_named_group_is_ecdhe( *group_list ) )
  276. {
  277. *group_id = *group_list;
  278. return( 0 );
  279. }
  280. }
  281. #else
  282. ((void) ssl);
  283. ((void) group_id);
  284. #endif /* MBEDTLS_ECDH_C */
  285. /*
  286. * Add DHE named groups here.
  287. * Pick first available DHE group compatible with TLS 1.3
  288. */
  289. return( ret );
  290. }
  291. /*
  292. * ssl_tls13_write_key_share_ext
  293. *
  294. * Structure of key_share extension in ClientHello:
  295. *
  296. * struct {
  297. * NamedGroup group;
  298. * opaque key_exchange<1..2^16-1>;
  299. * } KeyShareEntry;
  300. * struct {
  301. * KeyShareEntry client_shares<0..2^16-1>;
  302. * } KeyShareClientHello;
  303. */
  304. static int ssl_tls13_write_key_share_ext( mbedtls_ssl_context *ssl,
  305. unsigned char *buf,
  306. unsigned char *end,
  307. size_t *olen )
  308. {
  309. unsigned char *p = buf;
  310. unsigned char *client_shares_ptr; /* Start of client_shares */
  311. size_t client_shares_len; /* Length of client_shares */
  312. uint16_t group_id;
  313. int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
  314. *olen = 0;
  315. if( !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
  316. return( 0 );
  317. /* Check if we have space for header and length fields:
  318. * - extension_type (2 bytes)
  319. * - extension_data_length (2 bytes)
  320. * - client_shares_length (2 bytes)
  321. */
  322. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
  323. p += 6;
  324. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello: adding key share extension" ) );
  325. /* HRR could already have requested something else. */
  326. group_id = ssl->handshake->offered_group_id;
  327. if( !mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) &&
  328. !mbedtls_ssl_tls13_named_group_is_dhe( group_id ) )
  329. {
  330. MBEDTLS_SSL_PROC_CHK( ssl_tls13_get_default_group_id( ssl,
  331. &group_id ) );
  332. }
  333. /*
  334. * Dispatch to type-specific key generation function.
  335. *
  336. * So far, we're only supporting ECDHE. With the introduction
  337. * of PQC KEMs, we'll want to have multiple branches, one per
  338. * type of KEM, and dispatch to the corresponding crypto. And
  339. * only one key share entry is allowed.
  340. */
  341. client_shares_ptr = p;
  342. #if defined(MBEDTLS_ECDH_C)
  343. if( mbedtls_ssl_tls13_named_group_is_ecdhe( group_id ) )
  344. {
  345. /* Pointer to group */
  346. unsigned char *group_ptr = p;
  347. /* Length of key_exchange */
  348. size_t key_exchange_len;
  349. /* Check there is space for header of KeyShareEntry
  350. * - group (2 bytes)
  351. * - key_exchange_length (2 bytes)
  352. */
  353. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 4 );
  354. p += 4;
  355. ret = ssl_tls13_generate_and_write_ecdh_key_exchange( ssl, group_id,
  356. p, end,
  357. &key_exchange_len );
  358. p += key_exchange_len;
  359. if( ret != 0 )
  360. return( ret );
  361. /* Write group */
  362. MBEDTLS_PUT_UINT16_BE( group_id, group_ptr, 0 );
  363. /* Write key_exchange_length */
  364. MBEDTLS_PUT_UINT16_BE( key_exchange_len, group_ptr, 2 );
  365. }
  366. else
  367. #endif /* MBEDTLS_ECDH_C */
  368. if( 0 /* other KEMs? */ )
  369. {
  370. /* Do something */
  371. }
  372. else
  373. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  374. /* Length of client_shares */
  375. client_shares_len = p - client_shares_ptr;
  376. if( client_shares_len == 0)
  377. {
  378. MBEDTLS_SSL_DEBUG_MSG( 1, ( "No key share defined." ) );
  379. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  380. }
  381. /* Write extension_type */
  382. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_KEY_SHARE, buf, 0 );
  383. /* Write extension_data_length */
  384. MBEDTLS_PUT_UINT16_BE( client_shares_len + 2, buf, 2 );
  385. /* Write client_shares_length */
  386. MBEDTLS_PUT_UINT16_BE( client_shares_len, buf, 4 );
  387. /* Update offered_group_id field */
  388. ssl->handshake->offered_group_id = group_id;
  389. /* Output the total length of key_share extension. */
  390. *olen = p - buf;
  391. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, key_share extension", buf, *olen );
  392. ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
  393. cleanup:
  394. return( ret );
  395. }
  396. #if defined(MBEDTLS_ECDH_C)
  397. static int ssl_tls13_check_ecdh_params( const mbedtls_ssl_context *ssl )
  398. {
  399. const mbedtls_ecp_curve_info *curve_info;
  400. mbedtls_ecp_group_id grp_id;
  401. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  402. grp_id = ssl->handshake->ecdh_ctx.grp.id;
  403. #else
  404. grp_id = ssl->handshake->ecdh_ctx.grp_id;
  405. #endif
  406. curve_info = mbedtls_ecp_curve_info_from_grp_id( grp_id );
  407. if( curve_info == NULL )
  408. {
  409. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  410. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  411. }
  412. MBEDTLS_SSL_DEBUG_MSG( 2, ( "ECDH curve: %s", curve_info->name ) );
  413. if( mbedtls_ssl_check_curve( ssl, grp_id ) != 0 )
  414. return( -1 );
  415. MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
  416. MBEDTLS_DEBUG_ECDH_QP );
  417. return( 0 );
  418. }
  419. static int ssl_tls13_read_public_ecdhe_share( mbedtls_ssl_context *ssl,
  420. const unsigned char *buf,
  421. size_t buf_len )
  422. {
  423. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  424. ret = mbedtls_ecdh_tls13_read_public( &ssl->handshake->ecdh_ctx,
  425. buf, buf_len );
  426. if( ret != 0 )
  427. {
  428. MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ecdh_tls13_read_public" ), ret );
  429. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  430. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  431. return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  432. }
  433. if( ssl_tls13_check_ecdh_params( ssl ) != 0 )
  434. {
  435. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ssl_tls13_check_ecdh_params() failed!" ) );
  436. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  437. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  438. return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  439. }
  440. return( 0 );
  441. }
  442. #endif /* MBEDTLS_ECDH_C */
  443. /*
  444. * ssl_tls13_parse_key_share_ext()
  445. * Parse key_share extension in Server Hello
  446. *
  447. * struct {
  448. * KeyShareEntry server_share;
  449. * } KeyShareServerHello;
  450. * struct {
  451. * NamedGroup group;
  452. * opaque key_exchange<1..2^16-1>;
  453. * } KeyShareEntry;
  454. */
  455. static int ssl_tls13_parse_key_share_ext( mbedtls_ssl_context *ssl,
  456. const unsigned char *buf,
  457. const unsigned char *end )
  458. {
  459. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  460. const unsigned char *p = buf;
  461. uint16_t group, offered_group;
  462. /* ...
  463. * NamedGroup group; (2 bytes)
  464. * ...
  465. */
  466. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
  467. group = MBEDTLS_GET_UINT16_BE( p, 0 );
  468. p += 2;
  469. /* Check that the chosen group matches the one we offered. */
  470. offered_group = ssl->handshake->offered_group_id;
  471. if( offered_group != group )
  472. {
  473. MBEDTLS_SSL_DEBUG_MSG( 1,
  474. ( "Invalid server key share, our group %u, their group %u",
  475. (unsigned) offered_group, (unsigned) group ) );
  476. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  477. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  478. return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  479. }
  480. #if defined(MBEDTLS_ECDH_C)
  481. if( mbedtls_ssl_tls13_named_group_is_ecdhe( group ) )
  482. {
  483. /* Complete ECDHE key agreement */
  484. ret = ssl_tls13_read_public_ecdhe_share( ssl, p, end - p );
  485. if( ret != 0 )
  486. return( ret );
  487. }
  488. else
  489. #endif /* MBEDTLS_ECDH_C */
  490. if( 0 /* other KEMs? */ )
  491. {
  492. /* Do something */
  493. }
  494. else
  495. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  496. ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_KEY_SHARE;
  497. return( ret );
  498. }
  499. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  500. /* Write cipher_suites
  501. * CipherSuite cipher_suites<2..2^16-2>;
  502. */
  503. static int ssl_tls13_write_client_hello_cipher_suites(
  504. mbedtls_ssl_context *ssl,
  505. unsigned char *buf,
  506. unsigned char *end,
  507. size_t *olen )
  508. {
  509. unsigned char *p = buf;
  510. const int *ciphersuite_list;
  511. unsigned char *cipher_suites_ptr; /* Start of the cipher_suites list */
  512. size_t cipher_suites_len;
  513. *olen = 0 ;
  514. /*
  515. * Ciphersuite list
  516. *
  517. * This is a list of the symmetric cipher options supported by
  518. * the client, specifically the record protection algorithm
  519. * ( including secret key length ) and a hash to be used with
  520. * HKDF, in descending order of client preference.
  521. */
  522. ciphersuite_list = ssl->conf->ciphersuite_list;
  523. /* Check there is space for the cipher suite list length (2 bytes). */
  524. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  525. p += 2;
  526. /* Write cipher_suites */
  527. cipher_suites_ptr = p;
  528. for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
  529. {
  530. int cipher_suite = ciphersuite_list[i];
  531. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  532. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
  533. if( ciphersuite_info == NULL )
  534. continue;
  535. if( !( MBEDTLS_SSL_MINOR_VERSION_4 >= ciphersuite_info->min_minor_ver &&
  536. MBEDTLS_SSL_MINOR_VERSION_4 <= ciphersuite_info->max_minor_ver ) )
  537. continue;
  538. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, add ciphersuite: %04x, %s",
  539. (unsigned int) cipher_suite,
  540. ciphersuite_info->name ) );
  541. /* Check there is space for the cipher suite identifier (2 bytes). */
  542. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  543. MBEDTLS_PUT_UINT16_BE( cipher_suite, p, 0 );
  544. p += 2;
  545. }
  546. /* Write the cipher_suites length in number of bytes */
  547. cipher_suites_len = p - cipher_suites_ptr;
  548. MBEDTLS_PUT_UINT16_BE( cipher_suites_len, buf, 0 );
  549. MBEDTLS_SSL_DEBUG_MSG( 3,
  550. ( "client hello, got %" MBEDTLS_PRINTF_SIZET " cipher suites",
  551. cipher_suites_len/2 ) );
  552. /* Output the total length of cipher_suites field. */
  553. *olen = p - buf;
  554. return( 0 );
  555. }
  556. /*
  557. * Structure of ClientHello message:
  558. *
  559. * struct {
  560. * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
  561. * Random random;
  562. * opaque legacy_session_id<0..32>;
  563. * CipherSuite cipher_suites<2..2^16-2>;
  564. * opaque legacy_compression_methods<1..2^8-1>;
  565. * Extension extensions<8..2^16-1>;
  566. * } ClientHello;
  567. */
  568. static int ssl_tls13_write_client_hello_body( mbedtls_ssl_context *ssl,
  569. unsigned char *buf,
  570. unsigned char *end,
  571. size_t *olen )
  572. {
  573. int ret;
  574. unsigned char *extensions_len_ptr; /* Pointer to extensions length */
  575. size_t output_len; /* Length of buffer used by function */
  576. size_t extensions_len; /* Length of the list of extensions*/
  577. /* Buffer management */
  578. unsigned char *p = buf;
  579. *olen = 0;
  580. /* No validation needed here. It has been done by ssl_conf_check() */
  581. ssl->major_ver = ssl->conf->min_major_ver;
  582. ssl->minor_ver = ssl->conf->min_minor_ver;
  583. /*
  584. * Write legacy_version
  585. * ProtocolVersion legacy_version = 0x0303; // TLS v1.2
  586. *
  587. * For TLS 1.3 we use the legacy version number {0x03, 0x03}
  588. * instead of the true version number.
  589. */
  590. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  591. MBEDTLS_PUT_UINT16_BE( 0x0303, p, 0 );
  592. p += 2;
  593. /* Write the random bytes ( random ).*/
  594. MBEDTLS_SSL_CHK_BUF_PTR( p, end, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
  595. memcpy( p, ssl->handshake->randbytes, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
  596. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello, random bytes",
  597. p, MBEDTLS_CLIENT_HELLO_RANDOM_LEN );
  598. p += MBEDTLS_CLIENT_HELLO_RANDOM_LEN;
  599. /*
  600. * Write legacy_session_id
  601. *
  602. * Versions of TLS before TLS 1.3 supported a "session resumption" feature
  603. * which has been merged with pre-shared keys in this version. A client
  604. * which has a cached session ID set by a pre-TLS 1.3 server SHOULD set
  605. * this field to that value. In compatibility mode, this field MUST be
  606. * non-empty, so a client not offering a pre-TLS 1.3 session MUST generate
  607. * a new 32-byte value. This value need not be random but SHOULD be
  608. * unpredictable to avoid implementations fixating on a specific value
  609. * ( also known as ossification ). Otherwise, it MUST be set as a zero-length
  610. * vector ( i.e., a zero-valued single byte length field ).
  611. */
  612. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 1 );
  613. *p++ = 0; /* session id length set to zero */
  614. /* Write cipher_suites */
  615. ret = ssl_tls13_write_client_hello_cipher_suites( ssl, p, end, &output_len );
  616. if( ret != 0 )
  617. return( ret );
  618. p += output_len;
  619. /* Write legacy_compression_methods
  620. *
  621. * For every TLS 1.3 ClientHello, this vector MUST contain exactly
  622. * one byte set to zero, which corresponds to the 'null' compression
  623. * method in prior versions of TLS.
  624. */
  625. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  626. *p++ = 1;
  627. *p++ = MBEDTLS_SSL_COMPRESS_NULL;
  628. /* Write extensions */
  629. /* Keeping track of the included extensions */
  630. ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
  631. /* First write extensions, then the total length */
  632. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  633. extensions_len_ptr = p;
  634. p += 2;
  635. /* Write supported_versions extension
  636. *
  637. * Supported Versions Extension is mandatory with TLS 1.3.
  638. */
  639. ret = ssl_tls13_write_supported_versions_ext( ssl, p, end, &output_len );
  640. if( ret != 0 )
  641. return( ret );
  642. p += output_len;
  643. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  644. /* Write supported_groups extension
  645. *
  646. * It is REQUIRED for ECDHE cipher_suites.
  647. */
  648. ret = ssl_tls13_write_supported_groups_ext( ssl, p, end, &output_len );
  649. if( ret != 0 )
  650. return( ret );
  651. p += output_len;
  652. /* Write key_share extension
  653. *
  654. * We need to send the key shares under three conditions:
  655. * 1) A certificate-based ciphersuite is being offered. In this case
  656. * supported_groups and supported_signature extensions have been
  657. * successfully added.
  658. * 2) A PSK-based ciphersuite with ECDHE is offered. In this case the
  659. * psk_key_exchange_modes has been added as the last extension.
  660. * 3) Or, in case all ciphers are supported ( which includes #1 and #2
  661. * from above )
  662. */
  663. ret = ssl_tls13_write_key_share_ext( ssl, p, end, &output_len );
  664. if( ret != 0 )
  665. return( ret );
  666. p += output_len;
  667. /* Write signature_algorithms extension
  668. *
  669. * It is REQUIRED for certificate authenticated cipher_suites.
  670. */
  671. ret = mbedtls_ssl_tls13_write_sig_alg_ext( ssl, p, end, &output_len );
  672. if( ret != 0 )
  673. return( ret );
  674. p += output_len;
  675. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  676. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  677. /* Write server name extension */
  678. ret = mbedtls_ssl_write_hostname_ext( ssl, p, end, &output_len );
  679. if( ret != 0 )
  680. return( ret );
  681. p += output_len;
  682. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  683. /* Add more extensions here */
  684. /* Write the length of the list of extensions. */
  685. extensions_len = p - extensions_len_ptr - 2;
  686. MBEDTLS_PUT_UINT16_BE( extensions_len, extensions_len_ptr, 0 );
  687. MBEDTLS_SSL_DEBUG_MSG( 3, ( "client hello, total extension length: %" MBEDTLS_PRINTF_SIZET ,
  688. extensions_len ) );
  689. MBEDTLS_SSL_DEBUG_BUF( 3, "client hello extensions", extensions_len_ptr, extensions_len );
  690. *olen = p - buf;
  691. return( 0 );
  692. }
  693. static int ssl_tls13_finalize_client_hello( mbedtls_ssl_context *ssl )
  694. {
  695. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_HELLO );
  696. return( 0 );
  697. }
  698. static int ssl_tls13_prepare_client_hello( mbedtls_ssl_context *ssl )
  699. {
  700. int ret;
  701. if( ssl->conf->f_rng == NULL )
  702. {
  703. MBEDTLS_SSL_DEBUG_MSG( 1, ( "no RNG provided" ) );
  704. return( MBEDTLS_ERR_SSL_NO_RNG );
  705. }
  706. if( ( ret = ssl->conf->f_rng( ssl->conf->p_rng,
  707. ssl->handshake->randbytes,
  708. MBEDTLS_CLIENT_HELLO_RANDOM_LEN ) ) != 0 )
  709. {
  710. MBEDTLS_SSL_DEBUG_RET( 1, "f_rng", ret );
  711. return( ret );
  712. }
  713. return( 0 );
  714. }
  715. /*
  716. * Write ClientHello handshake message.
  717. * Handler for MBEDTLS_SSL_CLIENT_HELLO
  718. */
  719. static int ssl_tls13_write_client_hello( mbedtls_ssl_context *ssl )
  720. {
  721. int ret = 0;
  722. unsigned char *buf;
  723. size_t buf_len, msg_len;
  724. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write client hello" ) );
  725. MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_client_hello( ssl ) );
  726. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg(
  727. ssl, MBEDTLS_SSL_HS_CLIENT_HELLO,
  728. &buf, &buf_len ) );
  729. MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_client_hello_body( ssl, buf,
  730. buf + buf_len,
  731. &msg_len ) );
  732. mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl,
  733. MBEDTLS_SSL_HS_CLIENT_HELLO,
  734. msg_len );
  735. ssl->handshake->update_checksum( ssl, buf, msg_len );
  736. MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_client_hello( ssl ) );
  737. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
  738. buf_len,
  739. msg_len ) );
  740. cleanup:
  741. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write client hello" ) );
  742. return ret;
  743. }
  744. /*
  745. * Functions for parsing and processing Server Hello
  746. */
  747. /* Returns a negative value on failure, and otherwise
  748. * - SSL_SERVER_HELLO_COORDINATE_HELLO or
  749. * - SSL_SERVER_HELLO_COORDINATE_HRR
  750. * to indicate which message is expected and to be parsed next. */
  751. #define SSL_SERVER_HELLO_COORDINATE_HELLO 0
  752. #define SSL_SERVER_HELLO_COORDINATE_HRR 1
  753. static int ssl_server_hello_is_hrr( mbedtls_ssl_context *ssl,
  754. const unsigned char *buf,
  755. const unsigned char *end )
  756. {
  757. static const unsigned char magic_hrr_string[MBEDTLS_SERVER_HELLO_RANDOM_LEN] =
  758. { 0xCF, 0x21, 0xAD, 0x74, 0xE5, 0x9A, 0x61, 0x11,
  759. 0xBE, 0x1D, 0x8C, 0x02, 0x1E, 0x65, 0xB8, 0x91,
  760. 0xC2, 0xA2, 0x11, 0x16, 0x7A, 0xBB, 0x8C, 0x5E,
  761. 0x07, 0x9E, 0x09, 0xE2, 0xC8, 0xA8, 0x33 ,0x9C };
  762. /* Check whether this message is a HelloRetryRequest ( HRR ) message.
  763. *
  764. * Server Hello and HRR are only distinguished by Random set to the
  765. * special value of the SHA-256 of "HelloRetryRequest".
  766. *
  767. * struct {
  768. * ProtocolVersion legacy_version = 0x0303;
  769. * Random random;
  770. * opaque legacy_session_id_echo<0..32>;
  771. * CipherSuite cipher_suite;
  772. * uint8 legacy_compression_method = 0;
  773. * Extension extensions<6..2^16-1>;
  774. * } ServerHello;
  775. *
  776. */
  777. MBEDTLS_SSL_CHK_BUF_READ_PTR( buf, end, 2 + sizeof( magic_hrr_string ) );
  778. if( memcmp( buf + 2, magic_hrr_string, sizeof( magic_hrr_string ) ) == 0 )
  779. {
  780. return( SSL_SERVER_HELLO_COORDINATE_HRR );
  781. }
  782. return( SSL_SERVER_HELLO_COORDINATE_HELLO );
  783. }
  784. /* Fetch and preprocess
  785. * Returns a negative value on failure, and otherwise
  786. * - SSL_SERVER_HELLO_COORDINATE_HELLO or
  787. * - SSL_SERVER_HELLO_COORDINATE_HRR
  788. */
  789. static int ssl_tls13_server_hello_coordinate( mbedtls_ssl_context *ssl,
  790. unsigned char **buf,
  791. size_t *buf_len )
  792. {
  793. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  794. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_read_record( ssl, 0 ) );
  795. if( ( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ) ||
  796. ( ssl->in_msg[0] != MBEDTLS_SSL_HS_SERVER_HELLO ) )
  797. {
  798. MBEDTLS_SSL_DEBUG_MSG( 1, ( "unexpected message" ) );
  799. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
  800. MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  801. return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  802. }
  803. *buf = ssl->in_msg + 4;
  804. *buf_len = ssl->in_hslen - 4;
  805. ret = ssl_server_hello_is_hrr( ssl, *buf, *buf + *buf_len );
  806. switch( ret )
  807. {
  808. case SSL_SERVER_HELLO_COORDINATE_HELLO:
  809. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received ServerHello message" ) );
  810. break;
  811. case SSL_SERVER_HELLO_COORDINATE_HRR:
  812. MBEDTLS_SSL_DEBUG_MSG( 2, ( "received HelloRetryRequest message" ) );
  813. break;
  814. }
  815. cleanup:
  816. return( ret );
  817. }
  818. static int ssl_tls13_check_server_hello_session_id_echo( mbedtls_ssl_context *ssl,
  819. const unsigned char **buf,
  820. const unsigned char *end )
  821. {
  822. const unsigned char *p = *buf;
  823. size_t legacy_session_id_echo_len;
  824. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
  825. legacy_session_id_echo_len = *p++ ;
  826. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, legacy_session_id_echo_len );
  827. /* legacy_session_id_echo */
  828. if( ssl->session_negotiate->id_len != legacy_session_id_echo_len ||
  829. memcmp( ssl->session_negotiate->id, p , legacy_session_id_echo_len ) != 0 )
  830. {
  831. MBEDTLS_SSL_DEBUG_BUF( 3, "Expected Session ID",
  832. ssl->session_negotiate->id,
  833. ssl->session_negotiate->id_len );
  834. MBEDTLS_SSL_DEBUG_BUF( 3, "Received Session ID", p,
  835. legacy_session_id_echo_len );
  836. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  837. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
  838. return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  839. }
  840. p += legacy_session_id_echo_len;
  841. *buf = p;
  842. MBEDTLS_SSL_DEBUG_BUF( 3, "Session ID", ssl->session_negotiate->id,
  843. ssl->session_negotiate->id_len );
  844. return( 0 );
  845. }
  846. static int ssl_tls13_cipher_suite_is_offered( mbedtls_ssl_context *ssl,
  847. int cipher_suite )
  848. {
  849. const int *ciphersuite_list = ssl->conf->ciphersuite_list;
  850. /* Check whether we have offered this ciphersuite */
  851. for ( size_t i = 0; ciphersuite_list[i] != 0; i++ )
  852. {
  853. if( ciphersuite_list[i] == cipher_suite )
  854. {
  855. return( 1 );
  856. }
  857. }
  858. return( 0 );
  859. }
  860. /* Parse ServerHello message and configure context
  861. *
  862. * struct {
  863. * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
  864. * Random random;
  865. * opaque legacy_session_id_echo<0..32>;
  866. * CipherSuite cipher_suite;
  867. * uint8 legacy_compression_method = 0;
  868. * Extension extensions<6..2^16-1>;
  869. * } ServerHello;
  870. */
  871. static int ssl_tls13_parse_server_hello( mbedtls_ssl_context *ssl,
  872. const unsigned char *buf,
  873. const unsigned char *end )
  874. {
  875. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  876. const unsigned char *p = buf;
  877. size_t extensions_len;
  878. const unsigned char *extensions_end;
  879. uint16_t cipher_suite;
  880. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  881. /*
  882. * Check there is space for minimal fields
  883. *
  884. * - legacy_version ( 2 bytes)
  885. * - random (MBEDTLS_SERVER_HELLO_RANDOM_LEN bytes)
  886. * - legacy_session_id_echo ( 1 byte ), minimum size
  887. * - cipher_suite ( 2 bytes)
  888. * - legacy_compression_method ( 1 byte )
  889. */
  890. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, MBEDTLS_SERVER_HELLO_RANDOM_LEN + 6 );
  891. MBEDTLS_SSL_DEBUG_BUF( 4, "server hello", p, end - p );
  892. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, version", p, 2 );
  893. /* ...
  894. * ProtocolVersion legacy_version = 0x0303; // TLS 1.2
  895. * ...
  896. * with ProtocolVersion defined as:
  897. * uint16 ProtocolVersion;
  898. */
  899. if( !( p[0] == MBEDTLS_SSL_MAJOR_VERSION_3 &&
  900. p[1] == MBEDTLS_SSL_MINOR_VERSION_3 ) )
  901. {
  902. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unsupported version of TLS." ) );
  903. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION,
  904. MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
  905. return( MBEDTLS_ERR_SSL_BAD_PROTOCOL_VERSION );
  906. }
  907. p += 2;
  908. /* ...
  909. * Random random;
  910. * ...
  911. * with Random defined as:
  912. * opaque Random[MBEDTLS_SERVER_HELLO_RANDOM_LEN];
  913. */
  914. memcpy( &ssl->handshake->randbytes[MBEDTLS_CLIENT_HELLO_RANDOM_LEN], p,
  915. MBEDTLS_SERVER_HELLO_RANDOM_LEN );
  916. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello, random bytes",
  917. p, MBEDTLS_SERVER_HELLO_RANDOM_LEN );
  918. p += MBEDTLS_SERVER_HELLO_RANDOM_LEN;
  919. /* ...
  920. * opaque legacy_session_id_echo<0..32>;
  921. * ...
  922. */
  923. if( ssl_tls13_check_server_hello_session_id_echo( ssl, &p, end ) != 0 )
  924. {
  925. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  926. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  927. return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  928. }
  929. /* ...
  930. * CipherSuite cipher_suite;
  931. * ...
  932. * with CipherSuite defined as:
  933. * uint8 CipherSuite[2];
  934. */
  935. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
  936. cipher_suite = MBEDTLS_GET_UINT16_BE( p, 0 );
  937. p += 2;
  938. /*
  939. * Check whether this ciphersuite is supported and offered.
  940. * Via the force_ciphersuite version we may have instructed the client
  941. * to use a different ciphersuite.
  942. */
  943. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( cipher_suite );
  944. if( ciphersuite_info == NULL ||
  945. ssl_tls13_cipher_suite_is_offered( ssl, cipher_suite ) == 0 )
  946. {
  947. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite(%04x) not found or not offered",
  948. cipher_suite ) );
  949. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  950. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  951. return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  952. }
  953. /* Configure ciphersuites */
  954. mbedtls_ssl_optimize_checksum( ssl, ciphersuite_info );
  955. ssl->handshake->ciphersuite_info = ciphersuite_info;
  956. ssl->session_negotiate->ciphersuite = cipher_suite;
  957. MBEDTLS_SSL_DEBUG_MSG( 3, ( "server hello, chosen ciphersuite: ( %04x ) - %s",
  958. cipher_suite, ciphersuite_info->name ) );
  959. #if defined(MBEDTLS_HAVE_TIME)
  960. ssl->session_negotiate->start = time( NULL );
  961. #endif /* MBEDTLS_HAVE_TIME */
  962. /* ...
  963. * uint8 legacy_compression_method = 0;
  964. * ...
  965. */
  966. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 1 );
  967. if( p[0] != 0 )
  968. {
  969. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad legacy compression method" ) );
  970. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
  971. MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  972. return( MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER );
  973. }
  974. p++;
  975. /* ...
  976. * Extension extensions<6..2^16-1>;
  977. * ...
  978. * struct {
  979. * ExtensionType extension_type; (2 bytes)
  980. * opaque extension_data<0..2^16-1>;
  981. * } Extension;
  982. */
  983. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
  984. extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
  985. p += 2;
  986. /* Check extensions do not go beyond the buffer of data. */
  987. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
  988. extensions_end = p + extensions_len;
  989. MBEDTLS_SSL_DEBUG_BUF( 3, "server hello extensions", p, extensions_len );
  990. while( p < extensions_end )
  991. {
  992. unsigned int extension_type;
  993. size_t extension_data_len;
  994. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
  995. extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
  996. extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
  997. p += 4;
  998. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
  999. switch( extension_type )
  1000. {
  1001. case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
  1002. MBEDTLS_SSL_DEBUG_MSG( 3,
  1003. ( "found supported_versions extension" ) );
  1004. ret = ssl_tls13_parse_supported_versions_ext( ssl,
  1005. p,
  1006. p + extension_data_len );
  1007. if( ret != 0 )
  1008. return( ret );
  1009. break;
  1010. case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
  1011. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found pre_shared_key extension." ) );
  1012. MBEDTLS_SSL_DEBUG_MSG( 3, ( "pre_shared_key:Not supported yet" ) );
  1013. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1014. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
  1015. MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
  1016. return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
  1017. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1018. case MBEDTLS_TLS_EXT_KEY_SHARE:
  1019. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found key_shares extension" ) );
  1020. if( ( ret = ssl_tls13_parse_key_share_ext( ssl,
  1021. p, p + extension_data_len ) ) != 0 )
  1022. {
  1023. MBEDTLS_SSL_DEBUG_RET( 1,
  1024. "ssl_tls13_parse_key_share_ext",
  1025. ret );
  1026. return( ret );
  1027. }
  1028. break;
  1029. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1030. default:
  1031. MBEDTLS_SSL_DEBUG_MSG(
  1032. 3,
  1033. ( "unknown extension found: %u ( ignoring )",
  1034. extension_type ) );
  1035. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1036. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
  1037. MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
  1038. return( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
  1039. }
  1040. p += extension_data_len;
  1041. }
  1042. return( 0 );
  1043. }
  1044. static int ssl_tls13_finalize_server_hello( mbedtls_ssl_context *ssl )
  1045. {
  1046. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1047. mbedtls_ssl_key_set traffic_keys;
  1048. mbedtls_ssl_transform *transform_handshake = NULL;
  1049. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  1050. /* Determine the key exchange mode:
  1051. * 1) If both the pre_shared_key and key_share extensions were received
  1052. * then the key exchange mode is PSK with EPHEMERAL.
  1053. * 2) If only the pre_shared_key extension was received then the key
  1054. * exchange mode is PSK-only.
  1055. * 3) If only the key_share extension was received then the key
  1056. * exchange mode is EPHEMERAL-only.
  1057. */
  1058. switch( handshake->extensions_present &
  1059. ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ) )
  1060. {
  1061. /* Only the pre_shared_key extension was received */
  1062. case MBEDTLS_SSL_EXT_PRE_SHARED_KEY:
  1063. handshake->tls1_3_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK;
  1064. break;
  1065. /* Only the key_share extension was received */
  1066. case MBEDTLS_SSL_EXT_KEY_SHARE:
  1067. handshake->tls1_3_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL;
  1068. break;
  1069. /* Both the pre_shared_key and key_share extensions were received */
  1070. case ( MBEDTLS_SSL_EXT_PRE_SHARED_KEY | MBEDTLS_SSL_EXT_KEY_SHARE ):
  1071. handshake->tls1_3_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
  1072. break;
  1073. /* Neither pre_shared_key nor key_share extension was received */
  1074. default:
  1075. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Unknown key exchange." ) );
  1076. ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1077. goto cleanup;
  1078. }
  1079. /* Start the TLS 1.3 key schedule: Set the PSK and derive early secret.
  1080. *
  1081. * TODO: We don't have to do this in case we offered 0-RTT and the
  1082. * server accepted it. In this case, we could skip generating
  1083. * the early secret. */
  1084. ret = mbedtls_ssl_tls1_3_key_schedule_stage_early( ssl );
  1085. if( ret != 0 )
  1086. {
  1087. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_key_schedule_stage_early_data",
  1088. ret );
  1089. goto cleanup;
  1090. }
  1091. /* Compute handshake secret */
  1092. ret = mbedtls_ssl_tls13_key_schedule_stage_handshake( ssl );
  1093. if( ret != 0 )
  1094. {
  1095. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_derive_master_secret", ret );
  1096. goto cleanup;
  1097. }
  1098. /* Next evolution in key schedule: Establish handshake secret and
  1099. * key material. */
  1100. ret = mbedtls_ssl_tls13_generate_handshake_keys( ssl, &traffic_keys );
  1101. if( ret != 0 )
  1102. {
  1103. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_generate_handshake_keys",
  1104. ret );
  1105. goto cleanup;
  1106. }
  1107. transform_handshake = mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
  1108. if( transform_handshake == NULL )
  1109. {
  1110. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  1111. goto cleanup;
  1112. }
  1113. ret = mbedtls_ssl_tls13_populate_transform( transform_handshake,
  1114. ssl->conf->endpoint,
  1115. ssl->session_negotiate->ciphersuite,
  1116. &traffic_keys,
  1117. ssl );
  1118. if( ret != 0 )
  1119. {
  1120. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
  1121. goto cleanup;
  1122. }
  1123. handshake->transform_handshake = transform_handshake;
  1124. mbedtls_ssl_set_inbound_transform( ssl, transform_handshake );
  1125. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to handshake keys for inbound traffic" ) );
  1126. ssl->session_in = ssl->session_negotiate;
  1127. /*
  1128. * State machine update
  1129. */
  1130. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_ENCRYPTED_EXTENSIONS );
  1131. cleanup:
  1132. mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
  1133. if( ret != 0 )
  1134. {
  1135. mbedtls_free( transform_handshake );
  1136. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1137. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  1138. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  1139. }
  1140. return( ret );
  1141. }
  1142. /*
  1143. * Wait and parse ServerHello handshake message.
  1144. * Handler for MBEDTLS_SSL_SERVER_HELLO
  1145. */
  1146. static int ssl_tls1_3_process_server_hello( mbedtls_ssl_context *ssl )
  1147. {
  1148. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  1149. unsigned char *buf;
  1150. size_t buf_len;
  1151. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> %s", __func__ ) );
  1152. /* Coordination step
  1153. * - Fetch record
  1154. * - Make sure it's either a ServerHello or a HRR.
  1155. * - Switch processing routine in case of HRR
  1156. */
  1157. ssl->major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
  1158. ssl->handshake->extensions_present = MBEDTLS_SSL_EXT_NONE;
  1159. ret = ssl_tls13_server_hello_coordinate( ssl, &buf, &buf_len );
  1160. /* Parsing step
  1161. * We know what message to expect by now and call
  1162. * the respective parsing function.
  1163. */
  1164. if( ret == SSL_SERVER_HELLO_COORDINATE_HELLO )
  1165. {
  1166. MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_server_hello( ssl, buf,
  1167. buf + buf_len ) );
  1168. mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl,
  1169. MBEDTLS_SSL_HS_SERVER_HELLO,
  1170. buf, buf_len );
  1171. MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_server_hello( ssl ) );
  1172. }
  1173. else if( ret == SSL_SERVER_HELLO_COORDINATE_HRR )
  1174. {
  1175. MBEDTLS_SSL_DEBUG_MSG( 1, ( "HRR not supported" ) );
  1176. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ,
  1177. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  1178. ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  1179. }
  1180. cleanup:
  1181. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= %s", __func__ ) );
  1182. return( ret );
  1183. }
  1184. /*
  1185. *
  1186. * EncryptedExtensions message
  1187. *
  1188. * The EncryptedExtensions message contains any extensions which
  1189. * should be protected, i.e., any which are not needed to establish
  1190. * the cryptographic context.
  1191. */
  1192. /*
  1193. * Overview
  1194. */
  1195. /* Main entry point; orchestrates the other functions */
  1196. static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl );
  1197. static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
  1198. const unsigned char *buf,
  1199. const unsigned char *end );
  1200. static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl );
  1201. /*
  1202. * Handler for MBEDTLS_SSL_ENCRYPTED_EXTENSIONS
  1203. */
  1204. static int ssl_tls13_process_encrypted_extensions( mbedtls_ssl_context *ssl )
  1205. {
  1206. int ret;
  1207. unsigned char *buf;
  1208. size_t buf_len;
  1209. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse encrypted extensions" ) );
  1210. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls1_3_fetch_handshake_msg( ssl,
  1211. MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS,
  1212. &buf, &buf_len ) );
  1213. /* Process the message contents */
  1214. MBEDTLS_SSL_PROC_CHK(
  1215. ssl_tls13_parse_encrypted_extensions( ssl, buf, buf + buf_len ) );
  1216. mbedtls_ssl_tls1_3_add_hs_msg_to_checksum(
  1217. ssl, MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS, buf, buf_len );
  1218. MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_encrypted_extensions( ssl ) );
  1219. cleanup:
  1220. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse encrypted extensions" ) );
  1221. return( ret );
  1222. }
  1223. /* Parse EncryptedExtensions message
  1224. * struct {
  1225. * Extension extensions<0..2^16-1>;
  1226. * } EncryptedExtensions;
  1227. */
  1228. static int ssl_tls13_parse_encrypted_extensions( mbedtls_ssl_context *ssl,
  1229. const unsigned char *buf,
  1230. const unsigned char *end )
  1231. {
  1232. int ret = 0;
  1233. size_t extensions_len;
  1234. const unsigned char *p = buf;
  1235. const unsigned char *extensions_end;
  1236. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
  1237. extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
  1238. p += 2;
  1239. MBEDTLS_SSL_DEBUG_BUF( 3, "encrypted extensions", p, extensions_len );
  1240. extensions_end = p + extensions_len;
  1241. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, extensions_len );
  1242. while( p < extensions_end )
  1243. {
  1244. unsigned int extension_type;
  1245. size_t extension_data_len;
  1246. /*
  1247. * struct {
  1248. * ExtensionType extension_type; (2 bytes)
  1249. * opaque extension_data<0..2^16-1>;
  1250. * } Extension;
  1251. */
  1252. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, 4 );
  1253. extension_type = MBEDTLS_GET_UINT16_BE( p, 0 );
  1254. extension_data_len = MBEDTLS_GET_UINT16_BE( p, 2 );
  1255. p += 4;
  1256. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, extensions_end, extension_data_len );
  1257. /* The client MUST check EncryptedExtensions for the
  1258. * presence of any forbidden extensions and if any are found MUST abort
  1259. * the handshake with an "unsupported_extension" alert.
  1260. */
  1261. switch( extension_type )
  1262. {
  1263. case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
  1264. MBEDTLS_SSL_DEBUG_MSG( 3, ( "found extensions supported groups" ) );
  1265. break;
  1266. default:
  1267. MBEDTLS_SSL_DEBUG_MSG(
  1268. 3, ( "unsupported extension found: %u ", extension_type) );
  1269. MBEDTLS_SSL_PEND_FATAL_ALERT(
  1270. MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT,
  1271. MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
  1272. return ( MBEDTLS_ERR_SSL_UNSUPPORTED_EXTENSION );
  1273. }
  1274. p += extension_data_len;
  1275. }
  1276. /* Check that we consumed all the message. */
  1277. if( p != end )
  1278. {
  1279. MBEDTLS_SSL_DEBUG_MSG( 1, ( "EncryptedExtension lengths misaligned" ) );
  1280. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  1281. MBEDTLS_ERR_SSL_DECODE_ERROR );
  1282. return( MBEDTLS_ERR_SSL_DECODE_ERROR );
  1283. }
  1284. return( ret );
  1285. }
  1286. static int ssl_tls13_postprocess_encrypted_extensions( mbedtls_ssl_context *ssl )
  1287. {
  1288. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1289. if( mbedtls_ssl_tls1_3_some_psk_enabled( ssl ) )
  1290. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
  1291. else
  1292. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_REQUEST );
  1293. #else
  1294. ((void) ssl);
  1295. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
  1296. #endif
  1297. return( 0 );
  1298. }
  1299. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1300. /*
  1301. * Handler for MBEDTLS_SSL_CERTIFICATE_REQUEST
  1302. */
  1303. static int ssl_tls13_process_certificate_request( mbedtls_ssl_context *ssl )
  1304. {
  1305. int ret = mbedtls_ssl_read_record( ssl, 0 );
  1306. if( ret != 0 )
  1307. {
  1308. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  1309. return( ret );
  1310. }
  1311. if( ( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE ) &&
  1312. ( ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE_REQUEST ) )
  1313. {
  1314. MBEDTLS_SSL_DEBUG_MSG( 1, ( "CertificateRequest not supported" ) );
  1315. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  1316. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  1317. return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  1318. }
  1319. ssl->keep_current_message = 1;
  1320. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_CERTIFICATE );
  1321. return( 0 );
  1322. }
  1323. /*
  1324. * Handler for MBEDTLS_SSL_SERVER_CERTIFICATE
  1325. */
  1326. static int ssl_tls1_3_process_server_certificate( mbedtls_ssl_context *ssl )
  1327. {
  1328. int ret;
  1329. ret = mbedtls_ssl_tls13_process_certificate( ssl );
  1330. if( ret != 0 )
  1331. return( ret );
  1332. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CERTIFICATE_VERIFY );
  1333. return( 0 );
  1334. }
  1335. /*
  1336. * Handler for MBEDTLS_SSL_CERTIFICATE_VERIFY
  1337. */
  1338. static int ssl_tls1_3_process_certificate_verify( mbedtls_ssl_context *ssl )
  1339. {
  1340. int ret;
  1341. ret = mbedtls_ssl_tls13_process_certificate_verify( ssl );
  1342. if( ret != 0 )
  1343. return( ret );
  1344. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_SERVER_FINISHED );
  1345. return( 0 );
  1346. }
  1347. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1348. /*
  1349. * Handler for MBEDTLS_SSL_SERVER_FINISHED
  1350. */
  1351. static int ssl_tls1_3_process_server_finished( mbedtls_ssl_context *ssl )
  1352. {
  1353. int ret;
  1354. ret = mbedtls_ssl_tls13_process_finished_message( ssl );
  1355. if( ret != 0 )
  1356. return( ret );
  1357. mbedtls_ssl_set_outbound_transform( ssl, ssl->handshake->transform_handshake );
  1358. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_CLIENT_FINISHED );
  1359. return( 0 );
  1360. }
  1361. /*
  1362. * Handler for MBEDTLS_SSL_CLIENT_FINISHED
  1363. */
  1364. static int ssl_tls13_write_client_finished( mbedtls_ssl_context *ssl )
  1365. {
  1366. int ret;
  1367. ret = mbedtls_ssl_tls13_write_finished_message( ssl );
  1368. if( ret != 0 )
  1369. return( ret );
  1370. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_FLUSH_BUFFERS );
  1371. return( 0 );
  1372. }
  1373. /*
  1374. * Handler for MBEDTLS_SSL_FLUSH_BUFFERS
  1375. */
  1376. static int ssl_tls1_3_flush_buffers( mbedtls_ssl_context *ssl )
  1377. {
  1378. MBEDTLS_SSL_DEBUG_MSG( 2, ( "handshake: done" ) );
  1379. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_WRAPUP );
  1380. return( 0 );
  1381. }
  1382. /*
  1383. * Handler for MBEDTLS_SSL_HANDSHAKE_WRAPUP
  1384. */
  1385. static int ssl_tls1_3_handshake_wrapup( mbedtls_ssl_context *ssl )
  1386. {
  1387. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for inbound traffic" ) );
  1388. mbedtls_ssl_set_inbound_transform ( ssl, ssl->transform_application );
  1389. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Switch to application keys for outbound traffic" ) );
  1390. mbedtls_ssl_set_outbound_transform( ssl, ssl->transform_application );
  1391. mbedtls_ssl_tls13_handshake_wrapup( ssl );
  1392. mbedtls_ssl_handshake_set_state( ssl, MBEDTLS_SSL_HANDSHAKE_OVER );
  1393. return( 0 );
  1394. }
  1395. int mbedtls_ssl_tls13_handshake_client_step( mbedtls_ssl_context *ssl )
  1396. {
  1397. int ret = 0;
  1398. MBEDTLS_SSL_DEBUG_MSG( 2, ( "tls1_3 client state: %d", ssl->state ) );
  1399. switch( ssl->state )
  1400. {
  1401. /*
  1402. * ssl->state is initialized as HELLO_REQUEST. It is the same
  1403. * as CLIENT_HELLO state.
  1404. */
  1405. case MBEDTLS_SSL_HELLO_REQUEST:
  1406. case MBEDTLS_SSL_CLIENT_HELLO:
  1407. ret = ssl_tls13_write_client_hello( ssl );
  1408. break;
  1409. case MBEDTLS_SSL_SERVER_HELLO:
  1410. ret = ssl_tls1_3_process_server_hello( ssl );
  1411. break;
  1412. case MBEDTLS_SSL_ENCRYPTED_EXTENSIONS:
  1413. ret = ssl_tls13_process_encrypted_extensions( ssl );
  1414. break;
  1415. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1416. case MBEDTLS_SSL_CERTIFICATE_REQUEST:
  1417. ret = ssl_tls13_process_certificate_request( ssl );
  1418. break;
  1419. case MBEDTLS_SSL_SERVER_CERTIFICATE:
  1420. ret = ssl_tls1_3_process_server_certificate( ssl );
  1421. break;
  1422. case MBEDTLS_SSL_CERTIFICATE_VERIFY:
  1423. ret = ssl_tls1_3_process_certificate_verify( ssl );
  1424. break;
  1425. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1426. case MBEDTLS_SSL_SERVER_FINISHED:
  1427. ret = ssl_tls1_3_process_server_finished( ssl );
  1428. break;
  1429. case MBEDTLS_SSL_CLIENT_FINISHED:
  1430. ret = ssl_tls13_write_client_finished( ssl );
  1431. break;
  1432. case MBEDTLS_SSL_FLUSH_BUFFERS:
  1433. ret = ssl_tls1_3_flush_buffers( ssl );
  1434. break;
  1435. case MBEDTLS_SSL_HANDSHAKE_WRAPUP:
  1436. ret = ssl_tls1_3_handshake_wrapup( ssl );
  1437. break;
  1438. default:
  1439. MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid state %d", ssl->state ) );
  1440. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  1441. }
  1442. return( ret );
  1443. }
  1444. #endif /* MBEDTLS_SSL_CLI_C */
  1445. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */