blob: 6b23a94a9ae785bf0d44ef66c08e8c157aefbaa6 [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:
173 /* length of value for each name entry,
174 * maximum 4 bytes for the length field,
175 * 1 byte for the tag/type.
176 */
177 buflen += cur->node.san.unstructured_name.len + 4 + 1;
178 break;
179 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
Andrzej Kurekc6215b02023-04-04 09:30:12 -0400180 {
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400181 const mbedtls_asn1_named_data *chunk = &cur->node.san.directory_name;
182 while (chunk != NULL) {
183 // 5 bytes for OID, max 4 bytes for length, +1 for tag,
184 // additional 4 max for length, +1 for tag.
185 // See x509_write_name for more information.
186 buflen += chunk->val.len + 5 + 4 + 1 + 4 + 1;
187 chunk = chunk->next;
188 }
189 buflen += cur->node.san.unstructured_name.len + 4 + 1;
190 break;
Andrzej Kurekc6215b02023-04-04 09:30:12 -0400191 }
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400192 default:
Andrzej Kurekdc220902023-04-25 02:29:00 -0400193 /* Not supported - return. */
194 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400195 }
196 }
197
198 /* Add the extra length field and tag */
199 buflen += 4 + 1;
200
201 /* Allocate buffer */
202 buf = mbedtls_calloc(1, buflen);
203 if (buf == NULL) {
204 return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
205 }
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400206 p = buf + buflen;
207
208 /* Write ASN.1-based structure */
209 cur = san_list;
210 len = 0;
211 while (cur != NULL) {
212 size_t single_san_len = 0;
213 switch (cur->node.type) {
214 case MBEDTLS_X509_SAN_DNS_NAME:
215 case MBEDTLS_X509_SAN_RFC822_NAME:
216 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
217 case MBEDTLS_X509_SAN_IP_ADDRESS:
218 {
219 const unsigned char *unstructured_name =
220 (const unsigned char *) cur->node.san.unstructured_name.p;
221 size_t unstructured_name_len = cur->node.san.unstructured_name.len;
222
223 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
224 mbedtls_asn1_write_raw_buffer(
225 &p, buf,
226 unstructured_name, unstructured_name_len));
227 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, mbedtls_asn1_write_len(
228 &p, buf, unstructured_name_len));
229 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
230 mbedtls_asn1_write_tag(
231 &p, buf,
232 MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type));
233 }
234 break;
235 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
236 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
237 mbedtls_x509_write_names(&p, buf,
238 (mbedtls_asn1_named_data *) &
239 cur->node
240 .san.directory_name));
241 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
242 mbedtls_asn1_write_len(&p, buf, single_san_len));
243 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
244 mbedtls_asn1_write_tag(&p, buf,
245 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
246 MBEDTLS_ASN1_CONSTRUCTED |
247 MBEDTLS_X509_SAN_DIRECTORY_NAME));
248 break;
249 default:
Andrzej Kurekdc220902023-04-25 02:29:00 -0400250 /* Error out on an unsupported SAN */
251 ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
252 goto cleanup;
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400253 }
254 cur = cur->next;
255 len += single_san_len;
256 }
257
258 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
259 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
260 mbedtls_asn1_write_tag(&p, buf,
261 MBEDTLS_ASN1_CONSTRUCTED |
262 MBEDTLS_ASN1_SEQUENCE));
263
264 ret = mbedtls_x509write_crt_set_extension(
265 ctx,
266 MBEDTLS_OID_SUBJECT_ALT_NAME,
267 MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
268 0,
269 buf + buflen - len,
270 len);
271
272cleanup:
273 mbedtls_free(buf);
274 return ret;
275}
276
277
Gilles Peskine449bd832023-01-11 14:50:10 +0100278int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
279 const char *oid, size_t oid_len,
280 int critical,
281 const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200282{
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
284 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200285}
286
Gilles Peskine449bd832023-01-11 14:50:10 +0100287int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
288 int is_ca, int max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289{
Janos Follath865b3eb2019-12-16 11:46:15 +0000290 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291 unsigned char buf[9];
292 unsigned char *c = buf + sizeof(buf);
293 size_t len = 0;
294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 memset(buf, 0, sizeof(buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (is_ca && max_pathlen > 127) {
298 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200299 }
300
Gilles Peskine449bd832023-01-11 14:50:10 +0100301 if (is_ca) {
302 if (max_pathlen >= 0) {
303 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
304 max_pathlen));
305 }
306 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
307 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
310 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
311 MBEDTLS_ASN1_CONSTRUCTED |
312 MBEDTLS_ASN1_SEQUENCE));
313
314 return
315 mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
316 MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
317 is_ca, buf + sizeof(buf) - len, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200318}
319
Manuel Pégourié-Gonnarda9464892023-03-17 12:08:50 +0100320#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100321static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
322 int is_ca,
323 unsigned char tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324{
Janos Follath865b3eb2019-12-16 11:46:15 +0000325 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 unsigned char *c = buf + sizeof(buf);
328 size_t len = 0;
pespacek7599a772022-02-07 14:40:55 +0100329#if defined(MBEDTLS_USE_PSA_CRYPTO)
330 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
331 size_t hash_length;
332#endif /* MBEDTLS_USE_PSA_CRYPTO */
333
Gilles Peskine449bd832023-01-11 14:50:10 +0100334 memset(buf, 0, sizeof(buf));
335 MBEDTLS_ASN1_CHK_ADD(len,
336 mbedtls_pk_write_pubkey(&c,
337 buf,
338 is_ca ?
339 ctx->issuer_key :
340 ctx->subject_key));
pespacek30151482022-02-17 15:18:47 +0100341
pespaceka6e955e2022-02-14 15:20:57 +0100342
pespacek7599a772022-02-07 14:40:55 +0100343#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100344 status = psa_hash_compute(PSA_ALG_SHA_1,
345 buf + sizeof(buf) - len,
346 len,
347 buf + sizeof(buf) - 20,
348 20,
349 &hash_length);
350 if (status != PSA_SUCCESS) {
351 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100352 }
353#else
Manuel Pégourié-Gonnard2cd75142023-02-24 12:37:07 +0100354 ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
355 buf + sizeof(buf) - len, len,
356 buf + sizeof(buf) - 20);
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (ret != 0) {
358 return ret;
359 }
pespacek7599a772022-02-07 14:40:55 +0100360#endif /* MBEDTLS_USE_PSA_CRYPTO */
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 c = buf + sizeof(buf) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200363 len = 20;
364
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
366 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200367
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 if (is_ca) { // writes AuthorityKeyIdentifier sequence
369 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
370 MBEDTLS_ASN1_CHK_ADD(len,
371 mbedtls_asn1_write_tag(&c,
372 buf,
373 MBEDTLS_ASN1_CONSTRUCTED |
374 MBEDTLS_ASN1_SEQUENCE));
pespacek30151482022-02-17 15:18:47 +0100375 }
pespacekd924e552022-02-28 11:49:54 +0100376
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 if (is_ca) {
378 return mbedtls_x509write_crt_set_extension(ctx,
379 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
380 MBEDTLS_OID_SIZE(
381 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER),
382 0, buf + sizeof(buf) - len, len);
383 } else {
384 return mbedtls_x509write_crt_set_extension(ctx,
385 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
386 MBEDTLS_OID_SIZE(
387 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER),
388 0, buf + sizeof(buf) - len, len);
389 }
pespaceka6e955e2022-02-14 15:20:57 +0100390}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391
Gilles Peskine449bd832023-01-11 14:50:10 +0100392int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100393{
Gilles Peskine449bd832023-01-11 14:50:10 +0100394 return mbedtls_x509write_crt_set_key_identifier(ctx,
395 0,
396 MBEDTLS_ASN1_OCTET_STRING);
pespaceka6e955e2022-02-14 15:20:57 +0100397}
398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100400{
Gilles Peskine449bd832023-01-11 14:50:10 +0100401 return mbedtls_x509write_crt_set_key_identifier(ctx,
402 1,
403 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404}
Manuel Pégourié-Gonnarda9464892023-03-17 12:08:50 +0100405#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406
Gilles Peskine449bd832023-01-11 14:50:10 +0100407int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
408 unsigned int key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200409{
Gilles Peskine449bd832023-01-11 14:50:10 +0100410 unsigned char buf[5] = { 0 }, ku[2] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100413 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
Gilles Peskine449bd832023-01-11 14:50:10 +0100414 MBEDTLS_X509_KU_NON_REPUDIATION |
415 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
416 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
417 MBEDTLS_X509_KU_KEY_AGREEMENT |
418 MBEDTLS_X509_KU_KEY_CERT_SIGN |
419 MBEDTLS_X509_KU_CRL_SIGN |
420 MBEDTLS_X509_KU_ENCIPHER_ONLY |
421 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200422
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100423 /* Check that nothing other than the allowed flags is set */
Gilles Peskine449bd832023-01-11 14:50:10 +0100424 if ((key_usage & ~allowed_bits) != 0) {
425 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
426 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100428 c = buf + 5;
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
430 ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200431
Gilles Peskine449bd832023-01-11 14:50:10 +0100432 if (ret < 0) {
433 return ret;
434 } else if (ret < 3 || ret > 5) {
435 return MBEDTLS_ERR_X509_INVALID_FORMAT;
436 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200437
Gilles Peskine449bd832023-01-11 14:50:10 +0100438 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
439 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
440 1, c, (size_t) ret);
441 if (ret != 0) {
442 return ret;
443 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200444
Gilles Peskine449bd832023-01-11 14:50:10 +0100445 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446}
447
Gilles Peskine449bd832023-01-11 14:50:10 +0100448int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
449 const mbedtls_asn1_sequence *exts)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100450{
451 unsigned char buf[256];
452 unsigned char *c = buf + sizeof(buf);
453 int ret;
454 size_t len = 0;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100455 const mbedtls_asn1_sequence *last_ext = NULL;
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100456 const mbedtls_asn1_sequence *ext;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100457
Gilles Peskine449bd832023-01-11 14:50:10 +0100458 memset(buf, 0, sizeof(buf));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100459
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000460 /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
Gilles Peskine449bd832023-01-11 14:50:10 +0100461 if (exts == NULL) {
462 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
463 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100464
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000465 /* Iterate over exts backwards, so we write them out in the requested order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100466 while (last_ext != exts) {
467 for (ext = exts; ext->next != last_ext; ext = ext->next) {
468 }
469 if (ext->buf.tag != MBEDTLS_ASN1_OID) {
470 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
471 }
472 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len));
473 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len));
474 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID));
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000475 last_ext = ext;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100476 }
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
479 MBEDTLS_ASN1_CHK_ADD(len,
480 mbedtls_asn1_write_tag(&c, buf,
481 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100482
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 return mbedtls_x509write_crt_set_extension(ctx,
484 MBEDTLS_OID_EXTENDED_KEY_USAGE,
485 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
486 1, c, len);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100487}
488
Gilles Peskine449bd832023-01-11 14:50:10 +0100489int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
490 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491{
Gilles Peskine449bd832023-01-11 14:50:10 +0100492 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000494 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495
496 c = buf + 4;
497
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
499 if (ret < 3 || ret > 4) {
500 return ret;
501 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
504 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
505 0, c, (size_t) ret);
506 if (ret != 0) {
507 return ret;
508 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200509
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511}
512
Gilles Peskine449bd832023-01-11 14:50:10 +0100513static int x509_write_time(unsigned char **p, unsigned char *start,
514 const char *t, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200515{
Janos Follath865b3eb2019-12-16 11:46:15 +0000516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517 size_t len = 0;
518
519 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200520 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200521 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
523 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
524 (const unsigned char *) t + 2,
525 size - 2));
526 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
527 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
528 MBEDTLS_ASN1_UTC_TIME));
529 } else {
530 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
531 (const unsigned char *) t,
532 size));
533 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
534 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
535 MBEDTLS_ASN1_GENERALIZED_TIME));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536 }
537
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200539}
540
Gilles Peskine449bd832023-01-11 14:50:10 +0100541int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
542 unsigned char *buf, size_t size,
543 int (*f_rng)(void *, unsigned char *, size_t),
544 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200545{
Janos Follath865b3eb2019-12-16 11:46:15 +0000546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547 const char *sig_oid;
548 size_t sig_oid_len = 0;
549 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100550 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100551 size_t hash_length = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +0200552 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100553#if defined(MBEDTLS_USE_PSA_CRYPTO)
554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
555 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +0100556#endif /* MBEDTLS_USE_PSA_CRYPTO */
557
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
559 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_pk_type_t pk_alg;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200561
562 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100563 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200564 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100565 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566
567 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100568
569 /* There's no direct way of extracting a signature algorithm
570 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100572 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 } else {
576 return MBEDTLS_ERR_X509_INVALID_ALG;
577 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
580 &sig_oid, &sig_oid_len)) != 0) {
581 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200582 }
583
584 /*
585 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
586 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100587
588 /* Only for v3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
590 MBEDTLS_ASN1_CHK_ADD(len,
591 mbedtls_x509_write_extensions(&c,
592 buf, ctx->extensions));
593 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
594 MBEDTLS_ASN1_CHK_ADD(len,
595 mbedtls_asn1_write_tag(&c, buf,
596 MBEDTLS_ASN1_CONSTRUCTED |
597 MBEDTLS_ASN1_SEQUENCE));
598 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
599 MBEDTLS_ASN1_CHK_ADD(len,
600 mbedtls_asn1_write_tag(&c, buf,
601 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
602 MBEDTLS_ASN1_CONSTRUCTED | 3));
Hanno Beckerd7f35202017-09-13 12:00:15 +0100603 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200604
605 /*
606 * SubjectPublicKeyInfo
607 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100608 MBEDTLS_ASN1_CHK_ADD(pub_len,
609 mbedtls_pk_write_pubkey_der(ctx->subject_key,
610 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200611 c -= pub_len;
612 len += pub_len;
613
614 /*
615 * Subject ::= Name
616 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100617 MBEDTLS_ASN1_CHK_ADD(len,
618 mbedtls_x509_write_names(&c, buf,
619 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200620
621 /*
622 * Validity ::= SEQUENCE {
623 * notBefore Time,
624 * notAfter Time }
625 */
626 sub_len = 0;
627
Gilles Peskine449bd832023-01-11 14:50:10 +0100628 MBEDTLS_ASN1_CHK_ADD(sub_len,
629 x509_write_time(&c, buf, ctx->not_after,
630 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632 MBEDTLS_ASN1_CHK_ADD(sub_len,
633 x509_write_time(&c, buf, ctx->not_before,
634 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200635
636 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100637 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
638 MBEDTLS_ASN1_CHK_ADD(len,
639 mbedtls_asn1_write_tag(&c, buf,
640 MBEDTLS_ASN1_CONSTRUCTED |
641 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200642
643 /*
644 * Issuer ::= Name
645 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100646 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
647 ctx->issuer));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200648
649 /*
650 * Signature ::= AlgorithmIdentifier
651 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100652 MBEDTLS_ASN1_CHK_ADD(len,
653 mbedtls_asn1_write_algorithm_identifier(&c, buf,
654 sig_oid, strlen(sig_oid), 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200655
656 /*
657 * Serial ::= INTEGER
Valerio Settida0afcc2023-01-05 16:46:59 +0100658 *
659 * Written data is:
Valerio Setti4752aac2023-01-10 16:00:15 +0100660 * - "ctx->serial_len" bytes for the raw serial buffer
661 * - if MSb of "serial" is 1, then prepend an extra 0x00 byte
Valerio Settida0afcc2023-01-05 16:46:59 +0100662 * - 1 byte for the length
663 * - 1 byte for the TAG
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200664 */
Valerio Settida0afcc2023-01-05 16:46:59 +0100665 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf,
666 ctx->serial, ctx->serial_len));
Valerio Setti4752aac2023-01-10 16:00:15 +0100667 if (*c & 0x80) {
668 if (c - buf < 1) {
669 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
670 }
Valerio Setti856cec42023-01-12 14:56:54 +0100671 *(--c) = 0x0;
Valerio Setti4752aac2023-01-10 16:00:15 +0100672 len++;
673 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
674 ctx->serial_len + 1));
675 } else {
676 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
677 ctx->serial_len));
678 }
Valerio Settida0afcc2023-01-05 16:46:59 +0100679 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
680 MBEDTLS_ASN1_INTEGER));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200681
682 /*
683 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
684 */
Hanno Becker47698652017-09-13 11:59:26 +0100685
686 /* Can be omitted for v1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
Hanno Becker47698652017-09-13 11:59:26 +0100688 sub_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 MBEDTLS_ASN1_CHK_ADD(sub_len,
690 mbedtls_asn1_write_int(&c, buf, ctx->version));
Hanno Becker47698652017-09-13 11:59:26 +0100691 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 MBEDTLS_ASN1_CHK_ADD(len,
693 mbedtls_asn1_write_len(&c, buf, sub_len));
694 MBEDTLS_ASN1_CHK_ADD(len,
695 mbedtls_asn1_write_tag(&c, buf,
696 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
697 MBEDTLS_ASN1_CONSTRUCTED | 0));
Hanno Becker47698652017-09-13 11:59:26 +0100698 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200699
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
701 MBEDTLS_ASN1_CHK_ADD(len,
702 mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
703 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200704
705 /*
706 * Make signature
707 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100708
709 /* Compute hash of CRT. */
pespacek7599a772022-02-07 14:40:55 +0100710#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 psa_algorithm = mbedtls_hash_info_psa_from_md(ctx->md_alg);
pespacek7599a772022-02-07 14:40:55 +0100712
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 status = psa_hash_compute(psa_algorithm,
714 c,
715 len,
716 hash,
717 sizeof(hash),
718 &hash_length);
719 if (status != PSA_SUCCESS) {
720 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100721 }
722#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100723 if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c,
724 len, hash)) != 0) {
725 return ret;
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100726 }
pespacek7599a772022-02-07 14:40:55 +0100727#endif /* MBEDTLS_USE_PSA_CRYPTO */
728
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200729
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
731 hash, hash_length, sig, sizeof(sig), &sig_len,
732 f_rng, p_rng)) != 0) {
733 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200734 }
735
Hanno Beckerdef43052019-05-04 07:54:36 +0100736 /* Move CRT to the front of the buffer to have space
737 * for the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 memmove(buf, c, len);
Hanno Beckerdef43052019-05-04 07:54:36 +0100739 c = buf + len;
740
741 /* Add signature at the end of the buffer,
742 * making sure that it doesn't underflow
743 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200744 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
746 sig_oid, sig_oid_len, sig,
747 sig_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200748
Hanno Beckerdef43052019-05-04 07:54:36 +0100749 /*
750 * Memory layout after this step:
751 *
752 * buf c=buf+len c2 buf+size
753 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
754 */
Andres AG60dbc932016-09-02 15:23:48 +0100755
Hanno Beckerdef43052019-05-04 07:54:36 +0100756 /* Move raw CRT to just before the signature. */
757 c = c2 - len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100758 memmove(c, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200759
760 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100761 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
762 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
763 MBEDTLS_ASN1_CONSTRUCTED |
764 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200765
Gilles Peskine449bd832023-01-11 14:50:10 +0100766 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767}
768
769#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
770#define PEM_END_CRT "-----END CERTIFICATE-----\n"
771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100773int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt,
774 unsigned char *buf, size_t size,
775 int (*f_rng)(void *, unsigned char *, size_t),
776 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200777{
Janos Follath865b3eb2019-12-16 11:46:15 +0000778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100779 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if ((ret = mbedtls_x509write_crt_der(crt, buf, size,
782 f_rng, p_rng)) < 0) {
783 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200784 }
785
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
787 buf + size - ret, ret,
788 buf, size, &olen)) != 0) {
789 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200790 }
791
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200793}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200794#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#endif /* MBEDTLS_X509_CRT_WRITE_C */