| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Convert PEM to DER | 
|  | 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 | 8adf13b | 2013-08-25 14:50:09 +0200 | [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 | 8adf13b | 2013-08-25 14:50:09 +0200 | [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 | 78dabe0 | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 30 | #include <stdlib.h> | 
|  | 31 | #define mbedtls_free            free | 
|  | 32 | #define mbedtls_calloc          calloc | 
|  | 33 | #define mbedtls_printf          printf | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 34 | #define mbedtls_exit            exit | 
| Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 35 | #define MBEDTLS_EXIT_SUCCESS    EXIT_SUCCESS | 
| Andres Amaya Garcia | 78dabe0 | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 36 | #define MBEDTLS_EXIT_FAILURE    EXIT_FAILURE | 
|  | 37 | #endif /* MBEDTLS_PLATFORM_C */ | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 38 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/error.h" | 
|  | 41 | #include "mbedtls/base64.h" | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 42 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 43 | #include <stdio.h> | 
|  | 44 | #include <stdlib.h> | 
|  | 45 | #include <string.h> | 
|  | 46 | #endif | 
|  | 47 |  | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 48 | #define DFL_FILENAME            "file.pem" | 
|  | 49 | #define DFL_OUTPUT_FILENAME     "file.der" | 
|  | 50 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 51 | #define USAGE \ | 
|  | 52 | "\n usage: pem2der param=<>...\n"                   \ | 
|  | 53 | "\n acceptable parameters:\n"                       \ | 
|  | 54 | "    filename=%%s         default: file.pem\n"      \ | 
|  | 55 | "    output_file=%%s      default: file.der\n"      \ | 
|  | 56 | "\n" | 
|  | 57 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | #if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO) | 
| Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 59 | int main( void ) | 
| Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 60 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n"); | 
| k-stachowiak | 776521a | 2019-05-23 09:46:47 +0200 | [diff] [blame] | 62 | mbedtls_exit( 0 ); | 
| Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 63 | } | 
|  | 64 | #else | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 65 |  | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 66 |  | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 67 | /* | 
|  | 68 | * global options | 
|  | 69 | */ | 
|  | 70 | struct options | 
|  | 71 | { | 
| Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 72 | const char *filename;       /* filename of the input file             */ | 
|  | 73 | const char *output_file;    /* where to store the output              */ | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 74 | } opt; | 
|  | 75 |  | 
|  | 76 | int convert_pem_to_der( const unsigned char *input, size_t ilen, | 
|  | 77 | unsigned char *output, size_t *olen ) | 
|  | 78 | { | 
|  | 79 | int ret; | 
|  | 80 | const unsigned char *s1, *s2, *end = input + ilen; | 
|  | 81 | size_t len = 0; | 
|  | 82 |  | 
| Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 83 | s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 84 | if( s1 == NULL ) | 
|  | 85 | return( -1 ); | 
|  | 86 |  | 
| Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 87 | s2 = (unsigned char *) strstr( (const char *) input, "-----END" ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 88 | if( s2 == NULL ) | 
|  | 89 | return( -1 ); | 
|  | 90 |  | 
|  | 91 | s1 += 10; | 
|  | 92 | while( s1 < end && *s1 != '-' ) | 
|  | 93 | s1++; | 
|  | 94 | while( s1 < end && *s1 == '-' ) | 
|  | 95 | s1++; | 
|  | 96 | if( *s1 == '\r' ) s1++; | 
|  | 97 | if( *s1 == '\n' ) s1++; | 
|  | 98 |  | 
|  | 99 | if( s2 <= s1 || s2 > end ) | 
|  | 100 | return( -1 ); | 
|  | 101 |  | 
| Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 102 | ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 ); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER ) | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 104 | return( ret ); | 
|  | 105 |  | 
|  | 106 | if( len > *olen ) | 
|  | 107 | return( -1 ); | 
|  | 108 |  | 
| Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 109 | if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1, | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 110 | s2 - s1 ) ) != 0 ) | 
|  | 111 | { | 
|  | 112 | return( ret ); | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | *olen = len; | 
|  | 116 |  | 
|  | 117 | return( 0 ); | 
|  | 118 | } | 
|  | 119 |  | 
|  | 120 | /* | 
|  | 121 | * Load all data from a file into a given buffer. | 
|  | 122 | */ | 
|  | 123 | static int load_file( const char *path, unsigned char **buf, size_t *n ) | 
|  | 124 | { | 
|  | 125 | FILE *f; | 
|  | 126 | long size; | 
|  | 127 |  | 
|  | 128 | if( ( f = fopen( path, "rb" ) ) == NULL ) | 
|  | 129 | return( -1 ); | 
|  | 130 |  | 
|  | 131 | fseek( f, 0, SEEK_END ); | 
|  | 132 | if( ( size = ftell( f ) ) == -1 ) | 
|  | 133 | { | 
|  | 134 | fclose( f ); | 
|  | 135 | return( -1 ); | 
|  | 136 | } | 
|  | 137 | fseek( f, 0, SEEK_SET ); | 
|  | 138 |  | 
|  | 139 | *n = (size_t) size; | 
|  | 140 |  | 
|  | 141 | if( *n + 1 == 0 || | 
| Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 142 | ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL ) | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 143 | { | 
|  | 144 | fclose( f ); | 
|  | 145 | return( -1 ); | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | if( fread( *buf, 1, *n, f ) != *n ) | 
|  | 149 | { | 
|  | 150 | fclose( f ); | 
|  | 151 | free( *buf ); | 
| Alfred Klomp | 1d42b3e | 2014-07-14 22:09:21 +0200 | [diff] [blame] | 152 | *buf = NULL; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 153 | return( -1 ); | 
|  | 154 | } | 
|  | 155 |  | 
|  | 156 | fclose( f ); | 
|  | 157 |  | 
|  | 158 | (*buf)[*n] = '\0'; | 
|  | 159 |  | 
|  | 160 | return( 0 ); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | /* | 
|  | 164 | * Write buffer to a file | 
|  | 165 | */ | 
|  | 166 | static int write_file( const char *path, unsigned char *buf, size_t n ) | 
|  | 167 | { | 
|  | 168 | FILE *f; | 
|  | 169 |  | 
|  | 170 | if( ( f = fopen( path, "wb" ) ) == NULL ) | 
|  | 171 | return( -1 ); | 
|  | 172 |  | 
|  | 173 | if( fwrite( buf, 1, n, f ) != n ) | 
|  | 174 | { | 
|  | 175 | fclose( f ); | 
|  | 176 | return( -1 ); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | fclose( f ); | 
|  | 180 | return( 0 ); | 
|  | 181 | } | 
|  | 182 |  | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 183 | int main( int argc, char *argv[] ) | 
|  | 184 | { | 
| Andres Amaya Garcia | 78dabe0 | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 185 | int ret = 1; | 
|  | 186 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 187 | unsigned char *pem_buffer = NULL; | 
|  | 188 | unsigned char der_buffer[4096]; | 
|  | 189 | char buf[1024]; | 
|  | 190 | size_t pem_size, der_size = sizeof(der_buffer); | 
| Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 191 | int i; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 192 | char *p, *q; | 
|  | 193 |  | 
|  | 194 | /* | 
|  | 195 | * Set to sane values | 
|  | 196 | */ | 
|  | 197 | memset( buf, 0, sizeof(buf) ); | 
|  | 198 | memset( der_buffer, 0, sizeof(der_buffer) ); | 
|  | 199 |  | 
|  | 200 | if( argc == 0 ) | 
|  | 201 | { | 
|  | 202 | usage: | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | mbedtls_printf( USAGE ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 204 | goto exit; | 
|  | 205 | } | 
|  | 206 |  | 
|  | 207 | opt.filename            = DFL_FILENAME; | 
|  | 208 | opt.output_file         = DFL_OUTPUT_FILENAME; | 
|  | 209 |  | 
|  | 210 | for( i = 1; i < argc; i++ ) | 
|  | 211 | { | 
|  | 212 |  | 
|  | 213 | p = argv[i]; | 
|  | 214 | if( ( q = strchr( p, '=' ) ) == NULL ) | 
|  | 215 | goto usage; | 
|  | 216 | *q++ = '\0'; | 
|  | 217 |  | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 218 | if( strcmp( p, "filename" ) == 0 ) | 
|  | 219 | opt.filename = q; | 
|  | 220 | else if( strcmp( p, "output_file" ) == 0 ) | 
|  | 221 | opt.output_file = q; | 
|  | 222 | else | 
|  | 223 | goto usage; | 
|  | 224 | } | 
|  | 225 |  | 
|  | 226 | /* | 
|  | 227 | * 1.1. Load the PEM file | 
|  | 228 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | mbedtls_printf( "\n  . Loading the PEM file ..." ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 230 | fflush( stdout ); | 
|  | 231 |  | 
|  | 232 | ret = load_file( opt.filename, &pem_buffer, &pem_size ); | 
|  | 233 |  | 
|  | 234 | if( ret != 0 ) | 
|  | 235 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 236 | #ifdef MBEDTLS_ERROR_C | 
|  | 237 | mbedtls_strerror( ret, buf, 1024 ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 238 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 239 | mbedtls_printf( " failed\n  !  load_file returned %d - %s\n\n", ret, buf ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 240 | goto exit; | 
|  | 241 | } | 
|  | 242 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 243 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 244 |  | 
|  | 245 | /* | 
|  | 246 | * 1.2. Convert from PEM to DER | 
|  | 247 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | mbedtls_printf( "  . Converting from PEM to DER ..." ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 249 | fflush( stdout ); | 
|  | 250 |  | 
|  | 251 | if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 ) | 
|  | 252 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 253 | #ifdef MBEDTLS_ERROR_C | 
|  | 254 | mbedtls_strerror( ret, buf, 1024 ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 255 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 256 | mbedtls_printf( " failed\n  !  convert_pem_to_der %d - %s\n\n", ret, buf ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 257 | goto exit; | 
|  | 258 | } | 
|  | 259 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 261 |  | 
|  | 262 | /* | 
|  | 263 | * 1.3. Write the DER file | 
|  | 264 | */ | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | mbedtls_printf( "  . Writing the DER file ..." ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 266 | fflush( stdout ); | 
|  | 267 |  | 
|  | 268 | ret = write_file( opt.output_file, der_buffer, der_size ); | 
|  | 269 |  | 
|  | 270 | if( ret != 0 ) | 
|  | 271 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | #ifdef MBEDTLS_ERROR_C | 
|  | 273 | mbedtls_strerror( ret, buf, 1024 ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 274 | #endif | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 275 | mbedtls_printf( " failed\n  !  write_file returned %d - %s\n\n", ret, buf ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 276 | goto exit; | 
|  | 277 | } | 
|  | 278 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | mbedtls_printf( " ok\n" ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 280 |  | 
| Andres Amaya Garcia | 78dabe0 | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 281 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 282 |  | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 283 | exit: | 
|  | 284 | free( pem_buffer ); | 
|  | 285 |  | 
|  | 286 | #if defined(_WIN32) | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 287 | mbedtls_printf( "  + Press Enter to exit this program.\n" ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 288 | fflush( stdout ); getchar(); | 
|  | 289 | #endif | 
|  | 290 |  | 
| k-stachowiak | 776521a | 2019-05-23 09:46:47 +0200 | [diff] [blame] | 291 | mbedtls_exit( exit_code ); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 292 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */ |