Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Key reading application |
| 3 | * |
| 4 | * Copyright (C) 2006-2011, Brainspark B.V. |
| 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 | |
| 63 | #define DFL_MODE MODE_NONE |
| 64 | #define DFL_FILENAME "keyfile.key" |
| 65 | #define DFL_DEBUG_LEVEL 0 |
| 66 | #define DFL_OUTPUT_MODE OUTPUT_MODE_NONE |
| 67 | #define DFL_OUTPUT_FILENAME "keyfile.pem" |
| 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 debug_level; /* level of debugging */ |
| 77 | int output_mode; /* the output mode to use */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame^] | 78 | const char *output_file; /* where to store the constructed key file */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 79 | } opt; |
| 80 | |
| 81 | void my_debug( void *ctx, int level, const char *str ) |
| 82 | { |
| 83 | if( level < opt.debug_level ) |
| 84 | { |
| 85 | fprintf( (FILE *) ctx, "%s", str ); |
| 86 | fflush( (FILE *) ctx ); |
| 87 | } |
| 88 | } |
| 89 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame^] | 90 | void write_public_key( rsa_context *rsa, const char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 91 | { |
| 92 | FILE *f; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 93 | unsigned char output_buf[16000]; |
| 94 | unsigned char base_buf[16000]; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 95 | unsigned char *c; |
| 96 | int ret; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 97 | size_t len = 0, olen = 16000; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 98 | |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 99 | memset(output_buf, 0, 16000); |
| 100 | ret = x509_write_pubkey_der( output_buf, 16000, rsa ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 101 | |
| 102 | if( ret < 0 ) |
| 103 | return; |
| 104 | |
| 105 | len = ret; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 106 | c = output_buf + 15999 - len; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 107 | |
| 108 | base64_encode( base_buf, &olen, c, len ); |
| 109 | |
| 110 | c = base_buf; |
| 111 | |
| 112 | f = fopen( output_file, "w" ); |
| 113 | fprintf(f, "-----BEGIN PUBLIC KEY-----\n"); |
| 114 | while (olen) |
| 115 | { |
| 116 | int use_len = olen; |
| 117 | if (use_len > 64) use_len = 64; |
| 118 | fwrite( c, 1, use_len, f ); |
| 119 | olen -= use_len; |
| 120 | c += use_len; |
| 121 | fprintf(f, "\n"); |
| 122 | } |
| 123 | fprintf(f, "-----END PUBLIC KEY-----\n"); |
| 124 | fclose(f); |
| 125 | } |
| 126 | |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame^] | 127 | void write_private_key( rsa_context *rsa, const char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 128 | { |
| 129 | FILE *f; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 130 | unsigned char output_buf[16000]; |
| 131 | unsigned char base_buf[16000]; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 132 | unsigned char *c; |
| 133 | int ret; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 134 | size_t len = 0, olen = 16000; |
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); |
| 137 | ret = x509_write_key_der( output_buf, 16000, rsa ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 138 | if( ret < 0 ) |
| 139 | return; |
| 140 | |
| 141 | len = ret; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 142 | c = output_buf + 15999 - len; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 143 | |
| 144 | base64_encode( base_buf, &olen, c, len ); |
| 145 | |
| 146 | c = base_buf; |
| 147 | |
| 148 | f = fopen( output_file, "w" ); |
| 149 | fprintf(f, "-----BEGIN RSA PRIVATE KEY-----\n"); |
| 150 | while (olen) |
| 151 | { |
| 152 | int use_len = olen; |
| 153 | if (use_len > 64) use_len = 64; |
| 154 | fwrite( c, 1, use_len, f ); |
| 155 | olen -= use_len; |
| 156 | c += use_len; |
| 157 | fprintf(f, "\n"); |
| 158 | } |
| 159 | fprintf(f, "-----END RSA PRIVATE KEY-----\n"); |
| 160 | fclose(f); |
| 161 | } |
| 162 | |
| 163 | #define USAGE \ |
| 164 | "\n usage: key_app param=<>...\n" \ |
| 165 | "\n acceptable parameters:\n" \ |
| 166 | " mode=private|public default: none\n" \ |
| 167 | " filename=%%s default: keyfile.key\n" \ |
| 168 | " debug_level=%%d default: 0 (disabled)\n" \ |
| 169 | " output_mode=private|public default: none\n" \ |
| 170 | " output_file=%%s defeult: keyfile.pem\n" \ |
| 171 | "\n" |
| 172 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 173 | int main( int argc, char *argv[] ) |
| 174 | { |
| 175 | int ret = 0; |
| 176 | rsa_context rsa; |
| 177 | char buf[1024]; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 178 | int i; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 179 | char *p, *q; |
| 180 | |
| 181 | /* |
| 182 | * Set to sane values |
| 183 | */ |
| 184 | memset( &rsa, 0, sizeof( rsa_context ) ); |
| 185 | memset( buf, 0, 1024 ); |
| 186 | |
| 187 | if( argc == 0 ) |
| 188 | { |
| 189 | usage: |
| 190 | printf( USAGE ); |
| 191 | goto exit; |
| 192 | } |
| 193 | |
| 194 | opt.mode = DFL_MODE; |
| 195 | opt.filename = DFL_FILENAME; |
| 196 | opt.debug_level = DFL_DEBUG_LEVEL; |
| 197 | opt.output_mode = DFL_OUTPUT_MODE; |
| 198 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 199 | |
| 200 | for( i = 1; i < argc; i++ ) |
| 201 | { |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 202 | p = argv[i]; |
| 203 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 204 | goto usage; |
| 205 | *q++ = '\0'; |
| 206 | |
| 207 | if( strcmp( p, "mode" ) == 0 ) |
| 208 | { |
| 209 | if( strcmp( q, "private" ) == 0 ) |
| 210 | opt.mode = MODE_PRIVATE; |
| 211 | else if( strcmp( q, "public" ) == 0 ) |
| 212 | opt.mode = MODE_PUBLIC; |
| 213 | else |
| 214 | goto usage; |
| 215 | } |
| 216 | else if( strcmp( p, "output_mode" ) == 0 ) |
| 217 | { |
| 218 | if( strcmp( q, "private" ) == 0 ) |
| 219 | opt.output_mode = OUTPUT_MODE_PRIVATE; |
| 220 | else if( strcmp( q, "public" ) == 0 ) |
| 221 | opt.output_mode = OUTPUT_MODE_PUBLIC; |
| 222 | else |
| 223 | goto usage; |
| 224 | } |
| 225 | else if( strcmp( p, "filename" ) == 0 ) |
| 226 | opt.filename = q; |
| 227 | else if( strcmp( p, "output_file" ) == 0 ) |
| 228 | opt.output_file = q; |
| 229 | else if( strcmp( p, "debug_level" ) == 0 ) |
| 230 | { |
| 231 | opt.debug_level = atoi( q ); |
| 232 | if( opt.debug_level < 0 || opt.debug_level > 65535 ) |
| 233 | goto usage; |
| 234 | } |
| 235 | else |
| 236 | goto usage; |
| 237 | } |
| 238 | |
| 239 | if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE ) |
| 240 | { |
| 241 | printf( "\nCannot output a key without reading one.\n"); |
| 242 | goto exit; |
| 243 | } |
| 244 | |
| 245 | if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE ) |
| 246 | { |
| 247 | printf( "\nCannot output a private key from a public key.\n"); |
| 248 | goto exit; |
| 249 | } |
| 250 | |
| 251 | if( opt.mode == MODE_PRIVATE ) |
| 252 | { |
| 253 | /* |
| 254 | * 1.1. Load the key |
| 255 | */ |
| 256 | printf( "\n . Loading the private key ..." ); |
| 257 | fflush( stdout ); |
| 258 | |
| 259 | ret = x509parse_keyfile( &rsa, opt.filename, NULL ); |
| 260 | |
| 261 | if( ret != 0 ) |
| 262 | { |
| 263 | #ifdef POLARSSL_ERROR_C |
| 264 | error_strerror( ret, buf, 1024 ); |
| 265 | #endif |
| 266 | printf( " failed\n ! x509parse_key returned %d - %s\n\n", ret, buf ); |
| 267 | rsa_free( &rsa ); |
| 268 | goto exit; |
| 269 | } |
| 270 | |
| 271 | printf( " ok\n" ); |
| 272 | |
| 273 | /* |
| 274 | * 1.2 Print the key |
| 275 | */ |
| 276 | printf( " . Key information ...\n" ); |
| 277 | mpi_write_file( "N: ", &rsa.N, 16, NULL ); |
| 278 | mpi_write_file( "E: ", &rsa.E, 16, NULL ); |
| 279 | mpi_write_file( "D: ", &rsa.D, 16, NULL ); |
| 280 | mpi_write_file( "P: ", &rsa.P, 16, NULL ); |
| 281 | mpi_write_file( "Q: ", &rsa.Q, 16, NULL ); |
| 282 | mpi_write_file( "DP: ", &rsa.DP, 16, NULL ); |
| 283 | mpi_write_file( "DQ: ", &rsa.DQ, 16, NULL ); |
| 284 | mpi_write_file( "QP: ", &rsa.QP, 16, NULL ); |
| 285 | |
| 286 | } |
| 287 | else if( opt.mode == MODE_PUBLIC ) |
| 288 | { |
| 289 | /* |
| 290 | * 1.1. Load the key |
| 291 | */ |
| 292 | printf( "\n . Loading the public key ..." ); |
| 293 | fflush( stdout ); |
| 294 | |
| 295 | ret = x509parse_public_keyfile( &rsa, opt.filename ); |
| 296 | |
| 297 | if( ret != 0 ) |
| 298 | { |
| 299 | #ifdef POLARSSL_ERROR_C |
| 300 | error_strerror( ret, buf, 1024 ); |
| 301 | #endif |
| 302 | printf( " failed\n ! x509parse_public_key returned %d - %s\n\n", ret, buf ); |
| 303 | rsa_free( &rsa ); |
| 304 | goto exit; |
| 305 | } |
| 306 | |
| 307 | printf( " ok\n" ); |
| 308 | |
| 309 | /* |
| 310 | * 1.2 Print the key |
| 311 | */ |
| 312 | printf( " . Key information ...\n" ); |
| 313 | mpi_write_file( "N: ", &rsa.N, 16, NULL ); |
| 314 | mpi_write_file( "E: ", &rsa.E, 16, NULL ); |
| 315 | } |
| 316 | else |
| 317 | goto usage; |
| 318 | |
| 319 | if( opt.output_mode == OUTPUT_MODE_PUBLIC ) |
| 320 | { |
| 321 | write_public_key( &rsa, opt.output_file ); |
| 322 | } |
| 323 | if( opt.output_mode == OUTPUT_MODE_PRIVATE ) |
| 324 | { |
| 325 | write_private_key( &rsa, opt.output_file ); |
| 326 | } |
| 327 | |
| 328 | exit: |
| 329 | |
| 330 | rsa_free( &rsa ); |
| 331 | |
| 332 | #if defined(_WIN32) |
| 333 | printf( " + Press Enter to exit this program.\n" ); |
| 334 | fflush( stdout ); getchar(); |
| 335 | #endif |
| 336 | |
| 337 | return( ret ); |
| 338 | } |
| 339 | #endif /* POLARSSL_BIGNUM_C && POLARSSL_RSA_C && |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 340 | POLARSSL_X509_WRITE_C && POLARSSL_FS_IO */ |