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