Line data Source code
1 : /*
2 : * EAP-IKEv2 peer (RFC 5106)
3 : * Copyright (c) 2007-2014, Jouni Malinen <j@w1.fi>
4 : *
5 : * This software may be distributed under the terms of the BSD license.
6 : * See README for more details.
7 : */
8 :
9 : #include "includes.h"
10 :
11 : #include "common.h"
12 : #include "eap_i.h"
13 : #include "eap_common/eap_ikev2_common.h"
14 : #include "ikev2.h"
15 :
16 :
17 : struct eap_ikev2_data {
18 : struct ikev2_responder_data ikev2;
19 : enum { WAIT_START, PROC_MSG, WAIT_FRAG_ACK, DONE, FAIL } state;
20 : struct wpabuf *in_buf;
21 : struct wpabuf *out_buf;
22 : size_t out_used;
23 : size_t fragment_size;
24 : int keys_ready;
25 : u8 keymat[EAP_MSK_LEN + EAP_EMSK_LEN];
26 : int keymat_ok;
27 : };
28 :
29 :
30 66 : static const char * eap_ikev2_state_txt(int state)
31 : {
32 66 : switch (state) {
33 : case WAIT_START:
34 6 : return "WAIT_START";
35 : case PROC_MSG:
36 39 : return "PROC_MSG";
37 : case WAIT_FRAG_ACK:
38 16 : return "WAIT_FRAG_ACK";
39 : case DONE:
40 5 : return "DONE";
41 : case FAIL:
42 0 : return "FAIL";
43 : default:
44 0 : return "?";
45 : }
46 : }
47 :
48 :
49 33 : static void eap_ikev2_state(struct eap_ikev2_data *data, int state)
50 : {
51 66 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: %s -> %s",
52 33 : eap_ikev2_state_txt(data->state),
53 : eap_ikev2_state_txt(state));
54 33 : data->state = state;
55 33 : }
56 :
57 :
58 6 : static void * eap_ikev2_init(struct eap_sm *sm)
59 : {
60 : struct eap_ikev2_data *data;
61 : const u8 *identity, *password;
62 : size_t identity_len, password_len;
63 : int fragment_size;
64 :
65 6 : identity = eap_get_config_identity(sm, &identity_len);
66 6 : if (identity == NULL) {
67 0 : wpa_printf(MSG_INFO, "EAP-IKEV2: No identity available");
68 0 : return NULL;
69 : }
70 :
71 6 : data = os_zalloc(sizeof(*data));
72 6 : if (data == NULL)
73 0 : return NULL;
74 6 : data->state = WAIT_START;
75 6 : fragment_size = eap_get_config_fragment_size(sm);
76 6 : if (fragment_size <= 0)
77 0 : data->fragment_size = IKEV2_FRAGMENT_SIZE;
78 : else
79 6 : data->fragment_size = fragment_size;
80 6 : data->ikev2.state = SA_INIT;
81 6 : data->ikev2.peer_auth = PEER_AUTH_SECRET;
82 6 : data->ikev2.key_pad = (u8 *) os_strdup("Key Pad for EAP-IKEv2");
83 6 : if (data->ikev2.key_pad == NULL)
84 0 : goto failed;
85 6 : data->ikev2.key_pad_len = 21;
86 6 : data->ikev2.IDr = os_malloc(identity_len);
87 6 : if (data->ikev2.IDr == NULL)
88 0 : goto failed;
89 6 : os_memcpy(data->ikev2.IDr, identity, identity_len);
90 6 : data->ikev2.IDr_len = identity_len;
91 :
92 6 : password = eap_get_config_password(sm, &password_len);
93 6 : if (password) {
94 6 : data->ikev2.shared_secret = os_malloc(password_len);
95 6 : if (data->ikev2.shared_secret == NULL)
96 0 : goto failed;
97 6 : os_memcpy(data->ikev2.shared_secret, password, password_len);
98 6 : data->ikev2.shared_secret_len = password_len;
99 : }
100 :
101 6 : return data;
102 :
103 : failed:
104 0 : ikev2_responder_deinit(&data->ikev2);
105 0 : os_free(data);
106 0 : return NULL;
107 : }
108 :
109 :
110 6 : static void eap_ikev2_deinit(struct eap_sm *sm, void *priv)
111 : {
112 6 : struct eap_ikev2_data *data = priv;
113 6 : wpabuf_free(data->in_buf);
114 6 : wpabuf_free(data->out_buf);
115 6 : ikev2_responder_deinit(&data->ikev2);
116 6 : os_free(data);
117 6 : }
118 :
119 :
120 5 : static int eap_ikev2_peer_keymat(struct eap_ikev2_data *data)
121 : {
122 15 : if (eap_ikev2_derive_keymat(
123 : data->ikev2.proposal.prf, &data->ikev2.keys,
124 5 : data->ikev2.i_nonce, data->ikev2.i_nonce_len,
125 5 : data->ikev2.r_nonce, data->ikev2.r_nonce_len,
126 5 : data->keymat) < 0) {
127 0 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Failed to "
128 : "derive key material");
129 0 : return -1;
130 : }
131 5 : data->keymat_ok = 1;
132 5 : return 0;
133 : }
134 :
135 :
136 20 : static struct wpabuf * eap_ikev2_build_msg(struct eap_ikev2_data *data,
137 : struct eap_method_ret *ret, u8 id)
138 : {
139 : struct wpabuf *resp;
140 : u8 flags;
141 20 : size_t send_len, plen, icv_len = 0;
142 :
143 20 : ret->ignore = FALSE;
144 20 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Generating Response");
145 20 : ret->allowNotifications = TRUE;
146 :
147 20 : flags = 0;
148 20 : send_len = wpabuf_len(data->out_buf) - data->out_used;
149 20 : if (1 + send_len > data->fragment_size) {
150 8 : send_len = data->fragment_size - 1;
151 8 : flags |= IKEV2_FLAGS_MORE_FRAGMENTS;
152 8 : if (data->out_used == 0) {
153 2 : flags |= IKEV2_FLAGS_LENGTH_INCLUDED;
154 2 : send_len -= 4;
155 : }
156 : }
157 : #ifdef CCNS_PL
158 : /* Some issues figuring out the length of the message if Message Length
159 : * field not included?! */
160 : if (!(flags & IKEV2_FLAGS_LENGTH_INCLUDED))
161 : flags |= IKEV2_FLAGS_LENGTH_INCLUDED;
162 : #endif /* CCNS_PL */
163 :
164 20 : plen = 1 + send_len;
165 20 : if (flags & IKEV2_FLAGS_LENGTH_INCLUDED)
166 2 : plen += 4;
167 20 : if (data->keys_ready) {
168 : const struct ikev2_integ_alg *integ;
169 8 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Add Integrity Checksum "
170 : "Data");
171 8 : flags |= IKEV2_FLAGS_ICV_INCLUDED;
172 8 : integ = ikev2_get_integ(data->ikev2.proposal.integ);
173 8 : if (integ == NULL) {
174 0 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Unknown INTEG "
175 : "transform / cannot generate ICV");
176 0 : return NULL;
177 : }
178 8 : icv_len = integ->hash_len;
179 :
180 8 : plen += icv_len;
181 : }
182 20 : resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IKEV2, plen,
183 : EAP_CODE_RESPONSE, id);
184 20 : if (resp == NULL)
185 0 : return NULL;
186 :
187 20 : wpabuf_put_u8(resp, flags); /* Flags */
188 20 : if (flags & IKEV2_FLAGS_LENGTH_INCLUDED)
189 2 : wpabuf_put_be32(resp, wpabuf_len(data->out_buf));
190 :
191 20 : wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
192 : send_len);
193 20 : data->out_used += send_len;
194 :
195 20 : if (flags & IKEV2_FLAGS_ICV_INCLUDED) {
196 8 : const u8 *msg = wpabuf_head(resp);
197 8 : size_t len = wpabuf_len(resp);
198 16 : ikev2_integ_hash(data->ikev2.proposal.integ,
199 8 : data->ikev2.keys.SK_ar,
200 : data->ikev2.keys.SK_integ_len,
201 8 : msg, len, wpabuf_put(resp, icv_len));
202 : }
203 :
204 20 : ret->methodState = METHOD_MAY_CONT;
205 20 : ret->decision = DECISION_FAIL;
206 :
207 20 : if (data->out_used == wpabuf_len(data->out_buf)) {
208 12 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Sending out %lu bytes "
209 : "(message sent completely)",
210 : (unsigned long) send_len);
211 12 : wpabuf_free(data->out_buf);
212 12 : data->out_buf = NULL;
213 12 : data->out_used = 0;
214 12 : switch (data->ikev2.state) {
215 : case SA_AUTH:
216 : /* SA_INIT was sent out, so message have to be
217 : * integrity protected from now on. */
218 6 : data->keys_ready = 1;
219 6 : break;
220 : case IKEV2_DONE:
221 5 : ret->methodState = METHOD_DONE;
222 5 : if (data->state == FAIL)
223 0 : break;
224 5 : ret->decision = DECISION_COND_SUCC;
225 5 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Authentication "
226 : "completed successfully");
227 5 : if (eap_ikev2_peer_keymat(data))
228 0 : break;
229 5 : eap_ikev2_state(data, DONE);
230 5 : break;
231 : case IKEV2_FAILED:
232 1 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Authentication "
233 : "failed");
234 1 : ret->methodState = METHOD_DONE;
235 1 : ret->decision = DECISION_FAIL;
236 1 : break;
237 : default:
238 0 : break;
239 : }
240 : } else {
241 8 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Sending out %lu bytes "
242 : "(%lu more to send)", (unsigned long) send_len,
243 8 : (unsigned long) wpabuf_len(data->out_buf) -
244 8 : data->out_used);
245 8 : eap_ikev2_state(data, WAIT_FRAG_ACK);
246 : }
247 :
248 20 : return resp;
249 : }
250 :
251 :
252 32 : static int eap_ikev2_process_icv(struct eap_ikev2_data *data,
253 : const struct wpabuf *reqData,
254 : u8 flags, const u8 *pos, const u8 **end,
255 : int frag_ack)
256 : {
257 32 : if (flags & IKEV2_FLAGS_ICV_INCLUDED) {
258 10 : int icv_len = eap_ikev2_validate_icv(
259 : data->ikev2.proposal.integ, &data->ikev2.keys, 1,
260 : reqData, pos, *end);
261 10 : if (icv_len < 0)
262 0 : return -1;
263 : /* Hide Integrity Checksum Data from further processing */
264 10 : *end -= icv_len;
265 22 : } else if (data->keys_ready && !frag_ack) {
266 0 : wpa_printf(MSG_INFO, "EAP-IKEV2: The message should have "
267 : "included integrity checksum");
268 0 : return -1;
269 : }
270 :
271 32 : return 0;
272 : }
273 :
274 :
275 12 : static int eap_ikev2_process_cont(struct eap_ikev2_data *data,
276 : const u8 *buf, size_t len)
277 : {
278 : /* Process continuation of a pending message */
279 12 : if (len > wpabuf_tailroom(data->in_buf)) {
280 0 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Fragment overflow");
281 0 : eap_ikev2_state(data, FAIL);
282 0 : return -1;
283 : }
284 :
285 12 : wpabuf_put_data(data->in_buf, buf, len);
286 12 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Received %lu bytes, waiting "
287 : "for %lu bytes more", (unsigned long) len,
288 12 : (unsigned long) wpabuf_tailroom(data->in_buf));
289 :
290 12 : return 0;
291 : }
292 :
293 :
294 12 : static struct wpabuf * eap_ikev2_process_fragment(struct eap_ikev2_data *data,
295 : struct eap_method_ret *ret,
296 : u8 id, u8 flags,
297 : u32 message_length,
298 : const u8 *buf, size_t len)
299 : {
300 : /* Process a fragment that is not the last one of the message */
301 12 : if (data->in_buf == NULL && !(flags & IKEV2_FLAGS_LENGTH_INCLUDED)) {
302 0 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: No Message Length field in "
303 : "a fragmented packet");
304 0 : ret->ignore = TRUE;
305 0 : return NULL;
306 : }
307 :
308 12 : if (data->in_buf == NULL) {
309 : /* First fragment of the message */
310 4 : data->in_buf = wpabuf_alloc(message_length);
311 4 : if (data->in_buf == NULL) {
312 0 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: No memory for "
313 : "message");
314 0 : ret->ignore = TRUE;
315 0 : return NULL;
316 : }
317 4 : wpabuf_put_data(data->in_buf, buf, len);
318 4 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Received %lu bytes in first "
319 : "fragment, waiting for %lu bytes more",
320 : (unsigned long) len,
321 4 : (unsigned long) wpabuf_tailroom(data->in_buf));
322 : }
323 :
324 12 : return eap_ikev2_build_frag_ack(id, EAP_CODE_RESPONSE);
325 : }
326 :
327 :
328 32 : static struct wpabuf * eap_ikev2_process(struct eap_sm *sm, void *priv,
329 : struct eap_method_ret *ret,
330 : const struct wpabuf *reqData)
331 : {
332 32 : struct eap_ikev2_data *data = priv;
333 : const u8 *start, *pos, *end;
334 : size_t len;
335 : u8 flags, id;
336 32 : u32 message_length = 0;
337 : struct wpabuf tmpbuf;
338 :
339 32 : pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IKEV2, reqData, &len);
340 32 : if (pos == NULL) {
341 0 : ret->ignore = TRUE;
342 0 : return NULL;
343 : }
344 :
345 32 : id = eap_get_id(reqData);
346 :
347 32 : start = pos;
348 32 : end = start + len;
349 :
350 32 : if (len == 0)
351 8 : flags = 0; /* fragment ack */
352 : else
353 24 : flags = *pos++;
354 :
355 40 : if (eap_ikev2_process_icv(data, reqData, flags, pos, &end,
356 40 : data->state == WAIT_FRAG_ACK && len == 0) < 0)
357 : {
358 0 : ret->ignore = TRUE;
359 0 : return NULL;
360 : }
361 :
362 32 : if (flags & IKEV2_FLAGS_LENGTH_INCLUDED) {
363 4 : if (end - pos < 4) {
364 0 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Message underflow");
365 0 : ret->ignore = TRUE;
366 0 : return NULL;
367 : }
368 4 : message_length = WPA_GET_BE32(pos);
369 4 : pos += 4;
370 :
371 4 : if (message_length < (u32) (end - pos)) {
372 0 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Invalid Message "
373 : "Length (%d; %ld remaining in this msg)",
374 : message_length, (long) (end - pos));
375 0 : ret->ignore = TRUE;
376 0 : return NULL;
377 : }
378 : }
379 :
380 32 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Received packet: Flags 0x%x "
381 : "Message Length %u", flags, message_length);
382 :
383 32 : if (data->state == WAIT_FRAG_ACK) {
384 : #ifdef CCNS_PL
385 : if (len > 1) /* Empty Flags field included in ACK */
386 : #else /* CCNS_PL */
387 8 : if (len != 0)
388 : #endif /* CCNS_PL */
389 : {
390 0 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Unexpected payload "
391 : "in WAIT_FRAG_ACK state");
392 0 : ret->ignore = TRUE;
393 0 : return NULL;
394 : }
395 8 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Fragment acknowledged");
396 8 : eap_ikev2_state(data, PROC_MSG);
397 8 : return eap_ikev2_build_msg(data, ret, id);
398 : }
399 :
400 24 : if (data->in_buf && eap_ikev2_process_cont(data, pos, end - pos) < 0) {
401 0 : ret->ignore = TRUE;
402 0 : return NULL;
403 : }
404 :
405 24 : if (flags & IKEV2_FLAGS_MORE_FRAGMENTS) {
406 12 : return eap_ikev2_process_fragment(data, ret, id, flags,
407 : message_length, pos,
408 12 : end - pos);
409 : }
410 :
411 12 : if (data->in_buf == NULL) {
412 : /* Wrap unfragmented messages as wpabuf without extra copy */
413 8 : wpabuf_set(&tmpbuf, pos, end - pos);
414 8 : data->in_buf = &tmpbuf;
415 : }
416 :
417 12 : if (ikev2_responder_process(&data->ikev2, data->in_buf) < 0) {
418 0 : if (data->in_buf == &tmpbuf)
419 0 : data->in_buf = NULL;
420 0 : eap_ikev2_state(data, FAIL);
421 0 : return NULL;
422 : }
423 :
424 12 : if (data->in_buf != &tmpbuf)
425 4 : wpabuf_free(data->in_buf);
426 12 : data->in_buf = NULL;
427 :
428 12 : if (data->out_buf == NULL) {
429 12 : data->out_buf = ikev2_responder_build(&data->ikev2);
430 12 : if (data->out_buf == NULL) {
431 0 : wpa_printf(MSG_DEBUG, "EAP-IKEV2: Failed to generate "
432 : "IKEv2 message");
433 0 : return NULL;
434 : }
435 12 : data->out_used = 0;
436 : }
437 :
438 12 : eap_ikev2_state(data, PROC_MSG);
439 12 : return eap_ikev2_build_msg(data, ret, id);
440 : }
441 :
442 :
443 32 : static Boolean eap_ikev2_isKeyAvailable(struct eap_sm *sm, void *priv)
444 : {
445 32 : struct eap_ikev2_data *data = priv;
446 32 : return data->state == DONE && data->keymat_ok;
447 : }
448 :
449 :
450 5 : static u8 * eap_ikev2_getKey(struct eap_sm *sm, void *priv, size_t *len)
451 : {
452 5 : struct eap_ikev2_data *data = priv;
453 : u8 *key;
454 :
455 5 : if (data->state != DONE || !data->keymat_ok)
456 0 : return NULL;
457 :
458 5 : key = os_malloc(EAP_MSK_LEN);
459 5 : if (key) {
460 5 : os_memcpy(key, data->keymat, EAP_MSK_LEN);
461 5 : *len = EAP_MSK_LEN;
462 : }
463 :
464 5 : return key;
465 : }
466 :
467 :
468 0 : static u8 * eap_ikev2_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
469 : {
470 0 : struct eap_ikev2_data *data = priv;
471 : u8 *key;
472 :
473 0 : if (data->state != DONE || !data->keymat_ok)
474 0 : return NULL;
475 :
476 0 : key = os_malloc(EAP_EMSK_LEN);
477 0 : if (key) {
478 0 : os_memcpy(key, data->keymat + EAP_MSK_LEN, EAP_EMSK_LEN);
479 0 : *len = EAP_EMSK_LEN;
480 : }
481 :
482 0 : return key;
483 : }
484 :
485 :
486 5 : static u8 * eap_ikev2_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
487 : {
488 5 : struct eap_ikev2_data *data = priv;
489 : u8 *sid;
490 : size_t sid_len;
491 : size_t offset;
492 :
493 5 : if (data->state != DONE || !data->keymat_ok)
494 0 : return NULL;
495 :
496 5 : sid_len = 1 + data->ikev2.i_nonce_len + data->ikev2.r_nonce_len;
497 5 : sid = os_malloc(sid_len);
498 5 : if (sid) {
499 5 : offset = 0;
500 5 : sid[offset] = EAP_TYPE_IKEV2;
501 5 : offset++;
502 5 : os_memcpy(sid + offset, data->ikev2.i_nonce,
503 : data->ikev2.i_nonce_len);
504 5 : offset += data->ikev2.i_nonce_len;
505 5 : os_memcpy(sid + offset, data->ikev2.r_nonce,
506 : data->ikev2.r_nonce_len);
507 5 : *len = sid_len;
508 5 : wpa_hexdump(MSG_DEBUG, "EAP-IKEV2: Derived Session-Id",
509 : sid, sid_len);
510 : }
511 :
512 5 : return sid;
513 : }
514 :
515 :
516 4 : int eap_peer_ikev2_register(void)
517 : {
518 : struct eap_method *eap;
519 : int ret;
520 :
521 4 : eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
522 : EAP_VENDOR_IETF, EAP_TYPE_IKEV2,
523 : "IKEV2");
524 4 : if (eap == NULL)
525 0 : return -1;
526 :
527 4 : eap->init = eap_ikev2_init;
528 4 : eap->deinit = eap_ikev2_deinit;
529 4 : eap->process = eap_ikev2_process;
530 4 : eap->isKeyAvailable = eap_ikev2_isKeyAvailable;
531 4 : eap->getKey = eap_ikev2_getKey;
532 4 : eap->get_emsk = eap_ikev2_get_emsk;
533 4 : eap->getSessionId = eap_ikev2_get_session_id;
534 :
535 4 : ret = eap_peer_method_register(eap);
536 4 : if (ret)
537 0 : eap_peer_method_free(eap);
538 4 : return ret;
539 : }
|