1 /* $OpenBSD: dsa_ameth.c,v 1.23 2017/01/29 17:49:22 beck Exp $ */
2 /* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
61 #include <openssl/opensslconf.h>
63 #include <openssl/asn1.h>
64 #include <openssl/bn.h>
65 #include <openssl/dsa.h>
66 #include <openssl/err.h>
67 #include <openssl/x509.h>
69 #include "asn1_locl.h"
73 dsa_pub_decode(EVP_PKEY
*pkey
, X509_PUBKEY
*pubkey
)
75 const unsigned char *p
, *pm
;
81 ASN1_INTEGER
*public_key
= NULL
;
85 if (!X509_PUBKEY_get0_param(NULL
, &p
, &pklen
, &palg
, pubkey
))
87 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
89 if (ptype
== V_ASN1_SEQUENCE
) {
94 if (!(dsa
= d2i_DSAparams(NULL
, &pm
, pmlen
))) {
95 DSAerror(DSA_R_DECODE_ERROR
);
98 } else if (ptype
== V_ASN1_NULL
|| ptype
== V_ASN1_UNDEF
) {
99 if (!(dsa
= DSA_new())) {
100 DSAerror(ERR_R_MALLOC_FAILURE
);
104 DSAerror(DSA_R_PARAMETER_ENCODING_ERROR
);
108 if (!(public_key
=d2i_ASN1_INTEGER(NULL
, &p
, pklen
))) {
109 DSAerror(DSA_R_DECODE_ERROR
);
113 if (!(dsa
->pub_key
= ASN1_INTEGER_to_BN(public_key
, NULL
))) {
114 DSAerror(DSA_R_BN_DECODE_ERROR
);
118 ASN1_INTEGER_free(public_key
);
119 EVP_PKEY_assign_DSA(pkey
, dsa
);
124 ASN1_INTEGER_free(public_key
);
130 dsa_pub_encode(X509_PUBKEY
*pk
, const EVP_PKEY
*pkey
)
135 unsigned char *penc
= NULL
;
138 dsa
= pkey
->pkey
.dsa
;
139 if (pkey
->save_parameters
&& dsa
->p
&& dsa
->q
&& dsa
->g
) {
142 str
= ASN1_STRING_new();
144 DSAerror(ERR_R_MALLOC_FAILURE
);
147 str
->length
= i2d_DSAparams(dsa
, &str
->data
);
148 if (str
->length
<= 0) {
149 DSAerror(ERR_R_MALLOC_FAILURE
);
150 ASN1_STRING_free(str
);
154 ptype
= V_ASN1_SEQUENCE
;
156 ptype
= V_ASN1_UNDEF
;
158 dsa
->write_params
= 0;
160 penclen
= i2d_DSAPublicKey(dsa
, &penc
);
163 DSAerror(ERR_R_MALLOC_FAILURE
);
167 if (X509_PUBKEY_set0_param(pk
, OBJ_nid2obj(EVP_PKEY_DSA
), ptype
, pval
,
173 ASN1_STRING_free(pval
);
178 /* In PKCS#8 DSA: you just get a private key integer and parameters in the
179 * AlgorithmIdentifier the pubkey must be recalculated.
182 dsa_priv_decode(EVP_PKEY
*pkey
, PKCS8_PRIV_KEY_INFO
*p8
)
184 const unsigned char *p
, *pm
;
190 ASN1_INTEGER
*privkey
= NULL
;
196 if (!PKCS8_pkey_get0(NULL
, &p
, &pklen
, &palg
, p8
))
198 X509_ALGOR_get0(NULL
, &ptype
, &pval
, palg
);
199 if (ptype
!= V_ASN1_SEQUENCE
)
202 if ((privkey
= d2i_ASN1_INTEGER(NULL
, &p
, pklen
)) == NULL
)
204 if (privkey
->type
== V_ASN1_NEG_INTEGER
)
209 pmlen
= pstr
->length
;
210 if (!(dsa
= d2i_DSAparams(NULL
, &pm
, pmlen
)))
212 /* We have parameters now set private key */
213 if (!(dsa
->priv_key
= ASN1_INTEGER_to_BN(privkey
, NULL
))) {
214 DSAerror(DSA_R_BN_ERROR
);
217 /* Calculate public key */
218 if (!(dsa
->pub_key
= BN_new())) {
219 DSAerror(ERR_R_MALLOC_FAILURE
);
222 if (!(ctx
= BN_CTX_new())) {
223 DSAerror(ERR_R_MALLOC_FAILURE
);
227 if (!BN_mod_exp_ct(dsa
->pub_key
, dsa
->g
, dsa
->priv_key
, dsa
->p
, ctx
)) {
228 DSAerror(DSA_R_BN_ERROR
);
232 if (!EVP_PKEY_assign_DSA(pkey
, dsa
))
239 DSAerror(DSA_R_DECODE_ERROR
);
244 ASN1_INTEGER_free(privkey
);
249 dsa_priv_encode(PKCS8_PRIV_KEY_INFO
*p8
, const EVP_PKEY
*pkey
)
251 ASN1_STRING
*params
= NULL
;
252 ASN1_INTEGER
*prkey
= NULL
;
253 unsigned char *dp
= NULL
;
256 params
= ASN1_STRING_new();
258 DSAerror(ERR_R_MALLOC_FAILURE
);
262 params
->length
= i2d_DSAparams(pkey
->pkey
.dsa
, ¶ms
->data
);
263 if (params
->length
<= 0) {
264 DSAerror(ERR_R_MALLOC_FAILURE
);
267 params
->type
= V_ASN1_SEQUENCE
;
269 /* Get private key into integer */
270 prkey
= BN_to_ASN1_INTEGER(pkey
->pkey
.dsa
->priv_key
, NULL
);
272 DSAerror(DSA_R_BN_ERROR
);
276 dplen
= i2d_ASN1_INTEGER(prkey
, &dp
);
278 ASN1_INTEGER_free(prkey
);
281 if (!PKCS8_pkey_set0(p8
, OBJ_nid2obj(NID_dsa
), 0, V_ASN1_SEQUENCE
,
289 ASN1_STRING_free(params
);
290 ASN1_INTEGER_free(prkey
);
295 int_dsa_size(const EVP_PKEY
*pkey
)
297 return DSA_size(pkey
->pkey
.dsa
);
301 dsa_bits(const EVP_PKEY
*pkey
)
303 return BN_num_bits(pkey
->pkey
.dsa
->p
);
307 dsa_missing_parameters(const EVP_PKEY
*pkey
)
311 dsa
= pkey
->pkey
.dsa
;
312 if (dsa
->p
== NULL
|| dsa
->q
== NULL
|| dsa
->g
== NULL
)
318 dsa_copy_parameters(EVP_PKEY
*to
, const EVP_PKEY
*from
)
322 if ((a
= BN_dup(from
->pkey
.dsa
->p
)) == NULL
)
324 BN_free(to
->pkey
.dsa
->p
);
327 if ((a
= BN_dup(from
->pkey
.dsa
->q
)) == NULL
)
329 BN_free(to
->pkey
.dsa
->q
);
332 if ((a
= BN_dup(from
->pkey
.dsa
->g
)) == NULL
)
334 BN_free(to
->pkey
.dsa
->g
);
340 dsa_cmp_parameters(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
342 if (BN_cmp(a
->pkey
.dsa
->p
, b
->pkey
.dsa
->p
) ||
343 BN_cmp(a
->pkey
.dsa
->q
, b
->pkey
.dsa
->q
) ||
344 BN_cmp(a
->pkey
.dsa
->g
, b
->pkey
.dsa
->g
))
351 dsa_pub_cmp(const EVP_PKEY
*a
, const EVP_PKEY
*b
)
353 if (BN_cmp(b
->pkey
.dsa
->pub_key
, a
->pkey
.dsa
->pub_key
) != 0)
360 int_dsa_free(EVP_PKEY
*pkey
)
362 DSA_free(pkey
->pkey
.dsa
);
366 update_buflen(const BIGNUM
*b
, size_t *pbuflen
)
372 if (*pbuflen
< (i
= (size_t)BN_num_bytes(b
)))
377 do_dsa_print(BIO
*bp
, const DSA
*x
, int off
, int ptype
)
379 unsigned char *m
= NULL
;
382 const char *ktype
= NULL
;
383 const BIGNUM
*priv_key
, *pub_key
;
386 priv_key
= x
->priv_key
;
391 pub_key
= x
->pub_key
;
396 ktype
= "Private-Key";
398 ktype
= "Public-Key";
400 ktype
= "DSA-Parameters";
402 update_buflen(x
->p
, &buf_len
);
403 update_buflen(x
->q
, &buf_len
);
404 update_buflen(x
->g
, &buf_len
);
405 update_buflen(priv_key
, &buf_len
);
406 update_buflen(pub_key
, &buf_len
);
408 m
= malloc(buf_len
+ 10);
410 DSAerror(ERR_R_MALLOC_FAILURE
);
415 if (!BIO_indent(bp
, off
, 128))
417 if (BIO_printf(bp
, "%s: (%d bit)\n", ktype
,
418 BN_num_bits(x
->p
)) <= 0)
422 if (!ASN1_bn_print(bp
, "priv:", priv_key
, m
, off
))
424 if (!ASN1_bn_print(bp
, "pub: ", pub_key
, m
, off
))
426 if (!ASN1_bn_print(bp
, "P: ", x
->p
, m
, off
))
428 if (!ASN1_bn_print(bp
, "Q: ", x
->q
, m
, off
))
430 if (!ASN1_bn_print(bp
, "G: ", x
->g
, m
, off
))
439 dsa_param_decode(EVP_PKEY
*pkey
, const unsigned char **pder
, int derlen
)
443 if (!(dsa
= d2i_DSAparams(NULL
, pder
, derlen
))) {
444 DSAerror(ERR_R_DSA_LIB
);
447 EVP_PKEY_assign_DSA(pkey
, dsa
);
452 dsa_param_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
454 return i2d_DSAparams(pkey
->pkey
.dsa
, pder
);
458 dsa_param_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
, ASN1_PCTX
*ctx
)
460 return do_dsa_print(bp
, pkey
->pkey
.dsa
, indent
, 0);
464 dsa_pub_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
, ASN1_PCTX
*ctx
)
466 return do_dsa_print(bp
, pkey
->pkey
.dsa
, indent
, 1);
470 dsa_priv_print(BIO
*bp
, const EVP_PKEY
*pkey
, int indent
, ASN1_PCTX
*ctx
)
472 return do_dsa_print(bp
, pkey
->pkey
.dsa
, indent
, 2);
476 old_dsa_priv_decode(EVP_PKEY
*pkey
, const unsigned char **pder
, int derlen
)
480 BIGNUM
*j
, *p1
, *newp1
;
482 if (!(dsa
= d2i_DSAPrivateKey(NULL
, pder
, derlen
))) {
483 DSAerror(ERR_R_DSA_LIB
);
492 * Check that p and q are consistent with each other.
496 p1
= BN_CTX_get(ctx
);
497 newp1
= BN_CTX_get(ctx
);
498 if (j
== NULL
|| p1
== NULL
|| newp1
== NULL
)
501 if (BN_sub(p1
, dsa
->p
, BN_value_one()) == 0)
503 /* j = (p - 1) / q */
504 if (BN_div_ct(j
, NULL
, p1
, dsa
->q
, ctx
) == 0)
506 /* q * j should == p - 1 */
507 if (BN_mul(newp1
, dsa
->q
, j
, ctx
) == 0)
509 if (BN_cmp(newp1
, p1
) != 0) {
510 DSAerror(DSA_R_BAD_Q_VALUE
);
515 * Check that q is not a composite number.
518 if (BN_is_prime_ex(dsa
->q
, BN_prime_checks
, ctx
, NULL
) == 0) {
519 DSAerror(DSA_R_BAD_Q_VALUE
);
525 EVP_PKEY_assign_DSA(pkey
, dsa
);
535 old_dsa_priv_encode(const EVP_PKEY
*pkey
, unsigned char **pder
)
537 return i2d_DSAPrivateKey(pkey
->pkey
.dsa
, pder
);
541 dsa_sig_print(BIO
*bp
, const X509_ALGOR
*sigalg
, const ASN1_STRING
*sig
,
542 int indent
, ASN1_PCTX
*pctx
)
545 const unsigned char *p
;
548 if (BIO_puts(bp
, "\n") <= 0)
554 dsa_sig
= d2i_DSA_SIG(NULL
, &p
, sig
->length
);
558 unsigned char *m
= NULL
;
560 update_buflen(dsa_sig
->r
, &buf_len
);
561 update_buflen(dsa_sig
->s
, &buf_len
);
562 m
= malloc(buf_len
+ 10);
564 DSAerror(ERR_R_MALLOC_FAILURE
);
568 if (BIO_write(bp
, "\n", 1) != 1)
571 if (!ASN1_bn_print(bp
, "r: ", dsa_sig
->r
, m
, indent
))
573 if (!ASN1_bn_print(bp
, "s: ", dsa_sig
->s
, m
, indent
))
578 DSA_SIG_free(dsa_sig
);
581 return X509_signature_dump(bp
, sig
, indent
);
585 dsa_pkey_ctrl(EVP_PKEY
*pkey
, int op
, long arg1
, void *arg2
)
588 case ASN1_PKEY_CTRL_PKCS7_SIGN
:
591 X509_ALGOR
*alg1
, *alg2
;
593 PKCS7_SIGNER_INFO_get0_algs(arg2
, NULL
, &alg1
, &alg2
);
594 if (alg1
== NULL
|| alg1
->algorithm
== NULL
)
596 hnid
= OBJ_obj2nid(alg1
->algorithm
);
597 if (hnid
== NID_undef
)
599 if (!OBJ_find_sigid_by_algs(&snid
, hnid
,
602 X509_ALGOR_set0(alg2
, OBJ_nid2obj(snid
), V_ASN1_UNDEF
,
607 case ASN1_PKEY_CTRL_DEFAULT_MD_NID
:
608 *(int *)arg2
= NID_sha1
;
616 /* NB these are sorted in pkey_id order, lowest first */
618 const EVP_PKEY_ASN1_METHOD dsa_asn1_meths
[] = {
620 .pkey_id
= EVP_PKEY_DSA2
,
621 .pkey_base_id
= EVP_PKEY_DSA
,
622 .pkey_flags
= ASN1_PKEY_ALIAS
626 .pkey_id
= EVP_PKEY_DSA1
,
627 .pkey_base_id
= EVP_PKEY_DSA
,
628 .pkey_flags
= ASN1_PKEY_ALIAS
632 .pkey_id
= EVP_PKEY_DSA4
,
633 .pkey_base_id
= EVP_PKEY_DSA
,
634 .pkey_flags
= ASN1_PKEY_ALIAS
638 .pkey_id
= EVP_PKEY_DSA3
,
639 .pkey_base_id
= EVP_PKEY_DSA
,
640 .pkey_flags
= ASN1_PKEY_ALIAS
644 .pkey_id
= EVP_PKEY_DSA
,
645 .pkey_base_id
= EVP_PKEY_DSA
,
648 .info
= "OpenSSL DSA method",
650 .pub_decode
= dsa_pub_decode
,
651 .pub_encode
= dsa_pub_encode
,
652 .pub_cmp
= dsa_pub_cmp
,
653 .pub_print
= dsa_pub_print
,
655 .priv_decode
= dsa_priv_decode
,
656 .priv_encode
= dsa_priv_encode
,
657 .priv_print
= dsa_priv_print
,
659 .pkey_size
= int_dsa_size
,
660 .pkey_bits
= dsa_bits
,
662 .param_decode
= dsa_param_decode
,
663 .param_encode
= dsa_param_encode
,
664 .param_missing
= dsa_missing_parameters
,
665 .param_copy
= dsa_copy_parameters
,
666 .param_cmp
= dsa_cmp_parameters
,
667 .param_print
= dsa_param_print
,
668 .sig_print
= dsa_sig_print
,
670 .pkey_free
= int_dsa_free
,
671 .pkey_ctrl
= dsa_pkey_ctrl
,
672 .old_priv_decode
= old_dsa_priv_decode
,
673 .old_priv_encode
= old_dsa_priv_encode