started to implement process message
[strongswan.git] / Source / charon / ike_sa.c
1 /**
2 * @file ike_sa.c
3 *
4 * @brief Class ike_sa_t. An object of this type is managed by an
5 * ike_sa_manager_t object and represents an IKE_SA
6 *
7 */
8
9 /*
10 * Copyright (C) 2005 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include "ike_sa.h"
25
26 #include "types.h"
27 #include "globals.h"
28 #include "utils/allocator.h"
29 #include "utils/linked_list.h"
30 #include "utils/logger_manager.h"
31 #include "utils/randomizer.h"
32 #include "payloads/sa_payload.h"
33 #include "payloads/nonce_payload.h"
34 #include "payloads/ke_payload.h"
35 #include "payloads/transform_substructure.h"
36 #include "payloads/transform_attribute.h"
37
38
39 /**
40 * States in which a IKE_SA can actually be
41 */
42 typedef enum ike_sa_state_e ike_sa_state_t;
43
44 enum ike_sa_state_e {
45
46 /**
47 * IKE_SA is is not in a state
48 */
49 NO_STATE,
50
51 /**
52 * A IKE_SA_INIT-message was sent: role initiator
53 */
54 IKE_SA_INIT_REQUESTED,
55
56 /**
57 * A IKE_SA_INIT-message was replied: role responder
58 */
59 IKE_SA_INIT_RESPONDED,
60
61 /**
62 * An IKE_AUTH-message was sent after a successful
63 * IKE_SA_INIT-exchange: role initiator
64 */
65 IKE_AUTH_REQUESTED,
66
67 /**
68 * An IKE_AUTH-message was replied: role responder.
69 * In this state, all the informations for an IKE_SA
70 * and one CHILD_SA are known.
71 */
72 IKE_SA_INITIALIZED
73 };
74
75
76 /**
77 * Private data of an message_t object
78 */
79 typedef struct private_ike_sa_s private_ike_sa_t;
80
81 struct private_ike_sa_s {
82
83 /**
84 * Public part of a ike_sa_t object
85 */
86 ike_sa_t public;
87
88 status_t (*build_sa_payload) (private_ike_sa_t *this, sa_payload_t **payload);
89 status_t (*build_nonce_payload) (private_ike_sa_t *this, nonce_payload_t **payload);
90 status_t (*build_ke_payload) (private_ike_sa_t *this, ke_payload_t **payload);
91
92 /* Private values */
93 /**
94 * Identifier for the current IKE_SA
95 */
96 ike_sa_id_t *ike_sa_id;
97
98 /**
99 * Linked List containing the child sa's of the current IKE_SA
100 */
101 linked_list_t *child_sas;
102
103 /**
104 * Current state of the IKE_SA
105 */
106 ike_sa_state_t current_state;
107
108 /**
109 * is this IKE_SA the original initiator of this IKE_SA
110 */
111 bool original_initiator;
112
113 /**
114 * this SA's source for random data
115 */
116 randomizer_t *randomizer;
117
118 linked_list_t *sent_messages;
119
120 struct {
121 host_t *host;
122 } me;
123
124 struct {
125 host_t *host;
126 } other;
127
128 /**
129 * a logger for this IKE_SA
130 */
131 logger_t *logger;
132 };
133
134 /**
135 * @brief implements function process_message of private_ike_sa_t
136 */
137 static status_t process_message (private_ike_sa_t *this, message_t *message)
138 {
139 status_t status;
140 /* @TODO Add Message Processing here */
141
142 this->logger->log(this->logger, CONTROL_MORE, "Process message ...");
143
144 //this->logger->log(this->logger, CONTROL_MORE, "First Payload type %s",mapping_find(payload_type_m,message->get_next_payload(message)));
145
146 status = message->parse_body(message);
147
148
149 /*
150 iterator->current(iterator, (void**)&next_payload);
151 payload->set_next_type(payload, next_payload->get_type(next_payload));
152 status = generator->generate_payload(generator, payload);
153 if (status != SUCCESS)
154 {
155 generator->destroy(generator);
156 ike_header->destroy(ike_header);
157 return status;
158 }
159 payload = next_payload;
160 }*/
161
162
163 return status;
164 }
165
166 /**
167 * @brief implements function process_configuration of private_ike_sa_t
168 */
169 static status_t initialize_connection(private_ike_sa_t *this, char *name)
170 {
171 message_t *message;
172 payload_t *payload;
173 packet_t *packet;
174 status_t status;
175
176 this->logger->log(this->logger, CONTROL, "initializing connection");
177
178 this->original_initiator = TRUE;
179
180 message = message_create();
181
182 if (message == NULL)
183 {
184 return OUT_OF_RES;
185 }
186
187
188 message->set_exchange_type(message, IKE_SA_INIT);
189 message->set_original_initiator(message, this->original_initiator);
190 message->set_message_id(message, 0);
191 message->set_ike_sa_id(message, this->ike_sa_id);
192 message->set_request(message, TRUE);
193
194 status = this->build_sa_payload(this, (sa_payload_t**)&payload);
195 if (status != SUCCESS)
196 {
197 message->destroy(message);
198 return status;
199 }
200 payload->set_next_type(payload, KEY_EXCHANGE);
201 message->add_payload(message, payload);
202
203 status = this->build_ke_payload(this, (ke_payload_t**)&payload);
204 if (status != SUCCESS)
205 {
206 message->destroy(message);
207 return status;
208 }
209 payload->set_next_type(payload, NONCE);
210 message->add_payload(message, payload);
211
212 status = this->build_nonce_payload(this, (nonce_payload_t**)&payload);
213 if (status != SUCCESS)
214 {
215 message->destroy(message);
216 return status;
217 }
218 payload->set_next_type(payload, NO_PAYLOAD);
219 message->add_payload(message, payload);
220
221 status = message->generate(message, &packet);
222 if (status != SUCCESS)
223 {
224 message->destroy(message);
225 return status;
226 }
227
228 global_send_queue->add(global_send_queue, packet);
229
230
231 message->destroy(message);
232
233
234 return OUT_OF_RES;
235 }
236
237 /**
238 * @brief implements function private_ike_sa_t.get_id
239 */
240 static ike_sa_id_t* get_id(private_ike_sa_t *this)
241 {
242 return this->ike_sa_id;
243 }
244
245 /**
246 * implements private_ike_sa_t.build_sa_payload
247 */
248 static status_t build_sa_payload(private_ike_sa_t *this, sa_payload_t **payload)
249 {
250 sa_payload_t *sa_payload;
251 proposal_substructure_t *proposal;
252 transform_substructure_t *transform;
253 transform_attribute_t *attribute;
254
255
256 this->logger->log(this->logger, CONTROL_MORE, "building sa payload");
257
258 sa_payload = sa_payload_create();
259 if (sa_payload == NULL)
260 {
261 return OUT_OF_RES;
262 }
263
264 do
265 { /* no loop, just to break */
266 proposal = proposal_substructure_create();
267 if (proposal == NULL)
268 {
269 break;
270 }
271 sa_payload->add_proposal_substructure(sa_payload, proposal);
272
273 /*
274 * Encryption Algorithm
275 */
276 transform = transform_substructure_create();
277 if (transform == NULL)
278 {
279 break;
280 }
281 proposal->add_transform_substructure(proposal, transform);
282 transform->set_is_last_transform(transform, FALSE);
283 transform->set_transform_type(transform, ENCRYPTION_ALGORITHM);
284 transform->set_transform_id(transform, ENCR_AES_CBC);
285
286 attribute = transform_attribute_create();
287 if (attribute == NULL)
288 {
289 break;
290 }
291 transform->add_transform_attribute(transform, attribute);
292 attribute->set_attribute_type(attribute, KEY_LENGTH);
293 attribute->set_value(attribute, 16);
294
295 /*
296 * Pseudo-random Function
297 */
298 transform = transform_substructure_create();
299 if (transform == NULL)
300 {
301 break;
302 }
303 proposal->add_transform_substructure(proposal, transform);
304 transform->set_is_last_transform(transform, FALSE);
305 transform->set_transform_type(transform, PSEUDO_RANDOM_FUNCTION);
306 transform->set_transform_id(transform, PRF_HMAC_SHA1);
307
308 attribute = transform_attribute_create();
309 if (attribute == NULL)
310 {
311 break;
312 }
313 transform->add_transform_attribute(transform, attribute);
314 attribute->set_attribute_type(attribute, KEY_LENGTH);
315 attribute->set_value(attribute, 16);
316
317
318 /*
319 * Integrity Algorithm
320 */
321 transform = transform_substructure_create();
322 if (transform == NULL)
323 {
324 break;
325 }
326 proposal->add_transform_substructure(proposal, transform);
327 transform->set_is_last_transform(transform, FALSE);
328 transform->set_transform_type(transform, INTEGRITIY_ALGORITHM);
329 transform->set_transform_id(transform, AUTH_HMAC_SHA1_96);
330
331 attribute = transform_attribute_create();
332 if (attribute == NULL)
333 {
334 break;
335 }
336 transform->add_transform_attribute(transform, attribute);
337 attribute->set_attribute_type(attribute, KEY_LENGTH);
338 attribute->set_value(attribute, 16);
339
340
341 /*
342 * Diffie-Hellman Group
343 */
344 transform = transform_substructure_create();
345 if (transform == NULL)
346 {
347 break;
348 }
349 proposal->add_transform_substructure(proposal, transform);
350 transform->set_is_last_transform(transform, FALSE);
351 transform->set_transform_type(transform, DIFFIE_HELLMAN_GROUP);
352 transform->set_transform_id(transform, MODP_1024_BIT);
353
354 *payload = sa_payload;
355
356 return SUCCESS;
357
358 } while(FALSE);
359
360 return OUT_OF_RES;
361 }
362
363 /**
364 * implements private_ike_sa_t.build_ke_payload
365 */
366 static status_t build_ke_payload(private_ike_sa_t *this, ke_payload_t **payload)
367 {
368 ke_payload_t *ke_payload;
369 chunk_t key_data;
370
371
372 this->logger->log(this->logger, CONTROL_MORE, "building ke payload");
373
374 key_data.ptr = "12345";
375 key_data.len = strlen("12345");
376
377
378 ke_payload = ke_payload_create();
379 if (ke_payload == NULL)
380 {
381 return OUT_OF_RES;
382 }
383 ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
384 if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
385 {
386 ke_payload->destroy(ke_payload);
387 return OUT_OF_RES;
388 }
389 *payload = ke_payload;
390 return SUCCESS;
391 }
392
393 /**
394 * implements private_ike_sa_t.build_nonce_payload
395 */
396 static status_t build_nonce_payload(private_ike_sa_t *this, nonce_payload_t **payload)
397 {
398 nonce_payload_t *nonce_payload;
399 chunk_t nonce;
400
401 this->logger->log(this->logger, CONTROL_MORE, "building nonce payload");
402
403 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer, 16, &nonce) != SUCCESS)
404 {
405 return OUT_OF_RES;
406 }
407
408 nonce_payload = nonce_payload_create();
409 if (nonce_payload == NULL)
410 {
411 return OUT_OF_RES;
412 }
413
414 nonce_payload->set_nonce(nonce_payload, nonce);
415
416 *payload = nonce_payload;
417
418 return SUCCESS;
419 }
420
421 /**
422 * @brief implements function destroy of private_ike_sa_t
423 */
424 static status_t destroy (private_ike_sa_t *this)
425 {
426 linked_list_iterator_t *iterator;
427
428 this->child_sas->create_iterator(this->child_sas, &iterator, TRUE);
429 while (iterator->has_next(iterator))
430 {
431 payload_t *payload;
432 iterator->current(iterator, (void**)&payload);
433 payload->destroy(payload);
434 }
435 iterator->destroy(iterator);
436 this->child_sas->destroy(this->child_sas);
437
438 this->ike_sa_id->destroy(this->ike_sa_id);
439
440 this->randomizer->destroy(this->randomizer);
441
442 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
443
444 allocator_free(this);
445
446 return SUCCESS;
447 }
448
449 /*
450 * Described in Header
451 */
452 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
453 {
454 private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t);
455 if (this == NULL)
456 {
457 return NULL;
458 }
459
460
461 /* Public functions */
462 this->public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
463 this->public.initialize_connection = (status_t(*)(ike_sa_t*, char*)) initialize_connection;
464 this->public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
465 this->public.destroy = (status_t(*)(ike_sa_t*))destroy;
466
467 this->build_sa_payload = build_sa_payload;
468 this->build_ke_payload = build_ke_payload;
469 this->build_nonce_payload = build_nonce_payload;
470
471
472
473 /* initialize private fields */
474 if (ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)) != SUCCESS)
475 {
476 allocator_free(this);
477 return NULL;
478 }
479 this->child_sas = linked_list_create();
480 if (this->child_sas == NULL)
481 {
482 this->ike_sa_id->destroy(this->ike_sa_id);
483 allocator_free(this);
484 return NULL;
485 }
486 this->randomizer = randomizer_create();
487 if (this->randomizer == NULL)
488 {
489 this->child_sas->destroy(this->child_sas);
490 this->ike_sa_id->destroy(this->ike_sa_id);
491 allocator_free(this);
492 }
493 this->sent_messages = linked_list_create();
494 if (this->sent_messages == NULL)
495 {
496 this->randomizer->destroy(this->randomizer);
497 this->child_sas->destroy(this->child_sas);
498 this->ike_sa_id->destroy(this->ike_sa_id);
499 allocator_free(this);
500 }
501 this->logger = global_logger_manager->create_logger(global_logger_manager, IKE_SA, NULL);
502 if (this->logger == NULL)
503 {
504 this->randomizer->destroy(this->randomizer);
505 this->child_sas->destroy(this->child_sas);
506 this->ike_sa_id->destroy(this->ike_sa_id);
507 this->sent_messages->destroy(this->sent_messages);
508 allocator_free(this);
509 }
510
511 this->me.host = NULL;
512 this->other.host = NULL;
513
514
515 /* at creation time, IKE_SA isn't in a specific state */
516 this->current_state = NO_STATE;
517
518 return (&this->public);
519 }