dh_backend.c 6.1 KB

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