3 Parsing of ASN.1 DER encoded objects.
5 Copyright (C) 2005 Niels Möller
7 This file is part of GNU Nettle.
9 GNU Nettle is free software: you can redistribute it and/or
10 modify it under the terms of either:
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.
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.
22 or both in parallel, as here.
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.
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/.
47 /* Basic DER syntax: (reference: A Layman's Guide to a Subset of ASN.1, BER, and DER,
48 http://luca.ntop.org/Teaching/Appunti/asn1.html)
50 The DER header contains a tag and a length. First, the tag. cls is
51 the class number, c is one if the object is "constructed" and zero
52 if it is primitive. The tag is represented either using a single
57 |_cls_|_c_|_______tag_| 0 <= tag <= 30
63 |_cls_|_c_|_1_1_1_1_1_|
65 followed by the real tag number, in base 128, with all but the
66 final byte having the most significant bit set. The tag must be
67 represented with as few bytes as possible. High tag numbers are
68 currently *not* supported.
70 Next, the length, either a single byte with the most significant bit clear, or
76 followed by k additional bytes that give the length, in network
77 byte order. The length must be encoded using as few bytes as
78 possible, and k = 0 is reserved for the "indefinite length form"
79 which is not supported.
81 After the length comes the contets. For primitive objects (c == 0),
82 it's depends on the type. For constructed objects, it's a
83 concatenation of the DER encodings of zero or more other objects.
89 CONSTRUCTED_MASK = 0x20,
92 /* Initializes the iterator, but one has to call next to get to the
95 asn1_der_iterator_init(struct asn1_der_iterator *iterator,
96 size_t length, const uint8_t *input)
98 iterator->buffer_length = length;
99 iterator->buffer = input;
102 iterator->length = 0;
103 iterator->data = NULL;
106 #define LEFT(i) ((i)->buffer_length - (i)->pos)
107 #define NEXT(i) ((i)->buffer[(i)->pos++])
109 /* Gets type and length of the next object. */
110 enum asn1_iterator_result
111 asn1_der_iterator_next(struct asn1_der_iterator *i)
116 return ASN1_ITERATOR_END;
120 return ASN1_ITERATOR_ERROR;
122 if ( (tag & TAG_MASK) == TAG_MASK)
124 /* FIXME: Long tags not supported */
125 return ASN1_ITERATOR_ERROR;
129 if (i->length & 0x80)
131 unsigned k = i->length & 0x7f;
133 const uint8_t *data = i->buffer + i->pos;
136 /* Indefinite encoding. Not supported. */
137 return ASN1_ITERATOR_ERROR;
140 return ASN1_ITERATOR_ERROR;
142 if (k > sizeof(i->length))
143 return ASN1_ITERATOR_ERROR;
148 || (k == 1 && i->length < 0x80))
149 return ASN1_ITERATOR_ERROR;
151 for (j = 1; j < k; j++)
152 i->length = (i->length << 8) | data[j];
154 if (LEFT(i) < i->length)
155 return ASN1_ITERATOR_ERROR;
157 i->data = i->buffer + i->pos;
160 i->type = tag & TAG_MASK;
161 i->type |= (tag & CLASS_MASK) << (ASN1_CLASS_SHIFT - 6);
162 if (tag & CONSTRUCTED_MASK)
164 i->type |= ASN1_TYPE_CONSTRUCTED;
165 return ASN1_ITERATOR_CONSTRUCTED;
168 return ASN1_ITERATOR_PRIMITIVE;
171 enum asn1_iterator_result
172 asn1_der_iterator_first(struct asn1_der_iterator *i,
173 size_t length, const uint8_t *input)
175 asn1_der_iterator_init(i, length, input);
176 return asn1_der_iterator_next(i);
179 enum asn1_iterator_result
180 asn1_der_decode_constructed(struct asn1_der_iterator *i,
181 struct asn1_der_iterator *contents)
183 assert(i->type & ASN1_TYPE_CONSTRUCTED);
184 return asn1_der_iterator_first(contents, i->length, i->data);
187 enum asn1_iterator_result
188 asn1_der_decode_constructed_last(struct asn1_der_iterator *i)
191 return ASN1_ITERATOR_ERROR;
193 return asn1_der_decode_constructed(i, i);
196 /* Decoding a DER object which is wrapped in a bit string. */
197 enum asn1_iterator_result
198 asn1_der_decode_bitstring(struct asn1_der_iterator *i,
199 struct asn1_der_iterator *contents)
201 assert(i->type == ASN1_BITSTRING);
202 /* First byte is the number of padding bits, which must be zero. */
203 if (i->length == 0 || i->data[0] != 0)
204 return ASN1_ITERATOR_ERROR;
206 return asn1_der_iterator_first(contents, i->length - 1, i->data + 1);
209 enum asn1_iterator_result
210 asn1_der_decode_bitstring_last(struct asn1_der_iterator *i)
213 return ASN1_ITERATOR_ERROR;
215 return asn1_der_decode_bitstring(i, i);
219 asn1_der_get_uint32(struct asn1_der_iterator *i,
222 /* Big endian, two's complement, minimum number of octets (except 0,
223 which is encoded as a single octet */
225 size_t length = i->length;
228 if (!length || length > 5)
231 if (i->data[length - 1] >= 0x80)
236 && i->data[length -1] == 0
237 && i->data[length -2] < 0x80)
238 /* Non-minimal number of digits */
248 for (value = k = 0; k < length; k++)
249 value = (value << 8) | i->data[k];
255 /* NOTE: This is the only function in this file which needs bignums.
256 One could split this file in two, one in libnettle and one in
259 asn1_der_get_bignum(struct asn1_der_iterator *i,
260 mpz_t x, unsigned max_bits)
263 && ((i->data[0] == 0 && i->data[1] < 0x80)
264 || (i->data[0] == 0xff && i->data[1] >= 0x80)))
265 /* Non-minimal number of digits */
268 /* Allow some extra here, for leading sign octets. */
269 if (max_bits && (8 * i->length > (16 + max_bits)))
272 nettle_mpz_set_str_256_s(x, i->length, i->data);
274 /* FIXME: How to interpret a max_bits for negative numbers? */
275 if (max_bits && mpz_sizeinbase(x, 2) > max_bits)