]>
Commit | Line | Data |
---|---|---|
0f113f3e | 1 | /* |
4333b89f | 2 | * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved. |
448be743 | 3 | * |
3cdbea65 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
d2e9e320 RS |
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 | |
448be743 DSH |
8 | */ |
9 | ||
f41ac0ee P |
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 | ||
448be743 | 16 | #include <stdio.h> |
448be743 DSH |
17 | #include <openssl/x509.h> |
18 | #include <openssl/asn1.h> | |
1e26a8ba | 19 | #include <openssl/bn.h> |
4889dadc | 20 | #include <openssl/core_names.h> |
b03ec3b5 | 21 | #include <openssl/param_build.h> |
4889dadc | 22 | #include "internal/cryptlib.h" |
25f2138b | 23 | #include "crypto/asn1.h" |
0abae163 | 24 | #include "crypto/dsa.h" |
25f2138b | 25 | #include "crypto/evp.h" |
0abae163 | 26 | #include "internal/ffc.h" |
706457b7 | 27 | #include "dsa_local.h" |
448be743 | 28 | |
7674e923 | 29 | static int dsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey) |
0f113f3e MC |
30 | { |
31 | const unsigned char *p, *pm; | |
32 | int pklen, pmlen; | |
33 | int ptype; | |
ac4e2577 DSH |
34 | const void *pval; |
35 | const ASN1_STRING *pstr; | |
0f113f3e MC |
36 | X509_ALGOR *palg; |
37 | ASN1_INTEGER *public_key = NULL; | |
38 | ||
39 | DSA *dsa = NULL; | |
40 | ||
41 | if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey)) | |
42 | return 0; | |
43 | X509_ALGOR_get0(NULL, &ptype, &pval, palg); | |
44 | ||
45 | if (ptype == V_ASN1_SEQUENCE) { | |
46 | pstr = pval; | |
47 | pm = pstr->data; | |
48 | pmlen = pstr->length; | |
49 | ||
75ebbd9a | 50 | if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) { |
9311d0c4 | 51 | ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR); |
0f113f3e MC |
52 | goto err; |
53 | } | |
54 | ||
55 | } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) { | |
75ebbd9a | 56 | if ((dsa = DSA_new()) == NULL) { |
9311d0c4 | 57 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
58 | goto err; |
59 | } | |
60 | } else { | |
9311d0c4 | 61 | ERR_raise(ERR_LIB_DSA, DSA_R_PARAMETER_ENCODING_ERROR); |
0f113f3e MC |
62 | goto err; |
63 | } | |
64 | ||
75ebbd9a | 65 | if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) { |
9311d0c4 | 66 | ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR); |
0f113f3e MC |
67 | goto err; |
68 | } | |
69 | ||
75ebbd9a | 70 | if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) { |
9311d0c4 | 71 | ERR_raise(ERR_LIB_DSA, DSA_R_BN_DECODE_ERROR); |
0f113f3e MC |
72 | goto err; |
73 | } | |
74 | ||
4889dadc | 75 | dsa->dirty_cnt++; |
0f113f3e MC |
76 | ASN1_INTEGER_free(public_key); |
77 | EVP_PKEY_assign_DSA(pkey, dsa); | |
78 | return 1; | |
79 | ||
80 | err: | |
2ace7450 | 81 | ASN1_INTEGER_free(public_key); |
d6407083 | 82 | DSA_free(dsa); |
0f113f3e MC |
83 | return 0; |
84 | ||
85 | } | |
448be743 | 86 | |
6f81892e | 87 | static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey) |
0f113f3e MC |
88 | { |
89 | DSA *dsa; | |
0f113f3e MC |
90 | int ptype; |
91 | unsigned char *penc = NULL; | |
92 | int penclen; | |
0c7ca403 | 93 | ASN1_STRING *str = NULL; |
ea6b07b5 | 94 | ASN1_INTEGER *pubint = NULL; |
7760384b | 95 | ASN1_OBJECT *aobj; |
0f113f3e MC |
96 | |
97 | dsa = pkey->pkey.dsa; | |
dc8de3e6 SL |
98 | if (pkey->save_parameters |
99 | && dsa->params.p != NULL | |
100 | && dsa->params.q != NULL | |
101 | && dsa->params.g != NULL) { | |
0f113f3e | 102 | str = ASN1_STRING_new(); |
90945fa3 | 103 | if (str == NULL) { |
9311d0c4 | 104 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
0c7ca403 MC |
105 | goto err; |
106 | } | |
0f113f3e MC |
107 | str->length = i2d_DSAparams(dsa, &str->data); |
108 | if (str->length <= 0) { | |
9311d0c4 | 109 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
110 | goto err; |
111 | } | |
0f113f3e MC |
112 | ptype = V_ASN1_SEQUENCE; |
113 | } else | |
114 | ptype = V_ASN1_UNDEF; | |
115 | ||
ea6b07b5 | 116 | pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL); |
0f113f3e | 117 | |
ea6b07b5 | 118 | if (pubint == NULL) { |
9311d0c4 | 119 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
ea6b07b5 DSH |
120 | goto err; |
121 | } | |
122 | ||
123 | penclen = i2d_ASN1_INTEGER(pubint, &penc); | |
124 | ASN1_INTEGER_free(pubint); | |
0f113f3e MC |
125 | |
126 | if (penclen <= 0) { | |
9311d0c4 | 127 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
128 | goto err; |
129 | } | |
130 | ||
7760384b PK |
131 | aobj = OBJ_nid2obj(EVP_PKEY_DSA); |
132 | if (aobj == NULL) | |
133 | goto err; | |
134 | ||
135 | if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen)) | |
0f113f3e MC |
136 | return 1; |
137 | ||
138 | err: | |
b548a1f1 | 139 | OPENSSL_free(penc); |
0dfb9398 | 140 | ASN1_STRING_free(str); |
0f113f3e MC |
141 | |
142 | return 0; | |
143 | } | |
144 | ||
145 | /* | |
146 | * In PKCS#8 DSA: you just get a private key integer and parameters in the | |
448be743 DSH |
147 | * AlgorithmIdentifier the pubkey must be recalculated. |
148 | */ | |
0f113f3e | 149 | |
245c6bc3 | 150 | static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8) |
0f113f3e | 151 | { |
ab4a81f6 | 152 | int ret = 0; |
cf333799 | 153 | DSA *dsa = ossl_dsa_key_from_pkcs8(p8, NULL, NULL); |
ab4a81f6 | 154 | |
cf333799 RL |
155 | if (dsa != NULL) { |
156 | ret = 1; | |
157 | EVP_PKEY_assign_DSA(pkey, dsa); | |
0f113f3e MC |
158 | } |
159 | ||
ab4a81f6 | 160 | return ret; |
0f113f3e | 161 | } |
448be743 | 162 | |
6f81892e | 163 | static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey) |
448be743 | 164 | { |
0f113f3e MC |
165 | ASN1_STRING *params = NULL; |
166 | ASN1_INTEGER *prkey = NULL; | |
167 | unsigned char *dp = NULL; | |
168 | int dplen; | |
169 | ||
12a765a5 | 170 | if (pkey->pkey.dsa == NULL|| pkey->pkey.dsa->priv_key == NULL) { |
9311d0c4 | 171 | ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS); |
0f113f3e MC |
172 | goto err; |
173 | } | |
174 | ||
175 | params = ASN1_STRING_new(); | |
176 | ||
90945fa3 | 177 | if (params == NULL) { |
9311d0c4 | 178 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
179 | goto err; |
180 | } | |
181 | ||
182 | params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data); | |
183 | if (params->length <= 0) { | |
9311d0c4 | 184 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); |
0f113f3e MC |
185 | goto err; |
186 | } | |
187 | params->type = V_ASN1_SEQUENCE; | |
188 | ||
189 | /* Get private key into integer */ | |
190 | prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL); | |
191 | ||
12a765a5 | 192 | if (prkey == NULL) { |
9311d0c4 | 193 | ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR); |
0f113f3e MC |
194 | goto err; |
195 | } | |
196 | ||
197 | dplen = i2d_ASN1_INTEGER(prkey, &dp); | |
198 | ||
a8ae0891 | 199 | ASN1_STRING_clear_free(prkey); |
fa4629b6 | 200 | prkey = NULL; |
0f113f3e MC |
201 | |
202 | if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0, | |
203 | V_ASN1_SEQUENCE, params, dp, dplen)) | |
204 | goto err; | |
205 | ||
206 | return 1; | |
207 | ||
208 | err: | |
b548a1f1 | 209 | OPENSSL_free(dp); |
0dfb9398 | 210 | ASN1_STRING_free(params); |
2ace7450 | 211 | ASN1_STRING_clear_free(prkey); |
0f113f3e | 212 | return 0; |
448be743 DSH |
213 | } |
214 | ||
6f81892e | 215 | static int int_dsa_size(const EVP_PKEY *pkey) |
0f113f3e | 216 | { |
26a7d938 | 217 | return DSA_size(pkey->pkey.dsa); |
0f113f3e | 218 | } |
6f81892e DSH |
219 | |
220 | static int dsa_bits(const EVP_PKEY *pkey) | |
0f113f3e | 221 | { |
5d8d9a8e | 222 | return DSA_bits(pkey->pkey.dsa); |
0f113f3e | 223 | } |
6f81892e | 224 | |
2514fa79 | 225 | static int dsa_security_bits(const EVP_PKEY *pkey) |
0f113f3e MC |
226 | { |
227 | return DSA_security_bits(pkey->pkey.dsa); | |
228 | } | |
2514fa79 | 229 | |
6f81892e | 230 | static int dsa_missing_parameters(const EVP_PKEY *pkey) |
0f113f3e MC |
231 | { |
232 | DSA *dsa; | |
233 | dsa = pkey->pkey.dsa; | |
dc8de3e6 SL |
234 | return dsa == NULL |
235 | || dsa->params.p == NULL | |
236 | || dsa->params.q == NULL | |
237 | || dsa->params.g == NULL; | |
0f113f3e | 238 | } |
6f81892e DSH |
239 | |
240 | static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from) | |
0f113f3e | 241 | { |
2986ecdc DSH |
242 | if (to->pkey.dsa == NULL) { |
243 | to->pkey.dsa = DSA_new(); | |
244 | if (to->pkey.dsa == NULL) | |
245 | return 0; | |
246 | } | |
5357c106 | 247 | if (!ossl_ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params)) |
0f113f3e | 248 | return 0; |
0f113f3e | 249 | |
4889dadc | 250 | to->pkey.dsa->dirty_cnt++; |
0f113f3e MC |
251 | return 1; |
252 | } | |
6f81892e DSH |
253 | |
254 | static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b) | |
0f113f3e | 255 | { |
5357c106 | 256 | return ossl_ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1); |
0f113f3e | 257 | } |
6f81892e | 258 | |
0cb8499b | 259 | static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b) |
0f113f3e | 260 | { |
dc8de3e6 | 261 | return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0; |
0f113f3e | 262 | } |
0cb8499b | 263 | |
6f81892e | 264 | static void int_dsa_free(EVP_PKEY *pkey) |
0f113f3e MC |
265 | { |
266 | DSA_free(pkey->pkey.dsa); | |
267 | } | |
6f81892e | 268 | |
777c47ac | 269 | static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) |
0f113f3e | 270 | { |
0f113f3e | 271 | int ret = 0; |
0f113f3e | 272 | const char *ktype = NULL; |
0f113f3e | 273 | const BIGNUM *priv_key, *pub_key; |
aecf529b | 274 | int mod_len = 0; |
275 | ||
dc8de3e6 SL |
276 | if (x->params.p != NULL) |
277 | mod_len = DSA_bits(x); | |
0f113f3e MC |
278 | |
279 | if (ptype == 2) | |
280 | priv_key = x->priv_key; | |
281 | else | |
282 | priv_key = NULL; | |
283 | ||
284 | if (ptype > 0) | |
285 | pub_key = x->pub_key; | |
286 | else | |
287 | pub_key = NULL; | |
288 | ||
289 | if (ptype == 2) | |
290 | ktype = "Private-Key"; | |
291 | else if (ptype == 1) | |
292 | ktype = "Public-Key"; | |
293 | else | |
294 | ktype = "DSA-Parameters"; | |
295 | ||
dc8de3e6 | 296 | if (priv_key != NULL) { |
0f113f3e MC |
297 | if (!BIO_indent(bp, off, 128)) |
298 | goto err; | |
dc8de3e6 | 299 | if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0) |
0f113f3e | 300 | goto err; |
aecf529b | 301 | } else { |
302 | if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0) | |
303 | goto err; | |
0f113f3e MC |
304 | } |
305 | ||
a773b52a | 306 | if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off)) |
0f113f3e | 307 | goto err; |
a773b52a | 308 | if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off)) |
0f113f3e | 309 | goto err; |
5357c106 | 310 | if (!ossl_ffc_params_print(bp, &x->params, off)) |
0f113f3e MC |
311 | goto err; |
312 | ret = 1; | |
313 | err: | |
26a7d938 | 314 | return ret; |
0f113f3e | 315 | } |
35208f36 | 316 | |
3e4585c8 | 317 | static int dsa_param_decode(EVP_PKEY *pkey, |
0f113f3e MC |
318 | const unsigned char **pder, int derlen) |
319 | { | |
320 | DSA *dsa; | |
75ebbd9a | 321 | |
1dc51285 | 322 | if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) |
0f113f3e | 323 | return 0; |
1dc51285 | 324 | |
4889dadc | 325 | dsa->dirty_cnt++; |
0f113f3e MC |
326 | EVP_PKEY_assign_DSA(pkey, dsa); |
327 | return 1; | |
328 | } | |
3e4585c8 DSH |
329 | |
330 | static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
0f113f3e MC |
331 | { |
332 | return i2d_DSAparams(pkey->pkey.dsa, pder); | |
333 | } | |
35208f36 DSH |
334 | |
335 | static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
336 | ASN1_PCTX *ctx) |
337 | { | |
338 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 0); | |
339 | } | |
35208f36 DSH |
340 | |
341 | static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
342 | ASN1_PCTX *ctx) |
343 | { | |
344 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 1); | |
345 | } | |
35208f36 DSH |
346 | |
347 | static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent, | |
0f113f3e MC |
348 | ASN1_PCTX *ctx) |
349 | { | |
350 | return do_dsa_print(bp, pkey->pkey.dsa, indent, 2); | |
351 | } | |
35208f36 | 352 | |
e4263314 | 353 | static int old_dsa_priv_decode(EVP_PKEY *pkey, |
0f113f3e MC |
354 | const unsigned char **pder, int derlen) |
355 | { | |
356 | DSA *dsa; | |
75ebbd9a RS |
357 | |
358 | if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) { | |
9311d0c4 | 359 | ERR_raise(ERR_LIB_DSA, ERR_R_DSA_LIB); |
0f113f3e MC |
360 | return 0; |
361 | } | |
4889dadc | 362 | dsa->dirty_cnt++; |
0f113f3e MC |
363 | EVP_PKEY_assign_DSA(pkey, dsa); |
364 | return 1; | |
365 | } | |
e4263314 DSH |
366 | |
367 | static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder) | |
0f113f3e MC |
368 | { |
369 | return i2d_DSAPrivateKey(pkey->pkey.dsa, pder); | |
370 | } | |
e4263314 | 371 | |
fa1ba589 | 372 | static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg, |
0f113f3e MC |
373 | const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx) |
374 | { | |
375 | DSA_SIG *dsa_sig; | |
376 | const unsigned char *p; | |
a773b52a | 377 | |
dc8de3e6 | 378 | if (sig == NULL) { |
0f113f3e MC |
379 | if (BIO_puts(bp, "\n") <= 0) |
380 | return 0; | |
381 | else | |
382 | return 1; | |
383 | } | |
384 | p = sig->data; | |
385 | dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length); | |
dc8de3e6 | 386 | if (dsa_sig != NULL) { |
0f113f3e | 387 | int rv = 0; |
9267c11b | 388 | const BIGNUM *r, *s; |
706a13f1 | 389 | |
9267c11b | 390 | DSA_SIG_get0(dsa_sig, &r, &s); |
0f113f3e MC |
391 | |
392 | if (BIO_write(bp, "\n", 1) != 1) | |
393 | goto err; | |
394 | ||
706a13f1 | 395 | if (!ASN1_bn_print(bp, "r: ", r, NULL, indent)) |
0f113f3e | 396 | goto err; |
706a13f1 | 397 | if (!ASN1_bn_print(bp, "s: ", s, NULL, indent)) |
0f113f3e MC |
398 | goto err; |
399 | rv = 1; | |
400 | err: | |
0f113f3e MC |
401 | DSA_SIG_free(dsa_sig); |
402 | return rv; | |
403 | } | |
a4c467c9 DO |
404 | if (BIO_puts(bp, "\n") <= 0) |
405 | return 0; | |
0f113f3e MC |
406 | return X509_signature_dump(bp, sig, indent); |
407 | } | |
fa1ba589 | 408 | |
492a9e24 | 409 | static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2) |
0f113f3e MC |
410 | { |
411 | switch (op) { | |
0f113f3e MC |
412 | case ASN1_PKEY_CTRL_DEFAULT_MD_NID: |
413 | *(int *)arg2 = NID_sha256; | |
cd4c83b5 | 414 | return 1; |
03919683 | 415 | |
0f113f3e MC |
416 | default: |
417 | return -2; | |
0f113f3e | 418 | } |
0f113f3e | 419 | } |
492a9e24 | 420 | |
4889dadc MC |
421 | static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey) |
422 | { | |
423 | return pkey->pkey.dsa->dirty_cnt; | |
424 | } | |
425 | ||
b305452f | 426 | static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata, |
b4250010 | 427 | EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx, |
76e23fc5 | 428 | const char *propq) |
4889dadc | 429 | { |
b305452f | 430 | DSA *dsa = from->pkey.dsa; |
6d4e6009 | 431 | OSSL_PARAM_BLD *tmpl; |
4889dadc MC |
432 | const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa); |
433 | const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa); | |
434 | const BIGNUM *priv_key = DSA_get0_priv_key(dsa); | |
435 | OSSL_PARAM *params; | |
0996cff9 | 436 | int selection = 0; |
6d4e6009 | 437 | int rv = 0; |
4889dadc | 438 | |
df13defd RL |
439 | /* |
440 | * If the DSA method is foreign, then we can't be sure of anything, and | |
441 | * can therefore not export or pretend to export. | |
442 | */ | |
443 | if (DSA_get_method(dsa) != DSA_OpenSSL()) | |
444 | return 0; | |
445 | ||
4889dadc | 446 | if (p == NULL || q == NULL || g == NULL) |
b305452f | 447 | return 0; |
4889dadc | 448 | |
6d4e6009 P |
449 | tmpl = OSSL_PARAM_BLD_new(); |
450 | if (tmpl == NULL) | |
b305452f | 451 | return 0; |
6d4e6009 P |
452 | |
453 | if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) | |
454 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q) | |
455 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g)) | |
456 | goto err; | |
0996cff9 RL |
457 | selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS; |
458 | if (pub_key != NULL) { | |
6d4e6009 | 459 | if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY, |
0996cff9 | 460 | pub_key)) |
6d4e6009 | 461 | goto err; |
0996cff9 RL |
462 | selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY; |
463 | } | |
b305452f | 464 | if (priv_key != NULL) { |
6d4e6009 | 465 | if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY, |
b305452f | 466 | priv_key)) |
6d4e6009 | 467 | goto err; |
0996cff9 | 468 | selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY; |
4889dadc MC |
469 | } |
470 | ||
6d4e6009 P |
471 | if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) |
472 | goto err; | |
4889dadc MC |
473 | |
474 | /* We export, the provider imports */ | |
0996cff9 | 475 | rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params); |
4889dadc | 476 | |
3f883c7c | 477 | OSSL_PARAM_free(params); |
2145ba5e | 478 | err: |
6d4e6009 | 479 | OSSL_PARAM_BLD_free(tmpl); |
b305452f | 480 | return rv; |
4889dadc MC |
481 | } |
482 | ||
629c72db | 483 | static int dsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx) |
0abae163 | 484 | { |
629c72db MC |
485 | EVP_PKEY_CTX *pctx = vpctx; |
486 | EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx); | |
5af02212 | 487 | DSA *dsa = ossl_dsa_new(pctx->libctx); |
0abae163 RL |
488 | |
489 | if (dsa == NULL) { | |
490 | ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE); | |
491 | return 0; | |
492 | } | |
493 | ||
5af02212 SL |
494 | if (!ossl_dsa_ffc_params_fromdata(dsa, params) |
495 | || !ossl_dsa_key_fromdata(dsa, params) | |
0abae163 RL |
496 | || !EVP_PKEY_assign_DSA(pkey, dsa)) { |
497 | DSA_free(dsa); | |
498 | return 0; | |
499 | } | |
500 | return 1; | |
501 | } | |
502 | ||
2145ba5e TM |
503 | static ossl_inline int dsa_bn_dup_check(BIGNUM **out, const BIGNUM *f) |
504 | { | |
505 | if (f != NULL && (*out = BN_dup(f)) == NULL) | |
506 | return 0; | |
507 | return 1; | |
508 | } | |
509 | ||
510 | static DSA *dsa_dup(const DSA *dsa) | |
511 | { | |
512 | DSA *dupkey = NULL; | |
513 | ||
514 | /* Do not try to duplicate foreign DSA keys */ | |
515 | if (DSA_get_method((DSA *)dsa) != DSA_OpenSSL()) | |
516 | return NULL; | |
517 | ||
518 | if ((dupkey = ossl_dsa_new(dsa->libctx)) == NULL) | |
519 | return NULL; | |
520 | ||
521 | if (!ossl_ffc_params_copy(&dupkey->params, &dsa->params)) | |
522 | goto err; | |
523 | ||
524 | dupkey->flags = dsa->flags; | |
525 | ||
526 | if (!dsa_bn_dup_check(&dupkey->pub_key, dsa->pub_key)) | |
527 | goto err; | |
528 | if (!dsa_bn_dup_check(&dupkey->priv_key, dsa->priv_key)) | |
529 | goto err; | |
530 | ||
531 | if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_DSA, | |
532 | &dupkey->ex_data, &dsa->ex_data)) | |
533 | goto err; | |
534 | ||
535 | return dupkey; | |
536 | ||
537 | err: | |
538 | DSA_free(dupkey); | |
539 | return NULL; | |
540 | } | |
541 | ||
542 | static int dsa_pkey_copy(EVP_PKEY *to, EVP_PKEY *from) | |
543 | { | |
544 | DSA *dsa = from->pkey.dsa; | |
545 | DSA *dupkey = NULL; | |
546 | int ret; | |
547 | ||
548 | if (dsa != NULL) { | |
549 | dupkey = dsa_dup(dsa); | |
550 | if (dupkey == NULL) | |
551 | return 0; | |
552 | } | |
553 | ||
554 | ret = EVP_PKEY_assign_DSA(to, dupkey); | |
555 | if (!ret) | |
556 | DSA_free(dupkey); | |
557 | return ret; | |
558 | } | |
559 | ||
448be743 DSH |
560 | /* NB these are sorted in pkey_id order, lowest first */ |
561 | ||
adf7e6d1 | 562 | const EVP_PKEY_ASN1_METHOD ossl_dsa_asn1_meths[5] = { |
0f113f3e MC |
563 | |
564 | { | |
565 | EVP_PKEY_DSA2, | |
566 | EVP_PKEY_DSA, | |
567 | ASN1_PKEY_ALIAS}, | |
568 | ||
569 | { | |
570 | EVP_PKEY_DSA1, | |
571 | EVP_PKEY_DSA, | |
572 | ASN1_PKEY_ALIAS}, | |
573 | ||
574 | { | |
575 | EVP_PKEY_DSA4, | |
576 | EVP_PKEY_DSA, | |
577 | ASN1_PKEY_ALIAS}, | |
578 | ||
579 | { | |
580 | EVP_PKEY_DSA3, | |
581 | EVP_PKEY_DSA, | |
582 | ASN1_PKEY_ALIAS}, | |
583 | ||
584 | { | |
585 | EVP_PKEY_DSA, | |
586 | EVP_PKEY_DSA, | |
587 | 0, | |
588 | ||
589 | "DSA", | |
590 | "OpenSSL DSA method", | |
591 | ||
592 | dsa_pub_decode, | |
593 | dsa_pub_encode, | |
594 | dsa_pub_cmp, | |
595 | dsa_pub_print, | |
596 | ||
597 | dsa_priv_decode, | |
598 | dsa_priv_encode, | |
599 | dsa_priv_print, | |
600 | ||
601 | int_dsa_size, | |
602 | dsa_bits, | |
603 | dsa_security_bits, | |
604 | ||
605 | dsa_param_decode, | |
606 | dsa_param_encode, | |
607 | dsa_missing_parameters, | |
608 | dsa_copy_parameters, | |
609 | dsa_cmp_parameters, | |
610 | dsa_param_print, | |
611 | dsa_sig_print, | |
612 | ||
613 | int_dsa_free, | |
614 | dsa_pkey_ctrl, | |
615 | old_dsa_priv_decode, | |
4889dadc MC |
616 | old_dsa_priv_encode, |
617 | ||
618 | NULL, NULL, NULL, | |
619 | NULL, NULL, NULL, | |
620 | NULL, NULL, NULL, NULL, | |
621 | ||
622 | dsa_pkey_dirty_cnt, | |
0abae163 | 623 | dsa_pkey_export_to, |
2145ba5e TM |
624 | dsa_pkey_import_from, |
625 | dsa_pkey_copy | |
4889dadc | 626 | } |
0f113f3e | 627 | }; |