blob: e273200a9e1262599f40011cfa9137aceaf5339b [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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Rich Evans18b78c72015-02-11 14:06:19 +000025#include <stdio.h>
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +010026#include <stdlib.h>
27#define mbedtls_free free
28#define mbedtls_calloc calloc
29#define mbedtls_printf printf
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010030#define mbedtls_exit exit
Andres Amaya Garcia7d429652018-04-30 22:42:33 +010031#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +010032#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
33#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020035#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/error.h"
37#include "mbedtls/base64.h"
Paul Bakker8adf13b2013-08-25 14:50:09 +020038
Rich Evans18b78c72015-02-11 14:06:19 +000039#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#endif
43
Paul Bakker8adf13b2013-08-25 14:50:09 +020044#define DFL_FILENAME "file.pem"
45#define DFL_OUTPUT_FILENAME "file.der"
46
Rich Evans18b78c72015-02-11 14:06:19 +000047#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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000055int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020056{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
k-stachowiak776521a2019-05-23 09:46:47 +020058 mbedtls_exit( 0 );
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020059}
60#else
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010061
Manuel Pégourié-Gonnard3ef6a6d2018-12-10 14:31:45 +010062
Paul Bakker8adf13b2013-08-25 14:50:09 +020063/*
64 * global options
65 */
66struct options
67{
Paul Bakker8fc30b12013-11-25 13:29:43 +010068 const char *filename; /* filename of the input file */
69 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020070} opt;
71
72int 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
Paul Bakker8fc30b12013-11-25 13:29:43 +010079 s1 = (unsigned char *) strstr( (const char *) input, "-----BEGIN" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020080 if( s1 == NULL )
81 return( -1 );
82
Paul Bakker8fc30b12013-11-25 13:29:43 +010083 s2 = (unsigned char *) strstr( (const char *) input, "-----END" );
Paul Bakker8adf13b2013-08-25 14:50:09 +020084 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
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +010098 ret = mbedtls_base64_decode( NULL, 0, &len, (const unsigned char *) s1, s2 - s1 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 if( ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200100 return( ret );
101
102 if( len > *olen )
103 return( -1 );
104
Manuel Pégourié-Gonnardba561362015-06-02 16:30:35 +0100105 if( ( ret = mbedtls_base64_decode( output, len, &len, (const unsigned char *) s1,
Paul Bakker8adf13b2013-08-25 14:50:09 +0200106 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 */
119static 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 ||
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200138 ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
Paul Bakker8adf13b2013-08-25 14:50:09 +0200139 {
140 fclose( f );
141 return( -1 );
142 }
143
144 if( fread( *buf, 1, *n, f ) != *n )
145 {
146 fclose( f );
147 free( *buf );
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200148 *buf = NULL;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200149 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 */
162static 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
Paul Bakker8adf13b2013-08-25 14:50:09 +0200179int main( int argc, char *argv[] )
180{
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100181 int ret = 1;
182 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200183 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);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100187 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200188 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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 mbedtls_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200200 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
Paul Bakker8adf13b2013-08-25 14:50:09 +0200214 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 mbedtls_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200226 fflush( stdout );
227
228 ret = load_file( opt.filename, &pem_buffer, &pem_size );
229
230 if( ret != 0 )
231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200232#ifdef MBEDTLS_ERROR_C
233 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200236 goto exit;
237 }
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200240
241 /*
242 * 1.2. Convert from PEM to DER
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200245 fflush( stdout );
246
247 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
248 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#ifdef MBEDTLS_ERROR_C
250 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200253 goto exit;
254 }
255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200257
258 /*
259 * 1.3. Write the DER file
260 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261 mbedtls_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200262 fflush( stdout );
263
264 ret = write_file( opt.output_file, der_buffer, der_size );
265
266 if( ret != 0 )
267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#ifdef MBEDTLS_ERROR_C
269 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200271 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200272 goto exit;
273 }
274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200276
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100277 exit_code = MBEDTLS_EXIT_SUCCESS;
278
Paul Bakker8adf13b2013-08-25 14:50:09 +0200279exit:
280 free( pem_buffer );
281
282#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200284 fflush( stdout ); getchar();
285#endif
286
k-stachowiak776521a2019-05-23 09:46:47 +0200287 mbedtls_exit( exit_code );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200288}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */