3 Decoding of keys in PKCS#1 format.
5 Copyright (C) 2005 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
12 * the GNU Lesser General Public License as published by the Free
13 Software Foundation; either version 3 of the License, or (at your
14 option) any later version.
18 * the GNU General Public License as published by the Free
19 Software Foundation; either version 2 of the License, or (at your
20 option) any later version.
22 or both in parallel, as here.
24 GNU Nettle is distributed in the hope that it will be useful,
25 but WITHOUT ANY WARRANTY; without even the implied warranty of
26 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 General Public License for more details.
29 You should have received copies of the GNU General Public License and
30 the GNU Lesser General Public License along with this program. If
31 not, see http://www.gnu.org/licenses/.
43 #define GET(i, x, l) \
44 (asn1_der_iterator_next((i)) == ASN1_ITERATOR_PRIMITIVE \
45 && (i)->type == ASN1_INTEGER \
46 && asn1_der_get_bignum((i), (x), (l)) \
50 rsa_public_key_from_der_iterator(struct rsa_public_key *pub,
52 struct asn1_der_iterator *i)
54 /* RSAPublicKey ::= SEQUENCE {
56 publicExponent INTEGER -- e
60 return (i->type == ASN1_SEQUENCE
61 && asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
62 && asn1_der_get_bignum(i, pub->n, limit)
63 && mpz_sgn(pub->n) > 0
64 && GET(i, pub->e, limit)
65 && asn1_der_iterator_next(i) == ASN1_ITERATOR_END
66 && rsa_public_key_prepare(pub));
70 rsa_private_key_from_der_iterator(struct rsa_public_key *pub,
71 struct rsa_private_key *priv,
73 struct asn1_der_iterator *i)
75 /* RSAPrivateKey ::= SEQUENCE {
78 publicExponent INTEGER, -- e
79 privateExponent INTEGER, -- d
82 exponent1 INTEGER, -- d mod (p-1)
83 exponent2 INTEGER, -- d mod (q-1)
84 coefficient INTEGER, -- (inverse of q) mod p
85 otherPrimeInfos OtherPrimeInfos OPTIONAL
91 if (i->type != ASN1_SEQUENCE)
94 if (asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
95 && i->type == ASN1_INTEGER
96 && asn1_der_get_uint32(i, &version)
98 && GET(i, pub->n, limit)
99 && GET(i, pub->e, limit)
100 && rsa_public_key_prepare(pub)
101 && GET(i, priv->d, limit)
102 && GET(i, priv->p, limit)
103 && GET(i, priv->q, limit)
104 && GET(i, priv->a, limit)
105 && GET(i, priv->b, limit)
106 && GET(i, priv->c, limit)
107 && rsa_private_key_prepare(priv))
111 /* otherPrimeInfos must be present. We ignore the contents */
112 if (!(asn1_der_iterator_next(i) == ASN1_ITERATOR_CONSTRUCTED
113 && i->type == ASN1_SEQUENCE))
117 return (asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
124 rsa_keypair_from_der(struct rsa_public_key *pub,
125 struct rsa_private_key *priv,
127 size_t length, const uint8_t *data)
129 struct asn1_der_iterator i;
130 enum asn1_iterator_result res;
132 res = asn1_der_iterator_first(&i, length, data);
134 if (res != ASN1_ITERATOR_CONSTRUCTED)
138 return rsa_private_key_from_der_iterator(pub, priv, limit, &i);
140 return rsa_public_key_from_der_iterator(pub, limit, &i);