Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Key reading application |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
| 45 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 46 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 47 | */ |
| 48 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 50 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 53 | #endif |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 54 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 55 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 56 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 57 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 58 | #include <stdio.h> |
Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 59 | #include <stdlib.h> |
| 60 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 61 | #define mbedtls_exit exit |
Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 62 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 63 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 64 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 65 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | #if defined(MBEDTLS_BIGNUM_C) && \ |
| 67 | defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 68 | #include "mbedtls/error.h" |
| 69 | #include "mbedtls/rsa.h" |
| 70 | #include "mbedtls/x509.h" |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 71 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 72 | #include <string.h> |
| 73 | #endif |
| 74 | |
| 75 | #define MODE_NONE 0 |
| 76 | #define MODE_PRIVATE 1 |
| 77 | #define MODE_PUBLIC 2 |
| 78 | |
| 79 | #define DFL_MODE MODE_NONE |
| 80 | #define DFL_FILENAME "keyfile.key" |
| 81 | #define DFL_PASSWORD "" |
| 82 | #define DFL_PASSWORD_FILE "" |
| 83 | #define DFL_DEBUG_LEVEL 0 |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 84 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 85 | #define USAGE \ |
| 86 | "\n usage: key_app param=<>...\n" \ |
| 87 | "\n acceptable parameters:\n" \ |
| 88 | " mode=private|public default: none\n" \ |
| 89 | " filename=%%s default: keyfile.key\n" \ |
| 90 | " password=%%s default: \"\"\n" \ |
| 91 | " password_file=%%s default: \"\"\n" \ |
| 92 | "\n" |
| 93 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 94 | #if !defined(MBEDTLS_BIGNUM_C) || \ |
| 95 | !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 96 | int main( void ) |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 97 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or " |
| 99 | "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n"); |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 100 | mbedtls_exit( 0 ); |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 101 | } |
| 102 | #else |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 103 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 104 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 105 | /* |
| 106 | * global options |
| 107 | */ |
| 108 | struct options |
| 109 | { |
| 110 | int mode; /* the mode to run the application in */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 111 | const char *filename; /* filename of the key file */ |
| 112 | const char *password; /* password for the private key */ |
| 113 | const char *password_file; /* password_file for the private key */ |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 114 | } opt; |
| 115 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 116 | int main( int argc, char *argv[] ) |
| 117 | { |
Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 118 | int ret = 1; |
| 119 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 120 | char buf[1024]; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 121 | int i; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 122 | char *p, *q; |
| 123 | |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 124 | mbedtls_pk_context pk; |
| 125 | mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; |
| 126 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 127 | /* |
| 128 | * Set to sane values |
| 129 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | mbedtls_pk_init( &pk ); |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 131 | memset( buf, 0, sizeof(buf) ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 132 | |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 133 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 134 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP ); |
| 135 | mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP ); |
| 136 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 137 | if( argc == 0 ) |
| 138 | { |
| 139 | usage: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_printf( USAGE ); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 141 | goto cleanup; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | opt.mode = DFL_MODE; |
| 145 | opt.filename = DFL_FILENAME; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 146 | opt.password = DFL_PASSWORD; |
| 147 | opt.password_file = DFL_PASSWORD_FILE; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 148 | |
| 149 | for( i = 1; i < argc; i++ ) |
| 150 | { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 151 | p = argv[i]; |
| 152 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 153 | goto usage; |
| 154 | *q++ = '\0'; |
| 155 | |
| 156 | if( strcmp( p, "mode" ) == 0 ) |
| 157 | { |
| 158 | if( strcmp( q, "private" ) == 0 ) |
| 159 | opt.mode = MODE_PRIVATE; |
| 160 | else if( strcmp( q, "public" ) == 0 ) |
| 161 | opt.mode = MODE_PUBLIC; |
| 162 | else |
| 163 | goto usage; |
| 164 | } |
| 165 | else if( strcmp( p, "filename" ) == 0 ) |
| 166 | opt.filename = q; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 167 | else if( strcmp( p, "password" ) == 0 ) |
| 168 | opt.password = q; |
| 169 | else if( strcmp( p, "password_file" ) == 0 ) |
| 170 | opt.password_file = q; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 171 | else |
| 172 | goto usage; |
| 173 | } |
| 174 | |
| 175 | if( opt.mode == MODE_PRIVATE ) |
| 176 | { |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 177 | if( strlen( opt.password ) && strlen( opt.password_file ) ) |
| 178 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 179 | mbedtls_printf( "Error: cannot have both password and password_file\n" ); |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 180 | goto usage; |
| 181 | } |
| 182 | |
| 183 | if( strlen( opt.password_file ) ) |
| 184 | { |
| 185 | FILE *f; |
| 186 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 187 | mbedtls_printf( "\n . Loading the password file ..." ); |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 188 | if( ( f = fopen( opt.password_file, "rb" ) ) == NULL ) |
| 189 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 190 | mbedtls_printf( " failed\n ! fopen returned NULL\n" ); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 191 | goto cleanup; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 192 | } |
Paul Bakker | 8a0c0a9 | 2014-04-17 16:08:20 +0200 | [diff] [blame] | 193 | if( fgets( buf, sizeof(buf), f ) == NULL ) |
| 194 | { |
| 195 | fclose( f ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 196 | mbedtls_printf( "Error: fgets() failed to retrieve password\n" ); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 197 | goto cleanup; |
Paul Bakker | 8a0c0a9 | 2014-04-17 16:08:20 +0200 | [diff] [blame] | 198 | } |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 199 | fclose( f ); |
| 200 | |
Paul Bakker | 840ab20 | 2013-11-30 15:14:38 +0100 | [diff] [blame] | 201 | i = (int) strlen( buf ); |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 202 | if( buf[i - 1] == '\n' ) buf[i - 1] = '\0'; |
| 203 | if( buf[i - 2] == '\r' ) buf[i - 2] = '\0'; |
| 204 | opt.password = buf; |
| 205 | } |
| 206 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 207 | /* |
| 208 | * 1.1. Load the key |
| 209 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 210 | mbedtls_printf( "\n . Loading the private key ..." ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 211 | fflush( stdout ); |
| 212 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 214 | |
| 215 | if( ret != 0 ) |
| 216 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 217 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret ); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 218 | goto cleanup; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | mbedtls_printf( " ok\n" ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 222 | |
| 223 | /* |
| 224 | * 1.2 Print the key |
| 225 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 226 | mbedtls_printf( " . Key information ...\n" ); |
| 227 | #if defined(MBEDTLS_RSA_C) |
| 228 | if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA ) |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 229 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 230 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk ); |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 231 | |
| 232 | if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 || |
| 233 | ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 ) |
| 234 | { |
| 235 | mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); |
Ron Eldor | a522147 | 2018-06-27 08:49:00 +0300 | [diff] [blame] | 236 | goto cleanup; |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 237 | } |
| 238 | |
Ron Eldor | bf47099 | 2018-06-27 11:51:46 +0300 | [diff] [blame] | 239 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) ); |
| 240 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) ); |
| 241 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D: ", &D, 16, NULL ) ); |
| 242 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "P: ", &P, 16, NULL ) ); |
| 243 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ) ); |
| 244 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ) ); |
| 245 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ) ); |
| 246 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ) ); |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 247 | } |
| 248 | else |
| 249 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | #if defined(MBEDTLS_ECP_C) |
| 251 | if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ) |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 252 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk ); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 254 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) ); |
| 255 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) ); |
| 256 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) ); |
| 257 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ) ); |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 258 | } |
| 259 | else |
| 260 | #endif |
| 261 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 262 | 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] | 263 | goto cleanup; |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 264 | } |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 265 | } |
| 266 | else if( opt.mode == MODE_PUBLIC ) |
| 267 | { |
| 268 | /* |
| 269 | * 1.1. Load the key |
| 270 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | mbedtls_printf( "\n . Loading the public key ..." ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 272 | fflush( stdout ); |
| 273 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 274 | ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 275 | |
| 276 | if( ret != 0 ) |
| 277 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 279 | goto cleanup; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 282 | mbedtls_printf( " ok\n" ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 283 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | mbedtls_printf( " . Key information ...\n" ); |
| 285 | #if defined(MBEDTLS_RSA_C) |
| 286 | if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA ) |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 287 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 288 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk ); |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 289 | |
| 290 | if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL, |
| 291 | NULL, &E ) ) != 0 ) |
| 292 | { |
| 293 | mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); |
Ron Eldor | a522147 | 2018-06-27 08:49:00 +0300 | [diff] [blame] | 294 | goto cleanup; |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 295 | } |
Ron Eldor | bf47099 | 2018-06-27 11:51:46 +0300 | [diff] [blame] | 296 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "N: ", &N, 16, NULL ) ); |
| 297 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "E: ", &E, 16, NULL ) ); |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 298 | } |
| 299 | else |
| 300 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 301 | #if defined(MBEDTLS_ECP_C) |
| 302 | if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ) |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 303 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk ); |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 305 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ) ); |
| 306 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ) ); |
| 307 | MBEDTLS_MPI_CHK( mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ) ); |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 308 | } |
| 309 | else |
| 310 | #endif |
| 311 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | 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] | 313 | goto cleanup; |
Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 314 | } |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 315 | } |
| 316 | else |
| 317 | goto usage; |
| 318 | |
Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 319 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 320 | |
Ron Eldor | 6a9257b | 2017-08-24 14:20:17 +0300 | [diff] [blame] | 321 | cleanup: |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 322 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 323 | #if defined(MBEDTLS_ERROR_C) |
Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 324 | if( exit_code != MBEDTLS_EXIT_SUCCESS ) |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 325 | { |
Ron Eldor | 7a81426 | 2018-06-24 16:34:15 +0300 | [diff] [blame] | 326 | mbedtls_strerror( ret, buf, sizeof( buf ) ); |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 327 | mbedtls_printf( " ! Last error was: %s\n", buf ); |
| 328 | } |
Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 329 | #endif |
| 330 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | mbedtls_pk_free( &pk ); |
Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 332 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 333 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP ); |
| 334 | mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 335 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 336 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 338 | fflush( stdout ); getchar(); |
| 339 | #endif |
| 340 | |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 341 | mbedtls_exit( exit_code ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 342 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 343 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */ |