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