Use standard unsigned integer types
[strongswan.git] / src / libstrongswan / asn1 / asn1.h
1 /*
2 * Copyright (C) 2006 Martin Will
3 * Copyright (C) 2000-2008 Andreas Steffen
4 *
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 /**
19 * @defgroup asn1i asn1
20 * @{ @ingroup asn1
21 */
22
23 #ifndef ASN1_H_
24 #define ASN1_H_
25
26 #include <stdarg.h>
27
28 #include <library.h>
29 #include <asn1/asn1.h>
30
31 /**
32 * Definition of some primitive ASN1 types
33 */
34 typedef enum {
35 ASN1_EOC = 0x00,
36 ASN1_BOOLEAN = 0x01,
37 ASN1_INTEGER = 0x02,
38 ASN1_BIT_STRING = 0x03,
39 ASN1_OCTET_STRING = 0x04,
40 ASN1_NULL = 0x05,
41 ASN1_OID = 0x06,
42 ASN1_ENUMERATED = 0x0A,
43 ASN1_UTF8STRING = 0x0C,
44 ASN1_NUMERICSTRING = 0x12,
45 ASN1_PRINTABLESTRING = 0x13,
46 ASN1_T61STRING = 0x14,
47 ASN1_VIDEOTEXSTRING = 0x15,
48 ASN1_IA5STRING = 0x16,
49 ASN1_UTCTIME = 0x17,
50 ASN1_GENERALIZEDTIME = 0x18,
51 ASN1_GRAPHICSTRING = 0x19,
52 ASN1_VISIBLESTRING = 0x1A,
53 ASN1_GENERALSTRING = 0x1B,
54 ASN1_UNIVERSALSTRING = 0x1C,
55 ASN1_BMPSTRING = 0x1E,
56
57 ASN1_CONSTRUCTED = 0x20,
58
59 ASN1_SEQUENCE = 0x30,
60 ASN1_SET = 0x31,
61
62 ASN1_CONTEXT_S_0 = 0x80,
63 ASN1_CONTEXT_S_1 = 0x81,
64 ASN1_CONTEXT_S_2 = 0x82,
65 ASN1_CONTEXT_S_3 = 0x83,
66 ASN1_CONTEXT_S_4 = 0x84,
67 ASN1_CONTEXT_S_5 = 0x85,
68 ASN1_CONTEXT_S_6 = 0x86,
69 ASN1_CONTEXT_S_7 = 0x87,
70 ASN1_CONTEXT_S_8 = 0x88,
71
72 ASN1_CONTEXT_C_0 = 0xA0,
73 ASN1_CONTEXT_C_1 = 0xA1,
74 ASN1_CONTEXT_C_2 = 0xA2,
75 ASN1_CONTEXT_C_3 = 0xA3,
76 ASN1_CONTEXT_C_4 = 0xA4,
77 ASN1_CONTEXT_C_5 = 0xA5,
78
79 ASN1_INVALID = 0x100,
80 } asn1_t;
81
82 #define ASN1_INVALID_LENGTH 0xffffffff
83
84 /**
85 * Some common prefabricated ASN.1 constants
86 */
87 extern const chunk_t ASN1_INTEGER_0;
88 extern const chunk_t ASN1_INTEGER_1;
89 extern const chunk_t ASN1_INTEGER_2;
90
91
92 /** Some ASN.1 analysis functions */
93
94 /**
95 * Build an algorithmIdentifier from a known OID.
96 *
97 * @param oid known OID index
98 * @return body of the corresponding OID, allocated
99 */
100 chunk_t asn1_algorithmIdentifier(int oid);
101
102 /**
103 * Converts an ASN.1 OID into a known OID index
104 *
105 * @param object body of an OID
106 * @return index into the oid_names[] table or OID_UNKNOWN
107 */
108 int asn1_known_oid(chunk_t object);
109
110 /**
111 * Converts a known OID index to an ASN.1 OID
112 *
113 * @param n index into the oid_names[] table
114 * @return allocated OID chunk, chunk_empty if index out of range
115 */
116 chunk_t asn1_build_known_oid(int n);
117
118 /**
119 * Convert human readable OID to ASN.1 DER encoding, without OID header.
120 *
121 * @param str OID string (e.g. 1.2.345.67.8)
122 * @return allocated ASN.1 encoded OID, chunk_empty on error
123 */
124 chunk_t asn1_oid_from_string(char *str);
125
126 /**
127 * Convert a DER encoded ASN.1 OID to a human readable string.
128 *
129 * @param oid DER encoded OID, without header
130 * @return human readable OID string, allocated, NULL on error
131 */
132 char* asn1_oid_to_string(chunk_t oid);
133
134 /**
135 * Returns the length of an ASN.1 object
136 * The blob pointer is advanced past the tag length fields
137 *
138 * @param blob pointer to an ASN.1 coded blob
139 * @return length of ASN.1 object
140 */
141 size_t asn1_length(chunk_t *blob);
142
143 /**
144 * Unwrap the inner content of an ASN.1 type/length wrapped object.
145 *
146 * @param blob blob to parse header from, moved behind parsed content
147 * @param content inner content
148 * @return parsed type, ASN1_INVALID if length parsing failed
149 */
150 int asn1_unwrap(chunk_t *blob, chunk_t *content);
151
152 /**
153 * Parses an ASN.1 algorithmIdentifier object
154 *
155 * @param blob ASN.1 coded blob
156 * @param level0 top-most level offset
157 * @param params returns optional [ASN.1 coded] parameters
158 * @return known OID index or OID_UNKNOWN
159 */
160 int asn1_parse_algorithmIdentifier(chunk_t blob, int level0, chunk_t *params);
161
162 /**
163 * Parse the top-most level of an ASN.1 object
164 *
165 * @param object ASN.1 coded object
166 * @param type Expected ASN.1 type
167 * @param level0 top-most level offset
168 * @param name descriptive name of object
169 * @return TRUE if parsing successful
170 */
171 bool asn1_parse_simple_object(chunk_t *object, asn1_t type, u_int level0,
172 const char* name);
173
174 /**
175 * Converts an ASN.1 INTEGER object to an uint64_t. If the INTEGER is longer
176 * than 8 bytes only the 8 LSBs are returned.
177 *
178 * @param blob body of an ASN.1 coded integer object
179 * @return converted integer
180 */
181 uint64_t asn1_parse_integer_uint64(chunk_t blob);
182
183 /**
184 * Print the value of an ASN.1 simple object
185 *
186 * @param object ASN.1 object to be printed
187 * @param type asn1_t type
188 * @param private ASN.1 data is confidential (use debug level 4)
189 */
190 void asn1_debug_simple_object(chunk_t object, asn1_t type, bool private);
191
192 /**
193 * Converts an ASN.1 UTCTIME or GENERALIZEDTIME string to time_t
194 *
195 * On systems where sizeof(time_t) == 4 there will be an overflow
196 * for dates
197 * > Tue, 19 Jan 2038 03:14:07 UTC (0x7fffffff)
198 * and
199 * < Fri, 13 Dec 1901 20:45:52 UTC (0x80000000)
200 * in both cases TIME_32_BIT_SIGNED_MAX is returned.
201 *
202 * @param utctime body of an ASN.1 coded time object
203 * @param type ASN1_UTCTIME or ASN1_GENERALIZEDTIME
204 * @return time_t in UTC
205 */
206 time_t asn1_to_time(const chunk_t *utctime, asn1_t type);
207
208 /**
209 * Converts time_t to an ASN.1 UTCTIME or GENERALIZEDTIME string
210 *
211 * @note The type is automatically changed to GENERALIZEDTIME if needed
212 *
213 * @param time time_t in UTC
214 * @param type ASN1_UTCTIME or ASN1_GENERALIZEDTIME
215 * @return body of an ASN.1 code time object
216 */
217 chunk_t asn1_from_time(const time_t *time, asn1_t type);
218
219 /**
220 * Parse an ASN.1 UTCTIME or GENERALIZEDTIME object
221 *
222 * @param blob ASN.1 coded time object
223 * @param level0 top-most level offset
224 * @return time_t in UTC
225 */
226 time_t asn1_parse_time(chunk_t blob, int level0);
227
228 /**
229 * Determines if a binary blob is ASN.1 coded
230 *
231 * @param blob blob to be tested
232 * @return TRUE if blob is ASN.1 coded (SEQUENCE or SET)
233 */
234 bool is_asn1(chunk_t blob);
235
236 /**
237 * Determines if a character string can be coded as PRINTABLESTRING
238 *
239 * @param str character string to be tested
240 * @return TRUE if no special characters are contained
241 */
242 bool asn1_is_printablestring(chunk_t str);
243
244
245 /** some ASN.1 synthesis functions */
246
247 /**
248 * Build an empty ASN.1 object with tag and length fields already filled in
249 *
250 * @param object returned object - memory is allocated by function
251 * @param type ASN.1 type to be created
252 * @param datalen size of the body to be created
253 * @return points to the first position in the body
254 */
255 u_char* asn1_build_object(chunk_t *object, asn1_t type, size_t datalen);
256
257 /**
258 * Build a simple ASN.1 object
259 *
260 * @param tag ASN.1 type to be created
261 * @param content content of the ASN.1 object
262 * @return chunk containing the ASN.1 coded object
263 */
264 chunk_t asn1_simple_object(asn1_t tag, chunk_t content);
265
266 /**
267 * Build an ASN.1 BITSTRING object
268 *
269 * @param mode 'c' for copy or 'm' for move
270 * @param content content of the BITSTRING
271 * @return chunk containing the ASN.1 coded BITSTRING
272 */
273 chunk_t asn1_bitstring(const char *mode, chunk_t content);
274
275 /**
276 * Build an ASN.1 INTEGER object
277 *
278 * @param mode 'c' for copy or 'm' for move
279 * @param content content of the INTEGER
280 * @return chunk containing the ASN.1 coded INTEGER
281 */
282 chunk_t asn1_integer(const char *mode, chunk_t content);
283
284 /**
285 * Build an ASN.1 object from a variable number of individual chunks
286 *
287 * The mode string specifies the number of chunks, and how to handle each of
288 * them with a single character: 'c' for copy (allocate new chunk), 'm' for move
289 * (free given chunk) or 's' for sensitive-copy (clear given chunk, then free).
290 *
291 * @param type ASN.1 type to be created
292 * @param mode for each list member: 'c', 'm' or 's'
293 * @return chunk containing the ASN.1 coded object
294 */
295 chunk_t asn1_wrap(asn1_t type, const char *mode, ...);
296
297 #endif /** ASN1_H_ @}*/