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