2
0

provfetchtest.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/crypto.h>
  10. #include <openssl/provider.h>
  11. #include <openssl/decoder.h>
  12. #include <openssl/encoder.h>
  13. #include <openssl/store.h>
  14. #include <openssl/rand.h>
  15. #include <openssl/core_names.h>
  16. #include "testutil.h"
  17. static int dummy_decoder_decode(void *ctx, OSSL_CORE_BIO *cin, int selection,
  18. OSSL_CALLBACK *object_cb, void *object_cbarg,
  19. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  20. {
  21. return 0;
  22. }
  23. static const OSSL_DISPATCH dummy_decoder_functions[] = {
  24. { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_decoder_decode },
  25. OSSL_DISPATCH_END
  26. };
  27. static const OSSL_ALGORITHM dummy_decoders[] = {
  28. { "DUMMY", "provider=dummy,input=pem", dummy_decoder_functions },
  29. { NULL, NULL, NULL }
  30. };
  31. static int dummy_encoder_encode(void *ctx, OSSL_CORE_BIO *out,
  32. const void *obj_raw,
  33. const OSSL_PARAM obj_abstract[], int selection,
  34. OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
  35. {
  36. return 0;
  37. }
  38. static const OSSL_DISPATCH dummy_encoder_functions[] = {
  39. { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_encoder_encode },
  40. OSSL_DISPATCH_END
  41. };
  42. static const OSSL_ALGORITHM dummy_encoders[] = {
  43. { "DUMMY", "provider=dummy,output=pem", dummy_encoder_functions },
  44. { NULL, NULL, NULL }
  45. };
  46. static void *dummy_store_open(void *provctx, const char *uri)
  47. {
  48. return NULL;
  49. }
  50. static int dummy_store_load(void *loaderctx, OSSL_CALLBACK *object_cb,
  51. void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb,
  52. void *pw_cbarg)
  53. {
  54. return 0;
  55. }
  56. static int dumm_store_eof(void *loaderctx)
  57. {
  58. return 0;
  59. }
  60. static int dummy_store_close(void *loaderctx)
  61. {
  62. return 0;
  63. }
  64. static const OSSL_DISPATCH dummy_store_functions[] = {
  65. { OSSL_FUNC_STORE_OPEN, (void (*)(void))dummy_store_open },
  66. { OSSL_FUNC_STORE_LOAD, (void (*)(void))dummy_store_load },
  67. { OSSL_FUNC_STORE_EOF, (void (*)(void))dumm_store_eof },
  68. { OSSL_FUNC_STORE_CLOSE, (void (*)(void))dummy_store_close },
  69. OSSL_DISPATCH_END
  70. };
  71. static const OSSL_ALGORITHM dummy_store[] = {
  72. { "DUMMY", "provider=dummy", dummy_store_functions },
  73. { NULL, NULL, NULL }
  74. };
  75. static void *dummy_rand_newctx(void *provctx, void *parent,
  76. const OSSL_DISPATCH *parent_calls)
  77. {
  78. return provctx;
  79. }
  80. static void dummy_rand_freectx(void *vctx)
  81. {
  82. }
  83. static int dummy_rand_instantiate(void *vdrbg, unsigned int strength,
  84. int prediction_resistance,
  85. const unsigned char *pstr, size_t pstr_len,
  86. const OSSL_PARAM params[])
  87. {
  88. return 1;
  89. }
  90. static int dummy_rand_uninstantiate(void *vdrbg)
  91. {
  92. return 1;
  93. }
  94. static int dummy_rand_generate(void *vctx, unsigned char *out, size_t outlen,
  95. unsigned int strength, int prediction_resistance,
  96. const unsigned char *addin, size_t addin_len)
  97. {
  98. size_t i;
  99. for (i = 0; i <outlen; i++)
  100. out[i] = (unsigned char)(i & 0xff);
  101. return 1;
  102. }
  103. static const OSSL_PARAM *dummy_rand_gettable_ctx_params(void *vctx, void *provctx)
  104. {
  105. static const OSSL_PARAM known_gettable_ctx_params[] = {
  106. OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
  107. OSSL_PARAM_END
  108. };
  109. return known_gettable_ctx_params;
  110. }
  111. static int dummy_rand_get_ctx_params(void *vctx, OSSL_PARAM params[])
  112. {
  113. OSSL_PARAM *p;
  114. p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
  115. if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX))
  116. return 0;
  117. return 1;
  118. }
  119. static int dummy_rand_enable_locking(void *vtest)
  120. {
  121. return 1;
  122. }
  123. static int dummy_rand_lock(void *vtest)
  124. {
  125. return 1;
  126. }
  127. static void dummy_rand_unlock(void *vtest)
  128. {
  129. }
  130. static const OSSL_DISPATCH dummy_rand_functions[] = {
  131. { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))dummy_rand_newctx },
  132. { OSSL_FUNC_RAND_FREECTX, (void (*)(void))dummy_rand_freectx },
  133. { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))dummy_rand_instantiate },
  134. { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))dummy_rand_uninstantiate },
  135. { OSSL_FUNC_RAND_GENERATE, (void (*)(void))dummy_rand_generate },
  136. { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS,
  137. (void(*)(void))dummy_rand_gettable_ctx_params },
  138. { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))dummy_rand_get_ctx_params },
  139. { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))dummy_rand_enable_locking },
  140. { OSSL_FUNC_RAND_LOCK, (void(*)(void))dummy_rand_lock },
  141. { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))dummy_rand_unlock },
  142. OSSL_DISPATCH_END
  143. };
  144. static const OSSL_ALGORITHM dummy_rand[] = {
  145. { "DUMMY", "provider=dummy", dummy_rand_functions },
  146. { NULL, NULL, NULL }
  147. };
  148. static const OSSL_ALGORITHM *dummy_query(void *provctx, int operation_id,
  149. int *no_cache)
  150. {
  151. *no_cache = 0;
  152. switch (operation_id) {
  153. case OSSL_OP_DECODER:
  154. return dummy_decoders;
  155. case OSSL_OP_ENCODER:
  156. return dummy_encoders;
  157. case OSSL_OP_STORE:
  158. return dummy_store;
  159. case OSSL_OP_RAND:
  160. return dummy_rand;
  161. }
  162. return NULL;
  163. }
  164. static const OSSL_DISPATCH dummy_dispatch_table[] = {
  165. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))dummy_query },
  166. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free },
  167. OSSL_DISPATCH_END
  168. };
  169. static int dummy_provider_init(const OSSL_CORE_HANDLE *handle,
  170. const OSSL_DISPATCH *in,
  171. const OSSL_DISPATCH **out,
  172. void **provctx)
  173. {
  174. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_child(handle, in);
  175. unsigned char buf[32];
  176. *provctx = (void *)libctx;
  177. *out = dummy_dispatch_table;
  178. /*
  179. * Do some work using the child libctx, to make sure this is possible from
  180. * inside the init function.
  181. */
  182. if (RAND_bytes_ex(libctx, buf, sizeof(buf), 0) <= 0)
  183. return 0;
  184. return 1;
  185. }
  186. /*
  187. * Try fetching and freeing various things.
  188. * Test 0: Decoder
  189. * Test 1: Encoder
  190. * Test 2: Store loader
  191. * Test 3: EVP_RAND
  192. * Test 4-7: As above, but additionally with a query string
  193. */
  194. static int fetch_test(int tst)
  195. {
  196. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
  197. OSSL_PROVIDER *dummyprov = NULL;
  198. OSSL_PROVIDER *nullprov = NULL;
  199. OSSL_DECODER *decoder = NULL;
  200. OSSL_ENCODER *encoder = NULL;
  201. OSSL_STORE_LOADER *loader = NULL;
  202. int testresult = 0;
  203. unsigned char buf[32];
  204. int query = tst > 3;
  205. if (!TEST_ptr(libctx))
  206. goto err;
  207. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "dummy-prov",
  208. dummy_provider_init))
  209. || !TEST_ptr(nullprov = OSSL_PROVIDER_load(libctx, "default"))
  210. || !TEST_ptr(dummyprov = OSSL_PROVIDER_load(libctx, "dummy-prov")))
  211. goto err;
  212. switch (tst % 4) {
  213. case 0:
  214. decoder = OSSL_DECODER_fetch(libctx, "DUMMY",
  215. query ? "provider=dummy" : NULL);
  216. if (!TEST_ptr(decoder))
  217. goto err;
  218. break;
  219. case 1:
  220. encoder = OSSL_ENCODER_fetch(libctx, "DUMMY",
  221. query ? "provider=dummy" : NULL);
  222. if (!TEST_ptr(encoder))
  223. goto err;
  224. break;
  225. case 2:
  226. loader = OSSL_STORE_LOADER_fetch(libctx, "DUMMY",
  227. query ? "provider=dummy" : NULL);
  228. if (!TEST_ptr(loader))
  229. goto err;
  230. break;
  231. case 3:
  232. if (!TEST_true(RAND_set_DRBG_type(libctx, "DUMMY",
  233. query ? "provider=dummy" : NULL,
  234. NULL, NULL))
  235. || !TEST_int_ge(RAND_bytes_ex(libctx, buf, sizeof(buf), 0), 1))
  236. goto err;
  237. break;
  238. default:
  239. goto err;
  240. }
  241. testresult = 1;
  242. err:
  243. OSSL_DECODER_free(decoder);
  244. OSSL_ENCODER_free(encoder);
  245. OSSL_STORE_LOADER_free(loader);
  246. OSSL_PROVIDER_unload(dummyprov);
  247. OSSL_PROVIDER_unload(nullprov);
  248. OSSL_LIB_CTX_free(libctx);
  249. return testresult;
  250. }
  251. int setup_tests(void)
  252. {
  253. ADD_ALL_TESTS(fetch_test, 8);
  254. return 1;
  255. }