blob: a49b6d31959cf8869aa4792bc4e98452781a2e44 [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"
Valerio Setti77a75682023-05-15 11:18:46 +02006#include "pk_internal.h"
Paul Bakker1a7550a2013-09-15 13:01:22 +02007/* END_HEADER */
8
9/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010 * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C
Paul Bakker1a7550a2013-09-15 13:01:22 +020011 * END_DEPENDENCIES
12 */
13
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010015void pk_parse_keyfile_rsa(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020018 int res;
19 char *pwd = password;
20
Gilles Peskine449bd832023-01-11 14:50:10 +010021 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020022 MD_PSA_INIT();
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
Gilles Peskine449bd832023-01-11 14:50:10 +010053 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020054 MD_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +020055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010062 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
63 rsa = mbedtls_pk_rsa(ctx);
64 TEST_ASSERT(mbedtls_rsa_check_pubkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020065 }
66
Paul Bakkerbd51b262014-07-10 15:26:12 +020067exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010068 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010069 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020070}
71/* END_CASE */
72
Valerio Setti6c496a12023-04-07 15:53:51 +020073/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +010074void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020077 int res;
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020080 USE_PSA_INIT();
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) {
Gilles Peskine449bd832023-01-11 14:50:10 +010087 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti483738e2023-05-17 15:37:29 +020088#if defined(MBEDTLS_PK_USE_PSA_EC_DATA)
89 /* No need to check whether the parsed public point is on the curve or
90 * not because this is already done by the internal "pk_get_ecpubkey()"
91 * function */
92#else
93 const mbedtls_ecp_keypair *eckey;
Valerio Setti77a75682023-05-15 11:18:46 +020094 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +010095 TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0);
Valerio Setti483738e2023-05-17 15:37:29 +020096#endif
Paul Bakker1a7550a2013-09-15 13:01:22 +020097 }
98
Paul Bakkerbd51b262014-07-10 15:26:12 +020099exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_pk_free(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200101 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102}
103/* END_CASE */
104
Valerio Setti6c496a12023-04-07 15:53:51 +0200105/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_LIGHT */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200109 int res;
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_pk_init(&ctx);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200112 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
115 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (res == 0) {
Valerio Setti77a75682023-05-15 11:18:46 +0200120 const mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti77a75682023-05-15 11:18:46 +0200122 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 TEST_ASSERT(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124 }
125
Paul Bakkerbd51b262014-07-10 15:26:12 +0200126exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 mbedtls_pk_free(&ctx);
Valerio Setti3fddf252023-04-04 10:49:28 +0200128 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129}
130/* END_CASE */
131
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100132/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100133void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200134{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200138 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
141 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200142
Paul Bakkerbd51b262014-07-10 15:26:12 +0200143exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200145 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200146}
147/* END_CASE */