2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include <openssl/x509.h>
18 #include <openssl/asn1.h>
19 #include <openssl/bn.h>
20 #include <openssl/core_names.h>
21 #include <openssl/param_build.h>
22 #include "internal/cryptlib.h"
23 #include "crypto/asn1.h"
24 #include "crypto/dsa.h"
25 #include "crypto/evp.h"
26 #include "internal/ffc.h"
27 #include "dsa_local.h"
29 static int dsa_pub_decode(EVP_PKEY *pkey, const X509_PUBKEY *pubkey)
31 const unsigned char *p, *pm;
35 const ASN1_STRING *pstr;
37 ASN1_INTEGER *public_key = NULL;
41 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
43 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
45 if (ptype == V_ASN1_SEQUENCE) {
50 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
51 ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR);
55 } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
56 if ((dsa = DSA_new()) == NULL) {
57 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
61 ERR_raise(ERR_LIB_DSA, DSA_R_PARAMETER_ENCODING_ERROR);
65 if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
66 ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR);
70 if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
71 ERR_raise(ERR_LIB_DSA, DSA_R_BN_DECODE_ERROR);
76 ASN1_INTEGER_free(public_key);
77 EVP_PKEY_assign_DSA(pkey, dsa);
81 ASN1_INTEGER_free(public_key);
87 static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
91 unsigned char *penc = NULL;
93 ASN1_STRING *str = NULL;
94 ASN1_INTEGER *pubint = NULL;
98 if (pkey->save_parameters
99 && dsa->params.p != NULL
100 && dsa->params.q != NULL
101 && dsa->params.g != NULL) {
102 str = ASN1_STRING_new();
104 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
107 str->length = i2d_DSAparams(dsa, &str->data);
108 if (str->length <= 0) {
109 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
112 ptype = V_ASN1_SEQUENCE;
114 ptype = V_ASN1_UNDEF;
116 pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
118 if (pubint == NULL) {
119 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
123 penclen = i2d_ASN1_INTEGER(pubint, &penc);
124 ASN1_INTEGER_free(pubint);
127 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
131 aobj = OBJ_nid2obj(EVP_PKEY_DSA);
135 if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
140 ASN1_STRING_free(str);
146 * In PKCS#8 DSA: you just get a private key integer and parameters in the
147 * AlgorithmIdentifier the pubkey must be recalculated.
150 static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
153 DSA *dsa = ossl_dsa_key_from_pkcs8(p8, NULL, NULL);
157 EVP_PKEY_assign_DSA(pkey, dsa);
163 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
165 ASN1_STRING *params = NULL;
166 ASN1_INTEGER *prkey = NULL;
167 unsigned char *dp = NULL;
170 if (pkey->pkey.dsa == NULL|| pkey->pkey.dsa->priv_key == NULL) {
171 ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS);
175 params = ASN1_STRING_new();
177 if (params == NULL) {
178 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
182 params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data);
183 if (params->length <= 0) {
184 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
187 params->type = V_ASN1_SEQUENCE;
189 /* Get private key into integer */
190 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
193 ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
197 dplen = i2d_ASN1_INTEGER(prkey, &dp);
199 ASN1_STRING_clear_free(prkey);
202 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
203 V_ASN1_SEQUENCE, params, dp, dplen))
210 ASN1_STRING_free(params);
211 ASN1_STRING_clear_free(prkey);
215 static int int_dsa_size(const EVP_PKEY *pkey)
217 return DSA_size(pkey->pkey.dsa);
220 static int dsa_bits(const EVP_PKEY *pkey)
222 return DSA_bits(pkey->pkey.dsa);
225 static int dsa_security_bits(const EVP_PKEY *pkey)
227 return DSA_security_bits(pkey->pkey.dsa);
230 static int dsa_missing_parameters(const EVP_PKEY *pkey)
233 dsa = pkey->pkey.dsa;
235 || dsa->params.p == NULL
236 || dsa->params.q == NULL
237 || dsa->params.g == NULL;
240 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
242 if (to->pkey.dsa == NULL) {
243 to->pkey.dsa = DSA_new();
244 if (to->pkey.dsa == NULL)
247 if (!ossl_ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params))
250 to->pkey.dsa->dirty_cnt++;
254 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
256 return ossl_ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1);
259 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
261 return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
264 static void int_dsa_free(EVP_PKEY *pkey)
266 DSA_free(pkey->pkey.dsa);
269 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
272 const char *ktype = NULL;
273 const BIGNUM *priv_key, *pub_key;
276 if (x->params.p != NULL)
277 mod_len = DSA_bits(x);
280 priv_key = x->priv_key;
285 pub_key = x->pub_key;
290 ktype = "Private-Key";
292 ktype = "Public-Key";
294 ktype = "DSA-Parameters";
296 if (priv_key != NULL) {
297 if (!BIO_indent(bp, off, 128))
299 if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0)
302 if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
306 if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
308 if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
310 if (!ossl_ffc_params_print(bp, &x->params, off))
317 static int dsa_param_decode(EVP_PKEY *pkey,
318 const unsigned char **pder, int derlen)
322 if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL)
326 EVP_PKEY_assign_DSA(pkey, dsa);
330 static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
332 return i2d_DSAparams(pkey->pkey.dsa, pder);
335 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
338 return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
341 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
344 return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
347 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
350 return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
353 static int old_dsa_priv_decode(EVP_PKEY *pkey,
354 const unsigned char **pder, int derlen)
358 if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
359 ERR_raise(ERR_LIB_DSA, ERR_R_DSA_LIB);
363 EVP_PKEY_assign_DSA(pkey, dsa);
367 static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
369 return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
372 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
373 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
376 const unsigned char *p;
379 if (BIO_puts(bp, "\n") <= 0)
385 dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
386 if (dsa_sig != NULL) {
390 DSA_SIG_get0(dsa_sig, &r, &s);
392 if (BIO_write(bp, "\n", 1) != 1)
395 if (!ASN1_bn_print(bp, "r: ", r, NULL, indent))
397 if (!ASN1_bn_print(bp, "s: ", s, NULL, indent))
401 DSA_SIG_free(dsa_sig);
404 if (BIO_puts(bp, "\n") <= 0)
406 return X509_signature_dump(bp, sig, indent);
409 static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
412 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
413 *(int *)arg2 = NID_sha256;
421 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
423 return pkey->pkey.dsa->dirty_cnt;
426 static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
427 OSSL_FUNC_keymgmt_import_fn *importer,
428 OSSL_LIB_CTX *libctx, const char *propq)
430 DSA *dsa = from->pkey.dsa;
431 OSSL_PARAM_BLD *tmpl;
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);
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.
443 if (DSA_get_method(dsa) != DSA_OpenSSL())
446 if (p == NULL || q == NULL || g == NULL)
449 tmpl = OSSL_PARAM_BLD_new();
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))
457 selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
458 if (pub_key != NULL) {
459 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
462 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
464 if (priv_key != NULL) {
465 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
468 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
471 if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
474 /* We export, the provider imports */
475 rv = importer(to_keydata, selection, params);
477 OSSL_PARAM_free(params);
479 OSSL_PARAM_BLD_free(tmpl);
483 static int dsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
485 EVP_PKEY_CTX *pctx = vpctx;
486 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
487 DSA *dsa = ossl_dsa_new(pctx->libctx);
490 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
494 if (!ossl_dsa_ffc_params_fromdata(dsa, params)
495 || !ossl_dsa_key_fromdata(dsa, params)
496 || !EVP_PKEY_assign_DSA(pkey, dsa)) {
503 static int dsa_pkey_copy(EVP_PKEY *to, EVP_PKEY *from)
505 DSA *dsa = from->pkey.dsa;
510 dupkey = ossl_dsa_dup(dsa, OSSL_KEYMGMT_SELECT_ALL);
515 ret = EVP_PKEY_assign_DSA(to, dupkey);
521 /* NB these are sorted in pkey_id order, lowest first */
523 const EVP_PKEY_ASN1_METHOD ossl_dsa_asn1_meths[5] = {
551 "OpenSSL DSA method",
568 dsa_missing_parameters,
581 NULL, NULL, NULL, NULL,
585 dsa_pkey_import_from,