1c164510sam/*
2c164510sam * EAP-IKEv2 server (RFC 5106)
3c164510sam * Copyright (c) 2007, Jouni Malinen <j@w1.fi>
4c164510sam *
55e9e13erpaulo * This software may be distributed under the terms of the BSD license.
65e9e13erpaulo * See README for more details.
7c164510sam */
8c164510sam
9c164510sam#include "includes.h"
10c164510sam
11c164510sam#include "common.h"
12c164510sam#include "eap_i.h"
13c164510sam#include "eap_common/eap_ikev2_common.h"
14c164510sam#include "ikev2.h"
15c164510sam
16c164510sam
17c164510samstruct eap_ikev2_data {
18c164510sam	struct ikev2_initiator_data ikev2;
19c164510sam	enum { MSG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
20c164510sam	struct wpabuf *in_buf;
21c164510sam	struct wpabuf *out_buf;
22c164510sam	size_t out_used;
23c164510sam	size_t fragment_size;
24c164510sam	int keys_ready;
25c164510sam	u8 keymat[EAP_MSK_LEN + EAP_EMSK_LEN];
26c164510sam	int keymat_ok;
27c164510sam};
28c164510sam
29c164510sam
30c164510samstatic const u8 * eap_ikev2_get_shared_secret(void *ctx, const u8 *IDr,
31c164510sam					      size_t IDr_len,
32c164510sam					      size_t *secret_len)
33c164510sam{
34c164510sam	struct eap_sm *sm = ctx;
35c164510sam
36c164510sam	if (IDr == NULL) {
37c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: No IDr received - default "
38c164510sam			   "to user identity from EAP-Identity");
39c164510sam		IDr = sm->identity;
40c164510sam		IDr_len = sm->identity_len;
41c164510sam	}
42c164510sam
43c164510sam	if (eap_user_get(sm, IDr, IDr_len, 0) < 0 || sm->user == NULL ||
44c164510sam	    sm->user->password == NULL) {
45c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: No user entry found");
46c164510sam		return NULL;
47c164510sam	}
48c164510sam
49c164510sam	*secret_len = sm->user->password_len;
50c164510sam	return sm->user->password;
51c164510sam}
52c164510sam
53c164510sam
54c164510samstatic const char * eap_ikev2_state_txt(int state)
55c164510sam{
56c164510sam	switch (state) {
57c164510sam	case MSG:
58c164510sam		return "MSG";
59c164510sam	case FRAG_ACK:
60c164510sam		return "FRAG_ACK";
61c164510sam	case WAIT_FRAG_ACK:
62c164510sam		return "WAIT_FRAG_ACK";
63c164510sam	case DONE:
64c164510sam		return "DONE";
65c164510sam	case FAIL:
66c164510sam		return "FAIL";
67c164510sam	default:
68c164510sam		return "?";
69c164510sam	}
70c164510sam}
71c164510sam
72c164510sam
73c164510samstatic void eap_ikev2_state(struct eap_ikev2_data *data, int state)
74c164510sam{
75c164510sam	wpa_printf(MSG_DEBUG, "EAP-IKEV2: %s -> %s",
76c164510sam		   eap_ikev2_state_txt(data->state),
77c164510sam		   eap_ikev2_state_txt(state));
78c164510sam	data->state = state;
79c164510sam}
80c164510sam
81c164510sam
82c164510samstatic void * eap_ikev2_init(struct eap_sm *sm)
83c164510sam{
84c164510sam	struct eap_ikev2_data *data;
85c164510sam
86c164510sam	data = os_zalloc(sizeof(*data));
87c164510sam	if (data == NULL)
88c164510sam		return NULL;
89c164510sam	data->state = MSG;
905e9e13erpaulo	data->fragment_size = sm->fragment_size > 0 ? sm->fragment_size :
915e9e13erpaulo		IKEV2_FRAGMENT_SIZE;
92c164510sam	data->ikev2.state = SA_INIT;
93c164510sam	data->ikev2.peer_auth = PEER_AUTH_SECRET;
94c164510sam	data->ikev2.key_pad = (u8 *) os_strdup("Key Pad for EAP-IKEv2");
95c164510sam	if (data->ikev2.key_pad == NULL)
96c164510sam		goto failed;
97c164510sam	data->ikev2.key_pad_len = 21;
98c164510sam
99c164510sam	/* TODO: make proposals configurable */
100c164510sam	data->ikev2.proposal.proposal_num = 1;
101c164510sam	data->ikev2.proposal.integ = AUTH_HMAC_SHA1_96;
102c164510sam	data->ikev2.proposal.prf = PRF_HMAC_SHA1;
103c164510sam	data->ikev2.proposal.encr = ENCR_AES_CBC;
104c164510sam	data->ikev2.proposal.dh = DH_GROUP2_1024BIT_MODP;
105c164510sam
1068d61b8dcy	data->ikev2.IDi = os_memdup(sm->server_id, sm->server_id_len);
10730dc5aerpaulo	if (data->ikev2.IDi == NULL)
10830dc5aerpaulo		goto failed;
10930dc5aerpaulo	data->ikev2.IDi_len = sm->server_id_len;
110c164510sam
111c164510sam	data->ikev2.get_shared_secret = eap_ikev2_get_shared_secret;
112c164510sam	data->ikev2.cb_ctx = sm;
113c164510sam
114c164510sam	return data;
115c164510sam
116c164510samfailed:
117c164510sam	ikev2_initiator_deinit(&data->ikev2);
118c164510sam	os_free(data);
119c164510sam	return NULL;
120c164510sam}
121c164510sam
122c164510sam
123c164510samstatic void eap_ikev2_reset(struct eap_sm *sm, void *priv)
124c164510sam{
125c164510sam	struct eap_ikev2_data *data = priv;
126c164510sam	wpabuf_free(data->in_buf);
127c164510sam	wpabuf_free(data->out_buf);
128c164510sam	ikev2_initiator_deinit(&data->ikev2);
12930dc5aerpaulo	bin_clear_free(data, sizeof(*data));
130c164510sam}
131c164510sam
132c164510sam
133c164510samstatic struct wpabuf * eap_ikev2_build_msg(struct eap_ikev2_data *data, u8 id)
134c164510sam{
135c164510sam	struct wpabuf *req;
136c164510sam	u8 flags;
137c164510sam	size_t send_len, plen, icv_len = 0;
138c164510sam
139c164510sam	wpa_printf(MSG_DEBUG, "EAP-IKEV2: Generating Request");
140c164510sam
141c164510sam	flags = 0;
142c164510sam	send_len = wpabuf_len(data->out_buf) - data->out_used;
143c164510sam	if (1 + send_len > data->fragment_size) {
144c164510sam		send_len = data->fragment_size - 1;
145c164510sam		flags |= IKEV2_FLAGS_MORE_FRAGMENTS;
146c164510sam		if (data->out_used == 0) {
147c164510sam			flags |= IKEV2_FLAGS_LENGTH_INCLUDED;
148c164510sam			send_len -= 4;
149c164510sam		}
150c164510sam	}
151c164510sam
152c164510sam	plen = 1 + send_len;
153c164510sam	if (flags & IKEV2_FLAGS_LENGTH_INCLUDED)
154c164510sam		plen += 4;
155c164510sam	if (data->keys_ready) {
156c164510sam		const struct ikev2_integ_alg *integ;
157c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: Add Integrity Checksum "
158c164510sam			   "Data");
159c164510sam		flags |= IKEV2_FLAGS_ICV_INCLUDED;
160c164510sam		integ = ikev2_get_integ(data->ikev2.proposal.integ);
161c164510sam		if (integ == NULL) {
162c164510sam			wpa_printf(MSG_DEBUG, "EAP-IKEV2: Unknown INTEG "
163c164510sam				   "transform / cannot generate ICV");
164c164510sam			return NULL;
165c164510sam		}
166c164510sam		icv_len = integ->hash_len;
167c164510sam
168c164510sam		plen += icv_len;
169c164510sam	}
170c164510sam	req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IKEV2, plen,
171c164510sam			    EAP_CODE_REQUEST, id);
172c164510sam	if (req == NULL)
173c164510sam		return NULL;
174c164510sam
175c164510sam	wpabuf_put_u8(req, flags); /* Flags */
176c164510sam	if (flags & IKEV2_FLAGS_LENGTH_INCLUDED)
177c164510sam		wpabuf_put_be32(req, wpabuf_len(data->out_buf));
178c164510sam
179c164510sam	wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
180c164510sam			send_len);
181c164510sam	data->out_used += send_len;
182c164510sam
183c164510sam	if (flags & IKEV2_FLAGS_ICV_INCLUDED) {
184c164510sam		const u8 *msg = wpabuf_head(req);
185c164510sam		size_t len = wpabuf_len(req);
186c164510sam		ikev2_integ_hash(data->ikev2.proposal.integ,
187c164510sam				 data->ikev2.keys.SK_ai,
188c164510sam				 data->ikev2.keys.SK_integ_len,
189c164510sam				 msg, len, wpabuf_put(req, icv_len));
190c164510sam	}
191c164510sam
192c164510sam	if (data->out_used == wpabuf_len(data->out_buf)) {
193c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: Sending out %lu bytes "
194c164510sam			   "(message sent completely)",
195c164510sam			   (unsigned long) send_len);
196c164510sam		wpabuf_free(data->out_buf);
197c164510sam		data->out_buf = NULL;
198c164510sam		data->out_used = 0;
199c164510sam	} else {
200c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: Sending out %lu bytes "
201c164510sam			   "(%lu more to send)", (unsigned long) send_len,
202c164510sam			   (unsigned long) wpabuf_len(data->out_buf) -
203c164510sam			   data->out_used);
204c164510sam		eap_ikev2_state(data, WAIT_FRAG_ACK);
205c164510sam	}
206c164510sam
207c164510sam	return req;
208c164510sam}
209c164510sam
210c164510sam
211c164510samstatic struct wpabuf * eap_ikev2_buildReq(struct eap_sm *sm, void *priv, u8 id)
212c164510sam{
213c164510sam	struct eap_ikev2_data *data = priv;
214c164510sam
215c164510sam	switch (data->state) {
216c164510sam	case MSG:
217c164510sam		if (data->out_buf == NULL) {
218c164510sam			data->out_buf = ikev2_initiator_build(&data->ikev2);
219c164510sam			if (data->out_buf == NULL) {
220c164510sam				wpa_printf(MSG_DEBUG, "EAP-IKEV2: Failed to "
221c164510sam					   "generate IKEv2 message");
222c164510sam				return NULL;
223c164510sam			}
224c164510sam			data->out_used = 0;
225c164510sam		}
2268d61b8dcy		/* fall through */
227c164510sam	case WAIT_FRAG_ACK:
228c164510sam		return eap_ikev2_build_msg(data, id);
229c164510sam	case FRAG_ACK:
230c164510sam		return eap_ikev2_build_frag_ack(id, EAP_CODE_REQUEST);
231c164510sam	default:
232c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: Unexpected state %d in "
233c164510sam			   "buildReq", data->state);
234c164510sam		return NULL;
235c164510sam	}
236c164510sam}
237c164510sam
238c164510sam
239c164510samstatic Boolean eap_ikev2_check(struct eap_sm *sm, void *priv,
240c164510sam			       struct wpabuf *respData)
241c164510sam{
242c164510sam	const u8 *pos;
243c164510sam	size_t len;
244c164510sam
245c164510sam	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IKEV2, respData,
246c164510sam			       &len);
247c164510sam	if (pos == NULL) {
248c164510sam		wpa_printf(MSG_INFO, "EAP-IKEV2: Invalid frame");
249c164510sam		return TRUE;
250c164510sam	}
251c164510sam
252c164510sam	return FALSE;
253c164510sam}
254c164510sam
255c164510sam
256c164510samstatic int eap_ikev2_process_icv(struct eap_ikev2_data *data,
257c164510sam				 const struct wpabuf *respData,
25830dc5aerpaulo				 u8 flags, const u8 *pos, const u8 **end,
25930dc5aerpaulo				 int frag_ack)
260c164510sam{
261c164510sam	if (flags & IKEV2_FLAGS_ICV_INCLUDED) {
262c164510sam		int icv_len = eap_ikev2_validate_icv(
263c164510sam			data->ikev2.proposal.integ, &data->ikev2.keys, 0,
264c164510sam			respData, pos, *end);
265c164510sam		if (icv_len < 0)
266c164510sam			return -1;
267c164510sam		/* Hide Integrity Checksum Data from further processing */
268c164510sam		*end -= icv_len;
26930dc5aerpaulo	} else if (data->keys_ready && !frag_ack) {
270c164510sam		wpa_printf(MSG_INFO, "EAP-IKEV2: The message should have "
271c164510sam			   "included integrity checksum");
272c164510sam		return -1;
273c164510sam	}
274c164510sam
275c164510sam	return 0;
276c164510sam}
277c164510sam
278c164510sam
279c164510samstatic int eap_ikev2_process_cont(struct eap_ikev2_data *data,
280c164510sam				  const u8 *buf, size_t len)
281c164510sam{
282c164510sam	/* Process continuation of a pending message */
283c164510sam	if (len > wpabuf_tailroom(data->in_buf)) {
284c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: Fragment overflow");
285c164510sam		eap_ikev2_state(data, FAIL);
286c164510sam		return -1;
287c164510sam	}
288c164510sam
289c164510sam	wpabuf_put_data(data->in_buf, buf, len);
290c164510sam	wpa_printf(MSG_DEBUG, "EAP-IKEV2: Received %lu bytes, waiting for %lu "
291c164510sam		   "bytes more", (unsigned long) len,
292c164510sam		   (unsigned long) wpabuf_tailroom(data->in_buf));
293c164510sam
294c164510sam	return 0;
295c164510sam}
296c164510sam
297c164510sam
298c164510samstatic int eap_ikev2_process_fragment(struct eap_ikev2_data *data,
299c164510sam				      u8 flags, u32 message_length,
300c164510sam				      const u8 *buf, size_t len)
301c164510sam{
302c164510sam	/* Process a fragment that is not the last one of the message */
303c164510sam	if (data->in_buf == NULL && !(flags & IKEV2_FLAGS_LENGTH_INCLUDED)) {
304c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: No Message Length field in "
305c164510sam			   "a fragmented packet");
306c164510sam		return -1;
307c164510sam	}
308c164510sam
309c164510sam	if (data->in_buf == NULL) {
310c164510sam		/* First fragment of the message */
31130dc5aerpaulo		if (message_length > 50000) {
31230dc5aerpaulo			/* Limit maximum memory allocation */
31330dc5aerpaulo			wpa_printf(MSG_DEBUG,
31430dc5aerpaulo				   "EAP-IKEV2: Ignore too long message");
31530dc5aerpaulo			return -1;
31630dc5aerpaulo		}
317c164510sam		data->in_buf = wpabuf_alloc(message_length);
318c164510sam		if (data->in_buf == NULL) {
319c164510sam			wpa_printf(MSG_DEBUG, "EAP-IKEV2: No memory for "
320c164510sam				   "message");
321c164510sam			return -1;
322c164510sam		}
323c164510sam		wpabuf_put_data(data->in_buf, buf, len);
324c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: Received %lu bytes in first "
325c164510sam			   "fragment, waiting for %lu bytes more",
326c164510sam			   (unsigned long) len,
327c164510sam			   (unsigned long) wpabuf_tailroom(data->in_buf));
328c164510sam	}
329c164510sam
330c164510sam	return 0;
331c164510sam}
332c164510sam
333c164510sam
334c164510samstatic int eap_ikev2_server_keymat(struct eap_ikev2_data *data)
335c164510sam{
336c164510sam	if (eap_ikev2_derive_keymat(
337c164510sam		    data->ikev2.proposal.prf, &data->ikev2.keys,
338c164510sam		    data->ikev2.i_nonce, data->ikev2.i_nonce_len,
339c164510sam		    data->ikev2.r_nonce, data->ikev2.r_nonce_len,
340c164510sam		    data->keymat) < 0) {
341c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: Failed to derive "
342c164510sam			   "key material");
343c164510sam		return -1;
344c164510sam	}
345c164510sam	data->keymat_ok = 1;
346c164510sam	return 0;
347c164510sam}
348c164510sam
349c164510sam
350c164510samstatic void eap_ikev2_process(struct eap_sm *sm, void *priv,
351c164510sam			      struct wpabuf *respData)
352c164510sam{
353c164510sam	struct eap_ikev2_data *data = priv;
354c164510sam	const u8 *start, *pos, *end;
355c164510sam	size_t len;
356c164510sam	u8 flags;
357c164510sam	u32 message_length = 0;
358c164510sam	struct wpabuf tmpbuf;
359c164510sam
360c164510sam	pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_IKEV2, respData,
361c164510sam			       &len);
362c164510sam	if (pos == NULL)
363c164510sam		return; /* Should not happen; message already verified */
364c164510sam
365c164510sam	start = pos;
366c164510sam	end = start + len;
367c164510sam
368c164510sam	if (len == 0) {
369c164510sam		/* fragment ack */
370c164510sam		flags = 0;
371c164510sam	} else
372c164510sam		flags = *pos++;
373c164510sam
37430dc5aerpaulo	if (eap_ikev2_process_icv(data, respData, flags, pos, &end,
37530dc5aerpaulo				  data->state == WAIT_FRAG_ACK && len == 0) < 0)
37630dc5aerpaulo	{
377c164510sam		eap_ikev2_state(data, FAIL);
378c164510sam		return;
379c164510sam	}
380c164510sam
381c164510sam	if (flags & IKEV2_FLAGS_LENGTH_INCLUDED) {
382c164510sam		if (end - pos < 4) {
383c164510sam			wpa_printf(MSG_DEBUG, "EAP-IKEV2: Message underflow");
384c164510sam			eap_ikev2_state(data, FAIL);
385c164510sam			return;
386c164510sam		}
387c164510sam		message_length = WPA_GET_BE32(pos);
388c164510sam		pos += 4;
389c164510sam
390c164510sam		if (message_length < (u32) (end - pos)) {
391c164510sam			wpa_printf(MSG_DEBUG, "EAP-IKEV2: Invalid Message "
392c164510sam				   "Length (%d; %ld remaining in this msg)",
393c164510sam				   message_length, (long) (end - pos));
394c164510sam			eap_ikev2_state(data, FAIL);
395c164510sam			return;
396c164510sam		}
397c164510sam	}
398c164510sam	wpa_printf(MSG_DEBUG, "EAP-IKEV2: Received packet: Flags 0x%x "
399c164510sam		   "Message Length %u", flags, message_length);
400c164510sam
401c164510sam	if (data->state == WAIT_FRAG_ACK) {
402c164510sam		if (len != 0) {
403c164510sam			wpa_printf(MSG_DEBUG, "EAP-IKEV2: Unexpected payload "
404c164510sam				   "in WAIT_FRAG_ACK state");
405c164510sam			eap_ikev2_state(data, FAIL);
406c164510sam			return;
407c164510sam		}
408c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: Fragment acknowledged");
409c164510sam		eap_ikev2_state(data, MSG);
410c164510sam		return;
411c164510sam	}
412c164510sam
413c164510sam	if (data->in_buf && eap_ikev2_process_cont(data, pos, end - pos) < 0) {
414c164510sam		eap_ikev2_state(data, FAIL);
415c164510sam		return;
416c164510sam	}
417c164510sam
418c164510sam	if (flags & IKEV2_FLAGS_MORE_FRAGMENTS) {
419c164510sam		if (eap_ikev2_process_fragment(data, flags, message_length,
420c164510sam					       pos, end - pos) < 0)
421c164510sam			eap_ikev2_state(data, FAIL);
422c164510sam		else
423c164510sam			eap_ikev2_state(data, FRAG_ACK);
424c164510sam		return;
425c164510sam	} else if (data->state == FRAG_ACK) {
426c164510sam		wpa_printf(MSG_DEBUG, "EAP-TNC: All fragments received");
427c164510sam		data->state = MSG;
428c164510sam	}
429c164510sam
430c164510sam	if (data->in_buf == NULL) {
431c164510sam		/* Wrap unfragmented messages as wpabuf without extra copy */
432c164510sam		wpabuf_set(&tmpbuf, pos, end - pos);
433c164510sam		data->in_buf = &tmpbuf;
434c164510sam	}
435c164510sam
436c164510sam	if (ikev2_initiator_process(&data->ikev2, data->in_buf) < 0) {
437c164510sam		if (data->in_buf == &tmpbuf)
438c164510sam			data->in_buf = NULL;
439c164510sam		eap_ikev2_state(data, FAIL);
440c164510sam		return;
441c164510sam	}
442c164510sam
443c164510sam	switch (data->ikev2.state) {
444c164510sam	case SA_AUTH:
445c164510sam		/* SA_INIT was sent out, so message have to be
446c164510sam		 * integrity protected from now on. */
447c164510sam		data->keys_ready = 1;
448c164510sam		break;
449c164510sam	case IKEV2_DONE:
450c164510sam		if (data->state == FAIL)
451c164510sam			break;
452c164510sam		wpa_printf(MSG_DEBUG, "EAP-IKEV2: Authentication completed "
453c164510sam			   "successfully");
454c164510sam		if (eap_ikev2_server_keymat(data))
455c164510sam			break;
456c164510sam		eap_ikev2_state(data, DONE);
457c164510sam		break;
458c164510sam	default:
459c164510sam		break;
460c164510sam	}
461c164510sam
462c164510sam	if (data->in_buf != &tmpbuf)
463c164510sam		wpabuf_free(data->in_buf);
464c164510sam	data->in_buf = NULL;
465c164510sam}
466c164510sam
467c164510sam
468c164510samstatic Boolean eap_ikev2_isDone(struct eap_sm *sm, void *priv)
469c164510sam{
470c164510sam	struct eap_ikev2_data *data = priv;
471c164510sam	return data->state == DONE || data->state == FAIL;
472c164510sam}
473c164510sam
474c164510sam
475c164510samstatic Boolean eap_ikev2_isSuccess(struct eap_sm *sm, void *priv)
476c164510sam{
477c164510sam	struct eap_ikev2_data *data = priv;
478c164510sam	return data->state == DONE && data->ikev2.state == IKEV2_DONE &&
479c164510sam		data->keymat_ok;
480c164510sam}
481c164510sam
482c164510sam
483c164510samstatic u8 * eap_ikev2_getKey(struct eap_sm *sm, void *priv, size_t *len)
484c164510sam{
485c164510sam	struct eap_ikev2_data *data = priv;
486c164510sam	u8 *key;
487c164510sam
488c164510sam	if (data->state != DONE || !data->keymat_ok)
489c164510sam		return NULL;
490c164510sam
491c164510sam	key = os_malloc(EAP_MSK_LEN);
492c164510sam	if (key) {
493c164510sam		os_memcpy(key, data->keymat, EAP_MSK_LEN);
494c164510sam		*len = EAP_MSK_LEN;
495c164510sam	}
496c164510sam
497c164510sam	return key;
498c164510sam}
499c164510sam
500c164510sam
501c164510samstatic u8 * eap_ikev2_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
502c164510sam{
503c164510sam	struct eap_ikev2_data *data = priv;
504c164510sam	u8 *key;
505c164510sam
506c164510sam	if (data->state != DONE || !data->keymat_ok)
507c164510sam		return NULL;
508c164510sam
509c164510sam	key = os_malloc(EAP_EMSK_LEN);
510c164510sam	if (key) {
511c164510sam		os_memcpy(key, data->keymat + EAP_MSK_LEN, EAP_EMSK_LEN);
512c164510sam		*len = EAP_EMSK_LEN;
513c164510sam	}
514c164510sam
515c164510sam	return key;
516c164510sam}
517c164510sam
518c164510sam
51930dc5aerpaulostatic u8 * eap_ikev2_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
52030dc5aerpaulo{
52130dc5aerpaulo	struct eap_ikev2_data *data = priv;
52230dc5aerpaulo	u8 *sid;
52330dc5aerpaulo	size_t sid_len;
52430dc5aerpaulo	size_t offset;
52530dc5aerpaulo
52630dc5aerpaulo	if (data->state != DONE || !data->keymat_ok)
52730dc5aerpaulo		return NULL;
52830dc5aerpaulo
52930dc5aerpaulo	sid_len = 1 + data->ikev2.i_nonce_len + data->ikev2.r_nonce_len;
53030dc5aerpaulo	sid = os_malloc(sid_len);
53130dc5aerpaulo	if (sid) {
53230dc5aerpaulo		offset = 0;
53330dc5aerpaulo		sid[offset] = EAP_TYPE_IKEV2;
53430dc5aerpaulo		offset++;
53530dc5aerpaulo		os_memcpy(sid + offset, data->ikev2.i_nonce,
53630dc5aerpaulo			  data->ikev2.i_nonce_len);
53730dc5aerpaulo		offset += data->ikev2.i_nonce_len;
53830dc5aerpaulo		os_memcpy(sid + offset, data->ikev2.r_nonce,
53930dc5aerpaulo			  data->ikev2.r_nonce_len);
54030dc5aerpaulo		*len = sid_len;
54130dc5aerpaulo		wpa_hexdump(MSG_DEBUG, "EAP-IKEV2: Derived Session-Id",
54230dc5aerpaulo			    sid, sid_len);
54330dc5aerpaulo	}
54430dc5aerpaulo
54530dc5aerpaulo	return sid;
54630dc5aerpaulo}
54730dc5aerpaulo
54830dc5aerpaulo
549c164510samint eap_server_ikev2_register(void)
550c164510sam{
551c164510sam	struct eap_method *eap;
552c164510sam
553c164510sam	eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
554c164510sam				      EAP_VENDOR_IETF, EAP_TYPE_IKEV2,
555c164510sam				      "IKEV2");
556c164510sam	if (eap == NULL)
557c164510sam		return -1;
558c164510sam
559c164510sam	eap->init = eap_ikev2_init;
560c164510sam	eap->reset = eap_ikev2_reset;
561c164510sam	eap->buildReq = eap_ikev2_buildReq;
562c164510sam	eap->check = eap_ikev2_check;
563c164510sam	eap->process = eap_ikev2_process;
564c164510sam	eap->isDone = eap_ikev2_isDone;
565c164510sam	eap->getKey = eap_ikev2_getKey;
566c164510sam	eap->isSuccess = eap_ikev2_isSuccess;
567c164510sam	eap->get_emsk = eap_ikev2_get_emsk;
56830dc5aerpaulo	eap->getSessionId = eap_ikev2_get_session_id;
569c164510sam
57022c9018cy	return eap_server_method_register(eap);
571c164510sam}
572