- added compution of all needed keys and also creation of needed
[strongswan.git] / Source / charon / sa / states / ike_sa_init_requested.c
1 /**
2 * @file ike_sa_init_requested.c
3 *
4 * @brief State of a IKE_SA after requesting an IKE_SA_INIT
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 "ike_sa_init_requested.h"
24
25 #include <globals.h>
26 #include <utils/allocator.h>
27 #include <encoding/payloads/sa_payload.h>
28 #include <encoding/payloads/ke_payload.h>
29 #include <encoding/payloads/nonce_payload.h>
30 #include <transforms/diffie_hellman.h>
31
32
33 typedef struct private_ike_sa_init_requested_t private_ike_sa_init_requested_t;
34
35 /**
36 * Private data of a ike_sa_init_requested_t object.
37 *
38 */
39 struct private_ike_sa_init_requested_t {
40 /**
41 * methods of the state_t interface
42 */
43 ike_sa_init_requested_t public;
44
45 /**
46 * Assigned IKE_SA
47 */
48 protected_ike_sa_t *ike_sa;
49
50 /**
51 * Diffie Hellman object used to compute shared secret
52 */
53 diffie_hellman_t *diffie_hellman;
54
55 /**
56 * Shared secret of successful exchange
57 */
58 chunk_t shared_secret;
59
60 /**
61 * Sent nonce value
62 */
63 chunk_t sent_nonce;
64
65 /**
66 * Received nonce
67 */
68 chunk_t received_nonce;
69
70 /**
71 * DH group priority used to get dh_group_number from configuration manager.
72 *
73 * Currently uused but usable if informational messages of unsupported dh group number are processed.
74 */
75 u_int16_t dh_group_priority;
76
77 /**
78 * Logger used to log data
79 *
80 * Is logger of ike_sa!
81 */
82 logger_t *logger;
83 };
84
85 /**
86 * Implements state_t.get_state
87 */
88 static status_t process_message(private_ike_sa_init_requested_t *this, message_t *message)
89 {
90 status_t status;
91 iterator_t *payloads;
92 exchange_type_t exchange_type;
93 u_int64_t responder_spi;
94 ike_sa_id_t *ike_sa_id;
95
96
97 exchange_type = message->get_exchange_type(message);
98 if (exchange_type != IKE_SA_INIT)
99 {
100 this->logger->log(this->logger, ERROR | MORE, "Message of type %s not supported in state ike_sa_init_requested",mapping_find(exchange_type_m,exchange_type));
101 return FAILED;
102 }
103
104 if (message->get_request(message))
105 {
106 this->logger->log(this->logger, ERROR | MORE, "Only responses of type IKE_SA_INIT supported in state ike_sa_init_requested");
107 return FAILED;
108 }
109
110 /* parse incoming message */
111 status = message->parse_body(message);
112 if (status != SUCCESS)
113 {
114 this->logger->log(this->logger, ERROR | MORE, "Could not parse body");
115 return status;
116 }
117
118 responder_spi = message->get_responder_spi(message);
119 ike_sa_id = this->ike_sa->public.get_id(&(this->ike_sa->public));
120 ike_sa_id->set_responder_spi(ike_sa_id,responder_spi);
121
122 /* iterate over incoming payloads */
123 status = message->get_payload_iterator(message, &payloads);
124 if (status != SUCCESS)
125 {
126 this->logger->log(this->logger, ERROR, "Could not create payload interator");
127 return status;
128 }
129 while (payloads->has_next(payloads))
130 {
131 payload_t *payload;
132 payloads->current(payloads, (void**)&payload);
133
134 this->logger->log(this->logger, CONTROL|MORE, "Processing payload %s", mapping_find(payload_type_m, payload->get_type(payload)));
135 switch (payload->get_type(payload))
136 {
137 case SECURITY_ASSOCIATION:
138 {
139 sa_payload_t *sa_payload = (sa_payload_t*)payload;
140 iterator_t *suggested_proposals;
141 proposal_substructure_t *suggested_proposal;
142 bool valid;
143
144
145 /* get the list of suggested proposals */
146 status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
147 if (status != SUCCESS)
148 {
149 this->logger->log(this->logger, ERROR, "Fatal errror: Could not create iterator on suggested proposals");
150 payloads->destroy(payloads);
151 return status;
152 }
153
154 /* now let the configuration-manager check the selected proposals*/
155 this->logger->log(this->logger, CONTROL | MOST, "Check suggested proposals");
156 status = global_configuration_manager->check_selected_proposals_for_host(global_configuration_manager,
157 this->ike_sa->get_other_host(this->ike_sa), suggested_proposals,&valid);
158 if (status != SUCCESS)
159 {
160 this->logger->log(this->logger, ERROR | MORE, "Could not check suggested proposals!");
161 suggested_proposals->destroy(suggested_proposals);
162 payloads->destroy(payloads);
163 return status;
164 }
165
166 if (!valid)
167 {
168 this->logger->log(this->logger, ERROR | MORE, "Suggested proposals not accepted!");
169 payloads->destroy(payloads);
170 return status;
171 }
172
173
174 /* let the ike_sa create their own transforms from proposal informations */
175 suggested_proposals->reset(suggested_proposals);
176 /* TODO check for true*/
177 suggested_proposals->has_next(suggested_proposals);
178 status = suggested_proposals->current(suggested_proposals,(void **)&suggested_proposal);
179 suggested_proposals->destroy(suggested_proposals);
180 if (status != SUCCESS)
181 {
182 this->logger->log(this->logger, ERROR | MORE, "Could not get first proposal");
183 payloads->destroy(payloads);
184 return status;
185 }
186
187 status = this->ike_sa->create_transforms_from_proposal(this->ike_sa,suggested_proposal);
188 if (status != SUCCESS)
189 {
190 this->logger->log(this->logger, ERROR | MORE, "Transform objects could not be created from selected proposal");
191 payloads->destroy(payloads);
192 return status;
193 }
194
195
196 /* ok, we have what we need for sa_payload */
197 break;
198 }
199 case KEY_EXCHANGE:
200 {
201 ke_payload_t *ke_payload = (ke_payload_t*)payload;
202
203 status = this->diffie_hellman->set_other_public_value(this->diffie_hellman, ke_payload->get_key_exchange_data(ke_payload));
204 if (status != SUCCESS)
205 {
206 this->logger->log(this->logger, ERROR, "Could not set other public value for DH exchange. Status %s",mapping_find(status_m,status));
207 payloads->destroy(payloads);
208 return OUT_OF_RES;
209 }
210
211 /* shared secret is computed AFTER processing of all payloads... */
212 break;
213 }
214 case NONCE:
215 {
216 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
217
218 if (this->received_nonce.ptr != NULL)
219 {
220 this->logger->log(this->logger, CONTROL | MOST, "Destroy existing received nonce");
221 allocator_free(this->received_nonce.ptr);
222 this->received_nonce.ptr = NULL;
223 this->received_nonce.len = 0;
224 }
225
226 status = nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
227 if (status != SUCCESS)
228 {
229 this->logger->log(this->logger, ERROR, "Fatal error: Could not get received nonce");
230 payloads->destroy(payloads);
231 return OUT_OF_RES;
232 }
233
234 break;
235 }
236 default:
237 {
238 this->logger->log(this->logger, ERROR, "Fatal errror: Payload type not supported!!!!");
239 payloads->destroy(payloads);
240 return FAILED;
241 }
242
243 }
244
245 }
246 payloads->destroy(payloads);
247
248 if (this->shared_secret.ptr != NULL)
249 {
250 this->logger->log(this->logger, CONTROL | MOST, "Destroy existing shared_secret");
251 allocator_free(this->shared_secret.ptr);
252 this->shared_secret.ptr = NULL;
253 this->shared_secret.len = 0;
254 }
255
256
257 /* store shared secret */
258 this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it");
259 status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &(this->shared_secret));
260 this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &this->shared_secret);
261
262 status = this->ike_sa->compute_secrets(this->ike_sa,this->shared_secret,this->sent_nonce, this->received_nonce);
263 if (status != SUCCESS)
264 {
265 /* secrets could not be computed */
266 this->logger->log(this->logger, ERROR | MORE, "Secrets could not be computed!");
267 return status;
268 }
269
270
271
272
273 /****************************
274 *
275 * TODO
276 *
277 * Send IKE_SA_AUTH request
278 *
279 * Make state change
280 *
281 ****************************/
282
283
284 /* set up the reply */
285 // status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
286 // if (status != SUCCESS)
287 // {
288 // return status;
289 // }
290
291 // response->destroy(response);
292
293
294
295 return SUCCESS;
296 }
297
298 /**
299 * Implements state_t.get_state
300 */
301 static ike_sa_state_t get_state(private_ike_sa_init_requested_t *this)
302 {
303 return IKE_SA_INIT_REQUESTED;
304 }
305
306 /**
307 * Implements state_t.get_state
308 */
309 static status_t destroy(private_ike_sa_init_requested_t *this)
310 {
311 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy state of type ike_sa_init_requested_t");
312
313 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie hellman object");
314 this->diffie_hellman->destroy(this->diffie_hellman);
315 if (this->sent_nonce.ptr != NULL)
316 {
317 this->logger->log(this->logger, CONTROL | MOST, "Destroy sent nonce");
318 allocator_free(this->sent_nonce.ptr);
319 }
320 if (this->received_nonce.ptr != NULL)
321 {
322 this->logger->log(this->logger, CONTROL | MOST, "Destroy received nonce");
323 allocator_free(this->received_nonce.ptr);
324 }
325
326 if (this->shared_secret.ptr != NULL)
327 {
328 this->logger->log(this->logger, CONTROL | MOST, "Destroy shared secret");
329 allocator_free(this->shared_secret.ptr);
330 }
331
332 allocator_free(this);
333 return SUCCESS;
334 }
335
336 /*
337 * Described in header.
338 */
339 ike_sa_init_requested_t *ike_sa_init_requested_create(protected_ike_sa_t *ike_sa,u_int16_t dh_group_priority, diffie_hellman_t *diffie_hellman, chunk_t sent_nonce)
340 {
341 private_ike_sa_init_requested_t *this = allocator_alloc_thing(private_ike_sa_init_requested_t);
342
343 if (this == NULL)
344 {
345 return NULL;
346 }
347
348 /* interface functions */
349 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
350 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
351 this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy;
352
353 /* private data */
354 this->ike_sa = ike_sa;
355 this->received_nonce.ptr = NULL;
356 this->received_nonce.len = 0;
357 this->shared_secret.ptr = NULL;
358 this->shared_secret.len = 0;
359 this->logger = this->ike_sa->get_logger(this->ike_sa);
360 this->diffie_hellman = diffie_hellman;
361 this->sent_nonce = sent_nonce;
362 this->dh_group_priority = dh_group_priority;
363
364 return &(this->public);
365 }