ssl_tls13_generic.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. * TLS 1.3 functionality shared between client and server
  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_TLS_C)
  21. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  22. #include <string.h>
  23. #include "mbedtls/error.h"
  24. #include "mbedtls/debug.h"
  25. #include "mbedtls/oid.h"
  26. #include "mbedtls/platform.h"
  27. #include "mbedtls/constant_time.h"
  28. #include <string.h>
  29. #include "ssl_misc.h"
  30. #include "ssl_tls13_keys.h"
  31. int mbedtls_ssl_tls1_3_fetch_handshake_msg( mbedtls_ssl_context *ssl,
  32. unsigned hs_type,
  33. unsigned char **buf,
  34. size_t *buflen )
  35. {
  36. int ret;
  37. if( ( ret = mbedtls_ssl_read_record( ssl, 0 ) ) != 0 )
  38. {
  39. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
  40. goto cleanup;
  41. }
  42. if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE ||
  43. ssl->in_msg[0] != hs_type )
  44. {
  45. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Receive unexpected handshake message." ) );
  46. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE,
  47. MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
  48. ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
  49. goto cleanup;
  50. }
  51. /*
  52. * Jump handshake header (4 bytes, see Section 4 of RFC 8446).
  53. * ...
  54. * HandshakeType msg_type;
  55. * uint24 length;
  56. * ...
  57. */
  58. *buf = ssl->in_msg + 4;
  59. *buflen = ssl->in_hslen - 4;
  60. cleanup:
  61. return( ret );
  62. }
  63. int mbedtls_ssl_tls13_start_handshake_msg( mbedtls_ssl_context *ssl,
  64. unsigned hs_type,
  65. unsigned char **buf,
  66. size_t *buf_len )
  67. {
  68. /*
  69. * Reserve 4 bytes for hanshake header. ( Section 4,RFC 8446 )
  70. * ...
  71. * HandshakeType msg_type;
  72. * uint24 length;
  73. * ...
  74. */
  75. *buf = ssl->out_msg + 4;
  76. *buf_len = MBEDTLS_SSL_OUT_CONTENT_LEN - 4;
  77. ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
  78. ssl->out_msg[0] = hs_type;
  79. return( 0 );
  80. }
  81. int mbedtls_ssl_tls13_finish_handshake_msg( mbedtls_ssl_context *ssl,
  82. size_t buf_len,
  83. size_t msg_len )
  84. {
  85. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  86. size_t msg_len_with_header;
  87. ((void) buf_len);
  88. /* Add reserved 4 bytes for handshake header */
  89. msg_len_with_header = msg_len + 4;
  90. ssl->out_msglen = msg_len_with_header;
  91. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_write_handshake_msg_ext( ssl, 0 ) );
  92. cleanup:
  93. return( ret );
  94. }
  95. void mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( mbedtls_ssl_context *ssl,
  96. unsigned hs_type,
  97. unsigned char const *msg,
  98. size_t msg_len )
  99. {
  100. mbedtls_ssl_tls13_add_hs_hdr_to_checksum( ssl, hs_type, msg_len );
  101. ssl->handshake->update_checksum( ssl, msg, msg_len );
  102. }
  103. void mbedtls_ssl_tls13_add_hs_hdr_to_checksum( mbedtls_ssl_context *ssl,
  104. unsigned hs_type,
  105. size_t total_hs_len )
  106. {
  107. unsigned char hs_hdr[4];
  108. /* Build HS header for checksum update. */
  109. hs_hdr[0] = MBEDTLS_BYTE_0( hs_type );
  110. hs_hdr[1] = MBEDTLS_BYTE_2( total_hs_len );
  111. hs_hdr[2] = MBEDTLS_BYTE_1( total_hs_len );
  112. hs_hdr[3] = MBEDTLS_BYTE_0( total_hs_len );
  113. ssl->handshake->update_checksum( ssl, hs_hdr, sizeof( hs_hdr ) );
  114. }
  115. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  116. /*
  117. * mbedtls_ssl_tls13_write_sig_alg_ext( )
  118. *
  119. * enum {
  120. * ....
  121. * ecdsa_secp256r1_sha256( 0x0403 ),
  122. * ecdsa_secp384r1_sha384( 0x0503 ),
  123. * ecdsa_secp521r1_sha512( 0x0603 ),
  124. * ....
  125. * } SignatureScheme;
  126. *
  127. * struct {
  128. * SignatureScheme supported_signature_algorithms<2..2^16-2>;
  129. * } SignatureSchemeList;
  130. *
  131. * Only if we handle at least one key exchange that needs signatures.
  132. */
  133. int mbedtls_ssl_tls13_write_sig_alg_ext( mbedtls_ssl_context *ssl,
  134. unsigned char *buf,
  135. unsigned char *end,
  136. size_t *olen )
  137. {
  138. unsigned char *p = buf;
  139. unsigned char *supported_sig_alg_ptr; /* Start of supported_signature_algorithms */
  140. size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
  141. *olen = 0;
  142. /* Skip the extension on the client if all allowed key exchanges
  143. * are PSK-based. */
  144. #if defined(MBEDTLS_SSL_CLI_C)
  145. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
  146. !mbedtls_ssl_conf_tls13_some_ephemeral_enabled( ssl ) )
  147. {
  148. return( 0 );
  149. }
  150. #endif /* MBEDTLS_SSL_CLI_C */
  151. MBEDTLS_SSL_DEBUG_MSG( 3, ( "adding signature_algorithms extension" ) );
  152. /* Check if we have space for header and length field:
  153. * - extension_type (2 bytes)
  154. * - extension_data_length (2 bytes)
  155. * - supported_signature_algorithms_length (2 bytes)
  156. */
  157. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 6 );
  158. p += 6;
  159. /*
  160. * Write supported_signature_algorithms
  161. */
  162. supported_sig_alg_ptr = p;
  163. for( const uint16_t *sig_alg = ssl->conf->tls13_sig_algs;
  164. *sig_alg != MBEDTLS_TLS13_SIG_NONE; sig_alg++ )
  165. {
  166. MBEDTLS_SSL_CHK_BUF_PTR( p, end, 2 );
  167. MBEDTLS_PUT_UINT16_BE( *sig_alg, p, 0 );
  168. p += 2;
  169. MBEDTLS_SSL_DEBUG_MSG( 3, ( "signature scheme [%x]", *sig_alg ) );
  170. }
  171. /* Length of supported_signature_algorithms */
  172. supported_sig_alg_len = p - supported_sig_alg_ptr;
  173. if( supported_sig_alg_len == 0 )
  174. {
  175. MBEDTLS_SSL_DEBUG_MSG( 1, ( "No signature algorithms defined." ) );
  176. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  177. }
  178. /* Write extension_type */
  179. MBEDTLS_PUT_UINT16_BE( MBEDTLS_TLS_EXT_SIG_ALG, buf, 0 );
  180. /* Write extension_data_length */
  181. MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len + 2, buf, 2 );
  182. /* Write length of supported_signature_algorithms */
  183. MBEDTLS_PUT_UINT16_BE( supported_sig_alg_len, buf, 4 );
  184. /* Output the total length of signature algorithms extension. */
  185. *olen = p - buf;
  186. ssl->handshake->extensions_present |= MBEDTLS_SSL_EXT_SIG_ALG;
  187. return( 0 );
  188. }
  189. /*
  190. * STATE HANDLING: Read CertificateVerify
  191. */
  192. /* Macro to express the maximum length of the verify structure.
  193. *
  194. * The structure is computed per TLS 1.3 specification as:
  195. * - 64 bytes of octet 32,
  196. * - 33 bytes for the context string
  197. * (which is either "TLS 1.3, client CertificateVerify"
  198. * or "TLS 1.3, server CertificateVerify"),
  199. * - 1 byte for the octet 0x0, which serves as a separator,
  200. * - 32 or 48 bytes for the Transcript-Hash(Handshake Context, Certificate)
  201. * (depending on the size of the transcript_hash)
  202. *
  203. * This results in a total size of
  204. * - 130 bytes for a SHA256-based transcript hash, or
  205. * (64 + 33 + 1 + 32 bytes)
  206. * - 146 bytes for a SHA384-based transcript hash.
  207. * (64 + 33 + 1 + 48 bytes)
  208. *
  209. */
  210. #define SSL_VERIFY_STRUCT_MAX_SIZE ( 64 + \
  211. 33 + \
  212. 1 + \
  213. MBEDTLS_TLS1_3_MD_MAX_SIZE \
  214. )
  215. /*
  216. * The ssl_tls13_create_verify_structure() creates the verify structure.
  217. * As input, it requires the transcript hash.
  218. *
  219. * The caller has to ensure that the buffer has size at least
  220. * SSL_VERIFY_STRUCT_MAX_SIZE bytes.
  221. */
  222. static void ssl_tls13_create_verify_structure( const unsigned char *transcript_hash,
  223. size_t transcript_hash_len,
  224. unsigned char *verify_buffer,
  225. size_t *verify_buffer_len,
  226. int from )
  227. {
  228. size_t idx;
  229. /* RFC 8446, Section 4.4.3:
  230. *
  231. * The digital signature [in the CertificateVerify message] is then
  232. * computed over the concatenation of:
  233. * - A string that consists of octet 32 (0x20) repeated 64 times
  234. * - The context string
  235. * - A single 0 byte which serves as the separator
  236. * - The content to be signed
  237. */
  238. memset( verify_buffer, 0x20, 64 );
  239. idx = 64;
  240. if( from == MBEDTLS_SSL_IS_CLIENT )
  241. {
  242. memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( client_cv ) );
  243. idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( client_cv );
  244. }
  245. else
  246. { /* from == MBEDTLS_SSL_IS_SERVER */
  247. memcpy( verify_buffer + idx, MBEDTLS_SSL_TLS1_3_LBL_WITH_LEN( server_cv ) );
  248. idx += MBEDTLS_SSL_TLS1_3_LBL_LEN( server_cv );
  249. }
  250. verify_buffer[idx++] = 0x0;
  251. memcpy( verify_buffer + idx, transcript_hash, transcript_hash_len );
  252. idx += transcript_hash_len;
  253. *verify_buffer_len = idx;
  254. }
  255. static int ssl_tls13_sig_alg_is_offered( const mbedtls_ssl_context *ssl,
  256. uint16_t sig_alg )
  257. {
  258. const uint16_t *tls13_sig_alg = ssl->conf->tls13_sig_algs;
  259. for( ; *tls13_sig_alg != MBEDTLS_TLS13_SIG_NONE ; tls13_sig_alg++ )
  260. {
  261. if( *tls13_sig_alg == sig_alg )
  262. return( 1 );
  263. }
  264. return( 0 );
  265. }
  266. static int ssl_tls13_parse_certificate_verify( mbedtls_ssl_context *ssl,
  267. const unsigned char *buf,
  268. const unsigned char *end,
  269. const unsigned char *verify_buffer,
  270. size_t verify_buffer_len )
  271. {
  272. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  273. const unsigned char *p = buf;
  274. uint16_t algorithm;
  275. size_t signature_len;
  276. mbedtls_pk_type_t sig_alg;
  277. mbedtls_md_type_t md_alg;
  278. unsigned char verify_hash[MBEDTLS_MD_MAX_SIZE];
  279. size_t verify_hash_len;
  280. void const *opts_ptr = NULL;
  281. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  282. mbedtls_pk_rsassa_pss_options opts;
  283. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  284. /*
  285. * struct {
  286. * SignatureScheme algorithm;
  287. * opaque signature<0..2^16-1>;
  288. * } CertificateVerify;
  289. */
  290. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
  291. algorithm = MBEDTLS_GET_UINT16_BE( p, 0 );
  292. p += 2;
  293. /* RFC 8446 section 4.4.3
  294. *
  295. * If the CertificateVerify message is sent by a server, the signature algorithm
  296. * MUST be one offered in the client's "signature_algorithms" extension unless
  297. * no valid certificate chain can be produced without unsupported algorithms
  298. *
  299. * RFC 8446 section 4.4.2.2
  300. *
  301. * If the client cannot construct an acceptable chain using the provided
  302. * certificates and decides to abort the handshake, then it MUST abort the handshake
  303. * with an appropriate certificate-related alert (by default, "unsupported_certificate").
  304. *
  305. * Check if algorithm is an offered signature algorithm.
  306. */
  307. if( ! ssl_tls13_sig_alg_is_offered( ssl, algorithm ) )
  308. {
  309. /* algorithm not in offered signature algorithms list */
  310. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Received signature algorithm(%04x) is not "
  311. "offered.",
  312. ( unsigned int ) algorithm ) );
  313. goto error;
  314. }
  315. /* We currently only support ECDSA-based signatures */
  316. switch( algorithm )
  317. {
  318. case MBEDTLS_TLS13_SIG_ECDSA_SECP256R1_SHA256:
  319. md_alg = MBEDTLS_MD_SHA256;
  320. sig_alg = MBEDTLS_PK_ECDSA;
  321. break;
  322. case MBEDTLS_TLS13_SIG_ECDSA_SECP384R1_SHA384:
  323. md_alg = MBEDTLS_MD_SHA384;
  324. sig_alg = MBEDTLS_PK_ECDSA;
  325. break;
  326. case MBEDTLS_TLS13_SIG_ECDSA_SECP521R1_SHA512:
  327. md_alg = MBEDTLS_MD_SHA512;
  328. sig_alg = MBEDTLS_PK_ECDSA;
  329. break;
  330. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  331. case MBEDTLS_TLS13_SIG_RSA_PSS_RSAE_SHA256:
  332. MBEDTLS_SSL_DEBUG_MSG( 4, ( "Certificate Verify: using RSA PSS" ) );
  333. md_alg = MBEDTLS_MD_SHA256;
  334. sig_alg = MBEDTLS_PK_RSASSA_PSS;
  335. break;
  336. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  337. default:
  338. MBEDTLS_SSL_DEBUG_MSG( 1, ( "Certificate Verify: Unknown signature algorithm." ) );
  339. goto error;
  340. }
  341. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate Verify: Signature algorithm ( %04x )",
  342. ( unsigned int ) algorithm ) );
  343. /*
  344. * Check the certificate's key type matches the signature alg
  345. */
  346. if( !mbedtls_pk_can_do( &ssl->session_negotiate->peer_cert->pk, sig_alg ) )
  347. {
  348. MBEDTLS_SSL_DEBUG_MSG( 1, ( "signature algorithm doesn't match cert key" ) );
  349. goto error;
  350. }
  351. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 2 );
  352. signature_len = MBEDTLS_GET_UINT16_BE( p, 0 );
  353. p += 2;
  354. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, signature_len );
  355. /* Hash verify buffer with indicated hash function */
  356. switch( md_alg )
  357. {
  358. #if defined(MBEDTLS_SHA256_C)
  359. case MBEDTLS_MD_SHA256:
  360. verify_hash_len = 32;
  361. ret = mbedtls_sha256( verify_buffer, verify_buffer_len, verify_hash, 0 );
  362. break;
  363. #endif /* MBEDTLS_SHA256_C */
  364. #if defined(MBEDTLS_SHA384_C)
  365. case MBEDTLS_MD_SHA384:
  366. verify_hash_len = 48;
  367. ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 1 );
  368. break;
  369. #endif /* MBEDTLS_SHA384_C */
  370. #if defined(MBEDTLS_SHA512_C)
  371. case MBEDTLS_MD_SHA512:
  372. verify_hash_len = 64;
  373. ret = mbedtls_sha512( verify_buffer, verify_buffer_len, verify_hash, 0 );
  374. break;
  375. #endif /* MBEDTLS_SHA512_C */
  376. default:
  377. ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
  378. break;
  379. }
  380. if( ret != 0 )
  381. {
  382. MBEDTLS_SSL_DEBUG_RET( 1, "hash computation error", ret );
  383. goto error;
  384. }
  385. MBEDTLS_SSL_DEBUG_BUF( 3, "verify hash", verify_hash, verify_hash_len );
  386. #if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
  387. if( sig_alg == MBEDTLS_PK_RSASSA_PSS )
  388. {
  389. const mbedtls_md_info_t* md_info;
  390. opts.mgf1_hash_id = md_alg;
  391. if( ( md_info = mbedtls_md_info_from_type( md_alg ) ) == NULL )
  392. {
  393. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  394. }
  395. opts.expected_salt_len = mbedtls_md_get_size( md_info );
  396. opts_ptr = (const void*) &opts;
  397. }
  398. #endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
  399. if( ( ret = mbedtls_pk_verify_ext( sig_alg, opts_ptr,
  400. &ssl->session_negotiate->peer_cert->pk,
  401. md_alg, verify_hash, verify_hash_len,
  402. p, signature_len ) ) == 0 )
  403. {
  404. return( 0 );
  405. }
  406. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_pk_verify_ext", ret );
  407. error:
  408. /* RFC 8446 section 4.4.3
  409. *
  410. * If the verification fails, the receiver MUST terminate the handshake
  411. * with a "decrypt_error" alert.
  412. */
  413. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
  414. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  415. return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  416. }
  417. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  418. int mbedtls_ssl_tls13_process_certificate_verify( mbedtls_ssl_context *ssl )
  419. {
  420. #if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  421. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  422. unsigned char verify_buffer[SSL_VERIFY_STRUCT_MAX_SIZE];
  423. size_t verify_buffer_len;
  424. unsigned char transcript[MBEDTLS_TLS1_3_MD_MAX_SIZE];
  425. size_t transcript_len;
  426. unsigned char *buf;
  427. size_t buf_len;
  428. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate verify" ) );
  429. MBEDTLS_SSL_PROC_CHK(
  430. mbedtls_ssl_tls1_3_fetch_handshake_msg( ssl,
  431. MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, &buf, &buf_len ) );
  432. /* Need to calculate the hash of the transcript first
  433. * before reading the message since otherwise it gets
  434. * included in the transcript
  435. */
  436. ret = mbedtls_ssl_get_handshake_transcript( ssl,
  437. ssl->handshake->ciphersuite_info->mac,
  438. transcript, sizeof( transcript ),
  439. &transcript_len );
  440. if( ret != 0 )
  441. {
  442. MBEDTLS_SSL_PEND_FATAL_ALERT(
  443. MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
  444. MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  445. return( ret );
  446. }
  447. MBEDTLS_SSL_DEBUG_BUF( 3, "handshake hash", transcript, transcript_len );
  448. /* Create verify structure */
  449. ssl_tls13_create_verify_structure( transcript,
  450. transcript_len,
  451. verify_buffer,
  452. &verify_buffer_len,
  453. ( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) ?
  454. MBEDTLS_SSL_IS_SERVER :
  455. MBEDTLS_SSL_IS_CLIENT );
  456. /* Process the message contents */
  457. MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate_verify( ssl, buf,
  458. buf + buf_len, verify_buffer, verify_buffer_len ) );
  459. mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl,
  460. MBEDTLS_SSL_HS_CERTIFICATE_VERIFY, buf, buf_len );
  461. cleanup:
  462. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate verify" ) );
  463. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_process_certificate_verify", ret );
  464. return( ret );
  465. #else
  466. ((void) ssl);
  467. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  468. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  469. #endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  470. }
  471. /*
  472. *
  473. * STATE HANDLING: Incoming Certificate, client-side only currently.
  474. *
  475. */
  476. /*
  477. * Implementation
  478. */
  479. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  480. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  481. /*
  482. * Structure of Certificate message:
  483. *
  484. * enum {
  485. * X509(0),
  486. * RawPublicKey(2),
  487. * (255)
  488. * } CertificateType;
  489. *
  490. * struct {
  491. * select (certificate_type) {
  492. * case RawPublicKey:
  493. * * From RFC 7250 ASN.1_subjectPublicKeyInfo *
  494. * opaque ASN1_subjectPublicKeyInfo<1..2^24-1>;
  495. * case X509:
  496. * opaque cert_data<1..2^24-1>;
  497. * };
  498. * Extension extensions<0..2^16-1>;
  499. * } CertificateEntry;
  500. *
  501. * struct {
  502. * opaque certificate_request_context<0..2^8-1>;
  503. * CertificateEntry certificate_list<0..2^24-1>;
  504. * } Certificate;
  505. *
  506. */
  507. /* Parse certificate chain send by the server. */
  508. static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
  509. const unsigned char *buf,
  510. const unsigned char *end )
  511. {
  512. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  513. size_t certificate_request_context_len = 0;
  514. size_t certificate_list_len = 0;
  515. const unsigned char *p = buf;
  516. const unsigned char *certificate_list_end;
  517. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, end, 4 );
  518. certificate_request_context_len = p[0];
  519. certificate_list_len = MBEDTLS_GET_UINT24_BE( p, 1 );
  520. p += 4;
  521. /* In theory, the certificate list can be up to 2^24 Bytes, but we don't
  522. * support anything beyond 2^16 = 64K.
  523. */
  524. if( ( certificate_request_context_len != 0 ) ||
  525. ( certificate_list_len >= 0x10000 ) )
  526. {
  527. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
  528. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  529. MBEDTLS_ERR_SSL_DECODE_ERROR );
  530. return( MBEDTLS_ERR_SSL_DECODE_ERROR );
  531. }
  532. /* In case we tried to reuse a session but it failed */
  533. if( ssl->session_negotiate->peer_cert != NULL )
  534. {
  535. mbedtls_x509_crt_free( ssl->session_negotiate->peer_cert );
  536. mbedtls_free( ssl->session_negotiate->peer_cert );
  537. }
  538. if( ( ssl->session_negotiate->peer_cert =
  539. mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
  540. {
  541. MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc( %" MBEDTLS_PRINTF_SIZET " bytes ) failed",
  542. sizeof( mbedtls_x509_crt ) ) );
  543. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
  544. MBEDTLS_ERR_SSL_ALLOC_FAILED );
  545. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  546. }
  547. mbedtls_x509_crt_init( ssl->session_negotiate->peer_cert );
  548. certificate_list_end = p + certificate_list_len;
  549. while( p < certificate_list_end )
  550. {
  551. size_t cert_data_len, extensions_len;
  552. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 3 );
  553. cert_data_len = MBEDTLS_GET_UINT24_BE( p, 0 );
  554. p += 3;
  555. /* In theory, the CRT can be up to 2^24 Bytes, but we don't support
  556. * anything beyond 2^16 = 64K. Otherwise as in the TLS 1.2 code,
  557. * check that we have a minimum of 128 bytes of data, this is not
  558. * clear why we need that though.
  559. */
  560. if( ( cert_data_len < 128 ) || ( cert_data_len >= 0x10000 ) )
  561. {
  562. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
  563. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  564. MBEDTLS_ERR_SSL_DECODE_ERROR );
  565. return( MBEDTLS_ERR_SSL_DECODE_ERROR );
  566. }
  567. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, cert_data_len );
  568. ret = mbedtls_x509_crt_parse_der( ssl->session_negotiate->peer_cert,
  569. p, cert_data_len );
  570. switch( ret )
  571. {
  572. case 0: /*ok*/
  573. break;
  574. case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
  575. /* Ignore certificate with an unknown algorithm: maybe a
  576. prior certificate was already trusted. */
  577. break;
  578. case MBEDTLS_ERR_X509_ALLOC_FAILED:
  579. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR,
  580. MBEDTLS_ERR_X509_ALLOC_FAILED );
  581. MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
  582. return( ret );
  583. case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
  584. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT,
  585. MBEDTLS_ERR_X509_UNKNOWN_VERSION );
  586. MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
  587. return( ret );
  588. default:
  589. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT,
  590. ret );
  591. MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
  592. return( ret );
  593. }
  594. p += cert_data_len;
  595. /* Certificate extensions length */
  596. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, 2 );
  597. extensions_len = MBEDTLS_GET_UINT16_BE( p, 0 );
  598. p += 2;
  599. MBEDTLS_SSL_CHK_BUF_READ_PTR( p, certificate_list_end, extensions_len );
  600. p += extensions_len;
  601. }
  602. /* Check that all the message is consumed. */
  603. if( p != end )
  604. {
  605. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad Certificate message" ) );
  606. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR, \
  607. MBEDTLS_ERR_SSL_DECODE_ERROR );
  608. return( MBEDTLS_ERR_SSL_DECODE_ERROR );
  609. }
  610. MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", ssl->session_negotiate->peer_cert );
  611. return( ret );
  612. }
  613. #else
  614. static int ssl_tls13_parse_certificate( mbedtls_ssl_context *ssl,
  615. const unsigned char *buf,
  616. const unsigned char *end )
  617. {
  618. ((void) ssl);
  619. ((void) buf);
  620. ((void) end);
  621. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  622. }
  623. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  624. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  625. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  626. #if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
  627. /* Validate certificate chain sent by the server. */
  628. static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
  629. {
  630. int ret = 0;
  631. mbedtls_x509_crt *ca_chain;
  632. mbedtls_x509_crl *ca_crl;
  633. uint32_t verify_result = 0;
  634. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  635. if( ssl->handshake->sni_ca_chain != NULL )
  636. {
  637. ca_chain = ssl->handshake->sni_ca_chain;
  638. ca_crl = ssl->handshake->sni_ca_crl;
  639. }
  640. else
  641. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  642. {
  643. ca_chain = ssl->conf->ca_chain;
  644. ca_crl = ssl->conf->ca_crl;
  645. }
  646. /*
  647. * Main check: verify certificate
  648. */
  649. ret = mbedtls_x509_crt_verify_with_profile(
  650. ssl->session_negotiate->peer_cert,
  651. ca_chain, ca_crl,
  652. ssl->conf->cert_profile,
  653. ssl->hostname,
  654. &verify_result,
  655. ssl->conf->f_vrfy, ssl->conf->p_vrfy );
  656. if( ret != 0 )
  657. {
  658. MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
  659. }
  660. /*
  661. * Secondary checks: always done, but change 'ret' only if it was 0
  662. */
  663. #if defined(MBEDTLS_ECP_C)
  664. {
  665. const mbedtls_pk_context *pk = &ssl->session_negotiate->peer_cert->pk;
  666. /* If certificate uses an EC key, make sure the curve is OK */
  667. if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
  668. mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
  669. {
  670. verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
  671. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( EC key curve )" ) );
  672. if( ret == 0 )
  673. ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
  674. }
  675. }
  676. #endif /* MBEDTLS_ECP_C */
  677. if( mbedtls_ssl_check_cert_usage( ssl->session_negotiate->peer_cert,
  678. ssl->handshake->ciphersuite_info,
  679. !ssl->conf->endpoint,
  680. &verify_result ) != 0 )
  681. {
  682. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate ( usage extensions )" ) );
  683. if( ret == 0 )
  684. ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
  685. }
  686. if( ca_chain == NULL )
  687. {
  688. MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
  689. ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
  690. }
  691. if( ret != 0 )
  692. {
  693. /* The certificate may have been rejected for several reasons.
  694. Pick one and send the corresponding alert. Which alert to send
  695. may be a subject of debate in some cases. */
  696. if( verify_result & MBEDTLS_X509_BADCERT_OTHER )
  697. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED, ret );
  698. else if( verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
  699. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_BAD_CERT, ret );
  700. else if( verify_result & ( MBEDTLS_X509_BADCERT_KEY_USAGE |
  701. MBEDTLS_X509_BADCERT_EXT_KEY_USAGE |
  702. MBEDTLS_X509_BADCERT_NS_CERT_TYPE |
  703. MBEDTLS_X509_BADCERT_BAD_PK |
  704. MBEDTLS_X509_BADCERT_BAD_KEY ) )
  705. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT, ret );
  706. else if( verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
  707. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED, ret );
  708. else if( verify_result & MBEDTLS_X509_BADCERT_REVOKED )
  709. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED, ret );
  710. else if( verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
  711. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA, ret );
  712. else
  713. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN, ret );
  714. }
  715. #if defined(MBEDTLS_DEBUG_C)
  716. if( verify_result != 0 )
  717. {
  718. MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %08x",
  719. (unsigned int) verify_result ) );
  720. }
  721. else
  722. {
  723. MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
  724. }
  725. #endif /* MBEDTLS_DEBUG_C */
  726. ssl->session_negotiate->verify_result = verify_result;
  727. return( ret );
  728. }
  729. #else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  730. static int ssl_tls13_validate_certificate( mbedtls_ssl_context *ssl )
  731. {
  732. ((void) ssl);
  733. return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
  734. }
  735. #endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
  736. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  737. int mbedtls_ssl_tls13_process_certificate( mbedtls_ssl_context *ssl )
  738. {
  739. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  740. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
  741. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED)
  742. unsigned char *buf;
  743. size_t buf_len;
  744. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls1_3_fetch_handshake_msg(
  745. ssl, MBEDTLS_SSL_HS_CERTIFICATE,
  746. &buf, &buf_len ) );
  747. /* Parse the certificate chain sent by the peer. */
  748. MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_certificate( ssl, buf, buf + buf_len ) );
  749. /* Validate the certificate chain and set the verification results. */
  750. MBEDTLS_SSL_PROC_CHK( ssl_tls13_validate_certificate( ssl ) );
  751. mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_CERTIFICATE,
  752. buf, buf_len );
  753. cleanup:
  754. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
  755. #else
  756. MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
  757. ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
  758. #endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED */
  759. return( ret );
  760. }
  761. /*
  762. *
  763. * STATE HANDLING: Incoming Finished message.
  764. */
  765. /*
  766. * Implementation
  767. */
  768. static int ssl_tls13_preprocess_finished_message( mbedtls_ssl_context *ssl )
  769. {
  770. int ret;
  771. ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
  772. ssl->handshake->state_local.finished_in.digest,
  773. sizeof( ssl->handshake->state_local.finished_in.digest ),
  774. &ssl->handshake->state_local.finished_in.digest_len,
  775. ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ?
  776. MBEDTLS_SSL_IS_SERVER : MBEDTLS_SSL_IS_CLIENT );
  777. if( ret != 0 )
  778. {
  779. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_calculate_verify_data", ret );
  780. return( ret );
  781. }
  782. return( 0 );
  783. }
  784. static int ssl_tls13_parse_finished_message( mbedtls_ssl_context *ssl,
  785. const unsigned char *buf,
  786. const unsigned char *end )
  787. {
  788. /*
  789. * struct {
  790. * opaque verify_data[Hash.length];
  791. * } Finished;
  792. */
  793. const unsigned char *expected_verify_data =
  794. ssl->handshake->state_local.finished_in.digest;
  795. size_t expected_verify_data_len =
  796. ssl->handshake->state_local.finished_in.digest_len;
  797. /* Structural validation */
  798. if( (size_t)( end - buf ) != expected_verify_data_len )
  799. {
  800. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
  801. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
  802. MBEDTLS_ERR_SSL_DECODE_ERROR );
  803. return( MBEDTLS_ERR_SSL_DECODE_ERROR );
  804. }
  805. MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (self-computed):",
  806. expected_verify_data,
  807. expected_verify_data_len );
  808. MBEDTLS_SSL_DEBUG_BUF( 4, "verify_data (received message):", buf,
  809. expected_verify_data_len );
  810. /* Semantic validation */
  811. if( mbedtls_ct_memcmp( buf,
  812. expected_verify_data,
  813. expected_verify_data_len ) != 0 )
  814. {
  815. MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
  816. MBEDTLS_SSL_PEND_FATAL_ALERT( MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR,
  817. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  818. return( MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  819. }
  820. return( 0 );
  821. }
  822. #if defined(MBEDTLS_SSL_CLI_C)
  823. static int ssl_tls13_postprocess_server_finished_message( mbedtls_ssl_context *ssl )
  824. {
  825. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  826. mbedtls_ssl_key_set traffic_keys;
  827. mbedtls_ssl_transform *transform_application = NULL;
  828. ret = mbedtls_ssl_tls13_key_schedule_stage_application( ssl );
  829. if( ret != 0 )
  830. {
  831. MBEDTLS_SSL_DEBUG_RET( 1,
  832. "mbedtls_ssl_tls13_key_schedule_stage_application", ret );
  833. goto cleanup;
  834. }
  835. ret = mbedtls_ssl_tls13_generate_application_keys( ssl, &traffic_keys );
  836. if( ret != 0 )
  837. {
  838. MBEDTLS_SSL_DEBUG_RET( 1,
  839. "mbedtls_ssl_tls13_generate_application_keys", ret );
  840. goto cleanup;
  841. }
  842. transform_application =
  843. mbedtls_calloc( 1, sizeof( mbedtls_ssl_transform ) );
  844. if( transform_application == NULL )
  845. {
  846. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  847. goto cleanup;
  848. }
  849. ret = mbedtls_ssl_tls13_populate_transform(
  850. transform_application,
  851. ssl->conf->endpoint,
  852. ssl->session_negotiate->ciphersuite,
  853. &traffic_keys,
  854. ssl );
  855. if( ret != 0 )
  856. {
  857. MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_tls13_populate_transform", ret );
  858. goto cleanup;
  859. }
  860. ssl->transform_application = transform_application;
  861. cleanup:
  862. mbedtls_platform_zeroize( &traffic_keys, sizeof( traffic_keys ) );
  863. if( ret != 0 )
  864. {
  865. mbedtls_free( transform_application );
  866. MBEDTLS_SSL_PEND_FATAL_ALERT(
  867. MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
  868. MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE );
  869. }
  870. return( ret );
  871. }
  872. #endif /* MBEDTLS_SSL_CLI_C */
  873. static int ssl_tls13_postprocess_finished_message( mbedtls_ssl_context *ssl )
  874. {
  875. #if defined(MBEDTLS_SSL_CLI_C)
  876. if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
  877. {
  878. return( ssl_tls13_postprocess_server_finished_message( ssl ) );
  879. }
  880. #else
  881. ((void) ssl);
  882. #endif /* MBEDTLS_SSL_CLI_C */
  883. return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
  884. }
  885. int mbedtls_ssl_tls13_process_finished_message( mbedtls_ssl_context *ssl )
  886. {
  887. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  888. unsigned char *buf;
  889. size_t buflen;
  890. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished message" ) );
  891. /* Preprocessing step: Compute handshake digest */
  892. MBEDTLS_SSL_PROC_CHK( ssl_tls13_preprocess_finished_message( ssl ) );
  893. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls1_3_fetch_handshake_msg( ssl,
  894. MBEDTLS_SSL_HS_FINISHED,
  895. &buf, &buflen ) );
  896. MBEDTLS_SSL_PROC_CHK( ssl_tls13_parse_finished_message( ssl, buf, buf + buflen ) );
  897. mbedtls_ssl_tls1_3_add_hs_msg_to_checksum(
  898. ssl, MBEDTLS_SSL_HS_FINISHED, buf, buflen );
  899. MBEDTLS_SSL_PROC_CHK( ssl_tls13_postprocess_finished_message( ssl ) );
  900. cleanup:
  901. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished message" ) );
  902. return( ret );
  903. }
  904. /*
  905. *
  906. * STATE HANDLING: Write and send Finished message.
  907. *
  908. */
  909. /*
  910. * Implement
  911. */
  912. static int ssl_tls13_prepare_finished_message( mbedtls_ssl_context *ssl )
  913. {
  914. int ret;
  915. /* Compute transcript of handshake up to now. */
  916. ret = mbedtls_ssl_tls13_calculate_verify_data( ssl,
  917. ssl->handshake->state_local.finished_out.digest,
  918. sizeof( ssl->handshake->state_local.finished_out.digest ),
  919. &ssl->handshake->state_local.finished_out.digest_len,
  920. ssl->conf->endpoint );
  921. if( ret != 0 )
  922. {
  923. MBEDTLS_SSL_DEBUG_RET( 1, "calculate_verify_data failed", ret );
  924. return( ret );
  925. }
  926. return( 0 );
  927. }
  928. static int ssl_tls13_finalize_finished_message( mbedtls_ssl_context *ssl )
  929. {
  930. // TODO: Add back resumption keys calculation after MVP.
  931. ((void) ssl);
  932. return( 0 );
  933. }
  934. static int ssl_tls13_write_finished_message_body( mbedtls_ssl_context *ssl,
  935. unsigned char *buf,
  936. unsigned char *end,
  937. size_t *olen )
  938. {
  939. size_t verify_data_len = ssl->handshake->state_local.finished_out.digest_len;
  940. /*
  941. * struct {
  942. * opaque verify_data[Hash.length];
  943. * } Finished;
  944. */
  945. MBEDTLS_SSL_CHK_BUF_PTR( buf, end, verify_data_len );
  946. memcpy( buf, ssl->handshake->state_local.finished_out.digest,
  947. verify_data_len );
  948. *olen = verify_data_len;
  949. return( 0 );
  950. }
  951. /* Main entry point: orchestrates the other functions */
  952. int mbedtls_ssl_tls13_write_finished_message( mbedtls_ssl_context *ssl )
  953. {
  954. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  955. unsigned char *buf;
  956. size_t buf_len, msg_len;
  957. MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished message" ) );
  958. MBEDTLS_SSL_PROC_CHK( ssl_tls13_prepare_finished_message( ssl ) );
  959. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_start_handshake_msg( ssl,
  960. MBEDTLS_SSL_HS_FINISHED, &buf, &buf_len ) );
  961. MBEDTLS_SSL_PROC_CHK( ssl_tls13_write_finished_message_body(
  962. ssl, buf, buf + buf_len, &msg_len ) );
  963. mbedtls_ssl_tls1_3_add_hs_msg_to_checksum( ssl, MBEDTLS_SSL_HS_FINISHED,
  964. buf, msg_len );
  965. MBEDTLS_SSL_PROC_CHK( ssl_tls13_finalize_finished_message( ssl ) );
  966. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_tls13_finish_handshake_msg( ssl,
  967. buf_len, msg_len ) );
  968. MBEDTLS_SSL_PROC_CHK( mbedtls_ssl_flush_output( ssl ) );
  969. cleanup:
  970. MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished message" ) );
  971. return( ret );
  972. }
  973. void mbedtls_ssl_tls13_handshake_wrapup( mbedtls_ssl_context *ssl )
  974. {
  975. MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
  976. /*
  977. * Free the previous session and switch to the current one.
  978. */
  979. if( ssl->session )
  980. {
  981. mbedtls_ssl_session_free( ssl->session );
  982. mbedtls_free( ssl->session );
  983. }
  984. ssl->session = ssl->session_negotiate;
  985. ssl->session_negotiate = NULL;
  986. MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
  987. }
  988. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  989. #endif /* MBEDTLS_SSL_TLS_C */