blob: 153b4fc308f8b5064b5017c0c49a779a689d02df [file] [log] [blame]
Paul Bakker8adf13b2013-08-25 14:50:09 +02001/*
2 * Convert PEM to DER
3 *
Bence Szépkútia2947ac2020-08-19 16:37:36 +02004 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +02005 * 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é-Gonnard37ff1402015-09-04 14:21:07 +020012 *
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 Bakker8adf13b2013-08-25 14:50:09 +020024 *
Bence Szépkútif744bd72020-06-05 13:02:18 +020025 * **********
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 Bakker8adf13b2013-08-25 14:50:09 +020045 */
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000048#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020049#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#endif
Paul Bakker8adf13b2013-08-25 14:50:09 +020052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000054#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000055#else
Rich Evans18b78c72015-02-11 14:06:19 +000056#include <stdio.h>
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +010057#include <stdlib.h>
58#define mbedtls_free free
59#define mbedtls_calloc calloc
60#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010061#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010062#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +010063#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
64#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000067#include "mbedtls/error.h"
68#include "mbedtls/base64.h"
Paul Bakker8adf13b2013-08-25 14:50:09 +020069
Rich Evans18b78c72015-02-11 14:06:19 +000070#include <stdio.h>
71#include <stdlib.h>
72#include <string.h>
73#endif
74
Paul Bakker8adf13b2013-08-25 14:50:09 +020075#define DFL_FILENAME "file.pem"
76#define DFL_OUTPUT_FILENAME "file.der"
77
Rich Evans18b78c72015-02-11 14:06:19 +000078#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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020085#if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000086int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020087{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020089 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020090}
91#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010092
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010093
Paul Bakker8adf13b2013-08-25 14:50:09 +020094/*
95 * global options
96 */
97struct options
98{
Paul Bakker8fc30b12013-11-25 13:29:43 +010099 const char *filename; /* filename of the input file */
100 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +0200101} opt;
102
103int convert_pem_to_der( const unsigned char *input, size_t ilen,
104 unsigned char *output, size_t *olen )
105{
106 int ret;
107 const unsigned char *s1, *s2, *end = input + ilen;
108 size_t len = 0;
109
Paul Bakker8fc30b12013-11-25 13:29:43 +0100110 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200111 if( s1 == NULL )
112 return( -1 );
113
Paul Bakker8fc30b12013-11-25 13:29:43 +0100114 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200115 if( s2 == NULL )
116 return( -1 );
117
118 s1 += 10;
119 while( s1 < end && *s1 != '-' )
120 s1++;
121 while( s1 < end && *s1 == '-' )
122 s1++;
123 if( *s1 == '\r' ) s1++;
124 if( *s1 == '\n' ) s1++;
125
126 if( s2 <= s1 || s2 > end )
127 return( -1 );
128
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100129 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200131 return( ret );
132
133 if( len > *olen )
134 return( -1 );
135
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100136 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
Paul Bakker8adf13b2013-08-25 14:50:09 +0200137 s2 - s1 ) ) != 0 )
138 {
139 return( ret );
140 }
141
142 *olen = len;
143
144 return( 0 );
145}
146
147/*
148 * Load all data from a file into a given buffer.
149 */
150static int load_file( const char *path, unsigned char **buf, size_t *n )
151{
152 FILE *f;
153 long size;
154
155 if( ( f = fopen( path, "rb" ) ) == NULL )
156 return( -1 );
157
158 fseek( f, 0, SEEK_END );
159 if( ( size = ftell( f ) ) == -1 )
160 {
161 fclose( f );
162 return( -1 );
163 }
164 fseek( f, 0, SEEK_SET );
165
166 *n = (size_t) size;
167
168 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200169 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200170 {
171 fclose( f );
172 return( -1 );
173 }
174
175 if( fread( *buf, 1, *n, f ) != *n )
176 {
177 fclose( f );
178 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200179 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200180 return( -1 );
181 }
182
183 fclose( f );
184
185 (*buf)[*n] = '\0';
186
187 return( 0 );
188}
189
190/*
191 * Write buffer to a file
192 */
193static int write_file( const char *path, unsigned char *buf, size_t n )
194{
195 FILE *f;
196
197 if( ( f = fopen( path, "wb" ) ) == NULL )
198 return( -1 );
199
200 if( fwrite( buf, 1, n, f ) != n )
201 {
202 fclose( f );
203 return( -1 );
204 }
205
206 fclose( f );
207 return( 0 );
208}
209
Paul Bakker8adf13b2013-08-25 14:50:09 +0200210int main( int argc, char *argv[] )
211{
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100212 int ret = 1;
213 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200214 unsigned char *pem_buffer = NULL;
215 unsigned char der_buffer[4096];
216 char buf[1024];
217 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100218 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200219 char *p, *q;
220
221 /*
222 * Set to sane values
223 */
224 memset( buf, 0, sizeof(buf) );
225 memset( der_buffer, 0, sizeof(der_buffer) );
226
227 if( argc == 0 )
228 {
229 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 mbedtls_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200231 goto exit;
232 }
233
234 opt.filename = DFL_FILENAME;
235 opt.output_file = DFL_OUTPUT_FILENAME;
236
237 for( i = 1; i < argc; i++ )
238 {
239
240 p = argv[i];
241 if( ( q = strchr( p, '=' ) ) == NULL )
242 goto usage;
243 *q++ = '\0';
244
Paul Bakker8adf13b2013-08-25 14:50:09 +0200245 if( strcmp( p, "filename" ) == 0 )
246 opt.filename = q;
247 else if( strcmp( p, "output_file" ) == 0 )
248 opt.output_file = q;
249 else
250 goto usage;
251 }
252
253 /*
254 * 1.1. Load the PEM file
255 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200257 fflush( stdout );
258
259 ret = load_file( opt.filename, &pem_buffer, &pem_size );
260
261 if( ret != 0 )
262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263#ifdef MBEDTLS_ERROR_C
264 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200266 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200267 goto exit;
268 }
269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200271
272 /*
273 * 1.2. Convert from PEM to DER
274 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200276 fflush( stdout );
277
278 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280#ifdef MBEDTLS_ERROR_C
281 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200282#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200284 goto exit;
285 }
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200288
289 /*
290 * 1.3. Write the DER file
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292 mbedtls_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200293 fflush( stdout );
294
295 ret = write_file( opt.output_file, der_buffer, der_size );
296
297 if( ret != 0 )
298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#ifdef MBEDTLS_ERROR_C
300 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200301#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200303 goto exit;
304 }
305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200307
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100308 exit_code = MBEDTLS_EXIT_SUCCESS;
309
Paul Bakker8adf13b2013-08-25 14:50:09 +0200310exit:
311 free( pem_buffer );
312
313#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200315 fflush( stdout ); getchar();
316#endif
317
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200318 mbedtls_exit( exit_code );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200319}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */