Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Convert PEM to DER |
| 3 | * |
Bence Szépkúti | 44bfbe3 | 2020-08-19 16:54:51 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | 4e9f712 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 45 | */ |
| 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 49 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #endif |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 55 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 56 | #include <stdio.h> |
Andres Amaya Garcia | 6118ada | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 57 | #include <stdlib.h> |
| 58 | #define mbedtls_free free |
| 59 | #define mbedtls_calloc calloc |
| 60 | #define mbedtls_printf printf |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 61 | #define mbedtls_exit exit |
Andres Amaya Garcia | 2b0599b | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 62 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 6118ada | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 63 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 64 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 65 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 67 | #include "mbedtls/error.h" |
| 68 | #include "mbedtls/base64.h" |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 69 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 70 | #include <stdio.h> |
| 71 | #include <stdlib.h> |
| 72 | #include <string.h> |
| 73 | #endif |
| 74 | |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 75 | #define DFL_FILENAME "file.pem" |
| 76 | #define DFL_OUTPUT_FILENAME "file.der" |
| 77 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 78 | #define USAGE \ |
| 79 | "\n usage: pem2der param=<>...\n" \ |
| 80 | "\n acceptable parameters:\n" \ |
| 81 | " filename=%%s default: file.pem\n" \ |
| 82 | " output_file=%%s default: file.der\n" \ |
| 83 | "\n" |
| 84 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 85 | #if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 86 | int main( void ) |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 87 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n"); |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 89 | mbedtls_exit( 0 ); |
Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 90 | } |
| 91 | #else |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 92 | /* |
| 93 | * global options |
| 94 | */ |
| 95 | struct options |
| 96 | { |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 97 | const char *filename; /* filename of the input file */ |
| 98 | const char *output_file; /* where to store the output */ |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 99 | } opt; |
| 100 | |
| 101 | int convert_pem_to_der( const unsigned char *input, size_t ilen, |
| 102 | unsigned char *output, size_t *olen ) |
| 103 | { |
| 104 | int ret; |
| 105 | const unsigned char *s1, *s2, *end = input + ilen; |
| 106 | size_t len = 0; |
| 107 | |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 108 | s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 109 | if( s1 == NULL ) |
| 110 | return( -1 ); |
| 111 | |
Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 112 | s2 = (unsigned char *) strstr( (const char *) input, "-----END" ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 113 | if( s2 == NULL ) |
| 114 | return( -1 ); |
| 115 | |
| 116 | s1 += 10; |
| 117 | while( s1 < end && *s1 != '-' ) |
| 118 | s1++; |
| 119 | while( s1 < end && *s1 == '-' ) |
| 120 | s1++; |
| 121 | if( *s1 == '\r' ) s1++; |
| 122 | if( *s1 == '\n' ) s1++; |
| 123 | |
| 124 | if( s2 <= s1 || s2 > end ) |
| 125 | return( -1 ); |
| 126 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 127 | 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] | 128 | if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER ) |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 129 | return( ret ); |
| 130 | |
| 131 | if( len > *olen ) |
| 132 | return( -1 ); |
| 133 | |
Manuel Pégourié-Gonnard | ba56136 | 2015-06-02 16:30:35 +0100 | [diff] [blame] | 134 | if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1, |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 135 | s2 - s1 ) ) != 0 ) |
| 136 | { |
| 137 | return( ret ); |
| 138 | } |
| 139 | |
| 140 | *olen = len; |
| 141 | |
| 142 | return( 0 ); |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Load all data from a file into a given buffer. |
| 147 | */ |
| 148 | static int load_file( const char *path, unsigned char **buf, size_t *n ) |
| 149 | { |
| 150 | FILE *f; |
| 151 | long size; |
| 152 | |
| 153 | if( ( f = fopen( path, "rb" ) ) == NULL ) |
| 154 | return( -1 ); |
| 155 | |
| 156 | fseek( f, 0, SEEK_END ); |
| 157 | if( ( size = ftell( f ) ) == -1 ) |
| 158 | { |
| 159 | fclose( f ); |
| 160 | return( -1 ); |
| 161 | } |
| 162 | fseek( f, 0, SEEK_SET ); |
| 163 | |
| 164 | *n = (size_t) size; |
| 165 | |
| 166 | if( *n + 1 == 0 || |
Manuel Pégourié-Gonnard | 7551cb9 | 2015-05-26 16:04:06 +0200 | [diff] [blame] | 167 | ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL ) |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 168 | { |
| 169 | fclose( f ); |
| 170 | return( -1 ); |
| 171 | } |
| 172 | |
| 173 | if( fread( *buf, 1, *n, f ) != *n ) |
| 174 | { |
| 175 | fclose( f ); |
| 176 | free( *buf ); |
Alfred Klomp | 1d42b3e | 2014-07-14 22:09:21 +0200 | [diff] [blame] | 177 | *buf = NULL; |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 178 | return( -1 ); |
| 179 | } |
| 180 | |
| 181 | fclose( f ); |
| 182 | |
| 183 | (*buf)[*n] = '\0'; |
| 184 | |
| 185 | return( 0 ); |
| 186 | } |
| 187 | |
| 188 | /* |
| 189 | * Write buffer to a file |
| 190 | */ |
| 191 | static int write_file( const char *path, unsigned char *buf, size_t n ) |
| 192 | { |
| 193 | FILE *f; |
| 194 | |
| 195 | if( ( f = fopen( path, "wb" ) ) == NULL ) |
| 196 | return( -1 ); |
| 197 | |
| 198 | if( fwrite( buf, 1, n, f ) != n ) |
| 199 | { |
| 200 | fclose( f ); |
| 201 | return( -1 ); |
| 202 | } |
| 203 | |
| 204 | fclose( f ); |
| 205 | return( 0 ); |
| 206 | } |
| 207 | |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 208 | int main( int argc, char *argv[] ) |
| 209 | { |
Andres Amaya Garcia | 6118ada | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 210 | int ret = 1; |
| 211 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 212 | unsigned char *pem_buffer = NULL; |
| 213 | unsigned char der_buffer[4096]; |
| 214 | char buf[1024]; |
| 215 | size_t pem_size, der_size = sizeof(der_buffer); |
Paul Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 216 | int i; |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 217 | char *p, *q; |
| 218 | |
| 219 | /* |
| 220 | * Set to sane values |
| 221 | */ |
| 222 | memset( buf, 0, sizeof(buf) ); |
| 223 | memset( der_buffer, 0, sizeof(der_buffer) ); |
| 224 | |
| 225 | if( argc == 0 ) |
| 226 | { |
| 227 | usage: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | mbedtls_printf( USAGE ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 229 | goto exit; |
| 230 | } |
| 231 | |
| 232 | opt.filename = DFL_FILENAME; |
| 233 | opt.output_file = DFL_OUTPUT_FILENAME; |
| 234 | |
| 235 | for( i = 1; i < argc; i++ ) |
| 236 | { |
| 237 | |
| 238 | p = argv[i]; |
| 239 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 240 | goto usage; |
| 241 | *q++ = '\0'; |
| 242 | |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 243 | if( strcmp( p, "filename" ) == 0 ) |
| 244 | opt.filename = q; |
| 245 | else if( strcmp( p, "output_file" ) == 0 ) |
| 246 | opt.output_file = q; |
| 247 | else |
| 248 | goto usage; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * 1.1. Load the PEM file |
| 253 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | mbedtls_printf( "\n . Loading the PEM file ..." ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 255 | fflush( stdout ); |
| 256 | |
| 257 | ret = load_file( opt.filename, &pem_buffer, &pem_size ); |
| 258 | |
| 259 | if( ret != 0 ) |
| 260 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 261 | #ifdef MBEDTLS_ERROR_C |
| 262 | mbedtls_strerror( ret, buf, 1024 ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 263 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 264 | 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] | 265 | goto exit; |
| 266 | } |
| 267 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 269 | |
| 270 | /* |
| 271 | * 1.2. Convert from PEM to DER |
| 272 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 273 | mbedtls_printf( " . Converting from PEM to DER ..." ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 274 | fflush( stdout ); |
| 275 | |
| 276 | if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 ) |
| 277 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | #ifdef MBEDTLS_ERROR_C |
| 279 | mbedtls_strerror( ret, buf, 1024 ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 280 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | 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] | 282 | goto exit; |
| 283 | } |
| 284 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 285 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 286 | |
| 287 | /* |
| 288 | * 1.3. Write the DER file |
| 289 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | mbedtls_printf( " . Writing the DER file ..." ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 291 | fflush( stdout ); |
| 292 | |
| 293 | ret = write_file( opt.output_file, der_buffer, der_size ); |
| 294 | |
| 295 | if( ret != 0 ) |
| 296 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | #ifdef MBEDTLS_ERROR_C |
| 298 | mbedtls_strerror( ret, buf, 1024 ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 299 | #endif |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 300 | 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] | 301 | goto exit; |
| 302 | } |
| 303 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 304 | mbedtls_printf( " ok\n" ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 305 | |
Andres Amaya Garcia | 6118ada | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 306 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 307 | |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 308 | exit: |
| 309 | free( pem_buffer ); |
| 310 | |
| 311 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 312 | mbedtls_printf( " + Press Enter to exit this program.\n" ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 313 | fflush( stdout ); getchar(); |
| 314 | #endif |
| 315 | |
Krzysztof Stachowiak | a086522 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 316 | mbedtls_exit( exit_code ); |
Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 317 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 318 | #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */ |