blob: 932d28d435807f6427617e6b878e3071262caf84 [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"
21#include "mbedtls/oid.h"
Andrzej Kurek67fdb332023-04-04 06:56:14 -040022#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050023#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard2cd75142023-02-24 12:37:07 +010024#include "mbedtls/md.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020025
Rich Evans00ab4702015-02-06 13:43:58 +000026#include <string.h>
Andrzej Kurek63a6a262023-04-27 08:25:41 -040027#include <stdint.h>
Rich Evans00ab4702015-02-06 13:43:58 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/pem.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020031#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020032
pespacek7599a772022-02-07 14:40:55 +010033#include "psa/crypto.h"
Manuel Pégourié-Gonnard2be8c632023-06-07 13:07:21 +020034#include "psa_util_internal.h"
Valerio Setti384fbde2024-01-02 13:26:40 +010035#include "mbedtls/psa_util.h"
pespacek7599a772022-02-07 14:40:55 +010036
Gilles Peskine449bd832023-01-11 14:50:10 +010037void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020038{
Gilles Peskine449bd832023-01-11 14:50:10 +010039 memset(ctx, 0, sizeof(mbedtls_x509write_cert));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 ctx->version = MBEDTLS_X509_CRT_VERSION_3;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042}
43
Gilles Peskine449bd832023-01-11 14:50:10 +010044void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020045{
Troy-Butler9ac3e232024-03-22 14:46:04 -040046 if (ctx == NULL) {
47 return;
48 }
49
Gilles Peskine449bd832023-01-11 14:50:10 +010050 mbedtls_asn1_free_named_data_list(&ctx->subject);
51 mbedtls_asn1_free_named_data_list(&ctx->issuer);
52 mbedtls_asn1_free_named_data_list(&ctx->extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020053
Gilles Peskine449bd832023-01-11 14:50:10 +010054 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_cert));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020055}
56
Gilles Peskine449bd832023-01-11 14:50:10 +010057void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx,
58 int version)
Paul Bakker5191e922013-10-11 10:54:28 +020059{
60 ctx->version = version;
61}
62
Gilles Peskine449bd832023-01-11 14:50:10 +010063void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx,
64 mbedtls_md_type_t md_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065{
66 ctx->md_alg = md_alg;
67}
68
Gilles Peskine449bd832023-01-11 14:50:10 +010069void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx,
70 mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020071{
72 ctx->subject_key = key;
73}
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx,
76 mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020077{
78 ctx->issuer_key = key;
79}
80
Gilles Peskine449bd832023-01-11 14:50:10 +010081int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
82 const char *subject_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020083{
Manuel Pégourié-Gonnard2dc6b582025-05-05 16:49:45 +020084 mbedtls_asn1_free_named_data_list(&ctx->subject);
Gilles Peskine449bd832023-01-11 14:50:10 +010085 return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020086}
87
Gilles Peskine449bd832023-01-11 14:50:10 +010088int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
89 const char *issuer_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090{
Manuel Pégourié-Gonnard2dc6b582025-05-05 16:49:45 +020091 mbedtls_asn1_free_named_data_list(&ctx->issuer);
Gilles Peskine449bd832023-01-11 14:50:10 +010092 return mbedtls_x509_string_to_names(&ctx->issuer, issuer_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093}
94
Valerio Settiaf4815c2023-01-26 17:43:09 +010095int mbedtls_x509write_crt_set_serial_raw(mbedtls_x509write_cert *ctx,
Valerio Setti746def52023-01-10 11:46:34 +010096 unsigned char *serial, size_t serial_len)
Valerio Settida0afcc2023-01-05 16:46:59 +010097{
Valerio Setti746def52023-01-10 11:46:34 +010098 if (serial_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
Valerio Settida0afcc2023-01-05 16:46:59 +010099 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
100 }
101
Valerio Setti746def52023-01-10 11:46:34 +0100102 ctx->serial_len = serial_len;
103 memcpy(ctx->serial, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106}
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx,
109 const char *not_before,
110 const char *not_after)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200111{
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
113 strlen(not_after) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) {
114 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 strncpy(ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
117 strncpy(ctx->not_after, not_after, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
119 ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122}
123
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400124int mbedtls_x509write_crt_set_subject_alternative_name(mbedtls_x509write_cert *ctx,
125 const mbedtls_x509_san_list *san_list)
126{
Andrzej Kurekc508dc22023-07-07 08:20:02 -0400127 return mbedtls_x509_write_set_san_common(&ctx->extensions, san_list);
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400128}
129
130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
132 const char *oid, size_t oid_len,
133 int critical,
134 const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135{
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
137 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138}
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
141 int is_ca, int max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142{
Janos Follath865b3eb2019-12-16 11:46:15 +0000143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144 unsigned char buf[9];
145 unsigned char *c = buf + sizeof(buf);
146 size_t len = 0;
147
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 memset(buf, 0, sizeof(buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (is_ca && max_pathlen > 127) {
151 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152 }
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (is_ca) {
155 if (max_pathlen >= 0) {
156 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
157 max_pathlen));
158 }
159 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
160 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
163 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
164 MBEDTLS_ASN1_CONSTRUCTED |
165 MBEDTLS_ASN1_SEQUENCE));
166
167 return
168 mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
169 MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
170 is_ca, buf + sizeof(buf) - len, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171}
172
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100173#if defined(PSA_WANT_ALG_SHA_1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100174static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
175 int is_ca,
176 unsigned char tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200177{
Janos Follath865b3eb2019-12-16 11:46:15 +0000178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180 unsigned char *c = buf + sizeof(buf);
181 size_t len = 0;
pespacek7599a772022-02-07 14:40:55 +0100182 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
183 size_t hash_length;
pespacek7599a772022-02-07 14:40:55 +0100184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 memset(buf, 0, sizeof(buf));
186 MBEDTLS_ASN1_CHK_ADD(len,
187 mbedtls_pk_write_pubkey(&c,
188 buf,
189 is_ca ?
190 ctx->issuer_key :
191 ctx->subject_key));
pespacek30151482022-02-17 15:18:47 +0100192
pespaceka6e955e2022-02-14 15:20:57 +0100193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 status = psa_hash_compute(PSA_ALG_SHA_1,
195 buf + sizeof(buf) - len,
196 len,
197 buf + sizeof(buf) - 20,
198 20,
199 &hash_length);
200 if (status != PSA_SUCCESS) {
201 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100202 }
pespacek7599a772022-02-07 14:40:55 +0100203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 c = buf + sizeof(buf) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205 len = 20;
206
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
208 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (is_ca) { // writes AuthorityKeyIdentifier sequence
211 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
212 MBEDTLS_ASN1_CHK_ADD(len,
213 mbedtls_asn1_write_tag(&c,
214 buf,
215 MBEDTLS_ASN1_CONSTRUCTED |
216 MBEDTLS_ASN1_SEQUENCE));
pespacek30151482022-02-17 15:18:47 +0100217 }
pespacekd924e552022-02-28 11:49:54 +0100218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (is_ca) {
220 return mbedtls_x509write_crt_set_extension(ctx,
221 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
222 MBEDTLS_OID_SIZE(
223 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER),
224 0, buf + sizeof(buf) - len, len);
225 } else {
226 return mbedtls_x509write_crt_set_extension(ctx,
227 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
228 MBEDTLS_OID_SIZE(
229 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER),
230 0, buf + sizeof(buf) - len, len);
231 }
pespaceka6e955e2022-02-14 15:20:57 +0100232}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200233
Gilles Peskine449bd832023-01-11 14:50:10 +0100234int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100235{
Gilles Peskine449bd832023-01-11 14:50:10 +0100236 return mbedtls_x509write_crt_set_key_identifier(ctx,
237 0,
238 MBEDTLS_ASN1_OCTET_STRING);
pespaceka6e955e2022-02-14 15:20:57 +0100239}
240
Gilles Peskine449bd832023-01-11 14:50:10 +0100241int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100242{
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 return mbedtls_x509write_crt_set_key_identifier(ctx,
244 1,
245 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200246}
Elena Uziunaite9fc5be02024-09-04 18:12:59 +0100247#endif /* PSA_WANT_ALG_SHA_1 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
250 unsigned int key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200251{
Gilles Peskine449bd832023-01-11 14:50:10 +0100252 unsigned char buf[5] = { 0 }, ku[2] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200253 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000254 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100255 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
Gilles Peskine449bd832023-01-11 14:50:10 +0100256 MBEDTLS_X509_KU_NON_REPUDIATION |
257 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
258 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
259 MBEDTLS_X509_KU_KEY_AGREEMENT |
260 MBEDTLS_X509_KU_KEY_CERT_SIGN |
261 MBEDTLS_X509_KU_CRL_SIGN |
262 MBEDTLS_X509_KU_ENCIPHER_ONLY |
263 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200264
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100265 /* Check that nothing other than the allowed flags is set */
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 if ((key_usage & ~allowed_bits) != 0) {
267 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
268 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100270 c = buf + 5;
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
272 ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200273
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 if (ret < 0) {
275 return ret;
276 } else if (ret < 3 || ret > 5) {
277 return MBEDTLS_ERR_X509_INVALID_FORMAT;
278 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
281 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
282 1, c, (size_t) ret);
283 if (ret != 0) {
284 return ret;
285 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288}
289
Gilles Peskine449bd832023-01-11 14:50:10 +0100290int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
291 const mbedtls_asn1_sequence *exts)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100292{
293 unsigned char buf[256];
294 unsigned char *c = buf + sizeof(buf);
295 int ret;
296 size_t len = 0;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100297 const mbedtls_asn1_sequence *last_ext = NULL;
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100298 const mbedtls_asn1_sequence *ext;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100299
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 memset(buf, 0, sizeof(buf));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100301
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000302 /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (exts == NULL) {
304 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
305 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100306
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000307 /* Iterate over exts backwards, so we write them out in the requested order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 while (last_ext != exts) {
309 for (ext = exts; ext->next != last_ext; ext = ext->next) {
310 }
311 if (ext->buf.tag != MBEDTLS_ASN1_OID) {
312 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
313 }
314 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len));
315 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len));
316 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID));
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000317 last_ext = ext;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100318 }
319
Gilles Peskine449bd832023-01-11 14:50:10 +0100320 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
321 MBEDTLS_ASN1_CHK_ADD(len,
322 mbedtls_asn1_write_tag(&c, buf,
323 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 return mbedtls_x509write_crt_set_extension(ctx,
326 MBEDTLS_OID_EXTENDED_KEY_USAGE,
327 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
328 1, c, len);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100329}
330
Gilles Peskine449bd832023-01-11 14:50:10 +0100331int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
332 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200333{
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200335 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337
338 c = buf + 4;
339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
341 if (ret < 3 || ret > 4) {
342 return ret;
343 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
346 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
347 0, c, (size_t) ret);
348 if (ret != 0) {
349 return ret;
350 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200351
Gilles Peskine449bd832023-01-11 14:50:10 +0100352 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200353}
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355static int x509_write_time(unsigned char **p, unsigned char *start,
356 const char *t, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357{
Janos Follath865b3eb2019-12-16 11:46:15 +0000358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200359 size_t len = 0;
360
361 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
365 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
366 (const unsigned char *) t + 2,
367 size - 2));
368 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
369 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
370 MBEDTLS_ASN1_UTC_TIME));
371 } else {
372 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
373 (const unsigned char *) t,
374 size));
375 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
376 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
377 MBEDTLS_ASN1_GENERALIZED_TIME));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200378 }
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200381}
382
Gilles Peskine449bd832023-01-11 14:50:10 +0100383int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000384 unsigned char *buf, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385{
Janos Follath865b3eb2019-12-16 11:46:15 +0000386 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200387 const char *sig_oid;
388 size_t sig_oid_len = 0;
389 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100390 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100391 size_t hash_length = 0;
Manuel Pégourié-Gonnard88579842023-03-28 11:20:23 +0200392 unsigned char hash[MBEDTLS_MD_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100393 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
394 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +0100395
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200396 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
397 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200398 mbedtls_pk_type_t pk_alg;
Marek Jansta8bde6492022-11-07 12:38:38 +0100399 int write_sig_null_par;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200400
401 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100402 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200403 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100404 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405
406 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100407
408 /* There's no direct way of extracting a signature algorithm
409 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100411 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200413 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 } else {
415 return MBEDTLS_ERR_X509_INVALID_ALG;
416 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
419 &sig_oid, &sig_oid_len)) != 0) {
420 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421 }
422
423 /*
424 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
425 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100426
427 /* Only for v3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100428 if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
429 MBEDTLS_ASN1_CHK_ADD(len,
430 mbedtls_x509_write_extensions(&c,
431 buf, ctx->extensions));
432 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
433 MBEDTLS_ASN1_CHK_ADD(len,
434 mbedtls_asn1_write_tag(&c, buf,
435 MBEDTLS_ASN1_CONSTRUCTED |
436 MBEDTLS_ASN1_SEQUENCE));
437 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
438 MBEDTLS_ASN1_CHK_ADD(len,
439 mbedtls_asn1_write_tag(&c, buf,
440 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
441 MBEDTLS_ASN1_CONSTRUCTED | 3));
Hanno Beckerd7f35202017-09-13 12:00:15 +0100442 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
444 /*
445 * SubjectPublicKeyInfo
446 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 MBEDTLS_ASN1_CHK_ADD(pub_len,
448 mbedtls_pk_write_pubkey_der(ctx->subject_key,
Dave Rodgmane4a6f5a2023-11-04 12:20:09 +0000449 buf, (size_t) (c - buf)));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200450 c -= pub_len;
451 len += pub_len;
452
453 /*
454 * Subject ::= Name
455 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 MBEDTLS_ASN1_CHK_ADD(len,
457 mbedtls_x509_write_names(&c, buf,
458 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
460 /*
461 * Validity ::= SEQUENCE {
462 * notBefore Time,
463 * notAfter Time }
464 */
465 sub_len = 0;
466
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 MBEDTLS_ASN1_CHK_ADD(sub_len,
468 x509_write_time(&c, buf, ctx->not_after,
469 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 MBEDTLS_ASN1_CHK_ADD(sub_len,
472 x509_write_time(&c, buf, ctx->not_before,
473 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200474
475 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
477 MBEDTLS_ASN1_CHK_ADD(len,
478 mbedtls_asn1_write_tag(&c, buf,
479 MBEDTLS_ASN1_CONSTRUCTED |
480 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
482 /*
483 * Issuer ::= Name
484 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
486 ctx->issuer));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200487
488 /*
489 * Signature ::= AlgorithmIdentifier
490 */
Marek Jansta8bde6492022-11-07 12:38:38 +0100491 if (pk_alg == MBEDTLS_PK_ECDSA) {
492 /*
493 * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
494 * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
495 * https://www.rfc-editor.org/rfc/rfc5758#section-3.
496 */
497 write_sig_null_par = 0;
498 } else {
499 write_sig_null_par = 1;
500 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 MBEDTLS_ASN1_CHK_ADD(len,
Marek Jansta8bde6492022-11-07 12:38:38 +0100502 mbedtls_asn1_write_algorithm_identifier_ext(&c, buf,
503 sig_oid, strlen(sig_oid),
504 0, write_sig_null_par));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
506 /*
507 * Serial ::= INTEGER
Valerio Settida0afcc2023-01-05 16:46:59 +0100508 *
509 * Written data is:
Valerio Setti4752aac2023-01-10 16:00:15 +0100510 * - "ctx->serial_len" bytes for the raw serial buffer
511 * - if MSb of "serial" is 1, then prepend an extra 0x00 byte
Valerio Settida0afcc2023-01-05 16:46:59 +0100512 * - 1 byte for the length
513 * - 1 byte for the TAG
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200514 */
Valerio Settida0afcc2023-01-05 16:46:59 +0100515 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf,
516 ctx->serial, ctx->serial_len));
Valerio Setti4752aac2023-01-10 16:00:15 +0100517 if (*c & 0x80) {
518 if (c - buf < 1) {
519 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
520 }
Valerio Setti856cec42023-01-12 14:56:54 +0100521 *(--c) = 0x0;
Valerio Setti4752aac2023-01-10 16:00:15 +0100522 len++;
523 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
524 ctx->serial_len + 1));
525 } else {
526 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
527 ctx->serial_len));
528 }
Valerio Settida0afcc2023-01-05 16:46:59 +0100529 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
530 MBEDTLS_ASN1_INTEGER));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200531
532 /*
533 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
534 */
Hanno Becker47698652017-09-13 11:59:26 +0100535
536 /* Can be omitted for v1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
Hanno Becker47698652017-09-13 11:59:26 +0100538 sub_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 MBEDTLS_ASN1_CHK_ADD(sub_len,
540 mbedtls_asn1_write_int(&c, buf, ctx->version));
Hanno Becker47698652017-09-13 11:59:26 +0100541 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 MBEDTLS_ASN1_CHK_ADD(len,
543 mbedtls_asn1_write_len(&c, buf, sub_len));
544 MBEDTLS_ASN1_CHK_ADD(len,
545 mbedtls_asn1_write_tag(&c, buf,
546 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
547 MBEDTLS_ASN1_CONSTRUCTED | 0));
Hanno Becker47698652017-09-13 11:59:26 +0100548 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
551 MBEDTLS_ASN1_CHK_ADD(len,
552 mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
553 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
555 /*
556 * Make signature
557 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100558
559 /* Compute hash of CRT. */
Manuel Pégourié-Gonnard2d6d9932023-03-28 11:38:08 +0200560 psa_algorithm = mbedtls_md_psa_alg_from_type(ctx->md_alg);
pespacek7599a772022-02-07 14:40:55 +0100561
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 status = psa_hash_compute(psa_algorithm,
563 c,
564 len,
565 hash,
566 sizeof(hash),
567 &hash_length);
568 if (status != PSA_SUCCESS) {
569 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100570 }
pespacek7599a772022-02-07 14:40:55 +0100571
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000574 hash, hash_length, sig, sizeof(sig), &sig_len)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576 }
577
Hanno Beckerdef43052019-05-04 07:54:36 +0100578 /* Move CRT to the front of the buffer to have space
579 * for the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 memmove(buf, c, len);
Hanno Beckerdef43052019-05-04 07:54:36 +0100581 c = buf + len;
582
583 /* Add signature at the end of the buffer,
584 * making sure that it doesn't underflow
585 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200586 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100587 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
Marek Jansta8bde6492022-11-07 12:38:38 +0100588 sig_oid, sig_oid_len,
589 sig, sig_len, pk_alg));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
Hanno Beckerdef43052019-05-04 07:54:36 +0100591 /*
592 * Memory layout after this step:
593 *
594 * buf c=buf+len c2 buf+size
595 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
596 */
Andres AG60dbc932016-09-02 15:23:48 +0100597
Hanno Beckerdef43052019-05-04 07:54:36 +0100598 /* Move raw CRT to just before the signature. */
599 c = c2 - len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 memmove(c, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601
602 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
604 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
605 MBEDTLS_ASN1_CONSTRUCTED |
606 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200609}
610
611#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
612#define PEM_END_CRT "-----END CERTIFICATE-----\n"
613
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100615int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt,
Ben Taylor440cb2a2025-03-05 09:40:08 +0000616 unsigned char *buf, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200617{
Janos Follath865b3eb2019-12-16 11:46:15 +0000618 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100619 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620
Ben Taylor440cb2a2025-03-05 09:40:08 +0000621 if ((ret = mbedtls_x509write_crt_der(crt, buf, size)) < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200623 }
624
Gilles Peskine449bd832023-01-11 14:50:10 +0100625 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
626 buf + size - ret, ret,
627 buf, size, &olen)) != 0) {
628 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200629 }
630
Gilles Peskine449bd832023-01-11 14:50:10 +0100631 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635#endif /* MBEDTLS_X509_CRT_WRITE_C */