- added notify message handling to ike_sa_init_requested_t and
[strongswan.git] / Source / charon / sa / states / initiator_init.c
1 /**
2 * @file initiator_init.c
3 *
4 * @brief Implementation of initiator_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 "initiator_init.h"
24
25
26 #include <daemon.h>
27 #include <sa/states/state.h>
28 #include <sa/states/ike_sa_init_requested.h>
29 #include <utils/allocator.h>
30 #include <transforms/diffie_hellman.h>
31 #include <encoding/payloads/sa_payload.h>
32 #include <encoding/payloads/ke_payload.h>
33 #include <encoding/payloads/nonce_payload.h>
34
35
36 typedef struct private_initiator_init_t private_initiator_init_t;
37
38 /**
39 * Private data of a initiator_init_t object..
40 *
41 */
42 struct private_initiator_init_t {
43 /**
44 * Methods of the state_t interface.
45 */
46 initiator_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 generate public DH value.
55 * This objet is passed to the next state of type ike_sa_init_requested_t.
56 */
57 diffie_hellman_t *diffie_hellman;
58
59 /**
60 * DH group number.
61 */
62 u_int16_t dh_group_number;
63
64 /**
65 * DH group priority used to get dh_group_number from configuration manager.
66 * This priority is passed to the next state of type ike_sa_init_requested_t.
67 */
68 u_int16_t dh_group_priority;
69
70 /**
71 * Sent nonce.
72 * This nonce is passed to the next state of type ike_sa_init_requested_t.
73 */
74 chunk_t sent_nonce;
75
76 /**
77 * Logger used to log :-)
78 *
79 * Is logger of ike_sa!
80 */
81 logger_t *logger;
82
83 /**
84 * Builds the IKE_SA_INIT request message.
85 *
86 * @param this calling object
87 * @param message the created message will be stored at this location
88 */
89 void (*build_ike_sa_init_request) (private_initiator_init_t *this, message_t **message);
90
91 /**
92 * Builds the SA payload for this state.
93 *
94 * @param this calling object
95 * @param payload The generated SA payload object of type ke_payload_t is
96 * stored at this location.
97 */
98 void (*build_sa_payload) (private_initiator_init_t *this, payload_t **payload);
99
100 /**
101 * Builds the KE payload for this state.
102 *
103 * @param this calling object
104 * @param payload The generated KE payload object of type ke_payload_t is
105 * stored at this location.
106 */
107 void (*build_ke_payload) (private_initiator_init_t *this, payload_t **payload);
108
109 /**
110 * Builds the NONCE payload for this state.
111 *
112 * @param this calling object
113 * @param payload The generated NONCE payload object of type ke_payload_t is
114 * stored at this location.
115 */
116 void (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload);
117
118 /**
119 * Destroy function called internally of this class after state change succeeded.
120 *
121 * This destroy function does not destroy objects which were passed to the new state.
122 *
123 * @param this calling object
124 */
125 void (*destroy_after_state_change) (private_initiator_init_t *this);
126 };
127
128 /**
129 * Implementation of initiator_init_t.initiate_connection.
130 */
131 static status_t initiate_connection (private_initiator_init_t *this, char *name)
132 {
133 init_config_t *init_config;
134 sa_config_t *sa_config;
135 status_t status;
136
137 this->logger->log(this->logger, CONTROL, "Initializing connection %s",name);
138
139 status = charon->configuration_manager->get_init_config_for_name(charon->configuration_manager,name,&init_config);
140 if (status != SUCCESS)
141 {
142 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve INIT configuration informations for %s",name);
143 return DELETE_ME;
144 }
145
146 this->ike_sa->set_init_config(this->ike_sa,init_config);
147
148 status = charon->configuration_manager->get_sa_config_for_name(charon->configuration_manager,name,&sa_config);
149
150 if (status != SUCCESS)
151 {
152 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve SA configuration informations for %s",name);
153 return DELETE_ME;
154 }
155
156 this->ike_sa->set_sa_config(this->ike_sa,sa_config);
157
158 /* host informations are read from configuration */
159 this->ike_sa->set_other_host(this->ike_sa,init_config->get_other_host_clone(init_config));
160 this->ike_sa->set_my_host(this->ike_sa,init_config->get_my_host_clone(init_config));
161
162 this->dh_group_number = init_config->get_dh_group_number(init_config,this->dh_group_priority);
163 if (this->dh_group_number == MODP_UNDEFINED)
164 {
165 this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group could not be retrieved with priority %d", this->dh_group_priority);
166 return DELETE_ME;
167 }
168
169 /* next step is done in retry_initiate_connection */
170 return this->public.retry_initiate_connection(&(this->public),this->dh_group_priority);
171 }
172
173 /**
174 * Implementation of initiator_init_t.retry_initiate_connection.
175 */
176 status_t retry_initiate_connection (private_initiator_init_t *this, int dh_group_priority)
177 {
178 ike_sa_init_requested_t *next_state;
179 init_config_t *init_config;
180 randomizer_t *randomizer;
181 message_t *message;
182 packet_t *packet;
183 status_t status;
184 ike_sa_id_t *ike_sa_id;
185
186 this->dh_group_priority = dh_group_priority;
187
188 init_config = this->ike_sa->get_init_config(this->ike_sa);
189
190 ike_sa_id = this->ike_sa->public.get_id(&(this->ike_sa->public));
191 ike_sa_id->set_responder_spi(ike_sa_id,0);
192
193 this->dh_group_number = init_config->get_dh_group_number(init_config,dh_group_priority);
194 if (this->dh_group_number == MODP_UNDEFINED)
195 {
196 this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group could not be retrieved with priority %d", dh_group_priority);
197 return DELETE_ME;
198 }
199
200 this->diffie_hellman = diffie_hellman_create(this->dh_group_number);
201
202 this->logger->log(this->logger, CONTROL|MOST, "Get pseudo random bytes for nonce");
203 randomizer = this->ike_sa->get_randomizer(this->ike_sa);
204 randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
205
206 this->logger->log(this->logger, RAW|MOST, "Nonce",&(this->sent_nonce));
207
208 this->build_ike_sa_init_request (this,&message);
209
210 /* generate packet */
211 this->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
212 status = message->generate(message, NULL, NULL, &packet);
213 if (status != SUCCESS)
214 {
215 this->logger->log(this->logger, ERROR, "could not generate packet from message");
216 message->destroy(message);
217 return DELETE_ME;
218 }
219
220 this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
221 charon->send_queue->add(charon->send_queue, packet);
222
223 /* state can now be changed */
224 this->logger->log(this->logger, CONTROL|MOST, "Create next state object");
225 next_state = ike_sa_init_requested_create(this->ike_sa, this->dh_group_priority, this->diffie_hellman, this->sent_nonce);
226
227 /* last message can now be set */
228 status = this->ike_sa->set_last_requested_message(this->ike_sa, message);
229
230 if (status != SUCCESS)
231 {
232 this->logger->log(this->logger, ERROR, "Could not set last requested message");
233 (next_state->state_interface).destroy(&(next_state->state_interface));
234 message->destroy(message);
235 return DELETE_ME;
236 }
237
238 /* state can now be changed */
239 this->ike_sa->set_new_state(this->ike_sa,(state_t *) next_state);
240
241 /* state has NOW changed :-) */
242 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) );
243
244 this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
245 this->destroy_after_state_change(this);
246
247 return SUCCESS;
248 }
249
250 /**
251 * implements private_initiator_init_t.build_ike_sa_init_request
252 */
253 static void build_ike_sa_init_request (private_initiator_init_t *this, message_t **request)
254 {
255 payload_t *payload;
256 message_t *message;
257
258 /* going to build message */
259 this->logger->log(this->logger, CONTROL|MOST, "Going to build message");
260 this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
261
262 /* build SA payload */
263 this->build_sa_payload(this, &payload);
264 this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
265 message->add_payload(message, payload);
266
267 /* build KE payload */
268 this->build_ke_payload(this, &payload);
269 this->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
270 message->add_payload(message, payload);
271
272 /* build Nonce payload */
273 this->build_nonce_payload(this, &payload);
274 this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
275 message->add_payload(message, payload);
276
277 *request = message;
278 }
279
280 /**
281 * implements private_initiator_init_t.build_sa_payload
282 */
283 static void build_sa_payload(private_initiator_init_t *this, payload_t **payload)
284 {
285 sa_payload_t* sa_payload;
286 size_t proposal_count;
287 ike_proposal_t *proposals;
288 init_config_t *init_config;
289
290 this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
291
292 init_config = this->ike_sa->get_init_config(this->ike_sa);
293
294 proposal_count = init_config->get_proposals(init_config,&proposals);
295
296 sa_payload = sa_payload_create_from_ike_proposals(proposals,proposal_count);
297
298 allocator_free(proposals);
299 *payload = (payload_t *) sa_payload;
300 }
301
302 /**
303 * implements private_initiator_init_t.build_ke_payload
304 */
305 static void build_ke_payload(private_initiator_init_t *this, payload_t **payload)
306 {
307 ke_payload_t *ke_payload;
308 chunk_t key_data;
309
310 this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
311
312 this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
313
314 ke_payload = ke_payload_create();
315 ke_payload->set_dh_group_number(ke_payload, this->dh_group_number);
316 ke_payload->set_key_exchange_data(ke_payload, key_data);
317
318 allocator_free_chunk(&key_data);
319 *payload = (payload_t *) ke_payload;
320 }
321
322 /**
323 * implements private_initiator_init_t.build_nonce_payload
324 */
325 static void build_nonce_payload(private_initiator_init_t *this, payload_t **payload)
326 {
327 nonce_payload_t *nonce_payload;
328
329 this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
330
331 nonce_payload = nonce_payload_create();
332
333 nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
334
335 *payload = (payload_t *) nonce_payload;
336 }
337
338 /**
339 * Implements state_t.get_state
340 */
341 static status_t process_message(private_initiator_init_t *this, message_t *message)
342 {
343 this->logger->log(this->logger, ERROR|MORE, "In state INITIATOR_INIT no message is processed");
344 return FAILED;
345 }
346
347 /**
348 * Implements state_t.get_state
349 */
350 static ike_sa_state_t get_state(private_initiator_init_t *this)
351 {
352 return INITIATOR_INIT;
353 }
354
355 /**
356 * Implements state_t.get_state
357 */
358 static void destroy(private_initiator_init_t *this)
359 {
360 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
361
362 /* destroy stored proposal */
363 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
364
365 /* destroy diffie hellman object */
366 if (this->diffie_hellman != NULL)
367 {
368 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
369 this->diffie_hellman->destroy(this->diffie_hellman);
370 }
371 if (this->sent_nonce.ptr != NULL)
372 {
373 this->logger->log(this->logger, CONTROL | MOST, "Free memory of sent nonce");
374 allocator_free(this->sent_nonce.ptr);
375 }
376 allocator_free(this);
377 }
378
379 /**
380 * Implements private_initiator_init_t.destroy_after_state_change
381 */
382 static void destroy_after_state_change (private_initiator_init_t *this)
383 {
384 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
385
386 /* destroy stored proposal */
387 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
388 allocator_free(this);
389 }
390
391 /*
392 * Described in header.
393 */
394 initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
395 {
396 private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t);
397
398 /* interface functions */
399 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
400 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
401 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
402
403 /* public functions */
404 this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *)) initiate_connection;
405 this->public.retry_initiate_connection = (status_t (*)(initiator_init_t *, int )) retry_initiate_connection;
406
407 /* private functions */
408 this->destroy_after_state_change = destroy_after_state_change;
409 this->build_ike_sa_init_request = build_ike_sa_init_request;
410 this->build_nonce_payload = build_nonce_payload;
411 this->build_sa_payload = build_sa_payload;
412 this->build_ke_payload = build_ke_payload;
413
414 /* private data */
415 this->ike_sa = ike_sa;
416 this->dh_group_priority = 1;
417 this->logger = this->ike_sa->get_logger(this->ike_sa);
418 this->sent_nonce = CHUNK_INITIALIZER;
419
420 return &(this->public);
421 }