3978b23f35d1036fe705308b6a87e0e9c8a75802
[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 <chunk.h>
33 #include <utils/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 type which represents a GeneralName of type URI
130 */
131 ID_DER_ASN1_GN_URI = 201,
132
133 /**
134 * Private ID used by the pluto daemon for opportunistic encryption
135 */
136 ID_MYID = 203,
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 * @param wildcards returns the number of wildcards, may be NULL
245 * @return match value as described above
246 */
247 id_match_t (*matches) (identification_t *this, identification_t *other);
248
249 /**
250 * Check if an ID is a wildcard ID.
251 *
252 * If the ID represents multiple IDs (with wildcards, or
253 * as the type ID_ANY), TRUE is returned. If it is unique,
254 * FALSE is returned.
255 *
256 * @return TRUE if ID contains wildcards
257 */
258 bool (*contains_wildcards) (identification_t *this);
259
260 /**
261 * Create an enumerator over subparts of an identity.
262 *
263 * Some identities are built from several parts, e.g. an E-Mail consists
264 * of a username and a domain part, or a DistinguishedName contains several
265 * RDNs.
266 * For identity without subtypes (support), an empty enumerator is
267 * returned.
268 *
269 * @return an enumerator over (id_part_t type, chunk_t data)
270 */
271 enumerator_t* (*create_part_enumerator)(identification_t *this);
272
273 /**
274 * Clone a identification_t instance.
275 *
276 * @return clone of this
277 */
278 identification_t *(*clone) (identification_t *this);
279
280 /**
281 * Destroys a identification_t object.
282 */
283 void (*destroy) (identification_t *this);
284 };
285
286 /**
287 * Creates an identification_t object from a string.
288 *
289 * The input string may be e.g. one of the following:
290 * - ID_IPV4_ADDR: 192.168.0.1
291 * - ID_IPV6_ADDR: 2001:0db8:85a3:08d3:1319:8a2e:0370:7345
292 * - ID_FQDN: www.strongswan.org (optionally with a prepended @)
293 * - ID_RFC822_ADDR: alice@wonderland.org
294 * - ID_DER_ASN1_DN: C=CH, O=Linux strongSwan, CN=bob
295 *
296 * In favour of pluto, domainnames are prepended with an @, since
297 * pluto resolves domainnames without an @ to IPv4 addresses. Since
298 * we use a separate host_t class for addresses, this doesn't
299 * make sense for us.
300 *
301 * A distinguished name may contain one or more of the following RDNs:
302 * ND, UID, DC, CN, S, SN, serialNumber, C, L, ST, O, OU, T, D,
303 * N, G, I, dnQualifier, ID, EN, EmployeeNumber, E, Email, emailAddress, UN,
304 * unstructuredName, TCGID.
305 *
306 * This constructor never returns NULL. If it does not find a suitable
307 * conversion function, it will copy the string to an ID_KEY_ID.
308 *
309 * @param string input string, which will be converted
310 * @return identification_t
311 */
312 identification_t * identification_create_from_string(char *string);
313
314 /**
315 * Creates an identification from a chunk of data, guessing its type.
316 *
317 * @param data identification data
318 * @return identification_t
319 */
320 identification_t * identification_create_from_data(chunk_t data);
321
322 /**
323 * Creates an identification_t object from an encoded chunk.
324 *
325 * @param type type of this id, such as ID_IPV4_ADDR
326 * @param encoded encoded bytes, such as from identification_t.get_encoding
327 * @return identification_t
328 */
329 identification_t * identification_create_from_encoding(id_type_t type, chunk_t encoded);
330
331 /**
332 * Creates an identification_t object from a sockaddr struct
333 *
334 * @param sockaddr sockaddr struct which contains family and address
335 * @return identification_t
336 */
337 identification_t * identification_create_from_sockaddr(sockaddr_t *sockaddr);
338
339 /**
340 * printf hook function for identification_t.
341 *
342 * Arguments are:
343 * identification_t *identification
344 */
345 int identification_printf_hook(char *dst, size_t len, printf_hook_spec_t *spec,
346 const void *const *args);
347
348 #endif /** IDENTIFICATION_H_ @}*/