3 * Decoding of keys in PKCS#1 format.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2005 Niels Möller
10 * The nettle library is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or (at your
13 * option) any later version.
15 * The nettle library is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18 * License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with the nettle library; see the file COPYING.LIB. If not, write to
22 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
35 #define GET(i, x, l) \
36 (asn1_der_iterator_next((i)) == ASN1_ITERATOR_PRIMITIVE \
37 && (i)->type == ASN1_INTEGER \
38 && asn1_der_get_bignum((i), (x), (l)) \
42 rsa_public_key_from_der_iterator(struct rsa_public_key *pub,
44 struct asn1_der_iterator *i)
46 /* RSAPublicKey ::= SEQUENCE {
48 publicExponent INTEGER -- e
52 return (i->type == ASN1_SEQUENCE
53 && asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
54 && asn1_der_get_bignum(i, pub->n, limit)
55 && mpz_sgn(pub->n) > 0
56 && GET(i, pub->e, limit)
57 && asn1_der_iterator_next(i) == ASN1_ITERATOR_END
58 && rsa_public_key_prepare(pub));
62 rsa_private_key_from_der_iterator(struct rsa_public_key *pub,
63 struct rsa_private_key *priv,
65 struct asn1_der_iterator *i)
67 /* RSAPrivateKey ::= SEQUENCE {
70 publicExponent INTEGER, -- e
71 privateExponent INTEGER, -- d
74 exponent1 INTEGER, -- d mod (p-1)
75 exponent2 INTEGER, -- d mod (q-1)
76 coefficient INTEGER, -- (inverse of q) mod p
77 otherPrimeInfos OtherPrimeInfos OPTIONAL
83 if (i->type != ASN1_SEQUENCE)
86 if (asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
87 && i->type == ASN1_INTEGER
88 && asn1_der_get_uint32(i, &version)
90 && GET(i, pub->n, limit)
91 && GET(i, pub->e, limit)
92 && rsa_public_key_prepare(pub)
93 && GET(i, priv->d, limit)
94 && GET(i, priv->p, limit)
95 && GET(i, priv->q, limit)
96 && GET(i, priv->a, limit)
97 && GET(i, priv->b, limit)
98 && GET(i, priv->c, limit)
99 && rsa_private_key_prepare(priv))
103 /* otherPrimeInfos must be present. We ignore the contents */
104 if (!(asn1_der_iterator_next(i) == ASN1_ITERATOR_CONSTRUCTED
105 && i->type == ASN1_SEQUENCE))
109 return (asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
116 rsa_keypair_from_der(struct rsa_public_key *pub,
117 struct rsa_private_key *priv,
119 unsigned length, const uint8_t *data)
121 struct asn1_der_iterator i;
122 enum asn1_iterator_result res;
124 res = asn1_der_iterator_first(&i, length, data);
126 if (res != ASN1_ITERATOR_CONSTRUCTED)
130 return rsa_private_key_from_der_iterator(pub, priv, limit, &i);
132 return rsa_public_key_from_der_iterator(pub, limit, &i);