blob: 5386fdb9cf8d7fa3a3938b4e2b4241ea3752220c [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 );
137 return( -1 );
138 }
139
140 fclose( f );
141
142 (*buf)[*n] = '\0';
143
144 return( 0 );
145}
146
147/*
148 * Write buffer to a file
149 */
150static int write_file( const char *path, unsigned char *buf, size_t n )
151{
152 FILE *f;
153
154 if( ( f = fopen( path, "wb" ) ) == NULL )
155 return( -1 );
156
157 if( fwrite( buf, 1, n, f ) != n )
158 {
159 fclose( f );
160 return( -1 );
161 }
162
163 fclose( f );
164 return( 0 );
165}
166
167#define USAGE \
168 "\n usage: pem2der param=<>...\n" \
169 "\n acceptable parameters:\n" \
170 " filename=%%s default: file.pem\n" \
171 " output_file=%%s default: file.der\n" \
172 "\n"
173
Paul Bakker8adf13b2013-08-25 14:50:09 +0200174int main( int argc, char *argv[] )
175{
176 int ret = 0;
177 unsigned char *pem_buffer = NULL;
178 unsigned char der_buffer[4096];
179 char buf[1024];
180 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100181 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200182 char *p, *q;
183
184 /*
185 * Set to sane values
186 */
187 memset( buf, 0, sizeof(buf) );
188 memset( der_buffer, 0, sizeof(der_buffer) );
189
190 if( argc == 0 )
191 {
192 usage:
193 printf( USAGE );
194 goto exit;
195 }
196
197 opt.filename = DFL_FILENAME;
198 opt.output_file = DFL_OUTPUT_FILENAME;
199
200 for( i = 1; i < argc; i++ )
201 {
202
203 p = argv[i];
204 if( ( q = strchr( p, '=' ) ) == NULL )
205 goto usage;
206 *q++ = '\0';
207
Paul Bakker8adf13b2013-08-25 14:50:09 +0200208 if( strcmp( p, "filename" ) == 0 )
209 opt.filename = q;
210 else if( strcmp( p, "output_file" ) == 0 )
211 opt.output_file = q;
212 else
213 goto usage;
214 }
215
216 /*
217 * 1.1. Load the PEM file
218 */
219 printf( "\n . Loading the PEM file ..." );
220 fflush( stdout );
221
222 ret = load_file( opt.filename, &pem_buffer, &pem_size );
223
224 if( ret != 0 )
225 {
226#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700227 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200228#endif
229 printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
230 goto exit;
231 }
232
233 printf( " ok\n" );
234
235 /*
236 * 1.2. Convert from PEM to DER
237 */
238 printf( " . Converting from PEM to DER ..." );
239 fflush( stdout );
240
241 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
242 {
243#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700244 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200245#endif
246 printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
247 goto exit;
248 }
249
250 printf( " ok\n" );
251
252 /*
253 * 1.3. Write the DER file
254 */
255 printf( " . Writing the DER file ..." );
256 fflush( stdout );
257
258 ret = write_file( opt.output_file, der_buffer, der_size );
259
260 if( ret != 0 )
261 {
262#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700263 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200264#endif
265 printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
266 goto exit;
267 }
268
269 printf( " ok\n" );
270
271exit:
272 free( pem_buffer );
273
274#if defined(_WIN32)
275 printf( " + Press Enter to exit this program.\n" );
276 fflush( stdout ); getchar();
277#endif
278
279 return( ret );
280}
281#endif /* POLARSSL_BASE64_C && POLARSSL_FS_IO */