blob: ff19981e03adbaf409bad46e11b1b0b26f953430 [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"
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +02005#include "mbedtls/legacy_or_psa.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
Andrzej Kurek7a320722022-09-01 09:23:09 -040020 PSA_INIT_IF_NO_MD();
Gilles Peskine449bd832023-01-11 14:50:10 +010021 mbedtls_pk_init(&ctx);
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);
Andrzej Kurek7a320722022-09-01 09:23:09 -040041 PSA_DONE_IF_NO_MD();
Paul Bakker1a7550a2013-09-15 13:01:22 +020042}
43/* END_CASE */
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045/* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */
Gilles Peskine449bd832023-01-11 14:50:10 +010046void pk_parse_public_keyfile_rsa(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020047{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020049 int res;
50
Andrzej Kurek7a320722022-09-01 09:23:09 -040051 PSA_INIT_IF_NO_MD();
Gilles Peskine449bd832023-01-11 14:50:10 +010052 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020055
Gilles Peskine449bd832023-01-11 14:50:10 +010056 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_rsa_context *rsa;
Gilles Peskine449bd832023-01-11 14:50:10 +010060 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_RSA));
61 rsa = mbedtls_pk_rsa(ctx);
62 TEST_ASSERT(mbedtls_rsa_check_pubkey(rsa) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020063 }
64
Paul Bakkerbd51b262014-07-10 15:26:12 +020065exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010066 mbedtls_pk_free(&ctx);
Andrzej Kurek7a320722022-09-01 09:23:09 -040067 PSA_DONE_IF_NO_MD();
Paul Bakker1a7550a2013-09-15 13:01:22 +020068}
69/* END_CASE */
70
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010072void pk_parse_public_keyfile_ec(char *key_file, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020075 int res;
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020078
Gilles Peskine449bd832023-01-11 14:50:10 +010079 res = mbedtls_pk_parse_public_keyfile(&ctx, key_file);
Paul Bakker1a7550a2013-09-15 13:01:22 +020080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +020082
Gilles Peskine449bd832023-01-11 14:50:10 +010083 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +010085 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
86 eckey = mbedtls_pk_ec(ctx);
87 TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +020088 }
89
Paul Bakkerbd51b262014-07-10 15:26:12 +020090exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010091 mbedtls_pk_free(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +020092}
93/* END_CASE */
94
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_C */
Gilles Peskine449bd832023-01-11 14:50:10 +010096void pk_parse_keyfile_ec(char *key_file, char *password, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +020097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 mbedtls_pk_context ctx;
Paul Bakker1a7550a2013-09-15 13:01:22 +020099 int res;
100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 mbedtls_pk_init(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200102
Gilles Peskine449bd832023-01-11 14:50:10 +0100103 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
104 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if (res == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
111 eckey = mbedtls_pk_ec(ctx);
112 TEST_ASSERT(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200113 }
114
Paul Bakkerbd51b262014-07-10 15:26:12 +0200115exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_pk_free(&ctx);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200117}
118/* END_CASE */
119
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100120/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100121void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200122{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 mbedtls_pk_init(&pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
128 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200129
Paul Bakkerbd51b262014-07-10 15:26:12 +0200130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_pk_free(&pk);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200132}
133/* END_CASE */