blob: dfd7a496654772c22fa8673cdb8510b0f9c79d19 [file] [log] [blame]
Paul Bakker8adf13b2013-08-25 14:50:09 +02001/*
2 * Convert PEM to DER
3 *
4 * Copyright (C) 2006-2013, Brainspark B.V.
5 *
6 * This file is part of PolarSSL (http://www.polarssl.org)
7 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
8 *
9 * All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 */
25
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020027#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
29#include POLARSSL_CONFIG_FILE
30#endif
Paul Bakker8adf13b2013-08-25 14:50:09 +020031
32#include <string.h>
33#include <stdlib.h>
34#include <stdio.h>
35
Paul Bakker8adf13b2013-08-25 14:50:09 +020036#include "polarssl/error.h"
37#include "polarssl/base64.h"
38
39#define DFL_FILENAME "file.pem"
40#define DFL_OUTPUT_FILENAME "file.der"
41
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020042#if !defined(POLARSSL_BASE64_C) || !defined(POLARSSL_FS_IO)
43int main( int argc, char *argv[] )
44{
45 ((void) argc);
46 ((void) argv);
47
48 printf("POLARSSL_BASE64_C and/or POLARSSL_FS_IO not defined.\n");
49 return( 0 );
50}
51#else
Paul Bakker8adf13b2013-08-25 14:50:09 +020052/*
53 * global options
54 */
55struct options
56{
Paul Bakker8fc30b12013-11-25 13:29:43 +010057 const char *filename; /* filename of the input file */
58 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020059} opt;
60
61int convert_pem_to_der( const unsigned char *input, size_t ilen,
62 unsigned char *output, size_t *olen )
63{
64 int ret;
65 const unsigned char *s1, *s2, *end = input + ilen;
66 size_t len = 0;
67
Paul Bakker8fc30b12013-11-25 13:29:43 +010068 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020069 if( s1 == NULL )
70 return( -1 );
71
Paul Bakker8fc30b12013-11-25 13:29:43 +010072 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020073 if( s2 == NULL )
74 return( -1 );
75
76 s1 += 10;
77 while( s1 < end && *s1 != '-' )
78 s1++;
79 while( s1 < end && *s1 == '-' )
80 s1++;
81 if( *s1 == '\r' ) s1++;
82 if( *s1 == '\n' ) s1++;
83
84 if( s2 <= s1 || s2 > end )
85 return( -1 );
86
87 ret = base64_decode( NULL, &len, (const unsigned char *) s1, s2 - s1 );
88 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
89 return( ret );
90
91 if( len > *olen )
92 return( -1 );
93
94 if( ( ret = base64_decode( output, &len, (const unsigned char *) s1,
95 s2 - s1 ) ) != 0 )
96 {
97 return( ret );
98 }
99
100 *olen = len;
101
102 return( 0 );
103}
104
105/*
106 * Load all data from a file into a given buffer.
107 */
108static int load_file( const char *path, unsigned char **buf, size_t *n )
109{
110 FILE *f;
111 long size;
112
113 if( ( f = fopen( path, "rb" ) ) == NULL )
114 return( -1 );
115
116 fseek( f, 0, SEEK_END );
117 if( ( size = ftell( f ) ) == -1 )
118 {
119 fclose( f );
120 return( -1 );
121 }
122 fseek( f, 0, SEEK_SET );
123
124 *n = (size_t) size;
125
126 if( *n + 1 == 0 ||
127 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
128 {
129 fclose( f );
130 return( -1 );
131 }
132
133 if( fread( *buf, 1, *n, f ) != *n )
134 {
135 fclose( f );
136 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200137 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200138 return( -1 );
139 }
140
141 fclose( f );
142
143 (*buf)[*n] = '\0';
144
145 return( 0 );
146}
147
148/*
149 * Write buffer to a file
150 */
151static int write_file( const char *path, unsigned char *buf, size_t n )
152{
153 FILE *f;
154
155 if( ( f = fopen( path, "wb" ) ) == NULL )
156 return( -1 );
157
158 if( fwrite( buf, 1, n, f ) != n )
159 {
160 fclose( f );
161 return( -1 );
162 }
163
164 fclose( f );
165 return( 0 );
166}
167
168#define USAGE \
169 "\n usage: pem2der param=<>...\n" \
170 "\n acceptable parameters:\n" \
171 " filename=%%s default: file.pem\n" \
172 " output_file=%%s default: file.der\n" \
173 "\n"
174
Paul Bakker8adf13b2013-08-25 14:50:09 +0200175int main( int argc, char *argv[] )
176{
177 int ret = 0;
178 unsigned char *pem_buffer = NULL;
179 unsigned char der_buffer[4096];
180 char buf[1024];
181 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100182 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200183 char *p, *q;
184
185 /*
186 * Set to sane values
187 */
188 memset( buf, 0, sizeof(buf) );
189 memset( der_buffer, 0, sizeof(der_buffer) );
190
191 if( argc == 0 )
192 {
193 usage:
194 printf( USAGE );
195 goto exit;
196 }
197
198 opt.filename = DFL_FILENAME;
199 opt.output_file = DFL_OUTPUT_FILENAME;
200
201 for( i = 1; i < argc; i++ )
202 {
203
204 p = argv[i];
205 if( ( q = strchr( p, '=' ) ) == NULL )
206 goto usage;
207 *q++ = '\0';
208
Paul Bakker8adf13b2013-08-25 14:50:09 +0200209 if( strcmp( p, "filename" ) == 0 )
210 opt.filename = q;
211 else if( strcmp( p, "output_file" ) == 0 )
212 opt.output_file = q;
213 else
214 goto usage;
215 }
216
217 /*
218 * 1.1. Load the PEM file
219 */
220 printf( "\n . Loading the PEM file ..." );
221 fflush( stdout );
222
223 ret = load_file( opt.filename, &pem_buffer, &pem_size );
224
225 if( ret != 0 )
226 {
227#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700228 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200229#endif
230 printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
231 goto exit;
232 }
233
234 printf( " ok\n" );
235
236 /*
237 * 1.2. Convert from PEM to DER
238 */
239 printf( " . Converting from PEM to DER ..." );
240 fflush( stdout );
241
242 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
243 {
244#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700245 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200246#endif
247 printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
248 goto exit;
249 }
250
251 printf( " ok\n" );
252
253 /*
254 * 1.3. Write the DER file
255 */
256 printf( " . Writing the DER file ..." );
257 fflush( stdout );
258
259 ret = write_file( opt.output_file, der_buffer, der_size );
260
261 if( ret != 0 )
262 {
263#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700264 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200265#endif
266 printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
267 goto exit;
268 }
269
270 printf( " ok\n" );
271
272exit:
273 free( pem_buffer );
274
275#if defined(_WIN32)
276 printf( " + Press Enter to exit this program.\n" );
277 fflush( stdout ); getchar();
278#endif
279
280 return( ret );
281}
282#endif /* POLARSSL_BASE64_C && POLARSSL_FS_IO */