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