blob: 4a25f21b7a6eafc3a81e0c7b7ddbc96b8e6cf27a [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)
Gilles Peskine449bd832023-01-11 14:50:10 +010044int 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");
Gilles Peskine449bd832023-01-11 14:50:10 +010047 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 */
Gilles Peskine449bd832023-01-11 14:50:10 +010055struct options {
Paul Bakker8fc30b12013-11-25 13:29:43 +010056 const char *filename; /* filename of the input file */
57 const char *output_file; /* where to store the output */
Paul Bakker8adf13b2013-08-25 14:50:09 +020058} opt;
59
Gilles Peskine449bd832023-01-11 14:50:10 +010060int convert_pem_to_der(const unsigned char *input, size_t ilen,
61 unsigned char *output, size_t *olen)
Paul Bakker8adf13b2013-08-25 14:50:09 +020062{
63 int ret;
64 const unsigned char *s1, *s2, *end = input + ilen;
65 size_t len = 0;
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067 s1 = (unsigned char *) strstr((const char *) input, "-----BEGIN");
68 if (s1 == NULL) {
69 return -1;
70 }
Paul Bakker8adf13b2013-08-25 14:50:09 +020071
Gilles Peskine449bd832023-01-11 14:50:10 +010072 s2 = (unsigned char *) strstr((const char *) input, "-----END");
73 if (s2 == NULL) {
74 return -1;
75 }
Paul Bakker8adf13b2013-08-25 14:50:09 +020076
77 s1 += 10;
Gilles Peskine449bd832023-01-11 14:50:10 +010078 while (s1 < end && *s1 != '-') {
Paul Bakker8adf13b2013-08-25 14:50:09 +020079 s1++;
Gilles Peskine449bd832023-01-11 14:50:10 +010080 }
81 while (s1 < end && *s1 == '-') {
Paul Bakker8adf13b2013-08-25 14:50:09 +020082 s1++;
Gilles Peskine449bd832023-01-11 14:50:10 +010083 }
84 if (*s1 == '\r') {
85 s1++;
86 }
87 if (*s1 == '\n') {
88 s1++;
89 }
Paul Bakker8adf13b2013-08-25 14:50:09 +020090
Gilles Peskine449bd832023-01-11 14:50:10 +010091 if (s2 <= s1 || s2 > end) {
92 return -1;
93 }
Paul Bakker8adf13b2013-08-25 14:50:09 +020094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 ret = mbedtls_base64_decode(NULL, 0, &len, (const unsigned char *) s1, s2 - s1);
96 if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) {
97 return ret;
98 }
Paul Bakker8adf13b2013-08-25 14:50:09 +020099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 if (len > *olen) {
101 return -1;
102 }
Paul Bakker8adf13b2013-08-25 14:50:09 +0200103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 if ((ret = mbedtls_base64_decode(output, len, &len, (const unsigned char *) s1,
105 s2 - s1)) != 0) {
106 return ret;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200107 }
108
109 *olen = len;
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 return 0;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200112}
113
114/*
115 * Load all data from a file into a given buffer.
116 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100117static int load_file(const char *path, unsigned char **buf, size_t *n)
Paul Bakker8adf13b2013-08-25 14:50:09 +0200118{
119 FILE *f;
120 long size;
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if ((f = fopen(path, "rb")) == NULL) {
123 return -1;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200124 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100125
126 fseek(f, 0, SEEK_END);
127 if ((size = ftell(f)) == -1) {
128 fclose(f);
129 return -1;
130 }
131 fseek(f, 0, SEEK_SET);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200132
133 *n = (size_t) size;
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if (*n + 1 == 0 ||
136 (*buf = mbedtls_calloc(1, *n + 1)) == NULL) {
137 fclose(f);
138 return -1;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200139 }
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (fread(*buf, 1, *n, f) != *n) {
142 fclose(f);
143 free(*buf);
Alfred Klomp1d42b3e2014-07-14 22:09:21 +0200144 *buf = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100145 return -1;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200146 }
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 fclose(f);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200149
150 (*buf)[*n] = '\0';
151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 return 0;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200153}
154
155/*
156 * Write buffer to a file
157 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100158static int write_file(const char *path, unsigned char *buf, size_t n)
Paul Bakker8adf13b2013-08-25 14:50:09 +0200159{
160 FILE *f;
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 if ((f = fopen(path, "wb")) == NULL) {
163 return -1;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200164 }
165
Gilles Peskine449bd832023-01-11 14:50:10 +0100166 if (fwrite(buf, 1, n, f) != n) {
167 fclose(f);
168 return -1;
169 }
170
171 fclose(f);
172 return 0;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200173}
174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int main(int argc, char *argv[])
Paul Bakker8adf13b2013-08-25 14:50:09 +0200176{
Andres Amaya Garcia78dabe02018-04-29 22:08:41 +0100177 int ret = 1;
178 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200179 unsigned char *pem_buffer = NULL;
180 unsigned char der_buffer[4096];
181 char buf[1024];
182 size_t pem_size, der_size = sizeof(der_buffer);
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100183 int i;
Paul Bakker8adf13b2013-08-25 14:50:09 +0200184 char *p, *q;
185
186 /*
187 * Set to sane values
188 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 memset(buf, 0, sizeof(buf));
190 memset(der_buffer, 0, sizeof(der_buffer));
Paul Bakker8adf13b2013-08-25 14:50:09 +0200191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 if (argc == 0) {
193usage:
194 mbedtls_printf(USAGE);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200195 goto exit;
196 }
197
198 opt.filename = DFL_FILENAME;
199 opt.output_file = DFL_OUTPUT_FILENAME;
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 for (i = 1; i < argc; i++) {
Paul Bakker8adf13b2013-08-25 14:50:09 +0200202
203 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker8adf13b2013-08-25 14:50:09 +0200205 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 }
Paul Bakker8adf13b2013-08-25 14:50:09 +0200207 *q++ = '\0';
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 if (strcmp(p, "filename") == 0) {
Paul Bakker8adf13b2013-08-25 14:50:09 +0200210 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker8adf13b2013-08-25 14:50:09 +0200212 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100213 } else {
Paul Bakker8adf13b2013-08-25 14:50:09 +0200214 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 }
Paul Bakker8adf13b2013-08-25 14:50:09 +0200216 }
217
218 /*
219 * 1.1. Load the PEM file
220 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 mbedtls_printf("\n . Loading the PEM file ...");
222 fflush(stdout);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 ret = load_file(opt.filename, &pem_buffer, &pem_size);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_strerror(ret, buf, 1024);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200229#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 mbedtls_printf(" failed\n ! load_file returned %d - %s\n\n", ret, buf);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200231 goto exit;
232 }
233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_printf(" ok\n");
Paul Bakker8adf13b2013-08-25 14:50:09 +0200235
236 /*
237 * 1.2. Convert from PEM to DER
238 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 mbedtls_printf(" . Converting from PEM to DER ...");
240 fflush(stdout);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 if ((ret = convert_pem_to_der(pem_buffer, pem_size, der_buffer, &der_size)) != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200243#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 mbedtls_strerror(ret, buf, 1024);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200245#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 mbedtls_printf(" failed\n ! convert_pem_to_der %d - %s\n\n", ret, buf);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200247 goto exit;
248 }
249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 mbedtls_printf(" ok\n");
Paul Bakker8adf13b2013-08-25 14:50:09 +0200251
252 /*
253 * 1.3. Write the DER file
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_printf(" . Writing the DER file ...");
256 fflush(stdout);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 ret = write_file(opt.output_file, der_buffer, der_size);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200259
Gilles Peskine449bd832023-01-11 14:50:10 +0100260 if (ret != 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_strerror(ret, buf, 1024);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200263#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 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:
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 free(pem_buffer);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 mbedtls_exit(exit_code);
Paul Bakker8adf13b2013-08-25 14:50:09 +0200276}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */