changed to standard connection log output
[strongswan.git] / src / 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 <queues/jobs/retransmit_request_job.h>
30 #include <crypto/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.
56 */
57 diffie_hellman_t *diffie_hellman;
58
59 /**
60 * Sent nonce.
61 * This nonce is passed to the next state of type IKE_SA_INIT_REQUESTED.
62 */
63 chunk_t sent_nonce;
64
65 /**
66 * Assigned logger.
67 *
68 * Is logger of ike_sa!
69 */
70 logger_t *logger;
71
72 /**
73 * Builds the SA payload for this state.
74 *
75 * @param this calling object
76 * @param request message_t object to add the SA payload
77 */
78 void (*build_sa_payload) (private_initiator_init_t *this, message_t *request);
79
80 /**
81 * Builds the KE payload for this state.
82 *
83 * @param this calling object
84 * @param request message_t object to add the KE payload
85 */
86 void (*build_ke_payload) (private_initiator_init_t *this, message_t *request);
87
88 /**
89 * Builds the NONCE payload for this state.
90 *
91 * @param this calling object
92 * @param request message_t object to add the NONCE payload
93 */
94 status_t (*build_nonce_payload) (private_initiator_init_t *this,message_t *request);
95
96 /**
97 * Destroy function called internally of this class after state change to state
98 * IKE_SA_INIT_REQUESTED succeeded.
99 *
100 * This destroy function does not destroy objects which were passed to the new state.
101 *
102 * @param this calling object
103 */
104 void (*destroy_after_state_change) (private_initiator_init_t *this);
105 };
106
107 /**
108 * Implementation of initiator_init_t.initiate_connection.
109 */
110 static status_t initiate_connection (private_initiator_init_t *this, connection_t *connection)
111 {
112 policy_t *policy;
113 diffie_hellman_group_t dh_group;
114 host_t *my_host, *other_host;
115 identification_t *my_id, *other_id;
116
117 my_host = connection->get_my_host(connection);
118 other_host = connection->get_other_host(connection);
119 my_id = connection->get_my_id(connection);
120 other_id = connection->get_other_id(connection);
121
122 this->logger->log(this->logger, CONTROL, "initiating connection \"%s\": %s[%s]...%s[%s]",
123 connection->get_name(connection),
124 my_host->get_address(my_host),
125 my_id->get_string(my_id),
126 other_host->get_address(other_host),
127 other_id->get_string(other_id));
128
129 this->ike_sa->set_connection(this->ike_sa, connection);
130
131 /* get policy */
132 policy = charon->policies->get_policy(charon->policies, my_id, other_id);
133 if (policy == NULL)
134 {
135 this->logger->log(this->logger, ERROR | LEVEL1, "could not get a policy for '%s...%s', aborting",
136 my_id->get_string(my_id), other_id->get_string(other_id));
137 return DESTROY_ME;
138 }
139 this->ike_sa->set_policy(this->ike_sa,policy);
140
141 /* we must guess now a DH group. For that we choose our most preferred group */
142 dh_group = connection->get_dh_group(connection);
143
144 /* next step is done in retry_initiate_connection */
145 return this->public.retry_initiate_connection(&this->public, dh_group);
146 }
147
148 /**
149 * Implementation of initiator_init_t.retry_initiate_connection.
150 */
151 status_t retry_initiate_connection (private_initiator_init_t *this, diffie_hellman_group_t dh_group)
152 {
153 ike_sa_init_requested_t *next_state;
154 chunk_t ike_sa_init_request_data;
155 connection_t *connection;
156 ike_sa_id_t *ike_sa_id;
157 message_t *message;
158 status_t status;
159
160 if (dh_group == MODP_UNDEFINED)
161 {
162 this->logger->log(this->logger, AUDIT, "No DH group acceptable for initialization, Aborting");
163 return DESTROY_ME;
164 }
165
166 connection = this->ike_sa->get_connection(this->ike_sa);
167 this->diffie_hellman = diffie_hellman_create(dh_group);
168 ike_sa_id = this->ike_sa->public.get_id(&(this->ike_sa->public));
169 ike_sa_id->set_responder_spi(ike_sa_id,0);
170
171 /* going to build message */
172 this->logger->log(this->logger, CONTROL|LEVEL2, "Going to build message");
173 this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
174
175 /* build SA payload */
176 this->build_sa_payload(this, message);
177
178 /* build KE payload */
179 this->build_ke_payload(this, message);
180
181 /* build Nonce payload */
182 status = this->build_nonce_payload(this, message);
183 if (status != SUCCESS)
184 {
185 this->logger->log(this->logger, ERROR, "Building nonce payload failed. Aborting");
186 message->destroy(message);
187 return DESTROY_ME;
188 }
189
190 /* message can now be sent (must not be destroyed) */
191 status = this->ike_sa->send_request(this->ike_sa, message);
192 if (status != SUCCESS)
193 {
194 this->logger->log(this->logger, AUDIT, "Unable to initiate connection, could not send message. Aborting");
195 message->destroy(message);
196 return DESTROY_ME;
197 }
198
199 message = this->ike_sa->get_last_requested_message(this->ike_sa);
200
201 ike_sa_init_request_data = message->get_packet_data(message);
202
203 /* state can now be changed */
204 this->logger->log(this->logger, CONTROL|LEVEL2, "Create next state object");
205 next_state = ike_sa_init_requested_create(this->ike_sa, this->diffie_hellman, this->sent_nonce,ike_sa_init_request_data);
206 this->ike_sa->set_new_state(this->ike_sa,(state_t *) next_state);
207
208 this->logger->log(this->logger, CONTROL|LEVEL2, "Destroy old sate object");
209 this->destroy_after_state_change(this);
210 return SUCCESS;
211 }
212
213 /**
214 * Implementation of private_initiator_init_t.build_sa_payload.
215 */
216 static void build_sa_payload(private_initiator_init_t *this, message_t *request)
217 {
218 sa_payload_t* sa_payload;
219 linked_list_t *proposal_list;
220 connection_t *connection;
221
222 this->logger->log(this->logger, CONTROL|LEVEL1, "Building SA payload");
223
224 connection = this->ike_sa->get_connection(this->ike_sa);
225
226 proposal_list = connection->get_proposals(connection);
227
228 sa_payload = sa_payload_create_from_proposal_list(proposal_list);
229
230 this->logger->log(this->logger, CONTROL|LEVEL2, "Add SA payload to message");
231 request->add_payload(request, (payload_t *) sa_payload);
232 }
233
234 /**
235 * Implementation of private_initiator_init_t.build_ke_payload.
236 */
237 static void build_ke_payload(private_initiator_init_t *this, message_t *request)
238 {
239 ke_payload_t *ke_payload;
240 chunk_t key_data;
241 diffie_hellman_group_t dh_group;
242
243 this->logger->log(this->logger, CONTROL|LEVEL1, "Building KE payload");
244
245 this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
246 dh_group = this->diffie_hellman->get_dh_group(this->diffie_hellman);
247
248 ke_payload = ke_payload_create();
249 ke_payload->set_dh_group_number(ke_payload, dh_group);
250 ke_payload->set_key_exchange_data(ke_payload, key_data);
251
252 chunk_free(&key_data);
253
254 this->logger->log(this->logger, CONTROL|LEVEL2, "Add KE payload to message");
255 request->add_payload(request, (payload_t *) ke_payload);
256 }
257
258 /**
259 * Implementation of private_initiator_init_t.build_nonce_payload.
260 */
261 static status_t build_nonce_payload(private_initiator_init_t *this, message_t *request)
262 {
263 nonce_payload_t *nonce_payload;
264 randomizer_t *randomizer;
265 status_t status;
266
267 this->logger->log(this->logger, CONTROL|LEVEL1, "Building NONCE payload");
268
269 this->logger->log(this->logger, CONTROL|LEVEL2, "Get pseudo random bytes for NONCE");
270 randomizer = this->ike_sa->get_randomizer(this->ike_sa);
271
272 status = randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
273 if (status != SUCCESS)
274 {
275 return status;
276 }
277
278 this->logger->log(this->logger, RAW|LEVEL2, "Initiator NONCE",&(this->sent_nonce));
279
280 nonce_payload = nonce_payload_create();
281
282 nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
283
284 this->logger->log(this->logger, CONTROL|LEVEL2, "Add NONCE payload to message");
285 request->add_payload(request, (payload_t *) nonce_payload);
286 return SUCCESS;
287 }
288
289 /**
290 * Implementation of state_t.process_message.
291 */
292 static status_t process_message(private_initiator_init_t *this, message_t *message)
293 {
294 this->logger->log(this->logger, ERROR, "In state INITIATOR_INIT, no message is processed");
295 return FAILED;
296 }
297
298 /**
299 * Implementation of state_t.get_state.
300 */
301 static ike_sa_state_t get_state(private_initiator_init_t *this)
302 {
303 return INITIATOR_INIT;
304 }
305
306 /**
307 * Implementation of state_t.destroy.
308 */
309 static void destroy(private_initiator_init_t *this)
310 {
311 this->logger->log(this->logger, CONTROL | LEVEL3, "Going to destroy initiator_init_t state object");
312
313 /* destroy diffie hellman object */
314 if (this->diffie_hellman != NULL)
315 {
316 this->diffie_hellman->destroy(this->diffie_hellman);
317 }
318 if (this->sent_nonce.ptr != NULL)
319 {
320 free(this->sent_nonce.ptr);
321 }
322 free(this);
323 }
324
325 /**
326 * Implementation of private_initiator_init_t.destroy_after_state_change
327 */
328 static void destroy_after_state_change (private_initiator_init_t *this)
329 {
330 this->logger->log(this->logger, CONTROL | LEVEL3, "Going to destroy initiator_init_t state object");
331 free(this);
332 }
333
334 /*
335 * Described in header.
336 */
337 initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
338 {
339 private_initiator_init_t *this = malloc_thing(private_initiator_init_t);
340
341 /* interface functions */
342 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
343 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
344 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
345
346 /* public functions */
347 this->public.initiate_connection = (status_t (*)(initiator_init_t *, connection_t*)) initiate_connection;
348 this->public.retry_initiate_connection = (status_t (*)(initiator_init_t *, int )) retry_initiate_connection;
349
350 /* private functions */
351 this->destroy_after_state_change = destroy_after_state_change;
352 this->build_nonce_payload = build_nonce_payload;
353 this->build_sa_payload = build_sa_payload;
354 this->build_ke_payload = build_ke_payload;
355
356 /* private data */
357 this->ike_sa = ike_sa;
358 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
359 this->sent_nonce = CHUNK_INITIALIZER;
360 this->diffie_hellman = NULL;
361
362 return &(this->public);
363 }