blob: 0315f02ea9946c9e09f9ef58cc005aed70a9f69e [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
26#ifndef _CRT_SECURE_NO_DEPRECATE
27#define _CRT_SECURE_NO_DEPRECATE 1
28#endif
29
30#include <string.h>
31#include <stdlib.h>
32#include <stdio.h>
33
34#include "polarssl/config.h"
35
36#include "polarssl/error.h"
37#include "polarssl/base64.h"
38
39#define DFL_FILENAME "file.pem"
40#define DFL_OUTPUT_FILENAME "file.der"
41
42/*
43 * global options
44 */
45struct options
46{
47 char *filename; /* filename of the input file */
48 char *output_file; /* where to store the output */
49} opt;
50
51int convert_pem_to_der( const unsigned char *input, size_t ilen,
52 unsigned char *output, size_t *olen )
53{
54 int ret;
55 const unsigned char *s1, *s2, *end = input + ilen;
56 size_t len = 0;
57
58 s1 = (unsigned char *) strstr( (char *) input, "-----BEGIN" );
59 if( s1 == NULL )
60 return( -1 );
61
62 s2 = (unsigned char *) strstr( (char *) input, "-----END" );
63 if( s2 == NULL )
64 return( -1 );
65
66 s1 += 10;
67 while( s1 < end && *s1 != '-' )
68 s1++;
69 while( s1 < end && *s1 == '-' )
70 s1++;
71 if( *s1 == '\r' ) s1++;
72 if( *s1 == '\n' ) s1++;
73
74 if( s2 <= s1 || s2 > end )
75 return( -1 );
76
77 ret = base64_decode( NULL, &len, (const unsigned char *) s1, s2 - s1 );
78 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
79 return( ret );
80
81 if( len > *olen )
82 return( -1 );
83
84 if( ( ret = base64_decode( output, &len, (const unsigned char *) s1,
85 s2 - s1 ) ) != 0 )
86 {
87 return( ret );
88 }
89
90 *olen = len;
91
92 return( 0 );
93}
94
95/*
96 * Load all data from a file into a given buffer.
97 */
98static int load_file( const char *path, unsigned char **buf, size_t *n )
99{
100 FILE *f;
101 long size;
102
103 if( ( f = fopen( path, "rb" ) ) == NULL )
104 return( -1 );
105
106 fseek( f, 0, SEEK_END );
107 if( ( size = ftell( f ) ) == -1 )
108 {
109 fclose( f );
110 return( -1 );
111 }
112 fseek( f, 0, SEEK_SET );
113
114 *n = (size_t) size;
115
116 if( *n + 1 == 0 ||
117 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
118 {
119 fclose( f );
120 return( -1 );
121 }
122
123 if( fread( *buf, 1, *n, f ) != *n )
124 {
125 fclose( f );
126 free( *buf );
127 return( -1 );
128 }
129
130 fclose( f );
131
132 (*buf)[*n] = '\0';
133
134 return( 0 );
135}
136
137/*
138 * Write buffer to a file
139 */
140static int write_file( const char *path, unsigned char *buf, size_t n )
141{
142 FILE *f;
143
144 if( ( f = fopen( path, "wb" ) ) == NULL )
145 return( -1 );
146
147 if( fwrite( buf, 1, n, f ) != n )
148 {
149 fclose( f );
150 return( -1 );
151 }
152
153 fclose( f );
154 return( 0 );
155}
156
157#define USAGE \
158 "\n usage: pem2der param=<>...\n" \
159 "\n acceptable parameters:\n" \
160 " filename=%%s default: file.pem\n" \
161 " output_file=%%s default: file.der\n" \
162 "\n"
163
164#if !defined(POLARSSL_BASE64_C) || !defined(POLARSSL_FS_IO)
165int main( int argc, char *argv[] )
166{
167 ((void) argc);
168 ((void) argv);
169
170 printf("POLARSSL_BASE64_C and/or POLARSSL_FS_IO not defined.\n");
171 return( 0 );
172}
173#else
174int 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);
181 int i, j, n;
182 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
208 n = strlen( p );
209 for( j = 0; j < n; j++ )
210 {
211 if( argv[i][j] >= 'A' && argv[i][j] <= 'Z' )
212 argv[i][j] |= 0x20;
213 }
214
215 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 */
226 printf( "\n . Loading the PEM file ..." );
227 fflush( stdout );
228
229 ret = load_file( opt.filename, &pem_buffer, &pem_size );
230
231 if( ret != 0 )
232 {
233#ifdef POLARSSL_ERROR_C
234 error_strerror( ret, buf, 1024 );
235#endif
236 printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
237 goto exit;
238 }
239
240 printf( " ok\n" );
241
242 /*
243 * 1.2. Convert from PEM to DER
244 */
245 printf( " . Converting from PEM to DER ..." );
246 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
251 error_strerror( ret, buf, 1024 );
252#endif
253 printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
254 goto exit;
255 }
256
257 printf( " ok\n" );
258
259 /*
260 * 1.3. Write the DER file
261 */
262 printf( " . Writing the DER file ..." );
263 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
270 error_strerror( ret, buf, 1024 );
271#endif
272 printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
273 goto exit;
274 }
275
276 printf( " ok\n" );
277
278exit:
279 free( pem_buffer );
280
281#if defined(_WIN32)
282 printf( " + Press Enter to exit this program.\n" );
283 fflush( stdout ); getchar();
284#endif
285
286 return( ret );
287}
288#endif /* POLARSSL_BASE64_C && POLARSSL_FS_IO */