Skip to content

Expose WPA2 enterprise APIs #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 101 additions & 1 deletion main/CommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,106 @@ int setAnalogWrite(const uint8_t command[], uint8_t response[])
}


#include "esp_wpa2.h"

int wpa2EntSetIdentity(const uint8_t command[], uint8_t response[]) {
char identity[32 + 1];

memset(identity, 0x00, sizeof(identity));
memcpy(identity, &command[4], command[3]);

esp_wifi_sta_wpa2_ent_set_identity((const unsigned char*)identity, strlen(identity));

response[2] = 1; // number of parameters
response[3] = 1; // parameter 1 length
response[4] = 1;

return 6;
}

int wpa2EntSetUsername(const uint8_t command[], uint8_t response[]) {
char username[32 + 1];

memset(username, 0x00, sizeof(username));
memcpy(username, &command[4], command[3]);

esp_wifi_sta_wpa2_ent_set_username((const unsigned char*)username, strlen(username));

response[2] = 1; // number of parameters
response[3] = 1; // parameter 1 length
response[4] = 1;

return 6;
}

int wpa2EntSetPassword(const uint8_t command[], uint8_t response[]) {
char password[32 + 1];

memset(password, 0x00, sizeof(password));
memcpy(password, &command[4], command[3]);

esp_wifi_sta_wpa2_ent_set_password((const unsigned char*)password, strlen(password));

response[2] = 1; // number of parameters
response[3] = 1; // parameter 1 length
response[4] = 1;

return 6;
}

int wpa2EntSetCACert(const uint8_t command[], uint8_t response[]) {

// Add +1 for \0 termination
size_t ca_cert_buf_size = (command[3] << 8 | command[4]);
uint8_t* ca_cert_buf = (uint8_t*)malloc(ca_cert_buf_size + 1);

memset(ca_cert_buf, 0x00, ca_cert_buf_size + 1);
memcpy(ca_cert_buf, &command[6], ca_cert_buf_size);

esp_wifi_sta_wpa2_ent_set_ca_cert(ca_cert_buf, ca_cert_buf_size + 1);

response[2] = 1; // number of parameters
response[3] = 1; // parameter 1 length
response[4] = 1;

return 6;
}

int wpa2EntSetCertKey(const uint8_t command[], uint8_t response[]) {

// Add +1 for \0 termination
size_t client_crt_buf_size = (command[3] << 8 | command[4]);
size_t client_key_buf_size = (command[5] << 8 | command[6]);
uint8_t* client_crt_buf = (uint8_t*)malloc(client_crt_buf_size + 1);
uint8_t* client_key_buf = (uint8_t*)malloc(client_key_buf_size + 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this RAM be free'ed somewhere?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our use case is somehow particular since the certificates are usually in flash; after a successful connection is established they should not be useful but I'm not 100% sure.
Since our library resets the module upon attempting the first communication with it I think we can avoid freeing them probably.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'm still wondering if we should use realloc instead of malloc here, in case the sketch calls the lib side API twice in a row for whatever reason.


memset(client_crt_buf, 0x00, client_crt_buf_size + 1);
memset(client_key_buf, 0x00, client_key_buf_size + 1);

memcpy(client_crt_buf, &command[8], client_crt_buf_size);
memcpy(client_key_buf, &command[9 + client_crt_buf_size], client_key_buf_size);

esp_wifi_sta_wpa2_ent_set_cert_key(client_crt_buf, client_crt_buf_size + 1, client_key_buf, client_key_buf_size + 1, NULL, 0);

response[2] = 1; // number of parameters
response[3] = 1; // parameter 1 length
response[4] = 1;

return 6;
}

int wpa2EntEnable(const uint8_t command[], uint8_t response[]) {

esp_wpa2_config_t config = WPA2_CONFIG_INIT_DEFAULT();
esp_wifi_sta_wpa2_ent_enable(&config);

response[2] = 1; // number of parameters
response[3] = 1; // parameter 1 length
response[4] = 1;

return 6;
}

typedef int (*CommandHandlerType)(const uint8_t command[], uint8_t response[]);

const CommandHandlerType commandHandlers[] = {
Expand All @@ -1006,7 +1106,7 @@ const CommandHandlerType commandHandlers[] = {
disconnect, NULL, getIdxRSSI, getIdxEnct, reqHostByName, getHostByName, startScanNetworks, getFwVersion, NULL, sendUDPdata, getRemoteData, getTime, getIdxBSSID, getIdxChannel, ping, getSocket,

// 0x40 -> 0x4f
NULL, NULL, NULL, NULL, sendDataTcp, getDataBufTcp, insertDataBuf, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, sendDataTcp, getDataBufTcp, insertDataBuf, NULL, NULL, NULL, wpa2EntSetIdentity, wpa2EntSetUsername, wpa2EntSetPassword, wpa2EntSetCACert, wpa2EntSetCertKey, wpa2EntEnable,

// 0x50 -> 0x5f
setPinMode, setDigitalWrite, setAnalogWrite,
Expand Down