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