identification: Support custom types in string constructor prefixes
[strongswan.git] / src / libstrongswan / utils / identification.h
1 /*
2 * Copyright (C) 2009 Tobias Brunner
3 * Copyright (C) 2005-2009 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
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 identification identification
20 * @{ @ingroup utils
21 */
22
23
24 #ifndef IDENTIFICATION_H_
25 #define IDENTIFICATION_H_
26
27 typedef enum id_type_t id_type_t;
28 typedef struct identification_t identification_t;
29 typedef enum id_match_t id_match_t;
30 typedef enum id_part_t id_part_t;
31
32 #include <utils/chunk.h>
33 #include <collections/enumerator.h>
34
35 /**
36 * Matches returned from identification_t.match
37 */
38 enum id_match_t {
39 /* no match */
40 ID_MATCH_NONE = 0,
41 /* match to %any ID */
42 ID_MATCH_ANY = 1,
43 /* match with maximum allowed wildcards */
44 ID_MATCH_MAX_WILDCARDS = 2,
45 /* match with only one wildcard */
46 ID_MATCH_ONE_WILDCARD = 19,
47 /* perfect match, won't get better */
48 ID_MATCH_PERFECT = 20,
49 };
50
51 /**
52 * enum names for id_match_t.
53 */
54 extern enum_name_t *id_match_names;
55
56 /**
57 * ID Types in a ID payload.
58 */
59 enum id_type_t {
60
61 /**
62 * private type which matches any other id.
63 */
64 ID_ANY = 0,
65
66 /**
67 * ID data is a single four (4) octet IPv4 address.
68 */
69 ID_IPV4_ADDR = 1,
70
71 /**
72 * ID data is a fully-qualified domain name string.
73 * An example of a ID_FQDN is "example.com".
74 * The string MUST not contain any terminators (e.g., NULL, CR, etc.).
75 */
76 ID_FQDN = 2,
77
78 /**
79 * ID data is a fully-qualified RFC822 email address string.
80 * An example of an ID_RFC822_ADDR is "jsmith@example.com".
81 * The string MUST NOT contain any terminators.
82 */
83 ID_USER_FQDN = 3, /* IKEv1 only */
84 ID_RFC822_ADDR = 3, /* IKEv2 only */
85
86 /**
87 * ID data is an IPv4 subnet (IKEv1 only)
88 */
89 ID_IPV4_ADDR_SUBNET = 4,
90
91 /**
92 * ID data is a single sixteen (16) octet IPv6 address.
93 */
94 ID_IPV6_ADDR = 5,
95
96 /**
97 * ID data is an IPv6 subnet (IKEv1 only)
98 */
99 ID_IPV6_ADDR_SUBNET = 6,
100
101 /**
102 * ID data is an IPv4 address range (IKEv1 only)
103 */
104 ID_IPV4_ADDR_RANGE = 7,
105
106 /**
107 * ID data is an IPv6 address range (IKEv1 only)
108 */
109 ID_IPV6_ADDR_RANGE = 8,
110
111 /**
112 * ID data is the binary DER encoding of an ASN.1 X.501 Distinguished Name
113 */
114 ID_DER_ASN1_DN = 9,
115
116 /**
117 * ID data is the binary DER encoding of an ASN.1 X.509 GeneralName
118 */
119 ID_DER_ASN1_GN = 10,
120
121 /**
122 * ID data is an opaque octet stream which may be used to pass vendor-
123 * specific information necessary to do certain proprietary
124 * types of identification.
125 */
126 ID_KEY_ID = 11,
127
128 /**
129 * Private ID type which represents a GeneralName of type URI
130 */
131 ID_DER_ASN1_GN_URI = 201,
132
133 /**
134 * Private ID type which represents a user ID
135 */
136 ID_USER_ID = 202
137 };
138
139 /**
140 * enum names for id_type_t.
141 */
142 extern enum_name_t *id_type_names;
143
144 /**
145 * Type of an ID sub part.
146 */
147 enum id_part_t {
148 /** Username part of an RFC822_ADDR */
149 ID_PART_USERNAME,
150 /** Domain part of an RFC822_ADDR */
151 ID_PART_DOMAIN,
152
153 /** Top-Level domain of a FQDN */
154 ID_PART_TLD,
155 /** Second-Level domain of a FQDN */
156 ID_PART_SLD,
157 /** Another Level domain of a FQDN */
158 ID_PART_ALD,
159
160 /** Country RDN of a DN */
161 ID_PART_RDN_C,
162 /** CommonName RDN of a DN */
163 ID_PART_RDN_CN,
164 /** Description RDN of a DN */
165 ID_PART_RDN_D,
166 /** Email RDN of a DN */
167 ID_PART_RDN_E,
168 /** EmployeeNumber RDN of a DN */
169 ID_PART_RDN_EN,
170 /** GivenName RDN of a DN */
171 ID_PART_RDN_G,
172 /** Initials RDN of a DN */
173 ID_PART_RDN_I,
174 /** DN Qualifier RDN of a DN */
175 ID_PART_RDN_DNQ,
176 /** UniqueIdentifier RDN of a DN */
177 ID_PART_RDN_ID,
178 /** Locality RDN of a DN */
179 ID_PART_RDN_L,
180 /** Name RDN of a DN */
181 ID_PART_RDN_N,
182 /** Organization RDN of a DN */
183 ID_PART_RDN_O,
184 /** OrganizationUnit RDN of a DN */
185 ID_PART_RDN_OU,
186 /** Surname RDN of a DN */
187 ID_PART_RDN_S,
188 /** SerialNumber RDN of a DN */
189 ID_PART_RDN_SN,
190 /** StateOrProvince RDN of a DN */
191 ID_PART_RDN_ST,
192 /** Title RDN of a DN */
193 ID_PART_RDN_T,
194 };
195
196 /**
197 * Generic identification, such as used in ID payload.
198 *
199 * @todo Support for ID_DER_ASN1_GN is minimal right now. Comparison
200 * between them and ID_IPV4_ADDR/RFC822_ADDR would be nice.
201 */
202 struct identification_t {
203
204 /**
205 * Get the encoding of this id, to send over
206 * the network.
207 *
208 * Result points to internal data, do not free.
209 *
210 * @return a chunk containing the encoded bytes
211 */
212 chunk_t (*get_encoding) (identification_t *this);
213
214 /**
215 * Get the type of this identification.
216 *
217 * @return id_type_t
218 */
219 id_type_t (*get_type) (identification_t *this);
220
221 /**
222 * Check if two identification_t objects are equal.
223 *
224 * @param other other identification_t object
225 * @return TRUE if the IDs are equal
226 */
227 bool (*equals) (identification_t *this, identification_t *other);
228
229 /**
230 * Check if an ID matches a wildcard ID.
231 *
232 * An identification_t may contain wildcards, such as
233 * *.strongswan.org. This call checks if a given ID
234 * (e.g. tester.strongswan.org) belongs to a such wildcard
235 * ID. Returns > 0 if
236 * - IDs are identical
237 * - other is of type ID_ANY
238 * - other contains a wildcard and matches this
239 *
240 * The larger the return value is, the better is the match. Zero means
241 * no match at all, 1 means a bad match, and 2 a slightly better match.
242 *
243 * @param other the ID containing one or more wildcards
244 * @return match value as described above
245 */
246 id_match_t (*matches) (identification_t *this, identification_t *other);
247
248 /**
249 * Check if an ID is a wildcard ID.
250 *
251 * If the ID represents multiple IDs (with wildcards, or
252 * as the type ID_ANY), TRUE is returned. If it is unique,
253 * FALSE is returned.
254 *
255 * @return TRUE if ID contains wildcards
256 */
257 bool (*contains_wildcards) (identification_t *this);
258
259 /**
260 * Create an enumerator over subparts of an identity.
261 *
262 * Some identities are built from several parts, e.g. an E-Mail consists
263 * of a username and a domain part, or a DistinguishedName contains several
264 * RDNs.
265 * For identity without subtypes (support), an empty enumerator is
266 * returned.
267 *
268 * @return an enumerator over (id_part_t type, chunk_t data)
269 */
270 enumerator_t* (*create_part_enumerator)(identification_t *this);
271
272 /**
273 * Clone a identification_t instance.
274 *
275 * @return clone of this
276 */
277 identification_t *(*clone) (identification_t *this);
278
279 /**
280 * Destroys a identification_t object.
281 */
282 void (*destroy) (identification_t *this);
283 };
284
285 /**
286 * Creates an identification_t object from a string.
287 *
288 * The input string may be e.g. one of the following:
289 * - ID_IPV4_ADDR: 192.168.0.1
290 * - ID_IPV6_ADDR: 2001:0db8:85a3:08d3:1319:8a2e:0370:7345
291 * - ID_FQDN: www.strongswan.org (optionally with a prepended @)
292 * - ID_RFC822_ADDR: alice@wonderland.org
293 * - ID_DER_ASN1_DN: C=CH, O=Linux strongSwan, CN=bob
294 *
295 * In favour of pluto, domainnames are prepended with an @, since
296 * pluto resolves domainnames without an @ to IPv4 addresses. Since
297 * we use a separate host_t class for addresses, this doesn't
298 * make sense for us.
299 *
300 * A distinguished name may contain one or more of the following RDNs:
301 * ND, UID, DC, CN, S, SN, serialNumber, C, L, ST, O, OU, T, D,
302 * N, G, I, dnQualifier, ID, EN, EmployeeNumber, E, Email, emailAddress, UN,
303 * unstructuredName, TCGID.
304 *
305 * To skip automatic type detection the following prefixes may be used to
306 * enforce a specific type: ipv4:, ipv6:, rfc822:, email:, userfqdn:, fqdn:,
307 * dns:, asn1dn:, asn1gn: and keyid:. If a # follows the :, the remaining data
308 * is interpreted as hex encoded binary data for that ID, otherwise the raw
309 * string following the prefix is used as identity data, without conversion.
310 * To specify a non-standard ID type, the numerical type may be prefixed
311 * between curly backets, building a prefix. For instance the "{1}:" prefix
312 * defines an ID_IPV4_ADDR type.
313 *
314 * This constructor never returns NULL. If it does not find a suitable
315 * conversion function, it will copy the string to an ID_KEY_ID.
316 *
317 * @param string input string, which will be converted
318 * @return identification_t
319 */
320 identification_t * identification_create_from_string(char *string);
321
322 /**
323 * Creates an identification from a chunk of data, guessing its type.
324 *
325 * @param data identification data
326 * @return identification_t
327 */
328 identification_t * identification_create_from_data(chunk_t data);
329
330 /**
331 * Creates an identification_t object from an encoded chunk.
332 *
333 * @param type type of this id, such as ID_IPV4_ADDR
334 * @param encoded encoded bytes, such as from identification_t.get_encoding
335 * @return identification_t
336 */
337 identification_t * identification_create_from_encoding(id_type_t type, chunk_t encoded);
338
339 /**
340 * Creates an identification_t object from a sockaddr struct
341 *
342 * @param sockaddr sockaddr struct which contains family and address
343 * @return identification_t
344 */
345 identification_t * identification_create_from_sockaddr(sockaddr_t *sockaddr);
346
347 /**
348 * printf hook function for identification_t.
349 *
350 * Arguments are:
351 * identification_t *identification
352 */
353 int identification_printf_hook(printf_hook_data_t *data,
354 printf_hook_spec_t *spec, const void *const *args);
355
356 #endif /** IDENTIFICATION_H_ @}*/