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