Migrated auth_payload to INIT/METHOD macros
[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 * 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 METHOD(payload_t, verify, status_t,
107 private_auth_payload_t *this)
108 {
109 return SUCCESS;
110 }
111
112 METHOD(payload_t, get_encoding_rules, void,
113 private_auth_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
114 {
115 *rules = auth_payload_encodings;
116 *rule_count = countof(auth_payload_encodings);
117 }
118
119 METHOD(payload_t, get_type, payload_type_t,
120 private_auth_payload_t *this)
121 {
122 return AUTHENTICATION;
123 }
124
125 METHOD(payload_t, get_next_type, payload_type_t,
126 private_auth_payload_t *this)
127 {
128 return this->next_payload;
129 }
130
131 METHOD(payload_t, set_next_type, void,
132 private_auth_payload_t *this, payload_type_t type)
133 {
134 this->next_payload = type;
135 }
136
137 METHOD(payload_t, get_length, size_t,
138 private_auth_payload_t *this)
139 {
140 return this->payload_length;
141 }
142
143 METHOD(auth_payload_t, set_auth_method, void,
144 private_auth_payload_t *this, auth_method_t method)
145 {
146 this->auth_method = method;
147 }
148
149 METHOD(auth_payload_t, get_auth_method, auth_method_t,
150 private_auth_payload_t *this)
151 {
152 return this->auth_method;
153 }
154
155 METHOD(auth_payload_t, set_data, void,
156 private_auth_payload_t *this, chunk_t data)
157 {
158 free(this->auth_data.ptr);
159 this->auth_data = chunk_clone(data);
160 this->payload_length = AUTH_PAYLOAD_HEADER_LENGTH + this->auth_data.len;
161 }
162
163 METHOD(auth_payload_t, get_data, chunk_t,
164 private_auth_payload_t *this)
165 {
166 return this->auth_data;
167 }
168
169 METHOD2(payload_t, auth_payload_t, destroy, void,
170 private_auth_payload_t *this)
171 {
172 free(this->auth_data.ptr);
173 free(this);
174 }
175
176 /*
177 * Described in header
178 */
179 auth_payload_t *auth_payload_create()
180 {
181 private_auth_payload_t *this;
182
183 INIT(this,
184 .public = {
185 .payload_interface = {
186 .verify = _verify,
187 .get_encoding_rules = _get_encoding_rules,
188 .get_length = _get_length,
189 .get_next_type = _get_next_type,
190 .set_next_type = _set_next_type,
191 .get_type = _get_type,
192 .destroy = _destroy,
193 },
194 .set_auth_method = _set_auth_method,
195 .get_auth_method = _get_auth_method,
196 .set_data = _set_data,
197 .get_data = _get_data,
198 .destroy = _destroy,
199 },
200 .next_payload = NO_PAYLOAD,
201 .payload_length = AUTH_PAYLOAD_HEADER_LENGTH,
202 );
203 return &this->public;
204 }