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