Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 2 | * Key writing application |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | 860b516 | 2015-01-28 17:12:07 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://polarssl.org) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 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 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | abd6e02 | 2013-09-20 13:30:43 +0200 | [diff] [blame] | 24 | #include "polarssl/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
| 30 | #include "polarssl/platform.h" |
| 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_printf printf |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 34 | #endif |
| 35 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 36 | #if defined(POLARSSL_PK_WRITE_C) && defined(POLARSSL_FS_IO) |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 37 | #include "polarssl/error.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 38 | #include "polarssl/pk.h" |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 39 | #include "polarssl/error.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 40 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 41 | #include <stdio.h> |
| 42 | #include <string.h> |
| 43 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 44 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 45 | #if defined(POLARSSL_PEM_WRITE_C) |
| 46 | #define USAGE_OUT \ |
| 47 | " output_file=%%s default: keyfile.pem\n" \ |
| 48 | " output_format=pem|der default: pem\n" |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 49 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 50 | #define USAGE_OUT \ |
| 51 | " output_file=%%s default: keyfile.der\n" \ |
| 52 | " output_format=der default: der\n" |
| 53 | #endif |
| 54 | |
| 55 | #if defined(POLARSSL_PEM_WRITE_C) |
| 56 | #define DFL_OUTPUT_FILENAME "keyfile.pem" |
| 57 | #define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM |
| 58 | #else |
| 59 | #define DFL_OUTPUT_FILENAME "keyfile.der" |
| 60 | #define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER |
| 61 | #endif |
| 62 | |
| 63 | #define DFL_MODE MODE_NONE |
| 64 | #define DFL_FILENAME "keyfile.key" |
| 65 | #define DFL_DEBUG_LEVEL 0 |
| 66 | #define DFL_OUTPUT_MODE OUTPUT_MODE_NONE |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 67 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 68 | #define MODE_NONE 0 |
| 69 | #define MODE_PRIVATE 1 |
| 70 | #define MODE_PUBLIC 2 |
| 71 | |
| 72 | #define OUTPUT_MODE_NONE 0 |
| 73 | #define OUTPUT_MODE_PRIVATE 1 |
| 74 | #define OUTPUT_MODE_PUBLIC 2 |
| 75 | |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 76 | #define OUTPUT_FORMAT_PEM 0 |
| 77 | #define OUTPUT_FORMAT_DER 1 |
| 78 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 79 | #define USAGE \ |
| 80 | "\n usage: key_app param=<>...\n" \ |
| 81 | "\n acceptable parameters:\n" \ |
| 82 | " mode=private|public default: none\n" \ |
| 83 | " filename=%%s default: keyfile.key\n" \ |
| 84 | " output_mode=private|public default: none\n" \ |
| 85 | USAGE_OUT \ |
| 86 | "\n" |
| 87 | |
| 88 | #if !defined(POLARSSL_PK_WRITE_C) || !defined(POLARSSL_FS_IO) |
| 89 | int main( int argc, char *argv[] ) |
| 90 | { |
| 91 | ((void) argc); |
| 92 | ((void) argv); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 93 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame^] | 94 | polarssl_printf( "POLARSSL_PK_WRITE_C and/or POLARSSL_FS_IO not defined.\n" ); |
| 95 | return( 0 ); |
| 96 | } |
| 97 | #else |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 98 | /* |
| 99 | * global options |
| 100 | */ |
| 101 | struct options |
| 102 | { |
| 103 | int mode; /* the mode to run the application in */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 104 | const char *filename; /* filename of the key file */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 105 | int output_mode; /* the output mode to use */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 106 | const char *output_file; /* where to store the constructed key file */ |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 107 | int output_format; /* the output format to use */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 108 | } opt; |
| 109 | |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 110 | static int write_public_key( pk_context *key, const char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 111 | { |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 112 | int ret; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 113 | FILE *f; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 114 | unsigned char output_buf[16000]; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 115 | unsigned char *c = output_buf; |
| 116 | size_t len = 0; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 117 | |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 118 | memset(output_buf, 0, 16000); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 119 | |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 120 | #if defined(POLARSSL_PEM_WRITE_C) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 121 | if( opt.output_format == OUTPUT_FORMAT_PEM ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 122 | { |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 123 | if( ( ret = pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 124 | return( ret ); |
| 125 | |
| 126 | len = strlen( (char *) output_buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 127 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 128 | else |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 129 | #endif |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 130 | { |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 131 | if( ( ret = pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 132 | return( ret ); |
| 133 | |
| 134 | len = ret; |
| 135 | c = output_buf + sizeof(output_buf) - len - 1; |
| 136 | } |
| 137 | |
| 138 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 139 | return( -1 ); |
| 140 | |
| 141 | if( fwrite( c, 1, len, f ) != len ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 142 | { |
| 143 | fclose( f ); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 144 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 145 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 146 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 147 | fclose( f ); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 148 | |
| 149 | return( 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 152 | static int write_private_key( pk_context *key, const char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 153 | { |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 154 | int ret; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 155 | FILE *f; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 156 | unsigned char output_buf[16000]; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 157 | unsigned char *c = output_buf; |
| 158 | size_t len = 0; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 159 | |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 160 | memset(output_buf, 0, 16000); |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 161 | |
| 162 | #if defined(POLARSSL_PEM_WRITE_C) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 163 | if( opt.output_format == OUTPUT_FORMAT_PEM ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 164 | { |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 165 | if( ( ret = pk_write_key_pem( key, output_buf, 16000 ) ) != 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 166 | return( ret ); |
| 167 | |
| 168 | len = strlen( (char *) output_buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 169 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 170 | else |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 171 | #endif |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 172 | { |
Paul Bakker | c7bb02b | 2013-09-15 14:54:56 +0200 | [diff] [blame] | 173 | if( ( ret = pk_write_key_der( key, output_buf, 16000 ) ) < 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 174 | return( ret ); |
| 175 | |
| 176 | len = ret; |
| 177 | c = output_buf + sizeof(output_buf) - len - 1; |
| 178 | } |
| 179 | |
| 180 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 181 | return( -1 ); |
| 182 | |
| 183 | if( fwrite( c, 1, len, f ) != len ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 184 | { |
| 185 | fclose( f ); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 186 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 187 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 188 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 189 | fclose( f ); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 190 | |
| 191 | return( 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 194 | int main( int argc, char *argv[] ) |
| 195 | { |
| 196 | int ret = 0; |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 197 | pk_context key; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 198 | char buf[1024]; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 199 | int i; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 200 | char *p, *q; |
| 201 | |
| 202 | /* |
| 203 | * Set to sane values |
| 204 | */ |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 205 | pk_init( &key ); |
| 206 | memset( buf, 0, sizeof( buf ) ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 207 | |
| 208 | if( argc == 0 ) |
| 209 | { |
| 210 | usage: |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 211 | ret = 1; |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 212 | polarssl_printf( USAGE ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 213 | goto exit; |
| 214 | } |
| 215 | |
| 216 | opt.mode = DFL_MODE; |
| 217 | opt.filename = DFL_FILENAME; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 218 | opt.output_mode = DFL_OUTPUT_MODE; |
| 219 | opt.output_file = DFL_OUTPUT_FILENAME; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 220 | opt.output_format = DFL_OUTPUT_FORMAT; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 221 | |
| 222 | for( i = 1; i < argc; i++ ) |
| 223 | { |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 224 | p = argv[i]; |
| 225 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 226 | goto usage; |
| 227 | *q++ = '\0'; |
| 228 | |
| 229 | if( strcmp( p, "mode" ) == 0 ) |
| 230 | { |
| 231 | if( strcmp( q, "private" ) == 0 ) |
| 232 | opt.mode = MODE_PRIVATE; |
| 233 | else if( strcmp( q, "public" ) == 0 ) |
| 234 | opt.mode = MODE_PUBLIC; |
| 235 | else |
| 236 | goto usage; |
| 237 | } |
| 238 | else if( strcmp( p, "output_mode" ) == 0 ) |
| 239 | { |
| 240 | if( strcmp( q, "private" ) == 0 ) |
| 241 | opt.output_mode = OUTPUT_MODE_PRIVATE; |
| 242 | else if( strcmp( q, "public" ) == 0 ) |
| 243 | opt.output_mode = OUTPUT_MODE_PUBLIC; |
| 244 | else |
| 245 | goto usage; |
| 246 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 247 | else if( strcmp( p, "output_format" ) == 0 ) |
| 248 | { |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 249 | #if defined(POLARSSL_PEM_WRITE_C) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 250 | if( strcmp( q, "pem" ) == 0 ) |
| 251 | opt.output_format = OUTPUT_FORMAT_PEM; |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 252 | else |
| 253 | #endif |
| 254 | if( strcmp( q, "der" ) == 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 255 | opt.output_format = OUTPUT_FORMAT_DER; |
| 256 | else |
| 257 | goto usage; |
| 258 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 259 | else if( strcmp( p, "filename" ) == 0 ) |
| 260 | opt.filename = q; |
| 261 | else if( strcmp( p, "output_file" ) == 0 ) |
| 262 | opt.output_file = q; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 263 | else |
| 264 | goto usage; |
| 265 | } |
| 266 | |
| 267 | if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE ) |
| 268 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 269 | polarssl_printf( "\nCannot output a key without reading one.\n"); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 270 | goto exit; |
| 271 | } |
| 272 | |
| 273 | if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE ) |
| 274 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 275 | polarssl_printf( "\nCannot output a private key from a public key.\n"); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 276 | goto exit; |
| 277 | } |
| 278 | |
| 279 | if( opt.mode == MODE_PRIVATE ) |
| 280 | { |
| 281 | /* |
| 282 | * 1.1. Load the key |
| 283 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 284 | polarssl_printf( "\n . Loading the private key ..." ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 285 | fflush( stdout ); |
| 286 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 287 | ret = pk_parse_keyfile( &key, opt.filename, NULL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 288 | |
| 289 | if( ret != 0 ) |
| 290 | { |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 291 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 292 | polarssl_printf( " failed\n ! pk_parse_keyfile returned -0x%04x - %s\n\n", -ret, buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 293 | goto exit; |
| 294 | } |
| 295 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 296 | polarssl_printf( " ok\n" ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * 1.2 Print the key |
| 300 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 301 | polarssl_printf( " . Key information ...\n" ); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 302 | |
| 303 | #if defined(POLARSSL_RSA_C) |
| 304 | if( pk_get_type( &key ) == POLARSSL_PK_RSA ) |
| 305 | { |
| 306 | rsa_context *rsa = pk_rsa( key ); |
| 307 | mpi_write_file( "N: ", &rsa->N, 16, NULL ); |
| 308 | mpi_write_file( "E: ", &rsa->E, 16, NULL ); |
| 309 | mpi_write_file( "D: ", &rsa->D, 16, NULL ); |
| 310 | mpi_write_file( "P: ", &rsa->P, 16, NULL ); |
| 311 | mpi_write_file( "Q: ", &rsa->Q, 16, NULL ); |
| 312 | mpi_write_file( "DP: ", &rsa->DP, 16, NULL ); |
| 313 | mpi_write_file( "DQ: ", &rsa->DQ, 16, NULL ); |
| 314 | mpi_write_file( "QP: ", &rsa->QP, 16, NULL ); |
| 315 | } |
| 316 | else |
| 317 | #endif |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 318 | #if defined(POLARSSL_ECP_C) |
| 319 | if( pk_get_type( &key ) == POLARSSL_PK_ECKEY ) |
| 320 | { |
| 321 | ecp_keypair *ecp = pk_ec( key ); |
| 322 | mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); |
| 323 | mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); |
| 324 | mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); |
| 325 | mpi_write_file( "D : ", &ecp->d , 16, NULL ); |
| 326 | } |
| 327 | else |
| 328 | #endif |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 329 | polarssl_printf("key type not supported yet\n"); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 330 | |
| 331 | } |
| 332 | else if( opt.mode == MODE_PUBLIC ) |
| 333 | { |
| 334 | /* |
| 335 | * 1.1. Load the key |
| 336 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 337 | polarssl_printf( "\n . Loading the public key ..." ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 338 | fflush( stdout ); |
| 339 | |
Paul Bakker | 1a7550a | 2013-09-15 13:01:22 +0200 | [diff] [blame] | 340 | ret = pk_parse_public_keyfile( &key, opt.filename ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 341 | |
| 342 | if( ret != 0 ) |
| 343 | { |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 344 | polarssl_strerror( ret, (char *) buf, sizeof(buf) ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 345 | polarssl_printf( " failed\n ! pk_parse_public_key returned -0x%04x - %s\n\n", -ret, buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 346 | goto exit; |
| 347 | } |
| 348 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 349 | polarssl_printf( " ok\n" ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 350 | |
| 351 | /* |
| 352 | * 1.2 Print the key |
| 353 | */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 354 | polarssl_printf( " . Key information ...\n" ); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 355 | |
| 356 | #if defined(POLARSSL_RSA_C) |
| 357 | if( pk_get_type( &key ) == POLARSSL_PK_RSA ) |
| 358 | { |
| 359 | rsa_context *rsa = pk_rsa( key ); |
| 360 | mpi_write_file( "N: ", &rsa->N, 16, NULL ); |
| 361 | mpi_write_file( "E: ", &rsa->E, 16, NULL ); |
| 362 | } |
| 363 | else |
| 364 | #endif |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 365 | #if defined(POLARSSL_ECP_C) |
| 366 | if( pk_get_type( &key ) == POLARSSL_PK_ECKEY ) |
| 367 | { |
| 368 | ecp_keypair *ecp = pk_ec( key ); |
| 369 | mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); |
| 370 | mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); |
| 371 | mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); |
| 372 | } |
| 373 | else |
| 374 | #endif |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 375 | polarssl_printf("key type not supported yet\n"); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 376 | } |
| 377 | else |
| 378 | goto usage; |
| 379 | |
| 380 | if( opt.output_mode == OUTPUT_MODE_PUBLIC ) |
| 381 | { |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 382 | write_public_key( &key, opt.output_file ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 383 | } |
| 384 | if( opt.output_mode == OUTPUT_MODE_PRIVATE ) |
| 385 | { |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 386 | write_private_key( &key, opt.output_file ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | exit: |
| 390 | |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 391 | if( ret != 0 && ret != 1) |
| 392 | { |
| 393 | #ifdef POLARSSL_ERROR_C |
| 394 | polarssl_strerror( ret, buf, sizeof( buf ) ); |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 395 | polarssl_printf( " - %s\n", buf ); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 396 | #else |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 397 | polarssl_printf("\n"); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 398 | #endif |
| 399 | } |
| 400 | |
| 401 | pk_free( &key ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 402 | |
| 403 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 404 | polarssl_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 405 | fflush( stdout ); getchar(); |
| 406 | #endif |
| 407 | |
| 408 | return( ret ); |
| 409 | } |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 410 | #endif /* POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */ |