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