blob: 1ba478b32367f9d67b5203520f0c192d40ce05cf [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é-Gonnard085ab042015-01-23 11:06:27 +00006 * This file is part of mbed TLS (https://www.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
29#include <string.h>
30#include <stdlib.h>
31#include <stdio.h>
32
Paul Bakker8adf13b2013-08-25 14:50:09 +020033#include "polarssl/error.h"
34#include "polarssl/base64.h"
35
36#define DFL_FILENAME "file.pem"
37#define DFL_OUTPUT_FILENAME "file.der"
38
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020039#if !defined(POLARSSL_BASE64_C) || !defined(POLARSSL_FS_IO)
40int main( int argc, char *argv[] )
41{
42 ((void) argc);
43 ((void) argv);
44
45 printf("POLARSSL_BASE64_C and/or POLARSSL_FS_IO not defined.\n");
46 return( 0 );
47}
48#else
Paul Bakker8adf13b2013-08-25 14:50:09 +020049/*
50 * global options
51 */
52struct options
53{
Paul Bakker8fc30b12013-11-25 13:29:43 +010054 const char *filename; /* filename of the input file */
55 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020056} opt;
57
58int convert_pem_to_der( const unsigned char *input, size_t ilen,
59 unsigned char *output, size_t *olen )
60{
61 int ret;
62 const unsigned char *s1, *s2, *end = input + ilen;
63 size_t len = 0;
64
Paul Bakker8fc30b12013-11-25 13:29:43 +010065 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020066 if( s1 == NULL )
67 return( -1 );
68
Paul Bakker8fc30b12013-11-25 13:29:43 +010069 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020070 if( s2 == NULL )
71 return( -1 );
72
73 s1 += 10;
74 while( s1 < end && *s1 != '-' )
75 s1++;
76 while( s1 < end && *s1 == '-' )
77 s1++;
78 if( *s1 == '\r' ) s1++;
79 if( *s1 == '\n' ) s1++;
80
81 if( s2 <= s1 || s2 > end )
82 return( -1 );
83
84 ret = base64_decode( NULL, &len, (const unsigned char *) s1, s2 - s1 );
85 if( ret == POLARSSL_ERR_BASE64_INVALID_CHARACTER )
86 return( ret );
87
88 if( len > *olen )
89 return( -1 );
90
91 if( ( ret = base64_decode( output, &len, (const unsigned char *) s1,
92 s2 - s1 ) ) != 0 )
93 {
94 return( ret );
95 }
96
97 *olen = len;
98
99 return( 0 );
100}
101
102/*
103 * Load all data from a file into a given buffer.
104 */
105static int load_file( const char *path, unsigned char **buf, size_t *n )
106{
107 FILE *f;
108 long size;
109
110 if( ( f = fopen( path, "rb" ) ) == NULL )
111 return( -1 );
112
113 fseek( f, 0, SEEK_END );
114 if( ( size = ftell( f ) ) == -1 )
115 {
116 fclose( f );
117 return( -1 );
118 }
119 fseek( f, 0, SEEK_SET );
120
121 *n = (size_t) size;
122
123 if( *n + 1 == 0 ||
124 ( *buf = (unsigned char *) malloc( *n + 1 ) ) == NULL )
125 {
126 fclose( f );
127 return( -1 );
128 }
129
130 if( fread( *buf, 1, *n, f ) != *n )
131 {
132 fclose( f );
133 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200134 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200135 return( -1 );
136 }
137
138 fclose( f );
139
140 (*buf)[*n] = '\0';
141
142 return( 0 );
143}
144
145/*
146 * Write buffer to a file
147 */
148static int write_file( const char *path, unsigned char *buf, size_t n )
149{
150 FILE *f;
151
152 if( ( f = fopen( path, "wb" ) ) == NULL )
153 return( -1 );
154
155 if( fwrite( buf, 1, n, f ) != n )
156 {
157 fclose( f );
158 return( -1 );
159 }
160
161 fclose( f );
162 return( 0 );
163}
164
165#define USAGE \
166 "\n usage: pem2der param=<>...\n" \
167 "\n acceptable parameters:\n" \
168 " filename=%%s default: file.pem\n" \
169 " output_file=%%s default: file.der\n" \
170 "\n"
171
Paul Bakker8adf13b2013-08-25 14:50:09 +0200172int main( int argc, char *argv[] )
173{
174 int ret = 0;
175 unsigned char *pem_buffer = NULL;
176 unsigned char der_buffer[4096];
177 char buf[1024];
178 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100179 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200180 char *p, *q;
181
182 /*
183 * Set to sane values
184 */
185 memset( buf, 0, sizeof(buf) );
186 memset( der_buffer, 0, sizeof(der_buffer) );
187
188 if( argc == 0 )
189 {
190 usage:
191 printf( USAGE );
192 goto exit;
193 }
194
195 opt.filename = DFL_FILENAME;
196 opt.output_file = DFL_OUTPUT_FILENAME;
197
198 for( i = 1; i < argc; i++ )
199 {
200
201 p = argv[i];
202 if( ( q = strchr( p, '=' ) ) == NULL )
203 goto usage;
204 *q++ = '\0';
205
Paul Bakker8adf13b2013-08-25 14:50:09 +0200206 if( strcmp( p, "filename" ) == 0 )
207 opt.filename = q;
208 else if( strcmp( p, "output_file" ) == 0 )
209 opt.output_file = q;
210 else
211 goto usage;
212 }
213
214 /*
215 * 1.1. Load the PEM file
216 */
217 printf( "\n . Loading the PEM file ..." );
218 fflush( stdout );
219
220 ret = load_file( opt.filename, &pem_buffer, &pem_size );
221
222 if( ret != 0 )
223 {
224#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700225 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200226#endif
227 printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
228 goto exit;
229 }
230
231 printf( " ok\n" );
232
233 /*
234 * 1.2. Convert from PEM to DER
235 */
236 printf( " . Converting from PEM to DER ..." );
237 fflush( stdout );
238
239 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
240 {
241#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700242 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200243#endif
244 printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
245 goto exit;
246 }
247
248 printf( " ok\n" );
249
250 /*
251 * 1.3. Write the DER file
252 */
253 printf( " . Writing the DER file ..." );
254 fflush( stdout );
255
256 ret = write_file( opt.output_file, der_buffer, der_size );
257
258 if( ret != 0 )
259 {
260#ifdef POLARSSL_ERROR_C
Shuo Chen95a0d112014-04-04 21:04:40 -0700261 polarssl_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200262#endif
263 printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
264 goto exit;
265 }
266
267 printf( " ok\n" );
268
269exit:
270 free( pem_buffer );
271
272#if defined(_WIN32)
273 printf( " + Press Enter to exit this program.\n" );
274 fflush( stdout ); getchar();
275#endif
276
277 return( ret );
278}
279#endif /* POLARSSL_BASE64_C && POLARSSL_FS_IO */