blob: ad2c6ac13985dc11f47618f4f5093b5dc684f601 [file] [log] [blame]
Paul Bakker8adf13b2013-08-25 14:50:09 +02001/*
2 * Convert PEM to DER
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
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 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker8adf13b2013-08-25 14:50:09 +020020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker8adf13b2013-08-25 14:50:09 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#define mbedtls_free free
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +020033#define mbedtls_calloc calloc
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000038#include "mbedtls/error.h"
39#include "mbedtls/base64.h"
Paul Bakker8adf13b2013-08-25 14:50:09 +020040
Rich Evans18b78c72015-02-11 14:06:19 +000041#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#endif
45
Paul Bakker8adf13b2013-08-25 14:50:09 +020046#define DFL_FILENAME "file.pem"
47#define DFL_OUTPUT_FILENAME "file.der"
48
Rich Evans18b78c72015-02-11 14:06:19 +000049#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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO)
Rich Evans85b05ec2015-02-12 11:37:29 +000057int main( void )
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020058{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n");
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020060 return( 0 );
61}
62#else
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{
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);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100186 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200187 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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_printf( USAGE );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200199 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
Paul Bakker8adf13b2013-08-25 14:50:09 +0200213 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( "\n . Loading the PEM file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200225 fflush( stdout );
226
227 ret = load_file( opt.filename, &pem_buffer, &pem_size );
228
229 if( ret != 0 )
230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#ifdef MBEDTLS_ERROR_C
232 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200233#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 mbedtls_printf( " failed\n ! load_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200235 goto exit;
236 }
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200239
240 /*
241 * 1.2. Convert from PEM to DER
242 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243 mbedtls_printf( " . Converting from PEM to DER ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200244 fflush( stdout );
245
246 if( ( ret = convert_pem_to_der( pem_buffer, pem_size, der_buffer, &der_size ) ) != 0 )
247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#ifdef MBEDTLS_ERROR_C
249 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 mbedtls_printf( " failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200252 goto exit;
253 }
254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200256
257 /*
258 * 1.3. Write the DER file
259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260 mbedtls_printf( " . Writing the DER file ..." );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200261 fflush( stdout );
262
263 ret = write_file( opt.output_file, der_buffer, der_size );
264
265 if( ret != 0 )
266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267#ifdef MBEDTLS_ERROR_C
268 mbedtls_strerror( ret, buf, 1024 );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200269#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270 mbedtls_printf( " failed\n ! write_file returned %d - %s\n\n", ret, buf );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200271 goto exit;
272 }
273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_printf( " ok\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200275
276exit:
277 free( pem_buffer );
278
279#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker8adf13b2013-08-25 14:50:09 +0200281 fflush( stdout ); getchar();
282#endif
283
284 return( ret );
285}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */