Encode header to base64

This commit is contained in:
Pierre HUBERT 2024-09-21 20:43:02 +02:00
parent cf31de5b67
commit c55c55d56d
6 changed files with 62 additions and 2 deletions

View File

@ -47,6 +47,8 @@
"format": "c", "format": "c",
"span": "c", "span": "c",
"regex": "c", "regex": "c",
"stdlib.h": "c" "stdlib.h": "c",
"secure_api.h": "c",
"jwt.h": "c"
} }
} }

View File

@ -13,6 +13,7 @@
#include <mbedtls/sha256.h> #include <mbedtls/sha256.h>
#include <mbedtls/pk.h> #include <mbedtls/pk.h>
#include <mbedtls/x509_csr.h> #include <mbedtls/x509_csr.h>
#include <mbedtls/base64.h>
#include "esp_log.h" #include "esp_log.h"
#define ECPARAMS MBEDTLS_ECP_DP_SECP256R1 #define ECPARAMS MBEDTLS_ECP_DP_SECP256R1
@ -189,4 +190,38 @@ char *crypto_get_csr()
mbedtls_entropy_free(&entropy); mbedtls_entropy_free(&entropy);
return csr; return csr;
}
char *crypto_encode_base64_safe_url(const char *src, size_t srclen)
{
size_t olen = 0;
mbedtls_base64_encode(NULL, 0, &olen, (unsigned char *)src, srclen);
if (olen < 1)
{
ESP_LOGE(TAG, "Failed to determine base64 buffer size!");
return NULL;
}
char *out = calloc(1, olen);
if (!out)
{
ESP_LOGE(TAG, "Failed to allocate memory for destination buffer!");
return NULL;
}
if (mbedtls_base64_encode((unsigned char *)out, olen, &olen, (unsigned char *)src, srclen) != 0)
{
ESP_LOGE(TAG, "Failed to perfom base64 encoding!");
free(out);
return NULL;
}
if (out[olen - 1] == '=')
out[olen - 1] = '\0';
if (out[olen - 2] == '=')
out[olen - 2] = '\0';
return out;
} }

View File

@ -5,6 +5,7 @@
#pragma once #pragma once
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"
@ -31,6 +32,13 @@ extern "C"
*/ */
char *crypto_get_csr(); char *crypto_get_csr();
/**
* Encode buffer to base64 safe URL string
*
* @return A buffer that needs to be freed or NULL in case of failure
*/
char *crypto_encode_base64_safe_url(const char *src, size_t srclen);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -4,6 +4,7 @@
#include "jwt.h" #include "jwt.h"
#include "dev_name.h" #include "dev_name.h"
#include "crypto.h"
#include "esp_log.h" #include "esp_log.h"
@ -34,7 +35,19 @@ char *jwt_gen(cJSON *payload)
return NULL; return NULL;
} }
printf("header: %s\n", header); char *header_b64 = crypto_encode_base64_safe_url(header, strlen(header));
free(header);
if (!header_b64)
{
ESP_LOGE(TAG, "Failed to encode header to base64!");
return NULL;
}
printf("header = %s\n", header_b64);
free(header_b64);
// TODO : continue
return strdup("TODO:)"); return strdup("TODO:)");
} }

View File

@ -167,6 +167,7 @@ void app_main(void)
// Main loop // Main loop
ESP_LOGI(TAG, "Starting main loop"); ESP_LOGI(TAG, "Starting main loop");
// TODO : implement more properly
while (true) while (true)
{ {
if (!secure_api_sync_device()) if (!secure_api_sync_device())

View File

@ -218,6 +218,7 @@ void *secure_api_sync_device()
printf("JWT: %s\n", encoded_req); printf("JWT: %s\n", encoded_req);
free(encoded_req); free(encoded_req);
// TODO : replace
printf("here implement sync device logic\n"); printf("here implement sync device logic\n");
return NULL; return NULL;
} }