dsa_backend.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/core_names.h>
  15. #include <openssl/err.h>
  16. #include "crypto/dsa.h"
  17. #include "dsa_local.h"
  18. /*
  19. * The intention with the "backend" source file is to offer backend support
  20. * for legacy backends (EVP_PKEY_ASN1_METHOD and EVP_PKEY_METHOD) and provider
  21. * implementations alike.
  22. */
  23. int ossl_dsa_key_fromdata(DSA *dsa, const OSSL_PARAM params[])
  24. {
  25. const OSSL_PARAM *param_priv_key, *param_pub_key;
  26. BIGNUM *priv_key = NULL, *pub_key = NULL;
  27. if (dsa == NULL)
  28. return 0;
  29. param_priv_key =
  30. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  31. param_pub_key =
  32. OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  33. /* It's ok if neither half is present */
  34. if (param_priv_key == NULL && param_pub_key == NULL)
  35. return 1;
  36. if (param_pub_key != NULL && !OSSL_PARAM_get_BN(param_pub_key, &pub_key))
  37. goto err;
  38. if (param_priv_key != NULL && !OSSL_PARAM_get_BN(param_priv_key, &priv_key))
  39. goto err;
  40. if (!DSA_set0_key(dsa, pub_key, priv_key))
  41. goto err;
  42. return 1;
  43. err:
  44. BN_clear_free(priv_key);
  45. BN_free(pub_key);
  46. return 0;
  47. }
  48. static ossl_inline int dsa_bn_dup_check(BIGNUM **out, const BIGNUM *f)
  49. {
  50. if (f != NULL && (*out = BN_dup(f)) == NULL)
  51. return 0;
  52. return 1;
  53. }
  54. DSA *ossl_dsa_dup(const DSA *dsa, int selection)
  55. {
  56. DSA *dupkey = NULL;
  57. #ifndef FIPS_MODULE
  58. /* Do not try to duplicate foreign DSA keys */
  59. if (DSA_get_method((DSA *)dsa) != DSA_OpenSSL())
  60. return NULL;
  61. #endif
  62. if ((dupkey = ossl_dsa_new(dsa->libctx)) == NULL)
  63. return NULL;
  64. if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0
  65. && !ossl_ffc_params_copy(&dupkey->params, &dsa->params))
  66. goto err;
  67. dupkey->flags = dsa->flags;
  68. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0
  69. && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
  70. || !dsa_bn_dup_check(&dupkey->pub_key, dsa->pub_key)))
  71. goto err;
  72. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0
  73. && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0
  74. || !dsa_bn_dup_check(&dupkey->priv_key, dsa->priv_key)))
  75. goto err;
  76. #ifndef FIPS_MODULE
  77. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DSA,
  78. &dupkey->ex_data, &dsa->ex_data))
  79. goto err;
  80. #endif
  81. return dupkey;
  82. err:
  83. DSA_free(dupkey);
  84. return NULL;
  85. }
  86. #ifndef FIPS_MODULE
  87. DSA *ossl_dsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  88. OSSL_LIB_CTX *libctx, const char *propq)
  89. {
  90. const unsigned char *p, *pm;
  91. int pklen, pmlen;
  92. int ptype;
  93. const void *pval;
  94. const ASN1_STRING *pstr;
  95. const X509_ALGOR *palg;
  96. ASN1_INTEGER *privkey = NULL;
  97. const BIGNUM *dsa_p, *dsa_g;
  98. BIGNUM *dsa_pubkey = NULL, *dsa_privkey = NULL;
  99. BN_CTX *ctx = NULL;
  100. DSA *dsa = NULL;
  101. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf))
  102. return 0;
  103. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  104. if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
  105. goto decerr;
  106. if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
  107. goto decerr;
  108. pstr = pval;
  109. pm = pstr->data;
  110. pmlen = pstr->length;
  111. if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
  112. goto decerr;
  113. /* We have parameters now set private key */
  114. if ((dsa_privkey = BN_secure_new()) == NULL
  115. || !ASN1_INTEGER_to_BN(privkey, dsa_privkey)) {
  116. ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
  117. goto dsaerr;
  118. }
  119. /* Calculate public key */
  120. if ((dsa_pubkey = BN_new()) == NULL) {
  121. ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
  122. goto dsaerr;
  123. }
  124. if ((ctx = BN_CTX_new()) == NULL) {
  125. ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
  126. goto dsaerr;
  127. }
  128. dsa_p = DSA_get0_p(dsa);
  129. dsa_g = DSA_get0_g(dsa);
  130. BN_set_flags(dsa_privkey, BN_FLG_CONSTTIME);
  131. if (!BN_mod_exp(dsa_pubkey, dsa_g, dsa_privkey, dsa_p, ctx)) {
  132. ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
  133. goto dsaerr;
  134. }
  135. DSA_set0_key(dsa, dsa_pubkey, dsa_privkey);
  136. goto done;
  137. decerr:
  138. ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR);
  139. dsaerr:
  140. BN_free(dsa_privkey);
  141. BN_free(dsa_pubkey);
  142. DSA_free(dsa);
  143. dsa = NULL;
  144. done:
  145. BN_CTX_free(ctx);
  146. ASN1_STRING_clear_free(privkey);
  147. return dsa;
  148. }
  149. #endif