5b13d02a0b6383d59382da28975c0b081c865b99
[strongswan.git] / src / libstrongswan / utils / identification.h
1 /**
2 * @file identification.h
3 *
4 * @brief Interface of identification_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24
25 #ifndef IDENTIFICATION_H_
26 #define IDENTIFICATION_H_
27
28 #include "types.h"
29
30 #define MAX_WILDCARDS 14
31
32 typedef enum id_type_t id_type_t;
33
34 /**
35 * @brief ID Types in a ID payload.
36 *
37 * @ingroup utils
38 */
39 enum id_type_t {
40
41 /**
42 * private type which matches any other id.
43 */
44 ID_ANY = 0,
45
46 /**
47 * ID data is a single four (4) octet IPv4 address.
48 */
49 ID_IPV4_ADDR = 1,
50
51 /**
52 * ID data is a fully-qualified domain name string.
53 * An example of a ID_FQDN is "example.com".
54 * The string MUST not contain any terminators (e.g., NULL, CR, etc.).
55 */
56 ID_FQDN = 2,
57
58 /**
59 * ID data is a fully-qualified RFC822 email address string.
60 * An example of an ID_RFC822_ADDR is "jsmith@example.com".
61 * The string MUST NOT contain any terminators.
62 */
63 ID_RFC822_ADDR = 3,
64
65 /**
66 * ID data is an IPv4 subnet (IKEv1 only)
67 */
68 ID_IPV4_ADDR_SUBNET = 4,
69
70 /**
71 * ID data is a single sixteen (16) octet IPv6 address.
72 */
73 ID_IPV6_ADDR = 5,
74
75 /**
76 * ID data is an IPv6 subnet (IKEv1 only)
77 */
78 ID_IPV6_ADDR_SUBNET = 6,
79
80 /**
81 * ID data is an IPv4 address range (IKEv1 only)
82 */
83 ID_IPV4_ADDR_RANGE = 7,
84
85 /**
86 * ID data is an IPv6 address range (IKEv1 only)
87 */
88 ID_IPV6_ADDR_RANGE = 8,
89
90 /**
91 * ID data is the binary DER encoding of an ASN.1 X.501 Distinguished Name
92 */
93 ID_DER_ASN1_DN = 9,
94
95 /**
96 * ID data is the binary DER encoding of an ASN.1 X.509 GeneralName
97 */
98 ID_DER_ASN1_GN = 10,
99
100 /**
101 * ID data is an opaque octet stream which may be used to pass vendor-
102 * specific information necessary to do certain proprietary
103 * types of identification.
104 */
105 ID_KEY_ID = 11,
106
107 /**
108 * private type which represents a GeneralName of type URI
109 */
110 ID_DER_ASN1_GN_URI = 201,
111
112 };
113
114 /**
115 * String mappings for id_type_t.
116 */
117 extern enum_names id_type_names;
118
119 typedef struct identification_t identification_t;
120
121 /**
122 * @brief Generic identification, such as used in ID payload.
123 *
124 * The following types are possible:
125 * - ID_IPV4_ADDR
126 * - ID_FQDN
127 * - ID_RFC822_ADDR
128 * - ID_IPV6_ADDR
129 * - ID_DER_ASN1_DN
130 * - ID_DER_ASN1_GN
131 * - ID_KEY_ID
132 * - ID_DER_ASN1_GN_URI
133 *
134 * @b Constructors:
135 * - identification_create_from_string()
136 * - identification_create_from_encoding()
137 *
138 * @todo Support for ID_DER_ASN1_GN is minimal right now. Comparison
139 * between them and ID_IPV4_ADDR/RFC822_ADDR would be nice.
140 *
141 * @ingroup utils
142 */
143 struct identification_t {
144
145 /**
146 * @brief Get the encoding of this id, to send over
147 * the network.
148 *
149 * @warning Result points to internal data, do NOT free!
150 *
151 * @param this the identification_t object
152 * @return a chunk containing the encoded bytes
153 */
154 chunk_t (*get_encoding) (identification_t *this);
155
156 /**
157 * @brief Get the type of this identification.
158 *
159 * @param this the identification_t object
160 * @return id_type_t
161 */
162 id_type_t (*get_type) (identification_t *this);
163
164 /**
165 * @brief Get a string representation of this id.
166 *
167 * @warning Result points to internal data, do NOT free!
168 *
169 * @param this the identification_t object
170 * @return string
171 */
172 char *(*get_string) (identification_t *this);
173
174 /**
175 * @brief Check if two identification_t objects are equal.
176 *
177 * @param this the identification_t object
178 * @param other other identification_t object
179 * @return TRUE if the IDs are equal
180 */
181 bool (*equals) (identification_t *this, identification_t *other);
182
183 /**
184 * @brief Check if an ID matches a wildcard ID.
185 *
186 * An identification_t may contain wildcards, such as
187 * *@strongswan.org. This call checks if a given ID
188 * (e.g. tester@strongswan.org) belongs to a such wildcard
189 * ID. Returns TRUE if
190 * - IDs are identical
191 * - other is of type ID_ANY
192 * - other contains a wildcard and matches this
193 *
194 * @param this the ID without wildcard
195 * @param other the ID containing a wildcard
196 * @param wildcards returns the number of wildcards
197 * @return TRUE if match is found
198 */
199 bool (*matches) (identification_t *this, identification_t *other, int *wildcards);
200
201 /**
202 * @brief Check if an ID is a wildcard ID.
203 *
204 * If the ID represents multiple IDs (with wildcards, or
205 * as the type ID_ANY), TRUE is returned. If it is unique,
206 * FALSE is returned.
207 *
208 * @param this identification_t object
209 * @return TRUE if ID contains wildcards
210 */
211 bool (*contains_wildcards) (identification_t *this);
212
213 /**
214 * @brief Clone a identification_t instance.
215 *
216 * @param this the identification_t object to clone
217 * @return clone of this
218 */
219 identification_t *(*clone) (identification_t *this);
220
221 /**
222 * @brief Destroys a identification_t object.
223 *
224 * @param this identification_t object
225 */
226 void (*destroy) (identification_t *this);
227 };
228
229 /**
230 * @brief Creates an identification_t object from a string.
231 *
232 * @param string input string, which will be converted
233 * @return
234 * - created identification_t object, or
235 * - NULL if unsupported string supplied.
236 *
237 * The input string may be e.g. one of the following:
238 * - ID_IPV4_ADDR: 192.168.0.1
239 * - ID_IPV6_ADDR: 2001:0db8:85a3:08d3:1319:8a2e:0370:7345
240 * - ID_FQDN: @www.strongswan.org (@indicates FQDN)
241 * - ID_RFC822_ADDR: alice@wonderland.org
242 * - ID_DER_ASN1_DN: C=CH, O=Linux strongSwan, CN=bob
243 *
244 * In favour of pluto, domainnames are prepended with an @, since
245 * pluto resolves domainnames without an @ to IPv4 addresses. Since
246 * we use a seperate host_t class for addresses, this doesn't
247 * make sense for us.
248 *
249 * A distinguished name may contain one or more of the following RDNs:
250 * ND, UID, DC, CN, S, SN, serialNumber, C, L, ST, O, OU, T, D,
251 * N, G, I, ID, EN, EmployeeNumber, E, Email, emailAddress, UN,
252 * unstructuredName, TCGID.
253 *
254 * @ingroup utils
255 */
256 identification_t * identification_create_from_string(char *string);
257
258 /**
259 * @brief Creates an identification_t object from an encoded chunk.
260 *
261 * @param type type of this id, such as ID_IPV4_ADDR
262 * @param encoded encoded bytes, such as from identification_t.get_encoding
263 * @return identification_t object
264 *
265 * In contrast to identification_create_from_string(), this constructor never
266 * returns NULL, even when the conversion to a string representation fails.
267 *
268 * @ingroup utils
269 */
270 identification_t * identification_create_from_encoding(id_type_t type, chunk_t encoded);
271
272
273 #endif /* IDENTIFICATION_H_ */