blob: 53c2ac4ec9da93116e09e40bb84fa83573db2c71 [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 Settida0afcc2023-01-05 16:46:59 +0100103#if defined(MBEDTLS_BIGNUM_C)
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;
108 unsigned char tmp[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN];
109 size_t tmp_len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110
Valerio Settida0afcc2023-01-05 16:46:59 +0100111 /* Ensure that the MPI value fits into the buffer */
112 tmp_len = mbedtls_mpi_size(serial);
113 if (tmp_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
114 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
115 }
116
117 ctx->serial_len = tmp_len;
118
119 ret = mbedtls_mpi_write_binary(serial, tmp,
120 MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN);
121 if (ret < 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 return ret;
123 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200124
Valerio Settida0afcc2023-01-05 16:46:59 +0100125 /* Reverse the string since "tmp" is in big endian format */
126 for (int i = 0; i < MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN; i++) {
127 ctx->serial[i] = tmp[MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN - 1 - i];
128 }
129
130 return 0;
131}
132#endif
133
134int mbedtls_x509write_crt_set_serial_new(mbedtls_x509write_cert *ctx,
135 char *serial_buff, size_t serial_buff_len)
136{
137 int i, j;
138 char c;
139 unsigned char val;
140
141 if (serial_buff_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
142 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
143 }
144
145 /* Store data in little endian format */
146 for (i = 0, j = serial_buff_len - 1; j == 0; i++, j--) {
147 c = serial_buff[j];
148 if (c >= 0x30 && c <= 0x39) {
149 val = c - 0x30;
150 } else if (c >= 0x41 && c <= 0x46) {
151 val = c - 0x37;
152 } else if (c >= 0x61 && c <= 0x66) {
153 val = c - 0x57;
154 } else {
155 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
156 }
157
158 ctx->serial[i] = val;
159 }
160 ctx->serial_len = i;
161
Gilles Peskine449bd832023-01-11 14:50:10 +0100162 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200163}
164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx,
166 const char *not_before,
167 const char *not_after)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200168{
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
170 strlen(not_after) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) {
171 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 strncpy(ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
174 strncpy(ctx->not_after, not_after, MBEDTLS_X509_RFC5280_UTC_TIME_LEN);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
176 ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200179}
180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
182 const char *oid, size_t oid_len,
183 int critical,
184 const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200185{
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
187 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200188}
189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
191 int is_ca, int max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200192{
Janos Follath865b3eb2019-12-16 11:46:15 +0000193 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200194 unsigned char buf[9];
195 unsigned char *c = buf + sizeof(buf);
196 size_t len = 0;
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 memset(buf, 0, sizeof(buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (is_ca && max_pathlen > 127) {
201 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200202 }
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 if (is_ca) {
205 if (max_pathlen >= 0) {
206 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
207 max_pathlen));
208 }
209 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
210 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200211
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
213 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
214 MBEDTLS_ASN1_CONSTRUCTED |
215 MBEDTLS_ASN1_SEQUENCE));
216
217 return
218 mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
219 MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
220 is_ca, buf + sizeof(buf) - len, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221}
222
Przemek Stekiel76b753b2022-08-09 10:54:45 +0200223#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100224static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
225 int is_ca,
226 unsigned char tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200227{
Janos Follath865b3eb2019-12-16 11:46:15 +0000228 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230 unsigned char *c = buf + sizeof(buf);
231 size_t len = 0;
pespacek7599a772022-02-07 14:40:55 +0100232#if defined(MBEDTLS_USE_PSA_CRYPTO)
233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
234 size_t hash_length;
235#endif /* MBEDTLS_USE_PSA_CRYPTO */
236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 memset(buf, 0, sizeof(buf));
238 MBEDTLS_ASN1_CHK_ADD(len,
239 mbedtls_pk_write_pubkey(&c,
240 buf,
241 is_ca ?
242 ctx->issuer_key :
243 ctx->subject_key));
pespacek30151482022-02-17 15:18:47 +0100244
pespaceka6e955e2022-02-14 15:20:57 +0100245
pespacek7599a772022-02-07 14:40:55 +0100246#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 status = psa_hash_compute(PSA_ALG_SHA_1,
248 buf + sizeof(buf) - len,
249 len,
250 buf + sizeof(buf) - 20,
251 20,
252 &hash_length);
253 if (status != PSA_SUCCESS) {
254 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100255 }
256#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 ret = mbedtls_sha1(buf + sizeof(buf) - len, len,
258 buf + sizeof(buf) - 20);
259 if (ret != 0) {
260 return ret;
261 }
pespacek7599a772022-02-07 14:40:55 +0100262#endif /* MBEDTLS_USE_PSA_CRYPTO */
263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 c = buf + sizeof(buf) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265 len = 20;
266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
268 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 if (is_ca) { // writes AuthorityKeyIdentifier sequence
271 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
272 MBEDTLS_ASN1_CHK_ADD(len,
273 mbedtls_asn1_write_tag(&c,
274 buf,
275 MBEDTLS_ASN1_CONSTRUCTED |
276 MBEDTLS_ASN1_SEQUENCE));
pespacek30151482022-02-17 15:18:47 +0100277 }
pespacekd924e552022-02-28 11:49:54 +0100278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 if (is_ca) {
280 return mbedtls_x509write_crt_set_extension(ctx,
281 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
282 MBEDTLS_OID_SIZE(
283 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER),
284 0, buf + sizeof(buf) - len, len);
285 } else {
286 return mbedtls_x509write_crt_set_extension(ctx,
287 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
288 MBEDTLS_OID_SIZE(
289 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER),
290 0, buf + sizeof(buf) - len, len);
291 }
pespaceka6e955e2022-02-14 15:20:57 +0100292}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100295{
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 return mbedtls_x509write_crt_set_key_identifier(ctx,
297 0,
298 MBEDTLS_ASN1_OCTET_STRING);
pespaceka6e955e2022-02-14 15:20:57 +0100299}
300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100302{
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 return mbedtls_x509write_crt_set_key_identifier(ctx,
304 1,
305 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200306}
Przemek Stekiel41465252022-08-18 12:43:07 +0200307#endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
310 unsigned int key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311{
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 unsigned char buf[5] = { 0 }, ku[2] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100315 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
Gilles Peskine449bd832023-01-11 14:50:10 +0100316 MBEDTLS_X509_KU_NON_REPUDIATION |
317 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
318 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
319 MBEDTLS_X509_KU_KEY_AGREEMENT |
320 MBEDTLS_X509_KU_KEY_CERT_SIGN |
321 MBEDTLS_X509_KU_CRL_SIGN |
322 MBEDTLS_X509_KU_ENCIPHER_ONLY |
323 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100325 /* Check that nothing other than the allowed flags is set */
Gilles Peskine449bd832023-01-11 14:50:10 +0100326 if ((key_usage & ~allowed_bits) != 0) {
327 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
328 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100330 c = buf + 5;
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
332 ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 if (ret < 0) {
335 return ret;
336 } else if (ret < 3 || ret > 5) {
337 return MBEDTLS_ERR_X509_INVALID_FORMAT;
338 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
341 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
342 1, c, (size_t) ret);
343 if (ret != 0) {
344 return ret;
345 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200346
Gilles Peskine449bd832023-01-11 14:50:10 +0100347 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348}
349
Gilles Peskine449bd832023-01-11 14:50:10 +0100350int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
351 const mbedtls_asn1_sequence *exts)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100352{
353 unsigned char buf[256];
354 unsigned char *c = buf + sizeof(buf);
355 int ret;
356 size_t len = 0;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100357 const mbedtls_asn1_sequence *last_ext = NULL;
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100358 const mbedtls_asn1_sequence *ext;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100359
Gilles Peskine449bd832023-01-11 14:50:10 +0100360 memset(buf, 0, sizeof(buf));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100361
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000362 /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (exts == NULL) {
364 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
365 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100366
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000367 /* Iterate over exts backwards, so we write them out in the requested order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 while (last_ext != exts) {
369 for (ext = exts; ext->next != last_ext; ext = ext->next) {
370 }
371 if (ext->buf.tag != MBEDTLS_ASN1_OID) {
372 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
373 }
374 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len));
375 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len));
376 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID));
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000377 last_ext = ext;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100378 }
379
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
381 MBEDTLS_ASN1_CHK_ADD(len,
382 mbedtls_asn1_write_tag(&c, buf,
383 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 return mbedtls_x509write_crt_set_extension(ctx,
386 MBEDTLS_OID_EXTENDED_KEY_USAGE,
387 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
388 1, c, len);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100389}
390
Gilles Peskine449bd832023-01-11 14:50:10 +0100391int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
392 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393{
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200395 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200397
398 c = buf + 4;
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
401 if (ret < 3 || ret > 4) {
402 return ret;
403 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404
Gilles Peskine449bd832023-01-11 14:50:10 +0100405 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
406 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
407 0, c, (size_t) ret);
408 if (ret != 0) {
409 return ret;
410 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413}
414
Gilles Peskine449bd832023-01-11 14:50:10 +0100415static int x509_write_time(unsigned char **p, unsigned char *start,
416 const char *t, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200417{
Janos Follath865b3eb2019-12-16 11:46:15 +0000418 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200419 size_t len = 0;
420
421 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200422 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
425 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
426 (const unsigned char *) t + 2,
427 size - 2));
428 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
429 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
430 MBEDTLS_ASN1_UTC_TIME));
431 } else {
432 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
433 (const unsigned char *) t,
434 size));
435 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
436 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
437 MBEDTLS_ASN1_GENERALIZED_TIME));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438 }
439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200441}
442
Gilles Peskine449bd832023-01-11 14:50:10 +0100443int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
444 unsigned char *buf, size_t size,
445 int (*f_rng)(void *, unsigned char *, size_t),
446 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447{
Janos Follath865b3eb2019-12-16 11:46:15 +0000448 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200449 const char *sig_oid;
450 size_t sig_oid_len = 0;
451 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100452 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100453 size_t hash_length = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +0200454 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100455#if defined(MBEDTLS_USE_PSA_CRYPTO)
456 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
457 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +0100458#endif /* MBEDTLS_USE_PSA_CRYPTO */
459
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
461 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462 mbedtls_pk_type_t pk_alg;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
464 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100465 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200466 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100467 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200468
469 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100470
471 /* There's no direct way of extracting a signature algorithm
472 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100473 if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100474 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200476 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100477 } else {
478 return MBEDTLS_ERR_X509_INVALID_ALG;
479 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200480
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
482 &sig_oid, &sig_oid_len)) != 0) {
483 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200484 }
485
486 /*
487 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
488 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100489
490 /* Only for v3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
492 MBEDTLS_ASN1_CHK_ADD(len,
493 mbedtls_x509_write_extensions(&c,
494 buf, ctx->extensions));
495 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
496 MBEDTLS_ASN1_CHK_ADD(len,
497 mbedtls_asn1_write_tag(&c, buf,
498 MBEDTLS_ASN1_CONSTRUCTED |
499 MBEDTLS_ASN1_SEQUENCE));
500 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
501 MBEDTLS_ASN1_CHK_ADD(len,
502 mbedtls_asn1_write_tag(&c, buf,
503 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
504 MBEDTLS_ASN1_CONSTRUCTED | 3));
Hanno Beckerd7f35202017-09-13 12:00:15 +0100505 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200506
507 /*
508 * SubjectPublicKeyInfo
509 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 MBEDTLS_ASN1_CHK_ADD(pub_len,
511 mbedtls_pk_write_pubkey_der(ctx->subject_key,
512 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513 c -= pub_len;
514 len += pub_len;
515
516 /*
517 * Subject ::= Name
518 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 MBEDTLS_ASN1_CHK_ADD(len,
520 mbedtls_x509_write_names(&c, buf,
521 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522
523 /*
524 * Validity ::= SEQUENCE {
525 * notBefore Time,
526 * notAfter Time }
527 */
528 sub_len = 0;
529
Gilles Peskine449bd832023-01-11 14:50:10 +0100530 MBEDTLS_ASN1_CHK_ADD(sub_len,
531 x509_write_time(&c, buf, ctx->not_after,
532 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200533
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 MBEDTLS_ASN1_CHK_ADD(sub_len,
535 x509_write_time(&c, buf, ctx->not_before,
536 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537
538 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
540 MBEDTLS_ASN1_CHK_ADD(len,
541 mbedtls_asn1_write_tag(&c, buf,
542 MBEDTLS_ASN1_CONSTRUCTED |
543 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544
545 /*
546 * Issuer ::= Name
547 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100548 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
549 ctx->issuer));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200550
551 /*
552 * Signature ::= AlgorithmIdentifier
553 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100554 MBEDTLS_ASN1_CHK_ADD(len,
555 mbedtls_asn1_write_algorithm_identifier(&c, buf,
556 sig_oid, strlen(sig_oid), 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200557
558 /*
559 * Serial ::= INTEGER
Valerio Settida0afcc2023-01-05 16:46:59 +0100560 *
561 * Written data is:
562 * - [ctx->serial_len] bytes for the raw serial buffer
563 * - 1 byte for the length
564 * - 1 byte for the TAG
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565 */
Valerio Settida0afcc2023-01-05 16:46:59 +0100566 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf,
567 ctx->serial, ctx->serial_len));
568 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
569 ctx->serial_len));
570 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
571 MBEDTLS_ASN1_INTEGER));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572
573 /*
574 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
575 */
Hanno Becker47698652017-09-13 11:59:26 +0100576
577 /* Can be omitted for v1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100578 if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
Hanno Becker47698652017-09-13 11:59:26 +0100579 sub_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 MBEDTLS_ASN1_CHK_ADD(sub_len,
581 mbedtls_asn1_write_int(&c, buf, ctx->version));
Hanno Becker47698652017-09-13 11:59:26 +0100582 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 MBEDTLS_ASN1_CHK_ADD(len,
584 mbedtls_asn1_write_len(&c, buf, sub_len));
585 MBEDTLS_ASN1_CHK_ADD(len,
586 mbedtls_asn1_write_tag(&c, buf,
587 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
588 MBEDTLS_ASN1_CONSTRUCTED | 0));
Hanno Becker47698652017-09-13 11:59:26 +0100589 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200590
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
592 MBEDTLS_ASN1_CHK_ADD(len,
593 mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
594 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200595
596 /*
597 * Make signature
598 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100599
600 /* Compute hash of CRT. */
pespacek7599a772022-02-07 14:40:55 +0100601#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100602 psa_algorithm = mbedtls_hash_info_psa_from_md(ctx->md_alg);
pespacek7599a772022-02-07 14:40:55 +0100603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604 status = psa_hash_compute(psa_algorithm,
605 c,
606 len,
607 hash,
608 sizeof(hash),
609 &hash_length);
610 if (status != PSA_SUCCESS) {
611 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100612 }
613#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100614 if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c,
615 len, hash)) != 0) {
616 return ret;
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100617 }
pespacek7599a772022-02-07 14:40:55 +0100618#endif /* MBEDTLS_USE_PSA_CRYPTO */
619
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620
Gilles Peskine449bd832023-01-11 14:50:10 +0100621 if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
622 hash, hash_length, sig, sizeof(sig), &sig_len,
623 f_rng, p_rng)) != 0) {
624 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200625 }
626
Hanno Beckerdef43052019-05-04 07:54:36 +0100627 /* Move CRT to the front of the buffer to have space
628 * for the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 memmove(buf, c, len);
Hanno Beckerdef43052019-05-04 07:54:36 +0100630 c = buf + len;
631
632 /* Add signature at the end of the buffer,
633 * making sure that it doesn't underflow
634 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100636 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
637 sig_oid, sig_oid_len, sig,
638 sig_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200639
Hanno Beckerdef43052019-05-04 07:54:36 +0100640 /*
641 * Memory layout after this step:
642 *
643 * buf c=buf+len c2 buf+size
644 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
645 */
Andres AG60dbc932016-09-02 15:23:48 +0100646
Hanno Beckerdef43052019-05-04 07:54:36 +0100647 /* Move raw CRT to just before the signature. */
648 c = c2 - len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100649 memmove(c, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
651 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
653 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
654 MBEDTLS_ASN1_CONSTRUCTED |
655 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
Gilles Peskine449bd832023-01-11 14:50:10 +0100657 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200658}
659
660#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
661#define PEM_END_CRT "-----END CERTIFICATE-----\n"
662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100664int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt,
665 unsigned char *buf, size_t size,
666 int (*f_rng)(void *, unsigned char *, size_t),
667 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200668{
Janos Follath865b3eb2019-12-16 11:46:15 +0000669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100670 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672 if ((ret = mbedtls_x509write_crt_der(crt, buf, size,
673 f_rng, p_rng)) < 0) {
674 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200675 }
676
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
678 buf + size - ret, ret,
679 buf, size, &olen)) != 0) {
680 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681 }
682
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200684}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200685#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687#endif /* MBEDTLS_X509_CRT_WRITE_C */