3 Decoding of DSA keys in OpenSSL and X.509.1 format.
5 Copyright (C) 2005, 2009 Niels Möller, Magnus Holmgren
6 Copyright (C) 2014 Niels Möller
8 This file is part of GNU Nettle.
10 GNU Nettle is free software: you can redistribute it and/or
11 modify it under the terms of either:
13 * the GNU Lesser General Public License as published by the Free
14 Software Foundation; either version 3 of the License, or (at your
15 option) any later version.
19 * the GNU General Public License as published by the Free
20 Software Foundation; either version 2 of the License, or (at your
21 option) any later version.
23 or both in parallel, as here.
25 GNU Nettle is distributed in the hope that it will be useful,
26 but WITHOUT ANY WARRANTY; without even the implied warranty of
27 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28 General Public License for more details.
30 You should have received copies of the GNU General Public License and
31 the GNU Lesser General Public License along with this program. If
32 not, see http://www.gnu.org/licenses/.
44 #define GET(i, x, l) \
45 (asn1_der_iterator_next((i)) == ASN1_ITERATOR_PRIMITIVE \
46 && (i)->type == ASN1_INTEGER \
47 && asn1_der_get_bignum((i), (x), (l)) \
50 /* If q_bits > 0, q is required to be of exactly this size. */
52 dsa_params_from_der_iterator(struct dsa_params *params,
53 unsigned max_bits, unsigned q_bits,
54 struct asn1_der_iterator *i)
56 /* Dss-Parms ::= SEQUENCE {
62 if (i->type == ASN1_INTEGER
63 && asn1_der_get_bignum(i, params->p, max_bits)
64 && mpz_sgn(params->p) > 0)
66 unsigned p_bits = mpz_sizeinbase (params->p, 2);
67 return (GET(i, params->q, q_bits ? q_bits : p_bits)
68 && (q_bits == 0 || mpz_sizeinbase(params->q, 2) == q_bits)
69 && mpz_cmp (params->q, params->p) < 0
70 && GET(i, params->g, p_bits)
71 && mpz_cmp (params->g, params->p) < 0
72 && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
79 dsa_public_key_from_der_iterator(const struct dsa_params *params,
81 struct asn1_der_iterator *i)
83 /* DSAPublicKey ::= INTEGER
86 return (i->type == ASN1_INTEGER
87 && asn1_der_get_bignum(i, pub,
88 mpz_sizeinbase (params->p, 2))
90 && mpz_cmp(pub, params->p) < 0);
94 dsa_openssl_private_key_from_der_iterator(struct dsa_params *params,
98 struct asn1_der_iterator *i)
100 /* DSAPrivateKey ::= SEQUENCE {
105 pub_key INTEGER, -- y
106 priv_key INTEGER, -- x
112 if (i->type == ASN1_SEQUENCE
113 && asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
114 && i->type == ASN1_INTEGER
115 && asn1_der_get_uint32(i, &version)
117 && GET(i, params->p, p_max_bits))
119 unsigned p_bits = mpz_sizeinbase (params->p, 2);
120 return (GET(i, params->q, DSA_SHA1_Q_BITS)
121 && GET(i, params->g, p_bits)
122 && mpz_cmp (params->g, params->p) < 0
123 && GET(i, pub, p_bits)
124 && mpz_cmp (pub, params->p) < 0
125 && GET(i, priv, DSA_SHA1_Q_BITS)
126 && asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
133 dsa_openssl_private_key_from_der(struct dsa_params *params,
137 size_t length, const uint8_t *data)
139 struct asn1_der_iterator i;
140 enum asn1_iterator_result res;
142 res = asn1_der_iterator_first(&i, length, data);
144 return (res == ASN1_ITERATOR_CONSTRUCTED
145 && dsa_openssl_private_key_from_der_iterator(params, pub, priv,