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