ssl_test_lib.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. #define MBEDTLS_ALLOW_PRIVATE_ACCESS
  23. #include "ssl_test_lib.h"
  24. #if defined(MBEDTLS_TEST_HOOKS)
  25. #include "test/helpers.h"
  26. #endif
  27. #if !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
  28. void my_debug(void *ctx, int level,
  29. const char *file, int line,
  30. const char *str)
  31. {
  32. const char *p, *basename;
  33. /* Extract basename from file */
  34. for (p = basename = file; *p != '\0'; p++) {
  35. if (*p == '/' || *p == '\\') {
  36. basename = p + 1;
  37. }
  38. }
  39. mbedtls_fprintf((FILE *) ctx, "%s:%04d: |%d| %s",
  40. basename, line, level, str);
  41. fflush((FILE *) ctx);
  42. }
  43. #if defined(MBEDTLS_HAVE_TIME)
  44. mbedtls_time_t dummy_constant_time(mbedtls_time_t *time)
  45. {
  46. (void) time;
  47. return 0x5af2a056;
  48. }
  49. #endif
  50. #if !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  51. static int dummy_entropy(void *data, unsigned char *output, size_t len)
  52. {
  53. size_t i;
  54. int ret;
  55. (void) data;
  56. ret = mbedtls_entropy_func(data, output, len);
  57. for (i = 0; i < len; i++) {
  58. //replace result with pseudo random
  59. output[i] = (unsigned char) rand();
  60. }
  61. return ret;
  62. }
  63. #endif
  64. void rng_init(rng_context_t *rng)
  65. {
  66. #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  67. (void) rng;
  68. psa_crypto_init();
  69. #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  70. #if defined(MBEDTLS_CTR_DRBG_C)
  71. mbedtls_ctr_drbg_init(&rng->drbg);
  72. #elif defined(MBEDTLS_HMAC_DRBG_C)
  73. mbedtls_hmac_drbg_init(&rng->drbg);
  74. #else
  75. #error "No DRBG available"
  76. #endif
  77. mbedtls_entropy_init(&rng->entropy);
  78. #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  79. }
  80. int rng_seed(rng_context_t *rng, int reproducible, const char *pers)
  81. {
  82. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  83. if (reproducible) {
  84. mbedtls_fprintf(stderr,
  85. "MBEDTLS_USE_PSA_CRYPTO does not support reproducible mode.\n");
  86. return -1;
  87. }
  88. #endif
  89. #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  90. /* The PSA crypto RNG does its own seeding. */
  91. (void) rng;
  92. (void) pers;
  93. if (reproducible) {
  94. mbedtls_fprintf(stderr,
  95. "The PSA RNG does not support reproducible mode.\n");
  96. return -1;
  97. }
  98. return 0;
  99. #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  100. int (*f_entropy)(void *, unsigned char *, size_t) =
  101. (reproducible ? dummy_entropy : mbedtls_entropy_func);
  102. if (reproducible) {
  103. srand(1);
  104. }
  105. #if defined(MBEDTLS_CTR_DRBG_C)
  106. int ret = mbedtls_ctr_drbg_seed(&rng->drbg,
  107. f_entropy, &rng->entropy,
  108. (const unsigned char *) pers,
  109. strlen(pers));
  110. #elif defined(MBEDTLS_HMAC_DRBG_C)
  111. #if defined(MBEDTLS_SHA256_C)
  112. const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA256;
  113. #elif defined(MBEDTLS_SHA512_C)
  114. const mbedtls_md_type_t md_type = MBEDTLS_MD_SHA512;
  115. #else
  116. #error "No message digest available for HMAC_DRBG"
  117. #endif
  118. int ret = mbedtls_hmac_drbg_seed(&rng->drbg,
  119. mbedtls_md_info_from_type(md_type),
  120. f_entropy, &rng->entropy,
  121. (const unsigned char *) pers,
  122. strlen(pers));
  123. #else /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
  124. #error "No DRBG available"
  125. #endif /* !defined(MBEDTLS_CTR_DRBG_C) && !defined(MBEDTLS_HMAC_DRBG_C) */
  126. if (ret != 0) {
  127. mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
  128. (unsigned int) -ret);
  129. return ret;
  130. }
  131. #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  132. return 0;
  133. }
  134. void rng_free(rng_context_t *rng)
  135. {
  136. #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  137. (void) rng;
  138. /* Deinitialize the PSA crypto subsystem. This deactivates all PSA APIs.
  139. * This is ok because none of our applications try to do any crypto after
  140. * deinitializing the RNG. */
  141. mbedtls_psa_crypto_free();
  142. #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  143. #if defined(MBEDTLS_CTR_DRBG_C)
  144. mbedtls_ctr_drbg_free(&rng->drbg);
  145. #elif defined(MBEDTLS_HMAC_DRBG_C)
  146. mbedtls_hmac_drbg_free(&rng->drbg);
  147. #else
  148. #error "No DRBG available"
  149. #endif
  150. mbedtls_entropy_free(&rng->entropy);
  151. #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  152. }
  153. int rng_get(void *p_rng, unsigned char *output, size_t output_len)
  154. {
  155. #if defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  156. (void) p_rng;
  157. return mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE,
  158. output, output_len);
  159. #else /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  160. rng_context_t *rng = p_rng;
  161. #if defined(MBEDTLS_CTR_DRBG_C)
  162. return mbedtls_ctr_drbg_random(&rng->drbg, output, output_len);
  163. #elif defined(MBEDTLS_HMAC_DRBG_C)
  164. return mbedtls_hmac_drbg_random(&rng->drbg, output, output_len);
  165. #else
  166. #error "No DRBG available"
  167. #endif
  168. #endif /* !MBEDTLS_TEST_USE_PSA_CRYPTO_RNG */
  169. }
  170. int key_opaque_alg_parse(const char *arg, const char **alg1, const char **alg2)
  171. {
  172. char *separator;
  173. if ((separator = strchr(arg, ',')) == NULL) {
  174. return 1;
  175. }
  176. *separator = '\0';
  177. *alg1 = arg;
  178. *alg2 = separator + 1;
  179. if (strcmp(*alg1, "rsa-sign-pkcs1") != 0 &&
  180. strcmp(*alg1, "rsa-sign-pss") != 0 &&
  181. strcmp(*alg1, "rsa-sign-pss-sha256") != 0 &&
  182. strcmp(*alg1, "rsa-sign-pss-sha384") != 0 &&
  183. strcmp(*alg1, "rsa-sign-pss-sha512") != 0 &&
  184. strcmp(*alg1, "rsa-decrypt") != 0 &&
  185. strcmp(*alg1, "ecdsa-sign") != 0 &&
  186. strcmp(*alg1, "ecdh") != 0) {
  187. return 1;
  188. }
  189. if (strcmp(*alg2, "rsa-sign-pkcs1") != 0 &&
  190. strcmp(*alg2, "rsa-sign-pss") != 0 &&
  191. strcmp(*alg1, "rsa-sign-pss-sha256") != 0 &&
  192. strcmp(*alg1, "rsa-sign-pss-sha384") != 0 &&
  193. strcmp(*alg1, "rsa-sign-pss-sha512") != 0 &&
  194. strcmp(*alg2, "rsa-decrypt") != 0 &&
  195. strcmp(*alg2, "ecdsa-sign") != 0 &&
  196. strcmp(*alg2, "ecdh") != 0 &&
  197. strcmp(*alg2, "none") != 0) {
  198. return 1;
  199. }
  200. return 0;
  201. }
  202. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  203. int key_opaque_set_alg_usage(const char *alg1, const char *alg2,
  204. psa_algorithm_t *psa_alg1,
  205. psa_algorithm_t *psa_alg2,
  206. psa_key_usage_t *usage,
  207. mbedtls_pk_type_t key_type)
  208. {
  209. if (strcmp(alg1, "none") != 0) {
  210. const char *algs[] = { alg1, alg2 };
  211. psa_algorithm_t *psa_algs[] = { psa_alg1, psa_alg2 };
  212. for (int i = 0; i < 2; i++) {
  213. if (strcmp(algs[i], "rsa-sign-pkcs1") == 0) {
  214. *psa_algs[i] = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
  215. *usage |= PSA_KEY_USAGE_SIGN_HASH;
  216. } else if (strcmp(algs[i], "rsa-sign-pss") == 0) {
  217. *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH);
  218. *usage |= PSA_KEY_USAGE_SIGN_HASH;
  219. } else if (strcmp(algs[i], "rsa-sign-pss-sha256") == 0) {
  220. *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_256);
  221. *usage |= PSA_KEY_USAGE_SIGN_HASH;
  222. } else if (strcmp(algs[i], "rsa-sign-pss-sha384") == 0) {
  223. *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_384);
  224. *usage |= PSA_KEY_USAGE_SIGN_HASH;
  225. } else if (strcmp(algs[i], "rsa-sign-pss-sha512") == 0) {
  226. *psa_algs[i] = PSA_ALG_RSA_PSS(PSA_ALG_SHA_512);
  227. *usage |= PSA_KEY_USAGE_SIGN_HASH;
  228. } else if (strcmp(algs[i], "rsa-decrypt") == 0) {
  229. *psa_algs[i] = PSA_ALG_RSA_PKCS1V15_CRYPT;
  230. *usage |= PSA_KEY_USAGE_DECRYPT;
  231. } else if (strcmp(algs[i], "ecdsa-sign") == 0) {
  232. *psa_algs[i] = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
  233. *usage |= PSA_KEY_USAGE_SIGN_HASH;
  234. } else if (strcmp(algs[i], "ecdh") == 0) {
  235. *psa_algs[i] = PSA_ALG_ECDH;
  236. *usage |= PSA_KEY_USAGE_DERIVE;
  237. } else if (strcmp(algs[i], "none") == 0) {
  238. *psa_algs[i] = PSA_ALG_NONE;
  239. }
  240. }
  241. } else {
  242. if (key_type == MBEDTLS_PK_ECKEY) {
  243. *psa_alg1 = PSA_ALG_ECDSA(PSA_ALG_ANY_HASH);
  244. *psa_alg2 = PSA_ALG_ECDH;
  245. *usage = PSA_KEY_USAGE_SIGN_HASH | PSA_KEY_USAGE_DERIVE;
  246. } else if (key_type == MBEDTLS_PK_RSA) {
  247. *psa_alg1 = PSA_ALG_RSA_PKCS1V15_SIGN(PSA_ALG_ANY_HASH);
  248. *psa_alg2 = PSA_ALG_RSA_PSS(PSA_ALG_ANY_HASH);
  249. *usage = PSA_KEY_USAGE_SIGN_HASH;
  250. } else {
  251. return 1;
  252. }
  253. }
  254. return 0;
  255. }
  256. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  257. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  258. int ca_callback(void *data, mbedtls_x509_crt const *child,
  259. mbedtls_x509_crt **candidates)
  260. {
  261. int ret = 0;
  262. mbedtls_x509_crt *ca = (mbedtls_x509_crt *) data;
  263. mbedtls_x509_crt *first;
  264. /* This is a test-only implementation of the CA callback
  265. * which always returns the entire list of trusted certificates.
  266. * Production implementations managing a large number of CAs
  267. * should use an efficient presentation and lookup for the
  268. * set of trusted certificates (such as a hashtable) and only
  269. * return those trusted certificates which satisfy basic
  270. * parental checks, such as the matching of child `Issuer`
  271. * and parent `Subject` field or matching key identifiers. */
  272. ((void) child);
  273. first = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
  274. if (first == NULL) {
  275. ret = -1;
  276. goto exit;
  277. }
  278. mbedtls_x509_crt_init(first);
  279. if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
  280. ret = -1;
  281. goto exit;
  282. }
  283. while (ca->next != NULL) {
  284. ca = ca->next;
  285. if (mbedtls_x509_crt_parse_der(first, ca->raw.p, ca->raw.len) != 0) {
  286. ret = -1;
  287. goto exit;
  288. }
  289. }
  290. exit:
  291. if (ret != 0) {
  292. mbedtls_x509_crt_free(first);
  293. mbedtls_free(first);
  294. first = NULL;
  295. }
  296. *candidates = first;
  297. return ret;
  298. }
  299. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  300. int delayed_recv(void *ctx, unsigned char *buf, size_t len)
  301. {
  302. static int first_try = 1;
  303. int ret;
  304. if (first_try) {
  305. first_try = 0;
  306. return MBEDTLS_ERR_SSL_WANT_READ;
  307. }
  308. ret = mbedtls_net_recv(ctx, buf, len);
  309. if (ret != MBEDTLS_ERR_SSL_WANT_READ) {
  310. first_try = 1; /* Next call will be a new operation */
  311. }
  312. return ret;
  313. }
  314. int delayed_send(void *ctx, const unsigned char *buf, size_t len)
  315. {
  316. static int first_try = 1;
  317. int ret;
  318. if (first_try) {
  319. first_try = 0;
  320. return MBEDTLS_ERR_SSL_WANT_WRITE;
  321. }
  322. ret = mbedtls_net_send(ctx, buf, len);
  323. if (ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
  324. first_try = 1; /* Next call will be a new operation */
  325. }
  326. return ret;
  327. }
  328. #if !defined(MBEDTLS_TIMING_C)
  329. int idle(mbedtls_net_context *fd,
  330. int idle_reason)
  331. #else
  332. int idle(mbedtls_net_context *fd,
  333. mbedtls_timing_delay_context *timer,
  334. int idle_reason)
  335. #endif
  336. {
  337. int ret;
  338. int poll_type = 0;
  339. if (idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE) {
  340. poll_type = MBEDTLS_NET_POLL_WRITE;
  341. } else if (idle_reason == MBEDTLS_ERR_SSL_WANT_READ) {
  342. poll_type = MBEDTLS_NET_POLL_READ;
  343. }
  344. #if !defined(MBEDTLS_TIMING_C)
  345. else {
  346. return 0;
  347. }
  348. #endif
  349. while (1) {
  350. /* Check if timer has expired */
  351. #if defined(MBEDTLS_TIMING_C)
  352. if (timer != NULL &&
  353. mbedtls_timing_get_delay(timer) == 2) {
  354. break;
  355. }
  356. #endif /* MBEDTLS_TIMING_C */
  357. /* Check if underlying transport became available */
  358. if (poll_type != 0) {
  359. ret = mbedtls_net_poll(fd, poll_type, 0);
  360. if (ret < 0) {
  361. return ret;
  362. }
  363. if (ret == poll_type) {
  364. break;
  365. }
  366. }
  367. }
  368. return 0;
  369. }
  370. #if defined(MBEDTLS_TEST_HOOKS)
  371. void test_hooks_init(void)
  372. {
  373. mbedtls_test_info_reset();
  374. #if defined(MBEDTLS_TEST_MUTEX_USAGE)
  375. mbedtls_test_mutex_usage_init();
  376. #endif
  377. }
  378. int test_hooks_failure_detected(void)
  379. {
  380. #if defined(MBEDTLS_TEST_MUTEX_USAGE)
  381. /* Errors are reported via mbedtls_test_info. */
  382. mbedtls_test_mutex_usage_check();
  383. #endif
  384. if (mbedtls_test_info.result != MBEDTLS_TEST_RESULT_SUCCESS) {
  385. return 1;
  386. }
  387. return 0;
  388. }
  389. void test_hooks_free(void)
  390. {
  391. }
  392. #endif /* MBEDTLS_TEST_HOOKS */
  393. #endif /* !defined(MBEDTLS_SSL_TEST_IMPOSSIBLE) */