#include "utils/common.h"
#include "utils/wpa_debug.h"
-static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len, int uppercase)
+static inline int
+_wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len,
+ int uppercase, int whitespace)
{
size_t i;
char *pos = buf, *end = buf + buf_size;
int ret;
+ static const char *fmt_upper = "%02X";
+ static const char *fmt_lower = "%02x";
+ static const char *fmt_upper_ws = "%02X ";
+ static const char *fmt_lower_ws = "%02x ";
+ const char *fmt = uppercase ? (whitespace ? fmt_upper_ws : fmt_upper) :
+ (whitespace ? fmt_lower_ws : fmt_lower);
+
if (buf_size == 0)
return 0;
for (i = 0; i < len; i++) {
- ret = snprintf(pos, end - pos, uppercase? "%02X":"%02x", data[i]);
+ ret = snprintf(pos, end - pos, fmt, data[i]);
if (ret < 0 || ret >= end - pos) {
end[-1] = '\0';
return pos - buf;
int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data, size_t len)
{
- return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
+ return _wpa_snprintf_hex(buf, buf_size, data, len, 1, 0);
}
int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
{
- return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
+ return _wpa_snprintf_hex(buf, buf_size, data, len, 0, 0);
}
#ifdef DEBUG_PRINT
void wpa_dump_mem(char* desc, uint8_t *addr, uint16_t len)
{
+ char output[50];
wpa_printf(MSG_DEBUG, "%s\n", desc);
if (addr){
uint16_t i=0;
- for (i=0; i<len; i++){
- if (i%16==0) wpa_printf(MSG_DEBUG, "\n");
- wpa_printf(MSG_DEBUG, "%02x ", addr[i]);
+ for (i = 0; i < len / 16; i++) {
+ _wpa_snprintf_hex(output, 50, addr + i * 16, 16, 0, 1);
+ wpa_printf(MSG_DEBUG, "%s", output);
+ }
+ if (len % 16) {
+ int bytes_printed = (len / 16) * 16;
+ _wpa_snprintf_hex(output, 50, addr + bytes_printed,
+ len - bytes_printed, 0, 1);
+ wpa_printf(MSG_DEBUG, "%s", output);
}
- wpa_printf(MSG_DEBUG, "\n");
}
}
{
#ifdef DEBUG_PRINT
size_t i;
+ char output[50];
if (level < MSG_MSGDUMP)
return;
- wpa_printf(MSG_DEBUG, "%s - hexdump(len=%lu):\n", title, (unsigned long) len);
+ wpa_printf(MSG_DEBUG, "%s - hexdump(len=%lu):", title, (unsigned long) len);
if (buf == NULL) {
- wpa_printf(MSG_DEBUG, " [NULL]\n");
+ wpa_printf(MSG_DEBUG, " [NULL]");
} else {
- for (i = 0; i < len; i++) {
- wpa_printf(MSG_DEBUG, " %02x", buf[i]);
- if((i+1) % 16 == 0)
- wpa_printf(MSG_DEBUG, "\n");
- }
+ for (i = 0; i < len / 16; i++) {
+ _wpa_snprintf_hex(output, 50, buf + i * 16, 16, 0, 1);
+ wpa_printf(MSG_DEBUG, "%s", output);
+ }
+ if (len % 16) {
+ int bytes_printed = (len / 16) * 16;
+ _wpa_snprintf_hex(output, 50, buf + bytes_printed,
+ len - bytes_printed, 0, 1);
+ wpa_printf(MSG_DEBUG, "%s", output);
+ }
}
- wpa_printf(MSG_DEBUG, "\n");
#endif
}
help
SSID (network name) for the example to connect to.
- config EXAMPLE_EAP_METHOD
- int "EAP METHOD"
- default 1
+ config EXAMPLE_VALIDATE_SERVER_CERT
+ bool "Validate server"
+ default y
help
- EAP method (TLS, PEAP or TTLS) for the example to use.
- TLS: 0, PEAP: 1, TTLS: 2
+ Validate the servers' certificate using CA cert.
+
+ choice
+ prompt "EAP method for the example to use"
+ default EXAMPLE_EAP_METHOD_PEAP
+ config EXAMPLE_EAP_METHOD_TLS
+ bool "TLS"
+ config EXAMPLE_EAP_METHOD_PEAP
+ bool "PEAP"
+ config EXAMPLE_EAP_METHOD_TTLS
+ bool "TTLS"
+ endchoice
+
+ config EXAMPLE_EAP_METHOD
+ int
+ default 0 if EXAMPLE_EAP_METHOD_TLS
+ default 1 if EXAMPLE_EAP_METHOD_PEAP
+ default 2 if EXAMPLE_EAP_METHOD_TTLS
config EXAMPLE_EAP_ID
string "EAP ID"
config EXAMPLE_EAP_USERNAME
string "EAP USERNAME"
+ depends on EXAMPLE_EAP_METHOD_PEAP || EXAMPLE_EAP_METHOD_TTLS
default "espressif"
help
Username for EAP method (PEAP and TTLS).
config EXAMPLE_EAP_PASSWORD
string "EAP PASSWORD"
+ depends on EXAMPLE_EAP_METHOD_PEAP || EXAMPLE_EAP_METHOD_TTLS
default "test11"
help
Password for EAP method (PEAP and TTLS).
to the AP with an IP? */
const int CONNECTED_BIT = BIT0;
-/* Constants that aren't configurable in menuconfig */
-#define EAP_PEAP 1
-#define EAP_TTLS 2
-
static const char *TAG = "example";
/* CA cert, taken from wpa2_ca.pem
To embed it in the app binary, the PEM, CRT and KEY file is named
in the component.mk COMPONENT_EMBED_TXTFILES variable.
*/
+#ifdef CONFIG_EXAMPLE_VALIDATE_SERVER_CERT
extern uint8_t ca_pem_start[] asm("_binary_wpa2_ca_pem_start");
extern uint8_t ca_pem_end[] asm("_binary_wpa2_ca_pem_end");
+#endif /* CONFIG_EXAMPLE_VALIDATE_SERVER_CERT */
+
+#ifdef CONFIG_EXAMPLE_EAP_METHOD_TLS
extern uint8_t client_crt_start[] asm("_binary_wpa2_client_crt_start");
extern uint8_t client_crt_end[] asm("_binary_wpa2_client_crt_end");
extern uint8_t client_key_start[] asm("_binary_wpa2_client_key_start");
extern uint8_t client_key_end[] asm("_binary_wpa2_client_key_end");
+#endif /* CONFIG_EXAMPLE_EAP_METHOD_TLS */
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
static void initialise_wifi(void)
{
+#ifdef CONFIG_EXAMPLE_VALIDATE_SERVER_CERT
unsigned int ca_pem_bytes = ca_pem_end - ca_pem_start;
+#endif /* CONFIG_EXAMPLE_VALIDATE_SERVER_CERT */
+
+#ifdef CONFIG_EXAMPLE_EAP_METHOD_TLS
unsigned int client_crt_bytes = client_crt_end - client_crt_start;
unsigned int client_key_bytes = client_key_end - client_key_start;
+#endif /* CONFIG_EXAMPLE_EAP_METHOD_TLS */
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
+ ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EXAMPLE_EAP_ID, strlen(EXAMPLE_EAP_ID)) );
+
+#ifdef CONFIG_EXAMPLE_VALIDATE_SERVER_CERT
ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_ca_cert(ca_pem_start, ca_pem_bytes) );
+#endif /* CONFIG_EXAMPLE_VALIDATE_SERVER_CERT */
+
+#ifdef CONFIG_EXAMPLE_EAP_METHOD_TLS
ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_cert_key(client_crt_start, client_crt_bytes,\
client_key_start, client_key_bytes, NULL, 0) );
- ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_identity((uint8_t *)EXAMPLE_EAP_ID, strlen(EXAMPLE_EAP_ID)) );
- if (EXAMPLE_EAP_METHOD == EAP_PEAP || EXAMPLE_EAP_METHOD == EAP_TTLS) {
- ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EXAMPLE_EAP_USERNAME, strlen(EXAMPLE_EAP_USERNAME)) );
- ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EXAMPLE_EAP_PASSWORD, strlen(EXAMPLE_EAP_PASSWORD)) );
- }
+#endif /* CONFIG_EXAMPLE_EAP_METHOD_TLS */
+
+#if defined CONFIG_EXAMPLE_EAP_METHOD_PEAP || CONFIG_EXAMPLE_EAP_METHOD_TTLS
+ ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_username((uint8_t *)EXAMPLE_EAP_USERNAME, strlen(EXAMPLE_EAP_USERNAME)) );
+ ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_set_password((uint8_t *)EXAMPLE_EAP_PASSWORD, strlen(EXAMPLE_EAP_PASSWORD)) );
+#endif /* CONFIG_EXAMPLE_EAP_METHOD_PEAP || CONFIG_EXAMPLE_EAP_METHOD_TTLS */
ESP_ERROR_CHECK( esp_wifi_sta_wpa2_ent_enable() );
ESP_ERROR_CHECK( esp_wifi_start() );