ssl_tls13_keys.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232
  1. /*
  2. * TLS 1.3 key schedule
  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. #include "common.h"
  20. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  21. #include <stdint.h>
  22. #include <string.h>
  23. #include "mbedtls/hkdf.h"
  24. #include "mbedtls/debug.h"
  25. #include "mbedtls/error.h"
  26. #include "ssl_misc.h"
  27. #include "ssl_tls13_keys.h"
  28. #define MBEDTLS_SSL_TLS1_3_LABEL( name, string ) \
  29. .name = string,
  30. struct mbedtls_ssl_tls1_3_labels_struct const mbedtls_ssl_tls1_3_labels =
  31. {
  32. /* This seems to work in C, despite the string literal being one
  33. * character too long due to the 0-termination. */
  34. MBEDTLS_SSL_TLS1_3_LABEL_LIST
  35. };
  36. #undef MBEDTLS_SSL_TLS1_3_LABEL
  37. /*
  38. * This function creates a HkdfLabel structure used in the TLS 1.3 key schedule.
  39. *
  40. * The HkdfLabel is specified in RFC 8446 as follows:
  41. *
  42. * struct HkdfLabel {
  43. * uint16 length; // Length of expanded key material
  44. * opaque label<7..255>; // Always prefixed by "tls13 "
  45. * opaque context<0..255>; // Usually a communication transcript hash
  46. * };
  47. *
  48. * Parameters:
  49. * - desired_length: Length of expanded key material
  50. * Even though the standard allows expansion to up to
  51. * 2**16 Bytes, TLS 1.3 never uses expansion to more than
  52. * 255 Bytes, so we require `desired_length` to be at most
  53. * 255. This allows us to save a few Bytes of code by
  54. * hardcoding the writing of the high bytes.
  55. * - (label, llen): label + label length, without "tls13 " prefix
  56. * The label length MUST be less than or equal to
  57. * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN
  58. * It is the caller's responsibility to ensure this.
  59. * All (label, label length) pairs used in TLS 1.3
  60. * can be obtained via MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN().
  61. * - (ctx, clen): context + context length
  62. * The context length MUST be less than or equal to
  63. * MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN
  64. * It is the caller's responsibility to ensure this.
  65. * - dst: Target buffer for HkdfLabel structure,
  66. * This MUST be a writable buffer of size
  67. * at least SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN Bytes.
  68. * - dlen: Pointer at which to store the actual length of
  69. * the HkdfLabel structure on success.
  70. */
  71. static const char tls1_3_label_prefix[6] = "tls13 ";
  72. #define SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( label_len, context_len ) \
  73. ( 2 /* expansion length */ \
  74. + 1 /* label length */ \
  75. + label_len \
  76. + 1 /* context length */ \
  77. + context_len )
  78. #define SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN \
  79. SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( \
  80. sizeof(tls1_3_label_prefix) + \
  81. MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN, \
  82. MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
  83. static void ssl_tls1_3_hkdf_encode_label(
  84. size_t desired_length,
  85. const unsigned char *label, size_t llen,
  86. const unsigned char *ctx, size_t clen,
  87. unsigned char *dst, size_t *dlen )
  88. {
  89. size_t total_label_len =
  90. sizeof(tls1_3_label_prefix) + llen;
  91. size_t total_hkdf_lbl_len =
  92. SSL_TLS1_3_KEY_SCHEDULE_HKDF_LABEL_LEN( total_label_len, clen );
  93. unsigned char *p = dst;
  94. /* Add the size of the expanded key material.
  95. * We're hardcoding the high byte to 0 here assuming that we never use
  96. * TLS 1.3 HKDF key expansion to more than 255 Bytes. */
  97. #if MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN > 255
  98. #error "The implementation of ssl_tls1_3_hkdf_encode_label() is not fit for the \
  99. value of MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN"
  100. #endif
  101. *p++ = 0;
  102. *p++ = MBEDTLS_BYTE_0( desired_length );
  103. /* Add label incl. prefix */
  104. *p++ = MBEDTLS_BYTE_0( total_label_len );
  105. memcpy( p, tls1_3_label_prefix, sizeof(tls1_3_label_prefix) );
  106. p += sizeof(tls1_3_label_prefix);
  107. memcpy( p, label, llen );
  108. p += llen;
  109. /* Add context value */
  110. *p++ = MBEDTLS_BYTE_0( clen );
  111. if( clen != 0 )
  112. memcpy( p, ctx, clen );
  113. /* Return total length to the caller. */
  114. *dlen = total_hkdf_lbl_len;
  115. }
  116. int mbedtls_ssl_tls1_3_hkdf_expand_label(
  117. mbedtls_md_type_t hash_alg,
  118. const unsigned char *secret, size_t slen,
  119. const unsigned char *label, size_t llen,
  120. const unsigned char *ctx, size_t clen,
  121. unsigned char *buf, size_t blen )
  122. {
  123. const mbedtls_md_info_t *md;
  124. unsigned char hkdf_label[ SSL_TLS1_3_KEY_SCHEDULE_MAX_HKDF_LABEL_LEN ];
  125. size_t hkdf_label_len;
  126. if( llen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_LABEL_LEN )
  127. {
  128. /* Should never happen since this is an internal
  129. * function, and we know statically which labels
  130. * are allowed. */
  131. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  132. }
  133. if( clen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_CONTEXT_LEN )
  134. {
  135. /* Should not happen, as above. */
  136. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  137. }
  138. if( blen > MBEDTLS_SSL_TLS1_3_KEY_SCHEDULE_MAX_EXPANSION_LEN )
  139. {
  140. /* Should not happen, as above. */
  141. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  142. }
  143. md = mbedtls_md_info_from_type( hash_alg );
  144. if( md == NULL )
  145. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  146. ssl_tls1_3_hkdf_encode_label( blen,
  147. label, llen,
  148. ctx, clen,
  149. hkdf_label,
  150. &hkdf_label_len );
  151. return( mbedtls_hkdf_expand( md,
  152. secret, slen,
  153. hkdf_label, hkdf_label_len,
  154. buf, blen ) );
  155. }
  156. /*
  157. * The traffic keying material is generated from the following inputs:
  158. *
  159. * - One secret value per sender.
  160. * - A purpose value indicating the specific value being generated
  161. * - The desired lengths of key and IV.
  162. *
  163. * The expansion itself is based on HKDF:
  164. *
  165. * [sender]_write_key = HKDF-Expand-Label( Secret, "key", "", key_length )
  166. * [sender]_write_iv = HKDF-Expand-Label( Secret, "iv" , "", iv_length )
  167. *
  168. * [sender] denotes the sending side and the Secret value is provided
  169. * by the function caller. Note that we generate server and client side
  170. * keys in a single function call.
  171. */
  172. int mbedtls_ssl_tls1_3_make_traffic_keys(
  173. mbedtls_md_type_t hash_alg,
  174. const unsigned char *client_secret,
  175. const unsigned char *server_secret,
  176. size_t slen, size_t key_len, size_t iv_len,
  177. mbedtls_ssl_key_set *keys )
  178. {
  179. int ret = 0;
  180. ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
  181. client_secret, slen,
  182. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
  183. NULL, 0,
  184. keys->client_write_key, key_len );
  185. if( ret != 0 )
  186. return( ret );
  187. ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
  188. server_secret, slen,
  189. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( key ),
  190. NULL, 0,
  191. keys->server_write_key, key_len );
  192. if( ret != 0 )
  193. return( ret );
  194. ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
  195. client_secret, slen,
  196. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
  197. NULL, 0,
  198. keys->client_write_iv, iv_len );
  199. if( ret != 0 )
  200. return( ret );
  201. ret = mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
  202. server_secret, slen,
  203. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( iv ),
  204. NULL, 0,
  205. keys->server_write_iv, iv_len );
  206. if( ret != 0 )
  207. return( ret );
  208. keys->key_len = key_len;
  209. keys->iv_len = iv_len;
  210. return( 0 );
  211. }
  212. int mbedtls_ssl_tls1_3_derive_secret(
  213. mbedtls_md_type_t hash_alg,
  214. const unsigned char *secret, size_t slen,
  215. const unsigned char *label, size_t llen,
  216. const unsigned char *ctx, size_t clen,
  217. int ctx_hashed,
  218. unsigned char *dstbuf, size_t buflen )
  219. {
  220. int ret;
  221. unsigned char hashed_context[ MBEDTLS_MD_MAX_SIZE ];
  222. const mbedtls_md_info_t *md;
  223. md = mbedtls_md_info_from_type( hash_alg );
  224. if( md == NULL )
  225. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  226. if( ctx_hashed == MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED )
  227. {
  228. ret = mbedtls_md( md, ctx, clen, hashed_context );
  229. if( ret != 0 )
  230. return( ret );
  231. clen = mbedtls_md_get_size( md );
  232. }
  233. else
  234. {
  235. if( clen > sizeof(hashed_context) )
  236. {
  237. /* This should never happen since this function is internal
  238. * and the code sets `ctx_hashed` correctly.
  239. * Let's double-check nonetheless to not run at the risk
  240. * of getting a stack overflow. */
  241. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  242. }
  243. memcpy( hashed_context, ctx, clen );
  244. }
  245. return( mbedtls_ssl_tls1_3_hkdf_expand_label( hash_alg,
  246. secret, slen,
  247. label, llen,
  248. hashed_context, clen,
  249. dstbuf, buflen ) );
  250. }
  251. int mbedtls_ssl_tls1_3_evolve_secret(
  252. mbedtls_md_type_t hash_alg,
  253. const unsigned char *secret_old,
  254. const unsigned char *input, size_t input_len,
  255. unsigned char *secret_new )
  256. {
  257. int ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  258. size_t hlen, ilen;
  259. unsigned char tmp_secret[ MBEDTLS_MD_MAX_SIZE ] = { 0 };
  260. unsigned char tmp_input [ MBEDTLS_MD_MAX_SIZE ] = { 0 };
  261. const mbedtls_md_info_t *md;
  262. md = mbedtls_md_info_from_type( hash_alg );
  263. if( md == NULL )
  264. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  265. hlen = mbedtls_md_get_size( md );
  266. /* For non-initial runs, call Derive-Secret( ., "derived", "")
  267. * on the old secret. */
  268. if( secret_old != NULL )
  269. {
  270. ret = mbedtls_ssl_tls1_3_derive_secret(
  271. hash_alg,
  272. secret_old, hlen,
  273. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( derived ),
  274. NULL, 0, /* context */
  275. MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
  276. tmp_secret, hlen );
  277. if( ret != 0 )
  278. goto cleanup;
  279. }
  280. if( input != NULL )
  281. {
  282. memcpy( tmp_input, input, input_len );
  283. ilen = input_len;
  284. }
  285. else
  286. {
  287. ilen = hlen;
  288. }
  289. /* HKDF-Extract takes a salt and input key material.
  290. * The salt is the old secret, and the input key material
  291. * is the input secret (PSK / ECDHE). */
  292. ret = mbedtls_hkdf_extract( md,
  293. tmp_secret, hlen,
  294. tmp_input, ilen,
  295. secret_new );
  296. if( ret != 0 )
  297. goto cleanup;
  298. ret = 0;
  299. cleanup:
  300. mbedtls_platform_zeroize( tmp_secret, sizeof(tmp_secret) );
  301. mbedtls_platform_zeroize( tmp_input, sizeof(tmp_input) );
  302. return( ret );
  303. }
  304. int mbedtls_ssl_tls1_3_derive_early_secrets(
  305. mbedtls_md_type_t md_type,
  306. unsigned char const *early_secret,
  307. unsigned char const *transcript, size_t transcript_len,
  308. mbedtls_ssl_tls1_3_early_secrets *derived )
  309. {
  310. int ret;
  311. mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
  312. size_t const md_size = mbedtls_md_get_size( md_info );
  313. /* We should never call this function with an unknown hash,
  314. * but add an assertion anyway. */
  315. if( md_info == 0 )
  316. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  317. /*
  318. * 0
  319. * |
  320. * v
  321. * PSK -> HKDF-Extract = Early Secret
  322. * |
  323. * +-----> Derive-Secret(., "c e traffic", ClientHello)
  324. * | = client_early_traffic_secret
  325. * |
  326. * +-----> Derive-Secret(., "e exp master", ClientHello)
  327. * | = early_exporter_master_secret
  328. * v
  329. */
  330. /* Create client_early_traffic_secret */
  331. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  332. early_secret, md_size,
  333. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_e_traffic ),
  334. transcript, transcript_len,
  335. MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
  336. derived->client_early_traffic_secret,
  337. md_size );
  338. if( ret != 0 )
  339. return( ret );
  340. /* Create early exporter */
  341. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  342. early_secret, md_size,
  343. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( e_exp_master ),
  344. transcript, transcript_len,
  345. MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
  346. derived->early_exporter_master_secret,
  347. md_size );
  348. if( ret != 0 )
  349. return( ret );
  350. return( 0 );
  351. }
  352. int mbedtls_ssl_tls1_3_derive_handshake_secrets(
  353. mbedtls_md_type_t md_type,
  354. unsigned char const *handshake_secret,
  355. unsigned char const *transcript, size_t transcript_len,
  356. mbedtls_ssl_tls1_3_handshake_secrets *derived )
  357. {
  358. int ret;
  359. mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
  360. size_t const md_size = mbedtls_md_get_size( md_info );
  361. /* We should never call this function with an unknown hash,
  362. * but add an assertion anyway. */
  363. if( md_info == 0 )
  364. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  365. /*
  366. *
  367. * Handshake Secret
  368. * |
  369. * +-----> Derive-Secret( ., "c hs traffic",
  370. * | ClientHello...ServerHello )
  371. * | = client_handshake_traffic_secret
  372. * |
  373. * +-----> Derive-Secret( ., "s hs traffic",
  374. * | ClientHello...ServerHello )
  375. * | = server_handshake_traffic_secret
  376. *
  377. */
  378. /*
  379. * Compute client_handshake_traffic_secret with
  380. * Derive-Secret( ., "c hs traffic", ClientHello...ServerHello )
  381. */
  382. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  383. handshake_secret, md_size,
  384. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_hs_traffic ),
  385. transcript, transcript_len,
  386. MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
  387. derived->client_handshake_traffic_secret,
  388. md_size );
  389. if( ret != 0 )
  390. return( ret );
  391. /*
  392. * Compute server_handshake_traffic_secret with
  393. * Derive-Secret( ., "s hs traffic", ClientHello...ServerHello )
  394. */
  395. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  396. handshake_secret, md_size,
  397. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_hs_traffic ),
  398. transcript, transcript_len,
  399. MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
  400. derived->server_handshake_traffic_secret,
  401. md_size );
  402. if( ret != 0 )
  403. return( ret );
  404. return( 0 );
  405. }
  406. int mbedtls_ssl_tls1_3_derive_application_secrets(
  407. mbedtls_md_type_t md_type,
  408. unsigned char const *application_secret,
  409. unsigned char const *transcript, size_t transcript_len,
  410. mbedtls_ssl_tls1_3_application_secrets *derived )
  411. {
  412. int ret;
  413. mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
  414. size_t const md_size = mbedtls_md_get_size( md_info );
  415. /* We should never call this function with an unknown hash,
  416. * but add an assertion anyway. */
  417. if( md_info == 0 )
  418. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  419. /* Generate {client,server}_application_traffic_secret_0
  420. *
  421. * Master Secret
  422. * |
  423. * +-----> Derive-Secret( ., "c ap traffic",
  424. * | ClientHello...server Finished )
  425. * | = client_application_traffic_secret_0
  426. * |
  427. * +-----> Derive-Secret( ., "s ap traffic",
  428. * | ClientHello...Server Finished )
  429. * | = server_application_traffic_secret_0
  430. * |
  431. * +-----> Derive-Secret( ., "exp master",
  432. * | ClientHello...server Finished)
  433. * | = exporter_master_secret
  434. *
  435. */
  436. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  437. application_secret, md_size,
  438. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( c_ap_traffic ),
  439. transcript, transcript_len,
  440. MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
  441. derived->client_application_traffic_secret_N,
  442. md_size );
  443. if( ret != 0 )
  444. return( ret );
  445. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  446. application_secret, md_size,
  447. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( s_ap_traffic ),
  448. transcript, transcript_len,
  449. MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
  450. derived->server_application_traffic_secret_N,
  451. md_size );
  452. if( ret != 0 )
  453. return( ret );
  454. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  455. application_secret, md_size,
  456. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( exp_master ),
  457. transcript, transcript_len,
  458. MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
  459. derived->exporter_master_secret,
  460. md_size );
  461. if( ret != 0 )
  462. return( ret );
  463. return( 0 );
  464. }
  465. /* Generate resumption_master_secret for use with the ticket exchange.
  466. *
  467. * This is not integrated with mbedtls_ssl_tls1_3_derive_application_secrets()
  468. * because it uses the transcript hash up to and including ClientFinished. */
  469. int mbedtls_ssl_tls1_3_derive_resumption_master_secret(
  470. mbedtls_md_type_t md_type,
  471. unsigned char const *application_secret,
  472. unsigned char const *transcript, size_t transcript_len,
  473. mbedtls_ssl_tls1_3_application_secrets *derived )
  474. {
  475. int ret;
  476. mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
  477. size_t const md_size = mbedtls_md_get_size( md_info );
  478. /* We should never call this function with an unknown hash,
  479. * but add an assertion anyway. */
  480. if( md_info == 0 )
  481. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  482. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  483. application_secret, md_size,
  484. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_master ),
  485. transcript, transcript_len,
  486. MBEDTLS_SSL_TLS1_3_CONTEXT_HASHED,
  487. derived->resumption_master_secret,
  488. md_size );
  489. if( ret != 0 )
  490. return( ret );
  491. return( 0 );
  492. }
  493. int mbedtls_ssl_tls13_key_schedule_stage_application( mbedtls_ssl_context *ssl )
  494. {
  495. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  496. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  497. mbedtls_md_type_t const md_type = handshake->ciphersuite_info->mac;
  498. #if defined(MBEDTLS_DEBUG_C)
  499. mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
  500. size_t const md_size = mbedtls_md_get_size( md_info );
  501. #endif /* MBEDTLS_DEBUG_C */
  502. /*
  503. * Compute MasterSecret
  504. */
  505. ret = mbedtls_ssl_tls1_3_evolve_secret( md_type,
  506. handshake->tls1_3_master_secrets.handshake,
  507. NULL, 0,
  508. handshake->tls1_3_master_secrets.app );
  509. if( ret != 0 )
  510. {
  511. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_evolve_secret", ret );
  512. return( ret );
  513. }
  514. MBEDTLS_SSL_DEBUG_BUF( 4, "Master secret",
  515. handshake->tls1_3_master_secrets.app, md_size );
  516. return( 0 );
  517. }
  518. static int ssl_tls1_3_calc_finished_core( mbedtls_md_type_t md_type,
  519. unsigned char const *base_key,
  520. unsigned char const *transcript,
  521. unsigned char *dst )
  522. {
  523. const mbedtls_md_info_t* const md_info = mbedtls_md_info_from_type( md_type );
  524. size_t const md_size = mbedtls_md_get_size( md_info );
  525. unsigned char finished_key[MBEDTLS_MD_MAX_SIZE];
  526. int ret;
  527. /* We should never call this function with an unknown hash,
  528. * but add an assertion anyway. */
  529. if( md_info == 0 )
  530. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  531. /* TLS 1.3 Finished message
  532. *
  533. * struct {
  534. * opaque verify_data[Hash.length];
  535. * } Finished;
  536. *
  537. * verify_data =
  538. * HMAC( finished_key,
  539. * Hash( Handshake Context +
  540. * Certificate* +
  541. * CertificateVerify* )
  542. * )
  543. *
  544. * finished_key =
  545. * HKDF-Expand-Label( BaseKey, "finished", "", Hash.length )
  546. */
  547. ret = mbedtls_ssl_tls1_3_hkdf_expand_label(
  548. md_type, base_key, md_size,
  549. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( finished ),
  550. NULL, 0,
  551. finished_key, md_size );
  552. if( ret != 0 )
  553. goto exit;
  554. ret = mbedtls_md_hmac( md_info, finished_key, md_size, transcript, md_size, dst );
  555. if( ret != 0 )
  556. goto exit;
  557. exit:
  558. mbedtls_platform_zeroize( finished_key, sizeof( finished_key ) );
  559. return( ret );
  560. }
  561. int mbedtls_ssl_tls13_calculate_verify_data( mbedtls_ssl_context* ssl,
  562. unsigned char* dst,
  563. size_t dst_len,
  564. size_t *actual_len,
  565. int from )
  566. {
  567. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  568. unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  569. size_t transcript_len;
  570. unsigned char const *base_key = NULL;
  571. mbedtls_md_type_t const md_type = ssl->handshake->ciphersuite_info->mac;
  572. const mbedtls_md_info_t* const md = mbedtls_md_info_from_type( md_type );
  573. size_t const md_size = mbedtls_md_get_size( md );
  574. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_calculate_verify_data" ) );
  575. if( dst_len < md_size )
  576. return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
  577. ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
  578. transcript, sizeof( transcript ),
  579. &transcript_len );
  580. if( ret != 0 )
  581. {
  582. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_handshake_transcript", ret );
  583. goto exit;
  584. }
  585. MBEDTLS_SSL_DEBUG_BUF( 4, "handshake hash", transcript, transcript_len );
  586. if( from == MBEDTLS_SSL_IS_CLIENT )
  587. base_key = ssl->handshake->tls13_hs_secrets.client_handshake_traffic_secret;
  588. else
  589. base_key = ssl->handshake->tls13_hs_secrets.server_handshake_traffic_secret;
  590. ret = ssl_tls1_3_calc_finished_core( md_type, base_key, transcript, dst );
  591. if( ret != 0 )
  592. goto exit;
  593. *actual_len = md_size;
  594. MBEDTLS_SSL_DEBUG_BUF( 3, "verify_data for finished message", dst, md_size );
  595. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_calculate_verify_data" ) );
  596. exit:
  597. mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
  598. return( ret );
  599. }
  600. int mbedtls_ssl_tls1_3_create_psk_binder( mbedtls_ssl_context *ssl,
  601. const mbedtls_md_type_t md_type,
  602. unsigned char const *psk, size_t psk_len,
  603. int psk_type,
  604. unsigned char const *transcript,
  605. unsigned char *result )
  606. {
  607. int ret = 0;
  608. unsigned char binder_key[MBEDTLS_MD_MAX_SIZE];
  609. unsigned char early_secret[MBEDTLS_MD_MAX_SIZE];
  610. mbedtls_md_info_t const *md_info = mbedtls_md_info_from_type( md_type );
  611. size_t const md_size = mbedtls_md_get_size( md_info );
  612. #if !defined(MBEDTLS_DEBUG_C)
  613. ssl = NULL; /* make sure we don't use it except for debug */
  614. ((void) ssl);
  615. #endif
  616. /* We should never call this function with an unknown hash,
  617. * but add an assertion anyway. */
  618. if( md_info == 0 )
  619. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  620. /*
  621. * 0
  622. * |
  623. * v
  624. * PSK -> HKDF-Extract = Early Secret
  625. * |
  626. * +-----> Derive-Secret(., "ext binder" | "res binder", "")
  627. * | = binder_key
  628. * v
  629. */
  630. ret = mbedtls_ssl_tls1_3_evolve_secret( md_type,
  631. NULL, /* Old secret */
  632. psk, psk_len, /* Input */
  633. early_secret );
  634. if( ret != 0 )
  635. {
  636. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_evolve_secret", ret );
  637. goto exit;
  638. }
  639. if( psk_type == MBEDTLS_SSL_TLS1_3_PSK_RESUMPTION )
  640. {
  641. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  642. early_secret, md_size,
  643. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( res_binder ),
  644. NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
  645. binder_key, md_size );
  646. MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'res binder'" ) );
  647. }
  648. else
  649. {
  650. ret = mbedtls_ssl_tls1_3_derive_secret( md_type,
  651. early_secret, md_size,
  652. MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( ext_binder ),
  653. NULL, 0, MBEDTLS_SSL_TLS1_3_CONTEXT_UNHASHED,
  654. binder_key, md_size );
  655. MBEDTLS_SSL_DEBUG_MSG( 4, ( "Derive Early Secret with 'ext binder'" ) );
  656. }
  657. if( ret != 0 )
  658. {
  659. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_derive_secret", ret );
  660. goto exit;
  661. }
  662. /*
  663. * The binding_value is computed in the same way as the Finished message
  664. * but with the BaseKey being the binder_key.
  665. */
  666. ret = ssl_tls1_3_calc_finished_core( md_type, binder_key, transcript, result );
  667. if( ret != 0 )
  668. goto exit;
  669. MBEDTLS_SSL_DEBUG_BUF( 3, "psk binder", result, md_size );
  670. exit:
  671. mbedtls_platform_zeroize( early_secret, sizeof( early_secret ) );
  672. mbedtls_platform_zeroize( binder_key, sizeof( binder_key ) );
  673. return( ret );
  674. }
  675. int mbedtls_ssl_tls13_populate_transform( mbedtls_ssl_transform *transform,
  676. int endpoint,
  677. int ciphersuite,
  678. mbedtls_ssl_key_set const *traffic_keys,
  679. mbedtls_ssl_context *ssl /* DEBUG ONLY */ )
  680. {
  681. int ret;
  682. mbedtls_cipher_info_t const *cipher_info;
  683. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  684. unsigned char const *key_enc;
  685. unsigned char const *iv_enc;
  686. unsigned char const *key_dec;
  687. unsigned char const *iv_dec;
  688. #if !defined(MBEDTLS_DEBUG_C)
  689. ssl = NULL; /* make sure we don't use it except for those cases */
  690. (void) ssl;
  691. #endif
  692. ciphersuite_info = mbedtls_ssl_ciphersuite_from_id( ciphersuite );
  693. if( ciphersuite_info == NULL )
  694. {
  695. MBEDTLS_SSL_DEBUG_MSG( 1, ( "ciphersuite info for %d not found",
  696. ciphersuite ) );
  697. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  698. }
  699. cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
  700. if( cipher_info == NULL )
  701. {
  702. MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %u not found",
  703. ciphersuite_info->cipher ) );
  704. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  705. }
  706. /*
  707. * Setup cipher contexts in target transform
  708. */
  709. if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
  710. cipher_info ) ) != 0 )
  711. {
  712. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
  713. return( ret );
  714. }
  715. if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
  716. cipher_info ) ) != 0 )
  717. {
  718. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
  719. return( ret );
  720. }
  721. #if defined(MBEDTLS_SSL_SRV_C)
  722. if( endpoint == MBEDTLS_SSL_IS_SERVER )
  723. {
  724. key_enc = traffic_keys->server_write_key;
  725. key_dec = traffic_keys->client_write_key;
  726. iv_enc = traffic_keys->server_write_iv;
  727. iv_dec = traffic_keys->client_write_iv;
  728. }
  729. else
  730. #endif /* MBEDTLS_SSL_SRV_C */
  731. #if defined(MBEDTLS_SSL_CLI_C)
  732. if( endpoint == MBEDTLS_SSL_IS_CLIENT )
  733. {
  734. key_enc = traffic_keys->client_write_key;
  735. key_dec = traffic_keys->server_write_key;
  736. iv_enc = traffic_keys->client_write_iv;
  737. iv_dec = traffic_keys->server_write_iv;
  738. }
  739. else
  740. #endif /* MBEDTLS_SSL_CLI_C */
  741. {
  742. /* should not happen */
  743. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  744. }
  745. memcpy( transform->iv_enc, iv_enc, traffic_keys->iv_len );
  746. memcpy( transform->iv_dec, iv_dec, traffic_keys->iv_len );
  747. if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc,
  748. key_enc, cipher_info->key_bitlen,
  749. MBEDTLS_ENCRYPT ) ) != 0 )
  750. {
  751. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
  752. return( ret );
  753. }
  754. if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec,
  755. key_dec, cipher_info->key_bitlen,
  756. MBEDTLS_DECRYPT ) ) != 0 )
  757. {
  758. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
  759. return( ret );
  760. }
  761. /*
  762. * Setup other fields in SSL transform
  763. */
  764. if( ( ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ) != 0 )
  765. transform->taglen = 8;
  766. else
  767. transform->taglen = 16;
  768. transform->ivlen = traffic_keys->iv_len;
  769. transform->maclen = 0;
  770. transform->fixed_ivlen = transform->ivlen;
  771. transform->minor_ver = MBEDTLS_SSL_MINOR_VERSION_4;
  772. /* We add the true record content type (1 Byte) to the plaintext and
  773. * then pad to the configured granularity. The mimimum length of the
  774. * type-extended and padded plaintext is therefore the padding
  775. * granularity. */
  776. transform->minlen =
  777. transform->taglen + MBEDTLS_SSL_CID_TLS1_3_PADDING_GRANULARITY;
  778. return( 0 );
  779. }
  780. int mbedtls_ssl_tls1_3_key_schedule_stage_early( mbedtls_ssl_context *ssl )
  781. {
  782. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  783. mbedtls_md_type_t md_type;
  784. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  785. if( handshake->ciphersuite_info == NULL )
  786. {
  787. MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher suite info not found" ) );
  788. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  789. }
  790. md_type = handshake->ciphersuite_info->mac;
  791. ret = mbedtls_ssl_tls1_3_evolve_secret( md_type, NULL, NULL, 0,
  792. handshake->tls1_3_master_secrets.early );
  793. if( ret != 0 )
  794. {
  795. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_evolve_secret", ret );
  796. return( ret );
  797. }
  798. return( 0 );
  799. }
  800. /* mbedtls_ssl_tls13_generate_handshake_keys() generates keys necessary for
  801. * protecting the handshake messages, as described in Section 7 of TLS 1.3. */
  802. int mbedtls_ssl_tls13_generate_handshake_keys( mbedtls_ssl_context *ssl,
  803. mbedtls_ssl_key_set *traffic_keys )
  804. {
  805. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  806. mbedtls_md_type_t md_type;
  807. mbedtls_md_info_t const *md_info;
  808. size_t md_size;
  809. unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  810. size_t transcript_len;
  811. mbedtls_cipher_info_t const *cipher_info;
  812. size_t keylen, ivlen;
  813. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  814. const mbedtls_ssl_ciphersuite_t *ciphersuite_info = handshake->ciphersuite_info;
  815. mbedtls_ssl_tls1_3_handshake_secrets *tls13_hs_secrets = &handshake->tls13_hs_secrets;
  816. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_tls13_generate_handshake_keys" ) );
  817. cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
  818. keylen = cipher_info->key_bitlen >> 3;
  819. ivlen = cipher_info->iv_size;
  820. md_type = ciphersuite_info->mac;
  821. md_info = mbedtls_md_info_from_type( md_type );
  822. md_size = mbedtls_md_get_size( md_info );
  823. ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
  824. transcript,
  825. sizeof( transcript ),
  826. &transcript_len );
  827. if( ret != 0 )
  828. {
  829. MBEDTLS_SSL_DEBUG_RET( 1,
  830. "mbedtls_ssl_get_handshake_transcript",
  831. ret );
  832. return( ret );
  833. }
  834. ret = mbedtls_ssl_tls1_3_derive_handshake_secrets( md_type,
  835. handshake->tls1_3_master_secrets.handshake,
  836. transcript, transcript_len, tls13_hs_secrets );
  837. if( ret != 0 )
  838. {
  839. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_derive_handshake_secrets",
  840. ret );
  841. return( ret );
  842. }
  843. MBEDTLS_SSL_DEBUG_BUF( 4, "Client handshake traffic secret",
  844. tls13_hs_secrets->client_handshake_traffic_secret,
  845. md_size );
  846. MBEDTLS_SSL_DEBUG_BUF( 4, "Server handshake traffic secret",
  847. tls13_hs_secrets->server_handshake_traffic_secret,
  848. md_size );
  849. /*
  850. * Export client handshake traffic secret
  851. */
  852. if( ssl->f_export_keys != NULL )
  853. {
  854. ssl->f_export_keys( ssl->p_export_keys,
  855. MBEDTLS_SSL_KEY_EXPORT_TLS13_CLIENT_HANDSHAKE_TRAFFIC_SECRET,
  856. tls13_hs_secrets->client_handshake_traffic_secret,
  857. md_size,
  858. handshake->randbytes + 32,
  859. handshake->randbytes,
  860. MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
  861. ssl->f_export_keys( ssl->p_export_keys,
  862. MBEDTLS_SSL_KEY_EXPORT_TLS13_SERVER_HANDSHAKE_TRAFFIC_SECRET,
  863. tls13_hs_secrets->server_handshake_traffic_secret,
  864. md_size,
  865. handshake->randbytes + 32,
  866. handshake->randbytes,
  867. MBEDTLS_SSL_TLS_PRF_NONE /* TODO: FIX! */ );
  868. }
  869. ret = mbedtls_ssl_tls1_3_make_traffic_keys( md_type,
  870. tls13_hs_secrets->client_handshake_traffic_secret,
  871. tls13_hs_secrets->server_handshake_traffic_secret,
  872. md_size, keylen, ivlen, traffic_keys );
  873. if( ret != 0 )
  874. {
  875. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_make_traffic_keys", ret );
  876. goto exit;
  877. }
  878. MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_key",
  879. traffic_keys->client_write_key,
  880. traffic_keys->key_len);
  881. MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_key",
  882. traffic_keys->server_write_key,
  883. traffic_keys->key_len);
  884. MBEDTLS_SSL_DEBUG_BUF( 4, "client_handshake write_iv",
  885. traffic_keys->client_write_iv,
  886. traffic_keys->iv_len);
  887. MBEDTLS_SSL_DEBUG_BUF( 4, "server_handshake write_iv",
  888. traffic_keys->server_write_iv,
  889. traffic_keys->iv_len);
  890. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_tls13_generate_handshake_keys" ) );
  891. exit:
  892. return( ret );
  893. }
  894. int mbedtls_ssl_tls13_key_schedule_stage_handshake( mbedtls_ssl_context *ssl )
  895. {
  896. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  897. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  898. mbedtls_md_type_t const md_type = handshake->ciphersuite_info->mac;
  899. size_t ephemeral_len = 0;
  900. unsigned char ecdhe[MBEDTLS_ECP_MAX_BYTES];
  901. #if defined(MBEDTLS_DEBUG_C)
  902. mbedtls_md_info_t const * const md_info = mbedtls_md_info_from_type( md_type );
  903. size_t const md_size = mbedtls_md_get_size( md_info );
  904. #endif /* MBEDTLS_DEBUG_C */
  905. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
  906. /*
  907. * Compute ECDHE secret used to compute the handshake secret from which
  908. * client_handshake_traffic_secret and server_handshake_traffic_secret
  909. * are derived in the handshake secret derivation stage.
  910. */
  911. if( mbedtls_ssl_tls1_3_ephemeral_enabled( ssl ) )
  912. {
  913. if( mbedtls_ssl_tls13_named_group_is_ecdhe( handshake->offered_group_id ) )
  914. {
  915. #if defined(MBEDTLS_ECDH_C)
  916. ret = mbedtls_ecdh_calc_secret( &handshake->ecdh_ctx,
  917. &ephemeral_len, ecdhe, sizeof( ecdhe ),
  918. ssl->conf->f_rng,
  919. ssl->conf->p_rng );
  920. if( ret != 0 )
  921. {
  922. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
  923. return( ret );
  924. }
  925. #endif /* MBEDTLS_ECDH_C */
  926. }
  927. else if( mbedtls_ssl_tls13_named_group_is_dhe( handshake->offered_group_id ) )
  928. {
  929. MBEDTLS_SSL_DEBUG_MSG( 1, ( "DHE not supported." ) );
  930. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  931. }
  932. }
  933. #else
  934. return( MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE );
  935. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
  936. /*
  937. * Compute the Handshake Secret
  938. */
  939. ret = mbedtls_ssl_tls1_3_evolve_secret( md_type,
  940. handshake->tls1_3_master_secrets.early,
  941. ecdhe, ephemeral_len,
  942. handshake->tls1_3_master_secrets.handshake );
  943. if( ret != 0 )
  944. {
  945. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_evolve_secret", ret );
  946. return( ret );
  947. }
  948. MBEDTLS_SSL_DEBUG_BUF( 4, "Handshake secret",
  949. handshake->tls1_3_master_secrets.handshake, md_size );
  950. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED)
  951. mbedtls_platform_zeroize( ecdhe, sizeof( ecdhe ) );
  952. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_ECDHE_ENABLED */
  953. return( 0 );
  954. }
  955. /* Generate application traffic keys since any records following a 1-RTT Finished message
  956. * MUST be encrypted under the application traffic key.
  957. */
  958. int mbedtls_ssl_tls13_generate_application_keys(
  959. mbedtls_ssl_context *ssl,
  960. mbedtls_ssl_key_set *traffic_keys )
  961. {
  962. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  963. mbedtls_ssl_handshake_params *handshake = ssl->handshake;
  964. /* Address at which to store the application secrets */
  965. mbedtls_ssl_tls1_3_application_secrets * const app_secrets =
  966. &ssl->session_negotiate->app_secrets;
  967. /* Holding the transcript up to and including the ServerFinished */
  968. unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  969. size_t transcript_len;
  970. /* Variables relating to the hash for the chosen ciphersuite. */
  971. mbedtls_md_type_t md_type;
  972. mbedtls_md_info_t const *md_info;
  973. size_t md_size;
  974. /* Variables relating to the cipher for the chosen ciphersuite. */
  975. mbedtls_cipher_info_t const *cipher_info;
  976. size_t keylen, ivlen;
  977. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive application traffic keys" ) );
  978. /* Extract basic information about hash and ciphersuite */
  979. cipher_info = mbedtls_cipher_info_from_type(
  980. handshake->ciphersuite_info->cipher );
  981. keylen = cipher_info->key_bitlen / 8;
  982. ivlen = cipher_info->iv_size;
  983. md_type = handshake->ciphersuite_info->mac;
  984. md_info = mbedtls_md_info_from_type( md_type );
  985. md_size = mbedtls_md_get_size( md_info );
  986. /* Compute current handshake transcript. It's the caller's responsiblity
  987. * to call this at the right time, that is, after the ServerFinished. */
  988. ret = mbedtls_ssl_get_handshake_transcript( ssl, md_type,
  989. transcript, sizeof( transcript ),
  990. &transcript_len );
  991. if( ret != 0 )
  992. goto cleanup;
  993. /* Compute application secrets from master secret and transcript hash. */
  994. ret = mbedtls_ssl_tls1_3_derive_application_secrets( md_type,
  995. handshake->tls1_3_master_secrets.app,
  996. transcript, transcript_len,
  997. app_secrets );
  998. if( ret != 0 )
  999. {
  1000. MBEDTLS_SSL_DEBUG_RET( 1,
  1001. "mbedtls_ssl_tls1_3_derive_application_secrets", ret );
  1002. goto cleanup;
  1003. }
  1004. /* Derive first epoch of IV + Key for application traffic. */
  1005. ret = mbedtls_ssl_tls1_3_make_traffic_keys( md_type,
  1006. app_secrets->client_application_traffic_secret_N,
  1007. app_secrets->server_application_traffic_secret_N,
  1008. md_size, keylen, ivlen, traffic_keys );
  1009. if( ret != 0 )
  1010. {
  1011. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls1_3_make_traffic_keys", ret );
  1012. goto cleanup;
  1013. }
  1014. MBEDTLS_SSL_DEBUG_BUF( 4, "Client application traffic secret",
  1015. app_secrets->client_application_traffic_secret_N,
  1016. md_size );
  1017. MBEDTLS_SSL_DEBUG_BUF( 4, "Server application traffic secret",
  1018. app_secrets->server_application_traffic_secret_N,
  1019. md_size );
  1020. /*
  1021. * Export client/server application traffic secret 0
  1022. */
  1023. if( ssl->f_export_keys != NULL )
  1024. {
  1025. ssl->f_export_keys( ssl->p_export_keys,
  1026. MBEDTLS_SSL_KEY_EXPORT_TLS13_CLIENT_APPLICATION_TRAFFIC_SECRET,
  1027. app_secrets->client_application_traffic_secret_N, md_size,
  1028. handshake->randbytes + 32,
  1029. handshake->randbytes,
  1030. MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
  1031. a new constant for TLS 1.3! */ );
  1032. ssl->f_export_keys( ssl->p_export_keys,
  1033. MBEDTLS_SSL_KEY_EXPORT_TLS13_SERVER_APPLICATION_TRAFFIC_SECRET,
  1034. app_secrets->server_application_traffic_secret_N, md_size,
  1035. handshake->randbytes + 32,
  1036. handshake->randbytes,
  1037. MBEDTLS_SSL_TLS_PRF_NONE /* TODO: this should be replaced by
  1038. a new constant for TLS 1.3! */ );
  1039. }
  1040. MBEDTLS_SSL_DEBUG_BUF( 4, "client application_write_key:",
  1041. traffic_keys->client_write_key, keylen );
  1042. MBEDTLS_SSL_DEBUG_BUF( 4, "server application write key",
  1043. traffic_keys->server_write_key, keylen );
  1044. MBEDTLS_SSL_DEBUG_BUF( 4, "client application write IV",
  1045. traffic_keys->client_write_iv, ivlen );
  1046. MBEDTLS_SSL_DEBUG_BUF( 4, "server application write IV",
  1047. traffic_keys->server_write_iv, ivlen );
  1048. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive application traffic keys" ) );
  1049. cleanup:
  1050. mbedtls_platform_zeroize( transcript, sizeof( transcript ) );
  1051. return( ret );
  1052. }
  1053. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */