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
92 && dsa->params.p != NULL
93 && dsa->params.q != NULL
94 && dsa->params.g != NULL) {
95 str = ASN1_STRING_new();
97 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
100 str->length = i2d_DSAparams(dsa, &str->data);
101 if (str->length <= 0) {
102 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
105 ptype = V_ASN1_SEQUENCE;
107 ptype = V_ASN1_UNDEF;
109 pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
111 if (pubint == NULL) {
112 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
116 penclen = i2d_ASN1_INTEGER(pubint, &penc);
117 ASN1_INTEGER_free(pubint);
120 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
124 aobj = OBJ_nid2obj(EVP_PKEY_DSA);
128 if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
133 ASN1_STRING_free(str);
139 * In PKCS#8 DSA: you just get a private key integer and parameters in the
140 * AlgorithmIdentifier the pubkey must be recalculated.
143 static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
145 const unsigned char *p, *pm;
149 const ASN1_STRING *pstr;
150 const X509_ALGOR *palg;
151 ASN1_INTEGER *privkey = NULL;
158 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
160 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
162 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
164 if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
169 pmlen = pstr->length;
170 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
172 /* We have parameters now set private key */
173 if ((dsa->priv_key = BN_secure_new()) == NULL
174 || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
175 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
178 /* Calculate public key */
179 if ((dsa->pub_key = BN_new()) == NULL) {
180 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
183 if ((ctx = BN_CTX_new()) == NULL) {
184 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
188 BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
189 if (!BN_mod_exp(dsa->pub_key, dsa->params.g, dsa->priv_key, dsa->params.p,
191 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
196 EVP_PKEY_assign_DSA(pkey, dsa);
202 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
207 ASN1_STRING_clear_free(privkey);
211 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
213 ASN1_STRING *params = NULL;
214 ASN1_INTEGER *prkey = NULL;
215 unsigned char *dp = NULL;
218 if (pkey->pkey.dsa == NULL|| pkey->pkey.dsa->priv_key == NULL) {
219 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
223 params = ASN1_STRING_new();
225 if (params == NULL) {
226 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
230 params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data);
231 if (params->length <= 0) {
232 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
235 params->type = V_ASN1_SEQUENCE;
237 /* Get private key into integer */
238 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
241 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
245 dplen = i2d_ASN1_INTEGER(prkey, &dp);
247 ASN1_STRING_clear_free(prkey);
250 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
251 V_ASN1_SEQUENCE, params, dp, dplen))
258 ASN1_STRING_free(params);
259 ASN1_STRING_clear_free(prkey);
263 static int int_dsa_size(const EVP_PKEY *pkey)
265 return DSA_size(pkey->pkey.dsa);
268 static int dsa_bits(const EVP_PKEY *pkey)
270 return DSA_bits(pkey->pkey.dsa);
273 static int dsa_security_bits(const EVP_PKEY *pkey)
275 return DSA_security_bits(pkey->pkey.dsa);
278 static int dsa_missing_parameters(const EVP_PKEY *pkey)
281 dsa = pkey->pkey.dsa;
283 || dsa->params.p == NULL
284 || dsa->params.q == NULL
285 || dsa->params.g == NULL;
288 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
290 if (to->pkey.dsa == NULL) {
291 to->pkey.dsa = DSA_new();
292 if (to->pkey.dsa == NULL)
295 if (!ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params))
298 to->pkey.dsa->dirty_cnt++;
302 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
304 return ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1);
307 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
309 return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
312 static void int_dsa_free(EVP_PKEY *pkey)
314 DSA_free(pkey->pkey.dsa);
317 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
320 const char *ktype = NULL;
321 const BIGNUM *priv_key, *pub_key;
324 if (x->params.p != NULL)
325 mod_len = DSA_bits(x);
328 priv_key = x->priv_key;
333 pub_key = x->pub_key;
338 ktype = "Private-Key";
340 ktype = "Public-Key";
342 ktype = "DSA-Parameters";
344 if (priv_key != NULL) {
345 if (!BIO_indent(bp, off, 128))
347 if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0)
350 if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
354 if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
356 if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
358 if (!ffc_params_print(bp, &x->params, off))
365 static int dsa_param_decode(EVP_PKEY *pkey,
366 const unsigned char **pder, int derlen)
370 if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
371 DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
375 EVP_PKEY_assign_DSA(pkey, dsa);
379 static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
381 return i2d_DSAparams(pkey->pkey.dsa, pder);
384 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
387 return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
390 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
393 return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
396 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
399 return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
402 static int old_dsa_priv_decode(EVP_PKEY *pkey,
403 const unsigned char **pder, int derlen)
407 if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
408 DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
412 EVP_PKEY_assign_DSA(pkey, dsa);
416 static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
418 return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
421 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
422 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
425 const unsigned char *p;
428 if (BIO_puts(bp, "\n") <= 0)
434 dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
435 if (dsa_sig != NULL) {
439 DSA_SIG_get0(dsa_sig, &r, &s);
441 if (BIO_write(bp, "\n", 1) != 1)
444 if (!ASN1_bn_print(bp, "r: ", r, NULL, indent))
446 if (!ASN1_bn_print(bp, "s: ", s, NULL, indent))
450 DSA_SIG_free(dsa_sig);
453 if (BIO_puts(bp, "\n") <= 0)
455 return X509_signature_dump(bp, sig, indent);
458 static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
461 case ASN1_PKEY_CTRL_PKCS7_SIGN:
464 X509_ALGOR *alg1, *alg2;
465 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
466 if (alg1 == NULL || alg1->algorithm == NULL)
468 hnid = OBJ_obj2nid(alg1->algorithm);
469 if (hnid == NID_undef)
471 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
473 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
476 #ifndef OPENSSL_NO_CMS
477 case ASN1_PKEY_CTRL_CMS_SIGN:
480 X509_ALGOR *alg1, *alg2;
481 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
482 if (alg1 == NULL || alg1->algorithm == NULL)
484 hnid = OBJ_obj2nid(alg1->algorithm);
485 if (hnid == NID_undef)
487 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
489 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
493 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
494 *(int *)arg2 = CMS_RECIPINFO_NONE;
498 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
499 *(int *)arg2 = NID_sha256;
509 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
511 return pkey->pkey.dsa->dirty_cnt;
514 static void *dsa_pkey_export_to(const EVP_PKEY *pk, EVP_KEYMGMT *keymgmt,
515 int want_domainparams)
517 DSA *dsa = pk->pkey.dsa;
519 const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
520 const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
521 const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
523 void *provdata = NULL;
525 if (p == NULL || q == NULL || g == NULL)
528 ossl_param_bld_init(&tmpl);
529 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_P, p)
530 || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
531 || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_G, g))
534 if (!want_domainparams) {
535 /* A key must at least have a public part. */
536 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_DSA_PUB_KEY,
540 if (priv_key != NULL) {
541 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_DSA_PRIV_KEY,
547 params = ossl_param_bld_to_param(&tmpl);
549 /* We export, the provider imports */
550 provdata = want_domainparams
551 ? evp_keymgmt_importdomparams(keymgmt, params)
552 : evp_keymgmt_importkey(keymgmt, params);
554 ossl_param_bld_free(params);
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,