blob: 60ac4000479db5fc8d194a1e8d1935435c58ec77 [file] [log] [blame]
Paul Bakkerc7bb02b2013-09-15 14:54:56 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/pk.h"
3#include "mbedtls/pem.h"
4#include "mbedtls/oid.h"
Valerio Settif1477da2023-04-18 16:37:30 +02005
6typedef enum {
7 TEST_PEM,
8 TEST_DER
9} pkwrite_file_format_t;
10
11static void pk_write_check_common(char *key_file, int is_public_key, int is_der)
12{
13 mbedtls_pk_context key;
14 unsigned char *buf = NULL;
15 unsigned char *check_buf = NULL;
16 unsigned char *start_buf;
17 size_t buf_len, check_buf_len;
18 int ret;
19
20 /* Note: if mbedtls_pk_load_file() successfully reads the file, then
21 it also allocates check_buf, which should be freed on exit */
22 TEST_EQUAL(mbedtls_pk_load_file(key_file, &check_buf, &check_buf_len), 0);
23 TEST_ASSERT(check_buf_len > 0);
24
25 ASSERT_ALLOC(buf, check_buf_len);
26
27 mbedtls_pk_init(&key);
28 if (is_public_key) {
29 TEST_EQUAL(mbedtls_pk_parse_public_keyfile(&key, key_file), 0);
30 if (is_der) {
31 ret = mbedtls_pk_write_pubkey_der(&key, buf, check_buf_len);
32 } else {
33#if defined(MBEDTLS_PEM_WRITE_C)
34 ret = mbedtls_pk_write_pubkey_pem(&key, buf, check_buf_len);
35#else
36 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
37#endif
38 }
39 } else {
40 TEST_EQUAL(mbedtls_pk_parse_keyfile(&key, key_file, NULL), 0);
41 if (is_der) {
42 ret = mbedtls_pk_write_key_der(&key, buf, check_buf_len);
43 } else {
44#if defined(MBEDTLS_PEM_WRITE_C)
45 ret = mbedtls_pk_write_key_pem(&key, buf, check_buf_len);
46#else
47 ret = MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE;
48#endif
49 }
50 }
51
52 if (is_der) {
53 TEST_LE_U(1, ret);
54 buf_len = ret;
55 start_buf = buf + check_buf_len - buf_len;
56 } else {
57 TEST_EQUAL(ret, 0);
58 buf_len = strlen((char *) buf) + 1; /* +1 takes the string terminator into account */
59 start_buf = buf;
60 }
61
62 ASSERT_COMPARE(start_buf, buf_len, check_buf, check_buf_len);
63
64exit:
65 mbedtls_free(buf);
66 mbedtls_free(check_buf);
67 mbedtls_pk_free(&key);
68}
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020069/* END_HEADER */
70
71/* BEGIN_DEPENDENCIES
Hanno Becker19d858e2018-10-16 13:46:25 +010072 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_PK_WRITE_C:MBEDTLS_BIGNUM_C:MBEDTLS_FS_IO
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020073 * END_DEPENDENCIES
74 */
75
Valerio Settif1477da2023-04-18 16:37:30 +020076/* BEGIN_CASE */
77void pk_write_pubkey_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020078{
Valerio Settif1477da2023-04-18 16:37:30 +020079 pk_write_check_common(key_file, 1, is_der);
80 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020081}
82/* END_CASE */
83
Valerio Settif1477da2023-04-18 16:37:30 +020084/* BEGIN_CASE */
85void pk_write_key_check(char *key_file, int is_der)
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020086{
Valerio Settif1477da2023-04-18 16:37:30 +020087 pk_write_check_common(key_file, 0, is_der);
88 goto exit; /* make the compiler happy */
Paul Bakkerc7bb02b2013-09-15 14:54:56 +020089}
90/* END_CASE */