blob: 8a652fe577f77d3ce37ea8bf90d6d330fca6685a [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
Gilles Peskine449bd832023-01-11 14:50:10 +0100238int main(int argc, char *argv[])
Paul Bakker9397dcb2013-09-06 09:55:26 +0200239{
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100240 int ret = 1;
241 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242 mbedtls_x509_crt issuer_crt;
243 mbedtls_pk_context loaded_issuer_key, loaded_subject_key;
244 mbedtls_pk_context *issuer_key = &loaded_issuer_key,
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 *subject_key = &loaded_subject_key;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200246 char buf[1024];
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200247 char issuer_name[256];
Paul Bakkerc97f9f62013-11-30 15:13:02 +0100248 int i;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200249 char *p, *q, *r;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_X509_CSR_PARSE_C)
Jonathan Leroybbc75d92015-10-10 21:58:07 +0200251 char subject_name[256];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_x509_csr csr;
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200253#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_x509write_cert crt;
255 mbedtls_mpi serial;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100256 mbedtls_asn1_sequence *ext_key_usage;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257 mbedtls_entropy_context entropy;
258 mbedtls_ctr_drbg_context ctr_drbg;
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200259 const char *pers = "crt example app";
Paul Bakker9397dcb2013-09-06 09:55:26 +0200260
261 /*
262 * Set to sane values
263 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 mbedtls_x509write_crt_init(&crt);
265 mbedtls_pk_init(&loaded_issuer_key);
266 mbedtls_pk_init(&loaded_subject_key);
267 mbedtls_mpi_init(&serial);
268 mbedtls_ctr_drbg_init(&ctr_drbg);
269 mbedtls_entropy_init(&entropy);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200270#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_x509_csr_init(&csr);
Paul Bakker7fc7fa62013-09-17 14:44:00 +0200272#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 mbedtls_x509_crt_init(&issuer_crt);
274 memset(buf, 0, sizeof(buf));
Paul Bakker9397dcb2013-09-06 09:55:26 +0200275
Aditya Deshpande644a5c02023-01-30 15:58:50 +0000276 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100277usage:
278 mbedtls_printf(USAGE);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200279 goto exit;
280 }
281
Paul Bakker1014e952013-09-09 13:59:42 +0200282 opt.issuer_crt = DFL_ISSUER_CRT;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200283 opt.request_file = DFL_REQUEST_FILE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200284 opt.subject_key = DFL_SUBJECT_KEY;
285 opt.issuer_key = DFL_ISSUER_KEY;
286 opt.subject_pwd = DFL_SUBJECT_PWD;
287 opt.issuer_pwd = DFL_ISSUER_PWD;
288 opt.output_file = DFL_OUTPUT_FILENAME;
289 opt.subject_name = DFL_SUBJECT_NAME;
290 opt.issuer_name = DFL_ISSUER_NAME;
291 opt.not_before = DFL_NOT_BEFORE;
292 opt.not_after = DFL_NOT_AFTER;
293 opt.serial = DFL_SERIAL;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200294 opt.selfsign = DFL_SELFSIGN;
Paul Bakker15162a02013-09-06 19:27:21 +0200295 opt.is_ca = DFL_IS_CA;
296 opt.max_pathlen = DFL_MAX_PATHLEN;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200297 opt.key_usage = DFL_KEY_USAGE;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100298 opt.ext_key_usage = DFL_EXT_KEY_USAGE;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200299 opt.ns_cert_type = DFL_NS_CERT_TYPE;
Hanno Becker38eff432017-09-22 15:38:20 +0100300 opt.version = DFL_VERSION - 1;
Hanno Becker6c13d372017-09-13 12:49:22 +0100301 opt.md = DFL_DIGEST;
302 opt.subject_identifier = DFL_SUBJ_IDENT;
303 opt.authority_identifier = DFL_AUTH_IDENT;
304 opt.basic_constraints = DFL_CONSTRAINTS;
Dave Rodgman66e05502022-10-27 16:29:38 +0100305 opt.format = DFL_FORMAT;
Paul Bakker9397dcb2013-09-06 09:55:26 +0200306
Gilles Peskine449bd832023-01-11 14:50:10 +0100307 for (i = 1; i < argc; i++) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200308
309 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 if ((q = strchr(p, '=')) == NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200311 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200313 *q++ = '\0';
314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (strcmp(p, "request_file") == 0) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200316 opt.request_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100317 } else if (strcmp(p, "subject_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200318 opt.subject_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 } else if (strcmp(p, "issuer_key") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200320 opt.issuer_key = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 } else if (strcmp(p, "subject_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200322 opt.subject_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100323 } else if (strcmp(p, "issuer_pwd") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200324 opt.issuer_pwd = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 } else if (strcmp(p, "issuer_crt") == 0) {
Paul Bakker1014e952013-09-09 13:59:42 +0200326 opt.issuer_crt = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 } else if (strcmp(p, "output_file") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200328 opt.output_file = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100329 } else if (strcmp(p, "subject_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200330 opt.subject_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 } else if (strcmp(p, "issuer_name") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200332 opt.issuer_name = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100333 } else if (strcmp(p, "not_before") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200334 opt.not_before = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 } else if (strcmp(p, "not_after") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200336 opt.not_after = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100337 } else if (strcmp(p, "serial") == 0) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200338 opt.serial = q;
Gilles Peskine449bd832023-01-11 14:50:10 +0100339 } else if (strcmp(p, "authority_identifier") == 0) {
340 opt.authority_identifier = atoi(q);
341 if (opt.authority_identifier != 0 &&
342 opt.authority_identifier != 1) {
343 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100344 goto usage;
345 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 } else if (strcmp(p, "subject_identifier") == 0) {
347 opt.subject_identifier = atoi(q);
348 if (opt.subject_identifier != 0 &&
349 opt.subject_identifier != 1) {
350 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100351 goto usage;
352 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 } else if (strcmp(p, "basic_constraints") == 0) {
354 opt.basic_constraints = atoi(q);
355 if (opt.basic_constraints != 0 &&
356 opt.basic_constraints != 1) {
357 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100358 goto usage;
359 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 } else if (strcmp(p, "md") == 0) {
Gilles Peskine18292fe2020-08-21 20:42:32 +0200361 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 mbedtls_md_info_from_string(q);
363 if (md_info == NULL) {
364 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100365 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100366 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 opt.md = mbedtls_md_get_type(md_info);
368 } else if (strcmp(p, "version") == 0) {
369 opt.version = atoi(q);
370 if (opt.version < 1 || opt.version > 3) {
371 mbedtls_printf("Invalid argument for option %s\n", p);
Hanno Becker6c13d372017-09-13 12:49:22 +0100372 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100373 }
Hanno Becker38eff432017-09-22 15:38:20 +0100374 opt.version--;
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 } else if (strcmp(p, "selfsign") == 0) {
376 opt.selfsign = atoi(q);
377 if (opt.selfsign < 0 || opt.selfsign > 1) {
378 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200379 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100380 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 } else if (strcmp(p, "is_ca") == 0) {
382 opt.is_ca = atoi(q);
383 if (opt.is_ca < 0 || opt.is_ca > 1) {
384 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200385 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100386 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 } else if (strcmp(p, "max_pathlen") == 0) {
388 opt.max_pathlen = atoi(q);
389 if (opt.max_pathlen < -1 || opt.max_pathlen > 127) {
390 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker15162a02013-09-06 19:27:21 +0200391 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100392 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100393 } else if (strcmp(p, "key_usage") == 0) {
394 while (q != NULL) {
395 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200396 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100397 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (strcmp(q, "digital_signature") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200400 opt.key_usage |= MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 } else if (strcmp(q, "non_repudiation") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200402 opt.key_usage |= MBEDTLS_X509_KU_NON_REPUDIATION;
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 } else if (strcmp(q, "key_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100404 opt.key_usage |= MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 } else if (strcmp(q, "data_encipherment") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100406 opt.key_usage |= MBEDTLS_X509_KU_DATA_ENCIPHERMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 } else if (strcmp(q, "key_agreement") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100408 opt.key_usage |= MBEDTLS_X509_KU_KEY_AGREEMENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 } else if (strcmp(q, "key_cert_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410 opt.key_usage |= MBEDTLS_X509_KU_KEY_CERT_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 } else if (strcmp(q, "crl_sign") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412 opt.key_usage |= MBEDTLS_X509_KU_CRL_SIGN;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 } else {
414 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200415 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100416 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200417
418 q = r;
419 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 } else if (strcmp(p, "ext_key_usage") == 0) {
Dave Rodgman64937852022-08-15 14:12:25 +0100421 mbedtls_asn1_sequence **tail = &opt.ext_key_usage;
422
Gilles Peskine449bd832023-01-11 14:50:10 +0100423 while (q != NULL) {
424 if ((r = strchr(q, ',')) != NULL) {
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100425 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100427
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 ext_key_usage = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100429 ext_key_usage->buf.tag = MBEDTLS_ASN1_OID;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 if (strcmp(q, "serverAuth") == 0) {
431 SET_OID(ext_key_usage->buf, MBEDTLS_OID_SERVER_AUTH);
432 } else if (strcmp(q, "clientAuth") == 0) {
433 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CLIENT_AUTH);
434 } else if (strcmp(q, "codeSigning") == 0) {
435 SET_OID(ext_key_usage->buf, MBEDTLS_OID_CODE_SIGNING);
436 } else if (strcmp(q, "emailProtection") == 0) {
437 SET_OID(ext_key_usage->buf, MBEDTLS_OID_EMAIL_PROTECTION);
438 } else if (strcmp(q, "timeStamping") == 0) {
439 SET_OID(ext_key_usage->buf, MBEDTLS_OID_TIME_STAMPING);
440 } else if (strcmp(q, "OCSPSigning") == 0) {
441 SET_OID(ext_key_usage->buf, MBEDTLS_OID_OCSP_SIGNING);
442 } else {
443 mbedtls_printf("Invalid argument for option %s\n", p);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100444 goto usage;
Dave Rodgmanc5e0a8a2022-08-15 14:24:22 +0100445 }
Dave Rodgman64937852022-08-15 14:12:25 +0100446
447 *tail = ext_key_usage;
448 tail = &ext_key_usage->next;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100449
450 q = r;
451 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 } else if (strcmp(p, "ns_cert_type") == 0) {
453 while (q != NULL) {
454 if ((r = strchr(q, ',')) != NULL) {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200455 *r++ = '\0';
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 if (strcmp(q, "ssl_client") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200459 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 } else if (strcmp(q, "ssl_server") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100461 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 } else if (strcmp(q, "email") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200463 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100464 } else if (strcmp(q, "object_signing") == 0) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200465 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING;
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 } else if (strcmp(q, "ssl_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100467 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_SSL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 } else if (strcmp(q, "email_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100469 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 } else if (strcmp(q, "object_signing_ca") == 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +0100471 opt.ns_cert_type |= MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 } else {
473 mbedtls_printf("Invalid argument for option %s\n", p);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200474 goto usage;
Hanno Becker17c32762017-10-03 14:56:04 +0100475 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200476
477 q = r;
478 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 } else if (strcmp(p, "format") == 0) {
480 if (strcmp(q, "der") == 0) {
481 opt.format = FORMAT_DER;
482 } else if (strcmp(q, "pem") == 0) {
483 opt.format = FORMAT_PEM;
484 } else {
485 mbedtls_printf("Invalid argument for option %s\n", p);
Dave Rodgman66e05502022-10-27 16:29:38 +0100486 goto usage;
487 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 } else {
Paul Bakker9397dcb2013-09-06 09:55:26 +0200489 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +0100490 }
Paul Bakker9397dcb2013-09-06 09:55:26 +0200491 }
492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200493 mbedtls_printf("\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200494
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200495 /*
496 * 0. Seed the PRNG
497 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 mbedtls_printf(" . Seeding the random number generator...");
499 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200500
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
502 (const unsigned char *) pers,
503 strlen(pers))) != 0) {
504 mbedtls_strerror(ret, buf, sizeof(buf));
505 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d - %s\n",
506 ret, buf);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200507 goto exit;
508 }
509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200511
Paul Bakker9397dcb2013-09-06 09:55:26 +0200512 // Parse serial to MPI
513 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 mbedtls_printf(" . Reading serial number...");
515 fflush(stdout);
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if ((ret = mbedtls_mpi_read_string(&serial, 10, opt.serial)) != 0) {
518 mbedtls_strerror(ret, buf, sizeof(buf));
519 mbedtls_printf(" failed\n ! mbedtls_mpi_read_string "
520 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200521 goto exit;
522 }
523
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 mbedtls_printf(" ok\n");
Manuel Pégourié-Gonnard31e59402013-09-12 05:59:05 +0200525
Paul Bakker1014e952013-09-09 13:59:42 +0200526 // Parse issuer certificate if present
527 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (!opt.selfsign && strlen(opt.issuer_crt)) {
Paul Bakker1014e952013-09-09 13:59:42 +0200529 /*
Paul Bakkere2673fb2013-09-09 15:52:07 +0200530 * 1.0.a. Load the certificates
Paul Bakker1014e952013-09-09 13:59:42 +0200531 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 mbedtls_printf(" . Loading the issuer certificate ...");
533 fflush(stdout);
Paul Bakker1014e952013-09-09 13:59:42 +0200534
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 if ((ret = mbedtls_x509_crt_parse_file(&issuer_crt, opt.issuer_crt)) != 0) {
536 mbedtls_strerror(ret, buf, sizeof(buf));
537 mbedtls_printf(" failed\n ! mbedtls_x509_crt_parse_file "
538 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200539 goto exit;
540 }
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 ret = mbedtls_x509_dn_gets(issuer_name, sizeof(issuer_name),
543 &issuer_crt.subject);
544 if (ret < 0) {
545 mbedtls_strerror(ret, buf, sizeof(buf));
546 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
547 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker1014e952013-09-09 13:59:42 +0200548 goto exit;
549 }
550
Paul Bakkere2673fb2013-09-09 15:52:07 +0200551 opt.issuer_name = issuer_name;
552
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 mbedtls_printf(" ok\n");
Paul Bakkere2673fb2013-09-09 15:52:07 +0200554 }
555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556#if defined(MBEDTLS_X509_CSR_PARSE_C)
Paul Bakkere2673fb2013-09-09 15:52:07 +0200557 // Parse certificate request if present
558 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 if (!opt.selfsign && strlen(opt.request_file)) {
Paul Bakkere2673fb2013-09-09 15:52:07 +0200560 /*
561 * 1.0.b. Load the CSR
562 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 mbedtls_printf(" . Loading the certificate request ...");
564 fflush(stdout);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200565
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 if ((ret = mbedtls_x509_csr_parse_file(&csr, opt.request_file)) != 0) {
567 mbedtls_strerror(ret, buf, sizeof(buf));
568 mbedtls_printf(" failed\n ! mbedtls_x509_csr_parse_file "
569 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200570 goto exit;
571 }
572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 ret = mbedtls_x509_dn_gets(subject_name, sizeof(subject_name),
574 &csr.subject);
575 if (ret < 0) {
576 mbedtls_strerror(ret, buf, sizeof(buf));
577 mbedtls_printf(" failed\n ! mbedtls_x509_dn_gets "
578 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200579 goto exit;
580 }
581
582 opt.subject_name = subject_name;
Gilles Peskine842edf42021-08-04 21:56:10 +0200583 subject_key = &csr.pk;
Paul Bakkere2673fb2013-09-09 15:52:07 +0200584
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587#endif /* MBEDTLS_X509_CSR_PARSE_C */
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200588
589 /*
590 * 1.1. Load the keys
591 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 if (!opt.selfsign && !strlen(opt.request_file)) {
593 mbedtls_printf(" . Loading the subject key ...");
594 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200595
Gilles Peskine449bd832023-01-11 14:50:10 +0100596 ret = mbedtls_pk_parse_keyfile(&loaded_subject_key, opt.subject_key,
597 opt.subject_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
598 if (ret != 0) {
599 mbedtls_strerror(ret, buf, sizeof(buf));
600 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
601 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkere2673fb2013-09-09 15:52:07 +0200602 goto exit;
603 }
Paul Bakker1014e952013-09-09 13:59:42 +0200604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 mbedtls_printf(" ok\n");
Paul Bakker1014e952013-09-09 13:59:42 +0200606 }
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 mbedtls_printf(" . Loading the issuer key ...");
609 fflush(stdout);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200610
Gilles Peskine449bd832023-01-11 14:50:10 +0100611 ret = mbedtls_pk_parse_keyfile(&loaded_issuer_key, opt.issuer_key,
612 opt.issuer_pwd, mbedtls_ctr_drbg_random, &ctr_drbg);
613 if (ret != 0) {
614 mbedtls_strerror(ret, buf, sizeof(buf));
615 mbedtls_printf(" failed\n ! mbedtls_pk_parse_keyfile "
616 "returned -x%02x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200617 goto exit;
618 }
619
620 // Check if key and issuer certificate match
621 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 if (strlen(opt.issuer_crt)) {
623 if (mbedtls_pk_check_pair(&issuer_crt.pk, issuer_key,
624 mbedtls_ctr_drbg_random, &ctr_drbg) != 0) {
625 mbedtls_printf(" failed\n ! issuer_key does not match "
626 "issuer certificate\n\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200627 goto exit;
628 }
629 }
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 mbedtls_printf(" ok\n");
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 if (opt.selfsign) {
Paul Bakker93c6aa42013-10-28 22:28:09 +0100634 opt.subject_name = opt.issuer_name;
Manuel Pégourié-Gonnardf38e71a2013-09-12 05:21:54 +0200635 subject_key = issuer_key;
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200636 }
637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 mbedtls_x509write_crt_set_subject_key(&crt, subject_key);
639 mbedtls_x509write_crt_set_issuer_key(&crt, issuer_key);
Paul Bakkerb2d7f232013-09-09 16:24:18 +0200640
Paul Bakker9397dcb2013-09-06 09:55:26 +0200641 /*
642 * 1.0. Check the names for validity
643 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100644 if ((ret = mbedtls_x509write_crt_set_subject_name(&crt, opt.subject_name)) != 0) {
645 mbedtls_strerror(ret, buf, sizeof(buf));
646 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject_name "
647 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200648 goto exit;
649 }
650
Gilles Peskine449bd832023-01-11 14:50:10 +0100651 if ((ret = mbedtls_x509write_crt_set_issuer_name(&crt, opt.issuer_name)) != 0) {
652 mbedtls_strerror(ret, buf, sizeof(buf));
653 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_issuer_name "
654 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200655 goto exit;
656 }
657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 mbedtls_printf(" . Setting certificate values ...");
659 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200660
Gilles Peskine449bd832023-01-11 14:50:10 +0100661 mbedtls_x509write_crt_set_version(&crt, opt.version);
662 mbedtls_x509write_crt_set_md_alg(&crt, opt.md);
Hanno Becker6c13d372017-09-13 12:49:22 +0100663
Gilles Peskine449bd832023-01-11 14:50:10 +0100664 ret = mbedtls_x509write_crt_set_serial(&crt, &serial);
665 if (ret != 0) {
666 mbedtls_strerror(ret, buf, sizeof(buf));
667 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_serial "
668 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200669 goto exit;
670 }
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 ret = mbedtls_x509write_crt_set_validity(&crt, opt.not_before, opt.not_after);
673 if (ret != 0) {
674 mbedtls_strerror(ret, buf, sizeof(buf));
675 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_validity "
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 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
683 opt.basic_constraints != 0) {
684 mbedtls_printf(" . Adding the Basic Constraints extension ...");
685 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200686
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 ret = mbedtls_x509write_crt_set_basic_constraints(&crt, opt.is_ca,
688 opt.max_pathlen);
689 if (ret != 0) {
690 mbedtls_strerror(ret, buf, sizeof(buf));
691 mbedtls_printf(" failed\n ! x509write_crt_set_basic_constraints "
692 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100693 goto exit;
694 }
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100697 }
Paul Bakker15162a02013-09-06 19:27:21 +0200698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699#if defined(MBEDTLS_SHA1_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
701 opt.subject_identifier != 0) {
702 mbedtls_printf(" . Adding the Subject Key Identifier ...");
703 fflush(stdout);
Hanno Becker6c13d372017-09-13 12:49:22 +0100704
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
706 if (ret != 0) {
707 mbedtls_strerror(ret, buf, sizeof(buf));
708 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_subject"
709 "_key_identifier returned -0x%04x - %s\n\n",
710 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100711 goto exit;
712 }
713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 mbedtls_printf(" ok\n");
Paul Bakker15162a02013-09-06 19:27:21 +0200715 }
716
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
718 opt.authority_identifier != 0) {
719 mbedtls_printf(" . Adding the Authority Key Identifier ...");
720 fflush(stdout);
Paul Bakker15162a02013-09-06 19:27:21 +0200721
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
723 if (ret != 0) {
724 mbedtls_strerror(ret, buf, sizeof(buf));
725 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_authority_"
726 "key_identifier returned -0x%04x - %s\n\n",
727 (unsigned int) -ret, buf);
Hanno Becker6c13d372017-09-13 12:49:22 +0100728 goto exit;
729 }
730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 mbedtls_printf(" ok\n");
Hanno Becker6c13d372017-09-13 12:49:22 +0100732 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733#endif /* MBEDTLS_SHA1_C */
Paul Bakker15162a02013-09-06 19:27:21 +0200734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
736 opt.key_usage != 0) {
737 mbedtls_printf(" . Adding the Key Usage extension ...");
738 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 ret = mbedtls_x509write_crt_set_key_usage(&crt, opt.key_usage);
741 if (ret != 0) {
742 mbedtls_strerror(ret, buf, sizeof(buf));
743 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_key_usage "
744 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200745 goto exit;
746 }
747
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200749 }
750
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (opt.ext_key_usage) {
752 mbedtls_printf(" . Adding the Extended Key Usage extension ...");
753 fflush(stdout);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100754
Gilles Peskine449bd832023-01-11 14:50:10 +0100755 ret = mbedtls_x509write_crt_set_ext_key_usage(&crt, opt.ext_key_usage);
756 if (ret != 0) {
757 mbedtls_strerror(ret, buf, sizeof(buf));
758 mbedtls_printf(
759 " failed\n ! mbedtls_x509write_crt_set_ext_key_usage returned -0x%02x - %s\n\n",
760 (unsigned int) -ret,
761 buf);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100762 goto exit;
763 }
764
Gilles Peskine449bd832023-01-11 14:50:10 +0100765 mbedtls_printf(" ok\n");
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100766 }
767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 if (opt.version == MBEDTLS_X509_CRT_VERSION_3 &&
769 opt.ns_cert_type != 0) {
770 mbedtls_printf(" . Adding the NS Cert Type extension ...");
771 fflush(stdout);
Paul Bakker52be08c2013-09-09 12:37:54 +0200772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 ret = mbedtls_x509write_crt_set_ns_cert_type(&crt, opt.ns_cert_type);
774 if (ret != 0) {
775 mbedtls_strerror(ret, buf, sizeof(buf));
776 mbedtls_printf(" failed\n ! mbedtls_x509write_crt_set_ns_cert_type "
777 "returned -0x%04x - %s\n\n", (unsigned int) -ret, buf);
Paul Bakker52be08c2013-09-09 12:37:54 +0200778 goto exit;
779 }
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 mbedtls_printf(" ok\n");
Paul Bakker52be08c2013-09-09 12:37:54 +0200782 }
783
Paul Bakker9397dcb2013-09-06 09:55:26 +0200784 /*
Hanno Becker25d882b2018-08-23 15:26:06 +0100785 * 1.2. Writing the certificate
Paul Bakker9397dcb2013-09-06 09:55:26 +0200786 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 mbedtls_printf(" . Writing the certificate...");
788 fflush(stdout);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if ((ret = write_certificate(&crt, opt.output_file,
791 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
792 mbedtls_strerror(ret, buf, sizeof(buf));
793 mbedtls_printf(" failed\n ! write_certificate -0x%04x - %s\n\n",
794 (unsigned int) -ret, buf);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200795 goto exit;
796 }
797
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 mbedtls_printf(" ok\n");
Paul Bakker9397dcb2013-09-06 09:55:26 +0200799
Andres Amaya Garciaf9a54d32018-04-29 21:42:45 +0100800 exit_code = MBEDTLS_EXIT_SUCCESS;
801
Paul Bakker9397dcb2013-09-06 09:55:26 +0200802exit:
Hanno Becker30a95102018-10-05 09:49:33 +0100803#if defined(MBEDTLS_X509_CSR_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 mbedtls_x509_csr_free(&csr);
Hanno Becker30a95102018-10-05 09:49:33 +0100805#endif /* MBEDTLS_X509_CSR_PARSE_C */
Gilles Peskine449bd832023-01-11 14:50:10 +0100806 mbedtls_x509_crt_free(&issuer_crt);
807 mbedtls_x509write_crt_free(&crt);
808 mbedtls_pk_free(&loaded_subject_key);
809 mbedtls_pk_free(&loaded_issuer_key);
810 mbedtls_mpi_free(&serial);
811 mbedtls_ctr_drbg_free(&ctr_drbg);
812 mbedtls_entropy_free(&entropy);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814 mbedtls_exit(exit_code);
Paul Bakker9397dcb2013-09-06 09:55:26 +0200815}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200816#endif /* MBEDTLS_X509_CRT_WRITE_C && MBEDTLS_X509_CRT_PARSE_C &&
817 MBEDTLS_FS_IO && MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C &&
Simon Butcher203a6932016-10-07 15:00:17 +0100818 MBEDTLS_ERROR_C && MBEDTLS_PEM_WRITE_C */