blob: 751482a9e7d60dbab915c92ffa5412b8dbd6feca [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
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010020 MD_PSA_INIT();
21
Gilles Peskine449bd832023-01-11 14:50:10 +010022 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020023
Gilles Peskine449bd832023-01-11 14:50:10 +010024 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +020025 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010026 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020027
Gilles Peskine449bd832023-01-11 14:50:10 +010028 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
29 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +020030
Gilles Peskine449bd832023-01-11 14:50:10 +010031 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020032
Gilles Peskine449bd832023-01-11 14:50:10 +010033 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010035 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
36 rsa = mbedtls_pk_rsa(ctx);
37 TEST_ASSERT(mbedtls_rsa_check_privkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020038 }
39
Paul Bakkerbd51b262014-07-10 15:26:12 +020040exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010041 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010042 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020043}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010044
Paul Bakker1a7550a2013-09-15 13:01:22 +020045/* END_CASE */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010048void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020051 int res;
52
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010053 MD_PSA_INIT();
54
Gilles Peskine449bd832023-01-11 14:50:10 +010055 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020058
Gilles Peskine449bd832023-01-11 14:50:10 +010059 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020060
Gilles Peskine449bd832023-01-11 14:50:10 +010061 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010063 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
64 rsa = mbedtls_pk_rsa(ctx);
65 TEST_ASSERT(mbedtls_rsa_check_pubkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020066 }
67
Paul Bakkerbd51b262014-07-10 15:26:12 +020068exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010069 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010070 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020071}
72/* END_CASE */
73
Valerio Setti6c496a12023-04-07 15:53:51 +020074/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +010075void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020076{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020078 int res;
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +010088 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
89 eckey = mbedtls_pk_ec(ctx);
90 TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020091 }
92
Paul Bakkerbd51b262014-07-10 15:26:12 +020093exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_pk_free(&ctx);
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
Valerio Setti3fddf252023-04-04 10:49:28 +0200104 USE_PSA_INIT();
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_pk_init(&ctx);
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);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
133 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134
Paul Bakkerbd51b262014-07-10 15:26:12 +0200135exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 mbedtls_pk_free(&pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200137}
138/* END_CASE */