updated Doxyfile
[strongswan.git] / src / charon / encoding / payloads / auth_payload.c
1 /*
2 * Copyright (C) 2005-2006 Martin Willi
3 * Copyright (C) 2005 Jan Hutter
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 *
16 * $Id$
17 */
18
19 #include "auth_payload.h"
20
21 #include <encoding/payloads/encodings.h>
22
23
24 typedef struct private_auth_payload_t private_auth_payload_t;
25
26 /**
27 * Private data of an auth_payload_t object.
28 *
29 */
30 struct private_auth_payload_t {
31
32 /**
33 * Public auth_payload_t interface.
34 */
35 auth_payload_t public;
36
37 /**
38 * Next payload type.
39 */
40 u_int8_t next_payload;
41
42 /**
43 * Critical flag.
44 */
45 bool critical;
46
47 /**
48 * Length of this payload.
49 */
50 u_int16_t payload_length;
51
52 /**
53 * Method of the AUTH Data.
54 */
55 u_int8_t auth_method;
56
57 /**
58 * The contained auth data value.
59 */
60 chunk_t auth_data;
61 };
62
63 /**
64 * Encoding rules to parse or generate a AUTH payload
65 *
66 * The defined offsets are the positions in a object of type
67 * private_auth_payload_t.
68 */
69 encoding_rule_t auth_payload_encodings[] = {
70 /* 1 Byte next payload type, stored in the field next_payload */
71 { U_INT_8, offsetof(private_auth_payload_t, next_payload) },
72 /* the critical bit */
73 { FLAG, offsetof(private_auth_payload_t, critical) },
74 /* 7 Bit reserved bits, nowhere stored */
75 { RESERVED_BIT, 0 },
76 { RESERVED_BIT, 0 },
77 { RESERVED_BIT, 0 },
78 { RESERVED_BIT, 0 },
79 { RESERVED_BIT, 0 },
80 { RESERVED_BIT, 0 },
81 { RESERVED_BIT, 0 },
82 /* Length of the whole payload*/
83 { PAYLOAD_LENGTH, offsetof(private_auth_payload_t, payload_length)},
84 /* 1 Byte AUTH type*/
85 { U_INT_8, offsetof(private_auth_payload_t, auth_method) },
86 /* 3 reserved bytes */
87 { RESERVED_BYTE, 0 },
88 { RESERVED_BYTE, 0 },
89 { RESERVED_BYTE, 0 },
90 /* some auth data bytes, length is defined in PAYLOAD_LENGTH */
91 { AUTH_DATA, offsetof(private_auth_payload_t, auth_data) }
92 };
93
94 /*
95 1 2 3
96 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
97 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
98 ! Next Payload !C! RESERVED ! Payload Length !
99 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
100 ! Auth Method ! RESERVED !
101 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
102 ! !
103 ~ Authentication Data ~
104 ! !
105 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 */
107
108 /**
109 * Implementation of payload_t.verify.
110 */
111 static status_t verify(private_auth_payload_t *this)
112 {
113 if (this->auth_method == 0 ||
114 (this->auth_method >= 4 && this->auth_method <= 8) ||
115 (this->auth_method >= 12 && this->auth_method <= 200))
116 {
117 /* reserved IDs */
118 return FAILED;
119 }
120 return SUCCESS;
121 }
122
123 /**
124 * Implementation of auth_payload_t.get_encoding_rules.
125 */
126 static void get_encoding_rules(private_auth_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
127 {
128 *rules = auth_payload_encodings;
129 *rule_count = sizeof(auth_payload_encodings) / sizeof(encoding_rule_t);
130 }
131
132 /**
133 * Implementation of payload_t.get_type.
134 */
135 static payload_type_t get_payload_type(private_auth_payload_t *this)
136 {
137 return AUTHENTICATION;
138 }
139
140 /**
141 * Implementation of payload_t.get_next_type.
142 */
143 static payload_type_t get_next_type(private_auth_payload_t *this)
144 {
145 return (this->next_payload);
146 }
147
148 /**
149 * Implementation of payload_t.set_next_type.
150 */
151 static void set_next_type(private_auth_payload_t *this,payload_type_t type)
152 {
153 this->next_payload = type;
154 }
155
156 /**
157 * Implementation of payload_t.get_length.
158 */
159 static size_t get_length(private_auth_payload_t *this)
160 {
161 return this->payload_length;
162 }
163
164 /**
165 * Implementation of auth_payload_t.set_auth_method.
166 */
167 static void set_auth_method (private_auth_payload_t *this, auth_method_t method)
168 {
169 this->auth_method = method;
170 }
171
172 /**
173 * Implementation of auth_payload_t.get_auth_method.
174 */
175 static auth_method_t get_auth_method (private_auth_payload_t *this)
176 {
177 return (this->auth_method);
178 }
179
180 /**
181 * Implementation of auth_payload_t.set_data.
182 */
183 static void set_data (private_auth_payload_t *this, chunk_t data)
184 {
185 if (this->auth_data.ptr != NULL)
186 {
187 chunk_free(&(this->auth_data));
188 }
189 this->auth_data.ptr = clalloc(data.ptr,data.len);
190 this->auth_data.len = data.len;
191 this->payload_length = AUTH_PAYLOAD_HEADER_LENGTH + this->auth_data.len;
192 }
193
194 /**
195 * Implementation of auth_payload_t.get_data.
196 */
197 static chunk_t get_data (private_auth_payload_t *this)
198 {
199 return (this->auth_data);
200 }
201
202 /**
203 * Implementation of auth_payload_t.get_data_clone.
204 */
205 static chunk_t get_data_clone (private_auth_payload_t *this)
206 {
207 chunk_t cloned_data;
208 if (this->auth_data.ptr == NULL)
209 {
210 return (this->auth_data);
211 }
212 cloned_data.ptr = clalloc(this->auth_data.ptr,this->auth_data.len);
213 cloned_data.len = this->auth_data.len;
214 return cloned_data;
215 }
216
217 /**
218 * Implementation of payload_t.destroy and auth_payload_t.destroy.
219 */
220 static void destroy(private_auth_payload_t *this)
221 {
222 if (this->auth_data.ptr != NULL)
223 {
224 chunk_free(&(this->auth_data));
225 }
226
227 free(this);
228 }
229
230 /*
231 * Described in header
232 */
233 auth_payload_t *auth_payload_create()
234 {
235 private_auth_payload_t *this = malloc_thing(private_auth_payload_t);
236
237 /* interface functions */
238 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
239 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
240 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
241 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
242 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
243 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_payload_type;
244 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
245
246 /* public functions */
247 this->public.destroy = (void (*) (auth_payload_t *)) destroy;
248 this->public.set_auth_method = (void (*) (auth_payload_t *,auth_method_t)) set_auth_method;
249 this->public.get_auth_method = (auth_method_t (*) (auth_payload_t *)) get_auth_method;
250 this->public.set_data = (void (*) (auth_payload_t *,chunk_t)) set_data;
251 this->public.get_data_clone = (chunk_t (*) (auth_payload_t *)) get_data_clone;
252 this->public.get_data = (chunk_t (*) (auth_payload_t *)) get_data;
253
254 /* private variables */
255 this->critical = FALSE;
256 this->next_payload = NO_PAYLOAD;
257 this->payload_length =AUTH_PAYLOAD_HEADER_LENGTH;
258 this->auth_data = chunk_empty;
259
260 return (&(this->public));
261 }