2ff2b55e9ce08e6b35c84d0c49a770d491483eb3
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / isakmp_vendor.c
1 /*
2 * Copyright (C) 2012-2013 Tobias Brunner
3 * Copyright (C) 2009 Martin Willi
4 * Hochschule fuer Technik Rapperswil
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 */
16
17 /*
18 * Copyright (C) 2012 Volker RĂ¼melin
19 *
20 * Permission is hereby granted, free of charge, to any person obtaining a copy
21 * of this software and associated documentation files (the "Software"), to deal
22 * in the Software without restriction, including without limitation the rights
23 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24 * copies of the Software, and to permit persons to whom the Software is
25 * furnished to do so, subject to the following conditions:
26 *
27 * The above copyright notice and this permission notice shall be included in
28 * all copies or substantial portions of the Software.
29 *
30 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
36 * THE SOFTWARE.
37 */
38
39 #include "isakmp_vendor.h"
40
41 #include <daemon.h>
42 #include <encoding/payloads/vendor_id_payload.h>
43
44 typedef struct private_isakmp_vendor_t private_isakmp_vendor_t;
45
46 /**
47 * Private data of an isakmp_vendor_t object.
48 */
49 struct private_isakmp_vendor_t {
50
51 /**
52 * Public isakmp_vendor_t interface.
53 */
54 isakmp_vendor_t public;
55
56 /**
57 * Associated IKE_SA
58 */
59 ike_sa_t *ike_sa;
60
61 /**
62 * Are we the inititator of this task
63 */
64 bool initiator;
65
66 /**
67 * Index of best nat traversal VID found
68 */
69 int best_natt_ext;
70 };
71
72 /**
73 * IKEv1 Vendor ID database
74 */
75 static struct {
76 /* Description */
77 char *desc;
78 /* extension flag negotiated with vendor ID, if any */
79 ike_extension_t extension;
80 /* send yourself? */
81 bool send;
82 /* length of vendor ID string */
83 int len;
84 /* vendor ID string */
85 char *id;
86 } vendor_ids[] = {
87
88 /* strongSwan MD5("strongSwan") */
89 { "strongSwan", EXT_STRONGSWAN, FALSE, 16,
90 "\x88\x2f\xe5\x6d\x6f\xd2\x0d\xbc\x22\x51\x61\x3b\x2e\xbe\x5b\xeb"},
91
92 /* XAuth, MD5("draft-ietf-ipsra-isakmp-xauth-06.txt") */
93 { "XAuth", EXT_XAUTH, TRUE, 8,
94 "\x09\x00\x26\x89\xdf\xd6\xb7\x12"},
95
96 /* Dead peer detection, RFC 3706 */
97 { "DPD", EXT_DPD, TRUE, 16,
98 "\xaf\xca\xd7\x13\x68\xa1\xf1\xc9\x6b\x86\x96\xfc\x77\x57\x01\x00"},
99
100 { "Cisco Unity", EXT_CISCO_UNITY, FALSE, 16,
101 "\x12\xf5\xf2\x8c\x45\x71\x68\xa9\x70\x2d\x9f\xe2\x74\xcc\x01\x00"},
102
103 /* Proprietary IKE fragmentation extension. Capabilities are handled
104 * specially on receipt of this VID. */
105 { "FRAGMENTATION", EXT_IKE_FRAGMENTATION, FALSE, 20,
106 "\x40\x48\xb7\xd5\x6e\xbc\xe8\x85\x25\xe7\xde\x7f\x00\xd6\xc2\xd3\x80\x00\x00\x00"},
107
108 }, vendor_natt_ids[] = {
109
110 /* NAT-Traversal VIDs ordered by preference */
111
112 /* NAT-Traversal, MD5("RFC 3947") */
113 { "NAT-T (RFC 3947)", EXT_NATT, TRUE, 16,
114 "\x4a\x13\x1c\x81\x07\x03\x58\x45\x5c\x57\x28\xf2\x0e\x95\x45\x2f"},
115
116 { "draft-ietf-ipsec-nat-t-ike-03", EXT_NATT | EXT_NATT_DRAFT_02_03,
117 FALSE, 16,
118 "\x7d\x94\x19\xa6\x53\x10\xca\x6f\x2c\x17\x9d\x92\x15\x52\x9d\x56"},
119
120 { "draft-ietf-ipsec-nat-t-ike-02", EXT_NATT | EXT_NATT_DRAFT_02_03,
121 FALSE, 16,
122 "\xcd\x60\x46\x43\x35\xdf\x21\xf8\x7c\xfd\xb2\xfc\x68\xb6\xa4\x48"},
123
124 { "draft-ietf-ipsec-nat-t-ike-02\\n", EXT_NATT | EXT_NATT_DRAFT_02_03,
125 TRUE, 16,
126 "\x90\xcb\x80\x91\x3e\xbb\x69\x6e\x08\x63\x81\xb5\xec\x42\x7b\x1f"},
127
128 { "draft-ietf-ipsec-nat-t-ike-08", 0, FALSE, 16,
129 "\x8f\x8d\x83\x82\x6d\x24\x6b\x6f\xc7\xa8\xa6\xa4\x28\xc1\x1d\xe8"},
130
131 { "draft-ietf-ipsec-nat-t-ike-07", 0, FALSE, 16,
132 "\x43\x9b\x59\xf8\xba\x67\x6c\x4c\x77\x37\xae\x22\xea\xb8\xf5\x82"},
133
134 { "draft-ietf-ipsec-nat-t-ike-06", 0, FALSE, 16,
135 "\x4d\x1e\x0e\x13\x6d\xea\xfa\x34\xc4\xf3\xea\x9f\x02\xec\x72\x85"},
136
137 { "draft-ietf-ipsec-nat-t-ike-05", 0, FALSE, 16,
138 "\x80\xd0\xbb\x3d\xef\x54\x56\x5e\xe8\x46\x45\xd4\xc8\x5c\xe3\xee"},
139
140 { "draft-ietf-ipsec-nat-t-ike-04", 0, FALSE, 16,
141 "\x99\x09\xb6\x4e\xed\x93\x7c\x65\x73\xde\x52\xac\xe9\x52\xfa\x6b"},
142
143 { "draft-ietf-ipsec-nat-t-ike-00", 0, FALSE, 16,
144 "\x44\x85\x15\x2d\x18\xb6\xbb\xcd\x0b\xe8\xa8\x46\x95\x79\xdd\xcc"},
145
146 { "draft-ietf-ipsec-nat-t-ike", 0, FALSE, 16,
147 "\x4d\xf3\x79\x28\xe9\xfc\x4f\xd1\xb3\x26\x21\x70\xd5\x15\xc6\x62"},
148
149 { "draft-stenberg-ipsec-nat-traversal-02", 0, FALSE, 16,
150 "\x61\x05\xc4\x22\xe7\x68\x47\xe4\x3f\x96\x84\x80\x12\x92\xae\xcd"},
151
152 { "draft-stenberg-ipsec-nat-traversal-01", 0, FALSE, 16,
153 "\x27\xba\xb5\xdc\x01\xea\x07\x60\xea\x4e\x31\x90\xac\x27\xc0\xd0"},
154
155 };
156
157 /**
158 * According to racoon 0x80000000 seems to indicate support for fragmentation
159 * of Aggressive and Main mode messages. 0x40000000 seems to indicate support
160 * for fragmentation of base ISAKMP messages (Cisco adds that and thus sends
161 * 0xc0000000)
162 */
163 static const u_int32_t fragmentation_ike = 0x80000000;
164
165 /**
166 * Check if the given vendor ID indicate support for fragmentation
167 */
168 static bool fragmentation_supported(chunk_t data, int i)
169 {
170 if (vendor_ids[i].extension == EXT_IKE_FRAGMENTATION &&
171 data.len == 20 && memeq(data.ptr, vendor_ids[i].id, 16))
172 {
173 return untoh32(&data.ptr[16]) & fragmentation_ike;
174 }
175 return FALSE;
176 }
177
178 METHOD(task_t, build, status_t,
179 private_isakmp_vendor_t *this, message_t *message)
180 {
181 vendor_id_payload_t *vid_payload;
182 bool strongswan, cisco_unity, fragmentation;
183 ike_cfg_t *ike_cfg;
184 int i;
185
186 strongswan = lib->settings->get_bool(lib->settings,
187 "%s.send_vendor_id", FALSE, charon->name);
188 cisco_unity = lib->settings->get_bool(lib->settings,
189 "%s.cisco_unity", FALSE, charon->name);
190 ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
191 fragmentation = ike_cfg->fragmentation(ike_cfg) != FRAGMENTATION_NO;
192 if (!this->initiator && fragmentation)
193 {
194 fragmentation = this->ike_sa->supports_extension(this->ike_sa,
195 EXT_IKE_FRAGMENTATION);
196 }
197 for (i = 0; i < countof(vendor_ids); i++)
198 {
199 if (vendor_ids[i].send ||
200 (vendor_ids[i].extension == EXT_STRONGSWAN && strongswan) ||
201 (vendor_ids[i].extension == EXT_CISCO_UNITY && cisco_unity) ||
202 (vendor_ids[i].extension == EXT_IKE_FRAGMENTATION && fragmentation))
203 {
204 DBG2(DBG_IKE, "sending %s vendor ID", vendor_ids[i].desc);
205 vid_payload = vendor_id_payload_create_data(VENDOR_ID_V1,
206 chunk_clone(chunk_create(vendor_ids[i].id, vendor_ids[i].len)));
207 message->add_payload(message, &vid_payload->payload_interface);
208 }
209 }
210 for (i = 0; i < countof(vendor_natt_ids); i++)
211 {
212 if ((this->initiator && vendor_natt_ids[i].send) ||
213 this->best_natt_ext == i)
214 {
215 DBG2(DBG_IKE, "sending %s vendor ID", vendor_natt_ids[i].desc);
216 vid_payload = vendor_id_payload_create_data(VENDOR_ID_V1,
217 chunk_clone(chunk_create(vendor_natt_ids[i].id,
218 vendor_natt_ids[i].len)));
219 message->add_payload(message, &vid_payload->payload_interface);
220 }
221 }
222 return this->initiator ? NEED_MORE : SUCCESS;
223 }
224
225 METHOD(task_t, process, status_t,
226 private_isakmp_vendor_t *this, message_t *message)
227 {
228 enumerator_t *enumerator;
229 payload_t *payload;
230 int i;
231
232 enumerator = message->create_payload_enumerator(message);
233 while (enumerator->enumerate(enumerator, &payload))
234 {
235 if (payload->get_type(payload) == VENDOR_ID_V1)
236 {
237 vendor_id_payload_t *vid;
238 bool found = FALSE;
239 chunk_t data;
240
241 vid = (vendor_id_payload_t*)payload;
242 data = vid->get_data(vid);
243
244 for (i = 0; i < countof(vendor_ids); i++)
245 {
246 if (chunk_equals(data, chunk_create(vendor_ids[i].id,
247 vendor_ids[i].len)) ||
248 fragmentation_supported(data, i))
249 {
250 DBG1(DBG_IKE, "received %s vendor ID", vendor_ids[i].desc);
251 if (vendor_ids[i].extension)
252 {
253 this->ike_sa->enable_extension(this->ike_sa,
254 vendor_ids[i].extension);
255 }
256 found = TRUE;
257 break;
258 }
259 }
260 if (!found)
261 {
262 for (i = 0; i < countof(vendor_natt_ids); i++)
263 {
264 if (chunk_equals(data, chunk_create(vendor_natt_ids[i].id,
265 vendor_natt_ids[i].len)))
266 {
267 DBG1(DBG_IKE, "received %s vendor ID",
268 vendor_natt_ids[i].desc);
269 if (vendor_natt_ids[i].extension &&
270 (i < this->best_natt_ext || this->best_natt_ext < 0))
271 {
272 this->best_natt_ext = i;
273 }
274 found = TRUE;
275 break;
276 }
277 }
278 }
279 if (!found)
280 {
281 DBG1(DBG_ENC, "received unknown vendor ID: %#B", &data);
282 }
283 }
284 }
285 enumerator->destroy(enumerator);
286
287 if (this->best_natt_ext >= 0)
288 {
289 this->ike_sa->enable_extension(this->ike_sa,
290 vendor_natt_ids[this->best_natt_ext].extension);
291 }
292
293 return this->initiator ? SUCCESS : NEED_MORE;
294 }
295
296 METHOD(task_t, migrate, void,
297 private_isakmp_vendor_t *this, ike_sa_t *ike_sa)
298 {
299 this->ike_sa = ike_sa;
300 }
301
302 METHOD(task_t, get_type, task_type_t,
303 private_isakmp_vendor_t *this)
304 {
305 return TASK_ISAKMP_VENDOR;
306 }
307
308 METHOD(task_t, destroy, void,
309 private_isakmp_vendor_t *this)
310 {
311 free(this);
312 }
313
314 /**
315 * See header
316 */
317 isakmp_vendor_t *isakmp_vendor_create(ike_sa_t *ike_sa, bool initiator)
318 {
319 private_isakmp_vendor_t *this;
320
321 INIT(this,
322 .public = {
323 .task = {
324 .build = _build,
325 .process = _process,
326 .migrate = _migrate,
327 .get_type = _get_type,
328 .destroy = _destroy,
329 },
330 },
331 .initiator = initiator,
332 .ike_sa = ike_sa,
333 .best_natt_ext = -1,
334 );
335
336 return &this->public;
337 }