blob: 9f5d70a878dfe9651fa4382fd6e03293f03bf67a [file] [log] [blame]
Paul Bakker8adf13b2013-08-25 14:50:09 +02001/*
2 * Convert PEM to DER
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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.
Paul Bakker8adf13b2013-08-25 14:50:09 +020018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker8adf13b2013-08-25 14:50:09 +020025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/error.h"
30#include "mbedtls/base64.h"
Paul Bakker8adf13b2013-08-25 14:50:09 +020031
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
33#include <stdlib.h>
34#include <string.h>
35#endif
36
Paul Bakker8adf13b2013-08-25 14:50:09 +020037#define DFL_FILENAME "file.pem"
38#define DFL_OUTPUT_FILENAME "file.der"
39
Rich Evans18b78c72015-02-11 14:06:19 +000040#define USAGE \
41 "\n usage: pem2der param=<>...\n" \
42 "\n acceptable parameters:\n" \
43 " filename=%%s default: file.pem\n" \
44 " output_file=%%s default: file.der\n" \
45 "\n"
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000048int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
k-stachowiak776521a2019-05-23 09:46:47 +020051 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020052}
53#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010054
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010055
Paul Bakker8adf13b2013-08-25 14:50:09 +020056/*
57 * global options
58 */
59struct options
60{
Paul Bakker8fc30b12013-11-25 13:29:43 +010061 const char *filename; /* filename of the input file */
62 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020063} opt;
64
65int convert_pem_to_der( const unsigned char *input, size_t ilen,
66 unsigned char *output, size_t *olen )
67{
68 int ret;
69 const unsigned char *s1, *s2, *end = input + ilen;
70 size_t len = 0;
71
Paul Bakker8fc30b12013-11-25 13:29:43 +010072 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020073 if( s1 == NULL )
74 return( -1 );
75
Paul Bakker8fc30b12013-11-25 13:29:43 +010076 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020077 if( s2 == NULL )
78 return( -1 );
79
80 s1 += 10;
81 while( s1 < end && *s1 != '-' )
82 s1++;
83 while( s1 < end && *s1 == '-' )
84 s1++;
85 if( *s1 == '\r' ) s1++;
86 if( *s1 == '\n' ) s1++;
87
88 if( s2 <= s1 || s2 > end )
89 return( -1 );
90
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010091 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker8adf13b2013-08-25 14:50:09 +020093 return( ret );
94
95 if( len > *olen )
96 return( -1 );
97
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010098 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
Paul Bakker8adf13b2013-08-25 14:50:09 +020099 s2 - s1 ) ) != 0 )
100 {
101 return( ret );
102 }
103
104 *olen = len;
105
106 return( 0 );
107}
108
109/*
110 * Load all data from a file into a given buffer.
111 */
112static int load_file( const char *path, unsigned char **buf, size_t *n )
113{
114 FILE *f;
115 long size;
116
117 if( ( f = fopen( path, "rb" ) ) == NULL )
118 return( -1 );
119
120 fseek( f, 0, SEEK_END );
121 if( ( size = ftell( f ) ) == -1 )
122 {
123 fclose( f );
124 return( -1 );
125 }
126 fseek( f, 0, SEEK_SET );
127
128 *n = (size_t) size;
129
130 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200131 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200132 {
133 fclose( f );
134 return( -1 );
135 }
136
137 if( fread( *buf, 1, *n, f ) != *n )
138 {
139 fclose( f );
140 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200141 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200142 return( -1 );
143 }
144
145 fclose( f );
146
147 (*buf)[*n] = '\0';
148
149 return( 0 );
150}
151
152/*
153 * Write buffer to a file
154 */
155static int write_file( const char *path, unsigned char *buf, size_t n )
156{
157 FILE *f;
158
159 if( ( f = fopen( path, "wb" ) ) == NULL )
160 return( -1 );
161
162 if( fwrite( buf, 1, n, f ) != n )
163 {
164 fclose( f );
165 return( -1 );
166 }
167
168 fclose( f );
169 return( 0 );
170}
171
Paul Bakker8adf13b2013-08-25 14:50:09 +0200172int main( int argc, char *argv[] )
173{
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100174 int ret = 1;
175 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200176 unsigned char *pem_buffer = NULL;
177 unsigned char der_buffer[4096];
178 char buf[1024];
179 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100180 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200181 char *p, *q;
182
183 /*
184 * Set to sane values
185 */
186 memset( buf, 0, sizeof(buf) );
187 memset( der_buffer, 0, sizeof(der_buffer) );
188
189 if( argc == 0 )
190 {
191 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200193 goto exit;
194 }
195
196 opt.filename = DFL_FILENAME;
197 opt.output_file = DFL_OUTPUT_FILENAME;
198
199 for( i = 1; i < argc; i++ )
200 {
201
202 p = argv[i];
203 if( ( q = strchr( p, '=' ) ) == NULL )
204 goto usage;
205 *q++ = '\0';
206
Paul Bakker8adf13b2013-08-25 14:50:09 +0200207 if( strcmp( p, "filename" ) == 0 )
208 opt.filename = q;
209 else if( strcmp( p, "output_file" ) == 0 )
210 opt.output_file = q;
211 else
212 goto usage;
213 }
214
215 /*
216 * 1.1. Load the PEM file
217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 mbedtls_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200219 fflush( stdout );
220
221 ret = load_file( opt.filename, &pem_buffer, &pem_size );
222
223 if( ret != 0 )
224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#ifdef MBEDTLS_ERROR_C
226 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200229 goto exit;
230 }
231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200233
234 /*
235 * 1.2. Convert from PEM to DER
236 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200238 fflush( stdout );
239
240 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#ifdef MBEDTLS_ERROR_C
243 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200244#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200246 goto exit;
247 }
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200250
251 /*
252 * 1.3. Write the DER file
253 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200255 fflush( stdout );
256
257 ret = write_file( opt.output_file, der_buffer, der_size );
258
259 if( ret != 0 )
260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#ifdef MBEDTLS_ERROR_C
262 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200265 goto exit;
266 }
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200269
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100270 exit_code = MBEDTLS_EXIT_SUCCESS;
271
Paul Bakker8adf13b2013-08-25 14:50:09 +0200272exit:
273 free( pem_buffer );
274
275#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200277 fflush( stdout ); getchar();
278#endif
279
k-stachowiak776521a2019-05-23 09:46:47 +0200280 mbedtls_exit( exit_code );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200281}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */