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