blob: 0be9402dcd6d8ea709bafd13a12549cadc9742c9 [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
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 Bakker9397dcb2013-09-06 09:55:26 +020018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_PLATFORM_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020023# include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024#else
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020025# include <stdio.h>
26# include <stdlib.h>
27# define mbedtls_printf printf
28# define mbedtls_exit exit
29# define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
30# define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +010031#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000032
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020033#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
Simon Butcher203a6932016-10-07 15:00:17 +010034 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020035 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
36 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
Simon Butcher203a6932016-10-07 15:00:17 +010037 !defined(MBEDTLS_PEM_WRITE_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020038int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020039{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020040 mbedtls_printf(
41 "MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
42 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
43 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
44 "MBEDTLS_ERROR_C not defined.\n");
45 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020046}
47#else
48
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020049# include "mbedtls/x509_crt.h"
50# include "mbedtls/x509_csr.h"
51# include "mbedtls/entropy.h"
52# include "mbedtls/ctr_drbg.h"
53# include "mbedtls/md.h"
54# include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020055
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056# include <stdio.h>
57# include <stdlib.h>
58# include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000059
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020060# if defined(MBEDTLS_X509_CSR_PARSE_C)
61# define USAGE_CSR \
62 " request_file=%%s default: (empty)\n" \
63 " If request_file is specified, subject_key,\n" \
64 " subject_pwd and subject_name are ignored!\n"
65# else
66# define USAGE_CSR ""
67# endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000068
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020069# define DFL_ISSUER_CRT ""
70# define DFL_REQUEST_FILE ""
71# define DFL_SUBJECT_KEY "subject.key"
72# define DFL_ISSUER_KEY "ca.key"
73# define DFL_SUBJECT_PWD ""
74# define DFL_ISSUER_PWD ""
75# define DFL_OUTPUT_FILENAME "cert.crt"
76# define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
77# define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
78# define DFL_NOT_BEFORE "20010101000000"
79# define DFL_NOT_AFTER "20301231235959"
80# define DFL_SERIAL "1"
81# define DFL_SELFSIGN 0
82# define DFL_IS_CA 0
83# define DFL_MAX_PATHLEN -1
84# define DFL_KEY_USAGE 0
85# define DFL_NS_CERT_TYPE 0
86# define DFL_VERSION 3
87# define DFL_AUTH_IDENT 1
88# define DFL_SUBJ_IDENT 1
89# define DFL_CONSTRAINTS 1
90# define DFL_DIGEST MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020091
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020092# define USAGE \
93 "\n usage: cert_write param=<>...\n" \
94 "\n acceptable parameters:\n" USAGE_CSR \
95 " subject_key=%%s default: subject.key\n" \
96 " subject_pwd=%%s default: (empty)\n" \
97 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
98 "\n" \
99 " issuer_crt=%%s default: (empty)\n" \
100 " If issuer_crt is specified, issuer_name is\n" \
101 " ignored!\n" \
102 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
103 "\n" \
104 " selfsign=%%d default: 0 (false)\n" \
105 " If selfsign is enabled, issuer_name and\n" \
106 " issuer_key are required (issuer_crt and\n" \
107 " subject_* are ignored\n" \
108 " issuer_key=%%s default: ca.key\n" \
109 " issuer_pwd=%%s default: (empty)\n" \
110 " output_file=%%s default: cert.crt\n" \
111 " serial=%%s default: 1\n" \
112 " not_before=%%s default: 20010101000000\n" \
113 " not_after=%%s default: 20301231235959\n" \
114 " is_ca=%%d default: 0 (disabled)\n" \
115 " max_pathlen=%%d default: -1 (none)\n" \
116 " md=%%s default: SHA256\n" \
117 " Supported values (if enabled):\n" \
118 " MD5, RIPEMD160, SHA1,\n" \
119 " SHA224, SHA256, SHA384, SHA512\n" \
120 " version=%%d default: 3\n" \
121 " Possible values: 1, 2, 3\n" \
122 " subject_identifier=%%s default: 1\n" \
123 " Possible values: 0, 1\n" \
124 " (Considered for v3 only)\n" \
125 " authority_identifier=%%s default: 1\n" \
126 " Possible values: 0, 1\n" \
127 " (Considered for v3 only)\n" \
128 " basic_constraints=%%d default: 1\n" \
129 " Possible values: 0, 1\n" \
130 " (Considered for v3 only)\n" \
131 " key_usage=%%s default: (empty)\n" \
132 " Comma-separated-list of values:\n" \
133 " digital_signature\n" \
134 " non_repudiation\n" \
135 " key_encipherment\n" \
136 " data_encipherment\n" \
137 " key_agreement\n" \
138 " key_cert_sign\n" \
139 " crl_sign\n" \
140 " (Considered for v3 only)\n" \
141 " ns_cert_type=%%s default: (empty)\n" \
142 " Comma-separated-list of values:\n" \
143 " ssl_client\n" \
144 " ssl_server\n" \
145 " email\n" \
146 " object_signing\n" \
147 " ssl_ca\n" \
148 " email_ca\n" \
149 " object_signing_ca\n" \
150 "\n"
Simon Butcher63cb97e2018-12-06 17:43:31 +0000151
Paul Bakker9397dcb2013-09-06 09:55:26 +0200152/*
153 * global options
154 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200155struct options {
156 const char *issuer_crt; /* filename of the issuer certificate */
157 const char *request_file; /* filename of the certificate request */
158 const char *subject_key; /* filename of the subject key file */
159 const char *issuer_key; /* filename of the issuer key file */
160 const char *subject_pwd; /* password for the subject key file */
161 const char *issuer_pwd; /* password for the issuer key file */
162 const char *output_file; /* where to store the constructed CRT */
163 const char *subject_name; /* subject name for certificate */
164 const char *issuer_name; /* issuer name for certificate */
165 const char *not_before; /* validity period not before */
166 const char *not_after; /* validity period not after */
167 const char *serial; /* serial number string */
168 int selfsign; /* selfsign the certificate */
169 int is_ca; /* is a CA certificate */
170 int max_pathlen; /* maximum CA path length */
171 int authority_identifier; /* add authority identifier to CRT */
172 int subject_identifier; /* add subject identifier to CRT */
173 int basic_constraints; /* add basic constraints ext to CRT */
174 int version; /* CRT version */
175 mbedtls_md_type_t md; /* Hash used for signing */
176 unsigned char key_usage; /* key usage flags */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200177 unsigned char ns_cert_type; /* NS cert type */
178} opt;
179
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200180int write_certificate(mbedtls_x509write_cert *crt,
181 const char *output_file,
182 int (*f_rng)(void *, unsigned char *, size_t),
183 void *p_rng)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200184{
185 int ret;
186 FILE *f;
187 unsigned char output_buf[4096];
188 size_t len = 0;
189
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200190 memset(output_buf, 0, 4096);
191 if ((ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096, f_rng, p_rng)) <
192 0)
193 return ret;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200194
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200195 len = strlen((char *)output_buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200196
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200197 if ((f = fopen(output_file, "w")) == NULL)
198 return -1;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200199
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200200 if (fwrite(output_buf, 1, len, f) != len) {
201 fclose(f);
202 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200203 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200204
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200205 fclose(f);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200206
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200207 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200208}
209
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200210int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200211{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100212 int ret = 1;
213 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_x509_crt issuer_crt;
215 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
216 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200218 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200219 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100220 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200221 char *p, *q, *r;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200222# if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200223 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_x509_csr csr;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200225# endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 mbedtls_x509write_cert crt;
227 mbedtls_mpi serial;
228 mbedtls_entropy_context entropy;
229 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200230 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200231
232 /*
233 * Set to sane values
234 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200235 mbedtls_x509write_crt_init(&crt);
236 mbedtls_pk_init(&loaded_issuer_key);
237 mbedtls_pk_init(&loaded_subject_key);
238 mbedtls_mpi_init(&serial);
239 mbedtls_ctr_drbg_init(&ctr_drbg);
240 mbedtls_entropy_init(&entropy);
241# if defined(MBEDTLS_X509_CSR_PARSE_C)
242 mbedtls_x509_csr_init(&csr);
243# endif
244 mbedtls_x509_crt_init(&issuer_crt);
245 memset(buf, 0, 1024);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200246
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200247 if (argc == 0) {
248usage:
249 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200250 goto exit;
251 }
252
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200253 opt.issuer_crt = DFL_ISSUER_CRT;
254 opt.request_file = DFL_REQUEST_FILE;
255 opt.subject_key = DFL_SUBJECT_KEY;
256 opt.issuer_key = DFL_ISSUER_KEY;
257 opt.subject_pwd = DFL_SUBJECT_PWD;
258 opt.issuer_pwd = DFL_ISSUER_PWD;
259 opt.output_file = DFL_OUTPUT_FILENAME;
260 opt.subject_name = DFL_SUBJECT_NAME;
261 opt.issuer_name = DFL_ISSUER_NAME;
262 opt.not_before = DFL_NOT_BEFORE;
263 opt.not_after = DFL_NOT_AFTER;
264 opt.serial = DFL_SERIAL;
265 opt.selfsign = DFL_SELFSIGN;
266 opt.is_ca = DFL_IS_CA;
267 opt.max_pathlen = DFL_MAX_PATHLEN;
268 opt.key_usage = DFL_KEY_USAGE;
269 opt.ns_cert_type = DFL_NS_CERT_TYPE;
270 opt.version = DFL_VERSION - 1;
271 opt.md = DFL_DIGEST;
272 opt.subject_identifier = DFL_SUBJ_IDENT;
Hanno Becker6c13d372017-09-13 12:49:22 +0100273 opt.authority_identifier = DFL_AUTH_IDENT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200274 opt.basic_constraints = DFL_CONSTRAINTS;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200275
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200276 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200277 p = argv[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200278 if ((q = strchr(p, '=')) == NULL)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200279 goto usage;
280 *q++ = '\0';
281
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200282 if (strcmp(p, "request_file") == 0)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200283 opt.request_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200284 else if (strcmp(p, "subject_key") == 0)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200285 opt.subject_key = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200286 else if (strcmp(p, "issuer_key") == 0)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200287 opt.issuer_key = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200288 else if (strcmp(p, "subject_pwd") == 0)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200289 opt.subject_pwd = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290 else if (strcmp(p, "issuer_pwd") == 0)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200291 opt.issuer_pwd = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200292 else if (strcmp(p, "issuer_crt") == 0)
Paul Bakker1014e952013-09-09 13:59:42 +0200293 opt.issuer_crt = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200294 else if (strcmp(p, "output_file") == 0)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200295 opt.output_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200296 else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200297 opt.subject_name = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200298 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200299 opt.issuer_name = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200300 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200301 opt.not_before = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200302 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200303 opt.not_after = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200304 } else if (strcmp(p, "serial") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200305 opt.serial = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200306 } else if (strcmp(p, "authority_identifier") == 0) {
307 opt.authority_identifier = atoi(q);
308 if (opt.authority_identifier != 0 &&
309 opt.authority_identifier != 1) {
310 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100311 goto usage;
312 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200313 } else if (strcmp(p, "subject_identifier") == 0) {
314 opt.subject_identifier = atoi(q);
315 if (opt.subject_identifier != 0 && opt.subject_identifier != 1) {
316 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100317 goto usage;
318 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200319 } else if (strcmp(p, "basic_constraints") == 0) {
320 opt.basic_constraints = atoi(q);
321 if (opt.basic_constraints != 0 && opt.basic_constraints != 1) {
322 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100323 goto usage;
324 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200325 } else if (strcmp(p, "md") == 0) {
326 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_string(q);
327 if (md_info == NULL) {
328 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100329 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100330 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331 opt.md = mbedtls_md_get_type(md_info);
332 } else if (strcmp(p, "version") == 0) {
333 opt.version = atoi(q);
334 if (opt.version < 1 || opt.version > 3) {
335 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100336 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100337 }
Hanno Becker38eff432017-09-22 15:38:20 +0100338 opt.version--;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200339 } else if (strcmp(p, "selfsign") == 0) {
340 opt.selfsign = atoi(q);
341 if (opt.selfsign < 0 || opt.selfsign > 1) {
342 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200343 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100344 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200345 } else if (strcmp(p, "is_ca") == 0) {
346 opt.is_ca = atoi(q);
347 if (opt.is_ca < 0 || opt.is_ca > 1) {
348 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200349 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100350 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200351 } else if (strcmp(p, "max_pathlen") == 0) {
352 opt.max_pathlen = atoi(q);
353 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
354 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200355 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100356 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200357 } else if (strcmp(p, "key_usage") == 0) {
358 while (q != NULL) {
359 if ((r = strchr(q, ',')) != NULL)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200360 *r++ = '\0';
361
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200362 if (strcmp(q, "digital_signature") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200364 else if (strcmp(q, "non_repudiation") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200366 else if (strcmp(q, "key_encipherment") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100367 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200368 else if (strcmp(q, "data_encipherment") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100369 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200370 else if (strcmp(q, "key_agreement") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100371 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200372 else if (strcmp(q, "key_cert_sign") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200374 else if (strcmp(q, "crl_sign") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200376 else {
377 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200378 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100379 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200380
381 q = r;
382 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200383 } else if (strcmp(p, "ns_cert_type") == 0) {
384 while (q != NULL) {
385 if ((r = strchr(q, ',')) != NULL)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200386 *r++ = '\0';
387
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200388 if (strcmp(q, "ssl_client") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200390 else if (strcmp(q, "ssl_server") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100391 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200392 else if (strcmp(q, "email") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200394 else if (strcmp(q, "object_signing") == 0)
395 opt.ns_cert_type |=
396 MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
397 else if (strcmp(q, "ssl_ca") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100398 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200399 else if (strcmp(q, "email_ca") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100400 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200401 else if (strcmp(q, "object_signing_ca") == 0)
402 opt.ns_cert_type |=
403 MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
404 else {
405 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200406 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100407 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200408
409 q = r;
410 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200411 } else
Paul Bakker9397dcb2013-09-06 09:55:26 +0200412 goto usage;
413 }
414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200416
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200417 /*
418 * 0. Seed the PRNG
419 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200420 mbedtls_printf(" . Seeding the random number generator...");
421 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200422
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200423 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
424 (const unsigned char *)pers,
425 strlen(pers))) != 0) {
426 mbedtls_strerror(ret, buf, 1024);
427 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
428 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200429 goto exit;
430 }
431
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200432 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200433
Paul Bakker9397dcb2013-09-06 09:55:26 +0200434 // Parse serial to MPI
435 //
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200436 mbedtls_printf(" . Reading serial number...");
437 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200438
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200439 if ((ret = mbedtls_mpi_read_string(&serial, 10, opt.serial)) != 0) {
440 mbedtls_strerror(ret, buf, 1024);
441 mbedtls_printf(" failed\n ! mbedtls_mpi_read_string "
442 "returned -0x%04x - %s\n\n",
443 (unsigned int)-ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200444 goto exit;
445 }
446
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200447 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200448
Paul Bakker1014e952013-09-09 13:59:42 +0200449 // Parse issuer certificate if present
450 //
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200451 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200452 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200453 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200454 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200455 mbedtls_printf(" . Loading the issuer certificate ...");
456 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200457
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200458 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) !=
459 0) {
460 mbedtls_strerror(ret, buf, 1024);
461 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
462 "returned -0x%04x - %s\n\n",
463 (unsigned int)-ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200464 goto exit;
465 }
466
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200467 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
468 &issuer_crt.MBEDTLS_PRIVATE(subject));
469 if (ret < 0) {
470 mbedtls_strerror(ret, buf, 1024);
471 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
472 "returned -0x%04x - %s\n\n",
473 (unsigned int)-ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200474 goto exit;
475 }
476
Paul Bakkere2673fb2013-09-09 15:52:07 +0200477 opt.issuer_name = issuer_name;
478
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200479 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200480 }
481
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200482# if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200483 // Parse certificate request if present
484 //
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200485 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200486 /*
487 * 1.0.b. Load the CSR
488 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200489 mbedtls_printf(" . Loading the certificate request ...");
490 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200491
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200492 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
493 mbedtls_strerror(ret, buf, 1024);
494 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
495 "returned -0x%04x - %s\n\n",
496 (unsigned int)-ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200497 goto exit;
498 }
499
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200500 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
501 &csr.MBEDTLS_PRIVATE(subject));
502 if (ret < 0) {
503 mbedtls_strerror(ret, buf, 1024);
504 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
505 "returned -0x%04x - %s\n\n",
506 (unsigned int)-ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200507 goto exit;
508 }
509
510 opt.subject_name = subject_name;
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200511 subject_key = &csr.MBEDTLS_PRIVATE(pk);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200512
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200513 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200514 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200515# endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200516
517 /*
518 * 1.1. Load the keys
519 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200520 if (!opt.selfsign && !strlen(opt.request_file)) {
521 mbedtls_printf(" . Loading the subject key ...");
522 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200523
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200524 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
525 opt.subject_pwd, mbedtls_ctr_drbg_random,
526 &ctr_drbg);
527 if (ret != 0) {
528 mbedtls_strerror(ret, buf, 1024);
529 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
530 "returned -0x%04x - %s\n\n",
531 (unsigned int)-ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200532 goto exit;
533 }
Paul Bakker1014e952013-09-09 13:59:42 +0200534
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200535 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200536 }
537
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200538 mbedtls_printf(" . Loading the issuer key ...");
539 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200540
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200541 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
542 opt.issuer_pwd, mbedtls_ctr_drbg_random,
543 &ctr_drbg);
544 if (ret != 0) {
545 mbedtls_strerror(ret, buf, 1024);
546 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
547 "returned -x%02x - %s\n\n",
548 (unsigned int)-ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200549 goto exit;
550 }
551
552 // Check if key and issuer certificate match
553 //
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200554 if (strlen(opt.issuer_crt)) {
555 if (mbedtls_pk_check_pair(&issuer_crt.MBEDTLS_PRIVATE(pk), issuer_key,
556 mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
557 mbedtls_printf(" failed\n ! issuer_key does not match "
558 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200559 goto exit;
560 }
561 }
562
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200563 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200564
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200565 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100566 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200567 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200568 }
569
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200570 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
571 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200572
Paul Bakker9397dcb2013-09-06 09:55:26 +0200573 /*
574 * 1.0. Check the names for validity
575 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200576 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt,
577 opt.subject_name)) != 0) {
578 mbedtls_strerror(ret, buf, 1024);
579 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
580 "returned -0x%04x - %s\n\n",
581 (unsigned int)-ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200582 goto exit;
583 }
584
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200585 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) !=
586 0) {
587 mbedtls_strerror(ret, buf, 1024);
588 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
589 "returned -0x%04x - %s\n\n",
590 (unsigned int)-ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200591 goto exit;
592 }
593
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200594 mbedtls_printf(" . Setting certificate values ...");
595 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200596
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200597 mbedtls_x509write_crt_set_version(&crt, opt.version);
598 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100599
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200600 ret = mbedtls_x509write_crt_set_serial(&crt, &serial);
601 if (ret != 0) {
602 mbedtls_strerror(ret, buf, 1024);
603 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial "
604 "returned -0x%04x - %s\n\n",
605 (unsigned int)-ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200606 goto exit;
607 }
608
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200609 ret =
610 mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
611 if (ret != 0) {
612 mbedtls_strerror(ret, buf, 1024);
613 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
614 "returned -0x%04x - %s\n\n",
615 (unsigned int)-ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200616 goto exit;
617 }
618
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200619 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200620
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200621 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
622 opt.basic_constraints != 0) {
623 mbedtls_printf(" . Adding the Basic Constraints extension ...");
624 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200625
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200626 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
627 opt.max_pathlen);
628 if (ret != 0) {
629 mbedtls_strerror(ret, buf, 1024);
630 mbedtls_printf(" failed\n ! x509write_crt_set_basic_contraints "
631 "returned -0x%04x - %s\n\n",
632 (unsigned int)-ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100633 goto exit;
634 }
635
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200636 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100637 }
Paul Bakker15162a02013-09-06 19:27:21 +0200638
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200639# if defined(MBEDTLS_SHA1_C)
640 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
641 opt.subject_identifier != 0) {
642 mbedtls_printf(" . Adding the Subject Key Identifier ...");
643 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100644
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200645 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
646 if (ret != 0) {
647 mbedtls_strerror(ret, buf, 1024);
648 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
649 "_key_identifier returned -0x%04x - %s\n\n",
650 (unsigned int)-ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100651 goto exit;
652 }
653
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200654 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200655 }
656
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200657 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
658 opt.authority_identifier != 0) {
659 mbedtls_printf(" . Adding the Authority Key Identifier ...");
660 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200661
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200662 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
663 if (ret != 0) {
664 mbedtls_strerror(ret, buf, 1024);
665 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
666 "key_identifier returned -0x%04x - %s\n\n",
667 (unsigned int)-ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100668 goto exit;
669 }
670
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200671 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100672 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200673# endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200674
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200675 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && opt.key_usage != 0) {
676 mbedtls_printf(" . Adding the Key Usage extension ...");
677 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200678
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200679 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
680 if (ret != 0) {
681 mbedtls_strerror(ret, buf, 1024);
682 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
683 "returned -0x%04x - %s\n\n",
684 (unsigned int)-ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200685 goto exit;
686 }
687
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200688 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200689 }
690
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200691 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 && opt.ns_cert_type != 0) {
692 mbedtls_printf(" . Adding the NS Cert Type extension ...");
693 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200694
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200695 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
696 if (ret != 0) {
697 mbedtls_strerror(ret, buf, 1024);
698 mbedtls_printf(
699 " failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
700 "returned -0x%04x - %s\n\n",
701 (unsigned int)-ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200702 goto exit;
703 }
704
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200705 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200706 }
707
Paul Bakker9397dcb2013-09-06 09:55:26 +0200708 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100709 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200710 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200711 mbedtls_printf(" . Writing the certificate...");
712 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200713
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200714 if ((ret = write_certificate(&crt, opt.output_file, mbedtls_ctr_drbg_random,
715 &ctr_drbg)) != 0) {
716 mbedtls_strerror(ret, buf, 1024);
717 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
718 (unsigned int)-ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200719 goto exit;
720 }
721
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200722 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200723
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100724 exit_code = MBEDTLS_EXIT_SUCCESS;
725
Paul Bakker9397dcb2013-09-06 09:55:26 +0200726exit:
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200727# if defined(MBEDTLS_X509_CSR_PARSE_C)
728 mbedtls_x509_csr_free(&csr);
729# endif /* MBEDTLS_X509_CSR_PARSE_C */
730 mbedtls_x509_crt_free(&issuer_crt);
731 mbedtls_x509write_crt_free(&crt);
732 mbedtls_pk_free(&loaded_subject_key);
733 mbedtls_pk_free(&loaded_issuer_key);
734 mbedtls_mpi_free(&serial);
735 mbedtls_ctr_drbg_free(&ctr_drbg);
736 mbedtls_entropy_free(&entropy);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200737
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200738# if defined(_WIN32)
739 mbedtls_printf(" + Press Enter to exit this program.\n");
740 fflush(stdout);
741 getchar();
742# endif
Paul Bakker9397dcb2013-09-06 09:55:26 +0200743
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200744 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200745}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200746#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C && \
747 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && \
Simon Butcher203a6932016-10-07 15:00:17 +0100748 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */