blob: e3b8f605ea26d9b761050b4549db6944f427dd23 [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"
Andrzej Kurek67fdb332023-04-04 06:56:14 -040034#include "mbedtls/platform.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050035#include "mbedtls/platform_util.h"
Manuel Pégourié-Gonnard2cd75142023-02-24 12:37:07 +010036#include "mbedtls/md.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020037
Rich Evans00ab4702015-02-06 13:43:58 +000038#include <string.h>
39
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000041#include "mbedtls/pem.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020043
pespacek7599a772022-02-07 14:40:55 +010044#if defined(MBEDTLS_USE_PSA_CRYPTO)
45#include "psa/crypto.h"
46#include "mbedtls/psa_util.h"
47#endif /* MBEDTLS_USE_PSA_CRYPTO */
48
Przemek Stekiel40afdd22022-09-06 13:08:28 +020049#include "hash_info.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
Valerio Settiaf4815c2023-01-26 17:43:09 +0100127int mbedtls_x509write_crt_set_serial_raw(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
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400156int mbedtls_x509write_crt_set_subject_alternative_name(mbedtls_x509write_cert *ctx,
157 const mbedtls_x509_san_list *san_list)
158{
159 int ret = 0;
160 const mbedtls_x509_san_list *cur;
161 unsigned char *buf;
162 unsigned char *p;
163 size_t len;
164 size_t buflen = 0;
165
166 /* Determine the maximum size of the SubjectAltName list */
167 for (cur = san_list; cur != NULL; cur = cur->next) {
168 /* Calculate size of the required buffer */
169 switch (cur->node.type) {
170 case MBEDTLS_X509_SAN_DNS_NAME:
171 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
172 case MBEDTLS_X509_SAN_IP_ADDRESS:
Andrzej Kurek908716f2023-04-25 04:31:26 -0400173 case MBEDTLS_X509_SAN_RFC822_NAME:
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400174 /* length of value for each name entry,
175 * maximum 4 bytes for the length field,
176 * 1 byte for the tag/type.
177 */
178 buflen += cur->node.san.unstructured_name.len + 4 + 1;
179 break;
180 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
Andrzej Kurekc6215b02023-04-04 09:30:12 -0400181 {
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400182 const mbedtls_asn1_named_data *chunk = &cur->node.san.directory_name;
183 while (chunk != NULL) {
184 // 5 bytes for OID, max 4 bytes for length, +1 for tag,
185 // additional 4 max for length, +1 for tag.
186 // See x509_write_name for more information.
187 buflen += chunk->val.len + 5 + 4 + 1 + 4 + 1;
188 chunk = chunk->next;
189 }
190 buflen += cur->node.san.unstructured_name.len + 4 + 1;
191 break;
Andrzej Kurekc6215b02023-04-04 09:30:12 -0400192 }
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400193 default:
Andrzej Kurekdc220902023-04-25 02:29:00 -0400194 /* Not supported - return. */
195 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400196 }
197 }
198
199 /* Add the extra length field and tag */
200 buflen += 4 + 1;
201
202 /* Allocate buffer */
203 buf = mbedtls_calloc(1, buflen);
204 if (buf == NULL) {
205 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
206 }
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400207 p = buf + buflen;
208
209 /* Write ASN.1-based structure */
210 cur = san_list;
211 len = 0;
212 while (cur != NULL) {
213 size_t single_san_len = 0;
214 switch (cur->node.type) {
215 case MBEDTLS_X509_SAN_DNS_NAME:
216 case MBEDTLS_X509_SAN_RFC822_NAME:
217 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
218 case MBEDTLS_X509_SAN_IP_ADDRESS:
219 {
220 const unsigned char *unstructured_name =
221 (const unsigned char *) cur->node.san.unstructured_name.p;
222 size_t unstructured_name_len = cur->node.san.unstructured_name.len;
223
224 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
225 mbedtls_asn1_write_raw_buffer(
226 &p, buf,
227 unstructured_name, unstructured_name_len));
228 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, mbedtls_asn1_write_len(
229 &p, buf, unstructured_name_len));
230 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
231 mbedtls_asn1_write_tag(
232 &p, buf,
233 MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type));
234 }
235 break;
236 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
237 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
238 mbedtls_x509_write_names(&p, buf,
239 (mbedtls_asn1_named_data *) &
240 cur->node
241 .san.directory_name));
242 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
243 mbedtls_asn1_write_len(&p, buf, single_san_len));
244 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
245 mbedtls_asn1_write_tag(&p, buf,
246 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
247 MBEDTLS_ASN1_CONSTRUCTED |
248 MBEDTLS_X509_SAN_DIRECTORY_NAME));
249 break;
250 default:
Andrzej Kurekdc220902023-04-25 02:29:00 -0400251 /* Error out on an unsupported SAN */
252 ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
253 goto cleanup;
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400254 }
255 cur = cur->next;
256 len += single_san_len;
257 }
258
259 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
260 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
261 mbedtls_asn1_write_tag(&p, buf,
262 MBEDTLS_ASN1_CONSTRUCTED |
263 MBEDTLS_ASN1_SEQUENCE));
264
265 ret = mbedtls_x509write_crt_set_extension(
266 ctx,
267 MBEDTLS_OID_SUBJECT_ALT_NAME,
268 MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
269 0,
270 buf + buflen - len,
271 len);
272
273cleanup:
274 mbedtls_free(buf);
275 return ret;
276}
277
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
280 const char *oid, size_t oid_len,
281 int critical,
282 const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283{
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
285 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286}
287
Gilles Peskine449bd832023-01-11 14:50:10 +0100288int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
289 int is_ca, int max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290{
Janos Follath865b3eb2019-12-16 11:46:15 +0000291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200292 unsigned char buf[9];
293 unsigned char *c = buf + sizeof(buf);
294 size_t len = 0;
295
Gilles Peskine449bd832023-01-11 14:50:10 +0100296 memset(buf, 0, sizeof(buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200297
Gilles Peskine449bd832023-01-11 14:50:10 +0100298 if (is_ca && max_pathlen > 127) {
299 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 }
301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 if (is_ca) {
303 if (max_pathlen >= 0) {
304 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
305 max_pathlen));
306 }
307 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
308 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200309
Gilles Peskine449bd832023-01-11 14:50:10 +0100310 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
311 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
312 MBEDTLS_ASN1_CONSTRUCTED |
313 MBEDTLS_ASN1_SEQUENCE));
314
315 return
316 mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
317 MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
318 is_ca, buf + sizeof(buf) - len, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319}
320
Manuel Pégourié-Gonnarda9464892023-03-17 12:08:50 +0100321#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100322static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
323 int is_ca,
324 unsigned char tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325{
Janos Follath865b3eb2019-12-16 11:46:15 +0000326 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200328 unsigned char *c = buf + sizeof(buf);
329 size_t len = 0;
pespacek7599a772022-02-07 14:40:55 +0100330#if defined(MBEDTLS_USE_PSA_CRYPTO)
331 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
332 size_t hash_length;
333#endif /* MBEDTLS_USE_PSA_CRYPTO */
334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335 memset(buf, 0, sizeof(buf));
336 MBEDTLS_ASN1_CHK_ADD(len,
337 mbedtls_pk_write_pubkey(&c,
338 buf,
339 is_ca ?
340 ctx->issuer_key :
341 ctx->subject_key));
pespacek30151482022-02-17 15:18:47 +0100342
pespaceka6e955e2022-02-14 15:20:57 +0100343
pespacek7599a772022-02-07 14:40:55 +0100344#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 status = psa_hash_compute(PSA_ALG_SHA_1,
346 buf + sizeof(buf) - len,
347 len,
348 buf + sizeof(buf) - 20,
349 20,
350 &hash_length);
351 if (status != PSA_SUCCESS) {
352 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100353 }
354#else
Manuel Pégourié-Gonnard2cd75142023-02-24 12:37:07 +0100355 ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
356 buf + sizeof(buf) - len, len,
357 buf + sizeof(buf) - 20);
Gilles Peskine449bd832023-01-11 14:50:10 +0100358 if (ret != 0) {
359 return ret;
360 }
pespacek7599a772022-02-07 14:40:55 +0100361#endif /* MBEDTLS_USE_PSA_CRYPTO */
362
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 c = buf + sizeof(buf) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364 len = 20;
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
367 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200368
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (is_ca) { // writes AuthorityKeyIdentifier sequence
370 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
371 MBEDTLS_ASN1_CHK_ADD(len,
372 mbedtls_asn1_write_tag(&c,
373 buf,
374 MBEDTLS_ASN1_CONSTRUCTED |
375 MBEDTLS_ASN1_SEQUENCE));
pespacek30151482022-02-17 15:18:47 +0100376 }
pespacekd924e552022-02-28 11:49:54 +0100377
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (is_ca) {
379 return mbedtls_x509write_crt_set_extension(ctx,
380 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
381 MBEDTLS_OID_SIZE(
382 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER),
383 0, buf + sizeof(buf) - len, len);
384 } else {
385 return mbedtls_x509write_crt_set_extension(ctx,
386 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
387 MBEDTLS_OID_SIZE(
388 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER),
389 0, buf + sizeof(buf) - len, len);
390 }
pespaceka6e955e2022-02-14 15:20:57 +0100391}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100394{
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 return mbedtls_x509write_crt_set_key_identifier(ctx,
396 0,
397 MBEDTLS_ASN1_OCTET_STRING);
pespaceka6e955e2022-02-14 15:20:57 +0100398}
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100401{
Gilles Peskine449bd832023-01-11 14:50:10 +0100402 return mbedtls_x509write_crt_set_key_identifier(ctx,
403 1,
404 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200405}
Manuel Pégourié-Gonnarda9464892023-03-17 12:08:50 +0100406#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407
Gilles Peskine449bd832023-01-11 14:50:10 +0100408int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
409 unsigned int key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410{
Gilles Peskine449bd832023-01-11 14:50:10 +0100411 unsigned char buf[5] = { 0 }, ku[2] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200412 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000413 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100414 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 MBEDTLS_X509_KU_NON_REPUDIATION |
416 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
417 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
418 MBEDTLS_X509_KU_KEY_AGREEMENT |
419 MBEDTLS_X509_KU_KEY_CERT_SIGN |
420 MBEDTLS_X509_KU_CRL_SIGN |
421 MBEDTLS_X509_KU_ENCIPHER_ONLY |
422 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100424 /* Check that nothing other than the allowed flags is set */
Gilles Peskine449bd832023-01-11 14:50:10 +0100425 if ((key_usage & ~allowed_bits) != 0) {
426 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
427 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200428
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100429 c = buf + 5;
Gilles Peskine449bd832023-01-11 14:50:10 +0100430 MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
431 ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200432
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 if (ret < 0) {
434 return ret;
435 } else if (ret < 3 || ret > 5) {
436 return MBEDTLS_ERR_X509_INVALID_FORMAT;
437 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200438
Gilles Peskine449bd832023-01-11 14:50:10 +0100439 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
440 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
441 1, c, (size_t) ret);
442 if (ret != 0) {
443 return ret;
444 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200445
Gilles Peskine449bd832023-01-11 14:50:10 +0100446 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447}
448
Gilles Peskine449bd832023-01-11 14:50:10 +0100449int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
450 const mbedtls_asn1_sequence *exts)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100451{
452 unsigned char buf[256];
453 unsigned char *c = buf + sizeof(buf);
454 int ret;
455 size_t len = 0;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100456 const mbedtls_asn1_sequence *last_ext = NULL;
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100457 const mbedtls_asn1_sequence *ext;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100458
Gilles Peskine449bd832023-01-11 14:50:10 +0100459 memset(buf, 0, sizeof(buf));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100460
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000461 /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
Gilles Peskine449bd832023-01-11 14:50:10 +0100462 if (exts == NULL) {
463 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
464 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100465
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000466 /* Iterate over exts backwards, so we write them out in the requested order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100467 while (last_ext != exts) {
468 for (ext = exts; ext->next != last_ext; ext = ext->next) {
469 }
470 if (ext->buf.tag != MBEDTLS_ASN1_OID) {
471 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
472 }
473 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len));
474 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len));
475 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID));
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000476 last_ext = ext;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100477 }
478
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
480 MBEDTLS_ASN1_CHK_ADD(len,
481 mbedtls_asn1_write_tag(&c, buf,
482 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100483
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 return mbedtls_x509write_crt_set_extension(ctx,
485 MBEDTLS_OID_EXTENDED_KEY_USAGE,
486 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
487 1, c, len);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100488}
489
Gilles Peskine449bd832023-01-11 14:50:10 +0100490int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
491 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492{
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200494 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000495 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200496
497 c = buf + 4;
498
Gilles Peskine449bd832023-01-11 14:50:10 +0100499 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
500 if (ret < 3 || ret > 4) {
501 return ret;
502 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200503
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
505 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
506 0, c, (size_t) ret);
507 if (ret != 0) {
508 return ret;
509 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200510
Gilles Peskine449bd832023-01-11 14:50:10 +0100511 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200512}
513
Gilles Peskine449bd832023-01-11 14:50:10 +0100514static int x509_write_time(unsigned char **p, unsigned char *start,
515 const char *t, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200516{
Janos Follath865b3eb2019-12-16 11:46:15 +0000517 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200518 size_t len = 0;
519
520 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200522 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
524 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
525 (const unsigned char *) t + 2,
526 size - 2));
527 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
528 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
529 MBEDTLS_ASN1_UTC_TIME));
530 } else {
531 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
532 (const unsigned char *) t,
533 size));
534 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
535 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
536 MBEDTLS_ASN1_GENERALIZED_TIME));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200537 }
538
Gilles Peskine449bd832023-01-11 14:50:10 +0100539 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540}
541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
543 unsigned char *buf, size_t size,
544 int (*f_rng)(void *, unsigned char *, size_t),
545 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200546{
Janos Follath865b3eb2019-12-16 11:46:15 +0000547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200548 const char *sig_oid;
549 size_t sig_oid_len = 0;
550 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100551 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100552 size_t hash_length = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +0200553 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100554#if defined(MBEDTLS_USE_PSA_CRYPTO)
555 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
556 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +0100557#endif /* MBEDTLS_USE_PSA_CRYPTO */
558
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200559 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
560 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_pk_type_t pk_alg;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562
563 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100564 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200565 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100566 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200567
568 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100569
570 /* There's no direct way of extracting a signature algorithm
571 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100572 if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100573 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 } else {
577 return MBEDTLS_ERR_X509_INVALID_ALG;
578 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200579
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
581 &sig_oid, &sig_oid_len)) != 0) {
582 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200583 }
584
585 /*
586 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
587 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100588
589 /* Only for v3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100590 if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
591 MBEDTLS_ASN1_CHK_ADD(len,
592 mbedtls_x509_write_extensions(&c,
593 buf, ctx->extensions));
594 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
595 MBEDTLS_ASN1_CHK_ADD(len,
596 mbedtls_asn1_write_tag(&c, buf,
597 MBEDTLS_ASN1_CONSTRUCTED |
598 MBEDTLS_ASN1_SEQUENCE));
599 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
600 MBEDTLS_ASN1_CHK_ADD(len,
601 mbedtls_asn1_write_tag(&c, buf,
602 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
603 MBEDTLS_ASN1_CONSTRUCTED | 3));
Hanno Beckerd7f35202017-09-13 12:00:15 +0100604 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200605
606 /*
607 * SubjectPublicKeyInfo
608 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100609 MBEDTLS_ASN1_CHK_ADD(pub_len,
610 mbedtls_pk_write_pubkey_der(ctx->subject_key,
611 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200612 c -= pub_len;
613 len += pub_len;
614
615 /*
616 * Subject ::= Name
617 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100618 MBEDTLS_ASN1_CHK_ADD(len,
619 mbedtls_x509_write_names(&c, buf,
620 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200621
622 /*
623 * Validity ::= SEQUENCE {
624 * notBefore Time,
625 * notAfter Time }
626 */
627 sub_len = 0;
628
Gilles Peskine449bd832023-01-11 14:50:10 +0100629 MBEDTLS_ASN1_CHK_ADD(sub_len,
630 x509_write_time(&c, buf, ctx->not_after,
631 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200632
Gilles Peskine449bd832023-01-11 14:50:10 +0100633 MBEDTLS_ASN1_CHK_ADD(sub_len,
634 x509_write_time(&c, buf, ctx->not_before,
635 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200636
637 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100638 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
639 MBEDTLS_ASN1_CHK_ADD(len,
640 mbedtls_asn1_write_tag(&c, buf,
641 MBEDTLS_ASN1_CONSTRUCTED |
642 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200643
644 /*
645 * Issuer ::= Name
646 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100647 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
648 ctx->issuer));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200649
650 /*
651 * Signature ::= AlgorithmIdentifier
652 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100653 MBEDTLS_ASN1_CHK_ADD(len,
654 mbedtls_asn1_write_algorithm_identifier(&c, buf,
655 sig_oid, strlen(sig_oid), 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200656
657 /*
658 * Serial ::= INTEGER
Valerio Settida0afcc2023-01-05 16:46:59 +0100659 *
660 * Written data is:
Valerio Setti4752aac2023-01-10 16:00:15 +0100661 * - "ctx->serial_len" bytes for the raw serial buffer
662 * - if MSb of "serial" is 1, then prepend an extra 0x00 byte
Valerio Settida0afcc2023-01-05 16:46:59 +0100663 * - 1 byte for the length
664 * - 1 byte for the TAG
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200665 */
Valerio Settida0afcc2023-01-05 16:46:59 +0100666 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf,
667 ctx->serial, ctx->serial_len));
Valerio Setti4752aac2023-01-10 16:00:15 +0100668 if (*c & 0x80) {
669 if (c - buf < 1) {
670 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
671 }
Valerio Setti856cec42023-01-12 14:56:54 +0100672 *(--c) = 0x0;
Valerio Setti4752aac2023-01-10 16:00:15 +0100673 len++;
674 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
675 ctx->serial_len + 1));
676 } else {
677 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
678 ctx->serial_len));
679 }
Valerio Settida0afcc2023-01-05 16:46:59 +0100680 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
681 MBEDTLS_ASN1_INTEGER));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200682
683 /*
684 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
685 */
Hanno Becker47698652017-09-13 11:59:26 +0100686
687 /* Can be omitted for v1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
Hanno Becker47698652017-09-13 11:59:26 +0100689 sub_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 MBEDTLS_ASN1_CHK_ADD(sub_len,
691 mbedtls_asn1_write_int(&c, buf, ctx->version));
Hanno Becker47698652017-09-13 11:59:26 +0100692 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 MBEDTLS_ASN1_CHK_ADD(len,
694 mbedtls_asn1_write_len(&c, buf, sub_len));
695 MBEDTLS_ASN1_CHK_ADD(len,
696 mbedtls_asn1_write_tag(&c, buf,
697 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
698 MBEDTLS_ASN1_CONSTRUCTED | 0));
Hanno Becker47698652017-09-13 11:59:26 +0100699 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
702 MBEDTLS_ASN1_CHK_ADD(len,
703 mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
704 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200705
706 /*
707 * Make signature
708 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100709
710 /* Compute hash of CRT. */
pespacek7599a772022-02-07 14:40:55 +0100711#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 psa_algorithm = mbedtls_hash_info_psa_from_md(ctx->md_alg);
pespacek7599a772022-02-07 14:40:55 +0100713
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 status = psa_hash_compute(psa_algorithm,
715 c,
716 len,
717 hash,
718 sizeof(hash),
719 &hash_length);
720 if (status != PSA_SUCCESS) {
721 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100722 }
723#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c,
725 len, hash)) != 0) {
726 return ret;
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100727 }
pespacek7599a772022-02-07 14:40:55 +0100728#endif /* MBEDTLS_USE_PSA_CRYPTO */
729
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200730
Gilles Peskine449bd832023-01-11 14:50:10 +0100731 if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
732 hash, hash_length, sig, sizeof(sig), &sig_len,
733 f_rng, p_rng)) != 0) {
734 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200735 }
736
Hanno Beckerdef43052019-05-04 07:54:36 +0100737 /* Move CRT to the front of the buffer to have space
738 * for the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100739 memmove(buf, c, len);
Hanno Beckerdef43052019-05-04 07:54:36 +0100740 c = buf + len;
741
742 /* Add signature at the end of the buffer,
743 * making sure that it doesn't underflow
744 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200745 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
747 sig_oid, sig_oid_len, sig,
748 sig_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200749
Hanno Beckerdef43052019-05-04 07:54:36 +0100750 /*
751 * Memory layout after this step:
752 *
753 * buf c=buf+len c2 buf+size
754 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
755 */
Andres AG60dbc932016-09-02 15:23:48 +0100756
Hanno Beckerdef43052019-05-04 07:54:36 +0100757 /* Move raw CRT to just before the signature. */
758 c = c2 - len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100759 memmove(c, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200760
761 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100762 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
763 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
764 MBEDTLS_ASN1_CONSTRUCTED |
765 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200766
Gilles Peskine449bd832023-01-11 14:50:10 +0100767 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200768}
769
770#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
771#define PEM_END_CRT "-----END CERTIFICATE-----\n"
772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100774int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt,
775 unsigned char *buf, size_t size,
776 int (*f_rng)(void *, unsigned char *, size_t),
777 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200778{
Janos Follath865b3eb2019-12-16 11:46:15 +0000779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100780 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200781
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if ((ret = mbedtls_x509write_crt_der(crt, buf, size,
783 f_rng, p_rng)) < 0) {
784 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200785 }
786
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
788 buf + size - ret, ret,
789 buf, size, &olen)) != 0) {
790 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200791 }
792
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_X509_CRT_WRITE_C */