blob: 08df67169dc82595ce5ddeb15248c5cb385e8da5 [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
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{
57 char *filename; /* filename of the input file */
58 char *output_file; /* where to store the output */
59} 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
68 s1 = (unsigned char *) strstr( (char *) input, "-----BEGIN" );
69 if( s1 == NULL )
70 return( -1 );
71
72 s2 = (unsigned char *) strstr( (char *) input, "-----END" );
73 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);
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 */