blob: 0b2eff2c1d9c5a61a0de4e36bc99ff2fbfb4e2c6 [file] [log] [blame]
Paul Bakker8adf13b2013-08-25 14:50:09 +02001/*
2 * Convert PEM to DER
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker8adf13b2013-08-25 14:50:09 +020018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8adf13b2013-08-25 14:50:09 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker8adf13b2013-08-25 14:50:09 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Andres Amaya Garcia6118ada2018-04-29 22:08:41 +010032#include <stdlib.h>
33#define mbedtls_free free
34#define mbedtls_calloc calloc
35#define mbedtls_printf printf
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020036#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010037#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia6118ada2018-04-29 22:08:41 +010038#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
39#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/error.h"
43#include "mbedtls/base64.h"
Paul Bakker8adf13b2013-08-25 14:50:09 +020044
Rich Evans18b78c72015-02-11 14:06:19 +000045#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#endif
49
Paul Bakker8adf13b2013-08-25 14:50:09 +020050#define DFL_FILENAME "file.pem"
51#define DFL_OUTPUT_FILENAME "file.der"
52
Rich Evans18b78c72015-02-11 14:06:19 +000053#define USAGE \
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" \
58 "\n"
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000061int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020062{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020064 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020065}
66#else
Paul Bakker8adf13b2013-08-25 14:50:09 +020067/*
68 * global options
69 */
70struct options
71{
Paul Bakker8fc30b12013-11-25 13:29:43 +010072 const char *filename; /* filename of the input file */
73 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020074} opt;
75
76int 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 Bakker8fc30b12013-11-25 13:29:43 +010083 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020084 if( s1 == NULL )
85 return( -1 );
86
Paul Bakker8fc30b12013-11-25 13:29:43 +010087 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020088 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é-Gonnardba561362015-06-02 16:30:35 +0100102 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200104 return( ret );
105
106 if( len > *olen )
107 return( -1 );
108
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100109 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
Paul Bakker8adf13b2013-08-25 14:50:09 +0200110 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 */
123static 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é-Gonnard7551cb92015-05-26 16:04:06 +0200142 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200143 {
144 fclose( f );
145 return( -1 );
146 }
147
148 if( fread( *buf, 1, *n, f ) != *n )
149 {
150 fclose( f );
151 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200152 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200153 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 */
166static 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 Bakker8adf13b2013-08-25 14:50:09 +0200183int main( int argc, char *argv[] )
184{
Andres Amaya Garcia6118ada2018-04-29 22:08:41 +0100185 int ret = 1;
186 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200187 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 Bakkerc97f9f62013-11-30 15:13:02 +0100191 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200192 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 mbedtls_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200204 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 Bakker8adf13b2013-08-25 14:50:09 +0200218 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é-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 mbedtls_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200230 fflush( stdout );
231
232 ret = load_file( opt.filename, &pem_buffer, &pem_size );
233
234 if( ret != 0 )
235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#ifdef MBEDTLS_ERROR_C
237 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200240 goto exit;
241 }
242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200244
245 /*
246 * 1.2. Convert from PEM to DER
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200249 fflush( stdout );
250
251 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#ifdef MBEDTLS_ERROR_C
254 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200255#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200257 goto exit;
258 }
259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200261
262 /*
263 * 1.3. Write the DER file
264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200266 fflush( stdout );
267
268 ret = write_file( opt.output_file, der_buffer, der_size );
269
270 if( ret != 0 )
271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272#ifdef MBEDTLS_ERROR_C
273 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200276 goto exit;
277 }
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200280
Andres Amaya Garcia6118ada2018-04-29 22:08:41 +0100281 exit_code = MBEDTLS_EXIT_SUCCESS;
282
Paul Bakker8adf13b2013-08-25 14:50:09 +0200283exit:
284 free( pem_buffer );
285
286#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200288 fflush( stdout ); getchar();
289#endif
290
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200291 mbedtls_exit( exit_code );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */