nettle  3.7.3
About: Nettle is a low-level cryptographic library.
  Fossies Dox: nettle-3.7.3.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

der2rsa.c
Go to the documentation of this file.
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
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
62  && asn1_der_get_bignum(i, pub->n, limit)
63  && mpz_sgn(pub->n) > 0
64  && GET(i, pub->e, limit)
66  && rsa_public_key_prepare(pub));
67 }
68 
69 int
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 
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 */
113  && i->type == ASN1_SEQUENCE))
114  return 0;
115  }
116 
118  }
119 
120  return 0;
121 }
122 
123 int
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 
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 }
#define asn1_der_iterator_first
Definition: asn1.h:44
#define asn1_der_decode_constructed_last
Definition: asn1.h:47
#define asn1_der_get_uint32
Definition: asn1.h:50
@ ASN1_SEQUENCE
Definition: asn1.h:83
@ ASN1_INTEGER
Definition: asn1.h:75
#define asn1_der_iterator_next
Definition: asn1.h:45
asn1_iterator_result
Definition: asn1.h:94
@ ASN1_ITERATOR_END
Definition: asn1.h:98
@ ASN1_ITERATOR_PRIMITIVE
Definition: asn1.h:96
@ ASN1_ITERATOR_CONSTRUCTED
Definition: asn1.h:97
#define asn1_der_get_bignum
Definition: asn1.h:51
#define GET(i, x, l)
Definition: der2rsa.c:43
#define i
int mpz_sgn(const mpz_t u)
Definition: mini-gmp.c:1847
#define rsa_private_key_prepare
Definition: rsa.h:54
#define rsa_private_key_from_der_iterator
Definition: rsa.h:99
#define rsa_keypair_from_der
Definition: rsa.h:100
#define rsa_public_key_prepare
Definition: rsa.h:51
#define rsa_public_key_from_der_iterator
Definition: rsa.h:98
size_t length
Definition: asn1.h:113
const uint8_t * data
Definition: asn1.h:114
mpz_t p
Definition: rsa.h:136
mpz_t d
Definition: rsa.h:133
mpz_t c
Definition: rsa.h:145
mpz_t a
Definition: rsa.h:139
mpz_t q
Definition: rsa.h:136
mpz_t b
Definition: rsa.h:142
mpz_t e
Definition: rsa.h:124
mpz_t n
Definition: rsa.h:121