Write private key
This commit is contained in:
parent
402edb44d5
commit
752bf50ad3
3
esp32_device/.vscode/settings.json
vendored
3
esp32_device/.vscode/settings.json
vendored
@ -20,6 +20,7 @@
|
||||
"ecdsa.h": "c",
|
||||
"platform.h": "c",
|
||||
"build_info.h": "c",
|
||||
"config_adjust_ssl.h": "c"
|
||||
"config_adjust_ssl.h": "c",
|
||||
"pk.h": "c"
|
||||
}
|
||||
}
|
||||
|
@ -4,3 +4,8 @@
|
||||
* Device name len
|
||||
*/
|
||||
#define DEV_NAME_LEN 10
|
||||
|
||||
/**
|
||||
* Private key max length
|
||||
*/
|
||||
#define PRV_KEY_DER_MAX_BYTES 1500
|
@ -1,5 +1,7 @@
|
||||
#include "crypto.h"
|
||||
#include "system.h"
|
||||
#include "constants.h"
|
||||
#include "storage.h"
|
||||
|
||||
#include <string.h>
|
||||
#include <mbedtls/build_info.h>
|
||||
@ -10,9 +12,6 @@
|
||||
#include <mbedtls/sha256.h>
|
||||
#include <mbedtls/pk.h>
|
||||
|
||||
/*
|
||||
* Uncomment to force use of a specific curve
|
||||
*/
|
||||
#define ECPARAMS MBEDTLS_ECP_DP_SECP256R1
|
||||
|
||||
bool crypto_gen_priv_key()
|
||||
@ -60,7 +59,23 @@ bool crypto_gen_priv_key()
|
||||
reboot();
|
||||
}
|
||||
|
||||
// Show private key
|
||||
// Export private key
|
||||
printf("Export private key\n");
|
||||
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)
|
||||
{
|
||||
mbedtls_printf(" failed\n ! mbedtls_pk_write_key_der returned -0x%04x",
|
||||
(unsigned int)-ret);
|
||||
reboot();
|
||||
}
|
||||
|
||||
storage_set_priv_key(key_buff + PRV_KEY_DER_MAX_BYTES - ret, ret);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* // Show private key
|
||||
printf("Show private key\n");
|
||||
unsigned char *key_buff = malloc(16000);
|
||||
memset(key_buff, 0, 16000);
|
||||
@ -74,6 +89,4 @@ bool crypto_gen_priv_key()
|
||||
printf("%s", key_buff);
|
||||
free(key_buff);
|
||||
printf("done\n");
|
||||
|
||||
return true;
|
||||
}
|
||||
*/
|
@ -7,6 +7,7 @@
|
||||
#define STORAGE_NAMESPACE "storage"
|
||||
|
||||
#define DEV_NAME_KEY "dev_name"
|
||||
#define PRIVATE_KEY "prikey"
|
||||
|
||||
bool storage_init()
|
||||
{
|
||||
@ -53,3 +54,33 @@ size_t storage_get_dev_name(char *dest)
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
void storage_set_priv_key(unsigned char *key, size_t len)
|
||||
{
|
||||
nvs_handle_t my_handle;
|
||||
|
||||
ESP_ERROR_CHECK(nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle));
|
||||
|
||||
ESP_ERROR_CHECK(nvs_set_blob(my_handle, PRIVATE_KEY, key, len));
|
||||
|
||||
nvs_close(my_handle);
|
||||
}
|
||||
|
||||
size_t storage_get_priv_key(unsigned char *key)
|
||||
{
|
||||
nvs_handle_t my_handle;
|
||||
|
||||
ESP_ERROR_CHECK(nvs_open(STORAGE_NAMESPACE, NVS_READWRITE, &my_handle));
|
||||
|
||||
size_t len = (key == NULL ? 0 : PRV_KEY_DER_MAX_BYTES);
|
||||
esp_err_t res = nvs_get_blob(my_handle, PRIVATE_KEY, key, &len);
|
||||
|
||||
nvs_close(my_handle);
|
||||
|
||||
if (res == ESP_ERR_NVS_NOT_FOUND || len == 0)
|
||||
return 0;
|
||||
|
||||
ESP_ERROR_CHECK(res);
|
||||
|
||||
return len;
|
||||
}
|
@ -27,6 +27,16 @@ extern "C"
|
||||
*/
|
||||
size_t storage_get_dev_name(char *dest);
|
||||
|
||||
/**
|
||||
* Write private key
|
||||
*/
|
||||
void storage_set_priv_key(unsigned char *key, size_t len);
|
||||
|
||||
/**
|
||||
* Get current private key
|
||||
*/
|
||||
size_t storage_get_priv_key(unsigned char *key);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue
Block a user