2 * EAP peer method: EAP-PAX (RFC 4746)
3 * Copyright (c) 2005-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "eap_common/eap_pax_common.h"
22 * Note: only PAX_STD subprotocol is currently supported
24 * TODO: Add support with PAX_SEC with the mandatory to implement ciphersuite
25 * (HMAC_SHA1_128, IANA DH Group 14 (2048 bits), RSA-PKCS1-V1_5) and
26 * recommended ciphersuite (HMAC_SHA256_128, IANA DH Group 15 (3072 bits),
31 enum { PAX_INIT, PAX_STD_2_SENT, PAX_DONE } state;
32 u8 mac_id, dh_group_id, public_key_id;
34 u8 e[2 * EAP_PAX_RAND_LEN];
36 u8 x[EAP_PAX_RAND_LEN]; /* server rand */
37 u8 y[EAP_PAX_RAND_LEN]; /* client rand */
42 u8 ak[EAP_PAX_AK_LEN];
43 u8 mk[EAP_PAX_MK_LEN];
44 u8 ck[EAP_PAX_CK_LEN];
45 u8 ick[EAP_PAX_ICK_LEN];
49 static void eap_pax_deinit(struct eap_sm *sm, void *priv);
52 static void * eap_pax_init(struct eap_sm *sm)
54 struct eap_pax_data *data;
55 const u8 *identity, *password;
56 size_t identity_len, password_len;
58 identity = eap_get_config_identity(sm, &identity_len);
59 password = eap_get_config_password(sm, &password_len);
60 if (!identity || !password) {
61 wpa_printf(MSG_INFO, "EAP-PAX: CID (nai) or key (password) "
66 if (password_len != EAP_PAX_AK_LEN) {
67 wpa_printf(MSG_INFO, "EAP-PAX: Invalid PSK length");
71 data = os_zalloc(sizeof(*data));
74 data->state = PAX_INIT;
76 data->cid = os_malloc(identity_len);
77 if (data->cid == NULL) {
78 eap_pax_deinit(sm, data);
81 os_memcpy(data->cid, identity, identity_len);
82 data->cid_len = identity_len;
84 os_memcpy(data->ak, password, EAP_PAX_AK_LEN);
90 static void eap_pax_deinit(struct eap_sm *sm, void *priv)
92 struct eap_pax_data *data = priv;
98 static struct wpabuf * eap_pax_alloc_resp(const struct eap_pax_hdr *req,
99 u8 id, u8 op_code, size_t plen)
102 struct eap_pax_hdr *pax;
104 resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PAX,
105 sizeof(*pax) + plen, EAP_CODE_RESPONSE, id);
109 pax = wpabuf_put(resp, sizeof(*pax));
110 pax->op_code = op_code;
112 pax->mac_id = req->mac_id;
113 pax->dh_group_id = req->dh_group_id;
114 pax->public_key_id = req->public_key_id;
120 static struct wpabuf * eap_pax_process_std_1(struct eap_pax_data *data,
121 struct eap_method_ret *ret, u8 id,
122 const struct eap_pax_hdr *req,
130 wpa_printf(MSG_DEBUG, "EAP-PAX: PAX_STD-1 (received)");
132 if (data->state != PAX_INIT) {
133 wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-1 received in "
134 "unexpected state (%d) - ignored", data->state);
139 if (req->flags & EAP_PAX_FLAGS_CE) {
140 wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-1 with CE flag set - "
146 left = req_plen - sizeof(*req);
148 if (left < 2 + EAP_PAX_RAND_LEN) {
149 wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-1 with too short "
155 pos = (const u8 *) (req + 1);
156 if (WPA_GET_BE16(pos) != EAP_PAX_RAND_LEN) {
157 wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-1 with incorrect A "
158 "length %d (expected %d)",
159 WPA_GET_BE16(pos), EAP_PAX_RAND_LEN);
166 os_memcpy(data->rand.r.x, pos, EAP_PAX_RAND_LEN);
167 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: X (server rand)",
168 data->rand.r.x, EAP_PAX_RAND_LEN);
169 pos += EAP_PAX_RAND_LEN;
170 left -= EAP_PAX_RAND_LEN;
173 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ignored extra payload",
177 if (os_get_random(data->rand.r.y, EAP_PAX_RAND_LEN)) {
178 wpa_printf(MSG_ERROR, "EAP-PAX: Failed to get random data");
182 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: Y (client rand)",
183 data->rand.r.y, EAP_PAX_RAND_LEN);
185 if (eap_pax_initial_key_derivation(req->mac_id, data->ak, data->rand.e,
186 data->mk, data->ck, data->ick) < 0)
192 wpa_printf(MSG_DEBUG, "EAP-PAX: PAX_STD-2 (sending)");
194 plen = 2 + EAP_PAX_RAND_LEN + 2 + data->cid_len + 2 + EAP_PAX_MAC_LEN +
196 resp = eap_pax_alloc_resp(req, id, EAP_PAX_OP_STD_2, plen);
200 wpabuf_put_be16(resp, EAP_PAX_RAND_LEN);
201 wpabuf_put_data(resp, data->rand.r.y, EAP_PAX_RAND_LEN);
202 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: B = Y (client rand)",
203 data->rand.r.y, EAP_PAX_RAND_LEN);
205 wpabuf_put_be16(resp, data->cid_len);
206 wpabuf_put_data(resp, data->cid, data->cid_len);
207 wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-PAX: CID",
208 (u8 *) data->cid, data->cid_len);
210 wpabuf_put_be16(resp, EAP_PAX_MAC_LEN);
211 rpos = wpabuf_put(resp, EAP_PAX_MAC_LEN);
212 eap_pax_mac(req->mac_id, data->ck, EAP_PAX_CK_LEN,
213 data->rand.r.x, EAP_PAX_RAND_LEN,
214 data->rand.r.y, EAP_PAX_RAND_LEN,
215 (u8 *) data->cid, data->cid_len, rpos);
216 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: MAC_CK(A, B, CID)",
217 rpos, EAP_PAX_MAC_LEN);
219 /* Optional ADE could be added here, if needed */
221 rpos = wpabuf_put(resp, EAP_PAX_ICV_LEN);
222 eap_pax_mac(req->mac_id, data->ick, EAP_PAX_ICK_LEN,
223 wpabuf_head(resp), wpabuf_len(resp) - EAP_PAX_ICV_LEN,
224 NULL, 0, NULL, 0, rpos);
225 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", rpos, EAP_PAX_ICV_LEN);
227 data->state = PAX_STD_2_SENT;
228 data->mac_id = req->mac_id;
229 data->dh_group_id = req->dh_group_id;
230 data->public_key_id = req->public_key_id;
236 static struct wpabuf * eap_pax_process_std_3(struct eap_pax_data *data,
237 struct eap_method_ret *ret, u8 id,
238 const struct eap_pax_hdr *req,
242 u8 *rpos, mac[EAP_PAX_MAC_LEN];
246 wpa_printf(MSG_DEBUG, "EAP-PAX: PAX_STD-3 (received)");
248 if (data->state != PAX_STD_2_SENT) {
249 wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-3 received in "
250 "unexpected state (%d) - ignored", data->state);
255 if (req->flags & EAP_PAX_FLAGS_CE) {
256 wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-3 with CE flag set - "
262 left = req_plen - sizeof(*req);
264 if (left < 2 + EAP_PAX_MAC_LEN) {
265 wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-3 with too short "
271 pos = (const u8 *) (req + 1);
272 if (WPA_GET_BE16(pos) != EAP_PAX_MAC_LEN) {
273 wpa_printf(MSG_INFO, "EAP-PAX: PAX_STD-3 with incorrect "
274 "MAC_CK length %d (expected %d)",
275 WPA_GET_BE16(pos), EAP_PAX_MAC_LEN);
281 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: MAC_CK(B, CID)",
282 pos, EAP_PAX_MAC_LEN);
283 eap_pax_mac(data->mac_id, data->ck, EAP_PAX_CK_LEN,
284 data->rand.r.y, EAP_PAX_RAND_LEN,
285 (u8 *) data->cid, data->cid_len, NULL, 0, mac);
286 if (os_memcmp(pos, mac, EAP_PAX_MAC_LEN) != 0) {
287 wpa_printf(MSG_INFO, "EAP-PAX: Invalid MAC_CK(B, CID) "
289 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: expected MAC_CK(B, CID)",
290 mac, EAP_PAX_MAC_LEN);
291 ret->methodState = METHOD_DONE;
292 ret->decision = DECISION_FAIL;
296 pos += EAP_PAX_MAC_LEN;
297 left -= EAP_PAX_MAC_LEN;
300 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ignored extra payload",
304 wpa_printf(MSG_DEBUG, "EAP-PAX: PAX-ACK (sending)");
306 resp = eap_pax_alloc_resp(req, id, EAP_PAX_OP_ACK, EAP_PAX_ICV_LEN);
310 /* Optional ADE could be added here, if needed */
312 rpos = wpabuf_put(resp, EAP_PAX_ICV_LEN);
313 eap_pax_mac(data->mac_id, data->ick, EAP_PAX_ICK_LEN,
314 wpabuf_head(resp), wpabuf_len(resp) - EAP_PAX_ICV_LEN,
315 NULL, 0, NULL, 0, rpos);
316 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", rpos, EAP_PAX_ICV_LEN);
318 data->state = PAX_DONE;
319 ret->methodState = METHOD_DONE;
320 ret->decision = DECISION_UNCOND_SUCC;
321 ret->allowNotifications = FALSE;
327 static struct wpabuf * eap_pax_process(struct eap_sm *sm, void *priv,
328 struct eap_method_ret *ret,
329 const struct wpabuf *reqData)
331 struct eap_pax_data *data = priv;
332 const struct eap_pax_hdr *req;
334 u8 icvbuf[EAP_PAX_ICV_LEN], id;
339 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PAX, reqData, &len);
340 if (pos == NULL || len < EAP_PAX_ICV_LEN) {
344 id = eap_get_id(reqData);
345 req = (const struct eap_pax_hdr *) pos;
346 flen = len - EAP_PAX_ICV_LEN;
347 mlen = wpabuf_len(reqData) - EAP_PAX_ICV_LEN;
349 wpa_printf(MSG_DEBUG, "EAP-PAX: received frame: op_code 0x%x "
350 "flags 0x%x mac_id 0x%x dh_group_id 0x%x "
351 "public_key_id 0x%x",
352 req->op_code, req->flags, req->mac_id, req->dh_group_id,
354 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: received payload",
355 pos, len - EAP_PAX_ICV_LEN);
357 if (data->state != PAX_INIT && data->mac_id != req->mac_id) {
358 wpa_printf(MSG_INFO, "EAP-PAX: MAC ID changed during "
359 "authentication (was 0x%d, is 0x%d)",
360 data->mac_id, req->mac_id);
365 if (data->state != PAX_INIT && data->dh_group_id != req->dh_group_id) {
366 wpa_printf(MSG_INFO, "EAP-PAX: DH Group ID changed during "
367 "authentication (was 0x%d, is 0x%d)",
368 data->dh_group_id, req->dh_group_id);
373 if (data->state != PAX_INIT &&
374 data->public_key_id != req->public_key_id) {
375 wpa_printf(MSG_INFO, "EAP-PAX: Public Key ID changed during "
376 "authentication (was 0x%d, is 0x%d)",
377 data->public_key_id, req->public_key_id);
382 /* TODO: add support EAP_PAX_HMAC_SHA256_128 */
383 if (req->mac_id != EAP_PAX_MAC_HMAC_SHA1_128) {
384 wpa_printf(MSG_INFO, "EAP-PAX: Unsupported MAC ID 0x%x",
390 if (req->dh_group_id != EAP_PAX_DH_GROUP_NONE) {
391 wpa_printf(MSG_INFO, "EAP-PAX: Unsupported DH Group ID 0x%x",
397 if (req->public_key_id != EAP_PAX_PUBLIC_KEY_NONE) {
398 wpa_printf(MSG_INFO, "EAP-PAX: Unsupported Public Key ID 0x%x",
404 if (req->flags & EAP_PAX_FLAGS_MF) {
405 /* TODO: add support for reassembling fragments */
406 wpa_printf(MSG_INFO, "EAP-PAX: fragmentation not supported - "
412 icv = pos + len - EAP_PAX_ICV_LEN;
413 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: ICV", icv, EAP_PAX_ICV_LEN);
414 if (req->op_code == EAP_PAX_OP_STD_1) {
415 eap_pax_mac(req->mac_id, (u8 *) "", 0,
416 wpabuf_head(reqData), mlen, NULL, 0, NULL, 0,
419 eap_pax_mac(req->mac_id, data->ick, EAP_PAX_ICK_LEN,
420 wpabuf_head(reqData), mlen, NULL, 0, NULL, 0,
423 if (os_memcmp(icv, icvbuf, EAP_PAX_ICV_LEN) != 0) {
424 wpa_printf(MSG_DEBUG, "EAP-PAX: invalid ICV - ignoring the "
426 wpa_hexdump(MSG_MSGDUMP, "EAP-PAX: expected ICV",
427 icvbuf, EAP_PAX_ICV_LEN);
433 ret->methodState = METHOD_MAY_CONT;
434 ret->decision = DECISION_FAIL;
435 ret->allowNotifications = TRUE;
437 switch (req->op_code) {
438 case EAP_PAX_OP_STD_1:
439 resp = eap_pax_process_std_1(data, ret, id, req, flen);
441 case EAP_PAX_OP_STD_3:
442 resp = eap_pax_process_std_3(data, ret, id, req, flen);
445 wpa_printf(MSG_DEBUG, "EAP-PAX: ignoring message with unknown "
446 "op_code %d", req->op_code);
451 if (ret->methodState == METHOD_DONE) {
452 ret->allowNotifications = FALSE;
459 static Boolean eap_pax_isKeyAvailable(struct eap_sm *sm, void *priv)
461 struct eap_pax_data *data = priv;
462 return data->state == PAX_DONE;
466 static u8 * eap_pax_getKey(struct eap_sm *sm, void *priv, size_t *len)
468 struct eap_pax_data *data = priv;
471 if (data->state != PAX_DONE)
474 key = os_malloc(EAP_MSK_LEN);
479 eap_pax_kdf(data->mac_id, data->mk, EAP_PAX_MK_LEN,
480 "Master Session Key", data->rand.e, 2 * EAP_PAX_RAND_LEN,
487 static u8 * eap_pax_get_emsk(struct eap_sm *sm, void *priv, size_t *len)
489 struct eap_pax_data *data = priv;
492 if (data->state != PAX_DONE)
495 key = os_malloc(EAP_EMSK_LEN);
500 eap_pax_kdf(data->mac_id, data->mk, EAP_PAX_MK_LEN,
501 "Extended Master Session Key",
502 data->rand.e, 2 * EAP_PAX_RAND_LEN,
509 int eap_peer_pax_register(void)
511 struct eap_method *eap;
514 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
515 EAP_VENDOR_IETF, EAP_TYPE_PAX, "PAX");
519 eap->init = eap_pax_init;
520 eap->deinit = eap_pax_deinit;
521 eap->process = eap_pax_process;
522 eap->isKeyAvailable = eap_pax_isKeyAvailable;
523 eap->getKey = eap_pax_getKey;
524 eap->get_emsk = eap_pax_get_emsk;
526 ret = eap_peer_method_register(eap);
528 eap_peer_method_free(eap);