1 /*
2 * Certificate request generation
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #include "mbedtls/build_info.h"
9
10 #include "mbedtls/platform.h"
11 /* md.h is included this early since MD_CAN_XXX macros are defined there. */
12 #include "mbedtls/md.h"
13
14 #if !defined(MBEDTLS_X509_CSR_WRITE_C) || !defined(MBEDTLS_X509_CRT_PARSE_C) || \
15 !defined(MBEDTLS_PK_PARSE_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
16 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
17 !defined(MBEDTLS_PEM_WRITE_C) || !defined(MBEDTLS_FS_IO)
main(void)18 int main(void)
19 {
20 mbedtls_printf("MBEDTLS_X509_CSR_WRITE_C and/or MBEDTLS_FS_IO and/or "
21 "MBEDTLS_PK_PARSE_C and/or MBEDTLS_MD_CAN_SHA256 and/or "
22 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
23 "not defined.\n");
24 mbedtls_exit(0);
25 }
26 #else
27
28 #include "mbedtls/x509_csr.h"
29 #include "mbedtls/entropy.h"
30 #include "mbedtls/ctr_drbg.h"
31 #include "mbedtls/error.h"
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #define DFL_FILENAME "keyfile.key"
38 #define DFL_PASSWORD NULL
39 #define DFL_DEBUG_LEVEL 0
40 #define DFL_OUTPUT_FILENAME "cert.req"
41 #define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
42 #define DFL_KEY_USAGE 0
43 #define DFL_FORCE_KEY_USAGE 0
44 #define DFL_NS_CERT_TYPE 0
45 #define DFL_FORCE_NS_CERT_TYPE 0
46 #define DFL_MD_ALG MBEDTLS_MD_SHA256
47
48 #define USAGE \
49 "\n usage: cert_req param=<>...\n" \
50 "\n acceptable parameters:\n" \
51 " filename=%%s default: keyfile.key\n" \
52 " password=%%s default: NULL\n" \
53 " debug_level=%%d default: 0 (disabled)\n" \
54 " output_file=%%s default: cert.req\n" \
55 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
56 " san=%%s default: (none)\n" \
57 " Semicolon-separated-list of values:\n" \
58 " DNS:value\n" \
59 " URI:value\n" \
60 " RFC822:value\n" \
61 " IP:value (Only IPv4 is supported)\n" \
62 " DN:list of comma separated key=value pairs\n" \
63 " key_usage=%%s default: (empty)\n" \
64 " Comma-separated-list of values:\n" \
65 " digital_signature\n" \
66 " non_repudiation\n" \
67 " key_encipherment\n" \
68 " data_encipherment\n" \
69 " key_agreement\n" \
70 " key_cert_sign\n" \
71 " crl_sign\n" \
72 " force_key_usage=0/1 default: off\n" \
73 " Add KeyUsage even if it is empty\n" \
74 " ns_cert_type=%%s default: (empty)\n" \
75 " Comma-separated-list of values:\n" \
76 " ssl_client\n" \
77 " ssl_server\n" \
78 " email\n" \
79 " object_signing\n" \
80 " ssl_ca\n" \
81 " email_ca\n" \
82 " object_signing_ca\n" \
83 " force_ns_cert_type=0/1 default: off\n" \
84 " Add NsCertType even if it is empty\n" \
85 " md=%%s default: SHA256\n" \
86 " possible values:\n" \
87 " MD5, RIPEMD160, SHA1,\n" \
88 " SHA224, SHA256, SHA384, SHA512\n" \
89 "\n"
90
91
92 /*
93 * global options
94 */
95 struct options {
96 const char *filename; /* filename of the key file */
97 const char *password; /* password for the key file */
98 int debug_level; /* level of debugging */
99 const char *output_file; /* where to store the constructed key file */
100 const char *subject_name; /* subject name for certificate request */
101 mbedtls_x509_san_list *san_list; /* subjectAltName for certificate request */
102 unsigned char key_usage; /* key usage flags */
103 int force_key_usage; /* Force adding the KeyUsage extension */
104 unsigned char ns_cert_type; /* NS cert type */
105 int force_ns_cert_type; /* Force adding NsCertType extension */
106 mbedtls_md_type_t md_alg; /* Hash algorithm used for signature. */
107 } opt;
108
write_certificate_request(mbedtls_x509write_csr * req,const char * output_file,int (* f_rng)(void *,unsigned char *,size_t),void * p_rng)109 int write_certificate_request(mbedtls_x509write_csr *req, const char *output_file,
110 int (*f_rng)(void *, unsigned char *, size_t),
111 void *p_rng)
112 {
113 int ret;
114 FILE *f;
115 unsigned char output_buf[4096];
116 size_t len = 0;
117
118 memset(output_buf, 0, 4096);
119 if ((ret = mbedtls_x509write_csr_pem(req, output_buf, 4096, f_rng, p_rng)) < 0) {
120 return ret;
121 }
122
123 len = strlen((char *) output_buf);
124
125 if ((f = fopen(output_file, "w")) == NULL) {
126 return -1;
127 }
128
129 if (fwrite(output_buf, 1, len, f) != len) {
130 fclose(f);
131 return -1;
132 }
133
134 fclose(f);
135
136 return 0;
137 }
138
main(int argc,char * argv[])139 int main(int argc, char *argv[])
140 {
141 int ret = 1;
142 int exit_code = MBEDTLS_EXIT_FAILURE;
143 mbedtls_pk_context key;
144 char buf[1024];
145 int i;
146 char *p, *q, *r;
147 mbedtls_x509write_csr req;
148 mbedtls_entropy_context entropy;
149 mbedtls_ctr_drbg_context ctr_drbg;
150 const char *pers = "csr example app";
151 mbedtls_x509_san_list *cur, *prev;
152 mbedtls_asn1_named_data *ext_san_dirname = NULL;
153 #if defined(MBEDTLS_X509_CRT_PARSE_C)
154 uint8_t ip[4] = { 0 };
155 #endif
156 /*
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
165 #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
174 if (argc < 2) {
175 usage:
176 mbedtls_printf(USAGE);
177 goto exit;
178 }
179
180 opt.filename = DFL_FILENAME;
181 opt.password = DFL_PASSWORD;
182 opt.debug_level = DFL_DEBUG_LEVEL;
183 opt.output_file = DFL_OUTPUT_FILENAME;
184 opt.subject_name = DFL_SUBJECT_NAME;
185 opt.key_usage = DFL_KEY_USAGE;
186 opt.force_key_usage = DFL_FORCE_KEY_USAGE;
187 opt.ns_cert_type = DFL_NS_CERT_TYPE;
188 opt.force_ns_cert_type = DFL_FORCE_NS_CERT_TYPE;
189 opt.md_alg = DFL_MD_ALG;
190 opt.san_list = NULL;
191
192 for (i = 1; i < argc; i++) {
193 p = argv[i];
194 if ((q = strchr(p, '=')) == NULL) {
195 goto usage;
196 }
197 *q++ = '\0';
198 if (strcmp(p, "filename") == 0) {
199 opt.filename = q;
200 } else if (strcmp(p, "password") == 0) {
201 opt.password = q;
202 } else if (strcmp(p, "output_file") == 0) {
203 opt.output_file = q;
204 } else if (strcmp(p, "debug_level") == 0) {
205 opt.debug_level = atoi(q);
206 if (opt.debug_level < 0 || opt.debug_level > 65535) {
207 goto usage;
208 }
209 } else if (strcmp(p, "subject_name") == 0) {
210 opt.subject_name = q;
211 } else if (strcmp(p, "san") == 0) {
212 char *subtype_value;
213 prev = NULL;
214
215 while (q != NULL) {
216 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) {
237 *r++ = '\0';
238 } else {
239 r = NULL;
240 }
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
250 if ((subtype_value = strchr(q, ':')) != NULL) {
251 *subtype_value++ = '\0';
252 }
253 if (strcmp(q, "RFC822") == 0) {
254 cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME;
255 } else if (strcmp(q, "URI") == 0) {
256 cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
257 } else if (strcmp(q, "DNS") == 0) {
258 cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
259 } else if (strcmp(q, "IP") == 0) {
260 size_t ip_len = 0;
261 cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
262 ip_len = mbedtls_x509_crt_parse_cn_inet_pton(subtype_value, ip);
263 if (ip_len == 0) {
264 mbedtls_printf("mbedtls_x509_crt_parse_cn_inet_pton failed to parse %s\n",
265 subtype_value);
266 goto exit;
267 }
268 cur->node.san.unstructured_name.p = (unsigned char *) ip;
269 cur->node.san.unstructured_name.len = sizeof(ip);
270 } else if (strcmp(q, "DN") == 0) {
271 cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
272 if ((ret = mbedtls_x509_string_to_names(&ext_san_dirname,
273 subtype_value)) != 0) {
274 mbedtls_strerror(ret, buf, sizeof(buf));
275 mbedtls_printf(
276 " failed\n ! mbedtls_x509_string_to_names "
277 "returned -0x%04x - %s\n\n",
278 (unsigned int) -ret, buf);
279 goto exit;
280 }
281 cur->node.san.directory_name = *ext_san_dirname;
282 } else {
283 mbedtls_free(cur);
284 goto usage;
285 }
286
287 if (cur->node.type == MBEDTLS_X509_SAN_RFC822_NAME ||
288 cur->node.type == MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER ||
289 cur->node.type == MBEDTLS_X509_SAN_DNS_NAME) {
290 q = subtype_value;
291 cur->node.san.unstructured_name.p = (unsigned char *) q;
292 cur->node.san.unstructured_name.len = strlen(q);
293 }
294
295 if (prev == NULL) {
296 opt.san_list = cur;
297 } else {
298 prev->next = cur;
299 }
300
301 prev = cur;
302 q = r;
303 }
304 } else if (strcmp(p, "md") == 0) {
305 const mbedtls_md_info_t *md_info =
306 mbedtls_md_info_from_string(q);
307 if (md_info == NULL) {
308 mbedtls_printf("Invalid argument for option %s\n", p);
309 goto usage;
310 }
311 opt.md_alg = mbedtls_md_get_type(md_info);
312 } else if (strcmp(p, "key_usage") == 0) {
313 while (q != NULL) {
314 if ((r = strchr(q, ',')) != NULL) {
315 *r++ = '\0';
316 }
317
318 if (strcmp(q, "digital_signature") == 0) {
319 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
320 } else if (strcmp(q, "non_repudiation") == 0) {
321 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
322 } else if (strcmp(q, "key_encipherment") == 0) {
323 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
324 } else if (strcmp(q, "data_encipherment") == 0) {
325 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
326 } else if (strcmp(q, "key_agreement") == 0) {
327 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
328 } else if (strcmp(q, "key_cert_sign") == 0) {
329 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
330 } else if (strcmp(q, "crl_sign") == 0) {
331 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
332 } else {
333 goto usage;
334 }
335
336 q = r;
337 }
338 } else if (strcmp(p, "force_key_usage") == 0) {
339 switch (atoi(q)) {
340 case 0: opt.force_key_usage = 0; break;
341 case 1: opt.force_key_usage = 1; break;
342 default: goto usage;
343 }
344 } else if (strcmp(p, "ns_cert_type") == 0) {
345 while (q != NULL) {
346 if ((r = strchr(q, ',')) != NULL) {
347 *r++ = '\0';
348 }
349
350 if (strcmp(q, "ssl_client") == 0) {
351 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
352 } else if (strcmp(q, "ssl_server") == 0) {
353 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
354 } else if (strcmp(q, "email") == 0) {
355 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
356 } else if (strcmp(q, "object_signing") == 0) {
357 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
358 } else if (strcmp(q, "ssl_ca") == 0) {
359 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
360 } else if (strcmp(q, "email_ca") == 0) {
361 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
362 } else if (strcmp(q, "object_signing_ca") == 0) {
363 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
364 } else {
365 goto usage;
366 }
367
368 q = r;
369 }
370 } else if (strcmp(p, "force_ns_cert_type") == 0) {
371 switch (atoi(q)) {
372 case 0: opt.force_ns_cert_type = 0; break;
373 case 1: opt.force_ns_cert_type = 1; break;
374 default: goto usage;
375 }
376 } else {
377 goto usage;
378 }
379 }
380
381 /* Set the MD algorithm to use for the signature in the CSR */
382 mbedtls_x509write_csr_set_md_alg(&req, opt.md_alg);
383
384 /* Set the Key Usage Extension flags in the CSR */
385 if (opt.key_usage || opt.force_key_usage == 1) {
386 ret = mbedtls_x509write_csr_set_key_usage(&req, opt.key_usage);
387
388 if (ret != 0) {
389 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_key_usage returned %d", ret);
390 goto exit;
391 }
392 }
393
394 /* Set the Cert Type flags in the CSR */
395 if (opt.ns_cert_type || opt.force_ns_cert_type == 1) {
396 ret = mbedtls_x509write_csr_set_ns_cert_type(&req, opt.ns_cert_type);
397
398 if (ret != 0) {
399 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_ns_cert_type returned %d", ret);
400 goto exit;
401 }
402 }
403
404 /* Set the SubjectAltName in the CSR */
405 if (opt.san_list != NULL) {
406 ret = mbedtls_x509write_csr_set_subject_alternative_name(&req, opt.san_list);
407
408 if (ret != 0) {
409 mbedtls_printf(
410 " failed\n ! mbedtls_x509write_csr_set_subject_alternative_name returned %d",
411 ret);
412 goto exit;
413 }
414 }
415
416 /*
417 * 0. Seed the PRNG
418 */
419 mbedtls_printf(" . Seeding the random number generator...");
420 fflush(stdout);
421
422 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
423 (const unsigned char *) pers,
424 strlen(pers))) != 0) {
425 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d", ret);
426 goto exit;
427 }
428
429 mbedtls_printf(" ok\n");
430
431 /*
432 * 1.0. Check the subject name for validity
433 */
434 mbedtls_printf(" . Checking subject name...");
435 fflush(stdout);
436
437 if ((ret = mbedtls_x509write_csr_set_subject_name(&req, opt.subject_name)) != 0) {
438 mbedtls_printf(" failed\n ! mbedtls_x509write_csr_set_subject_name returned %d", ret);
439 goto exit;
440 }
441
442 mbedtls_printf(" ok\n");
443
444 /*
445 * 1.1. Load the key
446 */
447 mbedtls_printf(" . Loading the private key ...");
448 fflush(stdout);
449
450 ret = mbedtls_pk_parse_keyfile(&key, opt.filename, opt.password,
451 mbedtls_ctr_drbg_random, &ctr_drbg);
452
453 if (ret != 0) {
454 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile returned %d", ret);
455 goto exit;
456 }
457
458 mbedtls_x509write_csr_set_key(&req, &key);
459
460 mbedtls_printf(" ok\n");
461
462 /*
463 * 1.2. Writing the request
464 */
465 mbedtls_printf(" . Writing the certificate request ...");
466 fflush(stdout);
467
468 if ((ret = write_certificate_request(&req, opt.output_file,
469 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
470 mbedtls_printf(" failed\n ! write_certificate_request %d", ret);
471 goto exit;
472 }
473
474 mbedtls_printf(" ok\n");
475
476 exit_code = MBEDTLS_EXIT_SUCCESS;
477
478 exit:
479
480 if (exit_code != MBEDTLS_EXIT_SUCCESS) {
481 #ifdef MBEDTLS_ERROR_C
482 mbedtls_strerror(ret, buf, sizeof(buf));
483 mbedtls_printf(" - %s\n", buf);
484 #else
485 mbedtls_printf("\n");
486 #endif
487 }
488
489 mbedtls_x509write_csr_free(&req);
490 mbedtls_asn1_free_named_data_list(&ext_san_dirname);
491 mbedtls_pk_free(&key);
492 mbedtls_ctr_drbg_free(&ctr_drbg);
493 mbedtls_entropy_free(&entropy);
494 #if defined(MBEDTLS_USE_PSA_CRYPTO)
495 mbedtls_psa_crypto_free();
496 #endif /* MBEDTLS_USE_PSA_CRYPTO */
497
498 cur = opt.san_list;
499 while (cur != NULL) {
500 prev = cur;
501 cur = cur->next;
502 mbedtls_free(prev);
503 }
504
505
506 mbedtls_exit(exit_code);
507 }
508 #endif /* MBEDTLS_X509_CSR_WRITE_C && MBEDTLS_PK_PARSE_C && MBEDTLS_FS_IO &&
509 MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C && MBEDTLS_PEM_WRITE_C */
510