Revert "Merge branch 'upstream' into tizen"
[platform/upstream/nettle.git] / der2rsa.c
1 /* der2rsa.c
2  *
3  * Decoding of keys in PKCS#1 format.
4  */
5
6 /* nettle, low-level cryptographics library
7  *
8  * Copyright (C) 2005 Niels Möller
9  *  
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.
14  * 
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.
19  * 
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., 51 Franklin Street, Fifth Floor, Boston,
23  * MA 02111-1301, USA.
24  */
25
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include "rsa.h"
31
32 #include "bignum.h"
33 #include "asn1.h"
34
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))                  \
39  && mpz_sgn((x)) > 0)
40
41 int
42 rsa_public_key_from_der_iterator(struct rsa_public_key *pub,
43                                  unsigned limit,
44                                  struct asn1_der_iterator *i)
45 {
46   /* RSAPublicKey ::= SEQUENCE {
47          modulus           INTEGER,  -- n
48          publicExponent    INTEGER   -- e
49       }
50   */
51
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));
59 }
60
61 int
62 rsa_private_key_from_der_iterator(struct rsa_public_key *pub,
63                                   struct rsa_private_key *priv,
64                                   unsigned limit,
65                                   struct asn1_der_iterator *i)
66 {
67   /* RSAPrivateKey ::= SEQUENCE {
68          version           Version,
69          modulus           INTEGER,  -- n
70          publicExponent    INTEGER,  -- e
71          privateExponent   INTEGER,  -- d
72          prime1            INTEGER,  -- p
73          prime2            INTEGER,  -- q
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
78     }
79   */
80
81   uint32_t version;
82   
83   if (i->type != ASN1_SEQUENCE)
84     return 0;
85
86   if (asn1_der_decode_constructed_last(i) == ASN1_ITERATOR_PRIMITIVE
87       && i->type == ASN1_INTEGER
88       && asn1_der_get_uint32(i, &version)
89       && version <= 1
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))
100     {
101       if (version == 1)
102         {
103           /* otherPrimeInfos must be present. We ignore the contents */
104           if (!(asn1_der_iterator_next(i) == ASN1_ITERATOR_CONSTRUCTED
105                 && i->type == ASN1_SEQUENCE))
106             return 0;
107         }
108
109       return (asn1_der_iterator_next(i) == ASN1_ITERATOR_END);
110     }
111   
112   return 0;
113 }
114
115 int
116 rsa_keypair_from_der(struct rsa_public_key *pub,
117                      struct rsa_private_key *priv,
118                      unsigned limit, 
119                      unsigned length, const uint8_t *data)
120 {
121   struct asn1_der_iterator i;
122   enum asn1_iterator_result res;
123
124   res = asn1_der_iterator_first(&i, length, data);
125
126   if (res != ASN1_ITERATOR_CONSTRUCTED)
127     return 0;
128
129   if (priv)
130     return rsa_private_key_from_der_iterator(pub, priv, limit, &i);
131   else
132     return rsa_public_key_from_der_iterator(pub, limit, &i);    
133 }