4 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * This file is part of mbed TLS (https://tls.mbed.org)
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
25 #include MBEDTLS_CONFIG_FILE
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
33 #define mbedtls_free free
34 #define mbedtls_calloc calloc
35 #define mbedtls_printf printf
36 #define mbedtls_exit exit
37 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
38 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
39 #endif /* MBEDTLS_PLATFORM_C */
41 #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
42 #include "mbedtls/error.h"
43 #include "mbedtls/base64.h"
50 #define DFL_FILENAME "file.pem"
51 #define DFL_OUTPUT_FILENAME "file.der"
54 "\n usage: pem2der param=<>...\n" \
55 "\n acceptable parameters:\n" \
56 " filename=%%s default: file.pem\n" \
57 " output_file=%%s default: file.der\n" \
60 #if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
63 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
68 #if defined(MBEDTLS_CHECK_PARAMS)
69 #define mbedtls_exit exit
70 void mbedtls_param_failed( const char *failure_condition,
74 mbedtls_printf( "%s:%i: Input param failed - %s\n",
75 file, line, failure_condition );
76 mbedtls_exit( MBEDTLS_EXIT_FAILURE );
85 const char *filename; /* filename of the input file */
86 const char *output_file; /* where to store the output */
89 int convert_pem_to_der( const unsigned char *input, size_t ilen,
90 unsigned char *output, size_t *olen )
93 const unsigned char *s1, *s2, *end = input + ilen;
96 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
100 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
105 while( s1 < end && *s1 != '-' )
107 while( s1 < end && *s1 == '-' )
109 if( *s1 == '\r' ) s1++;
110 if( *s1 == '\n' ) s1++;
112 if( s2 <= s1 || s2 > end )
115 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
116 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
122 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
134 * Load all data from a file into a given buffer.
136 static int load_file( const char *path, unsigned char **buf, size_t *n )
141 if( ( f = fopen( path, "rb" ) ) == NULL )
144 fseek( f, 0, SEEK_END );
145 if( ( size = ftell( f ) ) == -1 )
150 fseek( f, 0, SEEK_SET );
155 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
161 if( fread( *buf, 1, *n, f ) != *n )
177 * Write buffer to a file
179 static int write_file( const char *path, unsigned char *buf, size_t n )
183 if( ( f = fopen( path, "wb" ) ) == NULL )
186 if( fwrite( buf, 1, n, f ) != n )
196 int main( int argc, char *argv[] )
199 int exit_code = MBEDTLS_EXIT_FAILURE;
200 unsigned char *pem_buffer = NULL;
201 unsigned char der_buffer[4096];
203 size_t pem_size, der_size = sizeof(der_buffer);
210 memset( buf, 0, sizeof(buf) );
211 memset( der_buffer, 0, sizeof(der_buffer) );
216 mbedtls_printf( USAGE );
220 opt.filename = DFL_FILENAME;
221 opt.output_file = DFL_OUTPUT_FILENAME;
223 for( i = 1; i < argc; i++ )
227 if( ( q = strchr( p, '=' ) ) == NULL )
231 if( strcmp( p, "filename" ) == 0 )
233 else if( strcmp( p, "output_file" ) == 0 )
240 * 1.1. Load the PEM file
242 mbedtls_printf( "\n . Loading the PEM file ..." );
245 ret = load_file( opt.filename, &pem_buffer, &pem_size );
249 #ifdef MBEDTLS_ERROR_C
250 mbedtls_strerror( ret, buf, 1024 );
252 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
256 mbedtls_printf( " ok\n" );
259 * 1.2. Convert from PEM to DER
261 mbedtls_printf( " . Converting from PEM to DER ..." );
264 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
266 #ifdef MBEDTLS_ERROR_C
267 mbedtls_strerror( ret, buf, 1024 );
269 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
273 mbedtls_printf( " ok\n" );
276 * 1.3. Write the DER file
278 mbedtls_printf( " . Writing the DER file ..." );
281 ret = write_file( opt.output_file, der_buffer, der_size );
285 #ifdef MBEDTLS_ERROR_C
286 mbedtls_strerror( ret, buf, 1024 );
288 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
292 mbedtls_printf( " ok\n" );
294 exit_code = MBEDTLS_EXIT_SUCCESS;
300 mbedtls_printf( " + Press Enter to exit this program.\n" );
301 fflush( stdout ); getchar();
306 #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */