Line data Source code
1 : /*
2 : * EAP peer method: EAP-SAKE (RFC 4763)
3 : * Copyright (c) 2006-2008, 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 "crypto/random.h"
13 : #include "eap_peer/eap_i.h"
14 : #include "eap_common/eap_sake_common.h"
15 :
16 : struct eap_sake_data {
17 : enum { IDENTITY, CHALLENGE, CONFIRM, SUCCESS, FAILURE } state;
18 : u8 root_secret_a[EAP_SAKE_ROOT_SECRET_LEN];
19 : u8 root_secret_b[EAP_SAKE_ROOT_SECRET_LEN];
20 : u8 rand_s[EAP_SAKE_RAND_LEN];
21 : u8 rand_p[EAP_SAKE_RAND_LEN];
22 : struct {
23 : u8 auth[EAP_SAKE_TEK_AUTH_LEN];
24 : u8 cipher[EAP_SAKE_TEK_CIPHER_LEN];
25 : } tek;
26 : u8 msk[EAP_MSK_LEN];
27 : u8 emsk[EAP_EMSK_LEN];
28 : u8 session_id;
29 : int session_id_set;
30 : u8 *peerid;
31 : size_t peerid_len;
32 : u8 *serverid;
33 : size_t serverid_len;
34 : };
35 :
36 :
37 92 : static const char * eap_sake_state_txt(int state)
38 : {
39 92 : switch (state) {
40 : case IDENTITY:
41 22 : return "IDENTITY";
42 : case CHALLENGE:
43 37 : return "CHALLENGE";
44 : case CONFIRM:
45 24 : return "CONFIRM";
46 : case SUCCESS:
47 6 : return "SUCCESS";
48 : case FAILURE:
49 3 : return "FAILURE";
50 : default:
51 0 : return "?";
52 : }
53 : }
54 :
55 :
56 46 : static void eap_sake_state(struct eap_sake_data *data, int state)
57 : {
58 92 : wpa_printf(MSG_DEBUG, "EAP-SAKE: %s -> %s",
59 46 : eap_sake_state_txt(data->state),
60 : eap_sake_state_txt(state));
61 46 : data->state = state;
62 46 : }
63 :
64 :
65 : static void eap_sake_deinit(struct eap_sm *sm, void *priv);
66 :
67 :
68 48 : static void * eap_sake_init(struct eap_sm *sm)
69 : {
70 : struct eap_sake_data *data;
71 : const u8 *identity, *password;
72 : size_t identity_len, password_len;
73 :
74 48 : password = eap_get_config_password(sm, &password_len);
75 48 : if (!password || password_len != 2 * EAP_SAKE_ROOT_SECRET_LEN) {
76 8 : wpa_printf(MSG_INFO, "EAP-SAKE: No key of correct length "
77 : "configured");
78 8 : return NULL;
79 : }
80 :
81 40 : data = os_zalloc(sizeof(*data));
82 40 : if (data == NULL)
83 1 : return NULL;
84 39 : data->state = IDENTITY;
85 :
86 39 : identity = eap_get_config_identity(sm, &identity_len);
87 39 : if (identity) {
88 39 : data->peerid = os_malloc(identity_len);
89 39 : if (data->peerid == NULL) {
90 1 : eap_sake_deinit(sm, data);
91 1 : return NULL;
92 : }
93 38 : os_memcpy(data->peerid, identity, identity_len);
94 38 : data->peerid_len = identity_len;
95 : }
96 :
97 38 : os_memcpy(data->root_secret_a, password, EAP_SAKE_ROOT_SECRET_LEN);
98 38 : os_memcpy(data->root_secret_b,
99 : password + EAP_SAKE_ROOT_SECRET_LEN,
100 : EAP_SAKE_ROOT_SECRET_LEN);
101 :
102 38 : return data;
103 : }
104 :
105 :
106 39 : static void eap_sake_deinit(struct eap_sm *sm, void *priv)
107 : {
108 39 : struct eap_sake_data *data = priv;
109 39 : os_free(data->serverid);
110 39 : os_free(data->peerid);
111 39 : bin_clear_free(data, sizeof(*data));
112 39 : }
113 :
114 :
115 29 : static struct wpabuf * eap_sake_build_msg(struct eap_sake_data *data,
116 : int id, size_t length, u8 subtype)
117 : {
118 : struct eap_sake_hdr *sake;
119 : struct wpabuf *msg;
120 : size_t plen;
121 :
122 29 : plen = length + sizeof(struct eap_sake_hdr);
123 :
124 29 : msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_SAKE, plen,
125 : EAP_CODE_RESPONSE, id);
126 29 : if (msg == NULL) {
127 3 : wpa_printf(MSG_ERROR, "EAP-SAKE: Failed to allocate memory "
128 : "request");
129 3 : return NULL;
130 : }
131 :
132 26 : sake = wpabuf_put(msg, sizeof(*sake));
133 26 : sake->version = EAP_SAKE_VERSION;
134 26 : sake->session_id = data->session_id;
135 26 : sake->subtype = subtype;
136 :
137 26 : return msg;
138 : }
139 :
140 :
141 15 : static struct wpabuf * eap_sake_process_identity(struct eap_sm *sm,
142 : struct eap_sake_data *data,
143 : struct eap_method_ret *ret,
144 : u8 id,
145 : const u8 *payload,
146 : size_t payload_len)
147 : {
148 : struct eap_sake_parse_attr attr;
149 : struct wpabuf *resp;
150 :
151 15 : if (data->state != IDENTITY) {
152 1 : ret->ignore = TRUE;
153 1 : return NULL;
154 : }
155 :
156 14 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Received Request/Identity");
157 :
158 14 : if (eap_sake_parse_attributes(payload, payload_len, &attr))
159 11 : return NULL;
160 :
161 3 : if (!attr.perm_id_req && !attr.any_id_req) {
162 1 : wpa_printf(MSG_INFO, "EAP-SAKE: No AT_PERM_ID_REQ or "
163 : "AT_ANY_ID_REQ in Request/Identity");
164 1 : return NULL;
165 : }
166 :
167 2 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Sending Response/Identity");
168 :
169 2 : resp = eap_sake_build_msg(data, id, 2 + data->peerid_len,
170 : EAP_SAKE_SUBTYPE_IDENTITY);
171 2 : if (resp == NULL)
172 1 : return NULL;
173 :
174 1 : wpa_printf(MSG_DEBUG, "EAP-SAKE: * AT_PEERID");
175 2 : eap_sake_add_attr(resp, EAP_SAKE_AT_PEERID,
176 1 : data->peerid, data->peerid_len);
177 :
178 1 : eap_sake_state(data, CHALLENGE);
179 :
180 1 : return resp;
181 : }
182 :
183 :
184 22 : static struct wpabuf * eap_sake_process_challenge(struct eap_sm *sm,
185 : struct eap_sake_data *data,
186 : struct eap_method_ret *ret,
187 : u8 id,
188 : const u8 *payload,
189 : size_t payload_len)
190 : {
191 : struct eap_sake_parse_attr attr;
192 : struct wpabuf *resp;
193 : u8 *rpos;
194 : size_t rlen;
195 :
196 22 : if (data->state != IDENTITY && data->state != CHALLENGE) {
197 1 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Request/Challenge received "
198 1 : "in unexpected state (%d)", data->state);
199 1 : ret->ignore = TRUE;
200 1 : return NULL;
201 : }
202 21 : if (data->state == IDENTITY)
203 21 : eap_sake_state(data, CHALLENGE);
204 :
205 21 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Received Request/Challenge");
206 :
207 21 : if (eap_sake_parse_attributes(payload, payload_len, &attr))
208 1 : return NULL;
209 :
210 20 : if (!attr.rand_s) {
211 1 : wpa_printf(MSG_INFO, "EAP-SAKE: Request/Challenge did not "
212 : "include AT_RAND_S");
213 1 : return NULL;
214 : }
215 :
216 19 : os_memcpy(data->rand_s, attr.rand_s, EAP_SAKE_RAND_LEN);
217 19 : wpa_hexdump(MSG_MSGDUMP, "EAP-SAKE: RAND_S (server rand)",
218 19 : data->rand_s, EAP_SAKE_RAND_LEN);
219 :
220 19 : if (random_get_bytes(data->rand_p, EAP_SAKE_RAND_LEN)) {
221 1 : wpa_printf(MSG_ERROR, "EAP-SAKE: Failed to get random data");
222 1 : return NULL;
223 : }
224 18 : wpa_hexdump(MSG_MSGDUMP, "EAP-SAKE: RAND_P (peer rand)",
225 18 : data->rand_p, EAP_SAKE_RAND_LEN);
226 :
227 18 : os_free(data->serverid);
228 18 : data->serverid = NULL;
229 18 : data->serverid_len = 0;
230 18 : if (attr.serverid) {
231 26 : wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-SAKE: SERVERID",
232 13 : attr.serverid, attr.serverid_len);
233 13 : data->serverid = os_malloc(attr.serverid_len);
234 13 : if (data->serverid == NULL)
235 1 : return NULL;
236 12 : os_memcpy(data->serverid, attr.serverid, attr.serverid_len);
237 12 : data->serverid_len = attr.serverid_len;
238 : }
239 :
240 17 : eap_sake_derive_keys(data->root_secret_a, data->root_secret_b,
241 17 : data->rand_s, data->rand_p,
242 17 : (u8 *) &data->tek, data->msk, data->emsk);
243 :
244 17 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Sending Response/Challenge");
245 :
246 17 : rlen = 2 + EAP_SAKE_RAND_LEN + 2 + EAP_SAKE_MIC_LEN;
247 17 : if (data->peerid)
248 17 : rlen += 2 + data->peerid_len;
249 17 : resp = eap_sake_build_msg(data, id, rlen, EAP_SAKE_SUBTYPE_CHALLENGE);
250 17 : if (resp == NULL)
251 1 : return NULL;
252 :
253 16 : wpa_printf(MSG_DEBUG, "EAP-SAKE: * AT_RAND_P");
254 16 : eap_sake_add_attr(resp, EAP_SAKE_AT_RAND_P,
255 16 : data->rand_p, EAP_SAKE_RAND_LEN);
256 :
257 16 : if (data->peerid) {
258 16 : wpa_printf(MSG_DEBUG, "EAP-SAKE: * AT_PEERID");
259 32 : eap_sake_add_attr(resp, EAP_SAKE_AT_PEERID,
260 16 : data->peerid, data->peerid_len);
261 : }
262 :
263 16 : wpa_printf(MSG_DEBUG, "EAP-SAKE: * AT_MIC_P");
264 16 : wpabuf_put_u8(resp, EAP_SAKE_AT_MIC_P);
265 16 : wpabuf_put_u8(resp, 2 + EAP_SAKE_MIC_LEN);
266 16 : rpos = wpabuf_put(resp, EAP_SAKE_MIC_LEN);
267 64 : if (eap_sake_compute_mic(data->tek.auth, data->rand_s, data->rand_p,
268 16 : data->serverid, data->serverid_len,
269 16 : data->peerid, data->peerid_len, 1,
270 16 : wpabuf_head(resp), wpabuf_len(resp), rpos,
271 : rpos)) {
272 1 : wpa_printf(MSG_INFO, "EAP-SAKE: Failed to compute MIC");
273 1 : wpabuf_free(resp);
274 1 : return NULL;
275 : }
276 :
277 15 : eap_sake_state(data, CONFIRM);
278 :
279 15 : return resp;
280 : }
281 :
282 :
283 13 : static struct wpabuf * eap_sake_process_confirm(struct eap_sm *sm,
284 : struct eap_sake_data *data,
285 : struct eap_method_ret *ret,
286 : u8 id,
287 : const struct wpabuf *reqData,
288 : const u8 *payload,
289 : size_t payload_len)
290 : {
291 : struct eap_sake_parse_attr attr;
292 : u8 mic_s[EAP_SAKE_MIC_LEN];
293 : struct wpabuf *resp;
294 : u8 *rpos;
295 :
296 13 : if (data->state != CONFIRM) {
297 1 : ret->ignore = TRUE;
298 1 : return NULL;
299 : }
300 :
301 12 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Received Request/Confirm");
302 :
303 12 : if (eap_sake_parse_attributes(payload, payload_len, &attr))
304 1 : return NULL;
305 :
306 11 : if (!attr.mic_s) {
307 1 : wpa_printf(MSG_INFO, "EAP-SAKE: Request/Confirm did not "
308 : "include AT_MIC_S");
309 1 : return NULL;
310 : }
311 :
312 40 : if (eap_sake_compute_mic(data->tek.auth, data->rand_s, data->rand_p,
313 10 : data->serverid, data->serverid_len,
314 10 : data->peerid, data->peerid_len, 0,
315 10 : wpabuf_head(reqData), wpabuf_len(reqData),
316 : attr.mic_s, mic_s)) {
317 1 : wpa_printf(MSG_INFO, "EAP-SAKE: Failed to compute MIC");
318 1 : eap_sake_state(data, FAILURE);
319 1 : ret->methodState = METHOD_DONE;
320 1 : ret->decision = DECISION_FAIL;
321 1 : ret->allowNotifications = FALSE;
322 1 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Sending Response/Auth-Reject");
323 1 : return eap_sake_build_msg(data, id, 0,
324 : EAP_SAKE_SUBTYPE_AUTH_REJECT);
325 : }
326 9 : if (os_memcmp_const(attr.mic_s, mic_s, EAP_SAKE_MIC_LEN) != 0) {
327 2 : wpa_printf(MSG_INFO, "EAP-SAKE: Incorrect AT_MIC_S");
328 2 : eap_sake_state(data, FAILURE);
329 2 : ret->methodState = METHOD_DONE;
330 2 : ret->decision = DECISION_FAIL;
331 2 : ret->allowNotifications = FALSE;
332 2 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Sending "
333 : "Response/Auth-Reject");
334 2 : return eap_sake_build_msg(data, id, 0,
335 : EAP_SAKE_SUBTYPE_AUTH_REJECT);
336 : }
337 :
338 7 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Sending Response/Confirm");
339 :
340 7 : resp = eap_sake_build_msg(data, id, 2 + EAP_SAKE_MIC_LEN,
341 : EAP_SAKE_SUBTYPE_CONFIRM);
342 7 : if (resp == NULL)
343 1 : return NULL;
344 :
345 6 : wpa_printf(MSG_DEBUG, "EAP-SAKE: * AT_MIC_P");
346 6 : wpabuf_put_u8(resp, EAP_SAKE_AT_MIC_P);
347 6 : wpabuf_put_u8(resp, 2 + EAP_SAKE_MIC_LEN);
348 6 : rpos = wpabuf_put(resp, EAP_SAKE_MIC_LEN);
349 24 : if (eap_sake_compute_mic(data->tek.auth, data->rand_s, data->rand_p,
350 6 : data->serverid, data->serverid_len,
351 6 : data->peerid, data->peerid_len, 1,
352 6 : wpabuf_head(resp), wpabuf_len(resp), rpos,
353 : rpos)) {
354 0 : wpa_printf(MSG_INFO, "EAP-SAKE: Failed to compute MIC");
355 0 : wpabuf_free(resp);
356 0 : return NULL;
357 : }
358 :
359 6 : eap_sake_state(data, SUCCESS);
360 6 : ret->methodState = METHOD_DONE;
361 6 : ret->decision = DECISION_UNCOND_SUCC;
362 6 : ret->allowNotifications = FALSE;
363 :
364 6 : return resp;
365 : }
366 :
367 :
368 53 : static struct wpabuf * eap_sake_process(struct eap_sm *sm, void *priv,
369 : struct eap_method_ret *ret,
370 : const struct wpabuf *reqData)
371 : {
372 53 : struct eap_sake_data *data = priv;
373 : const struct eap_sake_hdr *req;
374 : struct wpabuf *resp;
375 : const u8 *pos, *end;
376 : size_t len;
377 : u8 subtype, session_id, id;
378 :
379 53 : pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_SAKE, reqData, &len);
380 53 : if (pos == NULL || len < sizeof(struct eap_sake_hdr)) {
381 1 : ret->ignore = TRUE;
382 1 : return NULL;
383 : }
384 :
385 52 : req = (const struct eap_sake_hdr *) pos;
386 52 : end = pos + len;
387 52 : id = eap_get_id(reqData);
388 52 : subtype = req->subtype;
389 52 : session_id = req->session_id;
390 52 : pos = (const u8 *) (req + 1);
391 :
392 52 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Received frame: subtype %d "
393 : "session_id %d", subtype, session_id);
394 52 : wpa_hexdump(MSG_DEBUG, "EAP-SAKE: Received attributes",
395 52 : pos, end - pos);
396 :
397 52 : if (data->session_id_set && data->session_id != session_id) {
398 1 : wpa_printf(MSG_INFO, "EAP-SAKE: Session ID mismatch (%d,%d)",
399 1 : session_id, data->session_id);
400 1 : ret->ignore = TRUE;
401 1 : return NULL;
402 : }
403 51 : data->session_id = session_id;
404 51 : data->session_id_set = 1;
405 :
406 51 : ret->ignore = FALSE;
407 51 : ret->methodState = METHOD_MAY_CONT;
408 51 : ret->decision = DECISION_FAIL;
409 51 : ret->allowNotifications = TRUE;
410 :
411 51 : switch (subtype) {
412 : case EAP_SAKE_SUBTYPE_IDENTITY:
413 15 : resp = eap_sake_process_identity(sm, data, ret, id,
414 15 : pos, end - pos);
415 15 : break;
416 : case EAP_SAKE_SUBTYPE_CHALLENGE:
417 22 : resp = eap_sake_process_challenge(sm, data, ret, id,
418 22 : pos, end - pos);
419 22 : break;
420 : case EAP_SAKE_SUBTYPE_CONFIRM:
421 13 : resp = eap_sake_process_confirm(sm, data, ret, id, reqData,
422 13 : pos, end - pos);
423 13 : break;
424 : default:
425 1 : wpa_printf(MSG_DEBUG, "EAP-SAKE: Ignoring message with "
426 : "unknown subtype %d", subtype);
427 1 : ret->ignore = TRUE;
428 1 : return NULL;
429 : }
430 :
431 50 : if (ret->methodState == METHOD_DONE)
432 9 : ret->allowNotifications = FALSE;
433 :
434 50 : return resp;
435 : }
436 :
437 :
438 47 : static Boolean eap_sake_isKeyAvailable(struct eap_sm *sm, void *priv)
439 : {
440 47 : struct eap_sake_data *data = priv;
441 47 : return data->state == SUCCESS;
442 : }
443 :
444 :
445 6 : static u8 * eap_sake_getKey(struct eap_sm *sm, void *priv, size_t *len)
446 : {
447 6 : struct eap_sake_data *data = priv;
448 : u8 *key;
449 :
450 6 : if (data->state != SUCCESS)
451 0 : return NULL;
452 :
453 6 : key = os_malloc(EAP_MSK_LEN);
454 6 : if (key == NULL)
455 1 : return NULL;
456 5 : os_memcpy(key, data->msk, EAP_MSK_LEN);
457 5 : *len = EAP_MSK_LEN;
458 :
459 5 : return key;
460 : }
461 :
462 :
463 6 : static u8 * eap_sake_get_session_id(struct eap_sm *sm, void *priv, size_t *len)
464 : {
465 6 : struct eap_sake_data *data = priv;
466 : u8 *id;
467 :
468 6 : if (data->state != SUCCESS)
469 0 : return NULL;
470 :
471 6 : *len = 1 + 2 * EAP_SAKE_RAND_LEN;
472 6 : id = os_malloc(*len);
473 6 : if (id == NULL)
474 1 : return NULL;
475 :
476 5 : id[0] = EAP_TYPE_SAKE;
477 5 : os_memcpy(id + 1, data->rand_s, EAP_SAKE_RAND_LEN);
478 5 : os_memcpy(id + 1 + EAP_SAKE_RAND_LEN, data->rand_s, EAP_SAKE_RAND_LEN);
479 5 : wpa_hexdump(MSG_DEBUG, "EAP-SAKE: Derived Session-Id", id, *len);
480 :
481 5 : return id;
482 : }
483 :
484 :
485 3 : static u8 * eap_sake_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
486 : {
487 3 : struct eap_sake_data *data = priv;
488 : u8 *key;
489 :
490 3 : if (data->state != SUCCESS)
491 0 : return NULL;
492 :
493 3 : key = os_malloc(EAP_EMSK_LEN);
494 3 : if (key == NULL)
495 1 : return NULL;
496 2 : os_memcpy(key, data->emsk, EAP_EMSK_LEN);
497 2 : *len = EAP_EMSK_LEN;
498 :
499 2 : return key;
500 : }
501 :
502 :
503 49 : int eap_peer_sake_register(void)
504 : {
505 : struct eap_method *eap;
506 :
507 49 : eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
508 : EAP_VENDOR_IETF, EAP_TYPE_SAKE, "SAKE");
509 49 : if (eap == NULL)
510 0 : return -1;
511 :
512 49 : eap->init = eap_sake_init;
513 49 : eap->deinit = eap_sake_deinit;
514 49 : eap->process = eap_sake_process;
515 49 : eap->isKeyAvailable = eap_sake_isKeyAvailable;
516 49 : eap->getKey = eap_sake_getKey;
517 49 : eap->getSessionId = eap_sake_get_session_id;
518 49 : eap->get_emsk = eap_sake_get_emsk;
519 :
520 49 : return eap_peer_method_register(eap);
521 : }
|