blob: 1be335c0ad320bee972fc54120b52c557687beda [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
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Andrzej Kurek0af32482023-04-07 03:10:28 -040013/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040014#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Andrzej Kurek312b6df2023-07-10 08:45:30 -040016#if !defined(MBEDTLS_X509_CSR_WRITE_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
Elena Uziunaite0916cd72024-05-23 17:01:07 +010017 !defined(MBEDTLS_PK_PARSE_C) || !defined(PSA_WANT_ALG_SHA_256) || \
Simon Butcher203a6932016-10-07 15:00:17 +010018 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
Valerio Settie3511762024-01-22 16:28:23 +010019 !defined(MBEDTLS_PEM_WRITE_C) || !defined(MBEDTLS_FS_IO) || \
20 !defined(MBEDTLS_MD_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010021int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020022{
Gilles Peskine449bd832023-01-11 14:50:10 +010023 mbedtls_printf("MBEDTLS_X509_CSR_WRITE_C and/or MBEDTLS_FS_IO and/or "
Elena Uziunaite0916cd72024-05-23 17:01:07 +010024 "MBEDTLS_PK_PARSE_C and/or PSA_WANT_ALG_SHA_256 and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010025 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
26 "not defined.\n");
27 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020028}
29#else
30
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/x509_csr.h"
32#include "mbedtls/entropy.h"
33#include "mbedtls/ctr_drbg.h"
34#include "mbedtls/error.h"
Paul Bakkerbdb912d2012-02-13 23:11:30 +000035
Rich Evans18b78c72015-02-11 14:06:19 +000036#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000039
40#define DFL_FILENAME "keyfile.key"
Hanno Becker56e84632018-11-01 14:10:23 +000041#define DFL_PASSWORD NULL
Rich Evans18b78c72015-02-11 14:06:19 +000042#define DFL_DEBUG_LEVEL 0
43#define DFL_OUTPUT_FILENAME "cert.req"
44#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
45#define DFL_KEY_USAGE 0
Andres Amaya Garcia7067f812018-09-26 10:51:16 +010046#define DFL_FORCE_KEY_USAGE 0
Rich Evans18b78c72015-02-11 14:06:19 +000047#define DFL_NS_CERT_TYPE 0
Andres Amaya Garcia7067f812018-09-26 10:51:16 +010048#define DFL_FORCE_NS_CERT_TYPE 0
Hanno Beckerf7457332018-10-08 17:14:42 +010049#define DFL_MD_ALG MBEDTLS_MD_SHA256
Rich Evans18b78c72015-02-11 14:06:19 +000050
51#define USAGE \
52 "\n usage: cert_req param=<>...\n" \
53 "\n acceptable parameters:\n" \
54 " filename=%%s default: keyfile.key\n" \
Hanno Becker56e84632018-11-01 14:10:23 +000055 " password=%%s default: NULL\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000056 " debug_level=%%d default: 0 (disabled)\n" \
57 " output_file=%%s default: cert.req\n" \
58 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Hannes Tschofenig6b108602022-12-28 18:38:53 +010059 " san=%%s default: (none)\n" \
Andrzej Kurek6bc7a382023-07-07 05:13:13 -040060 " Semicolon-separated-list of values:\n" \
61 " DNS:value\n" \
62 " URI:value\n" \
63 " RFC822:value\n" \
64 " IP:value (Only IPv4 is supported)\n" \
65 " DN:list of comma separated key=value pairs\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000066 " key_usage=%%s default: (empty)\n" \
67 " Comma-separated-list of values:\n" \
68 " digital_signature\n" \
69 " non_repudiation\n" \
70 " key_encipherment\n" \
71 " data_encipherment\n" \
72 " key_agreement\n" \
Jonathan Leroy81962c32015-10-10 21:42:29 +020073 " key_cert_sign\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000074 " crl_sign\n" \
Andres Amaya Garcia7067f812018-09-26 10:51:16 +010075 " force_key_usage=0/1 default: off\n" \
76 " Add KeyUsage even if it is empty\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000077 " ns_cert_type=%%s default: (empty)\n" \
78 " Comma-separated-list of values:\n" \
79 " ssl_client\n" \
80 " ssl_server\n" \
81 " email\n" \
82 " object_signing\n" \
83 " ssl_ca\n" \
84 " email_ca\n" \
85 " object_signing_ca\n" \
Andres Amaya Garcia7067f812018-09-26 10:51:16 +010086 " force_ns_cert_type=0/1 default: off\n" \
87 " Add NsCertType even if it is empty\n" \
Hanno Beckerf7457332018-10-08 17:14:42 +010088 " md=%%s default: SHA256\n" \
89 " possible values:\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +020090 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine384e2742020-08-21 19:51:13 +020091 " SHA224, SHA256, SHA384, SHA512\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000092 "\n"
93
Simon Butcher63cb97e2018-12-06 17:43:31 +000094
Paul Bakkerbdb912d2012-02-13 23:11:30 +000095/*
96 * global options
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098struct options {
Michael Schuster04200932024-06-12 00:05:25 +020099 const char *filename; /* filename of the key file */
100 const char *password; /* password for the key file */
101 int debug_level; /* level of debugging */
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100102 const char *output_file; /* where to store the constructed key file */
Michael Schuster04200932024-06-12 00:05:25 +0200103 const char *subject_name; /* subject name for certificate request */
104 mbedtls_x509_san_list *san_list; /* subjectAltName for certificate request */
105 unsigned char key_usage; /* key usage flags */
106 int force_key_usage; /* Force adding the KeyUsage extension */
107 unsigned char ns_cert_type; /* NS cert type */
108 int force_ns_cert_type; /* Force adding NsCertType extension */
109 mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000110} opt;
111
Michael Schuster8db8d612024-06-01 21:15:02 +0200112static int write_certificate_request(mbedtls_x509write_csr *req, const char *output_file,
Michael Schuster04200932024-06-12 00:05:25 +0200113 int (*f_rng)(void *, unsigned char *, size_t),
114 void *p_rng)
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000115{
Paul Bakker135f1e92013-08-26 16:54:13 +0200116 int ret;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000117 FILE *f;
118 unsigned char output_buf[4096];
Paul Bakker135f1e92013-08-26 16:54:13 +0200119 size_t len = 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 memset(output_buf, 0, 4096);
122 if ((ret = mbedtls_x509write_csr_pem(req, output_buf, 4096, f_rng, p_rng)) < 0) {
123 return ret;
Paul Bakker0c226102014-04-17 16:02:36 +0200124 }
Paul Bakker135f1e92013-08-26 16:54:13 +0200125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 len = strlen((char *) output_buf);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if ((f = fopen(output_file, "w")) == NULL) {
129 return -1;
130 }
131
132 if (fwrite(output_buf, 1, len, f) != len) {
133 fclose(f);
134 return -1;
135 }
136
137 fclose(f);
138
139 return 0;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000140}
141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142int main(int argc, char *argv[])
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000143{
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100144 int ret = 1;
145 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_pk_context key;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000147 char buf[1024];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100148 int i;
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400149 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_x509write_csr req;
151 mbedtls_entropy_context entropy;
152 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200153 const char *pers = "csr example app";
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100154 mbedtls_x509_san_list *cur, *prev;
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400155 mbedtls_asn1_named_data *ext_san_dirname = NULL;
Andrzej Kurek0624e462023-05-23 10:57:14 -0400156#if defined(MBEDTLS_X509_CRT_PARSE_C)
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400157 uint8_t ip[4] = { 0 };
Andrzej Kurek0624e462023-05-23 10:57:14 -0400158#endif
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200159 /*
160 * Set to sane values
161 */
162 mbedtls_x509write_csr_init(&req);
163 mbedtls_pk_init(&key);
164 mbedtls_ctr_drbg_init(&ctr_drbg);
165 memset(buf, 0, sizeof(buf));
166 mbedtls_entropy_init(&entropy);
167
Przemek Stekiel89c636e2023-04-14 09:26:39 +0200168#if defined(MBEDTLS_USE_PSA_CRYPTO)
169 psa_status_t status = psa_crypto_init();
170 if (status != PSA_SUCCESS) {
171 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
172 (int) status);
173 goto exit;
174 }
175#endif /* MBEDTLS_USE_PSA_CRYPTO */
176
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000177 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100178usage:
179 mbedtls_printf(USAGE);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000180 goto exit;
181 }
182
183 opt.filename = DFL_FILENAME;
Hanno Becker56e84632018-11-01 14:10:23 +0000184 opt.password = DFL_PASSWORD;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000185 opt.debug_level = DFL_DEBUG_LEVEL;
186 opt.output_file = DFL_OUTPUT_FILENAME;
187 opt.subject_name = DFL_SUBJECT_NAME;
Paul Bakker57be6e22013-08-26 14:13:14 +0200188 opt.key_usage = DFL_KEY_USAGE;
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100189 opt.force_key_usage = DFL_FORCE_KEY_USAGE;
Paul Bakker57be6e22013-08-26 14:13:14 +0200190 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100191 opt.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
Hanno Beckerf7457332018-10-08 17:14:42 +0100192 opt.md_alg = DFL_MD_ALG;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100193 opt.san_list = NULL;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 for (i = 1; i < argc; i++) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000196 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100197 if ((q = strchr(p, '=')) == NULL) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000198 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000200 *q++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 if (strcmp(p, "filename") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000202 opt.filename = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 } else if (strcmp(p, "password") == 0) {
Hanno Becker56e84632018-11-01 14:10:23 +0000204 opt.password = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 } else if (strcmp(p, "output_file") == 0) {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000206 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 } else if (strcmp(p, "debug_level") == 0) {
208 opt.debug_level = atoi(q);
209 if (opt.debug_level < 0 || opt.debug_level > 65535) {
Hanno Beckerf7457332018-10-08 17:14:42 +0100210 goto usage;
211 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 } else if (strcmp(p, "subject_name") == 0) {
213 opt.subject_name = q;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100214 } else if (strcmp(p, "san") == 0) {
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400215 char *subtype_value;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100216 prev = NULL;
217
218 while (q != NULL) {
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400219 char *semicolon;
220 r = q;
221
222 /* Find the first non-escaped ; occurrence and remove escaped ones */
223 do {
224 if ((semicolon = strchr(r, ';')) != NULL) {
225 if (*(semicolon-1) != '\\') {
226 r = semicolon;
227 break;
228 }
229 /* Remove the escape character */
230 size_t size_left = strlen(semicolon);
231 memmove(semicolon-1, semicolon, size_left);
232 *(semicolon + size_left - 1) = '\0';
233 /* r will now point at the character after the semicolon */
234 r = semicolon;
235 }
236
237 } while (semicolon != NULL);
238
239 if (semicolon != NULL) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100240 *r++ = '\0';
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400241 } else {
242 r = NULL;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100243 }
244
245 cur = mbedtls_calloc(1, sizeof(mbedtls_x509_san_list));
246 if (cur == NULL) {
247 mbedtls_printf("Not enough memory for subjectAltName list\n");
248 goto usage;
249 }
250
251 cur->next = NULL;
252
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400253 if ((subtype_value = strchr(q, ':')) != NULL) {
254 *subtype_value++ = '\0';
Waleed Elmelegyac97af22023-10-12 15:46:06 +0100255 } else {
Waleed Elmelegyeade3fe2023-10-13 09:59:19 +0100256 mbedtls_printf(
David Horstmann9534dfd2023-10-17 14:59:31 +0100257 "Invalid argument for option SAN: Entry must be of the form TYPE:value\n");
Waleed Elmelegyac97af22023-10-12 15:46:06 +0100258 goto usage;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100259 }
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400260 if (strcmp(q, "RFC822") == 0) {
261 cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME;
262 } else if (strcmp(q, "URI") == 0) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100263 cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
264 } else if (strcmp(q, "DNS") == 0) {
265 cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
Przemek Stekiel3a925932023-02-14 13:05:24 +0100266 } else if (strcmp(q, "IP") == 0) {
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000267 size_t ip_addr_len = 0;
Przemek Stekiel3a925932023-02-14 13:05:24 +0100268 cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
Tom Cosgrove656d4b32023-12-08 21:51:15 +0000269 ip_addr_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip);
270 if (ip_addr_len == 0) {
Andrzej Kurek0624e462023-05-23 10:57:14 -0400271 mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n",
272 subtype_value);
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400273 goto exit;
274 }
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400275 cur->node.san.unstructured_name.p = (unsigned char *) ip;
276 cur->node.san.unstructured_name.len = sizeof(ip);
277 } else if (strcmp(q, "DN") == 0) {
278 cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
279 if ((ret = mbedtls_x509_string_to_names(&ext_san_dirname,
280 subtype_value)) != 0) {
281 mbedtls_strerror(ret, buf, sizeof(buf));
282 mbedtls_printf(
283 " failed\n ! mbedtls_x509_string_to_names "
284 "returned -0x%04x - %s\n\n",
285 (unsigned int) -ret, buf);
286 goto exit;
287 }
288 cur->node.san.directory_name = *ext_san_dirname;
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100289 } else {
290 mbedtls_free(cur);
291 goto usage;
292 }
293
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400294 if (cur->node.type == MBEDTLS_X509_SAN_RFC822_NAME ||
295 cur->node.type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER ||
296 cur->node.type == MBEDTLS_X509_SAN_DNS_NAME) {
Andrzej Kurekcd17ecf2023-06-05 17:02:17 -0400297 q = subtype_value;
Przemek Stekiel5a49d3c2023-02-24 13:12:55 +0100298 cur->node.san.unstructured_name.p = (unsigned char *) q;
299 cur->node.san.unstructured_name.len = strlen(q);
Przemek Stekiel3a925932023-02-14 13:05:24 +0100300 }
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100301
302 if (prev == NULL) {
303 opt.san_list = cur;
304 } else {
305 prev->next = cur;
306 }
307
308 prev = cur;
309 q = r;
310 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 } else if (strcmp(p, "md") == 0) {
312 const mbedtls_md_info_t *md_info =
313 mbedtls_md_info_from_string(q);
314 if (md_info == NULL) {
315 mbedtls_printf("Invalid argument for option %s\n", p);
316 goto usage;
317 }
318 opt.md_alg = mbedtls_md_get_type(md_info);
319 } else if (strcmp(p, "key_usage") == 0) {
320 while (q != NULL) {
321 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker57be6e22013-08-26 14:13:14 +0200322 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100330 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100332 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100334 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 } else {
Paul Bakker57be6e22013-08-26 14:13:14 +0200340 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200342
343 q = r;
344 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 } else if (strcmp(p, "force_key_usage") == 0) {
346 switch (atoi(q)) {
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100347 case 0: opt.force_key_usage = 0; break;
348 case 1: opt.force_key_usage = 1; break;
349 default: goto usage;
350 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 } else if (strcmp(p, "ns_cert_type") == 0) {
352 while (q != NULL) {
353 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker57be6e22013-08-26 14:13:14 +0200354 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100360 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100366 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100368 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100370 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 } else {
Paul Bakker57be6e22013-08-26 14:13:14 +0200372 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200374
375 q = r;
376 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 } else if (strcmp(p, "force_ns_cert_type") == 0) {
378 switch (atoi(q)) {
Andres Amaya Garcia7067f812018-09-26 10:51:16 +0100379 case 0: opt.force_ns_cert_type = 0; break;
380 case 1: opt.force_ns_cert_type = 1; break;
381 default: goto usage;
382 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 } else {
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000384 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 }
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000386 }
387
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100388 /* Set the MD algorithm to use for the signature in the CSR */
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 mbedtls_x509write_csr_set_md_alg(&req, opt.md_alg);
Hanno Beckerf7457332018-10-08 17:14:42 +0100390
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100391 /* Set the Key Usage Extension flags in the CSR */
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 if (opt.key_usage || opt.force_key_usage == 1) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100393 ret = mbedtls_x509write_csr_set_key_usage(&req, opt.key_usage);
394
395 if (ret != 0) {
396 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_key_usage returned %d", ret);
397 goto exit;
398 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200400
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100401 /* Set the Cert Type flags in the CSR */
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 if (opt.ns_cert_type || opt.force_ns_cert_type == 1) {
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100403 ret = mbedtls_x509write_csr_set_ns_cert_type(&req, opt.ns_cert_type);
404
405 if (ret != 0) {
406 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_ns_cert_type returned %d", ret);
407 goto exit;
408 }
409 }
410
411 /* Set the SubjectAltName in the CSR */
412 if (opt.san_list != NULL) {
413 ret = mbedtls_x509write_csr_set_subject_alternative_name(&req, opt.san_list);
414
415 if (ret != 0) {
416 mbedtls_printf(
417 " failed\n ! mbedtls_x509write_csr_set_subject_alternative_name returned %d",
418 ret);
419 goto exit;
420 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 }
Paul Bakker57be6e22013-08-26 14:13:14 +0200422
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000423 /*
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200424 * 0. Seed the PRNG
425 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 mbedtls_printf(" . Seeding the random number generator...");
427 fflush(stdout);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200428
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
430 (const unsigned char *) pers,
431 strlen(pers))) != 0) {
432 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d", ret);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200433 goto exit;
434 }
435
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200437
438 /*
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000439 * 1.0. Check the subject name for validity
440 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 mbedtls_printf(" . Checking subject name...");
442 fflush(stdout);
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200443
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 if ((ret = mbedtls_x509write_csr_set_subject_name(&req, opt.subject_name)) != 0) {
445 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200446 goto exit;
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000447 }
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnardee731792013-09-11 22:48:40 +0200450
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000451 /*
452 * 1.1. Load the key
453 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100454 mbedtls_printf(" . Loading the private key ...");
455 fflush(stdout);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, opt.password,
458 mbedtls_ctr_drbg_random, &ctr_drbg);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 if (ret != 0) {
461 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned %d", ret);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200462 goto exit;
463 }
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_x509write_csr_set_key(&req, &key);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 mbedtls_printf(" ok\n");
Paul Bakker8eabfc12013-08-25 10:18:25 +0200468
469 /*
470 * 1.2. Writing the request
471 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 mbedtls_printf(" . Writing the certificate request ...");
473 fflush(stdout);
Paul Bakker8eabfc12013-08-25 10:18:25 +0200474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if ((ret = write_certificate_request(&req, opt.output_file,
476 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
477 mbedtls_printf(" failed\n ! write_certificate_request %d", ret);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000478 goto exit;
479 }
480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 mbedtls_printf(" ok\n");
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000482
Andres Amaya Garciaaacd9282018-04-29 21:36:13 +0100483 exit_code = MBEDTLS_EXIT_SUCCESS;
484
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000485exit:
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200488#ifdef MBEDTLS_ERROR_C
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 mbedtls_strerror(ret, buf, sizeof(buf));
490 mbedtls_printf(" - %s\n", buf);
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200491#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200492 mbedtls_printf("\n");
Manuel Pégourié-Gonnard26b4d452013-09-12 06:56:06 +0200493#endif
494 }
495
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 mbedtls_x509write_csr_free(&req);
Andrzej Kurek6bc7a382023-07-07 05:13:13 -0400497 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 mbedtls_pk_free(&key);
499 mbedtls_ctr_drbg_free(&ctr_drbg);
500 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200501#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200502 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200503#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000504
Hannes Tschofenig6b108602022-12-28 18:38:53 +0100505 cur = opt.san_list;
506 while (cur != NULL) {
507 prev = cur;
508 cur = cur->next;
509 mbedtls_free(prev);
510 }
511
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 mbedtls_exit(exit_code);
Paul Bakkerbdb912d2012-02-13 23:11:30 +0000514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515#endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
Simon Butcher203a6932016-10-07 15:00:17 +0100516 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */