blob: 0856f3f38c854aac1fb01cbd01db914bf665b8b0 [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"
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +02006#include "mbedtls/legacy_or_psa.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
Andrzej Kurek7a320722022-09-01 09:23:09 -040021 PSA_INIT_IF_NO_MD();
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);
Andrzej Kurek7a320722022-09-01 09:23:09 -040042 PSA_DONE_IF_NO_MD();
Paul Bakker1a7550a2013-09-15 13:01:22 +020043}
44/* 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
Andrzej Kurek7a320722022-09-01 09:23:09 -040052 PSA_INIT_IF_NO_MD();
Gilles Peskine449bd832023-01-11 14:50:10 +010053 mbedtls_pk_init(&ctx);
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);
Andrzej Kurek7a320722022-09-01 09:23:09 -040068 PSA_DONE_IF_NO_MD();
Paul Bakker1a7550a2013-09-15 13:01:22 +020069}
70/* END_CASE */
71
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
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);
Paul Bakker1a7550a2013-09-15 13:01:22 +020079
Gilles Peskine449bd832023-01-11 14:50:10 +010080 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020081
Gilles Peskine449bd832023-01-11 14:50:10 +010082 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020083
Gilles Peskine449bd832023-01-11 14:50:10 +010084 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +010086 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
87 eckey = mbedtls_pk_ec(ctx);
88 TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020089 }
90
Paul Bakkerbd51b262014-07-10 15:26:12 +020091exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_pk_free(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020093}
94/* END_CASE */
95
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010097void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020098{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200100 int res;
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
105 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200106
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110 mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
112 eckey = mbedtls_pk_ec(ctx);
113 TEST_ASSERT(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200114 }
115
Paul Bakkerbd51b262014-07-10 15:26:12 +0200116exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 mbedtls_pk_free(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118}
119/* END_CASE */
120
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100121/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200123{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 mbedtls_pk_init(&pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
129 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130
Paul Bakkerbd51b262014-07-10 15:26:12 +0200131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 mbedtls_pk_free(&pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133}
134/* END_CASE */