SolarEnergy/esp32_device/main/secure_api.c

269 lines
6.0 KiB
C
Raw Normal View History

2024-08-18 18:13:03 +00:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "secure_api.h"
#include "http_client.h"
#include "constants.h"
2024-08-23 19:00:18 +00:00
#include "crypto.h"
2024-08-18 18:33:26 +00:00
#include "cJSON.h"
2024-08-23 21:06:14 +00:00
#include "dev_name.h"
#include "storage.h"
#include "http_client.h"
2024-09-21 18:16:44 +00:00
#include "jwt.h"
2024-09-28 18:27:12 +00:00
#include "relays.h"
2024-08-18 18:13:03 +00:00
#include "esp_log.h"
static const char *TAG = "secure_api";
2024-08-23 19:00:18 +00:00
static char *process_secure_request(const char *uri, const char *body)
2024-08-18 18:13:03 +00:00
{
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,
2024-08-23 21:06:14 +00:00
.root_ca = root_cat,
.body = body,
.method = body == NULL ? MethodGET : MethodPOST,
.content_type = body == NULL ? NULL : "application/json"};
2024-08-18 18:13:03 +00:00
char *res = http_client_exec(&opts);
free(url);
free(root_cat);
return res;
}
2024-08-28 22:09:47 +00:00
static char *dev_escaped_name()
2024-08-18 18:13:03 +00:00
{
2024-08-23 21:06:14 +00:00
unsigned char *name = (unsigned char *)dev_name();
assert(name);
size_t escaped_name_len = http_client_escape_uri(NULL, name, strlen((char *)name));
unsigned char *escaped_name = calloc(1, escaped_name_len + 1);
assert(escaped_name);
http_client_escape_uri(escaped_name, name, strlen((char *)name));
free(name);
2024-08-28 22:09:47 +00:00
return (char *)escaped_name;
}
enum DevEnrollmentStatus secure_api_get_device_enrollment_status()
{
ESP_LOGI(TAG, "Will check device enrollment status");
// Prepare URI
char *escaped_name = dev_escaped_name();
2024-08-18 18:13:03 +00:00
char *uri = calloc(1, 255);
assert(uri);
2024-08-23 21:06:14 +00:00
sprintf(uri, "/devices_api/mgmt/enrollment_status?id=%s", escaped_name);
free(escaped_name);
2024-08-18 18:13:03 +00:00
2024-08-23 19:00:18 +00:00
char *res = process_secure_request(uri, NULL);
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-23 19:00:18 +00:00
}
/**
* Generate device information. Pointer to be released by caller
*/
static cJSON *genDevInfo()
{
cJSON *json = cJSON_CreateObject();
if (!json)
return NULL;
cJSON_AddStringToObject(json, "reference", DEV_REFERENCE);
cJSON_AddStringToObject(json, "version", DEV_VERSION);
2024-09-28 18:27:12 +00:00
cJSON_AddNumberToObject(json, "max_relays", relays_count());
2024-08-23 19:00:18 +00:00
return json;
}
int secure_api_enroll_device()
{
char *csr = crypto_get_csr();
if (!csr)
{
ESP_LOGE(TAG, "Failed to get CSR!");
return 1;
}
cJSON *obj = cJSON_CreateObject();
if (!obj)
{
ESP_LOGE(TAG, "Failed allocate memory to store JSON object!");
2024-08-23 21:06:14 +00:00
free(csr);
2024-08-23 19:00:18 +00:00
return 1;
}
cJSON_AddItemToObject(obj, "info", genDevInfo());
cJSON_AddStringToObject(obj, "csr", csr);
2024-08-23 21:06:14 +00:00
free(csr);
2024-08-23 19:00:18 +00:00
char *body = cJSON_PrintUnformatted(obj);
2024-09-28 14:35:05 +00:00
cJSON_Delete(obj);
2024-08-23 19:00:18 +00:00
if (!body)
{
ESP_LOGE(TAG, "Failed to generate JSON body!");
return 1;
}
2024-08-23 21:06:14 +00:00
char *res = process_secure_request("/devices_api/mgmt/enroll", body);
2024-08-23 19:00:18 +00:00
free(body);
2024-08-23 21:06:14 +00:00
if (res == NULL)
{
ESP_LOGE(TAG, "Request failed!");
return 1;
}
free(res);
2024-08-23 19:00:18 +00:00
return 0;
2024-08-28 22:09:47 +00:00
}
char *secure_api_get_dev_certificate()
{
ESP_LOGI(TAG, "Will request device certificate");
// Prepare URI
char *escaped_name = dev_escaped_name();
char *uri = calloc(1, 255);
assert(uri);
sprintf(uri, "/devices_api/mgmt/get_certificate?id=%s", escaped_name);
free(escaped_name);
char *res = process_secure_request(uri, NULL);
free(uri);
if (res == NULL)
{
ESP_LOGE(TAG, "Failed to query device certificate!");
return NULL;
}
return res;
}
2024-09-28 14:35:05 +00:00
sync_response *secure_api_sync_device()
{
2024-09-21 18:16:44 +00:00
cJSON *obj = cJSON_CreateObject();
if (!obj)
{
ESP_LOGE(TAG, "Failed allocate memory to store JSON object!");
return NULL;
}
cJSON_AddItemToObject(obj, "info", genDevInfo());
char *encoded_req = jwt_gen(obj);
cJSON_Delete(obj);
if (!encoded_req)
{
ESP_LOGE(TAG, "Failed to encode JWT!");
return NULL;
}
2024-09-28 14:35:05 +00:00
// Prepare request body
cJSON *json_body = cJSON_CreateObject();
if (!json_body)
{
ESP_LOGE(TAG, "Failed to allocated memory to store sync request body!");
free(encoded_req);
return NULL;
}
cJSON_AddStringToObject(json_body, "payload", encoded_req);
2024-09-21 18:16:44 +00:00
free(encoded_req);
2024-09-28 14:35:05 +00:00
char *body = cJSON_PrintUnformatted(json_body);
cJSON_Delete(json_body);
if (!body)
{
ESP_LOGE(TAG, "Failed to allocated memory to store encoded sync request body!");
return NULL;
}
// Send request
char *res = process_secure_request("/devices_api/mgmt/sync", body);
2024-09-28 14:42:10 +00:00
free(body);
2024-09-28 14:35:05 +00:00
if (res == NULL)
{
ESP_LOGE(TAG, "Sync request failed!");
return NULL;
}
// Parse response
cJSON *states = cJSON_Parse(res);
free(res);
if (!states)
{
ESP_LOGE(TAG, "Failed to decode sync response from server!");
return NULL;
}
sync_response *sync_res = sync_response_parse(states);
cJSON_Delete(states);
if (!sync_res)
{
ESP_LOGE(TAG, "Failed to parse sync response from server!");
return NULL;
}
return sync_res;
2024-08-18 18:13:03 +00:00
}