blob: 74f7a3e81281b8f1e2686f3b8ed31b590c1db6ef [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é-Gonnard860b5162015-01-28 17:12:07 +00006 * This file is part of mbed TLS (https://polarssl.org)
Paul Bakker8adf13b2013-08-25 14:50:09 +02007 *
Paul Bakker8adf13b2013-08-25 14:50:09 +02008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020023#if !defined(POLARSSL_CONFIG_FILE)
Manuel Pégourié-Gonnardabd6e022013-09-20 13:30:43 +020024#include "polarssl/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
26#include POLARSSL_CONFIG_FILE
27#endif
Paul Bakker8adf13b2013-08-25 14:50:09 +020028
Rich Evansf90016a2015-01-19 14:26:37 +000029#if defined(POLARSSL_PLATFORM_C)
30#include "polarssl/platform.h"
31#else
32#define polarssl_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000033#define polarssl_malloc malloc
34#define polarssl_free free
35#endif
36
Paul Bakker8adf13b2013-08-25 14:50:09 +020037#include <string.h>
38#include <stdlib.h>
39#include <stdio.h>
40
Paul Bakker8adf13b2013-08-25 14:50:09 +020041#include "polarssl/error.h"
42#include "polarssl/base64.h"
43
44#define DFL_FILENAME "file.pem"
45#define DFL_OUTPUT_FILENAME "file.der"
46
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020047#if !defined(POLARSSL_BASE64_C) || !defined(POLARSSL_FS_IO)
48int main( int argc, char *argv[] )
49{
50 ((void) argc);
51 ((void) argv);
52
Rich Evansf90016a2015-01-19 14:26:37 +000053 polarssl_printf("POLARSSL_BASE64_C and/or POLARSSL_FS_IO not defined.\n");
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020054 return( 0 );
55}
56#else
Paul Bakker8adf13b2013-08-25 14:50:09 +020057/*
58 * global options
59 */
60struct options
61{
Paul Bakker8fc30b12013-11-25 13:29:43 +010062 const char *filename; /* filename of the input file */
63 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020064} opt;
65
66int convert_pem_to_der( const unsigned char *input, size_t ilen,
67 unsigned char *output, size_t *olen )
68{
69 int ret;
70 const unsigned char *s1, *s2, *end = input + ilen;
71 size_t len = 0;
72
Paul Bakker8fc30b12013-11-25 13:29:43 +010073 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020074 if( s1 == NULL )
75 return( -1 );
76
Paul Bakker8fc30b12013-11-25 13:29:43 +010077 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020078 if( s2 == NULL )
79 return( -1 );
80
81 s1 += 10;
82 while( s1 < end && *s1 != '-' )
83 s1++;
84 while( s1 < end && *s1 == '-' )
85 s1++;
86 if( *s1 == '\r' ) s1++;
87 if( *s1 == '\n' ) s1++;
88
89 if( s2 <= s1 || s2 > end )
90 return( -1 );
91
92 ret = base64_decode( NULL, &len, (const unsigned char *) s1, s2 - s1 );
93 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
94 return( ret );
95
96 if( len > *olen )
97 return( -1 );
98
99 if( ( ret = base64_decode( output, &len, (const unsigned char *) s1,
100 s2 - s1 ) ) != 0 )
101 {
102 return( ret );
103 }
104
105 *olen = len;
106
107 return( 0 );
108}
109
110/*
111 * Load all data from a file into a given buffer.
112 */
113static int load_file( const char *path, unsigned char **buf, size_t *n )
114{
115 FILE *f;
116 long size;
117
118 if( ( f = fopen( path, "rb" ) ) == NULL )
119 return( -1 );
120
121 fseek( f, 0, SEEK_END );
122 if( ( size = ftell( f ) ) == -1 )
123 {
124 fclose( f );
125 return( -1 );
126 }
127 fseek( f, 0, SEEK_SET );
128
129 *n = (size_t) size;
130
131 if( *n + 1 == 0 ||
Rich Evansf90016a2015-01-19 14:26:37 +0000132 ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200133 {
134 fclose( f );
135 return( -1 );
136 }
137
138 if( fread( *buf, 1, *n, f ) != *n )
139 {
140 fclose( f );
141 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200142 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200143 return( -1 );
144 }
145
146 fclose( f );
147
148 (*buf)[*n] = '\0';
149
150 return( 0 );
151}
152
153/*
154 * Write buffer to a file
155 */
156static int write_file( const char *path, unsigned char *buf, size_t n )
157{
158 FILE *f;
159
160 if( ( f = fopen( path, "wb" ) ) == NULL )
161 return( -1 );
162
163 if( fwrite( buf, 1, n, f ) != n )
164 {
165 fclose( f );
166 return( -1 );
167 }
168
169 fclose( f );
170 return( 0 );
171}
172
173#define USAGE \
174 "\n usage: pem2der param=<>...\n" \
175 "\n acceptable parameters:\n" \
176 " filename=%%s default: file.pem\n" \
177 " output_file=%%s default: file.der\n" \
178 "\n"
179
Paul Bakker8adf13b2013-08-25 14:50:09 +0200180int main( int argc, char *argv[] )
181{
182 int ret = 0;
183 unsigned char *pem_buffer = NULL;
184 unsigned char der_buffer[4096];
185 char buf[1024];
186 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100187 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200188 char *p, *q;
189
190 /*
191 * Set to sane values
192 */
193 memset( buf, 0, sizeof(buf) );
194 memset( der_buffer, 0, sizeof(der_buffer) );
195
196 if( argc == 0 )
197 {
198 usage:
Rich Evansf90016a2015-01-19 14:26:37 +0000199 polarssl_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200200 goto exit;
201 }
202
203 opt.filename = DFL_FILENAME;
204 opt.output_file = DFL_OUTPUT_FILENAME;
205
206 for( i = 1; i < argc; i++ )
207 {
208
209 p = argv[i];
210 if( ( q = strchr( p, '=' ) ) == NULL )
211 goto usage;
212 *q++ = '\0';
213
Paul Bakker8adf13b2013-08-25 14:50:09 +0200214 if( strcmp( p, "filename" ) == 0 )
215 opt.filename = q;
216 else if( strcmp( p, "output_file" ) == 0 )
217 opt.output_file = q;
218 else
219 goto usage;
220 }
221
222 /*
223 * 1.1. Load the PEM file
224 */
Rich Evansf90016a2015-01-19 14:26:37 +0000225 polarssl_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200226 fflush( stdout );
227
228 ret = load_file( opt.filename, &pem_buffer, &pem_size );
229
230 if( ret != 0 )
231 {
232#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700233 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200234#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000235 polarssl_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200236 goto exit;
237 }
238
Rich Evansf90016a2015-01-19 14:26:37 +0000239 polarssl_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200240
241 /*
242 * 1.2. Convert from PEM to DER
243 */
Rich Evansf90016a2015-01-19 14:26:37 +0000244 polarssl_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200245 fflush( stdout );
246
247 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
248 {
249#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700250 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200251#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000252 polarssl_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200253 goto exit;
254 }
255
Rich Evansf90016a2015-01-19 14:26:37 +0000256 polarssl_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200257
258 /*
259 * 1.3. Write the DER file
260 */
Rich Evansf90016a2015-01-19 14:26:37 +0000261 polarssl_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200262 fflush( stdout );
263
264 ret = write_file( opt.output_file, der_buffer, der_size );
265
266 if( ret != 0 )
267 {
268#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700269 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200270#endif
Rich Evansf90016a2015-01-19 14:26:37 +0000271 polarssl_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200272 goto exit;
273 }
274
Rich Evansf90016a2015-01-19 14:26:37 +0000275 polarssl_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200276
277exit:
278 free( pem_buffer );
279
280#if defined(_WIN32)
Rich Evansf90016a2015-01-19 14:26:37 +0000281 polarssl_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200282 fflush( stdout ); getchar();
283#endif
284
285 return( ret );
286}
287#endif /* POLARSSL_BASE64_C && POLARSSL_FS_IO */