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 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 |
| 6 | * |
| 7 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 8 | * not use this file except in compliance with the License. |
| 9 | * You may obtain a copy of the License at |
| 10 | * |
| 11 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | * |
| 13 | * Unless required by applicable law or agreed to in writing, software |
| 14 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | * See the License for the specific language governing permissions and |
| 17 | * limitations under the License. |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 22 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #endif |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 25 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 26 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PK_WRITE_C) && defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/error.h" |
| 30 | #include "mbedtls/pk.h" |
| 31 | #include "mbedtls/error.h" |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 32 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #endif |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 36 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 37 | #if defined(MBEDTLS_PEM_WRITE_C) |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 38 | #define USAGE_OUT \ |
| 39 | " output_file=%%s default: keyfile.pem\n" \ |
| 40 | " output_format=pem|der default: pem\n" |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 41 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 42 | #define USAGE_OUT \ |
| 43 | " output_file=%%s default: keyfile.der\n" \ |
| 44 | " output_format=der default: der\n" |
| 45 | #endif |
| 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #if defined(MBEDTLS_PEM_WRITE_C) |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 48 | #define DFL_OUTPUT_FILENAME "keyfile.pem" |
| 49 | #define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_PEM |
| 50 | #else |
| 51 | #define DFL_OUTPUT_FILENAME "keyfile.der" |
| 52 | #define DFL_OUTPUT_FORMAT OUTPUT_FORMAT_DER |
| 53 | #endif |
| 54 | |
| 55 | #define DFL_MODE MODE_NONE |
| 56 | #define DFL_FILENAME "keyfile.key" |
| 57 | #define DFL_DEBUG_LEVEL 0 |
| 58 | #define DFL_OUTPUT_MODE OUTPUT_MODE_NONE |
Paul Bakker | ed27a04 | 2013-04-18 22:46:23 +0200 | [diff] [blame] | 59 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 60 | #define MODE_NONE 0 |
| 61 | #define MODE_PRIVATE 1 |
| 62 | #define MODE_PUBLIC 2 |
| 63 | |
| 64 | #define OUTPUT_MODE_NONE 0 |
| 65 | #define OUTPUT_MODE_PRIVATE 1 |
| 66 | #define OUTPUT_MODE_PUBLIC 2 |
| 67 | |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 68 | #define OUTPUT_FORMAT_PEM 0 |
| 69 | #define OUTPUT_FORMAT_DER 1 |
| 70 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 71 | #define USAGE \ |
Hanno Becker | 40371ec | 2017-08-23 06:46:17 +0100 | [diff] [blame] | 72 | "\n usage: key_app_writer param=<>...\n" \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 73 | "\n acceptable parameters:\n" \ |
| 74 | " mode=private|public default: none\n" \ |
| 75 | " filename=%%s default: keyfile.key\n" \ |
| 76 | " output_mode=private|public default: none\n" \ |
| 77 | USAGE_OUT \ |
| 78 | "\n" |
Manuel Pégourié-Gonnard | 6c5abfa | 2015-02-13 14:12:07 +0000 | [diff] [blame] | 79 | |
Hanno Becker | b14c331 | 2018-10-16 13:45:22 +0100 | [diff] [blame] | 80 | #if !defined(MBEDTLS_PK_PARSE_C) || \ |
| 81 | !defined(MBEDTLS_PK_WRITE_C) || \ |
| 82 | !defined(MBEDTLS_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 83 | int main( void ) |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 84 | { |
Hanno Becker | b14c331 | 2018-10-16 13:45:22 +0100 | [diff] [blame] | 85 | mbedtls_printf( "MBEDTLS_PK_PARSE_C and/or MBEDTLS_PK_WRITE_C and/or MBEDTLS_FS_IO not defined.\n" ); |
Krzysztof Stachowiak | 5e1b195 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 86 | mbedtls_exit( 0 ); |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 87 | } |
| 88 | #else |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 89 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 90 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 91 | /* |
| 92 | * global options |
| 93 | */ |
| 94 | struct options |
| 95 | { |
| 96 | int mode; /* the mode to run the application in */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 97 | const char *filename; /* filename of the key file */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 98 | int output_mode; /* the output mode to use */ |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 99 | const char *output_file; /* where to store the constructed key file */ |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 100 | int output_format; /* the output format to use */ |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 101 | } opt; |
| 102 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | static int write_public_key( mbedtls_pk_context *key, const char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 104 | { |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 105 | int ret; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 106 | FILE *f; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 107 | unsigned char output_buf[16000]; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 108 | unsigned char *c = output_buf; |
| 109 | size_t len = 0; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 110 | |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 111 | memset(output_buf, 0, 16000); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 112 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | #if defined(MBEDTLS_PEM_WRITE_C) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 114 | if( opt.output_format == OUTPUT_FORMAT_PEM ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 115 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | if( ( ret = mbedtls_pk_write_pubkey_pem( key, output_buf, 16000 ) ) != 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 117 | return( ret ); |
| 118 | |
| 119 | len = strlen( (char *) output_buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 120 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 121 | else |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 122 | #endif |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 123 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | if( ( ret = mbedtls_pk_write_pubkey_der( key, output_buf, 16000 ) ) < 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 125 | return( ret ); |
| 126 | |
| 127 | len = ret; |
Ron Eldor | bb51cb3 | 2018-01-07 18:10:43 +0200 | [diff] [blame] | 128 | c = output_buf + sizeof(output_buf) - len; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 132 | return( -1 ); |
| 133 | |
| 134 | if( fwrite( c, 1, len, f ) != len ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 135 | { |
| 136 | fclose( f ); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 137 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 138 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 139 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 140 | fclose( f ); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 141 | |
| 142 | return( 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | static int write_private_key( mbedtls_pk_context *key, const char *output_file ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 146 | { |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 147 | int ret; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 148 | FILE *f; |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 149 | unsigned char output_buf[16000]; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 150 | unsigned char *c = output_buf; |
| 151 | size_t len = 0; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 152 | |
Paul Bakker | 1d56958 | 2012-10-03 20:35:44 +0000 | [diff] [blame] | 153 | memset(output_buf, 0, 16000); |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 154 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | #if defined(MBEDTLS_PEM_WRITE_C) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 156 | if( opt.output_format == OUTPUT_FORMAT_PEM ) |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 157 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | if( ( ret = mbedtls_pk_write_key_pem( key, output_buf, 16000 ) ) != 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 159 | return( ret ); |
| 160 | |
| 161 | len = strlen( (char *) output_buf ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 162 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 163 | else |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 164 | #endif |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 165 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | if( ( ret = mbedtls_pk_write_key_der( key, output_buf, 16000 ) ) < 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 167 | return( ret ); |
| 168 | |
| 169 | len = ret; |
Christian Walther | a92c545 | 2018-11-28 13:32:27 +0100 | [diff] [blame] | 170 | c = output_buf + sizeof(output_buf) - len; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | if( ( f = fopen( output_file, "w" ) ) == NULL ) |
| 174 | return( -1 ); |
| 175 | |
| 176 | if( fwrite( c, 1, len, f ) != len ) |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 177 | { |
| 178 | fclose( f ); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 179 | return( -1 ); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 180 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 181 | |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 182 | fclose( f ); |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 183 | |
| 184 | return( 0 ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 187 | int main( int argc, char *argv[] ) |
| 188 | { |
Andres Amaya Garcia | ed68488 | 2018-04-29 20:07:30 +0100 | [diff] [blame] | 189 | int ret = 1; |
| 190 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Gilles Peskine | 3f0722c | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 191 | #if defined(MBEDTLS_ERROR_C) |
| 192 | char buf[200]; |
| 193 | #endif |
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 | |
Hanno Becker | 40371ec | 2017-08-23 06:46:17 +0100 | [diff] [blame] | 197 | mbedtls_pk_context key; |
| 198 | mbedtls_mpi N, P, Q, D, E, DP, DQ, QP; |
| 199 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 200 | /* |
| 201 | * Set to sane values |
| 202 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | mbedtls_pk_init( &key ); |
Gilles Peskine | 3f0722c | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 204 | #if defined(MBEDTLS_ERROR_C) |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 205 | memset( buf, 0, sizeof( buf ) ); |
Gilles Peskine | 3f0722c | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 206 | #endif |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 207 | |
Hanno Becker | 40371ec | 2017-08-23 06:46:17 +0100 | [diff] [blame] | 208 | mbedtls_mpi_init( &N ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
| 209 | mbedtls_mpi_init( &D ); mbedtls_mpi_init( &E ); mbedtls_mpi_init( &DP ); |
| 210 | mbedtls_mpi_init( &DQ ); mbedtls_mpi_init( &QP ); |
| 211 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 212 | if( argc == 0 ) |
| 213 | { |
| 214 | usage: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 215 | mbedtls_printf( USAGE ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 216 | goto exit; |
| 217 | } |
| 218 | |
| 219 | opt.mode = DFL_MODE; |
| 220 | opt.filename = DFL_FILENAME; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 221 | opt.output_mode = DFL_OUTPUT_MODE; |
| 222 | opt.output_file = DFL_OUTPUT_FILENAME; |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 223 | opt.output_format = DFL_OUTPUT_FORMAT; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 224 | |
| 225 | for( i = 1; i < argc; i++ ) |
| 226 | { |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 227 | p = argv[i]; |
| 228 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 229 | goto usage; |
| 230 | *q++ = '\0'; |
| 231 | |
| 232 | if( strcmp( p, "mode" ) == 0 ) |
| 233 | { |
| 234 | if( strcmp( q, "private" ) == 0 ) |
| 235 | opt.mode = MODE_PRIVATE; |
| 236 | else if( strcmp( q, "public" ) == 0 ) |
| 237 | opt.mode = MODE_PUBLIC; |
| 238 | else |
| 239 | goto usage; |
| 240 | } |
| 241 | else if( strcmp( p, "output_mode" ) == 0 ) |
| 242 | { |
| 243 | if( strcmp( q, "private" ) == 0 ) |
| 244 | opt.output_mode = OUTPUT_MODE_PRIVATE; |
| 245 | else if( strcmp( q, "public" ) == 0 ) |
| 246 | opt.output_mode = OUTPUT_MODE_PUBLIC; |
| 247 | else |
| 248 | goto usage; |
| 249 | } |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 250 | else if( strcmp( p, "output_format" ) == 0 ) |
| 251 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 252 | #if defined(MBEDTLS_PEM_WRITE_C) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 253 | if( strcmp( q, "pem" ) == 0 ) |
| 254 | opt.output_format = OUTPUT_FORMAT_PEM; |
Manuel Pégourié-Gonnard | f9378d8 | 2014-06-24 13:11:25 +0200 | [diff] [blame] | 255 | else |
| 256 | #endif |
| 257 | if( strcmp( q, "der" ) == 0 ) |
Paul Bakker | f3df61a | 2013-08-26 17:22:23 +0200 | [diff] [blame] | 258 | opt.output_format = OUTPUT_FORMAT_DER; |
| 259 | else |
| 260 | goto usage; |
| 261 | } |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 262 | else if( strcmp( p, "filename" ) == 0 ) |
| 263 | opt.filename = q; |
| 264 | else if( strcmp( p, "output_file" ) == 0 ) |
| 265 | opt.output_file = q; |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 266 | else |
| 267 | goto usage; |
| 268 | } |
| 269 | |
| 270 | if( opt.mode == MODE_NONE && opt.output_mode != OUTPUT_MODE_NONE ) |
| 271 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | mbedtls_printf( "\nCannot output a key without reading one.\n"); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 273 | goto exit; |
| 274 | } |
| 275 | |
| 276 | if( opt.mode == MODE_PUBLIC && opt.output_mode == OUTPUT_MODE_PRIVATE ) |
| 277 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | mbedtls_printf( "\nCannot output a private key from a public key.\n"); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 279 | goto exit; |
| 280 | } |
| 281 | |
| 282 | if( opt.mode == MODE_PRIVATE ) |
| 283 | { |
| 284 | /* |
| 285 | * 1.1. Load the key |
| 286 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | mbedtls_printf( "\n . Loading the private key ..." ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 288 | fflush( stdout ); |
| 289 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | ret = mbedtls_pk_parse_keyfile( &key, opt.filename, NULL ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 291 | |
| 292 | if( ret != 0 ) |
| 293 | { |
Gilles Peskine | 3f0722c | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 294 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%04x", (unsigned int) -ret ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 295 | goto exit; |
| 296 | } |
| 297 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 298 | mbedtls_printf( " ok\n" ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 299 | |
| 300 | /* |
| 301 | * 1.2 Print the key |
| 302 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | mbedtls_printf( " . Key information ...\n" ); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 304 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | #if defined(MBEDTLS_RSA_C) |
| 306 | if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA ) |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 307 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 308 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key ); |
Hanno Becker | 40371ec | 2017-08-23 06:46:17 +0100 | [diff] [blame] | 309 | |
| 310 | if( ( ret = mbedtls_rsa_export ( rsa, &N, &P, &Q, &D, &E ) ) != 0 || |
| 311 | ( ret = mbedtls_rsa_export_crt( rsa, &DP, &DQ, &QP ) ) != 0 ) |
| 312 | { |
| 313 | mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); |
| 314 | goto exit; |
| 315 | } |
| 316 | |
| 317 | mbedtls_mpi_write_file( "N: ", &N, 16, NULL ); |
| 318 | mbedtls_mpi_write_file( "E: ", &E, 16, NULL ); |
| 319 | mbedtls_mpi_write_file( "D: ", &D, 16, NULL ); |
| 320 | mbedtls_mpi_write_file( "P: ", &P, 16, NULL ); |
| 321 | mbedtls_mpi_write_file( "Q: ", &Q, 16, NULL ); |
| 322 | mbedtls_mpi_write_file( "DP: ", &DP, 16, NULL ); |
| 323 | mbedtls_mpi_write_file( "DQ: ", &DQ, 16, NULL ); |
| 324 | mbedtls_mpi_write_file( "QP: ", &QP, 16, NULL ); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 325 | } |
| 326 | else |
| 327 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | #if defined(MBEDTLS_ECP_C) |
| 329 | if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY ) |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 330 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 331 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key ); |
| 332 | mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); |
| 333 | mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); |
| 334 | mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); |
| 335 | mbedtls_mpi_write_file( "D : ", &ecp->d , 16, NULL ); |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 336 | } |
| 337 | else |
| 338 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 339 | mbedtls_printf("key type not supported yet\n"); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 340 | |
| 341 | } |
| 342 | else if( opt.mode == MODE_PUBLIC ) |
| 343 | { |
| 344 | /* |
| 345 | * 1.1. Load the key |
| 346 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 347 | mbedtls_printf( "\n . Loading the public key ..." ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 348 | fflush( stdout ); |
| 349 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | ret = mbedtls_pk_parse_public_keyfile( &key, opt.filename ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 351 | |
| 352 | if( ret != 0 ) |
| 353 | { |
Gilles Peskine | 3f0722c | 2021-08-06 14:37:01 +0200 | [diff] [blame] | 354 | mbedtls_printf( " failed\n ! mbedtls_pk_parse_public_key returned -0x%04x", (unsigned int) -ret ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 355 | goto exit; |
| 356 | } |
| 357 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | mbedtls_printf( " ok\n" ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 359 | |
| 360 | /* |
| 361 | * 1.2 Print the key |
| 362 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 363 | mbedtls_printf( " . Key information ...\n" ); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 364 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 365 | #if defined(MBEDTLS_RSA_C) |
| 366 | if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_RSA ) |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 367 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | mbedtls_rsa_context *rsa = mbedtls_pk_rsa( key ); |
Hanno Becker | 40371ec | 2017-08-23 06:46:17 +0100 | [diff] [blame] | 369 | |
| 370 | if( ( ret = mbedtls_rsa_export( rsa, &N, NULL, NULL, |
| 371 | NULL, &E ) ) != 0 ) |
| 372 | { |
| 373 | mbedtls_printf( " failed\n ! could not export RSA parameters\n\n" ); |
| 374 | goto exit; |
| 375 | } |
| 376 | mbedtls_mpi_write_file( "N: ", &N, 16, NULL ); |
| 377 | mbedtls_mpi_write_file( "E: ", &E, 16, NULL ); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 378 | } |
| 379 | else |
| 380 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | #if defined(MBEDTLS_ECP_C) |
| 382 | if( mbedtls_pk_get_type( &key ) == MBEDTLS_PK_ECKEY ) |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 383 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 384 | mbedtls_ecp_keypair *ecp = mbedtls_pk_ec( key ); |
| 385 | mbedtls_mpi_write_file( "Q(X): ", &ecp->Q.X, 16, NULL ); |
| 386 | mbedtls_mpi_write_file( "Q(Y): ", &ecp->Q.Y, 16, NULL ); |
| 387 | mbedtls_mpi_write_file( "Q(Z): ", &ecp->Q.Z, 16, NULL ); |
Paul Bakker | 2e24ca7 | 2013-09-18 15:21:27 +0200 | [diff] [blame] | 388 | } |
| 389 | else |
| 390 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | mbedtls_printf("key type not supported yet\n"); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 392 | } |
| 393 | else |
| 394 | goto usage; |
| 395 | |
| 396 | if( opt.output_mode == OUTPUT_MODE_PUBLIC ) |
| 397 | { |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 398 | write_public_key( &key, opt.output_file ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 399 | } |
| 400 | if( opt.output_mode == OUTPUT_MODE_PRIVATE ) |
| 401 | { |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 402 | write_private_key( &key, opt.output_file ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Andres Amaya Garcia | ed68488 | 2018-04-29 20:07:30 +0100 | [diff] [blame] | 405 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 406 | |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 407 | exit: |
| 408 | |
Andres Amaya Garcia | ed68488 | 2018-04-29 20:07:30 +0100 | [diff] [blame] | 409 | if( exit_code != MBEDTLS_EXIT_SUCCESS ) |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 410 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 411 | #ifdef MBEDTLS_ERROR_C |
| 412 | mbedtls_strerror( ret, buf, sizeof( buf ) ); |
| 413 | mbedtls_printf( " - %s\n", buf ); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 414 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_printf("\n"); |
Manuel Pégourié-Gonnard | 26b4d45 | 2013-09-12 06:56:06 +0200 | [diff] [blame] | 416 | #endif |
| 417 | } |
| 418 | |
Hanno Becker | 40371ec | 2017-08-23 06:46:17 +0100 | [diff] [blame] | 419 | mbedtls_mpi_free( &N ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 420 | mbedtls_mpi_free( &D ); mbedtls_mpi_free( &E ); mbedtls_mpi_free( &DP ); |
| 421 | mbedtls_mpi_free( &DQ ); mbedtls_mpi_free( &QP ); |
| 422 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | mbedtls_pk_free( &key ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 424 | |
| 425 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 426 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 427 | fflush( stdout ); getchar(); |
| 428 | #endif |
| 429 | |
Krzysztof Stachowiak | 5e1b195 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 430 | mbedtls_exit( exit_code ); |
Paul Bakker | bdb912d | 2012-02-13 23:11:30 +0000 | [diff] [blame] | 431 | } |
Hanno Becker | b14c331 | 2018-10-16 13:45:22 +0100 | [diff] [blame] | 432 | #endif /* MBEDTLS_PK_PARSE_C && MBEDTLS_PK_WRITE_C && MBEDTLS_FS_IO */ |