| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 1 | /* | 
|  | 2 | *  Convert PEM to DER | 
|  | 3 | * | 
| Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | *  Copyright The Mbed TLS Contributors | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  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 Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 18 | */ | 
|  | 19 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 20 | #if !defined(MBEDTLS_CONFIG_FILE) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/config.h" | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 22 | #else | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 23 | #include MBEDTLS_CONFIG_FILE | 
| Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #endif | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 25 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 26 | #include "mbedtls/platform.h" | 
| Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_BASE64_C) && defined(MBEDTLS_FS_IO) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/error.h" | 
|  | 30 | #include "mbedtls/base64.h" | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 31 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> | 
|  | 33 | #include <stdlib.h> | 
|  | 34 | #include <string.h> | 
|  | 35 | #endif | 
|  | 36 |  | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 37 | #define DFL_FILENAME            "file.pem" | 
|  | 38 | #define DFL_OUTPUT_FILENAME     "file.der" | 
|  | 39 |  | 
| Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 40 | #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é-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_BASE64_C) || !defined(MBEDTLS_FS_IO) | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 48 | int main(void) | 
| Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 49 | { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | mbedtls_printf("MBEDTLS_BASE64_C and/or MBEDTLS_FS_IO not defined.\n"); | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 51 | mbedtls_exit(0); | 
| Manuel Pégourié-Gonnard | 7831b0c | 2013-09-20 12:29:56 +0200 | [diff] [blame] | 52 | } | 
|  | 53 | #else | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 54 |  | 
| Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 55 |  | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 56 | /* | 
|  | 57 | * global options | 
|  | 58 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 59 | struct options { | 
| Paul Bakker | 8fc30b1 | 2013-11-25 13:29:43 +0100 | [diff] [blame] | 60 | const char *filename;       /* filename of the input file             */ | 
|  | 61 | const char *output_file;    /* where to store the output              */ | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 62 | } opt; | 
|  | 63 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 64 | int convert_pem_to_der(const unsigned char *input, size_t ilen, | 
|  | 65 | unsigned char *output, size_t *olen) | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 66 | { | 
|  | 67 | int ret; | 
|  | 68 | const unsigned char *s1, *s2, *end = input + ilen; | 
|  | 69 | size_t len = 0; | 
|  | 70 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 71 | s1 = (unsigned char *) strstr((const char *) input, "-----BEGIN"); | 
|  | 72 | if (s1 == NULL) { | 
|  | 73 | return -1; | 
|  | 74 | } | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 75 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | s2 = (unsigned char *) strstr((const char *) input, "-----END"); | 
|  | 77 | if (s2 == NULL) { | 
|  | 78 | return -1; | 
|  | 79 | } | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 80 |  | 
|  | 81 | s1 += 10; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 82 | while (s1 < end && *s1 != '-') { | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 83 | s1++; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | } | 
|  | 85 | while (s1 < end && *s1 == '-') { | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 86 | s1++; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 87 | } | 
|  | 88 | if (*s1 == '\r') { | 
|  | 89 | s1++; | 
|  | 90 | } | 
|  | 91 | if (*s1 == '\n') { | 
|  | 92 | s1++; | 
|  | 93 | } | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 94 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | if (s2 <= s1 || s2 > end) { | 
|  | 96 | return -1; | 
|  | 97 | } | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 98 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 99 | ret = mbedtls_base64_decode(NULL, 0, &len, (const unsigned char *) s1, s2 - s1); | 
|  | 100 | if (ret == MBEDTLS_ERR_BASE64_INVALID_CHARACTER) { | 
|  | 101 | return ret; | 
|  | 102 | } | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 103 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | if (len > *olen) { | 
|  | 105 | return -1; | 
|  | 106 | } | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 107 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 108 | if ((ret = mbedtls_base64_decode(output, len, &len, (const unsigned char *) s1, | 
|  | 109 | s2 - s1)) != 0) { | 
|  | 110 | return ret; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
|  | 113 | *olen = len; | 
|  | 114 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | return 0; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 116 | } | 
|  | 117 |  | 
|  | 118 | /* | 
|  | 119 | * Load all data from a file into a given buffer. | 
|  | 120 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | static int load_file(const char *path, unsigned char **buf, size_t *n) | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 122 | { | 
|  | 123 | FILE *f; | 
|  | 124 | long size; | 
|  | 125 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | if ((f = fopen(path, "rb")) == NULL) { | 
|  | 127 | return -1; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 128 | } | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 129 |  | 
|  | 130 | fseek(f, 0, SEEK_END); | 
|  | 131 | if ((size = ftell(f)) == -1) { | 
|  | 132 | fclose(f); | 
|  | 133 | return -1; | 
|  | 134 | } | 
|  | 135 | fseek(f, 0, SEEK_SET); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 136 |  | 
|  | 137 | *n = (size_t) size; | 
|  | 138 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | if (*n + 1 == 0 || | 
|  | 140 | (*buf = mbedtls_calloc(1, *n + 1)) == NULL) { | 
|  | 141 | fclose(f); | 
|  | 142 | return -1; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 143 | } | 
|  | 144 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 145 | if (fread(*buf, 1, *n, f) != *n) { | 
|  | 146 | fclose(f); | 
|  | 147 | free(*buf); | 
| Alfred Klomp | 1d42b3e | 2014-07-14 22:09:21 +0200 | [diff] [blame] | 148 | *buf = NULL; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 149 | return -1; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 150 | } | 
|  | 151 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | fclose(f); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 153 |  | 
|  | 154 | (*buf)[*n] = '\0'; | 
|  | 155 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 156 | return 0; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 157 | } | 
|  | 158 |  | 
|  | 159 | /* | 
|  | 160 | * Write buffer to a file | 
|  | 161 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 162 | static int write_file(const char *path, unsigned char *buf, size_t n) | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 163 | { | 
|  | 164 | FILE *f; | 
|  | 165 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | if ((f = fopen(path, "wb")) == NULL) { | 
|  | 167 | return -1; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 168 | } | 
|  | 169 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 170 | if (fwrite(buf, 1, n, f) != n) { | 
|  | 171 | fclose(f); | 
|  | 172 | return -1; | 
|  | 173 | } | 
|  | 174 |  | 
|  | 175 | fclose(f); | 
|  | 176 | return 0; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 177 | } | 
|  | 178 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 179 | int main(int argc, char *argv[]) | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 180 | { | 
| Andres Amaya Garcia | 78dabe0 | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 181 | int ret = 1; | 
|  | 182 | int exit_code = MBEDTLS_EXIT_FAILURE; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 183 | 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 Bakker | c97f9f6 | 2013-11-30 15:13:02 +0100 | [diff] [blame] | 187 | int i; | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 188 | char *p, *q; | 
|  | 189 |  | 
|  | 190 | /* | 
|  | 191 | * Set to sane values | 
|  | 192 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 193 | memset(buf, 0, sizeof(buf)); | 
|  | 194 | memset(der_buffer, 0, sizeof(der_buffer)); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 195 |  | 
| Aditya Deshpande | 0504ac2 | 2023-01-30 15:58:50 +0000 | [diff] [blame] | 196 | if (argc < 2) { | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 197 | usage: | 
|  | 198 | mbedtls_printf(USAGE); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 199 | goto exit; | 
|  | 200 | } | 
|  | 201 |  | 
|  | 202 | opt.filename            = DFL_FILENAME; | 
|  | 203 | opt.output_file         = DFL_OUTPUT_FILENAME; | 
|  | 204 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 205 | for (i = 1; i < argc; i++) { | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 206 |  | 
|  | 207 | p = argv[i]; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 208 | if ((q = strchr(p, '=')) == NULL) { | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 209 | goto usage; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 210 | } | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 211 | *q++ = '\0'; | 
|  | 212 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 213 | if (strcmp(p, "filename") == 0) { | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 214 | opt.filename = q; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 215 | } else if (strcmp(p, "output_file") == 0) { | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 216 | opt.output_file = q; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 217 | } else { | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 218 | goto usage; | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 219 | } | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 220 | } | 
|  | 221 |  | 
|  | 222 | /* | 
|  | 223 | * 1.1. Load the PEM file | 
|  | 224 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 225 | mbedtls_printf("\n  . Loading the PEM file ..."); | 
|  | 226 | fflush(stdout); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 227 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 228 | ret = load_file(opt.filename, &pem_buffer, &pem_size); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 229 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 230 | if (ret != 0) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 231 | #ifdef MBEDTLS_ERROR_C | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 232 | mbedtls_strerror(ret, buf, 1024); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 233 | #endif | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 234 | mbedtls_printf(" failed\n  !  load_file returned %d - %s\n\n", ret, buf); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 235 | goto exit; | 
|  | 236 | } | 
|  | 237 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | mbedtls_printf(" ok\n"); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 239 |  | 
|  | 240 | /* | 
|  | 241 | * 1.2. Convert from PEM to DER | 
|  | 242 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 243 | mbedtls_printf("  . Converting from PEM to DER ..."); | 
|  | 244 | fflush(stdout); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 245 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | if ((ret = convert_pem_to_der(pem_buffer, pem_size, der_buffer, &der_size)) != 0) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | #ifdef MBEDTLS_ERROR_C | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 248 | mbedtls_strerror(ret, buf, 1024); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 249 | #endif | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 250 | mbedtls_printf(" failed\n  !  convert_pem_to_der %d - %s\n\n", ret, buf); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 251 | goto exit; | 
|  | 252 | } | 
|  | 253 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 254 | mbedtls_printf(" ok\n"); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 255 |  | 
|  | 256 | /* | 
|  | 257 | * 1.3. Write the DER file | 
|  | 258 | */ | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 259 | mbedtls_printf("  . Writing the DER file ..."); | 
|  | 260 | fflush(stdout); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 261 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 262 | ret = write_file(opt.output_file, der_buffer, der_size); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 263 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | if (ret != 0) { | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 265 | #ifdef MBEDTLS_ERROR_C | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 266 | mbedtls_strerror(ret, buf, 1024); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 267 | #endif | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 268 | mbedtls_printf(" failed\n  !  write_file returned %d - %s\n\n", ret, buf); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 269 | goto exit; | 
|  | 270 | } | 
|  | 271 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 272 | mbedtls_printf(" ok\n"); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 273 |  | 
| Andres Amaya Garcia | 78dabe0 | 2018-04-29 22:08:41 +0100 | [diff] [blame] | 274 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
|  | 275 |  | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 276 | exit: | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | free(pem_buffer); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 278 |  | 
|  | 279 | #if defined(_WIN32) | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 280 | mbedtls_printf("  + Press Enter to exit this program.\n"); | 
|  | 281 | fflush(stdout); getchar(); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 282 | #endif | 
|  | 283 |  | 
| Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 284 | mbedtls_exit(exit_code); | 
| Paul Bakker | 8adf13b | 2013-08-25 14:50:09 +0200 | [diff] [blame] | 285 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 286 | #endif /* MBEDTLS_BASE64_C && MBEDTLS_FS_IO */ |