/src/strongswan/src/libcharon/encoding/payloads/auth_payload.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2005-2010 Martin Willi |
3 | | * Copyright (C) 2005 Jan Hutter |
4 | | * |
5 | | * Copyright (C) secunet Security Networks AG |
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 | | uint8_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 | | uint8_t reserved_byte[3]; |
54 | | |
55 | | /** |
56 | | * Length of this payload. |
57 | | */ |
58 | | uint16_t payload_length; |
59 | | |
60 | | /** |
61 | | * Method of the AUTH Data. |
62 | | */ |
63 | | uint8_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 | | { CHUNK_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 | 448 | { |
119 | 448 | return SUCCESS; |
120 | 448 | } |
121 | | |
122 | | METHOD(payload_t, get_encoding_rules, int, |
123 | | private_auth_payload_t *this, encoding_rule_t **rules) |
124 | 471 | { |
125 | 471 | *rules = encodings; |
126 | 471 | return countof(encodings); |
127 | 471 | } |
128 | | |
129 | | METHOD(payload_t, get_header_length, int, |
130 | | private_auth_payload_t *this) |
131 | 7.25k | { |
132 | 7.25k | return 8; |
133 | 7.25k | } |
134 | | |
135 | | METHOD(payload_t, get_type, payload_type_t, |
136 | | private_auth_payload_t *this) |
137 | 664 | { |
138 | 664 | return PLV2_AUTH; |
139 | 664 | } |
140 | | |
141 | | METHOD(payload_t, get_next_type, payload_type_t, |
142 | | private_auth_payload_t *this) |
143 | 448 | { |
144 | 448 | return this->next_payload; |
145 | 448 | } |
146 | | |
147 | | METHOD(payload_t, set_next_type, void, |
148 | | private_auth_payload_t *this, payload_type_t type) |
149 | 0 | { |
150 | 0 | this->next_payload = type; |
151 | 0 | } |
152 | | |
153 | | METHOD(payload_t, get_length, size_t, |
154 | | private_auth_payload_t *this) |
155 | 0 | { |
156 | 0 | return this->payload_length; |
157 | 0 | } |
158 | | |
159 | | METHOD(auth_payload_t, set_auth_method, void, |
160 | | private_auth_payload_t *this, auth_method_t method) |
161 | 0 | { |
162 | 0 | this->auth_method = method; |
163 | 0 | } |
164 | | |
165 | | METHOD(auth_payload_t, get_auth_method, auth_method_t, |
166 | | private_auth_payload_t *this) |
167 | 0 | { |
168 | 0 | return this->auth_method; |
169 | 0 | } |
170 | | |
171 | | METHOD(auth_payload_t, set_data, void, |
172 | | private_auth_payload_t *this, chunk_t data) |
173 | 0 | { |
174 | 0 | free(this->auth_data.ptr); |
175 | 0 | this->auth_data = chunk_clone(data); |
176 | 0 | this->payload_length = get_header_length(this) + this->auth_data.len; |
177 | 0 | } |
178 | | |
179 | | METHOD(auth_payload_t, get_data, chunk_t, |
180 | | private_auth_payload_t *this) |
181 | 0 | { |
182 | 0 | return this->auth_data; |
183 | 0 | } |
184 | | |
185 | | METHOD2(payload_t, auth_payload_t, destroy, void, |
186 | | private_auth_payload_t *this) |
187 | 471 | { |
188 | 471 | free(this->auth_data.ptr); |
189 | 471 | free(this); |
190 | 471 | } |
191 | | |
192 | | /* |
193 | | * Described in header |
194 | | */ |
195 | | auth_payload_t *auth_payload_create() |
196 | 471 | { |
197 | 471 | private_auth_payload_t *this; |
198 | | |
199 | 471 | INIT(this, |
200 | 471 | .public = { |
201 | 471 | .payload_interface = { |
202 | 471 | .verify = _verify, |
203 | 471 | .get_encoding_rules = _get_encoding_rules, |
204 | 471 | .get_header_length = _get_header_length, |
205 | 471 | .get_length = _get_length, |
206 | 471 | .get_next_type = _get_next_type, |
207 | 471 | .set_next_type = _set_next_type, |
208 | 471 | .get_type = _get_type, |
209 | 471 | .destroy = _destroy, |
210 | 471 | }, |
211 | 471 | .set_auth_method = _set_auth_method, |
212 | 471 | .get_auth_method = _get_auth_method, |
213 | 471 | .set_data = _set_data, |
214 | 471 | .get_data = _get_data, |
215 | 471 | .destroy = _destroy, |
216 | 471 | }, |
217 | 471 | .next_payload = PL_NONE, |
218 | 471 | .payload_length = get_header_length(this), |
219 | 471 | ); |
220 | 471 | return &this->public; |
221 | 471 | } |