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