1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
|
/**
* @file initiator_init.c
*
* @brief Start state of a IKE_SA as initiator
*
*/
/*
* Copyright (C) 2005 Jan Hutter, Martin Willi
* Hochschule fuer Technik Rapperswil
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*/
#include "initiator_init.h"
#include <globals.h>
#include <states/state.h>
#include <states/ike_sa_init_requested.h>
#include <utils/allocator.h>
#include <transforms/diffie_hellman.h>
#include <payloads/sa_payload.h>
#include <payloads/ke_payload.h>
#include <payloads/nonce_payload.h>
/**
* Private data of a initiator_init_t object.
*
*/
typedef struct private_initiator_init_s private_initiator_init_t;
struct private_initiator_init_s {
/**
* Methods of the state_t interface.
*/
initiator_init_t public;
/**
* Assigned IKE_SA.
*/
protected_ike_sa_t *ike_sa;
/**
* Diffie hellman object used to generate public DH value.
* This objet is passed to the next state of type ike_sa_init_requested_t.
*/
diffie_hellman_t *diffie_hellman;
/**
* DH group number.
*/
u_int16_t dh_group_number;
/**
* DH group priority used to get dh_group_number from configuration manager.
* This priority is passed to the next state of type ike_sa_init_requested_t.
*/
u_int16_t dh_group_priority;
/**
* Sent nonce.
* This nonce is passed to the next state of type ike_sa_init_requested_t.
*/
chunk_t sent_nonce;
/**
* Proposals used to initiate connection.
*
*/
linked_list_t *proposals;
/**
* Logger used to log :-)
*
* Is logger of ike_sa!
*/
logger_t *logger;
/**
* Builds the IKE_SA_INIT request message.
*
* @param this calling object
* @param message the created message will be stored at this location
* @return
* - SUCCESS
* - OUT_OF_RES
*/
status_t (*build_ike_sa_init_request) (private_initiator_init_t *this, message_t **message);
/**
* Builds the SA payload for this state.
*
* @param this calling object
* @param payload The generated SA payload object of type ke_payload_t is
* stored at this location.
* @return
* - SUCCESS
* - OUT_OF_RES
*/
status_t (*build_sa_payload) (private_initiator_init_t *this, payload_t **payload);
/**
* Builds the KE payload for this state.
*
* @param this calling object
* @param payload The generated KE payload object of type ke_payload_t is
* stored at this location.
* @return
* - SUCCESS
* - OUT_OF_RES
*/
status_t (*build_ke_payload) (private_initiator_init_t *this, payload_t **payload);
/**
* Builds the NONCE payload for this state.
*
* @param this calling object
* @param payload The generated NONCE payload object of type ke_payload_t is
* stored at this location.
* @return
* - SUCCESS
* - OUT_OF_RES
*/
status_t (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload);
/**
* Destroy function called internally of this class after state change succeeded.
*
* This destroy function does not destroy objects which were passed to the new state.
*
* @param this calling object
* @return SUCCESS in any case
*/
status_t (*destroy_after_state_change) (private_initiator_init_t *this);
};
/**
* Implements function initiator_init_t.initiate_connection.
*/
static status_t initiate_connection (private_initiator_init_t *this, char *name, state_t **new_state)
{
linked_list_iterator_t *proposal_iterator;
ike_sa_init_requested_t *next_state;
message_t *message;
packet_t *packet;
status_t status;
this->logger->log(this->logger, CONTROL, "Initializing connection %s",name);
status = global_configuration_manager->get_local_host(global_configuration_manager, name, &(this->ike_sa->me.host));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not retrieve local host configuration information for %s",name);
return INVALID_ARG;
}
status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &(this->ike_sa->other.host));
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not retrieve remote host configuration information for %s",name);
return INVALID_ARG;
}
status = global_configuration_manager->get_dh_group_number(global_configuration_manager, name, &(this->dh_group_number), this->dh_group_priority);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not retrieve DH group number configuration for %s",name);
return INVALID_ARG;
}
status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
return status;
}
status = global_configuration_manager->get_proposals_for_host(global_configuration_manager, this->ike_sa->other.host, proposal_iterator);
/* not needed anymore */
proposal_iterator->destroy(proposal_iterator);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR | MORE, "Could not retrieve Proposals for %s",name);
return status;
}
/* a diffie hellman object could allready exist caused by an failed initiate_connection call */
if (this->diffie_hellman == NULL)
{
this ->logger->log(this->logger, CONTROL|MOST, "create diffie hellman object");
this->diffie_hellman = diffie_hellman_create(this->dh_group_number);
}
if (this->diffie_hellman == NULL)
{
this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!");
return FAILED;
}
if (this->sent_nonce.ptr != NULL)
{
this->logger->log(this->logger, ERROR, "Free existing sent nonce!");
allocator_free(this->sent_nonce.ptr);
this->sent_nonce = CHUNK_INITIALIZER;
}
this ->logger->log(this->logger, CONTROL|MOST, "Get pseudo random bytes for nonce");
if (this->ike_sa->randomizer->allocate_pseudo_random_bytes(this->ike_sa->randomizer, NONCE_SIZE, &(this->sent_nonce)) != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not create nonce!");
return OUT_OF_RES;
}
this ->logger->log(this->logger, RAW|MOST, "Nonce",&(this->sent_nonce));
status = this->build_ike_sa_init_request (this,&message);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not build IKE_SA_INIT request message");
return status;
}
/* generate packet */
this ->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
status = message->generate(message, &packet);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not generate packet from message");
message->destroy(message);
return status;
}
this ->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
status = global_send_queue->add(global_send_queue, packet);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not add packet to send queue");
packet->destroy(packet);
message->destroy(message);
return status;
}
/* state can now be changed */
this ->logger->log(this->logger, CONTROL|MOST, "Create next state object");
next_state = ike_sa_init_requested_create(this->ike_sa, this->dh_group_number, this->diffie_hellman, this->sent_nonce);
if (next_state == NULL)
{
this ->logger->log(this->logger, ERROR, "Fatal error: could not create next state object of type ike_sa_init_requested_t");
return FAILED;
}
if ( this->ike_sa->last_requested_message != NULL)
{
/* destroy message */
this ->logger->log(this->logger, CONTROL|MOST, "Destroy stored last requested message");
this->ike_sa->last_requested_message->destroy(this->ike_sa->last_requested_message);
}
/* message is set after state create */
this ->logger->log(this->logger, CONTROL|MOST, "replace last requested message with current one");
this->ike_sa->last_requested_message = message;
/* message counter can now be increased */
this ->logger->log(this->logger, CONTROL|MOST, "Increate message counter for outgoing messages");
this->ike_sa->message_id_out++;
*new_state = (state_t *) next_state;
/* state has NOW changed :-) */
this ->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m,INITIATOR_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_REQUESTED) );
this ->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
this->destroy_after_state_change(this);
return SUCCESS;
}
/**
* implements private_initiator_init_t.build_ike_sa_init_request
*/
static status_t build_ike_sa_init_request (private_initiator_init_t *this, message_t **request)
{
status_t status;
payload_t *payload;
message_t *message;
/* going to build message */
this ->logger->log(this->logger, CONTROL|MOST, "Going to build message");
status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not build empty message");
return status;
}
/* build SA payload */
status = this->build_sa_payload(this, &payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not build SA payload");
message->destroy(message);
return status;
}
this ->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
status = message->add_payload(message, payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not add SA payload to message");
payload->destroy(payload);
message->destroy(message);
return status;
}
/* build KE payload */
status = this->build_ke_payload(this, &payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not build KE payload");
message->destroy(message);
return status;
}
this ->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
status = message->add_payload(message, payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not add KE payload to message");
payload->destroy(payload);
message->destroy(message);
return status;
}
/* build Nonce payload */
status = this->build_nonce_payload(this, &payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
message->destroy(message);
return status;
}
this ->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
status = message->add_payload(message, payload);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not add nonce payload to message");
payload->destroy(payload);
message->destroy(message);
return status;
}
*request = message;
return SUCCESS;
}
/**
* implements private_initiator_init_t.build_sa_payload
*/
static status_t build_sa_payload(private_initiator_init_t *this, payload_t **payload)
{
sa_payload_t* sa_payload;
linked_list_iterator_t *proposal_iterator;
status_t status;
/* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
return status;
}
sa_payload = sa_payload_create();
if (sa_payload == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: Could not create SA payload object");
return OUT_OF_RES;
}
while (proposal_iterator->has_next(proposal_iterator))
{
proposal_substructure_t *current_proposal;
proposal_substructure_t *current_proposal_clone;
status = proposal_iterator->current(proposal_iterator,(void **) ¤t_proposal);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not get current proposal needed to copy");
proposal_iterator->destroy(proposal_iterator);
sa_payload->destroy(sa_payload);
return status;
}
status = current_proposal->clone(current_proposal,¤t_proposal_clone);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not clone current proposal");
proposal_iterator->destroy(proposal_iterator);
sa_payload->destroy(sa_payload);
return status;
}
status = sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not add cloned proposal to SA payload");
proposal_iterator->destroy(proposal_iterator);
sa_payload->destroy(sa_payload);
return status;
}
}
proposal_iterator->destroy(proposal_iterator);
this->logger->log(this->logger, CONTROL|MORE, "sa payload builded");
*payload = (payload_t *) sa_payload;
return SUCCESS;
}
/**
* implements private_initiator_init_t.build_ke_payload
*/
static status_t build_ke_payload(private_initiator_init_t *this, payload_t **payload)
{
ke_payload_t *ke_payload;
chunk_t key_data;
status_t status;
this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
this ->logger->log(this->logger, CONTROL|MORE, "get public dh value to send in ke payload");
status = this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not get my DH public value");
return status;
}
ke_payload = ke_payload_create();
if (ke_payload == NULL)
{
this->logger->log(this->logger, ERROR, "Could not create KE payload");
allocator_free_chunk(key_data);
return OUT_OF_RES;
}
ke_payload->set_dh_group_number(ke_payload, this->dh_group_number);
if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Could not set key exchange data of KE payload");
ke_payload->destroy(ke_payload);
allocator_free_chunk(key_data);
return OUT_OF_RES;
}
allocator_free_chunk(key_data);
this->logger->log(this->logger, CONTROL|MORE, "ke payload builded");
*payload = (payload_t *) ke_payload;
return SUCCESS;
}
/**
* implements private_initiator_init_t.build_nonce_payload
*/
static status_t build_nonce_payload(private_initiator_init_t *this, payload_t **payload)
{
nonce_payload_t *nonce_payload;
status_t status;
this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
nonce_payload = nonce_payload_create();
if (nonce_payload == NULL)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not create nonce payload object");
return OUT_OF_RES;
}
status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
if (status != SUCCESS)
{
this->logger->log(this->logger, ERROR, "Fatal error: could not set nonce data of payload");
nonce_payload->destroy(nonce_payload);
return status;
}
*payload = (payload_t *) nonce_payload;
this->logger->log(this->logger, CONTROL|MORE, "nonce payload builded");
return SUCCESS;
}
/**
* Implements state_t.get_state
*/
static status_t process_message(private_initiator_init_t *this, message_t *message, state_t **new_state)
{
*new_state = (state_t *) this;
this->logger->log(this->logger, ERROR|MORE, "In state INITIATOR_INIT no message is processed");
return FAILED;
}
/**
* Implements state_t.get_state
*/
static ike_sa_state_t get_state(private_initiator_init_t *this)
{
return INITIATOR_INIT;
}
/**
* Implements state_t.get_state
*/
static status_t destroy(private_initiator_init_t *this)
{
this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
/* destroy stored proposal */
this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
while (this->proposals->get_count(this->proposals) > 0)
{
proposal_substructure_t *current_proposal;
this->proposals->remove_first(this->proposals,(void **)¤t_proposal);
current_proposal->destroy(current_proposal);
}
this->proposals->destroy(this->proposals);
/* destroy diffie hellman object */
if (this->diffie_hellman != NULL)
{
this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
this->diffie_hellman->destroy(this->diffie_hellman);
}
if (this->sent_nonce.ptr != NULL)
{
this->logger->log(this->logger, CONTROL | MOST, "Free memory of sent nonce");
allocator_free(this->sent_nonce.ptr);
}
allocator_free(this);
return SUCCESS;
}
/**
* Implements private_initiator_init_t.destroy_after_state_change
*/
static status_t destroy_after_state_change (private_initiator_init_t *this)
{
this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
/* destroy stored proposal */
this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
while (this->proposals->get_count(this->proposals) > 0)
{
proposal_substructure_t *current_proposal;
this->proposals->remove_first(this->proposals,(void **)¤t_proposal);
current_proposal->destroy(current_proposal);
}
this->proposals->destroy(this->proposals);
allocator_free(this);
return SUCCESS;
}
/*
* Described in header.
*/
initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
{
private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t);
if (this == NULL)
{
return NULL;
}
/* interface functions */
this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *,state_t **)) process_message;
this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy;
/* public functions */
this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *, state_t **)) initiate_connection;
/* private functions */
this->destroy_after_state_change = destroy_after_state_change;
this->build_ike_sa_init_request = build_ike_sa_init_request;
this->build_nonce_payload = build_nonce_payload;
this->build_sa_payload = build_sa_payload;
this->build_ke_payload = build_ke_payload;
/* private data */
this->ike_sa = ike_sa;
this->dh_group_priority = 1;
this->logger = this->ike_sa->logger;
this->proposals = linked_list_create();
this->sent_nonce = CHUNK_INITIALIZER;
if (this->proposals == NULL)
{
allocator_free(this);
return NULL;
}
return &(this->public);
}
|