Use vendor id payload for IKEv1 payloads, too
[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 "NOTIFICATION_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, CONFIGURATION_ATTRIBUTE, ID_PEER,
77 "HEADER",
78 "PROPOSAL_SUBSTRUCTURE",
79 "TRANSFORM_SUBSTRUCTURE",
80 "TRANSFORM_ATTRIBUTE",
81 "TRAFFIC_SELECTOR_SUBSTRUCTURE",
82 "CONFIGURATION_ATTRIBUTE");
83 #else
84 ENUM_NEXT(payload_type_names, HEADER, CONFIGURATION_ATTRIBUTE, EXTENSIBLE_AUTHENTICATION,
85 "HEADER",
86 "PROPOSAL_SUBSTRUCTURE",
87 "TRANSFORM_SUBSTRUCTURE",
88 "TRANSFORM_ATTRIBUTE",
89 "TRAFFIC_SELECTOR_SUBSTRUCTURE",
90 "CONFIGURATION_ATTRIBUTE");
91 #endif /* ME */
92 ENUM_END(payload_type_names, CONFIGURATION_ATTRIBUTE);
93
94 /* short forms of payload names */
95 ENUM_BEGIN(payload_type_short_names, NO_PAYLOAD, NO_PAYLOAD,
96 "--");
97 ENUM_NEXT(payload_type_short_names, SECURITY_ASSOCIATION_V1, VENDOR_ID_V1, NO_PAYLOAD,
98 "SA",
99 "PROP",
100 "TRANS",
101 "KE",
102 "ID",
103 "CERT",
104 "CERTREQ",
105 "HASH",
106 "SIG",
107 "No",
108 "NO",
109 "D",
110 "V");
111 ENUM_NEXT(payload_type_short_names, SECURITY_ASSOCIATION, EXTENSIBLE_AUTHENTICATION, VENDOR_ID_V1,
112 "SA",
113 "KE",
114 "IDi",
115 "IDr",
116 "CERT",
117 "CERTREQ",
118 "AUTH",
119 "No",
120 "N",
121 "D",
122 "V",
123 "TSi",
124 "TSr",
125 "E",
126 "CP",
127 "EAP");
128 #ifdef ME
129 ENUM_NEXT(payload_type_short_names, ID_PEER, ID_PEER, EXTENSIBLE_AUTHENTICATION,
130 "IDp");
131 ENUM_NEXT(payload_type_short_names, HEADER, CONFIGURATION_ATTRIBUTE, ID_PEER,
132 "HDR",
133 "PROP",
134 "TRANS",
135 "TRANSATTR",
136 "TSSUB",
137 "CPATTR");
138 #else
139 ENUM_NEXT(payload_type_short_names, HEADER, CONFIGURATION_ATTRIBUTE, EXTENSIBLE_AUTHENTICATION,
140 "HDR",
141 "PROP",
142 "TRANS",
143 "TRANSATTR",
144 "TSSUB",
145 "CPATTR");
146 #endif /* ME */
147 ENUM_END(payload_type_short_names, CONFIGURATION_ATTRIBUTE);
148
149 /*
150 * see header
151 */
152 payload_t *payload_create(payload_type_t type)
153 {
154 switch (type)
155 {
156 case HEADER:
157 return (payload_t*)ike_header_create();
158 case SECURITY_ASSOCIATION:
159 return (payload_t*)sa_payload_create();
160 case PROPOSAL_SUBSTRUCTURE:
161 return (payload_t*)proposal_substructure_create();
162 case TRANSFORM_SUBSTRUCTURE:
163 return (payload_t*)transform_substructure_create();
164 case TRANSFORM_ATTRIBUTE:
165 return (payload_t*)transform_attribute_create();
166 case NONCE:
167 return (payload_t*)nonce_payload_create();
168 case ID_INITIATOR:
169 return (payload_t*)id_payload_create(ID_INITIATOR);
170 case ID_RESPONDER:
171 return (payload_t*)id_payload_create(ID_RESPONDER);
172 #ifdef ME
173 case ID_PEER:
174 return (payload_t*)id_payload_create(ID_PEER);
175 #endif /* ME */
176 case AUTHENTICATION:
177 return (payload_t*)auth_payload_create();
178 case CERTIFICATE:
179 return (payload_t*)cert_payload_create();
180 case CERTIFICATE_REQUEST:
181 return (payload_t*)certreq_payload_create();
182 case TRAFFIC_SELECTOR_SUBSTRUCTURE:
183 return (payload_t*)traffic_selector_substructure_create();
184 case TRAFFIC_SELECTOR_INITIATOR:
185 return (payload_t*)ts_payload_create(TRUE);
186 case TRAFFIC_SELECTOR_RESPONDER:
187 return (payload_t*)ts_payload_create(FALSE);
188 case KEY_EXCHANGE:
189 return (payload_t*)ke_payload_create();
190 case NOTIFY:
191 return (payload_t*)notify_payload_create();
192 case DELETE:
193 return (payload_t*)delete_payload_create(0);
194 case VENDOR_ID:
195 case VENDOR_ID_V1:
196 return (payload_t*)vendor_id_payload_create(type);
197 case CONFIGURATION:
198 return (payload_t*)cp_payload_create();
199 case CONFIGURATION_ATTRIBUTE:
200 return (payload_t*)configuration_attribute_create();
201 case EXTENSIBLE_AUTHENTICATION:
202 return (payload_t*)eap_payload_create();
203 case ENCRYPTED:
204 return (payload_t*)encryption_payload_create();
205 default:
206 return (payload_t*)unknown_payload_create(type);
207 }
208 }
209
210 /**
211 * See header.
212 */
213 bool payload_is_known(payload_type_t type)
214 {
215 if (type == HEADER)
216 {
217 return TRUE;
218 }
219 if (type >= SECURITY_ASSOCIATION && type <= EXTENSIBLE_AUTHENTICATION)
220 {
221 return TRUE;
222 }
223 if (type >= SECURITY_ASSOCIATION_V1 && type <= VENDOR_ID_V1)
224 {
225 return TRUE;
226 }
227 #ifdef ME
228 if (type == ID_PEER)
229 {
230 return TRUE;
231 }
232 #endif
233 return FALSE;
234 }
235
236 /**
237 * See header.
238 */
239 void* payload_get_field(payload_t *payload, encoding_type_t type, u_int skip)
240 {
241 encoding_rule_t *rule;
242 size_t count;
243 int i;
244
245 payload->get_encoding_rules(payload, &rule, &count);
246 for (i = 0; i < count; i++)
247 {
248 if (rule[i].type == type && skip-- == 0)
249 {
250 return ((char*)payload) + rule[i].offset;
251 }
252 }
253 return NULL;
254 }