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