Skip to content

Commit d7ccad3

Browse files
hpdhabeespressif-bot
authored andcommitted
wpa_supplicant: add support for PAP, MS-CHAP and CHAP as Phase 2 algorithms for TTLS alongside MS-CHAPv2
1 parent a60dccb commit d7ccad3

File tree

12 files changed

+1154
-77
lines changed

12 files changed

+1154
-77
lines changed

components/wpa_supplicant/include/esp_supplicant/esp_wpa2.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@
1919

2020
#include "esp_err.h"
2121

22+
typedef enum {
23+
ESP_EAP_TTLS_PHASE2_EAP,
24+
ESP_EAP_TTLS_PHASE2_MSCHAPV2,
25+
ESP_EAP_TTLS_PHASE2_MSCHAP,
26+
ESP_EAP_TTLS_PHASE2_PAP,
27+
ESP_EAP_TTLS_PHASE2_CHAP
28+
} esp_eap_ttls_phase2_types ;
29+
2230
#ifdef __cplusplus
2331
extern "C" {
2432
#endif
@@ -191,6 +199,16 @@ esp_err_t esp_wifi_sta_wpa2_ent_set_disable_time_check(bool disable);
191199
*/
192200
esp_err_t esp_wifi_sta_wpa2_ent_get_disable_time_check(bool *disable);
193201

202+
/**
203+
* @brief Set wpa2 enterprise ttls phase2 method
204+
*
205+
* @param type: the type of phase 2 method to be used
206+
*
207+
* @return
208+
* - ESP_OK: succeed
209+
*/
210+
esp_err_t esp_wifi_sta_wpa2_ent_set_ttls_phase2_method(esp_eap_ttls_phase2_types type);
211+
194212
#ifdef __cplusplus
195213
}
196214
#endif

components/wpa_supplicant/src/eap_peer/chap.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
* CHAP-MD5
33
*
44
*/
5-
#ifdef CHAP_MD5
5+
#ifndef CHAP_MD5
6+
#define CHAP_MD5
67

78
#include "utils/includes.h"
89
#include "utils/common.h"
910
#include "crypto/crypto.h"
10-
#include "wpa2/eap_peer/chap.h"
11+
#include "eap_peer/chap.h"
1112

1213
int chap_md5(u8 id, const u8 *secret, size_t secret_len, const u8 *challenge,
1314
size_t challenge_len, u8 *response)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* CHAP-MD5 (RFC 1994)
3+
* Copyright (c) 2007-2009, Jouni Malinen <[email protected]>
4+
*
5+
* This software may be distributed under the terms of the BSD license.
6+
* See README for more details.
7+
*/
8+
9+
#ifndef CHAP_H
10+
#define CHAP_H
11+
12+
#define CHAP_MD5_LEN 16
13+
14+
int chap_md5(u8 id, const u8 *secret, size_t secret_len, const u8 *challenge,
15+
size_t challenge_len, u8 *response);
16+
17+
#endif /* CHAP_H */

components/wpa_supplicant/src/eap_peer/eap.c

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "rsn_supp/wpa.h"
2828

2929
#include "crypto/crypto.h"
30+
#include "crypto/sha256.h"
3031

3132
#include "utils/ext_password.h"
3233
#include "tls/tls.h"
@@ -191,6 +192,8 @@ void eap_peer_unregister_methods(void)
191192
}
192193
}
193194

195+
196+
194197
int eap_peer_register_methods(void)
195198
{
196199
int ret = 0;
@@ -232,6 +235,90 @@ void eap_deinit_prev_method(struct eap_sm *sm, const char *txt)
232235
sm->m = NULL;
233236
}
234237

