current state
[strongswan.git] / Source / charon / states / responder_init.c
1 /**
2 * @file responder_init.c
3 *
4 * @brief Start state of a IKE_SA as responder
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 "../globals.h"
26 #include "../utils/allocator.h"
27 #include "../payloads/sa_payload.h"
28 #include "../payloads/ke_payload.h"
29 #include "../payloads/nonce_payload.h"
30 #include "../transforms/diffie_hellman.h"
31
32 /**
33 * Private data of a responder_init_t object.
34 *
35 */
36 typedef struct private_responder_init_s private_responder_init_t;
37 struct private_responder_init_s {
38 /**
39 * methods of the state_t interface
40 */
41 responder_init_t public;
42
43 /**
44 * Assigned IKE_SA
45 */
46 protected_ike_sa_t *ike_sa;
47
48 /**
49 * Diffie Hellman object used to compute shared secret
50 */
51 diffie_hellman_t *diffie_hellman;
52
53 /**
54 * Diffie Hellman group number
55 */
56 u_int16_t dh_group_number;
57
58 /**
59 * Priority used get matching dh_group number
60 */
61 u_int16_t dh_group_priority;
62
63 /**
64 * Sent nonce value
65 */
66 chunk_t sent_nonce;
67
68 /**
69 * Received nonce value
70 */
71 chunk_t received_nonce;
72
73 /**
74 * Logger used to log data
75 *
76 * Is logger of ike_sa!
77 */
78 logger_t *logger;
79
80 /**
81 * Proposals used to initiate connection
82 */
83 linked_list_t *proposals;
84
85 status_t (*build_sa_payload) (private_responder_init_t *this, payload_t **payload);
86 status_t (*build_ke_payload) (private_responder_init_t *this, payload_t **payload);
87 status_t (*build_nonce_payload) (private_responder_init_t *this, payload_t **payload);
88 };
89
90 /**
91 * Implements state_t.get_state
92 */
93 static status_t process_message(private_responder_init_t *this, message_t *message, state_t **new_state)
94 {
95 linked_list_iterator_t *payloads;
96 host_t *source, *destination;
97 status_t status;
98 message_t *response;
99 payload_t *payload;
100 packet_t *packet;
101
102 /* this is the first message we process, so copy host infos */
103 message->get_source(message, &source);
104 message->get_destination(message, &destination);
105
106 /* we need to clone them, since we destroy the message later */
107 destination->clone(destination, &(this->ike_sa->me.host));
108 source->clone(source, &(this->ike_sa->other.host));
109
110 /* parse incoming message */
111 status = message->parse_body(message);
112 if (status != SUCCESS)
113 {
114 this->logger->log(this->logger, ERROR | MORE, "Could not parse body of request message");
115 return status;
116 }
117
118 /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
119 status = message->get_payload_iterator(message, &payloads);
120 if (status != SUCCESS)
121 {
122 this->logger->log(this->logger, ERROR, "Fatal error: Could not get payload interator");
123 return status;
124 }
125
126 while (payloads->has_next(payloads))
127 {
128 payload_t *payload;
129
130 /* get current payload */
131 payloads->current(payloads, (void**)&payload);
132
133 this->logger->log(this->logger, CONTROL|MORE, "Processing payload of type %s", mapping_find(payload_type_m, payload->get_type(payload)));
134 switch (payload->get_type(payload))
135 {
136 case SECURITY_ASSOCIATION:
137 {
138 sa_payload_t *sa_payload = (sa_payload_t*)payload;
139 linked_list_iterator_t *suggested_proposals, *accepted_proposals;
140
141 status = this->proposals->create_iterator(this->proposals, &accepted_proposals, FALSE);
142 if (status != SUCCESS)
143 {
144 this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
145 payloads->destroy(payloads);
146 return status;
147 }
148
149 /* get the list of suggested proposals */
150 status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
151 if (status != SUCCESS)
152 {
153 this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on suggested proposals");
154 accepted_proposals->destroy(accepted_proposals);
155 payloads->destroy(payloads);
156 return status;
157 }
158
159 /* now let the configuration-manager select a subset of the proposals */
160 status = global_configuration_manager->select_proposals_for_host(global_configuration_manager,
161 this->ike_sa->other.host, suggested_proposals, accepted_proposals);
162 if (status != SUCCESS)
163 {
164 this->logger->log(this->logger, CONTROL | MORE, "No proposal of suggested proposals selected");
165 suggested_proposals->destroy(suggested_proposals);
166 accepted_proposals->destroy(accepted_proposals);
167 payloads->destroy(payloads);
168 return status;
169 }
170
171 /* iterators are not needed anymore */
172 suggested_proposals->destroy(suggested_proposals);
173 accepted_proposals->destroy(accepted_proposals);
174
175 this->logger->log(this->logger, CONTROL | MORE, "SA Payload processed");
176 /* ok, we have what we need for sa_payload (proposals are stored in this->proposals)*/
177 break;
178 }
179 case KEY_EXCHANGE:
180 {
181 ke_payload_t *ke_payload = (ke_payload_t*)payload;
182 diffie_hellman_group_t group;
183 diffie_hellman_t *dh;
184 bool allowed_group;
185
186 group = ke_payload->get_dh_group_number(ke_payload);
187
188 status = global_configuration_manager->is_dh_group_allowed_for_host(global_configuration_manager,
189 this->ike_sa->other.host, group, &allowed_group);
190
191 if (status != SUCCESS)
192 {
193 this->logger->log(this->logger, ERROR | MORE, "Could not get informations about DH group");
194 payloads->destroy(payloads);
195 return status;
196 }
197 if (!allowed_group)
198 {
199 /** @todo Send info reply */
200 }
201
202 /* create diffie hellman object to handle DH exchange */
203 dh = diffie_hellman_create(group);
204 if (dh == NULL)
205 {
206 this->logger->log(this->logger, ERROR, "Could not generate DH object");
207 payloads->destroy(payloads);
208 return OUT_OF_RES;
209 }
210
211 this->logger->log(this->logger, CONTROL | MORE, "Set other DH public value");
212
213 status = dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload));
214 if (status != SUCCESS)
215 {
216 this->logger->log(this->logger, ERROR, "Could not set other DH public value");
217 dh->destroy(dh);
218 payloads->destroy(payloads);
219 return OUT_OF_RES;
220 }
221
222 this->diffie_hellman = dh;
223
224 this->logger->log(this->logger, CONTROL | MORE, "KE Payload processed");
225 break;
226 }
227 case NONCE:
228 {
229 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
230 chunk_t nonce;
231
232 this->logger->log(this->logger, CONTROL | MORE, "Get nonce value and store it");
233 nonce_payload->get_nonce(nonce_payload, &nonce);
234 /** @todo free if there is already one */
235 this->received_nonce.ptr = allocator_clone_bytes(nonce.ptr, nonce.len);
236 this->received_nonce.len = nonce.len;
237 if (this->received_nonce.ptr == NULL)
238 {
239 payloads->destroy(payloads);
240 return OUT_OF_RES;
241 }
242
243 this->logger->log(this->logger, CONTROL | MORE, "Nonce Payload processed");
244 break;
245 }
246 default:
247 {
248 this->logger->log(this->logger, ERROR | MORE, "Payload type not supported!");
249 payloads->destroy(payloads);
250 return OUT_OF_RES;
251 }
252
253 }
254
255 }
256 /* iterator can be destroyed */
257 payloads->destroy(payloads);
258
259 this->logger->log(this->logger, CONTROL | MORE, "Request successfully handled. Going to create reply.");
260
261 this->logger->log(this->logger, CONTROL | MOST, "Going to create nonce.");
262 if (this->ike_sa->randomizer->allocate_pseudo_random_bytes(this->ike_sa->randomizer, NONCE_SIZE, &(this->sent_nonce)) != SUCCESS)
263 {
264 this->logger->log(this->logger, ERROR, "Could not create nonce!");
265 return OUT_OF_RES;
266 }
267
268
269 /* set up the reply */
270 status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
271 if (status != SUCCESS)
272 {
273 this->logger->log(this->logger, ERROR, "Could not create empty message");
274 return status;
275 }
276
277 /* build SA payload */
278 status = this->build_sa_payload(this, &payload);
279 if (status != SUCCESS)
280 {
281 this->logger->log(this->logger, ERROR, "Could not build SA payload");
282 return status;
283 }
284
285 this ->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
286 status = response->add_payload(response, payload);
287 if (status != SUCCESS)
288 {
289 this->logger->log(this->logger, ERROR, "Could not add SA payload to message");
290 return status;
291 }
292
293 /* build KE payload */
294 status = this->build_ke_payload(this,&payload);
295 if (status != SUCCESS)
296 {
297 this->logger->log(this->logger, ERROR, "Could not build KE payload");
298 return status;
299 }
300
301 this ->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
302 status = response->add_payload(response, payload);
303 if (status != SUCCESS)
304 {
305 this->logger->log(this->logger, ERROR, "Could not add KE payload to message");
306 return status;
307 }
308
309 /* build Nonce payload */
310 status = this->build_nonce_payload(this, &payload);
311 if (status != SUCCESS)
312 {
313 this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
314 return status;
315 }
316
317 this ->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
318 status = response->add_payload(response, payload);
319 if (status != SUCCESS)
320 {
321 this->logger->log(this->logger, ERROR, "Could not add nonce payload to message");
322 return status;
323 }
324
325 /* generate packet */
326 this ->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
327 status = response->generate(response, &packet);
328 if (status != SUCCESS)
329 {
330 this->logger->log(this->logger, ERROR, "Fatal error: could not generate packet from message");
331 return status;
332 }
333
334 this ->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
335 status = global_send_queue->add(global_send_queue, packet);
336 if (status != SUCCESS)
337 {
338 this->logger->log(this->logger, ERROR, "Could not add packet to send queue");
339 return status;
340 }
341
342 if ( this->ike_sa->last_responded_message != NULL)
343 {
344 /* destroy message */
345 this ->logger->log(this->logger, CONTROL|MOST, "Destroy stored last responded message");
346 this->ike_sa->last_responded_message->destroy(this->ike_sa->last_responded_message);
347 }
348
349 this->ike_sa->last_responded_message = response;
350
351 /* state has NOW changed :-) */
352 // this ->logger->log(this->logger, CONTROL|MORE, "Change state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m,this->state),mapping_find(ike_sa_state_m,IKE_SA_INIT_REQUESTED) );
353
354
355 return SUCCESS;
356 }
357
358 /**
359 * implements private_initiator_init_t.build_sa_payload
360 */
361 static status_t build_sa_payload(private_responder_init_t *this, payload_t **payload)
362 {
363 sa_payload_t* sa_payload;
364 linked_list_iterator_t *proposal_iterator;
365 status_t status;
366
367
368 /* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
369
370 this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
371
372 status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
373 if (status != SUCCESS)
374 {
375 this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
376 return status;
377 }
378
379 sa_payload = sa_payload_create();
380 if (sa_payload == NULL)
381 {
382 this->logger->log(this->logger, ERROR, "Fatal error: Could not create SA payload object");
383 return OUT_OF_RES;
384 }
385
386 while (proposal_iterator->has_next(proposal_iterator))
387 {
388 proposal_substructure_t *current_proposal;
389 proposal_substructure_t *current_proposal_clone;
390 status = proposal_iterator->current(proposal_iterator,(void **) &current_proposal);
391 if (status != SUCCESS)
392 {
393 this->logger->log(this->logger, ERROR, "Could not get current proposal needed to copy");
394 sa_payload->destroy(sa_payload);
395 return status;
396 }
397 status = current_proposal->clone(current_proposal,&current_proposal_clone);
398 if (status != SUCCESS)
399 {
400 this->logger->log(this->logger, ERROR, "Could not clone current proposal");
401 sa_payload->destroy(sa_payload);
402 return status;
403 }
404
405 status = sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone);
406 if (status != SUCCESS)
407 {
408 this->logger->log(this->logger, ERROR, "Could not add cloned proposal to SA payload");
409 sa_payload->destroy(sa_payload);
410 return status;
411 }
412
413 }
414
415 this->logger->log(this->logger, CONTROL|MORE, "sa payload builded");
416
417 *payload = (payload_t *) sa_payload;
418
419 return SUCCESS;
420 }
421
422 /**
423 * implements private_initiator_init_t.build_ke_payload
424 */
425 static status_t build_ke_payload(private_responder_init_t *this, payload_t **payload)
426 {
427 ke_payload_t *ke_payload;
428 chunk_t key_data;
429 status_t status;
430
431 this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
432
433
434 this ->logger->log(this->logger, CONTROL|MORE, "get public dh value to send in ke payload");
435 status = this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
436 if (status != SUCCESS)
437 {
438 this->logger->log(this->logger, ERROR, "Could not get my DH public value");
439 return status;
440 }
441
442 ke_payload = ke_payload_create();
443 if (ke_payload == NULL)
444 {
445 this->logger->log(this->logger, ERROR, "Could not create KE payload");
446 allocator_free_chunk(key_data);
447 return OUT_OF_RES;
448 }
449 ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
450 if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
451 {
452 this->logger->log(this->logger, ERROR, "Could not set key exchange data of KE payload");
453 ke_payload->destroy(ke_payload);
454 allocator_free_chunk(key_data);
455 return OUT_OF_RES;
456 }
457 allocator_free_chunk(key_data);
458
459 *payload = (payload_t *) ke_payload;
460 return SUCCESS;
461 }
462
463 /**
464 * implements private_initiator_init_t.build_nonce_payload
465 */
466 static status_t build_nonce_payload(private_responder_init_t *this, payload_t **payload)
467 {
468 nonce_payload_t *nonce_payload;
469 status_t status;
470
471 this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
472
473 nonce_payload = nonce_payload_create();
474 if (nonce_payload == NULL)
475 {
476 this->logger->log(this->logger, ERROR, "Fatal error: could not create nonce payload object");
477 return OUT_OF_RES;
478 }
479
480 status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
481
482 if (status != SUCCESS)
483 {
484 this->logger->log(this->logger, ERROR, "Fatal error: could not set nonce data of payload");
485 nonce_payload->destroy(nonce_payload);
486 return status;
487 }
488
489 *payload = (payload_t *) nonce_payload;
490
491 return SUCCESS;
492 }
493
494
495 /**
496 * Implements state_t.get_state
497 */
498 static ike_sa_state_t get_state(private_responder_init_t *this)
499 {
500 return RESPONDER_INIT;
501 }
502
503 /**
504 * Implements state_t.get_state
505 */
506 static status_t destroy(private_responder_init_t *this)
507 {
508 /* destroy stored proposal */
509 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
510 while (this->proposals->get_count(this->proposals) > 0)
511 {
512 proposal_substructure_t *current_proposal;
513 this->proposals->remove_first(this->proposals,(void **)&current_proposal);
514 current_proposal->destroy(current_proposal);
515 }
516 this->proposals->destroy(this->proposals);
517 allocator_free(this);
518 return SUCCESS;
519 }
520
521 /*
522 * Described in header.
523 */
524 responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa)
525 {
526 private_responder_init_t *this = allocator_alloc_thing(private_responder_init_t);
527
528 if (this == NULL)
529 {
530 return NULL;
531 }
532
533 /* interface functions */
534 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *,state_t **)) process_message;
535 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
536 this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy;
537
538 /* private functions */
539 this->build_sa_payload = build_sa_payload;
540 this->build_ke_payload = build_ke_payload;
541 this->build_nonce_payload = build_nonce_payload;
542
543 /* private data */
544 this->ike_sa = ike_sa;
545 this->logger = this->ike_sa->logger;
546 this->sent_nonce.ptr = NULL;
547 this->sent_nonce.len = 0;
548 this->received_nonce.ptr = NULL;
549 this->received_nonce.len = 0;
550 this->proposals = linked_list_create();
551 if (this->proposals == NULL)
552 {
553 allocator_free(this);
554 return NULL;
555 }
556
557 return &(this->public);
558 }