]> git.ipfire.org Git - thirdparty/strongswan.git/blob - Source/charon/sa/states/responder_init.c
- added notify message handling to ike_sa_init_requested_t and
[thirdparty/strongswan.git] / Source / charon / sa / states / responder_init.c
1 /**
2 * @file responder_init.c
3 *
4 * @brief Implementation of responder_init_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 "responder_init.h"
24
25 #include <daemon.h>
26 #include <sa/states/state.h>
27 #include <sa/states/ike_sa_init_responded.h>
28 #include <utils/allocator.h>
29 #include <encoding/payloads/sa_payload.h>
30 #include <encoding/payloads/ke_payload.h>
31 #include <encoding/payloads/nonce_payload.h>
32 #include <encoding/payloads/notify_payload.h>
33 #include <transforms/diffie_hellman.h>
34
35
36 typedef struct private_responder_init_t private_responder_init_t;
37
38 /**
39 * Private data of a responder_init_t object.
40 *
41 */
42 struct private_responder_init_t {
43 /**
44 * Methods of the state_t interface.
45 */
46 responder_init_t public;
47
48 /**
49 * Assigned IKE_SA.
50 */
51 protected_ike_sa_t *ike_sa;
52
53 /**
54 * Diffie Hellman object used to compute shared secret.
55 *
56 * After processing of incoming IKE_SA_INIT-Request the shared key is
57 * passed to the next state of type ike_sa_init_responded_t.
58 */
59 diffie_hellman_t *diffie_hellman;
60
61 /**
62 * Diffie Hellman group number.
63 */
64 u_int16_t dh_group_number;
65
66 /**
67 * Priority used to get matching dh_group number.
68 */
69 u_int16_t dh_group_priority;
70
71 /**
72 * Sent nonce value.
73 *
74 * This value is passed to the next state of type ike_sa_init_responded_t.
75 */
76 chunk_t sent_nonce;
77
78 /**
79 * Received nonce value
80 *
81 * This value is passed to the next state of type ike_sa_init_responded_t.
82 */
83 chunk_t received_nonce;
84
85 /**
86 * Logger used to log data
87 *
88 * Is logger of ike_sa!
89 */
90 logger_t *logger;
91
92 /**
93 * Selected proposal from suggested ones.
94 */
95 ike_proposal_t selected_proposal;
96
97 /**
98 * Builds the IKE_SA_INIT reply message
99 *
100 * @param this calling object
101 * @param message the message will be written to this location.
102 */
103 void (*build_ike_sa_init_reply) (private_responder_init_t *this, message_t **message);
104
105 /**
106 * Builds the SA payload for this state.
107 *
108 * @param this calling object
109 * @param payload The generated SA payload object of type ke_payload_t is
110 * stored at this location.
111 */
112 void (*build_sa_payload) (private_responder_init_t *this, payload_t **payload);
113
114 /**
115 * Builds the KE payload for this state.
116 *
117 * @param this calling object
118 * @param payload The generated KE payload object of type ke_payload_t is
119 * stored at this location.
120 */
121 void (*build_ke_payload) (private_responder_init_t *this, payload_t **payload);
122
123 /**
124 * Builds the NONCE payload for this state.
125 *
126 * @param this calling object
127 * @param payload The generated NONCE payload object of type ke_payload_t is
128 * stored at this location.
129 */
130 void (*build_nonce_payload) (private_responder_init_t *this, payload_t **payload);
131
132 /**
133 * Destroy function called internally of this class after state change succeeded.
134 *
135 * This destroy function does not destroy objects which were passed to the new state.
136 *
137 * @param this calling object
138 */
139 void (*destroy_after_state_change) (private_responder_init_t *this);
140
141 /**
142 * Sends a IKE_SA_INIT reply with a notify payload.
143 *
144 * @param this calling object
145 * @param type type of notify message
146 * @param data data of notify message
147 */
148 void (*send_notify_reply) (private_responder_init_t *this,notify_message_type_t type, chunk_t data);
149
150 };
151
152 /**
153 * Implements state_t.get_state
154 */
155 static status_t process_message(private_responder_init_t *this, message_t *message)
156 {
157 ike_sa_init_responded_t *next_state;
158 exchange_type_t exchange_type;
159 host_t *source, *destination;
160 init_config_t *init_config;
161 randomizer_t *randomizer;
162 chunk_t shared_secret;
163 iterator_t *payloads;
164 message_t *response;
165 host_t *other_host;
166 packet_t *packet;
167 host_t *my_host;
168 status_t status;
169
170 exchange_type = message->get_exchange_type(message);
171 if (exchange_type != IKE_SA_INIT)
172 {
173 this->logger->log(this->logger, ERROR | MORE, "Message of type %s not supported in state responder_init",mapping_find(exchange_type_m,exchange_type));
174 return DELETE_ME;
175 }
176 if (!message->get_request(message))
177 {
178 this->logger->log(this->logger, ERROR | MORE, "Only requests of type IKE_SA_INIT supported in state responder_init");
179 return DELETE_ME;
180 }
181 /* this is the first message we process, so get host infos */
182 source = message->get_source(message);
183 destination = message->get_destination(message);
184
185 status = charon->configuration_manager->get_init_config_for_host(charon->configuration_manager,destination,source,&init_config);
186 if (status != SUCCESS)
187 {
188 /* no configuration matches given host */
189 this->logger->log(this->logger, ERROR | MORE, "No INIT configuration found for given remote and local hosts");
190 return DELETE_ME;
191 }
192
193 this->ike_sa->set_init_config(this->ike_sa,init_config);
194
195 /* we need to clone them, since we destroy the message later */
196 my_host = destination->clone(destination);
197 other_host = source->clone(source);
198
199 this->ike_sa->set_my_host(this->ike_sa, my_host);
200 this->ike_sa->set_other_host(this->ike_sa, other_host);
201
202 /* parse incoming message */
203 status = message->parse_body(message, NULL, NULL);
204 if (status != SUCCESS)
205 {
206 this->logger->log(this->logger, ERROR | MORE, "Could not parse body of request message");
207 return DELETE_ME;
208 }
209
210 /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
211 payloads = message->get_payload_iterator(message);
212
213 while (payloads->has_next(payloads))
214 {
215 payload_t *payload;
216
217 /* get current payload */
218 payloads->current(payloads, (void**)&payload);
219
220 this->logger->log(this->logger, CONTROL|MORE, "Processing payload of type %s", mapping_find(payload_type_m, payload->get_type(payload)));
221 switch (payload->get_type(payload))
222 {
223 case SECURITY_ASSOCIATION:
224 {
225 sa_payload_t *sa_payload = (sa_payload_t*)payload;
226 ike_proposal_t *ike_proposals;
227 size_t proposal_count;
228
229 /* get the list of suggested proposals */
230 status = sa_payload->get_ike_proposals (sa_payload, &ike_proposals,&proposal_count);
231 if (status != SUCCESS)
232 {
233 this->logger->log(this->logger, ERROR | MORE, "SA payload does not contain IKE proposals");
234 payloads->destroy(payloads);
235 return DELETE_ME;
236 }
237
238 status = init_config->select_proposal(init_config, ike_proposals,proposal_count,&(this->selected_proposal));
239 allocator_free(ike_proposals);
240 if (status != SUCCESS)
241 {
242 this->logger->log(this->logger, ERROR | MORE, "No proposal of suggested proposals selected");
243 payloads->destroy(payloads);
244 this->send_notify_reply(this,NO_PROPOSAL_CHOSEN,CHUNK_INITIALIZER);
245 return DELETE_ME;
246 }
247
248 this->dh_group_number = this->selected_proposal.diffie_hellman_group;
249
250 status = this->ike_sa->create_transforms_from_proposal(this->ike_sa,&(this->selected_proposal));
251 if (status != SUCCESS)
252 {
253 this->logger->log(this->logger, ERROR | MORE, "Transform objects could not be created from selected proposal");
254 payloads->destroy(payloads);
255 return DELETE_ME;
256 }
257
258 this->logger->log(this->logger, CONTROL | MORE, "SA Payload processed");
259 /* ok, we have what we need for sa_payload (proposals are stored in this->proposals)*/
260 break;
261 }
262 case KEY_EXCHANGE:
263 {
264 ke_payload_t *ke_payload = (ke_payload_t*)payload;
265 diffie_hellman_group_t group;
266 diffie_hellman_t *dh;
267
268 group = ke_payload->get_dh_group_number(ke_payload);
269
270 if (group == MODP_UNDEFINED)
271 {
272 this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group set to undefined!");
273 payloads->destroy(payloads);
274 return DELETE_ME;
275 }
276 if (this->dh_group_number != group)
277 {
278 u_int16_t accepted_group;
279 chunk_t accepted_group_chunk;
280 /* group not same as selected one
281 * Maybe key exchange payload is before SA payload */
282 this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group not as in selected proposal!");
283 payloads->destroy(payloads);
284
285 accepted_group = htons(this->dh_group_number);
286 accepted_group_chunk.ptr = (u_int8_t*) &(accepted_group);
287 accepted_group_chunk.len = 2;
288 this->send_notify_reply(this,INVALID_KE_PAYLOAD,accepted_group_chunk);
289 return DELETE_ME;
290 }
291
292 /* create diffie hellman object to handle DH exchange */
293 dh = diffie_hellman_create(group);
294 if (dh == NULL)
295 {
296 this->logger->log(this->logger, ERROR, "Could not generate DH object with group %d",mapping_find(diffie_hellman_group_m,group) );
297 payloads->destroy(payloads);
298 return DELETE_ME;
299 }
300 this->logger->log(this->logger, CONTROL | MORE, "Set other DH public value");
301
302 dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload));
303
304 this->diffie_hellman = dh;
305
306 this->logger->log(this->logger, CONTROL | MORE, "KE Payload processed.");
307 break;
308 }
309 case NONCE:
310 {
311 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
312
313 allocator_free(this->received_nonce.ptr);
314 this->received_nonce = CHUNK_INITIALIZER;
315
316 this->logger->log(this->logger, CONTROL | MORE, "Get nonce value and store it");
317 nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
318
319 this->logger->log(this->logger, CONTROL | MORE, "Nonce Payload processed");
320 break;
321 }
322 default:
323 {
324 this->logger->log(this->logger, ERROR | MORE, "Payload type not supported!");
325 payloads->destroy(payloads);
326 return DELETE_ME;
327 }
328 }
329 }
330 /* iterator can be destroyed */
331 payloads->destroy(payloads);
332
333 this->logger->log(this->logger, CONTROL | MORE, "Request successfully handled. Going to create reply.");
334
335 this->logger->log(this->logger, CONTROL | MOST, "Going to create nonce.");
336
337 randomizer = this->ike_sa->get_randomizer(this->ike_sa);
338 randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
339
340 /* store shared secret */
341 this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it.");
342 status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &shared_secret);
343 this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &shared_secret);
344
345 this->ike_sa->compute_secrets(this->ike_sa,shared_secret,this->received_nonce, this->sent_nonce);
346
347 this->build_ike_sa_init_reply(this,&response);
348
349 /* generate packet */
350 this->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
351 status = response->generate(response, NULL, NULL, &packet);
352 if (status != SUCCESS)
353 {
354 this->logger->log(this->logger, ERROR, "could not generate packet from message");
355 return DELETE_ME;
356 }
357
358 this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
359 charon->send_queue->add(charon->send_queue, packet);
360
361 /* state can now be changed */
362 this->logger->log(this->logger, CONTROL|MOST, "Create next state object");
363
364 next_state = ike_sa_init_responded_create(this->ike_sa, shared_secret, this->received_nonce, this->sent_nonce);
365
366 /* last message can now be set */
367 status = this->ike_sa->set_last_responded_message(this->ike_sa, response);
368
369 if (status != SUCCESS)
370 {
371 this->logger->log(this->logger, ERROR, "Could not set last responded message");
372 response->destroy(response);
373 (next_state->state_interface).destroy(&(next_state->state_interface));
374 return DELETE_ME;
375 }
376
377 /* state can now be changed */
378 this->ike_sa->set_new_state(this->ike_sa, (state_t *) next_state);
379 /* state has NOW changed :-) */
380 this->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m,RESPONDER_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_RESPONDED) );
381
382 this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
383 this->destroy_after_state_change(this);
384
385 return SUCCESS;
386 }
387
388
389 /**
390 * implements private_responder_init_t.build_ike_sa_init_reply
391 */
392 static void build_ike_sa_init_reply (private_responder_init_t *this, message_t **message)
393 {
394 message_t *response;
395 payload_t *payload;
396
397 this->logger->log(this->logger, CONTROL|MOST, "Going to build message");
398 /* set up the reply */
399 this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
400
401 /* build SA payload */
402 this->build_sa_payload(this, &payload);
403 this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
404 response->add_payload(response, payload);
405
406 /* build KE payload */
407 this->build_ke_payload(this,&payload);
408 this->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
409 response->add_payload(response, payload);
410
411 /* build Nonce payload */
412 this->build_nonce_payload(this, &payload);
413 this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
414 response->add_payload(response, payload);
415
416 *message = response;
417 }
418
419 /**
420 * implements private_initiator_init_t.build_sa_payload
421 */
422 static void build_sa_payload(private_responder_init_t *this, payload_t **payload)
423 {
424 sa_payload_t* sa_payload;
425
426 this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
427 sa_payload = sa_payload_create_from_ike_proposals(&(this->selected_proposal),1);
428
429 *payload = (payload_t *) sa_payload;
430 }
431
432 /**
433 * implements private_initiator_init_t.build_ke_payload
434 */
435 static void build_ke_payload(private_responder_init_t *this, payload_t **payload)
436 {
437 ke_payload_t *ke_payload;
438 chunk_t key_data;
439
440 this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
441 this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
442
443 ke_payload = ke_payload_create();
444 ke_payload->set_key_exchange_data(ke_payload,key_data);
445 ke_payload->set_dh_group_number(ke_payload, this->dh_group_number);
446 allocator_free_chunk(&key_data);
447
448 *payload = (payload_t *) ke_payload;
449 }
450
451 /**
452 * implements private_initiator_init_t.build_nonce_payload
453 */
454 static void build_nonce_payload(private_responder_init_t *this, payload_t **payload)
455 {
456 nonce_payload_t *nonce_payload;
457
458 this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
459 nonce_payload = nonce_payload_create();
460 nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
461
462 *payload = (payload_t *) nonce_payload;
463 }
464
465
466 /**
467 * Implements state_t.get_state
468 */
469 static ike_sa_state_t get_state(private_responder_init_t *this)
470 {
471 return RESPONDER_INIT;
472 }
473
474 /**
475 * Implementation of private_initiator_init_t.send_notify_reply.
476 */
477 static void send_notify_reply (private_responder_init_t *this,notify_message_type_t type, chunk_t data)
478 {
479 notify_payload_t *payload;
480 message_t *response;
481 packet_t *packet;
482 status_t status;
483
484 this->logger->log(this->logger, CONTROL|MOST, "Going to build message with notify payload");
485 /* set up the reply */
486 this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
487 payload = notify_payload_create_from_protocol_and_type(IKE,type);
488 if ((data.ptr != NULL) && (data.len > 0))
489 {
490 this->logger->log(this->logger, CONTROL|MOST, "Add Data to notify payload");
491 payload->set_notification_data(payload,data);
492 }
493
494 this->logger->log(this->logger, CONTROL|MOST, "Add Notify payload to message");
495 response->add_payload(response,(payload_t *) payload);
496
497 /* generate packet */
498 this->logger->log(this->logger, CONTROL|MOST, "Gnerate packet from message");
499 status = response->generate(response, NULL, NULL, &packet);
500 if (status != SUCCESS)
501 {
502 this->logger->log(this->logger, ERROR, "Could not generate packet from message");
503 return;
504 }
505
506 this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
507 charon->send_queue->add(charon->send_queue, packet);
508 this->logger->log(this->logger, CONTROL|MOST, "Destroy message");
509 response->destroy(response);
510 }
511
512 /**
513 * Implements state_t.get_state
514 */
515 static void destroy(private_responder_init_t *this)
516 {
517 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder init state object");
518
519 this->logger->log(this->logger, CONTROL | MOST, "Destroy sent nonce");
520 allocator_free(this->sent_nonce.ptr);
521 this->logger->log(this->logger, CONTROL | MOST, "Destroy received nonce");
522 allocator_free(this->received_nonce.ptr);
523
524 if (this->diffie_hellman != NULL)
525 {
526 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t hellman object");
527 this->diffie_hellman->destroy(this->diffie_hellman);
528 }
529 this->logger->log(this->logger, CONTROL | MOST, "Destroy object");
530 allocator_free(this);
531 }
532
533 /**
534 * Implements private_responder_init_t.destroy_after_state_change
535 */
536 static void destroy_after_state_change (private_responder_init_t *this)
537 {
538 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder_init_t state object");
539
540 /* destroy diffie hellman object */
541 if (this->diffie_hellman != NULL)
542 {
543 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
544 this->diffie_hellman->destroy(this->diffie_hellman);
545 }
546
547 this->logger->log(this->logger, CONTROL | MOST, "Destroy object");
548 allocator_free(this);
549 }
550
551 /*
552 * Described in header.
553 */
554 responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa)
555 {
556 private_responder_init_t *this = allocator_alloc_thing(private_responder_init_t);
557
558 /* interface functions */
559 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
560 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
561 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
562
563 /* private functions */
564 this->build_ike_sa_init_reply = build_ike_sa_init_reply;
565 this->build_sa_payload = build_sa_payload;
566 this->build_ke_payload = build_ke_payload;
567 this->build_nonce_payload = build_nonce_payload;
568 this->destroy_after_state_change = destroy_after_state_change;
569 this->send_notify_reply = send_notify_reply;
570
571 /* private data */
572 this->ike_sa = ike_sa;
573 this->logger = this->ike_sa->get_logger(this->ike_sa);
574 this->sent_nonce = CHUNK_INITIALIZER;
575 this->received_nonce = CHUNK_INITIALIZER;
576 this->dh_group_number = MODP_UNDEFINED;
577
578 return &(this->public);
579 }