blob: 09c2328b1aac422f96ea6ce678daeda561a209da [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate writing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker7c6b2c32013-09-16 13:49:26 +02006 */
7/*
8 * References:
9 * - certificates: RFC 5280, updated by RFC 6818
10 * - CSRs: PKCS#10 v1.7 aka RFC 2986
11 * - attributes: PKCS#9 v2.0 aka RFC 2985
12 */
13
Harry Ramsey0f6bc412024-10-04 10:36:54 +010014#include "x509_internal.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020017
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000018#include "mbedtls/x509_crt.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/asn1write.h"
Janos Follath73c616b2019-12-18 15:07:04 +000020#include "mbedtls/error.h"
Gilles Peskinecd4c0d72025-05-07 23:45:12 +020021#include "mbedtls/oid.h"
Gilles Peskine86a47f82025-05-07 20:20:12 +020022#include "x509_oid.h"
Andrzej Kurek67fdb332023-04-04 06:56:14 -040023#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050024#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard2cd75142023-02-24 12:37:07 +010025#include "mbedtls/md.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020026
Rich Evans00ab4702015-02-06 13:43:58 +000027#include <string.h>
Andrzej Kurek63a6a262023-04-27 08:25:41 -040028#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/pem.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020033
pespacek7599a772022-02-07 14:40:55 +010034#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020035#include "psa_util_internal.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010036#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010037
Gilles Peskine449bd832023-01-11 14:50:10 +010038void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020039{
Gilles Peskine449bd832023-01-11 14:50:10 +010040 memset(ctx, 0, sizeof(mbedtls_x509write_cert));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 ctx->version = MBEDTLS_X509_CRT_VERSION_3;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020043}
44
Gilles Peskine449bd832023-01-11 14:50:10 +010045void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046{
Troy-Butler9ac3e232024-03-22 14:46:04 -040047 if (ctx == NULL) {
48 return;
49 }
50
Gilles Peskine449bd832023-01-11 14:50:10 +010051 mbedtls_asn1_free_named_data_list(&ctx->subject);
52 mbedtls_asn1_free_named_data_list(&ctx->issuer);
53 mbedtls_asn1_free_named_data_list(&ctx->extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Gilles Peskine449bd832023-01-11 14:50:10 +010055 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_cert));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056}
57
Gilles Peskine449bd832023-01-11 14:50:10 +010058void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx,
59 int version)
Paul Bakker5191e922013-10-11 10:54:28 +020060{
61 ctx->version = version;
62}
63
Gilles Peskine449bd832023-01-11 14:50:10 +010064void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx,
65 mbedtls_md_type_t md_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066{
67 ctx->md_alg = md_alg;
68}
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx,
71 mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020072{
73 ctx->subject_key = key;
74}
75
Gilles Peskine449bd832023-01-11 14:50:10 +010076void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx,
77 mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078{
79 ctx->issuer_key = key;
80}
81
Gilles Peskine449bd832023-01-11 14:50:10 +010082int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
83 const char *subject_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084{
Manuel Pégourié-Gonnard2dc6b582025-05-05 16:49:45 +020085 mbedtls_asn1_free_named_data_list(&ctx->subject);
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087}
88
Gilles Peskine449bd832023-01-11 14:50:10 +010089int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
90 const char *issuer_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020091{
Manuel Pégourié-Gonnard2dc6b582025-05-05 16:49:45 +020092 mbedtls_asn1_free_named_data_list(&ctx->issuer);
Gilles Peskine449bd832023-01-11 14:50:10 +010093 return mbedtls_x509_string_to_names(&ctx->issuer, issuer_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094}
95
Valerio Settiaf4815c2023-01-26 17:43:09 +010096int mbedtls_x509write_crt_set_serial_raw(mbedtls_x509write_cert *ctx,
Valerio Setti746def52023-01-10 11:46:34 +010097 unsigned char *serial, size_t serial_len)
Valerio Settida0afcc2023-01-05 16:46:59 +010098{
Valerio Setti746def52023-01-10 11:46:34 +010099 if (serial_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
Valerio Settida0afcc2023-01-05 16:46:59 +0100100 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
101 }
102
Valerio Setti746def52023-01-10 11:46:34 +0100103 ctx->serial_len = serial_len;
104 memcpy(ctx->serial, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107}
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx,
110 const char *not_before,
111 const char *not_after)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112{
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
114 strlen(not_after) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) {
115 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200116 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 strncpy(ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
118 strncpy(ctx->not_after, not_after, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
120 ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123}
124
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400125int mbedtls_x509write_crt_set_subject_alternative_name(mbedtls_x509write_cert *ctx,
126 const mbedtls_x509_san_list *san_list)
127{
Andrzej Kurekc508dc22023-07-07 08:20:02 -0400128 return mbedtls_x509_write_set_san_common(&ctx->extensions, san_list);
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400129}
130
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
133 const char *oid, size_t oid_len,
134 int critical,
135 const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200136{
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
138 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200139}
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
142 int is_ca, int max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200143{
Janos Follath865b3eb2019-12-16 11:46:15 +0000144 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145 unsigned char buf[9];
146 unsigned char *c = buf + sizeof(buf);
147 size_t len = 0;
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 memset(buf, 0, sizeof(buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 if (is_ca && max_pathlen > 127) {
152 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200153 }
154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 if (is_ca) {
156 if (max_pathlen >= 0) {
157 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
158 max_pathlen));
159 }
160 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
161 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
164 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
165 MBEDTLS_ASN1_CONSTRUCTED |
166 MBEDTLS_ASN1_SEQUENCE));
167
168 return
169 mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
170 MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
171 is_ca, buf + sizeof(buf) - len, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172}
173
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100174#if defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100175static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
176 int is_ca,
177 unsigned char tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200178{
Janos Follath865b3eb2019-12-16 11:46:15 +0000179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181 unsigned char *c = buf + sizeof(buf);
182 size_t len = 0;
pespacek7599a772022-02-07 14:40:55 +0100183 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
184 size_t hash_length;
pespacek7599a772022-02-07 14:40:55 +0100185
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 memset(buf, 0, sizeof(buf));
187 MBEDTLS_ASN1_CHK_ADD(len,
188 mbedtls_pk_write_pubkey(&c,
189 buf,
190 is_ca ?
191 ctx->issuer_key :
192 ctx->subject_key));
pespacek30151482022-02-17 15:18:47 +0100193
pespaceka6e955e2022-02-14 15:20:57 +0100194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 status = psa_hash_compute(PSA_ALG_SHA_1,
196 buf + sizeof(buf) - len,
197 len,
198 buf + sizeof(buf) - 20,
199 20,
200 &hash_length);
201 if (status != PSA_SUCCESS) {
202 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100203 }
pespacek7599a772022-02-07 14:40:55 +0100204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 c = buf + sizeof(buf) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200206 len = 20;
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
209 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210
Gilles Peskine449bd832023-01-11 14:50:10 +0100211 if (is_ca) { // writes AuthorityKeyIdentifier sequence
212 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
213 MBEDTLS_ASN1_CHK_ADD(len,
214 mbedtls_asn1_write_tag(&c,
215 buf,
216 MBEDTLS_ASN1_CONSTRUCTED |
217 MBEDTLS_ASN1_SEQUENCE));
pespacek30151482022-02-17 15:18:47 +0100218 }
pespacekd924e552022-02-28 11:49:54 +0100219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 if (is_ca) {
221 return mbedtls_x509write_crt_set_extension(ctx,
222 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
223 MBEDTLS_OID_SIZE(
224 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER),
225 0, buf + sizeof(buf) - len, len);
226 } else {
227 return mbedtls_x509write_crt_set_extension(ctx,
228 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
229 MBEDTLS_OID_SIZE(
230 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER),
231 0, buf + sizeof(buf) - len, len);
232 }
pespaceka6e955e2022-02-14 15:20:57 +0100233}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100236{
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 return mbedtls_x509write_crt_set_key_identifier(ctx,
238 0,
239 MBEDTLS_ASN1_OCTET_STRING);
pespaceka6e955e2022-02-14 15:20:57 +0100240}
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100243{
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 return mbedtls_x509write_crt_set_key_identifier(ctx,
245 1,
246 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200247}
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100248#endif /* PSA_WANT_ALG_SHA_1 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200249
Gilles Peskine449bd832023-01-11 14:50:10 +0100250int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
251 unsigned int key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252{
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 unsigned char buf[5] = { 0 }, ku[2] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200254 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100256 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 MBEDTLS_X509_KU_NON_REPUDIATION |
258 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
259 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
260 MBEDTLS_X509_KU_KEY_AGREEMENT |
261 MBEDTLS_X509_KU_KEY_CERT_SIGN |
262 MBEDTLS_X509_KU_CRL_SIGN |
263 MBEDTLS_X509_KU_ENCIPHER_ONLY |
264 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100266 /* Check that nothing other than the allowed flags is set */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 if ((key_usage & ~allowed_bits) != 0) {
268 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
269 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200270
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100271 c = buf + 5;
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
273 ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200274
Gilles Peskine449bd832023-01-11 14:50:10 +0100275 if (ret < 0) {
276 return ret;
277 } else if (ret < 3 || ret > 5) {
278 return MBEDTLS_ERR_X509_INVALID_FORMAT;
279 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
282 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
283 1, c, (size_t) ret);
284 if (ret != 0) {
285 return ret;
286 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289}
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
292 const mbedtls_asn1_sequence *exts)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100293{
294 unsigned char buf[256];
295 unsigned char *c = buf + sizeof(buf);
296 int ret;
297 size_t len = 0;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100298 const mbedtls_asn1_sequence *last_ext = NULL;
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100299 const mbedtls_asn1_sequence *ext;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 memset(buf, 0, sizeof(buf));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100302
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000303 /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (exts == NULL) {
305 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
306 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100307
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000308 /* Iterate over exts backwards, so we write them out in the requested order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 while (last_ext != exts) {
310 for (ext = exts; ext->next != last_ext; ext = ext->next) {
311 }
312 if (ext->buf.tag != MBEDTLS_ASN1_OID) {
313 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
314 }
315 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len));
316 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len));
317 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID));
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000318 last_ext = ext;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100319 }
320
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
322 MBEDTLS_ASN1_CHK_ADD(len,
323 mbedtls_asn1_write_tag(&c, buf,
324 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100325
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 return mbedtls_x509write_crt_set_extension(ctx,
327 MBEDTLS_OID_EXTENDED_KEY_USAGE,
328 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
329 1, c, len);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100330}
331
Gilles Peskine449bd832023-01-11 14:50:10 +0100332int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
333 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334{
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000337 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338
339 c = buf + 4;
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
342 if (ret < 3 || ret > 4) {
343 return ret;
344 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
347 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
348 0, c, (size_t) ret);
349 if (ret != 0) {
350 return ret;
351 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354}
355
Gilles Peskine449bd832023-01-11 14:50:10 +0100356static int x509_write_time(unsigned char **p, unsigned char *start,
357 const char *t, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358{
Janos Follath865b3eb2019-12-16 11:46:15 +0000359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200360 size_t len = 0;
361
362 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200363 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
366 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
367 (const unsigned char *) t + 2,
368 size - 2));
369 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
370 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
371 MBEDTLS_ASN1_UTC_TIME));
372 } else {
373 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
374 (const unsigned char *) t,
375 size));
376 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
377 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
378 MBEDTLS_ASN1_GENERALIZED_TIME));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379 }
380
Gilles Peskine449bd832023-01-11 14:50:10 +0100381 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200382}
383
Gilles Peskine449bd832023-01-11 14:50:10 +0100384int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000385 unsigned char *buf, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386{
Janos Follath865b3eb2019-12-16 11:46:15 +0000387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388 const char *sig_oid;
389 size_t sig_oid_len = 0;
390 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100391 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100392 size_t hash_length = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +0200393 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100394 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
395 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +0100396
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
398 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399 mbedtls_pk_type_t pk_alg;
Marek Jansta8bde6492022-11-07 12:38:38 +0100400 int write_sig_null_par;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200401
402 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100403 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100405 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
407 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100408
409 /* There's no direct way of extracting a signature algorithm
410 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100412 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200414 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 } else {
416 return MBEDTLS_ERR_X509_INVALID_ALG;
417 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200418
Gilles Peskine532e3ee2025-05-07 20:37:15 +0200419 if ((ret = mbedtls_x509_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
420 &sig_oid, &sig_oid_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100421 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422 }
423
424 /*
425 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
426 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100427
428 /* Only for v3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
430 MBEDTLS_ASN1_CHK_ADD(len,
431 mbedtls_x509_write_extensions(&c,
432 buf, ctx->extensions));
433 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
434 MBEDTLS_ASN1_CHK_ADD(len,
435 mbedtls_asn1_write_tag(&c, buf,
436 MBEDTLS_ASN1_CONSTRUCTED |
437 MBEDTLS_ASN1_SEQUENCE));
438 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
439 MBEDTLS_ASN1_CHK_ADD(len,
440 mbedtls_asn1_write_tag(&c, buf,
441 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
442 MBEDTLS_ASN1_CONSTRUCTED | 3));
Hanno Beckerd7f35202017-09-13 12:00:15 +0100443 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
445 /*
446 * SubjectPublicKeyInfo
447 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 MBEDTLS_ASN1_CHK_ADD(pub_len,
449 mbedtls_pk_write_pubkey_der(ctx->subject_key,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000450 buf, (size_t) (c - buf)));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200451 c -= pub_len;
452 len += pub_len;
453
454 /*
455 * Subject ::= Name
456 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 MBEDTLS_ASN1_CHK_ADD(len,
458 mbedtls_x509_write_names(&c, buf,
459 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 /*
462 * Validity ::= SEQUENCE {
463 * notBefore Time,
464 * notAfter Time }
465 */
466 sub_len = 0;
467
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 MBEDTLS_ASN1_CHK_ADD(sub_len,
469 x509_write_time(&c, buf, ctx->not_after,
470 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200471
Gilles Peskine449bd832023-01-11 14:50:10 +0100472 MBEDTLS_ASN1_CHK_ADD(sub_len,
473 x509_write_time(&c, buf, ctx->not_before,
474 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
476 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
478 MBEDTLS_ASN1_CHK_ADD(len,
479 mbedtls_asn1_write_tag(&c, buf,
480 MBEDTLS_ASN1_CONSTRUCTED |
481 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200482
483 /*
484 * Issuer ::= Name
485 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100486 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
487 ctx->issuer));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488
489 /*
490 * Signature ::= AlgorithmIdentifier
491 */
Marek Jansta8bde6492022-11-07 12:38:38 +0100492 if (pk_alg == MBEDTLS_PK_ECDSA) {
493 /*
494 * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
495 * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
496 * https://www.rfc-editor.org/rfc/rfc5758#section-3.
497 */
498 write_sig_null_par = 0;
499 } else {
500 write_sig_null_par = 1;
501 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 MBEDTLS_ASN1_CHK_ADD(len,
Marek Jansta8bde6492022-11-07 12:38:38 +0100503 mbedtls_asn1_write_algorithm_identifier_ext(&c, buf,
504 sig_oid, strlen(sig_oid),
505 0, write_sig_null_par));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507 /*
508 * Serial ::= INTEGER
Valerio Settida0afcc2023-01-05 16:46:59 +0100509 *
510 * Written data is:
Valerio Setti4752aac2023-01-10 16:00:15 +0100511 * - "ctx->serial_len" bytes for the raw serial buffer
512 * - if MSb of "serial" is 1, then prepend an extra 0x00 byte
Valerio Settida0afcc2023-01-05 16:46:59 +0100513 * - 1 byte for the length
514 * - 1 byte for the TAG
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515 */
Valerio Settida0afcc2023-01-05 16:46:59 +0100516 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf,
517 ctx->serial, ctx->serial_len));
Valerio Setti4752aac2023-01-10 16:00:15 +0100518 if (*c & 0x80) {
519 if (c - buf < 1) {
520 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
521 }
Valerio Setti856cec42023-01-12 14:56:54 +0100522 *(--c) = 0x0;
Valerio Setti4752aac2023-01-10 16:00:15 +0100523 len++;
524 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
525 ctx->serial_len + 1));
526 } else {
527 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
528 ctx->serial_len));
529 }
Valerio Settida0afcc2023-01-05 16:46:59 +0100530 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
531 MBEDTLS_ASN1_INTEGER));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532
533 /*
534 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
535 */
Hanno Becker47698652017-09-13 11:59:26 +0100536
537 /* Can be omitted for v1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
Hanno Becker47698652017-09-13 11:59:26 +0100539 sub_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 MBEDTLS_ASN1_CHK_ADD(sub_len,
541 mbedtls_asn1_write_int(&c, buf, ctx->version));
Hanno Becker47698652017-09-13 11:59:26 +0100542 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 MBEDTLS_ASN1_CHK_ADD(len,
544 mbedtls_asn1_write_len(&c, buf, sub_len));
545 MBEDTLS_ASN1_CHK_ADD(len,
546 mbedtls_asn1_write_tag(&c, buf,
547 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
548 MBEDTLS_ASN1_CONSTRUCTED | 0));
Hanno Becker47698652017-09-13 11:59:26 +0100549 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
552 MBEDTLS_ASN1_CHK_ADD(len,
553 mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
554 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200555
556 /*
557 * Make signature
558 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100559
560 /* Compute hash of CRT. */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200561 psa_algorithm = mbedtls_md_psa_alg_from_type(ctx->md_alg);
pespacek7599a772022-02-07 14:40:55 +0100562
Gilles Peskine449bd832023-01-11 14:50:10 +0100563 status = psa_hash_compute(psa_algorithm,
564 c,
565 len,
566 hash,
567 sizeof(hash),
568 &hash_length);
569 if (status != PSA_SUCCESS) {
570 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100571 }
pespacek7599a772022-02-07 14:40:55 +0100572
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000575 hash, hash_length, sig, sizeof(sig), &sig_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577 }
578
Hanno Beckerdef43052019-05-04 07:54:36 +0100579 /* Move CRT to the front of the buffer to have space
580 * for the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 memmove(buf, c, len);
Hanno Beckerdef43052019-05-04 07:54:36 +0100582 c = buf + len;
583
584 /* Add signature at the end of the buffer,
585 * making sure that it doesn't underflow
586 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200587 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
Marek Jansta8bde6492022-11-07 12:38:38 +0100589 sig_oid, sig_oid_len,
590 sig, sig_len, pk_alg));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
Hanno Beckerdef43052019-05-04 07:54:36 +0100592 /*
593 * Memory layout after this step:
594 *
595 * buf c=buf+len c2 buf+size
596 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
597 */
Andres AG60dbc932016-09-02 15:23:48 +0100598
Hanno Beckerdef43052019-05-04 07:54:36 +0100599 /* Move raw CRT to just before the signature. */
600 c = c2 - len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 memmove(c, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200602
603 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
605 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
606 MBEDTLS_ASN1_CONSTRUCTED |
607 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200610}
611
612#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
613#define PEM_END_CRT "-----END CERTIFICATE-----\n"
614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100616int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000617 unsigned char *buf, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200618{
Janos Follath865b3eb2019-12-16 11:46:15 +0000619 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100620 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621
Ben Taylor440cb2a2025-03-05 09:40:08 +0000622 if ((ret = mbedtls_x509write_crt_der(crt, buf, size)) < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100623 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200624 }
625
Gilles Peskine449bd832023-01-11 14:50:10 +0100626 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
627 buf + size - ret, ret,
628 buf, size, &olen)) != 0) {
629 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200630 }
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200636#endif /* MBEDTLS_X509_CRT_WRITE_C */