Debian Codegraph @

debian/rust-vendor/openssl-src/openssl/crypto/dsa/dsa_backend.c

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