SolarEnergy/esp32_device/main/crypto.c

192 lines
5.3 KiB
C
Raw Normal View History

2024-07-27 14:15:35 +00:00
#include "crypto.h"
#include "system.h"
2024-08-15 11:09:01 +00:00
#include "constants.h"
#include "storage.h"
2024-08-16 09:51:33 +00:00
#include "dev_name.h"
2024-07-27 14:15:35 +00:00
2024-07-27 14:34:41 +00:00
#include <string.h>
2024-07-27 14:15:35 +00:00
#include <mbedtls/build_info.h>
#include <mbedtls/platform.h>
#include <mbedtls/entropy.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/ecdsa.h>
#include <mbedtls/sha256.h>
#include <mbedtls/pk.h>
2024-08-16 09:51:33 +00:00
#include <mbedtls/x509_csr.h>
2024-08-17 15:19:47 +00:00
#include "esp_log.h"
2024-07-27 14:15:35 +00:00
#define ECPARAMS MBEDTLS_ECP_DP_SECP256R1
2024-08-17 15:19:47 +00:00
static const char *TAG = "crypto";
2024-08-15 11:32:01 +00:00
static const char *pers = "ecdsa";
2024-07-27 14:15:35 +00:00
2024-08-15 11:32:01 +00:00
static void seed_ctr_drbg_context(mbedtls_entropy_context *entropy, mbedtls_ctr_drbg_context *ctr_drbg)
{
int ret;
2024-07-27 14:15:35 +00:00
2024-08-15 11:32:01 +00:00
mbedtls_entropy_init(entropy);
mbedtls_ctr_drbg_init(ctr_drbg);
2024-07-27 14:15:35 +00:00
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Seed Mbedtls\n");
2024-08-15 11:32:01 +00:00
if ((ret = mbedtls_ctr_drbg_seed(ctr_drbg, mbedtls_entropy_func, entropy,
2024-07-27 14:15:35 +00:00
(const unsigned char *)pers,
strlen(pers))) != 0)
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
2024-07-27 14:15:35 +00:00
reboot();
}
2024-08-15 11:32:01 +00:00
}
bool crypto_gen_priv_key()
{
// Check if a private key has already been defined for this device
if (storage_get_priv_key(NULL) > 0)
return false;
int ret = 1;
mbedtls_pk_context key;
mbedtls_pk_init(&key);
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
seed_ctr_drbg_context(&entropy, &ctr_drbg);
2024-07-27 14:15:35 +00:00
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "PK info from type\n");
2024-07-27 14:34:41 +00:00
if ((ret = mbedtls_pk_setup(&key, mbedtls_pk_info_from_type(MBEDTLS_PK_ECKEY))) != 0)
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, " failed\n ! mbedtls_pk_setup returned -0x%04x", (unsigned int)-ret);
2024-07-27 14:34:41 +00:00
reboot();
}
2024-07-27 14:15:35 +00:00
// Generate private key
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Generate private key\n");
2024-07-27 14:34:41 +00:00
ret = mbedtls_ecp_gen_key(ECPARAMS,
mbedtls_pk_ec(key),
mbedtls_ctr_drbg_random, &ctr_drbg);
if (ret != 0)
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, " failed\n ! mbedtls_ecp_gen_key returned -0x%04x",
(unsigned int)-ret);
2024-07-27 14:34:41 +00:00
reboot();
}
2024-08-15 11:09:01 +00:00
// Export private key
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Export private key\n");
2024-08-15 11:09:01 +00:00
unsigned char *key_buff = malloc(PRV_KEY_DER_MAX_BYTES);
if ((ret = mbedtls_pk_write_key_der(&key, key_buff, PRV_KEY_DER_MAX_BYTES)) < 1)
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, " failed\n ! mbedtls_pk_write_key_der returned -0x%04x",
(unsigned int)-ret);
2024-08-15 11:09:01 +00:00
reboot();
}
storage_set_priv_key(key_buff + PRV_KEY_DER_MAX_BYTES - ret, ret);
2024-08-15 11:32:01 +00:00
free(key_buff);
2024-08-15 11:09:01 +00:00
2024-08-16 09:51:33 +00:00
mbedtls_pk_free(&key);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
2024-08-15 11:09:01 +00:00
return true;
}
2024-08-15 11:32:01 +00:00
void crypto_print_priv_key()
{
int ret;
unsigned char *key_buff = malloc(PRV_KEY_DER_MAX_BYTES);
size_t key_len = storage_get_priv_key(key_buff);
assert(key_len > 0);
mbedtls_pk_context key;
mbedtls_pk_init(&key);
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
seed_ctr_drbg_context(&entropy, &ctr_drbg);
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Parse private key (len = %d)\n", key_len);
2024-08-15 11:32:01 +00:00
if ((ret = mbedtls_pk_parse_key(&key, key_buff, key_len, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0)
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, " failed\n ! mbedtls_pk_parse_key returned -0x%04x",
(unsigned int)-ret);
2024-08-15 11:32:01 +00:00
reboot();
}
2024-08-16 09:51:33 +00:00
free(key_buff);
2024-08-15 11:32:01 +00:00
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Show private key\n");
2024-08-15 11:32:01 +00:00
unsigned char *out = malloc(16000);
memset(out, 0, 16000);
if ((ret = mbedtls_pk_write_key_pem(&key, out, 16000)) != 0)
2024-07-27 14:15:35 +00:00
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, " failed\n ! mbedtls_pk_write_key_pem returned -0x%04x",
(unsigned int)-ret);
2024-07-27 14:15:35 +00:00
reboot();
}
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "%s", out);
2024-08-15 11:32:01 +00:00
free(out);
2024-08-16 09:51:33 +00:00
mbedtls_pk_free(&key);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
2024-08-15 11:32:01 +00:00
}
2024-08-16 09:51:33 +00:00
char *crypto_get_csr()
{
int ret;
unsigned char *key_buff = malloc(PRV_KEY_DER_MAX_BYTES);
size_t key_len = storage_get_priv_key(key_buff);
assert(key_len > 0);
mbedtls_pk_context key;
mbedtls_pk_init(&key);
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
seed_ctr_drbg_context(&entropy, &ctr_drbg);
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Parse private key (len = %d)\n", key_len);
2024-08-16 09:51:33 +00:00
if ((ret = mbedtls_pk_parse_key(&key, key_buff, key_len, NULL, 0, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0)
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, " failed\n ! mbedtls_pk_parse_key returned -0x%04x",
(unsigned int)-ret);
2024-08-16 09:51:33 +00:00
reboot();
}
free(key_buff);
// Create CSR
mbedtls_x509write_csr req;
mbedtls_x509write_csr_init(&req);
mbedtls_x509write_csr_set_md_alg(&req, MBEDTLS_MD_SHA256);
char subj[DEV_NAME_LEN + 4];
char *n = dev_name();
sprintf(subj, "CN=%s", n);
free(n);
if ((ret = mbedtls_x509write_csr_set_subject_name(&req, subj)) != 0)
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, " failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret);
2024-08-16 09:51:33 +00:00
reboot();
}
2024-08-17 15:19:47 +00:00
ESP_LOGI(TAG, "Sign CSR with private key\n");
2024-08-16 09:51:33 +00:00
mbedtls_x509write_csr_set_key(&req, &key);
char *csr = malloc(4096);
if ((ret = mbedtls_x509write_csr_pem(&req, (u_char *)csr, 4096, mbedtls_ctr_drbg_random, &ctr_drbg)) < 0)
{
2024-08-17 15:19:47 +00:00
ESP_LOGE(TAG, " failed\n ! mbedtls_x509write_csr_pem returned %d", ret);
2024-08-16 09:51:33 +00:00
reboot();
}
mbedtls_x509write_csr_free(&req);
mbedtls_pk_free(&key);
mbedtls_ctr_drbg_free(&ctr_drbg);
mbedtls_entropy_free(&entropy);
return csr;
}