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