Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / nettle / der-iterator.c
blob2e6efd5fac85f0775e6f090f1e542cb5084c2de9
1 /* der-iterator.c
3 * Parses DER encoded objects.
4 */
6 /* nettle, low-level cryptographics library
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.
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., 51 Franklin Street, Fifth Floor, Boston,
23 * MA 02111-1301, USA.
26 #if HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <assert.h>
31 #include <stdlib.h>
33 #if HAVE_LIBGMP
34 #include "bignum.h"
35 #endif
37 #include "asn1.h"
39 #include "macros.h"
41 /* Basic DER syntax: (reference: A Layman's Guide to a Subset of ASN.1, BER, and DER,
42 http://luca.ntop.org/Teaching/Appunti/asn1.html)
44 The DER header contains a tag and a length. First, the tag. cls is
45 the class number, c is one if the object is "constructed" and zero
46 if it is primitive. The tag is represented either using a single
47 byte,
49 7 6 5 4 3 2 1 0
50 _____________________
51 |_cls_|_c_|_______tag_| 0 <= tag <= 30
53 or multiple bytes
55 7 6 5 4 3 2 1 0
56 _____________________
57 |_cls_|_c_|_1_1_1_1_1_|
59 followed by the real tag number, in base 128, with all but the
60 final byte having the most significant bit set. The tag must be
61 represented with as few bytes as possible. High tag numbers are
62 currently *not* supported.
64 Next, the length, either a single byte with the most significant bit clear, or
66 7 6 5 4 3 2 1 0
67 _________________
68 |_1_|___________k_|
70 followed by k additional bytes that give the length, in network
71 byte order. The length must be encoded using as few bytes as
72 possible, and k = 0 is reserved for the "indefinite length form"
73 which is not supported.
75 After the length comes the contets. For primitive objects (c == 0),
76 it's depends on the type. For constructed objects, it's a
77 concatenation of the DER encodings of zero or more other objects.
80 enum {
81 TAG_MASK = 0x1f,
82 CLASS_MASK = 0xc0,
83 CONSTRUCTED_MASK = 0x20,
86 /* Initializes the iterator, but one has to call next to get to the
87 * first element. */
88 static void
89 asn1_der_iterator_init(struct asn1_der_iterator *iterator,
90 unsigned length, const uint8_t *input)
92 iterator->buffer_length = length;
93 iterator->buffer = input;
94 iterator->pos = 0;
95 iterator->type = 0;
96 iterator->length = 0;
97 iterator->data = NULL;
100 #define LEFT(i) ((i)->buffer_length - (i)->pos)
101 #define NEXT(i) ((i)->buffer[(i)->pos++])
103 /* Gets type and length of the next object. */
104 enum asn1_iterator_result
105 asn1_der_iterator_next(struct asn1_der_iterator *i)
107 uint8_t tag;
109 if (!LEFT(i))
110 return ASN1_ITERATOR_END;
112 tag = NEXT(i);
113 if (!LEFT(i))
114 return ASN1_ITERATOR_ERROR;
116 if ( (tag & TAG_MASK) == TAG_MASK)
118 /* FIXME: Long tags not supported */
119 return ASN1_ITERATOR_ERROR;
122 i->length = NEXT(i);
123 if (i->length & 0x80)
125 unsigned k = i->length & 0x7f;
126 unsigned j;
127 const uint8_t *data = i->buffer + i->pos;
129 if (k == 0)
130 /* Indefinite encoding. Not supported. */
131 return ASN1_ITERATOR_ERROR;
133 if (LEFT(i) < k)
134 return ASN1_ITERATOR_ERROR;
136 if (k > sizeof(unsigned))
137 return ASN1_ITERATOR_ERROR;
139 i->pos += k;
140 i->length = data[0];
141 if (i->length == 0
142 || (k == 1 && i->length < 0x80))
143 return ASN1_ITERATOR_ERROR;
145 for (j = 1; j < k; j++)
146 i->length = (i->length << 8) | data[j];
148 if (LEFT(i) < i->length)
149 return ASN1_ITERATOR_ERROR;
151 i->data = i->buffer + i->pos;
152 i->pos += i->length;
154 i->type = tag & TAG_MASK;
155 i->type |= (tag & CLASS_MASK) << (ASN1_CLASS_SHIFT - 6);
156 if (tag & CONSTRUCTED_MASK)
158 i->type |= ASN1_TYPE_CONSTRUCTED;
159 return ASN1_ITERATOR_CONSTRUCTED;
161 else
162 return ASN1_ITERATOR_PRIMITIVE;
165 enum asn1_iterator_result
166 asn1_der_iterator_first(struct asn1_der_iterator *i,
167 unsigned length, const uint8_t *input)
169 asn1_der_iterator_init(i, length, input);
170 return asn1_der_iterator_next(i);
173 enum asn1_iterator_result
174 asn1_der_decode_constructed(struct asn1_der_iterator *i,
175 struct asn1_der_iterator *contents)
177 assert(i->type & ASN1_TYPE_CONSTRUCTED);
178 return asn1_der_iterator_first(contents, i->length, i->data);
181 enum asn1_iterator_result
182 asn1_der_decode_constructed_last(struct asn1_der_iterator *i)
184 if (LEFT(i) > 0)
185 return ASN1_ITERATOR_ERROR;
187 return asn1_der_decode_constructed(i, i);
190 /* Decoding a DER object which is wrapped in a bit string. */
191 enum asn1_iterator_result
192 asn1_der_decode_bitstring(struct asn1_der_iterator *i,
193 struct asn1_der_iterator *contents)
195 assert(i->type == ASN1_BITSTRING);
196 /* First byte is the number of padding bits, which must be zero. */
197 if (i->length == 0 || i->data[0] != 0)
198 return ASN1_ITERATOR_ERROR;
200 return asn1_der_iterator_first(contents, i->length - 1, i->data + 1);
203 enum asn1_iterator_result
204 asn1_der_decode_bitstring_last(struct asn1_der_iterator *i)
206 if (LEFT(i) > 0)
207 return ASN1_ITERATOR_ERROR;
209 return asn1_der_decode_bitstring(i, i);
213 asn1_der_get_uint32(struct asn1_der_iterator *i,
214 uint32_t *x)
216 /* Big endian, two's complement, minimum number of octets (except 0,
217 which is encoded as a single octet */
218 uint32_t value = 0;
219 unsigned length = i->length;
220 unsigned k;
222 if (!length || length > 5)
223 return 0;
225 if (i->data[length - 1] >= 0x80)
226 /* Signed number */
227 return 0;
229 if (length > 1
230 && i->data[length -1] == 0
231 && i->data[length -2] < 0x80)
232 /* Non-minimal number of digits */
233 return 0;
235 if (length == 5)
237 if (i->data[4])
238 return 0;
239 length--;
242 for (value = k = 0; k < length; k++)
243 value = (value << 8) | i->data[k];
245 *x = value;
246 return 1;
249 #if HAVE_LIBGMP
251 asn1_der_get_bignum(struct asn1_der_iterator *i,
252 mpz_t x, unsigned max_bits)
254 if (i->length > 1
255 && ((i->data[0] == 0 && i->data[1] < 0x80)
256 || (i->data[0] == 0xff && i->data[1] >= 0x80)))
257 /* Non-minimal number of digits */
258 return 0;
260 /* Allow some extra here, for leading sign octets. */
261 if (max_bits && (8 * i->length > (16 + max_bits)))
262 return 0;
264 nettle_mpz_set_str_256_s(x, i->length, i->data);
266 /* FIXME: How to interpret a max_bits for negative numbers? */
267 if (max_bits && mpz_sizeinbase(x, 2) > max_bits)
268 return 0;
270 return 1;
272 #endif /* HAVE_LIBGMP */