Update copyright headers for dual licensing.
[gd/nettle] / asn1.h
1 /* asn1.h
2
3    Limited support for ASN.1 DER decoding.
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 #ifndef NETTLE_ASN1_H_INCLUDED
35 #define NETTLE_ASN1_H_INCLUDED
36
37 #include "nettle-types.h"
38
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42
43 /* Name mangling */
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
52
53
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
57    supported). */
58
59 enum
60   {
61     ASN1_TYPE_CONSTRUCTED = 1 << 12,
62
63     ASN1_CLASS_UNIVERSAL = 0,
64     ASN1_CLASS_APPLICATION = 1 << 13,
65     ASN1_CLASS_CONTEXT_SPECIFIC = 2 << 13,
66     ASN1_CLASS_PRIVATE = 3 << 13,
67
68     ASN1_CLASS_MASK = 3 << 13,
69     ASN1_CLASS_SHIFT = 13,
70   };
71
72 enum asn1_type
73   {
74     ASN1_BOOLEAN = 1,
75     ASN1_INTEGER = 2,
76     ASN1_BITSTRING = 3,
77     ASN1_OCTETSTRING = 4,
78     ASN1_NULL = 5,
79     ASN1_IDENTIFIER = 6,
80     ASN1_REAL = 9,
81     ASN1_ENUMERATED = 10,
82     ASN1_UTF8STRING = 12,
83     ASN1_SEQUENCE = 16 | ASN1_TYPE_CONSTRUCTED,
84     ASN1_SET = 17 | ASN1_TYPE_CONSTRUCTED,
85     ASN1_PRINTABLESTRING = 19,
86     ASN1_TELETEXSTRING = 20,
87     ASN1_IA5STRING = 22,
88     ASN1_UTC = 23,
89     ASN1_UNIVERSALSTRING = 28,
90     ASN1_BMPSTRING = 30,
91   };
92
93 enum asn1_iterator_result
94   {
95     ASN1_ITERATOR_ERROR,
96     ASN1_ITERATOR_PRIMITIVE,
97     ASN1_ITERATOR_CONSTRUCTED,
98     ASN1_ITERATOR_END,
99   };
100
101 /* Parsing DER objects. */
102 struct asn1_der_iterator
103 {
104   size_t buffer_length;
105   const uint8_t *buffer;
106
107   /* Next object to parse. */
108   size_t pos;
109
110   enum asn1_type type;
111
112   /* Pointer to the current object */
113   size_t length;
114   const uint8_t *data;
115 };
116
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);
121
122 enum asn1_iterator_result
123 asn1_der_iterator_next(struct asn1_der_iterator *iterator);
124
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);
129
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);
135
136 enum asn1_iterator_result
137 asn1_der_decode_bitstring(struct asn1_der_iterator *i,
138                           struct asn1_der_iterator *contents);
139
140 enum asn1_iterator_result
141 asn1_der_decode_bitstring_last(struct asn1_der_iterator *i);
142
143 /* All these functions return 1 on success, 0 on failure */
144 int
145 asn1_der_get_uint32(struct asn1_der_iterator *i,
146                     uint32_t *x);
147
148 #ifdef __cplusplus
149 }
150 #endif
151
152 #endif /* NETTLE_ASN1_H_INCLUDED */