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