blob: 46dc72778a8532a6f9ec9740a009deb845feb2e8 [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
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8adf13b2013-08-25 14:50:09 +020047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker8adf13b2013-08-25 14:50:09 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +010059#include <stdlib.h>
60#define mbedtls_free free
61#define mbedtls_calloc calloc
62#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010063#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010064#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +010065#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
66#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000069#include "mbedtls/error.h"
70#include "mbedtls/base64.h"
Paul Bakker8adf13b2013-08-25 14:50:09 +020071
Rich Evans18b78c72015-02-11 14:06:19 +000072#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#endif
76
Paul Bakker8adf13b2013-08-25 14:50:09 +020077#define DFL_FILENAME "file.pem"
78#define DFL_OUTPUT_FILENAME "file.der"
79
Rich Evans18b78c72015-02-11 14:06:19 +000080#define USAGE \
81 "\n usage: pem2der param=<>...\n" \
82 "\n acceptable parameters:\n" \
83 " filename=%%s default: file.pem\n" \
84 " output_file=%%s default: file.der\n" \
85 "\n"
86
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087#if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000088int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +020091 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020092}
93#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010094
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010095
Paul Bakker8adf13b2013-08-25 14:50:09 +020096/*
97 * global options
98 */
99struct options
100{
Paul Bakker8fc30b12013-11-25 13:29:43 +0100101 const char *filename; /* filename of the input file */
102 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +0200103} opt;
104
105int convert_pem_to_der( const unsigned char *input, size_t ilen,
106 unsigned char *output, size_t *olen )
107{
108 int ret;
109 const unsigned char *s1, *s2, *end = input + ilen;
110 size_t len = 0;
111
Paul Bakker8fc30b12013-11-25 13:29:43 +0100112 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200113 if( s1 == NULL )
114 return( -1 );
115
Paul Bakker8fc30b12013-11-25 13:29:43 +0100116 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200117 if( s2 == NULL )
118 return( -1 );
119
120 s1 += 10;
121 while( s1 < end && *s1 != '-' )
122 s1++;
123 while( s1 < end && *s1 == '-' )
124 s1++;
125 if( *s1 == '\r' ) s1++;
126 if( *s1 == '\n' ) s1++;
127
128 if( s2 <= s1 || s2 > end )
129 return( -1 );
130
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100131 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200133 return( ret );
134
135 if( len > *olen )
136 return( -1 );
137
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100138 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
Paul Bakker8adf13b2013-08-25 14:50:09 +0200139 s2 - s1 ) ) != 0 )
140 {
141 return( ret );
142 }
143
144 *olen = len;
145
146 return( 0 );
147}
148
149/*
150 * Load all data from a file into a given buffer.
151 */
152static int load_file( const char *path, unsigned char **buf, size_t *n )
153{
154 FILE *f;
155 long size;
156
157 if( ( f = fopen( path, "rb" ) ) == NULL )
158 return( -1 );
159
160 fseek( f, 0, SEEK_END );
161 if( ( size = ftell( f ) ) == -1 )
162 {
163 fclose( f );
164 return( -1 );
165 }
166 fseek( f, 0, SEEK_SET );
167
168 *n = (size_t) size;
169
170 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200171 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200172 {
173 fclose( f );
174 return( -1 );
175 }
176
177 if( fread( *buf, 1, *n, f ) != *n )
178 {
179 fclose( f );
180 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200181 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200182 return( -1 );
183 }
184
185 fclose( f );
186
187 (*buf)[*n] = '\0';
188
189 return( 0 );
190}
191
192/*
193 * Write buffer to a file
194 */
195static int write_file( const char *path, unsigned char *buf, size_t n )
196{
197 FILE *f;
198
199 if( ( f = fopen( path, "wb" ) ) == NULL )
200 return( -1 );
201
202 if( fwrite( buf, 1, n, f ) != n )
203 {
204 fclose( f );
205 return( -1 );
206 }
207
208 fclose( f );
209 return( 0 );
210}
211
Paul Bakker8adf13b2013-08-25 14:50:09 +0200212int main( int argc, char *argv[] )
213{
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100214 int ret = 1;
215 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200216 unsigned char *pem_buffer = NULL;
217 unsigned char der_buffer[4096];
218 char buf[1024];
219 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100220 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200221 char *p, *q;
222
223 /*
224 * Set to sane values
225 */
226 memset( buf, 0, sizeof(buf) );
227 memset( der_buffer, 0, sizeof(der_buffer) );
228
229 if( argc == 0 )
230 {
231 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200233 goto exit;
234 }
235
236 opt.filename = DFL_FILENAME;
237 opt.output_file = DFL_OUTPUT_FILENAME;
238
239 for( i = 1; i < argc; i++ )
240 {
241
242 p = argv[i];
243 if( ( q = strchr( p, '=' ) ) == NULL )
244 goto usage;
245 *q++ = '\0';
246
Paul Bakker8adf13b2013-08-25 14:50:09 +0200247 if( strcmp( p, "filename" ) == 0 )
248 opt.filename = q;
249 else if( strcmp( p, "output_file" ) == 0 )
250 opt.output_file = q;
251 else
252 goto usage;
253 }
254
255 /*
256 * 1.1. Load the PEM file
257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200259 fflush( stdout );
260
261 ret = load_file( opt.filename, &pem_buffer, &pem_size );
262
263 if( ret != 0 )
264 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#ifdef MBEDTLS_ERROR_C
266 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200267#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200269 goto exit;
270 }
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200273
274 /*
275 * 1.2. Convert from PEM to DER
276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200278 fflush( stdout );
279
280 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
281 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#ifdef MBEDTLS_ERROR_C
283 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200284#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200286 goto exit;
287 }
288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200290
291 /*
292 * 1.3. Write the DER file
293 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200295 fflush( stdout );
296
297 ret = write_file( opt.output_file, der_buffer, der_size );
298
299 if( ret != 0 )
300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#ifdef MBEDTLS_ERROR_C
302 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200305 goto exit;
306 }
307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200309
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100310 exit_code = MBEDTLS_EXIT_SUCCESS;
311
Paul Bakker8adf13b2013-08-25 14:50:09 +0200312exit:
313 free( pem_buffer );
314
315#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200317 fflush( stdout ); getchar();
318#endif
319
Krzysztof Stachowiak3b0c4302019-04-24 14:24:46 +0200320 mbedtls_exit( exit_code );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200321}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */