wifi_provisioniong: Update wifi provisioning example to demonstrate security version 2

This commit is contained in:
Aditya Patwardhan 2022-06-01 11:27:20 +05:30 committed by BOT
parent 0b88785286
commit 15531e1023
2 changed files with 148 additions and 45 deletions

View File

@ -15,6 +15,41 @@ menu "Example Configuration"
bool "Soft AP"
endchoice
choice EXAMPLE_PROV_SECURITY_VERSION
bool "Protocomm security version"
default EXAMPLE_PROV_SECURITY_VERSION_1
help
Wi-Fi provisioning component offers 3 security versions.
The example offers a choice between security version 1 and 2.
config EXAMPLE_PROV_SECURITY_VERSION_1
bool "Security version 1"
config EXAMPLE_PROV_SECURITY_VERSION_2
bool "Security version 2"
endchoice
choice EXAMPLE_PROV_MODE
bool "Security version 2 mode"
depends on EXAMPLE_PROV_SECURITY_VERSION_2
default EXAMPLE_PROV_SEC2_DEV_MODE
config EXAMPLE_PROV_SEC2_DEV_MODE
bool "Security version 2 development mode"
depends on EXAMPLE_PROV_SECURITY_VERSION_2
help
This enables the development mode for
security version 2.
Please note that this mode is NOT recommended for production purpose.
config EXAMPLE_PROV_SEC2_PROD_MODE
bool "Security version 2 production mode"
depends on EXAMPLE_PROV_SECURITY_VERSION_2
help
This enables the production mode for
security version 2.
endchoice
config EXAMPLE_PROV_TRANSPORT
int
default 1 if EXAMPLE_PROV_TRANSPORT_BLE

View File

