blob: 7138fa8544a855e2cc3e2a4f1765bb5aaa936536 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker8adf13b2013-08-25 14:50:09 +020021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000025#include "mbedtls/error.h"
26#include "mbedtls/base64.h"
Paul Bakker8adf13b2013-08-25 14:50:09 +020027
Rich Evans18b78c72015-02-11 14:06:19 +000028#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31#endif
32
Paul Bakker8adf13b2013-08-25 14:50:09 +020033#define DFL_FILENAME "file.pem"
34#define DFL_OUTPUT_FILENAME "file.der"
35
Rich Evans18b78c72015-02-11 14:06:19 +000036#define USAGE \
37 "\n usage: pem2der param=<>...\n" \
38 "\n acceptable parameters:\n" \
39 " filename=%%s default: file.pem\n" \
40 " output_file=%%s default: file.der\n" \
41 "\n"
42
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000044int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020045{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
k-stachowiak776521a2019-05-23 09:46:47 +020047 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020048}
49#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010050
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010051
Paul Bakker8adf13b2013-08-25 14:50:09 +020052/*
53 * global options
54 */
55struct options
56{
Paul Bakker8fc30b12013-11-25 13:29:43 +010057 const char *filename; /* filename of the input file */
58 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020059} opt;
60
61int convert_pem_to_der( const unsigned char *input, size_t ilen,
62 unsigned char *output, size_t *olen )
63{
64 int ret;
65 const unsigned char *s1, *s2, *end = input + ilen;
66 size_t len = 0;
67
Paul Bakker8fc30b12013-11-25 13:29:43 +010068 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020069 if( s1 == NULL )
70 return( -1 );
71
Paul Bakker8fc30b12013-11-25 13:29:43 +010072 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020073 if( s2 == NULL )
74 return( -1 );
75
76 s1 += 10;
77 while( s1 < end && *s1 != '-' )
78 s1++;
79 while( s1 < end && *s1 == '-' )
80 s1++;
81 if( *s1 == '\r' ) s1++;
82 if( *s1 == '\n' ) s1++;
83
84 if( s2 <= s1 || s2 > end )
85 return( -1 );
86
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010087 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker8adf13b2013-08-25 14:50:09 +020089 return( ret );
90
91 if( len > *olen )
92 return( -1 );
93
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010094 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
Paul Bakker8adf13b2013-08-25 14:50:09 +020095 s2 - s1 ) ) != 0 )
96 {
97 return( ret );
98 }
99
100 *olen = len;
101
102 return( 0 );
103}
104
105/*
106 * Load all data from a file into a given buffer.
107 */
108static int load_file( const char *path, unsigned char **buf, size_t *n )
109{
110 FILE *f;
111 long size;
112
113 if( ( f = fopen( path, "rb" ) ) == NULL )
114 return( -1 );
115
116 fseek( f, 0, SEEK_END );
117 if( ( size = ftell( f ) ) == -1 )
118 {
119 fclose( f );
120 return( -1 );
121 }
122 fseek( f, 0, SEEK_SET );
123
124 *n = (size_t) size;
125
126 if( *n + 1 == 0 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200127 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200128 {
129 fclose( f );
130 return( -1 );
131 }
132
133 if( fread( *buf, 1, *n, f ) != *n )
134 {
135 fclose( f );
136 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200137 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200138 return( -1 );
139 }
140
141 fclose( f );
142
143 (*buf)[*n] = '\0';
144
145 return( 0 );
146}
147
148/*
149 * Write buffer to a file
150 */
151static int write_file( const char *path, unsigned char *buf, size_t n )
152{
153 FILE *f;
154
155 if( ( f = fopen( path, "wb" ) ) == NULL )
156 return( -1 );
157
158 if( fwrite( buf, 1, n, f ) != n )
159 {
160 fclose( f );
161 return( -1 );
162 }
163
164 fclose( f );
165 return( 0 );
166}
167
Paul Bakker8adf13b2013-08-25 14:50:09 +0200168int main( int argc, char *argv[] )
169{
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100170 int ret = 1;
171 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200172 unsigned char *pem_buffer = NULL;
173 unsigned char der_buffer[4096];
174 char buf[1024];
175 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100176 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200177 char *p, *q;
178
179 /*
180 * Set to sane values
181 */
182 memset( buf, 0, sizeof(buf) );
183 memset( der_buffer, 0, sizeof(der_buffer) );
184
185 if( argc == 0 )
186 {
187 usage:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 mbedtls_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200189 goto exit;
190 }
191
192 opt.filename = DFL_FILENAME;
193 opt.output_file = DFL_OUTPUT_FILENAME;
194
195 for( i = 1; i < argc; i++ )
196 {
197
198 p = argv[i];
199 if( ( q = strchr( p, '=' ) ) == NULL )
200 goto usage;
201 *q++ = '\0';
202
Paul Bakker8adf13b2013-08-25 14:50:09 +0200203 if( strcmp( p, "filename" ) == 0 )
204 opt.filename = q;
205 else if( strcmp( p, "output_file" ) == 0 )
206 opt.output_file = q;
207 else
208 goto usage;
209 }
210
211 /*
212 * 1.1. Load the PEM file
213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200215 fflush( stdout );
216
217 ret = load_file( opt.filename, &pem_buffer, &pem_size );
218
219 if( ret != 0 )
220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#ifdef MBEDTLS_ERROR_C
222 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200225 goto exit;
226 }
227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200229
230 /*
231 * 1.2. Convert from PEM to DER
232 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 mbedtls_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200234 fflush( stdout );
235
236 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238#ifdef MBEDTLS_ERROR_C
239 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200240#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200242 goto exit;
243 }
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200246
247 /*
248 * 1.3. Write the DER file
249 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200251 fflush( stdout );
252
253 ret = write_file( opt.output_file, der_buffer, der_size );
254
255 if( ret != 0 )
256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#ifdef MBEDTLS_ERROR_C
258 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200259#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200261 goto exit;
262 }
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200265
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100266 exit_code = MBEDTLS_EXIT_SUCCESS;
267
Paul Bakker8adf13b2013-08-25 14:50:09 +0200268exit:
269 free( pem_buffer );
270
k-stachowiak776521a2019-05-23 09:46:47 +0200271 mbedtls_exit( exit_code );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */