dh_backend.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * Copyright 2020-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. /*
  10. * DH low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/err.h>
  15. #include <openssl/core_names.h>
  16. #ifndef FIPS_MODULE
  17. # include <openssl/x509.h>
  18. #endif
  19. #include "internal/param_build_set.h"
  20. #include "crypto/dh.h"
  21. #include "dh_local.h"
  22. /*
  23. * The intention with the "backend" source file is to offer backend functions
  24. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  25. * implementations alike.
  26. */
  27. static int dh_ffc_params_fromdata(DH *dh, const OSSL_PARAM params[])
  28. {
  29. int ret;
  30. FFC_PARAMS *ffc = ossl_dh_get0_params(dh);
  31. ret = ossl_ffc_params_fromdata(ffc, params);
  32. if (ret)
  33. ossl_dh_cache_named_group(dh); /* This increments dh->dirty_cnt */
  34. return ret;
  35. }
  36. int ossl_dh_params_fromdata(DH *dh, const OSSL_PARAM params[])
  37. {
  38. const OSSL_PARAM *param_priv_len;
  39. long priv_len;
  40. if (!dh_ffc_params_fromdata(dh, params))
  41. return 0;
  42. param_priv_len =
  43. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_DH_PRIV_LEN);
  44. if (param_priv_len != NULL
  45. && (!OSSL_PARAM_get_long(param_priv_len, &priv_len)
  46. || !DH_set_length(dh, priv_len)))
  47. return 0;
  48. return 1;
  49. }
  50. int ossl_dh_key_fromdata(DH *dh, const OSSL_PARAM params[], int include_private)
  51. {
  52. const OSSL_PARAM *param_priv_key, *param_pub_key;
  53. BIGNUM *priv_key = NULL, *pub_key = NULL;
  54. if (dh == NULL)
  55. return 0;
  56. param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  57. param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  58. if (include_private
  59. && param_priv_key != NULL
  60. && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
  61. goto err;
  62. if (param_pub_key != NULL
  63. && !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
  64. goto err;
  65. if (!DH_set0_key(dh, pub_key, priv_key))
  66. goto err;
  67. return 1;
  68. err:
  69. BN_clear_free(priv_key);
  70. BN_free(pub_key);
  71. return 0;
  72. }
  73. int ossl_dh_params_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[])
  74. {
  75. long l = DH_get_length(dh);
  76. if (!ossl_ffc_params_todata(ossl_dh_get0_params(dh), bld, params))
  77. return 0;
  78. if (l > 0
  79. && !ossl_param_build_set_long(bld, params, OSSL_PKEY_PARAM_DH_PRIV_LEN, l))
  80. return 0;
  81. return 1;
  82. }
  83. int ossl_dh_key_todata(DH *dh, OSSL_PARAM_BLD *bld, OSSL_PARAM params[],
  84. int include_private)
  85. {
  86. const BIGNUM *priv = NULL, *pub = NULL;
  87. if (dh == NULL)
  88. return 0;
  89. DH_get0_key(dh, &pub, &priv);
  90. if (priv != NULL
  91. && include_private
  92. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PRIV_KEY, priv))
  93. return 0;
  94. if (pub != NULL
  95. && !ossl_param_build_set_bn(bld, params, OSSL_PKEY_PARAM_PUB_KEY, pub))
  96. return 0;
  97. return 1;
  98. }
  99. int ossl_dh_is_foreign(const DH *dh)
  100. {
  101. #ifndef FIPS_MODULE
  102. if (dh->engine != NULL || ossl_dh_get_method(dh) != DH_OpenSSL())
  103. return 1;
  104. #endif
  105. return 0;
  106. }
  107. static ossl_inline int dh_bn_dup_check(BIGNUM **out, const BIGNUM *f)
  108. {
  109. if (f != NULL && (*out = BN_dup(f)) == NULL)
  110. return 0;
  111. return 1;
  112. }
  113. DH *ossl_dh_dup(const DH *dh, int selection)
  114. {
  115. DH *dupkey = NULL;
  116. /* Do not try to duplicate foreign DH keys */
  117. if (ossl_dh_is_foreign(dh))
  118. return NULL;
  119. if ((dupkey = ossl_dh_new_ex(dh->libctx)) == NULL)
  120. return NULL;
  121. dupkey->length = DH_get_length(dh);
  122. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0
  123. && !ossl_ffc_params_copy(&dupkey->params, &dh->params))
  124. goto err;
  125. dupkey->flags = dh->flags;
  126. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
  127. && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
  128. || !dh_bn_dup_check(&dupkey->pub_key, dh->pub_key)))
  129. goto err;
  130. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  131. && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
  132. || !dh_bn_dup_check(&dupkey->priv_key, dh->priv_key)))
  133. goto err;
  134. #ifndef FIPS_MODULE
  135. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DH,
  136. &dupkey->ex_data, &dh->ex_data))
  137. goto err;
  138. #endif
  139. return dupkey;
  140. err:
  141. DH_free(dupkey);
  142. return NULL;
  143. }
  144. #ifndef FIPS_MODULE
  145. DH *ossl_dh_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  146. OSSL_LIB_CTX *libctx, const char *propq)
  147. {
  148. const unsigned char *p, *pm;
  149. int pklen, pmlen;
  150. int ptype;
  151. const void *pval;
  152. const ASN1_STRING *pstr;
  153. const X509_ALGOR *palg;
  154. BIGNUM *privkey_bn = NULL;
  155. ASN1_INTEGER *privkey = NULL;
  156. DH *dh = NULL;
  157. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
  158. return 0;
  159. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  160. if (ptype != V_ASN1_SEQUENCE)
  161. goto decerr;
  162. if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
  163. goto decerr;
  164. pstr = pval;
  165. pm = pstr->data;
  166. pmlen = pstr->length;
  167. switch (OBJ_obj2nid(palg->algorithm)) {
  168. case NID_dhKeyAgreement:
  169. dh = d2i_DHparams(NULL, &pm, pmlen);
  170. break;
  171. case NID_dhpublicnumber:
  172. dh = d2i_DHxparams(NULL, &pm, pmlen);
  173. break;
  174. default:
  175. goto decerr;
  176. }
  177. if (dh == NULL)
  178. goto decerr;
  179. /* We have parameters now set private key */
  180. if ((privkey_bn = BN_secure_new()) == NULL
  181. || !ASN1_INTEGER_to_BN(privkey, privkey_bn)) {
  182. ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);
  183. BN_clear_free(privkey_bn);
  184. goto dherr;
  185. }
  186. if (!DH_set0_key(dh, NULL, privkey_bn))
  187. goto dherr;
  188. /* Calculate public key, increments dirty_cnt */
  189. if (!DH_generate_key(dh))
  190. goto dherr;
  191. goto done;
  192. decerr:
  193. ERR_raise(ERR_LIB_DH, EVP_R_DECODE_ERROR);
  194. dherr:
  195. DH_free(dh);
  196. dh = NULL;
  197. done:
  198. ASN1_STRING_clear_free(privkey);
  199. return dh;
  200. }
  201. #endif