cosmetics
[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 char *name;
117
118 name = connection->get_name(connection);
119 this->ike_sa->set_connection(this->ike_sa, connection);
120
121 /* get policy */
122 policy = charon->policies->get_policy_by_name(charon->policies, name);
123 if (policy == NULL)
124 {
125 this->logger->log(this->logger, ERROR | LEVEL1,
126 "could not get a policy named '%s', aborting", name);
127 return DESTROY_ME;
128 }
129 this->ike_sa->set_policy(this->ike_sa, policy);
130
131 my_host = connection->get_my_host(connection);
132 other_host = connection->get_other_host(connection);
133 my_id = policy->get_my_id(policy);
134 other_id = policy->get_other_id(policy);
135
136 this->logger->log(this->logger, CONTROL, "initiating connection \"%s\": %s[%s]...%s[%s]",
137 name,
138 my_host->get_address(my_host),
139 my_id->get_string(my_id),
140 other_host->get_address(other_host),
141 other_id->get_string(other_id));
142
143 /* we must guess now a DH group. For that we choose our most preferred group */
144 dh_group = connection->get_dh_group(connection);
145
146 /* next step is done in retry_initiate_connection */
147 return this->public.retry_initiate_connection(&this->public, dh_group);
148 }
149
150 /**
151 * Implementation of initiator_init_t.retry_initiate_connection.
152 */
153 status_t retry_initiate_connection (private_initiator_init_t *this, diffie_hellman_group_t dh_group)
154 {
155 ike_sa_init_requested_t *next_state;
156 chunk_t ike_sa_init_request_data;
157 connection_t *connection;
158 ike_sa_id_t *ike_sa_id;
159 message_t *message;
160 status_t status;
161
162 this->diffie_hellman = diffie_hellman_create(dh_group);
163 if (this->diffie_hellman == NULL)
164 {
165 this->logger->log(this->logger, AUDIT, "DH group %s (%d) not supported, aborting",
166 mapping_find(diffie_hellman_group_m, dh_group), dh_group);
167 return DESTROY_ME;
168 }
169
170 connection = this->ike_sa->get_connection(this->ike_sa);
171 ike_sa_id = this->ike_sa->public.get_id(&(this->ike_sa->public));
172 ike_sa_id->set_responder_spi(ike_sa_id,0);
173
174 /* going to build message */
175 this->logger->log(this->logger, CONTROL|LEVEL2, "going to build message");
176 this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
177
178 /* build SA payload */
179 this->build_sa_payload(this, message);
180 /* build KE payload */
181 this->build_ke_payload(this, message);
182 /* build Nonce payload */
183 status = this->build_nonce_payload(this, message);
184 if (status != SUCCESS)
185 {
186 this->logger->log(this->logger, ERROR, "building nonce payload failed, aborting");
187 message->destroy(message);
188 return DESTROY_ME;
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 state 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 }