- added and tested auth_payload_t class
[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 /**
87 * Encoding rules to parse or generate a ID payload
88 *
89 * The defined offsets are the positions in a object of type
90 * private_id_payload_t.
91 *
92 */
93 encoding_rule_t id_payload_encodings[] = {
94 /* 1 Byte next payload type, stored in the field next_payload */
95 { U_INT_8, offsetof(private_id_payload_t, next_payload) },
96 /* the critical bit */
97 { FLAG, offsetof(private_id_payload_t, critical) },
98 /* 7 Bit reserved bits, nowhere stored */
99 { RESERVED_BIT, 0 },
100 { RESERVED_BIT, 0 },
101 { RESERVED_BIT, 0 },
102 { RESERVED_BIT, 0 },
103 { RESERVED_BIT, 0 },
104 { RESERVED_BIT, 0 },
105 { RESERVED_BIT, 0 },
106 /* Length of the whole payload*/
107 { PAYLOAD_LENGTH, offsetof(private_id_payload_t, payload_length) },
108 /* 1 Byte ID type*/
109 { U_INT_8, offsetof(private_id_payload_t, id_type) },
110 /* 3 reserved bytes */
111 { RESERVED_BYTE, 0 },
112 { RESERVED_BYTE, 0 },
113 { RESERVED_BYTE, 0 },
114 /* some id data bytes, length is defined in PAYLOAD_LENGTH */
115 { ID_DATA, offsetof(private_id_payload_t, id_data) }
116 };
117
118 /*
119 1 2 3
120 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
121 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 ! Next Payload !C! RESERVED ! Payload Length !
123 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
124 ! ID Type ! RESERVED |
125 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126 ! !
127 ~ Identification Data ~
128 ! !
129 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 */
131
132 /**
133 * Implementation of payload_t.verify.
134 */
135 static status_t verify(private_id_payload_t *this)
136 {
137 if (this->critical)
138 {
139 /* critical bit is set! */
140 return FAILED;
141 }
142 if ((this->id_type == 0) ||
143 (this->id_type == 4) ||
144 ((this->id_type >= 6) && (this->id_type <= 8)) ||
145 ((this->id_type >= 12) && (this->id_type <= 200)))
146 {
147 /* reserved IDs */
148 return FAILED;
149 }
150
151 return SUCCESS;
152 }
153
154 /**
155 * Implementation of id_payload_t.get_encoding_rules.
156 */
157 static void get_encoding_rules(private_id_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
158 {
159 *rules = id_payload_encodings;
160 *rule_count = sizeof(id_payload_encodings) / sizeof(encoding_rule_t);
161 }
162
163 /**
164 * Implementation of payload_t.get_type.
165 */
166 static payload_type_t get_payload_type(private_id_payload_t *this)
167 {
168 return ((this->is_initiator) ? ID_INITIATOR : ID_RESPONDER);
169 }
170
171 /**
172 * Implementation of payload_t.get_next_type.
173 */
174 static payload_type_t get_next_type(private_id_payload_t *this)
175 {
176 return (this->next_payload);
177 }
178
179 /**
180 * Implementation of payload_t.set_next_type.
181 */
182 static void set_next_type(private_id_payload_t *this,payload_type_t type)
183 {
184 this->next_payload = type;
185 }
186
187 /**
188 * Implementation of payload_t.get_length.
189 */
190 static size_t get_length(private_id_payload_t *this)
191 {
192 return this->payload_length;
193 }
194
195 /**
196 * Implementation of id_payload_t.set_type.
197 */
198 static void set_id_type (private_id_payload_t *this, id_type_t type)
199 {
200 this->id_type = type;
201 }
202
203 /**
204 * Implementation of id_payload_t.get_id_type.
205 */
206 static id_type_t get_id_type (private_id_payload_t *this)
207 {
208 return (this->id_type);
209 }
210
211 /**
212 * Implementation of id_payload_t.set_data.
213 */
214 static void set_data (private_id_payload_t *this, chunk_t data)
215 {
216 if (this->id_data.ptr != NULL)
217 {
218 allocator_free_chunk(&(this->id_data));
219 }
220 this->id_data.ptr = allocator_clone_bytes(data.ptr,data.len);
221 this->id_data.len = data.len;
222 this->payload_length = ID_PAYLOAD_HEADER_LENGTH + this->id_data.len;
223 }
224
225 /**
226 * Implementation of id_payload_t.get_data.
227 */
228 static chunk_t get_data (private_id_payload_t *this)
229 {
230 chunk_t cloned_data;
231 if (this->id_data.ptr == NULL)
232 {
233 return (this->id_data);
234 }
235 cloned_data.ptr = allocator_clone_bytes(this->id_data.ptr,this->id_data.len);
236 cloned_data.len = this->id_data.len;
237 return cloned_data;
238 }
239
240 /**
241 * Implementation of id_payload_t.get_initiator.
242 */
243 static bool get_initiator (private_id_payload_t *this)
244 {
245 return (this->is_initiator);
246 }
247
248 /**
249 * Implementation of id_payload_t.set_initiator.
250 */
251 static void set_initiator (private_id_payload_t *this,bool is_initiator)
252 {
253 this->is_initiator = is_initiator;
254 }
255
256 /**
257 * Implementation of payload_t.destroy and id_payload_t.destroy.
258 */
259 static void destroy(private_id_payload_t *this)
260 {
261 if (this->id_data.ptr != NULL)
262 {
263 allocator_free_chunk(&(this->id_data));
264 }
265
266 allocator_free(this);
267 }
268
269 /*
270 * Described in header
271 */
272 id_payload_t *id_payload_create(bool is_initiator)
273 {
274 private_id_payload_t *this = allocator_alloc_thing(private_id_payload_t);
275
276 /* interface functions */
277 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
278 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
279 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
280 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
281 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
282 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
283 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
284
285 /* public functions */
286 this->public.destroy = (void (*) (id_payload_t *)) destroy;
287 this->public.set_id_type = (void (*) (id_payload_t *,id_type_t)) set_id_type;
288 this->public.get_id_type = (id_type_t (*) (id_payload_t *)) get_id_type;
289 this->public.set_data = (void (*) (id_payload_t *,chunk_t)) set_data;
290 this->public.get_data = (chunk_t (*) (id_payload_t *)) get_data;
291 this->public.get_initiator = (bool (*) (id_payload_t *)) get_initiator;
292 this->public.set_initiator = (void (*) (id_payload_t *,bool)) set_initiator;
293
294 /* private variables */
295 this->critical = FALSE;
296 this->next_payload = NO_PAYLOAD;
297 this->payload_length =ID_PAYLOAD_HEADER_LENGTH;
298 this->id_data = CHUNK_INITIALIZER;
299 this->is_initiator = is_initiator;
300
301 return (&(this->public));
302 }