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 | |
| 26 | #ifndef _CRT_SECURE_NO_DEPRECATE |
| 27 | #define _CRT_SECURE_NO_DEPRECATE 1 |
| 28 | #endif |
| 29 | |
| 30 | #include <string.h> |
| 31 | #include <stdlib.h> |
| 32 | #include <stdio.h> |
| 33 | |
| 34 | #include "polarssl/config.h" |
| 35 | |
| 36 | #include "polarssl/error.h" |
| 37 | #include "polarssl/rsa.h" |
| 38 | #include "polarssl/x509.h" |
| 39 | #include "polarssl/base64.h" |
| 40 | #include "polarssl/x509write.h" |
| 41 | |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 42 | #if !defined(POLARSSL_BIGNUM_C) || !defined(POLARSSL_RSA_C) || \ |
| 43 | !defined(POLARSSL_X509_WRITE_C) || !defined(POLARSSL_FS_IO) |
| 44 | int main( int argc, char *argv[] ) |
| 45 | { |
| 46 | ((void) argc); |
| 47 | ((void) argv); |
| 48 | |
| 49 | printf("POLARSSL_BIGNUM_C and/or POLARSSL_RSA_C and/or " |
| 50 | "POLARSSL_X509_WRITE_C and/or POLARSSL_FS_IO not defined.\n"); |
| 51 | return( 0 ); |
| 52 | } |
| 53 | #else |
| 54 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 55 | #define MODE_NONE 0 |
| 56 | #define MODE_PRIVATE 1 |
| 57 | #define MODE_PUBLIC 2 |
| 58 | |
| 59 | #define OUTPUT_MODE_NONE 0 |
| 60 | #define OUTPUT_MODE_PRIVATE 1 |
| 61 | #define OUTPUT_MODE_PUBLIC 2 |
| 62 | |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 63 | #define OUTPUT_FORMAT_PEM 0 |
| 64 | #define OUTPUT_FORMAT_DER 1 |
| 65 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 66 | #define DFL_MODE MODE_NONE |
| 67 | #define DFL_FILENAME "keyfile.key" |
| 68 | #define DFL_DEBUG_LEVEL 0 |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 69 | #define DFL_OUTPUT_MODE OUTPUT_MODE_NONE |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 70 | #define DFL_OUTPUT_FILENAME "keyfile.pem" |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 71 | #define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * global options |
| 75 | */ |
| 76 | struct options |
| 77 | { |
| 78 | int mode; /* the mode to run the application in */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 79 | const char *filename; /* filename of the key file */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 80 | int output_mode; /* the output mode to use */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 81 | const char *output_file; /* where to store the constructed key file */ |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 82 | int output_format; /* the output format to use */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 83 | } opt; |
| 84 | |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 85 | static int write_public_key( rsa_context *rsa, const char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 86 | { |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 87 | int ret; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 88 | FILE *f; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 89 | unsigned char output_buf[16000]; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 90 | unsigned char *c = output_buf; |
| 91 | size_t len = 0; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 92 | |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 93 | memset(output_buf, 0, 16000); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 94 | |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 95 | if( opt.output_format == OUTPUT_FORMAT_PEM ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 96 | { |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 97 | if( ( ret = x509write_pubkey_pem( rsa, output_buf, 16000 ) ) != 0 ) |
| 98 | return( ret ); |
| 99 | |
| 100 | len = strlen( (char *) output_buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 101 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 102 | else |
| 103 | { |
| 104 | if( ( ret = x509write_pubkey_der( rsa, output_buf, 16000 ) ) < 0 ) |
| 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 ) |
| 115 | return( -1 ); |
| 116 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 117 | fclose(f); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 118 | |
| 119 | return( 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 122 | static int write_private_key( rsa_context *rsa, const char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 123 | { |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 124 | int ret; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 125 | FILE *f; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 126 | unsigned char output_buf[16000]; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 127 | unsigned char *c = output_buf; |
| 128 | size_t len = 0; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 129 | |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 130 | memset(output_buf, 0, 16000); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 131 | if( opt.output_format == OUTPUT_FORMAT_PEM ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 132 | { |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 133 | if( ( ret = x509write_key_pem( rsa, output_buf, 16000 ) ) != 0 ) |
| 134 | return( ret ); |
| 135 | |
| 136 | len = strlen( (char *) output_buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 137 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 138 | else |
| 139 | { |
| 140 | if( ( ret = x509write_key_der( rsa, output_buf, 16000 ) ) < 0 ) |
| 141 | return( ret ); |
| 142 | |
| 143 | len = ret; |
| 144 | c = output_buf + sizeof(output_buf) - len - 1; |
| 145 | } |
| 146 | |
| 147 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 148 | return( -1 ); |
| 149 | |
| 150 | if( fwrite( c, 1, len, f ) != len ) |
| 151 | return( -1 ); |
| 152 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 153 | fclose(f); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 154 | |
| 155 | return( 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | #define USAGE \ |
| 159 | "\n usage: key_app param=<>...\n" \ |
| 160 | "\n acceptable parameters:\n" \ |
| 161 | " mode=private|public default: none\n" \ |
| 162 | " filename=%%s default: keyfile.key\n" \ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 163 | " output_mode=private|public default: none\n" \ |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 164 | " output_file=%%s default: keyfile.pem\n" \ |
| 165 | " output_format=pem|der default: pem\n" \ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 166 | "\n" |
| 167 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 168 | int main( int argc, char *argv[] ) |
| 169 | { |
| 170 | int ret = 0; |
| 171 | rsa_context rsa; |
| 172 | char buf[1024]; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 173 | int i; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 174 | char *p, *q; |
| 175 | |
| 176 | /* |
| 177 | * Set to sane values |
| 178 | */ |
| 179 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 180 | memset( buf, 0, 1024 ); |
| 181 | |
| 182 | if( argc == 0 ) |
| 183 | { |
| 184 | usage: |
| 185 | printf( USAGE ); |
| 186 | goto exit; |
| 187 | } |
| 188 | |
| 189 | opt.mode = DFL_MODE; |
| 190 | opt.filename = DFL_FILENAME; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 191 | opt.output_mode = DFL_OUTPUT_MODE; |
| 192 | opt.output_file = DFL_OUTPUT_FILENAME; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 193 | opt.output_format = DFL_OUTPUT_FORMAT; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 194 | |
| 195 | for( i = 1; i < argc; i++ ) |
| 196 | { |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 197 | p = argv[i]; |
| 198 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 199 | goto usage; |
| 200 | *q++ = '\0'; |
| 201 | |
| 202 | if( strcmp( p, "mode" ) == 0 ) |
| 203 | { |
| 204 | if( strcmp( q, "private" ) == 0 ) |
| 205 | opt.mode = MODE_PRIVATE; |
| 206 | else if( strcmp( q, "public" ) == 0 ) |
| 207 | opt.mode = MODE_PUBLIC; |
| 208 | else |
| 209 | goto usage; |
| 210 | } |
| 211 | else if( strcmp( p, "output_mode" ) == 0 ) |
| 212 | { |
| 213 | if( strcmp( q, "private" ) == 0 ) |
| 214 | opt.output_mode = OUTPUT_MODE_PRIVATE; |
| 215 | else if( strcmp( q, "public" ) == 0 ) |
| 216 | opt.output_mode = OUTPUT_MODE_PUBLIC; |
| 217 | else |
| 218 | goto usage; |
| 219 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame^] | 220 | else if( strcmp( p, "output_format" ) == 0 ) |
| 221 | { |
| 222 | if( strcmp( q, "pem" ) == 0 ) |
| 223 | opt.output_format = OUTPUT_FORMAT_PEM; |
| 224 | else if( strcmp( q, "der" ) == 0 ) |
| 225 | opt.output_format = OUTPUT_FORMAT_DER; |
| 226 | else |
| 227 | goto usage; |
| 228 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 229 | else if( strcmp( p, "filename" ) == 0 ) |
| 230 | opt.filename = q; |
| 231 | else if( strcmp( p, "output_file" ) == 0 ) |
| 232 | opt.output_file = q; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 233 | else |
| 234 | goto usage; |
| 235 | } |
| 236 | |
| 237 | if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE ) |
| 238 | { |
| 239 | printf( "\nCannot output a key without reading one.\n"); |
| 240 | goto exit; |
| 241 | } |
| 242 | |
| 243 | if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE ) |
| 244 | { |
| 245 | printf( "\nCannot output a private key from a public key.\n"); |
| 246 | goto exit; |
| 247 | } |
| 248 | |
| 249 | if( opt.mode == MODE_PRIVATE ) |
| 250 | { |
| 251 | /* |
| 252 | * 1.1. Load the key |
| 253 | */ |
| 254 | printf( "\n . Loading the private key ..." ); |
| 255 | fflush( stdout ); |
| 256 | |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 257 | ret = x509parse_keyfile_rsa( &rsa, opt.filename, NULL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 258 | |
| 259 | if( ret != 0 ) |
| 260 | { |
| 261 | #ifdef POLARSSL_ERROR_C |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 262 | polarssl_strerror( ret, buf, 1024 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 263 | #endif |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 264 | printf( " failed\n ! x509parse_key_rsa returned %d - %s\n\n", ret, buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 265 | rsa_free( &rsa ); |
| 266 | goto exit; |
| 267 | } |
| 268 | |
| 269 | printf( " ok\n" ); |
| 270 | |
| 271 | /* |
| 272 | * 1.2 Print the key |
| 273 | */ |
| 274 | printf( " . Key information ...\n" ); |
| 275 | mpi_write_file( "N: ", &rsa.N, 16, NULL ); |
| 276 | mpi_write_file( "E: ", &rsa.E, 16, NULL ); |
| 277 | mpi_write_file( "D: ", &rsa.D, 16, NULL ); |
| 278 | mpi_write_file( "P: ", &rsa.P, 16, NULL ); |
| 279 | mpi_write_file( "Q: ", &rsa.Q, 16, NULL ); |
| 280 | mpi_write_file( "DP: ", &rsa.DP, 16, NULL ); |
| 281 | mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL ); |
| 282 | mpi_write_file( "QP: ", &rsa.QP, 16, NULL ); |
| 283 | |
| 284 | } |
| 285 | else if( opt.mode == MODE_PUBLIC ) |
| 286 | { |
| 287 | /* |
| 288 | * 1.1. Load the key |
| 289 | */ |
| 290 | printf( "\n . Loading the public key ..." ); |
| 291 | fflush( stdout ); |
| 292 | |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 293 | ret = x509parse_public_keyfile_rsa( &rsa, opt.filename ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 294 | |
| 295 | if( ret != 0 ) |
| 296 | { |
| 297 | #ifdef POLARSSL_ERROR_C |
Paul Bakker | 03a8a79 | 2013-06-30 12:18:08 +0200 | [diff] [blame] | 298 | polarssl_strerror( ret, buf, 1024 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 299 | #endif |
Manuel Pégourié-Gonnard | ba4878a | 2013-06-27 10:51:01 +0200 | [diff] [blame] | 300 | printf( " failed\n ! x509parse_public_key_rsa returned %d - %s\n\n", ret, buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 301 | rsa_free( &rsa ); |
| 302 | goto exit; |
| 303 | } |
| 304 | |
| 305 | printf( " ok\n" ); |
| 306 | |
| 307 | /* |
| 308 | * 1.2 Print the key |
| 309 | */ |
| 310 | printf( " . Key information ...\n" ); |
| 311 | mpi_write_file( "N: ", &rsa.N, 16, NULL ); |
| 312 | mpi_write_file( "E: ", &rsa.E, 16, NULL ); |
| 313 | } |
| 314 | else |
| 315 | goto usage; |
| 316 | |
| 317 | if( opt.output_mode == OUTPUT_MODE_PUBLIC ) |
| 318 | { |
| 319 | write_public_key( &rsa, opt.output_file ); |
| 320 | } |
| 321 | if( opt.output_mode == OUTPUT_MODE_PRIVATE ) |
| 322 | { |
| 323 | write_private_key( &rsa, opt.output_file ); |
| 324 | } |
| 325 | |
| 326 | exit: |
| 327 | |
| 328 | rsa_free( &rsa ); |
| 329 | |
| 330 | #if defined(_WIN32) |
| 331 | printf( " + Press Enter to exit this program.\n" ); |
| 332 | fflush( stdout ); getchar(); |
| 333 | #endif |
| 334 | |
| 335 | return( ret ); |
| 336 | } |
| 337 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 338 | POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */ |