keymgmt_internal_test.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright 2019-2020 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 <string.h>
  10. #include <openssl/bio.h>
  11. #include <openssl/bn.h>
  12. #include <openssl/rsa.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/provider.h>
  15. #include <openssl/core_names.h>
  16. #include "internal/core.h"
  17. #include "internal/nelem.h"
  18. #include "crypto/evp.h" /* For the internal API */
  19. #include "testutil.h"
  20. typedef struct {
  21. OSSL_LIB_CTX *ctx1;
  22. OSSL_PROVIDER *prov1;
  23. OSSL_LIB_CTX *ctx2;
  24. OSSL_PROVIDER *prov2;
  25. } FIXTURE;
  26. static void tear_down(FIXTURE *fixture)
  27. {
  28. if (fixture != NULL) {
  29. OSSL_PROVIDER_unload(fixture->prov1);
  30. OSSL_PROVIDER_unload(fixture->prov2);
  31. OSSL_LIB_CTX_free(fixture->ctx1);
  32. OSSL_LIB_CTX_free(fixture->ctx2);
  33. OPENSSL_free(fixture);
  34. }
  35. }
  36. static FIXTURE *set_up(const char *testcase_name)
  37. {
  38. FIXTURE *fixture;
  39. if (!TEST_ptr(fixture = OPENSSL_zalloc(sizeof(*fixture)))
  40. || !TEST_ptr(fixture->ctx1 = OSSL_LIB_CTX_new())
  41. || !TEST_ptr(fixture->prov1 = OSSL_PROVIDER_load(fixture->ctx1,
  42. "default"))
  43. || !TEST_ptr(fixture->ctx2 = OSSL_LIB_CTX_new())
  44. || !TEST_ptr(fixture->prov2 = OSSL_PROVIDER_load(fixture->ctx2,
  45. "default"))) {
  46. tear_down(fixture);
  47. return NULL;
  48. }
  49. return fixture;
  50. }
  51. /* Array indexes */
  52. #define N 0
  53. #define E 1
  54. #define D 2
  55. #define P 3
  56. #define Q 4
  57. #define F3 5 /* Extra factor */
  58. #define DP 6
  59. #define DQ 7
  60. #define E3 8 /* Extra exponent */
  61. #define QINV 9
  62. #define C2 10 /* Extra coefficient */
  63. /*
  64. * We have to do this because OSSL_PARAM_get_ulong() can't handle params
  65. * holding data that isn't exactly sizeof(uint32_t) or sizeof(uint64_t),
  66. * and because the other end deals with BIGNUM, the resulting param might
  67. * be any size. In this particular test, we know that the expected data
  68. * fits within an unsigned long, and we want to get the data in that form
  69. * to make testing of values easier.
  70. */
  71. static int get_ulong_via_BN(const OSSL_PARAM *p, unsigned long *goal)
  72. {
  73. BIGNUM *n = NULL;
  74. int ret = 1; /* Ever so hopeful */
  75. if (!TEST_true(OSSL_PARAM_get_BN(p, &n))
  76. || !TEST_true(BN_bn2nativepad(n, (unsigned char *)goal, sizeof(*goal))))
  77. ret = 0;
  78. BN_free(n);
  79. return ret;
  80. }
  81. static int export_cb(const OSSL_PARAM *params, void *arg)
  82. {
  83. unsigned long *keydata = arg;
  84. const OSSL_PARAM *p = NULL;
  85. if (keydata == NULL)
  86. return 0;
  87. if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N))
  88. || !TEST_true(get_ulong_via_BN(p, &keydata[N]))
  89. || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E))
  90. || !TEST_true(get_ulong_via_BN(p, &keydata[E]))
  91. || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D))
  92. || !TEST_true(get_ulong_via_BN(p, &keydata[D])))
  93. return 0;
  94. if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR1))
  95. || !TEST_true(get_ulong_via_BN(p, &keydata[P]))
  96. || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR2))
  97. || !TEST_true(get_ulong_via_BN(p, &keydata[Q]))
  98. || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_FACTOR3))
  99. || !TEST_true(get_ulong_via_BN(p, &keydata[F3])))
  100. return 0;
  101. if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_EXPONENT1))
  102. || !TEST_true(get_ulong_via_BN(p, &keydata[DP]))
  103. || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_EXPONENT2))
  104. || !TEST_true(get_ulong_via_BN(p, &keydata[DQ]))
  105. || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_EXPONENT3))
  106. || !TEST_true(get_ulong_via_BN(p, &keydata[E3])))
  107. return 0;
  108. if (!TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_COEFFICIENT1))
  109. || !TEST_true(get_ulong_via_BN(p, &keydata[QINV]))
  110. || !TEST_ptr(p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_COEFFICIENT2))
  111. || !TEST_true(get_ulong_via_BN(p, &keydata[C2])))
  112. return 0;
  113. return 1;
  114. }
  115. static int test_pass_rsa(FIXTURE *fixture)
  116. {
  117. size_t i;
  118. int ret = 0;
  119. RSA *rsa = NULL;
  120. BIGNUM *bn1 = NULL, *bn2 = NULL, *bn3 = NULL;
  121. EVP_PKEY *pk = NULL;
  122. EVP_KEYMGMT *km1 = NULL, *km2 = NULL;
  123. void *provkey = NULL;
  124. BIGNUM *bn_primes[1] = { NULL };
  125. BIGNUM *bn_exps[1] = { NULL };
  126. BIGNUM *bn_coeffs[1] = { NULL };
  127. /*
  128. * 32-bit RSA key, extracted from this command,
  129. * executed with OpenSSL 1.0.2:
  130. * An extra factor was added just for testing purposes.
  131. *
  132. * openssl genrsa 32 | openssl rsa -text
  133. */
  134. static BN_ULONG expected[] = {
  135. 0xbc747fc5, /* N */
  136. 0x10001, /* E */
  137. 0x7b133399, /* D */
  138. 0xe963, /* P */
  139. 0xceb7, /* Q */
  140. 1, /* F3 */
  141. 0x8599, /* DP */
  142. 0xbd87, /* DQ */
  143. 2, /* E3 */
  144. 0xcc3b, /* QINV */
  145. 3, /* C3 */
  146. 0 /* Extra, should remain zero */
  147. };
  148. static unsigned long keydata[OSSL_NELEM(expected)] = { 0, };
  149. if (!TEST_ptr(rsa = RSA_new()))
  150. goto err;
  151. if (!TEST_ptr(bn1 = BN_new())
  152. || !TEST_true(BN_set_word(bn1, expected[N]))
  153. || !TEST_ptr(bn2 = BN_new())
  154. || !TEST_true(BN_set_word(bn2, expected[E]))
  155. || !TEST_ptr(bn3 = BN_new())
  156. || !TEST_true(BN_set_word(bn3, expected[D]))
  157. || !TEST_true(RSA_set0_key(rsa, bn1, bn2, bn3)))
  158. goto err;
  159. if (!TEST_ptr(bn1 = BN_new())
  160. || !TEST_true(BN_set_word(bn1, expected[P]))
  161. || !TEST_ptr(bn2 = BN_new())
  162. || !TEST_true(BN_set_word(bn2, expected[Q]))
  163. || !TEST_true(RSA_set0_factors(rsa, bn1, bn2)))
  164. goto err;
  165. if (!TEST_ptr(bn1 = BN_new())
  166. || !TEST_true(BN_set_word(bn1, expected[DP]))
  167. || !TEST_ptr(bn2 = BN_new())
  168. || !TEST_true(BN_set_word(bn2, expected[DQ]))
  169. || !TEST_ptr(bn3 = BN_new())
  170. || !TEST_true(BN_set_word(bn3, expected[QINV]))
  171. || !TEST_true(RSA_set0_crt_params(rsa, bn1, bn2, bn3)))
  172. goto err;
  173. bn1 = bn2 = bn3 = NULL;
  174. if (!TEST_ptr(bn_primes[0] = BN_new())
  175. || !TEST_true(BN_set_word(bn_primes[0], expected[F3]))
  176. || !TEST_ptr(bn_exps[0] = BN_new())
  177. || !TEST_true(BN_set_word(bn_exps[0], expected[E3]))
  178. || !TEST_ptr(bn_coeffs[0] = BN_new())
  179. || !TEST_true(BN_set_word(bn_coeffs[0], expected[C2]))
  180. || !TEST_true(RSA_set0_multi_prime_params(rsa, bn_primes, bn_exps,
  181. bn_coeffs, 1)))
  182. goto err;
  183. if (!TEST_ptr(pk = EVP_PKEY_new())
  184. || !TEST_true(EVP_PKEY_assign_RSA(pk, rsa)))
  185. goto err;
  186. rsa = NULL;
  187. if (!TEST_ptr(km1 = EVP_KEYMGMT_fetch(fixture->ctx1, "RSA", NULL))
  188. || !TEST_ptr(km2 = EVP_KEYMGMT_fetch(fixture->ctx2, "RSA", NULL))
  189. || !TEST_ptr_ne(km1, km2))
  190. goto err;
  191. if (!TEST_ptr(provkey = evp_pkey_export_to_provider(pk, NULL, &km1, NULL))
  192. || !TEST_true(evp_keymgmt_export(km2, provkey,
  193. OSSL_KEYMGMT_SELECT_KEYPAIR,
  194. &export_cb, keydata)))
  195. goto err;
  196. /*
  197. * At this point, the hope is that keydata will have all the numbers
  198. * from the key.
  199. */
  200. for (i = 0; i < OSSL_NELEM(expected); i++) {
  201. int rv = TEST_int_eq(expected[i], keydata[i]);
  202. if (!rv)
  203. TEST_info("i = %zu", i);
  204. else
  205. ret++;
  206. }
  207. ret = (ret == OSSL_NELEM(expected));
  208. err:
  209. RSA_free(rsa);
  210. BN_free(bn1);
  211. BN_free(bn2);
  212. BN_free(bn3);
  213. EVP_PKEY_free(pk);
  214. EVP_KEYMGMT_free(km1);
  215. EVP_KEYMGMT_free(km2);
  216. return ret;
  217. }
  218. static int (*tests[])(FIXTURE *) = {
  219. test_pass_rsa
  220. };
  221. static int test_pass_key(int n)
  222. {
  223. SETUP_TEST_FIXTURE(FIXTURE, set_up);
  224. EXECUTE_TEST(tests[n], tear_down);
  225. return result;
  226. }
  227. int setup_tests(void)
  228. {
  229. ADD_ALL_TESTS(test_pass_key, 1);
  230. return 1;
  231. }