@ -32,33 +32,71 @@
static const char *TAG = "app";
static const char sec2_salt[] = {0x75, 0x45, 0x54, 0xac};
#if CONFIG_EXAMPLE_PROV_SECURITY_VERSION_2
#if CONFIG_EXAMPLE_PROV_SEC2_DEV_MODE
#define EXAMPLE_PROV_SEC2_USERNAME "testuser"
#define EXAMPLE_PROV_SEC2_PWD "testpassword"
static const char sec2_verifier[] = {0xf4, 0x3c, 0x43, 0x37, 0x1c, 0xf8, 0x69, 0x39, 0x40, 0x65, 0xd1, 0xdb, 0xb5, 0x2f, 0x5f, 0xd5,
0xce, 0x3b, 0x2d, 0x95, 0xe9, 0x03, 0x4b, 0xfd, 0x84, 0x08, 0x5a,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38, 0x5b, 0x8a, 0x68, 0x16, 0x0c, 0xec, 0x72, 0xb3, 0xfb, 0xa3, 0x8d, 0xfe, 0xbc, 0x5c,
0xc3, 0xc4, 0xbe, 0x88, 0x32, 0x37, 0x86, 0x57, 0x82, 0x6f, 0x7e, 0xf3, 0xdc, 0x83, 0xfe, 0x03,
0xdf, 0xba, 0x07, 0x0b, 0x4e, 0x18, 0x38, 0x6b, 0xcb, 0x1a, 0xda, 0x2b, 0xf0, 0x1c, 0x60, 0x6f,
0xc0, 0x74, 0x34, 0x5f, 0x70, 0x94, 0xab, 0x36, 0x5a, 0x7b, 0x67, 0x1c, 0x26, 0xd6, 0x03, 0x25,
0x37, 0x7a, 0x41, 0xbd, 0x6e, 0x3c, 0x5f, 0x74, 0x74, 0x38, 0x5b, 0x7b, 0x75, 0x5e, 0x2f, 0xe0,
0xb9, 0xd0, 0x99, 0x88, 0x02, 0x6c, 0xe7, 0x3f, 0x8a, 0x62, 0xb3, 0x54, 0x06, 0xe5, 0x84, 0xff,
0xc5, 0x68, 0x20, 0x55, 0x80, 0x9e, 0x7f, 0x0f, 0xd5, 0x3d, 0xe2, 0xd3, 0x57, 0xee, 0x36, 0x7e,
0xe8, 0x78, 0x05, 0x5a, 0xf5, 0xc2, 0x92, 0x6e, 0xac, 0x13, 0x3f, 0x27, 0x11, 0x54, 0x74, 0xd2,
0x56, 0x1d, 0xac, 0x6f, 0x36, 0xbb, 0xaa, 0x9d, 0x4e, 0x7a, 0x61, 0x53, 0x47, 0x79, 0xcb, 0xb8,
0x76, 0x99, 0x0f, 0x80, 0xc2, 0x62, 0x95, 0x5f, 0x7d, 0x85, 0xfe, 0xd8, 0xd9, 0x0d, 0x05, 0xee,
0x78, 0xa1, 0x18, 0xa5, 0x71, 0x84, 0x6a, 0x2c, 0xc7, 0x05, 0xf1, 0x1a, 0x31, 0x31, 0xc3, 0x1d};
/* This salt,verifier has been generated for username = "testuser" and password = "testpassword"
* IMPORTANT NOTE: For production cases, this must be unique to every device
* and should come from device manufacturing partition.*/
static const char sec2_salt[] = {
0x2f, 0x3d, 0x3c, 0xf8, 0x0d, 0xbd, 0x0c, 0xa9, 0x6f, 0x30, 0xb4, 0x4d, 0x89, 0xd5, 0x2f, 0x0e
};
static const char sec2_verifier[] = {
0xf2, 0x9f, 0xc1, 0xf5, 0x28, 0x4a, 0x11, 0x74, 0xb4, 0x24, 0x09, 0x23, 0xd8, 0x27, 0xb7, 0x5a,
0x95, 0x3a, 0x99, 0xed, 0xf4, 0x6e, 0xe9, 0x8c, 0x4f, 0x07, 0xf2, 0xf5, 0x43, 0x3d, 0x7f, 0x9a,
0x11, 0x60, 0x66, 0xaf, 0xcd, 0xa5, 0xf6, 0xfa, 0xcb, 0x06, 0xe9, 0xc5, 0x3f, 0x4d, 0x77, 0x16,
0x4c, 0x68, 0x6d, 0x7f, 0x7c, 0xd7, 0xc7, 0x5a, 0x83, 0xc0, 0xfb, 0x94, 0x2d, 0xa9, 0x60, 0xf0,
0x09, 0x11, 0xa0, 0xe1, 0x95, 0x33, 0xd1, 0x30, 0x7f, 0x82, 0x1b, 0x1b, 0x0f, 0x6d, 0xf1, 0xdc,
0x93, 0x1c, 0x20, 0xa7, 0xc0, 0x8d, 0x48, 0x38, 0xff, 0x46, 0xb9, 0xaf, 0xf7, 0x93, 0x78, 0xae,
0xff, 0xb8, 0x3b, 0xdf, 0x99, 0x7b, 0x64, 0x47, 0x02, 0xba, 0x01, 0x39, 0x0f, 0x5c, 0xd8, 0x4e,
0x6f, 0xc8, 0xd0, 0x82, 0x7f, 0x2d, 0x33, 0x1a, 0x09, 0x65, 0x77, 0x85, 0xbc, 0x8a, 0x84, 0xe0,
0x46, 0x7e, 0x3b, 0x0e, 0x6e, 0x3b, 0xdf, 0x70, 0x17, 0x70, 0x0a, 0xbc, 0x84, 0x67, 0xfa, 0xf9,
0x84, 0x53, 0xda, 0xb4, 0xca, 0x38, 0x71, 0xe4, 0x06, 0xf6, 0x7d, 0xc8, 0x32, 0xbb, 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8e, 0xdf, 0xfc, 0x6e, 0x13, 0xcc, 0x1b, 0x2b, 0x54, 0x1a,
0x6e, 0x3d, 0xe6, 0x1c, 0xec, 0x5d, 0xa1, 0xf1, 0xd4, 0x86, 0x9d, 0xcd, 0xb9, 0xe8, 0x98, 0xf1,
0xe5, 0x16, 0xa5, 0x48, 0xe5, 0xec, 0x12, 0xe8, 0x17, 0xe2, 0x55, 0xb5, 0xb3, 0x7c, 0xce, 0xfd
};
#endif
static esp_err_t example_get_sec2_salt(const char **salt, uint16_t *salt_len) {
#if CONFIG_EXAMPLE_PROV_SEC2_DEV_MODE
ESP_LOGI(TAG, "Development mode: using hard coded salt");
*salt = sec2_salt;
*salt_len = sizeof(sec2_salt);
return ESP_OK;
#elif CONFIG_EXAMPLE_PROV_SEC2_PROD_MODE
ESP_LOGE(TAG, "Not implemented!");
return ESP_FAIL;
#endif
}
static esp_err_t example_get_sec2_verifier(const char **verifier, uint16_t *verifier_len) {
#if CONFIG_EXAMPLE_PROV_SEC2_DEV_MODE
ESP_LOGI(TAG, "Development mode: using hard coded verifier");
*verifier = sec2_verifier;
*verifier_len = sizeof(sec2_verifier);
return ESP_OK;
#elif CONFIG_EXAMPLE_PROV_SEC2_PROD_MODE
/* This code needs to be updated with appropriate implementation to provide verifier */
ESP_LOGE(TAG, "Not implemented!");
return ESP_FAIL;
#endif
}
#endif
/* Signal Wi-Fi events on this event-group */
const int WIFI_CONNECTED_EVENT = BIT0;
@ -168,7 +206,7 @@ esp_err_t custom_prov_data_handler(uint32_t session_id, const uint8_t *inbuf, ss
return ESP_OK;
}
static void wifi_prov_print_qr(const char *name, const char *pop, const char *transport)
static void wifi_prov_print_qr(const char *name, const char *username, const char *pop, const char *transport)
{
if (!name || !transport) {
ESP_LOGW(TAG, "Cannot generate QR code payload. Data missing.");
@ -176,9 +214,15 @@ static void wifi_prov_print_qr(const char *name, const char *pop, const char *tr
}
char payload[150] = {0};
if (pop) {
#if CONFIG_EXAMPLE_PROV_SECURITY_VERSION_1
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"pop\":\"%s\",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, pop, transport);
#elif CONFIG_EXAMPLE_PROV_SECURITY_VERSION_2
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"username\":\"%s\",\"pop\":\"%s\",\"transport\":\"%s\"}",
PROV_QR_VERSION, name, username, pop, transport);
#endif
} else {
snprintf(payload, sizeof(payload), "{\"ver\":\"%s\",\"name\":\"%s\"" \
",\"transport\":\"%s\"}",
@ -276,16 +320,16 @@ void app_main(void)
char service_name[12];
get_device_service_name(service_name, sizeof(service_name));
/* What is the security level that we want (0 or 1):
#ifdef CONFIG_EXAMPLE_PROV_SECURITY_VERSION_1
/* What is the security level that we want (0, 1, 2):
* - WIFI_PROV_SECURITY_0 is simply plain text communication.
* - WIFI_PROV_SECURITY_1 is secure communication which consists of secure handshake
* using X25519 key exchange and proof of possession (pop) and AES-CTR
* for encryption/decryption of messages.
* - WIFI_PROV_SECURITY_2 SRP6a based authentication and key exchange
* + AES-CTR encryption/decryption of messages
* + AES-GCM encryption/decryption of messages
*/
// wifi_prov_security_t security = WIFI_PROV_SECURITY_1;
wifi_prov_security_t security = WIFI_PROV_SECURITY_2;
wifi_prov_security_t security = WIFI_PROV_SECURITY_1;
/* Do we want a proof-of-possession (ignored if Security 0 is selected):
* - this should be a string with length > 0
@ -293,19 +337,44 @@ void app_main(void)
*/
const char *pop = "abcd1234";
/* This is the structure for passing security parameters
* for the protocomm security 1
* for the protocomm security 1.
* This does not need not be static i.e. could be dynamically allocated
*/
//const wifi_prov_security1_params_t sec1_params = {
// .data = (const unsigned char *)pop,
// .len = strlen(pop),
//};
static const wifi_prov_security2_params_t sec2_params = {
.salt = sec2_salt,
.salt_len = sizeof(sec2_salt),
.verifier = sec2_verifier,
.verifier_len = sizeof(sec2_verifier),
wifi_prov_security1_params_t sec1_params = {
.data = (const uint8_t *)pop,
.len = strlen(pop),
};
wifi_prov_security1_params_t *sec_params = &sec1_params;
const char *username = NULL;
#elif CONFIG_EXAMPLE_PROV_SECURITY_VERSION_2
wifi_prov_security_t security = WIFI_PROV_SECURITY_2;
/* The username must be the same one, which has been used in the generation of salt and verifier */
#if CONFIG_EXAMPLE_PROV_SEC2_DEV_MODE
/* This pop field represents the password that will be used to generate salt and verifier.
* The field is present here in order to generate the QR code containing password.
* In production this password field shall not be stored on the device */
const char *username = EXAMPLE_PROV_SEC2_USERNAME;
const char *pop = EXAMPLE_PROV_SEC2_PWD;
#elif CONFIG_EXAMPLE_PROV_SEC2_PROD_MODE
/* The username and password shall not be embedded in the firmware,
* they should be provided to the user by other means.
* e.g. QR code sticker */
const char *username = NULL;
const char *pop = NULL;
#endif
/* This is the structure for passing security parameters
* for the protocomm security 2.
* This does not need not be static i.e. could be dynamically allocated
*/
wifi_prov_security2_params_t sec2_params = {};
ESP_ERROR_CHECK(example_get_sec2_salt(&sec2_params.salt, &sec2_params.salt_len));
ESP_ERROR_CHECK(example_get_sec2_verifier(&sec2_params.verifier, &sec2_params.verifier_len));
wifi_prov_security2_params_t *sec_params = &sec2_params;
#endif
/* What is the service key (could be NULL)
* This translates to :
* - Wi-Fi password when scheme is wifi_prov_scheme_softap
@ -344,9 +413,8 @@ void app_main(void)
*/
wifi_prov_mgr_endpoint_create("custom-data");
/* Start provisioning service */
// ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, pop, service_name, service_key));
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, (void *) &sec2_params, service_name, service_key));
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, (const void *) sec_params, service_name, service_key));
/* The handler for the optional endpoint created above.
* This call must be made after starting the provisioning, and only if the endpoint
@ -361,9 +429,9 @@ void app_main(void)
// wifi_prov_mgr_deinit();
/* Print QR code for provisioning */
#ifdef CONFIG_EXAMPLE_PROV_TRANSPORT_BLE
wifi_prov_print_qr(service_name, pop, PROV_TRANSPORT_BLE);
wifi_prov_print_qr(service_name, username, pop, PROV_TRANSPORT_BLE);
#else /* CONFIG_EXAMPLE_PROV_TRANSPORT_SOFTAP */
wifi_prov_print_qr(service_name, pop, PROV_TRANSPORT_SOFTAP);
wifi_prov_print_qr(service_name, username, pop, PROV_TRANSPORT_SOFTAP);
#endif /* CONFIG_EXAMPLE_PROV_TRANSPORT_BLE */
} else {
ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA");