blob: f184939266754f0e97b5ae8d7a1972ebcd8806fa [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 Garcia78dabe02018-04-29 22:08:41 +010032#include <stdlib.h>
33#define mbedtls_free free
34#define mbedtls_calloc calloc
35#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010036#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010037#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia78dabe02018-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");
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020064 return( 0 );
65}
66#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010067
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010068
Paul Bakker8adf13b2013-08-25 14:50:09 +020069/*
70 * global options
71 */
72struct options
73{
Paul Bakker8fc30b12013-11-25 13:29:43 +010074 const char *filename; /* filename of the input file */
75 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020076} opt;
77
78int convert_pem_to_der( const unsigned char *input, size_t ilen,
79 unsigned char *output, size_t *olen )
80{
81 int ret;
82 const unsigned char *s1, *s2, *end = input + ilen;
83 size_t len = 0;
84
Paul Bakker8fc30b12013-11-25 13:29:43 +010085 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020086 if( s1 == NULL )
87 return( -1 );
88
Paul Bakker8fc30b12013-11-25 13:29:43 +010089 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020090 if( s2 == NULL )
91 return( -1 );
92
93 s1 += 10;
94 while( s1 < end && *s1 != '-' )
95 s1++;
96 while( s1 < end && *s1 == '-' )
97 s1++;
98 if( *s1 == '\r' ) s1++;
99 if( *s1 == '\n' ) s1++;
100
101 if( s2 <= s1 || s2 > end )
102 return( -1 );
103
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100104 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200106 return( ret );
107
108 if( len > *olen )
109 return( -1 );
110
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100111 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
Paul Bakker8adf13b2013-08-25 14:50:09 +0200112 s2 - s1 ) ) != 0 )
113 {
114 return( ret );
115 }
116
117 *olen = len;
118
119 return( 0 );
120}
121
122/*
123 * Load all data from a file into a given buffer.
124 */
125static int load_file( const char *path, unsigned char **buf, size_t *n )
126{
127 FILE *f;
128 long size;
129
130 if( ( f = fopen( path, "rb" ) ) == NULL )
131 return( -1 );
132
133 fseek( f, 0, SEEK_END );
134 if( ( size = ftell( f ) ) == -1 )
135 {
136 fclose( f );
137 return( -1 );
138 }
139 fseek( f, 0, SEEK_SET );
140
141 *n = (size_t) size;
142
143 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200144 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200145 {
146 fclose( f );
147 return( -1 );
148 }
149
150 if( fread( *buf, 1, *n, f ) != *n )
151 {
152 fclose( f );
153 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200154 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200155 return( -1 );
156 }
157
158 fclose( f );
159
160 (*buf)[*n] = '\0';
161
162 return( 0 );
163}
164
165/*
166 * Write buffer to a file
167 */
168static int write_file( const char *path, unsigned char *buf, size_t n )
169{
170 FILE *f;
171
172 if( ( f = fopen( path, "wb" ) ) == NULL )
173 return( -1 );
174
175 if( fwrite( buf, 1, n, f ) != n )
176 {
177 fclose( f );
178 return( -1 );
179 }
180
181 fclose( f );
182 return( 0 );
183}
184
Paul Bakker8adf13b2013-08-25 14:50:09 +0200185int main( int argc, char *argv[] )
186{
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100187 int ret = 1;
188 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200189 unsigned char *pem_buffer = NULL;
190 unsigned char der_buffer[4096];
191 char buf[1024];
192 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100193 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200194 char *p, *q;
195
196 /*
197 * Set to sane values
198 */
199 memset( buf, 0, sizeof(buf) );
200 memset( der_buffer, 0, sizeof(der_buffer) );
201
202 if( argc == 0 )
203 {
204 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200206 goto exit;
207 }
208
209 opt.filename = DFL_FILENAME;
210 opt.output_file = DFL_OUTPUT_FILENAME;
211
212 for( i = 1; i < argc; i++ )
213 {
214
215 p = argv[i];
216 if( ( q = strchr( p, '=' ) ) == NULL )
217 goto usage;
218 *q++ = '\0';
219
Paul Bakker8adf13b2013-08-25 14:50:09 +0200220 if( strcmp( p, "filename" ) == 0 )
221 opt.filename = q;
222 else if( strcmp( p, "output_file" ) == 0 )
223 opt.output_file = q;
224 else
225 goto usage;
226 }
227
228 /*
229 * 1.1. Load the PEM file
230 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200232 fflush( stdout );
233
234 ret = load_file( opt.filename, &pem_buffer, &pem_size );
235
236 if( ret != 0 )
237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#ifdef MBEDTLS_ERROR_C
239 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200242 goto exit;
243 }
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200246
247 /*
248 * 1.2. Convert from PEM to DER
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200251 fflush( stdout );
252
253 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#ifdef MBEDTLS_ERROR_C
256 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200257#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200259 goto exit;
260 }
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200263
264 /*
265 * 1.3. Write the DER file
266 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200268 fflush( stdout );
269
270 ret = write_file( opt.output_file, der_buffer, der_size );
271
272 if( ret != 0 )
273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274#ifdef MBEDTLS_ERROR_C
275 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200276#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200278 goto exit;
279 }
280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200282
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100283 exit_code = MBEDTLS_EXIT_SUCCESS;
284
Paul Bakker8adf13b2013-08-25 14:50:09 +0200285exit:
286 free( pem_buffer );
287
288#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200290 fflush( stdout ); getchar();
291#endif
292
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100293 return( exit_code );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200294}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */