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 "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
;
88 if (pkey
->save_parameters
&& dsa
->p
&& dsa
->q
&& dsa
->g
) {
89 str
= ASN1_STRING_new();
91 DSAerr(DSA_F_DSA_PUB_ENCODE
, ERR_R_MALLOC_FAILURE
);
94 str
->length
= i2d_DSAparams(dsa
, &str
->data
);
95 if (str
->length
<= 0) {
96 DSAerr(DSA_F_DSA_PUB_ENCODE
, ERR_R_MALLOC_FAILURE
);
99 ptype
= V_ASN1_SEQUENCE
;
101 ptype
= V_ASN1_UNDEF
;
103 pubint
= BN_to_ASN1_INTEGER(dsa
->pub_key
, NULL
);
105 if (pubint
== NULL
) {
106 DSAerr(DSA_F_DSA_PUB_ENCODE
, ERR_R_MALLOC_FAILURE
);
110 penclen
= i2d_ASN1_INTEGER(pubint
, &penc
);
111 ASN1_INTEGER_free(pubint
);
114 DSAerr(DSA_F_DSA_PUB_ENCODE
, ERR_R_MALLOC_FAILURE
);
118 aobj
= OBJ_nid2obj(EVP_PKEY_DSA
);
122 if (X509_PUBKEY_set0_param(pk
, aobj
, ptype
, str
, penc
, penclen
))
127 ASN1_STRING_free(str
);
133 * In PKCS#8 DSA: you just get a private key integer and parameters in the
134 * AlgorithmIdentifier the pubkey must be recalculated.
137 static int dsa_priv_decode(EVP_PKEY
*pkey
, const PKCS8_PRIV_KEY_INFO
*p8
)
139 const unsigned char *p
, *pm
;
143 const ASN1_STRING
*pstr
;
144 const X509_ALGOR
*palg
;
145 ASN1_INTEGER
*privkey
= NULL
;
152 if (!PKCS8_pkey_get0(NULL
, &p
, &pklen
, &palg
, p8
))
154 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
156 if ((privkey
= d2i_ASN1_INTEGER(NULL
, &p
, pklen
)) == NULL
)
158 if (privkey
->type
== V_ASN1_NEG_INTEGER
|| ptype
!= V_ASN1_SEQUENCE
)
163 pmlen
= pstr
->length
;
164 if ((dsa
= d2i_DSAparams(NULL
, &pm
, pmlen
)) == NULL
)
166 /* We have parameters now set private key */
167 if ((dsa
->priv_key
= BN_secure_new()) == NULL
168 || !ASN1_INTEGER_to_BN(privkey
, dsa
->priv_key
)) {
169 DSAerr(DSA_F_DSA_PRIV_DECODE
, DSA_R_BN_ERROR
);
172 /* Calculate public key */
173 if ((dsa
->pub_key
= BN_new()) == NULL
) {
174 DSAerr(DSA_F_DSA_PRIV_DECODE
, ERR_R_MALLOC_FAILURE
);
177 if ((ctx
= BN_CTX_new()) == NULL
) {
178 DSAerr(DSA_F_DSA_PRIV_DECODE
, ERR_R_MALLOC_FAILURE
);
182 BN_set_flags(dsa
->priv_key
, BN_FLG_CONSTTIME
);
183 if (!BN_mod_exp(dsa
->pub_key
, dsa
->g
, dsa
->priv_key
, dsa
->p
, ctx
)) {
184 DSAerr(DSA_F_DSA_PRIV_DECODE
, DSA_R_BN_ERROR
);
188 EVP_PKEY_assign_DSA(pkey
, dsa
);
194 DSAerr(DSA_F_DSA_PRIV_DECODE
, DSA_R_DECODE_ERROR
);
199 ASN1_STRING_clear_free(privkey
);
203 static int dsa_priv_encode(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pkey
)
205 ASN1_STRING
*params
= NULL
;
206 ASN1_INTEGER
*prkey
= NULL
;
207 unsigned char *dp
= NULL
;
210 if (!pkey
->pkey
.dsa
|| !pkey
->pkey
.dsa
->priv_key
) {
211 DSAerr(DSA_F_DSA_PRIV_ENCODE
, DSA_R_MISSING_PARAMETERS
);
215 params
= ASN1_STRING_new();
217 if (params
== NULL
) {
218 DSAerr(DSA_F_DSA_PRIV_ENCODE
, ERR_R_MALLOC_FAILURE
);
222 params
->length
= i2d_DSAparams(pkey
->pkey
.dsa
, ¶ms
->data
);
223 if (params
->length
<= 0) {
224 DSAerr(DSA_F_DSA_PRIV_ENCODE
, ERR_R_MALLOC_FAILURE
);
227 params
->type
= V_ASN1_SEQUENCE
;
229 /* Get private key into integer */
230 prkey
= BN_to_ASN1_INTEGER(pkey
->pkey
.dsa
->priv_key
, NULL
);
233 DSAerr(DSA_F_DSA_PRIV_ENCODE
, DSA_R_BN_ERROR
);
237 dplen
= i2d_ASN1_INTEGER(prkey
, &dp
);
239 ASN1_STRING_clear_free(prkey
);
242 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(NID_dsa
), 0,
243 V_ASN1_SEQUENCE
, params
, dp
, dplen
))
250 ASN1_STRING_free(params
);
251 ASN1_STRING_clear_free(prkey
);
255 static int int_dsa_size(const EVP_PKEY
*pkey
)
257 return DSA_size(pkey
->pkey
.dsa
);
260 static int dsa_bits(const EVP_PKEY
*pkey
)
262 return DSA_bits(pkey
->pkey
.dsa
);
265 static int dsa_security_bits(const EVP_PKEY
*pkey
)
267 return DSA_security_bits(pkey
->pkey
.dsa
);
270 static int dsa_missing_parameters(const EVP_PKEY
*pkey
)
273 dsa
= pkey
->pkey
.dsa
;
274 if (dsa
== NULL
|| dsa
->p
== NULL
|| dsa
->q
== NULL
|| dsa
->g
== NULL
)
279 static int dsa_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
283 if (to
->pkey
.dsa
== NULL
) {
284 to
->pkey
.dsa
= DSA_new();
285 if (to
->pkey
.dsa
== NULL
)
289 if ((a
= BN_dup(from
->pkey
.dsa
->p
)) == NULL
)
291 BN_free(to
->pkey
.dsa
->p
);
294 if ((a
= BN_dup(from
->pkey
.dsa
->q
)) == NULL
)
296 BN_free(to
->pkey
.dsa
->q
);
299 if ((a
= BN_dup(from
->pkey
.dsa
->g
)) == NULL
)
301 BN_free(to
->pkey
.dsa
->g
);
306 static int dsa_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
308 if (BN_cmp(a
->pkey
.dsa
->p
, b
->pkey
.dsa
->p
) ||
309 BN_cmp(a
->pkey
.dsa
->q
, b
->pkey
.dsa
->q
) ||
310 BN_cmp(a
->pkey
.dsa
->g
, b
->pkey
.dsa
->g
))
316 static int dsa_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
318 if (BN_cmp(b
->pkey
.dsa
->pub_key
, a
->pkey
.dsa
->pub_key
) != 0)
324 static void int_dsa_free(EVP_PKEY
*pkey
)
326 DSA_free(pkey
->pkey
.dsa
);
329 static int do_dsa_print(BIO
*bp
, const DSA
*x
, int off
, int ptype
)
332 const char *ktype
= NULL
;
333 const BIGNUM
*priv_key
, *pub_key
;
336 priv_key
= x
->priv_key
;
341 pub_key
= x
->pub_key
;
346 ktype
= "Private-Key";
348 ktype
= "Public-Key";
350 ktype
= "DSA-Parameters";
353 if (!BIO_indent(bp
, off
, 128))
355 if (BIO_printf(bp
, "%s: (%d bit)\n", ktype
, BN_num_bits(x
->p
))
360 if (!ASN1_bn_print(bp
, "priv:", priv_key
, NULL
, off
))
362 if (!ASN1_bn_print(bp
, "pub: ", pub_key
, NULL
, off
))
364 if (!ASN1_bn_print(bp
, "P: ", x
->p
, NULL
, off
))
366 if (!ASN1_bn_print(bp
, "Q: ", x
->q
, NULL
, off
))
368 if (!ASN1_bn_print(bp
, "G: ", x
->g
, NULL
, off
))
375 static int dsa_param_decode(EVP_PKEY
*pkey
,
376 const unsigned char **pder
, int derlen
)
380 if ((dsa
= d2i_DSAparams(NULL
, pder
, derlen
)) == NULL
) {
381 DSAerr(DSA_F_DSA_PARAM_DECODE
, ERR_R_DSA_LIB
);
384 EVP_PKEY_assign_DSA(pkey
, dsa
);
388 static int dsa_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
390 return i2d_DSAparams(pkey
->pkey
.dsa
, pder
);
393 static int dsa_param_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
396 return do_dsa_print(bp
, pkey
->pkey
.dsa
, indent
, 0);
399 static int dsa_pub_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
402 return do_dsa_print(bp
, pkey
->pkey
.dsa
, indent
, 1);
405 static int dsa_priv_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
,
408 return do_dsa_print(bp
, pkey
->pkey
.dsa
, indent
, 2);
411 static int old_dsa_priv_decode(EVP_PKEY
*pkey
,
412 const unsigned char **pder
, int derlen
)
416 if ((dsa
= d2i_DSAPrivateKey(NULL
, pder
, derlen
)) == NULL
) {
417 DSAerr(DSA_F_OLD_DSA_PRIV_DECODE
, ERR_R_DSA_LIB
);
420 EVP_PKEY_assign_DSA(pkey
, dsa
);
424 static int old_dsa_priv_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
426 return i2d_DSAPrivateKey(pkey
->pkey
.dsa
, pder
);
429 static int dsa_sig_print(BIO
*bp
, const X509_ALGOR
*sigalg
,
430 const ASN1_STRING
*sig
, int indent
, ASN1_PCTX
*pctx
)
433 const unsigned char *p
;
436 if (BIO_puts(bp
, "\n") <= 0)
442 dsa_sig
= d2i_DSA_SIG(NULL
, &p
, sig
->length
);
447 DSA_SIG_get0(dsa_sig
, &r
, &s
);
449 if (BIO_write(bp
, "\n", 1) != 1)
452 if (!ASN1_bn_print(bp
, "r: ", r
, NULL
, indent
))
454 if (!ASN1_bn_print(bp
, "s: ", s
, NULL
, indent
))
458 DSA_SIG_free(dsa_sig
);
461 if (BIO_puts(bp
, "\n") <= 0)
463 return X509_signature_dump(bp
, sig
, indent
);
466 static int dsa_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
469 case ASN1_PKEY_CTRL_PKCS7_SIGN
:
472 X509_ALGOR
*alg1
, *alg2
;
473 PKCS7_SIGNER_INFO_get0_algs(arg2
, NULL
, &alg1
, &alg2
);
474 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
476 hnid
= OBJ_obj2nid(alg1
->algorithm
);
477 if (hnid
== NID_undef
)
479 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
481 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
484 #ifndef OPENSSL_NO_CMS
485 case ASN1_PKEY_CTRL_CMS_SIGN
:
488 X509_ALGOR
*alg1
, *alg2
;
489 CMS_SignerInfo_get0_algs(arg2
, NULL
, NULL
, &alg1
, &alg2
);
490 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
492 hnid
= OBJ_obj2nid(alg1
->algorithm
);
493 if (hnid
== NID_undef
)
495 if (!OBJ_find_sigid_by_algs(&snid
, hnid
, EVP_PKEY_id(pkey
)))
497 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
, 0);
501 case ASN1_PKEY_CTRL_CMS_RI_TYPE
:
502 *(int *)arg2
= CMS_RECIPINFO_NONE
;
506 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
507 *(int *)arg2
= NID_sha256
;
517 /* NB these are sorted in pkey_id order, lowest first */
519 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths
[5] = {
547 "OpenSSL DSA method",
564 dsa_missing_parameters
,