Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Key reading application |
| 3 | * |
Manuel Pégourié-Gonnard | 0edee5e | 2015-01-26 15:29:40 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2012, ARM Limited, All Rights Reserved |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | e12abf9 | 2015-01-28 17:13:45 +0000 | [diff] [blame^] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
| 23 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 24 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 25 | #endif |
| 26 | |
| 27 | #include <string.h> |
| 28 | #include <stdlib.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | #include "polarssl/config.h" |
| 32 | |
| 33 | #include "polarssl/error.h" |
| 34 | #include "polarssl/rsa.h" |
| 35 | #include "polarssl/x509.h" |
| 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" |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 43 | #define DFL_PASSWORD "" |
| 44 | #define DFL_PASSWORD_FILE "" |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 45 | #define DFL_DEBUG_LEVEL 0 |
| 46 | |
| 47 | /* |
| 48 | * global options |
| 49 | */ |
| 50 | struct options |
| 51 | { |
| 52 | int mode; /* the mode to run the application in */ |
Paul Bakker | e0225e4 | 2013-06-06 12:52:24 +0200 | [diff] [blame] | 53 | const char *filename; /* filename of the key file */ |
| 54 | const char *password; /* password for the private key */ |
| 55 | const char *password_file; /* password_file for the private key */ |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 56 | int debug_level; /* level of debugging */ |
| 57 | } opt; |
| 58 | |
| 59 | void my_debug( void *ctx, int level, const char *str ) |
| 60 | { |
| 61 | if( level < opt.debug_level ) |
| 62 | { |
| 63 | fprintf( (FILE *) ctx, "%s", str ); |
| 64 | fflush( (FILE *) ctx ); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | #define USAGE \ |
| 69 | "\n usage: key_app param=<>...\n" \ |
| 70 | "\n acceptable parameters:\n" \ |
| 71 | " mode=private|public default: none\n" \ |
| 72 | " filename=%%s default: keyfile.key\n" \ |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 73 | " password=%%s default: \"\"\n" \ |
| 74 | " password_file=%%s default: \"\"\n" \ |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 75 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 76 | "\n" |
| 77 | |
| 78 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
| 79 | !defined(POLARSSL_X509_PARSE_C) || !defined(POLARSSL_FS_IO) |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 80 | int main( int argc, char *argv[] ) |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 81 | { |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 82 | ((void) argc); |
| 83 | ((void) argv); |
| 84 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 85 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
| 86 | "POLARSSL_X509_PARSE_C and/or POLARSSL_FS_IO not defined.\n"); |
| 87 | return( 0 ); |
| 88 | } |
| 89 | #else |
| 90 | int main( int argc, char *argv[] ) |
| 91 | { |
| 92 | int ret = 0; |
| 93 | rsa_context rsa; |
| 94 | char buf[1024]; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 95 | int i; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 96 | char *p, *q; |
| 97 | |
| 98 | /* |
| 99 | * Set to sane values |
| 100 | */ |
| 101 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 102 | memset( buf, 0, 1024 ); |
| 103 | |
| 104 | if( argc == 0 ) |
| 105 | { |
| 106 | usage: |
| 107 | printf( USAGE ); |
| 108 | goto exit; |
| 109 | } |
| 110 | |
| 111 | opt.mode = DFL_MODE; |
| 112 | opt.filename = DFL_FILENAME; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 113 | opt.password = DFL_PASSWORD; |
| 114 | opt.password_file = DFL_PASSWORD_FILE; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 115 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 116 | |
| 117 | for( i = 1; i < argc; i++ ) |
| 118 | { |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 119 | p = argv[i]; |
| 120 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 121 | goto usage; |
| 122 | *q++ = '\0'; |
| 123 | |
| 124 | if( strcmp( p, "mode" ) == 0 ) |
| 125 | { |
| 126 | if( strcmp( q, "private" ) == 0 ) |
| 127 | opt.mode = MODE_PRIVATE; |
| 128 | else if( strcmp( q, "public" ) == 0 ) |
| 129 | opt.mode = MODE_PUBLIC; |
| 130 | else |
| 131 | goto usage; |
| 132 | } |
| 133 | else if( strcmp( p, "filename" ) == 0 ) |
| 134 | opt.filename = q; |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 135 | else if( strcmp( p, "password" ) == 0 ) |
| 136 | opt.password = q; |
| 137 | else if( strcmp( p, "password_file" ) == 0 ) |
| 138 | opt.password_file = q; |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 139 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 140 | { |
| 141 | opt.debug_level = atoi( q ); |
| 142 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 143 | goto usage; |
| 144 | } |
| 145 | else |
| 146 | goto usage; |
| 147 | } |
| 148 | |
| 149 | if( opt.mode == MODE_PRIVATE ) |
| 150 | { |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 151 | if( strlen( opt.password ) && strlen( opt.password_file ) ) |
| 152 | { |
| 153 | printf( "Error: cannot have both password and password_file\n" ); |
| 154 | goto usage; |
| 155 | } |
| 156 | |
| 157 | if( strlen( opt.password_file ) ) |
| 158 | { |
| 159 | FILE *f; |
| 160 | |
| 161 | printf( "\n . Loading the password file ..." ); |
| 162 | if( ( f = fopen( opt.password_file, "rb" ) ) == NULL ) |
| 163 | { |
| 164 | printf( " failed\n ! fopen returned NULL\n" ); |
| 165 | goto exit; |
| 166 | } |
| 167 | fgets( buf, 1024, f ); |
| 168 | fclose( f ); |
| 169 | |
Paul Bakker | 0748895 | 2013-11-30 15:14:38 +0100 | [diff] [blame] | 170 | i = (int) strlen( buf ); |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 171 | if( buf[i - 1] == '\n' ) buf[i - 1] = '\0'; |
| 172 | if( buf[i - 2] == '\r' ) buf[i - 2] = '\0'; |
| 173 | opt.password = buf; |
| 174 | } |
| 175 | |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 176 | /* |
| 177 | * 1.1. Load the key |
| 178 | */ |
| 179 | printf( "\n . Loading the private key ..." ); |
| 180 | fflush( stdout ); |
| 181 | |
Paul Bakker | db2509c | 2012-09-27 12:44:31 +0000 | [diff] [blame] | 182 | ret = x509parse_keyfile( &rsa, opt.filename, opt.password ); |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 183 | |
| 184 | if( ret != 0 ) |
| 185 | { |
| 186 | #ifdef POLARSSL_ERROR_C |
| 187 | error_strerror( ret, buf, 1024 ); |
| 188 | #endif |
| 189 | printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf ); |
| 190 | rsa_free( &rsa ); |
| 191 | goto exit; |
| 192 | } |
| 193 | |
| 194 | printf( " ok\n" ); |
| 195 | |
| 196 | /* |
| 197 | * 1.2 Print the key |
| 198 | */ |
| 199 | printf( " . Key information ...\n" ); |
| 200 | mpi_write_file( "N: ", &rsa.N, 16, NULL ); |
| 201 | mpi_write_file( "E: ", &rsa.E, 16, NULL ); |
| 202 | mpi_write_file( "D: ", &rsa.D, 16, NULL ); |
| 203 | mpi_write_file( "P: ", &rsa.P, 16, NULL ); |
| 204 | mpi_write_file( "Q: ", &rsa.Q, 16, NULL ); |
| 205 | mpi_write_file( "DP: ", &rsa.DP, 16, NULL ); |
| 206 | mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL ); |
| 207 | mpi_write_file( "QP: ", &rsa.QP, 16, NULL ); |
| 208 | } |
| 209 | else if( opt.mode == MODE_PUBLIC ) |
| 210 | { |
| 211 | /* |
| 212 | * 1.1. Load the key |
| 213 | */ |
| 214 | printf( "\n . Loading the public key ..." ); |
| 215 | fflush( stdout ); |
| 216 | |
| 217 | ret = x509parse_public_keyfile( &rsa, opt.filename ); |
| 218 | |
| 219 | if( ret != 0 ) |
| 220 | { |
| 221 | #ifdef POLARSSL_ERROR_C |
| 222 | error_strerror( ret, buf, 1024 ); |
| 223 | #endif |
| 224 | printf( " failed\n ! x509parse_public_key returned %d - %s\n\n", ret, buf ); |
| 225 | rsa_free( &rsa ); |
| 226 | goto exit; |
| 227 | } |
| 228 | |
| 229 | printf( " ok\n" ); |
| 230 | |
| 231 | /* |
| 232 | * 1.2 Print the key |
| 233 | */ |
| 234 | printf( " . Key information ...\n" ); |
| 235 | mpi_write_file( "N: ", &rsa.N, 16, NULL ); |
| 236 | mpi_write_file( "E: ", &rsa.E, 16, NULL ); |
| 237 | } |
| 238 | else |
| 239 | goto usage; |
| 240 | |
| 241 | exit: |
| 242 | |
| 243 | rsa_free( &rsa ); |
| 244 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 245 | #if defined(_WIN32) |
Paul Bakker | ed56b22 | 2011-07-13 11:26:43 +0000 | [diff] [blame] | 246 | printf( " + Press Enter to exit this program.\n" ); |
| 247 | fflush( stdout ); getchar(); |
| 248 | #endif |
| 249 | |
| 250 | return( ret ); |
| 251 | } |
| 252 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && |
| 253 | POLARSSL_X509_PARSE_C && POLARSSL_FS_IO */ |