| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/pk.h" | 
|  | 3 | #include "mbedtls/pem.h" | 
|  | 4 | #include "mbedtls/oid.h" | 
| Valerio Setti | fa49a8e | 2023-01-26 10:00:55 +0100 | [diff] [blame] | 5 | #include "mbedtls/ecp.h" | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 6 | /* END_HEADER */ | 
|  | 7 |  | 
|  | 8 | /* BEGIN_DEPENDENCIES | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 9 | * depends_on:MBEDTLS_PK_PARSE_C:MBEDTLS_BIGNUM_C | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 10 | * END_DEPENDENCIES | 
|  | 11 | */ | 
|  | 12 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 13 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 14 | void pk_parse_keyfile_rsa(char *key_file, char *password, int result) | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 15 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 16 | mbedtls_pk_context ctx; | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 17 | int res; | 
|  | 18 | char *pwd = password; | 
|  | 19 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 20 | mbedtls_pk_init(&ctx); | 
| Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 21 | MD_PSA_INIT(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 22 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 23 | if (strcmp(pwd, "NULL") == 0) { | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 24 | pwd = NULL; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 25 | } | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 26 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 27 | res = mbedtls_pk_parse_keyfile(&ctx, key_file, pwd, | 
|  | 28 | mbedtls_test_rnd_std_rand, NULL); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 29 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 30 | TEST_ASSERT(res == result); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 31 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 32 | if (res == 0) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | mbedtls_rsa_context *rsa; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 34 | 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 Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 39 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | mbedtls_pk_free(&ctx); | 
| Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 41 | MD_PSA_DONE(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 42 | } | 
| Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 43 |  | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 44 | /* END_CASE */ | 
|  | 45 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 46 | /* BEGIN_CASE depends_on:MBEDTLS_RSA_C:MBEDTLS_FS_IO */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | void pk_parse_public_keyfile_rsa(char *key_file, int result) | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 48 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | mbedtls_pk_context ctx; | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 50 | int res; | 
|  | 51 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 52 | mbedtls_pk_init(&ctx); | 
| Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 53 | MD_PSA_INIT(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 54 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 55 | res = mbedtls_pk_parse_public_keyfile(&ctx, key_file); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 56 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 57 | TEST_ASSERT(res == result); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 58 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | if (res == 0) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | mbedtls_rsa_context *rsa; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | 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 Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 64 | } | 
|  | 65 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 66 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 67 | mbedtls_pk_free(&ctx); | 
| Manuel Pégourié-Gonnard | fa99afa | 2023-03-17 11:59:12 +0100 | [diff] [blame] | 68 | MD_PSA_DONE(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 69 | } | 
|  | 70 | /* END_CASE */ | 
|  | 71 |  | 
| Valerio Setti | 6c496a1 | 2023-04-07 15:53:51 +0200 | [diff] [blame] | 72 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_LIGHT */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 73 | void pk_parse_public_keyfile_ec(char *key_file, int result) | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 74 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | mbedtls_pk_context ctx; | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 76 | int res; | 
|  | 77 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 78 | mbedtls_pk_init(&ctx); | 
| Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 79 | USE_PSA_INIT(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 80 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 81 | res = mbedtls_pk_parse_public_keyfile(&ctx, key_file); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 82 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | TEST_ASSERT(res == result); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 84 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | if (res == 0) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | mbedtls_ecp_keypair *eckey; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 87 | TEST_ASSERT(mbedtls_pk_can_do(&ctx, MBEDTLS_PK_ECKEY)); | 
|  | 88 | eckey = mbedtls_pk_ec(ctx); | 
|  | 89 | TEST_ASSERT(mbedtls_ecp_check_pubkey(&eckey->grp, &eckey->Q) == 0); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 90 | } | 
|  | 91 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 92 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | mbedtls_pk_free(&ctx); | 
| Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 94 | USE_PSA_DONE(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 95 | } | 
|  | 96 | /* END_CASE */ | 
|  | 97 |  | 
| Valerio Setti | 6c496a1 | 2023-04-07 15:53:51 +0200 | [diff] [blame] | 98 | /* BEGIN_CASE depends_on:MBEDTLS_FS_IO:MBEDTLS_ECP_LIGHT */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 99 | void pk_parse_keyfile_ec(char *key_file, char *password, int result) | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 100 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | mbedtls_pk_context ctx; | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 102 | int res; | 
|  | 103 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | mbedtls_pk_init(&ctx); | 
| Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 105 | USE_PSA_INIT(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 106 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | res = mbedtls_pk_parse_keyfile(&ctx, key_file, password, | 
|  | 108 | mbedtls_test_rnd_std_rand, NULL); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 109 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | TEST_ASSERT(res == result); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 111 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | if (res == 0) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | mbedtls_ecp_keypair *eckey; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | 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 Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 117 | } | 
|  | 118 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 119 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 120 | mbedtls_pk_free(&ctx); | 
| Valerio Setti | 3fddf25 | 2023-04-04 10:49:28 +0200 | [diff] [blame] | 121 | USE_PSA_DONE(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 122 | } | 
|  | 123 | /* END_CASE */ | 
|  | 124 |  | 
| Manuel Pégourié-Gonnard | b65370f | 2020-02-10 10:50:16 +0100 | [diff] [blame] | 125 | /* BEGIN_CASE */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | void pk_parse_key(data_t *buf, int result) | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 127 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | mbedtls_pk_context pk; | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 129 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | mbedtls_pk_init(&pk); | 
| Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 131 | USE_PSA_INIT(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 132 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | TEST_ASSERT(mbedtls_pk_parse_key(&pk, buf->x, buf->len, NULL, 0, | 
|  | 134 | mbedtls_test_rnd_std_rand, NULL) == result); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 135 |  | 
| Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 136 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | mbedtls_pk_free(&pk); | 
| Valerio Setti | 14bfdbf | 2023-04-24 13:53:21 +0200 | [diff] [blame] | 138 | USE_PSA_DONE(); | 
| Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 139 | } | 
|  | 140 | /* END_CASE */ |