blob: 18eb070023c61fa29970de28b19af0124c6c66e3 [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
Paul Bakker8adf13b2013-08-25 14:50:09 +02005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8adf13b2013-08-25 14:50:09 +02007 *
Paul Bakker8adf13b2013-08-25 14:50:09 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker8adf13b2013-08-25 14:50:09 +020028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020034#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000036#endif
37
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020038#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/error.h"
40#include "mbedtls/base64.h"
Paul Bakker8adf13b2013-08-25 14:50:09 +020041
Rich Evans18b78c72015-02-11 14:06:19 +000042#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#endif
46
Paul Bakker8adf13b2013-08-25 14:50:09 +020047#define DFL_FILENAME "file.pem"
48#define DFL_OUTPUT_FILENAME "file.der"
49
Rich Evans18b78c72015-02-11 14:06:19 +000050#define USAGE \
51 "\n usage: pem2der param=<>...\n" \
52 "\n acceptable parameters:\n" \
53 " filename=%%s default: file.pem\n" \
54 " output_file=%%s default: file.der\n" \
55 "\n"
56
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000058int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020059{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020061 return( 0 );
62}
63#else
Paul Bakker8adf13b2013-08-25 14:50:09 +020064/*
65 * global options
66 */
67struct options
68{
Paul Bakker8fc30b12013-11-25 13:29:43 +010069 const char *filename; /* filename of the input file */
70 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020071} opt;
72
73int convert_pem_to_der( const unsigned char *input, size_t ilen,
74 unsigned char *output, size_t *olen )
75{
76 int ret;
77 const unsigned char *s1, *s2, *end = input + ilen;
78 size_t len = 0;
79
Paul Bakker8fc30b12013-11-25 13:29:43 +010080 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020081 if( s1 == NULL )
82 return( -1 );
83
Paul Bakker8fc30b12013-11-25 13:29:43 +010084 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020085 if( s2 == NULL )
86 return( -1 );
87
88 s1 += 10;
89 while( s1 < end && *s1 != '-' )
90 s1++;
91 while( s1 < end && *s1 == '-' )
92 s1++;
93 if( *s1 == '\r' ) s1++;
94 if( *s1 == '\n' ) s1++;
95
96 if( s2 <= s1 || s2 > end )
97 return( -1 );
98
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010099 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200101 return( ret );
102
103 if( len > *olen )
104 return( -1 );
105
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100106 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
Paul Bakker8adf13b2013-08-25 14:50:09 +0200107 s2 - s1 ) ) != 0 )
108 {
109 return( ret );
110 }
111
112 *olen = len;
113
114 return( 0 );
115}
116
117/*
118 * Load all data from a file into a given buffer.
119 */
120static int load_file( const char *path, unsigned char **buf, size_t *n )
121{
122 FILE *f;
123 long size;
124
125 if( ( f = fopen( path, "rb" ) ) == NULL )
126 return( -1 );
127
128 fseek( f, 0, SEEK_END );
129 if( ( size = ftell( f ) ) == -1 )
130 {
131 fclose( f );
132 return( -1 );
133 }
134 fseek( f, 0, SEEK_SET );
135
136 *n = (size_t) size;
137
138 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200139 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200140 {
141 fclose( f );
142 return( -1 );
143 }
144
145 if( fread( *buf, 1, *n, f ) != *n )
146 {
147 fclose( f );
148 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200149 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200150 return( -1 );
151 }
152
153 fclose( f );
154
155 (*buf)[*n] = '\0';
156
157 return( 0 );
158}
159
160/*
161 * Write buffer to a file
162 */
163static int write_file( const char *path, unsigned char *buf, size_t n )
164{
165 FILE *f;
166
167 if( ( f = fopen( path, "wb" ) ) == NULL )
168 return( -1 );
169
170 if( fwrite( buf, 1, n, f ) != n )
171 {
172 fclose( f );
173 return( -1 );
174 }
175
176 fclose( f );
177 return( 0 );
178}
179
Paul Bakker8adf13b2013-08-25 14:50:09 +0200180int main( int argc, char *argv[] )
181{
182 int ret = 0;
183 unsigned char *pem_buffer = NULL;
184 unsigned char der_buffer[4096];
185 char buf[1024];
186 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100187 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200188 char *p, *q;
189
190 /*
191 * Set to sane values
192 */
193 memset( buf, 0, sizeof(buf) );
194 memset( der_buffer, 0, sizeof(der_buffer) );
195
196 if( argc == 0 )
197 {
198 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200200 goto exit;
201 }
202
203 opt.filename = DFL_FILENAME;
204 opt.output_file = DFL_OUTPUT_FILENAME;
205
206 for( i = 1; i < argc; i++ )
207 {
208
209 p = argv[i];
210 if( ( q = strchr( p, '=' ) ) == NULL )
211 goto usage;
212 *q++ = '\0';
213
Paul Bakker8adf13b2013-08-25 14:50:09 +0200214 if( strcmp( p, "filename" ) == 0 )
215 opt.filename = q;
216 else if( strcmp( p, "output_file" ) == 0 )
217 opt.output_file = q;
218 else
219 goto usage;
220 }
221
222 /*
223 * 1.1. Load the PEM file
224 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200226 fflush( stdout );
227
228 ret = load_file( opt.filename, &pem_buffer, &pem_size );
229
230 if( ret != 0 )
231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#ifdef MBEDTLS_ERROR_C
233 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200236 goto exit;
237 }
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200240
241 /*
242 * 1.2. Convert from PEM to DER
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200245 fflush( stdout );
246
247 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#ifdef MBEDTLS_ERROR_C
250 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200253 goto exit;
254 }
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200257
258 /*
259 * 1.3. Write the DER file
260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200262 fflush( stdout );
263
264 ret = write_file( opt.output_file, der_buffer, der_size );
265
266 if( ret != 0 )
267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#ifdef MBEDTLS_ERROR_C
269 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200272 goto exit;
273 }
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200276
277exit:
278 free( pem_buffer );
279
280#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200282 fflush( stdout ); getchar();
283#endif
284
285 return( ret );
286}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */