2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 "internal/cryptlib.h"
12 #include <openssl/x509.h>
13 #include <openssl/asn1.h>
15 #include <openssl/bn.h>
16 #include <openssl/cms.h>
17 #include "internal/asn1_int.h"
18 #include "internal/evp_int.h"
20 static int dsa_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
22 const unsigned char *p, *pm;
26 const ASN1_STRING *pstr;
28 ASN1_INTEGER *public_key = NULL;
32 if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
34 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
36 if (ptype == V_ASN1_SEQUENCE) {
41 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL) {
42 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
46 } else if ((ptype == V_ASN1_NULL) || (ptype == V_ASN1_UNDEF)) {
47 if ((dsa = DSA_new()) == NULL) {
48 DSAerr(DSA_F_DSA_PUB_DECODE, ERR_R_MALLOC_FAILURE);
52 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_PARAMETER_ENCODING_ERROR);
56 if ((public_key = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL) {
57 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_DECODE_ERROR);
61 if ((dsa->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)) == NULL) {
62 DSAerr(DSA_F_DSA_PUB_DECODE, DSA_R_BN_DECODE_ERROR);
66 ASN1_INTEGER_free(public_key);
67 EVP_PKEY_assign_DSA(pkey, dsa);
71 ASN1_INTEGER_free(public_key);
77 static int dsa_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
81 unsigned char *penc = NULL;
83 ASN1_STRING *str = NULL;
84 ASN1_INTEGER *pubint = NULL;
87 if (pkey->save_parameters && dsa->p && dsa->q && dsa->g) {
88 str = ASN1_STRING_new();
90 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
93 str->length = i2d_DSAparams(dsa, &str->data);
94 if (str->length <= 0) {
95 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
98 ptype = V_ASN1_SEQUENCE;
100 ptype = V_ASN1_UNDEF;
102 pubint = BN_to_ASN1_INTEGER(dsa->pub_key, NULL);
104 if (pubint == NULL) {
105 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
109 penclen = i2d_ASN1_INTEGER(pubint, &penc);
110 ASN1_INTEGER_free(pubint);
113 DSAerr(DSA_F_DSA_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
117 if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DSA),
118 ptype, str, penc, penclen))
123 ASN1_STRING_free(str);
129 * In PKCS#8 DSA: you just get a private key integer and parameters in the
130 * AlgorithmIdentifier the pubkey must be recalculated.
133 static int dsa_priv_decode(EVP_PKEY *pkey, const PKCS8_PRIV_KEY_INFO *p8)
135 const unsigned char *p, *pm;
139 const ASN1_STRING *pstr;
140 const X509_ALGOR *palg;
141 ASN1_INTEGER *privkey = NULL;
148 if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
150 X509_ALGOR_get0(NULL, &ptype, &pval, palg);
152 if ((privkey = d2i_ASN1_INTEGER(NULL, &p, pklen)) == NULL)
154 if (privkey->type == V_ASN1_NEG_INTEGER || ptype != V_ASN1_SEQUENCE)
159 pmlen = pstr->length;
160 if ((dsa = d2i_DSAparams(NULL, &pm, pmlen)) == NULL)
162 /* We have parameters now set private key */
163 if ((dsa->priv_key = BN_secure_new()) == NULL
164 || !ASN1_INTEGER_to_BN(privkey, dsa->priv_key)) {
165 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
168 /* Calculate public key */
169 if ((dsa->pub_key = BN_new()) == NULL) {
170 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
173 if ((ctx = BN_CTX_new()) == NULL) {
174 DSAerr(DSA_F_DSA_PRIV_DECODE, ERR_R_MALLOC_FAILURE);
178 if (!BN_mod_exp(dsa->pub_key, dsa->g, dsa->priv_key, dsa->p, ctx)) {
179 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_BN_ERROR);
183 EVP_PKEY_assign_DSA(pkey, dsa);
189 DSAerr(DSA_F_DSA_PRIV_DECODE, DSA_R_DECODE_ERROR);
194 ASN1_STRING_clear_free(privkey);
198 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
200 ASN1_STRING *params = NULL;
201 ASN1_INTEGER *prkey = NULL;
202 unsigned char *dp = NULL;
205 if (!pkey->pkey.dsa || !pkey->pkey.dsa->priv_key) {
206 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_MISSING_PARAMETERS);
210 params = ASN1_STRING_new();
212 if (params == NULL) {
213 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
217 params->length = i2d_DSAparams(pkey->pkey.dsa, ¶ms->data);
218 if (params->length <= 0) {
219 DSAerr(DSA_F_DSA_PRIV_ENCODE, ERR_R_MALLOC_FAILURE);
222 params->type = V_ASN1_SEQUENCE;
224 /* Get private key into integer */
225 prkey = BN_to_ASN1_INTEGER(pkey->pkey.dsa->priv_key, NULL);
228 DSAerr(DSA_F_DSA_PRIV_ENCODE, DSA_R_BN_ERROR);
232 dplen = i2d_ASN1_INTEGER(prkey, &dp);
234 ASN1_STRING_clear_free(prkey);
237 if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dsa), 0,
238 V_ASN1_SEQUENCE, params, dp, dplen))
245 ASN1_STRING_free(params);
246 ASN1_STRING_clear_free(prkey);
250 static int int_dsa_size(const EVP_PKEY *pkey)
252 return (DSA_size(pkey->pkey.dsa));
255 static int dsa_bits(const EVP_PKEY *pkey)
257 return DSA_bits(pkey->pkey.dsa);
260 static int dsa_security_bits(const EVP_PKEY *pkey)
262 return DSA_security_bits(pkey->pkey.dsa);
265 static int dsa_missing_parameters(const EVP_PKEY *pkey)
268 dsa = pkey->pkey.dsa;
269 if (dsa == NULL || dsa->p == NULL || dsa->q == NULL || dsa->g == NULL)
274 static int dsa_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
278 if (to->pkey.dsa == NULL) {
279 to->pkey.dsa = DSA_new();
280 if (to->pkey.dsa == NULL)
284 if ((a = BN_dup(from->pkey.dsa->p)) == NULL)
286 BN_free(to->pkey.dsa->p);
289 if ((a = BN_dup(from->pkey.dsa->q)) == NULL)
291 BN_free(to->pkey.dsa->q);
294 if ((a = BN_dup(from->pkey.dsa->g)) == NULL)
296 BN_free(to->pkey.dsa->g);
301 static int dsa_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
303 if (BN_cmp(a->pkey.dsa->p, b->pkey.dsa->p) ||
304 BN_cmp(a->pkey.dsa->q, b->pkey.dsa->q) ||
305 BN_cmp(a->pkey.dsa->g, b->pkey.dsa->g))
311 static int dsa_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
313 if (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 priv_key = x->priv_key;
336 pub_key = x->pub_key;
341 ktype = "Private-Key";
343 ktype = "Public-Key";
345 ktype = "DSA-Parameters";
348 if (!BIO_indent(bp, off, 128))
350 if (BIO_printf(bp, "%s: (%d bit)\n", ktype, BN_num_bits(x->p))
355 if (!ASN1_bn_print(bp, "priv:", priv_key, NULL, off))
357 if (!ASN1_bn_print(bp, "pub: ", pub_key, NULL, off))
359 if (!ASN1_bn_print(bp, "P: ", x->p, NULL, off))
361 if (!ASN1_bn_print(bp, "Q: ", x->q, NULL, off))
363 if (!ASN1_bn_print(bp, "G: ", x->g, NULL, off))
370 static int dsa_param_decode(EVP_PKEY *pkey,
371 const unsigned char **pder, int derlen)
375 if ((dsa = d2i_DSAparams(NULL, pder, derlen)) == NULL) {
376 DSAerr(DSA_F_DSA_PARAM_DECODE, ERR_R_DSA_LIB);
379 EVP_PKEY_assign_DSA(pkey, dsa);
383 static int dsa_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
385 return i2d_DSAparams(pkey->pkey.dsa, pder);
388 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
391 return do_dsa_print(bp, pkey->pkey.dsa, indent, 0);
394 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent,
397 return do_dsa_print(bp, pkey->pkey.dsa, indent, 1);
400 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent,
403 return do_dsa_print(bp, pkey->pkey.dsa, indent, 2);
406 static int old_dsa_priv_decode(EVP_PKEY *pkey,
407 const unsigned char **pder, int derlen)
411 if ((dsa = d2i_DSAPrivateKey(NULL, pder, derlen)) == NULL) {
412 DSAerr(DSA_F_OLD_DSA_PRIV_DECODE, ERR_R_DSA_LIB);
415 EVP_PKEY_assign_DSA(pkey, dsa);
419 static int old_dsa_priv_encode(const EVP_PKEY *pkey, unsigned char **pder)
421 return i2d_DSAPrivateKey(pkey->pkey.dsa, pder);
424 static int dsa_sig_print(BIO *bp, const X509_ALGOR *sigalg,
425 const ASN1_STRING *sig, int indent, ASN1_PCTX *pctx)
428 const unsigned char *p;
431 if (BIO_puts(bp, "\n") <= 0)
437 dsa_sig = d2i_DSA_SIG(NULL, &p, sig->length);
442 DSA_SIG_get0(dsa_sig, &r, &s);
444 if (BIO_write(bp, "\n", 1) != 1)
447 if (!ASN1_bn_print(bp, "r: ", r, NULL, indent))
449 if (!ASN1_bn_print(bp, "s: ", s, NULL, indent))
453 DSA_SIG_free(dsa_sig);
456 return X509_signature_dump(bp, sig, indent);
459 static int dsa_pkey_ctrl(EVP_PKEY *pkey, int op, long arg1, void *arg2)
462 case ASN1_PKEY_CTRL_PKCS7_SIGN:
465 X509_ALGOR *alg1, *alg2;
466 PKCS7_SIGNER_INFO_get0_algs(arg2, NULL, &alg1, &alg2);
467 if (alg1 == NULL || alg1->algorithm == NULL)
469 hnid = OBJ_obj2nid(alg1->algorithm);
470 if (hnid == NID_undef)
472 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
474 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
477 #ifndef OPENSSL_NO_CMS
478 case ASN1_PKEY_CTRL_CMS_SIGN:
481 X509_ALGOR *alg1, *alg2;
482 CMS_SignerInfo_get0_algs(arg2, NULL, NULL, &alg1, &alg2);
483 if (alg1 == NULL || alg1->algorithm == NULL)
485 hnid = OBJ_obj2nid(alg1->algorithm);
486 if (hnid == NID_undef)
488 if (!OBJ_find_sigid_by_algs(&snid, hnid, EVP_PKEY_id(pkey)))
490 X509_ALGOR_set0(alg2, OBJ_nid2obj(snid), V_ASN1_UNDEF, 0);
494 case ASN1_PKEY_CTRL_CMS_RI_TYPE:
495 *(int *)arg2 = CMS_RECIPINFO_NONE;
499 case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
500 *(int *)arg2 = NID_sha256;
510 /* NB these are sorted in pkey_id order, lowest first */
512 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths[5] = {
540 "OpenSSL DSA method",
557 dsa_missing_parameters,