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"
|
2013-09-15 14:54:56 +02:00
|
|
|
|
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-17 17:34:09 +02:00
|
|
|
size_t check_buf_len;
|
2013-09-15 14:54:56 +02:00
|
|
|
int ret;
|
|
|
|
|
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 */
|
|
|
|
TEST_ASSERT(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len) == 0);
|
|
|
|
TEST_ASSERT(check_buf_len > 0);
|
2013-09-15 14:54:56 +02:00
|
|
|
|
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
|
|
|
mbedtls_pk_init(&key);
|
|
|
|
if (is_public_key) {
|
|
|
|
TEST_ASSERT(mbedtls_pk_parse_public_keyfile(&key, key_file) == 0);
|
2023-04-17 18:43:06 +02:00
|
|
|
if (is_der) {
|
|
|
|
ret = mbedtls_pk_write_pubkey_der(&key, buf, check_buf_len);
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_pk_write_pubkey_pem(&key, buf, check_buf_len);
|
|
|
|
}
|
2023-04-17 17:34:09 +02:00
|
|
|
} else {
|
|
|
|
TEST_ASSERT(mbedtls_pk_parse_keyfile(&key, key_file, NULL,
|
|
|
|
mbedtls_test_rnd_std_rand, NULL) == 0);
|
2023-04-17 18:43:06 +02:00
|
|
|
if (is_der) {
|
|
|
|
ret = mbedtls_pk_write_key_der(&key, buf, check_buf_len);
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_pk_write_key_pem(&key, buf, check_buf_len);
|
|
|
|
}
|
2020-11-19 10:46:56 +01:00
|
|
|
}
|
|
|
|
|
2023-04-17 18:43:06 +02:00
|
|
|
if (is_der) {
|
|
|
|
TEST_ASSERT(ret > 0);
|
|
|
|
start_buf = buf + check_buf_len - ret;
|
|
|
|
} else {
|
|
|
|
TEST_ASSERT(ret == 0);
|
|
|
|
start_buf = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_ASSERT(memcmp((char *) start_buf, (char *) check_buf, check_buf_len) == 0);
|
2013-09-15 14:54:56 +02:00
|
|
|
|
2014-07-10 15:26:12 +02:00
|
|
|
exit:
|
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);
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
|
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 */
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
/* BEGIN_CASE depends_on:MBEDTLS_PEM_WRITE_C */
|
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 */
|