(no commit message)
[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 return SUCCESS;
150 }
151
152 /**
153 * Implementation of id_payload_t.get_encoding_rules.
154 */
155 static void get_encoding_rules(private_id_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
156 {
157 *rules = id_payload_encodings;
158 *rule_count = sizeof(id_payload_encodings) / sizeof(encoding_rule_t);
159 }
160
161 /**
162 * Implementation of payload_t.get_type.
163 */
164 static payload_type_t get_payload_type(private_id_payload_t *this)
165 {
166 return ((this->is_initiator) ? ID_INITIATOR : ID_RESPONDER);
167 }
168
169 /**
170 * Implementation of payload_t.get_next_type.
171 */
172 static payload_type_t get_next_type(private_id_payload_t *this)
173 {
174 return (this->next_payload);
175 }
176
177 /**
178 * Implementation of payload_t.set_next_type.
179 */
180 static void set_next_type(private_id_payload_t *this,payload_type_t type)
181 {
182 this->next_payload = type;
183 }
184
185 /**
186 * Implementation of payload_t.get_length.
187 */
188 static size_t get_length(private_id_payload_t *this)
189 {
190 this->compute_length(this);
191 return this->payload_length;
192 }
193
194 /**
195 * Implementation of id_payload_t.set_type.
196 */
197 static void set_id_type (private_id_payload_t *this, id_type_t type)
198 {
199 this->id_type = type;
200 }
201
202 /**
203 * Implementation of id_payload_t.get_id_type.
204 */
205 static id_type_t get_id_type (private_id_payload_t *this)
206 {
207 return (this->id_type);
208 }
209
210 /**
211 * Implementation of id_payload_t.set_data.
212 */
213 static void set_data (private_id_payload_t *this, chunk_t data)
214 {
215 if (this->id_data.ptr != NULL)
216 {
217 allocator_free_chunk(&(this->id_data));
218 }
219 this->id_data.ptr = allocator_clone_bytes(data.ptr,data.len);
220 this->id_data.len = data.len;
221 }
222
223 /**
224 * Implementation of id_payload_t.get_data.
225 */
226 static chunk_t get_data (private_id_payload_t *this)
227 {
228 chunk_t cloned_data;
229 if (this->id_data.ptr == NULL)
230 {
231 return (this->id_data);
232 }
233 cloned_data.ptr = allocator_clone_bytes(this->id_data.ptr,this->id_data.len);
234 cloned_data.len = this->id_data.len;
235 return cloned_data;
236 }
237
238 /**
239 * Implementation of id_payload_t.get_initiator.
240 */
241 static bool get_initiator (private_id_payload_t *this)
242 {
243 return (this->is_initiator);
244 }
245
246 /**
247 * Implementation of id_payload_t.set_initiator.
248 */
249 static void set_initiator (private_id_payload_t *this,bool is_initiator)
250 {
251 this->is_initiator = is_initiator;
252 }
253
254 /**
255 * Implementation of payload_t.destroy and id_payload_t.destroy.
256 */
257 static void destroy(private_id_payload_t *this)
258 {
259 if (this->id_data.ptr != NULL)
260 {
261 allocator_free_chunk(&(this->id_data));
262 }
263
264 allocator_free(this);
265 }
266
267 /*
268 * Described in header
269 */
270 id_payload_t *id_payload_create(bool is_initiator)
271 {
272 private_id_payload_t *this = allocator_alloc_thing(private_id_payload_t);
273
274 /* interface functions */
275 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
276 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
277 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
278 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
279 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
280 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
281 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
282
283 /* public functions */
284 this->public.destroy = (void (*) (id_payload_t *)) destroy;
285 this->public.set_id_type = (void (*) (id_payload_t *,id_type_t)) set_id_type;
286 this->public.get_id_type = (id_type_t (*) (id_payload_t *)) get_id_type;
287 this->public.set_data = (void (*) (id_payload_t *,chunk_t)) set_data;
288 this->public.get_data = (chunk_t (*) (id_payload_t *)) get_data;
289 this->public.get_initiator = (bool (*) (id_payload_t *)) get_initiator;
290 this->public.set_initiator = (void (*) (id_payload_t *,bool)) set_initiator;
291
292 /* private variables */
293 this->critical = FALSE;
294 this->next_payload = NO_PAYLOAD;
295 this->payload_length =ID_PAYLOAD_HEADER_LENGTH;
296 this->id_data = CHUNK_INITIALIZER;
297 this->is_initiator = is_initiator;
298
299 return (&(this->public));
300 }