Extended nonce payload for IKEv1 support
[strongswan.git] / src / libcharon / sa / tasks / ike_init.c
1 /*
2 * Copyright (C) 2008-2009 Tobias Brunner
3 * Copyright (C) 2005-2008 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 #include "ike_init.h"
19
20 #include <string.h>
21
22 #include <daemon.h>
23 #include <crypto/diffie_hellman.h>
24 #include <encoding/payloads/sa_payload.h>
25 #include <encoding/payloads/ke_payload.h>
26 #include <encoding/payloads/nonce_payload.h>
27
28 /** maximum retries to do with cookies/other dh groups */
29 #define MAX_RETRIES 5
30
31 typedef struct private_ike_init_t private_ike_init_t;
32
33 /**
34 * Private members of a ike_init_t task.
35 */
36 struct private_ike_init_t {
37
38 /**
39 * Public methods and task_t interface.
40 */
41 ike_init_t public;
42
43 /**
44 * Assigned IKE_SA.
45 */
46 ike_sa_t *ike_sa;
47
48 /**
49 * Are we the initiator?
50 */
51 bool initiator;
52
53 /**
54 * IKE config to establish
55 */
56 ike_cfg_t *config;
57
58 /**
59 * diffie hellman group to use
60 */
61 diffie_hellman_group_t dh_group;
62
63 /**
64 * diffie hellman key exchange
65 */
66 diffie_hellman_t *dh;
67
68 /**
69 * Keymat derivation (from IKE_SA)
70 */
71 keymat_t *keymat;
72
73 /**
74 * nonce chosen by us
75 */
76 chunk_t my_nonce;
77
78 /**
79 * nonce chosen by peer
80 */
81 chunk_t other_nonce;
82
83 /**
84 * Negotiated proposal used for IKE_SA
85 */
86 proposal_t *proposal;
87
88 /**
89 * Old IKE_SA which gets rekeyed
90 */
91 ike_sa_t *old_sa;
92
93 /**
94 * cookie received from responder
95 */
96 chunk_t cookie;
97
98 /**
99 * retries done so far after failure (cookie or bad dh group)
100 */
101 u_int retry;
102 };
103
104 /**
105 * build the payloads for the message
106 */
107 static void build_payloads(private_ike_init_t *this, message_t *message)
108 {
109 sa_payload_t *sa_payload;
110 ke_payload_t *ke_payload;
111 nonce_payload_t *nonce_payload;
112 linked_list_t *proposal_list;
113 ike_sa_id_t *id;
114 proposal_t *proposal;
115 enumerator_t *enumerator;
116
117 id = this->ike_sa->get_id(this->ike_sa);
118
119 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
120
121 if (this->initiator)
122 {
123 proposal_list = this->config->get_proposals(this->config);
124 if (this->old_sa)
125 {
126 /* include SPI of new IKE_SA when we are rekeying */
127 enumerator = proposal_list->create_enumerator(proposal_list);
128 while (enumerator->enumerate(enumerator, (void**)&proposal))
129 {
130 proposal->set_spi(proposal, id->get_initiator_spi(id));
131 }
132 enumerator->destroy(enumerator);
133 }
134
135 sa_payload = sa_payload_create_from_proposal_list(SECURITY_ASSOCIATION,
136 proposal_list);
137 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
138 }
139 else
140 {
141 if (this->old_sa)
142 {
143 /* include SPI of new IKE_SA when we are rekeying */
144 this->proposal->set_spi(this->proposal, id->get_responder_spi(id));
145 }
146 sa_payload = sa_payload_create_from_proposal(SECURITY_ASSOCIATION,
147 this->proposal);
148 }
149 message->add_payload(message, (payload_t*)sa_payload);
150
151 nonce_payload = nonce_payload_create(NONCE);
152 nonce_payload->set_nonce(nonce_payload, this->my_nonce);
153 ke_payload = ke_payload_create_from_diffie_hellman(this->dh);
154
155 if (this->old_sa)
156 { /* payload order differs if we are rekeying */
157 message->add_payload(message, (payload_t*)nonce_payload);
158 message->add_payload(message, (payload_t*)ke_payload);
159 }
160 else
161 {
162 message->add_payload(message, (payload_t*)ke_payload);
163 message->add_payload(message, (payload_t*)nonce_payload);
164 }
165 }
166
167 /**
168 * Read payloads from message
169 */
170 static void process_payloads(private_ike_init_t *this, message_t *message)
171 {
172 enumerator_t *enumerator;
173 payload_t *payload;
174
175 enumerator = message->create_payload_enumerator(message);
176 while (enumerator->enumerate(enumerator, &payload))
177 {
178 switch (payload->get_type(payload))
179 {
180 case SECURITY_ASSOCIATION:
181 {
182 sa_payload_t *sa_payload = (sa_payload_t*)payload;
183 linked_list_t *proposal_list;
184 bool private;
185
186 proposal_list = sa_payload->get_proposals(sa_payload);
187 private = this->ike_sa->supports_extension(this->ike_sa,
188 EXT_STRONGSWAN);
189 this->proposal = this->config->select_proposal(this->config,
190 proposal_list, private);
191 proposal_list->destroy_offset(proposal_list,
192 offsetof(proposal_t, destroy));
193 break;
194 }
195 case KEY_EXCHANGE:
196 {
197 ke_payload_t *ke_payload = (ke_payload_t*)payload;
198
199 this->dh_group = ke_payload->get_dh_group_number(ke_payload);
200 if (!this->initiator)
201 {
202 this->dh = this->keymat->create_dh(this->keymat,
203 this->dh_group);
204 }
205 if (this->dh)
206 {
207 this->dh->set_other_public_value(this->dh,
208 ke_payload->get_key_exchange_data(ke_payload));
209 }
210 break;
211 }
212 case NONCE:
213 {
214 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
215
216 this->other_nonce = nonce_payload->get_nonce(nonce_payload);
217 break;
218 }
219 default:
220 break;
221 }
222 }
223 enumerator->destroy(enumerator);
224 }
225
226 METHOD(task_t, build_i, status_t,
227 private_ike_init_t *this, message_t *message)
228 {
229 rng_t *rng;
230
231 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
232 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
233 this->ike_sa->get_name(this->ike_sa),
234 this->ike_sa->get_unique_id(this->ike_sa),
235 this->ike_sa->get_other_host(this->ike_sa));
236 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
237
238 if (this->retry >= MAX_RETRIES)
239 {
240 DBG1(DBG_IKE, "giving up after %d retries", MAX_RETRIES);
241 return FAILED;
242 }
243
244 /* if the DH group is set via use_dh_group(), we already have a DH object */
245 if (!this->dh)
246 {
247 this->dh_group = this->config->get_dh_group(this->config);
248 this->dh = this->keymat->create_dh(this->keymat, this->dh_group);
249 if (!this->dh)
250 {
251 DBG1(DBG_IKE, "configured DH group %N not supported",
252 diffie_hellman_group_names, this->dh_group);
253 return FAILED;
254 }
255 }
256
257 /* generate nonce only when we are trying the first time */
258 if (this->my_nonce.ptr == NULL)
259 {
260 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
261 if (!rng)
262 {
263 DBG1(DBG_IKE, "error generating nonce");
264 return FAILED;
265 }
266 rng->allocate_bytes(rng, NONCE_SIZE, &this->my_nonce);
267 rng->destroy(rng);
268 }
269
270 if (this->cookie.ptr)
271 {
272 message->add_notify(message, FALSE, COOKIE, this->cookie);
273 }
274
275 build_payloads(this, message);
276
277 #ifdef ME
278 {
279 chunk_t connect_id = this->ike_sa->get_connect_id(this->ike_sa);
280 if (connect_id.ptr)
281 {
282 message->add_notify(message, FALSE, ME_CONNECTID, connect_id);
283 }
284 }
285 #endif /* ME */
286
287 return NEED_MORE;
288 }
289
290 METHOD(task_t, process_r, status_t,
291 private_ike_init_t *this, message_t *message)
292 {
293 rng_t *rng;
294
295 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
296 DBG0(DBG_IKE, "%H is initiating an IKE_SA", message->get_source(message));
297 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
298
299 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
300 if (!rng)
301 {
302 DBG1(DBG_IKE, "error generating nonce");
303 return FAILED;
304 }
305 rng->allocate_bytes(rng, NONCE_SIZE, &this->my_nonce);
306 rng->destroy(rng);
307
308 #ifdef ME
309 {
310 notify_payload_t *notify = message->get_notify(message, ME_CONNECTID);
311 if (notify)
312 {
313 chunk_t connect_id = notify->get_notification_data(notify);
314 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &connect_id);
315 charon->connect_manager->stop_checks(charon->connect_manager,
316 connect_id);
317 }
318 }
319 #endif /* ME */
320
321 process_payloads(this, message);
322
323 return NEED_MORE;
324 }
325
326 /**
327 * Derive the keymat for the IKE_SA
328 */
329 static bool derive_keys(private_ike_init_t *this,
330 chunk_t nonce_i, chunk_t nonce_r)
331 {
332 keymat_t *old_keymat;
333 pseudo_random_function_t prf_alg = PRF_UNDEFINED;
334 chunk_t skd = chunk_empty;
335 ike_sa_id_t *id;
336
337 id = this->ike_sa->get_id(this->ike_sa);
338 if (this->old_sa)
339 {
340 /* rekeying: Include old SKd, use old PRF, apply SPI */
341 old_keymat = this->old_sa->get_keymat(this->old_sa);
342 prf_alg = old_keymat->get_skd(old_keymat, &skd);
343 if (this->initiator)
344 {
345 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
346 }
347 else
348 {
349 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
350 }
351 }
352 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
353 nonce_i, nonce_r, id, prf_alg, skd))
354 {
355 return FALSE;
356 }
357 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh,
358 nonce_i, nonce_r, this->old_sa);
359 return TRUE;
360 }
361
362 METHOD(task_t, build_r, status_t,
363 private_ike_init_t *this, message_t *message)
364 {
365 /* check if we have everything we need */
366 if (this->proposal == NULL ||
367 this->other_nonce.len == 0 || this->my_nonce.len == 0)
368 {
369 DBG1(DBG_IKE, "received proposals inacceptable");
370 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
371 return FAILED;
372 }
373 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
374
375 if (this->dh == NULL ||
376 !this->proposal->has_dh_group(this->proposal, this->dh_group))
377 {
378 u_int16_t group;
379
380 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
381 &group, NULL))
382 {
383 DBG1(DBG_IKE, "DH group %N inacceptable, requesting %N",
384 diffie_hellman_group_names, this->dh_group,
385 diffie_hellman_group_names, group);
386 this->dh_group = group;
387 group = htons(group);
388 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
389 chunk_from_thing(group));
390 }
391 else
392 {
393 DBG1(DBG_IKE, "no acceptable proposal found");
394 }
395 return FAILED;
396 }
397
398 if (!derive_keys(this, this->other_nonce, this->my_nonce))
399 {
400 DBG1(DBG_IKE, "key derivation failed");
401 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
402 return FAILED;
403 }
404 build_payloads(this, message);
405 return SUCCESS;
406 }
407
408 METHOD(task_t, process_i, status_t,
409 private_ike_init_t *this, message_t *message)
410 {
411 enumerator_t *enumerator;
412 payload_t *payload;
413
414 /* check for erronous notifies */
415 enumerator = message->create_payload_enumerator(message);
416 while (enumerator->enumerate(enumerator, &payload))
417 {
418 if (payload->get_type(payload) == NOTIFY)
419 {
420 notify_payload_t *notify = (notify_payload_t*)payload;
421 notify_type_t type = notify->get_notify_type(notify);
422
423 switch (type)
424 {
425 case INVALID_KE_PAYLOAD:
426 {
427 chunk_t data;
428 diffie_hellman_group_t bad_group;
429
430 bad_group = this->dh_group;
431 data = notify->get_notification_data(notify);
432 this->dh_group = ntohs(*((u_int16_t*)data.ptr));
433 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
434 "it requested %N", diffie_hellman_group_names,
435 bad_group, diffie_hellman_group_names, this->dh_group);
436
437 if (this->old_sa == NULL)
438 { /* reset the IKE_SA if we are not rekeying */
439 this->ike_sa->reset(this->ike_sa);
440 }
441
442 enumerator->destroy(enumerator);
443 this->retry++;
444 return NEED_MORE;
445 }
446 case NAT_DETECTION_SOURCE_IP:
447 case NAT_DETECTION_DESTINATION_IP:
448 /* skip, handled in ike_natd_t */
449 break;
450 case MULTIPLE_AUTH_SUPPORTED:
451 /* handled in ike_auth_t */
452 break;
453 case COOKIE:
454 {
455 chunk_free(&this->cookie);
456 this->cookie = chunk_clone(notify->get_notification_data(notify));
457 this->ike_sa->reset(this->ike_sa);
458 enumerator->destroy(enumerator);
459 DBG2(DBG_IKE, "received %N notify", notify_type_names, type);
460 this->retry++;
461 return NEED_MORE;
462 }
463 default:
464 {
465 if (type <= 16383)
466 {
467 DBG1(DBG_IKE, "received %N notify error",
468 notify_type_names, type);
469 enumerator->destroy(enumerator);
470 return FAILED;
471 }
472 DBG2(DBG_IKE, "received %N notify",
473 notify_type_names, type);
474 break;
475 }
476 }
477 }
478 }
479 enumerator->destroy(enumerator);
480
481 process_payloads(this, message);
482
483 /* check if we have everything */
484 if (this->proposal == NULL ||
485 this->other_nonce.len == 0 || this->my_nonce.len == 0)
486 {
487 DBG1(DBG_IKE, "peers proposal selection invalid");
488 return FAILED;
489 }
490 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
491
492 if (this->dh == NULL ||
493 !this->proposal->has_dh_group(this->proposal, this->dh_group))
494 {
495 DBG1(DBG_IKE, "peer DH group selection invalid");
496 return FAILED;
497 }
498
499 if (!derive_keys(this, this->my_nonce, this->other_nonce))
500 {
501 DBG1(DBG_IKE, "key derivation failed");
502 return FAILED;
503 }
504 return SUCCESS;
505 }
506
507 METHOD(task_t, get_type, task_type_t,
508 private_ike_init_t *this)
509 {
510 return IKE_INIT;
511 }
512
513 METHOD(task_t, migrate, void,
514 private_ike_init_t *this, ike_sa_t *ike_sa)
515 {
516 DESTROY_IF(this->proposal);
517 chunk_free(&this->other_nonce);
518
519 this->ike_sa = ike_sa;
520 this->keymat = ike_sa->get_keymat(ike_sa);
521 this->proposal = NULL;
522 DESTROY_IF(this->dh);
523 this->dh = this->keymat->create_dh(this->keymat, this->dh_group);
524 }
525
526 METHOD(task_t, destroy, void,
527 private_ike_init_t *this)
528 {
529 DESTROY_IF(this->dh);
530 DESTROY_IF(this->proposal);
531 chunk_free(&this->my_nonce);
532 chunk_free(&this->other_nonce);
533 chunk_free(&this->cookie);
534 free(this);
535 }
536
537 METHOD(ike_init_t, get_lower_nonce, chunk_t,
538 private_ike_init_t *this)
539 {
540 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
541 min(this->my_nonce.len, this->other_nonce.len)) < 0)
542 {
543 return this->my_nonce;
544 }
545 else
546 {
547 return this->other_nonce;
548 }
549 }
550
551 /*
552 * Described in header.
553 */
554 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
555 {
556 private_ike_init_t *this;
557
558 INIT(this,
559 .public = {
560 .task = {
561 .get_type = _get_type,
562 .migrate = _migrate,
563 .destroy = _destroy,
564 },
565 .get_lower_nonce = _get_lower_nonce,
566 },
567 .ike_sa = ike_sa,
568 .initiator = initiator,
569 .dh_group = MODP_NONE,
570 .keymat = ike_sa->get_keymat(ike_sa),
571 .old_sa = old_sa,
572 );
573
574 if (initiator)
575 {
576 this->public.task.build = _build_i;
577 this->public.task.process = _process_i;
578 }
579 else
580 {
581 this->public.task.build = _build_r;
582 this->public.task.process = _process_r;
583 }
584
585 return &this->public;
586 }