Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / wpa / src / eap_peer / eap_peap.c
blob954ecf1312b859561c68b2dadc8c3bd49ba0c550
1 /*
2 * EAP peer method: EAP-PEAP (draft-josefsson-pppext-eap-tls-eap-10.txt)
3 * Copyright (c) 2004-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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "crypto/sha1.h"
19 #include "eap_i.h"
20 #include "eap_tls_common.h"
21 #include "eap_config.h"
22 #include "tls.h"
23 #include "eap_common/eap_tlv_common.h"
24 #include "eap_common/eap_peap_common.h"
25 #include "tncc.h"
28 /* Maximum supported PEAP version
29 * 0 = Microsoft's PEAP version 0; draft-kamath-pppext-peapv0-00.txt
30 * 1 = draft-josefsson-ppext-eap-tls-eap-05.txt
31 * 2 = draft-josefsson-ppext-eap-tls-eap-10.txt
33 #define EAP_PEAP_VERSION 1
36 static void eap_peap_deinit(struct eap_sm *sm, void *priv);
39 struct eap_peap_data {
40 struct eap_ssl_data ssl;
42 int peap_version, force_peap_version, force_new_label;
44 const struct eap_method *phase2_method;
45 void *phase2_priv;
46 int phase2_success;
47 int phase2_eap_success;
48 int phase2_eap_started;
50 struct eap_method_type phase2_type;
51 struct eap_method_type *phase2_types;
52 size_t num_phase2_types;
54 int peap_outer_success; /* 0 = PEAP terminated on Phase 2 inner
55 * EAP-Success
56 * 1 = reply with tunneled EAP-Success to inner
57 * EAP-Success and expect AS to send outer
58 * (unencrypted) EAP-Success after this
59 * 2 = reply with PEAP/TLS ACK to inner
60 * EAP-Success and expect AS to send outer
61 * (unencrypted) EAP-Success after this */
62 int resuming; /* starting a resumed session */
63 u8 *key_data;
65 struct wpabuf *pending_phase2_req;
66 enum { NO_BINDING, OPTIONAL_BINDING, REQUIRE_BINDING } crypto_binding;
67 int crypto_binding_used;
68 u8 ipmk[40];
69 u8 cmk[20];
70 int soh; /* Whether IF-TNCCS-SOH (Statement of Health; Microsoft NAP)
71 * is enabled. */
75 static int eap_peap_parse_phase1(struct eap_peap_data *data,
76 const char *phase1)
78 const char *pos;
80 pos = os_strstr(phase1, "peapver=");
81 if (pos) {
82 data->force_peap_version = atoi(pos + 8);
83 data->peap_version = data->force_peap_version;
84 wpa_printf(MSG_DEBUG, "EAP-PEAP: Forced PEAP version %d",
85 data->force_peap_version);
88 if (os_strstr(phase1, "peaplabel=1")) {
89 data->force_new_label = 1;
90 wpa_printf(MSG_DEBUG, "EAP-PEAP: Force new label for key "
91 "derivation");
94 if (os_strstr(phase1, "peap_outer_success=0")) {
95 data->peap_outer_success = 0;
96 wpa_printf(MSG_DEBUG, "EAP-PEAP: terminate authentication on "
97 "tunneled EAP-Success");
98 } else if (os_strstr(phase1, "peap_outer_success=1")) {
99 data->peap_outer_success = 1;
100 wpa_printf(MSG_DEBUG, "EAP-PEAP: send tunneled EAP-Success "
101 "after receiving tunneled EAP-Success");
102 } else if (os_strstr(phase1, "peap_outer_success=2")) {
103 data->peap_outer_success = 2;
104 wpa_printf(MSG_DEBUG, "EAP-PEAP: send PEAP/TLS ACK after "
105 "receiving tunneled EAP-Success");
108 if (os_strstr(phase1, "crypto_binding=0")) {
109 data->crypto_binding = NO_BINDING;
110 wpa_printf(MSG_DEBUG, "EAP-PEAP: Do not use cryptobinding");
111 } else if (os_strstr(phase1, "crypto_binding=1")) {
112 data->crypto_binding = OPTIONAL_BINDING;
113 wpa_printf(MSG_DEBUG, "EAP-PEAP: Optional cryptobinding");
114 } else if (os_strstr(phase1, "crypto_binding=2")) {
115 data->crypto_binding = REQUIRE_BINDING;
116 wpa_printf(MSG_DEBUG, "EAP-PEAP: Require cryptobinding");
119 #ifdef EAP_TNC
120 if (os_strstr(phase1, "tnc=soh")) {
121 data->soh = 1;
122 wpa_printf(MSG_DEBUG, "EAP-PEAP: SoH enabled");
124 #endif /* EAP_TNC */
126 return 0;
130 static void * eap_peap_init(struct eap_sm *sm)
132 struct eap_peap_data *data;
133 struct eap_peer_config *config = eap_get_config(sm);
135 data = os_zalloc(sizeof(*data));
136 if (data == NULL)
137 return NULL;
138 sm->peap_done = FALSE;
139 data->peap_version = EAP_PEAP_VERSION;
140 data->force_peap_version = -1;
141 data->peap_outer_success = 2;
142 data->crypto_binding = OPTIONAL_BINDING;
144 if (config && config->phase1 &&
145 eap_peap_parse_phase1(data, config->phase1) < 0) {
146 eap_peap_deinit(sm, data);
147 return NULL;
150 if (eap_peer_select_phase2_methods(config, "auth=",
151 &data->phase2_types,
152 &data->num_phase2_types) < 0) {
153 eap_peap_deinit(sm, data);
154 return NULL;
157 data->phase2_type.vendor = EAP_VENDOR_IETF;
158 data->phase2_type.method = EAP_TYPE_NONE;
160 if (eap_peer_tls_ssl_init(sm, &data->ssl, config)) {
161 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to initialize SSL.");
162 eap_peap_deinit(sm, data);
163 return NULL;
166 return data;
170 static void eap_peap_deinit(struct eap_sm *sm, void *priv)
172 struct eap_peap_data *data = priv;
173 if (data == NULL)
174 return;
175 if (data->phase2_priv && data->phase2_method)
176 data->phase2_method->deinit(sm, data->phase2_priv);
177 os_free(data->phase2_types);
178 eap_peer_tls_ssl_deinit(sm, &data->ssl);
179 os_free(data->key_data);
180 wpabuf_free(data->pending_phase2_req);
181 os_free(data);
186 * eap_tlv_build_nak - Build EAP-TLV NAK message
187 * @id: EAP identifier for the header
188 * @nak_type: TLV type (EAP_TLV_*)
189 * Returns: Buffer to the allocated EAP-TLV NAK message or %NULL on failure
191 * This funtion builds an EAP-TLV NAK message. The caller is responsible for
192 * freeing the returned buffer.
194 static struct wpabuf * eap_tlv_build_nak(int id, u16 nak_type)
196 struct wpabuf *msg;
198 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, 10,
199 EAP_CODE_RESPONSE, id);
200 if (msg == NULL)
201 return NULL;
203 wpabuf_put_u8(msg, 0x80); /* Mandatory */
204 wpabuf_put_u8(msg, EAP_TLV_NAK_TLV);
205 wpabuf_put_be16(msg, 6); /* Length */
206 wpabuf_put_be32(msg, 0); /* Vendor-Id */
207 wpabuf_put_be16(msg, nak_type); /* NAK-Type */
209 return msg;
213 static int eap_peap_get_isk(struct eap_sm *sm, struct eap_peap_data *data,
214 u8 *isk, size_t isk_len)
216 u8 *key;
217 size_t key_len;
219 os_memset(isk, 0, isk_len);
220 if (data->phase2_method == NULL || data->phase2_priv == NULL ||
221 data->phase2_method->isKeyAvailable == NULL ||
222 data->phase2_method->getKey == NULL)
223 return 0;
225 if (!data->phase2_method->isKeyAvailable(sm, data->phase2_priv) ||
226 (key = data->phase2_method->getKey(sm, data->phase2_priv,
227 &key_len)) == NULL) {
228 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not get key material "
229 "from Phase 2");
230 return -1;
233 if (key_len == 32 &&
234 data->phase2_method->vendor == EAP_VENDOR_IETF &&
235 data->phase2_method->method == EAP_TYPE_MSCHAPV2) {
237 * Microsoft uses reverse order for MS-MPPE keys in
238 * EAP-PEAP when compared to EAP-FAST derivation of
239 * ISK. Swap the keys here to get the correct ISK for
240 * EAP-PEAPv0 cryptobinding.
242 u8 tmp[16];
243 os_memcpy(tmp, key, 16);
244 os_memcpy(key, key + 16, 16);
245 os_memcpy(key + 16, tmp, 16);
248 if (key_len > isk_len)
249 key_len = isk_len;
250 os_memcpy(isk, key, key_len);
251 os_free(key);
253 return 0;
257 static int eap_peap_derive_cmk(struct eap_sm *sm, struct eap_peap_data *data)
259 u8 *tk;
260 u8 isk[32], imck[60];
263 * Tunnel key (TK) is the first 60 octets of the key generated by
264 * phase 1 of PEAP (based on TLS).
266 tk = data->key_data;
267 if (tk == NULL)
268 return -1;
269 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TK", tk, 60);
271 if (eap_peap_get_isk(sm, data, isk, sizeof(isk)) < 0)
272 return -1;
273 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: ISK", isk, sizeof(isk));
276 * IPMK Seed = "Inner Methods Compound Keys" | ISK
277 * TempKey = First 40 octets of TK
278 * IPMK|CMK = PRF+(TempKey, IPMK Seed, 60)
279 * (note: draft-josefsson-pppext-eap-tls-eap-10.txt includes a space
280 * in the end of the label just before ISK; is that just a typo?)
282 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: TempKey", tk, 40);
283 peap_prfplus(data->peap_version, tk, 40, "Inner Methods Compound Keys",
284 isk, sizeof(isk), imck, sizeof(imck));
285 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IMCK (IPMKj)",
286 imck, sizeof(imck));
288 /* TODO: fast-connect: IPMK|CMK = TK */
289 os_memcpy(data->ipmk, imck, 40);
290 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: IPMK (S-IPMKj)", data->ipmk, 40);
291 os_memcpy(data->cmk, imck + 40, 20);
292 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CMK (CMKj)", data->cmk, 20);
294 return 0;
298 static int eap_tlv_add_cryptobinding(struct eap_sm *sm,
299 struct eap_peap_data *data,
300 struct wpabuf *buf)
302 u8 *mac;
303 u8 eap_type = EAP_TYPE_PEAP;
304 const u8 *addr[2];
305 size_t len[2];
306 u16 tlv_type;
307 u8 binding_nonce[32];
309 /* FIX: should binding_nonce be copied from request? */
310 if (os_get_random(binding_nonce, 32))
311 return -1;
313 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
314 addr[0] = wpabuf_put(buf, 0);
315 len[0] = 60;
316 addr[1] = &eap_type;
317 len[1] = 1;
319 tlv_type = EAP_TLV_CRYPTO_BINDING_TLV;
320 if (data->peap_version >= 2)
321 tlv_type |= EAP_TLV_TYPE_MANDATORY;
322 wpabuf_put_be16(buf, tlv_type);
323 wpabuf_put_be16(buf, 56);
325 wpabuf_put_u8(buf, 0); /* Reserved */
326 wpabuf_put_u8(buf, data->peap_version); /* Version */
327 wpabuf_put_u8(buf, data->peap_version); /* RecvVersion */
328 wpabuf_put_u8(buf, 1); /* SubType: 0 = Request, 1 = Response */
329 wpabuf_put_data(buf, binding_nonce, 32); /* Nonce */
330 mac = wpabuf_put(buf, 20); /* Compound_MAC */
331 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC CMK", data->cmk, 20);
332 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 1",
333 addr[0], len[0]);
334 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC data 2",
335 addr[1], len[1]);
336 hmac_sha1_vector(data->cmk, 20, 2, addr, len, mac);
337 wpa_hexdump(MSG_MSGDUMP, "EAP-PEAP: Compound_MAC", mac, SHA1_MAC_LEN);
338 data->crypto_binding_used = 1;
340 return 0;
345 * eap_tlv_build_result - Build EAP-TLV Result message
346 * @id: EAP identifier for the header
347 * @status: Status (EAP_TLV_RESULT_SUCCESS or EAP_TLV_RESULT_FAILURE)
348 * Returns: Buffer to the allocated EAP-TLV Result message or %NULL on failure
350 * This funtion builds an EAP-TLV Result message. The caller is responsible for
351 * freeing the returned buffer.
353 static struct wpabuf * eap_tlv_build_result(struct eap_sm *sm,
354 struct eap_peap_data *data,
355 int crypto_tlv_used,
356 int id, u16 status)
358 struct wpabuf *msg;
359 size_t len;
361 if (data->crypto_binding == NO_BINDING)
362 crypto_tlv_used = 0;
364 len = 6;
365 if (crypto_tlv_used)
366 len += 60; /* Cryptobinding TLV */
367 msg = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_TLV, len,
368 EAP_CODE_RESPONSE, id);
369 if (msg == NULL)
370 return NULL;
372 wpabuf_put_u8(msg, 0x80); /* Mandatory */
373 wpabuf_put_u8(msg, EAP_TLV_RESULT_TLV);
374 wpabuf_put_be16(msg, 2); /* Length */
375 wpabuf_put_be16(msg, status); /* Status */
377 if (crypto_tlv_used && eap_tlv_add_cryptobinding(sm, data, msg)) {
378 wpabuf_free(msg);
379 return NULL;
382 return msg;
386 static int eap_tlv_validate_cryptobinding(struct eap_sm *sm,
387 struct eap_peap_data *data,
388 const u8 *crypto_tlv,
389 size_t crypto_tlv_len)
391 u8 buf[61], mac[SHA1_MAC_LEN];
392 const u8 *pos;
394 if (eap_peap_derive_cmk(sm, data) < 0) {
395 wpa_printf(MSG_DEBUG, "EAP-PEAP: Could not derive CMK");
396 return -1;
399 if (crypto_tlv_len != 4 + 56) {
400 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid cryptobinding TLV "
401 "length %d", (int) crypto_tlv_len);
402 return -1;
405 pos = crypto_tlv;
406 pos += 4; /* TLV header */
407 if (pos[1] != data->peap_version) {
408 wpa_printf(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV Version "
409 "mismatch (was %d; expected %d)",
410 pos[1], data->peap_version);
411 return -1;
414 if (pos[3] != 0) {
415 wpa_printf(MSG_DEBUG, "EAP-PEAP: Unexpected Cryptobinding TLV "
416 "SubType %d", pos[3]);
417 return -1;
419 pos += 4;
420 pos += 32; /* Nonce */
422 /* Compound_MAC: HMAC-SHA1-160(cryptobinding TLV | EAP type) */
423 os_memcpy(buf, crypto_tlv, 60);
424 os_memset(buf + 4 + 4 + 32, 0, 20); /* Compound_MAC */
425 buf[60] = EAP_TYPE_PEAP;
426 hmac_sha1(data->cmk, 20, buf, sizeof(buf), mac);
428 if (os_memcmp(mac, pos, SHA1_MAC_LEN) != 0) {
429 wpa_printf(MSG_DEBUG, "EAP-PEAP: Invalid Compound_MAC in "
430 "cryptobinding TLV");
431 return -1;
434 wpa_printf(MSG_DEBUG, "EAP-PEAP: Valid cryptobinding TLV received");
436 return 0;
441 * eap_tlv_process - Process a received EAP-TLV message and generate a response
442 * @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
443 * @ret: Return values from EAP request validation and processing
444 * @req: EAP-TLV request to be processed. The caller must have validated that
445 * the buffer is large enough to contain full request (hdr->length bytes) and
446 * that the EAP type is EAP_TYPE_TLV.
447 * @resp: Buffer to return a pointer to the allocated response message. This
448 * field should be initialized to %NULL before the call. The value will be
449 * updated if a response message is generated. The caller is responsible for
450 * freeing the allocated message.
451 * @force_failure: Force negotiation to fail
452 * Returns: 0 on success, -1 on failure
454 static int eap_tlv_process(struct eap_sm *sm, struct eap_peap_data *data,
455 struct eap_method_ret *ret,
456 const struct wpabuf *req, struct wpabuf **resp,
457 int force_failure)
459 size_t left, tlv_len;
460 const u8 *pos;
461 const u8 *result_tlv = NULL, *crypto_tlv = NULL;
462 size_t result_tlv_len = 0, crypto_tlv_len = 0;
463 int tlv_type, mandatory;
465 /* Parse TLVs */
466 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_TLV, req, &left);
467 if (pos == NULL)
468 return -1;
469 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Received TLVs", pos, left);
470 while (left >= 4) {
471 mandatory = !!(pos[0] & 0x80);
472 tlv_type = WPA_GET_BE16(pos) & 0x3fff;
473 pos += 2;
474 tlv_len = WPA_GET_BE16(pos);
475 pos += 2;
476 left -= 4;
477 if (tlv_len > left) {
478 wpa_printf(MSG_DEBUG, "EAP-TLV: TLV underrun "
479 "(tlv_len=%lu left=%lu)",
480 (unsigned long) tlv_len,
481 (unsigned long) left);
482 return -1;
484 switch (tlv_type) {
485 case EAP_TLV_RESULT_TLV:
486 result_tlv = pos;
487 result_tlv_len = tlv_len;
488 break;
489 case EAP_TLV_CRYPTO_BINDING_TLV:
490 crypto_tlv = pos;
491 crypto_tlv_len = tlv_len;
492 break;
493 default:
494 wpa_printf(MSG_DEBUG, "EAP-TLV: Unsupported TLV Type "
495 "%d%s", tlv_type,
496 mandatory ? " (mandatory)" : "");
497 if (mandatory) {
498 /* NAK TLV and ignore all TLVs in this packet.
500 *resp = eap_tlv_build_nak(eap_get_id(req),
501 tlv_type);
502 return *resp == NULL ? -1 : 0;
504 /* Ignore this TLV, but process other TLVs */
505 break;
508 pos += tlv_len;
509 left -= tlv_len;
511 if (left) {
512 wpa_printf(MSG_DEBUG, "EAP-TLV: Last TLV too short in "
513 "Request (left=%lu)", (unsigned long) left);
514 return -1;
517 /* Process supported TLVs */
518 if (crypto_tlv && data->crypto_binding != NO_BINDING) {
519 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Cryptobinding TLV",
520 crypto_tlv, crypto_tlv_len);
521 if (eap_tlv_validate_cryptobinding(sm, data, crypto_tlv - 4,
522 crypto_tlv_len + 4) < 0) {
523 if (result_tlv == NULL)
524 return -1;
525 force_failure = 1;
527 } else if (!crypto_tlv && data->crypto_binding == REQUIRE_BINDING) {
528 wpa_printf(MSG_DEBUG, "EAP-PEAP: No cryptobinding TLV");
529 return -1;
532 if (result_tlv) {
533 int status, resp_status;
534 wpa_hexdump(MSG_DEBUG, "EAP-TLV: Result TLV",
535 result_tlv, result_tlv_len);
536 if (result_tlv_len < 2) {
537 wpa_printf(MSG_INFO, "EAP-TLV: Too short Result TLV "
538 "(len=%lu)",
539 (unsigned long) result_tlv_len);
540 return -1;
542 status = WPA_GET_BE16(result_tlv);
543 if (status == EAP_TLV_RESULT_SUCCESS) {
544 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Success "
545 "- EAP-TLV/Phase2 Completed");
546 if (force_failure) {
547 wpa_printf(MSG_INFO, "EAP-TLV: Earlier failure"
548 " - force failed Phase 2");
549 resp_status = EAP_TLV_RESULT_FAILURE;
550 ret->decision = DECISION_FAIL;
551 } else {
552 resp_status = EAP_TLV_RESULT_SUCCESS;
553 ret->decision = DECISION_UNCOND_SUCC;
555 } else if (status == EAP_TLV_RESULT_FAILURE) {
556 wpa_printf(MSG_INFO, "EAP-TLV: TLV Result - Failure");
557 resp_status = EAP_TLV_RESULT_FAILURE;
558 ret->decision = DECISION_FAIL;
559 } else {
560 wpa_printf(MSG_INFO, "EAP-TLV: Unknown TLV Result "
561 "Status %d", status);
562 resp_status = EAP_TLV_RESULT_FAILURE;
563 ret->decision = DECISION_FAIL;
565 ret->methodState = METHOD_DONE;
567 *resp = eap_tlv_build_result(sm, data, crypto_tlv != NULL,
568 eap_get_id(req), resp_status);
571 return 0;
575 static struct wpabuf * eap_peapv2_tlv_eap_payload(struct wpabuf *buf)
577 struct wpabuf *e;
578 struct eap_tlv_hdr *tlv;
580 if (buf == NULL)
581 return NULL;
583 /* Encapsulate EAP packet in EAP-Payload TLV */
584 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Add EAP-Payload TLV");
585 e = wpabuf_alloc(sizeof(*tlv) + wpabuf_len(buf));
586 if (e == NULL) {
587 wpa_printf(MSG_DEBUG, "EAP-PEAPv2: Failed to allocate memory "
588 "for TLV encapsulation");
589 wpabuf_free(buf);
590 return NULL;
592 tlv = wpabuf_put(e, sizeof(*tlv));
593 tlv->tlv_type = host_to_be16(EAP_TLV_TYPE_MANDATORY |
594 EAP_TLV_EAP_PAYLOAD_TLV);
595 tlv->length = host_to_be16(wpabuf_len(buf));
596 wpabuf_put_buf(e, buf);
597 wpabuf_free(buf);
598 return e;
602 static int eap_peap_phase2_request(struct eap_sm *sm,
603 struct eap_peap_data *data,
604 struct eap_method_ret *ret,
605 struct wpabuf *req,
606 struct wpabuf **resp)
608 struct eap_hdr *hdr = wpabuf_mhead(req);
609 size_t len = be_to_host16(hdr->length);
610 u8 *pos;
611 struct eap_method_ret iret;
612 struct eap_peer_config *config = eap_get_config(sm);
614 if (len <= sizeof(struct eap_hdr)) {
615 wpa_printf(MSG_INFO, "EAP-PEAP: too short "
616 "Phase 2 request (len=%lu)", (unsigned long) len);
617 return -1;
619 pos = (u8 *) (hdr + 1);
620 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Request: type=%d", *pos);
621 switch (*pos) {
622 case EAP_TYPE_IDENTITY:
623 *resp = eap_sm_buildIdentity(sm, hdr->identifier, 1);
624 break;
625 case EAP_TYPE_TLV:
626 os_memset(&iret, 0, sizeof(iret));
627 if (eap_tlv_process(sm, data, &iret, req, resp,
628 data->phase2_eap_started &&
629 !data->phase2_eap_success)) {
630 ret->methodState = METHOD_DONE;
631 ret->decision = DECISION_FAIL;
632 return -1;
634 if (iret.methodState == METHOD_DONE ||
635 iret.methodState == METHOD_MAY_CONT) {
636 ret->methodState = iret.methodState;
637 ret->decision = iret.decision;
638 data->phase2_success = 1;
640 break;
641 case EAP_TYPE_EXPANDED:
642 #ifdef EAP_TNC
643 if (data->soh) {
644 const u8 *epos;
645 size_t eleft;
647 epos = eap_hdr_validate(EAP_VENDOR_MICROSOFT, 0x21,
648 req, &eleft);
649 if (epos) {
650 struct wpabuf *buf;
651 wpa_printf(MSG_DEBUG,
652 "EAP-PEAP: SoH EAP Extensions");
653 buf = tncc_process_soh_request(epos, eleft);
654 if (buf) {
655 *resp = eap_msg_alloc(
656 EAP_VENDOR_MICROSOFT, 0x21,
657 wpabuf_len(buf),
658 EAP_CODE_RESPONSE,
659 hdr->identifier);
660 if (*resp == NULL) {
661 ret->methodState = METHOD_DONE;
662 ret->decision = DECISION_FAIL;
663 return -1;
665 wpabuf_put_buf(*resp, buf);
666 wpabuf_free(buf);
667 break;
671 #endif /* EAP_TNC */
672 /* fall through */
673 default:
674 if (data->phase2_type.vendor == EAP_VENDOR_IETF &&
675 data->phase2_type.method == EAP_TYPE_NONE) {
676 size_t i;
677 for (i = 0; i < data->num_phase2_types; i++) {
678 if (data->phase2_types[i].vendor !=
679 EAP_VENDOR_IETF ||
680 data->phase2_types[i].method != *pos)
681 continue;
683 data->phase2_type.vendor =
684 data->phase2_types[i].vendor;
685 data->phase2_type.method =
686 data->phase2_types[i].method;
687 wpa_printf(MSG_DEBUG, "EAP-PEAP: Selected "
688 "Phase 2 EAP vendor %d method %d",
689 data->phase2_type.vendor,
690 data->phase2_type.method);
691 break;
694 if (*pos != data->phase2_type.method ||
695 *pos == EAP_TYPE_NONE) {
696 if (eap_peer_tls_phase2_nak(data->phase2_types,
697 data->num_phase2_types,
698 hdr, resp))
699 return -1;
700 return 0;
703 if (data->phase2_priv == NULL) {
704 data->phase2_method = eap_peer_get_eap_method(
705 data->phase2_type.vendor,
706 data->phase2_type.method);
707 if (data->phase2_method) {
708 sm->init_phase2 = 1;
709 sm->mschapv2_full_key = 1;
710 data->phase2_priv =
711 data->phase2_method->init(sm);
712 sm->init_phase2 = 0;
713 sm->mschapv2_full_key = 0;
716 if (data->phase2_priv == NULL || data->phase2_method == NULL) {
717 wpa_printf(MSG_INFO, "EAP-PEAP: failed to initialize "
718 "Phase 2 EAP method %d", *pos);
719 ret->methodState = METHOD_DONE;
720 ret->decision = DECISION_FAIL;
721 return -1;
723 data->phase2_eap_started = 1;
724 os_memset(&iret, 0, sizeof(iret));
725 *resp = data->phase2_method->process(sm, data->phase2_priv,
726 &iret, req);
727 if ((iret.methodState == METHOD_DONE ||
728 iret.methodState == METHOD_MAY_CONT) &&
729 (iret.decision == DECISION_UNCOND_SUCC ||
730 iret.decision == DECISION_COND_SUCC)) {
731 data->phase2_eap_success = 1;
732 data->phase2_success = 1;
734 break;
737 if (*resp == NULL &&
738 (config->pending_req_identity || config->pending_req_password ||
739 config->pending_req_otp || config->pending_req_new_password)) {
740 wpabuf_free(data->pending_phase2_req);
741 data->pending_phase2_req = wpabuf_alloc_copy(hdr, len);
744 return 0;
748 static int eap_peap_decrypt(struct eap_sm *sm, struct eap_peap_data *data,
749 struct eap_method_ret *ret,
750 const struct eap_hdr *req,
751 const struct wpabuf *in_data,
752 struct wpabuf **out_data)
754 struct wpabuf *in_decrypted = NULL;
755 int res, skip_change = 0;
756 struct eap_hdr *hdr, *rhdr;
757 struct wpabuf *resp = NULL;
758 size_t len;
760 wpa_printf(MSG_DEBUG, "EAP-PEAP: received %lu bytes encrypted data for"
761 " Phase 2", (unsigned long) wpabuf_len(in_data));
763 if (data->pending_phase2_req) {
764 wpa_printf(MSG_DEBUG, "EAP-PEAP: Pending Phase 2 request - "
765 "skip decryption and use old data");
766 /* Clear TLS reassembly state. */
767 eap_peer_tls_reset_input(&data->ssl);
768 in_decrypted = data->pending_phase2_req;
769 data->pending_phase2_req = NULL;
770 skip_change = 1;
771 goto continue_req;
774 if (wpabuf_len(in_data) == 0 && sm->workaround &&
775 data->phase2_success) {
777 * Cisco ACS seems to be using TLS ACK to terminate
778 * EAP-PEAPv0/GTC. Try to reply with TLS ACK.
780 wpa_printf(MSG_DEBUG, "EAP-PEAP: Received TLS ACK, but "
781 "expected data - acknowledge with TLS ACK since "
782 "Phase 2 has been completed");
783 ret->decision = DECISION_COND_SUCC;
784 ret->methodState = METHOD_DONE;
785 return 1;
786 } else if (wpabuf_len(in_data) == 0) {
787 /* Received TLS ACK - requesting more fragments */
788 return eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
789 data->peap_version,
790 req->identifier, NULL, out_data);
793 res = eap_peer_tls_decrypt(sm, &data->ssl, in_data, &in_decrypted);
794 if (res)
795 return res;
797 continue_req:
798 wpa_hexdump_buf(MSG_DEBUG, "EAP-PEAP: Decrypted Phase 2 EAP",
799 in_decrypted);
801 hdr = wpabuf_mhead(in_decrypted);
802 if (wpabuf_len(in_decrypted) == 5 && hdr->code == EAP_CODE_REQUEST &&
803 be_to_host16(hdr->length) == 5 &&
804 eap_get_type(in_decrypted) == EAP_TYPE_IDENTITY) {
805 /* At least FreeRADIUS seems to send full EAP header with
806 * EAP Request Identity */
807 skip_change = 1;
809 if (wpabuf_len(in_decrypted) >= 5 && hdr->code == EAP_CODE_REQUEST &&
810 eap_get_type(in_decrypted) == EAP_TYPE_TLV) {
811 skip_change = 1;
814 if (data->peap_version == 0 && !skip_change) {
815 struct eap_hdr *nhdr;
816 struct wpabuf *nmsg = wpabuf_alloc(sizeof(struct eap_hdr) +
817 wpabuf_len(in_decrypted));
818 if (nmsg == NULL) {
819 wpabuf_free(in_decrypted);
820 return 0;
822 nhdr = wpabuf_put(nmsg, sizeof(*nhdr));
823 wpabuf_put_buf(nmsg, in_decrypted);
824 nhdr->code = req->code;
825 nhdr->identifier = req->identifier;
826 nhdr->length = host_to_be16(sizeof(struct eap_hdr) +
827 wpabuf_len(in_decrypted));
829 wpabuf_free(in_decrypted);
830 in_decrypted = nmsg;
833 if (data->peap_version >= 2) {
834 struct eap_tlv_hdr *tlv;
835 struct wpabuf *nmsg;
837 if (wpabuf_len(in_decrypted) < sizeof(*tlv) + sizeof(*hdr)) {
838 wpa_printf(MSG_INFO, "EAP-PEAPv2: Too short Phase 2 "
839 "EAP TLV");
840 wpabuf_free(in_decrypted);
841 return 0;
843 tlv = wpabuf_mhead(in_decrypted);
844 if ((be_to_host16(tlv->tlv_type) & 0x3fff) !=
845 EAP_TLV_EAP_PAYLOAD_TLV) {
846 wpa_printf(MSG_INFO, "EAP-PEAPv2: Not an EAP TLV");
847 wpabuf_free(in_decrypted);
848 return 0;
850 if (sizeof(*tlv) + be_to_host16(tlv->length) >
851 wpabuf_len(in_decrypted)) {
852 wpa_printf(MSG_INFO, "EAP-PEAPv2: Invalid EAP TLV "
853 "length");
854 wpabuf_free(in_decrypted);
855 return 0;
857 hdr = (struct eap_hdr *) (tlv + 1);
858 if (be_to_host16(hdr->length) > be_to_host16(tlv->length)) {
859 wpa_printf(MSG_INFO, "EAP-PEAPv2: No room for full "
860 "EAP packet in EAP TLV");
861 wpabuf_free(in_decrypted);
862 return 0;
865 nmsg = wpabuf_alloc(be_to_host16(hdr->length));
866 if (nmsg == NULL) {
867 wpabuf_free(in_decrypted);
868 return 0;
871 wpabuf_put_data(nmsg, hdr, be_to_host16(hdr->length));
872 wpabuf_free(in_decrypted);
873 in_decrypted = nmsg;
876 hdr = wpabuf_mhead(in_decrypted);
877 if (wpabuf_len(in_decrypted) < sizeof(*hdr)) {
878 wpa_printf(MSG_INFO, "EAP-PEAP: Too short Phase 2 "
879 "EAP frame (len=%lu)",
880 (unsigned long) wpabuf_len(in_decrypted));
881 wpabuf_free(in_decrypted);
882 return 0;
884 len = be_to_host16(hdr->length);
885 if (len > wpabuf_len(in_decrypted)) {
886 wpa_printf(MSG_INFO, "EAP-PEAP: Length mismatch in "
887 "Phase 2 EAP frame (len=%lu hdr->length=%lu)",
888 (unsigned long) wpabuf_len(in_decrypted),
889 (unsigned long) len);
890 wpabuf_free(in_decrypted);
891 return 0;
893 if (len < wpabuf_len(in_decrypted)) {
894 wpa_printf(MSG_INFO, "EAP-PEAP: Odd.. Phase 2 EAP header has "
895 "shorter length than full decrypted data "
896 "(%lu < %lu)",
897 (unsigned long) len,
898 (unsigned long) wpabuf_len(in_decrypted));
900 wpa_printf(MSG_DEBUG, "EAP-PEAP: received Phase 2: code=%d "
901 "identifier=%d length=%lu", hdr->code, hdr->identifier,
902 (unsigned long) len);
903 switch (hdr->code) {
904 case EAP_CODE_REQUEST:
905 if (eap_peap_phase2_request(sm, data, ret, in_decrypted,
906 &resp)) {
907 wpabuf_free(in_decrypted);
908 wpa_printf(MSG_INFO, "EAP-PEAP: Phase2 Request "
909 "processing failed");
910 return 0;
912 break;
913 case EAP_CODE_SUCCESS:
914 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Success");
915 if (data->peap_version == 1) {
916 /* EAP-Success within TLS tunnel is used to indicate
917 * shutdown of the TLS channel. The authentication has
918 * been completed. */
919 if (data->phase2_eap_started &&
920 !data->phase2_eap_success) {
921 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 "
922 "Success used to indicate success, "
923 "but Phase 2 EAP was not yet "
924 "completed successfully");
925 ret->methodState = METHOD_DONE;
926 ret->decision = DECISION_FAIL;
927 wpabuf_free(in_decrypted);
928 return 0;
930 wpa_printf(MSG_DEBUG, "EAP-PEAP: Version 1 - "
931 "EAP-Success within TLS tunnel - "
932 "authentication completed");
933 ret->decision = DECISION_UNCOND_SUCC;
934 ret->methodState = METHOD_DONE;
935 data->phase2_success = 1;
936 if (data->peap_outer_success == 2) {
937 wpabuf_free(in_decrypted);
938 wpa_printf(MSG_DEBUG, "EAP-PEAP: Use TLS ACK "
939 "to finish authentication");
940 return 1;
941 } else if (data->peap_outer_success == 1) {
942 /* Reply with EAP-Success within the TLS
943 * channel to complete the authentication. */
944 resp = wpabuf_alloc(sizeof(struct eap_hdr));
945 if (resp) {
946 rhdr = wpabuf_put(resp, sizeof(*rhdr));
947 rhdr->code = EAP_CODE_SUCCESS;
948 rhdr->identifier = hdr->identifier;
949 rhdr->length =
950 host_to_be16(sizeof(*rhdr));
952 } else {
953 /* No EAP-Success expected for Phase 1 (outer,
954 * unencrypted auth), so force EAP state
955 * machine to SUCCESS state. */
956 sm->peap_done = TRUE;
958 } else {
959 /* FIX: ? */
961 break;
962 case EAP_CODE_FAILURE:
963 wpa_printf(MSG_DEBUG, "EAP-PEAP: Phase 2 Failure");
964 ret->decision = DECISION_FAIL;
965 ret->methodState = METHOD_MAY_CONT;
966 ret->allowNotifications = FALSE;
967 /* Reply with EAP-Failure within the TLS channel to complete
968 * failure reporting. */
969 resp = wpabuf_alloc(sizeof(struct eap_hdr));
970 if (resp) {
971 rhdr = wpabuf_put(resp, sizeof(*rhdr));
972 rhdr->code = EAP_CODE_FAILURE;
973 rhdr->identifier = hdr->identifier;
974 rhdr->length = host_to_be16(sizeof(*rhdr));
976 break;
977 default:
978 wpa_printf(MSG_INFO, "EAP-PEAP: Unexpected code=%d in "
979 "Phase 2 EAP header", hdr->code);
980 break;
983 wpabuf_free(in_decrypted);
985 if (resp) {
986 int skip_change2 = 0;
987 struct wpabuf *rmsg, buf;
989 wpa_hexdump_buf_key(MSG_DEBUG,
990 "EAP-PEAP: Encrypting Phase 2 data", resp);
991 /* PEAP version changes */
992 if (data->peap_version >= 2) {
993 resp = eap_peapv2_tlv_eap_payload(resp);
994 if (resp == NULL)
995 return -1;
997 if (wpabuf_len(resp) >= 5 &&
998 wpabuf_head_u8(resp)[0] == EAP_CODE_RESPONSE &&
999 eap_get_type(resp) == EAP_TYPE_TLV)
1000 skip_change2 = 1;
1001 rmsg = resp;
1002 if (data->peap_version == 0 && !skip_change2) {
1003 wpabuf_set(&buf, wpabuf_head_u8(resp) +
1004 sizeof(struct eap_hdr),
1005 wpabuf_len(resp) - sizeof(struct eap_hdr));
1006 rmsg = &buf;
1009 if (eap_peer_tls_encrypt(sm, &data->ssl, EAP_TYPE_PEAP,
1010 data->peap_version, req->identifier,
1011 rmsg, out_data)) {
1012 wpa_printf(MSG_INFO, "EAP-PEAP: Failed to encrypt "
1013 "a Phase 2 frame");
1015 wpabuf_free(resp);
1018 return 0;
1022 static struct wpabuf * eap_peap_process(struct eap_sm *sm, void *priv,
1023 struct eap_method_ret *ret,
1024 const struct wpabuf *reqData)
1026 const struct eap_hdr *req;
1027 size_t left;
1028 int res;
1029 u8 flags, id;
1030 struct wpabuf *resp;
1031 const u8 *pos;
1032 struct eap_peap_data *data = priv;
1034 pos = eap_peer_tls_process_init(sm, &data->ssl, EAP_TYPE_PEAP, ret,
1035 reqData, &left, &flags);
1036 if (pos == NULL)
1037 return NULL;
1038 req = wpabuf_head(reqData);
1039 id = req->identifier;
1041 if (flags & EAP_TLS_FLAGS_START) {
1042 wpa_printf(MSG_DEBUG, "EAP-PEAP: Start (server ver=%d, own "
1043 "ver=%d)", flags & EAP_PEAP_VERSION_MASK,
1044 data->peap_version);
1045 if ((flags & EAP_PEAP_VERSION_MASK) < data->peap_version)
1046 data->peap_version = flags & EAP_PEAP_VERSION_MASK;
1047 if (data->force_peap_version >= 0 &&
1048 data->force_peap_version != data->peap_version) {
1049 wpa_printf(MSG_WARNING, "EAP-PEAP: Failed to select "
1050 "forced PEAP version %d",
1051 data->force_peap_version);
1052 ret->methodState = METHOD_DONE;
1053 ret->decision = DECISION_FAIL;
1054 ret->allowNotifications = FALSE;
1055 return NULL;
1057 wpa_printf(MSG_DEBUG, "EAP-PEAP: Using PEAP version %d",
1058 data->peap_version);
1059 left = 0; /* make sure that this frame is empty, even though it
1060 * should always be, anyway */
1063 resp = NULL;
1064 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1065 !data->resuming) {
1066 struct wpabuf msg;
1067 wpabuf_set(&msg, pos, left);
1068 res = eap_peap_decrypt(sm, data, ret, req, &msg, &resp);
1069 } else {
1070 res = eap_peer_tls_process_helper(sm, &data->ssl,
1071 EAP_TYPE_PEAP,
1072 data->peap_version, id, pos,
1073 left, &resp);
1075 if (tls_connection_established(sm->ssl_ctx, data->ssl.conn)) {
1076 char *label;
1077 wpa_printf(MSG_DEBUG,
1078 "EAP-PEAP: TLS done, proceed to Phase 2");
1079 os_free(data->key_data);
1080 /* draft-josefsson-ppext-eap-tls-eap-05.txt
1081 * specifies that PEAPv1 would use "client PEAP
1082 * encryption" as the label. However, most existing
1083 * PEAPv1 implementations seem to be using the old
1084 * label, "client EAP encryption", instead. Use the old
1085 * label by default, but allow it to be configured with
1086 * phase1 parameter peaplabel=1. */
1087 if (data->peap_version > 1 || data->force_new_label)
1088 label = "client PEAP encryption";
1089 else
1090 label = "client EAP encryption";
1091 wpa_printf(MSG_DEBUG, "EAP-PEAP: using label '%s' in "
1092 "key derivation", label);
1093 data->key_data =
1094 eap_peer_tls_derive_key(sm, &data->ssl, label,
1095 EAP_TLS_KEY_LEN);
1096 if (data->key_data) {
1097 wpa_hexdump_key(MSG_DEBUG,
1098 "EAP-PEAP: Derived key",
1099 data->key_data,
1100 EAP_TLS_KEY_LEN);
1101 } else {
1102 wpa_printf(MSG_DEBUG, "EAP-PEAP: Failed to "
1103 "derive key");
1106 if (sm->workaround && data->resuming) {
1108 * At least few RADIUS servers (Aegis v1.1.6;
1109 * but not v1.1.4; and Cisco ACS) seem to be
1110 * terminating PEAPv1 (Aegis) or PEAPv0 (Cisco
1111 * ACS) session resumption with outer
1112 * EAP-Success. This does not seem to follow
1113 * draft-josefsson-pppext-eap-tls-eap-05.txt
1114 * section 4.2, so only allow this if EAP
1115 * workarounds are enabled.
1117 wpa_printf(MSG_DEBUG, "EAP-PEAP: Workaround - "
1118 "allow outer EAP-Success to "
1119 "terminate PEAP resumption");
1120 ret->decision = DECISION_COND_SUCC;
1121 data->phase2_success = 1;
1124 data->resuming = 0;
1127 if (res == 2) {
1128 struct wpabuf msg;
1130 * Application data included in the handshake message.
1132 wpabuf_free(data->pending_phase2_req);
1133 data->pending_phase2_req = resp;
1134 resp = NULL;
1135 wpabuf_set(&msg, pos, left);
1136 res = eap_peap_decrypt(sm, data, ret, req, &msg,
1137 &resp);
1141 if (ret->methodState == METHOD_DONE) {
1142 ret->allowNotifications = FALSE;
1145 if (res == 1) {
1146 wpabuf_free(resp);
1147 return eap_peer_tls_build_ack(id, EAP_TYPE_PEAP,
1148 data->peap_version);
1151 return resp;
1155 static Boolean eap_peap_has_reauth_data(struct eap_sm *sm, void *priv)
1157 struct eap_peap_data *data = priv;
1158 return tls_connection_established(sm->ssl_ctx, data->ssl.conn) &&
1159 data->phase2_success;
1163 static void eap_peap_deinit_for_reauth(struct eap_sm *sm, void *priv)
1165 struct eap_peap_data *data = priv;
1166 wpabuf_free(data->pending_phase2_req);
1167 data->pending_phase2_req = NULL;
1168 data->crypto_binding_used = 0;
1172 static void * eap_peap_init_for_reauth(struct eap_sm *sm, void *priv)
1174 struct eap_peap_data *data = priv;
1175 os_free(data->key_data);
1176 data->key_data = NULL;
1177 if (eap_peer_tls_reauth_init(sm, &data->ssl)) {
1178 os_free(data);
1179 return NULL;
1181 if (data->phase2_priv && data->phase2_method &&
1182 data->phase2_method->init_for_reauth)
1183 data->phase2_method->init_for_reauth(sm, data->phase2_priv);
1184 data->phase2_success = 0;
1185 data->phase2_eap_success = 0;
1186 data->phase2_eap_started = 0;
1187 data->resuming = 1;
1188 sm->peap_done = FALSE;
1189 return priv;
1193 static int eap_peap_get_status(struct eap_sm *sm, void *priv, char *buf,
1194 size_t buflen, int verbose)
1196 struct eap_peap_data *data = priv;
1197 int len, ret;
1199 len = eap_peer_tls_status(sm, &data->ssl, buf, buflen, verbose);
1200 if (data->phase2_method) {
1201 ret = os_snprintf(buf + len, buflen - len,
1202 "EAP-PEAPv%d Phase2 method=%s\n",
1203 data->peap_version,
1204 data->phase2_method->name);
1205 if (ret < 0 || (size_t) ret >= buflen - len)
1206 return len;
1207 len += ret;
1209 return len;
1213 static Boolean eap_peap_isKeyAvailable(struct eap_sm *sm, void *priv)
1215 struct eap_peap_data *data = priv;
1216 return data->key_data != NULL && data->phase2_success;
1220 static u8 * eap_peap_getKey(struct eap_sm *sm, void *priv, size_t *len)
1222 struct eap_peap_data *data = priv;
1223 u8 *key;
1225 if (data->key_data == NULL || !data->phase2_success)
1226 return NULL;
1228 key = os_malloc(EAP_TLS_KEY_LEN);
1229 if (key == NULL)
1230 return NULL;
1232 *len = EAP_TLS_KEY_LEN;
1234 if (data->crypto_binding_used) {
1235 u8 csk[128];
1237 * Note: It looks like Microsoft implementation requires null
1238 * termination for this label while the one used for deriving
1239 * IPMK|CMK did not use null termination.
1241 peap_prfplus(data->peap_version, data->ipmk, 40,
1242 "Session Key Generating Function",
1243 (u8 *) "\00", 1, csk, sizeof(csk));
1244 wpa_hexdump_key(MSG_DEBUG, "EAP-PEAP: CSK", csk, sizeof(csk));
1245 os_memcpy(key, csk, EAP_TLS_KEY_LEN);
1246 wpa_hexdump(MSG_DEBUG, "EAP-PEAP: Derived key",
1247 key, EAP_TLS_KEY_LEN);
1248 } else
1249 os_memcpy(key, data->key_data, EAP_TLS_KEY_LEN);
1251 return key;
1255 int eap_peer_peap_register(void)
1257 struct eap_method *eap;
1258 int ret;
1260 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
1261 EAP_VENDOR_IETF, EAP_TYPE_PEAP, "PEAP");
1262 if (eap == NULL)
1263 return -1;
1265 eap->init = eap_peap_init;
1266 eap->deinit = eap_peap_deinit;
1267 eap->process = eap_peap_process;
1268 eap->isKeyAvailable = eap_peap_isKeyAvailable;
1269 eap->getKey = eap_peap_getKey;
1270 eap->get_status = eap_peap_get_status;
1271 eap->has_reauth_data = eap_peap_has_reauth_data;
1272 eap->deinit_for_reauth = eap_peap_deinit_for_reauth;
1273 eap->init_for_reauth = eap_peap_init_for_reauth;
1275 ret = eap_peer_method_register(eap);
1276 if (ret)
1277 eap_peer_method_free(eap);
1278 return ret;