ssl_internal.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /**
  2. * \file ssl_ticket.h
  3. *
  4. * \brief Internal functions shared by the SSL modules
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_SSL_INTERNAL_H
  26. #define MBEDTLS_SSL_INTERNAL_H
  27. #include "ssl.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. /* Determine maximum supported version */
  65. #define MBEDTLS_SSL_MAX_MAJOR_VERSION MBEDTLS_SSL_MAJOR_VERSION_3
  66. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  67. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_3
  68. #else
  69. #if defined(MBEDTLS_SSL_PROTO_TLS1_1)
  70. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_2
  71. #else
  72. #if defined(MBEDTLS_SSL_PROTO_TLS1)
  73. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_1
  74. #else
  75. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  76. #define MBEDTLS_SSL_MAX_MINOR_VERSION MBEDTLS_SSL_MINOR_VERSION_0
  77. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  78. #endif /* MBEDTLS_SSL_PROTO_TLS1 */
  79. #endif /* MBEDTLS_SSL_PROTO_TLS1_1 */
  80. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  81. #define MBEDTLS_SSL_INITIAL_HANDSHAKE 0
  82. #define MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS 1 /* In progress */
  83. #define MBEDTLS_SSL_RENEGOTIATION_DONE 2 /* Done or aborted */
  84. #define MBEDTLS_SSL_RENEGOTIATION_PENDING 3 /* Requested (server only) */
  85. /*
  86. * DTLS retransmission states, see RFC 6347 4.2.4
  87. *
  88. * The SENDING state is merged in PREPARING for initial sends,
  89. * but is distinct for resends.
  90. *
  91. * Note: initial state is wrong for server, but is not used anyway.
  92. */
  93. #define MBEDTLS_SSL_RETRANS_PREPARING 0
  94. #define MBEDTLS_SSL_RETRANS_SENDING 1
  95. #define MBEDTLS_SSL_RETRANS_WAITING 2
  96. #define MBEDTLS_SSL_RETRANS_FINISHED 3
  97. /*
  98. * Allow extra bytes for record, authentication and encryption overhead:
  99. * counter (8) + header (5) + IV(16) + MAC (16-48) + padding (0-256)
  100. * and allow for a maximum of 1024 of compression expansion if
  101. * enabled.
  102. */
  103. #if defined(MBEDTLS_ZLIB_SUPPORT)
  104. #define MBEDTLS_SSL_COMPRESSION_ADD 1024
  105. #else
  106. #define MBEDTLS_SSL_COMPRESSION_ADD 0
  107. #endif
  108. #if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_MODE_CBC)
  109. /* Ciphersuites using HMAC */
  110. #if defined(MBEDTLS_SHA512_C)
  111. #define MBEDTLS_SSL_MAC_ADD 48 /* SHA-384 used for HMAC */
  112. #elif defined(MBEDTLS_SHA256_C)
  113. #define MBEDTLS_SSL_MAC_ADD 32 /* SHA-256 used for HMAC */
  114. #else
  115. #define MBEDTLS_SSL_MAC_ADD 20 /* SHA-1 used for HMAC */
  116. #endif
  117. #else
  118. /* AEAD ciphersuites: GCM and CCM use a 128 bits tag */
  119. #define MBEDTLS_SSL_MAC_ADD 16
  120. #endif
  121. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  122. #define MBEDTLS_SSL_PADDING_ADD 256
  123. #else
  124. #define MBEDTLS_SSL_PADDING_ADD 0
  125. #endif
  126. #define MBEDTLS_SSL_BUFFER_LEN ( MBEDTLS_SSL_MAX_CONTENT_LEN \
  127. + MBEDTLS_SSL_COMPRESSION_ADD \
  128. + 29 /* counter + header + IV */ \
  129. + MBEDTLS_SSL_MAC_ADD \
  130. + MBEDTLS_SSL_PADDING_ADD \
  131. )
  132. /*
  133. * TLS extension flags (for extensions with outgoing ServerHello content
  134. * that need it (e.g. for RENEGOTIATION_INFO the server already knows because
  135. * of state of the renegotiation flag, so no indicator is required)
  136. */
  137. #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS_PRESENT (1 << 0)
  138. #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP_OK (1 << 1)
  139. #ifdef __cplusplus
  140. extern "C" {
  141. #endif
  142. /*
  143. * This structure contains the parameters only needed during handshake.
  144. */
  145. struct mbedtls_ssl_handshake_params
  146. {
  147. /*
  148. * Handshake specific crypto variables
  149. */
  150. int sig_alg; /*!< Hash algorithm for signature */
  151. int verify_sig_alg; /*!< Signature algorithm for verify */
  152. #if defined(MBEDTLS_DHM_C)
  153. mbedtls_dhm_context dhm_ctx; /*!< DHM key exchange */
  154. #endif
  155. #if defined(MBEDTLS_ECDH_C)
  156. mbedtls_ecdh_context ecdh_ctx; /*!< ECDH key exchange */
  157. #endif
  158. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  159. mbedtls_ecjpake_context ecjpake_ctx; /*!< EC J-PAKE key exchange */
  160. #if defined(MBEDTLS_SSL_CLI_C)
  161. unsigned char *ecjpake_cache; /*!< Cache for ClientHello ext */
  162. size_t ecjpake_cache_len; /*!< Length of cached data */
  163. #endif
  164. #endif
  165. #if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
  166. defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  167. const mbedtls_ecp_curve_info **curves; /*!< Supported elliptic curves */
  168. #endif
  169. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  170. unsigned char *psk; /*!< PSK from the callback */
  171. size_t psk_len; /*!< Length of PSK from callback */
  172. #endif
  173. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  174. mbedtls_ssl_key_cert *key_cert; /*!< chosen key/cert pair (server) */
  175. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  176. int sni_authmode; /*!< authmode from SNI callback */
  177. mbedtls_ssl_key_cert *sni_key_cert; /*!< key/cert list from SNI */
  178. mbedtls_x509_crt *sni_ca_chain; /*!< trusted CAs from SNI callback */
  179. mbedtls_x509_crl *sni_ca_crl; /*!< trusted CAs CRLs from SNI */
  180. #endif
  181. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  182. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  183. unsigned int out_msg_seq; /*!< Outgoing handshake sequence number */
  184. unsigned int in_msg_seq; /*!< Incoming handshake sequence number */
  185. unsigned char *verify_cookie; /*!< Cli: HelloVerifyRequest cookie
  186. Srv: unused */
  187. unsigned char verify_cookie_len; /*!< Cli: cookie length
  188. Srv: flag for sending a cookie */
  189. unsigned char *hs_msg; /*!< Reassembled handshake message */
  190. uint32_t retransmit_timeout; /*!< Current value of timeout */
  191. unsigned char retransmit_state; /*!< Retransmission state */
  192. mbedtls_ssl_flight_item *flight; /*!< Current outgoing flight */
  193. mbedtls_ssl_flight_item *cur_msg; /*!< Current message in flight */
  194. unsigned int in_flight_start_seq; /*!< Minimum message sequence in the
  195. flight being received */
  196. mbedtls_ssl_transform *alt_transform_out; /*!< Alternative transform for
  197. resending messages */
  198. unsigned char alt_out_ctr[8]; /*!< Alternative record epoch/counter
  199. for resending messages */
  200. #endif
  201. /*
  202. * Checksum contexts
  203. */
  204. #if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
  205. defined(MBEDTLS_SSL_PROTO_TLS1_1)
  206. mbedtls_md5_context fin_md5;
  207. mbedtls_sha1_context fin_sha1;
  208. #endif
  209. #if defined(MBEDTLS_SSL_PROTO_TLS1_2)
  210. #if defined(MBEDTLS_SHA256_C)
  211. mbedtls_sha256_context fin_sha256;
  212. #endif
  213. #if defined(MBEDTLS_SHA512_C)
  214. mbedtls_sha512_context fin_sha512;
  215. #endif
  216. #endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
  217. void (*update_checksum)(mbedtls_ssl_context *, const unsigned char *, size_t);
  218. void (*calc_verify)(mbedtls_ssl_context *, unsigned char *);
  219. void (*calc_finished)(mbedtls_ssl_context *, unsigned char *, int);
  220. int (*tls_prf)(const unsigned char *, size_t, const char *,
  221. const unsigned char *, size_t,
  222. unsigned char *, size_t);
  223. size_t pmslen; /*!< premaster length */
  224. unsigned char randbytes[64]; /*!< random bytes */
  225. unsigned char premaster[MBEDTLS_PREMASTER_SIZE];
  226. /*!< premaster secret */
  227. int resume; /*!< session resume indicator*/
  228. int max_major_ver; /*!< max. major version client*/
  229. int max_minor_ver; /*!< max. minor version client*/
  230. int cli_exts; /*!< client extension presence*/
  231. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  232. int new_session_ticket; /*!< use NewSessionTicket? */
  233. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  234. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  235. int extended_ms; /*!< use Extended Master Secret? */
  236. #endif
  237. };
  238. /*
  239. * This structure contains a full set of runtime transform parameters
  240. * either in negotiation or active.
  241. */
  242. struct mbedtls_ssl_transform
  243. {
  244. /*
  245. * Session specific crypto layer
  246. */
  247. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  248. /*!< Chosen cipersuite_info */
  249. unsigned int keylen; /*!< symmetric key length (bytes) */
  250. size_t minlen; /*!< min. ciphertext length */
  251. size_t ivlen; /*!< IV length */
  252. size_t fixed_ivlen; /*!< Fixed part of IV (AEAD) */
  253. size_t maclen; /*!< MAC length */
  254. unsigned char iv_enc[16]; /*!< IV (encryption) */
  255. unsigned char iv_dec[16]; /*!< IV (decryption) */
  256. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  257. /* Needed only for SSL v3.0 secret */
  258. unsigned char mac_enc[20]; /*!< SSL v3.0 secret (enc) */
  259. unsigned char mac_dec[20]; /*!< SSL v3.0 secret (dec) */
  260. #endif /* MBEDTLS_SSL_PROTO_SSL3 */
  261. mbedtls_md_context_t md_ctx_enc; /*!< MAC (encryption) */
  262. mbedtls_md_context_t md_ctx_dec; /*!< MAC (decryption) */
  263. mbedtls_cipher_context_t cipher_ctx_enc; /*!< encryption context */
  264. mbedtls_cipher_context_t cipher_ctx_dec; /*!< decryption context */
  265. /*
  266. * Session specific compression layer
  267. */
  268. #if defined(MBEDTLS_ZLIB_SUPPORT)
  269. z_stream ctx_deflate; /*!< compression context */
  270. z_stream ctx_inflate; /*!< decompression context */
  271. #endif
  272. };
  273. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  274. /*
  275. * List of certificate + private key pairs
  276. */
  277. struct mbedtls_ssl_key_cert
  278. {
  279. mbedtls_x509_crt *cert; /*!< cert */
  280. mbedtls_pk_context *key; /*!< private key */
  281. mbedtls_ssl_key_cert *next; /*!< next key/cert pair */
  282. };
  283. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  284. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  285. /*
  286. * List of handshake messages kept around for resending
  287. */
  288. struct mbedtls_ssl_flight_item
  289. {
  290. unsigned char *p; /*!< message, including handshake headers */
  291. size_t len; /*!< length of p */
  292. unsigned char type; /*!< type of the message: handshake or CCS */
  293. mbedtls_ssl_flight_item *next; /*!< next handshake message(s) */
  294. };
  295. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  296. /**
  297. * \brief Free referenced items in an SSL transform context and clear
  298. * memory
  299. *
  300. * \param transform SSL transform context
  301. */
  302. void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform );
  303. /**
  304. * \brief Free referenced items in an SSL handshake context and clear
  305. * memory
  306. *
  307. * \param handshake SSL handshake context
  308. */
  309. void mbedtls_ssl_handshake_free( mbedtls_ssl_handshake_params *handshake );
  310. int mbedtls_ssl_handshake_client_step( mbedtls_ssl_context *ssl );
  311. int mbedtls_ssl_handshake_server_step( mbedtls_ssl_context *ssl );
  312. void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl );
  313. int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl );
  314. void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl );
  315. int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl );
  316. int mbedtls_ssl_read_record_layer( mbedtls_ssl_context *ssl );
  317. int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl );
  318. int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl );
  319. void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl );
  320. int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl );
  321. int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want );
  322. int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl );
  323. int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl );
  324. int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl );
  325. int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl );
  326. int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl );
  327. int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl );
  328. int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl );
  329. int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl );
  330. void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
  331. const mbedtls_ssl_ciphersuite_t *ciphersuite_info );
  332. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  333. int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex );
  334. #endif
  335. #if defined(MBEDTLS_PK_C)
  336. unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk );
  337. mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig );
  338. #endif
  339. mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash );
  340. unsigned char mbedtls_ssl_hash_from_md_alg( int md );
  341. int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md );
  342. #if defined(MBEDTLS_ECP_C)
  343. int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id );
  344. #endif
  345. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  346. int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
  347. mbedtls_md_type_t md );
  348. #endif
  349. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  350. static inline mbedtls_pk_context *mbedtls_ssl_own_key( mbedtls_ssl_context *ssl )
  351. {
  352. mbedtls_ssl_key_cert *key_cert;
  353. if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
  354. key_cert = ssl->handshake->key_cert;
  355. else
  356. key_cert = ssl->conf->key_cert;
  357. return( key_cert == NULL ? NULL : key_cert->key );
  358. }
  359. static inline mbedtls_x509_crt *mbedtls_ssl_own_cert( mbedtls_ssl_context *ssl )
  360. {
  361. mbedtls_ssl_key_cert *key_cert;
  362. if( ssl->handshake != NULL && ssl->handshake->key_cert != NULL )
  363. key_cert = ssl->handshake->key_cert;
  364. else
  365. key_cert = ssl->conf->key_cert;
  366. return( key_cert == NULL ? NULL : key_cert->cert );
  367. }
  368. /*
  369. * Check usage of a certificate wrt extensions:
  370. * keyUsage, extendedKeyUsage (later), and nSCertType (later).
  371. *
  372. * Warning: cert_endpoint is the endpoint of the cert (ie, of our peer when we
  373. * check a cert we received from them)!
  374. *
  375. * Return 0 if everything is OK, -1 if not.
  376. */
  377. int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
  378. const mbedtls_ssl_ciphersuite_t *ciphersuite,
  379. int cert_endpoint,
  380. uint32_t *flags );
  381. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  382. void mbedtls_ssl_write_version( int major, int minor, int transport,
  383. unsigned char ver[2] );
  384. void mbedtls_ssl_read_version( int *major, int *minor, int transport,
  385. const unsigned char ver[2] );
  386. static inline size_t mbedtls_ssl_hdr_len( const mbedtls_ssl_context *ssl )
  387. {
  388. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  389. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  390. return( 13 );
  391. #else
  392. ((void) ssl);
  393. #endif
  394. return( 5 );
  395. }
  396. static inline size_t mbedtls_ssl_hs_hdr_len( const mbedtls_ssl_context *ssl )
  397. {
  398. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  399. if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  400. return( 12 );
  401. #else
  402. ((void) ssl);
  403. #endif
  404. return( 4 );
  405. }
  406. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  407. void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl );
  408. void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl );
  409. int mbedtls_ssl_resend( mbedtls_ssl_context *ssl );
  410. #endif
  411. /* Visible for testing purposes only */
  412. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  413. int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl );
  414. void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl );
  415. #endif
  416. /* constant-time buffer comparison */
  417. static inline int mbedtls_ssl_safer_memcmp( const void *a, const void *b, size_t n )
  418. {
  419. size_t i;
  420. const unsigned char *A = (const unsigned char *) a;
  421. const unsigned char *B = (const unsigned char *) b;
  422. unsigned char diff = 0;
  423. for( i = 0; i < n; i++ )
  424. diff |= A[i] ^ B[i];
  425. return( diff );
  426. }
  427. #ifdef __cplusplus
  428. }
  429. #endif
  430. #endif /* ssl_internal.h */