/src/openssl31/crypto/dsa/dsa_backend.c
Line | Count | Source (jump to first uncovered line) |
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 | 50.8k | { |
33 | 50.8k | const OSSL_PARAM *param_priv_key = NULL, *param_pub_key; |
34 | 50.8k | BIGNUM *priv_key = NULL, *pub_key = NULL; |
35 | | |
36 | 50.8k | if (dsa == NULL) |
37 | 0 | return 0; |
38 | | |
39 | 50.8k | if (include_private) { |
40 | 50.8k | param_priv_key = |
41 | 50.8k | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY); |
42 | 50.8k | } |
43 | 50.8k | param_pub_key = |
44 | 50.8k | OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY); |
45 | | |
46 | | /* It's ok if neither half is present */ |
47 | 50.8k | if (param_priv_key == NULL && param_pub_key == NULL) |
48 | 0 | return 1; |
49 | | |
50 | 50.8k | if (param_pub_key != NULL && !OSSL_PARAM_get_BN(param_pub_key, &pub_key)) |
51 | 0 | goto err; |
52 | 50.8k | if (param_priv_key != NULL && !OSSL_PARAM_get_BN(param_priv_key, &priv_key)) |
53 | 0 | goto err; |
54 | | |
55 | 50.8k | if (!DSA_set0_key(dsa, pub_key, priv_key)) |
56 | 0 | goto err; |
57 | | |
58 | 50.8k | return 1; |
59 | | |
60 | 0 | err: |
61 | 0 | BN_clear_free(priv_key); |
62 | 0 | BN_free(pub_key); |
63 | 0 | return 0; |
64 | 50.8k | } |
65 | | |
66 | | int ossl_dsa_is_foreign(const DSA *dsa) |
67 | 90.3k | { |
68 | 90.3k | #ifndef FIPS_MODULE |
69 | 90.3k | if (dsa->engine != NULL || DSA_get_method((DSA *)dsa) != DSA_OpenSSL()) |
70 | 0 | return 1; |
71 | 90.3k | #endif |
72 | 90.3k | return 0; |
73 | 90.3k | } |
74 | | |
75 | | static ossl_inline int dsa_bn_dup_check(BIGNUM **out, const BIGNUM *f) |
76 | 1.86k | { |
77 | 1.86k | if (f != NULL && (*out = BN_dup(f)) == NULL) |
78 | 0 | return 0; |
79 | 1.86k | return 1; |
80 | 1.86k | } |
81 | | |
82 | | DSA *ossl_dsa_dup(const DSA *dsa, int selection) |
83 | 933 | { |
84 | 933 | DSA *dupkey = NULL; |
85 | | |
86 | | /* Do not try to duplicate foreign DSA keys */ |
87 | 933 | if (ossl_dsa_is_foreign(dsa)) |
88 | 0 | return NULL; |
89 | | |
90 | 933 | if ((dupkey = ossl_dsa_new(dsa->libctx)) == NULL) |
91 | 0 | return NULL; |
92 | | |
93 | 933 | if ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) != 0 |
94 | 933 | && !ossl_ffc_params_copy(&dupkey->params, &dsa->params)) |
95 | 0 | goto err; |
96 | | |
97 | 933 | dupkey->flags = dsa->flags; |
98 | | |
99 | 933 | if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0 |
100 | 933 | && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0 |
101 | 933 | || !dsa_bn_dup_check(&dupkey->pub_key, dsa->pub_key))) |
102 | 0 | goto err; |
103 | | |
104 | 933 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0 |
105 | 933 | && ((selection & OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) == 0 |
106 | 933 | || !dsa_bn_dup_check(&dupkey->priv_key, dsa->priv_key))) |
107 | 0 | goto err; |
108 | | |
109 | 933 | #ifndef FIPS_MODULE |
110 | 933 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DSA, |
111 | 933 | &dupkey->ex_data, &dsa->ex_data)) |
112 | 0 | goto err; |
113 | 933 | #endif |
114 | | |
115 | 933 | return dupkey; |
116 | | |
117 | 0 | err: |
118 | 0 | DSA_free(dupkey); |
119 | 0 | return NULL; |
120 | 933 | } |
121 | | |
122 | | #ifndef FIPS_MODULE |
123 | | DSA *ossl_dsa_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf, |
124 | | OSSL_LIB_CTX *libctx, const char *propq) |
125 | 1.99k | { |
126 | 1.99k | const unsigned char *p, *pm; |
127 | 1.99k | int pklen, pmlen; |
128 | 1.99k | int ptype; |
129 | 1.99k | const void *pval; |
130 | 1.99k | const ASN1_STRING *pstr; |
131 | 1.99k | const X509_ALGOR *palg; |
132 | 1.99k | ASN1_INTEGER *privkey = NULL; |
133 | 1.99k | const BIGNUM *dsa_p, *dsa_g; |
134 | 1.99k | BIGNUM *dsa_pubkey = NULL, *dsa_privkey = NULL; |
135 | 1.99k | BN_CTX *ctx = NULL; |
136 | | |
137 | 1.99k | DSA *dsa = NULL; |
138 | | |
139 | 1.99k | if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8inf)) |
140 | 0 | return 0; |
141 | 1.99k | X509_ALGOR_get0(NULL, &ptype, &pval, palg); |
142 | | |
143 | 1.99k | if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) |
144 | 29 | goto decerr; |
145 | 1.96k | if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE) |
146 | 48 | goto decerr; |
147 | | |
148 | 1.92k | pstr = pval; |
149 | 1.92k | pm = pstr->data; |
150 | 1.92k | pmlen = pstr->length; |
151 | 1.92k | if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) |
152 | 19 | goto decerr; |
153 | | /* We have parameters now set private key */ |
154 | 1.90k | if ((dsa_privkey = BN_secure_new()) == NULL |
155 | 1.90k | || !ASN1_INTEGER_to_BN(privkey, dsa_privkey)) { |
156 | 0 | ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR); |
157 | 0 | goto dsaerr; |
158 | 0 | } |
159 | | /* Calculate public key */ |
160 | 1.90k | if ((dsa_pubkey = BN_new()) == NULL) { |
161 | 0 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
162 | 0 | goto dsaerr; |
163 | 0 | } |
164 | 1.90k | if ((ctx = BN_CTX_new()) == NULL) { |
165 | 0 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
166 | 0 | goto dsaerr; |
167 | 0 | } |
168 | | |
169 | 1.90k | dsa_p = DSA_get0_p(dsa); |
170 | 1.90k | dsa_g = DSA_get0_g(dsa); |
171 | 1.90k | BN_set_flags(dsa_privkey, BN_FLG_CONSTTIME); |
172 | 1.90k | if (!BN_mod_exp(dsa_pubkey, dsa_g, dsa_privkey, dsa_p, ctx)) { |
173 | 25 | ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR); |
174 | 25 | goto dsaerr; |
175 | 25 | } |
176 | 1.87k | if (!DSA_set0_key(dsa, dsa_pubkey, dsa_privkey)) { |
177 | 0 | ERR_raise(ERR_LIB_DSA, ERR_R_INTERNAL_ERROR); |
178 | 0 | goto dsaerr; |
179 | 0 | } |
180 | | |
181 | 1.87k | goto done; |
182 | | |
183 | 1.87k | decerr: |
184 | 96 | ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR); |
185 | 121 | dsaerr: |
186 | 121 | BN_free(dsa_privkey); |
187 | 121 | BN_free(dsa_pubkey); |
188 | 121 | DSA_free(dsa); |
189 | 121 | dsa = NULL; |
190 | 1.99k | done: |
191 | 1.99k | BN_CTX_free(ctx); |
192 | 1.99k | ASN1_STRING_clear_free(privkey); |
193 | 1.99k | return dsa; |
194 | 121 | } |
195 | | #endif |