5c0222347f3ab72252f82156cc4e5d201fe1390e
[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 * $Id$
18 */
19
20 /**
21 * @defgroup asn1i asn1
22 * @{ @ingroup asn1
23 */
24
25 #ifndef ASN1_H_
26 #define ASN1_H_
27
28 #include <stdarg.h>
29
30 #include <library.h>
31
32 /**
33 * Definition of some primitive ASN1 types
34 */
35 typedef enum {
36 ASN1_EOC = 0x00,
37 ASN1_BOOLEAN = 0x01,
38 ASN1_INTEGER = 0x02,
39 ASN1_BIT_STRING = 0x03,
40 ASN1_OCTET_STRING = 0x04,
41 ASN1_NULL = 0x05,
42 ASN1_OID = 0x06,
43 ASN1_ENUMERATED = 0x0A,
44 ASN1_UTF8STRING = 0x0C,
45 ASN1_NUMERICSTRING = 0x12,
46 ASN1_PRINTABLESTRING = 0x13,
47 ASN1_T61STRING = 0x14,
48 ASN1_VIDEOTEXSTRING = 0x15,
49 ASN1_IA5STRING = 0x16,
50 ASN1_UTCTIME = 0x17,
51 ASN1_GENERALIZEDTIME = 0x18,
52 ASN1_GRAPHICSTRING = 0x19,
53 ASN1_VISIBLESTRING = 0x1A,
54 ASN1_GENERALSTRING = 0x1B,
55 ASN1_UNIVERSALSTRING = 0x1C,
56 ASN1_BMPSTRING = 0x1E,
57
58 ASN1_CONSTRUCTED = 0x20,
59
60 ASN1_SEQUENCE = 0x30,
61 ASN1_SET = 0x31,
62
63 ASN1_CONTEXT_S_0 = 0x80,
64 ASN1_CONTEXT_S_1 = 0x81,
65 ASN1_CONTEXT_S_2 = 0x82,
66 ASN1_CONTEXT_S_3 = 0x83,
67 ASN1_CONTEXT_S_4 = 0x84,
68 ASN1_CONTEXT_S_5 = 0x85,
69 ASN1_CONTEXT_S_6 = 0x86,
70 ASN1_CONTEXT_S_7 = 0x87,
71 ASN1_CONTEXT_S_8 = 0x88,
72
73 ASN1_CONTEXT_C_0 = 0xA0,
74 ASN1_CONTEXT_C_1 = 0xA1,
75 ASN1_CONTEXT_C_2 = 0xA2,
76 ASN1_CONTEXT_C_3 = 0xA3,
77 ASN1_CONTEXT_C_4 = 0xA4,
78 ASN1_CONTEXT_C_5 = 0xA5
79 } asn1_t;
80
81 #define ASN1_INVALID_LENGTH 0xffffffff
82
83 /**
84 * Some common prefabricated ASN.1 constants
85 */
86 extern const chunk_t ASN1_INTEGER_0;
87 extern const chunk_t ASN1_INTEGER_1;
88 extern const chunk_t ASN1_INTEGER_2;
89
90
91 /** Some ASN.1 analysis functions */
92
93 /**
94 * Returns some popular algorithmIdentifiers
95 *
96 * @param oid known OID index
97 * @return body of the corresponding OID
98 */
99 chunk_t asn1_algorithmIdentifier(int oid);
100
101 /**
102 * Converts an ASN.1 OID into a known OID index
103 *
104 * @param object body of an OID
105 * @return index into the oid_names[] table or OID_UNKNOWN
106 */
107 int asn1_known_oid(chunk_t object);
108
109 /**
110 * Returns the length of an ASN.1 object
111 * The blob pointer is advanced past the tag length fields
112 *
113 * @param pointer to an ASN.1 coded blob
114 * @return length of ASN.1 object
115 */
116 u_int asn1_length(chunk_t *blob);
117
118 /**
119 * Parses an ASN.1 algorithmIdentifier object
120 *
121 * @param blob ASN.1 coded blob
122 * @param level0 top-most level offset
123 * @param params returns optional [ASN.1 coded] parameters
124 * @return known OID index or OID_UNKNOWN
125 */
126 int asn1_parse_algorithmIdentifier(chunk_t blob, int level0, chunk_t *params);
127
128 /**
129 * Parse the top-most level of an ASN.1 object
130 *
131 * @param object ASN.1 coded object
132 * @param type Expected ASN.1 type
133 * @param level0 top-most level offset
134 * @param name descriptive name of object
135 * @return TRUE if parsing successful
136 */
137 bool asn1_parse_simple_object(chunk_t *object, asn1_t type, u_int level0,
138 const char* name);
139
140 /**
141 * Print the value of an ASN.1 simple object
142 *
143 * @param object ASN.1 object to be printed
144 * @param type asn1_t type
145 * @param private ASN.1 data is confidential (use debug level 4)
146 */
147 void asn1_debug_simple_object(chunk_t object, asn1_t type, bool private);
148
149 /**
150 * Converts an ASN.1 UTCTIME or GENERALIZEDTIME string to time_t
151 *
152 * @param utctime body of an ASN.1 coded time object
153 * @param type ASN1_UTCTIME or ASN1_GENERALIZEDTIME
154 * @return time_t in UTC
155 */
156 time_t asn1_to_time(const chunk_t *utctime, asn1_t type);
157
158 /**
159 * Converts time_t to an ASN.1 UTCTIME or GENERALIZEDTIME string
160 *
161 * @param time time_t in UTC
162 * @param type ASN1_UTCTIME or ASN1_GENERALIZEDTIME
163 * @return body of an ASN.1 code time object
164 */
165 chunk_t asn1_from_time(const time_t *time, asn1_t type);
166
167 /**
168 * Parse an ASN.1 UTCTIME or GENERALIZEDTIME object
169 *
170 * @param blob ASN.1 coded time object
171 * @param level top-most level offset
172 * @return time_t in UTC
173 */
174 time_t asn1_parse_time(chunk_t blob, int level0);
175
176 /**
177 * Determines if a binary blob is ASN.1 coded
178 *
179 * @param blob blob to be tested
180 * @return TRUE if blob is ASN.1 coded (SEQUENCE or SET)
181 */
182 bool is_asn1(chunk_t blob);
183
184 /**
185 * Determines if a character string can be coded as PRINTABLESTRING
186 *
187 * @param str character string to be tested
188 * @return TRUE if no special characters are contained
189 */
190 bool asn1_is_printablestring(chunk_t str);
191
192
193 /** some ASN.1 synthesis functions */
194
195 /**
196 * Build an empty ASN.1 object with tag and length fields already filled in
197 *
198 * @param object returned object - memory is allocated by function
199 * @param type ASN.1 type to be created
200 * @param datalen size of the body to be created
201 * @return points to the first position in the body
202 */
203 u_char* asn1_build_object(chunk_t *object, asn1_t type, size_t datalen);
204
205 /**
206 * Build a simple ASN.1 object
207 *
208 * @param tag ASN.1 type to be created
209 * @param content content of the ASN.1 object
210 * @return chunk containing the ASN.1 coded object
211 */
212 chunk_t asn1_simple_object(asn1_t tag, chunk_t content);
213
214 /**
215 * Build an ASN.1 BITSTRING object
216 *
217 * @param mode 'c' for copy or 'm' for move
218 * @param content content of the BITSTRING
219 * @return chunk containing the ASN.1 coded BITSTRING
220 */
221 chunk_t asn1_bitstring(const char *mode, chunk_t content);
222
223 /**
224 * Build an ASN.1 object from a variable number of individual chunks
225 *
226 * @param typ ASN.1 type to be created
227 * @param mode for each list member: 'c' for copy or 'm' for move
228 * @return chunk containing the ASN.1 coded object
229 */
230 chunk_t asn1_wrap(asn1_t type, const char *mode, ...);
231
232 #endif /* ASN1_H_ @}*/