blob: 0daee5627052788a807355902af4728e978a534c [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker9397dcb2013-09-06 09:55:26 +020018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Simon Butcher203a6932016-10-07 15:00:17 +010024#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
25 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
26 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
27 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
28 !defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010029int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020030{
Gilles Peskine449bd832023-01-11 14:50:10 +010031 mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
32 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
33 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
34 "MBEDTLS_ERROR_C not defined.\n");
35 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020036}
37#else
38
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/x509_crt.h"
40#include "mbedtls/x509_csr.h"
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010041#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/entropy.h"
43#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010044#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/error.h"
Valerio Setti791bbe62023-01-11 10:40:18 +010046#include "test/helpers.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020047
Rich Evans18b78c72015-02-11 14:06:19 +000048#include <stdio.h>
49#include <stdlib.h>
50#include <string.h>
Valerio Setti791bbe62023-01-11 10:40:18 +010051#include <errno.h>
Rich Evans18b78c72015-02-11 14:06:19 +000052
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010053#define SET_OID(x, oid) \
Gilles Peskine449bd832023-01-11 14:50:10 +010054 do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char *) oid; } while (0)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000057#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010058 " request_file=%%s default: (empty)\n" \
59 " If request_file is specified, subject_key,\n" \
60 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000061#else
62#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000064
Dave Rodgman66e05502022-10-27 16:29:38 +010065#define FORMAT_PEM 0
66#define FORMAT_DER 1
67
Paul Bakker1014e952013-09-09 13:59:42 +020068#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020069#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020070#define DFL_SUBJECT_KEY "subject.key"
71#define DFL_ISSUER_KEY "ca.key"
72#define DFL_SUBJECT_PWD ""
73#define DFL_ISSUER_PWD ""
74#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000075#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
76#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020077#define DFL_NOT_BEFORE "20010101000000"
78#define DFL_NOT_AFTER "20301231235959"
79#define DFL_SERIAL "1"
Valerio Setti791bbe62023-01-11 10:40:18 +010080#define DFL_SERIAL_HEX "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020081#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020082#define DFL_IS_CA 0
83#define DFL_MAX_PATHLEN -1
Nicholas Wilson99a96b12015-09-10 18:28:01 +010084#define DFL_SIG_ALG MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020085#define DFL_KEY_USAGE 0
Dave Rodgman1577c542022-09-09 10:22:15 +010086#define DFL_EXT_KEY_USAGE NULL
Paul Bakker9397dcb2013-09-06 09:55:26 +020087#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010088#define DFL_VERSION 3
89#define DFL_AUTH_IDENT 1
90#define DFL_SUBJ_IDENT 1
91#define DFL_CONSTRAINTS 1
92#define DFL_DIGEST MBEDTLS_MD_SHA256
Dave Rodgman66e05502022-10-27 16:29:38 +010093#define DFL_FORMAT FORMAT_PEM
Paul Bakker9397dcb2013-09-06 09:55:26 +020094
Rich Evans18b78c72015-02-11 14:06:19 +000095#define USAGE \
96 "\n usage: cert_write param=<>...\n" \
97 "\n acceptable parameters:\n" \
98 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010099 " subject_key=%%s default: subject.key\n" \
100 " subject_pwd=%%s default: (empty)\n" \
101 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000102 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100103 " issuer_crt=%%s default: (empty)\n" \
104 " If issuer_crt is specified, issuer_name is\n" \
105 " ignored!\n" \
106 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000107 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100108 " selfsign=%%d default: 0 (false)\n" \
109 " If selfsign is enabled, issuer_name and\n" \
110 " issuer_key are required (issuer_crt and\n" \
111 " subject_* are ignored\n" \
112 " issuer_key=%%s default: ca.key\n" \
113 " issuer_pwd=%%s default: (empty)\n" \
114 " output_file=%%s default: cert.crt\n" \
115 " serial=%%s default: 1\n" \
Valerio Setti791bbe62023-01-11 10:40:18 +0100116 " In decimal format; it can be used as\n" \
117 " alternative to serial_hex, but it's\n" \
118 " limited in max length to\n" \
119 " unsigned long long int\n" \
120 " serial_hex=%%s default: 1\n" \
121 " In hex format; it can be used as\n" \
122 " alternative to serial\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100123 " not_before=%%s default: 20010101000000\n" \
124 " not_after=%%s default: 20301231235959\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100125 " is_ca=%%d default: 0 (disabled)\n" \
126 " max_pathlen=%%d default: -1 (none)\n" \
127 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200128 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200129 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200130 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100131 " version=%%d default: 3\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 " Possible values: 1, 2, 3\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100133 " subject_identifier=%%s default: 1\n" \
134 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100136 " authority_identifier=%%s default: 1\n" \
137 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100139 " basic_constraints=%%d default: 1\n" \
140 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100142 " key_usage=%%s default: (empty)\n" \
143 " Comma-separated-list of values:\n" \
144 " digital_signature\n" \
145 " non_repudiation\n" \
146 " key_encipherment\n" \
147 " data_encipherment\n" \
148 " key_agreement\n" \
149 " key_cert_sign\n" \
150 " crl_sign\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 " (Considered for v3 only)\n" \
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100152 " ext_key_usage=%%s default: (empty)\n" \
153 " Comma-separated-list of values:\n" \
154 " serverAuth\n" \
155 " clientAuth\n" \
156 " codeSigning\n" \
157 " emailProtection\n" \
158 " timeStamping\n" \
159 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100160 " ns_cert_type=%%s default: (empty)\n" \
161 " Comma-separated-list of values:\n" \
162 " ssl_client\n" \
163 " ssl_server\n" \
164 " email\n" \
165 " object_signing\n" \
166 " ssl_ca\n" \
167 " email_ca\n" \
168 " object_signing_ca\n" \
Dave Rodgman66e05502022-10-27 16:29:38 +0100169 " format=pem|der default: pem\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000170 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000171
Valerio Setti791bbe62023-01-11 10:40:18 +0100172typedef enum {
173 SERIAL_FRMT_UNSPEC,
174 SERIAL_FRMT_DEC,
175 SERIAL_FRMT_HEX
176} serial_format_t;
Simon Butcher63cb97e2018-12-06 17:43:31 +0000177
Paul Bakker9397dcb2013-09-06 09:55:26 +0200178/*
179 * global options
180 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100181struct options {
Paul Bakker8fc30b12013-11-25 13:29:43 +0100182 const char *issuer_crt; /* filename of the issuer certificate */
183 const char *request_file; /* filename of the certificate request */
184 const char *subject_key; /* filename of the subject key file */
185 const char *issuer_key; /* filename of the issuer key file */
186 const char *subject_pwd; /* password for the subject key file */
187 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100188 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100189 const char *subject_name; /* subject name for certificate */
190 const char *issuer_name; /* issuer name for certificate */
191 const char *not_before; /* validity period not before */
192 const char *not_after; /* validity period not after */
Valerio Setti791bbe62023-01-11 10:40:18 +0100193 const char *serial; /* serial number string (decimal) */
194 const char *serial_hex; /* serial number string (hex) */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200195 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200196 int is_ca; /* is a CA certificate */
197 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100198 int authority_identifier; /* add authority identifier to CRT */
199 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100200 int basic_constraints; /* add basic constraints ext to CRT */
201 int version; /* CRT version */
202 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200203 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100204 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200205 unsigned char ns_cert_type; /* NS cert type */
Dave Rodgman66e05502022-10-27 16:29:38 +0100206 int format; /* format */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200207} opt;
208
Gilles Peskine449bd832023-01-11 14:50:10 +0100209int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
210 int (*f_rng)(void *, unsigned char *, size_t),
211 void *p_rng)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200212{
213 int ret;
214 FILE *f;
215 unsigned char output_buf[4096];
Dave Rodgman66e05502022-10-27 16:29:38 +0100216 unsigned char *output_start;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200217 size_t len = 0;
218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 memset(output_buf, 0, 4096);
220 if (opt.format == FORMAT_DER) {
221 ret = mbedtls_x509write_crt_der(crt, output_buf, 4096,
222 f_rng, p_rng);
223 if (ret < 0) {
224 return ret;
225 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200226
Dave Rodgman66e05502022-10-27 16:29:38 +0100227 len = ret;
228 output_start = output_buf + 4096 - len;
229 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
231 f_rng, p_rng);
232 if (ret < 0) {
233 return ret;
234 }
Dave Rodgman66e05502022-10-27 16:29:38 +0100235
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 len = strlen((char *) output_buf);
Dave Rodgman66e05502022-10-27 16:29:38 +0100237 output_start = output_buf;
238 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200239
Gilles Peskine449bd832023-01-11 14:50:10 +0100240 if ((f = fopen(output_file, "w")) == NULL) {
241 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200242 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200243
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 if (fwrite(output_start, 1, len, f) != len) {
245 fclose(f);
246 return -1;
247 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 fclose(f);
250
251 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200252}
253
Valerio Setti791bbe62023-01-11 10:40:18 +0100254int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax,
255 const char *ibuf, size_t *len)
Valerio Settiacf12fb2023-01-09 17:19:26 +0100256{
Valerio Setti791bbe62023-01-11 10:40:18 +0100257 unsigned long long int dec;
258 unsigned int remaining_bytes = sizeof(dec);
259 unsigned char *p = obuf;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100260 unsigned char val;
Valerio Setti791bbe62023-01-11 10:40:18 +0100261 char *end_ptr = NULL;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100262
Valerio Setti791bbe62023-01-11 10:40:18 +0100263 errno = 0;
264 dec = strtoull(ibuf, &end_ptr, 10);
265
266 if ((errno != 0) || (end_ptr == ibuf)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100267 return -1;
268 }
269
Valerio Setti791bbe62023-01-11 10:40:18 +0100270 *len = 0;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100271
Valerio Setti791bbe62023-01-11 10:40:18 +0100272 while (remaining_bytes > 0) {
273 if (obufmax < (*len + 1)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100274 return -1;
275 }
276
Valerio Setti791bbe62023-01-11 10:40:18 +0100277 val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF;
278
279 /* Skip leading zeros */
Valerio Setti48fdbb32023-01-12 15:31:35 +0100280 if ((val != 0) || (*len != 0)) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100281 *p = val;
282 (*len)++;
283 p++;
284 }
285
286 remaining_bytes--;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100287 }
288
289 return 0;
290}
291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200293{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100294 int ret = 1;
295 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_x509_crt issuer_crt;
297 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
298 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200300 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200301 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100302 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200303 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200305 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 mbedtls_x509write_cert crt;
Valerio Setti791bbe62023-01-11 10:40:18 +0100309 serial_format_t serial_frmt = SERIAL_FRMT_UNSPEC;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100310 unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
311 size_t serial_len;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100312 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313 mbedtls_entropy_context entropy;
314 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200315 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200316
317 /*
318 * Set to sane values
319 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 mbedtls_x509write_crt_init(&crt);
321 mbedtls_pk_init(&loaded_issuer_key);
322 mbedtls_pk_init(&loaded_subject_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 mbedtls_ctr_drbg_init(&ctr_drbg);
324 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200325#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200327#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100328 mbedtls_x509_crt_init(&issuer_crt);
329 memset(buf, 0, sizeof(buf));
Valerio Setti791bbe62023-01-11 10:40:18 +0100330 memset(serial, 0, sizeof(serial));
Paul Bakker9397dcb2013-09-06 09:55:26 +0200331
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000332 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100333usage:
334 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200335 goto exit;
336 }
337
Paul Bakker1014e952013-09-09 13:59:42 +0200338 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200339 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200340 opt.subject_key = DFL_SUBJECT_KEY;
341 opt.issuer_key = DFL_ISSUER_KEY;
342 opt.subject_pwd = DFL_SUBJECT_PWD;
343 opt.issuer_pwd = DFL_ISSUER_PWD;
344 opt.output_file = DFL_OUTPUT_FILENAME;
345 opt.subject_name = DFL_SUBJECT_NAME;
346 opt.issuer_name = DFL_ISSUER_NAME;
347 opt.not_before = DFL_NOT_BEFORE;
348 opt.not_after = DFL_NOT_AFTER;
349 opt.serial = DFL_SERIAL;
Valerio Setti791bbe62023-01-11 10:40:18 +0100350 opt.serial_hex = DFL_SERIAL_HEX;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200351 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200352 opt.is_ca = DFL_IS_CA;
353 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200354 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100355 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200356 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100357 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100358 opt.md = DFL_DIGEST;
359 opt.subject_identifier = DFL_SUBJ_IDENT;
360 opt.authority_identifier = DFL_AUTH_IDENT;
361 opt.basic_constraints = DFL_CONSTRAINTS;
Dave Rodgman66e05502022-10-27 16:29:38 +0100362 opt.format = DFL_FORMAT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200365
366 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200368 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200370 *q++ = '\0';
371
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200373 opt.request_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200375 opt.subject_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100376 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200377 opt.issuer_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200379 opt.subject_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200381 opt.issuer_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200383 opt.issuer_crt = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200385 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100386 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200387 opt.subject_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100388 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200389 opt.issuer_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100390 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200391 opt.not_before = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100392 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200393 opt.not_after = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 } else if (strcmp(p, "serial") == 0) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100395 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
396 mbedtls_printf("Invalid attempt to set the serial more than once\n");
397 goto usage;
398 }
399 serial_frmt = SERIAL_FRMT_DEC;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200400 opt.serial = q;
Valerio Setti791bbe62023-01-11 10:40:18 +0100401 } else if (strcmp(p, "serial_hex") == 0) {
402 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
403 mbedtls_printf("Invalid attempt to set the serial more than once\n");
404 goto usage;
405 }
406 serial_frmt = SERIAL_FRMT_HEX;
407 opt.serial_hex = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 } else if (strcmp(p, "authority_identifier") == 0) {
409 opt.authority_identifier = atoi(q);
410 if (opt.authority_identifier != 0 &&
411 opt.authority_identifier != 1) {
412 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100413 goto usage;
414 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 } else if (strcmp(p, "subject_identifier") == 0) {
416 opt.subject_identifier = atoi(q);
417 if (opt.subject_identifier != 0 &&
418 opt.subject_identifier != 1) {
419 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100420 goto usage;
421 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 } else if (strcmp(p, "basic_constraints") == 0) {
423 opt.basic_constraints = atoi(q);
424 if (opt.basic_constraints != 0 &&
425 opt.basic_constraints != 1) {
426 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100427 goto usage;
428 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200430 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 mbedtls_md_info_from_string(q);
432 if (md_info == NULL) {
433 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100434 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100435 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100436 opt.md = mbedtls_md_get_type(md_info);
437 } else if (strcmp(p, "version") == 0) {
438 opt.version = atoi(q);
439 if (opt.version < 1 || opt.version > 3) {
440 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100441 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100442 }
Hanno Becker38eff432017-09-22 15:38:20 +0100443 opt.version--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100444 } else if (strcmp(p, "selfsign") == 0) {
445 opt.selfsign = atoi(q);
446 if (opt.selfsign < 0 || opt.selfsign > 1) {
447 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200448 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100449 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 } else if (strcmp(p, "is_ca") == 0) {
451 opt.is_ca = atoi(q);
452 if (opt.is_ca < 0 || opt.is_ca > 1) {
453 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200454 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100455 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } else if (strcmp(p, "max_pathlen") == 0) {
457 opt.max_pathlen = atoi(q);
458 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
459 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200460 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100461 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 } else if (strcmp(p, "key_usage") == 0) {
463 while (q != NULL) {
464 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200465 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200469 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200471 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100473 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100475 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100477 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200481 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 } else {
483 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200484 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100485 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200486
487 q = r;
488 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 } else if (strcmp(p, "ext_key_usage") == 0) {
Dave Rodgman64937852022-08-15 14:12:25 +0100490 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
491
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 while (q != NULL) {
493 if ((r = strchr(q, ',')) != NULL) {
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100494 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100496
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100498 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 if (strcmp(q, "serverAuth") == 0) {
500 SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
501 } else if (strcmp(q, "clientAuth") == 0) {
502 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
503 } else if (strcmp(q, "codeSigning") == 0) {
504 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
505 } else if (strcmp(q, "emailProtection") == 0) {
506 SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
507 } else if (strcmp(q, "timeStamping") == 0) {
508 SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
509 } else if (strcmp(q, "OCSPSigning") == 0) {
510 SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
511 } else {
512 mbedtls_printf("Invalid argument for option %s\n", p);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100513 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100514 }
Dave Rodgman64937852022-08-15 14:12:25 +0100515
516 *tail = ext_key_usage;
517 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100518
519 q = r;
520 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 } else if (strcmp(p, "ns_cert_type") == 0) {
522 while (q != NULL) {
523 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200524 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200526
Gilles Peskine449bd832023-01-11 14:50:10 +0100527 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200528 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100530 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200532 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200534 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100536 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100538 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100540 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 } else {
542 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200543 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100544 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200545
546 q = r;
547 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 } else if (strcmp(p, "format") == 0) {
549 if (strcmp(q, "der") == 0) {
550 opt.format = FORMAT_DER;
551 } else if (strcmp(q, "pem") == 0) {
552 opt.format = FORMAT_PEM;
553 } else {
554 mbedtls_printf("Invalid argument for option %s\n", p);
Dave Rodgman66e05502022-10-27 16:29:38 +0100555 goto usage;
556 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200558 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200560 }
561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200563
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200564 /*
565 * 0. Seed the PRNG
566 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 mbedtls_printf(" . Seeding the random number generator...");
568 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200569
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
571 (const unsigned char *) pers,
572 strlen(pers))) != 0) {
573 mbedtls_strerror(ret, buf, sizeof(buf));
574 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
575 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200576 goto exit;
577 }
578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200580
Paul Bakker9397dcb2013-09-06 09:55:26 +0200581 // Parse serial to MPI
582 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 mbedtls_printf(" . Reading serial number...");
584 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200585
Valerio Setti791bbe62023-01-11 10:40:18 +0100586 if (serial_frmt == SERIAL_FRMT_HEX) {
587 ret = mbedtls_test_unhexify(serial, sizeof(serial),
588 opt.serial_hex, &serial_len);
589 } else { // SERIAL_FRMT_DEC || SERIAL_FRMT_UNSPEC
590 ret = parse_serial_decimal_format(serial, sizeof(serial),
591 opt.serial, &serial_len);
592 }
593
594 if (ret != 0) {
595 mbedtls_printf(" failed\n ! Unable to parse serial\n");
596 goto exit;
597 }
598
Gilles Peskine449bd832023-01-11 14:50:10 +0100599 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200600
Paul Bakker1014e952013-09-09 13:59:42 +0200601 // Parse issuer certificate if present
602 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200604 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200605 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200606 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 mbedtls_printf(" . Loading the issuer certificate ...");
608 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200609
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
611 mbedtls_strerror(ret, buf, sizeof(buf));
612 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
613 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200614 goto exit;
615 }
616
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
618 &issuer_crt.subject);
619 if (ret < 0) {
620 mbedtls_strerror(ret, buf, sizeof(buf));
621 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
622 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200623 goto exit;
624 }
625
Paul Bakkere2673fb2013-09-09 15:52:07 +0200626 opt.issuer_name = issuer_name;
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200629 }
630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200632 // Parse certificate request if present
633 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200635 /*
636 * 1.0.b. Load the CSR
637 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 mbedtls_printf(" . Loading the certificate request ...");
639 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
642 mbedtls_strerror(ret, buf, sizeof(buf));
643 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
644 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200645 goto exit;
646 }
647
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
649 &csr.subject);
650 if (ret < 0) {
651 mbedtls_strerror(ret, buf, sizeof(buf));
652 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
653 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200654 goto exit;
655 }
656
657 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200658 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200662#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200663
664 /*
665 * 1.1. Load the keys
666 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if (!opt.selfsign && !strlen(opt.request_file)) {
668 mbedtls_printf(" . Loading the subject key ...");
669 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200670
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
672 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
673 if (ret != 0) {
674 mbedtls_strerror(ret, buf, sizeof(buf));
675 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
676 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200677 goto exit;
678 }
Paul Bakker1014e952013-09-09 13:59:42 +0200679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200681 }
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 mbedtls_printf(" . Loading the issuer key ...");
684 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200685
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
687 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
688 if (ret != 0) {
689 mbedtls_strerror(ret, buf, sizeof(buf));
690 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
691 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200692 goto exit;
693 }
694
695 // Check if key and issuer certificate match
696 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100697 if (strlen(opt.issuer_crt)) {
698 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key,
699 mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
700 mbedtls_printf(" failed\n ! issuer_key does not match "
701 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200702 goto exit;
703 }
704 }
705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200707
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100709 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200710 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200711 }
712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
714 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200715
Paul Bakker9397dcb2013-09-06 09:55:26 +0200716 /*
717 * 1.0. Check the names for validity
718 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
720 mbedtls_strerror(ret, buf, sizeof(buf));
721 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
722 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200723 goto exit;
724 }
725
Gilles Peskine449bd832023-01-11 14:50:10 +0100726 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
727 mbedtls_strerror(ret, buf, sizeof(buf));
728 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
729 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200730 goto exit;
731 }
732
Gilles Peskine449bd832023-01-11 14:50:10 +0100733 mbedtls_printf(" . Setting certificate values ...");
734 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736 mbedtls_x509write_crt_set_version(&crt, opt.version);
737 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100738
Valerio Settiaf4815c2023-01-26 17:43:09 +0100739 ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100740 if (ret != 0) {
741 mbedtls_strerror(ret, buf, sizeof(buf));
Valerio Settiaf4815c2023-01-26 17:43:09 +0100742 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_raw "
Valerio Settida0afcc2023-01-05 16:46:59 +0100743 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
744 goto exit;
745 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200746
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
748 if (ret != 0) {
749 mbedtls_strerror(ret, buf, sizeof(buf));
750 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
751 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200752 goto exit;
753 }
754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
758 opt.basic_constraints != 0) {
759 mbedtls_printf(" . Adding the Basic Constraints extension ...");
760 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200761
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
763 opt.max_pathlen);
764 if (ret != 0) {
765 mbedtls_strerror(ret, buf, sizeof(buf));
766 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
767 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100768 goto exit;
769 }
770
Gilles Peskine449bd832023-01-11 14:50:10 +0100771 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100772 }
Paul Bakker15162a02013-09-06 19:27:21 +0200773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
776 opt.subject_identifier != 0) {
777 mbedtls_printf(" . Adding the Subject Key Identifier ...");
778 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
781 if (ret != 0) {
782 mbedtls_strerror(ret, buf, sizeof(buf));
783 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
784 "_key_identifier returned -0x%04x - %s\n\n",
785 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100786 goto exit;
787 }
788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200790 }
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
793 opt.authority_identifier != 0) {
794 mbedtls_printf(" . Adding the Authority Key Identifier ...");
795 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200796
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
798 if (ret != 0) {
799 mbedtls_strerror(ret, buf, sizeof(buf));
800 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
801 "key_identifier returned -0x%04x - %s\n\n",
802 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100803 goto exit;
804 }
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100807 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
811 opt.key_usage != 0) {
812 mbedtls_printf(" . Adding the Key Usage extension ...");
813 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
816 if (ret != 0) {
817 mbedtls_strerror(ret, buf, sizeof(buf));
818 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
819 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200820 goto exit;
821 }
822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200824 }
825
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 if (opt.ext_key_usage) {
827 mbedtls_printf(" . Adding the Extended Key Usage extension ...");
828 fflush(stdout);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
831 if (ret != 0) {
832 mbedtls_strerror(ret, buf, sizeof(buf));
833 mbedtls_printf(
834 " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
835 (unsigned int) -ret,
836 buf);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100837 goto exit;
838 }
839
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 mbedtls_printf(" ok\n");
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100841 }
842
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
844 opt.ns_cert_type != 0) {
845 mbedtls_printf(" . Adding the NS Cert Type extension ...");
846 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200847
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
849 if (ret != 0) {
850 mbedtls_strerror(ret, buf, sizeof(buf));
851 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
852 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200853 goto exit;
854 }
855
Gilles Peskine449bd832023-01-11 14:50:10 +0100856 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200857 }
858
Paul Bakker9397dcb2013-09-06 09:55:26 +0200859 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100860 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200861 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 mbedtls_printf(" . Writing the certificate...");
863 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200864
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 if ((ret = write_certificate(&crt, opt.output_file,
866 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
867 mbedtls_strerror(ret, buf, sizeof(buf));
868 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
869 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200870 goto exit;
871 }
872
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200874
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100875 exit_code = MBEDTLS_EXIT_SUCCESS;
876
Paul Bakker9397dcb2013-09-06 09:55:26 +0200877exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100878#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +0100880#endif /* MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 mbedtls_x509_crt_free(&issuer_crt);
882 mbedtls_x509write_crt_free(&crt);
883 mbedtls_pk_free(&loaded_subject_key);
884 mbedtls_pk_free(&loaded_issuer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100885 mbedtls_ctr_drbg_free(&ctr_drbg);
886 mbedtls_entropy_free(&entropy);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200887
Gilles Peskine449bd832023-01-11 14:50:10 +0100888 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200889}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200890#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
891 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100892 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */