blob: b8f68b97f28f7b613378c91666e8dc6637f8b43c [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"
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040023#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024
Simon Butcher203a6932016-10-07 15:00:17 +010025#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
26 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
27 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010028 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
Simon Butcher203a6932016-10-07 15:00:17 +010029 !defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010030int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020031{
Gilles Peskine449bd832023-01-11 14:50:10 +010032 mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010033 "MBEDTLS_FS_IO and/or MBEDTLS_MD_CAN_SHA256 and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010034 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
35 "MBEDTLS_ERROR_C not defined.\n");
36 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020037}
38#else
39
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/x509_crt.h"
41#include "mbedtls/x509_csr.h"
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010042#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000043#include "mbedtls/entropy.h"
44#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010045#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/error.h"
Valerio Setti791bbe62023-01-11 10:40:18 +010047#include "test/helpers.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020048
Rich Evans18b78c72015-02-11 14:06:19 +000049#include <stdio.h>
50#include <stdlib.h>
51#include <string.h>
Valerio Setti791bbe62023-01-11 10:40:18 +010052#include <errno.h>
Rich Evans18b78c72015-02-11 14:06:19 +000053
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010054#define SET_OID(x, oid) \
Gilles Peskine449bd832023-01-11 14:50:10 +010055 do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char *) oid; } while (0)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000058#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010059 " request_file=%%s default: (empty)\n" \
60 " If request_file is specified, subject_key,\n" \
61 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000062#else
63#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000065
Dave Rodgman66e05502022-10-27 16:29:38 +010066#define FORMAT_PEM 0
67#define FORMAT_DER 1
68
Paul Bakker1014e952013-09-09 13:59:42 +020069#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020070#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020071#define DFL_SUBJECT_KEY "subject.key"
72#define DFL_ISSUER_KEY "ca.key"
73#define DFL_SUBJECT_PWD ""
74#define DFL_ISSUER_PWD ""
75#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000076#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
77#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020078#define DFL_NOT_BEFORE "20010101000000"
79#define DFL_NOT_AFTER "20301231235959"
80#define DFL_SERIAL "1"
Valerio Setti791bbe62023-01-11 10:40:18 +010081#define DFL_SERIAL_HEX "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020082#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020083#define DFL_IS_CA 0
84#define DFL_MAX_PATHLEN -1
Nicholas Wilson99a96b12015-09-10 18:28:01 +010085#define DFL_SIG_ALG MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020086#define DFL_KEY_USAGE 0
Dave Rodgman1577c542022-09-09 10:22:15 +010087#define DFL_EXT_KEY_USAGE NULL
Paul Bakker9397dcb2013-09-06 09:55:26 +020088#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010089#define DFL_VERSION 3
90#define DFL_AUTH_IDENT 1
91#define DFL_SUBJ_IDENT 1
92#define DFL_CONSTRAINTS 1
93#define DFL_DIGEST MBEDTLS_MD_SHA256
Dave Rodgman66e05502022-10-27 16:29:38 +010094#define DFL_FORMAT FORMAT_PEM
Paul Bakker9397dcb2013-09-06 09:55:26 +020095
Rich Evans18b78c72015-02-11 14:06:19 +000096#define USAGE \
97 "\n usage: cert_write param=<>...\n" \
98 "\n acceptable parameters:\n" \
99 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100100 " subject_key=%%s default: subject.key\n" \
101 " subject_pwd=%%s default: (empty)\n" \
102 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000103 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100104 " issuer_crt=%%s default: (empty)\n" \
105 " If issuer_crt is specified, issuer_name is\n" \
106 " ignored!\n" \
107 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000108 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100109 " selfsign=%%d default: 0 (false)\n" \
110 " If selfsign is enabled, issuer_name and\n" \
111 " issuer_key are required (issuer_crt and\n" \
112 " subject_* are ignored\n" \
113 " issuer_key=%%s default: ca.key\n" \
114 " issuer_pwd=%%s default: (empty)\n" \
115 " output_file=%%s default: cert.crt\n" \
116 " serial=%%s default: 1\n" \
Valerio Setti791bbe62023-01-11 10:40:18 +0100117 " In decimal format; it can be used as\n" \
118 " alternative to serial_hex, but it's\n" \
119 " limited in max length to\n" \
120 " unsigned long long int\n" \
121 " serial_hex=%%s default: 1\n" \
122 " In hex format; it can be used as\n" \
123 " alternative to serial\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 " not_before=%%s default: 20010101000000\n" \
125 " not_after=%%s default: 20301231235959\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100126 " is_ca=%%d default: 0 (disabled)\n" \
127 " max_pathlen=%%d default: -1 (none)\n" \
128 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200129 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200130 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200131 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100132 " version=%%d default: 3\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100133 " Possible values: 1, 2, 3\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100134 " subject_identifier=%%s default: 1\n" \
135 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100137 " authority_identifier=%%s default: 1\n" \
138 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100140 " basic_constraints=%%d default: 1\n" \
141 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100143 " key_usage=%%s default: (empty)\n" \
144 " Comma-separated-list of values:\n" \
145 " digital_signature\n" \
146 " non_repudiation\n" \
147 " key_encipherment\n" \
148 " data_encipherment\n" \
149 " key_agreement\n" \
150 " key_cert_sign\n" \
151 " crl_sign\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 " (Considered for v3 only)\n" \
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100153 " ext_key_usage=%%s default: (empty)\n" \
154 " Comma-separated-list of values:\n" \
155 " serverAuth\n" \
156 " clientAuth\n" \
157 " codeSigning\n" \
158 " emailProtection\n" \
159 " timeStamping\n" \
160 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100161 " ns_cert_type=%%s default: (empty)\n" \
162 " Comma-separated-list of values:\n" \
163 " ssl_client\n" \
164 " ssl_server\n" \
165 " email\n" \
166 " object_signing\n" \
167 " ssl_ca\n" \
168 " email_ca\n" \
169 " object_signing_ca\n" \
Dave Rodgman66e05502022-10-27 16:29:38 +0100170 " format=pem|der default: pem\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000171 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000172
Valerio Setti791bbe62023-01-11 10:40:18 +0100173typedef enum {
174 SERIAL_FRMT_UNSPEC,
175 SERIAL_FRMT_DEC,
176 SERIAL_FRMT_HEX
177} serial_format_t;
Simon Butcher63cb97e2018-12-06 17:43:31 +0000178
Paul Bakker9397dcb2013-09-06 09:55:26 +0200179/*
180 * global options
181 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100182struct options {
Paul Bakker8fc30b12013-11-25 13:29:43 +0100183 const char *issuer_crt; /* filename of the issuer certificate */
184 const char *request_file; /* filename of the certificate request */
185 const char *subject_key; /* filename of the subject key file */
186 const char *issuer_key; /* filename of the issuer key file */
187 const char *subject_pwd; /* password for the subject key file */
188 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100189 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100190 const char *subject_name; /* subject name for certificate */
191 const char *issuer_name; /* issuer name for certificate */
192 const char *not_before; /* validity period not before */
193 const char *not_after; /* validity period not after */
Valerio Setti791bbe62023-01-11 10:40:18 +0100194 const char *serial; /* serial number string (decimal) */
195 const char *serial_hex; /* serial number string (hex) */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200196 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200197 int is_ca; /* is a CA certificate */
198 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100199 int authority_identifier; /* add authority identifier to CRT */
200 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100201 int basic_constraints; /* add basic constraints ext to CRT */
202 int version; /* CRT version */
203 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200204 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100205 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200206 unsigned char ns_cert_type; /* NS cert type */
Dave Rodgman66e05502022-10-27 16:29:38 +0100207 int format; /* format */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200208} opt;
209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
211 int (*f_rng)(void *, unsigned char *, size_t),
212 void *p_rng)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200213{
214 int ret;
215 FILE *f;
216 unsigned char output_buf[4096];
Dave Rodgman66e05502022-10-27 16:29:38 +0100217 unsigned char *output_start;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200218 size_t len = 0;
219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 memset(output_buf, 0, 4096);
221 if (opt.format == FORMAT_DER) {
222 ret = mbedtls_x509write_crt_der(crt, output_buf, 4096,
223 f_rng, p_rng);
224 if (ret < 0) {
225 return ret;
226 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227
Dave Rodgman66e05502022-10-27 16:29:38 +0100228 len = ret;
229 output_start = output_buf + 4096 - len;
230 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100231 ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
232 f_rng, p_rng);
233 if (ret < 0) {
234 return ret;
235 }
Dave Rodgman66e05502022-10-27 16:29:38 +0100236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 len = strlen((char *) output_buf);
Dave Rodgman66e05502022-10-27 16:29:38 +0100238 output_start = output_buf;
239 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 if ((f = fopen(output_file, "w")) == NULL) {
242 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200243 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (fwrite(output_start, 1, len, f) != len) {
246 fclose(f);
247 return -1;
248 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250 fclose(f);
251
252 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200253}
254
Valerio Setti791bbe62023-01-11 10:40:18 +0100255int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax,
256 const char *ibuf, size_t *len)
Valerio Settiacf12fb2023-01-09 17:19:26 +0100257{
Valerio Setti791bbe62023-01-11 10:40:18 +0100258 unsigned long long int dec;
259 unsigned int remaining_bytes = sizeof(dec);
260 unsigned char *p = obuf;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100261 unsigned char val;
Valerio Setti791bbe62023-01-11 10:40:18 +0100262 char *end_ptr = NULL;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100263
Valerio Setti791bbe62023-01-11 10:40:18 +0100264 errno = 0;
265 dec = strtoull(ibuf, &end_ptr, 10);
266
267 if ((errno != 0) || (end_ptr == ibuf)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100268 return -1;
269 }
270
Valerio Setti791bbe62023-01-11 10:40:18 +0100271 *len = 0;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100272
Valerio Setti791bbe62023-01-11 10:40:18 +0100273 while (remaining_bytes > 0) {
274 if (obufmax < (*len + 1)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100275 return -1;
276 }
277
Valerio Setti791bbe62023-01-11 10:40:18 +0100278 val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF;
279
280 /* Skip leading zeros */
Valerio Setti48fdbb32023-01-12 15:31:35 +0100281 if ((val != 0) || (*len != 0)) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100282 *p = val;
283 (*len)++;
284 p++;
285 }
286
287 remaining_bytes--;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100288 }
289
290 return 0;
291}
292
Gilles Peskine449bd832023-01-11 14:50:10 +0100293int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200294{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100295 int ret = 1;
296 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297 mbedtls_x509_crt issuer_crt;
298 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
299 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200301 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200302 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100303 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200304 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200306 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200308#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309 mbedtls_x509write_cert crt;
Valerio Setti791bbe62023-01-11 10:40:18 +0100310 serial_format_t serial_frmt = SERIAL_FRMT_UNSPEC;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100311 unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
312 size_t serial_len;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100313 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 mbedtls_entropy_context entropy;
315 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200316 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200317
318 /*
319 * Set to sane values
320 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 mbedtls_x509write_crt_init(&crt);
322 mbedtls_pk_init(&loaded_issuer_key);
323 mbedtls_pk_init(&loaded_subject_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 mbedtls_ctr_drbg_init(&ctr_drbg);
325 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200328#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 mbedtls_x509_crt_init(&issuer_crt);
330 memset(buf, 0, sizeof(buf));
Valerio Setti791bbe62023-01-11 10:40:18 +0100331 memset(serial, 0, sizeof(serial));
Paul Bakker9397dcb2013-09-06 09:55:26 +0200332
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000333 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100334usage:
335 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200336 goto exit;
337 }
338
Paul Bakker1014e952013-09-09 13:59:42 +0200339 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200340 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200341 opt.subject_key = DFL_SUBJECT_KEY;
342 opt.issuer_key = DFL_ISSUER_KEY;
343 opt.subject_pwd = DFL_SUBJECT_PWD;
344 opt.issuer_pwd = DFL_ISSUER_PWD;
345 opt.output_file = DFL_OUTPUT_FILENAME;
346 opt.subject_name = DFL_SUBJECT_NAME;
347 opt.issuer_name = DFL_ISSUER_NAME;
348 opt.not_before = DFL_NOT_BEFORE;
349 opt.not_after = DFL_NOT_AFTER;
350 opt.serial = DFL_SERIAL;
Valerio Setti791bbe62023-01-11 10:40:18 +0100351 opt.serial_hex = DFL_SERIAL_HEX;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200352 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200353 opt.is_ca = DFL_IS_CA;
354 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200355 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100356 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200357 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100358 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100359 opt.md = DFL_DIGEST;
360 opt.subject_identifier = DFL_SUBJ_IDENT;
361 opt.authority_identifier = DFL_AUTH_IDENT;
362 opt.basic_constraints = DFL_CONSTRAINTS;
Dave Rodgman66e05502022-10-27 16:29:38 +0100363 opt.format = DFL_FORMAT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200366
367 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200369 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200371 *q++ = '\0';
372
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200374 opt.request_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200376 opt.subject_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200378 opt.issuer_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200380 opt.subject_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200382 opt.issuer_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100383 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200384 opt.issuer_crt = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200386 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200388 opt.subject_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200390 opt.issuer_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100391 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200392 opt.not_before = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200394 opt.not_after = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 } else if (strcmp(p, "serial") == 0) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100396 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
397 mbedtls_printf("Invalid attempt to set the serial more than once\n");
398 goto usage;
399 }
400 serial_frmt = SERIAL_FRMT_DEC;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200401 opt.serial = q;
Valerio Setti791bbe62023-01-11 10:40:18 +0100402 } else if (strcmp(p, "serial_hex") == 0) {
403 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
404 mbedtls_printf("Invalid attempt to set the serial more than once\n");
405 goto usage;
406 }
407 serial_frmt = SERIAL_FRMT_HEX;
408 opt.serial_hex = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 } else if (strcmp(p, "authority_identifier") == 0) {
410 opt.authority_identifier = atoi(q);
411 if (opt.authority_identifier != 0 &&
412 opt.authority_identifier != 1) {
413 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100414 goto usage;
415 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 } else if (strcmp(p, "subject_identifier") == 0) {
417 opt.subject_identifier = atoi(q);
418 if (opt.subject_identifier != 0 &&
419 opt.subject_identifier != 1) {
420 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100421 goto usage;
422 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 } else if (strcmp(p, "basic_constraints") == 0) {
424 opt.basic_constraints = atoi(q);
425 if (opt.basic_constraints != 0 &&
426 opt.basic_constraints != 1) {
427 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100428 goto usage;
429 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200431 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 mbedtls_md_info_from_string(q);
433 if (md_info == NULL) {
434 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100435 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100436 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 opt.md = mbedtls_md_get_type(md_info);
438 } else if (strcmp(p, "version") == 0) {
439 opt.version = atoi(q);
440 if (opt.version < 1 || opt.version > 3) {
441 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100442 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100443 }
Hanno Becker38eff432017-09-22 15:38:20 +0100444 opt.version--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 } else if (strcmp(p, "selfsign") == 0) {
446 opt.selfsign = atoi(q);
447 if (opt.selfsign < 0 || opt.selfsign > 1) {
448 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200449 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100450 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 } else if (strcmp(p, "is_ca") == 0) {
452 opt.is_ca = atoi(q);
453 if (opt.is_ca < 0 || opt.is_ca > 1) {
454 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200455 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100456 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 } else if (strcmp(p, "max_pathlen") == 0) {
458 opt.max_pathlen = atoi(q);
459 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
460 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200461 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100462 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 } else if (strcmp(p, "key_usage") == 0) {
464 while (q != NULL) {
465 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200466 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200468
Gilles Peskine449bd832023-01-11 14:50:10 +0100469 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100474 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100476 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100478 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200482 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 } else {
484 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200485 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100486 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200487
488 q = r;
489 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 } else if (strcmp(p, "ext_key_usage") == 0) {
Dave Rodgman64937852022-08-15 14:12:25 +0100491 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 while (q != NULL) {
494 if ((r = strchr(q, ',')) != NULL) {
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100495 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100499 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 if (strcmp(q, "serverAuth") == 0) {
501 SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
502 } else if (strcmp(q, "clientAuth") == 0) {
503 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
504 } else if (strcmp(q, "codeSigning") == 0) {
505 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
506 } else if (strcmp(q, "emailProtection") == 0) {
507 SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
508 } else if (strcmp(q, "timeStamping") == 0) {
509 SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
510 } else if (strcmp(q, "OCSPSigning") == 0) {
511 SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
512 } else {
513 mbedtls_printf("Invalid argument for option %s\n", p);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100514 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100515 }
Dave Rodgman64937852022-08-15 14:12:25 +0100516
517 *tail = ext_key_usage;
518 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100519
520 q = r;
521 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 } else if (strcmp(p, "ns_cert_type") == 0) {
523 while (q != NULL) {
524 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200525 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100531 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100536 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100537 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100539 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100541 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 } else {
543 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200544 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100545 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200546
547 q = r;
548 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 } else if (strcmp(p, "format") == 0) {
550 if (strcmp(q, "der") == 0) {
551 opt.format = FORMAT_DER;
552 } else if (strcmp(q, "pem") == 0) {
553 opt.format = FORMAT_PEM;
554 } else {
555 mbedtls_printf("Invalid argument for option %s\n", p);
Dave Rodgman66e05502022-10-27 16:29:38 +0100556 goto usage;
557 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200559 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100560 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200561 }
562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200563 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200564
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200565 /*
566 * 0. Seed the PRNG
567 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 mbedtls_printf(" . Seeding the random number generator...");
569 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
572 (const unsigned char *) pers,
573 strlen(pers))) != 0) {
574 mbedtls_strerror(ret, buf, sizeof(buf));
575 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
576 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200577 goto exit;
578 }
579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200581
Paul Bakker9397dcb2013-09-06 09:55:26 +0200582 // Parse serial to MPI
583 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100584 mbedtls_printf(" . Reading serial number...");
585 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200586
Valerio Setti791bbe62023-01-11 10:40:18 +0100587 if (serial_frmt == SERIAL_FRMT_HEX) {
588 ret = mbedtls_test_unhexify(serial, sizeof(serial),
589 opt.serial_hex, &serial_len);
590 } else { // SERIAL_FRMT_DEC || SERIAL_FRMT_UNSPEC
591 ret = parse_serial_decimal_format(serial, sizeof(serial),
592 opt.serial, &serial_len);
593 }
594
595 if (ret != 0) {
596 mbedtls_printf(" failed\n ! Unable to parse serial\n");
597 goto exit;
598 }
599
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200601
Paul Bakker1014e952013-09-09 13:59:42 +0200602 // Parse issuer certificate if present
603 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200605 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200606 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200607 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 mbedtls_printf(" . Loading the issuer certificate ...");
609 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
612 mbedtls_strerror(ret, buf, sizeof(buf));
613 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
614 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200615 goto exit;
616 }
617
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
619 &issuer_crt.subject);
620 if (ret < 0) {
621 mbedtls_strerror(ret, buf, sizeof(buf));
622 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
623 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200624 goto exit;
625 }
626
Paul Bakkere2673fb2013-09-09 15:52:07 +0200627 opt.issuer_name = issuer_name;
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200630 }
631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200633 // Parse certificate request if present
634 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200636 /*
637 * 1.0.b. Load the CSR
638 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 mbedtls_printf(" . Loading the certificate request ...");
640 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
643 mbedtls_strerror(ret, buf, sizeof(buf));
644 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
645 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200646 goto exit;
647 }
648
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
650 &csr.subject);
651 if (ret < 0) {
652 mbedtls_strerror(ret, buf, sizeof(buf));
653 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
654 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200655 goto exit;
656 }
657
658 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200659 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200662 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200664
665 /*
666 * 1.1. Load the keys
667 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100668 if (!opt.selfsign && !strlen(opt.request_file)) {
669 mbedtls_printf(" . Loading the subject key ...");
670 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
673 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
674 if (ret != 0) {
675 mbedtls_strerror(ret, buf, sizeof(buf));
676 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
677 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200678 goto exit;
679 }
Paul Bakker1014e952013-09-09 13:59:42 +0200680
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200682 }
683
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 mbedtls_printf(" . Loading the issuer key ...");
685 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
688 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
689 if (ret != 0) {
690 mbedtls_strerror(ret, buf, sizeof(buf));
691 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
692 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200693 goto exit;
694 }
695
696 // Check if key and issuer certificate match
697 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 if (strlen(opt.issuer_crt)) {
699 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key,
700 mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
701 mbedtls_printf(" failed\n ! issuer_key does not match "
702 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200703 goto exit;
704 }
705 }
706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200708
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100710 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200711 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200712 }
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
715 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200716
Paul Bakker9397dcb2013-09-06 09:55:26 +0200717 /*
718 * 1.0. Check the names for validity
719 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
721 mbedtls_strerror(ret, buf, sizeof(buf));
722 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
723 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200724 goto exit;
725 }
726
Gilles Peskine449bd832023-01-11 14:50:10 +0100727 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
728 mbedtls_strerror(ret, buf, sizeof(buf));
729 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
730 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200731 goto exit;
732 }
733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 mbedtls_printf(" . Setting certificate values ...");
735 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200736
Gilles Peskine449bd832023-01-11 14:50:10 +0100737 mbedtls_x509write_crt_set_version(&crt, opt.version);
738 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100739
Valerio Settiaf4815c2023-01-26 17:43:09 +0100740 ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100741 if (ret != 0) {
742 mbedtls_strerror(ret, buf, sizeof(buf));
Valerio Settiaf4815c2023-01-26 17:43:09 +0100743 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_raw "
Valerio Settida0afcc2023-01-05 16:46:59 +0100744 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
745 goto exit;
746 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
749 if (ret != 0) {
750 mbedtls_strerror(ret, buf, sizeof(buf));
751 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
752 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200753 goto exit;
754 }
755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200757
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
759 opt.basic_constraints != 0) {
760 mbedtls_printf(" . Adding the Basic Constraints extension ...");
761 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200762
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
764 opt.max_pathlen);
765 if (ret != 0) {
766 mbedtls_strerror(ret, buf, sizeof(buf));
767 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
768 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100769 goto exit;
770 }
771
Gilles Peskine449bd832023-01-11 14:50:10 +0100772 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100773 }
Paul Bakker15162a02013-09-06 19:27:21 +0200774
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100775#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
777 opt.subject_identifier != 0) {
778 mbedtls_printf(" . Adding the Subject Key Identifier ...");
779 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
782 if (ret != 0) {
783 mbedtls_strerror(ret, buf, sizeof(buf));
784 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
785 "_key_identifier returned -0x%04x - %s\n\n",
786 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100787 goto exit;
788 }
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200791 }
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
794 opt.authority_identifier != 0) {
795 mbedtls_printf(" . Adding the Authority Key Identifier ...");
796 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
799 if (ret != 0) {
800 mbedtls_strerror(ret, buf, sizeof(buf));
801 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
802 "key_identifier returned -0x%04x - %s\n\n",
803 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100804 goto exit;
805 }
806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100808 }
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100809#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker15162a02013-09-06 19:27:21 +0200810
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
812 opt.key_usage != 0) {
813 mbedtls_printf(" . Adding the Key Usage extension ...");
814 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200815
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
817 if (ret != 0) {
818 mbedtls_strerror(ret, buf, sizeof(buf));
819 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
820 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200821 goto exit;
822 }
823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200825 }
826
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 if (opt.ext_key_usage) {
828 mbedtls_printf(" . Adding the Extended Key Usage extension ...");
829 fflush(stdout);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100830
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
832 if (ret != 0) {
833 mbedtls_strerror(ret, buf, sizeof(buf));
834 mbedtls_printf(
835 " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
836 (unsigned int) -ret,
837 buf);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100838 goto exit;
839 }
840
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 mbedtls_printf(" ok\n");
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100842 }
843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
845 opt.ns_cert_type != 0) {
846 mbedtls_printf(" . Adding the NS Cert Type extension ...");
847 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200848
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
850 if (ret != 0) {
851 mbedtls_strerror(ret, buf, sizeof(buf));
852 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
853 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200854 goto exit;
855 }
856
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200858 }
859
Paul Bakker9397dcb2013-09-06 09:55:26 +0200860 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100861 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200862 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100863 mbedtls_printf(" . Writing the certificate...");
864 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200865
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 if ((ret = write_certificate(&crt, opt.output_file,
867 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
868 mbedtls_strerror(ret, buf, sizeof(buf));
869 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
870 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200871 goto exit;
872 }
873
Gilles Peskine449bd832023-01-11 14:50:10 +0100874 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200875
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100876 exit_code = MBEDTLS_EXIT_SUCCESS;
877
Paul Bakker9397dcb2013-09-06 09:55:26 +0200878exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100879#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +0100881#endif /* MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100882 mbedtls_x509_crt_free(&issuer_crt);
883 mbedtls_x509write_crt_free(&crt);
884 mbedtls_pk_free(&loaded_subject_key);
885 mbedtls_pk_free(&loaded_issuer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 mbedtls_ctr_drbg_free(&ctr_drbg);
887 mbedtls_entropy_free(&entropy);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200890}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200891#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
892 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100893 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */