2 * Copyright 2006-2020 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)
152 const unsigned char *p, *pm;
156 const ASN1_STRING *pstr;
157 const X509_ALGOR *palg;
158 ASN1_INTEGER *privkey = NULL;
165 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
167 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
169 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
171 if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
176 pmlen = pstr->length;
177 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
179 /* We have parameters now set private key */
180 if ((dsa->priv_key = BN_secure_new()) == NULL
181 || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
182 ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
185 /* Calculate public key */
186 if ((dsa->pub_key = BN_new()) == NULL) {
187 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
190 if ((ctx = BN_CTX_new()) == NULL) {
191 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
195 BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
196 if (!BN_mod_exp(dsa->pub_key, dsa->params.g, dsa->priv_key, dsa->params.p,
198 ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
203 EVP_PKEY_assign_DSA(pkey, dsa);
209 ERR_raise(ERR_LIB_DSA, DSA_R_DECODE_ERROR);
214 ASN1_STRING_clear_free(privkey);
218 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
220 ASN1_STRING *params = NULL;
221 ASN1_INTEGER *prkey = NULL;
222 unsigned char *dp = NULL;
225 if (pkey->pkey.dsa == NULL|| pkey->pkey.dsa->priv_key == NULL) {
226 ERR_raise(ERR_LIB_DSA, DSA_R_MISSING_PARAMETERS);
230 params = ASN1_STRING_new();
232 if (params == NULL) {
233 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
237 params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data);
238 if (params->length <= 0) {
239 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
242 params->type = V_ASN1_SEQUENCE;
244 /* Get private key into integer */
245 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
248 ERR_raise(ERR_LIB_DSA, DSA_R_BN_ERROR);
252 dplen = i2d_ASN1_INTEGER(prkey, &dp);
254 ASN1_STRING_clear_free(prkey);
257 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
258 V_ASN1_SEQUENCE, params, dp, dplen))
265 ASN1_STRING_free(params);
266 ASN1_STRING_clear_free(prkey);
270 static int int_dsa_size(const EVP_PKEY *pkey)
272 return DSA_size(pkey->pkey.dsa);
275 static int dsa_bits(const EVP_PKEY *pkey)
277 return DSA_bits(pkey->pkey.dsa);
280 static int dsa_security_bits(const EVP_PKEY *pkey)
282 return DSA_security_bits(pkey->pkey.dsa);
285 static int dsa_missing_parameters(const EVP_PKEY *pkey)
288 dsa = pkey->pkey.dsa;
290 || dsa->params.p == NULL
291 || dsa->params.q == NULL
292 || dsa->params.g == NULL;
295 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
297 if (to->pkey.dsa == NULL) {
298 to->pkey.dsa = DSA_new();
299 if (to->pkey.dsa == NULL)
302 if (!ossl_ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params))
305 to->pkey.dsa->dirty_cnt++;
309 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
311 return ossl_ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1);
314 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
316 return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
319 static void int_dsa_free(EVP_PKEY *pkey)
321 DSA_free(pkey->pkey.dsa);
324 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
327 const char *ktype = NULL;
328 const BIGNUM *priv_key, *pub_key;
331 if (x->params.p != NULL)
332 mod_len = DSA_bits(x);
335 priv_key = x->priv_key;
340 pub_key = x->pub_key;
345 ktype = "Private-Key";
347 ktype = "Public-Key";
349 ktype = "DSA-Parameters";
351 if (priv_key != NULL) {
352 if (!BIO_indent(bp, off, 128))
354 if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0)
357 if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
361 if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
363 if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
365 if (!ossl_ffc_params_print(bp, &x->params, off))
372 static int dsa_param_decode(EVP_PKEY *pkey,
373 const unsigned char **pder, int derlen)
377 if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL)
381 EVP_PKEY_assign_DSA(pkey, dsa);
385 static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
387 return i2d_DSAparams(pkey->pkey.dsa, pder);
390 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
393 return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
396 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
399 return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
402 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
405 return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
408 static int old_dsa_priv_decode(EVP_PKEY *pkey,
409 const unsigned char **pder, int derlen)
413 if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
414 ERR_raise(ERR_LIB_DSA, ERR_R_DSA_LIB);
418 EVP_PKEY_assign_DSA(pkey, dsa);
422 static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
424 return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
427 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
428 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
431 const unsigned char *p;
434 if (BIO_puts(bp, "\n") <= 0)
440 dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
441 if (dsa_sig != NULL) {
445 DSA_SIG_get0(dsa_sig, &r, &s);
447 if (BIO_write(bp, "\n", 1) != 1)
450 if (!ASN1_bn_print(bp, "r: ", r, NULL, indent))
452 if (!ASN1_bn_print(bp, "s: ", s, NULL, indent))
456 DSA_SIG_free(dsa_sig);
459 if (BIO_puts(bp, "\n") <= 0)
461 return X509_signature_dump(bp, sig, indent);
464 static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
467 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
468 *(int *)arg2 = NID_sha256;
476 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
478 return pkey->pkey.dsa->dirty_cnt;
481 static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
482 EVP_KEYMGMT *to_keymgmt, OSSL_LIB_CTX *libctx,
485 DSA *dsa = from->pkey.dsa;
486 OSSL_PARAM_BLD *tmpl;
487 const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
488 const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
489 const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
495 * If the DSA method is foreign, then we can't be sure of anything, and
496 * can therefore not export or pretend to export.
498 if (DSA_get_method(dsa) != DSA_OpenSSL())
501 if (p == NULL || q == NULL || g == NULL)
504 tmpl = OSSL_PARAM_BLD_new();
508 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p)
509 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
510 || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g))
512 selection |= OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS;
513 if (pub_key != NULL) {
514 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PUB_KEY,
517 selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
519 if (priv_key != NULL) {
520 if (!OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
523 selection |= OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
526 if ((params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL)
529 /* We export, the provider imports */
530 rv = evp_keymgmt_import(to_keymgmt, to_keydata, selection, params);
532 OSSL_PARAM_BLD_free_params(params);
534 OSSL_PARAM_BLD_free(tmpl);
538 static int dsa_pkey_import_from(const OSSL_PARAM params[], void *vpctx)
540 EVP_PKEY_CTX *pctx = vpctx;
541 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(pctx);
542 DSA *dsa = dsa_new_with_ctx(pctx->libctx);
545 ERR_raise(ERR_LIB_DSA, ERR_R_MALLOC_FAILURE);
549 if (!dsa_ffc_params_fromdata(dsa, params)
550 || !dsa_key_fromdata(dsa, params)
551 || !EVP_PKEY_assign_DSA(pkey, dsa)) {
558 /* NB these are sorted in pkey_id order, lowest first */
560 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
588 "OpenSSL DSA method",
605 dsa_missing_parameters,
618 NULL, NULL, NULL, NULL,