Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * X.509 certificate writing |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 6 | */ |
| 7 | /* |
| 8 | * References: |
| 9 | * - certificates: RFC 5280, updated by RFC 6818 |
| 10 | * - CSRs: PKCS#10 v1.7 aka RFC 2986 |
| 11 | * - attributes: PKCS#9 v2.0 aka RFC 2985 |
| 12 | */ |
| 13 | |
Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 14 | #include "common.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 16 | #if defined(MBEDTLS_X509_CRT_WRITE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 18 | #include "mbedtls/x509_crt.h" |
Valerio Setti | 25b282e | 2024-01-17 10:55:32 +0100 | [diff] [blame] | 19 | #include "x509_internal.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 20 | #include "mbedtls/asn1write.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 21 | #include "mbedtls/error.h" |
| 22 | #include "mbedtls/oid.h" |
Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 23 | #include "mbedtls/platform.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 24 | #include "mbedtls/platform_util.h" |
Manuel Pégourié-Gonnard | 2cd7514 | 2023-02-24 12:37:07 +0100 | [diff] [blame] | 25 | #include "mbedtls/md.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 26 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 27 | #include <string.h> |
Andrzej Kurek | 63a6a26 | 2023-04-27 08:25:41 -0400 | [diff] [blame] | 28 | #include <stdint.h> |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | #if defined(MBEDTLS_PEM_WRITE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/pem.h" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 33 | |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 34 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 35 | #include "psa/crypto.h" |
Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 36 | #include "psa_util_internal.h" |
Valerio Setti | 384fbde | 2024-01-02 13:26:40 +0100 | [diff] [blame] | 37 | #include "mbedtls/psa_util.h" |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 38 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 39 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 40 | void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 41 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 42 | memset(ctx, 0, sizeof(mbedtls_x509write_cert)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 43 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | ctx->version = MBEDTLS_X509_CRT_VERSION_3; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 47 | void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 48 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 49 | mbedtls_asn1_free_named_data_list(&ctx->subject); |
| 50 | mbedtls_asn1_free_named_data_list(&ctx->issuer); |
| 51 | mbedtls_asn1_free_named_data_list(&ctx->extensions); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 52 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_cert)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 56 | void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx, |
| 57 | int version) |
Paul Bakker | 5191e92 | 2013-10-11 10:54:28 +0200 | [diff] [blame] | 58 | { |
| 59 | ctx->version = version; |
| 60 | } |
| 61 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 62 | void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx, |
| 63 | mbedtls_md_type_t md_alg) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 64 | { |
| 65 | ctx->md_alg = md_alg; |
| 66 | } |
| 67 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx, |
| 69 | mbedtls_pk_context *key) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 70 | { |
| 71 | ctx->subject_key = key; |
| 72 | } |
| 73 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx, |
| 75 | mbedtls_pk_context *key) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 76 | { |
| 77 | ctx->issuer_key = key; |
| 78 | } |
| 79 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 80 | int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx, |
| 81 | const char *subject_name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 82 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | return mbedtls_x509_string_to_names(&ctx->subject, subject_name); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 84 | } |
| 85 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx, |
| 87 | const char *issuer_name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 88 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | return mbedtls_x509_string_to_names(&ctx->issuer, issuer_name); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Valerio Setti | 5d164c4 | 2023-01-09 12:19:40 +0100 | [diff] [blame] | 92 | #if defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 93 | int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx, |
| 94 | const mbedtls_mpi *serial) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 95 | { |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 96 | int ret; |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 97 | size_t tmp_len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 98 | |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 99 | /* Ensure that the MPI value fits into the buffer */ |
| 100 | tmp_len = mbedtls_mpi_size(serial); |
| 101 | if (tmp_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) { |
| 102 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 103 | } |
| 104 | |
| 105 | ctx->serial_len = tmp_len; |
| 106 | |
Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 107 | ret = mbedtls_mpi_write_binary(serial, ctx->serial, tmp_len); |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 108 | if (ret < 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 109 | return ret; |
| 110 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 111 | |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 112 | return 0; |
| 113 | } |
Valerio Setti | 5d164c4 | 2023-01-09 12:19:40 +0100 | [diff] [blame] | 114 | #endif // MBEDTLS_BIGNUM_C && !MBEDTLS_DEPRECATED_REMOVED |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 115 | |
Valerio Setti | af4815c | 2023-01-26 17:43:09 +0100 | [diff] [blame] | 116 | int mbedtls_x509write_crt_set_serial_raw(mbedtls_x509write_cert *ctx, |
Valerio Setti | 746def5 | 2023-01-10 11:46:34 +0100 | [diff] [blame] | 117 | unsigned char *serial, size_t serial_len) |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 118 | { |
Valerio Setti | 746def5 | 2023-01-10 11:46:34 +0100 | [diff] [blame] | 119 | if (serial_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) { |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 120 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 121 | } |
| 122 | |
Valerio Setti | 746def5 | 2023-01-10 11:46:34 +0100 | [diff] [blame] | 123 | ctx->serial_len = serial_len; |
| 124 | memcpy(ctx->serial, serial, serial_len); |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 127 | } |
| 128 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 129 | int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx, |
| 130 | const char *not_before, |
| 131 | const char *not_after) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 132 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 || |
| 134 | strlen(not_after) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) { |
| 135 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 136 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | strncpy(ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN); |
| 138 | strncpy(ctx->not_after, not_after, MBEDTLS_X509_RFC5280_UTC_TIME_LEN); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
| 140 | ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 145 | int mbedtls_x509write_crt_set_subject_alternative_name(mbedtls_x509write_cert *ctx, |
| 146 | const mbedtls_x509_san_list *san_list) |
| 147 | { |
Andrzej Kurek | c508dc2 | 2023-07-07 08:20:02 -0400 | [diff] [blame] | 148 | return mbedtls_x509_write_set_san_common(&ctx->extensions, san_list); |
Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 152 | int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx, |
| 153 | const char *oid, size_t oid_len, |
| 154 | int critical, |
| 155 | const unsigned char *val, size_t val_len) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 156 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 157 | return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len, |
| 158 | critical, val, val_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 161 | int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx, |
| 162 | int is_ca, int max_pathlen) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 163 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 164 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 165 | unsigned char buf[9]; |
| 166 | unsigned char *c = buf + sizeof(buf); |
| 167 | size_t len = 0; |
| 168 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 169 | memset(buf, 0, sizeof(buf)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 170 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 171 | if (is_ca && max_pathlen > 127) { |
| 172 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 173 | } |
| 174 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 175 | if (is_ca) { |
| 176 | if (max_pathlen >= 0) { |
| 177 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, |
| 178 | max_pathlen)); |
| 179 | } |
| 180 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1)); |
| 181 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 182 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 183 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 184 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, |
| 185 | MBEDTLS_ASN1_CONSTRUCTED | |
| 186 | MBEDTLS_ASN1_SEQUENCE)); |
| 187 | |
| 188 | return |
| 189 | mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS, |
| 190 | MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS), |
| 191 | is_ca, buf + sizeof(buf) - len, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 192 | } |
| 193 | |
Manuel Pégourié-Gonnard | a946489 | 2023-03-17 12:08:50 +0100 | [diff] [blame] | 194 | #if defined(MBEDTLS_MD_CAN_SHA1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 195 | static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx, |
| 196 | int is_ca, |
| 197 | unsigned char tag) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 198 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 199 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 201 | unsigned char *c = buf + sizeof(buf); |
| 202 | size_t len = 0; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 203 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 204 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 205 | size_t hash_length; |
| 206 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 207 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 208 | memset(buf, 0, sizeof(buf)); |
| 209 | MBEDTLS_ASN1_CHK_ADD(len, |
| 210 | mbedtls_pk_write_pubkey(&c, |
| 211 | buf, |
| 212 | is_ca ? |
| 213 | ctx->issuer_key : |
| 214 | ctx->subject_key)); |
pespacek | 3015148 | 2022-02-17 15:18:47 +0100 | [diff] [blame] | 215 | |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 216 | |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 217 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 218 | status = psa_hash_compute(PSA_ALG_SHA_1, |
| 219 | buf + sizeof(buf) - len, |
| 220 | len, |
| 221 | buf + sizeof(buf) - 20, |
| 222 | 20, |
| 223 | &hash_length); |
| 224 | if (status != PSA_SUCCESS) { |
| 225 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 226 | } |
| 227 | #else |
Manuel Pégourié-Gonnard | 2cd7514 | 2023-02-24 12:37:07 +0100 | [diff] [blame] | 228 | ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), |
| 229 | buf + sizeof(buf) - len, len, |
| 230 | buf + sizeof(buf) - 20); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 231 | if (ret != 0) { |
| 232 | return ret; |
| 233 | } |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 234 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | c = buf + sizeof(buf) - 20; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 237 | len = 20; |
| 238 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 239 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 240 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 241 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 242 | if (is_ca) { // writes AuthorityKeyIdentifier sequence |
| 243 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 244 | MBEDTLS_ASN1_CHK_ADD(len, |
| 245 | mbedtls_asn1_write_tag(&c, |
| 246 | buf, |
| 247 | MBEDTLS_ASN1_CONSTRUCTED | |
| 248 | MBEDTLS_ASN1_SEQUENCE)); |
pespacek | 3015148 | 2022-02-17 15:18:47 +0100 | [diff] [blame] | 249 | } |
pespacek | d924e55 | 2022-02-28 11:49:54 +0100 | [diff] [blame] | 250 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 251 | if (is_ca) { |
| 252 | return mbedtls_x509write_crt_set_extension(ctx, |
| 253 | MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER, |
| 254 | MBEDTLS_OID_SIZE( |
| 255 | MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER), |
| 256 | 0, buf + sizeof(buf) - len, len); |
| 257 | } else { |
| 258 | return mbedtls_x509write_crt_set_extension(ctx, |
| 259 | MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER, |
| 260 | MBEDTLS_OID_SIZE( |
| 261 | MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER), |
| 262 | 0, buf + sizeof(buf) - len, len); |
| 263 | } |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 264 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 265 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx) |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 267 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 268 | return mbedtls_x509write_crt_set_key_identifier(ctx, |
| 269 | 0, |
| 270 | MBEDTLS_ASN1_OCTET_STRING); |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 271 | } |
| 272 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 273 | int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx) |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 274 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | return mbedtls_x509write_crt_set_key_identifier(ctx, |
| 276 | 1, |
| 277 | (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 278 | } |
Manuel Pégourié-Gonnard | a946489 | 2023-03-17 12:08:50 +0100 | [diff] [blame] | 279 | #endif /* MBEDTLS_MD_CAN_SHA1 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 280 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 281 | int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx, |
| 282 | unsigned int key_usage) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 283 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | unsigned char buf[5] = { 0 }, ku[2] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 285 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 286 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 287 | const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | MBEDTLS_X509_KU_NON_REPUDIATION | |
| 289 | MBEDTLS_X509_KU_KEY_ENCIPHERMENT | |
| 290 | MBEDTLS_X509_KU_DATA_ENCIPHERMENT | |
| 291 | MBEDTLS_X509_KU_KEY_AGREEMENT | |
| 292 | MBEDTLS_X509_KU_KEY_CERT_SIGN | |
| 293 | MBEDTLS_X509_KU_CRL_SIGN | |
| 294 | MBEDTLS_X509_KU_ENCIPHER_ONLY | |
| 295 | MBEDTLS_X509_KU_DECIPHER_ONLY; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 296 | |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 297 | /* Check that nothing other than the allowed flags is set */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | if ((key_usage & ~allowed_bits) != 0) { |
| 299 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 300 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 301 | |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 302 | c = buf + 5; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 303 | MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0); |
| 304 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9); |
Manuel Pégourié-Gonnard | 1cd10ad | 2015-06-23 11:07:37 +0200 | [diff] [blame] | 305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | if (ret < 0) { |
| 307 | return ret; |
| 308 | } else if (ret < 3 || ret > 5) { |
| 309 | return MBEDTLS_ERR_X509_INVALID_FORMAT; |
| 310 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 311 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 312 | ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE, |
| 313 | MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE), |
| 314 | 1, c, (size_t) ret); |
| 315 | if (ret != 0) { |
| 316 | return ret; |
| 317 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 320 | } |
| 321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx, |
| 323 | const mbedtls_asn1_sequence *exts) |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 324 | { |
| 325 | unsigned char buf[256]; |
| 326 | unsigned char *c = buf + sizeof(buf); |
| 327 | int ret; |
| 328 | size_t len = 0; |
Dave Rodgman | 5f3f0d0 | 2022-08-11 14:38:26 +0100 | [diff] [blame] | 329 | const mbedtls_asn1_sequence *last_ext = NULL; |
Dave Rodgman | e2b772d | 2022-08-11 16:04:13 +0100 | [diff] [blame] | 330 | const mbedtls_asn1_sequence *ext; |
Dave Rodgman | 5f3f0d0 | 2022-08-11 14:38:26 +0100 | [diff] [blame] | 331 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 332 | memset(buf, 0, sizeof(buf)); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 333 | |
Nicholas Wilson | ca841d3 | 2015-11-13 14:22:36 +0000 | [diff] [blame] | 334 | /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 335 | if (exts == NULL) { |
| 336 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 337 | } |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 338 | |
Nicholas Wilson | ca841d3 | 2015-11-13 14:22:36 +0000 | [diff] [blame] | 339 | /* Iterate over exts backwards, so we write them out in the requested order */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | while (last_ext != exts) { |
| 341 | for (ext = exts; ext->next != last_ext; ext = ext->next) { |
| 342 | } |
| 343 | if (ext->buf.tag != MBEDTLS_ASN1_OID) { |
| 344 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 345 | } |
| 346 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len)); |
| 347 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len)); |
| 348 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID)); |
Nicholas Wilson | ca841d3 | 2015-11-13 14:22:36 +0000 | [diff] [blame] | 349 | last_ext = ext; |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 350 | } |
| 351 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 352 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 353 | MBEDTLS_ASN1_CHK_ADD(len, |
| 354 | mbedtls_asn1_write_tag(&c, buf, |
| 355 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 356 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 357 | return mbedtls_x509write_crt_set_extension(ctx, |
| 358 | MBEDTLS_OID_EXTENDED_KEY_USAGE, |
| 359 | MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE), |
| 360 | 1, c, len); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 361 | } |
| 362 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx, |
| 364 | unsigned char ns_cert_type) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 365 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 366 | unsigned char buf[4] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 367 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 368 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 369 | |
| 370 | c = buf + 4; |
| 371 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8); |
| 373 | if (ret < 3 || ret > 4) { |
| 374 | return ret; |
| 375 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 376 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 377 | ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE, |
| 378 | MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE), |
| 379 | 0, c, (size_t) ret); |
| 380 | if (ret != 0) { |
| 381 | return ret; |
| 382 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 385 | } |
| 386 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 387 | static int x509_write_time(unsigned char **p, unsigned char *start, |
| 388 | const char *t, size_t size) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 389 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 390 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 391 | size_t len = 0; |
| 392 | |
| 393 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 394 | * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 395 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) { |
| 397 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, |
| 398 | (const unsigned char *) t + 2, |
| 399 | size - 2)); |
| 400 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 401 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 402 | MBEDTLS_ASN1_UTC_TIME)); |
| 403 | } else { |
| 404 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, |
| 405 | (const unsigned char *) t, |
| 406 | size)); |
| 407 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 408 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 409 | MBEDTLS_ASN1_GENERALIZED_TIME)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 410 | } |
| 411 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 415 | int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, |
| 416 | unsigned char *buf, size_t size, |
| 417 | int (*f_rng)(void *, unsigned char *, size_t), |
| 418 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 419 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 420 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 421 | const char *sig_oid; |
| 422 | size_t sig_oid_len = 0; |
| 423 | unsigned char *c, *c2; |
Gilles Peskine | bf88780 | 2019-11-08 19:21:51 +0100 | [diff] [blame] | 424 | unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 425 | size_t hash_length = 0; |
Manuel Pégourié-Gonnard | 8857984 | 2023-03-28 11:20:23 +0200 | [diff] [blame] | 426 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 427 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 428 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 429 | psa_algorithm_t psa_algorithm; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 430 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 431 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 432 | size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len; |
| 433 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 434 | mbedtls_pk_type_t pk_alg; |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 435 | int write_sig_null_par; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 436 | |
| 437 | /* |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 438 | * Prepare data to be signed at the end of the target buffer |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 439 | */ |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 440 | c = buf + size; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 441 | |
| 442 | /* Signature algorithm needed in TBS, and later for actual signature */ |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 443 | |
| 444 | /* There's no direct way of extracting a signature algorithm |
| 445 | * (represented as an element of mbedtls_pk_type_t) from a PK instance. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 446 | if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) { |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 447 | pk_alg = MBEDTLS_PK_RSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | } else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 449 | pk_alg = MBEDTLS_PK_ECDSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 450 | } else { |
| 451 | return MBEDTLS_ERR_X509_INVALID_ALG; |
| 452 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 453 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg, |
| 455 | &sig_oid, &sig_oid_len)) != 0) { |
| 456 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | /* |
| 460 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 461 | */ |
Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 462 | |
| 463 | /* Only for v3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) { |
| 465 | MBEDTLS_ASN1_CHK_ADD(len, |
| 466 | mbedtls_x509_write_extensions(&c, |
| 467 | buf, ctx->extensions)); |
| 468 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 469 | MBEDTLS_ASN1_CHK_ADD(len, |
| 470 | mbedtls_asn1_write_tag(&c, buf, |
| 471 | MBEDTLS_ASN1_CONSTRUCTED | |
| 472 | MBEDTLS_ASN1_SEQUENCE)); |
| 473 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 474 | MBEDTLS_ASN1_CHK_ADD(len, |
| 475 | mbedtls_asn1_write_tag(&c, buf, |
| 476 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 477 | MBEDTLS_ASN1_CONSTRUCTED | 3)); |
Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 478 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 479 | |
| 480 | /* |
| 481 | * SubjectPublicKeyInfo |
| 482 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 483 | MBEDTLS_ASN1_CHK_ADD(pub_len, |
| 484 | mbedtls_pk_write_pubkey_der(ctx->subject_key, |
Dave Rodgman | e4a6f5a | 2023-11-04 12:20:09 +0000 | [diff] [blame] | 485 | buf, (size_t) (c - buf))); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 486 | c -= pub_len; |
| 487 | len += pub_len; |
| 488 | |
| 489 | /* |
| 490 | * Subject ::= Name |
| 491 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 492 | MBEDTLS_ASN1_CHK_ADD(len, |
| 493 | mbedtls_x509_write_names(&c, buf, |
| 494 | ctx->subject)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 495 | |
| 496 | /* |
| 497 | * Validity ::= SEQUENCE { |
| 498 | * notBefore Time, |
| 499 | * notAfter Time } |
| 500 | */ |
| 501 | sub_len = 0; |
| 502 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 503 | MBEDTLS_ASN1_CHK_ADD(sub_len, |
| 504 | x509_write_time(&c, buf, ctx->not_after, |
| 505 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | MBEDTLS_ASN1_CHK_ADD(sub_len, |
| 508 | x509_write_time(&c, buf, ctx->not_before, |
| 509 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 510 | |
| 511 | len += sub_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 512 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len)); |
| 513 | MBEDTLS_ASN1_CHK_ADD(len, |
| 514 | mbedtls_asn1_write_tag(&c, buf, |
| 515 | MBEDTLS_ASN1_CONSTRUCTED | |
| 516 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * Issuer ::= Name |
| 520 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 521 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf, |
| 522 | ctx->issuer)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 523 | |
| 524 | /* |
| 525 | * Signature ::= AlgorithmIdentifier |
| 526 | */ |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 527 | if (pk_alg == MBEDTLS_PK_ECDSA) { |
| 528 | /* |
| 529 | * The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature |
| 530 | * algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and |
| 531 | * https://www.rfc-editor.org/rfc/rfc5758#section-3. |
| 532 | */ |
| 533 | write_sig_null_par = 0; |
| 534 | } else { |
| 535 | write_sig_null_par = 1; |
| 536 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | MBEDTLS_ASN1_CHK_ADD(len, |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 538 | mbedtls_asn1_write_algorithm_identifier_ext(&c, buf, |
| 539 | sig_oid, strlen(sig_oid), |
| 540 | 0, write_sig_null_par)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 541 | |
| 542 | /* |
| 543 | * Serial ::= INTEGER |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 544 | * |
| 545 | * Written data is: |
Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 546 | * - "ctx->serial_len" bytes for the raw serial buffer |
| 547 | * - if MSb of "serial" is 1, then prepend an extra 0x00 byte |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 548 | * - 1 byte for the length |
| 549 | * - 1 byte for the TAG |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 550 | */ |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 551 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, |
| 552 | ctx->serial, ctx->serial_len)); |
Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 553 | if (*c & 0x80) { |
| 554 | if (c - buf < 1) { |
| 555 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 556 | } |
Valerio Setti | 856cec4 | 2023-01-12 14:56:54 +0100 | [diff] [blame] | 557 | *(--c) = 0x0; |
Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 558 | len++; |
| 559 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, |
| 560 | ctx->serial_len + 1)); |
| 561 | } else { |
| 562 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, |
| 563 | ctx->serial_len)); |
| 564 | } |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 565 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, |
| 566 | MBEDTLS_ASN1_INTEGER)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 567 | |
| 568 | /* |
| 569 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 570 | */ |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 571 | |
| 572 | /* Can be omitted for v1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) { |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 574 | sub_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 575 | MBEDTLS_ASN1_CHK_ADD(sub_len, |
| 576 | mbedtls_asn1_write_int(&c, buf, ctx->version)); |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 577 | len += sub_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 578 | MBEDTLS_ASN1_CHK_ADD(len, |
| 579 | mbedtls_asn1_write_len(&c, buf, sub_len)); |
| 580 | MBEDTLS_ASN1_CHK_ADD(len, |
| 581 | mbedtls_asn1_write_tag(&c, buf, |
| 582 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 583 | MBEDTLS_ASN1_CONSTRUCTED | 0)); |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 584 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 585 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 586 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 587 | MBEDTLS_ASN1_CHK_ADD(len, |
| 588 | mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
| 589 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 590 | |
| 591 | /* |
| 592 | * Make signature |
| 593 | */ |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 594 | |
| 595 | /* Compute hash of CRT. */ |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 596 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 597 | psa_algorithm = mbedtls_md_psa_alg_from_type(ctx->md_alg); |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 598 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | status = psa_hash_compute(psa_algorithm, |
| 600 | c, |
| 601 | len, |
| 602 | hash, |
| 603 | sizeof(hash), |
| 604 | &hash_length); |
| 605 | if (status != PSA_SUCCESS) { |
| 606 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 607 | } |
| 608 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 609 | if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, |
| 610 | len, hash)) != 0) { |
| 611 | return ret; |
Andres Amaya Garcia | 8d8204f | 2017-06-28 11:07:30 +0100 | [diff] [blame] | 612 | } |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 613 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 614 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 615 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 616 | if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg, |
| 617 | hash, hash_length, sig, sizeof(sig), &sig_len, |
| 618 | f_rng, p_rng)) != 0) { |
| 619 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 620 | } |
| 621 | |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 622 | /* Move CRT to the front of the buffer to have space |
| 623 | * for the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 624 | memmove(buf, c, len); |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 625 | c = buf + len; |
| 626 | |
| 627 | /* Add signature at the end of the buffer, |
| 628 | * making sure that it doesn't underflow |
| 629 | * into the CRT buffer. */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 630 | c2 = buf + size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 631 | MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c, |
Marek Jansta | 8bde649 | 2022-11-07 12:38:38 +0100 | [diff] [blame] | 632 | sig_oid, sig_oid_len, |
| 633 | sig, sig_len, pk_alg)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 634 | |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 635 | /* |
| 636 | * Memory layout after this step: |
| 637 | * |
| 638 | * buf c=buf+len c2 buf+size |
| 639 | * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm] |
| 640 | */ |
Andres AG | 60dbc93 | 2016-09-02 15:23:48 +0100 | [diff] [blame] | 641 | |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 642 | /* Move raw CRT to just before the signature. */ |
| 643 | c = c2 - len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 644 | memmove(c, buf, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 645 | |
| 646 | len += sig_and_oid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 648 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, |
| 649 | MBEDTLS_ASN1_CONSTRUCTED | |
| 650 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n" |
| 656 | #define PEM_END_CRT "-----END CERTIFICATE-----\n" |
| 657 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 658 | #if defined(MBEDTLS_PEM_WRITE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 659 | int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt, |
| 660 | unsigned char *buf, size_t size, |
| 661 | int (*f_rng)(void *, unsigned char *, size_t), |
| 662 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 663 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 664 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 67d4259 | 2019-05-04 08:13:23 +0100 | [diff] [blame] | 665 | size_t olen; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 666 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 667 | if ((ret = mbedtls_x509write_crt_der(crt, buf, size, |
| 668 | f_rng, p_rng)) < 0) { |
| 669 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 670 | } |
| 671 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 672 | if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT, |
| 673 | buf + size - ret, ret, |
| 674 | buf, size, &olen)) != 0) { |
| 675 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 676 | } |
| 677 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 678 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 679 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 680 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 681 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 682 | #endif /* MBEDTLS_X509_CRT_WRITE_C */ |