| 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 | 
| 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 | * | 
| Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | *  This file is part of mbed TLS (https://tls.mbed.org) | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 20 | */ | 
|  | 21 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.h> | 
| Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 32 | #include <stdlib.h> | 
|  | 33 | #define mbedtls_printf          printf | 
| Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 34 | #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS | 
| Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 35 | #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE | 
|  | 36 | #endif /* MBEDTLS_PLATFORM_C */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 37 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 38 | #if defined(MBEDTLS_BIGNUM_C) && \ | 
|  | 39 | defined(MBEDTLS_PK_PARSE_C) && defined(MBEDTLS_FS_IO) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/error.h" | 
|  | 41 | #include "mbedtls/rsa.h" | 
|  | 42 | #include "mbedtls/x509.h" | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 43 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 44 | #include <string.h> | 
|  | 45 | #endif | 
|  | 46 |  | 
|  | 47 | #define MODE_NONE               0 | 
|  | 48 | #define MODE_PRIVATE            1 | 
|  | 49 | #define MODE_PUBLIC             2 | 
|  | 50 |  | 
|  | 51 | #define DFL_MODE                MODE_NONE | 
|  | 52 | #define DFL_FILENAME            "keyfile.key" | 
|  | 53 | #define DFL_PASSWORD            "" | 
|  | 54 | #define DFL_PASSWORD_FILE       "" | 
|  | 55 | #define DFL_DEBUG_LEVEL         0 | 
| Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 56 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 57 | #define USAGE \ | 
|  | 58 | "\n usage: key_app param=<>...\n"                   \ | 
|  | 59 | "\n acceptable parameters:\n"                       \ | 
|  | 60 | "    mode=private|public default: none\n"           \ | 
|  | 61 | "    filename=%%s         default: keyfile.key\n"   \ | 
|  | 62 | "    password=%%s         default: \"\"\n"          \ | 
|  | 63 | "    password_file=%%s    default: \"\"\n"          \ | 
|  | 64 | "\n" | 
|  | 65 |  | 
|  | 66 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | #if !defined(MBEDTLS_BIGNUM_C) ||                                  \ | 
|  | 68 | !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_FS_IO) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 69 | int main( void ) | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 70 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or " | 
|  | 72 | "MBEDTLS_PK_PARSE_C and/or MBEDTLS_FS_IO not defined.\n"); | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 73 | return( 0 ); | 
|  | 74 | } | 
|  | 75 | #else | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 76 | /* | 
|  | 77 | * global options | 
|  | 78 | */ | 
|  | 79 | struct options | 
|  | 80 | { | 
|  | 81 | int mode;                   /* the mode to run the application in   */ | 
| Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 82 | const char *filename;       /* filename of the key file             */ | 
|  | 83 | const char *password;       /* password for the private key         */ | 
|  | 84 | const char *password_file;  /* password_file for the private key    */ | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 85 | } opt; | 
|  | 86 |  | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 87 | int main( int argc, char *argv[] ) | 
|  | 88 | { | 
| Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 89 | int ret = 1; | 
|  | 90 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 91 | char buf[1024]; | 
| Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 92 | int i; | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 93 | char *p, *q; | 
|  | 94 |  | 
| Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 95 | mbedtls_pk_context pk; | 
|  | 96 | mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; | 
|  | 97 |  | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 98 | /* | 
|  | 99 | * Set to sane values | 
|  | 100 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | mbedtls_pk_init( &pk ); | 
| Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 102 | memset( buf, 0, sizeof(buf) ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 103 |  | 
| Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 104 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); | 
|  | 105 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP ); | 
|  | 106 | mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP ); | 
|  | 107 |  | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 108 | if( argc == 0 ) | 
|  | 109 | { | 
|  | 110 | usage: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | mbedtls_printf( USAGE ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 112 | goto exit; | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | opt.mode                = DFL_MODE; | 
|  | 116 | opt.filename            = DFL_FILENAME; | 
| Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 117 | opt.password            = DFL_PASSWORD; | 
|  | 118 | opt.password_file       = DFL_PASSWORD_FILE; | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 119 |  | 
|  | 120 | for( i = 1; i < argc; i++ ) | 
|  | 121 | { | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 122 | p = argv[i]; | 
|  | 123 | if( ( q = strchr( p, '=' ) ) == NULL ) | 
|  | 124 | goto usage; | 
|  | 125 | *q++ = '\0'; | 
|  | 126 |  | 
|  | 127 | if( strcmp( p, "mode" ) == 0 ) | 
|  | 128 | { | 
|  | 129 | if( strcmp( q, "private" ) == 0 ) | 
|  | 130 | opt.mode = MODE_PRIVATE; | 
|  | 131 | else if( strcmp( q, "public" ) == 0 ) | 
|  | 132 | opt.mode = MODE_PUBLIC; | 
|  | 133 | else | 
|  | 134 | goto usage; | 
|  | 135 | } | 
|  | 136 | else if( strcmp( p, "filename" ) == 0 ) | 
|  | 137 | opt.filename = q; | 
| Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 138 | else if( strcmp( p, "password" ) == 0 ) | 
|  | 139 | opt.password = q; | 
|  | 140 | else if( strcmp( p, "password_file" ) == 0 ) | 
|  | 141 | opt.password_file = q; | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 142 | else | 
|  | 143 | goto usage; | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | if( opt.mode == MODE_PRIVATE ) | 
|  | 147 | { | 
| Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 148 | if( strlen( opt.password ) && strlen( opt.password_file ) ) | 
|  | 149 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | mbedtls_printf( "Error: cannot have both password and password_file\n" ); | 
| Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 151 | goto usage; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | if( strlen( opt.password_file ) ) | 
|  | 155 | { | 
|  | 156 | FILE *f; | 
|  | 157 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | mbedtls_printf( "\n  . Loading the password file ..." ); | 
| Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 159 | if( ( f = fopen( opt.password_file, "rb" ) ) == NULL ) | 
|  | 160 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | mbedtls_printf( " failed\n  !  fopen returned NULL\n" ); | 
| Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 162 | goto exit; | 
|  | 163 | } | 
| Paul Bakker | 8a0c0a9 | 2014-04-17 16:08:20 +0200 | [diff] [blame] | 164 | if( fgets( buf, sizeof(buf), f ) == NULL ) | 
|  | 165 | { | 
|  | 166 | fclose( f ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | mbedtls_printf( "Error: fgets() failed to retrieve password\n" ); | 
| Paul Bakker | 8a0c0a9 | 2014-04-17 16:08:20 +0200 | [diff] [blame] | 168 | goto exit; | 
|  | 169 | } | 
| Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 170 | fclose( f ); | 
|  | 171 |  | 
| Paul Bakker | 840ab20 | 2013-11-30 15:14:38 +0100 | [diff] [blame] | 172 | i = (int) strlen( buf ); | 
| Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 173 | if( buf[i - 1] == '\n' ) buf[i - 1] = '\0'; | 
|  | 174 | if( buf[i - 2] == '\r' ) buf[i - 2] = '\0'; | 
|  | 175 | opt.password = buf; | 
|  | 176 | } | 
|  | 177 |  | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 178 | /* | 
|  | 179 | * 1.1. Load the key | 
|  | 180 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | mbedtls_printf( "\n  . Loading the private key ..." ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 182 | fflush( stdout ); | 
|  | 183 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 184 | ret = mbedtls_pk_parse_keyfile( &pk, opt.filename, opt.password ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 185 |  | 
|  | 186 | if( ret != 0 ) | 
|  | 187 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | mbedtls_printf( " failed\n  !  mbedtls_pk_parse_keyfile returned -0x%04x\n", -ret ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 189 | goto exit; | 
|  | 190 | } | 
|  | 191 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 193 |  | 
|  | 194 | /* | 
|  | 195 | * 1.2 Print the key | 
|  | 196 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | mbedtls_printf( "  . Key information    ...\n" ); | 
|  | 198 | #if defined(MBEDTLS_RSA_C) | 
|  | 199 | if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA ) | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 200 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk ); | 
| Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 202 |  | 
|  | 203 | if( ( ret = mbedtls_rsa_export    ( rsa, &N, &P, &Q, &D, &E ) ) != 0 || | 
|  | 204 | ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) )      != 0 ) | 
|  | 205 | { | 
|  | 206 | mbedtls_printf( " failed\n  ! could not export RSA parameters\n\n" ); | 
|  | 207 | goto exit; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | mbedtls_mpi_write_file( "N:  ",  &N,  16, NULL ); | 
|  | 211 | mbedtls_mpi_write_file( "E:  ",  &E,  16, NULL ); | 
|  | 212 | mbedtls_mpi_write_file( "D:  ",  &D,  16, NULL ); | 
|  | 213 | mbedtls_mpi_write_file( "P:  ",  &P,  16, NULL ); | 
|  | 214 | mbedtls_mpi_write_file( "Q:  ",  &Q,  16, NULL ); | 
|  | 215 | mbedtls_mpi_write_file( "DP: ",  &DP, 16, NULL ); | 
|  | 216 | mbedtls_mpi_write_file( "DQ:  ", &DQ, 16, NULL ); | 
|  | 217 | mbedtls_mpi_write_file( "QP:  ", &QP, 16, NULL ); | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 218 | } | 
|  | 219 | else | 
|  | 220 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | #if defined(MBEDTLS_ECP_C) | 
|  | 222 | if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ) | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 223 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk ); | 
|  | 225 | mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); | 
|  | 226 | mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); | 
|  | 227 | mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); | 
|  | 228 | mbedtls_mpi_write_file( "D   : ", &ecp->d  , 16, NULL ); | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 229 | } | 
|  | 230 | else | 
|  | 231 | #endif | 
|  | 232 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | mbedtls_printf("Do not know how to print key information for this type\n" ); | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 234 | goto exit; | 
|  | 235 | } | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 236 | } | 
|  | 237 | else if( opt.mode == MODE_PUBLIC ) | 
|  | 238 | { | 
|  | 239 | /* | 
|  | 240 | * 1.1. Load the key | 
|  | 241 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | mbedtls_printf( "\n  . Loading the public key ..." ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 243 | fflush( stdout ); | 
|  | 244 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 245 | ret = mbedtls_pk_parse_public_keyfile( &pk, opt.filename ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 246 |  | 
|  | 247 | if( ret != 0 ) | 
|  | 248 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 249 | mbedtls_printf( " failed\n  !  mbedtls_pk_parse_public_keyfile returned -0x%04x\n", -ret ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 250 | goto exit; | 
|  | 251 | } | 
|  | 252 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 254 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | mbedtls_printf( "  . Key information    ...\n" ); | 
|  | 256 | #if defined(MBEDTLS_RSA_C) | 
|  | 257 | if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_RSA ) | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 258 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa( pk ); | 
| Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 260 |  | 
|  | 261 | if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL, | 
|  | 262 | NULL, &E ) ) != 0 ) | 
|  | 263 | { | 
|  | 264 | mbedtls_printf( " failed\n  ! could not export RSA parameters\n\n" ); | 
|  | 265 | goto exit; | 
|  | 266 | } | 
|  | 267 | mbedtls_mpi_write_file( "N: ", &N, 16, NULL ); | 
|  | 268 | mbedtls_mpi_write_file( "E: ", &E, 16, NULL ); | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 269 | } | 
|  | 270 | else | 
|  | 271 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | #if defined(MBEDTLS_ECP_C) | 
|  | 273 | if( mbedtls_pk_get_type( &pk ) == MBEDTLS_PK_ECKEY ) | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 274 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( pk ); | 
|  | 276 | mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); | 
|  | 277 | mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); | 
|  | 278 | mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 279 | } | 
|  | 280 | else | 
|  | 281 | #endif | 
|  | 282 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 283 | mbedtls_printf("Do not know how to print key information for this type\n" ); | 
| Paul Bakker | 1525495 | 2013-09-17 11:24:56 +0200 | [diff] [blame] | 284 | goto exit; | 
|  | 285 | } | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 286 | } | 
|  | 287 | else | 
|  | 288 | goto usage; | 
|  | 289 |  | 
| Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 290 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 291 |  | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 292 | exit: | 
|  | 293 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | #if defined(MBEDTLS_ERROR_C) | 
| Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 295 | if( exit_code != MBEDTLS_EXIT_SUCCESS ) | 
| Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 296 | { | 
|  | 297 | mbedtls_strerror( ret, buf, sizeof(buf) ); | 
|  | 298 | mbedtls_printf( "  !  Last error was: %s\n", buf ); | 
|  | 299 | } | 
| Manuel Pégourié-Gonnard | 92e5b59 | 2013-09-18 18:57:10 +0200 | [diff] [blame] | 300 | #endif | 
|  | 301 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | mbedtls_pk_free( &pk ); | 
| Hanno Becker | 54ebf99 | 2017-08-23 06:45:38 +0100 | [diff] [blame] | 303 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); | 
|  | 304 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP ); | 
|  | 305 | mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 306 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 307 | #if defined(_WIN32) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | mbedtls_printf( "  + Press Enter to exit this program.\n" ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 309 | fflush( stdout ); getchar(); | 
|  | 310 | #endif | 
|  | 311 |  | 
| Andres Amaya Garcia | 0faf1a5 | 2018-04-29 20:02:18 +0100 | [diff] [blame] | 312 | return( exit_code ); | 
| Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 313 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO */ |