dh_backend.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. static ossl_inline int dh_bn_dup_check(BIGNUM **out, const BIGNUM *f)
  98. {
  99. if (f != NULL && (*out = BN_dup(f)) == NULL)
  100. return 0;
  101. return 1;
  102. }
  103. DH *ossl_dh_dup(const DH *dh)
  104. {
  105. DH *dupkey = NULL;
  106. #ifndef FIPS_MODULE
  107. /* Do not try to duplicate foreign DH keys */
  108. if (ossl_dh_get_method(dh) != DH_OpenSSL())
  109. return NULL;
  110. #endif
  111. if ((dupkey = ossl_dh_new_ex(dh->libctx)) == NULL)
  112. return NULL;
  113. dupkey->length = DH_get_length(dh);
  114. if (!ossl_ffc_params_copy(&dupkey->params, &dh->params))
  115. goto err;
  116. dupkey->flags = dh->flags;
  117. if (!dh_bn_dup_check(&dupkey->pub_key, dh->pub_key))
  118. goto err;
  119. if (!dh_bn_dup_check(&dupkey->priv_key, dh->priv_key))
  120. goto err;
  121. #ifndef FIPS_MODULE
  122. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DH,
  123. &dupkey->ex_data, &dh->ex_data))
  124. goto err;
  125. #endif
  126. return dupkey;
  127. err:
  128. DH_free(dupkey);
  129. return NULL;
  130. }
  131. #ifndef FIPS_MODULE
  132. DH *ossl_dh_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  133. OSSL_LIB_CTX *libctx, const char *propq)
  134. {
  135. const unsigned char *p, *pm;
  136. int pklen, pmlen;
  137. int ptype;
  138. const void *pval;
  139. const ASN1_STRING *pstr;
  140. const X509_ALGOR *palg;
  141. BIGNUM *privkey_bn = NULL;
  142. ASN1_INTEGER *privkey = NULL;
  143. DH *dh = NULL;
  144. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
  145. return 0;
  146. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  147. if (ptype != V_ASN1_SEQUENCE)
  148. goto decerr;
  149. if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
  150. goto decerr;
  151. pstr = pval;
  152. pm = pstr->data;
  153. pmlen = pstr->length;
  154. switch (OBJ_obj2nid(palg->algorithm)) {
  155. case NID_dhKeyAgreement:
  156. dh = d2i_DHparams(NULL, &pm, pmlen);
  157. break;
  158. case NID_dhpublicnumber:
  159. dh = d2i_DHxparams(NULL, &pm, pmlen);
  160. break;
  161. default:
  162. goto decerr;
  163. }
  164. if (dh == NULL)
  165. goto decerr;
  166. /* We have parameters now set private key */
  167. if ((privkey_bn = BN_secure_new()) == NULL
  168. || !ASN1_INTEGER_to_BN(privkey, privkey_bn)) {
  169. ERR_raise(ERR_LIB_DH, DH_R_BN_ERROR);
  170. BN_clear_free(privkey_bn);
  171. goto dherr;
  172. }
  173. if (!DH_set0_key(dh, NULL, privkey_bn))
  174. goto dherr;
  175. /* Calculate public key, increments dirty_cnt */
  176. if (!DH_generate_key(dh))
  177. goto dherr;
  178. goto done;
  179. decerr:
  180. ERR_raise(ERR_LIB_DH, EVP_R_DECODE_ERROR);
  181. dherr:
  182. DH_free(dh);
  183. dh = NULL;
  184. done:
  185. ASN1_STRING_clear_free(privkey);
  186. return dh;
  187. }
  188. #endif