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 #include "mbedtls/build_info.h"
21
22 #if defined(MBEDTLS_PLATFORM_C)
23 #include "mbedtls/platform.h"
24 #else
25 #include <stdio.h>
26 #include <stdlib.h>
27 #define mbedtls_free free
28 #define mbedtls_calloc calloc
29 #define mbedtls_printf printf
30 #define mbedtls_exit exit
31 #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
32 #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
33 #endif /* MBEDTLS_PLATFORM_C */
34
35 #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
36 #include "mbedtls/error.h"
37 #include "mbedtls/base64.h"
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #endif
43
44 #define DFL_FILENAME "file.pem"
45 #define DFL_OUTPUT_FILENAME "file.der"
46
47 #define USAGE \
48 "\n usage: pem2der param=<>...\n" \
49 "\n acceptable parameters:\n" \
50 " filename=%%s default: file.pem\n" \
51 " output_file=%%s default: file.der\n" \
52 "\n"
53
54 #if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
main(void)55 int main( void )
56 {
57 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
58 mbedtls_exit( 0 );
59 }
60 #else
61
62
63 /*
64 * global options
65 */
66 struct options
67 {
68 const char *filename; /* filename of the input file */
69 const char *output_file; /* where to store the output */
70 } opt;
71
convert_pem_to_der(const unsigned char * input,size_t ilen,unsigned char * output,size_t * olen)72 int convert_pem_to_der( const unsigned char *input, size_t ilen,
73 unsigned char *output, size_t *olen )
74 {
75 int ret;
76 const unsigned char *s1, *s2, *end = input + ilen;
77 size_t len = 0;
78
79 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
80 if( s1 == NULL )
81 return( -1 );
82
83 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
84 if( s2 == NULL )
85 return( -1 );
86
87 s1 += 10;
88 while( s1 < end && *s1 != '-' )
89 s1++;
90 while( s1 < end && *s1 == '-' )
91 s1++;
92 if( *s1 == '\r' ) s1++;
93 if( *s1 == '\n' ) s1++;
94
95 if( s2 <= s1 || s2 > end )
96 return( -1 );
97
98 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
99 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
100 return( ret );
101
102 if( len > *olen )
103 return( -1 );
104
105 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
106 s2 - s1 ) ) != 0 )
107 {
108 return( ret );
109 }
110
111 *olen = len;
112
113 return( 0 );
114 }
115
116 /*
117 * Load all data from a file into a given buffer.
118 */
load_file(const char * path,unsigned char ** buf,size_t * n)119 static int load_file( const char *path, unsigned char **buf, size_t *n )
120 {
121 FILE *f;
122 long size;
123
124 if( ( f = fopen( path, "rb" ) ) == NULL )
125 return( -1 );
126
127 fseek( f, 0, SEEK_END );
128 if( ( size = ftell( f ) ) == -1 )
129 {
130 fclose( f );
131 return( -1 );
132 }
133 fseek( f, 0, SEEK_SET );
134
135 *n = (size_t) size;
136
137 if( *n + 1 == 0 ||
138 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
139 {
140 fclose( f );
141 return( -1 );
142 }
143
144 if( fread( *buf, 1, *n, f ) != *n )
145 {
146 fclose( f );
147 free( *buf );
148 *buf = NULL;
149 return( -1 );
150 }
151
152 fclose( f );
153
154 (*buf)[*n] = '\0';
155
156 return( 0 );
157 }
158
159 /*
160 * Write buffer to a file
161 */
write_file(const char * path,unsigned char * buf,size_t n)162 static int write_file( const char *path, unsigned char *buf, size_t n )
163 {
164 FILE *f;
165
166 if( ( f = fopen( path, "wb" ) ) == NULL )
167 return( -1 );
168
169 if( fwrite( buf, 1, n, f ) != n )
170 {
171 fclose( f );
172 return( -1 );
173 }
174
175 fclose( f );
176 return( 0 );
177 }
178
main(int argc,char * argv[])179 int main( int argc, char *argv[] )
180 {
181 int ret = 1;
182 int exit_code = MBEDTLS_EXIT_FAILURE;
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);
187 int i;
188 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:
199 mbedtls_printf( USAGE );
200 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
214 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 */
225 mbedtls_printf( "\n . Loading the PEM file ..." );
226 fflush( stdout );
227
228 ret = load_file( opt.filename, &pem_buffer, &pem_size );
229
230 if( ret != 0 )
231 {
232 #ifdef MBEDTLS_ERROR_C
233 mbedtls_strerror( ret, buf, 1024 );
234 #endif
235 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
236 goto exit;
237 }
238
239 mbedtls_printf( " ok\n" );
240
241 /*
242 * 1.2. Convert from PEM to DER
243 */
244 mbedtls_printf( " . Converting from PEM to DER ..." );
245 fflush( stdout );
246
247 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
248 {
249 #ifdef MBEDTLS_ERROR_C
250 mbedtls_strerror( ret, buf, 1024 );
251 #endif
252 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
253 goto exit;
254 }
255
256 mbedtls_printf( " ok\n" );
257
258 /*
259 * 1.3. Write the DER file
260 */
261 mbedtls_printf( " . Writing the DER file ..." );
262 fflush( stdout );
263
264 ret = write_file( opt.output_file, der_buffer, der_size );
265
266 if( ret != 0 )
267 {
268 #ifdef MBEDTLS_ERROR_C
269 mbedtls_strerror( ret, buf, 1024 );
270 #endif
271 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
272 goto exit;
273 }
274
275 mbedtls_printf( " ok\n" );
276
277 exit_code = MBEDTLS_EXIT_SUCCESS;
278
279 exit:
280 free( pem_buffer );
281
282 #if defined(_WIN32)
283 mbedtls_printf( " + Press Enter to exit this program.\n" );
284 fflush( stdout ); getchar();
285 #endif
286
287 mbedtls_exit( exit_code );
288 }
289 #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */
290