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