blob: ac6187a1982512afeb72f77dd764dd6fb0ee18b1 [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 Kurek0af32482023-04-07 03:10:28 -040023/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040024#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000025
Simon Butcher203a6932016-10-07 15:00:17 +010026#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
27 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
28 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010029 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
Simon Butcher203a6932016-10-07 15:00:17 +010030 !defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010031int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020032{
Gilles Peskine449bd832023-01-11 14:50:10 +010033 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 +010034 "MBEDTLS_FS_IO and/or MBEDTLS_MD_CAN_SHA256 and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010035 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
36 "MBEDTLS_ERROR_C not defined.\n");
37 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020038}
39#else
40
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/x509_crt.h"
42#include "mbedtls/x509_csr.h"
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010043#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000044#include "mbedtls/entropy.h"
45#include "mbedtls/ctr_drbg.h"
46#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"
Andrzej Kurekccdd9752023-04-01 10:38:30 -040082#define DFL_EXT_SUBJECTALTNAME ""
Paul Bakkerb2d7f232013-09-09 16:24:18 +020083#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020084#define DFL_IS_CA 0
85#define DFL_MAX_PATHLEN -1
Nicholas Wilson99a96b12015-09-10 18:28:01 +010086#define DFL_SIG_ALG MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020087#define DFL_KEY_USAGE 0
Dave Rodgman1577c542022-09-09 10:22:15 +010088#define DFL_EXT_KEY_USAGE NULL
Paul Bakker9397dcb2013-09-06 09:55:26 +020089#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010090#define DFL_VERSION 3
91#define DFL_AUTH_IDENT 1
92#define DFL_SUBJ_IDENT 1
93#define DFL_CONSTRAINTS 1
94#define DFL_DIGEST MBEDTLS_MD_SHA256
Dave Rodgman66e05502022-10-27 16:29:38 +010095#define DFL_FORMAT FORMAT_PEM
Paul Bakker9397dcb2013-09-06 09:55:26 +020096
Rich Evans18b78c72015-02-11 14:06:19 +000097#define USAGE \
98 "\n usage: cert_write param=<>...\n" \
99 "\n acceptable parameters:\n" \
100 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +0100101 " subject_key=%%s default: subject.key\n" \
102 " subject_pwd=%%s default: (empty)\n" \
103 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000104 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100105 " issuer_crt=%%s default: (empty)\n" \
106 " If issuer_crt is specified, issuer_name is\n" \
107 " ignored!\n" \
108 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000109 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100110 " selfsign=%%d default: 0 (false)\n" \
111 " If selfsign is enabled, issuer_name and\n" \
112 " issuer_key are required (issuer_crt and\n" \
113 " subject_* are ignored\n" \
114 " issuer_key=%%s default: ca.key\n" \
115 " issuer_pwd=%%s default: (empty)\n" \
116 " output_file=%%s default: cert.crt\n" \
117 " serial=%%s default: 1\n" \
Valerio Setti791bbe62023-01-11 10:40:18 +0100118 " In decimal format; it can be used as\n" \
119 " alternative to serial_hex, but it's\n" \
120 " limited in max length to\n" \
121 " unsigned long long int\n" \
122 " serial_hex=%%s default: 1\n" \
123 " In hex format; it can be used as\n" \
124 " alternative to serial\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 " not_before=%%s default: 20010101000000\n" \
126 " not_after=%%s default: 20301231235959\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100127 " is_ca=%%d default: 0 (disabled)\n" \
128 " max_pathlen=%%d default: -1 (none)\n" \
129 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200130 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200131 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200132 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100133 " version=%%d default: 3\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 " Possible values: 1, 2, 3\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100135 " subject_identifier=%%s default: 1\n" \
136 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 " (Considered for v3 only)\n" \
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400138 " san=%%s default: (none)\n" \
139 " Comma-separated-list of values:\n" \
140 " DNS:value\n" \
141 " URI:value\n" \
142 " RFC822:value\n" \
143 " IP:value (Only IPv4 is supported)\n" \
144 " DN:value\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100145 " authority_identifier=%%s default: 1\n" \
146 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100148 " basic_constraints=%%d default: 1\n" \
149 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100151 " key_usage=%%s default: (empty)\n" \
152 " Comma-separated-list of values:\n" \
153 " digital_signature\n" \
154 " non_repudiation\n" \
155 " key_encipherment\n" \
156 " data_encipherment\n" \
157 " key_agreement\n" \
158 " key_cert_sign\n" \
159 " crl_sign\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 " (Considered for v3 only)\n" \
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100161 " ext_key_usage=%%s default: (empty)\n" \
162 " Comma-separated-list of values:\n" \
163 " serverAuth\n" \
164 " clientAuth\n" \
165 " codeSigning\n" \
166 " emailProtection\n" \
167 " timeStamping\n" \
168 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100169 " ns_cert_type=%%s default: (empty)\n" \
170 " Comma-separated-list of values:\n" \
171 " ssl_client\n" \
172 " ssl_server\n" \
173 " email\n" \
174 " object_signing\n" \
175 " ssl_ca\n" \
176 " email_ca\n" \
177 " object_signing_ca\n" \
Dave Rodgman66e05502022-10-27 16:29:38 +0100178 " format=pem|der default: pem\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000179 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000180
Valerio Setti791bbe62023-01-11 10:40:18 +0100181typedef enum {
182 SERIAL_FRMT_UNSPEC,
183 SERIAL_FRMT_DEC,
184 SERIAL_FRMT_HEX
185} serial_format_t;
Simon Butcher63cb97e2018-12-06 17:43:31 +0000186
Paul Bakker9397dcb2013-09-06 09:55:26 +0200187/*
188 * global options
189 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100190struct options {
Paul Bakker8fc30b12013-11-25 13:29:43 +0100191 const char *issuer_crt; /* filename of the issuer certificate */
192 const char *request_file; /* filename of the certificate request */
193 const char *subject_key; /* filename of the subject key file */
194 const char *issuer_key; /* filename of the issuer key file */
195 const char *subject_pwd; /* password for the subject key file */
196 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100197 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100198 const char *subject_name; /* subject name for certificate */
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400199 mbedtls_x509_san_list *san_list; /* subjectAltName for certificate */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100200 const char *issuer_name; /* issuer name for certificate */
201 const char *not_before; /* validity period not before */
202 const char *not_after; /* validity period not after */
Valerio Setti791bbe62023-01-11 10:40:18 +0100203 const char *serial; /* serial number string (decimal) */
204 const char *serial_hex; /* serial number string (hex) */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200205 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200206 int is_ca; /* is a CA certificate */
207 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100208 int authority_identifier; /* add authority identifier to CRT */
209 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100210 int basic_constraints; /* add basic constraints ext to CRT */
211 int version; /* CRT version */
212 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200213 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100214 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200215 unsigned char ns_cert_type; /* NS cert type */
Dave Rodgman66e05502022-10-27 16:29:38 +0100216 int format; /* format */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200217} opt;
218
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400219static int ip_string_to_bytes(const char *str, uint8_t *bytes, int maxBytes)
220{
221 for (int i = 0; i < maxBytes; i++) {
222 bytes[i] = strtoul(str, NULL, 16);
223 str = strchr(str, '.');
224 if (str == NULL || *str == '\0') {
225 break;
226 }
227 str++;
228 }
229 return 0;
230}
231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
233 int (*f_rng)(void *, unsigned char *, size_t),
234 void *p_rng)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200235{
236 int ret;
237 FILE *f;
238 unsigned char output_buf[4096];
Dave Rodgman66e05502022-10-27 16:29:38 +0100239 unsigned char *output_start;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200240 size_t len = 0;
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 memset(output_buf, 0, 4096);
243 if (opt.format == FORMAT_DER) {
244 ret = mbedtls_x509write_crt_der(crt, output_buf, 4096,
245 f_rng, p_rng);
246 if (ret < 0) {
247 return ret;
248 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249
Dave Rodgman66e05502022-10-27 16:29:38 +0100250 len = ret;
251 output_start = output_buf + 4096 - len;
252 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
254 f_rng, p_rng);
255 if (ret < 0) {
256 return ret;
257 }
Dave Rodgman66e05502022-10-27 16:29:38 +0100258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 len = strlen((char *) output_buf);
Dave Rodgman66e05502022-10-27 16:29:38 +0100260 output_start = output_buf;
261 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if ((f = fopen(output_file, "w")) == NULL) {
264 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200265 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if (fwrite(output_start, 1, len, f) != len) {
268 fclose(f);
269 return -1;
270 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200271
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 fclose(f);
273
274 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200275}
276
Valerio Setti791bbe62023-01-11 10:40:18 +0100277int parse_serial_decimal_format(unsigned char *obuf, size_t obufmax,
278 const char *ibuf, size_t *len)
Valerio Settiacf12fb2023-01-09 17:19:26 +0100279{
Valerio Setti791bbe62023-01-11 10:40:18 +0100280 unsigned long long int dec;
281 unsigned int remaining_bytes = sizeof(dec);
282 unsigned char *p = obuf;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100283 unsigned char val;
Valerio Setti791bbe62023-01-11 10:40:18 +0100284 char *end_ptr = NULL;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100285
Valerio Setti791bbe62023-01-11 10:40:18 +0100286 errno = 0;
287 dec = strtoull(ibuf, &end_ptr, 10);
288
289 if ((errno != 0) || (end_ptr == ibuf)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100290 return -1;
291 }
292
Valerio Setti791bbe62023-01-11 10:40:18 +0100293 *len = 0;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100294
Valerio Setti791bbe62023-01-11 10:40:18 +0100295 while (remaining_bytes > 0) {
296 if (obufmax < (*len + 1)) {
Valerio Settiacf12fb2023-01-09 17:19:26 +0100297 return -1;
298 }
299
Valerio Setti791bbe62023-01-11 10:40:18 +0100300 val = (dec >> ((remaining_bytes - 1) * 8)) & 0xFF;
301
302 /* Skip leading zeros */
Valerio Setti48fdbb32023-01-12 15:31:35 +0100303 if ((val != 0) || (*len != 0)) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100304 *p = val;
305 (*len)++;
306 p++;
307 }
308
309 remaining_bytes--;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100310 }
311
312 return 0;
313}
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200316{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100317 int ret = 1;
318 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200319 mbedtls_x509_crt issuer_crt;
320 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
321 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200323 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200324 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100325 int i;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400326 char *p, *q, *r, *r2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200328 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200330#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 mbedtls_x509write_cert crt;
Valerio Setti791bbe62023-01-11 10:40:18 +0100332 serial_format_t serial_frmt = SERIAL_FRMT_UNSPEC;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100333 unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
334 size_t serial_len;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100335 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 mbedtls_entropy_context entropy;
337 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200338 const char *pers = "crt example app";
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400339 mbedtls_x509_san_list *cur, *prev;
340 uint8_t ip[4] = { 0 };
Paul Bakker9397dcb2013-09-06 09:55:26 +0200341 /*
342 * Set to sane values
343 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 mbedtls_x509write_crt_init(&crt);
345 mbedtls_pk_init(&loaded_issuer_key);
346 mbedtls_pk_init(&loaded_subject_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 mbedtls_ctr_drbg_init(&ctr_drbg);
348 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100350 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200351#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 mbedtls_x509_crt_init(&issuer_crt);
353 memset(buf, 0, sizeof(buf));
Valerio Setti791bbe62023-01-11 10:40:18 +0100354 memset(serial, 0, sizeof(serial));
Paul Bakker9397dcb2013-09-06 09:55:26 +0200355
Przemek Stekiela0a1c1e2023-04-17 11:10:05 +0200356#if defined(MBEDTLS_USE_PSA_CRYPTO)
357 psa_status_t status = psa_crypto_init();
358 if (status != PSA_SUCCESS) {
359 mbedtls_fprintf(stderr, "Failed to initialize PSA Crypto implementation: %d\n",
360 (int) status);
361 goto exit;
362 }
363#endif /* MBEDTLS_USE_PSA_CRYPTO */
364
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000365 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100366usage:
367 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200368 goto exit;
369 }
370
Paul Bakker1014e952013-09-09 13:59:42 +0200371 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200372 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200373 opt.subject_key = DFL_SUBJECT_KEY;
374 opt.issuer_key = DFL_ISSUER_KEY;
375 opt.subject_pwd = DFL_SUBJECT_PWD;
376 opt.issuer_pwd = DFL_ISSUER_PWD;
377 opt.output_file = DFL_OUTPUT_FILENAME;
378 opt.subject_name = DFL_SUBJECT_NAME;
379 opt.issuer_name = DFL_ISSUER_NAME;
380 opt.not_before = DFL_NOT_BEFORE;
381 opt.not_after = DFL_NOT_AFTER;
382 opt.serial = DFL_SERIAL;
Valerio Setti791bbe62023-01-11 10:40:18 +0100383 opt.serial_hex = DFL_SERIAL_HEX;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200384 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200385 opt.is_ca = DFL_IS_CA;
386 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200387 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100388 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200389 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100390 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100391 opt.md = DFL_DIGEST;
392 opt.subject_identifier = DFL_SUBJ_IDENT;
393 opt.authority_identifier = DFL_AUTH_IDENT;
394 opt.basic_constraints = DFL_CONSTRAINTS;
Dave Rodgman66e05502022-10-27 16:29:38 +0100395 opt.format = DFL_FORMAT;
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400396 opt.san_list = NULL;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200397
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200399
400 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200402 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200404 *q++ = '\0';
405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200407 opt.request_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200409 opt.subject_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200411 opt.issuer_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200413 opt.subject_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200415 opt.issuer_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200417 opt.issuer_crt = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200419 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200421 opt.subject_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200423 opt.issuer_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200425 opt.not_before = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200427 opt.not_after = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 } else if (strcmp(p, "serial") == 0) {
Valerio Setti791bbe62023-01-11 10:40:18 +0100429 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
430 mbedtls_printf("Invalid attempt to set the serial more than once\n");
431 goto usage;
432 }
433 serial_frmt = SERIAL_FRMT_DEC;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200434 opt.serial = q;
Valerio Setti791bbe62023-01-11 10:40:18 +0100435 } else if (strcmp(p, "serial_hex") == 0) {
436 if (serial_frmt != SERIAL_FRMT_UNSPEC) {
437 mbedtls_printf("Invalid attempt to set the serial more than once\n");
438 goto usage;
439 }
440 serial_frmt = SERIAL_FRMT_HEX;
441 opt.serial_hex = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100442 } else if (strcmp(p, "authority_identifier") == 0) {
443 opt.authority_identifier = atoi(q);
444 if (opt.authority_identifier != 0 &&
445 opt.authority_identifier != 1) {
446 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100447 goto usage;
448 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 } else if (strcmp(p, "subject_identifier") == 0) {
450 opt.subject_identifier = atoi(q);
451 if (opt.subject_identifier != 0 &&
452 opt.subject_identifier != 1) {
453 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100454 goto usage;
455 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } else if (strcmp(p, "basic_constraints") == 0) {
457 opt.basic_constraints = atoi(q);
458 if (opt.basic_constraints != 0 &&
459 opt.basic_constraints != 1) {
460 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100461 goto usage;
462 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200464 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 mbedtls_md_info_from_string(q);
466 if (md_info == NULL) {
467 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100468 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100469 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 opt.md = mbedtls_md_get_type(md_info);
471 } else if (strcmp(p, "version") == 0) {
472 opt.version = atoi(q);
473 if (opt.version < 1 || opt.version > 3) {
474 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100475 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100476 }
Hanno Becker38eff432017-09-22 15:38:20 +0100477 opt.version--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 } else if (strcmp(p, "selfsign") == 0) {
479 opt.selfsign = atoi(q);
480 if (opt.selfsign < 0 || opt.selfsign > 1) {
481 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200482 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100483 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 } else if (strcmp(p, "is_ca") == 0) {
485 opt.is_ca = atoi(q);
486 if (opt.is_ca < 0 || opt.is_ca > 1) {
487 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200488 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100489 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 } else if (strcmp(p, "max_pathlen") == 0) {
491 opt.max_pathlen = atoi(q);
492 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
493 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200494 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100495 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 } else if (strcmp(p, "key_usage") == 0) {
497 while (q != NULL) {
498 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200499 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200501
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200503 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100507 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100509 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100511 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200515 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 } else {
517 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200518 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100519 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200520
521 q = r;
522 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 } else if (strcmp(p, "ext_key_usage") == 0) {
Dave Rodgman64937852022-08-15 14:12:25 +0100524 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
525
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 while (q != NULL) {
527 if ((r = strchr(q, ',')) != NULL) {
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100528 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100530
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100532 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100533 if (strcmp(q, "serverAuth") == 0) {
534 SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
535 } else if (strcmp(q, "clientAuth") == 0) {
536 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
537 } else if (strcmp(q, "codeSigning") == 0) {
538 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
539 } else if (strcmp(q, "emailProtection") == 0) {
540 SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
541 } else if (strcmp(q, "timeStamping") == 0) {
542 SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
543 } else if (strcmp(q, "OCSPSigning") == 0) {
544 SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
545 } else {
546 mbedtls_printf("Invalid argument for option %s\n", p);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100547 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100548 }
Dave Rodgman64937852022-08-15 14:12:25 +0100549
550 *tail = ext_key_usage;
551 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100552
553 q = r;
554 }
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400555 } else if (strcmp(p, "san") == 0) {
556 prev = NULL;
557
558 while (q != NULL) {
559 if ((r = strchr(q, ';')) != NULL) {
560 *r++ = '\0';
561 }
562
563 cur = mbedtls_calloc(1, sizeof(mbedtls_x509_san_list));
564 if (cur == NULL) {
565 mbedtls_printf("Not enough memory for subjectAltName list\n");
566 goto usage;
567 }
568
569 cur->next = NULL;
570
571 if ((r2 = strchr(q, ':')) != NULL) {
572 *r2++ = '\0';
573 }
574 if (strcmp(q, "RFC822") == 0) {
575 cur->node.type = MBEDTLS_X509_SAN_RFC822_NAME;
576 } else if (strcmp(q, "URI") == 0) {
577 cur->node.type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
578 } else if (strcmp(q, "DNS") == 0) {
579 cur->node.type = MBEDTLS_X509_SAN_DNS_NAME;
580 } else if (strcmp(q, "IP") == 0) {
581 cur->node.type = MBEDTLS_X509_SAN_IP_ADDRESS;
582 ip_string_to_bytes(r2, ip, 4);
583 cur->node.san.unstructured_name.p = (unsigned char *) ip;
584 cur->node.san.unstructured_name.len = sizeof(ip);
585 } else if (strcmp(q, "DN") == 0) {
586 mbedtls_asn1_named_data *ext_san_dirname = NULL;
587 cur->node.type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
588 if ((ret = mbedtls_x509_string_to_names(&ext_san_dirname,
589 r2)) != 0) {
590 mbedtls_strerror(ret, buf, sizeof(buf));
591 mbedtls_printf(
592 " failed\n ! mbedtls_x509_string_to_names "
593 "returned -0x%04x - %s\n\n",
594 (unsigned int) -ret, buf);
595 goto exit;
596 }
597 cur->node.san.directory_name = *ext_san_dirname;
598 } else {
599 mbedtls_free(cur);
600 goto usage;
601 }
602
603 if (strcmp(q, "IP") != 0 && strcmp(q, "DN") != 0) {
604 q = r2;
605 cur->node.san.unstructured_name.p = (unsigned char *) q;
606 cur->node.san.unstructured_name.len = strlen(q);
607 }
608
609 if (prev == NULL) {
610 opt.san_list = cur;
611 } else {
612 prev->next = cur;
613 }
614
615 prev = cur;
616 q = r;
617 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 } else if (strcmp(p, "ns_cert_type") == 0) {
619 while (q != NULL) {
620 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200621 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200623
Gilles Peskine449bd832023-01-11 14:50:10 +0100624 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100627 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100633 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100635 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100637 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 } else {
639 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200640 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100641 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200642
643 q = r;
644 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100645 } else if (strcmp(p, "format") == 0) {
646 if (strcmp(q, "der") == 0) {
647 opt.format = FORMAT_DER;
648 } else if (strcmp(q, "pem") == 0) {
649 opt.format = FORMAT_PEM;
650 } else {
651 mbedtls_printf("Invalid argument for option %s\n", p);
Dave Rodgman66e05502022-10-27 16:29:38 +0100652 goto usage;
653 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200655 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100656 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200657 }
658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200659 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200660
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200661 /*
662 * 0. Seed the PRNG
663 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 mbedtls_printf(" . Seeding the random number generator...");
665 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
668 (const unsigned char *) pers,
669 strlen(pers))) != 0) {
670 mbedtls_strerror(ret, buf, sizeof(buf));
671 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
672 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200673 goto exit;
674 }
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200677
Paul Bakker9397dcb2013-09-06 09:55:26 +0200678 // Parse serial to MPI
679 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 mbedtls_printf(" . Reading serial number...");
681 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200682
Valerio Setti791bbe62023-01-11 10:40:18 +0100683 if (serial_frmt == SERIAL_FRMT_HEX) {
684 ret = mbedtls_test_unhexify(serial, sizeof(serial),
685 opt.serial_hex, &serial_len);
686 } else { // SERIAL_FRMT_DEC || SERIAL_FRMT_UNSPEC
687 ret = parse_serial_decimal_format(serial, sizeof(serial),
688 opt.serial, &serial_len);
689 }
690
691 if (ret != 0) {
692 mbedtls_printf(" failed\n ! Unable to parse serial\n");
693 goto exit;
694 }
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200697
Paul Bakker1014e952013-09-09 13:59:42 +0200698 // Parse issuer certificate if present
699 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200701 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200702 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200703 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100704 mbedtls_printf(" . Loading the issuer certificate ...");
705 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200706
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
708 mbedtls_strerror(ret, buf, sizeof(buf));
709 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
710 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200711 goto exit;
712 }
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
715 &issuer_crt.subject);
716 if (ret < 0) {
717 mbedtls_strerror(ret, buf, sizeof(buf));
718 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
719 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200720 goto exit;
721 }
722
Paul Bakkere2673fb2013-09-09 15:52:07 +0200723 opt.issuer_name = issuer_name;
724
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200726 }
727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200728#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200729 // Parse certificate request if present
730 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200732 /*
733 * 1.0.b. Load the CSR
734 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 mbedtls_printf(" . Loading the certificate request ...");
736 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
739 mbedtls_strerror(ret, buf, sizeof(buf));
740 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
741 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200742 goto exit;
743 }
744
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
746 &csr.subject);
747 if (ret < 0) {
748 mbedtls_strerror(ret, buf, sizeof(buf));
749 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
750 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200751 goto exit;
752 }
753
754 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200755 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200756
Gilles Peskine449bd832023-01-11 14:50:10 +0100757 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200758 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200760
761 /*
762 * 1.1. Load the keys
763 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100764 if (!opt.selfsign && !strlen(opt.request_file)) {
765 mbedtls_printf(" . Loading the subject key ...");
766 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
769 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
770 if (ret != 0) {
771 mbedtls_strerror(ret, buf, sizeof(buf));
772 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
773 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200774 goto exit;
775 }
Paul Bakker1014e952013-09-09 13:59:42 +0200776
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200778 }
779
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 mbedtls_printf(" . Loading the issuer key ...");
781 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
784 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
785 if (ret != 0) {
786 mbedtls_strerror(ret, buf, sizeof(buf));
787 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
788 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200789 goto exit;
790 }
791
792 // Check if key and issuer certificate match
793 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (strlen(opt.issuer_crt)) {
795 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key,
796 mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
797 mbedtls_printf(" failed\n ! issuer_key does not match "
798 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200799 goto exit;
800 }
801 }
802
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100806 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200807 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200808 }
809
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
811 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200812
Paul Bakker9397dcb2013-09-06 09:55:26 +0200813 /*
814 * 1.0. Check the names for validity
815 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100816 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
817 mbedtls_strerror(ret, buf, sizeof(buf));
818 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
819 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200820 goto exit;
821 }
822
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
824 mbedtls_strerror(ret, buf, sizeof(buf));
825 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
826 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200827 goto exit;
828 }
829
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 mbedtls_printf(" . Setting certificate values ...");
831 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200832
Gilles Peskine449bd832023-01-11 14:50:10 +0100833 mbedtls_x509write_crt_set_version(&crt, opt.version);
834 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100835
Valerio Settiaf4815c2023-01-26 17:43:09 +0100836 ret = mbedtls_x509write_crt_set_serial_raw(&crt, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100837 if (ret != 0) {
838 mbedtls_strerror(ret, buf, sizeof(buf));
Valerio Settiaf4815c2023-01-26 17:43:09 +0100839 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_raw "
Valerio Settida0afcc2023-01-05 16:46:59 +0100840 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
841 goto exit;
842 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200843
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
845 if (ret != 0) {
846 mbedtls_strerror(ret, buf, sizeof(buf));
847 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
848 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200849 goto exit;
850 }
851
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200853
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
855 opt.basic_constraints != 0) {
856 mbedtls_printf(" . Adding the Basic Constraints extension ...");
857 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200858
Gilles Peskine449bd832023-01-11 14:50:10 +0100859 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
860 opt.max_pathlen);
861 if (ret != 0) {
862 mbedtls_strerror(ret, buf, sizeof(buf));
863 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
864 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100865 goto exit;
866 }
867
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100869 }
Paul Bakker15162a02013-09-06 19:27:21 +0200870
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100871#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100872 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
873 opt.subject_identifier != 0) {
874 mbedtls_printf(" . Adding the Subject Key Identifier ...");
875 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100876
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
878 if (ret != 0) {
879 mbedtls_strerror(ret, buf, sizeof(buf));
880 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
881 "_key_identifier returned -0x%04x - %s\n\n",
882 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100883 goto exit;
884 }
885
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200887 }
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
890 opt.authority_identifier != 0) {
891 mbedtls_printf(" . Adding the Authority Key Identifier ...");
892 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200893
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
895 if (ret != 0) {
896 mbedtls_strerror(ret, buf, sizeof(buf));
897 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
898 "key_identifier returned -0x%04x - %s\n\n",
899 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100900 goto exit;
901 }
902
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100904 }
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100905#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker15162a02013-09-06 19:27:21 +0200906
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
908 opt.key_usage != 0) {
909 mbedtls_printf(" . Adding the Key Usage extension ...");
910 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200911
Gilles Peskine449bd832023-01-11 14:50:10 +0100912 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
913 if (ret != 0) {
914 mbedtls_strerror(ret, buf, sizeof(buf));
915 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
916 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200917 goto exit;
918 }
919
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200921 }
922
Andrzej Kurekccdd9752023-04-01 10:38:30 -0400923 if (opt.san_list != NULL) {
924 ret = mbedtls_x509write_crt_set_subject_alternative_name(&crt, opt.san_list);
925
926 if (ret != 0) {
927 mbedtls_printf(
928 " failed\n ! mbedtls_x509write_csr_set_subject_alternative_name returned %d",
929 ret);
930 goto exit;
931 }
932 }
933
Gilles Peskine449bd832023-01-11 14:50:10 +0100934 if (opt.ext_key_usage) {
935 mbedtls_printf(" . Adding the Extended Key Usage extension ...");
936 fflush(stdout);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100937
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
939 if (ret != 0) {
940 mbedtls_strerror(ret, buf, sizeof(buf));
941 mbedtls_printf(
942 " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
943 (unsigned int) -ret,
944 buf);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100945 goto exit;
946 }
947
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 mbedtls_printf(" ok\n");
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100949 }
950
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
952 opt.ns_cert_type != 0) {
953 mbedtls_printf(" . Adding the NS Cert Type extension ...");
954 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200955
Gilles Peskine449bd832023-01-11 14:50:10 +0100956 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
957 if (ret != 0) {
958 mbedtls_strerror(ret, buf, sizeof(buf));
959 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
960 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200961 goto exit;
962 }
963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200965 }
966
Paul Bakker9397dcb2013-09-06 09:55:26 +0200967 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100968 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200969 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100970 mbedtls_printf(" . Writing the certificate...");
971 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200972
Gilles Peskine449bd832023-01-11 14:50:10 +0100973 if ((ret = write_certificate(&crt, opt.output_file,
974 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
975 mbedtls_strerror(ret, buf, sizeof(buf));
976 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
977 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200978 goto exit;
979 }
980
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200982
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100983 exit_code = MBEDTLS_EXIT_SUCCESS;
984
Paul Bakker9397dcb2013-09-06 09:55:26 +0200985exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100986#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +0100988#endif /* MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 mbedtls_x509_crt_free(&issuer_crt);
990 mbedtls_x509write_crt_free(&crt);
991 mbedtls_pk_free(&loaded_subject_key);
992 mbedtls_pk_free(&loaded_issuer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 mbedtls_ctr_drbg_free(&ctr_drbg);
994 mbedtls_entropy_free(&entropy);
Przemek Stekiel758aef62023-04-19 13:47:43 +0200995#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekiela8c560a2023-04-19 10:15:26 +0200996 mbedtls_psa_crypto_free();
Przemek Stekiel758aef62023-04-19 13:47:43 +0200997#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200998
Gilles Peskine449bd832023-01-11 14:50:10 +0100999 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +02001000}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
1002 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +01001003 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */