blob: 838a7dba7ff410d8888929ad713c395846614185 [file] [log] [blame]
Paul Bakker1a7550a2013-09-15 13:01:22 +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 Settifa49a8e2023-01-26 10:00:55 +01005#include "mbedtls/ecp.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02006/* END_HEADER */
7
8/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020010 * END_DEPENDENCIES
11 */
12
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020013/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010014void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020017 int res;
18 char *pwd = password;
19
Gilles Peskine449bd832023-01-11 14:50:10 +010020 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020021 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020022
Gilles Peskine449bd832023-01-11 14:50:10 +010023 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +020024 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010025 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020026
Gilles Peskine449bd832023-01-11 14:50:10 +010027 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
28 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +020029
Gilles Peskine449bd832023-01-11 14:50:10 +010030 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020031
Gilles Peskine449bd832023-01-11 14:50:10 +010032 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010034 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
35 rsa = mbedtls_pk_rsa(ctx);
36 TEST_ASSERT(mbedtls_rsa_check_privkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020037 }
38
Paul Bakkerbd51b262014-07-10 15:26:12 +020039exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010040 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010041 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020042}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010043
Paul Bakker1a7550a2013-09-15 13:01:22 +020044/* END_CASE */
45
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010047void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020050 int res;
51
Gilles Peskine449bd832023-01-11 14:50:10 +010052 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020053 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020054
Gilles Peskine449bd832023-01-11 14:50:10 +010055 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020058
Gilles Peskine449bd832023-01-11 14:50:10 +010059 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010061 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
62 rsa = mbedtls_pk_rsa(ctx);
63 TEST_ASSERT(mbedtls_rsa_check_pubkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020064 }
65
Paul Bakkerbd51b262014-07-10 15:26:12 +020066exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010067 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010068 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020069}
70/* END_CASE */
71
Valerio Setti6c496a12023-04-07 15:53:51 +020072/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +010073void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020074{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020076 int res;
77
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020079 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +010087 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
88 eckey = mbedtls_pk_ec(ctx);
89 TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020090 }
91
Paul Bakkerbd51b262014-07-10 15:26:12 +020092exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010093 mbedtls_pk_free(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020094 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020095}
96/* END_CASE */
97
Valerio Setti6c496a12023-04-07 15:53:51 +020098/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +010099void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200100{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102 int res;
103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200105 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
108 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
115 eckey = mbedtls_pk_ec(ctx);
116 TEST_ASSERT(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117 }
118
Paul Bakkerbd51b262014-07-10 15:26:12 +0200119exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 mbedtls_pk_free(&ctx);
Valerio Setti3fddf252023-04-04 10:49:28 +0200121 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200122}
123/* END_CASE */
124
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100125/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100126void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200131 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
134 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200135
Paul Bakkerbd51b262014-07-10 15:26:12 +0200136exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200138 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139}
140/* END_CASE */