provisioning.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Unlicense OR CC0-1.0
  5. */
  6. #include <wifi_provisioning/manager.h>
  7. #include "esp_log.h"
  8. #include "esp_mac.h"
  9. #include "esp_event.h"
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/event_groups.h"
  12. #include "dns_server.h"
  13. static const char *TAG = "NCM_provisioning";
  14. #if CONFIG_EXAMPLE_PROV_SECURITY_VERSION_2
  15. #if CONFIG_EXAMPLE_PROV_SEC2_DEV_MODE
  16. #define EXAMPLE_PROV_SEC2_USERNAME "wifiprov"
  17. #define EXAMPLE_PROV_SEC2_PWD "abcd1234"
  18. /* This salt,verifier has been generated for username = "wifiprov" and password = "abcd1234"
  19. * IMPORTANT NOTE: For production cases, this must be unique to every device
  20. * and should come from device manufacturing partition.*/
  21. static const char sec2_salt[] = {
  22. 0x03, 0x6e, 0xe0, 0xc7, 0xbc, 0xb9, 0xed, 0xa8, 0x4c, 0x9e, 0xac, 0x97, 0xd9, 0x3d, 0xec, 0xf4
  23. };
  24. static const char sec2_verifier[] = {
  25. 0x7c, 0x7c, 0x85, 0x47, 0x65, 0x08, 0x94, 0x6d, 0xd6, 0x36, 0xaf, 0x37, 0xd7, 0xe8, 0x91, 0x43,
  26. 0x78, 0xcf, 0xfd, 0x61, 0x6c, 0x59, 0xd2, 0xf8, 0x39, 0x08, 0x12, 0x72, 0x38, 0xde, 0x9e, 0x24,
  27. 0xa4, 0x70, 0x26, 0x1c, 0xdf, 0xa9, 0x03, 0xc2, 0xb2, 0x70, 0xe7, 0xb1, 0x32, 0x24, 0xda, 0x11,
  28. 0x1d, 0x97, 0x18, 0xdc, 0x60, 0x72, 0x08, 0xcc, 0x9a, 0xc9, 0x0c, 0x48, 0x27, 0xe2, 0xae, 0x89,
  29. 0xaa, 0x16, 0x25, 0xb8, 0x04, 0xd2, 0x1a, 0x9b, 0x3a, 0x8f, 0x37, 0xf6, 0xe4, 0x3a, 0x71, 0x2e,
  30. 0xe1, 0x27, 0x86, 0x6e, 0xad, 0xce, 0x28, 0xff, 0x54, 0x46, 0x60, 0x1f, 0xb9, 0x96, 0x87, 0xdc,
  31. 0x57, 0x40, 0xa7, 0xd4, 0x6c, 0xc9, 0x77, 0x54, 0xdc, 0x16, 0x82, 0xf0, 0xed, 0x35, 0x6a, 0xc4,
  32. 0x70, 0xad, 0x3d, 0x90, 0xb5, 0x81, 0x94, 0x70, 0xd7, 0xbc, 0x65, 0xb2, 0xd5, 0x18, 0xe0, 0x2e,
  33. 0xc3, 0xa5, 0xf9, 0x68, 0xdd, 0x64, 0x7b, 0xb8, 0xb7, 0x3c, 0x9c, 0xfc, 0x00, 0xd8, 0x71, 0x7e,
  34. 0xb7, 0x9a, 0x7c, 0xb1, 0xb7, 0xc2, 0xc3, 0x18, 0x34, 0x29, 0x32, 0x43, 0x3e, 0x00, 0x99, 0xe9,
  35. 0x82, 0x94, 0xe3, 0xd8, 0x2a, 0xb0, 0x96, 0x29, 0xb7, 0xdf, 0x0e, 0x5f, 0x08, 0x33, 0x40, 0x76,
  36. 0x52, 0x91, 0x32, 0x00, 0x9f, 0x97, 0x2c, 0x89, 0x6c, 0x39, 0x1e, 0xc8, 0x28, 0x05, 0x44, 0x17,
  37. 0x3f, 0x68, 0x02, 0x8a, 0x9f, 0x44, 0x61, 0xd1, 0xf5, 0xa1, 0x7e, 0x5a, 0x70, 0xd2, 0xc7, 0x23,
  38. 0x81, 0xcb, 0x38, 0x68, 0xe4, 0x2c, 0x20, 0xbc, 0x40, 0x57, 0x76, 0x17, 0xbd, 0x08, 0xb8, 0x96,
  39. 0xbc, 0x26, 0xeb, 0x32, 0x46, 0x69, 0x35, 0x05, 0x8c, 0x15, 0x70, 0xd9, 0x1b, 0xe9, 0xbe, 0xcc,
  40. 0xa9, 0x38, 0xa6, 0x67, 0xf0, 0xad, 0x50, 0x13, 0x19, 0x72, 0x64, 0xbf, 0x52, 0xc2, 0x34, 0xe2,
  41. 0x1b, 0x11, 0x79, 0x74, 0x72, 0xbd, 0x34, 0x5b, 0xb1, 0xe2, 0xfd, 0x66, 0x73, 0xfe, 0x71, 0x64,
  42. 0x74, 0xd0, 0x4e, 0xbc, 0x51, 0x24, 0x19, 0x40, 0x87, 0x0e, 0x92, 0x40, 0xe6, 0x21, 0xe7, 0x2d,
  43. 0x4e, 0x37, 0x76, 0x2f, 0x2e, 0xe2, 0x68, 0xc7, 0x89, 0xe8, 0x32, 0x13, 0x42, 0x06, 0x84, 0x84,
  44. 0x53, 0x4a, 0xb3, 0x0c, 0x1b, 0x4c, 0x8d, 0x1c, 0x51, 0x97, 0x19, 0xab, 0xae, 0x77, 0xff, 0xdb,
  45. 0xec, 0xf0, 0x10, 0x95, 0x34, 0x33, 0x6b, 0xcb, 0x3e, 0x84, 0x0f, 0xb9, 0xd8, 0x5f, 0xb8, 0xa0,
  46. 0xb8, 0x55, 0x53, 0x3e, 0x70, 0xf7, 0x18, 0xf5, 0xce, 0x7b, 0x4e, 0xbf, 0x27, 0xce, 0xce, 0xa8,
  47. 0xb3, 0xbe, 0x40, 0xc5, 0xc5, 0x32, 0x29, 0x3e, 0x71, 0x64, 0x9e, 0xde, 0x8c, 0xf6, 0x75, 0xa1,
  48. 0xe6, 0xf6, 0x53, 0xc8, 0x31, 0xa8, 0x78, 0xde, 0x50, 0x40, 0xf7, 0x62, 0xde, 0x36, 0xb2, 0xba
  49. };
  50. #endif
  51. static esp_err_t example_get_sec2_salt(const char **salt, uint16_t *salt_len)
  52. {
  53. #if CONFIG_EXAMPLE_PROV_SEC2_DEV_MODE
  54. ESP_LOGI(TAG, "Development mode: using hard coded salt");
  55. *salt = sec2_salt;
  56. *salt_len = sizeof(sec2_salt);
  57. return ESP_OK;
  58. #elif CONFIG_EXAMPLE_PROV_SEC2_PROD_MODE
  59. ESP_LOGE(TAG, "Not implemented!");
  60. return ESP_FAIL;
  61. #endif
  62. }
  63. static esp_err_t example_get_sec2_verifier(const char **verifier, uint16_t *verifier_len)
  64. {
  65. #if CONFIG_EXAMPLE_PROV_SEC2_DEV_MODE
  66. ESP_LOGI(TAG, "Development mode: using hard coded verifier");
  67. *verifier = sec2_verifier;
  68. *verifier_len = sizeof(sec2_verifier);
  69. return ESP_OK;
  70. #elif CONFIG_EXAMPLE_PROV_SEC2_PROD_MODE
  71. /* This code needs to be updated with appropriate implementation to provide verifier */
  72. ESP_LOGE(TAG, "Not implemented!");
  73. return ESP_FAIL;
  74. #endif
  75. }
  76. #endif // CONFIG_EXAMPLE_PROV_SECURITY_VERSION_2
  77. struct events {
  78. EventGroupHandle_t *flags;
  79. int success_bit;
  80. int fail_bit;
  81. bool success;
  82. };
  83. static void event_handler(void *arg, esp_event_base_t event_base,
  84. int32_t event_id, void *event_data)
  85. {
  86. struct events *handler_args = arg;
  87. switch (event_id) {
  88. case WIFI_PROV_START:
  89. ESP_LOGI(TAG, "Provisioning started");
  90. break;
  91. case WIFI_PROV_CRED_RECV: {
  92. wifi_sta_config_t *wifi_sta_cfg = (wifi_sta_config_t *) event_data;
  93. ESP_LOGI(TAG, "Received Wi-Fi credentials"
  94. "\n\tSSID : %s\n\tPassword : %s",
  95. (const char *) wifi_sta_cfg->ssid,
  96. (const char *) wifi_sta_cfg->password);
  97. break;
  98. }
  99. case WIFI_PROV_CRED_FAIL: {
  100. wifi_prov_sta_fail_reason_t *reason = (wifi_prov_sta_fail_reason_t *) event_data;
  101. ESP_LOGE(TAG, "Provisioning failed!\n\tReason : %s"
  102. "\n\tPlease reset to factory and retry provisioning",
  103. (*reason == WIFI_PROV_STA_AUTH_ERROR) ?
  104. "Wi-Fi station authentication failed" : "Wi-Fi access-point not found");
  105. handler_args->success = false;
  106. break;
  107. }
  108. case WIFI_PROV_CRED_SUCCESS:
  109. ESP_LOGI(TAG, "Provisioning successful");
  110. handler_args->success = true;
  111. break;
  112. case WIFI_PROV_END:
  113. /* De-initialize manager once provisioning is finished */
  114. wifi_prov_mgr_deinit();
  115. xEventGroupSetBits(*handler_args->flags, handler_args->success ? handler_args->success_bit : handler_args->fail_bit);
  116. free(handler_args);
  117. break;
  118. default:
  119. break;
  120. }
  121. }
  122. extern const wifi_prov_scheme_t wifi_prov_scheme_httpd;
  123. esp_err_t start_provisioning(EventGroupHandle_t *flags, int success_bit, int fail_bit)
  124. {
  125. // Start the DNS server that will reply to "wifi.settings" with "usb" network interface address
  126. dns_server_config_t dns_config = DNS_SERVER_CONFIG_SINGLE("wifi.settings" /* name */, "wired" /* wired netif ID */);
  127. start_dns_server(&dns_config);
  128. struct events *handler_args = malloc(sizeof(struct events));
  129. handler_args->flags = flags;
  130. handler_args->success_bit = success_bit;
  131. handler_args->fail_bit = fail_bit;
  132. ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID, event_handler, handler_args));
  133. /* Configuration for the provisioning manager */
  134. wifi_prov_mgr_config_t config = {
  135. .scheme = wifi_prov_scheme_httpd,
  136. };
  137. /* Initialize provisioning manager with the
  138. * configuration parameters set above */
  139. ESP_ERROR_CHECK(wifi_prov_mgr_init(config));
  140. /* What is the security level that we want (0, 1, 2):
  141. * - WIFI_PROV_SECURITY_0 is simply plain text communication.
  142. * - WIFI_PROV_SECURITY_1 is secure communication which consists of secure handshake
  143. * - WIFI_PROV_SECURITY_2 SRP6a based authentication and key exchange
  144. * Please check unified provisioning documentation for more details
  145. */
  146. #ifdef CONFIG_EXAMPLE_PROV_SECURITY_VERSION_0
  147. wifi_prov_security_t security = WIFI_PROV_SECURITY_0;
  148. #elif CONFIG_EXAMPLE_PROV_SECURITY_VERSION_1
  149. wifi_prov_security_t security = WIFI_PROV_SECURITY_1;
  150. const char *pop = "abcd1234"; /* Proof of possession */
  151. wifi_prov_security1_params_t *sec_params = pop;
  152. #elif CONFIG_EXAMPLE_PROV_SECURITY_VERSION_2
  153. wifi_prov_security_t security = WIFI_PROV_SECURITY_2;
  154. /* The username must be the same one, which has been used in the generation of salt and verifier */
  155. #if CONFIG_EXAMPLE_PROV_SEC2_DEV_MODE
  156. /* This pop field represents the password that will be used to generate salt and verifier.
  157. * The field is present here in order to generate the QR code containing password.
  158. * In production this password field shall not be stored on the device */
  159. const char *username = EXAMPLE_PROV_SEC2_USERNAME;
  160. const char *pop = EXAMPLE_PROV_SEC2_PWD;
  161. #elif CONFIG_EXAMPLE_PROV_SEC2_PROD_MODE
  162. /* The username and password shall not be embedded in the firmware */
  163. const char *username = NULL;
  164. const char *pop = NULL;
  165. #endif
  166. /* This is the structure for passing security parameters
  167. * for the protocomm security 2.
  168. * If dynamically allocated, sec2_params pointer and its content
  169. * must be valid till WIFI_PROV_END event is triggered.
  170. */
  171. wifi_prov_security2_params_t sec2_params = {};
  172. ESP_ERROR_CHECK(example_get_sec2_salt(&sec2_params.salt, &sec2_params.salt_len));
  173. ESP_ERROR_CHECK(example_get_sec2_verifier(&sec2_params.verifier, &sec2_params.verifier_len));
  174. wifi_prov_security2_params_t *sec_params = &sec2_params;
  175. #endif // CONFIG_EXAMPLE_PROV_SECURITY_VERSION_0 (VERSION_1, VERSION_2)
  176. ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(security, (const void *) sec_params, NULL, NULL)); // service name and key could be NULL
  177. return ESP_OK;
  178. }
  179. bool is_provisioned(void)
  180. {
  181. bool provisioned = false;
  182. ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned));
  183. return provisioned;
  184. }