ssl_test_lib.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Common code for SSL test programs
  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. #ifndef MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H
  20. #define MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #define mbedtls_calloc calloc
  32. #define mbedtls_free free
  33. #define mbedtls_time time
  34. #define mbedtls_time_t time_t
  35. #define mbedtls_printf printf
  36. #define mbedtls_fprintf fprintf
  37. #define mbedtls_snprintf snprintf
  38. #define mbedtls_exit exit
  39. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  40. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  41. #endif
  42. #undef HAVE_RNG
  43. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
  44. ( defined(MBEDTLS_USE_PSA_CRYPTO) || \
  45. defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG) )
  46. #define HAVE_RNG
  47. #elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_CTR_DRBG_C)
  48. #define HAVE_RNG
  49. #elif defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_HMAC_DRBG_C) && \
  50. ( defined(MBEDTLS_SHA256_C) || defined(MBEDTLS_SHA512_C) )
  51. #define HAVE_RNG
  52. #endif
  53. #if !defined(MBEDTLS_NET_C) || \
  54. !defined(MBEDTLS_SSL_TLS_C) || \
  55. defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
  56. #define MBEDTLS_SSL_TEST_IMPOSSIBLE \
  57. "MBEDTLS_NET_C and/or " \
  58. "MBEDTLS_SSL_TLS_C not defined, " \
  59. "and/or MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER defined.\n"
  60. #elif !defined(HAVE_RNG)
  61. #define MBEDTLS_SSL_TEST_IMPOSSIBLE \
  62. "No random generator is available.\n"
  63. #else
  64. #undef MBEDTLS_SSL_TEST_IMPOSSIBLE
  65. #undef HAVE_RNG
  66. #include <stdio.h>
  67. #include <stdlib.h>
  68. #include <string.h>
  69. #include "mbedtls/net_sockets.h"
  70. #include "mbedtls/ssl.h"
  71. #include "mbedtls/entropy.h"
  72. #include "mbedtls/ctr_drbg.h"
  73. #include "mbedtls/hmac_drbg.h"
  74. #include "mbedtls/certs.h"
  75. #include "mbedtls/x509.h"
  76. #include "mbedtls/error.h"
  77. #include "mbedtls/debug.h"
  78. #include "mbedtls/timing.h"
  79. #include "mbedtls/base64.h"
  80. #if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  81. #include "psa/crypto.h"
  82. #include "mbedtls/psa_util.h"
  83. #endif
  84. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  85. #include "mbedtls/memory_buffer_alloc.h"
  86. #endif
  87. #include <test/helpers.h>
  88. #include "../test/query_config.h"
  89. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  90. typedef struct eap_tls_keys
  91. {
  92. unsigned char master_secret[48];
  93. unsigned char randbytes[64];
  94. mbedtls_tls_prf_types tls_prf_type;
  95. } eap_tls_keys;
  96. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  97. /* Supported SRTP mode needs a maximum of :
  98. * - 16 bytes for key (AES-128)
  99. * - 14 bytes SALT
  100. * One for sender, one for receiver context
  101. */
  102. #define MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH 60
  103. typedef struct dtls_srtp_keys
  104. {
  105. unsigned char master_secret[48];
  106. unsigned char randbytes[64];
  107. mbedtls_tls_prf_types tls_prf_type;
  108. } dtls_srtp_keys;
  109. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  110. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  111. typedef struct
  112. {
  113. mbedtls_ssl_context *ssl;
  114. mbedtls_net_context *net;
  115. } io_ctx_t;
  116. void my_debug( void *ctx, int level,
  117. const char *file, int line,
  118. const char *str );
  119. mbedtls_time_t dummy_constant_time( mbedtls_time_t* time );
  120. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  121. /* If MBEDTLS_TEST_USE_PSA_CRYPTO_RNG is defined, the SSL test programs will use
  122. * mbedtls_psa_get_random() rather than entropy+DRBG as a random generator.
  123. *
  124. * The constraints are:
  125. * - Without the entropy module, the PSA RNG is the only option.
  126. * - Without at least one of the DRBG modules, the PSA RNG is the only option.
  127. * - The PSA RNG does not support explicit seeding, so it is incompatible with
  128. * the reproducible mode used by test programs.
  129. * - For good overall test coverage, there should be at least one configuration
  130. * where the test programs use the PSA RNG while the PSA RNG is itself based
  131. * on entropy+DRBG, and at least one configuration where the test programs
  132. * do not use the PSA RNG even though it's there.
  133. *
  134. * A simple choice that meets the constraints is to use the PSA RNG whenever
  135. * MBEDTLS_USE_PSA_CRYPTO is enabled. There's no real technical reason the
  136. * choice to use the PSA RNG in the test programs and the choice to use
  137. * PSA crypto when TLS code needs crypto have to be tied together, but it
  138. * happens to be a good match. It's also a good match from an application
  139. * perspective: either PSA is preferred for TLS (both for crypto and for
  140. * random generation) or it isn't.
  141. */
  142. #define MBEDTLS_TEST_USE_PSA_CRYPTO_RNG
  143. #endif
  144. /** A context for random number generation (RNG).
  145. */
  146. typedef struct
  147. {
  148. #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  149. unsigned char dummy;
  150. #else /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  151. mbedtls_entropy_context entropy;
  152. #if defined(MBEDTLS_CTR_DRBG_C)
  153. mbedtls_ctr_drbg_context drbg;
  154. #elif defined(MBEDTLS_HMAC_DRBG_C)
  155. mbedtls_hmac_drbg_context drbg;
  156. #else
  157. #error "No DRBG available"
  158. #endif
  159. #endif /* MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  160. } rng_context_t;
  161. /** Initialize the RNG.
  162. *
  163. * This function only initializes the memory used by the RNG context.
  164. * Before using the RNG, it must be seeded with rng_seed().
  165. */
  166. void rng_init( rng_context_t *rng );
  167. /* Seed the random number generator.
  168. *
  169. * \param rng The RNG context to use. It must have been initialized
  170. * with rng_init().
  171. * \param reproducible If zero, seed the RNG from entropy.
  172. * If nonzero, use a fixed seed, so that the program
  173. * will produce the same sequence of random numbers
  174. * each time it is invoked.
  175. * \param pers A null-terminated string. Different values for this
  176. * string cause the RNG to emit different output for
  177. * the same seed.
  178. *
  179. * return 0 on success, a negative value on error.
  180. */
  181. int rng_seed( rng_context_t *rng, int reproducible, const char *pers );
  182. /** Deinitialize the RNG. Free any embedded resource.
  183. *
  184. * \param rng The RNG context to deinitialize. It must have been
  185. * initialized with rng_init().
  186. */
  187. void rng_free( rng_context_t *rng );
  188. /** Generate random data.
  189. *
  190. * This function is suitable for use as the \c f_rng argument to Mbed TLS
  191. * library functions.
  192. *
  193. * \param p_rng The random generator context. This must be a pointer to
  194. * a #rng_context_t structure.
  195. * \param output The buffer to fill.
  196. * \param output_len The length of the buffer in bytes.
  197. *
  198. * \return \c 0 on success.
  199. * \return An Mbed TLS error code on error.
  200. */
  201. int rng_get( void *p_rng, unsigned char *output, size_t output_len );
  202. #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  203. /* The test implementation of the PSA external RNG is insecure. When
  204. * MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG is enabled, before using any PSA crypto
  205. * function that makes use of an RNG, you must call
  206. * mbedtls_test_enable_insecure_external_rng(). */
  207. #include <test/fake_external_rng_for_test.h>
  208. #endif
  209. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  210. int ca_callback( void *data, mbedtls_x509_crt const *child,
  211. mbedtls_x509_crt **candidates );
  212. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  213. /*
  214. * Test recv/send functions that make sure each try returns
  215. * WANT_READ/WANT_WRITE at least once before sucesseding
  216. */
  217. int delayed_recv( void *ctx, unsigned char *buf, size_t len );
  218. int delayed_send( void *ctx, const unsigned char *buf, size_t len );
  219. /*
  220. * Wait for an event from the underlying transport or the timer
  221. * (Used in event-driven IO mode).
  222. */
  223. int idle( mbedtls_net_context *fd,
  224. #if defined(MBEDTLS_TIMING_C)
  225. mbedtls_timing_delay_context *timer,
  226. #endif
  227. int idle_reason );
  228. #if defined(MBEDTLS_TEST_HOOKS)
  229. /** Initialize whatever test hooks are enabled by the compile-time
  230. * configuration and make sense for the TLS test programs. */
  231. void test_hooks_init( void );
  232. /** Check if any test hooks detected a problem.
  233. *
  234. * If a problem was detected, it's ok for the calling program to keep going,
  235. * but it should ultimately exit with an error status.
  236. *
  237. * \note When implementing a test hook that detects errors on its own
  238. * (as opposed to e.g. leaving the error for a memory sanitizer to
  239. * report), make sure to print a message to standard error either at
  240. * the time the problem is detected or during the execution of this
  241. * function. This function does not indicate what problem was detected,
  242. * so printing a message is the only way to provide feedback in the
  243. * logs of the calling program.
  244. *
  245. * \return Nonzero if a problem was detected.
  246. * \c 0 if no problem was detected.
  247. */
  248. int test_hooks_failure_detected( void );
  249. /** Free any resources allocated for the sake of test hooks.
  250. *
  251. * Call this at the end of the program so that resource leak analyzers
  252. * don't complain.
  253. */
  254. void test_hooks_free( void );
  255. #endif /* !MBEDTLS_TEST_HOOKS */
  256. #endif /* MBEDTLS_SSL_TEST_IMPOSSIBLE conditions: else */
  257. #endif /* MBEDTLS_PROGRAMS_SSL_SSL_TEST_LIB_H */