Use the EAP-SIM/AKA crypto helper in EAP-SIM
[strongswan.git] / src / charon / plugins / eap_sim / eap_sim_server.c
1 /*
2 * Copyright (C) 2007-2009 Martin Willi
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "eap_sim_server.h"
17
18 #include <daemon.h>
19
20 #include <simaka_message.h>
21 #include <simaka_crypto.h>
22
23 /* number of triplets for one authentication */
24 #define TRIPLET_COUNT 3
25
26 /** length of the AT_MAC value */
27 #define MAC_LEN 16
28 /** length of the AT_RAND value */
29 #define RAND_LEN 16
30 /** length of Kc */
31 #define KC_LEN 8
32 /** length of SRES */
33 #define SRES_LEN 4
34
35 typedef struct private_eap_sim_server_t private_eap_sim_server_t;
36
37 /**
38 * Private data of an eap_sim_server_t object.
39 */
40 struct private_eap_sim_server_t {
41
42 /**
43 * Public authenticator_t interface.
44 */
45 eap_sim_server_t public;
46
47 /**
48 * permanent ID of peer
49 */
50 identification_t *peer;
51
52 /**
53 * EAP-SIM/AKA crypto helper
54 */
55 simaka_crypto_t *crypto;
56
57 /**
58 * unique EAP identifier
59 */
60 u_int8_t identifier;
61
62 /**
63 * concatenated SRES values
64 */
65 chunk_t sreses;
66
67 /**
68 * MSK, used for EAP-SIM based IKEv2 authentication
69 */
70 chunk_t msk;
71 };
72
73 /* version of SIM protocol we speak */
74 static chunk_t version = chunk_from_chars(0x00,0x01);
75
76 /**
77 * Fetch a triplet from a provider
78 */
79 static bool get_provider_triplet(private_eap_sim_server_t *this,
80 char *rand, char *sres, char *kc)
81 {
82 enumerator_t *enumerator;
83 sim_provider_t *provider;
84 int tried = 0;
85
86 enumerator = charon->sim->create_provider_enumerator(charon->sim);
87 while (enumerator->enumerate(enumerator, &provider))
88 {
89 if (provider->get_triplet(provider, this->peer, rand, sres, kc))
90 {
91 enumerator->destroy(enumerator);
92 return TRUE;
93 }
94 tried++;
95 }
96 enumerator->destroy(enumerator);
97 DBG1(DBG_IKE, "tried %d SIM providers, but none had a triplet for '%Y'",
98 tried, this->peer);
99 return FALSE;
100 }
101
102 /**
103 * process an EAP-SIM/Response/Start message
104 */
105 static status_t process_start(private_eap_sim_server_t *this,
106 simaka_message_t *in, eap_payload_t **out)
107 {
108 simaka_message_t *message;
109 enumerator_t *enumerator;
110 simaka_attribute_t type;
111 chunk_t data, rands, rand, kcs, kc, sreses, sres, nonce = chunk_empty;
112 bool supported = FALSE;
113 int i;
114
115 enumerator = in->create_attribute_enumerator(in);
116 while (enumerator->enumerate(enumerator, &type, &data))
117 {
118 switch (type)
119 {
120 case AT_NONCE_MT:
121 nonce = data;
122 break;
123 case AT_SELECTED_VERSION:
124 if (chunk_equals(data, version))
125 {
126 supported = TRUE;
127 }
128 break;
129 default:
130 DBG1(DBG_IKE, "ignoring EAP-SIM attribute %N",
131 simaka_attribute_names, type);
132 break;
133 }
134 }
135 enumerator->destroy(enumerator);
136
137 if (!supported || !nonce.len)
138 {
139 DBG1(DBG_IKE, "received incomplete EAP-SIM/Response/Start");
140 return FAILED;
141 }
142
143 /* read triplets from provider */
144 rand = rands = chunk_alloca(RAND_LEN * TRIPLET_COUNT);
145 kc = kcs = chunk_alloca(KC_LEN * TRIPLET_COUNT);
146 sres = sreses = chunk_alloca(SRES_LEN * TRIPLET_COUNT);
147 rands.len = kcs.len = sreses.len = 0;
148 for (i = 0; i < TRIPLET_COUNT; i++)
149 {
150 if (!get_provider_triplet(this, rand.ptr, sres.ptr, kc.ptr))
151 {
152 DBG1(DBG_IKE, "getting EAP-SIM triplet %d failed", i);
153 return FAILED;
154 }
155 rands.len += RAND_LEN;
156 sreses.len += SRES_LEN;
157 kcs.len += KC_LEN;
158 rand = chunk_skip(rand, RAND_LEN);
159 sres = chunk_skip(sres, SRES_LEN);
160 kc = chunk_skip(kc, KC_LEN);
161 }
162 free(this->sreses.ptr);
163 this->sreses = chunk_clone(sreses);
164
165 data = chunk_cata("cccc", kcs, nonce, version, version);
166 free(this->msk.ptr);
167 this->msk = this->crypto->derive_keys_full(this->crypto, this->peer, data);
168
169 /* build response with AT_MAC, built over "EAP packet | NONCE_MT" */
170 message = simaka_message_create(TRUE, this->identifier++,
171 EAP_SIM, SIM_CHALLENGE);
172 message->add_attribute(message, AT_RAND, rands);
173 *out = message->generate(message, this->crypto, nonce);
174 message->destroy(message);
175 return NEED_MORE;
176 }
177
178 /**
179 * process an EAP-SIM/Response/Challenge message
180 */
181 static status_t process_challenge(private_eap_sim_server_t *this,
182 simaka_message_t *in, eap_payload_t **out)
183 {
184 enumerator_t *enumerator;
185 simaka_attribute_t type;
186 chunk_t data;
187
188 enumerator = in->create_attribute_enumerator(in);
189 while (enumerator->enumerate(enumerator, &type, &data))
190 {
191 DBG1(DBG_IKE, "ignoring EAP-SIM attribute %N",
192 simaka_attribute_names, type);
193 }
194 enumerator->destroy(enumerator);
195
196 /* verify AT_MAC attribute, signature is over "EAP packet | n*SRES" */
197 if (!in->verify(in, this->crypto, this->sreses))
198 {
199 DBG1(DBG_IKE, "AT_MAC verification failed");
200 return FAILED;
201 }
202 return SUCCESS;
203 }
204
205 /**
206 * EAP-SIM/Response/ClientErrorCode message
207 */
208 static status_t process_client_error(private_eap_sim_server_t *this,
209 simaka_message_t *in, eap_payload_t **out)
210 {
211 enumerator_t *enumerator;
212 simaka_attribute_t type;
213 chunk_t data;
214
215 enumerator = in->create_attribute_enumerator(in);
216 while (enumerator->enumerate(enumerator, &type, &data))
217 {
218 if (type == AT_CLIENT_ERROR_CODE)
219 {
220 DBG1(DBG_IKE, "received EAP-SIM client error code %#B", &data);
221 }
222 else
223 {
224 DBG1(DBG_IKE, "ignoring EAP-SIM attribute %N",
225 simaka_attribute_names, type);
226 }
227 }
228 enumerator->destroy(enumerator);
229 return FAILED;
230 }
231
232 /**
233 * Implementation of eap_method_t.process
234 */
235 static status_t process(private_eap_sim_server_t *this,
236 eap_payload_t *in, eap_payload_t **out)
237 {
238 simaka_message_t *message;
239 status_t status;
240
241 message = simaka_message_create_from_payload(in);
242 if (!message)
243 {
244 return FAILED;
245 }
246 if (!message->parse(message, this->crypto))
247 {
248 message->destroy(message);
249 return FAILED;
250 }
251 switch (message->get_subtype(message))
252 {
253 case SIM_START:
254 status = process_start(this, message, out);
255 break;
256 case SIM_CHALLENGE:
257 status = process_challenge(this, message, out);
258 break;
259 case SIM_CLIENT_ERROR:
260 status = process_client_error(this, message, out);
261 break;
262 default:
263 DBG1(DBG_IKE, "unable to process EAP-SIM subtype %N",
264 simaka_subtype_names, message->get_subtype(message));
265 status = FAILED;
266 break;
267 }
268 message->destroy(message);
269 return status;
270 }
271
272 /**
273 * Implementation of eap_method_t.initiate
274 */
275 static status_t initiate(private_eap_sim_server_t *this, eap_payload_t **out)
276 {
277 simaka_message_t *message;
278
279 message = simaka_message_create(TRUE, this->identifier++,
280 EAP_SIM, SIM_START);
281 message->add_attribute(message, AT_VERSION_LIST, version);
282 *out = message->generate(message, this->crypto, chunk_empty);
283 message->destroy(message);
284 return NEED_MORE;
285 }
286
287 /**
288 * Implementation of eap_method_t.get_type.
289 */
290 static eap_type_t get_type(private_eap_sim_server_t *this, u_int32_t *vendor)
291 {
292 *vendor = 0;
293 return EAP_SIM;
294 }
295
296 /**
297 * Implementation of eap_method_t.get_msk.
298 */
299 static status_t get_msk(private_eap_sim_server_t *this, chunk_t *msk)
300 {
301 if (this->msk.ptr)
302 {
303 *msk = this->msk;
304 return SUCCESS;
305 }
306 return FAILED;
307 }
308
309 /**
310 * Implementation of eap_method_t.is_mutual.
311 */
312 static bool is_mutual(private_eap_sim_server_t *this)
313 {
314 return TRUE;
315 }
316
317 /**
318 * Implementation of eap_method_t.destroy.
319 */
320 static void destroy(private_eap_sim_server_t *this)
321 {
322 this->crypto->destroy(this->crypto);
323 this->peer->destroy(this->peer);
324 free(this->sreses.ptr);
325 free(this->msk.ptr);
326 free(this);
327 }
328
329 /*
330 * Described in header.
331 */
332 eap_sim_server_t *eap_sim_server_create(identification_t *server,
333 identification_t *peer)
334 {
335 private_eap_sim_server_t *this = malloc_thing(private_eap_sim_server_t);
336
337 this->public.interface.initiate = (status_t(*)(eap_method_t*,eap_payload_t**))initiate;
338 this->public.interface.process = (status_t(*)(eap_method_t*,eap_payload_t*,eap_payload_t**))process;
339 this->public.interface.get_type = (eap_type_t(*)(eap_method_t*,u_int32_t*))get_type;
340 this->public.interface.is_mutual = (bool(*)(eap_method_t*))is_mutual;
341 this->public.interface.get_msk = (status_t(*)(eap_method_t*,chunk_t*))get_msk;
342 this->public.interface.destroy = (void(*)(eap_method_t*))destroy;
343
344 this->crypto = simaka_crypto_create();
345 if (!this->crypto)
346 {
347 free(this);
348 return NULL;
349 }
350 this->peer = peer->clone(peer);
351 this->sreses = chunk_empty;
352 this->msk = chunk_empty;
353 /* generate a non-zero identifier */
354 do {
355 this->identifier = random();
356 } while (!this->identifier);
357
358 return &this->public;
359 }
360