238+
static int eap_sm_set_scard_pin(struct eap_sm *sm,
239+
struct eap_peer_config *conf)
240+
{
241+
return -1;
242+
}
243+
244+
static int eap_sm_get_scard_identity(struct eap_sm *sm,
245+
struct eap_peer_config *conf)
246+
{
247+
return -1;
248+
}
249+
250+
251+
/**
252+
* eap_sm_buildIdentity - Build EAP-Identity/Response for the current network
253+
* @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
254+
* @id: EAP identifier for the packet
255+
* @encrypted: Whether the packet is for encrypted tunnel (EAP phase 2)
256+
* Returns: Pointer to the allocated EAP-Identity/Response packet or %NULL on
257+
* failure
258+
*
259+
* This function allocates and builds an EAP-Identity/Response packet for the
260+
* current network. The caller is responsible for freeing the returned data.
261+
*/
262+
struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted)
263+
{
264+
struct eap_peer_config *config = eap_get_config(sm);
265+
struct wpabuf *resp;
266+
const u8 *identity;
267+
size_t identity_len;
268+
269+
if (config == NULL) {
270+
wpa_printf(MSG_ERROR, "EAP: buildIdentity: configuration "
271+
"was not available");
272+
return NULL;
273+
}
274+
275+
if (sm->m && sm->m->get_identity &&
276+
(identity = sm->m->get_identity(sm, sm->eap_method_priv,
277+
&identity_len)) != NULL) {
278+
wpa_hexdump_ascii(MSG_DEBUG, "EAP: using method re-auth "
279+
"identity", identity, identity_len);
280+
} else if (!encrypted && config->anonymous_identity) {
281+
identity = config->anonymous_identity;
282+
identity_len = config->anonymous_identity_len;
283+
wpa_hexdump_ascii(MSG_DEBUG, "EAP: using anonymous identity",
284+
identity, identity_len);
285+
} else {
286+
identity = config->identity;
287+
identity_len = config->identity_len;
288+
wpa_hexdump_ascii(MSG_DEBUG, "EAP: using real identity",
289+
identity, identity_len);
290+
}
291+
292+
if (identity == NULL) {
293+
wpa_printf(MSG_ERROR, "EAP: buildIdentity: identity "
294+
"configuration was not available");
295+
if (config->pcsc) {
296+
if (eap_sm_get_scard_identity(sm, config) < 0)
297+
return NULL;
298+
identity = config->identity;
299+
identity_len = config->identity_len;
300+
wpa_hexdump_ascii(MSG_DEBUG, "permanent identity from "
301+
"IMSI", identity, identity_len);
302+
} else {
303+
eap_sm_request_identity(sm);
304+
return NULL;
305+
}
306+
} else if (config->pcsc) {
307+
if (eap_sm_set_scard_pin(sm, config) < 0)
308+
return NULL;
309+
}
310+
311+
resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_IDENTITY, identity_len,
312+
EAP_CODE_RESPONSE, id);
313+
if (resp == NULL)
314+
return NULL;
315+
316+
wpabuf_put_data(resp, identity, identity_len);
317+
318+
return resp;
319+
}
320+
321+
235322
struct wpabuf * eap_sm_build_identity_resp(struct eap_sm *sm, u8 id, int encrypted)
236323
{
237324
const u8 *identity;
@@ -389,6 +476,10 @@ int eap_peer_config_init(
389476
sm->config.new_password_len);
390477
}
391478

479+
if (g_wpa_ttls_phase2_type) {
480+
sm->config.phase2 = g_wpa_ttls_phase2_type;
481+
}
482+
392483
return 0;
393484

394485
}
@@ -458,6 +549,65 @@ int eap_peer_blob_init(struct eap_sm *sm)
458549
return ret;
459550
}
460551

552+
#if defined(CONFIG_CTRL_IFACE) || !defined(CONFIG_NO_STDOUT_DEBUG)
553+
static void eap_sm_request(struct eap_sm *sm, enum wpa_ctrl_req_type field,
554+
const char *msg, size_t msglen)
555+
{
556+
struct eap_peer_config *config;
557+
558+
if (sm == NULL)
559+
return;
560+
config = eap_get_config(sm);
561+
if (config == NULL)
562+
return;
563+
564+
switch (field) {
565+
case WPA_CTRL_REQ_EAP_IDENTITY:
566+
config->pending_req_identity++;
567+
break;
568+
case WPA_CTRL_REQ_EAP_PASSWORD:
569+
config->pending_req_password++;
570+
break;
571+
case WPA_CTRL_REQ_EAP_NEW_PASSWORD:
572+
config->pending_req_new_password++;
573+
break;
574+
case WPA_CTRL_REQ_EAP_PIN:
575+
config->pending_req_pin++;
576+
break;
577+
case WPA_CTRL_REQ_EAP_PASSPHRASE:
578+
config->pending_req_passphrase++;
579+
break;
580+
default:
581+
return;
582+
}
583+
584+
}
585+
#else /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
586+
#define eap_sm_request(sm, type, msg, msglen) do { } while (0)
587+
#endif /* CONFIG_CTRL_IFACE || !CONFIG_NO_STDOUT_DEBUG */
588+
589+
const char * eap_sm_get_method_name(struct eap_sm *sm)
590+
{
591+
if (sm->m == NULL)
592+
return "UNKNOWN";
593+
return sm->m->name;
594+
}
595+
596+
597+
/**
598+
* eap_sm_request_identity - Request identity from user (ctrl_iface)
599+
* @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
600+
*
601+
* EAP methods can call this function to request identity information for the
602+
* current network. This is normally called when the identity is not included
603+
* in the network configuration. The request will be sent to monitor programs
604+
* through the control interface.
605+
*/
606+
void eap_sm_request_identity(struct eap_sm *sm)
607+
{
608+
eap_sm_request(sm, WPA_CTRL_REQ_EAP_IDENTITY, NULL, 0);
609+
}
610+
461611
void eap_peer_blob_deinit(struct eap_sm *sm)
462612
{
463613
int i;
@@ -495,6 +645,13 @@ struct eap_peer_config * eap_get_config(struct eap_sm *sm)
495645
return &sm->config;
496646
}
497647

