blob: 95543b719860fc4bc3444c729e77c9609cf00f9b [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
32#define polarssl_printf printf
33#define polarssl_fprintf fprintf
34#define polarssl_malloc malloc
35#define polarssl_free free
36#endif
37
Paul Bakker8adf13b2013-08-25 14:50:09 +020038#include <string.h>
39#include <stdlib.h>
40#include <stdio.h>
41
Paul Bakker8adf13b2013-08-25 14:50:09 +020042#include "polarssl/error.h"
43#include "polarssl/base64.h"
44
45#define DFL_FILENAME "file.pem"
46#define DFL_OUTPUT_FILENAME "file.der"
47
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020048#if !defined(POLARSSL_BASE64_C) || !defined(POLARSSL_FS_IO)
49int main( int argc, char *argv[] )
50{
51 ((void) argc);
52 ((void) argv);
53
Rich Evansf90016a2015-01-19 14:26:37 +000054 polarssl_printf("POLARSSL_BASE64_C and/or POLARSSL_FS_IO not defined.\n");
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020055 return( 0 );
56}
57#else
Paul Bakker8adf13b2013-08-25 14:50:09 +020058/*
59 * global options
60 */
61struct options
62{
Paul Bakker8fc30b12013-11-25 13:29:43 +010063 const char *filename; /* filename of the input file */
64 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020065} opt;
66
67int convert_pem_to_der( const unsigned char *input, size_t ilen,
68 unsigned char *output, size_t *olen )
69{
70 int ret;
71 const unsigned char *s1, *s2, *end = input + ilen;
72 size_t len = 0;
73
Paul Bakker8fc30b12013-11-25 13:29:43 +010074 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020075 if( s1 == NULL )
76 return( -1 );
77
Paul Bakker8fc30b12013-11-25 13:29:43 +010078 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020079 if( s2 == NULL )
80 return( -1 );
81
82 s1 += 10;
83 while( s1 < end && *s1 != '-' )
84 s1++;
85 while( s1 < end && *s1 == '-' )
86 s1++;
87 if( *s1 == '\r' ) s1++;
88 if( *s1 == '\n' ) s1++;
89
90 if( s2 <= s1 || s2 > end )
91 return( -1 );
92
93 ret = base64_decode( NULL, &len, (const unsigned char *) s1, s2 - s1 );
94 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
95 return( ret );
96
97 if( len > *olen )
98 return( -1 );
99
100 if( ( ret = base64_decode( output, &len, (const unsigned char *) s1,
101 s2 - s1 ) ) != 0 )
102 {
103 return( ret );
104 }
105
106 *olen = len;
107
108 return( 0 );
109}
110
111/*
112 * Load all data from a file into a given buffer.
113 */
114static int load_file( const char *path, unsigned char **buf, size_t *n )
115{
116 FILE *f;
117 long size;
118
119 if( ( f = fopen( path, "rb" ) ) == NULL )
120 return( -1 );
121
122 fseek( f, 0, SEEK_END );
123 if( ( size = ftell( f ) ) == -1 )
124 {
125 fclose( f );
126 return( -1 );
127 }
128 fseek( f, 0, SEEK_SET );
129
130 *n = (size_t) size;
131
132 if( *n + 1 == 0 ||
Rich Evansf90016a2015-01-19 14:26:37 +0000133 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200134 {
135 fclose( f );
136 return( -1 );
137 }
138
139 if( fread( *buf, 1, *n, f ) != *n )
140 {
141 fclose( f );
142 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200143 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200144 return( -1 );
145 }
146
147 fclose( f );
148
149 (*buf)[*n] = '\0';
150
151 return( 0 );
152}
153
154/*
155 * Write buffer to a file
156 */
157static int write_file( const char *path, unsigned char *buf, size_t n )
158{
159 FILE *f;
160
161 if( ( f = fopen( path, "wb" ) ) == NULL )
162 return( -1 );
163
164 if( fwrite( buf, 1, n, f ) != n )
165 {
166 fclose( f );
167 return( -1 );
168 }
169
170 fclose( f );
171 return( 0 );
172}
173
174#define USAGE \
175 "\n usage: pem2der param=<>...\n" \
176 "\n acceptable parameters:\n" \
177 " filename=%%s default: file.pem\n" \
178 " output_file=%%s default: file.der\n" \
179 "\n"
180
Paul Bakker8adf13b2013-08-25 14:50:09 +0200181int main( int argc, char *argv[] )
182{
183 int ret = 0;
184 unsigned char *pem_buffer = NULL;
185 unsigned char der_buffer[4096];
186 char buf[1024];
187 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100188 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200189 char *p, *q;
190
191 /*
192 * Set to sane values
193 */
194 memset( buf, 0, sizeof(buf) );
195 memset( der_buffer, 0, sizeof(der_buffer) );
196
197 if( argc == 0 )
198 {
199 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000200 polarssl_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200201 goto exit;
202 }
203
204 opt.filename = DFL_FILENAME;
205 opt.output_file = DFL_OUTPUT_FILENAME;
206
207 for( i = 1; i < argc; i++ )
208 {
209
210 p = argv[i];
211 if( ( q = strchr( p, '=' ) ) == NULL )
212 goto usage;
213 *q++ = '\0';
214
Paul Bakker8adf13b2013-08-25 14:50:09 +0200215 if( strcmp( p, "filename" ) == 0 )
216 opt.filename = q;
217 else if( strcmp( p, "output_file" ) == 0 )
218 opt.output_file = q;
219 else
220 goto usage;
221 }
222
223 /*
224 * 1.1. Load the PEM file
225 */
Rich Evansf90016a2015-01-19 14:26:37 +0000226 polarssl_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200227 fflush( stdout );
228
229 ret = load_file( opt.filename, &pem_buffer, &pem_size );
230
231 if( ret != 0 )
232 {
233#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700234 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200235#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000236 polarssl_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200237 goto exit;
238 }
239
Rich Evansf90016a2015-01-19 14:26:37 +0000240 polarssl_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200241
242 /*
243 * 1.2. Convert from PEM to DER
244 */
Rich Evansf90016a2015-01-19 14:26:37 +0000245 polarssl_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200246 fflush( stdout );
247
248 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
249 {
250#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700251 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200252#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000253 polarssl_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200254 goto exit;
255 }
256
Rich Evansf90016a2015-01-19 14:26:37 +0000257 polarssl_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200258
259 /*
260 * 1.3. Write the DER file
261 */
Rich Evansf90016a2015-01-19 14:26:37 +0000262 polarssl_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200263 fflush( stdout );
264
265 ret = write_file( opt.output_file, der_buffer, der_size );
266
267 if( ret != 0 )
268 {
269#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700270 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200271#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000272 polarssl_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200273 goto exit;
274 }
275
Rich Evansf90016a2015-01-19 14:26:37 +0000276 polarssl_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200277
278exit:
279 free( pem_buffer );
280
281#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000282 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200283 fflush( stdout ); getchar();
284#endif
285
286 return( ret );
287}
288#endif /* POLARSSL_BASE64_C && POLARSSL_FS_IO */