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