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