provider_pkey_test.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright 2021 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 <stddef.h>
  10. #include <string.h>
  11. #include <openssl/provider.h>
  12. #include <openssl/params.h>
  13. #include <openssl/core_names.h>
  14. #include <openssl/evp.h>
  15. #include <openssl/store.h>
  16. #include "testutil.h"
  17. #include "fake_rsaprov.h"
  18. static OSSL_LIB_CTX *libctx = NULL;
  19. /* Fetch SIGNATURE method using a libctx and propq */
  20. static int fetch_sig(OSSL_LIB_CTX *ctx, const char *alg, const char *propq,
  21. OSSL_PROVIDER *expected_prov)
  22. {
  23. OSSL_PROVIDER *prov;
  24. EVP_SIGNATURE *sig = EVP_SIGNATURE_fetch(ctx, "RSA", propq);
  25. int ret = 0;
  26. if (!TEST_ptr(sig))
  27. return 0;
  28. if (!TEST_ptr(prov = EVP_SIGNATURE_get0_provider(sig)))
  29. goto end;
  30. if (!TEST_ptr_eq(prov, expected_prov)) {
  31. TEST_info("Fetched provider: %s, Expected provider: %s",
  32. OSSL_PROVIDER_get0_name(prov),
  33. OSSL_PROVIDER_get0_name(expected_prov));
  34. goto end;
  35. }
  36. ret = 1;
  37. end:
  38. EVP_SIGNATURE_free(sig);
  39. return ret;
  40. }
  41. static int test_pkey_sig(void)
  42. {
  43. OSSL_PROVIDER *deflt = NULL;
  44. OSSL_PROVIDER *fake_rsa = NULL;
  45. int i, ret = 0;
  46. EVP_PKEY *pkey = NULL;
  47. EVP_PKEY_CTX *ctx = NULL;
  48. if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx)))
  49. return 0;
  50. if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default")))
  51. goto end;
  52. /* Do a direct fetch to see it works */
  53. if (!TEST_true(fetch_sig(libctx, "RSA", "provider=fake-rsa", fake_rsa))
  54. || !TEST_true(fetch_sig(libctx, "RSA", "?provider=fake-rsa", fake_rsa)))
  55. goto end;
  56. /* Construct a pkey using precise propq to use our provider */
  57. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA",
  58. "provider=fake-rsa"))
  59. || !TEST_true(EVP_PKEY_fromdata_init(ctx))
  60. || !TEST_true(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, NULL))
  61. || !TEST_ptr(pkey))
  62. goto end;
  63. EVP_PKEY_CTX_free(ctx);
  64. ctx = NULL;
  65. /* try exercising signature_init ops a few times */
  66. for (i = 0; i < 3; i++) {
  67. size_t siglen;
  68. /*
  69. * Create a signing context for our pkey with optional propq.
  70. * The sign init should pick both keymgmt and signature from
  71. * fake-rsa as the key is not exportable.
  72. */
  73. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey,
  74. "?provider=default")))
  75. goto end;
  76. /*
  77. * If this picks the wrong signature without realizing it
  78. * we can get a segfault or some internal error. At least watch
  79. * whether fake-rsa sign_init is is exercised by calling sign.
  80. */
  81. if (!TEST_int_eq(EVP_PKEY_sign_init(ctx), 1))
  82. goto end;
  83. if (!TEST_int_eq(EVP_PKEY_sign(ctx, NULL, &siglen, NULL, 0), 1)
  84. || !TEST_size_t_eq(siglen, 256))
  85. goto end;
  86. EVP_PKEY_CTX_free(ctx);
  87. ctx = NULL;
  88. }
  89. ret = 1;
  90. end:
  91. fake_rsa_finish(fake_rsa);
  92. OSSL_PROVIDER_unload(deflt);
  93. EVP_PKEY_CTX_free(ctx);
  94. EVP_PKEY_free(pkey);
  95. return ret;
  96. }
  97. static int test_alternative_keygen_init(void)
  98. {
  99. EVP_PKEY_CTX *ctx = NULL;
  100. OSSL_PROVIDER *deflt = NULL;
  101. OSSL_PROVIDER *fake_rsa = NULL;
  102. const OSSL_PROVIDER *provider;
  103. const char *provname;
  104. int ret = 0;
  105. if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default")))
  106. goto end;
  107. /* first try without the fake RSA provider loaded */
  108. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA", NULL)))
  109. goto end;
  110. if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0))
  111. goto end;
  112. if (!TEST_ptr(provider = EVP_PKEY_CTX_get0_provider(ctx)))
  113. goto end;
  114. if (!TEST_ptr(provname = OSSL_PROVIDER_get0_name(provider)))
  115. goto end;
  116. if (!TEST_str_eq(provname, "default"))
  117. goto end;
  118. EVP_PKEY_CTX_free(ctx);
  119. ctx = NULL;
  120. /* now load fake RSA and try again */
  121. if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx)))
  122. return 0;
  123. if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA",
  124. "?provider=fake-rsa")))
  125. goto end;
  126. if (!TEST_int_gt(EVP_PKEY_keygen_init(ctx), 0))
  127. goto end;
  128. if (!TEST_ptr(provider = EVP_PKEY_CTX_get0_provider(ctx)))
  129. goto end;
  130. if (!TEST_ptr(provname = OSSL_PROVIDER_get0_name(provider)))
  131. goto end;
  132. if (!TEST_str_eq(provname, "fake-rsa"))
  133. goto end;
  134. ret = 1;
  135. end:
  136. fake_rsa_finish(fake_rsa);
  137. OSSL_PROVIDER_unload(deflt);
  138. EVP_PKEY_CTX_free(ctx);
  139. return ret;
  140. }
  141. static int test_pkey_eq(void)
  142. {
  143. OSSL_PROVIDER *deflt = NULL;
  144. OSSL_PROVIDER *fake_rsa = NULL;
  145. EVP_PKEY *pkey_fake = NULL;
  146. EVP_PKEY *pkey_dflt = NULL;
  147. EVP_PKEY_CTX *ctx = NULL;
  148. OSSL_PARAM *params = NULL;
  149. int ret = 0;
  150. if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx)))
  151. return 0;
  152. if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default")))
  153. goto end;
  154. /* Construct a public key for fake-rsa */
  155. if (!TEST_ptr(params = fake_rsa_key_params(0))
  156. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA",
  157. "provider=fake-rsa"))
  158. || !TEST_true(EVP_PKEY_fromdata_init(ctx))
  159. || !TEST_true(EVP_PKEY_fromdata(ctx, &pkey_fake, EVP_PKEY_PUBLIC_KEY,
  160. params))
  161. || !TEST_ptr(pkey_fake))
  162. goto end;
  163. EVP_PKEY_CTX_free(ctx);
  164. ctx = NULL;
  165. OSSL_PARAM_free(params);
  166. params = NULL;
  167. /* Construct a public key for default */
  168. if (!TEST_ptr(params = fake_rsa_key_params(0))
  169. || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(libctx, "RSA",
  170. "provider=default"))
  171. || !TEST_true(EVP_PKEY_fromdata_init(ctx))
  172. || !TEST_true(EVP_PKEY_fromdata(ctx, &pkey_dflt, EVP_PKEY_PUBLIC_KEY,
  173. params))
  174. || !TEST_ptr(pkey_dflt))
  175. goto end;
  176. EVP_PKEY_CTX_free(ctx);
  177. ctx = NULL;
  178. OSSL_PARAM_free(params);
  179. params = NULL;
  180. /* now test for equality */
  181. if (!TEST_int_eq(EVP_PKEY_eq(pkey_fake, pkey_dflt), 1))
  182. goto end;
  183. ret = 1;
  184. end:
  185. fake_rsa_finish(fake_rsa);
  186. OSSL_PROVIDER_unload(deflt);
  187. EVP_PKEY_CTX_free(ctx);
  188. EVP_PKEY_free(pkey_fake);
  189. EVP_PKEY_free(pkey_dflt);
  190. OSSL_PARAM_free(params);
  191. return ret;
  192. }
  193. static int test_pkey_store(int idx)
  194. {
  195. OSSL_PROVIDER *deflt = NULL;
  196. OSSL_PROVIDER *fake_rsa = NULL;
  197. int ret = 0;
  198. EVP_PKEY *pkey = NULL;
  199. OSSL_STORE_LOADER *loader = NULL;
  200. OSSL_STORE_CTX *ctx = NULL;
  201. OSSL_STORE_INFO *info;
  202. const char *propq = idx == 0 ? "?provider=fake-rsa"
  203. : "?provider=default";
  204. /* It's important to load the default provider first for this test */
  205. if (!TEST_ptr(deflt = OSSL_PROVIDER_load(libctx, "default")))
  206. goto end;
  207. if (!TEST_ptr(fake_rsa = fake_rsa_start(libctx)))
  208. goto end;
  209. if (!TEST_ptr(loader = OSSL_STORE_LOADER_fetch(libctx, "fake_rsa",
  210. propq)))
  211. goto end;
  212. OSSL_STORE_LOADER_free(loader);
  213. if (!TEST_ptr(ctx = OSSL_STORE_open_ex("fake_rsa:test", libctx, propq,
  214. NULL, NULL, NULL, NULL, NULL)))
  215. goto end;
  216. while (!OSSL_STORE_eof(ctx)
  217. && (info = OSSL_STORE_load(ctx)) != NULL
  218. && pkey == NULL) {
  219. if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY)
  220. pkey = OSSL_STORE_INFO_get1_PKEY(info);
  221. OSSL_STORE_INFO_free(info);
  222. info = NULL;
  223. }
  224. if (!TEST_ptr(pkey) || !TEST_int_eq(EVP_PKEY_is_a(pkey, "RSA"), 1))
  225. goto end;
  226. ret = 1;
  227. end:
  228. fake_rsa_finish(fake_rsa);
  229. OSSL_PROVIDER_unload(deflt);
  230. OSSL_STORE_close(ctx);
  231. EVP_PKEY_free(pkey);
  232. return ret;
  233. }
  234. int setup_tests(void)
  235. {
  236. libctx = OSSL_LIB_CTX_new();
  237. if (libctx == NULL)
  238. return 0;
  239. ADD_TEST(test_pkey_sig);
  240. ADD_TEST(test_alternative_keygen_init);
  241. ADD_TEST(test_pkey_eq);
  242. ADD_ALL_TESTS(test_pkey_store, 2);
  243. return 1;
  244. }
  245. void cleanup_tests(void)
  246. {
  247. OSSL_LIB_CTX_free(libctx);
  248. }