Encode header to base64

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

View File

@ -13,6 +13,7 @@
#include <mbedtls/sha256.h>
#include <mbedtls/pk.h>
#include <mbedtls/x509_csr.h>
#include <mbedtls/base64.h>
#include "esp_log.h"
#define ECPARAMS MBEDTLS_ECP_DP_SECP256R1
@ -189,4 +190,38 @@ char *crypto_get_csr()
mbedtls_entropy_free(&entropy);
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;
}