2024-08-18 18:13:03 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "secure_api.h"
|
|
|
|
#include "storage.h"
|
|
|
|
#include "http_client.h"
|
|
|
|
#include "constants.h"
|
2024-08-18 18:33:26 +00:00
|
|
|
#include "cJSON.h"
|
2024-08-18 18:13:03 +00:00
|
|
|
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
|
|
|
static const char *TAG = "secure_api";
|
|
|
|
|
|
|
|
static char *process_secure_request(const char *uri)
|
|
|
|
{
|
|
|
|
char *url = calloc(1, 255);
|
|
|
|
assert(url);
|
|
|
|
size_t orig_len = storage_get_secure_origin(url);
|
|
|
|
assert(orig_len > 0);
|
|
|
|
strcat(url + strlen(url), uri);
|
|
|
|
ESP_LOGI(TAG, "HTTP request on %s", url);
|
|
|
|
|
|
|
|
char *root_cat = calloc(1, ROOT_CA_MAX_BYTES);
|
|
|
|
assert(root_cat);
|
|
|
|
assert(storage_get_root_ca(root_cat) > 0);
|
|
|
|
|
|
|
|
http_request_opts opts = {
|
|
|
|
.url = url,
|
|
|
|
.root_ca = root_cat};
|
|
|
|
char *res = http_client_exec(&opts);
|
|
|
|
|
|
|
|
free(url);
|
|
|
|
free(root_cat);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum DevEnrollmentStatus secure_api_get_device_enrollment_status()
|
|
|
|
{
|
|
|
|
ESP_LOGI(TAG, "Will check device enrollment status");
|
|
|
|
|
|
|
|
// Prepare URI
|
|
|
|
char *uri = calloc(1, 255);
|
|
|
|
assert(uri);
|
|
|
|
sprintf(uri, "/devices_api/mgmt/enrollment_status?id=");
|
|
|
|
assert(storage_get_dev_name(uri + strlen(uri)) > 0);
|
|
|
|
|
|
|
|
char *res = process_secure_request(uri);
|
2024-08-18 18:33:26 +00:00
|
|
|
|
2024-08-18 18:13:03 +00:00
|
|
|
free(uri);
|
|
|
|
|
|
|
|
if (res == NULL)
|
|
|
|
{
|
|
|
|
ESP_LOGE(TAG, "Failed to query device enrollment status!");
|
|
|
|
return DevEnrollError;
|
|
|
|
}
|
|
|
|
|
2024-08-18 18:33:26 +00:00
|
|
|
enum DevEnrollmentStatus s = DevEnrollError;
|
|
|
|
cJSON *root = cJSON_Parse(res);
|
|
|
|
if (root == NULL)
|
|
|
|
{
|
|
|
|
ESP_LOGE(TAG, "Failed to decode JSON response from server!");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
cJSON *status = cJSON_GetObjectItem(root, "status");
|
|
|
|
if (status == NULL)
|
|
|
|
{
|
|
|
|
ESP_LOGE(TAG, "Status missing in response from server!");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(status->valuestring, "Unknown"))
|
|
|
|
s = DevEnrollUnknown;
|
|
|
|
else if (!strcmp(status->valuestring, "Pending"))
|
|
|
|
s = DevEnrollPending;
|
|
|
|
else if (!strcmp(status->valuestring, "Validated"))
|
|
|
|
s = DevEnrollValidated;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ESP_LOGE(TAG, "Unknown enrollment status: %s", status->valuestring);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
fail:
|
|
|
|
|
|
|
|
cJSON_Delete(root);
|
2024-08-18 18:13:03 +00:00
|
|
|
free(res);
|
|
|
|
|
2024-08-18 18:33:26 +00:00
|
|
|
return s;
|
2024-08-18 18:13:03 +00:00
|
|
|
}
|