baa838715414477b85e1fa6094c9e75cfdd44486
[strongswan.git] / src / libcharon / encoding / payloads / payload.c
1 /*
2 * Copyright (C) 2007 Tobias Brunner
3 * Copyright (C) 2005-2006 Martin Willi
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
19 #include "payload.h"
20
21 #include <encoding/payloads/ike_header.h>
22 #include <encoding/payloads/sa_payload.h>
23
24 #include <encoding/payloads/nonce_payload.h>
25 #include <encoding/payloads/id_payload.h>
26 #include <encoding/payloads/ke_payload.h>
27 #include <encoding/payloads/notify_payload.h>
28 #include <encoding/payloads/auth_payload.h>
29 #include <encoding/payloads/cert_payload.h>
30 #include <encoding/payloads/certreq_payload.h>
31 #include <encoding/payloads/encryption_payload.h>
32 #include <encoding/payloads/ts_payload.h>
33 #include <encoding/payloads/delete_payload.h>
34 #include <encoding/payloads/vendor_id_payload.h>
35 #include <encoding/payloads/cp_payload.h>
36 #include <encoding/payloads/configuration_attribute.h>
37 #include <encoding/payloads/eap_payload.h>
38 #include <encoding/payloads/hash_payload.h>
39 #include <encoding/payloads/unknown_payload.h>
40
41 ENUM_BEGIN(payload_type_names, NO_PAYLOAD, NO_PAYLOAD,
42 "NO_PAYLOAD");
43 ENUM_NEXT(payload_type_names, SECURITY_ASSOCIATION_V1, CONFIGURATION_V1, NO_PAYLOAD,
44 "SECURITY_ASSOCIATION_V1",
45 "PROPOSAL_V1",
46 "TRANSFORM_V1",
47 "KEY_EXCHANGE_V1",
48 "ID_V1",
49 "CERTIFICATE_V1",
50 "CERTIFICATE_REQUEST_V1",
51 "HASH_V1",
52 "SIGNATURE_V1",
53 "NONCE_V1",
54 "NOTIFY_V1",
55 "DELETE_V1",
56 "VENDOR_ID_V1",
57 "CONFIGURATION_V1");
58 ENUM_NEXT(payload_type_names, NAT_D_V1, NAT_OA_V1, CONFIGURATION_V1,
59 "NAT_D_V1",
60 "NAT_OA_V1");
61 ENUM_NEXT(payload_type_names, SECURITY_ASSOCIATION, EXTENSIBLE_AUTHENTICATION, NAT_OA_V1,
62 "SECURITY_ASSOCIATION",
63 "KEY_EXCHANGE",
64 "ID_INITIATOR",
65 "ID_RESPONDER",
66 "CERTIFICATE",
67 "CERTIFICATE_REQUEST",
68 "AUTHENTICATION",
69 "NONCE",
70 "NOTIFY",
71 "DELETE",
72 "VENDOR_ID",
73 "TRAFFIC_SELECTOR_INITIATOR",
74 "TRAFFIC_SELECTOR_RESPONDER",
75 "ENCRYPTED",
76 "CONFIGURATION",
77 "EXTENSIBLE_AUTHENTICATION");
78 #ifdef ME
79 ENUM_NEXT(payload_type_names, ID_PEER, ID_PEER, EXTENSIBLE_AUTHENTICATION,
80 "ID_PEER");
81 ENUM_NEXT(payload_type_names, HEADER, ENCRYPTED_V1, ID_PEER,
82 "HEADER",
83 "PROPOSAL_SUBSTRUCTURE",
84 "PROPOSAL_SUBSTRUCTURE_V1",
85 "TRANSFORM_SUBSTRUCTURE",
86 "TRANSFORM_SUBSTRUCTURE_V1",
87 "TRANSFORM_ATTRIBUTE",
88 "TRANSFORM_ATTRIBUTE_V1",
89 "TRAFFIC_SELECTOR_SUBSTRUCTURE",
90 "CONFIGURATION_ATTRIBUTE",
91 "CONFIGURATION_ATTRIBUTE_V1",
92 "ENCRYPTED_V1");
93 #else
94 ENUM_NEXT(payload_type_names, HEADER, ENCRYPTED_V1, EXTENSIBLE_AUTHENTICATION,
95 "HEADER",
96 "PROPOSAL_SUBSTRUCTURE",
97 "PROPOSAL_SUBSTRUCTURE_V1",
98 "TRANSFORM_SUBSTRUCTURE",
99 "TRANSFORM_SUBSTRUCTURE_V1",
100 "TRANSFORM_ATTRIBUTE",
101 "TRANSFORM_ATTRIBUTE_V1",
102 "TRAFFIC_SELECTOR_SUBSTRUCTURE",
103 "CONFIGURATION_ATTRIBUTE",
104 "CONFIGURATION_ATTRIBUTE_V1",
105 "ENCRYPTED_V1");
106 #endif /* ME */
107 ENUM_END(payload_type_names, ENCRYPTED_V1);
108
109 /* short forms of payload names */
110 ENUM_BEGIN(payload_type_short_names, NO_PAYLOAD, NO_PAYLOAD,
111 "--");
112 ENUM_NEXT(payload_type_short_names, SECURITY_ASSOCIATION_V1, CONFIGURATION_V1, NO_PAYLOAD,
113 "SA",
114 "PROP",
115 "TRANS",
116 "KE",
117 "ID",
118 "CERT",
119 "CERTREQ",
120 "HASH",
121 "SIG",
122 "No",
123 "N",
124 "D",
125 "V",
126 "CP");
127 ENUM_NEXT(payload_type_short_names, NAT_D_V1, NAT_OA_V1, CONFIGURATION_V1,
128 "NAT-D",
129 "NAT-OA");
130 ENUM_NEXT(payload_type_short_names, SECURITY_ASSOCIATION, EXTENSIBLE_AUTHENTICATION, NAT_OA_V1,
131 "SA",
132 "KE",
133 "IDi",
134 "IDr",
135 "CERT",
136 "CERTREQ",
137 "AUTH",
138 "No",
139 "N",
140 "D",
141 "V",
142 "TSi",
143 "TSr",
144 "E",
145 "CP",
146 "EAP");
147 #ifdef ME
148 ENUM_NEXT(payload_type_short_names, ID_PEER, ID_PEER, EXTENSIBLE_AUTHENTICATION,
149 "IDp");
150 ENUM_NEXT(payload_type_short_names, HEADER, ENCRYPTED_V1, ID_PEER,
151 "HDR",
152 "PROP",
153 "PROP",
154 "TRANS",
155 "TRANS",
156 "TRANSATTR",
157 "TRANSATTR",
158 "TSSUB",
159 "CATTR",
160 "CATTR",
161 "E");
162 #else
163 ENUM_NEXT(payload_type_short_names, HEADER, ENCRYPTED_V1, EXTENSIBLE_AUTHENTICATION,
164 "HDR",
165 "PROP",
166 "PROP",
167 "TRANS",
168 "TRANS",
169 "TRANSATTR",
170 "TRANSATTR",
171 "TSSUB",
172 "CATTR",
173 "CATTR",
174 "E");
175 #endif /* ME */
176 ENUM_END(payload_type_short_names, ENCRYPTED_V1);
177
178 /*
179 * see header
180 */
181 payload_t *payload_create(payload_type_t type)
182 {
183 switch (type)
184 {
185 case HEADER:
186 return (payload_t*)ike_header_create();
187 case SECURITY_ASSOCIATION:
188 case SECURITY_ASSOCIATION_V1:
189 return (payload_t*)sa_payload_create(type);
190 case PROPOSAL_SUBSTRUCTURE:
191 case PROPOSAL_SUBSTRUCTURE_V1:
192 return (payload_t*)proposal_substructure_create(type);
193 case TRANSFORM_SUBSTRUCTURE:
194 case TRANSFORM_SUBSTRUCTURE_V1:
195 return (payload_t*)transform_substructure_create(type);
196 case TRANSFORM_ATTRIBUTE:
197 case TRANSFORM_ATTRIBUTE_V1:
198 return (payload_t*)transform_attribute_create(type);
199 case NONCE:
200 case NONCE_V1:
201 return (payload_t*)nonce_payload_create(type);
202 case ID_INITIATOR:
203 case ID_RESPONDER:
204 case ID_V1:
205 case NAT_OA_V1:
206 #ifdef ME
207 case ID_PEER:
208 #endif /* ME */
209 return (payload_t*)id_payload_create(type);
210 case AUTHENTICATION:
211 return (payload_t*)auth_payload_create();
212 case CERTIFICATE:
213 case CERTIFICATE_V1:
214 return (payload_t*)cert_payload_create(type);
215 case CERTIFICATE_REQUEST:
216 return (payload_t*)certreq_payload_create();
217 case TRAFFIC_SELECTOR_SUBSTRUCTURE:
218 return (payload_t*)traffic_selector_substructure_create();
219 case TRAFFIC_SELECTOR_INITIATOR:
220 return (payload_t*)ts_payload_create(TRUE);
221 case TRAFFIC_SELECTOR_RESPONDER:
222 return (payload_t*)ts_payload_create(FALSE);
223 case KEY_EXCHANGE:
224 case KEY_EXCHANGE_V1:
225 return (payload_t*)ke_payload_create(type);
226 case NOTIFY:
227 case NOTIFY_V1:
228 return (payload_t*)notify_payload_create(type);
229 case DELETE:
230 case DELETE_V1:
231 return (payload_t*)delete_payload_create(type, 0);
232 case VENDOR_ID:
233 case VENDOR_ID_V1:
234 return (payload_t*)vendor_id_payload_create(type);
235 case HASH_V1:
236 case NAT_D_V1:
237 return (payload_t*)hash_payload_create(type);
238 case CONFIGURATION:
239 case CONFIGURATION_V1:
240 return (payload_t*)cp_payload_create(type);
241 case CONFIGURATION_ATTRIBUTE:
242 case CONFIGURATION_ATTRIBUTE_V1:
243 return (payload_t*)configuration_attribute_create(type);
244 case EXTENSIBLE_AUTHENTICATION:
245 return (payload_t*)eap_payload_create();
246 case ENCRYPTED:
247 case ENCRYPTED_V1:
248 return (payload_t*)encryption_payload_create(type);
249 default:
250 return (payload_t*)unknown_payload_create(type);
251 }
252 }
253
254 /**
255 * See header.
256 */
257 bool payload_is_known(payload_type_t type)
258 {
259 if (type == HEADER)
260 {
261 return TRUE;
262 }
263 if (type >= SECURITY_ASSOCIATION && type <= EXTENSIBLE_AUTHENTICATION)
264 {
265 return TRUE;
266 }
267 if (type >= SECURITY_ASSOCIATION_V1 && type <= CONFIGURATION_V1)
268 {
269 return TRUE;
270 }
271 if (type >= NAT_D_V1 && type <= NAT_OA_V1)
272 {
273 return TRUE;
274 }
275 #ifdef ME
276 if (type == ID_PEER)
277 {
278 return TRUE;
279 }
280 #endif
281 return FALSE;
282 }
283
284 /**
285 * See header.
286 */
287 void* payload_get_field(payload_t *payload, encoding_type_t type, u_int skip)
288 {
289 encoding_rule_t *rule;
290 int i, count;
291
292 count = payload->get_encoding_rules(payload, &rule);
293 for (i = 0; i < count; i++)
294 {
295 if (rule[i].type == type && skip-- == 0)
296 {
297 return ((char*)payload) + rule[i].offset;
298 }
299 }
300 return NULL;
301 }