evp_libctx_test.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright 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. /*
  10. * These tests are setup to load null into the default library context.
  11. * Any tests are expected to use the created 'libctx' to find algorithms.
  12. * The framework runs the tests twice using the 'default' provider or
  13. * 'fips' provider as inputs.
  14. */
  15. /*
  16. * DSA/DH low level APIs are deprecated for public use, but still ok for
  17. * internal use.
  18. */
  19. #include "internal/deprecated.h"
  20. #include <openssl/evp.h>
  21. #include <openssl/provider.h>
  22. #include <openssl/dsa.h>
  23. #include "testutil.h"
  24. #include "internal/nelem.h"
  25. #include "crypto/bn_dh.h" /* _bignum_ffdhe2048_p */
  26. static OPENSSL_CTX *libctx = NULL;
  27. static OSSL_PROVIDER *nullprov = NULL;
  28. static OSSL_PROVIDER *libprov = NULL;
  29. typedef enum OPTION_choice {
  30. OPT_ERR = -1,
  31. OPT_EOF = 0,
  32. OPT_CONFIG_FILE,
  33. OPT_PROVIDER_NAME,
  34. OPT_TEST_ENUM
  35. } OPTION_CHOICE;
  36. const OPTIONS *test_get_options(void)
  37. {
  38. static const OPTIONS test_options[] = {
  39. OPT_TEST_OPTIONS_DEFAULT_USAGE,
  40. { "config", OPT_CONFIG_FILE, '<',
  41. "The configuration file to use for the libctx" },
  42. { "provider", OPT_PROVIDER_NAME, 's',
  43. "The provider to load (The default value is 'default'" },
  44. { NULL }
  45. };
  46. return test_options;
  47. }
  48. #if !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_DH)
  49. static const char *getname(int id)
  50. {
  51. const char *name[] = {"p", "q", "g" };
  52. if (id >= 0 && id < 3)
  53. return name[id];
  54. return "?";
  55. }
  56. #endif
  57. #ifndef OPENSSL_NO_DSA
  58. static int test_dsa_param_keygen(int tstid)
  59. {
  60. int ret = 0;
  61. int expected;
  62. EVP_PKEY_CTX *gen_ctx = NULL;
  63. EVP_PKEY *pkey_parm = NULL;
  64. EVP_PKEY *pkey = NULL;
  65. DSA *dsa = NULL;
  66. int pind, qind, gind;
  67. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  68. /*
  69. * Just grab some fixed dh p, q, g values for testing,
  70. * these 'safe primes' should not be used normally for dsa *.
  71. */
  72. static const BIGNUM *bn[] = {
  73. &_bignum_dh2048_256_p, &_bignum_dh2048_256_q, &_bignum_dh2048_256_g
  74. };
  75. /*
  76. * These tests are using bad values for p, q, g by reusing the values.
  77. * A value of 0 uses p, 1 uses q and 2 uses g.
  78. * There are 27 different combinations, with only the 1 valid combination.
  79. */
  80. pind = tstid / 9;
  81. qind = (tstid / 3) % 3;
  82. gind = tstid % 3;
  83. expected = (pind == 0 && qind == 1 && gind == 2);
  84. TEST_note("Testing with (p, q, g) = (%s, %s, %s)\n", getname(pind),
  85. getname(qind), getname(gind));
  86. if (!TEST_ptr(pkey_parm = EVP_PKEY_new())
  87. || !TEST_ptr(dsa = DSA_new())
  88. || !TEST_ptr(p = BN_dup(bn[pind]))
  89. || !TEST_ptr(q = BN_dup(bn[qind]))
  90. || !TEST_ptr(g = BN_dup(bn[gind]))
  91. || !TEST_true(DSA_set0_pqg(dsa, p, q, g)))
  92. goto err;
  93. p = q = g = NULL;
  94. if (!TEST_true(EVP_PKEY_assign_DSA(pkey_parm, dsa)))
  95. goto err;
  96. dsa = NULL;
  97. if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL))
  98. || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0)
  99. || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected))
  100. goto err;
  101. ret = 1;
  102. err:
  103. EVP_PKEY_free(pkey);
  104. EVP_PKEY_CTX_free(gen_ctx);
  105. EVP_PKEY_free(pkey_parm);
  106. DSA_free(dsa);
  107. BN_free(g);
  108. BN_free(q);
  109. BN_free(p);
  110. return ret;
  111. }
  112. #endif /* OPENSSL_NO_DSA */
  113. #ifndef OPENSSL_NO_DH
  114. static int do_dh_param_keygen(int tstid, const BIGNUM **bn)
  115. {
  116. int ret = 0;
  117. int expected;
  118. EVP_PKEY_CTX *gen_ctx = NULL;
  119. EVP_PKEY *pkey_parm = NULL;
  120. EVP_PKEY *pkey = NULL;
  121. DH *dh = NULL;
  122. int pind, qind, gind;
  123. BIGNUM *p = NULL, *q = NULL, *g = NULL;
  124. /*
  125. * These tests are using bad values for p, q, g by reusing the values.
  126. * A value of 0 uses p, 1 uses q and 2 uses g.
  127. * There are 27 different combinations, with only the 1 valid combination.
  128. */
  129. pind = tstid / 9;
  130. qind = (tstid / 3) % 3;
  131. gind = tstid % 3;
  132. expected = (pind == 0 && qind == 1 && gind == 2);
  133. TEST_note("Testing with (p, q, g) = (%s, %s, %s)", getname(pind),
  134. getname(qind), getname(gind));
  135. if (!TEST_ptr(pkey_parm = EVP_PKEY_new())
  136. || !TEST_ptr(dh = DH_new())
  137. || !TEST_ptr(p = BN_dup(bn[pind]))
  138. || !TEST_ptr(q = BN_dup(bn[qind]))
  139. || !TEST_ptr(g = BN_dup(bn[gind]))
  140. || !TEST_true(DH_set0_pqg(dh, p, q, g)))
  141. goto err;
  142. p = q = g = NULL;
  143. if (!TEST_true(EVP_PKEY_assign_DH(pkey_parm, dh)))
  144. goto err;
  145. dh = NULL;
  146. if (!TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey_parm, NULL))
  147. || !TEST_int_gt(EVP_PKEY_keygen_init(gen_ctx), 0)
  148. || !TEST_int_eq(EVP_PKEY_keygen(gen_ctx, &pkey), expected))
  149. goto err;
  150. ret = 1;
  151. err:
  152. EVP_PKEY_free(pkey);
  153. EVP_PKEY_CTX_free(gen_ctx);
  154. EVP_PKEY_free(pkey_parm);
  155. DH_free(dh);
  156. BN_free(g);
  157. BN_free(q);
  158. BN_free(p);
  159. return ret;
  160. }
  161. /*
  162. * Note that we get the fips186-4 path being run for most of these cases since
  163. * the internal code will detect that the p, q, g does not match a safe prime
  164. * group (Except for when tstid = 5, which sets the correct p, q, g)
  165. */
  166. static int test_dh_safeprime_param_keygen(int tstid)
  167. {
  168. static const BIGNUM *bn[] = {
  169. &_bignum_ffdhe2048_p, &_bignum_ffdhe2048_q, &_bignum_const_2
  170. };
  171. return do_dh_param_keygen(tstid, bn);
  172. }
  173. #endif /* OPENSSL_NO_DH */
  174. int setup_tests(void)
  175. {
  176. const char *prov_name = "default";
  177. char *config_file = NULL;
  178. OPTION_CHOICE o;
  179. while ((o = opt_next()) != OPT_EOF) {
  180. switch (o) {
  181. case OPT_PROVIDER_NAME:
  182. prov_name = opt_arg();
  183. break;
  184. case OPT_CONFIG_FILE:
  185. config_file = opt_arg();
  186. break;
  187. case OPT_TEST_CASES:
  188. break;
  189. default:
  190. case OPT_ERR:
  191. return 0;
  192. }
  193. }
  194. nullprov = OSSL_PROVIDER_load(NULL, "null");
  195. if (!TEST_ptr(nullprov))
  196. return 0;
  197. libctx = OPENSSL_CTX_new();
  198. if (!TEST_ptr(libctx))
  199. return 0;
  200. if (config_file != NULL) {
  201. if (!TEST_true(OPENSSL_CTX_load_config(libctx, config_file)))
  202. return 0;
  203. }
  204. libprov = OSSL_PROVIDER_load(libctx, prov_name);
  205. if (!TEST_ptr(libprov))
  206. return 0;
  207. #ifndef OPENSSL_NO_DSA
  208. ADD_ALL_TESTS(test_dsa_param_keygen, 3 * 3 * 3);
  209. #endif
  210. #ifndef OPENSSL_NO_DH
  211. ADD_ALL_TESTS(test_dh_safeprime_param_keygen, 3 * 3 * 3);
  212. #endif
  213. return 1;
  214. }
  215. void cleanup_tests(void)
  216. {
  217. OSSL_PROVIDER_unload(libprov);
  218. OPENSSL_CTX_free(libctx);
  219. OSSL_PROVIDER_unload(nullprov);
  220. }