blob: 0a275b5c8371054168b7793e8d751d4b2fb1c0fb [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
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010021 MD_PSA_INIT();
22
Gilles Peskine449bd832023-01-11 14:50:10 +010023 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020024
Gilles Peskine449bd832023-01-11 14:50:10 +010025 if (strcmp(pwd, "NULL") == 0) {
Paul Bakker1a7550a2013-09-15 13:01:22 +020026 pwd = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +010027 }
Paul Bakker1a7550a2013-09-15 13:01:22 +020028
Gilles Peskine449bd832023-01-11 14:50:10 +010029 res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd,
30 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +020031
Gilles Peskine449bd832023-01-11 14:50:10 +010032 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020033
Gilles Peskine449bd832023-01-11 14:50:10 +010034 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010036 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
37 rsa = mbedtls_pk_rsa(ctx);
38 TEST_ASSERT(mbedtls_rsa_check_privkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020039 }
40
Paul Bakkerbd51b262014-07-10 15:26:12 +020041exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010042 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010043 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020044}
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010045
Paul Bakker1a7550a2013-09-15 13:01:22 +020046/* END_CASE */
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010049void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020052 int res;
53
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010054 MD_PSA_INIT();
55
Gilles Peskine449bd832023-01-11 14:50:10 +010056 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020061
Gilles Peskine449bd832023-01-11 14:50:10 +010062 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010064 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
65 rsa = mbedtls_pk_rsa(ctx);
66 TEST_ASSERT(mbedtls_rsa_check_pubkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020067 }
68
Paul Bakkerbd51b262014-07-10 15:26:12 +020069exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010070 mbedtls_pk_free(&ctx);
Manuel Pégourié-Gonnardfa99afa2023-03-17 11:59:12 +010071 MD_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020072}
73/* END_CASE */
74
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010076void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020077{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020079 int res;
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020084
Gilles Peskine449bd832023-01-11 14:50:10 +010085 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020086
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +010089 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
90 eckey = mbedtls_pk_ec(ctx);
91 TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020092 }
93
Paul Bakkerbd51b262014-07-10 15:26:12 +020094exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010095 mbedtls_pk_free(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020096}
97/* END_CASE */
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100100void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200101{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200103 int res;
104
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);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200121}
122/* END_CASE */
123
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100124/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100125void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 mbedtls_pk_init(&pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
132 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133
Paul Bakkerbd51b262014-07-10 15:26:12 +0200134exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_pk_free(&pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200136}
137/* END_CASE */