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