1 /*
2  *  Convert PEM to DER
3  *
4  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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  *  This file is part of mbed TLS (https://tls.mbed.org)
20  */
21 
22 #if !defined(MBEDTLS_CONFIG_FILE)
23 #include "mbedtls/config.h"
24 #else
25 #include MBEDTLS_CONFIG_FILE
26 #endif
27 
28 #if defined(MBEDTLS_PLATFORM_C)
29 #include "mbedtls/platform.h"
30 #else
31 #include <stdio.h>
32 #define mbedtls_free       free
33 #define mbedtls_calloc    calloc
34 #define mbedtls_printf     printf
35 #endif
36 
37 #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
38 #include "mbedtls/error.h"
39 #include "mbedtls/base64.h"
40 
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #endif
45 
46 #define DFL_FILENAME            "file.pem"
47 #define DFL_OUTPUT_FILENAME     "file.der"
48 
49 #define USAGE \
50     "\n usage: pem2der param=<>...\n"                   \
51     "\n acceptable parameters:\n"                       \
52     "    filename=%%s         default: file.pem\n"      \
53     "    output_file=%%s      default: file.der\n"      \
54     "\n"
55 
56 #if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
main(void)57 int main( void )
58 {
59     mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
60     return( 0 );
61 }
62 #else
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 = 0;
182     unsigned char *pem_buffer = NULL;
183     unsigned char der_buffer[4096];
184     char buf[1024];
185     size_t pem_size, der_size = sizeof(der_buffer);
186     int i;
187     char *p, *q;
188 
189     /*
190      * Set to sane values
191      */
192     memset( buf, 0, sizeof(buf) );
193     memset( der_buffer, 0, sizeof(der_buffer) );
194 
195     if( argc == 0 )
196     {
197     usage:
198         mbedtls_printf( USAGE );
199         goto exit;
200     }
201 
202     opt.filename            = DFL_FILENAME;
203     opt.output_file         = DFL_OUTPUT_FILENAME;
204 
205     for( i = 1; i < argc; i++ )
206     {
207 
208         p = argv[i];
209         if( ( q = strchr( p, '=' ) ) == NULL )
210             goto usage;
211         *q++ = '\0';
212 
213         if( strcmp( p, "filename" ) == 0 )
214             opt.filename = q;
215         else if( strcmp( p, "output_file" ) == 0 )
216             opt.output_file = q;
217         else
218             goto usage;
219     }
220 
221     /*
222      * 1.1. Load the PEM file
223      */
224     mbedtls_printf( "\n  . Loading the PEM file ..." );
225     fflush( stdout );
226 
227     ret = load_file( opt.filename, &pem_buffer, &pem_size );
228 
229     if( ret != 0 )
230     {
231 #ifdef MBEDTLS_ERROR_C
232         mbedtls_strerror( ret, buf, 1024 );
233 #endif
234         mbedtls_printf( " failed\n  !  load_file returned %d - %s\n\n", ret, buf );
235         goto exit;
236     }
237 
238     mbedtls_printf( " ok\n" );
239 
240     /*
241      * 1.2. Convert from PEM to DER
242      */
243     mbedtls_printf( "  . Converting from PEM to DER ..." );
244     fflush( stdout );
245 
246     if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
247     {
248 #ifdef MBEDTLS_ERROR_C
249         mbedtls_strerror( ret, buf, 1024 );
250 #endif
251         mbedtls_printf( " failed\n  !  convert_pem_to_der %d - %s\n\n", ret, buf );
252         goto exit;
253     }
254 
255     mbedtls_printf( " ok\n" );
256 
257     /*
258      * 1.3. Write the DER file
259      */
260     mbedtls_printf( "  . Writing the DER file ..." );
261     fflush( stdout );
262 
263     ret = write_file( opt.output_file, der_buffer, der_size );
264 
265     if( ret != 0 )
266     {
267 #ifdef MBEDTLS_ERROR_C
268         mbedtls_strerror( ret, buf, 1024 );
269 #endif
270         mbedtls_printf( " failed\n  !  write_file returned %d - %s\n\n", ret, buf );
271         goto exit;
272     }
273 
274     mbedtls_printf( " ok\n" );
275 
276 exit:
277     free( pem_buffer );
278 
279 #if defined(_WIN32)
280     mbedtls_printf( "  + Press Enter to exit this program.\n" );
281     fflush( stdout ); getchar();
282 #endif
283 
284     return( ret );
285 }
286 #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */
287