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