Simplify signature of get_encoding_rules(), make all rules static
[strongswan.git] / src / libcharon / encoding / payloads / auth_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
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>.
11 *
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
15 * for more details.
16 */
17
18 #include "auth_payload.h"
19
20 #include <encoding/payloads/encodings.h>
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 * Reserved bits
47 */
48 bool reserved_bit[7];
49
50 /**
51 * Reserved bytes
52 */
53 u_int8_t reserved_byte[3];
54
55 /**
56 * Length of this payload.
57 */
58 u_int16_t payload_length;
59
60 /**
61 * Method of the AUTH Data.
62 */
63 u_int8_t auth_method;
64
65 /**
66 * The contained auth data value.
67 */
68 chunk_t auth_data;
69 };
70
71 /**
72 * Encoding rules to parse or generate a AUTH payload
73 *
74 * The defined offsets are the positions in a object of type
75 * private_auth_payload_t.
76 */
77 static encoding_rule_t 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) },
92 /* 1 Byte AUTH type*/
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) }
100 };
101
102 /*
103 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 ! !
111 ~ Authentication Data ~
112 ! !
113 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 */
115
116 METHOD(payload_t, verify, status_t,
117 private_auth_payload_t *this)
118 {
119 return SUCCESS;
120 }
121
122 METHOD(payload_t, get_encoding_rules, int,
123 private_auth_payload_t *this, encoding_rule_t **rules)
124 {
125 *rules = encodings;
126 return countof(encodings);
127 }
128
129 METHOD(payload_t, get_type, payload_type_t,
130 private_auth_payload_t *this)
131 {
132 return AUTHENTICATION;
133 }
134
135 METHOD(payload_t, get_next_type, payload_type_t,
136 private_auth_payload_t *this)
137 {
138 return this->next_payload;
139 }
140
141 METHOD(payload_t, set_next_type, void,
142 private_auth_payload_t *this, payload_type_t type)
143 {
144 this->next_payload = type;
145 }
146
147 METHOD(payload_t, get_length, size_t,
148 private_auth_payload_t *this)
149 {
150 return this->payload_length;
151 }
152
153 METHOD(auth_payload_t, set_auth_method, void,
154 private_auth_payload_t *this, auth_method_t method)
155 {
156 this->auth_method = method;
157 }
158
159 METHOD(auth_payload_t, get_auth_method, auth_method_t,
160 private_auth_payload_t *this)
161 {
162 return this->auth_method;
163 }
164
165 METHOD(auth_payload_t, set_data, void,
166 private_auth_payload_t *this, chunk_t data)
167 {
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;
171 }
172
173 METHOD(auth_payload_t, get_data, chunk_t,
174 private_auth_payload_t *this)
175 {
176 return this->auth_data;
177 }
178
179 METHOD2(payload_t, auth_payload_t, destroy, void,
180 private_auth_payload_t *this)
181 {
182 free(this->auth_data.ptr);
183 free(this);
184 }
185
186 /*
187 * Described in header
188 */
189 auth_payload_t *auth_payload_create()
190 {
191 private_auth_payload_t *this;
192
193 INIT(this,
194 .public = {
195 .payload_interface = {
196 .verify = _verify,
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,
202 .destroy = _destroy,
203 },
204 .set_auth_method = _set_auth_method,
205 .get_auth_method = _get_auth_method,
206 .set_data = _set_data,
207 .get_data = _get_data,
208 .destroy = _destroy,
209 },
210 .next_payload = NO_PAYLOAD,
211 .payload_length = AUTH_PAYLOAD_HEADER_LENGTH,
212 );
213 return &this->public;
214 }