blob: 05d7677c78882eee672e86e465dd0f5f0835018c [file] [log] [blame]
Paul Bakker9397dcb2013-09-06 09:55:26 +02001/*
2 * Certificate generation and signing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker9397dcb2013-09-06 09:55:26 +020018 */
19
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker9397dcb2013-09-06 09:55:26 +020021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Simon Butcher203a6932016-10-07 15:00:17 +010024#if !defined(MBEDTLS_X509_CRT_WRITE_C) || \
25 !defined(MBEDTLS_X509_CRT_PARSE_C) || !defined(MBEDTLS_FS_IO) || \
26 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C) || \
27 !defined(MBEDTLS_ERROR_C) || !defined(MBEDTLS_SHA256_C) || \
28 !defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010029int main(void)
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020030{
Gilles Peskine449bd832023-01-11 14:50:10 +010031 mbedtls_printf("MBEDTLS_X509_CRT_WRITE_C and/or MBEDTLS_X509_CRT_PARSE_C and/or "
32 "MBEDTLS_FS_IO and/or MBEDTLS_SHA256_C and/or "
33 "MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C and/or "
34 "MBEDTLS_ERROR_C not defined.\n");
35 mbedtls_exit(0);
Manuel Pégourié-Gonnard8d649c62015-03-31 15:10:03 +020036}
37#else
38
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000039#include "mbedtls/x509_crt.h"
40#include "mbedtls/x509_csr.h"
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010041#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000042#include "mbedtls/entropy.h"
43#include "mbedtls/ctr_drbg.h"
Hanno Becker6c13d372017-09-13 12:49:22 +010044#include "mbedtls/md.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000045#include "mbedtls/error.h"
Manuel Pégourié-Gonnard7831b0c2013-09-20 12:29:56 +020046
Rich Evans18b78c72015-02-11 14:06:19 +000047#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
Rich Evans18b78c72015-02-11 14:06:19 +000050
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010051#define SET_OID(x, oid) \
Gilles Peskine449bd832023-01-11 14:50:10 +010052 do { x.len = MBEDTLS_OID_SIZE(oid); x.p = (unsigned char *) oid; } while (0)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +010053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#if defined(MBEDTLS_X509_CSR_PARSE_C)
Rich Evans18b78c72015-02-11 14:06:19 +000055#define USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010056 " request_file=%%s default: (empty)\n" \
57 " If request_file is specified, subject_key,\n" \
58 " subject_pwd and subject_name are ignored!\n"
Rich Evans18b78c72015-02-11 14:06:19 +000059#else
60#define USAGE_CSR ""
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061#endif /* MBEDTLS_X509_CSR_PARSE_C */
Rich Evans18b78c72015-02-11 14:06:19 +000062
Dave Rodgman66e05502022-10-27 16:29:38 +010063#define FORMAT_PEM 0
64#define FORMAT_DER 1
65
Paul Bakker1014e952013-09-09 13:59:42 +020066#define DFL_ISSUER_CRT ""
Paul Bakkere2673fb2013-09-09 15:52:07 +020067#define DFL_REQUEST_FILE ""
Paul Bakker9397dcb2013-09-06 09:55:26 +020068#define DFL_SUBJECT_KEY "subject.key"
69#define DFL_ISSUER_KEY "ca.key"
70#define DFL_SUBJECT_PWD ""
71#define DFL_ISSUER_PWD ""
72#define DFL_OUTPUT_FILENAME "cert.crt"
Manuel Pégourié-Gonnard91699212015-01-22 16:26:39 +000073#define DFL_SUBJECT_NAME "CN=Cert,O=mbed TLS,C=UK"
74#define DFL_ISSUER_NAME "CN=CA,O=mbed TLS,C=UK"
Paul Bakker9397dcb2013-09-06 09:55:26 +020075#define DFL_NOT_BEFORE "20010101000000"
76#define DFL_NOT_AFTER "20301231235959"
77#define DFL_SERIAL "1"
Paul Bakkerb2d7f232013-09-09 16:24:18 +020078#define DFL_SELFSIGN 0
Paul Bakker15162a02013-09-06 19:27:21 +020079#define DFL_IS_CA 0
80#define DFL_MAX_PATHLEN -1
Nicholas Wilson99a96b12015-09-10 18:28:01 +010081#define DFL_SIG_ALG MBEDTLS_MD_SHA256
Paul Bakker9397dcb2013-09-06 09:55:26 +020082#define DFL_KEY_USAGE 0
Dave Rodgman1577c542022-09-09 10:22:15 +010083#define DFL_EXT_KEY_USAGE NULL
Paul Bakker9397dcb2013-09-06 09:55:26 +020084#define DFL_NS_CERT_TYPE 0
Hanno Becker6c13d372017-09-13 12:49:22 +010085#define DFL_VERSION 3
86#define DFL_AUTH_IDENT 1
87#define DFL_SUBJ_IDENT 1
88#define DFL_CONSTRAINTS 1
89#define DFL_DIGEST MBEDTLS_MD_SHA256
Dave Rodgman66e05502022-10-27 16:29:38 +010090#define DFL_FORMAT FORMAT_PEM
Paul Bakker9397dcb2013-09-06 09:55:26 +020091
Rich Evans18b78c72015-02-11 14:06:19 +000092#define USAGE \
93 "\n usage: cert_write param=<>...\n" \
94 "\n acceptable parameters:\n" \
95 USAGE_CSR \
Hanno Becker81535d02017-09-13 15:39:59 +010096 " subject_key=%%s default: subject.key\n" \
97 " subject_pwd=%%s default: (empty)\n" \
98 " subject_name=%%s default: CN=Cert,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +000099 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100100 " issuer_crt=%%s default: (empty)\n" \
101 " If issuer_crt is specified, issuer_name is\n" \
102 " ignored!\n" \
103 " issuer_name=%%s default: CN=CA,O=mbed TLS,C=UK\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000104 "\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100105 " selfsign=%%d default: 0 (false)\n" \
106 " If selfsign is enabled, issuer_name and\n" \
107 " issuer_key are required (issuer_crt and\n" \
108 " subject_* are ignored\n" \
109 " issuer_key=%%s default: ca.key\n" \
110 " issuer_pwd=%%s default: (empty)\n" \
111 " output_file=%%s default: cert.crt\n" \
112 " serial=%%s default: 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 " not_before=%%s default: 20010101000000\n" \
114 " not_after=%%s default: 20301231235959\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100115 " is_ca=%%d default: 0 (disabled)\n" \
116 " max_pathlen=%%d default: -1 (none)\n" \
117 " md=%%s default: SHA256\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200118 " Supported values (if enabled):\n" \
TRodziewicz10e8cf52021-05-31 17:58:57 +0200119 " MD5, RIPEMD160, SHA1,\n" \
Gilles Peskine18292fe2020-08-21 20:42:32 +0200120 " SHA224, SHA256, SHA384, SHA512\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100121 " version=%%d default: 3\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 " Possible values: 1, 2, 3\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100123 " subject_identifier=%%s default: 1\n" \
124 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100126 " authority_identifier=%%s default: 1\n" \
127 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100129 " basic_constraints=%%d default: 1\n" \
130 " Possible values: 0, 1\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 " (Considered for v3 only)\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100132 " key_usage=%%s default: (empty)\n" \
133 " Comma-separated-list of values:\n" \
134 " digital_signature\n" \
135 " non_repudiation\n" \
136 " key_encipherment\n" \
137 " data_encipherment\n" \
138 " key_agreement\n" \
139 " key_cert_sign\n" \
140 " crl_sign\n" \
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 " (Considered for v3 only)\n" \
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100142 " ext_key_usage=%%s default: (empty)\n" \
143 " Comma-separated-list of values:\n" \
144 " serverAuth\n" \
145 " clientAuth\n" \
146 " codeSigning\n" \
147 " emailProtection\n" \
148 " timeStamping\n" \
149 " OCSPSigning\n" \
Hanno Becker81535d02017-09-13 15:39:59 +0100150 " ns_cert_type=%%s default: (empty)\n" \
151 " Comma-separated-list of values:\n" \
152 " ssl_client\n" \
153 " ssl_server\n" \
154 " email\n" \
155 " object_signing\n" \
156 " ssl_ca\n" \
157 " email_ca\n" \
158 " object_signing_ca\n" \
Dave Rodgman66e05502022-10-27 16:29:38 +0100159 " format=pem|der default: pem\n" \
Rich Evans18b78c72015-02-11 14:06:19 +0000160 "\n"
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000161
Simon Butcher63cb97e2018-12-06 17:43:31 +0000162
Paul Bakker9397dcb2013-09-06 09:55:26 +0200163/*
164 * global options
165 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100166struct options {
Paul Bakker8fc30b12013-11-25 13:29:43 +0100167 const char *issuer_crt; /* filename of the issuer certificate */
168 const char *request_file; /* filename of the certificate request */
169 const char *subject_key; /* filename of the subject key file */
170 const char *issuer_key; /* filename of the issuer key file */
171 const char *subject_pwd; /* password for the subject key file */
172 const char *issuer_pwd; /* password for the issuer key file */
Hanno Becker25d882b2018-08-23 15:26:06 +0100173 const char *output_file; /* where to store the constructed CRT */
Paul Bakker8fc30b12013-11-25 13:29:43 +0100174 const char *subject_name; /* subject name for certificate */
175 const char *issuer_name; /* issuer name for certificate */
176 const char *not_before; /* validity period not before */
177 const char *not_after; /* validity period not after */
178 const char *serial; /* serial number string */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200179 int selfsign; /* selfsign the certificate */
Paul Bakker15162a02013-09-06 19:27:21 +0200180 int is_ca; /* is a CA certificate */
181 int max_pathlen; /* maximum CA path length */
Hanno Beckere1b1d0a2017-09-22 15:35:16 +0100182 int authority_identifier; /* add authority identifier to CRT */
183 int subject_identifier; /* add subject identifier to CRT */
Hanno Becker6c13d372017-09-13 12:49:22 +0100184 int basic_constraints; /* add basic constraints ext to CRT */
185 int version; /* CRT version */
186 mbedtls_md_type_t md; /* Hash used for signing */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200187 unsigned char key_usage; /* key usage flags */
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100188 mbedtls_asn1_sequence *ext_key_usage; /* extended key usages */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200189 unsigned char ns_cert_type; /* NS cert type */
Dave Rodgman66e05502022-10-27 16:29:38 +0100190 int format; /* format */
Paul Bakker9397dcb2013-09-06 09:55:26 +0200191} opt;
192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193int write_certificate(mbedtls_x509write_cert *crt, const char *output_file,
194 int (*f_rng)(void *, unsigned char *, size_t),
195 void *p_rng)
Paul Bakker9397dcb2013-09-06 09:55:26 +0200196{
197 int ret;
198 FILE *f;
199 unsigned char output_buf[4096];
Dave Rodgman66e05502022-10-27 16:29:38 +0100200 unsigned char *output_start;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200201 size_t len = 0;
202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 memset(output_buf, 0, 4096);
204 if (opt.format == FORMAT_DER) {
205 ret = mbedtls_x509write_crt_der(crt, output_buf, 4096,
206 f_rng, p_rng);
207 if (ret < 0) {
208 return ret;
209 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200210
Dave Rodgman66e05502022-10-27 16:29:38 +0100211 len = ret;
212 output_start = output_buf + 4096 - len;
213 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +0100214 ret = mbedtls_x509write_crt_pem(crt, output_buf, 4096,
215 f_rng, p_rng);
216 if (ret < 0) {
217 return ret;
218 }
Dave Rodgman66e05502022-10-27 16:29:38 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 len = strlen((char *) output_buf);
Dave Rodgman66e05502022-10-27 16:29:38 +0100221 output_start = output_buf;
222 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200223
Gilles Peskine449bd832023-01-11 14:50:10 +0100224 if ((f = fopen(output_file, "w")) == NULL) {
225 return -1;
Paul Bakker0c226102014-04-17 16:02:36 +0200226 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200227
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 if (fwrite(output_start, 1, len, f) != len) {
229 fclose(f);
230 return -1;
231 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 fclose(f);
234
235 return 0;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200236}
237
Valerio Settiacf12fb2023-01-09 17:19:26 +0100238/*
239 * Convert the input "in_buff" string to a raw byte array "out_buff". The amount
240 * of converted data is returned on "written_data".
241 */
242static int parse_serial(const char *in_buff, size_t in_buff_len,
243 unsigned char *out_buff, size_t out_buff_len,
244 size_t *written_data)
245{
246 char c;
247 unsigned char val;
248 int i;
249
250 if (out_buff_len < in_buff_len) {
251 return -1;
252 }
253
254 *written_data = 0;
255
256 for (i = 0; i < (int) in_buff_len; i++, (*written_data)++) {
257 c = in_buff[i];
258 if (c >= 0x30 && c <= 0x39) {
259 val = c - 0x30;
260 } else if (c >= 0x41 && c <= 0x46) {
261 val = c - 0x37;
262 } else if (c >= 0x61 && c <= 0x66) {
263 val = c - 0x57;
264 } else {
265 return -1;
266 }
267
268 out_buff[i] = val;
269 }
270
271 return 0;
272}
273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200275{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100276 int ret = 1;
277 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_x509_crt issuer_crt;
279 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
280 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200282 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200283 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100284 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200285 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200287 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200289#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 mbedtls_x509write_cert crt;
Valerio Settiacf12fb2023-01-09 17:19:26 +0100291 unsigned char serial[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
292 size_t serial_len;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100293 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 mbedtls_entropy_context entropy;
295 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200296 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200297
298 /*
299 * Set to sane values
300 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 mbedtls_x509write_crt_init(&crt);
302 mbedtls_pk_init(&loaded_issuer_key);
303 mbedtls_pk_init(&loaded_subject_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 mbedtls_ctr_drbg_init(&ctr_drbg);
305 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200308#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 mbedtls_x509_crt_init(&issuer_crt);
310 memset(buf, 0, sizeof(buf));
Paul Bakker9397dcb2013-09-06 09:55:26 +0200311
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 if (argc == 0) {
313usage:
314 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200315 goto exit;
316 }
317
Paul Bakker1014e952013-09-09 13:59:42 +0200318 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200319 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200320 opt.subject_key = DFL_SUBJECT_KEY;
321 opt.issuer_key = DFL_ISSUER_KEY;
322 opt.subject_pwd = DFL_SUBJECT_PWD;
323 opt.issuer_pwd = DFL_ISSUER_PWD;
324 opt.output_file = DFL_OUTPUT_FILENAME;
325 opt.subject_name = DFL_SUBJECT_NAME;
326 opt.issuer_name = DFL_ISSUER_NAME;
327 opt.not_before = DFL_NOT_BEFORE;
328 opt.not_after = DFL_NOT_AFTER;
329 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200330 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200331 opt.is_ca = DFL_IS_CA;
332 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200333 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100334 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200335 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100336 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100337 opt.md = DFL_DIGEST;
338 opt.subject_identifier = DFL_SUBJ_IDENT;
339 opt.authority_identifier = DFL_AUTH_IDENT;
340 opt.basic_constraints = DFL_CONSTRAINTS;
Dave Rodgman66e05502022-10-27 16:29:38 +0100341 opt.format = DFL_FORMAT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200342
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200344
345 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200347 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200349 *q++ = '\0';
350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200352 opt.request_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200354 opt.subject_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200356 opt.issuer_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200358 opt.subject_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200360 opt.issuer_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200362 opt.issuer_crt = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200364 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200366 opt.subject_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200368 opt.issuer_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200370 opt.not_before = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200372 opt.not_after = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100373 } else if (strcmp(p, "serial") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200374 opt.serial = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 } else if (strcmp(p, "authority_identifier") == 0) {
376 opt.authority_identifier = atoi(q);
377 if (opt.authority_identifier != 0 &&
378 opt.authority_identifier != 1) {
379 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100380 goto usage;
381 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 } else if (strcmp(p, "subject_identifier") == 0) {
383 opt.subject_identifier = atoi(q);
384 if (opt.subject_identifier != 0 &&
385 opt.subject_identifier != 1) {
386 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100387 goto usage;
388 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100389 } else if (strcmp(p, "basic_constraints") == 0) {
390 opt.basic_constraints = atoi(q);
391 if (opt.basic_constraints != 0 &&
392 opt.basic_constraints != 1) {
393 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100394 goto usage;
395 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200397 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100398 mbedtls_md_info_from_string(q);
399 if (md_info == NULL) {
400 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100401 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100402 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 opt.md = mbedtls_md_get_type(md_info);
404 } else if (strcmp(p, "version") == 0) {
405 opt.version = atoi(q);
406 if (opt.version < 1 || opt.version > 3) {
407 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100408 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100409 }
Hanno Becker38eff432017-09-22 15:38:20 +0100410 opt.version--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 } else if (strcmp(p, "selfsign") == 0) {
412 opt.selfsign = atoi(q);
413 if (opt.selfsign < 0 || opt.selfsign > 1) {
414 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200415 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100416 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100417 } else if (strcmp(p, "is_ca") == 0) {
418 opt.is_ca = atoi(q);
419 if (opt.is_ca < 0 || opt.is_ca > 1) {
420 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200421 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100422 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 } else if (strcmp(p, "max_pathlen") == 0) {
424 opt.max_pathlen = atoi(q);
425 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
426 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200427 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100428 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 } else if (strcmp(p, "key_usage") == 0) {
430 while (q != NULL) {
431 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200432 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200434
Gilles Peskine449bd832023-01-11 14:50:10 +0100435 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100437 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100440 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100441 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100442 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100444 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200448 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100449 } else {
450 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200451 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100452 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200453
454 q = r;
455 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 } else if (strcmp(p, "ext_key_usage") == 0) {
Dave Rodgman64937852022-08-15 14:12:25 +0100457 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 while (q != NULL) {
460 if ((r = strchr(q, ',')) != NULL) {
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100461 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100463
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100465 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (strcmp(q, "serverAuth") == 0) {
467 SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
468 } else if (strcmp(q, "clientAuth") == 0) {
469 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
470 } else if (strcmp(q, "codeSigning") == 0) {
471 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
472 } else if (strcmp(q, "emailProtection") == 0) {
473 SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
474 } else if (strcmp(q, "timeStamping") == 0) {
475 SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
476 } else if (strcmp(q, "OCSPSigning") == 0) {
477 SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
478 } else {
479 mbedtls_printf("Invalid argument for option %s\n", p);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100480 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100481 }
Dave Rodgman64937852022-08-15 14:12:25 +0100482
483 *tail = ext_key_usage;
484 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100485
486 q = r;
487 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 } else if (strcmp(p, "ns_cert_type") == 0) {
489 while (q != NULL) {
490 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200491 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200493
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100496 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100497 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100503 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100505 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100507 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 } else {
509 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200510 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100511 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200512
513 q = r;
514 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100515 } else if (strcmp(p, "format") == 0) {
516 if (strcmp(q, "der") == 0) {
517 opt.format = FORMAT_DER;
518 } else if (strcmp(q, "pem") == 0) {
519 opt.format = FORMAT_PEM;
520 } else {
521 mbedtls_printf("Invalid argument for option %s\n", p);
Dave Rodgman66e05502022-10-27 16:29:38 +0100522 goto usage;
523 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200525 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200527 }
528
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200530
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200531 /*
532 * 0. Seed the PRNG
533 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 mbedtls_printf(" . Seeding the random number generator...");
535 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200536
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
538 (const unsigned char *) pers,
539 strlen(pers))) != 0) {
540 mbedtls_strerror(ret, buf, sizeof(buf));
541 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
542 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200543 goto exit;
544 }
545
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200547
Paul Bakker9397dcb2013-09-06 09:55:26 +0200548 // Parse serial to MPI
549 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 mbedtls_printf(" . Reading serial number...");
551 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200554
Paul Bakker1014e952013-09-09 13:59:42 +0200555 // Parse issuer certificate if present
556 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100557 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200558 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200559 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200560 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 mbedtls_printf(" . Loading the issuer certificate ...");
562 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200563
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
565 mbedtls_strerror(ret, buf, sizeof(buf));
566 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
567 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200568 goto exit;
569 }
570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
572 &issuer_crt.subject);
573 if (ret < 0) {
574 mbedtls_strerror(ret, buf, sizeof(buf));
575 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
576 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200577 goto exit;
578 }
579
Paul Bakkere2673fb2013-09-09 15:52:07 +0200580 opt.issuer_name = issuer_name;
581
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200583 }
584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200586 // Parse certificate request if present
587 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200589 /*
590 * 1.0.b. Load the CSR
591 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 mbedtls_printf(" . Loading the certificate request ...");
593 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
596 mbedtls_strerror(ret, buf, sizeof(buf));
597 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
598 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200599 goto exit;
600 }
601
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
603 &csr.subject);
604 if (ret < 0) {
605 mbedtls_strerror(ret, buf, sizeof(buf));
606 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
607 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200608 goto exit;
609 }
610
611 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200612 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200613
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200615 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200617
618 /*
619 * 1.1. Load the keys
620 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if (!opt.selfsign && !strlen(opt.request_file)) {
622 mbedtls_printf(" . Loading the subject key ...");
623 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
626 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
627 if (ret != 0) {
628 mbedtls_strerror(ret, buf, sizeof(buf));
629 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
630 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200631 goto exit;
632 }
Paul Bakker1014e952013-09-09 13:59:42 +0200633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200635 }
636
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 mbedtls_printf(" . Loading the issuer key ...");
638 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200639
Gilles Peskine449bd832023-01-11 14:50:10 +0100640 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
641 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
642 if (ret != 0) {
643 mbedtls_strerror(ret, buf, sizeof(buf));
644 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
645 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200646 goto exit;
647 }
648
649 // Check if key and issuer certificate match
650 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if (strlen(opt.issuer_crt)) {
652 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key,
653 mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
654 mbedtls_printf(" failed\n ! issuer_key does not match "
655 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200656 goto exit;
657 }
658 }
659
Gilles Peskine449bd832023-01-11 14:50:10 +0100660 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200661
Gilles Peskine449bd832023-01-11 14:50:10 +0100662 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100663 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200664 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200665 }
666
Gilles Peskine449bd832023-01-11 14:50:10 +0100667 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
668 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200669
Paul Bakker9397dcb2013-09-06 09:55:26 +0200670 /*
671 * 1.0. Check the names for validity
672 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
674 mbedtls_strerror(ret, buf, sizeof(buf));
675 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
676 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200677 goto exit;
678 }
679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
681 mbedtls_strerror(ret, buf, sizeof(buf));
682 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
683 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200684 goto exit;
685 }
686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 mbedtls_printf(" . Setting certificate values ...");
688 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 mbedtls_x509write_crt_set_version(&crt, opt.version);
691 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100692
Valerio Settiacf12fb2023-01-09 17:19:26 +0100693 if (parse_serial(opt.serial, strlen(opt.serial),
694 serial, sizeof(serial), &serial_len) < 0) {
695 mbedtls_printf(" failed\n ! Unable to parse serial\n\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200696 goto exit;
697 }
Valerio Settiacf12fb2023-01-09 17:19:26 +0100698 ret = mbedtls_x509write_crt_set_serial_new(&crt, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100699 if (ret != 0) {
700 mbedtls_strerror(ret, buf, sizeof(buf));
701 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial_new "
702 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
703 goto exit;
704 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200705
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
707 if (ret != 0) {
708 mbedtls_strerror(ret, buf, sizeof(buf));
709 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
710 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200711 goto exit;
712 }
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200715
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
717 opt.basic_constraints != 0) {
718 mbedtls_printf(" . Adding the Basic Constraints extension ...");
719 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200720
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
722 opt.max_pathlen);
723 if (ret != 0) {
724 mbedtls_strerror(ret, buf, sizeof(buf));
725 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
726 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100727 goto exit;
728 }
729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100731 }
Paul Bakker15162a02013-09-06 19:27:21 +0200732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
735 opt.subject_identifier != 0) {
736 mbedtls_printf(" . Adding the Subject Key Identifier ...");
737 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100738
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
740 if (ret != 0) {
741 mbedtls_strerror(ret, buf, sizeof(buf));
742 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
743 "_key_identifier returned -0x%04x - %s\n\n",
744 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100745 goto exit;
746 }
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200749 }
750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
752 opt.authority_identifier != 0) {
753 mbedtls_printf(" . Adding the Authority Key Identifier ...");
754 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200755
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
757 if (ret != 0) {
758 mbedtls_strerror(ret, buf, sizeof(buf));
759 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
760 "key_identifier returned -0x%04x - %s\n\n",
761 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100762 goto exit;
763 }
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100766 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
770 opt.key_usage != 0) {
771 mbedtls_printf(" . Adding the Key Usage extension ...");
772 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200773
Gilles Peskine449bd832023-01-11 14:50:10 +0100774 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
775 if (ret != 0) {
776 mbedtls_strerror(ret, buf, sizeof(buf));
777 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
778 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200779 goto exit;
780 }
781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200783 }
784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (opt.ext_key_usage) {
786 mbedtls_printf(" . Adding the Extended Key Usage extension ...");
787 fflush(stdout);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100788
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
790 if (ret != 0) {
791 mbedtls_strerror(ret, buf, sizeof(buf));
792 mbedtls_printf(
793 " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
794 (unsigned int) -ret,
795 buf);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100796 goto exit;
797 }
798
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 mbedtls_printf(" ok\n");
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100800 }
801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
803 opt.ns_cert_type != 0) {
804 mbedtls_printf(" . Adding the NS Cert Type extension ...");
805 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200806
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
808 if (ret != 0) {
809 mbedtls_strerror(ret, buf, sizeof(buf));
810 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
811 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200812 goto exit;
813 }
814
Gilles Peskine449bd832023-01-11 14:50:10 +0100815 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200816 }
817
Paul Bakker9397dcb2013-09-06 09:55:26 +0200818 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100819 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200820 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100821 mbedtls_printf(" . Writing the certificate...");
822 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200823
Gilles Peskine449bd832023-01-11 14:50:10 +0100824 if ((ret = write_certificate(&crt, opt.output_file,
825 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
826 mbedtls_strerror(ret, buf, sizeof(buf));
827 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
828 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200829 goto exit;
830 }
831
Gilles Peskine449bd832023-01-11 14:50:10 +0100832 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200833
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100834 exit_code = MBEDTLS_EXIT_SUCCESS;
835
Paul Bakker9397dcb2013-09-06 09:55:26 +0200836exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100837#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +0100839#endif /* MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 mbedtls_x509_crt_free(&issuer_crt);
841 mbedtls_x509write_crt_free(&crt);
842 mbedtls_pk_free(&loaded_subject_key);
843 mbedtls_pk_free(&loaded_issuer_key);
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_ctr_drbg_free(&ctr_drbg);
845 mbedtls_entropy_free(&entropy);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200846
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200848}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200849#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
850 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100851 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */