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