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 BN_set_flags(dsa
->priv_key
, BN_FLG_CONSTTIME
);
179 if (!BN_mod_exp(dsa
->pub_key
, dsa
->g
, dsa
->priv_key
, dsa
->p
, ctx
)) {
180 DSAerr(DSA_F_DSA_PRIV_DECODE
, DSA_R_BN_ERROR
);
184 EVP_PKEY_assign_DSA(pkey
, dsa
);
190 DSAerr(DSA_F_DSA_PRIV_DECODE
, DSA_R_DECODE_ERROR
);
195 ASN1_STRING_clear_free(privkey
);
199 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pkey
)
201 ASN1_STRING
*params
= NULL
;
202 ASN1_INTEGER
*prkey
= NULL
;
203 unsigned char *dp
= NULL
;
206 if (!pkey
->pkey
.dsa
|| !pkey
->pkey
.dsa
->priv_key
) {
207 DSAerr(DSA_F_DSA_PRIV_ENCODE
, DSA_R_MISSING_PARAMETERS
);
211 params
= ASN1_STRING_new();
213 if (params
== NULL
) {
214 DSAerr(DSA_F_DSA_PRIV_ENCODE
, ERR_R_MALLOC_FAILURE
);
218 params
->length
= i2d_DSAparams(pkey
->pkey
.dsa
, ¶ms
->data
);
219 if (params
->length
<= 0) {
220 DSAerr(DSA_F_DSA_PRIV_ENCODE
, ERR_R_MALLOC_FAILURE
);
223 params
->type
= V_ASN1_SEQUENCE
;
225 /* Get private key into integer */
226 prkey
= BN_to_ASN1_INTEGER(pkey
->pkey
.dsa
->priv_key
, NULL
);
229 DSAerr(DSA_F_DSA_PRIV_ENCODE
, DSA_R_BN_ERROR
);
233 dplen
= i2d_ASN1_INTEGER(prkey
, &dp
);
235 ASN1_STRING_clear_free(prkey
);
238 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(NID_dsa
), 0,
239 V_ASN1_SEQUENCE
, params
, dp
, dplen
))
246 ASN1_STRING_free(params
);
247 ASN1_STRING_clear_free(prkey
);
251 static int int_dsa_size(const EVP_PKEY
*pkey
)
253 return DSA_size(pkey
->pkey
.dsa
);
256 static int dsa_bits(const EVP_PKEY
*pkey
)
258 return DSA_bits(pkey
->pkey
.dsa
);
261 static int dsa_security_bits(const EVP_PKEY
*pkey
)
263 return DSA_security_bits(pkey
->pkey
.dsa
);
266 static int dsa_missing_parameters(const EVP_PKEY
*pkey
)
269 dsa
= pkey
->pkey
.dsa
;
270 if (dsa
== NULL
|| dsa
->p
== NULL
|| dsa
->q
== NULL
|| dsa
->g
== NULL
)
275 static int dsa_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
279 if (to
->pkey
.dsa
== NULL
) {
280 to
->pkey
.dsa
= DSA_new();
281 if (to
->pkey
.dsa
== NULL
)
285 if ((a
= BN_dup(from
->pkey
.dsa
->p
)) == NULL
)
287 BN_free(to
->pkey
.dsa
->p
);
290 if ((a
= BN_dup(from
->pkey
.dsa
->q
)) == NULL
)
292 BN_free(to
->pkey
.dsa
->q
);
295 if ((a
= BN_dup(from
->pkey
.dsa
->g
)) == NULL
)
297 BN_free(to
->pkey
.dsa
->g
);
302 static int dsa_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
304 if (BN_cmp(a
->pkey
.dsa
->p
, b
->pkey
.dsa
->p
) ||
305 BN_cmp(a
->pkey
.dsa
->q
, b
->pkey
.dsa
->q
) ||
306 BN_cmp(a
->pkey
.dsa
->g
, b
->pkey
.dsa
->g
))
312 static int dsa_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
314 if (BN_cmp(b
->pkey
.dsa
->pub_key
, a
->pkey
.dsa
->pub_key
) != 0)
320 static void int_dsa_free(EVP_PKEY
*pkey
)
322 DSA_free(pkey
->pkey
.dsa
);
325 static int do_dsa_print(BIO
*bp
, const DSA
*x
, int off
, int ptype
)
328 const char *ktype
= NULL
;
329 const BIGNUM
*priv_key
, *pub_key
;
332 priv_key
= x
->priv_key
;
337 pub_key
= x
->pub_key
;
342 ktype
= "Private-Key";
344 ktype
= "Public-Key";
346 ktype
= "DSA-Parameters";
349 if (!BIO_indent(bp
, off
, 128))
351 if (BIO_printf(bp
, "%s: (%d bit)\n", ktype
, BN_num_bits(x
->p
))
356 if (!ASN1_bn_print(bp
, "priv:", priv_key
, NULL
, off
))
358 if (!ASN1_bn_print(bp
, "pub: ", pub_key
, NULL
, off
))
360 if (!ASN1_bn_print(bp
, "P: ", x
->p
, NULL
, off
))
362 if (!ASN1_bn_print(bp
, "Q: ", x
->q
, NULL
, off
))
364 if (!ASN1_bn_print(bp
, "G: ", x
->g
, NULL
, 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
);
380 EVP_PKEY_assign_DSA(pkey
, dsa
);
384 static int dsa_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
386 return i2d_DSAparams(pkey
->pkey
.dsa
, pder
);
389 static int dsa_param_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
392 return do_dsa_print(bp
, pkey
->pkey
.dsa
, indent
, 0);
395 static int dsa_pub_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
398 return do_dsa_print(bp
, pkey
->pkey
.dsa
, indent
, 1);
401 static int dsa_priv_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
404 return do_dsa_print(bp
, pkey
->pkey
.dsa
, indent
, 2);
407 static int old_dsa_priv_decode(EVP_PKEY
*pkey
,
408 const unsigned char **pder
, int derlen
)
412 if ((dsa
= d2i_DSAPrivateKey(NULL
, pder
, derlen
)) == NULL
) {
413 DSAerr(DSA_F_OLD_DSA_PRIV_DECODE
, ERR_R_DSA_LIB
);
416 EVP_PKEY_assign_DSA(pkey
, dsa
);
420 static int old_dsa_priv_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
422 return i2d_DSAPrivateKey(pkey
->pkey
.dsa
, pder
);
425 static int dsa_sig_print(BIO
*bp
, const X509_ALGOR
*sigalg
,
426 const ASN1_STRING
*sig
, int indent
, ASN1_PCTX
*pctx
)
429 const unsigned char *p
;
432 if (BIO_puts(bp
, "\n") <= 0)
438 dsa_sig
= d2i_DSA_SIG(NULL
, &p
, sig
->length
);
443 DSA_SIG_get0(dsa_sig
, &r
, &s
);
445 if (BIO_write(bp
, "\n", 1) != 1)
448 if (!ASN1_bn_print(bp
, "r: ", r
, NULL
, indent
))
450 if (!ASN1_bn_print(bp
, "s: ", s
, NULL
, indent
))
454 DSA_SIG_free(dsa_sig
);
457 return X509_signature_dump(bp
, sig
, indent
);
460 static int dsa_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
463 case ASN1_PKEY_CTRL_PKCS7_SIGN
:
466 X509_ALGOR
*alg1
, *alg2
;
467 PKCS7_SIGNER_INFO_get0_algs(arg2
, NULL
, &alg1
, &alg2
);
468 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
470 hnid
= OBJ_obj2nid(alg1
->algorithm
);
471 if (hnid
== NID_undef
)
473 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
475 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
478 #ifndef OPENSSL_NO_CMS
479 case ASN1_PKEY_CTRL_CMS_SIGN
:
482 X509_ALGOR
*alg1
, *alg2
;
483 CMS_SignerInfo_get0_algs(arg2
, NULL
, NULL
, &alg1
, &alg2
);
484 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
486 hnid
= OBJ_obj2nid(alg1
->algorithm
);
487 if (hnid
== NID_undef
)
489 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
491 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
495 case ASN1_PKEY_CTRL_CMS_RI_TYPE
:
496 *(int *)arg2
= CMS_RECIPINFO_NONE
;
500 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
501 *(int *)arg2
= NID_sha256
;
511 /* NB these are sorted in pkey_id order, lowest first */
513 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths
[5] = {
541 "OpenSSL DSA method",
558 dsa_missing_parameters
,