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