• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* BEGIN_HEADER */
2#include "mbedtls/pk.h"
3#include "mbedtls/pem.h"
4#include "mbedtls/oid.h"
5/* END_HEADER */
6
7/* BEGIN_DEPENDENCIES
8 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
9 * END_DEPENDENCIES
10 */
11
12/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
13void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
14{
15    mbedtls_pk_context ctx;
16    int res;
17    char *pwd = password;
18
19    mbedtls_pk_init(&ctx);
20    USE_PSA_INIT();
21
22    if (strcmp(pwd, "NULL") == 0) {
23        pwd = NULL;
24    }
25
26    res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd);
27
28    TEST_ASSERT(res == result);
29
30    if (res == 0) {
31        mbedtls_rsa_context *rsa;
32        TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
33        rsa = mbedtls_pk_rsa(ctx);
34        TEST_ASSERT(mbedtls_rsa_check_privkey(rsa) == 0);
35    }
36
37exit:
38    mbedtls_pk_free(&ctx);
39    USE_PSA_DONE();
40}
41/* END_CASE */
42
43/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
44void pk_parse_public_keyfile_rsa(char *key_file, int result)
45{
46    mbedtls_pk_context ctx;
47    int res;
48
49    mbedtls_pk_init(&ctx);
50    USE_PSA_INIT();
51
52    res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
53
54    TEST_ASSERT(res == result);
55
56    if (res == 0) {
57        mbedtls_rsa_context *rsa;
58        TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
59        rsa = mbedtls_pk_rsa(ctx);
60        TEST_ASSERT(mbedtls_rsa_check_pubkey(rsa) == 0);
61    }
62
63exit:
64    mbedtls_pk_free(&ctx);
65    USE_PSA_DONE();
66}
67/* END_CASE */
68
69/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
70void pk_parse_public_keyfile_ec(char *key_file, int result)
71{
72    mbedtls_pk_context ctx;
73    int res;
74
75    mbedtls_pk_init(&ctx);
76    USE_PSA_INIT();
77
78    res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
79
80    TEST_ASSERT(res == result);
81
82    if (res == 0) {
83        mbedtls_ecp_keypair *eckey;
84        TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
85        eckey = mbedtls_pk_ec(ctx);
86        TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0);
87    }
88
89exit:
90    mbedtls_pk_free(&ctx);
91    USE_PSA_DONE();
92}
93/* END_CASE */
94
95/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
96void pk_parse_keyfile_ec(char *key_file, char *password, int result)
97{
98    mbedtls_pk_context ctx;
99    int res;
100
101    mbedtls_pk_init(&ctx);
102    USE_PSA_INIT();
103
104    res = mbedtls_pk_parse_keyfile(&ctx, key_file, password);
105
106    TEST_ASSERT(res == result);
107
108    if (res == 0) {
109        mbedtls_ecp_keypair *eckey;
110        TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
111        eckey = mbedtls_pk_ec(ctx);
112        TEST_ASSERT(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d) == 0);
113    }
114
115exit:
116    mbedtls_pk_free(&ctx);
117    USE_PSA_DONE();
118}
119/* END_CASE */
120
121/* BEGIN_CASE */
122void pk_parse_key(data_t *buf, int result)
123{
124    mbedtls_pk_context pk;
125
126    mbedtls_pk_init(&pk);
127    USE_PSA_INIT();
128
129    TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0) == result);
130
131exit:
132    mbedtls_pk_free(&pk);
133    USE_PSA_DONE();
134}
135/* END_CASE */
136