648+
649+
/**
650+
* eap_get_config_identity - Get identity from the network configuration
651+
* @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
652+
* @len: Buffer for the length of the identity
653+
* Returns: Pointer to the identity or %NULL if not found
654+
*/
498655
const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
499656
{
500657
struct eap_peer_config *config = eap_get_config(sm);
@@ -504,6 +661,13 @@ const u8 * eap_get_config_identity(struct eap_sm *sm, size_t *len)
504661
return config->identity;
505662
}
506663

664+
665+
/**
666+
* eap_get_config_password - Get password from the network configuration
667+
* @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
668+
* @len: Buffer for the length of the password
669+
* Returns: Pointer to the password or %NULL if not found
670+
*/
507671
const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
508672
{
509673
struct eap_peer_config *config = eap_get_config(sm);
@@ -513,6 +677,16 @@ const u8 * eap_get_config_password(struct eap_sm *sm, size_t *len)
513677
return config->password;
514678
}
515679

680+
681+
/**
682+
* eap_get_config_password2 - Get password from the network configuration
683+
* @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
684+
* @len: Buffer for the length of the password
685+
* @hash: Buffer for returning whether the password is stored as a
686+
* NtPasswordHash instead of plaintext password; can be %NULL if this
687+
* information is not needed
688+
* Returns: Pointer to the password or %NULL if not found
689+
*/
516690
const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
517691
{
518692
struct eap_peer_config *config = eap_get_config(sm);
@@ -525,6 +699,13 @@ const u8 * eap_get_config_password2(struct eap_sm *sm, size_t *len, int *hash)
525699
return config->password;
526700
}
527701

702+
703+
/**
704+
* eap_get_config_new_password - Get new password from network configuration
705+
* @sm: Pointer to EAP state machine allocated with eap_peer_sm_init()
706+
* @len: Buffer for the length of the new password
707+
* Returns: Pointer to the new password or %NULL if not found
708+
*/
528709
const u8 * eap_get_config_new_password(struct eap_sm *sm, size_t *len)
529710
{
530711
struct eap_peer_config *config = eap_get_config(sm);

components/wpa_supplicant/src/eap_peer/eap.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,12 @@ int g_wpa_password_len;
3939
u8 *g_wpa_new_password;
4040
int g_wpa_new_password_len;
4141

42+
char *g_wpa_ttls_phase2_type;
43+
4244
const u8 * eap_get_eapKeyData(struct eap_sm *sm, size_t *len);
4345
void eap_deinit_prev_method(struct eap_sm *sm, const char *txt);
4446
struct wpabuf * eap_sm_build_nak(struct eap_sm *sm, EapType type, u8 id);
47+
struct wpabuf * eap_sm_buildIdentity(struct eap_sm *sm, int id, int encrypted);
4548
int eap_peer_blob_init(struct eap_sm *sm);
4649
void eap_peer_blob_deinit(struct eap_sm *sm);
4750
int eap_peer_config_init(
@@ -50,5 +53,6 @@ int eap_peer_config_init(
5053
void eap_peer_config_deinit(struct eap_sm *sm);
5154
void eap_sm_abort(struct eap_sm *sm);
5255
int eap_peer_register_methods(void);
56+
void eap_sm_request_identity(struct eap_sm *sm);
5357

5458
#endif /* EAP_H */

0 commit comments

Comments
 (0)