00c691d03218e885b8c0d24e30e6c48f7ef05014
[strongswan.git] / Source / charon / sa / states / ike_auth_requested.c
1 /**
2 * @file ike_auth_requested.c
3 *
4 * @brief Implementation of ike_auth_requested_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "ike_auth_requested.h"
24
25 #include <daemon.h>
26 #include <utils/allocator.h>
27 #include <encoding/payloads/ts_payload.h>
28 #include <encoding/payloads/sa_payload.h>
29 #include <encoding/payloads/id_payload.h>
30 #include <encoding/payloads/auth_payload.h>
31 #include <encoding/payloads/notify_payload.h>
32 #include <transforms/signers/signer.h>
33 #include <transforms/crypters/crypter.h>
34 #include <sa/states/ike_sa_established.h>
35 #include <sa/authenticator.h>
36
37 typedef struct private_ike_auth_requested_t private_ike_auth_requested_t;
38
39 /**
40 * Private data of a ike_auth_requested_t object.
41 *
42 */
43 struct private_ike_auth_requested_t {
44 /**
45 * Public interface of ike_auth_requested_t.
46 */
47 ike_auth_requested_t public;
48
49 /**
50 * Assigned IKE_SA.
51 */
52 protected_ike_sa_t *ike_sa;
53
54 /**
55 * SA config, just a copy of the one stored in the ike_sa.
56 */
57 sa_config_t *sa_config;
58
59 /**
60 * Received nonce from responder.
61 */
62 chunk_t received_nonce;
63
64 /**
65 * Sent nonce in IKE_SA_INIT request.
66 */
67 chunk_t sent_nonce;
68
69 /**
70 * IKE_SA_INIT-Request in binary form.
71 */
72 chunk_t ike_sa_init_reply_data;
73
74 /**
75 * Assigned Logger.
76 *
77 * Is logger of ike_sa!
78 */
79 logger_t *logger;
80
81 /**
82 * Process the IDr payload (check if other id is valid)
83 *
84 * @param this calling object
85 * @param idr_payload ID payload of responder
86 * @return
87 * - SUCCESS
88 * - DELETE_ME
89 */
90 status_t (*process_idr_payload) (private_ike_auth_requested_t *this, id_payload_t *idr_payload);
91
92 /**
93 * Process the SA payload (check if selected proposals are valid, setup child sa)
94 *
95 * @param this calling object
96 * @param sa_payload SA payload of responder
97 *
98 * - SUCCESS
99 * - DELETE_ME
100 */
101 status_t (*process_sa_payload) (private_ike_auth_requested_t *this, sa_payload_t *sa_payload);
102
103 /**
104 * Process the AUTH payload (check authenticity of message)
105 *
106 * @param this calling object
107 * @param auth_payload AUTH payload of responder
108 * @param other_id_payload ID payload of responder
109 *
110 * - SUCCESS
111 * - DELETE_ME
112 */
113 status_t (*process_auth_payload) (private_ike_auth_requested_t *this, auth_payload_t *auth_payload, id_payload_t *other_id_payload);
114
115 /**
116 * Process the TS payload (check if selected traffic selectors are valid)
117 *
118 * @param this calling object
119 * @param ts_initiator TRUE if TS payload is TSi, FALSE for TSr
120 * @param ts_payload TS payload of responder
121 *
122 * - SUCCESS
123 * - DELETE_ME
124 */
125 status_t (*process_ts_payload) (private_ike_auth_requested_t *this, bool ts_initiator, ts_payload_t *ts_payload);
126
127 /**
128 * Process a notify payload
129 *
130 * @param this calling object
131 * @param notify_payload notify payload
132 *
133 * - SUCCESS
134 * - FAILED
135 * - DELETE_ME
136 */
137 status_t (*process_notify_payload) (private_ike_auth_requested_t *this, notify_payload_t *notify_payload);
138 };
139
140
141 /**
142 * Implements state_t.process_message
143 */
144 static status_t process_message(private_ike_auth_requested_t *this, message_t *ike_auth_reply)
145 {
146 ts_payload_t *tsi_payload = NULL, *tsr_payload = NULL;
147 id_payload_t *idr_payload = NULL;
148 auth_payload_t *auth_payload = NULL;
149 sa_payload_t *sa_payload = NULL;
150 iterator_t *payloads = NULL;
151 crypter_t *crypter = NULL;
152 signer_t *signer = NULL;
153 status_t status;
154 host_t *my_host, *other_host;
155
156 if (ike_auth_reply->get_exchange_type(ike_auth_reply) != IKE_AUTH)
157 {
158 this->logger->log(this->logger, ERROR | LEVEL1, "Message of type %s not supported in state ike_auth_requested",
159 mapping_find(exchange_type_m,ike_auth_reply->get_exchange_type(ike_auth_reply)));
160 return FAILED;
161 }
162
163 if (ike_auth_reply->get_request(ike_auth_reply))
164 {
165 this->logger->log(this->logger, ERROR | LEVEL1, "IKE_AUTH requests not allowed state ike_sa_init_responded");
166 return FAILED;
167 }
168
169 /* get signer for verification and crypter for decryption */
170 signer = this->ike_sa->get_signer_responder(this->ike_sa);
171 crypter = this->ike_sa->get_crypter_responder(this->ike_sa);
172
173 /* parse incoming message */
174 status = ike_auth_reply->parse_body(ike_auth_reply, crypter, signer);
175 if (status != SUCCESS)
176 {
177 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply decryption failed. Ignoring message");
178 return status;
179 }
180
181 this->sa_config = this->ike_sa->get_sa_config(this->ike_sa);
182
183 /* we collect all payloads, which are processed later. Notify's are processed
184 * in place, since we don't know how may are there.
185 */
186 payloads = ike_auth_reply->get_payload_iterator(ike_auth_reply);
187 while (payloads->has_next(payloads))
188 {
189 payload_t *payload;
190 payloads->current(payloads, (void**)&payload);
191
192 switch (payload->get_type(payload))
193 {
194 case AUTHENTICATION:
195 {
196 auth_payload = (auth_payload_t*)payload;
197 break;
198 }
199 case ID_RESPONDER:
200 {
201 idr_payload = (id_payload_t*)payload;
202 break;
203 }
204 case SECURITY_ASSOCIATION:
205 {
206 sa_payload = (sa_payload_t*)payload;
207 break;
208 }
209 case TRAFFIC_SELECTOR_INITIATOR:
210 {
211 tsi_payload = (ts_payload_t*)payload;
212 break;
213 }
214 case TRAFFIC_SELECTOR_RESPONDER:
215 {
216 tsr_payload = (ts_payload_t*)payload;
217 break;
218 }
219 case NOTIFY:
220 {
221 notify_payload_t *notify_payload = (notify_payload_t *) payload;
222 /* handle the notify directly, abort if no further processing required */
223 status = this->process_notify_payload(this, notify_payload);
224 if (status != SUCCESS)
225 {
226 payloads->destroy(payloads);
227 return status;
228 }
229 }
230 case CERTIFICATE:
231 {
232 /* TODO handle cert payloads */
233 }
234 default:
235 {
236 this->logger->log(this->logger, ERROR|LEVEL1, "Ignoring Payload %s (%d)",
237 mapping_find(payload_type_m, payload->get_type(payload)), payload->get_type(payload));
238 break;
239 }
240 }
241 }
242 /* iterator can be destroyed */
243 payloads->destroy(payloads);
244
245 /* check if we have all payloads */
246 if (!(idr_payload && sa_payload && auth_payload && tsi_payload && tsr_payload))
247 {
248 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply did not contain all required payloads. Deleting IKE_SA");
249 return DELETE_ME;
250 }
251
252 /* process all payloads */
253 status = this->process_idr_payload(this, idr_payload);
254 if (status != SUCCESS)
255 {
256 return status;
257 }
258 status = this->process_sa_payload(this, sa_payload);
259 if (status != SUCCESS)
260 {
261 return status;
262 }
263 status = this->process_auth_payload(this, auth_payload,idr_payload);
264 if (status != SUCCESS)
265 {
266 return status;
267 }
268 status = this->process_ts_payload(this, TRUE, tsi_payload);
269 if (status != SUCCESS)
270 {
271 return status;
272 }
273 status = this->process_ts_payload(this, FALSE, tsr_payload);
274 if (status != SUCCESS)
275 {
276 return status;
277 }
278
279 this->ike_sa->set_last_replied_message_id(this->ike_sa,ike_auth_reply->get_message_id(ike_auth_reply));
280 /* create new state */
281
282 my_host = this->ike_sa->get_my_host(this->ike_sa);
283 other_host = this->ike_sa->get_other_host(this->ike_sa);
284 this->logger->log(this->logger, AUDIT, "IKE_SA established between %s - %s, authenticated peer with %s",
285 my_host->get_address(my_host), other_host->get_address(other_host),
286 mapping_find(auth_method_m, auth_payload->get_auth_method(auth_payload)));
287
288 this->ike_sa->create_delete_established_ike_sa_job(this->ike_sa,this->sa_config->get_ike_sa_lifetime(this->sa_config));
289 this->ike_sa->set_new_state(this->ike_sa, (state_t*)ike_sa_established_create(this->ike_sa));
290 this->public.state_interface.destroy(&(this->public.state_interface));
291 return SUCCESS;
292 }
293
294 /**
295 * Implements private_ike_auth_requested_t.process_idr_payload
296 */
297 static status_t process_idr_payload(private_ike_auth_requested_t *this, id_payload_t *idr_payload)
298 {
299 identification_t *other_id, *configured_other_id;
300
301 other_id = idr_payload->get_identification(idr_payload);
302
303 configured_other_id = this->sa_config->get_other_id(this->sa_config);
304 if (configured_other_id)
305 {
306 this->logger->log(this->logger, CONTROL|LEVEL1, "configured ID: %s, ID of responder: %s",
307 configured_other_id->get_string(configured_other_id),
308 other_id->get_string(other_id));
309
310 if (!other_id->equals(other_id, configured_other_id))
311 {
312 other_id->destroy(other_id);
313 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a not requested ID. Deleting IKE_SA");
314 return DELETE_ME;
315 }
316 }
317
318 other_id->destroy(other_id);
319 /* TODO do we have to store other_id somewhere ? */
320 return SUCCESS;
321 }
322
323 /**
324 * Implements private_ike_auth_requested_t.process_sa_payload
325 */
326 static status_t process_sa_payload(private_ike_auth_requested_t *this, sa_payload_t *sa_payload)
327 {
328 child_proposal_t *proposal;
329 linked_list_t *proposal_list;
330 protocol_id_t proto;
331
332 /* TODO fix mem allocation */
333 /* TODO child sa stuff */
334
335 /* get selected proposal */
336 proposal_list = sa_payload->get_child_proposals(sa_payload);
337 /* check count of proposals */
338 if (proposal_list->get_count(proposal_list) == 0)
339 {
340 /* no proposal? we accept this, no child sa is built */
341 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply's SA_PAYLOAD didn't contain any proposals. No CHILD_SA created",
342 proposal_list->get_count(proposal_list));
343 return SUCCESS;
344 }
345 if (proposal_list->get_count(proposal_list) > 1)
346 {
347 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply's SA_PAYLOAD contained %d proposal. Deleting IKE_SA",
348 proposal_list->get_count(proposal_list));
349 return DELETE_ME;
350 }
351
352 /* we have to re-check here if other's selection is valid */
353 proposal = this->sa_config->select_proposal(this->sa_config, proposal_list);
354 if (proposal == NULL)
355 {
356 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a not offered proposal. Deleting IKE_SA");
357 return DELETE_ME;
358 }
359 this->logger->log(this->logger, CONTROL|LEVEL1, "selected proposals:");
360 for (proto = AH; proto <= ESP; proto++)
361 {
362 transform_type_t types[] = {ENCRYPTION_ALGORITHM, INTEGRITY_ALGORITHM, DIFFIE_HELLMAN_GROUP, EXTENDED_SEQUENCE_NUMBERS};
363 mapping_t *mappings[] = {encryption_algorithm_m, integrity_algorithm_m, diffie_hellman_group_m, extended_sequence_numbers_m};
364 algorithm_t *algo;
365 int i;
366 for (i = 0; i<sizeof(types)/sizeof(transform_type_t); i++)
367 {
368 if (proposal->get_algorithm(proposal, proto, types[i], &algo))
369 {
370 this->logger->log(this->logger, CONTROL|LEVEL1, "%s: using %s %s (keysize: %d)",
371 mapping_find(protocol_id_m, proto),
372 mapping_find(transform_type_m, types[i]),
373 mapping_find(mappings[i], algo->algorithm),
374 algo->key_size);
375 }
376 }
377 }
378
379 return SUCCESS;
380 }
381
382 /**
383 * Implements private_ike_auth_requested_t.process_auth_payload
384 */
385 static status_t process_auth_payload(private_ike_auth_requested_t *this, auth_payload_t *auth_payload, id_payload_t *other_id_payload)
386 {
387 authenticator_t *authenticator;
388 status_t status;
389
390 authenticator = authenticator_create(this->ike_sa);
391 status = authenticator->verify_auth_data(authenticator,auth_payload,this->ike_sa_init_reply_data,this->sent_nonce,other_id_payload,FALSE);
392 authenticator->destroy(authenticator);
393 if (status != SUCCESS)
394 {
395 this->logger->log(this->logger, AUDIT, "Verification of IKE_AUTH reply failed. Deleting IKE_SA");
396 return DELETE_ME;
397 }
398
399 this->logger->log(this->logger, CONTROL|LEVEL1, "AUTH data verified successfully");
400 return SUCCESS;
401 }
402
403 /**
404 * Implements private_ike_auth_requested_t.process_ts_payload
405 */
406 static status_t process_ts_payload(private_ike_auth_requested_t *this, bool ts_initiator, ts_payload_t *ts_payload)
407 {
408 traffic_selector_t **ts_received, **ts_selected;
409 size_t ts_received_count, ts_selected_count;
410 status_t status = SUCCESS;
411
412 /* get ts form payload */
413 ts_received_count = ts_payload->get_traffic_selectors(ts_payload, &ts_received);
414 /* select ts depending on payload type */
415 if (ts_initiator)
416 {
417 ts_selected_count = this->sa_config->select_traffic_selectors_initiator(this->sa_config, ts_received, ts_received_count, &ts_selected);
418 }
419 else
420 {
421 ts_selected_count = this->sa_config->select_traffic_selectors_responder(this->sa_config, ts_received, ts_received_count, &ts_selected);
422 }
423 /* check if the responder selected valid proposals */
424 if (ts_selected_count != ts_received_count)
425 {
426 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained not offered traffic selectors.");
427 }
428
429 /* cleanup */
430 while(ts_received_count--)
431 {
432 traffic_selector_t *ts = *ts_received + ts_received_count;
433 ts->destroy(ts);
434 }
435 allocator_free(ts_received);
436 while(ts_selected_count--)
437 {
438 traffic_selector_t *ts = *ts_selected + ts_selected_count;
439 ts->destroy(ts);
440 }
441 allocator_free(ts_selected);
442 return status;
443 }
444
445 /**
446 * Implements private_ike_auth_requested_t.process_notify_payload
447 */
448 static status_t process_notify_payload(private_ike_auth_requested_t *this, notify_payload_t *notify_payload)
449 {
450 notify_message_type_t notify_message_type = notify_payload->get_notify_message_type(notify_payload);
451
452 this->logger->log(this->logger, CONTROL|LEVEL1, "Process notify type %s for protocol %s",
453 mapping_find(notify_message_type_m, notify_message_type),
454 mapping_find(protocol_id_m, notify_payload->get_protocol_id(notify_payload)));
455
456 if (notify_payload->get_protocol_id(notify_payload) != IKE)
457 {
458 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a notify for an invalid protocol. Deleting IKE_SA");
459 return DELETE_ME;
460 }
461
462 switch (notify_message_type)
463 {
464 case INVALID_SYNTAX:
465 {
466 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an INVALID_SYNTAX notify. Deleting IKE_SA");
467 return DELETE_ME;
468
469 }
470 case AUTHENTICATION_FAILED:
471 {
472 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an AUTHENTICATION_FAILED notify. Deleting IKE_SA");
473 return DELETE_ME;
474
475 }
476 case SINGLE_PAIR_REQUIRED:
477 {
478 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a SINGLE_PAIR_REQUIRED notify. Deleting IKE_SA");
479 return DELETE_ME;
480 }
481 default:
482 {
483 /*
484 * - In case of unknown error: IKE_SA gets destroyed.
485 * - In case of unknown status: logging
486 */
487
488 if (notify_message_type < 16383)
489 {
490 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an unknown notify error (%d). Deleting IKE_SA",
491 notify_message_type);
492 return DELETE_ME;
493
494 }
495 else
496 {
497 this->logger->log(this->logger, CONTROL, "IKE_AUTH reply contained an unknown notify (%d), ignored.",
498 notify_message_type);
499 return SUCCESS;
500 }
501 }
502 }
503 }
504
505 /**
506 * Implements state_t.get_state
507 */
508 static ike_sa_state_t get_state(private_ike_auth_requested_t *this)
509 {
510 return IKE_AUTH_REQUESTED;
511 }
512
513 /**
514 * Implements state_t.get_state
515 */
516 static void destroy(private_ike_auth_requested_t *this)
517 {
518 allocator_free_chunk(&(this->received_nonce));
519 allocator_free_chunk(&(this->sent_nonce));
520 allocator_free_chunk(&(this->ike_sa_init_reply_data));
521 allocator_free(this);
522 }
523
524 /*
525 * Described in header.
526 */
527 ike_auth_requested_t *ike_auth_requested_create(protected_ike_sa_t *ike_sa,chunk_t sent_nonce,chunk_t received_nonce,chunk_t ike_sa_init_reply_data)
528 {
529 private_ike_auth_requested_t *this = allocator_alloc_thing(private_ike_auth_requested_t);
530
531 /* interface functions */
532 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
533 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
534 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
535
536 /* private functions */
537
538 this->process_idr_payload = process_idr_payload;
539 this->process_sa_payload = process_sa_payload;
540 this->process_auth_payload = process_auth_payload;
541 this->process_ts_payload = process_ts_payload;
542 this->process_notify_payload = process_notify_payload;
543
544 /* private data */
545 this->ike_sa = ike_sa;
546 this->received_nonce = received_nonce;
547 this->sent_nonce = sent_nonce;
548 this->ike_sa_init_reply_data = ike_sa_init_reply_data;
549 this->logger = this->ike_sa->get_logger(this->ike_sa);
550
551 return &(this->public);
552 }