- implemented compute length function
[strongswan.git] / Source / charon / encoding / payloads / id_payload.c
1 /**
2 * @file id_payload.h
3 *
4 * @brief Interface of id_payload_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "id_payload.h"
24
25 #include <encoding/payloads/encodings.h>
26 #include <utils/allocator.h>
27
28 /**
29 * String mappings for id_type_t.
30 */
31 mapping_t id_type_m[] = {
32 {ID_IPV4_ADDR, "ID_IPV4_ADDR"},
33 {ID_FQDN, "ID_FQDN"},
34 {ID_RFC822_ADDR, "ID_RFC822_ADDR"},
35 {ID_IPV6_ADDR, "ID_IPV6_ADDR"},
36 {ID_DER_ASN1_DN, "ID_DER_ASN1_DN"},
37 {ID_DER_ASN1_GN, "ID_DER_ASN1_GN"},
38 {ID_KEY_ID, "ID_KEY_ID"},
39 {MAPPING_END, NULL}
40 };
41
42
43 typedef struct private_id_payload_t private_id_payload_t;
44
45 /**
46 * Private data of an id_payload_t object.
47 *
48 */
49 struct private_id_payload_t {
50 /**
51 * Public id_payload_t interface.
52 */
53 id_payload_t public;
54
55 /**
56 * TRUE if this ID payload is of type IDi, FALSE for IDr
57 */
58 bool is_initiator;
59
60 /**
61 * Next payload type.
62 */
63 u_int8_t next_payload;
64
65 /**
66 * Critical flag.
67 */
68 bool critical;
69
70 /**
71 * Length of this payload.
72 */
73 u_int16_t payload_length;
74
75 /**
76 * Type of the ID Data.
77 */
78 u_int8_t id_type;
79
80 /**
81 * The contained id data value.
82 */
83 chunk_t id_data;
84
85 /**
86 * @brief Computes the length of this payload.
87 *
88 * @param this calling private_id_payload_t object
89 */
90 void (*compute_length) (private_id_payload_t *this);
91 };
92
93 /**
94 * Encoding rules to parse or generate a ID payload
95 *
96 * The defined offsets are the positions in a object of type
97 * private_id_payload_t.
98 *
99 */
100 encoding_rule_t id_payload_encodings[] = {
101 /* 1 Byte next payload type, stored in the field next_payload */
102 { U_INT_8, offsetof(private_id_payload_t, next_payload) },
103 /* the critical bit */
104 { FLAG, offsetof(private_id_payload_t, critical) },
105 /* 7 Bit reserved bits, nowhere stored */
106 { RESERVED_BIT, 0 },
107 { RESERVED_BIT, 0 },
108 { RESERVED_BIT, 0 },
109 { RESERVED_BIT, 0 },
110 { RESERVED_BIT, 0 },
111 { RESERVED_BIT, 0 },
112 { RESERVED_BIT, 0 },
113 /* Length of the whole payload*/
114 { PAYLOAD_LENGTH, offsetof(private_id_payload_t, payload_length) },
115 /* 1 Byte ID type*/
116 { U_INT_8, offsetof(private_id_payload_t, id_type) },
117 /* 3 reserved bytes */
118 { RESERVED_BYTE, 0 },
119 { RESERVED_BYTE, 0 },
120 { RESERVED_BYTE, 0 },
121 /* some id data bytes, length is defined in PAYLOAD_LENGTH */
122 { ID_DATA, offsetof(private_id_payload_t, id_data) }
123 };
124
125 /*
126 1 2 3
127 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
128 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 ! Next Payload !C! RESERVED ! Payload Length !
130 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
131 ! ID Type ! RESERVED |
132 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 ! !
134 ~ Identification Data ~
135 ! !
136 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 */
138
139 /**
140 * Implementation of payload_t.verify.
141 */
142 static status_t verify(private_id_payload_t *this)
143 {
144 if (this->critical)
145 {
146 /* critical bit is set! */
147 return FAILED;
148 }
149 if ((this->id_type == 0) ||
150 (this->id_type == 4) ||
151 ((this->id_type >= 6) && (this->id_type <= 8)) ||
152 ((this->id_type >= 12) && (this->id_type <= 200)))
153 {
154 /* reserved IDs */
155 return FAILED;
156 }
157
158 return SUCCESS;
159 }
160
161 /**
162 * Implementation of id_payload_t.get_encoding_rules.
163 */
164 static void get_encoding_rules(private_id_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
165 {
166 *rules = id_payload_encodings;
167 *rule_count = sizeof(id_payload_encodings) / sizeof(encoding_rule_t);
168 }
169
170 /**
171 * Implementation of payload_t.get_type.
172 */
173 static payload_type_t get_payload_type(private_id_payload_t *this)
174 {
175 return ((this->is_initiator) ? ID_INITIATOR : ID_RESPONDER);
176 }
177
178 /**
179 * Implementation of payload_t.get_next_type.
180 */
181 static payload_type_t get_next_type(private_id_payload_t *this)
182 {
183 return (this->next_payload);
184 }
185
186 /**
187 * Implementation of payload_t.set_next_type.
188 */
189 static void set_next_type(private_id_payload_t *this,payload_type_t type)
190 {
191 this->next_payload = type;
192 }
193
194 /**
195 * Implementation of payload_t.get_length.
196 */
197 static size_t get_length(private_id_payload_t *this)
198 {
199 this->compute_length(this);
200 return this->payload_length;
201 }
202
203 /**
204 * Implementation of id_payload_t.set_type.
205 */
206 static void set_id_type (private_id_payload_t *this, id_type_t type)
207 {
208 this->id_type = type;
209 }
210
211 /**
212 * Implementation of id_payload_t.get_id_type.
213 */
214 static id_type_t get_id_type (private_id_payload_t *this)
215 {
216 return (this->id_type);
217 }
218
219 /**
220 * Implementation of id_payload_t.set_data.
221 */
222 static void set_data (private_id_payload_t *this, chunk_t data)
223 {
224 if (this->id_data.ptr != NULL)
225 {
226 allocator_free_chunk(&(this->id_data));
227 }
228 this->id_data.ptr = allocator_clone_bytes(data.ptr,data.len);
229 this->id_data.len = data.len;
230 }
231
232 /**
233 * Implementation of id_payload_t.get_data.
234 */
235 static chunk_t get_data (private_id_payload_t *this)
236 {
237 chunk_t cloned_data;
238 if (this->id_data.ptr == NULL)
239 {
240 return (this->id_data);
241 }
242 cloned_data.ptr = allocator_clone_bytes(this->id_data.ptr,this->id_data.len);
243 cloned_data.len = this->id_data.len;
244 return cloned_data;
245 }
246
247 /**
248 * Implementation of id_payload_t.get_initiator.
249 */
250 static bool get_initiator (private_id_payload_t *this)
251 {
252 return (this->is_initiator);
253 }
254
255 /**
256 * Implementation of id_payload_t.set_initiator.
257 */
258 static void set_initiator (private_id_payload_t *this,bool is_initiator)
259 {
260 this->is_initiator = is_initiator;
261 }
262
263 /**
264 * Implementation of private_id_payload_t.compute_length.
265 */
266 static void compute_length(private_id_payload_t *this)
267 {
268 this->payload_length = ID_PAYLOAD_HEADER_LENGTH + this->id_data.len;
269 }
270
271 /**
272 * Implementation of payload_t.destroy and id_payload_t.destroy.
273 */
274 static void destroy(private_id_payload_t *this)
275 {
276 if (this->id_data.ptr != NULL)
277 {
278 allocator_free_chunk(&(this->id_data));
279 }
280
281 allocator_free(this);
282 }
283
284 /*
285 * Described in header
286 */
287 id_payload_t *id_payload_create(bool is_initiator)
288 {
289 private_id_payload_t *this = allocator_alloc_thing(private_id_payload_t);
290
291 /* interface functions */
292 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
293 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
294 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
295 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
296 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
297 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
298 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
299
300 /* public functions */
301 this->public.destroy = (void (*) (id_payload_t *)) destroy;
302 this->public.set_id_type = (void (*) (id_payload_t *,id_type_t)) set_id_type;
303 this->public.get_id_type = (id_type_t (*) (id_payload_t *)) get_id_type;
304 this->public.set_data = (void (*) (id_payload_t *,chunk_t)) set_data;
305 this->public.get_data = (chunk_t (*) (id_payload_t *)) get_data;
306 this->public.get_initiator = (bool (*) (id_payload_t *)) get_initiator;
307 this->public.set_initiator = (void (*) (id_payload_t *,bool)) set_initiator;
308
309 /* private functions */
310 this->compute_length = compute_length;
311
312 /* private variables */
313 this->critical = FALSE;
314 this->next_payload = NO_PAYLOAD;
315 this->payload_length =ID_PAYLOAD_HEADER_LENGTH;
316 this->id_data = CHUNK_INITIALIZER;
317 this->is_initiator = is_initiator;
318
319 return (&(this->public));
320 }