blob: 995ee499d57e98c341ffedf1637f5ad91fe7304d [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 Kurek6bc7a382023-07-07 05:13:13 -0400153 mbedtls_asn1_named_data *ext_san_dirname = NULL;
Andrzej Kurek0624e462023-05-23 10:57:14 -0400154#if defined(MBEDTLS_X509_CRT_PARSE_C)
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400155 uint8_t ip[4] = { 0 };
Andrzej Kurek0624e462023-05-23 10:57:14 -0400156#endif
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200157 /*
158 * Set to sane values
159 */
160 mbedtls_x509write_csr_init(&req);
161 mbedtls_pk_init(&key);
162 mbedtls_ctr_drbg_init(&ctr_drbg);
163 memset(buf, 0, sizeof(buf));
164 mbedtls_entropy_init(&entropy);
165
Przemek Stekiel89c636e2023-04-14 09:26:39 +0200166#if defined(MBEDTLS_USE_PSA_CRYPTO)
167 psa_status_t status = psa_crypto_init();
168 if (status != PSA_SUCCESS) {
169 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
170 (int) status);
171 goto exit;
172 }
173#endif /* MBEDTLS_USE_PSA_CRYPTO */
174
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000175 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100176usage:
177 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000178 goto exit;
179 }
180
181 opt.filename = DFL_FILENAME;
Hanno Becker56e84632018-11-01 14:10:23 +0000182 opt.password = DFL_PASSWORD;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000183 opt.debug_level = DFL_DEBUG_LEVEL;
184 opt.output_file = DFL_OUTPUT_FILENAME;
185 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200186 opt.key_usage = DFL_KEY_USAGE;
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100187 opt.force_key_usage = DFL_FORCE_KEY_USAGE;
Paul Bakker57be6e22013-08-26 14:13:14 +0200188 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100189 opt.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
Hanno Beckerf7457332018-10-08 17:14:42 +0100190 opt.md_alg = DFL_MD_ALG;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100191 opt.san_list = NULL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000198 *q++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000200 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 } else if (strcmp(p, "password") == 0) {
Hanno Becker56e84632018-11-01 14:10:23 +0000202 opt.password = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000204 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 } else if (strcmp(p, "debug_level") == 0) {
206 opt.debug_level = atoi(q);
207 if (opt.debug_level < 0 || opt.debug_level > 65535) {
Hanno Beckerf7457332018-10-08 17:14:42 +0100208 goto usage;
209 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 } else if (strcmp(p, "subject_name") == 0) {
211 opt.subject_name = q;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100212 } else if (strcmp(p, "san") == 0) {
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400213 char *subtype_value;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100214 prev = NULL;
215
216 while (q != NULL) {
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400217 char *semicolon;
218 r = q;
219
220 /* Find the first non-escaped ; occurrence and remove escaped ones */
221 do {
222 if ((semicolon = strchr(r, ';')) != NULL) {
223 if (*(semicolon-1) != '\\') {
224 r = semicolon;
225 break;
226 }
227 /* Remove the escape character */
228 size_t size_left = strlen(semicolon);
229 memmove(semicolon-1, semicolon, size_left);
230 *(semicolon + size_left - 1) = '\0';
231 /* r will now point at the character after the semicolon */
232 r = semicolon;
233 }
234
235 } while (semicolon != NULL);
236
237 if (semicolon != NULL) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100238 *r++ = '\0';
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400239 } else {
240 r = NULL;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100241 }
242
243 cur = mbedtls_calloc(1, sizeof(mbedtls_x509_san_list));
244 if (cur == NULL) {
245 mbedtls_printf("Not enough memory for subjectAltName list\n");
246 goto usage;
247 }
248
249 cur->next = NULL;
250
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400251 if ((subtype_value = strchr(q, ':')) != NULL) {
252 *subtype_value++ = '\0';
Waleed Elmelegyac97af22023-10-12 15:46:06 +0100253 } else {
Waleed Elmelegyeade3fe2023-10-13 09:59:19 +0100254 mbedtls_printf(
David Horstmann9534dfd2023-10-17 14:59:31 +0100255 "Invalid argument for option SAN: Entry must be of the form TYPE:value\n");
Waleed Elmelegyac97af22023-10-12 15:46:06 +0100256 goto usage;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100257 }
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400258 if (strcmp(q, "RFC822") == 0) {
259 cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME;
260 } else if (strcmp(q, "URI") == 0) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100261 cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
262 } else if (strcmp(q, "DNS") == 0) {
263 cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
Przemek Stekiel3a925932023-02-14 13:05:24 +0100264 } else if (strcmp(q, "IP") == 0) {
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000265 size_t ip_addr_len = 0;
Przemek Stekiel3a925932023-02-14 13:05:24 +0100266 cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000267 ip_addr_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip);
268 if (ip_addr_len == 0) {
Andrzej Kurek0624e462023-05-23 10:57:14 -0400269 mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n",
270 subtype_value);
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400271 goto exit;
272 }
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400273 cur->node.san.unstructured_name.p = (unsigned char *) ip;
274 cur->node.san.unstructured_name.len = sizeof(ip);
275 } else if (strcmp(q, "DN") == 0) {
276 cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
277 if ((ret = mbedtls_x509_string_to_names(&ext_san_dirname,
278 subtype_value)) != 0) {
279 mbedtls_strerror(ret, buf, sizeof(buf));
280 mbedtls_printf(
281 " failed\n ! mbedtls_x509_string_to_names "
282 "returned -0x%04x - %s\n\n",
283 (unsigned int) -ret, buf);
284 goto exit;
285 }
286 cur->node.san.directory_name = *ext_san_dirname;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100287 } else {
288 mbedtls_free(cur);
289 goto usage;
290 }
291
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400292 if (cur->node.type == MBEDTLS_X509_SAN_RFC822_NAME ||
293 cur->node.type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER ||
294 cur->node.type == MBEDTLS_X509_SAN_DNS_NAME) {
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400295 q = subtype_value;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100296 cur->node.san.unstructured_name.p = (unsigned char *) q;
297 cur->node.san.unstructured_name.len = strlen(q);
Przemek Stekiel3a925932023-02-14 13:05:24 +0100298 }
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100299
300 if (prev == NULL) {
301 opt.san_list = cur;
302 } else {
303 prev->next = cur;
304 }
305
306 prev = cur;
307 q = r;
308 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 } else if (strcmp(p, "md") == 0) {
310 const mbedtls_md_info_t *md_info =
311 mbedtls_md_info_from_string(q);
312 if (md_info == NULL) {
313 mbedtls_printf("Invalid argument for option %s\n", p);
314 goto usage;
315 }
316 opt.md_alg = mbedtls_md_get_type(md_info);
317 } else if (strcmp(p, "key_usage") == 0) {
318 while (q != NULL) {
319 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker57be6e22013-08-26 14:13:14 +0200320 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200322
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100328 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100330 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100332 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 } else {
Paul Bakker57be6e22013-08-26 14:13:14 +0200338 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200340
341 q = r;
342 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 } else if (strcmp(p, "force_key_usage") == 0) {
344 switch (atoi(q)) {
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100345 case 0: opt.force_key_usage = 0; break;
346 case 1: opt.force_key_usage = 1; break;
347 default: goto usage;
348 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 } else if (strcmp(p, "ns_cert_type") == 0) {
350 while (q != NULL) {
351 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker57be6e22013-08-26 14:13:14 +0200352 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100358 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200360 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100364 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100366 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100368 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 } else {
Paul Bakker57be6e22013-08-26 14:13:14 +0200370 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200372
373 q = r;
374 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 } else if (strcmp(p, "force_ns_cert_type") == 0) {
376 switch (atoi(q)) {
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100377 case 0: opt.force_ns_cert_type = 0; break;
378 case 1: opt.force_ns_cert_type = 1; break;
379 default: goto usage;
380 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000382 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000384 }
385
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100386 /* Set the MD algorithm to use for the signature in the CSR */
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 mbedtls_x509write_csr_set_md_alg(&req, opt.md_alg);
Hanno Beckerf7457332018-10-08 17:14:42 +0100388
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100389 /* Set the Key Usage Extension flags in the CSR */
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 if (opt.key_usage || opt.force_key_usage == 1) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100391 ret = mbedtls_x509write_csr_set_key_usage(&req, opt.key_usage);
392
393 if (ret != 0) {
394 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_key_usage returned %d", ret);
395 goto exit;
396 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200398
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100399 /* Set the Cert Type flags in the CSR */
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (opt.ns_cert_type || opt.force_ns_cert_type == 1) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100401 ret = mbedtls_x509write_csr_set_ns_cert_type(&req, opt.ns_cert_type);
402
403 if (ret != 0) {
404 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_ns_cert_type returned %d", ret);
405 goto exit;
406 }
407 }
408
409 /* Set the SubjectAltName in the CSR */
410 if (opt.san_list != NULL) {
411 ret = mbedtls_x509write_csr_set_subject_alternative_name(&req, opt.san_list);
412
413 if (ret != 0) {
414 mbedtls_printf(
415 " failed\n ! mbedtls_x509write_csr_set_subject_alternative_name returned %d",
416 ret);
417 goto exit;
418 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200420
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000421 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200422 * 0. Seed the PRNG
423 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 mbedtls_printf(" . Seeding the random number generator...");
425 fflush(stdout);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200426
Gilles Peskine449bd832023-01-11 14:50:10 +0100427 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
428 (const unsigned char *) pers,
429 strlen(pers))) != 0) {
430 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d", ret);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200431 goto exit;
432 }
433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200435
436 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000437 * 1.0. Check the subject name for validity
438 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 mbedtls_printf(" . Checking subject name...");
440 fflush(stdout);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200441
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 if ((ret = mbedtls_x509write_csr_set_subject_name(&req, opt.subject_name)) != 0) {
443 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200444 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000445 }
446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200448
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000449 /*
450 * 1.1. Load the key
451 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 mbedtls_printf(" . Loading the private key ...");
453 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000454
Gilles Peskine449bd832023-01-11 14:50:10 +0100455 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, opt.password,
456 mbedtls_ctr_drbg_random, &ctr_drbg);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (ret != 0) {
459 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned %d", ret);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200460 goto exit;
461 }
462
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 mbedtls_x509write_csr_set_key(&req, &key);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_printf(" ok\n");
Paul Bakker8eabfc12013-08-25 10:18:25 +0200466
467 /*
468 * 1.2. Writing the request
469 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 mbedtls_printf(" . Writing the certificate request ...");
471 fflush(stdout);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200472
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if ((ret = write_certificate_request(&req, opt.output_file,
474 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
475 mbedtls_printf(" failed\n ! write_certificate_request %d", ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000476 goto exit;
477 }
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000480
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100481 exit_code = MBEDTLS_EXIT_SUCCESS;
482
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000483exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 mbedtls_strerror(ret, buf, sizeof(buf));
488 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200489#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200491#endif
492 }
493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 mbedtls_x509write_csr_free(&req);
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400495 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 mbedtls_pk_free(&key);
497 mbedtls_ctr_drbg_free(&ctr_drbg);
498 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200499#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200500 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200501#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000502
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100503 cur = opt.san_list;
504 while (cur != NULL) {
505 prev = cur;
506 cur = cur->next;
507 mbedtls_free(prev);
508 }
509
510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000512}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513#endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
Simon Butcher203a6932016-10-07 15:00:17 +0100514 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */