blob: 38b7af092515af3a5346310f6e0b968e06cc122c [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
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakkerbdb912d2012-02-13 23:11:30 +00006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Andrzej Kurek0af32482023-04-07 03:10:28 -040011/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040012#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Andrzej Kurek312b6df2023-07-10 08:45:30 -040014#if !defined(MBEDTLS_X509_CSR_WRITE_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010015 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
Simon Butcher203a6932016-10-07 15:00:17 +010016 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
Valerio Settie3511762024-01-22 16:28:23 +010017 !defined(MBEDTLS_PEM_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
18 !defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010019int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020020{
Gilles Peskine449bd832023-01-11 14:50:10 +010021 mbedtls_printf("MBEDTLS_X509_CSR_WRITE_C and/or MBEDTLS_FS_IO and/or "
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010022 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_MD_CAN_SHA256 and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010023 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
24 "not defined.\n");
25 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020026}
27#else
28
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/x509_csr.h"
30#include "mbedtls/entropy.h"
31#include "mbedtls/ctr_drbg.h"
32#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000033
Rich Evans18b78c72015-02-11 14:06:19 +000034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000037
38#define DFL_FILENAME "keyfile.key"
Hanno Becker56e84632018-11-01 14:10:23 +000039#define DFL_PASSWORD NULL
Rich Evans18b78c72015-02-11 14:06:19 +000040#define DFL_DEBUG_LEVEL 0
41#define DFL_OUTPUT_FILENAME "cert.req"
42#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
43#define DFL_KEY_USAGE 0
Andres Amaya Garcia7067f812018-09-26 10:51:16 +010044#define DFL_FORCE_KEY_USAGE 0
Rich Evans18b78c72015-02-11 14:06:19 +000045#define DFL_NS_CERT_TYPE 0
Andres Amaya Garcia7067f812018-09-26 10:51:16 +010046#define DFL_FORCE_NS_CERT_TYPE 0
Hanno Beckerf7457332018-10-08 17:14:42 +010047#define DFL_MD_ALG MBEDTLS_MD_SHA256
Rich Evans18b78c72015-02-11 14:06:19 +000048
49#define USAGE \
50 "\n usage: cert_req param=<>...\n" \
51 "\n acceptable parameters:\n" \
52 " filename=%%s default: keyfile.key\n" \
Hanno Becker56e84632018-11-01 14:10:23 +000053 " password=%%s default: NULL\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000054 " debug_level=%%d default: 0 (disabled)\n" \
55 " output_file=%%s default: cert.req\n" \
56 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Hannes Tschofenig6b108602022-12-28 18:38:53 +010057 " san=%%s default: (none)\n" \
Andrzej Kurek6bc7a382023-07-07 05:13:13 -040058 " Semicolon-separated-list of values:\n" \
59 " DNS:value\n" \
60 " URI:value\n" \
61 " RFC822:value\n" \
62 " IP:value (Only IPv4 is supported)\n" \
63 " DN:list of comma separated key=value pairs\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000064 " key_usage=%%s default: (empty)\n" \
65 " Comma-separated-list of values:\n" \
66 " digital_signature\n" \
67 " non_repudiation\n" \
68 " key_encipherment\n" \
69 " data_encipherment\n" \
70 " key_agreement\n" \
Jonathan Leroy81962c32015-10-10 21:42:29 +020071 " key_cert_sign\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000072 " crl_sign\n" \
Andres Amaya Garcia7067f812018-09-26 10:51:16 +010073 " force_key_usage=0/1 default: off\n" \
74 " Add KeyUsage even if it is empty\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000075 " ns_cert_type=%%s default: (empty)\n" \
76 " Comma-separated-list of values:\n" \
77 " ssl_client\n" \
78 " ssl_server\n" \
79 " email\n" \
80 " object_signing\n" \
81 " ssl_ca\n" \
82 " email_ca\n" \
83 " object_signing_ca\n" \
Andres Amaya Garcia7067f812018-09-26 10:51:16 +010084 " force_ns_cert_type=0/1 default: off\n" \
85 " Add NsCertType even if it is empty\n" \
Hanno Beckerf7457332018-10-08 17:14:42 +010086 " md=%%s default: SHA256\n" \
87 " possible values:\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +020088 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine384e2742020-08-21 19:51:13 +020089 " SHA224, SHA256, SHA384, SHA512\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000090 "\n"
91
Simon Butcher63cb97e2018-12-06 17:43:31 +000092
Paul Bakkerbdb912d2012-02-13 23:11:30 +000093/*
94 * global options
95 */
Gilles Peskine449bd832023-01-11 14:50:10 +010096struct options {
Michael Schuster82984bc2024-06-12 00:05:25 +020097 const char *filename; /* filename of the key file */
98 const char *password; /* password for the key file */
99 int debug_level; /* level of debugging */
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100100 const char *output_file; /* where to store the constructed key file */
Michael Schuster82984bc2024-06-12 00:05:25 +0200101 const char *subject_name; /* subject name for certificate request */
102 mbedtls_x509_san_list *san_list; /* subjectAltName for certificate request */
103 unsigned char key_usage; /* key usage flags */
104 int force_key_usage; /* Force adding the KeyUsage extension */
105 unsigned char ns_cert_type; /* NS cert type */
106 int force_ns_cert_type; /* Force adding NsCertType extension */
107 mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000108} opt;
109
Michael Schuster6fa32fd2024-06-01 21:15:02 +0200110static int write_certificate_request(mbedtls_x509write_csr *req, const char *output_file,
Michael Schuster82984bc2024-06-12 00:05:25 +0200111 int (*f_rng)(void *, unsigned char *, size_t),
112 void *p_rng)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000113{
Paul Bakker135f1e92013-08-26 16:54:13 +0200114 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115 FILE *f;
116 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +0200117 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 memset(output_buf, 0, 4096);
120 if ((ret = mbedtls_x509write_csr_pem(req, output_buf, 4096, f_rng, p_rng)) < 0) {
121 return ret;
Paul Bakker0c226102014-04-17 16:02:36 +0200122 }
Paul Bakker135f1e92013-08-26 16:54:13 +0200123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 len = strlen((char *) output_buf);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if ((f = fopen(output_file, "w")) == NULL) {
127 return -1;
128 }
129
130 if (fwrite(output_buf, 1, len, f) != len) {
131 fclose(f);
132 return -1;
133 }
134
135 fclose(f);
136
137 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000138}
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000141{
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100142 int ret = 1;
143 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 mbedtls_pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000145 char buf[1024];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100146 int i;
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400147 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_x509write_csr req;
149 mbedtls_entropy_context entropy;
150 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200151 const char *pers = "csr example app";
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100152 mbedtls_x509_san_list *cur, *prev;
Andrzej Kurek0624e462023-05-23 10:57:14 -0400153#if defined(MBEDTLS_X509_CRT_PARSE_C)
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400154 uint8_t ip[4] = { 0 };
Andrzej Kurek0624e462023-05-23 10:57:14 -0400155#endif
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200156 /*
157 * Set to sane values
158 */
159 mbedtls_x509write_csr_init(&req);
160 mbedtls_pk_init(&key);
161 mbedtls_ctr_drbg_init(&ctr_drbg);
162 memset(buf, 0, sizeof(buf));
163 mbedtls_entropy_init(&entropy);
164
Przemek Stekiel89c636e2023-04-14 09:26:39 +0200165#if defined(MBEDTLS_USE_PSA_CRYPTO)
166 psa_status_t status = psa_crypto_init();
167 if (status != PSA_SUCCESS) {
168 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
169 (int) status);
170 goto exit;
171 }
172#endif /* MBEDTLS_USE_PSA_CRYPTO */
173
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000174 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100175usage:
176 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000177 goto exit;
178 }
179
180 opt.filename = DFL_FILENAME;
Hanno Becker56e84632018-11-01 14:10:23 +0000181 opt.password = DFL_PASSWORD;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000182 opt.debug_level = DFL_DEBUG_LEVEL;
183 opt.output_file = DFL_OUTPUT_FILENAME;
184 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200185 opt.key_usage = DFL_KEY_USAGE;
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100186 opt.force_key_usage = DFL_FORCE_KEY_USAGE;
Paul Bakker57be6e22013-08-26 14:13:14 +0200187 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100188 opt.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
Hanno Beckerf7457332018-10-08 17:14:42 +0100189 opt.md_alg = DFL_MD_ALG;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100190 opt.san_list = NULL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000191
Gilles Peskine449bd832023-01-11 14:50:10 +0100192 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000193 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000195 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000197 *q++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000199 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 } else if (strcmp(p, "password") == 0) {
Hanno Becker56e84632018-11-01 14:10:23 +0000201 opt.password = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100202 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000203 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 } else if (strcmp(p, "debug_level") == 0) {
205 opt.debug_level = atoi(q);
206 if (opt.debug_level < 0 || opt.debug_level > 65535) {
Hanno Beckerf7457332018-10-08 17:14:42 +0100207 goto usage;
208 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100209 } else if (strcmp(p, "subject_name") == 0) {
210 opt.subject_name = q;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100211 } else if (strcmp(p, "san") == 0) {
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400212 char *subtype_value;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100213 prev = NULL;
214
215 while (q != NULL) {
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400216 char *semicolon;
217 r = q;
218
219 /* Find the first non-escaped ; occurrence and remove escaped ones */
220 do {
221 if ((semicolon = strchr(r, ';')) != NULL) {
222 if (*(semicolon-1) != '\\') {
223 r = semicolon;
224 break;
225 }
226 /* Remove the escape character */
227 size_t size_left = strlen(semicolon);
228 memmove(semicolon-1, semicolon, size_left);
229 *(semicolon + size_left - 1) = '\0';
230 /* r will now point at the character after the semicolon */
231 r = semicolon;
232 }
233
234 } while (semicolon != NULL);
235
236 if (semicolon != NULL) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100237 *r++ = '\0';
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400238 } else {
239 r = NULL;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100240 }
241
242 cur = mbedtls_calloc(1, sizeof(mbedtls_x509_san_list));
243 if (cur == NULL) {
244 mbedtls_printf("Not enough memory for subjectAltName list\n");
245 goto usage;
246 }
247
248 cur->next = NULL;
249
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400250 if ((subtype_value = strchr(q, ':')) != NULL) {
251 *subtype_value++ = '\0';
Waleed Elmelegyac97af22023-10-12 15:46:06 +0100252 } else {
Waleed Elmelegyeade3fe2023-10-13 09:59:19 +0100253 mbedtls_printf(
David Horstmann9534dfd2023-10-17 14:59:31 +0100254 "Invalid argument for option SAN: Entry must be of the form TYPE:value\n");
Waleed Elmelegyac97af22023-10-12 15:46:06 +0100255 goto usage;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100256 }
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400257 if (strcmp(q, "RFC822") == 0) {
258 cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME;
259 } else if (strcmp(q, "URI") == 0) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100260 cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
261 } else if (strcmp(q, "DNS") == 0) {
262 cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
Przemek Stekiel3a925932023-02-14 13:05:24 +0100263 } else if (strcmp(q, "IP") == 0) {
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000264 size_t ip_addr_len = 0;
Przemek Stekiel3a925932023-02-14 13:05:24 +0100265 cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000266 ip_addr_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip);
267 if (ip_addr_len == 0) {
Andrzej Kurek0624e462023-05-23 10:57:14 -0400268 mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n",
269 subtype_value);
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400270 goto exit;
271 }
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400272 cur->node.san.unstructured_name.p = (unsigned char *) ip;
273 cur->node.san.unstructured_name.len = sizeof(ip);
274 } else if (strcmp(q, "DN") == 0) {
275 cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
Manuel Pégourié-Gonnard4dd52b72025-05-05 17:09:14 +0200276 /* Work around an API mismatch between string_to_names() and
277 * mbedtls_x509_subject_alternative_name, which holds an
278 * actual mbedtls_x509_name while a pointer to one would be
Manuel Pégourié-Gonnard8a6fc082025-05-19 12:28:42 +0200279 * more convenient here. (Note mbedtls_x509_name and
280 * mbedtls_asn1_named_data are synonymous, again
281 * string_to_names() uses one while
282 * cur->node.san.directory_name is nominally the other.) */
Manuel Pégourié-Gonnard4dd52b72025-05-05 17:09:14 +0200283 mbedtls_asn1_named_data *tmp_san_dirname = NULL;
284 if ((ret = mbedtls_x509_string_to_names(&tmp_san_dirname,
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400285 subtype_value)) != 0) {
286 mbedtls_strerror(ret, buf, sizeof(buf));
287 mbedtls_printf(
288 " failed\n ! mbedtls_x509_string_to_names "
289 "returned -0x%04x - %s\n\n",
290 (unsigned int) -ret, buf);
291 goto exit;
292 }
Manuel Pégourié-Gonnard4dd52b72025-05-05 17:09:14 +0200293 cur->node.san.directory_name = *tmp_san_dirname;
294 mbedtls_free(tmp_san_dirname);
295 tmp_san_dirname = NULL;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100296 } else {
297 mbedtls_free(cur);
298 goto usage;
299 }
300
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400301 if (cur->node.type == MBEDTLS_X509_SAN_RFC822_NAME ||
302 cur->node.type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER ||
303 cur->node.type == MBEDTLS_X509_SAN_DNS_NAME) {
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400304 q = subtype_value;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100305 cur->node.san.unstructured_name.p = (unsigned char *) q;
306 cur->node.san.unstructured_name.len = strlen(q);
Przemek Stekiel3a925932023-02-14 13:05:24 +0100307 }
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100308
309 if (prev == NULL) {
310 opt.san_list = cur;
311 } else {
312 prev->next = cur;
313 }
314
315 prev = cur;
316 q = r;
317 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 } else if (strcmp(p, "md") == 0) {
319 const mbedtls_md_info_t *md_info =
320 mbedtls_md_info_from_string(q);
321 if (md_info == NULL) {
322 mbedtls_printf("Invalid argument for option %s\n", p);
323 goto usage;
324 }
325 opt.md_alg = mbedtls_md_get_type(md_info);
326 } else if (strcmp(p, "key_usage") == 0) {
327 while (q != NULL) {
328 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker57be6e22013-08-26 14:13:14 +0200329 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100330 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100337 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100339 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100341 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 } else {
Paul Bakker57be6e22013-08-26 14:13:14 +0200347 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200349
350 q = r;
351 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 } else if (strcmp(p, "force_key_usage") == 0) {
353 switch (atoi(q)) {
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100354 case 0: opt.force_key_usage = 0; break;
355 case 1: opt.force_key_usage = 1; break;
356 default: goto usage;
357 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 } else if (strcmp(p, "ns_cert_type") == 0) {
359 while (q != NULL) {
360 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker57be6e22013-08-26 14:13:14 +0200361 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200365 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100367 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100373 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100375 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100377 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 } else {
Paul Bakker57be6e22013-08-26 14:13:14 +0200379 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200381
382 q = r;
383 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 } else if (strcmp(p, "force_ns_cert_type") == 0) {
385 switch (atoi(q)) {
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100386 case 0: opt.force_ns_cert_type = 0; break;
387 case 1: opt.force_ns_cert_type = 1; break;
388 default: goto usage;
389 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000391 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000393 }
394
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100395 /* Set the MD algorithm to use for the signature in the CSR */
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 mbedtls_x509write_csr_set_md_alg(&req, opt.md_alg);
Hanno Beckerf7457332018-10-08 17:14:42 +0100397
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100398 /* Set the Key Usage Extension flags in the CSR */
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (opt.key_usage || opt.force_key_usage == 1) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100400 ret = mbedtls_x509write_csr_set_key_usage(&req, opt.key_usage);
401
402 if (ret != 0) {
403 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_key_usage returned %d", ret);
404 goto exit;
405 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200407
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100408 /* Set the Cert Type flags in the CSR */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 if (opt.ns_cert_type || opt.force_ns_cert_type == 1) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100410 ret = mbedtls_x509write_csr_set_ns_cert_type(&req, opt.ns_cert_type);
411
412 if (ret != 0) {
413 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_ns_cert_type returned %d", ret);
414 goto exit;
415 }
416 }
417
418 /* Set the SubjectAltName in the CSR */
419 if (opt.san_list != NULL) {
420 ret = mbedtls_x509write_csr_set_subject_alternative_name(&req, opt.san_list);
421
422 if (ret != 0) {
423 mbedtls_printf(
424 " failed\n ! mbedtls_x509write_csr_set_subject_alternative_name returned %d",
425 ret);
426 goto exit;
427 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200429
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000430 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200431 * 0. Seed the PRNG
432 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 mbedtls_printf(" . Seeding the random number generator...");
434 fflush(stdout);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
437 (const unsigned char *) pers,
438 strlen(pers))) != 0) {
439 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d", ret);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200440 goto exit;
441 }
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200444
445 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000446 * 1.0. Check the subject name for validity
447 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 mbedtls_printf(" . Checking subject name...");
449 fflush(stdout);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200450
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if ((ret = mbedtls_x509write_csr_set_subject_name(&req, opt.subject_name)) != 0) {
452 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200453 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000454 }
455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200457
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000458 /*
459 * 1.1. Load the key
460 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 mbedtls_printf(" . Loading the private key ...");
462 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, opt.password,
465 mbedtls_ctr_drbg_random, &ctr_drbg);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 if (ret != 0) {
468 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned %d", ret);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200469 goto exit;
470 }
471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 mbedtls_x509write_csr_set_key(&req, &key);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 mbedtls_printf(" ok\n");
Paul Bakker8eabfc12013-08-25 10:18:25 +0200475
476 /*
477 * 1.2. Writing the request
478 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 mbedtls_printf(" . Writing the certificate request ...");
480 fflush(stdout);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 if ((ret = write_certificate_request(&req, opt.output_file,
483 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
484 mbedtls_printf(" failed\n ! write_certificate_request %d", ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000485 goto exit;
486 }
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000489
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100490 exit_code = MBEDTLS_EXIT_SUCCESS;
491
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000492exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 mbedtls_strerror(ret, buf, sizeof(buf));
497 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200498#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200500#endif
501 }
502
Manuel Pégourié-Gonnard35f22202025-05-19 12:21:32 +0200503 mbedtls_x509write_csr_free(&req);
504 mbedtls_pk_free(&key);
505 mbedtls_ctr_drbg_free(&ctr_drbg);
506 mbedtls_entropy_free(&entropy);
507#if defined(MBEDTLS_USE_PSA_CRYPTO)
508 mbedtls_psa_crypto_free();
509#endif /* MBEDTLS_USE_PSA_CRYPTO */
510
Manuel Pégourié-Gonnard0803df22025-05-05 17:31:35 +0200511 cur = opt.san_list;
512 while (cur != NULL) {
513 mbedtls_x509_san_list *next = cur->next;
514 /* Note: mbedtls_x509_free_subject_alt_name() is not what we want here.
515 * It's the right thing for entries that were parsed from a certificate,
516 * where pointers are to the raw certificate, but here all the
517 * pointers were allocated while parsing from a user-provided string. */
518 if (cur->node.type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
Manuel Pégourié-Gonnard8ac3eb92025-05-21 11:17:39 +0200519 mbedtls_x509_name *dn = &cur->node.san.directory_name;
520 mbedtls_free(dn->oid.p);
521 mbedtls_free(dn->val.p);
522 mbedtls_asn1_free_named_data_list(&dn->next);
Manuel Pégourié-Gonnard0803df22025-05-05 17:31:35 +0200523 }
524 mbedtls_free(cur);
525 cur = next;
526 }
527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000529}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530#endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
Simon Butcher203a6932016-10-07 15:00:17 +0100531 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */