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 * 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/cms.h>
21 #include <openssl/core_names.h>
22 #include "internal/cryptlib.h"
23 #include "crypto/asn1.h"
24 #include "crypto/evp.h"
25 #include "internal/param_build.h"
26 #include "dsa_local.h"
28 static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
30 const unsigned char *p, *pm;
34 const ASN1_STRING *pstr;
36 ASN1_INTEGER *public_key = NULL;
40 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
42 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
44 if (ptype == V_ASN1_SEQUENCE) {
49 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
50 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
54 } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
55 if ((dsa = DSA_new()) == NULL) {
56 DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
60 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
64 if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
65 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
69 if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
70 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
75 ASN1_INTEGER_free(public_key);
76 EVP_PKEY_assign_DSA(pkey, dsa);
80 ASN1_INTEGER_free(public_key);
86 static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
90 unsigned char *penc = NULL;
92 ASN1_STRING *str = NULL;
93 ASN1_INTEGER *pubint = NULL;
97 if (pkey->save_parameters
98 && dsa->params.p != NULL
99 && dsa->params.q != NULL
100 && dsa->params.g != NULL) {
101 str = ASN1_STRING_new();
103 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
106 str->length = i2d_DSAparams(dsa, &str->data);
107 if (str->length <= 0) {
108 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
111 ptype = V_ASN1_SEQUENCE;
113 ptype = V_ASN1_UNDEF;
115 pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
117 if (pubint == NULL) {
118 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
122 penclen = i2d_ASN1_INTEGER(pubint, &penc);
123 ASN1_INTEGER_free(pubint);
126 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
130 aobj = OBJ_nid2obj(EVP_PKEY_DSA);
134 if (X509_PUBKEY_set0_param(pk, aobj, ptype, str, penc, penclen))
139 ASN1_STRING_free(str);
145 * In PKCS#8 DSA: you just get a private key integer and parameters in the
146 * AlgorithmIdentifier the pubkey must be recalculated.
149 static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
151 const unsigned char *p, *pm;
155 const ASN1_STRING *pstr;
156 const X509_ALGOR *palg;
157 ASN1_INTEGER *privkey = NULL;
164 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
166 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
168 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
170 if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
175 pmlen = pstr->length;
176 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
178 /* We have parameters now set private key */
179 if ((dsa->priv_key = BN_secure_new()) == NULL
180 || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
181 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
184 /* Calculate public key */
185 if ((dsa->pub_key = BN_new()) == NULL) {
186 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
189 if ((ctx = BN_CTX_new()) == NULL) {
190 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
194 BN_set_flags(dsa->priv_key, BN_FLG_CONSTTIME);
195 if (!BN_mod_exp(dsa->pub_key, dsa->params.g, dsa->priv_key, dsa->params.p,
197 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
202 EVP_PKEY_assign_DSA(pkey, dsa);
208 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
213 ASN1_STRING_clear_free(privkey);
217 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
219 ASN1_STRING *params = NULL;
220 ASN1_INTEGER *prkey = NULL;
221 unsigned char *dp = NULL;
224 if (pkey->pkey.dsa == NULL|| pkey->pkey.dsa->priv_key == NULL) {
225 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
229 params = ASN1_STRING_new();
231 if (params == NULL) {
232 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
236 params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data);
237 if (params->length <= 0) {
238 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
241 params->type = V_ASN1_SEQUENCE;
243 /* Get private key into integer */
244 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
247 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
251 dplen = i2d_ASN1_INTEGER(prkey, &dp);
253 ASN1_STRING_clear_free(prkey);
256 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
257 V_ASN1_SEQUENCE, params, dp, dplen))
264 ASN1_STRING_free(params);
265 ASN1_STRING_clear_free(prkey);
269 static int int_dsa_size(const EVP_PKEY *pkey)
271 return DSA_size(pkey->pkey.dsa);
274 static int dsa_bits(const EVP_PKEY *pkey)
276 return DSA_bits(pkey->pkey.dsa);
279 static int dsa_security_bits(const EVP_PKEY *pkey)
281 return DSA_security_bits(pkey->pkey.dsa);
284 static int dsa_missing_parameters(const EVP_PKEY *pkey)
287 dsa = pkey->pkey.dsa;
289 || dsa->params.p == NULL
290 || dsa->params.q == NULL
291 || dsa->params.g == NULL;
294 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
296 if (to->pkey.dsa == NULL) {
297 to->pkey.dsa = DSA_new();
298 if (to->pkey.dsa == NULL)
301 if (!ffc_params_copy(&to->pkey.dsa->params, &from->pkey.dsa->params))
304 to->pkey.dsa->dirty_cnt++;
308 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
310 return ffc_params_cmp(&a->pkey.dsa->params, &b->pkey.dsa->params, 1);
313 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
315 return BN_cmp(b->pkey.dsa->pub_key, a->pkey.dsa->pub_key) == 0;
318 static void int_dsa_free(EVP_PKEY *pkey)
320 DSA_free(pkey->pkey.dsa);
323 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype)
326 const char *ktype = NULL;
327 const BIGNUM *priv_key, *pub_key;
330 if (x->params.p != NULL)
331 mod_len = DSA_bits(x);
334 priv_key = x->priv_key;
339 pub_key = x->pub_key;
344 ktype = "Private-Key";
346 ktype = "Public-Key";
348 ktype = "DSA-Parameters";
350 if (priv_key != NULL) {
351 if (!BIO_indent(bp, off, 128))
353 if (BIO_printf(bp, "%s: (%d bit)\n", ktype, mod_len) <= 0)
356 if (BIO_printf(bp, "Public-Key: (%d bit)\n", mod_len) <= 0)
360 if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
362 if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
364 if (!ffc_params_print(bp, &x->params, off))
371 static int dsa_param_decode(EVP_PKEY *pkey,
372 const unsigned char **pder, int derlen)
376 if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
377 DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
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 DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, 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_PKCS7_SIGN:
470 X509_ALGOR *alg1, *alg2;
471 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
472 if (alg1 == NULL || alg1->algorithm == NULL)
474 hnid = OBJ_obj2nid(alg1->algorithm);
475 if (hnid == NID_undef)
477 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
479 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
482 #ifndef OPENSSL_NO_CMS
483 case ASN1_PKEY_CTRL_CMS_SIGN:
486 X509_ALGOR *alg1, *alg2;
487 CMS_SignerInfo_get0_algs(arg2, NULL, 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);
499 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
500 *(int *)arg2 = CMS_RECIPINFO_NONE;
504 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
505 *(int *)arg2 = NID_sha256;
515 static size_t dsa_pkey_dirty_cnt(const EVP_PKEY *pkey)
517 return pkey->pkey.dsa->dirty_cnt;
520 static int dsa_pkey_export_to(const EVP_PKEY *from, void *to_keydata,
521 EVP_KEYMGMT *to_keymgmt)
523 DSA *dsa = from->pkey.dsa;
525 const BIGNUM *p = DSA_get0_p(dsa), *g = DSA_get0_g(dsa);
526 const BIGNUM *q = DSA_get0_q(dsa), *pub_key = DSA_get0_pub_key(dsa);
527 const BIGNUM *priv_key = DSA_get0_priv_key(dsa);
531 if (p == NULL || q == NULL || g == NULL)
534 ossl_param_bld_init(&tmpl);
535 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_P, p)
536 || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_Q, q)
537 || !ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_FFC_G, g))
539 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_PUB_KEY,
542 if (priv_key != NULL) {
543 if (!ossl_param_bld_push_BN(&tmpl, OSSL_PKEY_PARAM_PRIV_KEY,
548 if ((params = ossl_param_bld_to_param(&tmpl)) == NULL)
551 /* We export, the provider imports */
552 rv = evp_keymgmt_import(to_keymgmt, to_keydata, OSSL_KEYMGMT_SELECT_ALL,
555 ossl_param_bld_free(params);
560 /* NB these are sorted in pkey_id order, lowest first */
562 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
590 "OpenSSL DSA method",
607 dsa_missing_parameters,
620 NULL, NULL, NULL, NULL,