4 * @brief Implementation of auth_payload_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
23 #include "auth_payload.h"
25 #include <encoding/payloads/encodings.h>
28 typedef struct private_auth_payload_t private_auth_payload_t
;
31 * Private data of an auth_payload_t object.
34 struct private_auth_payload_t
{
37 * Public auth_payload_t interface.
39 auth_payload_t
public;
44 u_int8_t next_payload
;
52 * Length of this payload.
54 u_int16_t payload_length
;
57 * Method of the AUTH Data.
62 * The contained auth data value.
68 * Encoding rules to parse or generate a AUTH payload
70 * The defined offsets are the positions in a object of type
71 * private_auth_payload_t.
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 */
87 /* Length of the whole payload*/
88 { PAYLOAD_LENGTH
, offsetof(private_auth_payload_t
, payload_length
)},
90 { U_INT_8
, offsetof(private_auth_payload_t
, auth_method
) },
91 /* 3 reserved bytes */
95 /* some auth data bytes, length is defined in PAYLOAD_LENGTH */
96 { AUTH_DATA
, offsetof(private_auth_payload_t
, auth_data
) }
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 ~ Authentication Data ~
110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 * Implementation of payload_t.verify.
116 static status_t
verify(private_auth_payload_t
*this)
118 if ((this->auth_method
== 0) ||
119 ((this->auth_method
>= 4) && (this->auth_method
<= 200)))
128 * Implementation of auth_payload_t.get_encoding_rules.
130 static void get_encoding_rules(private_auth_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
132 *rules
= auth_payload_encodings
;
133 *rule_count
= sizeof(auth_payload_encodings
) / sizeof(encoding_rule_t
);
137 * Implementation of payload_t.get_type.
139 static payload_type_t
get_payload_type(private_auth_payload_t
*this)
141 return AUTHENTICATION
;
145 * Implementation of payload_t.get_next_type.
147 static payload_type_t
get_next_type(private_auth_payload_t
*this)
149 return (this->next_payload
);
153 * Implementation of payload_t.set_next_type.
155 static void set_next_type(private_auth_payload_t
*this,payload_type_t type
)
157 this->next_payload
= type
;
161 * Implementation of payload_t.get_length.
163 static size_t get_length(private_auth_payload_t
*this)
165 return this->payload_length
;
169 * Implementation of auth_payload_t.set_auth_method.
171 static void set_auth_method (private_auth_payload_t
*this, auth_method_t method
)
173 this->auth_method
= method
;
177 * Implementation of auth_payload_t.get_auth_method.
179 static auth_method_t
get_auth_method (private_auth_payload_t
*this)
181 return (this->auth_method
);
185 * Implementation of auth_payload_t.set_data.
187 static void set_data (private_auth_payload_t
*this, chunk_t data
)
189 if (this->auth_data
.ptr
!= NULL
)
191 chunk_free(&(this->auth_data
));
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
;
199 * Implementation of auth_payload_t.get_data.
201 static chunk_t
get_data (private_auth_payload_t
*this)
203 return (this->auth_data
);
207 * Implementation of auth_payload_t.get_data_clone.
209 static chunk_t
get_data_clone (private_auth_payload_t
*this)
212 if (this->auth_data
.ptr
== NULL
)
214 return (this->auth_data
);
216 cloned_data
.ptr
= clalloc(this->auth_data
.ptr
,this->auth_data
.len
);
217 cloned_data
.len
= this->auth_data
.len
;
222 * Implementation of payload_t.destroy and auth_payload_t.destroy.
224 static void destroy(private_auth_payload_t
*this)
226 if (this->auth_data
.ptr
!= NULL
)
228 chunk_free(&(this->auth_data
));
235 * Described in header
237 auth_payload_t
*auth_payload_create()
239 private_auth_payload_t
*this = malloc_thing(private_auth_payload_t
);
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
;
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
;
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
;
264 return (&(this->public));