2 * EAP peer method: EAP-GPSK (RFC 5433)
3 * Copyright (c) 2006-2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/random.h"
13 #include "eap_peer/eap_i.h"
14 #include "eap_common/eap_gpsk_common.h"
16 struct eap_gpsk_data {
17 enum { GPSK_1, GPSK_3, SUCCESS, FAILURE } state;
18 u8 rand_server[EAP_GPSK_RAND_LEN];
19 u8 rand_peer[EAP_GPSK_RAND_LEN];
21 u8 emsk[EAP_EMSK_LEN];
22 u8 sk[EAP_GPSK_MAX_SK_LEN];
24 u8 pk[EAP_GPSK_MAX_PK_LEN];
32 int vendor; /* CSuite/Specifier */
33 int specifier; /* CSuite/Specifier */
39 static struct wpabuf * eap_gpsk_send_gpsk_2(struct eap_gpsk_data *data,
41 const u8 *csuite_list,
42 size_t csuite_list_len);
43 static struct wpabuf * eap_gpsk_send_gpsk_4(struct eap_gpsk_data *data,
47 #ifndef CONFIG_NO_STDOUT_DEBUG
48 static const char * eap_gpsk_state_txt(int state)
63 #endif /* CONFIG_NO_STDOUT_DEBUG */
66 static void eap_gpsk_state(struct eap_gpsk_data *data, int state)
68 wpa_printf(MSG_DEBUG, "EAP-GPSK: %s -> %s",
69 eap_gpsk_state_txt(data->state),
70 eap_gpsk_state_txt(state));
75 static void eap_gpsk_deinit(struct eap_sm *sm, void *priv);
78 static void * eap_gpsk_init(struct eap_sm *sm)
80 struct eap_gpsk_data *data;
81 const u8 *identity, *password;
82 size_t identity_len, password_len;
84 password = eap_get_config_password(sm, &password_len);
85 if (password == NULL) {
86 wpa_printf(MSG_INFO, "EAP-GPSK: No key (password) configured");
90 data = os_zalloc(sizeof(*data));
95 identity = eap_get_config_identity(sm, &identity_len);
97 data->id_peer = os_malloc(identity_len);
98 if (data->id_peer == NULL) {
99 eap_gpsk_deinit(sm, data);
102 os_memcpy(data->id_peer, identity, identity_len);
103 data->id_peer_len = identity_len;
106 data->psk = os_malloc(password_len);
107 if (data->psk == NULL) {
108 eap_gpsk_deinit(sm, data);
111 os_memcpy(data->psk, password, password_len);
112 data->psk_len = password_len;
118 static void eap_gpsk_deinit(struct eap_sm *sm, void *priv)
120 struct eap_gpsk_data *data = priv;
121 os_free(data->id_server);
122 os_free(data->id_peer);
128 static const u8 * eap_gpsk_process_id_server(struct eap_gpsk_data *data,
129 const u8 *pos, const u8 *end)
134 wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short GPSK-1 packet");
137 alen = WPA_GET_BE16(pos);
139 if (end - pos < alen) {
140 wpa_printf(MSG_DEBUG, "EAP-GPSK: ID_Server overflow");
143 os_free(data->id_server);
144 data->id_server = os_malloc(alen);
145 if (data->id_server == NULL) {
146 wpa_printf(MSG_DEBUG, "EAP-GPSK: No memory for ID_Server");
149 os_memcpy(data->id_server, pos, alen);
150 data->id_server_len = alen;
151 wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server",
152 data->id_server, data->id_server_len);
159 static const u8 * eap_gpsk_process_rand_server(struct eap_gpsk_data *data,
160 const u8 *pos, const u8 *end)
165 if (end - pos < EAP_GPSK_RAND_LEN) {
166 wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Server overflow");
169 os_memcpy(data->rand_server, pos, EAP_GPSK_RAND_LEN);
170 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server",
171 data->rand_server, EAP_GPSK_RAND_LEN);
172 pos += EAP_GPSK_RAND_LEN;
178 static int eap_gpsk_select_csuite(struct eap_sm *sm,
179 struct eap_gpsk_data *data,
180 const u8 *csuite_list,
181 size_t csuite_list_len)
183 struct eap_gpsk_csuite *csuite;
186 count = csuite_list_len / sizeof(struct eap_gpsk_csuite);
187 data->vendor = EAP_GPSK_VENDOR_IETF;
188 data->specifier = EAP_GPSK_CIPHER_RESERVED;
189 csuite = (struct eap_gpsk_csuite *) csuite_list;
190 for (i = 0; i < count; i++) {
191 int vendor, specifier;
192 vendor = WPA_GET_BE32(csuite->vendor);
193 specifier = WPA_GET_BE16(csuite->specifier);
194 wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite[%d]: %d:%d",
195 i, vendor, specifier);
196 if (data->vendor == EAP_GPSK_VENDOR_IETF &&
197 data->specifier == EAP_GPSK_CIPHER_RESERVED &&
198 eap_gpsk_supported_ciphersuite(vendor, specifier)) {
199 data->vendor = vendor;
200 data->specifier = specifier;
204 if (data->vendor == EAP_GPSK_VENDOR_IETF &&
205 data->specifier == EAP_GPSK_CIPHER_RESERVED) {
206 wpa_msg(sm->msg_ctx, MSG_INFO, "EAP-GPSK: No supported "
207 "ciphersuite found");
210 wpa_printf(MSG_DEBUG, "EAP-GPSK: Selected ciphersuite %d:%d",
211 data->vendor, data->specifier);
217 static const u8 * eap_gpsk_process_csuite_list(struct eap_sm *sm,
218 struct eap_gpsk_data *data,
221 const u8 *pos, const u8 *end)
227 wpa_printf(MSG_DEBUG, "EAP-GPSK: Too short GPSK-1 packet");
230 *list_len = WPA_GET_BE16(pos);
232 if (end - pos < (int) *list_len) {
233 wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_List overflow");
236 if (*list_len == 0 || (*list_len % sizeof(struct eap_gpsk_csuite))) {
237 wpa_printf(MSG_DEBUG, "EAP-GPSK: Invalid CSuite_List len %lu",
238 (unsigned long) *list_len);
244 if (eap_gpsk_select_csuite(sm, data, *list, *list_len) < 0)
251 static struct wpabuf * eap_gpsk_process_gpsk_1(struct eap_sm *sm,
252 struct eap_gpsk_data *data,
253 struct eap_method_ret *ret,
254 const struct wpabuf *reqData,
258 size_t csuite_list_len;
259 const u8 *csuite_list, *pos, *end;
262 if (data->state != GPSK_1) {
267 wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Request/GPSK-1");
269 end = payload + payload_len;
271 pos = eap_gpsk_process_id_server(data, payload, end);
272 pos = eap_gpsk_process_rand_server(data, pos, end);
273 pos = eap_gpsk_process_csuite_list(sm, data, &csuite_list,
274 &csuite_list_len, pos, end);
276 eap_gpsk_state(data, FAILURE);
280 resp = eap_gpsk_send_gpsk_2(data, eap_get_id(reqData),
281 csuite_list, csuite_list_len);
285 eap_gpsk_state(data, GPSK_3);
291 static struct wpabuf * eap_gpsk_send_gpsk_2(struct eap_gpsk_data *data,
293 const u8 *csuite_list,
294 size_t csuite_list_len)
299 struct eap_gpsk_csuite *csuite;
301 wpa_printf(MSG_DEBUG, "EAP-GPSK: Sending Response/GPSK-2");
303 miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
304 len = 1 + 2 + data->id_peer_len + 2 + data->id_server_len +
305 2 * EAP_GPSK_RAND_LEN + 2 + csuite_list_len +
306 sizeof(struct eap_gpsk_csuite) + 2 + miclen;
308 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, len,
309 EAP_CODE_RESPONSE, identifier);
313 wpabuf_put_u8(resp, EAP_GPSK_OPCODE_GPSK_2);
314 start = wpabuf_put(resp, 0);
316 wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Peer",
317 data->id_peer, data->id_peer_len);
318 wpabuf_put_be16(resp, data->id_peer_len);
319 wpabuf_put_data(resp, data->id_peer, data->id_peer_len);
321 wpabuf_put_be16(resp, data->id_server_len);
322 wpabuf_put_data(resp, data->id_server, data->id_server_len);
324 if (random_get_bytes(data->rand_peer, EAP_GPSK_RAND_LEN)) {
325 wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to get random data "
327 eap_gpsk_state(data, FAILURE);
331 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer",
332 data->rand_peer, EAP_GPSK_RAND_LEN);
333 wpabuf_put_data(resp, data->rand_peer, EAP_GPSK_RAND_LEN);
334 wpabuf_put_data(resp, data->rand_server, EAP_GPSK_RAND_LEN);
336 wpabuf_put_be16(resp, csuite_list_len);
337 wpabuf_put_data(resp, csuite_list, csuite_list_len);
339 csuite = wpabuf_put(resp, sizeof(*csuite));
340 WPA_PUT_BE32(csuite->vendor, data->vendor);
341 WPA_PUT_BE16(csuite->specifier, data->specifier);
343 if (eap_gpsk_derive_keys(data->psk, data->psk_len,
344 data->vendor, data->specifier,
345 data->rand_peer, data->rand_server,
346 data->id_peer, data->id_peer_len,
347 data->id_server, data->id_server_len,
348 data->msk, data->emsk,
349 data->sk, &data->sk_len,
350 data->pk, &data->pk_len) < 0) {
351 wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to derive keys");
352 eap_gpsk_state(data, FAILURE);
357 /* No PD_Payload_1 */
358 wpabuf_put_be16(resp, 0);
360 rpos = wpabuf_put(resp, miclen);
361 if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
362 data->specifier, start, rpos - start, rpos) <
364 eap_gpsk_state(data, FAILURE);
373 static const u8 * eap_gpsk_validate_rand(struct eap_gpsk_data *data,
374 const u8 *pos, const u8 *end)
376 if (end - pos < EAP_GPSK_RAND_LEN) {
377 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
381 if (os_memcmp(pos, data->rand_peer, EAP_GPSK_RAND_LEN) != 0) {
382 wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-2 and "
383 "GPSK-3 did not match");
384 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-2",
385 data->rand_peer, EAP_GPSK_RAND_LEN);
386 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Peer in GPSK-3",
387 pos, EAP_GPSK_RAND_LEN);
390 pos += EAP_GPSK_RAND_LEN;
392 if (end - pos < EAP_GPSK_RAND_LEN) {
393 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
397 if (os_memcmp(pos, data->rand_server, EAP_GPSK_RAND_LEN) != 0) {
398 wpa_printf(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1 and "
399 "GPSK-3 did not match");
400 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-1",
401 data->rand_server, EAP_GPSK_RAND_LEN);
402 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: RAND_Server in GPSK-3",
403 pos, EAP_GPSK_RAND_LEN);
406 pos += EAP_GPSK_RAND_LEN;
412 static const u8 * eap_gpsk_validate_id_server(struct eap_gpsk_data *data,
413 const u8 *pos, const u8 *end)
420 if (end - pos < (int) 2) {
421 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
422 "length(ID_Server)");
426 len = WPA_GET_BE16(pos);
429 if (end - pos < (int) len) {
430 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
435 if (len != data->id_server_len ||
436 os_memcmp(pos, data->id_server, len) != 0) {
437 wpa_printf(MSG_INFO, "EAP-GPSK: ID_Server did not match with "
438 "the one used in GPSK-1");
439 wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-1",
440 data->id_server, data->id_server_len);
441 wpa_hexdump_ascii(MSG_DEBUG, "EAP-GPSK: ID_Server in GPSK-3",
452 static const u8 * eap_gpsk_validate_csuite(struct eap_gpsk_data *data,
453 const u8 *pos, const u8 *end)
455 int vendor, specifier;
456 const struct eap_gpsk_csuite *csuite;
461 if (end - pos < (int) sizeof(*csuite)) {
462 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
466 csuite = (const struct eap_gpsk_csuite *) pos;
467 vendor = WPA_GET_BE32(csuite->vendor);
468 specifier = WPA_GET_BE16(csuite->specifier);
469 pos += sizeof(*csuite);
470 if (vendor != data->vendor || specifier != data->specifier) {
471 wpa_printf(MSG_DEBUG, "EAP-GPSK: CSuite_Sel (%d:%d) does not "
472 "match with the one sent in GPSK-2 (%d:%d)",
473 vendor, specifier, data->vendor, data->specifier);
481 static const u8 * eap_gpsk_validate_pd_payload_2(struct eap_gpsk_data *data,
482 const u8 *pos, const u8 *end)
490 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
491 "PD_Payload_2 length");
494 alen = WPA_GET_BE16(pos);
496 if (end - pos < alen) {
497 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for "
498 "%d-octet PD_Payload_2", alen);
501 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: PD_Payload_2", pos, alen);
508 static const u8 * eap_gpsk_validate_gpsk_3_mic(struct eap_gpsk_data *data,
510 const u8 *pos, const u8 *end)
513 u8 mic[EAP_GPSK_MAX_MIC_LEN];
518 miclen = eap_gpsk_mic_len(data->vendor, data->specifier);
519 if (end - pos < (int) miclen) {
520 wpa_printf(MSG_DEBUG, "EAP-GPSK: Message too short for MIC "
521 "(left=%lu miclen=%lu)",
522 (unsigned long) (end - pos),
523 (unsigned long) miclen);
526 if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
527 data->specifier, payload, pos - payload, mic)
529 wpa_printf(MSG_DEBUG, "EAP-GPSK: Failed to compute MIC");
532 if (os_memcmp(mic, pos, miclen) != 0) {
533 wpa_printf(MSG_INFO, "EAP-GPSK: Incorrect MIC in GPSK-3");
534 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Received MIC", pos, miclen);
535 wpa_hexdump(MSG_DEBUG, "EAP-GPSK: Computed MIC", mic, miclen);
544 static struct wpabuf * eap_gpsk_process_gpsk_3(struct eap_sm *sm,
545 struct eap_gpsk_data *data,
546 struct eap_method_ret *ret,
547 const struct wpabuf *reqData,
554 if (data->state != GPSK_3) {
559 wpa_printf(MSG_DEBUG, "EAP-GPSK: Received Request/GPSK-3");
561 end = payload + payload_len;
563 pos = eap_gpsk_validate_rand(data, payload, end);
564 pos = eap_gpsk_validate_id_server(data, pos, end);
565 pos = eap_gpsk_validate_csuite(data, pos, end);
566 pos = eap_gpsk_validate_pd_payload_2(data, pos, end);
567 pos = eap_gpsk_validate_gpsk_3_mic(data, payload, pos, end);
570 eap_gpsk_state(data, FAILURE);
574 wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignored %lu bytes of extra "
575 "data in the end of GPSK-2",
576 (unsigned long) (end - pos));
579 resp = eap_gpsk_send_gpsk_4(data, eap_get_id(reqData));
583 eap_gpsk_state(data, SUCCESS);
584 ret->methodState = METHOD_DONE;
585 ret->decision = DECISION_UNCOND_SUCC;
591 static struct wpabuf * eap_gpsk_send_gpsk_4(struct eap_gpsk_data *data,
598 wpa_printf(MSG_DEBUG, "EAP-GPSK: Sending Response/GPSK-4");
600 mlen = eap_gpsk_mic_len(data->vendor, data->specifier);
602 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_GPSK, 1 + 2 + mlen,
603 EAP_CODE_RESPONSE, identifier);
607 wpabuf_put_u8(resp, EAP_GPSK_OPCODE_GPSK_4);
608 start = wpabuf_put(resp, 0);
610 /* No PD_Payload_3 */
611 wpabuf_put_be16(resp, 0);
613 rpos = wpabuf_put(resp, mlen);
614 if (eap_gpsk_compute_mic(data->sk, data->sk_len, data->vendor,
615 data->specifier, start, rpos - start, rpos) <
617 eap_gpsk_state(data, FAILURE);
626 static struct wpabuf * eap_gpsk_process(struct eap_sm *sm, void *priv,
627 struct eap_method_ret *ret,
628 const struct wpabuf *reqData)
630 struct eap_gpsk_data *data = priv;
635 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_GPSK, reqData, &len);
636 if (pos == NULL || len < 1) {
641 wpa_printf(MSG_DEBUG, "EAP-GPSK: Received frame: opcode %d", *pos);
644 ret->methodState = METHOD_MAY_CONT;
645 ret->decision = DECISION_FAIL;
646 ret->allowNotifications = FALSE;
649 case EAP_GPSK_OPCODE_GPSK_1:
650 resp = eap_gpsk_process_gpsk_1(sm, data, ret, reqData,
653 case EAP_GPSK_OPCODE_GPSK_3:
654 resp = eap_gpsk_process_gpsk_3(sm, data, ret, reqData,
658 wpa_printf(MSG_DEBUG, "EAP-GPSK: Ignoring message with "
659 "unknown opcode %d", *pos);
668 static Boolean eap_gpsk_isKeyAvailable(struct eap_sm *sm, void *priv)
670 struct eap_gpsk_data *data = priv;
671 return data->state == SUCCESS;
675 static u8 * eap_gpsk_getKey(struct eap_sm *sm, void *priv, size_t *len)
677 struct eap_gpsk_data *data = priv;
680 if (data->state != SUCCESS)
683 key = os_malloc(EAP_MSK_LEN);
686 os_memcpy(key, data->msk, EAP_MSK_LEN);
693 static u8 * eap_gpsk_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
695 struct eap_gpsk_data *data = priv;
698 if (data->state != SUCCESS)
701 key = os_malloc(EAP_EMSK_LEN);
704 os_memcpy(key, data->emsk, EAP_EMSK_LEN);
711 int eap_peer_gpsk_register(void)
713 struct eap_method *eap;
716 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
717 EAP_VENDOR_IETF, EAP_TYPE_GPSK, "GPSK");
721 eap->init = eap_gpsk_init;
722 eap->deinit = eap_gpsk_deinit;
723 eap->process = eap_gpsk_process;
724 eap->isKeyAvailable = eap_gpsk_isKeyAvailable;
725 eap->getKey = eap_gpsk_getKey;
726 eap->get_emsk = eap_gpsk_get_emsk;
728 ret = eap_peer_method_register(eap);
730 eap_peer_method_free(eap);