blob: c967e53da2bfb894a70a7033db4afaa373f71fd1 [file] [log] [blame]
Paul Bakker8adf13b2013-08-25 14:50:09 +02001/*
2 * Convert PEM to DER
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved
Paul Bakker8adf13b2013-08-25 14:50:09 +02005 *
Manuel Pégourié-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.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é-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker8adf13b2013-08-25 14:50:09 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_free free
Rich Evans18b78c72015-02-11 14:06:19 +000034#define polarssl_malloc malloc
35#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000036#endif
37
Rich Evans18b78c72015-02-11 14:06:19 +000038#if defined(POLARSSL_BASE64_C) && defined(POLARSSL_FS_IO)
Paul Bakker8adf13b2013-08-25 14:50:09 +020039#include "polarssl/error.h"
40#include "polarssl/base64.h"
41
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é-Gonnard7831b0c2013-09-20 12:29:56 +020057#if !defined(POLARSSL_BASE64_C) || !defined(POLARSSL_FS_IO)
58int main( int argc, char *argv[] )
59{
60 ((void) argc);
61 ((void) argv);
62
Rich Evansf90016a2015-01-19 14:26:37 +000063 polarssl_printf("POLARSSL_BASE64_C and/or POLARSSL_FS_IO not defined.\n");
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020064 return( 0 );
65}
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
102 ret = base64_decode( NULL, &len, (const unsigned char *) s1, s2 - s1 );
103 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
104 return( ret );
105
106 if( len > *olen )
107 return( -1 );
108
109 if( ( ret = base64_decode( output, &len, (const unsigned char *) s1,
110 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 ||
Rich Evansf90016a2015-01-19 14:26:37 +0000142 ( *buf = (unsigned char *) polarssl_malloc( *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{
185 int ret = 0;
186 unsigned char *pem_buffer = NULL;
187 unsigned char der_buffer[4096];
188 char buf[1024];
189 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100190 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200191 char *p, *q;
192
193 /*
194 * Set to sane values
195 */
196 memset( buf, 0, sizeof(buf) );
197 memset( der_buffer, 0, sizeof(der_buffer) );
198
199 if( argc == 0 )
200 {
201 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000202 polarssl_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200203 goto exit;
204 }
205
206 opt.filename = DFL_FILENAME;
207 opt.output_file = DFL_OUTPUT_FILENAME;
208
209 for( i = 1; i < argc; i++ )
210 {
211
212 p = argv[i];
213 if( ( q = strchr( p, '=' ) ) == NULL )
214 goto usage;
215 *q++ = '\0';
216
Paul Bakker8adf13b2013-08-25 14:50:09 +0200217 if( strcmp( p, "filename" ) == 0 )
218 opt.filename = q;
219 else if( strcmp( p, "output_file" ) == 0 )
220 opt.output_file = q;
221 else
222 goto usage;
223 }
224
225 /*
226 * 1.1. Load the PEM file
227 */
Rich Evansf90016a2015-01-19 14:26:37 +0000228 polarssl_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200229 fflush( stdout );
230
231 ret = load_file( opt.filename, &pem_buffer, &pem_size );
232
233 if( ret != 0 )
234 {
235#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700236 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200237#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000238 polarssl_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200239 goto exit;
240 }
241
Rich Evansf90016a2015-01-19 14:26:37 +0000242 polarssl_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200243
244 /*
245 * 1.2. Convert from PEM to DER
246 */
Rich Evansf90016a2015-01-19 14:26:37 +0000247 polarssl_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200248 fflush( stdout );
249
250 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
251 {
252#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700253 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200254#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000255 polarssl_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200256 goto exit;
257 }
258
Rich Evansf90016a2015-01-19 14:26:37 +0000259 polarssl_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200260
261 /*
262 * 1.3. Write the DER file
263 */
Rich Evansf90016a2015-01-19 14:26:37 +0000264 polarssl_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200265 fflush( stdout );
266
267 ret = write_file( opt.output_file, der_buffer, der_size );
268
269 if( ret != 0 )
270 {
271#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700272 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200273#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000274 polarssl_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200275 goto exit;
276 }
277
Rich Evansf90016a2015-01-19 14:26:37 +0000278 polarssl_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200279
280exit:
281 free( pem_buffer );
282
283#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000284 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200285 fflush( stdout ); getchar();
286#endif
287
288 return( ret );
289}
290#endif /* POLARSSL_BASE64_C && POLARSSL_FS_IO */