2
0

dh_backend.c 6.2 KB

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