2013-09-15 14:54:56 +02:00
|
|
|
/* BEGIN_HEADER */
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pk.h"
|
|
|
|
#include "mbedtls/pem.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2023-04-26 14:48:43 +02:00
|
|
|
#include "psa/crypto_sizes.h"
|
2013-09-15 14:54:56 +02:00
|
|
|
|
2023-04-18 11:20:36 +02:00
|
|
|
typedef enum {
|
|
|
|
TEST_PEM,
|
|
|
|
TEST_DER
|
|
|
|
} pkwrite_file_format_t;
|
|
|
|
|
2023-04-24 10:24:37 +02:00
|
|
|
/* Helper function for removing "\r" chars from a buffer. */
|
2023-04-24 08:52:16 +02:00
|
|
|
static void fix_new_lines(unsigned char *in_str, size_t *len)
|
|
|
|
{
|
|
|
|
size_t chars_left;
|
|
|
|
unsigned int i;
|
|
|
|
|
|
|
|
for (i = 0; (i < *len) && (*len > 0); i++) {
|
|
|
|
if (in_str[i] == '\r') {
|
|
|
|
if (i < (*len - 1)) {
|
|
|
|
chars_left = *len - i - 1;
|
2023-04-24 10:26:24 +02:00
|
|
|
memmove(&in_str[i], &in_str[i+1], chars_left);
|
2023-04-24 08:52:16 +02:00
|
|
|
} else {
|
|
|
|
in_str[i] = '\0';
|
|
|
|
}
|
|
|
|
*len = *len - 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-31 12:00:11 +02:00
|
|
|
static int pk_write_any_key(mbedtls_pk_context *pk, unsigned char **p,
|
|
|
|
size_t *buf_len, int is_public_key, int is_der)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
if (is_der) {
|
|
|
|
if (is_public_key) {
|
|
|
|
ret = mbedtls_pk_write_pubkey_der(pk, *p, *buf_len);
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_pk_write_key_der(pk, *p, *buf_len);
|
|
|
|
}
|
|
|
|
if (ret <= 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = *p + *buf_len - ret;
|
|
|
|
*buf_len = ret;
|
|
|
|
} else {
|
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
|
|
|
if (is_public_key) {
|
|
|
|
ret = mbedtls_pk_write_pubkey_pem(pk, *p, *buf_len);
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_pk_write_key_pem(pk, *p, *buf_len);
|
|
|
|
}
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
*buf_len = strlen((char *) *p) + 1; /* +1 takes the string terminator into account */
|
|
|
|
#else
|
|
|
|
return MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-04-17 18:43:06 +02:00
|
|
|
static void pk_write_check_common(char *key_file, int is_public_key, int is_der)
|
2013-09-15 14:54:56 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_context key;
|
2023-04-17 17:34:09 +02:00
|
|
|
unsigned char *buf = NULL;
|
|
|
|
unsigned char *check_buf = NULL;
|
2023-04-17 18:43:06 +02:00
|
|
|
unsigned char *start_buf;
|
2023-04-18 10:42:53 +02:00
|
|
|
size_t buf_len, check_buf_len;
|
2023-05-31 12:00:11 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
mbedtls_svc_key_id_t opaque_id = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
|
|
|
USE_PSA_INIT();
|
2013-09-15 14:54:56 +02:00
|
|
|
|
2023-04-24 13:53:21 +02:00
|
|
|
mbedtls_pk_init(&key);
|
|
|
|
USE_PSA_INIT();
|
|
|
|
|
2023-04-17 17:34:09 +02:00
|
|
|
/* Note: if mbedtls_pk_load_file() successfully reads the file, then
|
|
|
|
it also allocates check_buf, which should be freed on exit */
|
2023-04-18 10:42:53 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
|
2023-04-17 17:34:09 +02:00
|
|
|
TEST_ASSERT(check_buf_len > 0);
|
2013-09-15 14:54:56 +02:00
|
|
|
|
2023-04-24 08:52:16 +02:00
|
|
|
/* Windows' line ending is different from the Linux's one ("\r\n" vs "\n").
|
|
|
|
* Git treats PEM files as text, so when on Windows, it replaces new lines
|
|
|
|
* with "\r\n" on checkout.
|
|
|
|
* Unfortunately mbedtls_pk_load_file() loads files in binary format,
|
|
|
|
* while mbedtls_pk_write_pubkey_pem() goes through the I/O layer which
|
|
|
|
* uses "\n" for newlines in both Windows and Linux.
|
|
|
|
* Here we remove the extra "\r" so that "buf" and "check_buf" can be
|
|
|
|
* easily compared later. */
|
|
|
|
if (!is_der) {
|
|
|
|
fix_new_lines(check_buf, &check_buf_len);
|
|
|
|
}
|
|
|
|
TEST_ASSERT(check_buf_len > 0);
|
|
|
|
|
2023-04-17 17:34:09 +02:00
|
|
|
ASSERT_ALLOC(buf, check_buf_len);
|
2013-09-15 14:54:56 +02:00
|
|
|
|
2023-04-17 17:34:09 +02:00
|
|
|
if (is_public_key) {
|
2023-04-18 10:42:53 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_parse_public_keyfile(&key, key_file), 0);
|
2023-04-17 17:34:09 +02:00
|
|
|
} else {
|
2023-04-18 10:42:53 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
|
2023-04-18 12:57:52 +02:00
|
|
|
mbedtls_test_rnd_std_rand, NULL), 0);
|
2020-11-19 10:46:56 +01:00
|
|
|
}
|
|
|
|
|
2023-05-31 12:00:11 +02:00
|
|
|
start_buf = buf;
|
|
|
|
buf_len = check_buf_len;
|
|
|
|
TEST_EQUAL(pk_write_any_key(&key, &start_buf, &buf_len, is_public_key,
|
|
|
|
is_der), 0);
|
2023-04-17 18:43:06 +02:00
|
|
|
|
2023-04-18 10:42:53 +02:00
|
|
|
ASSERT_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
|
2013-09-15 14:54:56 +02:00
|
|
|
|
2023-05-31 12:00:11 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
/* Verify that pk_write works also for opaque private keys */
|
|
|
|
if (!is_public_key) {
|
|
|
|
memset(buf, 0, check_buf_len);
|
|
|
|
TEST_EQUAL(mbedtls_pk_wrap_as_opaque(&key, &opaque_id,
|
|
|
|
PSA_ALG_NONE,
|
|
|
|
PSA_KEY_USAGE_EXPORT,
|
|
|
|
PSA_ALG_NONE), 0);
|
|
|
|
start_buf = buf;
|
|
|
|
buf_len = check_buf_len;
|
|
|
|
TEST_EQUAL(pk_write_any_key(&key, &start_buf, &buf_len, is_public_key,
|
|
|
|
is_der), 0);
|
|
|
|
|
|
|
|
ASSERT_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
2023-05-31 12:00:11 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_destroy_key(opaque_id);
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-04-17 17:34:09 +02:00
|
|
|
mbedtls_free(buf);
|
|
|
|
mbedtls_free(check_buf);
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&key);
|
2023-04-24 13:53:21 +02:00
|
|
|
USE_PSA_DONE();
|
2013-09-15 14:54:56 +02:00
|
|
|
}
|
2023-04-17 17:34:09 +02:00
|
|
|
/* END_HEADER */
|
|
|
|
|
|
|
|
/* BEGIN_DEPENDENCIES
|
|
|
|
* depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO
|
|
|
|
* END_DEPENDENCIES
|
|
|
|
*/
|
|
|
|
|
2023-04-18 11:25:30 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-04-17 18:43:06 +02:00
|
|
|
void pk_write_pubkey_check(char *key_file, int is_der)
|
2023-04-17 17:34:09 +02:00
|
|
|
{
|
2023-04-17 18:43:06 +02:00
|
|
|
pk_write_check_common(key_file, 1, is_der);
|
2023-04-17 17:34:09 +02:00
|
|
|
goto exit; /* make the compiler happy */
|
|
|
|
}
|
2013-09-15 14:54:56 +02:00
|
|
|
/* END_CASE */
|
|
|
|
|
2023-04-18 11:25:30 +02:00
|
|
|
/* BEGIN_CASE */
|
2023-04-17 18:43:06 +02:00
|
|
|
void pk_write_key_check(char *key_file, int is_der)
|
2013-09-15 14:54:56 +02:00
|
|
|
{
|
2023-04-17 18:43:06 +02:00
|
|
|
pk_write_check_common(key_file, 0, is_der);
|
2023-04-17 17:34:09 +02:00
|
|
|
goto exit; /* make the compiler happy */
|
2013-09-15 14:54:56 +02:00
|
|
|
}
|
|
|
|
/* END_CASE */
|
2023-04-26 14:48:43 +02:00
|
|
|
|
|
|
|
/* BEGIN_CASE */
|
2023-04-27 10:52:57 +02:00
|
|
|
void pk_write_public_from_private(char *priv_key_file, char *pub_key_file)
|
2023-04-26 14:48:43 +02:00
|
|
|
{
|
2023-04-27 10:52:57 +02:00
|
|
|
mbedtls_pk_context priv_key;
|
|
|
|
uint8_t *derived_key_raw = NULL;
|
2023-04-26 14:48:43 +02:00
|
|
|
size_t derived_key_len = 0;
|
2023-04-27 10:52:57 +02:00
|
|
|
uint8_t *pub_key_raw = NULL;
|
2023-04-26 14:48:43 +02:00
|
|
|
size_t pub_key_len = 0;
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
mbedtls_svc_key_id_t opaque_key_id = MBEDTLS_SVC_KEY_ID_INIT;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
|
|
|
mbedtls_pk_init(&priv_key);
|
|
|
|
USE_PSA_INIT();
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_pk_parse_keyfile(&priv_key, priv_key_file, NULL,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL), 0);
|
2023-04-27 10:52:57 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_load_file(pub_key_file, &pub_key_raw,
|
|
|
|
&pub_key_len), 0);
|
2023-04-26 14:48:43 +02:00
|
|
|
|
2023-04-27 10:52:57 +02:00
|
|
|
derived_key_len = pub_key_len;
|
|
|
|
ASSERT_ALLOC(derived_key_raw, derived_key_len);
|
2023-04-26 14:48:43 +02:00
|
|
|
|
2023-04-27 12:07:23 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
|
|
|
|
derived_key_len), pub_key_len);
|
2023-04-26 14:48:43 +02:00
|
|
|
|
2023-04-27 10:52:57 +02:00
|
|
|
ASSERT_COMPARE(derived_key_raw, derived_key_len,
|
|
|
|
pub_key_raw, pub_key_len);
|
2023-04-26 14:48:43 +02:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
mbedtls_platform_zeroize(derived_key_raw, sizeof(derived_key_raw));
|
|
|
|
|
|
|
|
TEST_EQUAL(mbedtls_pk_wrap_as_opaque(&priv_key, &opaque_key_id,
|
|
|
|
PSA_ALG_NONE, PSA_KEY_USAGE_EXPORT,
|
|
|
|
PSA_ALG_NONE), 0);
|
|
|
|
|
2023-04-27 12:07:23 +02:00
|
|
|
TEST_EQUAL(mbedtls_pk_write_pubkey_der(&priv_key, derived_key_raw,
|
|
|
|
derived_key_len), pub_key_len);
|
2023-04-26 14:48:43 +02:00
|
|
|
|
2023-04-27 10:52:57 +02:00
|
|
|
ASSERT_COMPARE(derived_key_raw, derived_key_len,
|
|
|
|
pub_key_raw, pub_key_len);
|
2023-04-26 14:48:43 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
|
|
|
exit:
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_destroy_key(opaque_key_id);
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-04-27 10:52:57 +02:00
|
|
|
mbedtls_free(derived_key_raw);
|
|
|
|
mbedtls_free(pub_key_raw);
|
2023-04-26 14:48:43 +02:00
|
|
|
mbedtls_pk_free(&priv_key);
|
|
|
|
USE_PSA_DONE();
|
|
|
|
}
|
|
|
|
/* END_CASE */
|