Submit CSR to server

This commit is contained in:
2024-08-23 23:06:14 +02:00
parent 3b7e2f9a0c
commit d890b23670
4 changed files with 130 additions and 7 deletions

View File

@ -3,11 +3,13 @@
#include <stdio.h>
#include "secure_api.h"
#include "storage.h"
#include "http_client.h"
#include "constants.h"
#include "crypto.h"
#include "cJSON.h"
#include "dev_name.h"
#include "storage.h"
#include "http_client.h"
#include "esp_log.h"
@ -28,7 +30,10 @@ static char *process_secure_request(const char *uri, const char *body)
http_request_opts opts = {
.url = url,
.root_ca = root_cat};
.root_ca = root_cat,
.body = body,
.method = body == NULL ? MethodGET : MethodPOST,
.content_type = body == NULL ? NULL : "application/json"};
char *res = http_client_exec(&opts);
free(url);
@ -42,10 +47,18 @@ enum DevEnrollmentStatus secure_api_get_device_enrollment_status()
ESP_LOGI(TAG, "Will check device enrollment status");
// Prepare URI
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);
char *uri = calloc(1, 255);
assert(uri);
sprintf(uri, "/devices_api/mgmt/enrollment_status?id=");
assert(storage_get_dev_name(uri + strlen(uri)) > 0);
sprintf(uri, "/devices_api/mgmt/enrollment_status?id=%s", escaped_name);
free(escaped_name);
char *res = process_secure_request(uri, NULL);
@ -119,25 +132,34 @@ int secure_api_enroll_device()
if (!obj)
{
ESP_LOGE(TAG, "Failed allocate memory to store JSON object!");
free(csr);
return 1;
}
cJSON_AddItemToObject(obj, "info", genDevInfo());
cJSON_AddStringToObject(obj, "csr", csr);
free(csr);
char *body = cJSON_PrintUnformatted(obj);
if (!body)
{
ESP_LOGE(TAG, "Failed to generate JSON body!");
cJSON_Delete(obj);
return 1;
}
// TODO : perform request
printf("res = %s\n", body);
char *res = process_secure_request("/devices_api/mgmt/enroll", body);
cJSON_Delete(obj);
free(body);
free(csr);
if (res == NULL)
{
ESP_LOGE(TAG, "Request failed!");
return 1;
}
free(res);
return 0;
}