040942aee9c3ce1c9bc1f076441503109fc91457
[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, state_t **new_state)
89 {
90 status_t status;
91 iterator_t *payloads;
92 exchange_type_t exchange_type;
93 u_int64_t responder_spi;
94
95 exchange_type = message->get_exchange_type(message);
96 if (exchange_type != IKE_SA_INIT)
97 {
98 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));
99 return FAILED;
100 }
101
102 if (message->get_request(message))
103 {
104 this->logger->log(this->logger, ERROR | MORE, "Only responses of type IKE_SA_INIT supported in state ike_sa_init_requested");
105 return FAILED;
106 }
107
108 /* parse incoming message */
109 status = message->parse_body(message);
110 if (status != SUCCESS)
111 {
112 this->logger->log(this->logger, ERROR | MORE, "Could not parse body");
113 return status;
114 }
115
116 responder_spi = message->get_responder_spi(message);
117 this->ike_sa->ike_sa_id->set_responder_spi(this->ike_sa->ike_sa_id,responder_spi);
118
119 /* iterate over incoming payloads */
120 status = message->get_payload_iterator(message, &payloads);
121 if (status != SUCCESS)
122 {
123 this->logger->log(this->logger, ERROR, "Could not create payload interator");
124 return status;
125 }
126 while (payloads->has_next(payloads))
127 {
128 payload_t *payload;
129 payloads->current(payloads, (void**)&payload);
130
131 this->logger->log(this->logger, CONTROL|MORE, "Processing payload %s", mapping_find(payload_type_m, payload->get_type(payload)));
132 switch (payload->get_type(payload))
133 {
134 case SECURITY_ASSOCIATION:
135 {
136 sa_payload_t *sa_payload = (sa_payload_t*)payload;
137 iterator_t *suggested_proposals;
138 encryption_algorithm_t encryption_algorithm = ENCR_UNDEFINED;
139 pseudo_random_function_t pseudo_random_function = PRF_UNDEFINED;
140 integrity_algorithm_t integrity_algorithm = AUTH_UNDEFINED;
141
142 /* get the list of suggested proposals */
143 status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
144 if (status != SUCCESS)
145 {
146 this->logger->log(this->logger, ERROR, "Fatal errror: Could not create iterator on suggested proposals");
147 payloads->destroy(payloads);
148 return status;
149 }
150
151 /* now let the configuration-manager return the transforms for the given proposal*/
152 this->logger->log(this->logger, CONTROL | MOST, "Get transforms for suggested proposal");
153 status = global_configuration_manager->get_transforms_for_host_and_proposals(global_configuration_manager,
154 this->ike_sa->other.host, suggested_proposals, &encryption_algorithm,&pseudo_random_function,&integrity_algorithm);
155 if (status != SUCCESS)
156 {
157 this->logger->log(this->logger, ERROR | MORE, "Suggested proposals not supported!");
158 suggested_proposals->destroy(suggested_proposals);
159 payloads->destroy(payloads);
160 return status;
161 }
162 suggested_proposals->destroy(suggested_proposals);
163
164 this->ike_sa->prf = prf_create(pseudo_random_function);
165 if (this->ike_sa->prf == NULL)
166 {
167 this->logger->log(this->logger, ERROR | MORE, "PRF type not supported");
168 payloads->destroy(payloads);
169 return FAILED;
170 }
171
172
173 /* ok, we have what we need for sa_payload */
174 break;
175 }
176 case KEY_EXCHANGE:
177 {
178 ke_payload_t *ke_payload = (ke_payload_t*)payload;
179
180 status = this->diffie_hellman->set_other_public_value(this->diffie_hellman, ke_payload->get_key_exchange_data(ke_payload));
181 if (status != SUCCESS)
182 {
183 this->logger->log(this->logger, ERROR, "Could not set other public value for DH exchange. Status %s",mapping_find(status_m,status));
184 payloads->destroy(payloads);
185 return OUT_OF_RES;
186 }
187
188 /* shared secret is computed AFTER processing of all payloads... */
189 break;
190 }
191 case NONCE:
192 {
193 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
194
195 if (this->received_nonce.ptr != NULL)
196 {
197 this->logger->log(this->logger, CONTROL | MOST, "Destroy existing received nonce");
198 allocator_free(this->received_nonce.ptr);
199 this->received_nonce.ptr = NULL;
200 this->received_nonce.len = 0;
201 }
202
203 status = nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
204 if (status != SUCCESS)
205 {
206 this->logger->log(this->logger, ERROR, "Fatal error: Could not get received nonce");
207 payloads->destroy(payloads);
208 return OUT_OF_RES;
209 }
210
211 break;
212 }
213 default:
214 {
215 this->logger->log(this->logger, ERROR, "Fatal errror: Payload type not supported!!!!");
216 payloads->destroy(payloads);
217 return FAILED;
218 }
219
220 }
221
222 }
223 payloads->destroy(payloads);
224
225 if (this->shared_secret.ptr != NULL)
226 {
227 this->logger->log(this->logger, CONTROL | MOST, "Destroy existing shared_secret");
228 allocator_free(this->shared_secret.ptr);
229 this->shared_secret.ptr = NULL;
230 this->shared_secret.len = 0;
231 }
232
233
234 /* store shared secret */
235 this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it");
236 status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &(this->shared_secret));
237 this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &this->shared_secret);
238
239 status = this->ike_sa->compute_secrets(this->ike_sa,this->shared_secret,this->sent_nonce, this->received_nonce);
240 if (status != SUCCESS)
241 {
242 /* secrets could not be computed */
243 this->logger->log(this->logger, ERROR | MORE, "Secrets could not be computed!");
244 return status;
245 }
246
247
248
249
250 /****************************
251 *
252 * TODO
253 *
254 * Create PRF+ object
255 *
256 * Create Keys for next process
257 *
258 * Send IKE_SA_AUTH request
259 *
260 ****************************/
261
262
263 /* set up the reply */
264 // status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
265 // if (status != SUCCESS)
266 // {
267 // return status;
268 // }
269
270 // response->destroy(response);
271
272 *new_state = (state_t *) this;
273
274 return SUCCESS;
275 }
276
277 /**
278 * Implements state_t.get_state
279 */
280 static ike_sa_state_t get_state(private_ike_sa_init_requested_t *this)
281 {
282 return IKE_SA_INIT_REQUESTED;
283 }
284
285 /**
286 * Implements state_t.get_state
287 */
288 static status_t destroy(private_ike_sa_init_requested_t *this)
289 {
290 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy state of type ike_sa_init_requested_t");
291
292 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie hellman object");
293 this->diffie_hellman->destroy(this->diffie_hellman);
294 if (this->sent_nonce.ptr != NULL)
295 {
296 this->logger->log(this->logger, CONTROL | MOST, "Destroy sent nonce");
297 allocator_free(this->sent_nonce.ptr);
298 }
299 if (this->received_nonce.ptr != NULL)
300 {
301 this->logger->log(this->logger, CONTROL | MOST, "Destroy received nonce");
302 allocator_free(this->received_nonce.ptr);
303 }
304
305 if (this->shared_secret.ptr != NULL)
306 {
307 this->logger->log(this->logger, CONTROL | MOST, "Destroy shared secret");
308 allocator_free(this->shared_secret.ptr);
309 }
310
311 allocator_free(this);
312 return SUCCESS;
313 }
314
315 /*
316 * Described in header.
317 */
318 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)
319 {
320 private_ike_sa_init_requested_t *this = allocator_alloc_thing(private_ike_sa_init_requested_t);
321
322 if (this == NULL)
323 {
324 return NULL;
325 }
326
327 /* interface functions */
328 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *,state_t **)) process_message;
329 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
330 this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy;
331
332 /* private data */
333 this->ike_sa = ike_sa;
334 this->received_nonce.ptr = NULL;
335 this->received_nonce.len = 0;
336 this->shared_secret.ptr = NULL;
337 this->shared_secret.len = 0;
338 this->logger = this->ike_sa->logger;
339 this->diffie_hellman = diffie_hellman;
340 this->sent_nonce = sent_nonce;
341 this->dh_group_priority = dh_group_priority;
342
343 return &(this->public);
344 }