blob: aa4b9074c0e895817c05f4cf1554e22176ed7317 [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 }
206
207 mbedtls_platform_zeroize(buf, buflen);
208 p = buf + buflen;
209
210 /* Write ASN.1-based structure */
211 cur = san_list;
212 len = 0;
213 while (cur != NULL) {
214 size_t single_san_len = 0;
215 switch (cur->node.type) {
216 case MBEDTLS_X509_SAN_DNS_NAME:
217 case MBEDTLS_X509_SAN_RFC822_NAME:
218 case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
219 case MBEDTLS_X509_SAN_IP_ADDRESS:
220 {
221 const unsigned char *unstructured_name =
222 (const unsigned char *) cur->node.san.unstructured_name.p;
223 size_t unstructured_name_len = cur->node.san.unstructured_name.len;
224
225 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
226 mbedtls_asn1_write_raw_buffer(
227 &p, buf,
228 unstructured_name, unstructured_name_len));
229 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, mbedtls_asn1_write_len(
230 &p, buf, unstructured_name_len));
231 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
232 mbedtls_asn1_write_tag(
233 &p, buf,
234 MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type));
235 }
236 break;
237 case MBEDTLS_X509_SAN_DIRECTORY_NAME:
238 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
239 mbedtls_x509_write_names(&p, buf,
240 (mbedtls_asn1_named_data *) &
241 cur->node
242 .san.directory_name));
243 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
244 mbedtls_asn1_write_len(&p, buf, single_san_len));
245 MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len,
246 mbedtls_asn1_write_tag(&p, buf,
247 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
248 MBEDTLS_ASN1_CONSTRUCTED |
249 MBEDTLS_X509_SAN_DIRECTORY_NAME));
250 break;
251 default:
Andrzej Kurekdc220902023-04-25 02:29:00 -0400252 /* Error out on an unsupported SAN */
253 ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
254 goto cleanup;
Andrzej Kurek67fdb332023-04-04 06:56:14 -0400255 }
256 cur = cur->next;
257 len += single_san_len;
258 }
259
260 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
261 MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
262 mbedtls_asn1_write_tag(&p, buf,
263 MBEDTLS_ASN1_CONSTRUCTED |
264 MBEDTLS_ASN1_SEQUENCE));
265
266 ret = mbedtls_x509write_crt_set_extension(
267 ctx,
268 MBEDTLS_OID_SUBJECT_ALT_NAME,
269 MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
270 0,
271 buf + buflen - len,
272 len);
273
274cleanup:
275 mbedtls_free(buf);
276 return ret;
277}
278
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
281 const char *oid, size_t oid_len,
282 int critical,
283 const unsigned char *val, size_t val_len)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284{
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
286 critical, val, val_len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287}
288
Gilles Peskine449bd832023-01-11 14:50:10 +0100289int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
290 int is_ca, int max_pathlen)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291{
Janos Follath865b3eb2019-12-16 11:46:15 +0000292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 unsigned char buf[9];
294 unsigned char *c = buf + sizeof(buf);
295 size_t len = 0;
296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 memset(buf, 0, sizeof(buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 if (is_ca && max_pathlen > 127) {
300 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301 }
302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 if (is_ca) {
304 if (max_pathlen >= 0) {
305 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
306 max_pathlen));
307 }
308 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
309 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
312 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
313 MBEDTLS_ASN1_CONSTRUCTED |
314 MBEDTLS_ASN1_SEQUENCE));
315
316 return
317 mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
318 MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
319 is_ca, buf + sizeof(buf) - len, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320}
321
Manuel Pégourié-Gonnarda9464892023-03-17 12:08:50 +0100322#if defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +0100323static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
324 int is_ca,
325 unsigned char tag)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200326{
Janos Follath865b3eb2019-12-16 11:46:15 +0000327 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 unsigned char *c = buf + sizeof(buf);
330 size_t len = 0;
pespacek7599a772022-02-07 14:40:55 +0100331#if defined(MBEDTLS_USE_PSA_CRYPTO)
332 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
333 size_t hash_length;
334#endif /* MBEDTLS_USE_PSA_CRYPTO */
335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 memset(buf, 0, sizeof(buf));
337 MBEDTLS_ASN1_CHK_ADD(len,
338 mbedtls_pk_write_pubkey(&c,
339 buf,
340 is_ca ?
341 ctx->issuer_key :
342 ctx->subject_key));
pespacek30151482022-02-17 15:18:47 +0100343
pespaceka6e955e2022-02-14 15:20:57 +0100344
pespacek7599a772022-02-07 14:40:55 +0100345#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 status = psa_hash_compute(PSA_ALG_SHA_1,
347 buf + sizeof(buf) - len,
348 len,
349 buf + sizeof(buf) - 20,
350 20,
351 &hash_length);
352 if (status != PSA_SUCCESS) {
353 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100354 }
355#else
Manuel Pégourié-Gonnard2cd75142023-02-24 12:37:07 +0100356 ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
357 buf + sizeof(buf) - len, len,
358 buf + sizeof(buf) - 20);
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (ret != 0) {
360 return ret;
361 }
pespacek7599a772022-02-07 14:40:55 +0100362#endif /* MBEDTLS_USE_PSA_CRYPTO */
363
Gilles Peskine449bd832023-01-11 14:50:10 +0100364 c = buf + sizeof(buf) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200365 len = 20;
366
Gilles Peskine449bd832023-01-11 14:50:10 +0100367 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
368 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (is_ca) { // writes AuthorityKeyIdentifier sequence
371 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
372 MBEDTLS_ASN1_CHK_ADD(len,
373 mbedtls_asn1_write_tag(&c,
374 buf,
375 MBEDTLS_ASN1_CONSTRUCTED |
376 MBEDTLS_ASN1_SEQUENCE));
pespacek30151482022-02-17 15:18:47 +0100377 }
pespacekd924e552022-02-28 11:49:54 +0100378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (is_ca) {
380 return mbedtls_x509write_crt_set_extension(ctx,
381 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
382 MBEDTLS_OID_SIZE(
383 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER),
384 0, buf + sizeof(buf) - len, len);
385 } else {
386 return mbedtls_x509write_crt_set_extension(ctx,
387 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
388 MBEDTLS_OID_SIZE(
389 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER),
390 0, buf + sizeof(buf) - len, len);
391 }
pespaceka6e955e2022-02-14 15:20:57 +0100392}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393
Gilles Peskine449bd832023-01-11 14:50:10 +0100394int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100395{
Gilles Peskine449bd832023-01-11 14:50:10 +0100396 return mbedtls_x509write_crt_set_key_identifier(ctx,
397 0,
398 MBEDTLS_ASN1_OCTET_STRING);
pespaceka6e955e2022-02-14 15:20:57 +0100399}
400
Gilles Peskine449bd832023-01-11 14:50:10 +0100401int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
pespaceka6e955e2022-02-14 15:20:57 +0100402{
Gilles Peskine449bd832023-01-11 14:50:10 +0100403 return mbedtls_x509write_crt_set_key_identifier(ctx,
404 1,
405 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200406}
Manuel Pégourié-Gonnarda9464892023-03-17 12:08:50 +0100407#endif /* MBEDTLS_MD_CAN_SHA1 */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200408
Gilles Peskine449bd832023-01-11 14:50:10 +0100409int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
410 unsigned int key_usage)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200411{
Gilles Peskine449bd832023-01-11 14:50:10 +0100412 unsigned char buf[5] = { 0 }, ku[2] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200413 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000414 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100415 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 MBEDTLS_X509_KU_NON_REPUDIATION |
417 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
418 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
419 MBEDTLS_X509_KU_KEY_AGREEMENT |
420 MBEDTLS_X509_KU_KEY_CERT_SIGN |
421 MBEDTLS_X509_KU_CRL_SIGN |
422 MBEDTLS_X509_KU_ENCIPHER_ONLY |
423 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200424
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100425 /* Check that nothing other than the allowed flags is set */
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 if ((key_usage & ~allowed_bits) != 0) {
427 return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
428 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200429
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100430 c = buf + 5;
Gilles Peskine449bd832023-01-11 14:50:10 +0100431 MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
432 ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200433
Gilles Peskine449bd832023-01-11 14:50:10 +0100434 if (ret < 0) {
435 return ret;
436 } else if (ret < 3 || ret > 5) {
437 return MBEDTLS_ERR_X509_INVALID_FORMAT;
438 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439
Gilles Peskine449bd832023-01-11 14:50:10 +0100440 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
441 MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
442 1, c, (size_t) ret);
443 if (ret != 0) {
444 return ret;
445 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200446
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448}
449
Gilles Peskine449bd832023-01-11 14:50:10 +0100450int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
451 const mbedtls_asn1_sequence *exts)
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100452{
453 unsigned char buf[256];
454 unsigned char *c = buf + sizeof(buf);
455 int ret;
456 size_t len = 0;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100457 const mbedtls_asn1_sequence *last_ext = NULL;
Dave Rodgmane2b772d2022-08-11 16:04:13 +0100458 const mbedtls_asn1_sequence *ext;
Dave Rodgman5f3f0d02022-08-11 14:38:26 +0100459
Gilles Peskine449bd832023-01-11 14:50:10 +0100460 memset(buf, 0, sizeof(buf));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100461
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000462 /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
Gilles Peskine449bd832023-01-11 14:50:10 +0100463 if (exts == NULL) {
464 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
465 }
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100466
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000467 /* Iterate over exts backwards, so we write them out in the requested order */
Gilles Peskine449bd832023-01-11 14:50:10 +0100468 while (last_ext != exts) {
469 for (ext = exts; ext->next != last_ext; ext = ext->next) {
470 }
471 if (ext->buf.tag != MBEDTLS_ASN1_OID) {
472 return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
473 }
474 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len));
475 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len));
476 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID));
Nicholas Wilsonca841d32015-11-13 14:22:36 +0000477 last_ext = ext;
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100478 }
479
Gilles Peskine449bd832023-01-11 14:50:10 +0100480 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
481 MBEDTLS_ASN1_CHK_ADD(len,
482 mbedtls_asn1_write_tag(&c, buf,
483 MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100484
Gilles Peskine449bd832023-01-11 14:50:10 +0100485 return mbedtls_x509write_crt_set_extension(ctx,
486 MBEDTLS_OID_EXTENDED_KEY_USAGE,
487 MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
488 1, c, len);
Nicholas Wilson8e5bdfb2015-09-09 19:03:34 +0100489}
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
492 unsigned char ns_cert_type)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493{
Gilles Peskine449bd832023-01-11 14:50:10 +0100494 unsigned char buf[4] = { 0 };
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200495 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 c = buf + 4;
499
Gilles Peskine449bd832023-01-11 14:50:10 +0100500 ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
501 if (ret < 3 || ret > 4) {
502 return ret;
503 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200504
Gilles Peskine449bd832023-01-11 14:50:10 +0100505 ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
506 MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
507 0, c, (size_t) ret);
508 if (ret != 0) {
509 return ret;
510 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513}
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515static int x509_write_time(unsigned char **p, unsigned char *start,
516 const char *t, size_t size)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200517{
Janos Follath865b3eb2019-12-16 11:46:15 +0000518 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200519 size_t len = 0;
520
521 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200522 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
525 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
526 (const unsigned char *) t + 2,
527 size - 2));
528 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
529 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
530 MBEDTLS_ASN1_UTC_TIME));
531 } else {
532 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
533 (const unsigned char *) t,
534 size));
535 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
536 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
537 MBEDTLS_ASN1_GENERALIZED_TIME));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200538 }
539
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200541}
542
Gilles Peskine449bd832023-01-11 14:50:10 +0100543int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx,
544 unsigned char *buf, size_t size,
545 int (*f_rng)(void *, unsigned char *, size_t),
546 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200547{
Janos Follath865b3eb2019-12-16 11:46:15 +0000548 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200549 const char *sig_oid;
550 size_t sig_oid_len = 0;
551 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100552 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100553 size_t hash_length = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +0200554 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100555#if defined(MBEDTLS_USE_PSA_CRYPTO)
556 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
557 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +0100558#endif /* MBEDTLS_USE_PSA_CRYPTO */
559
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
561 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_pk_type_t pk_alg;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200563
564 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100565 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200566 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100567 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200568
569 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100570
571 /* There's no direct way of extracting a signature algorithm
572 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
Hanno Beckerfc771442017-09-13 08:45:48 +0100574 pk_alg = MBEDTLS_PK_RSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100575 } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 pk_alg = MBEDTLS_PK_ECDSA;
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 } else {
578 return MBEDTLS_ERR_X509_INVALID_ALG;
579 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200580
Gilles Peskine449bd832023-01-11 14:50:10 +0100581 if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
582 &sig_oid, &sig_oid_len)) != 0) {
583 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200584 }
585
586 /*
587 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
588 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100589
590 /* Only for v3 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
592 MBEDTLS_ASN1_CHK_ADD(len,
593 mbedtls_x509_write_extensions(&c,
594 buf, ctx->extensions));
595 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
596 MBEDTLS_ASN1_CHK_ADD(len,
597 mbedtls_asn1_write_tag(&c, buf,
598 MBEDTLS_ASN1_CONSTRUCTED |
599 MBEDTLS_ASN1_SEQUENCE));
600 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
601 MBEDTLS_ASN1_CHK_ADD(len,
602 mbedtls_asn1_write_tag(&c, buf,
603 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
604 MBEDTLS_ASN1_CONSTRUCTED | 3));
Hanno Beckerd7f35202017-09-13 12:00:15 +0100605 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200606
607 /*
608 * SubjectPublicKeyInfo
609 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 MBEDTLS_ASN1_CHK_ADD(pub_len,
611 mbedtls_pk_write_pubkey_der(ctx->subject_key,
612 buf, c - buf));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200613 c -= pub_len;
614 len += pub_len;
615
616 /*
617 * Subject ::= Name
618 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100619 MBEDTLS_ASN1_CHK_ADD(len,
620 mbedtls_x509_write_names(&c, buf,
621 ctx->subject));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200622
623 /*
624 * Validity ::= SEQUENCE {
625 * notBefore Time,
626 * notAfter Time }
627 */
628 sub_len = 0;
629
Gilles Peskine449bd832023-01-11 14:50:10 +0100630 MBEDTLS_ASN1_CHK_ADD(sub_len,
631 x509_write_time(&c, buf, ctx->not_after,
632 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200633
Gilles Peskine449bd832023-01-11 14:50:10 +0100634 MBEDTLS_ASN1_CHK_ADD(sub_len,
635 x509_write_time(&c, buf, ctx->not_before,
636 MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200637
638 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100639 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
640 MBEDTLS_ASN1_CHK_ADD(len,
641 mbedtls_asn1_write_tag(&c, buf,
642 MBEDTLS_ASN1_CONSTRUCTED |
643 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200644
645 /*
646 * Issuer ::= Name
647 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100648 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
649 ctx->issuer));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200650
651 /*
652 * Signature ::= AlgorithmIdentifier
653 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100654 MBEDTLS_ASN1_CHK_ADD(len,
655 mbedtls_asn1_write_algorithm_identifier(&c, buf,
656 sig_oid, strlen(sig_oid), 0));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200657
658 /*
659 * Serial ::= INTEGER
Valerio Settida0afcc2023-01-05 16:46:59 +0100660 *
661 * Written data is:
Valerio Setti4752aac2023-01-10 16:00:15 +0100662 * - "ctx->serial_len" bytes for the raw serial buffer
663 * - if MSb of "serial" is 1, then prepend an extra 0x00 byte
Valerio Settida0afcc2023-01-05 16:46:59 +0100664 * - 1 byte for the length
665 * - 1 byte for the TAG
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200666 */
Valerio Settida0afcc2023-01-05 16:46:59 +0100667 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf,
668 ctx->serial, ctx->serial_len));
Valerio Setti4752aac2023-01-10 16:00:15 +0100669 if (*c & 0x80) {
670 if (c - buf < 1) {
671 return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
672 }
Valerio Setti856cec42023-01-12 14:56:54 +0100673 *(--c) = 0x0;
Valerio Setti4752aac2023-01-10 16:00:15 +0100674 len++;
675 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
676 ctx->serial_len + 1));
677 } else {
678 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
679 ctx->serial_len));
680 }
Valerio Settida0afcc2023-01-05 16:46:59 +0100681 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
682 MBEDTLS_ASN1_INTEGER));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200683
684 /*
685 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
686 */
Hanno Becker47698652017-09-13 11:59:26 +0100687
688 /* Can be omitted for v1 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
Hanno Becker47698652017-09-13 11:59:26 +0100690 sub_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 MBEDTLS_ASN1_CHK_ADD(sub_len,
692 mbedtls_asn1_write_int(&c, buf, ctx->version));
Hanno Becker47698652017-09-13 11:59:26 +0100693 len += sub_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 MBEDTLS_ASN1_CHK_ADD(len,
695 mbedtls_asn1_write_len(&c, buf, sub_len));
696 MBEDTLS_ASN1_CHK_ADD(len,
697 mbedtls_asn1_write_tag(&c, buf,
698 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
699 MBEDTLS_ASN1_CONSTRUCTED | 0));
Hanno Becker47698652017-09-13 11:59:26 +0100700 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200701
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
703 MBEDTLS_ASN1_CHK_ADD(len,
704 mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
705 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200706
707 /*
708 * Make signature
709 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100710
711 /* Compute hash of CRT. */
pespacek7599a772022-02-07 14:40:55 +0100712#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 psa_algorithm = mbedtls_hash_info_psa_from_md(ctx->md_alg);
pespacek7599a772022-02-07 14:40:55 +0100714
Gilles Peskine449bd832023-01-11 14:50:10 +0100715 status = psa_hash_compute(psa_algorithm,
716 c,
717 len,
718 hash,
719 sizeof(hash),
720 &hash_length);
721 if (status != PSA_SUCCESS) {
722 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
pespacek7599a772022-02-07 14:40:55 +0100723 }
724#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c,
726 len, hash)) != 0) {
727 return ret;
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100728 }
pespacek7599a772022-02-07 14:40:55 +0100729#endif /* MBEDTLS_USE_PSA_CRYPTO */
730
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
733 hash, hash_length, sig, sizeof(sig), &sig_len,
734 f_rng, p_rng)) != 0) {
735 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200736 }
737
Hanno Beckerdef43052019-05-04 07:54:36 +0100738 /* Move CRT to the front of the buffer to have space
739 * for the signature. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 memmove(buf, c, len);
Hanno Beckerdef43052019-05-04 07:54:36 +0100741 c = buf + len;
742
743 /* Add signature at the end of the buffer,
744 * making sure that it doesn't underflow
745 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200746 c2 = buf + size;
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
748 sig_oid, sig_oid_len, sig,
749 sig_len));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200750
Hanno Beckerdef43052019-05-04 07:54:36 +0100751 /*
752 * Memory layout after this step:
753 *
754 * buf c=buf+len c2 buf+size
755 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
756 */
Andres AG60dbc932016-09-02 15:23:48 +0100757
Hanno Beckerdef43052019-05-04 07:54:36 +0100758 /* Move raw CRT to just before the signature. */
759 c = c2 - len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100760 memmove(c, buf, len);
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200761
762 len += sig_and_oid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100763 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
764 MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
765 MBEDTLS_ASN1_CONSTRUCTED |
766 MBEDTLS_ASN1_SEQUENCE));
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200767
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 return (int) len;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200769}
770
771#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
772#define PEM_END_CRT "-----END CERTIFICATE-----\n"
773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200774#if defined(MBEDTLS_PEM_WRITE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100775int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt,
776 unsigned char *buf, size_t size,
777 int (*f_rng)(void *, unsigned char *, size_t),
778 void *p_rng)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200779{
Janos Follath865b3eb2019-12-16 11:46:15 +0000780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100781 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 if ((ret = mbedtls_x509write_crt_der(crt, buf, size,
784 f_rng, p_rng)) < 0) {
785 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200786 }
787
Gilles Peskine449bd832023-01-11 14:50:10 +0100788 if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
789 buf + size - ret, ret,
790 buf, size, &olen)) != 0) {
791 return ret;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200792 }
793
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 return 0;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200798#endif /* MBEDTLS_X509_CRT_WRITE_C */