2 * Copyright 2006-2016 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 #include <openssl/x509.h>
12 #include <openssl/asn1.h>
13 #include <openssl/bn.h>
14 #include <openssl/cms.h>
15 #include <openssl/core_names.h>
16 #include "internal/cryptlib.h"
17 #include "crypto/asn1.h"
18 #include "crypto/evp.h"
19 #include "internal/param_build.h"
20 #include "dsa_local.h"
22 static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
24 const unsigned char *p, *pm;
28 const ASN1_STRING *pstr;
30 ASN1_INTEGER *public_key = NULL;
34 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
36 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
38 if (ptype == V_ASN1_SEQUENCE) {
43 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
44 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
48 } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
49 if ((dsa = DSA_new()) == NULL) {
50 DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
54 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
58 if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
59 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
63 if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
64 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
69 ASN1_INTEGER_free(public_key);
70 EVP_PKEY_assign_DSA(pkey, dsa);
74 ASN1_INTEGER_free(public_key);
80 static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
84 unsigned char *penc = NULL;
86 ASN1_STRING *str = NULL;
87 ASN1_INTEGER *pubint = NULL;
91 if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) {
92 str = ASN1_STRING_new();
94 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
97 str->length = i2d_DSAparams(dsa, &str->data);
98 if (str->length <= 0) {
99 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
102 ptype = V_ASN1_SEQUENCE;
104 ptype = V_ASN1_UNDEF;
106 pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
108 if (pubint == NULL) {
109 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
113 penclen = i2d_ASN1_INTEGER(pubint, &penc);
114 ASN1_INTEGER_free(pubint);
117 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
121 aobj = OBJ_nid2obj(EVP_PKEY_DSA);
125 if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
130 ASN1_STRING_free(str);
136 * In PKCS#8 DSA: you just get a private key integer and parameters in the
137 * AlgorithmIdentifier the pubkey must be recalculated.
140 static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
142 const unsigned char *p, *pm;
146 const ASN1_STRING *pstr;
147 const X509_ALGOR *palg;
148 ASN1_INTEGER *privkey = NULL;
155 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
157 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
159 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
161 if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
166 pmlen = pstr->length;
167 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
169 /* We have parameters now set private key */
170 if ((dsa->priv_key = BN_secure_new()) == NULL
171 || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
172 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
175 /* Calculate public key */
176 if ((dsa->pub_key = BN_new()) == NULL) {
177 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
180 if ((ctx = BN_CTX_new()) == NULL) {
181 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
185 BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
186 if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
187 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
192 EVP_PKEY_assign_DSA(pkey, dsa);
198 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
203 ASN1_STRING_clear_free(privkey);
207 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
209 ASN1_STRING *params = NULL;
210 ASN1_INTEGER *prkey = NULL;
211 unsigned char *dp = NULL;
214 if (pkey->pkey.dsa == NULL|| pkey->pkey.dsa->priv_key == NULL) {
215 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
219 params = ASN1_STRING_new();
221 if (params == NULL) {
222 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
226 params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data);
227 if (params->length <= 0) {
228 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
231 params->type = V_ASN1_SEQUENCE;
233 /* Get private key into integer */
234 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
237 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
241 dplen = i2d_ASN1_INTEGER(prkey, &dp);
243 ASN1_STRING_clear_free(prkey);
246 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
247 V_ASN1_SEQUENCE, params, dp, dplen))
254 ASN1_STRING_free(params);
255 ASN1_STRING_clear_free(prkey);
259 static int int_dsa_size(const EVP_PKEY *pkey)
261 return DSA_size(pkey->pkey.dsa);
264 static int dsa_bits(const EVP_PKEY *pkey)
266 return DSA_bits(pkey->pkey.dsa);
269 static int dsa_security_bits(const EVP_PKEY *pkey)
271 return DSA_security_bits(pkey->pkey.dsa);
274 static int dsa_missing_parameters(const EVP_PKEY *pkey)
277 dsa = pkey->pkey.dsa;
278 if (dsa == NULL || dsa->p == NULL || dsa->q == NULL || dsa->g == NULL)
283 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
287 if (to->pkey.dsa == NULL) {
288 to->pkey.dsa = DSA_new();
289 if (to->pkey.dsa == NULL)
293 if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
295 BN_free(to->pkey.dsa->p);
298 if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
300 BN_free(to->pkey.dsa->q);
303 if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
305 BN_free(to->pkey.dsa->g);
307 to->pkey.dsa->dirty_cnt++;
311 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
313 if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) ||
314 BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) ||
315 BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g))
321 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
323 if (BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) != 0)
329 static void int_dsa_free(EVP_PKEY *pkey)
331 DSA_free(pkey->pkey.dsa);
334 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
337 const char *ktype = NULL;
338 const BIGNUM *priv_key, *pub_key;
342 mod_len = BN_num_bits(x->p);
345 priv_key = x->priv_key;
350 pub_key = x->pub_key;
355 ktype = "Private-Key";
357 ktype = "Public-Key";
359 ktype = "DSA-Parameters";
362 if (!BIO_indent(bp, off, 128))
364 if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p))
368 if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
372 if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
374 if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
376 if (!ASN1_bn_print(bp, "P: ", x->p, NULL, off))
378 if (!ASN1_bn_print(bp, "Q: ", x->q, NULL, off))
380 if (!ASN1_bn_print(bp, "G: ", x->g, NULL, off))
387 static int dsa_param_decode(EVP_PKEY *pkey,
388 const unsigned char **pder, int derlen)
392 if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
393 DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
397 EVP_PKEY_assign_DSA(pkey, dsa);
401 static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
403 return i2d_DSAparams(pkey->pkey.dsa, pder);
406 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
409 return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
412 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
415 return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
418 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
421 return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
424 static int old_dsa_priv_decode(EVP_PKEY *pkey,
425 const unsigned char **pder, int derlen)
429 if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
430 DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
434 EVP_PKEY_assign_DSA(pkey, dsa);
438 static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
440 return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
443 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
444 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
447 const unsigned char *p;
450 if (BIO_puts(bp, "\n") <= 0)
456 dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
461 DSA_SIG_get0(dsa_sig, &r, &s);
463 if (BIO_write(bp, "\n", 1) != 1)
466 if (!ASN1_bn_print(bp, "r: ", r, NULL, indent))
468 if (!ASN1_bn_print(bp, "s: ", s, NULL, indent))
472 DSA_SIG_free(dsa_sig);
475 if (BIO_puts(bp, "\n") <= 0)
477 return X509_signature_dump(bp, sig, indent);
480 static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
483 case ASN1_PKEY_CTRL_PKCS7_SIGN:
486 X509_ALGOR *alg1, *alg2;
487 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
488 if (alg1 == NULL || alg1->algorithm == NULL)
490 hnid = OBJ_obj2nid(alg1->algorithm);
491 if (hnid == NID_undef)
493 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
495 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
498 #ifndef OPENSSL_NO_CMS
499 case ASN1_PKEY_CTRL_CMS_SIGN:
502 X509_ALGOR *alg1, *alg2;
503 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
504 if (alg1 == NULL || alg1->algorithm == NULL)
506 hnid = OBJ_obj2nid(alg1->algorithm);
507 if (hnid == NID_undef)
509 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
511 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
515 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
516 *(int *)arg2 = CMS_RECIPINFO_NONE;
520 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
521 *(int *)arg2 = NID_sha256;
531 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
533 return pkey->pkey.dsa->dirty_cnt;
536 static void *dsa_pkey_export_to(const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
537 int want_domainparams)
539 DSA *dsa = pk->pkey.dsa;
541 const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
542 const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
543 const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
545 void *provdata = NULL;
547 if (p == NULL || q == NULL || g == NULL)
550 ossl_param_bld_init(&tmpl);
551 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_P, p)
552 || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
553 || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_G, g))
556 if (!want_domainparams) {
557 /* A key must at least have a public part. */
558 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_DSA_PUB_KEY,
562 if (priv_key != NULL) {
563 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_DSA_PRIV_KEY,
569 params = ossl_param_bld_to_param(&tmpl);
571 /* We export, the provider imports */
572 provdata = want_domainparams
573 ? evp_keymgmt_importdomparams(keymgmt, params)
574 : evp_keymgmt_importkey(keymgmt, params);
576 ossl_param_bld_free(params);
580 /* NB these are sorted in pkey_id order, lowest first */
582 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
610 "OpenSSL DSA method",
627 dsa_missing_parameters,
640 NULL, NULL, NULL, NULL,