208898593d5ca26a4e4b607db8facc137cb55f59
[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 ts_initiator TRUE if TS payload is TSi, FALSE for TSr
132 * @param ts_payload TS payload of responder
133 *
134 * - SUCCESS
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 faild. 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 this->ike_sa->set_new_state(this->ike_sa, (state_t*)ike_sa_established_create(this->ike_sa));
282 this->ike_sa->create_delete_established_ike_sa_job(this->ike_sa,this->sa_config->get_ike_sa_lifetime(this->sa_config));
283 this->public.state_interface.destroy(&(this->public.state_interface));
284
285 my_host = this->ike_sa->get_my_host(this->ike_sa);
286 other_host = this->ike_sa->get_other_host(this->ike_sa);
287 this->logger->log(this->logger, AUDIT, "IKE_SA established between %s - %s",
288 my_host->get_address(my_host), other_host->get_address(other_host));
289 return SUCCESS;
290 }
291
292 /**
293 * Implements private_ike_auth_requested_t.process_idr_payload
294 */
295 static status_t process_idr_payload(private_ike_auth_requested_t *this, id_payload_t *idr_payload)
296 {
297 identification_t *other_id, *configured_other_id;
298
299 other_id = idr_payload->get_identification(idr_payload);
300
301 configured_other_id = this->sa_config->get_other_id(this->sa_config);
302 if (configured_other_id)
303 {
304 this->logger->log(this->logger, CONTROL|LEVEL1, "configured ID: %s, ID of responder: %s",
305 configured_other_id->get_string(configured_other_id),
306 other_id->get_string(other_id));
307
308 if (!other_id->equals(other_id, configured_other_id))
309 {
310 other_id->destroy(other_id);
311 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a not requested ID. Deleting IKE_SA");
312 return DELETE_ME;
313 }
314 }
315
316 other_id->destroy(other_id);
317 /* TODO do we have to store other_id somewhere ? */
318 return SUCCESS;
319 }
320
321 /**
322 * Implements private_ike_auth_requested_t.process_sa_payload
323 */
324 static status_t process_sa_payload(private_ike_auth_requested_t *this, sa_payload_t *sa_payload)
325 {
326 child_proposal_t *proposals, *proposal_chosen;
327 size_t proposal_count;
328 status_t status;
329
330 /* dummy spis, until we have a child sa to request them */
331 u_int8_t ah_spi[4] = {0x01, 0x02, 0x03, 0x04};
332 u_int8_t esp_spi[4] = {0x05, 0x06, 0x07, 0x08};
333
334 /* check selected proposal */
335 status = sa_payload->get_child_proposals(sa_payload, &proposals, &proposal_count);
336 if (status != SUCCESS)
337 {
338 /* there are no proposals. This is possible if the requester doesn't want to setup a child sa */
339 this->logger->log(this->logger, CONTROL, "Responders SA_PAYLOAD contained no proposals, no CHILD_SA is built");
340 return SUCCESS;
341 }
342 if (proposal_count > 1)
343 {
344 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply's SA_PAYLOAD contained more than one proposal. Deleting IKE_SA");
345 allocator_free(proposals);
346 return DELETE_ME;
347 }
348
349 proposal_chosen = this->sa_config->select_proposal(this->sa_config, ah_spi, esp_spi, proposals, proposal_count);
350 if (proposal_chosen == NULL)
351 {
352 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a not offered proposal. Deleting IKE_SA");
353 allocator_free(proposals);
354 return DELETE_ME;
355 }
356 else
357 {
358 allocator_free(proposal_chosen);
359 }
360
361 allocator_free(proposals);
362
363 return SUCCESS;
364 }
365
366 /**
367 * Implements private_ike_auth_requested_t.process_auth_payload
368 */
369 static status_t process_auth_payload(private_ike_auth_requested_t *this, auth_payload_t *auth_payload, id_payload_t *other_id_payload)
370 {
371 authenticator_t *authenticator;
372 status_t status;
373
374 authenticator = authenticator_create(this->ike_sa);
375 status = authenticator->verify_auth_data(authenticator,auth_payload,this->ike_sa_init_reply_data,this->sent_nonce,other_id_payload,FALSE);
376 authenticator->destroy(authenticator);
377 if (status != SUCCESS)
378 {
379 this->logger->log(this->logger, AUDIT, "Verification of IKE_AUTH reply failed. Deleting IKE_SA");
380 return DELETE_ME;
381 }
382
383 this->logger->log(this->logger, CONTROL|LEVEL1, "AUTH data verified successfully");
384 return SUCCESS;
385 }
386
387 /**
388 * Implements private_ike_auth_requested_t.process_ts_payload
389 */
390 static status_t process_ts_payload(private_ike_auth_requested_t *this, bool ts_initiator, ts_payload_t *ts_payload)
391 {
392 traffic_selector_t **ts_received, **ts_selected;
393 size_t ts_received_count, ts_selected_count;
394 status_t status = SUCCESS;
395
396 /* get ts form payload */
397 ts_received_count = ts_payload->get_traffic_selectors(ts_payload, &ts_received);
398 /* select ts depending on payload type */
399 if (ts_initiator)
400 {
401 ts_selected_count = this->sa_config->select_traffic_selectors_initiator(this->sa_config, ts_received, ts_received_count, &ts_selected);
402 }
403 else
404 {
405 ts_selected_count = this->sa_config->select_traffic_selectors_responder(this->sa_config, ts_received, ts_received_count, &ts_selected);
406 }
407 /* check if the responder selected valid proposals */
408 if (ts_selected_count != ts_received_count)
409 {
410 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained not offered traffic selectors. Deleting IKE_SA");
411 status = DELETE_ME;
412 }
413
414 /* cleanup */
415 while(ts_received_count--)
416 {
417 traffic_selector_t *ts = *ts_received + ts_received_count;
418 ts->destroy(ts);
419 }
420 allocator_free(ts_received);
421 while(ts_selected_count--)
422 {
423 traffic_selector_t *ts = *ts_selected + ts_selected_count;
424 ts->destroy(ts);
425 }
426 allocator_free(ts_selected);
427 return status;
428 }
429
430 /**
431 * Implements private_ike_auth_requested_t.process_notify_payload
432 */
433 static status_t process_notify_payload(private_ike_auth_requested_t *this, notify_payload_t *notify_payload)
434 {
435 notify_message_type_t notify_message_type = notify_payload->get_notify_message_type(notify_payload);
436
437 this->logger->log(this->logger, CONTROL|LEVEL1, "Process notify type %s for protocol %s",
438 mapping_find(notify_message_type_m, notify_message_type),
439 mapping_find(protocol_id_m, notify_payload->get_protocol_id(notify_payload)));
440
441 if (notify_payload->get_protocol_id(notify_payload) != IKE)
442 {
443 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a notify for an invalid protocol. Deleting IKE_SA");
444 return DELETE_ME;
445 }
446
447 switch (notify_message_type)
448 {
449 case INVALID_SYNTAX:
450 {
451 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an INVALID_SYNTAX notify. Deleting IKE_SA");
452 return DELETE_ME;
453
454 }
455 case AUTHENTICATION_FAILED:
456 {
457 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an AUTHENTICATION_FAILED notify. Deleting IKE_SA");
458 return DELETE_ME;
459
460 }
461 case SINGLE_PAIR_REQUIRED:
462 {
463 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a SINGLE_PAIR_REQUIRED notify. Deleting IKE_SA");
464 return DELETE_ME;
465 }
466 default:
467 {
468 /*
469 * - In case of unknown error: IKE_SA gets destroyed.
470 * - In case of unknown status: logging
471 */
472
473 if (notify_message_type < 16383)
474 {
475 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an unknown notify error (%d). Deleting IKE_SA",
476 notify_message_type);
477 return DELETE_ME;
478
479 }
480 else
481 {
482 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an unknown notify (%d), ignored.", notify_message_type);
483 return SUCCESS;
484 }
485 }
486 }
487 }
488
489 /**
490 * Implements state_t.get_state
491 */
492 static ike_sa_state_t get_state(private_ike_auth_requested_t *this)
493 {
494 return IKE_AUTH_REQUESTED;
495 }
496
497 /**
498 * Implements state_t.get_state
499 */
500 static void destroy(private_ike_auth_requested_t *this)
501 {
502 allocator_free_chunk(&(this->received_nonce));
503 allocator_free_chunk(&(this->sent_nonce));
504 allocator_free_chunk(&(this->ike_sa_init_reply_data));
505 allocator_free(this);
506 }
507
508 /*
509 * Described in header.
510 */
511 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)
512 {
513 private_ike_auth_requested_t *this = allocator_alloc_thing(private_ike_auth_requested_t);
514
515 /* interface functions */
516 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
517 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
518 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
519
520 /* private functions */
521
522 this->process_idr_payload = process_idr_payload;
523 this->process_sa_payload = process_sa_payload;
524 this->process_auth_payload = process_auth_payload;
525 this->process_ts_payload = process_ts_payload;
526 this->process_notify_payload = process_notify_payload;
527
528 /* private data */
529 this->ike_sa = ike_sa;
530 this->received_nonce = received_nonce;
531 this->sent_nonce = sent_nonce;
532 this->ike_sa_init_reply_data = ike_sa_init_reply_data;
533 this->logger = this->ike_sa->get_logger(this->ike_sa);
534
535 return &(this->public);
536 }