ssl_internal.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /**
  2. * \file ssl_internal.h
  3. *
  4. * \brief Internal functions shared by the SSL modules
  5. */
  6. /*
  7. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  8. * SPDX-License-Identifier: Apache-2.0
  9. *
  10. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  11. * not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  18. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. *
  22. * This file is part of mbed TLS (https://tls.mbed.org)
  23. */
  24. #ifndef MBEDTLS_SSL_INTERNAL_H
  25. #define MBEDTLS_SSL_INTERNAL_H
  26. #include "ssl.h"
  27. #include "cipher.h"
  28. #if defined(MBEDTLS_MD5_C)
  29. #include "md5.h"
  30. #endif
  31. #if defined(MBEDTLS_SHA1_C)
  32. #include "sha1.h"
  33. #endif
  34. #if defined(MBEDTLS_SHA256_C)
  35. #include "sha256.h"
  36. #endif
  37. #if defined(MBEDTLS_SHA512_C)
  38. #include "sha512.h"
  39. #endif
  40. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  41. #include "ecjpake.h"
  42. #endif
  43. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  44. !defined(inline) && !defined(__cplusplus)
  45. #define inline __inline
  46. #endif
  47. /* Determine minimum supported version */
  48. #define MBEDTLS_SSL_MIN_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
  49. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  50. #define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
  51. #else
  52. #if defined(MBEDTLS_SSL_PROTO_TLS1)
  53. #define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
  54. #else
  55. #if defined(MBEDTLS_SSL_PROTO_TLS1_1)
  56. #define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
  57. #else
  58. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  59. #define MBEDTLS_SSL_MIN_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
  60. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  61. #endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
  62. #endif /* MBEDTLS_SSL_PROTO_TLS1 */
  63. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  64. #define MBEDTLS_SSL_MIN_VALID_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
  65. #define MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
  66. /* Determine maximum supported version */
  67. #define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
  68. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  69. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
  70. #else
  71. #if defined(MBEDTLS_SSL_PROTO_TLS1_1)
  72. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
  73. #else
  74. #if defined(MBEDTLS_SSL_PROTO_TLS1)
  75. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
  76. #else
  77. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  78. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
  79. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  80. #endif /* MBEDTLS_SSL_PROTO_TLS1 */
  81. #endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
  82. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  83. #define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
  84. #define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
  85. #define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
  86. #define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
  87. /*
  88. * DTLS retransmission states, see RFC 6347 4.2.4
  89. *
  90. * The SENDING state is merged in PREPARING for initial sends,
  91. * but is distinct for resends.
  92. *
  93. * Note: initial state is wrong for server, but is not used anyway.
  94. */
  95. #define MBEDTLS_SSL_RETRANS_PREPARING 0
  96. #define MBEDTLS_SSL_RETRANS_SENDING 1
  97. #define MBEDTLS_SSL_RETRANS_WAITING 2
  98. #define MBEDTLS_SSL_RETRANS_FINISHED 3
  99. /*
  100. * Allow extra bytes for record, authentication and encryption overhead:
  101. * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
  102. * and allow for a maximum of 1024 of compression expansion if
  103. * enabled.
  104. */
  105. #if defined(MBEDTLS_ZLIB_SUPPORT)
  106. #define MBEDTLS_SSL_COMPRESSION_ADD 1024
  107. #else
  108. #define MBEDTLS_SSL_COMPRESSION_ADD 0
  109. #endif
  110. #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_MODE_CBC)
  111. /* Ciphersuites using HMAC */
  112. #if defined(MBEDTLS_SHA512_C)
  113. #define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
  114. #elif defined(MBEDTLS_SHA256_C)
  115. #define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
  116. #else
  117. #define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
  118. #endif
  119. #else
  120. /* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
  121. #define MBEDTLS_SSL_MAC_ADD 16
  122. #endif
  123. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  124. #define MBEDTLS_SSL_PADDING_ADD 256
  125. #else
  126. #define MBEDTLS_SSL_PADDING_ADD 0
  127. #endif
  128. #define MBEDTLS_SSL_PAYLOAD_OVERHEAD ( MBEDTLS_SSL_COMPRESSION_ADD + \
  129. MBEDTLS_MAX_IV_LENGTH + \
  130. MBEDTLS_SSL_MAC_ADD + \
  131. MBEDTLS_SSL_PADDING_ADD \
  132. )
  133. #define MBEDTLS_SSL_IN_PAYLOAD_LEN ( MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
  134. ( MBEDTLS_SSL_IN_CONTENT_LEN ) )
  135. #define MBEDTLS_SSL_OUT_PAYLOAD_LEN ( MBEDTLS_SSL_PAYLOAD_OVERHEAD + \
  136. ( MBEDTLS_SSL_OUT_CONTENT_LEN ) )
  137. /* Maximum length we can advertise as our max content length for
  138. RFC 6066 max_fragment_length extension negotiation purposes
  139. (the lesser of both sizes, if they are unequal.)
  140. */
  141. #define MBEDTLS_TLS_EXT_ADV_CONTENT_LEN ( \
  142. (MBEDTLS_SSL_IN_CONTENT_LEN > MBEDTLS_SSL_OUT_CONTENT_LEN) \
  143. ? ( MBEDTLS_SSL_OUT_CONTENT_LEN ) \
  144. : ( MBEDTLS_SSL_IN_CONTENT_LEN ) \
  145. )
  146. /*
  147. * Check that we obey the standard's message size bounds
  148. */
  149. #if MBEDTLS_SSL_MAX_CONTENT_LEN > 16384
  150. #error "Bad configuration - record content too large."
  151. #endif
  152. #if MBEDTLS_SSL_IN_CONTENT_LEN > MBEDTLS_SSL_MAX_CONTENT_LEN
  153. #error "Bad configuration - incoming record content should not be larger than MBEDTLS_SSL_MAX_CONTENT_LEN."
  154. #endif
  155. #if MBEDTLS_SSL_OUT_CONTENT_LEN > MBEDTLS_SSL_MAX_CONTENT_LEN
  156. #error "Bad configuration - outgoing record content should not be larger than MBEDTLS_SSL_MAX_CONTENT_LEN."
  157. #endif
  158. #if MBEDTLS_SSL_IN_PAYLOAD_LEN > MBEDTLS_SSL_MAX_CONTENT_LEN + 2048
  159. #error "Bad configuration - incoming protected record payload too large."
  160. #endif
  161. #if MBEDTLS_SSL_OUT_PAYLOAD_LEN > MBEDTLS_SSL_MAX_CONTENT_LEN + 2048
  162. #error "Bad configuration - outgoing protected record payload too large."
  163. #endif
  164. /* Calculate buffer sizes */
  165. /* Note: Even though the TLS record header is only 5 bytes
  166. long, we're internally using 8 bytes to store the
  167. implicit sequence number. */
  168. #define MBEDTLS_SSL_HEADER_LEN 13
  169. #define MBEDTLS_SSL_IN_BUFFER_LEN \
  170. ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_IN_PAYLOAD_LEN ) )
  171. #define MBEDTLS_SSL_OUT_BUFFER_LEN \
  172. ( ( MBEDTLS_SSL_HEADER_LEN ) + ( MBEDTLS_SSL_OUT_PAYLOAD_LEN ) )
  173. #ifdef MBEDTLS_ZLIB_SUPPORT
  174. /* Compression buffer holds both IN and OUT buffers, so should be size of the larger */
  175. #define MBEDTLS_SSL_COMPRESS_BUFFER_LEN ( \
  176. ( MBEDTLS_SSL_IN_BUFFER_LEN > MBEDTLS_SSL_OUT_BUFFER_LEN ) \
  177. ? MBEDTLS_SSL_IN_BUFFER_LEN \
  178. : MBEDTLS_SSL_OUT_BUFFER_LEN \
  179. )
  180. #endif
  181. /*
  182. * TLS extension flags (for extensions with outgoing ServerHello content
  183. * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
  184. * of state of the renegotiation flag, so no indicator is required)
  185. */
  186. #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
  187. #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
  188. #ifdef __cplusplus
  189. extern "C" {
  190. #endif
  191. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  192. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  193. /*
  194. * Abstraction for a grid of allowed signature-hash-algorithm pairs.
  195. */
  196. struct mbedtls_ssl_sig_hash_set_t
  197. {
  198. /* At the moment, we only need to remember a single suitable
  199. * hash algorithm per signature algorithm. As long as that's
  200. * the case - and we don't need a general lookup function -
  201. * we can implement the sig-hash-set as a map from signatures
  202. * to hash algorithms. */
  203. mbedtls_md_type_t rsa;
  204. mbedtls_md_type_t ecdsa;
  205. };
  206. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 &&
  207. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  208. /*
  209. * This structure contains the parameters only needed during handshake.
  210. */
  211. struct mbedtls_ssl_handshake_params
  212. {
  213. /*
  214. * Handshake specific crypto variables
  215. */
  216. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  217. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  218. mbedtls_ssl_sig_hash_set_t hash_algs; /*!< Set of suitable sig-hash pairs */
  219. #endif
  220. #if defined(MBEDTLS_DHM_C)
  221. mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
  222. #endif
  223. #if defined(MBEDTLS_ECDH_C)
  224. mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
  225. #endif
  226. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  227. mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
  228. #if defined(MBEDTLS_SSL_CLI_C)
  229. unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
  230. size_t ecjpake_cache_len; /*!< Length of cached data */
  231. #endif
  232. #endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  233. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  234. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  235. const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
  236. #endif
  237. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  238. unsigned char *psk; /*!< PSK from the callback */
  239. size_t psk_len; /*!< Length of PSK from callback */
  240. #endif
  241. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  242. mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
  243. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  244. int sni_authmode; /*!< authmode from SNI callback */
  245. mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
  246. mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
  247. mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
  248. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  249. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  250. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  251. unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
  252. unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
  253. unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
  254. Srv: unused */
  255. unsigned char verify_cookie_len; /*!< Cli: cookie length
  256. Srv: flag for sending a cookie */
  257. unsigned char *hs_msg; /*!< Reassembled handshake message */
  258. uint32_t retransmit_timeout; /*!< Current value of timeout */
  259. unsigned char retransmit_state; /*!< Retransmission state */
  260. mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
  261. mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
  262. unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
  263. flight being received */
  264. mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
  265. resending messages */
  266. unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
  267. for resending messages */
  268. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  269. /*
  270. * Checksum contexts
  271. */
  272. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  273. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  274. mbedtls_md5_context fin_md5;
  275. mbedtls_sha1_context fin_sha1;
  276. #endif
  277. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  278. #if defined(MBEDTLS_SHA256_C)
  279. mbedtls_sha256_context fin_sha256;
  280. #endif
  281. #if defined(MBEDTLS_SHA512_C)
  282. mbedtls_sha512_context fin_sha512;
  283. #endif
  284. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  285. void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
  286. void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
  287. void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
  288. int (*tls_prf)(const unsigned char *, size_t, const char *,
  289. const unsigned char *, size_t,
  290. unsigned char *, size_t);
  291. size_t pmslen; /*!< premaster length */
  292. unsigned char randbytes[64]; /*!< random bytes */
  293. unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
  294. /*!< premaster secret */
  295. int resume; /*!< session resume indicator*/
  296. int max_major_ver; /*!< max. major version client*/
  297. int max_minor_ver; /*!< max. minor version client*/
  298. int cli_exts; /*!< client extension presence*/
  299. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  300. int new_session_ticket; /*!< use NewSessionTicket? */
  301. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  302. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  303. int extended_ms; /*!< use Extended Master Secret? */
  304. #endif
  305. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  306. unsigned int async_in_progress : 1; /*!< an asynchronous operation is in progress */
  307. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  308. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  309. /** Asynchronous operation context. This field is meant for use by the
  310. * asynchronous operation callbacks (mbedtls_ssl_config::f_async_sign_start,
  311. * mbedtls_ssl_config::f_async_decrypt_start,
  312. * mbedtls_ssl_config::f_async_resume, mbedtls_ssl_config::f_async_cancel).
  313. * The library does not use it internally. */
  314. void *user_async_ctx;
  315. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  316. };
  317. /*
  318. * This structure contains a full set of runtime transform parameters
  319. * either in negotiation or active.
  320. */
  321. struct mbedtls_ssl_transform
  322. {
  323. /*
  324. * Session specific crypto layer
  325. */
  326. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  327. /*!< Chosen cipersuite_info */
  328. unsigned int keylen; /*!< symmetric key length (bytes) */
  329. size_t minlen; /*!< min. ciphertext length */
  330. size_t ivlen; /*!< IV length */
  331. size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
  332. size_t maclen; /*!< MAC length */
  333. unsigned char iv_enc[16]; /*!< IV (encryption) */
  334. unsigned char iv_dec[16]; /*!< IV (decryption) */
  335. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  336. /* Needed only for SSL v3.0 secret */
  337. unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
  338. unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
  339. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  340. mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
  341. mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
  342. mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
  343. mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
  344. /*
  345. * Session specific compression layer
  346. */
  347. #if defined(MBEDTLS_ZLIB_SUPPORT)
  348. z_stream ctx_deflate; /*!< compression context */
  349. z_stream ctx_inflate; /*!< decompression context */
  350. #endif
  351. };
  352. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  353. /*
  354. * List of certificate + private key pairs
  355. */
  356. struct mbedtls_ssl_key_cert
  357. {
  358. mbedtls_x509_crt *cert; /*!< cert */
  359. mbedtls_pk_context *key; /*!< private key */
  360. mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
  361. };
  362. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  363. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  364. /*
  365. * List of handshake messages kept around for resending
  366. */
  367. struct mbedtls_ssl_flight_item
  368. {
  369. unsigned char *p; /*!< message, including handshake headers */
  370. size_t len; /*!< length of p */
  371. unsigned char type; /*!< type of the message: handshake or CCS */
  372. mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
  373. };
  374. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  375. #if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
  376. defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  377. /* Find an entry in a signature-hash set matching a given hash algorithm. */
  378. mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
  379. mbedtls_pk_type_t sig_alg );
  380. /* Add a signature-hash-pair to a signature-hash set */
  381. void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
  382. mbedtls_pk_type_t sig_alg,
  383. mbedtls_md_type_t md_alg );
  384. /* Allow exactly one hash algorithm for each signature. */
  385. void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
  386. mbedtls_md_type_t md_alg );
  387. /* Setup an empty signature-hash set */
  388. static inline void mbedtls_ssl_sig_hash_set_init( mbedtls_ssl_sig_hash_set_t *set )
  389. {
  390. mbedtls_ssl_sig_hash_set_const_hash( set, MBEDTLS_MD_NONE );
  391. }
  392. #endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
  393. MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  394. /**
  395. * \brief Free referenced items in an SSL transform context and clear
  396. * memory
  397. *
  398. * \param transform SSL transform context
  399. */
  400. void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
  401. /**
  402. * \brief Free referenced items in an SSL handshake context and clear
  403. * memory
  404. *
  405. * \param ssl SSL context
  406. */
  407. void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl );
  408. int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
  409. int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
  410. void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
  411. int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
  412. void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
  413. int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
  414. int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl );
  415. int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
  416. int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
  417. void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
  418. /**
  419. * \brief Update record layer
  420. *
  421. * This function roughly separates the implementation
  422. * of the logic of (D)TLS from the implementation
  423. * of the secure transport.
  424. *
  425. * \param ssl SSL context to use
  426. *
  427. * \return 0 or non-zero error code.
  428. *
  429. * \note A clarification on what is called 'record layer' here
  430. * is in order, as many sensible definitions are possible:
  431. *
  432. * The record layer takes as input an untrusted underlying
  433. * transport (stream or datagram) and transforms it into
  434. * a serially multiplexed, secure transport, which
  435. * conceptually provides the following:
  436. *
  437. * (1) Three datagram based, content-agnostic transports
  438. * for handshake, alert and CCS messages.
  439. * (2) One stream- or datagram-based transport
  440. * for application data.
  441. * (3) Functionality for changing the underlying transform
  442. * securing the contents.
  443. *
  444. * The interface to this functionality is given as follows:
  445. *
  446. * a Updating
  447. * [Currently implemented by mbedtls_ssl_read_record]
  448. *
  449. * Check if and on which of the four 'ports' data is pending:
  450. * Nothing, a controlling datagram of type (1), or application
  451. * data (2). In any case data is present, internal buffers
  452. * provide access to the data for the user to process it.
  453. * Consumption of type (1) datagrams is done automatically
  454. * on the next update, invalidating that the internal buffers
  455. * for previous datagrams, while consumption of application
  456. * data (2) is user-controlled.
  457. *
  458. * b Reading of application data
  459. * [Currently manual adaption of ssl->in_offt pointer]
  460. *
  461. * As mentioned in the last paragraph, consumption of data
  462. * is different from the automatic consumption of control
  463. * datagrams (1) because application data is treated as a stream.
  464. *
  465. * c Tracking availability of application data
  466. * [Currently manually through decreasing ssl->in_msglen]
  467. *
  468. * For efficiency and to retain datagram semantics for
  469. * application data in case of DTLS, the record layer
  470. * provides functionality for checking how much application
  471. * data is still available in the internal buffer.
  472. *
  473. * d Changing the transformation securing the communication.
  474. *
  475. * Given an opaque implementation of the record layer in the
  476. * above sense, it should be possible to implement the logic
  477. * of (D)TLS on top of it without the need to know anything
  478. * about the record layer's internals. This is done e.g.
  479. * in all the handshake handling functions, and in the
  480. * application data reading function mbedtls_ssl_read.
  481. *
  482. * \note The above tries to give a conceptual picture of the
  483. * record layer, but the current implementation deviates
  484. * from it in some places. For example, our implementation of
  485. * the update functionality through mbedtls_ssl_read_record
  486. * discards datagrams depending on the current state, which
  487. * wouldn't fall under the record layer's responsibility
  488. * following the above definition.
  489. *
  490. */
  491. int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
  492. int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
  493. int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
  494. int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
  495. int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
  496. int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
  497. int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
  498. int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
  499. int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
  500. int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
  501. void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
  502. const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
  503. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  504. int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
  505. #endif
  506. #if defined(MBEDTLS_PK_C)
  507. unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
  508. unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type );
  509. mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
  510. #endif
  511. mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
  512. unsigned char mbedtls_ssl_hash_from_md_alg( int md );
  513. int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
  514. #if defined(MBEDTLS_ECP_C)
  515. int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
  516. #endif
  517. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  518. int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
  519. mbedtls_md_type_t md );
  520. #endif
  521. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  522. static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
  523. {
  524. mbedtls_ssl_key_cert *key_cert;
  525. if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
  526. key_cert = ssl->handshake->key_cert;
  527. else
  528. key_cert = ssl->conf->key_cert;
  529. return( key_cert == NULL ? NULL : key_cert->key );
  530. }
  531. static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
  532. {
  533. mbedtls_ssl_key_cert *key_cert;
  534. if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
  535. key_cert = ssl->handshake->key_cert;
  536. else
  537. key_cert = ssl->conf->key_cert;
  538. return( key_cert == NULL ? NULL : key_cert->cert );
  539. }
  540. /*
  541. * Check usage of a certificate wrt extensions:
  542. * keyUsage, extendedKeyUsage (later), and nSCertType (later).
  543. *
  544. * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
  545. * check a cert we received from them)!
  546. *
  547. * Return 0 if everything is OK, -1 if not.
  548. */
  549. int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
  550. const mbedtls_ssl_ciphersuite_t *ciphersuite,
  551. int cert_endpoint,
  552. uint32_t *flags );
  553. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  554. void mbedtls_ssl_write_version( int major, int minor, int transport,
  555. unsigned char ver[2] );
  556. void mbedtls_ssl_read_version( int *major, int *minor, int transport,
  557. const unsigned char ver[2] );
  558. static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
  559. {
  560. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  561. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  562. return( 13 );
  563. #else
  564. ((void) ssl);
  565. #endif
  566. return( 5 );
  567. }
  568. static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
  569. {
  570. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  571. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  572. return( 12 );
  573. #else
  574. ((void) ssl);
  575. #endif
  576. return( 4 );
  577. }
  578. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  579. void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
  580. void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
  581. int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
  582. #endif
  583. /* Visible for testing purposes only */
  584. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  585. int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
  586. void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
  587. #endif
  588. /* constant-time buffer comparison */
  589. static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
  590. {
  591. size_t i;
  592. volatile const unsigned char *A = (volatile const unsigned char *) a;
  593. volatile const unsigned char *B = (volatile const unsigned char *) b;
  594. volatile unsigned char diff = 0;
  595. for( i = 0; i < n; i++ )
  596. {
  597. /* Read volatile data in order before computing diff.
  598. * This avoids IAR compiler warning:
  599. * 'the order of volatile accesses is undefined ..' */
  600. unsigned char x = A[i], y = B[i];
  601. diff |= x ^ y;
  602. }
  603. return( diff );
  604. }
  605. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  606. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  607. int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
  608. unsigned char *output,
  609. unsigned char *data, size_t data_len );
  610. #endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
  611. MBEDTLS_SSL_PROTO_TLS1_1 */
  612. #if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
  613. defined(MBEDTLS_SSL_PROTO_TLS1_2)
  614. int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
  615. unsigned char *hash, size_t *hashlen,
  616. unsigned char *data, size_t data_len,
  617. mbedtls_md_type_t md_alg );
  618. #endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
  619. MBEDTLS_SSL_PROTO_TLS1_2 */
  620. #ifdef __cplusplus
  621. }
  622. #endif
  623. #endif /* ssl_internal.h */