Skip to content

Commit bd2815d

Browse files
committed
wpa_supplicant: Support for mbedtls tls handshake
Add support for mbedtls based tls handshake, this removes dependency from internal implementation of EAP client.
1 parent 7c5a561 commit bd2815d

21 files changed

+1408
-350
lines changed

components/wpa_supplicant/CMakeLists.txt

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ set(srcs "port/os_xtensa.c"
1818
"src/crypto/aes-unwrap.c"
1919
"src/crypto/aes-wrap.c"
2020
"src/crypto/aes-omac1.c"
21-
"src/crypto/sha256-tlsprf.c"
2221
"src/crypto/bignum.c"
2322
"src/crypto/ccmp.c"
23+
"src/crypto/crypto_mbedtls.c"
2424
"src/crypto/crypto_mbedtls-bignum.c"
2525
"src/crypto/crypto_mbedtls-ec.c"
2626
"src/crypto/crypto_ops.c"
@@ -42,6 +42,10 @@ set(srcs "port/os_xtensa.c"
4242
"src/crypto/sha1.c"
4343
"src/crypto/sha256-internal.c"
4444
"src/crypto/sha256.c"
45+
"src/crypto/sha1-tlsprf.c"
46+
"src/crypto/sha256-tlsprf.c"
47+
"src/crypto/sha384-tlsprf.c"
48+
"src/crypto/sha256-prf.c"
4549
"src/eap_peer/chap.c"
4650
"src/eap_peer/eap.c"
4751
"src/eap_peer/eap_common.c"
@@ -61,6 +65,27 @@ set(srcs "port/os_xtensa.c"
6165
"src/rsn_supp/pmksa_cache.c"
6266
"src/rsn_supp/wpa.c"
6367
"src/rsn_supp/wpa_ie.c"
68+
"src/utils/base64.c"
69+
"src/utils/common.c"
70+
"src/utils/ext_password.c"
71+
"src/utils/uuid.c"
72+
"src/utils/wpabuf.c"
73+
"src/utils/wpa_debug.c"
74+
"src/utils/json.c"
75+
"src/wps/wps.c"
76+
"src/wps/wps_attr_build.c"
77+
"src/wps/wps_attr_parse.c"
78+
"src/wps/wps_attr_process.c"
79+
"src/wps/wps_common.c"
80+
"src/wps/wps_dev_attr.c"
81+
"src/wps/wps_enrollee.c"
82+
"src/wps/wps_registrar.c"
83+
"src/wps/wps_validate.c")
84+
85+
if(CONFIG_WPA_MBEDTLS_CRYPTO)
86+
set(tls_src "src/crypto/tls_mbedtls.c")
87+
else()
88+
set(tls_src
6489
"src/tls/asn1.c"
6590
"src/tls/bignum.c"
6691
"src/tls/pkcs1.c"
@@ -78,24 +103,10 @@ set(srcs "port/os_xtensa.c"
78103
"src/tls/tlsv1_server_read.c"
79104
"src/tls/tlsv1_server_write.c"
80105
"src/tls/x509v3.c"
81-
"src/utils/base64.c"
82-
"src/utils/common.c"
83-
"src/utils/ext_password.c"
84-
"src/utils/uuid.c"
85-
"src/utils/wpabuf.c"
86-
"src/utils/wpa_debug.c"
87-
"src/utils/json.c"
88-
"src/wps/wps.c"
89-
"src/wps/wps_attr_build.c"
90-
"src/wps/wps_attr_parse.c"
91-
"src/wps/wps_attr_process.c"
92-
"src/wps/wps_common.c"
93-
"src/wps/wps_dev_attr.c"
94-
"src/wps/wps_enrollee.c"
95-
"src/wps/wps_registrar.c"
96-
"src/wps/wps_validate.c")
106+
)
107+
endif()
97108

98-
idf_component_register(SRCS "${srcs}"
109+
idf_component_register(SRCS "${srcs}" "${tls_src}"
99110
INCLUDE_DIRS include port/include include/esp_supplicant
100111
PRIV_INCLUDE_DIRS src
101112
PRIV_REQUIRES mbedtls esp_timer)

components/wpa_supplicant/Kconfig

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,6 @@ menu "Supplicant"
2323
help
2424
Select this to enable unity test for DPP.
2525

26-
config WPA_TLS_V12
27-
bool "Enable TLS v1.2"
28-
default n
29-
help
30-
Select this to enable TLS v1.2 for WPA2-Enterprise Authentication.
31-
3226
config WPA_WPS_WARS
3327
bool "Add WPS Inter operatability Fixes"
3428
default n
Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,29 @@
1-
COMPONENT_ADD_INCLUDEDIRS := include port/include include/esp_supplicant
1+
# supplicant make file
2+
23
COMPONENT_PRIV_INCLUDEDIRS := src
34
COMPONENT_SRCDIRS := port src/ap src/common src/crypto src/eap_peer src/rsn_supp src/tls src/utils src/esp_supplicant src/wps
5+
COMPONENT_ADD_INCLUDEDIRS := include port/include include/esp_supplicant
6+
7+
ifeq ($(CONFIG_WPA_MBEDTLS_CRYPTO), y)
8+
COMPONENT_OBJEXCLUDE := src/tls/asn1.o \
9+
src/tls/bignum.o \
10+
src/tls/pkcs1.o \
11+
src/tls/pkcs5.o \
12+
src/tls/pkcs8.o \
13+
src/tls/rsa.o \
14+
src/tls/tls_internal.o \
15+
src/tls/tlsv1_client.o \
16+
src/tls/tlsv1_client_read.o \
17+
src/tls/tlsv1_client_write.o \
18+
src/tls/tlsv1_common.o \
19+
src/tls/tlsv1_cred.o \
20+
src/tls/tlsv1_record.o \
21+
src/tls/tlsv1_server.o \
22+
src/tls/tlsv1_server_read.o \
23+
src/tls/tlsv1_server_write.o \
24+
src/tls/x509v3.o
25+
else
26+
COMPONENT_OBJEXCLUDE := src/crypto/tls_mbedtls.o
27+
endif
428

529
CFLAGS += -DCONFIG_DPP -DCONFIG_WPA3_SAE -DCONFIG_IEEE80211W -DESP_SUPPLICANT -DIEEE8021X_EAPOL -DEAP_PEER_METHOD -DEAP_TLS -DEAP_TTLS -DEAP_PEAP -DEAP_MSCHAPv2 -DUSE_WPA2_TASK -DCONFIG_WPS2 -DCONFIG_WPS_PIN -DUSE_WPS_TASK -DESPRESSIF_USE -DESP32_WORKAROUND -DCONFIG_ECC -D__ets__ -Wno-strict-aliasing

components/wpa_supplicant/port/include/os.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ char * ets_strdup(const char *s);
278278
#ifndef os_strstr
279279
#define os_strstr(h, n) strstr((h), (n))
280280
#endif
281+
#ifndef os_strlcpy
282+
#define os_strlcpy(d, s, n) strlcpy((d), (s), (n))
283+
#endif
281284

282285
#ifndef os_snprintf
283286
#ifdef _MSC_VER
@@ -291,16 +294,4 @@ static inline int os_snprintf_error(size_t size, int res)
291294
{
292295
return res < 0 || (unsigned int) res >= size;
293296
}
294-
295-
/**
296-
* os_strlcpy - Copy a string with size bound and NUL-termination
297-
* @dest: Destination
298-
* @src: Source
299-
* @siz: Size of the target buffer
300-
* Returns: Total length of the target string (length of src) (not including
301-
* NUL-termination)
302-
*
303-
* This function matches in behavior with the strlcpy(3) function in OpenBSD.
304-
*/
305-
size_t os_strlcpy(char *dest, const char *src, size_t siz);
306297
#endif /* OS_H */

components/wpa_supplicant/port/include/supplicant_opt.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919

2020
#if CONFIG_WPA_MBEDTLS_CRYPTO
2121
#define USE_MBEDTLS_CRYPTO 1
22+
#else
23+
#define CONFIG_TLS_INTERNAL_CLIENT
24+
#define CONFIG_TLSV12
2225
#endif
2326

2427
#if CONFIG_WPA_DEBUG_PRINT
2528
#define DEBUG_PRINT
2629
#endif
2730

28-
#if CONFIG_WPA_TLS_V12
29-
#define CONFIG_TLSV12
30-
#endif
31-
3231
#endif /* _SUPPLICANT_OPT_H */
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Copyright 2020 Espressif Systems (Shanghai) PTE LTD
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifdef ESP_PLATFORM
18+
#include "esp_system.h"
19+
#endif
20+
21+
#include "utils/includes.h"
22+
#include "utils/common.h"
23+
#include "crypto.h"
24+
#include "random.h"
25+
#include "sha256.h"
26+
27+
#include "mbedtls/ecp.h"
28+
#include "mbedtls/entropy.h"
29+
#include "mbedtls/ctr_drbg.h"
30+
#include "mbedtls/md.h"
31+
32+
int mbedtls_hmac_vector(mbedtls_md_type_t md_type, const u8 *key, size_t key_len,
33+
size_t num_elem, const u8 *addr[], const size_t *len, u8 *mac)
34+
{
35+
size_t i;
36+
const mbedtls_md_info_t *md_info;
37+
mbedtls_md_context_t md_ctx;
38+
int ret;
39+
40+
mbedtls_md_init(&md_ctx);
41+
42+
if((md_info = mbedtls_md_info_from_type(md_type)) == NULL )
43+
return -1;
44+
45+
if ((ret = mbedtls_md_setup( &md_ctx, md_info, 1)) != 0)
46+
return(ret);
47+
48+
mbedtls_md_hmac_starts(&md_ctx, key, key_len);
49+
50+
for( i = 0; i < num_elem; i++)
51+
mbedtls_md_hmac_update(&md_ctx, addr[i], len[i]);
52+
53+
mbedtls_md_hmac_finish(&md_ctx, mac);
54+
55+
mbedtls_md_free(&md_ctx);
56+
57+
return 0;
58+
}
59+
60+
int hmac_sha384_vector(const u8 *key, size_t key_len, size_t num_elem,
61+
const u8 *addr[], const size_t *len, u8 *mac)
62+
{
63+
return mbedtls_hmac_vector(MBEDTLS_MD_SHA384, key, key_len, num_elem, addr,
64+
len, mac);
65+
}
66+
67+
68+
int hmac_sha384(const u8 *key, size_t key_len, const u8 *data,
69+
size_t data_len, u8 *mac)
70+
{
71+
return hmac_sha384_vector(key, key_len, 1, &data, &data_len, mac);
72+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*
2+
* TLS PRF (SHA1 + MD5)
3+
* Copyright (c) 2003-2005, 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+
#include "utils/includes.h"
10+
11+
#include "utils/common.h"
12+
#include "sha1.h"
13+
#include "md5.h"
14+
15+
16+
/**
17+
* tls_prf_sha1_md5 - Pseudo-Random Function for TLS (TLS-PRF, RFC 2246)
18+
* @secret: Key for PRF
19+
* @secret_len: Length of the key in bytes
20+
* @label: A unique label for each purpose of the PRF
21+
* @seed: Seed value to bind into the key
22+
* @seed_len: Length of the seed
23+
* @out: Buffer for the generated pseudo-random key
24+
* @outlen: Number of bytes of key to generate
25+
* Returns: 0 on success, -1 on failure.
26+
*
27+
* This function is used to derive new, cryptographically separate keys from a
28+
* given key in TLS. This PRF is defined in RFC 2246, Chapter 5.
29+
*/
30+
int tls_prf_sha1_md5(const u8 *secret, size_t secret_len, const char *label,
31+
const u8 *seed, size_t seed_len, u8 *out, size_t outlen)
32+
{
33+
size_t L_S1, L_S2, i;
34+
const u8 *S1, *S2;
35+
u8 A_MD5[MD5_MAC_LEN], A_SHA1[SHA1_MAC_LEN];
36+
u8 P_MD5[MD5_MAC_LEN], P_SHA1[SHA1_MAC_LEN];
37+
int MD5_pos, SHA1_pos;
38+
const u8 *MD5_addr[3];
39+
size_t MD5_len[3];
40+
const unsigned char *SHA1_addr[3];
41+
size_t SHA1_len[3];
42+
43+
MD5_addr[0] = A_MD5;
44+
MD5_len[0] = MD5_MAC_LEN;
45+
MD5_addr[1] = (unsigned char *) label;
46+
MD5_len[1] = os_strlen(label);
47+
MD5_addr[2] = seed;
48+
MD5_len[2] = seed_len;
49+
50+
SHA1_addr[0] = A_SHA1;
51+
SHA1_len[0] = SHA1_MAC_LEN;
52+
SHA1_addr[1] = (unsigned char *) label;
53+
SHA1_len[1] = os_strlen(label);
54+
SHA1_addr[2] = seed;
55+
SHA1_len[2] = seed_len;
56+
57+
/* RFC 2246, Chapter 5
58+
* A(0) = seed, A(i) = HMAC(secret, A(i-1))
59+
* P_hash = HMAC(secret, A(1) + seed) + HMAC(secret, A(2) + seed) + ..
60+
* PRF = P_MD5(S1, label + seed) XOR P_SHA-1(S2, label + seed)
61+
*/
62+
63+
L_S1 = L_S2 = (secret_len + 1) / 2;
64+
S1 = secret;
65+
S2 = secret + L_S1;
66+
if (secret_len & 1) {
67+
/* The last byte of S1 will be shared with S2 */
68+
S2--;
69+
}
70+
71+
hmac_md5_vector(S1, L_S1, 2, &MD5_addr[1], &MD5_len[1], A_MD5);
72+
hmac_sha1_vector(S2, L_S2, 2, &SHA1_addr[1], &SHA1_len[1], A_SHA1);
73+
74+
MD5_pos = MD5_MAC_LEN;
75+
SHA1_pos = SHA1_MAC_LEN;
76+
for (i = 0; i < outlen; i++) {
77+
if (MD5_pos == MD5_MAC_LEN) {
78+
hmac_md5_vector(S1, L_S1, 3, MD5_addr, MD5_len, P_MD5);
79+
MD5_pos = 0;
80+
hmac_md5(S1, L_S1, A_MD5, MD5_MAC_LEN, A_MD5);
81+
}
82+
if (SHA1_pos == SHA1_MAC_LEN) {
83+
hmac_sha1_vector(S2, L_S2, 3, SHA1_addr, SHA1_len,
84+
P_SHA1);
85+
SHA1_pos = 0;
86+
hmac_sha1(S2, L_S2, A_SHA1, SHA1_MAC_LEN, A_SHA1);
87+
}
88+
89+
out[i] = P_MD5[MD5_pos] ^ P_SHA1[SHA1_pos];
90+
91+
MD5_pos++;
92+
SHA1_pos++;
93+
}
94+
95+
os_memset(A_MD5, 0, MD5_MAC_LEN);
96+
os_memset(P_MD5, 0, MD5_MAC_LEN);
97+
os_memset(A_SHA1, 0, SHA1_MAC_LEN);
98+
os_memset(P_SHA1, 0, SHA1_MAC_LEN);
99+
100+
return 0;
101+
}

0 commit comments

Comments
 (0)