rsa_backend.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. #include <string.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/params.h>
  12. #include <openssl/evp.h>
  13. #include "internal/sizes.h"
  14. #include "internal/param_build_set.h"
  15. #include "crypto/rsa.h"
  16. #include "e_os.h" /* strcasecmp for Windows() */
  17. /*
  18. * The intention with the "backend" source file is to offer backend support
  19. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  20. * implementations alike.
  21. */
  22. DEFINE_STACK_OF(BIGNUM)
  23. static int collect_numbers(STACK_OF(BIGNUM) *numbers,
  24. const OSSL_PARAM params[], const char *names[])
  25. {
  26. const OSSL_PARAM *p = NULL;
  27. int i;
  28. if (numbers == NULL)
  29. return 0;
  30. for (i = 0; names[i] != NULL; i++){
  31. p = OSSL_PARAM_locate_const(params, names[i]);
  32. if (p != NULL) {
  33. BIGNUM *tmp = NULL;
  34. if (!OSSL_PARAM_get_BN(p, &tmp)
  35. || sk_BIGNUM_push(numbers, tmp) == 0)
  36. return 0;
  37. }
  38. }
  39. return 1;
  40. }
  41. int rsa_fromdata(RSA *rsa, const OSSL_PARAM params[])
  42. {
  43. const OSSL_PARAM *param_n, *param_e, *param_d;
  44. BIGNUM *n = NULL, *e = NULL, *d = NULL;
  45. STACK_OF(BIGNUM) *factors = NULL, *exps = NULL, *coeffs = NULL;
  46. int is_private = 0;
  47. if (rsa == NULL)
  48. return 0;
  49. param_n = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_N);
  50. param_e = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_E);
  51. param_d = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_D);
  52. if ((param_n != NULL && !OSSL_PARAM_get_BN(param_n, &n))
  53. || (param_e != NULL && !OSSL_PARAM_get_BN(param_e, &e))
  54. || (param_d != NULL && !OSSL_PARAM_get_BN(param_d, &d)))
  55. goto err;
  56. is_private = (d != NULL);
  57. if (!RSA_set0_key(rsa, n, e, d))
  58. goto err;
  59. n = e = d = NULL;
  60. if (is_private) {
  61. if (!collect_numbers(factors = sk_BIGNUM_new_null(), params,
  62. rsa_mp_factor_names)
  63. || !collect_numbers(exps = sk_BIGNUM_new_null(), params,
  64. rsa_mp_exp_names)
  65. || !collect_numbers(coeffs = sk_BIGNUM_new_null(), params,
  66. rsa_mp_coeff_names))
  67. goto err;
  68. /* It's ok if this private key just has n, e and d */
  69. if (sk_BIGNUM_num(factors) != 0
  70. && !rsa_set0_all_params(rsa, factors, exps, coeffs))
  71. goto err;
  72. }
  73. sk_BIGNUM_free(factors);
  74. sk_BIGNUM_free(exps);
  75. sk_BIGNUM_free(coeffs);
  76. return 1;
  77. err:
  78. BN_free(n);
  79. BN_free(e);
  80. BN_free(d);
  81. sk_BIGNUM_pop_free(factors, BN_free);
  82. sk_BIGNUM_pop_free(exps, BN_free);
  83. sk_BIGNUM_pop_free(coeffs, BN_free);
  84. return 0;
  85. }
  86. DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
  87. int rsa_todata(RSA *rsa, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
  88. {
  89. int ret = 0;
  90. const BIGNUM *rsa_d = NULL, *rsa_n = NULL, *rsa_e = NULL;
  91. STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null();
  92. STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null();
  93. STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null();
  94. if (rsa == NULL || factors == NULL || exps == NULL || coeffs == NULL)
  95. goto err;
  96. RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
  97. rsa_get0_all_params(rsa, factors, exps, coeffs);
  98. /* Check private key data integrity */
  99. if (rsa_d != NULL) {
  100. int numprimes = sk_BIGNUM_const_num(factors);
  101. int numexps = sk_BIGNUM_const_num(exps);
  102. int numcoeffs = sk_BIGNUM_const_num(coeffs);
  103. /*
  104. * It's permissible to have zero primes, i.e. no CRT params.
  105. * Otherwise, there must be at least two, as many exponents,
  106. * and one coefficient less.
  107. */
  108. if (numprimes != 0
  109. && (numprimes < 2 || numexps < 2 || numcoeffs < 1))
  110. goto err;
  111. }
  112. if (!ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_N, rsa_n)
  113. || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_E, rsa_e)
  114. || !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_RSA_D, rsa_d)
  115. || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_factor_names,
  116. factors)
  117. || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_exp_names,
  118. exps)
  119. || !ossl_param_build_set_multi_key_bn(bld, params, rsa_mp_coeff_names,
  120. coeffs))
  121. goto err;
  122. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  123. /* The acvp test results are not meant for export so check for bld == NULL */
  124. if (bld == NULL)
  125. rsa_acvp_test_get_params(rsa, params);
  126. #endif
  127. ret = 1;
  128. err:
  129. sk_BIGNUM_const_free(factors);
  130. sk_BIGNUM_const_free(exps);
  131. sk_BIGNUM_const_free(coeffs);
  132. return ret;
  133. }
  134. int rsa_pss_params_30_todata(const RSA_PSS_PARAMS_30 *pss, const char *propq,
  135. OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
  136. {
  137. if (!rsa_pss_params_30_is_unrestricted(pss)) {
  138. int hashalg_nid = rsa_pss_params_30_hashalg(pss);
  139. int maskgenalg_nid = rsa_pss_params_30_maskgenalg(pss);
  140. int maskgenhashalg_nid = rsa_pss_params_30_maskgenhashalg(pss);
  141. int saltlen = rsa_pss_params_30_saltlen(pss);
  142. int default_hashalg_nid = rsa_pss_params_30_hashalg(NULL);
  143. int default_maskgenalg_nid = rsa_pss_params_30_maskgenalg(NULL);
  144. int default_maskgenhashalg_nid = rsa_pss_params_30_maskgenhashalg(NULL);
  145. const char *mdname =
  146. (hashalg_nid == default_hashalg_nid
  147. ? NULL : rsa_oaeppss_nid2name(hashalg_nid));
  148. const char *mgfname =
  149. (maskgenalg_nid == default_maskgenalg_nid
  150. ? NULL : rsa_oaeppss_nid2name(maskgenalg_nid));
  151. const char *mgf1mdname =
  152. (maskgenhashalg_nid == default_maskgenhashalg_nid
  153. ? NULL : rsa_oaeppss_nid2name(maskgenhashalg_nid));
  154. const char *key_md = OSSL_PKEY_PARAM_RSA_DIGEST;
  155. const char *key_mgf = OSSL_PKEY_PARAM_RSA_MASKGENFUNC;
  156. const char *key_mgf1_md = OSSL_PKEY_PARAM_RSA_MGF1_DIGEST;
  157. const char *key_saltlen = OSSL_PKEY_PARAM_RSA_PSS_SALTLEN;
  158. /*
  159. * To ensure that the key isn't seen as unrestricted by the recipient,
  160. * we make sure that at least one PSS-related parameter is passed, even
  161. * if it has a default value; saltlen.
  162. */
  163. if ((mdname != NULL
  164. && !ossl_param_build_set_utf8_string(bld, params, key_md, mdname))
  165. || (mgfname != NULL
  166. && !ossl_param_build_set_utf8_string(bld, params,
  167. key_mgf, mgfname))
  168. || (mgf1mdname != NULL
  169. && !ossl_param_build_set_utf8_string(bld, params,
  170. key_mgf1_md, mgf1mdname))
  171. || (!ossl_param_build_set_int(bld, params, key_saltlen, saltlen)))
  172. return 0;
  173. }
  174. return 1;
  175. }
  176. int rsa_pss_params_30_fromdata(RSA_PSS_PARAMS_30 *pss_params,
  177. const OSSL_PARAM params[], OPENSSL_CTX *libctx)
  178. {
  179. const OSSL_PARAM *param_md, *param_mgf, *param_mgf1md, *param_saltlen;
  180. EVP_MD *md = NULL, *mgf1md = NULL;
  181. int saltlen;
  182. int ret = 0;
  183. if (pss_params == NULL)
  184. return 0;
  185. param_md =
  186. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_DIGEST);
  187. param_mgf =
  188. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MASKGENFUNC);
  189. param_mgf1md =
  190. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_MGF1_DIGEST);
  191. param_saltlen =
  192. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_RSA_PSS_SALTLEN);
  193. /*
  194. * If we get any of the parameters, we know we have at least some
  195. * restrictions, so we start by setting default values, and let each
  196. * parameter override their specific restriction data.
  197. */
  198. if (param_md != NULL || param_mgf != NULL || param_mgf1md != NULL
  199. || param_saltlen != NULL)
  200. if (!rsa_pss_params_30_set_defaults(pss_params))
  201. return 0;
  202. if (param_mgf != NULL) {
  203. int default_maskgenalg_nid = rsa_pss_params_30_maskgenalg(NULL);
  204. const char *mgfname = NULL;
  205. if (param_mgf->data_type == OSSL_PARAM_UTF8_STRING)
  206. mgfname = param_mgf->data;
  207. else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgfname))
  208. return 0;
  209. /* TODO Revisit this if / when a new MGF algorithm appears */
  210. if (strcasecmp(param_mgf->data,
  211. rsa_mgf_nid2name(default_maskgenalg_nid)) != 0)
  212. return 0;
  213. }
  214. /*
  215. * We're only interested in the NIDs that correspond to the MDs, so the
  216. * exact propquery is unimportant in the EVP_MD_fetch() calls below.
  217. */
  218. if (param_md != NULL) {
  219. const char *mdname = NULL;
  220. if (param_md->data_type == OSSL_PARAM_UTF8_STRING)
  221. mdname = param_md->data;
  222. else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mdname))
  223. goto err;
  224. if ((md = EVP_MD_fetch(libctx, mdname, NULL)) == NULL
  225. || !rsa_pss_params_30_set_hashalg(pss_params,
  226. rsa_oaeppss_md2nid(md)))
  227. goto err;
  228. }
  229. if (param_mgf1md != NULL) {
  230. const char *mgf1mdname = NULL;
  231. if (param_mgf1md->data_type == OSSL_PARAM_UTF8_STRING)
  232. mgf1mdname = param_mgf1md->data;
  233. else if (!OSSL_PARAM_get_utf8_ptr(param_mgf, &mgf1mdname))
  234. goto err;
  235. if ((mgf1md = EVP_MD_fetch(libctx, mgf1mdname, NULL)) == NULL
  236. || !rsa_pss_params_30_set_maskgenhashalg(pss_params,
  237. rsa_oaeppss_md2nid(mgf1md)))
  238. goto err;
  239. }
  240. if (param_saltlen != NULL) {
  241. if (!OSSL_PARAM_get_int(param_saltlen, &saltlen)
  242. || !rsa_pss_params_30_set_saltlen(pss_params, saltlen))
  243. goto err;
  244. }
  245. ret = 1;
  246. err:
  247. EVP_MD_free(md);
  248. EVP_MD_free(mgf1md);
  249. return ret;
  250. }