ssl_internal.h 18 KB

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