blob: 29962b8d379bb0fba86825e81e17ba2e2ff54c5e [file] [log] [blame]
Paul Bakkerbdb912d2012-02-13 23:11:30 +00001/*
2 * Certificate request generation
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 Bakkerbdb912d2012-02-13 23:11:30 +000018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000021
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 Garciaaacd9282018-04-29 21:36:13 +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_CSR_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
34 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_SHA256_C) || \
35 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
Simon Butcher203a6932016-10-07 15:00:17 +010036 !defined(MBEDTLS_PEM_WRITE_C)
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020037int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020038{
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020039 mbedtls_printf("MBEDTLS_X509_CSR_WRITE_C and/or MBEDTLS_FS_IO and/or "
40 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_SHA256_C and/or "
41 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
42 "not defined.\n");
43 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020044}
45#else
46
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020047# include "mbedtls/x509_csr.h"
48# include "mbedtls/entropy.h"
49# include "mbedtls/ctr_drbg.h"
50# include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000051
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020052# include <stdio.h>
53# include <stdlib.h>
54# include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000055
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020056# define DFL_FILENAME "keyfile.key"
57# define DFL_PASSWORD NULL
58# define DFL_DEBUG_LEVEL 0
59# define DFL_OUTPUT_FILENAME "cert.req"
60# define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
61# define DFL_KEY_USAGE 0
62# define DFL_FORCE_KEY_USAGE 0
63# define DFL_NS_CERT_TYPE 0
64# define DFL_FORCE_NS_CERT_TYPE 0
65# define DFL_MD_ALG MBEDTLS_MD_SHA256
Rich Evans18b78c72015-02-11 14:06:19 +000066
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020067# define USAGE \
68 "\n usage: cert_req param=<>...\n" \
69 "\n acceptable parameters:\n" \
70 " filename=%%s default: keyfile.key\n" \
71 " password=%%s default: NULL\n" \
72 " debug_level=%%d default: 0 (disabled)\n" \
73 " output_file=%%s default: cert.req\n" \
74 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
75 " key_usage=%%s default: (empty)\n" \
76 " Comma-separated-list of values:\n" \
77 " digital_signature\n" \
78 " non_repudiation\n" \
79 " key_encipherment\n" \
80 " data_encipherment\n" \
81 " key_agreement\n" \
82 " key_cert_sign\n" \
83 " crl_sign\n" \
84 " force_key_usage=0/1 default: off\n" \
85 " Add KeyUsage even if it is empty\n" \
86 " ns_cert_type=%%s default: (empty)\n" \
87 " Comma-separated-list of values:\n" \
88 " ssl_client\n" \
89 " ssl_server\n" \
90 " email\n" \
91 " object_signing\n" \
92 " ssl_ca\n" \
93 " email_ca\n" \
94 " object_signing_ca\n" \
95 " force_ns_cert_type=0/1 default: off\n" \
96 " Add NsCertType even if it is empty\n" \
97 " md=%%s default: SHA256\n" \
98 " possible values:\n" \
99 " MD5, RIPEMD160, SHA1,\n" \
100 " SHA224, SHA256, SHA384, SHA512\n" \
101 "\n"
Simon Butcher63cb97e2018-12-06 17:43:31 +0000102
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000103/*
104 * global options
105 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200106struct options {
107 const char *filename; /* filename of the key file */
108 const char *password; /* password for the key file */
109 int debug_level; /* level of debugging */
110 const char *output_file; /* where to store the constructed key file */
111 const char *subject_name; /* subject name for certificate request */
112 unsigned char key_usage; /* key usage flags */
113 int force_key_usage; /* Force adding the KeyUsage extension */
Paul Bakker57be6e22013-08-26 14:13:14 +0200114 unsigned char ns_cert_type; /* NS cert type */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200115 int force_ns_cert_type; /* Force adding NsCertType extension */
116 mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000117} opt;
118
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200119int write_certificate_request(mbedtls_x509write_csr *req,
120 const char *output_file,
121 int (*f_rng)(void *, unsigned char *, size_t),
122 void *p_rng)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000123{
Paul Bakker135f1e92013-08-26 16:54:13 +0200124 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000125 FILE *f;
126 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +0200127 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000128
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200129 memset(output_buf, 0, 4096);
130 if ((ret = mbedtls_x509write_csr_pem(req, output_buf, 4096, f_rng, p_rng)) <
131 0)
132 return ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000133
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200134 len = strlen((char *)output_buf);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000135
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200136 if ((f = fopen(output_file, "w")) == NULL)
137 return -1;
Paul Bakker8eabfc12013-08-25 10:18:25 +0200138
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200139 if (fwrite(output_buf, 1, len, f) != len) {
140 fclose(f);
141 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200142 }
Paul Bakker135f1e92013-08-26 16:54:13 +0200143
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200144 fclose(f);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200145
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200146 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147}
148
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200149int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000150{
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100151 int ret = 1;
152 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000154 char buf[1024];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100155 int i;
Paul Bakker57be6e22013-08-26 14:13:14 +0200156 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_x509write_csr req;
158 mbedtls_entropy_context entropy;
159 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200160 const char *pers = "csr example app";
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000161
162 /*
163 * Set to sane values
164 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200165 mbedtls_x509write_csr_init(&req);
166 mbedtls_pk_init(&key);
167 mbedtls_ctr_drbg_init(&ctr_drbg);
168 memset(buf, 0, sizeof(buf));
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000169
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200170 if (argc == 0) {
171usage:
172 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000173 goto exit;
174 }
175
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200176 opt.filename = DFL_FILENAME;
177 opt.password = DFL_PASSWORD;
178 opt.debug_level = DFL_DEBUG_LEVEL;
179 opt.output_file = DFL_OUTPUT_FILENAME;
180 opt.subject_name = DFL_SUBJECT_NAME;
181 opt.key_usage = DFL_KEY_USAGE;
182 opt.force_key_usage = DFL_FORCE_KEY_USAGE;
183 opt.ns_cert_type = DFL_NS_CERT_TYPE;
184 opt.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
185 opt.md_alg = DFL_MD_ALG;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000186
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200187 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000188 p = argv[i];
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200189 if ((q = strchr(p, '=')) == NULL)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000190 goto usage;
191 *q++ = '\0';
192
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200193 if (strcmp(p, "filename") == 0)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194 opt.filename = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200195 else if (strcmp(p, "password") == 0)
Hanno Becker56e84632018-11-01 14:10:23 +0000196 opt.password = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200197 else if (strcmp(p, "output_file") == 0)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000198 opt.output_file = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200199 else if (strcmp(p, "debug_level") == 0) {
200 opt.debug_level = atoi(q);
201 if (opt.debug_level < 0 || opt.debug_level > 65535)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000202 goto usage;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204 opt.subject_name = q;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200205 } else if (strcmp(p, "md") == 0) {
206 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_string(q);
207 if (md_info == NULL) {
208 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Beckerf7457332018-10-08 17:14:42 +0100209 goto usage;
210 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200211 opt.md_alg = mbedtls_md_get_type(md_info);
212 } else if (strcmp(p, "key_usage") == 0) {
213 while (q != NULL) {
214 if ((r = strchr(q, ',')) != NULL)
Paul Bakker57be6e22013-08-26 14:13:14 +0200215 *r++ = '\0';
216
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200217 if (strcmp(q, "digital_signature") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200219 else if (strcmp(q, "non_repudiation") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200221 else if (strcmp(q, "key_encipherment") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100222 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200223 else if (strcmp(q, "data_encipherment") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100224 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200225 else if (strcmp(q, "key_agreement") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100226 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200227 else if (strcmp(q, "key_cert_sign") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200229 else if (strcmp(q, "crl_sign") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Paul Bakker57be6e22013-08-26 14:13:14 +0200231 else
232 goto usage;
233
234 q = r;
235 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200236 } else if (strcmp(p, "force_key_usage") == 0) {
237 switch (atoi(q)) {
238 case 0:
239 opt.force_key_usage = 0;
240 break;
241 case 1:
242 opt.force_key_usage = 1;
243 break;
244 default:
245 goto usage;
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100246 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200247 } else if (strcmp(p, "ns_cert_type") == 0) {
248 while (q != NULL) {
249 if ((r = strchr(q, ',')) != NULL)
Paul Bakker57be6e22013-08-26 14:13:14 +0200250 *r++ = '\0';
251
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200252 if (strcmp(q, "ssl_client") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200254 else if (strcmp(q, "ssl_server") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100255 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200256 else if (strcmp(q, "email") == 0)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200258 else if (strcmp(q, "object_signing") == 0)
259 opt.ns_cert_type |=
260 MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
261 else if (strcmp(q, "ssl_ca") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100262 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200263 else if (strcmp(q, "email_ca") == 0)
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100264 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200265 else if (strcmp(q, "object_signing_ca") == 0)
266 opt.ns_cert_type |=
267 MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Paul Bakker57be6e22013-08-26 14:13:14 +0200268 else
269 goto usage;
270
271 q = r;
272 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200273 } else if (strcmp(p, "force_ns_cert_type") == 0) {
274 switch (atoi(q)) {
275 case 0:
276 opt.force_ns_cert_type = 0;
277 break;
278 case 1:
279 opt.force_ns_cert_type = 1;
280 break;
281 default:
282 goto usage;
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100283 }
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200284 } else
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000285 goto usage;
286 }
287
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200288 mbedtls_x509write_csr_set_md_alg(&req, opt.md_alg);
Hanno Beckerf7457332018-10-08 17:14:42 +0100289
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200290 if (opt.key_usage || opt.force_key_usage == 1)
291 mbedtls_x509write_csr_set_key_usage(&req, opt.key_usage);
Paul Bakker57be6e22013-08-26 14:13:14 +0200292
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200293 if (opt.ns_cert_type || opt.force_ns_cert_type == 1)
294 mbedtls_x509write_csr_set_ns_cert_type(&req, opt.ns_cert_type);
Paul Bakker57be6e22013-08-26 14:13:14 +0200295
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000296 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200297 * 0. Seed the PRNG
298 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200299 mbedtls_printf(" . Seeding the random number generator...");
300 fflush(stdout);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200301
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200302 mbedtls_entropy_init(&entropy);
303 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
304 (const unsigned char *)pers,
305 strlen(pers))) != 0) {
306 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d", ret);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200307 goto exit;
308 }
309
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200310 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200311
312 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000313 * 1.0. Check the subject name for validity
314 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200315 mbedtls_printf(" . Checking subject name...");
316 fflush(stdout);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200317
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200318 if ((ret = mbedtls_x509write_csr_set_subject_name(&req,
319 opt.subject_name)) != 0) {
320 mbedtls_printf(
321 " failed\n ! mbedtls_x509write_csr_set_subject_name returned %d",
322 ret);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200323 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000324 }
325
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200326 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200327
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000328 /*
329 * 1.1. Load the key
330 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200331 mbedtls_printf(" . Loading the private key ...");
332 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000333
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200334 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, opt.password,
335 mbedtls_ctr_drbg_random, &ctr_drbg);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000336
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200337 if (ret != 0) {
338 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned %d",
339 ret);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200340 goto exit;
341 }
342
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200343 mbedtls_x509write_csr_set_key(&req, &key);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200344
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200345 mbedtls_printf(" ok\n");
Paul Bakker8eabfc12013-08-25 10:18:25 +0200346
347 /*
348 * 1.2. Writing the request
349 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200350 mbedtls_printf(" . Writing the certificate request ...");
351 fflush(stdout);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200352
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200353 if ((ret = write_certificate_request(
354 &req, opt.output_file, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
355 mbedtls_printf(" failed\n ! write_certifcate_request %d", ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000356 goto exit;
357 }
358
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200359 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000360
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100361 exit_code = MBEDTLS_EXIT_SUCCESS;
362
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000363exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200364
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200365 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
366# ifdef MBEDTLS_ERROR_C
367 mbedtls_strerror(ret, buf, sizeof(buf));
368 mbedtls_printf(" - %s\n", buf);
369# else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370 mbedtls_printf("\n");
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200371# endif
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200372 }
373
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200374 mbedtls_x509write_csr_free(&req);
375 mbedtls_pk_free(&key);
376 mbedtls_ctr_drbg_free(&ctr_drbg);
377 mbedtls_entropy_free(&entropy);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000378
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200379# if defined(_WIN32)
380 mbedtls_printf(" + Press Enter to exit this program.\n");
381 fflush(stdout);
382 getchar();
383# endif
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000384
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200385 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000386}
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200387#endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO && \
Simon Butcher203a6932016-10-07 15:00:17 +0100388 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */