blob: bac849f09ee9d53126ae95f288c071e797ea6ea3 [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
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 Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * References:
21 * - certificates: RFC 5280, updated by RFC 6818
22 * - CSRs: PKCS#10 v1.7 aka RFC 2986
23 * - attributes: PKCS#9 v2.0 aka RFC 2985
24 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/x509_crt.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/asn1write.h"
Janos Follath73c616b2019-12-18 15:07:04 +000032#include "mbedtls/error.h"
33#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050034#include "mbedtls/platform_util.h"
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/sha1.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <string.h>
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/pem.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
pespacek7599a772022-02-07 14:40:55 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "psa/crypto.h"
45#include "mbedtls/psa_util.h"
46#endif /* MBEDTLS_USE_PSA_CRYPTO */
47
Przemek Stekiel40afdd22022-09-06 13:08:28 +020048#include "hash_info.h"
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Przemek Stekielfd183662022-08-02 15:29:20 +020050
Gilles Peskine449bd832023-01-11 14:50:10 +010051void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052{
Gilles Peskine449bd832023-01-11 14:50:10 +010053 memset(ctx, 0, sizeof(mbedtls_x509write_cert));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 ctx->version = MBEDTLS_X509_CRT_VERSION_3;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020056}
57
Gilles Peskine449bd832023-01-11 14:50:10 +010058void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059{
Gilles Peskine449bd832023-01-11 14:50:10 +010060 mbedtls_asn1_free_named_data_list(&ctx->subject);
61 mbedtls_asn1_free_named_data_list(&ctx->issuer);
62 mbedtls_asn1_free_named_data_list(&ctx->extensions);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063
Gilles Peskine449bd832023-01-11 14:50:10 +010064 mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_cert));
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065}
66
Gilles Peskine449bd832023-01-11 14:50:10 +010067void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx,
68 int version)
Paul Bakker5191e922013-10-11 10:54:28 +020069{
70 ctx->version = version;
71}
72
Gilles Peskine449bd832023-01-11 14:50:10 +010073void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx,
74 mbedtls_md_type_t md_alg)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075{
76 ctx->md_alg = md_alg;
77}
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx,
80 mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081{
82 ctx->subject_key = key;
83}
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx,
86 mbedtls_pk_context *key)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087{
88 ctx->issuer_key = key;
89}
90
Gilles Peskine449bd832023-01-11 14:50:10 +010091int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
92 const char *subject_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093{
Gilles Peskine449bd832023-01-11 14:50:10 +010094 return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095}
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
98 const char *issuer_name)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099{
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 return mbedtls_x509_string_to_names(&ctx->issuer, issuer_name);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101}
102
Valerio Setti5d164c42023-01-09 12:19:40 +0100103#if defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100104int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx,
105 const mbedtls_mpi *serial)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200106{
Valerio Settida0afcc2023-01-05 16:46:59 +0100107 int ret;
Valerio Settida0afcc2023-01-05 16:46:59 +0100108 size_t tmp_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109
Valerio Settida0afcc2023-01-05 16:46:59 +0100110 /* Ensure that the MPI value fits into the buffer */
111 tmp_len = mbedtls_mpi_size(serial);
112 if (tmp_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
113 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
114 }
115
116 ctx->serial_len = tmp_len;
117
Valerio Setti4752aac2023-01-10 16:00:15 +0100118 ret = mbedtls_mpi_write_binary(serial, ctx->serial, tmp_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100119 if (ret < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 return ret;
121 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
Valerio Settida0afcc2023-01-05 16:46:59 +0100123 return 0;
124}
Valerio Setti5d164c42023-01-09 12:19:40 +0100125#endif // MBEDTLS_BIGNUM_C && !MBEDTLS_DEPRECATED_REMOVED
Valerio Settida0afcc2023-01-05 16:46:59 +0100126
127int mbedtls_x509write_crt_set_serial_new(mbedtls_x509write_cert *ctx,
Valerio Setti746def52023-01-10 11:46:34 +0100128 unsigned char *serial, size_t serial_len)
Valerio Settida0afcc2023-01-05 16:46:59 +0100129{
Valerio Setti746def52023-01-10 11:46:34 +0100130 if (serial_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
Valerio Settida0afcc2023-01-05 16:46:59 +0100131 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
132 }
133
Valerio Setti746def52023-01-10 11:46:34 +0100134 ctx->serial_len = serial_len;
135 memcpy(ctx->serial, serial, serial_len);
Valerio Settida0afcc2023-01-05 16:46:59 +0100136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138}
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx,
141 const char *not_before,
142 const char *not_after)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200143{
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
145 strlen(not_after) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) {
146 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 strncpy(ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
149 strncpy(ctx->not_after, not_after, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
151 ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154}
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
157 const char *oid, size_t oid_len,
158 int critical,
159 const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200160{
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
162 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163}
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
166 int is_ca, int max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167{
Janos Follath865b3eb2019-12-16 11:46:15 +0000168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200169 unsigned char buf[9];
170 unsigned char *c = buf + sizeof(buf);
171 size_t len = 0;
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 memset(buf, 0, sizeof(buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (is_ca && max_pathlen > 127) {
176 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200177 }
178
Gilles Peskine449bd832023-01-11 14:50:10 +0100179 if (is_ca) {
180 if (max_pathlen >= 0) {
181 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
182 max_pathlen));
183 }
184 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
185 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
188 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
189 MBEDTLS_ASN1_CONSTRUCTED |
190 MBEDTLS_ASN1_SEQUENCE));
191
192 return
193 mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
194 MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
195 is_ca, buf + sizeof(buf) - len, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200196}
197
Przemek Stekiel76b753b2022-08-09 10:54:45 +0200198#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100199static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
200 int is_ca,
201 unsigned char tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202{
Janos Follath865b3eb2019-12-16 11:46:15 +0000203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200205 unsigned char *c = buf + sizeof(buf);
206 size_t len = 0;
pespacek7599a772022-02-07 14:40:55 +0100207#if defined(MBEDTLS_USE_PSA_CRYPTO)
208 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
209 size_t hash_length;
210#endif /* MBEDTLS_USE_PSA_CRYPTO */
211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 memset(buf, 0, sizeof(buf));
213 MBEDTLS_ASN1_CHK_ADD(len,
214 mbedtls_pk_write_pubkey(&c,
215 buf,
216 is_ca ?
217 ctx->issuer_key :
218 ctx->subject_key));
pespacek30151482022-02-17 15:18:47 +0100219
pespaceka6e955e2022-02-14 15:20:57 +0100220
pespacek7599a772022-02-07 14:40:55 +0100221#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100222 status = psa_hash_compute(PSA_ALG_SHA_1,
223 buf + sizeof(buf) - len,
224 len,
225 buf + sizeof(buf) - 20,
226 20,
227 &hash_length);
228 if (status != PSA_SUCCESS) {
229 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100230 }
231#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 ret = mbedtls_sha1(buf + sizeof(buf) - len, len,
233 buf + sizeof(buf) - 20);
234 if (ret != 0) {
235 return ret;
236 }
pespacek7599a772022-02-07 14:40:55 +0100237#endif /* MBEDTLS_USE_PSA_CRYPTO */
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 c = buf + sizeof(buf) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200240 len = 20;
241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
243 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200244
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (is_ca) { // writes AuthorityKeyIdentifier sequence
246 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
247 MBEDTLS_ASN1_CHK_ADD(len,
248 mbedtls_asn1_write_tag(&c,
249 buf,
250 MBEDTLS_ASN1_CONSTRUCTED |
251 MBEDTLS_ASN1_SEQUENCE));
pespacek30151482022-02-17 15:18:47 +0100252 }
pespacekd924e552022-02-28 11:49:54 +0100253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (is_ca) {
255 return mbedtls_x509write_crt_set_extension(ctx,
256 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
257 MBEDTLS_OID_SIZE(
258 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER),
259 0, buf + sizeof(buf) - len, len);
260 } else {
261 return mbedtls_x509write_crt_set_extension(ctx,
262 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
263 MBEDTLS_OID_SIZE(
264 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER),
265 0, buf + sizeof(buf) - len, len);
266 }
pespaceka6e955e2022-02-14 15:20:57 +0100267}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200268
Gilles Peskine449bd832023-01-11 14:50:10 +0100269int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100270{
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 return mbedtls_x509write_crt_set_key_identifier(ctx,
272 0,
273 MBEDTLS_ASN1_OCTET_STRING);
pespaceka6e955e2022-02-14 15:20:57 +0100274}
275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100277{
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 return mbedtls_x509write_crt_set_key_identifier(ctx,
279 1,
280 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200281}
Przemek Stekiel41465252022-08-18 12:43:07 +0200282#endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
285 unsigned int key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286{
Gilles Peskine449bd832023-01-11 14:50:10 +0100287 unsigned char buf[5] = { 0 }, ku[2] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000289 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100290 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 MBEDTLS_X509_KU_NON_REPUDIATION |
292 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
293 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
294 MBEDTLS_X509_KU_KEY_AGREEMENT |
295 MBEDTLS_X509_KU_KEY_CERT_SIGN |
296 MBEDTLS_X509_KU_CRL_SIGN |
297 MBEDTLS_X509_KU_ENCIPHER_ONLY |
298 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100300 /* Check that nothing other than the allowed flags is set */
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if ((key_usage & ~allowed_bits) != 0) {
302 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
303 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100305 c = buf + 5;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
307 ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 if (ret < 0) {
310 return ret;
311 } else if (ret < 3 || ret > 5) {
312 return MBEDTLS_ERR_X509_INVALID_FORMAT;
313 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
316 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
317 1, c, (size_t) ret);
318 if (ret != 0) {
319 return ret;
320 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200321
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323}
324
Gilles Peskine449bd832023-01-11 14:50:10 +0100325int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
326 const mbedtls_asn1_sequence *exts)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100327{
328 unsigned char buf[256];
329 unsigned char *c = buf + sizeof(buf);
330 int ret;
331 size_t len = 0;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100332 const mbedtls_asn1_sequence *last_ext = NULL;
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100333 const mbedtls_asn1_sequence *ext;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 memset(buf, 0, sizeof(buf));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100336
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000337 /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
Gilles Peskine449bd832023-01-11 14:50:10 +0100338 if (exts == NULL) {
339 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
340 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100341
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000342 /* Iterate over exts backwards, so we write them out in the requested order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100343 while (last_ext != exts) {
344 for (ext = exts; ext->next != last_ext; ext = ext->next) {
345 }
346 if (ext->buf.tag != MBEDTLS_ASN1_OID) {
347 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
348 }
349 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len));
350 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len));
351 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID));
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000352 last_ext = ext;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100353 }
354
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
356 MBEDTLS_ASN1_CHK_ADD(len,
357 mbedtls_asn1_write_tag(&c, buf,
358 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 return mbedtls_x509write_crt_set_extension(ctx,
361 MBEDTLS_OID_EXTENDED_KEY_USAGE,
362 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
363 1, c, len);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100364}
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
367 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368{
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
373 c = buf + 4;
374
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
376 if (ret < 3 || ret > 4) {
377 return ret;
378 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
381 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
382 0, c, (size_t) ret);
383 if (ret != 0) {
384 return ret;
385 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200386
Gilles Peskine449bd832023-01-11 14:50:10 +0100387 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388}
389
Gilles Peskine449bd832023-01-11 14:50:10 +0100390static int x509_write_time(unsigned char **p, unsigned char *start,
391 const char *t, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392{
Janos Follath865b3eb2019-12-16 11:46:15 +0000393 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200394 size_t len = 0;
395
396 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200398 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100399 if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
400 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
401 (const unsigned char *) t + 2,
402 size - 2));
403 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
404 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
405 MBEDTLS_ASN1_UTC_TIME));
406 } else {
407 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
408 (const unsigned char *) t,
409 size));
410 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
411 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
412 MBEDTLS_ASN1_GENERALIZED_TIME));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 }
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416}
417
Gilles Peskine449bd832023-01-11 14:50:10 +0100418int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
419 unsigned char *buf, size_t size,
420 int (*f_rng)(void *, unsigned char *, size_t),
421 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422{
Janos Follath865b3eb2019-12-16 11:46:15 +0000423 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424 const char *sig_oid;
425 size_t sig_oid_len = 0;
426 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100427 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100428 size_t hash_length = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +0200429 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100430#if defined(MBEDTLS_USE_PSA_CRYPTO)
431 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
432 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +0100433#endif /* MBEDTLS_USE_PSA_CRYPTO */
434
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200435 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
436 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200437 mbedtls_pk_type_t pk_alg;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438
439 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100440 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100442 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
444 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100445
446 /* There's no direct way of extracting a signature algorithm
447 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100448 if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100449 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100450 } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200451 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100452 } else {
453 return MBEDTLS_ERR_X509_INVALID_ALG;
454 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200455
Gilles Peskine449bd832023-01-11 14:50:10 +0100456 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
457 &sig_oid, &sig_oid_len)) != 0) {
458 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459 }
460
461 /*
462 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
463 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100464
465 /* Only for v3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
467 MBEDTLS_ASN1_CHK_ADD(len,
468 mbedtls_x509_write_extensions(&c,
469 buf, ctx->extensions));
470 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
471 MBEDTLS_ASN1_CHK_ADD(len,
472 mbedtls_asn1_write_tag(&c, buf,
473 MBEDTLS_ASN1_CONSTRUCTED |
474 MBEDTLS_ASN1_SEQUENCE));
475 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
476 MBEDTLS_ASN1_CHK_ADD(len,
477 mbedtls_asn1_write_tag(&c, buf,
478 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
479 MBEDTLS_ASN1_CONSTRUCTED | 3));
Hanno Beckerd7f35202017-09-13 12:00:15 +0100480 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200481
482 /*
483 * SubjectPublicKeyInfo
484 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 MBEDTLS_ASN1_CHK_ADD(pub_len,
486 mbedtls_pk_write_pubkey_der(ctx->subject_key,
487 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200488 c -= pub_len;
489 len += pub_len;
490
491 /*
492 * Subject ::= Name
493 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 MBEDTLS_ASN1_CHK_ADD(len,
495 mbedtls_x509_write_names(&c, buf,
496 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 /*
499 * Validity ::= SEQUENCE {
500 * notBefore Time,
501 * notAfter Time }
502 */
503 sub_len = 0;
504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 MBEDTLS_ASN1_CHK_ADD(sub_len,
506 x509_write_time(&c, buf, ctx->not_after,
507 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
Gilles Peskine449bd832023-01-11 14:50:10 +0100509 MBEDTLS_ASN1_CHK_ADD(sub_len,
510 x509_write_time(&c, buf, ctx->not_before,
511 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512
513 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
515 MBEDTLS_ASN1_CHK_ADD(len,
516 mbedtls_asn1_write_tag(&c, buf,
517 MBEDTLS_ASN1_CONSTRUCTED |
518 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519
520 /*
521 * Issuer ::= Name
522 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
524 ctx->issuer));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200525
526 /*
527 * Signature ::= AlgorithmIdentifier
528 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 MBEDTLS_ASN1_CHK_ADD(len,
530 mbedtls_asn1_write_algorithm_identifier(&c, buf,
531 sig_oid, strlen(sig_oid), 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200532
533 /*
534 * Serial ::= INTEGER
Valerio Settida0afcc2023-01-05 16:46:59 +0100535 *
536 * Written data is:
Valerio Setti4752aac2023-01-10 16:00:15 +0100537 * - "ctx->serial_len" bytes for the raw serial buffer
538 * - if MSb of "serial" is 1, then prepend an extra 0x00 byte
Valerio Settida0afcc2023-01-05 16:46:59 +0100539 * - 1 byte for the length
540 * - 1 byte for the TAG
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541 */
Valerio Settida0afcc2023-01-05 16:46:59 +0100542 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf,
543 ctx->serial, ctx->serial_len));
Valerio Setti4752aac2023-01-10 16:00:15 +0100544 if (*c & 0x80) {
545 if (c - buf < 1) {
546 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
547 }
Valerio Setti856cec42023-01-12 14:56:54 +0100548 *(--c) = 0x0;
Valerio Setti4752aac2023-01-10 16:00:15 +0100549 len++;
550 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
551 ctx->serial_len + 1));
552 } else {
553 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
554 ctx->serial_len));
555 }
Valerio Settida0afcc2023-01-05 16:46:59 +0100556 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
557 MBEDTLS_ASN1_INTEGER));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558
559 /*
560 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
561 */
Hanno Becker47698652017-09-13 11:59:26 +0100562
563 /* Can be omitted for v1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
Hanno Becker47698652017-09-13 11:59:26 +0100565 sub_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100566 MBEDTLS_ASN1_CHK_ADD(sub_len,
567 mbedtls_asn1_write_int(&c, buf, ctx->version));
Hanno Becker47698652017-09-13 11:59:26 +0100568 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100569 MBEDTLS_ASN1_CHK_ADD(len,
570 mbedtls_asn1_write_len(&c, buf, sub_len));
571 MBEDTLS_ASN1_CHK_ADD(len,
572 mbedtls_asn1_write_tag(&c, buf,
573 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
574 MBEDTLS_ASN1_CONSTRUCTED | 0));
Hanno Becker47698652017-09-13 11:59:26 +0100575 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
578 MBEDTLS_ASN1_CHK_ADD(len,
579 mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
580 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200581
582 /*
583 * Make signature
584 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100585
586 /* Compute hash of CRT. */
pespacek7599a772022-02-07 14:40:55 +0100587#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 psa_algorithm = mbedtls_hash_info_psa_from_md(ctx->md_alg);
pespacek7599a772022-02-07 14:40:55 +0100589
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 status = psa_hash_compute(psa_algorithm,
591 c,
592 len,
593 hash,
594 sizeof(hash),
595 &hash_length);
596 if (status != PSA_SUCCESS) {
597 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100598 }
599#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100600 if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c,
601 len, hash)) != 0) {
602 return ret;
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100603 }
pespacek7599a772022-02-07 14:40:55 +0100604#endif /* MBEDTLS_USE_PSA_CRYPTO */
605
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606
Gilles Peskine449bd832023-01-11 14:50:10 +0100607 if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
608 hash, hash_length, sig, sizeof(sig), &sig_len,
609 f_rng, p_rng)) != 0) {
610 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 }
612
Hanno Beckerdef43052019-05-04 07:54:36 +0100613 /* Move CRT to the front of the buffer to have space
614 * for the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100615 memmove(buf, c, len);
Hanno Beckerdef43052019-05-04 07:54:36 +0100616 c = buf + len;
617
618 /* Add signature at the end of the buffer,
619 * making sure that it doesn't underflow
620 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100622 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
623 sig_oid, sig_oid_len, sig,
624 sig_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625
Hanno Beckerdef43052019-05-04 07:54:36 +0100626 /*
627 * Memory layout after this step:
628 *
629 * buf c=buf+len c2 buf+size
630 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
631 */
Andres AG60dbc932016-09-02 15:23:48 +0100632
Hanno Beckerdef43052019-05-04 07:54:36 +0100633 /* Move raw CRT to just before the signature. */
634 c = c2 - len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100635 memmove(c, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636
637 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
639 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
640 MBEDTLS_ASN1_CONSTRUCTED |
641 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
Gilles Peskine449bd832023-01-11 14:50:10 +0100643 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644}
645
646#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
647#define PEM_END_CRT "-----END CERTIFICATE-----\n"
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100650int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt,
651 unsigned char *buf, size_t size,
652 int (*f_rng)(void *, unsigned char *, size_t),
653 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200654{
Janos Follath865b3eb2019-12-16 11:46:15 +0000655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100656 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
Gilles Peskine449bd832023-01-11 14:50:10 +0100658 if ((ret = mbedtls_x509write_crt_der(crt, buf, size,
659 f_rng, p_rng)) < 0) {
660 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200661 }
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
664 buf + size - ret, ret,
665 buf, size, &olen)) != 0) {
666 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200667 }
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200670}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200671#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673#endif /* MBEDTLS_X509_CRT_WRITE_C */