testutils.c: Fix high bits of the mpz_urandomb used with mini-gmp.
[gd/nettle] / der2rsa.c
1 /* der2rsa.c
2
3    Decoding of keys in PKCS#1 format.
4
5    Copyright (C) 2005 Niels Möller
6
7    This file is part of GNU Nettle.
8
9    GNU Nettle is free software: you can redistribute it and/or
10    modify it under the terms of either:
11
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.
15
16    or
17
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.
21
22    or both in parallel, as here.
23
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.
28
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/.
32 */
33
34 #if HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include "rsa.h"
39
40 #include "bignum.h"
41 #include "asn1.h"
42
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))                  \
47  && mpz_sgn((x)) > 0)
48
49 int
50 rsa_public_key_from_der_iterator(struct rsa_public_key *pub,
51                                  unsigned limit,
52                                  struct asn1_der_iterator *i)
53 {
54   /* RSAPublicKey ::= SEQUENCE {
55          modulus           INTEGER,  -- n
56          publicExponent    INTEGER   -- e
57       }
58   */
59
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));
67 }
68
69 int
70 rsa_private_key_from_der_iterator(struct rsa_public_key *pub,
71                                   struct rsa_private_key *priv,
72                                   unsigned limit,
73                                   struct asn1_der_iterator *i)
74 {
75   /* RSAPrivateKey ::= SEQUENCE {
76          version           Version,
77          modulus           INTEGER,  -- n
78          publicExponent    INTEGER,  -- e
79          privateExponent   INTEGER,  -- d
80          prime1            INTEGER,  -- p
81          prime2            INTEGER,  -- q
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
86     }
87   */
88
89   uint32_t version;
90   
91   if (i->type != ASN1_SEQUENCE)
92     return 0;
93
94   if (asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
95       && i->type == ASN1_INTEGER
96       && asn1_der_get_uint32(i, &version)
97       && version <= 1
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))
108     {
109       if (version == 1)
110         {
111           /* otherPrimeInfos must be present. We ignore the contents */
112           if (!(asn1_der_iterator_next(i) == ASN1_ITERATOR_CONSTRUCTED
113                 && i->type == ASN1_SEQUENCE))
114             return 0;
115         }
116
117       return (asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
118     }
119   
120   return 0;
121 }
122
123 int
124 rsa_keypair_from_der(struct rsa_public_key *pub,
125                      struct rsa_private_key *priv,
126                      unsigned limit, 
127                      size_t length, const uint8_t *data)
128 {
129   struct asn1_der_iterator i;
130   enum asn1_iterator_result res;
131
132   res = asn1_der_iterator_first(&i, length, data);
133
134   if (res != ASN1_ITERATOR_CONSTRUCTED)
135     return 0;
136
137   if (priv)
138     return rsa_private_key_from_der_iterator(pub, priv, limit, &i);
139   else
140     return rsa_public_key_from_der_iterator(pub, limit, &i);    
141 }