Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Key reading application |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 22 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #endif |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 26 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_BIGNUM_C) && \ |
| 29 | defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/error.h" |
| 31 | #include "mbedtls/rsa.h" |
Jaeden Amero | ed73699 | 2018-10-26 16:55:14 +0100 | [diff] [blame] | 32 | #include "mbedtls/pk.h" |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 33 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #include <string.h> |
| 35 | #endif |
| 36 | |
| 37 | #define MODE_NONE 0 |
| 38 | #define MODE_PRIVATE 1 |
| 39 | #define MODE_PUBLIC 2 |
| 40 | |
| 41 | #define DFL_MODE MODE_NONE |
| 42 | #define DFL_FILENAME "keyfile.key" |
| 43 | #define DFL_PASSWORD "" |
| 44 | #define DFL_PASSWORD_FILE "" |
| 45 | #define DFL_DEBUG_LEVEL 0 |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 46 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 47 | #define USAGE \ |
| 48 | "\n usage: key_app param=<>...\n" \ |
| 49 | "\n acceptable parameters:\n" \ |
| 50 | " mode=private|public default: none\n" \ |
| 51 | " filename=%%s default: keyfile.key\n" \ |
| 52 | " password=%%s default: \"\"\n" \ |
| 53 | " password_file=%%s default: \"\"\n" \ |
| 54 | "\n" |
| 55 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #if !defined(MBEDTLS_BIGNUM_C) || \ |
| 57 | !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 58 | int main(void) |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 59 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 60 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or " |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 61 | "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n"); |
| 62 | mbedtls_exit(0); |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 63 | } |
| 64 | #else |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 65 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 66 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 67 | /* |
| 68 | * global options |
| 69 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 70 | struct options { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 71 | int mode; /* the mode to run the application in */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 72 | const char *filename; /* filename of the key file */ |
| 73 | const char *password; /* password for the private key */ |
| 74 | const char *password_file; /* password_file for the private key */ |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 75 | } opt; |
| 76 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 77 | int main(int argc, char *argv[]) |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 78 | { |
Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 79 | int ret = 1; |
| 80 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 81 | char buf[1024]; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 82 | int i; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 83 | char *p, *q; |
| 84 | |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 85 | mbedtls_pk_context pk; |
| 86 | mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; |
| 87 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 88 | /* |
| 89 | * Set to sane values |
| 90 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 91 | mbedtls_pk_init(&pk); |
| 92 | memset(buf, 0, sizeof(buf)); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 93 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q); |
| 95 | mbedtls_mpi_init(&D); mbedtls_mpi_init(&E); mbedtls_mpi_init(&DP); |
| 96 | mbedtls_mpi_init(&DQ); mbedtls_mpi_init(&QP); |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 97 | |
Aditya Deshpande | 0504ac2 | 2023-01-30 15:58:50 +0000 | [diff] [blame] | 98 | if (argc < 2) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 99 | usage: |
| 100 | mbedtls_printf(USAGE); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 101 | goto cleanup; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | opt.mode = DFL_MODE; |
| 105 | opt.filename = DFL_FILENAME; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 106 | opt.password = DFL_PASSWORD; |
| 107 | opt.password_file = DFL_PASSWORD_FILE; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 108 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 109 | for (i = 1; i < argc; i++) { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 110 | p = argv[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 111 | if ((q = strchr(p, '=')) == NULL) { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 112 | goto usage; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | } |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 114 | *q++ = '\0'; |
| 115 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 116 | if (strcmp(p, "mode") == 0) { |
| 117 | if (strcmp(q, "private") == 0) { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 118 | opt.mode = MODE_PRIVATE; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 119 | } else if (strcmp(q, "public") == 0) { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 120 | opt.mode = MODE_PUBLIC; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | } else { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 122 | goto usage; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 123 | } |
| 124 | } else if (strcmp(p, "filename") == 0) { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 125 | opt.filename = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | } else if (strcmp(p, "password") == 0) { |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 127 | opt.password = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 128 | } else if (strcmp(p, "password_file") == 0) { |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 129 | opt.password_file = q; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 130 | } else { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 131 | goto usage; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 132 | } |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 135 | if (opt.mode == MODE_PRIVATE) { |
| 136 | if (strlen(opt.password) && strlen(opt.password_file)) { |
| 137 | mbedtls_printf("Error: cannot have both password and password_file\n"); |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 138 | goto usage; |
| 139 | } |
| 140 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 141 | if (strlen(opt.password_file)) { |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 142 | FILE *f; |
| 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | mbedtls_printf("\n . Loading the password file ..."); |
| 145 | if ((f = fopen(opt.password_file, "rb")) == NULL) { |
| 146 | mbedtls_printf(" failed\n ! fopen returned NULL\n"); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 147 | goto cleanup; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 148 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 149 | if (fgets(buf, sizeof(buf), f) == NULL) { |
| 150 | fclose(f); |
| 151 | mbedtls_printf("Error: fgets() failed to retrieve password\n"); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 152 | goto cleanup; |
Paul Bakker | 8a0c0a9 | 2014-04-17 16:08:20 +0200 | [diff] [blame] | 153 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 154 | fclose(f); |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 155 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | i = (int) strlen(buf); |
| 157 | if (buf[i - 1] == '\n') { |
| 158 | buf[i - 1] = '\0'; |
| 159 | } |
| 160 | if (buf[i - 2] == '\r') { |
| 161 | buf[i - 2] = '\0'; |
| 162 | } |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 163 | opt.password = buf; |
| 164 | } |
| 165 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 166 | /* |
| 167 | * 1.1. Load the key |
| 168 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 169 | mbedtls_printf("\n . Loading the private key ..."); |
| 170 | fflush(stdout); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 171 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 172 | ret = mbedtls_pk_parse_keyfile(&pk, opt.filename, opt.password); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 173 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 174 | if (ret != 0) { |
| 175 | mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", |
| 176 | (unsigned int) -ret); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 177 | goto cleanup; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | mbedtls_printf(" ok\n"); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 181 | |
| 182 | /* |
| 183 | * 1.2 Print the key |
| 184 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | mbedtls_printf(" . Key information ...\n"); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 187 | if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) { |
| 188 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk); |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 190 | if ((ret = mbedtls_rsa_export(rsa, &N, &P, &Q, &D, &E)) != 0 || |
| 191 | (ret = mbedtls_rsa_export_crt(rsa, &DP, &DQ, &QP)) != 0) { |
| 192 | mbedtls_printf(" failed\n ! could not export RSA parameters\n\n"); |
Ron Eldor | a522147 | 2018-06-27 08:49:00 +0300 | [diff] [blame] | 193 | goto cleanup; |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 194 | } |
| 195 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 196 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL)); |
| 197 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL)); |
| 198 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D: ", &D, 16, NULL)); |
| 199 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("P: ", &P, 16, NULL)); |
| 200 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q: ", &Q, 16, NULL)); |
| 201 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DP: ", &DP, 16, NULL)); |
| 202 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("DQ: ", &DQ, 16, NULL)); |
| 203 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("QP: ", &QP, 16, NULL)); |
| 204 | } else |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 205 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 206 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 207 | if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) { |
| 208 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk); |
| 209 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL)); |
| 210 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL)); |
| 211 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL)); |
| 212 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("D : ", &ecp->d, 16, NULL)); |
| 213 | } else |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 214 | #endif |
| 215 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 216 | mbedtls_printf("Do not know how to print key information for this type\n"); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 217 | goto cleanup; |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 218 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 219 | } else if (opt.mode == MODE_PUBLIC) { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 220 | /* |
| 221 | * 1.1. Load the key |
| 222 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 223 | mbedtls_printf("\n . Loading the public key ..."); |
| 224 | fflush(stdout); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 225 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 226 | ret = mbedtls_pk_parse_public_keyfile(&pk, opt.filename); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 227 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | if (ret != 0) { |
| 229 | mbedtls_printf(" failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", |
| 230 | (unsigned int) -ret); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 231 | goto cleanup; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 234 | mbedtls_printf(" ok\n"); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 235 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 236 | mbedtls_printf(" . Key information ...\n"); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | #if defined(MBEDTLS_RSA_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_RSA) { |
| 239 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk); |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 241 | if ((ret = mbedtls_rsa_export(rsa, &N, NULL, NULL, |
| 242 | NULL, &E)) != 0) { |
| 243 | mbedtls_printf(" failed\n ! could not export RSA parameters\n\n"); |
Ron Eldor | a522147 | 2018-06-27 08:49:00 +0300 | [diff] [blame] | 244 | goto cleanup; |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 245 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("N: ", &N, 16, NULL)); |
| 247 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("E: ", &E, 16, NULL)); |
| 248 | } else |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 249 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | #if defined(MBEDTLS_ECP_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | if (mbedtls_pk_get_type(&pk) == MBEDTLS_PK_ECKEY) { |
| 252 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec(pk); |
| 253 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(X): ", &ecp->Q.X, 16, NULL)); |
| 254 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Y): ", &ecp->Q.Y, 16, NULL)); |
| 255 | MBEDTLS_MPI_CHK(mbedtls_mpi_write_file("Q(Z): ", &ecp->Q.Z, 16, NULL)); |
| 256 | } else |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 257 | #endif |
| 258 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 259 | mbedtls_printf("Do not know how to print key information for this type\n"); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 260 | goto cleanup; |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 261 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | } else { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 263 | goto usage; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | } |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 265 | |
Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 266 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 267 | |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 268 | cleanup: |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 269 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 270 | #if defined(MBEDTLS_ERROR_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 271 | if (exit_code != MBEDTLS_EXIT_SUCCESS) { |
| 272 | mbedtls_strerror(ret, buf, sizeof(buf)); |
| 273 | mbedtls_printf(" ! Last error was: %s\n", buf); |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 274 | } |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 275 | #endif |
| 276 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | mbedtls_pk_free(&pk); |
| 278 | mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q); |
| 279 | mbedtls_mpi_free(&D); mbedtls_mpi_free(&E); mbedtls_mpi_free(&DP); |
| 280 | mbedtls_mpi_free(&DQ); mbedtls_mpi_free(&QP); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 281 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 282 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 283 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 284 | fflush(stdout); getchar(); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 285 | #endif |
| 286 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 287 | mbedtls_exit(exit_code); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 288 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */ |