ssl_test_lib.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Common code library for SSL test programs.
  3. *
  4. * In addition to the functions in this file, there is shared source code
  5. * that cannot be compiled separately in "ssl_test_common_source.c".
  6. *
  7. * Copyright The Mbed TLS Contributors
  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. #include "ssl_test_lib.h"
  23. #if defined(MBEDTLS_TEST_HOOKS)
  24. #include "test/helpers.h"
  25. #endif
  26. #if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
  27. void my_debug( void *ctx, int level,
  28. const char *file, int line,
  29. const char *str )
  30. {
  31. const char *p, *basename;
  32. /* Extract basename from file */
  33. for( p = basename = file; *p != '\0'; p++ )
  34. if( *p == '/' || *p == '\\' )
  35. basename = p + 1;
  36. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
  37. basename, line, level, str );
  38. fflush( (FILE *) ctx );
  39. }
  40. mbedtls_time_t dummy_constant_time( mbedtls_time_t* time )
  41. {
  42. (void) time;
  43. return 0x5af2a056;
  44. }
  45. #if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  46. static int dummy_entropy( void *data, unsigned char *output, size_t len )
  47. {
  48. size_t i;
  49. int ret;
  50. (void) data;
  51. ret = mbedtls_entropy_func( data, output, len );
  52. for( i = 0; i < len; i++ )
  53. {
  54. //replace result with pseudo random
  55. output[i] = (unsigned char) rand();
  56. }
  57. return( ret );
  58. }
  59. #endif
  60. void rng_init( rng_context_t *rng )
  61. {
  62. #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  63. (void) rng;
  64. psa_crypto_init( );
  65. #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  66. #if defined(MBEDTLS_CTR_DRBG_C)
  67. mbedtls_ctr_drbg_init( &rng->drbg );
  68. #elif defined(MBEDTLS_HMAC_DRBG_C)
  69. mbedtls_hmac_drbg_init( &rng->drbg );
  70. #else
  71. #error "No DRBG available"
  72. #endif
  73. mbedtls_entropy_init( &rng->entropy );
  74. #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  75. }
  76. int rng_seed( rng_context_t *rng, int reproducible, const char *pers )
  77. {
  78. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  79. if( reproducible )
  80. {
  81. mbedtls_fprintf( stderr,
  82. "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n" );
  83. return( -1 );
  84. }
  85. #endif
  86. #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  87. /* The PSA crypto RNG does its own seeding. */
  88. (void) rng;
  89. (void) pers;
  90. if( reproducible )
  91. {
  92. mbedtls_fprintf( stderr,
  93. "The PSA RNG does not support reproducible mode.\n" );
  94. return( -1 );
  95. }
  96. return( 0 );
  97. #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  98. int ( *f_entropy )( void *, unsigned char *, size_t ) =
  99. ( reproducible ? dummy_entropy : mbedtls_entropy_func );
  100. if ( reproducible )
  101. srand( 1 );
  102. #if defined(MBEDTLS_CTR_DRBG_C)
  103. int ret = mbedtls_ctr_drbg_seed( &rng->drbg,
  104. f_entropy, &rng->entropy,
  105. (const unsigned char *) pers,
  106. strlen( pers ) );
  107. #elif defined(MBEDTLS_HMAC_DRBG_C)
  108. #if defined(MBEDTLS_SHA256_C)
  109. const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
  110. #elif defined(MBEDTLS_SHA512_C)
  111. const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
  112. #else
  113. #error "No message digest available for HMAC_DRBG"
  114. #endif
  115. int ret = mbedtls_hmac_drbg_seed( &rng->drbg,
  116. mbedtls_md_info_from_type( md_type ),
  117. f_entropy, &rng->entropy,
  118. (const unsigned char *) pers,
  119. strlen( pers ) );
  120. #else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
  121. #error "No DRBG available"
  122. #endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
  123. if( ret != 0 )
  124. {
  125. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
  126. (unsigned int) -ret );
  127. return( ret );
  128. }
  129. #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  130. return( 0 );
  131. }
  132. void rng_free( rng_context_t *rng )
  133. {
  134. #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  135. (void) rng;
  136. /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs.
  137. * This is ok because none of our applications try to do any crypto after
  138. * deinitializing the RNG. */
  139. mbedtls_psa_crypto_free( );
  140. #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  141. #if defined(MBEDTLS_CTR_DRBG_C)
  142. mbedtls_ctr_drbg_free( &rng->drbg );
  143. #elif defined(MBEDTLS_HMAC_DRBG_C)
  144. mbedtls_hmac_drbg_free( &rng->drbg );
  145. #else
  146. #error "No DRBG available"
  147. #endif
  148. mbedtls_entropy_free( &rng->entropy );
  149. #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  150. }
  151. int rng_get( void *p_rng, unsigned char *output, size_t output_len )
  152. {
  153. #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  154. (void) p_rng;
  155. return( mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
  156. output, output_len ) );
  157. #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  158. rng_context_t *rng = p_rng;
  159. #if defined(MBEDTLS_CTR_DRBG_C)
  160. return( mbedtls_ctr_drbg_random( &rng->drbg, output, output_len ) );
  161. #elif defined(MBEDTLS_HMAC_DRBG_C)
  162. return( mbedtls_hmac_drbg_random( &rng->drbg, output, output_len ) );
  163. #else
  164. #error "No DRBG available"
  165. #endif
  166. #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  167. }
  168. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  169. int ca_callback( void *data, mbedtls_x509_crt const *child,
  170. mbedtls_x509_crt **candidates )
  171. {
  172. int ret = 0;
  173. mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
  174. mbedtls_x509_crt *first;
  175. /* This is a test-only implementation of the CA callback
  176. * which always returns the entire list of trusted certificates.
  177. * Production implementations managing a large number of CAs
  178. * should use an efficient presentation and lookup for the
  179. * set of trusted certificates (such as a hashtable) and only
  180. * return those trusted certificates which satisfy basic
  181. * parental checks, such as the matching of child `Issuer`
  182. * and parent `Subject` field or matching key identifiers. */
  183. ((void) child);
  184. first = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
  185. if( first == NULL )
  186. {
  187. ret = -1;
  188. goto exit;
  189. }
  190. mbedtls_x509_crt_init( first );
  191. if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
  192. {
  193. ret = -1;
  194. goto exit;
  195. }
  196. while( ca->next != NULL )
  197. {
  198. ca = ca->next;
  199. if( mbedtls_x509_crt_parse_der( first, ca->raw.p, ca->raw.len ) != 0 )
  200. {
  201. ret = -1;
  202. goto exit;
  203. }
  204. }
  205. exit:
  206. if( ret != 0 )
  207. {
  208. mbedtls_x509_crt_free( first );
  209. mbedtls_free( first );
  210. first = NULL;
  211. }
  212. *candidates = first;
  213. return( ret );
  214. }
  215. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  216. int delayed_recv( void *ctx, unsigned char *buf, size_t len )
  217. {
  218. static int first_try = 1;
  219. int ret;
  220. if( first_try )
  221. {
  222. first_try = 0;
  223. return( MBEDTLS_ERR_SSL_WANT_READ );
  224. }
  225. ret = mbedtls_net_recv( ctx, buf, len );
  226. if( ret != MBEDTLS_ERR_SSL_WANT_READ )
  227. first_try = 1; /* Next call will be a new operation */
  228. return( ret );
  229. }
  230. int delayed_send( void *ctx, const unsigned char *buf, size_t len )
  231. {
  232. static int first_try = 1;
  233. int ret;
  234. if( first_try )
  235. {
  236. first_try = 0;
  237. return( MBEDTLS_ERR_SSL_WANT_WRITE );
  238. }
  239. ret = mbedtls_net_send( ctx, buf, len );
  240. if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  241. first_try = 1; /* Next call will be a new operation */
  242. return( ret );
  243. }
  244. #if !defined(MBEDTLS_TIMING_C)
  245. int idle( mbedtls_net_context *fd,
  246. int idle_reason )
  247. #else
  248. int idle( mbedtls_net_context *fd,
  249. mbedtls_timing_delay_context *timer,
  250. int idle_reason )
  251. #endif
  252. {
  253. int ret;
  254. int poll_type = 0;
  255. if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
  256. poll_type = MBEDTLS_NET_POLL_WRITE;
  257. else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
  258. poll_type = MBEDTLS_NET_POLL_READ;
  259. #if !defined(MBEDTLS_TIMING_C)
  260. else
  261. return( 0 );
  262. #endif
  263. while( 1 )
  264. {
  265. /* Check if timer has expired */
  266. #if defined(MBEDTLS_TIMING_C)
  267. if( timer != NULL &&
  268. mbedtls_timing_get_delay( timer ) == 2 )
  269. {
  270. break;
  271. }
  272. #endif /* MBEDTLS_TIMING_C */
  273. /* Check if underlying transport became available */
  274. if( poll_type != 0 )
  275. {
  276. ret = mbedtls_net_poll( fd, poll_type, 0 );
  277. if( ret < 0 )
  278. return( ret );
  279. if( ret == poll_type )
  280. break;
  281. }
  282. }
  283. return( 0 );
  284. }
  285. #if defined(MBEDTLS_TEST_HOOKS)
  286. void test_hooks_init( void )
  287. {
  288. mbedtls_test_info_reset( );
  289. #if defined(MBEDTLS_TEST_MUTEX_USAGE)
  290. mbedtls_test_mutex_usage_init( );
  291. #endif
  292. }
  293. int test_hooks_failure_detected( void )
  294. {
  295. #if defined(MBEDTLS_TEST_MUTEX_USAGE)
  296. /* Errors are reported via mbedtls_test_info. */
  297. mbedtls_test_mutex_usage_check( );
  298. #endif
  299. if( mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS )
  300. return( 1 );
  301. return( 0 );
  302. }
  303. void test_hooks_free( void )
  304. {
  305. }
  306. #endif /* MBEDTLS_TEST_HOOKS */
  307. #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */