1 /*
2 * Convert PEM to DER
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20 #if !defined(MBEDTLS_CONFIG_FILE)
21 #include "mbedtls/config.h"
22 #else
23 #include MBEDTLS_CONFIG_FILE
24 #endif
25
26 #if defined(MBEDTLS_PLATFORM_C)
27 #include "mbedtls/platform.h"
28 #else
29 #include <stdio.h>
30 #include <stdlib.h>
31 #define mbedtls_free free
32 #define mbedtls_calloc calloc
33 #define mbedtls_printf printf
34 #define mbedtls_exit exit
35 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
36 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
37 #endif /* MBEDTLS_PLATFORM_C */
38
39 #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
40 #include "mbedtls/error.h"
41 #include "mbedtls/base64.h"
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #endif
47
48 #define DFL_FILENAME "file.pem"
49 #define DFL_OUTPUT_FILENAME "file.der"
50
51 #define USAGE \
52 "\n usage: pem2der param=<>...\n" \
53 "\n acceptable parameters:\n" \
54 " filename=%%s default: file.pem\n" \
55 " output_file=%%s default: file.der\n" \
56 "\n"
57
58 #if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
main(void)59 int main( void )
60 {
61 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
62 mbedtls_exit( 0 );
63 }
64 #else
65
66
67 /*
68 * global options
69 */
70 struct options
71 {
72 const char *filename; /* filename of the input file */
73 const char *output_file; /* where to store the output */
74 } opt;
75
convert_pem_to_der(const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen)76 int convert_pem_to_der( const unsigned char *input, size_t ilen,
77 unsigned char *output, size_t *olen )
78 {
79 int ret;
80 const unsigned char *s1, *s2, *end = input + ilen;
81 size_t len = 0;
82
83 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
84 if( s1 == NULL )
85 return( -1 );
86
87 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
88 if( s2 == NULL )
89 return( -1 );
90
91 s1 += 10;
92 while( s1 < end && *s1 != '-' )
93 s1++;
94 while( s1 < end && *s1 == '-' )
95 s1++;
96 if( *s1 == '\r' ) s1++;
97 if( *s1 == '\n' ) s1++;
98
99 if( s2 <= s1 || s2 > end )
100 return( -1 );
101
102 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
103 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
104 return( ret );
105
106 if( len > *olen )
107 return( -1 );
108
109 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
110 s2 - s1 ) ) != 0 )
111 {
112 return( ret );
113 }
114
115 *olen = len;
116
117 return( 0 );
118 }
119
120 /*
121 * Load all data from a file into a given buffer.
122 */
load_file(const char * path,unsigned char ** buf,size_t * n)123 static int load_file( const char *path, unsigned char **buf, size_t *n )
124 {
125 FILE *f;
126 long size;
127
128 if( ( f = fopen( path, "rb" ) ) == NULL )
129 return( -1 );
130
131 fseek( f, 0, SEEK_END );
132 if( ( size = ftell( f ) ) == -1 )
133 {
134 fclose( f );
135 return( -1 );
136 }
137 fseek( f, 0, SEEK_SET );
138
139 *n = (size_t) size;
140
141 if( *n + 1 == 0 ||
142 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
143 {
144 fclose( f );
145 return( -1 );
146 }
147
148 if( fread( *buf, 1, *n, f ) != *n )
149 {
150 fclose( f );
151 free( *buf );
152 *buf = NULL;
153 return( -1 );
154 }
155
156 fclose( f );
157
158 (*buf)[*n] = '\0';
159
160 return( 0 );
161 }
162
163 /*
164 * Write buffer to a file
165 */
write_file(const char * path,unsigned char * buf,size_t n)166 static int write_file( const char *path, unsigned char *buf, size_t n )
167 {
168 FILE *f;
169
170 if( ( f = fopen( path, "wb" ) ) == NULL )
171 return( -1 );
172
173 if( fwrite( buf, 1, n, f ) != n )
174 {
175 fclose( f );
176 return( -1 );
177 }
178
179 fclose( f );
180 return( 0 );
181 }
182
main(int argc,char * argv[])183 int main( int argc, char *argv[] )
184 {
185 int ret = 1;
186 int exit_code = MBEDTLS_EXIT_FAILURE;
187 unsigned char *pem_buffer = NULL;
188 unsigned char der_buffer[4096];
189 char buf[1024];
190 size_t pem_size, der_size = sizeof(der_buffer);
191 int i;
192 char *p, *q;
193
194 /*
195 * Set to sane values
196 */
197 memset( buf, 0, sizeof(buf) );
198 memset( der_buffer, 0, sizeof(der_buffer) );
199
200 if( argc == 0 )
201 {
202 usage:
203 mbedtls_printf( USAGE );
204 goto exit;
205 }
206
207 opt.filename = DFL_FILENAME;
208 opt.output_file = DFL_OUTPUT_FILENAME;
209
210 for( i = 1; i < argc; i++ )
211 {
212
213 p = argv[i];
214 if( ( q = strchr( p, '=' ) ) == NULL )
215 goto usage;
216 *q++ = '\0';
217
218 if( strcmp( p, "filename" ) == 0 )
219 opt.filename = q;
220 else if( strcmp( p, "output_file" ) == 0 )
221 opt.output_file = q;
222 else
223 goto usage;
224 }
225
226 /*
227 * 1.1. Load the PEM file
228 */
229 mbedtls_printf( "\n . Loading the PEM file ..." );
230 fflush( stdout );
231
232 ret = load_file( opt.filename, &pem_buffer, &pem_size );
233
234 if( ret != 0 )
235 {
236 #ifdef MBEDTLS_ERROR_C
237 mbedtls_strerror( ret, buf, 1024 );
238 #endif
239 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
240 goto exit;
241 }
242
243 mbedtls_printf( " ok\n" );
244
245 /*
246 * 1.2. Convert from PEM to DER
247 */
248 mbedtls_printf( " . Converting from PEM to DER ..." );
249 fflush( stdout );
250
251 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
252 {
253 #ifdef MBEDTLS_ERROR_C
254 mbedtls_strerror( ret, buf, 1024 );
255 #endif
256 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
257 goto exit;
258 }
259
260 mbedtls_printf( " ok\n" );
261
262 /*
263 * 1.3. Write the DER file
264 */
265 mbedtls_printf( " . Writing the DER file ..." );
266 fflush( stdout );
267
268 ret = write_file( opt.output_file, der_buffer, der_size );
269
270 if( ret != 0 )
271 {
272 #ifdef MBEDTLS_ERROR_C
273 mbedtls_strerror( ret, buf, 1024 );
274 #endif
275 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
276 goto exit;
277 }
278
279 mbedtls_printf( " ok\n" );
280
281 exit_code = MBEDTLS_EXIT_SUCCESS;
282
283 exit:
284 free( pem_buffer );
285
286 #if defined(_WIN32)
287 mbedtls_printf( " + Press Enter to exit this program.\n" );
288 fflush( stdout ); getchar();
289 #endif
290
291 mbedtls_exit( exit_code );
292 }
293 #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */
294