3 Limited support for ASN.1 DER decoding.
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/.
34 #ifndef NETTLE_ASN1_H_INCLUDED
35 #define NETTLE_ASN1_H_INCLUDED
37 #include "nettle-types.h"
44 #define asn1_der_iterator_first nettle_asn1_der_iterator_first
45 #define asn1_der_iterator_next nettle_asn1_der_iterator_next
46 #define asn1_der_decode_constructed nettle_asn1_der_decode_constructed
47 #define asn1_der_decode_constructed_last nettle_asn1_der_decode_constructed_last
48 #define asn1_der_decode_bitstring nettle_asn1_der_decode_bitstring
49 #define asn1_der_decode_bitstring_last nettle_asn1_der_decode_bitstring_last
50 #define asn1_der_get_uint32 nettle_asn1_der_get_uint32
51 #define asn1_der_get_bignum nettle_asn1_der_get_bignum
54 /* enum asn1_type keeps the class number and the constructive in bits
55 13-14, and the constructive flag in bit 12. The remaining 14 bits
56 are the tag (although currently, only tags in the range 0-30 are
61 ASN1_TYPE_CONSTRUCTED = 1 << 12,
63 ASN1_CLASS_UNIVERSAL = 0,
64 ASN1_CLASS_APPLICATION = 1 << 13,
65 ASN1_CLASS_CONTEXT_SPECIFIC = 2 << 13,
66 ASN1_CLASS_PRIVATE = 3 << 13,
68 ASN1_CLASS_MASK = 3 << 13,
69 ASN1_CLASS_SHIFT = 13,
83 ASN1_SEQUENCE = 16 | ASN1_TYPE_CONSTRUCTED,
84 ASN1_SET = 17 | ASN1_TYPE_CONSTRUCTED,
85 ASN1_PRINTABLESTRING = 19,
86 ASN1_TELETEXSTRING = 20,
89 ASN1_UNIVERSALSTRING = 28,
93 enum asn1_iterator_result
96 ASN1_ITERATOR_PRIMITIVE,
97 ASN1_ITERATOR_CONSTRUCTED,
101 /* Parsing DER objects. */
102 struct asn1_der_iterator
104 size_t buffer_length;
105 const uint8_t *buffer;
107 /* Next object to parse. */
112 /* Pointer to the current object */
117 /* Initializes the iterator. */
118 enum asn1_iterator_result
119 asn1_der_iterator_first(struct asn1_der_iterator *iterator,
120 size_t length, const uint8_t *input);
122 enum asn1_iterator_result
123 asn1_der_iterator_next(struct asn1_der_iterator *iterator);
125 /* Starts parsing of a constructed object. */
126 enum asn1_iterator_result
127 asn1_der_decode_constructed(struct asn1_der_iterator *i,
128 struct asn1_der_iterator *contents);
130 /* For the common case that we have a sequence at the end of the
131 object. Checks that the current object is the final one, and then
132 reinitializes the iterator to parse its ontents. */
133 enum asn1_iterator_result
134 asn1_der_decode_constructed_last(struct asn1_der_iterator *i);
136 enum asn1_iterator_result
137 asn1_der_decode_bitstring(struct asn1_der_iterator *i,
138 struct asn1_der_iterator *contents);
140 enum asn1_iterator_result
141 asn1_der_decode_bitstring_last(struct asn1_der_iterator *i);
143 /* All these functions return 1 on success, 0 on failure */
145 asn1_der_get_uint32(struct asn1_der_iterator *i,
152 #endif /* NETTLE_ASN1_H_INCLUDED */