3 * Parses DER encoded objects.
6 /* nettle, low-level cryptographics library
8 * Copyright (C) 2005 Niels Möller
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,
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
51 |_cls_|_c_|_______tag_| 0 <= tag <= 30
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
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.
83 CONSTRUCTED_MASK
= 0x20,
86 /* Initializes the iterator, but one has to call next to get to the
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
;
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
)
110 return ASN1_ITERATOR_END
;
114 return ASN1_ITERATOR_ERROR
;
116 if ( (tag
& TAG_MASK
) == TAG_MASK
)
118 /* FIXME: Long tags not supported */
119 return ASN1_ITERATOR_ERROR
;
123 if (i
->length
& 0x80)
125 unsigned k
= i
->length
& 0x7f;
127 const uint8_t *data
= i
->buffer
+ i
->pos
;
130 /* Indefinite encoding. Not supported. */
131 return ASN1_ITERATOR_ERROR
;
134 return ASN1_ITERATOR_ERROR
;
136 if (k
> sizeof(unsigned))
137 return ASN1_ITERATOR_ERROR
;
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
;
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
;
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
)
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
)
207 return ASN1_ITERATOR_ERROR
;
209 return asn1_der_decode_bitstring(i
, i
);
213 asn1_der_get_uint32(struct asn1_der_iterator
*i
,
216 /* Big endian, two's complement, minimum number of octets (except 0,
217 which is encoded as a single octet */
219 unsigned length
= i
->length
;
222 if (!length
|| length
> 5)
225 if (i
->data
[length
- 1] >= 0x80)
230 && i
->data
[length
-1] == 0
231 && i
->data
[length
-2] < 0x80)
232 /* Non-minimal number of digits */
242 for (value
= k
= 0; k
< length
; k
++)
243 value
= (value
<< 8) | i
->data
[k
];
251 asn1_der_get_bignum(struct asn1_der_iterator
*i
,
252 mpz_t x
, unsigned max_bits
)
255 && ((i
->data
[0] == 0 && i
->data
[1] < 0x80)
256 || (i
->data
[0] == 0xff && i
->data
[1] >= 0x80)))
257 /* Non-minimal number of digits */
260 /* Allow some extra here, for leading sign octets. */
261 if (max_bits
&& (8 * i
->length
> (16 + max_bits
)))
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
)
272 #endif /* HAVE_LIBGMP */