blob: e0e33000da5445e5da72841bd68322c638706ca0 [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) {
Valerio Setti77a75682023-05-15 11:18:46 +020087 const mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +010088 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti77a75682023-05-15 11:18:46 +020089 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +010090 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);
Valerio Setti14bfdbf2023-04-24 13:53:21 +020095 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +020096}
97/* END_CASE */
98
Valerio Setti6c496a12023-04-07 15:53:51 +020099/* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_LIGHT */
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);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200106 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 res = mbedtls_pk_parse_keyfile(&ctx, key_file, password,
109 mbedtls_test_rnd_std_rand, NULL);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 TEST_ASSERT(res == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200112
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (res == 0) {
Valerio Setti77a75682023-05-15 11:18:46 +0200114 const mbedtls_ecp_keypair *eckey;
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY));
Valerio Setti77a75682023-05-15 11:18:46 +0200116 eckey = mbedtls_pk_ec_ro(ctx);
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 TEST_ASSERT(mbedtls_ecp_check_privkey(&eckey->grp, &eckey->d) == 0);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200118 }
119
Paul Bakkerbd51b262014-07-10 15:26:12 +0200120exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 mbedtls_pk_free(&ctx);
Valerio Setti3fddf252023-04-04 10:49:28 +0200122 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200123}
124/* END_CASE */
125
Manuel Pégourié-Gonnardb65370f2020-02-10 10:50:16 +0100126/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127void pk_parse_key(data_t *buf, int result)
Paul Bakker1a7550a2013-09-15 13:01:22 +0200128{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129 mbedtls_pk_context pk;
Paul Bakker1a7550a2013-09-15 13:01:22 +0200130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_pk_init(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200132 USE_PSA_INIT();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0,
135 mbedtls_test_rnd_std_rand, NULL) == result);
Paul Bakker1a7550a2013-09-15 13:01:22 +0200136
Paul Bakkerbd51b262014-07-10 15:26:12 +0200137exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 mbedtls_pk_free(&pk);
Valerio Setti14bfdbf2023-04-24 13:53:21 +0200139 USE_PSA_DONE();
Paul Bakker1a7550a2013-09-15 13:01:22 +0200140}
141/* END_CASE */