Ready to implement JWT signature

This commit is contained in:
Pierre HUBERT 2024-09-27 20:13:50 +02:00
parent fa431857ee
commit 77c3702986
3 changed files with 45 additions and 5 deletions

View File

@ -225,3 +225,9 @@ char *crypto_encode_base64_safe_url(const char *src, size_t srclen)
return out;
}
char *crypto_sign_sha256_payload(const char *src, const size_t src_len, size_t *srclen)
{
*srclen = 10;
return calloc(1, 10);
}

View File

@ -39,6 +39,11 @@ extern "C"
*/
char *crypto_encode_base64_safe_url(const char *src, size_t srclen);
/**
* Sign some data using sha256
*/
char *crypto_sign_sha256_payload(const char *src, const size_t src_len, size_t *srclen);
#ifdef __cplusplus
}
#endif

View File

@ -77,10 +77,39 @@ char *jwt_gen(cJSON *payload)
free(header_b64);
free(body_b64);
// TODO : wip
printf("unsigned = %s\n", unsigned_jwt);
free(unsigned_jwt);
size_t sig_len = 0;
char *sig = crypto_sign_sha256_payload(unsigned_jwt, strlen(unsigned_jwt), &sig_len);
// TODO : continue
return strdup("TODO:)");
if (!sig || sig_len == 0)
{
ESP_LOGE(TAG, "Failed to sign JWT!");
if (sig)
free(sig);
free(unsigned_jwt);
return NULL;
}
char *sig_b64 = crypto_encode_base64_safe_url(sig, sig_len);
free(sig);
if (!sig_b64)
{
ESP_LOGE(TAG, "Failed to encode base64 signature to base64!");
free(unsigned_jwt);
return NULL;
}
char *jwt = calloc(1, 1 + strlen(unsigned_jwt) + 1 + strlen(sig_b64));
if (!jwt)
{
ESP_LOGE(TAG, "Failed to allocate memory to store final JWT!");
free(unsigned_jwt);
free(sig_b64);
return NULL;
}
sprintf(jwt, "%s.%s", unsigned_jwt, sig_b64);
free(unsigned_jwt);
free(sig_b64);
return jwt;
}