2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include "auth_payload.h"
20 #include <encoding/payloads/encodings.h>
22 typedef struct private_auth_payload_t private_auth_payload_t
;
25 * Private data of an auth_payload_t object.
28 struct private_auth_payload_t
{
31 * Public auth_payload_t interface.
33 auth_payload_t
public;
38 u_int8_t next_payload
;
53 u_int8_t reserved_byte
[3];
56 * Length of this payload.
58 u_int16_t payload_length
;
61 * Method of the AUTH Data.
66 * The contained auth data value.
72 * Encoding rules to parse or generate a AUTH payload
74 * The defined offsets are the positions in a object of type
75 * private_auth_payload_t.
77 encoding_rule_t auth_payload_encodings
[] = {
78 /* 1 Byte next payload type, stored in the field next_payload */
79 { U_INT_8
, offsetof(private_auth_payload_t
, next_payload
) },
80 /* the critical bit */
81 { FLAG
, offsetof(private_auth_payload_t
, critical
) },
82 /* 7 Bit reserved bits */
83 { RESERVED_BIT
, offsetof(private_auth_payload_t
, reserved_bit
[0]) },
84 { RESERVED_BIT
, offsetof(private_auth_payload_t
, reserved_bit
[1]) },
85 { RESERVED_BIT
, offsetof(private_auth_payload_t
, reserved_bit
[2]) },
86 { RESERVED_BIT
, offsetof(private_auth_payload_t
, reserved_bit
[3]) },
87 { RESERVED_BIT
, offsetof(private_auth_payload_t
, reserved_bit
[4]) },
88 { RESERVED_BIT
, offsetof(private_auth_payload_t
, reserved_bit
[5]) },
89 { RESERVED_BIT
, offsetof(private_auth_payload_t
, reserved_bit
[6]) },
90 /* Length of the whole payload*/
91 { PAYLOAD_LENGTH
, offsetof(private_auth_payload_t
, payload_length
) },
93 { U_INT_8
, offsetof(private_auth_payload_t
, auth_method
) },
94 /* 3 reserved bytes */
95 { RESERVED_BYTE
, offsetof(private_auth_payload_t
, reserved_byte
[0]) },
96 { RESERVED_BYTE
, offsetof(private_auth_payload_t
, reserved_byte
[1]) },
97 { RESERVED_BYTE
, offsetof(private_auth_payload_t
, reserved_byte
[2]) },
98 /* some auth data bytes, length is defined in PAYLOAD_LENGTH */
99 { AUTH_DATA
, offsetof(private_auth_payload_t
, auth_data
) }
104 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
105 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
106 ! Next Payload !C! RESERVED ! Payload Length !
107 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 ! Auth Method ! RESERVED !
109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 ~ Authentication Data ~
113 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 METHOD(payload_t
, verify
, status_t
,
117 private_auth_payload_t
*this)
122 METHOD(payload_t
, get_encoding_rules
, void,
123 private_auth_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
125 *rules
= auth_payload_encodings
;
126 *rule_count
= countof(auth_payload_encodings
);
129 METHOD(payload_t
, get_type
, payload_type_t
,
130 private_auth_payload_t
*this)
132 return AUTHENTICATION
;
135 METHOD(payload_t
, get_next_type
, payload_type_t
,
136 private_auth_payload_t
*this)
138 return this->next_payload
;
141 METHOD(payload_t
, set_next_type
, void,
142 private_auth_payload_t
*this, payload_type_t type
)
144 this->next_payload
= type
;
147 METHOD(payload_t
, get_length
, size_t,
148 private_auth_payload_t
*this)
150 return this->payload_length
;
153 METHOD(auth_payload_t
, set_auth_method
, void,
154 private_auth_payload_t
*this, auth_method_t method
)
156 this->auth_method
= method
;
159 METHOD(auth_payload_t
, get_auth_method
, auth_method_t
,
160 private_auth_payload_t
*this)
162 return this->auth_method
;
165 METHOD(auth_payload_t
, set_data
, void,
166 private_auth_payload_t
*this, chunk_t data
)
168 free(this->auth_data
.ptr
);
169 this->auth_data
= chunk_clone(data
);
170 this->payload_length
= AUTH_PAYLOAD_HEADER_LENGTH
+ this->auth_data
.len
;
173 METHOD(auth_payload_t
, get_data
, chunk_t
,
174 private_auth_payload_t
*this)
176 return this->auth_data
;
179 METHOD2(payload_t
, auth_payload_t
, destroy
, void,
180 private_auth_payload_t
*this)
182 free(this->auth_data
.ptr
);
187 * Described in header
189 auth_payload_t
*auth_payload_create()
191 private_auth_payload_t
*this;
195 .payload_interface
= {
197 .get_encoding_rules
= _get_encoding_rules
,
198 .get_length
= _get_length
,
199 .get_next_type
= _get_next_type
,
200 .set_next_type
= _set_next_type
,
201 .get_type
= _get_type
,
204 .set_auth_method
= _set_auth_method
,
205 .get_auth_method
= _get_auth_method
,
206 .set_data
= _set_data
,
207 .get_data
= _get_data
,
210 .next_payload
= NO_PAYLOAD
,
211 .payload_length
= AUTH_PAYLOAD_HEADER_LENGTH
,
213 return &this->public;