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