| 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 | 
| Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | *  SPDX-License-Identifier: Apache-2.0 | 
|  | 6 | * | 
|  | 7 | *  Licensed under the Apache License, Version 2.0 (the "License"); you may | 
|  | 8 | *  not use this file except in compliance with the License. | 
|  | 9 | *  You may obtain a copy of the License at | 
|  | 10 | * | 
|  | 11 | *  http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 12 | * | 
|  | 13 | *  Unless required by applicable law or agreed to in writing, software | 
|  | 14 | *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | 
|  | 15 | *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 16 | *  See the License for the specific language governing permissions and | 
|  | 17 | *  limitations under the License. | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 18 | */ | 
|  | 19 | /* | 
|  | 20 | * References: | 
|  | 21 | * - certificates: RFC 5280, updated by RFC 6818 | 
|  | 22 | * - CSRs: PKCS#10 v1.7 aka RFC 2986 | 
|  | 23 | * - attributes: PKCS#9 v2.0 aka RFC 2985 | 
|  | 24 | */ | 
|  | 25 |  | 
| Gilles Peskine | db09ef6 | 2020-06-03 01:43:33 +0200 | [diff] [blame] | 26 | #include "common.h" | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 27 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_X509_CRT_WRITE_C) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 29 |  | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 30 | #include "mbedtls/x509_crt.h" | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 31 | #include "mbedtls/asn1write.h" | 
| Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 32 | #include "mbedtls/error.h" | 
|  | 33 | #include "mbedtls/oid.h" | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 34 | #include "mbedtls/platform.h" | 
| Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 35 | #include "mbedtls/platform_util.h" | 
| Manuel Pégourié-Gonnard | 2cd7514 | 2023-02-24 12:37:07 +0100 | [diff] [blame] | 36 | #include "mbedtls/md.h" | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 37 |  | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 38 | #include <string.h> | 
| Andrzej Kurek | 63a6a26 | 2023-04-27 08:25:41 -0400 | [diff] [blame] | 39 | #include <stdint.h> | 
| Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 40 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #if defined(MBEDTLS_PEM_WRITE_C) | 
| Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 42 | #include "mbedtls/pem.h" | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | #endif /* MBEDTLS_PEM_WRITE_C */ | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 44 |  | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 45 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 46 | #include "psa/crypto.h" | 
| Manuel Pégourié-Gonnard | 2be8c63 | 2023-06-07 13:07:21 +0200 | [diff] [blame] | 47 | #include "psa_util_internal.h" | 
| Manuel Pégourié-Gonnard | 02b10d8 | 2023-03-28 12:33:20 +0200 | [diff] [blame] | 48 | #include "md_psa.h" | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 49 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 50 |  | 
| Andrzej Kurek | 63a6a26 | 2023-04-27 08:25:41 -0400 | [diff] [blame] | 51 | #define CHECK_OVERFLOW_ADD(a, b) \ | 
|  | 52 | do                         \ | 
|  | 53 | {                           \ | 
|  | 54 | if (a > SIZE_MAX - (b)) \ | 
|  | 55 | { \ | 
|  | 56 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; \ | 
|  | 57 | }                            \ | 
|  | 58 | a += b; \ | 
|  | 59 | } while (0) | 
|  | 60 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 61 | void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 62 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 63 | memset(ctx, 0, sizeof(mbedtls_x509write_cert)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 64 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | ctx->version = MBEDTLS_X509_CRT_VERSION_3; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 68 | void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 69 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | mbedtls_asn1_free_named_data_list(&ctx->subject); | 
|  | 71 | mbedtls_asn1_free_named_data_list(&ctx->issuer); | 
|  | 72 | mbedtls_asn1_free_named_data_list(&ctx->extensions); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 73 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 74 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_cert)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 75 | } | 
|  | 76 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 77 | void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx, | 
|  | 78 | int version) | 
| Paul Bakker | 5191e92 | 2013-10-11 10:54:28 +0200 | [diff] [blame] | 79 | { | 
|  | 80 | ctx->version = version; | 
|  | 81 | } | 
|  | 82 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 83 | void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx, | 
|  | 84 | mbedtls_md_type_t md_alg) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 85 | { | 
|  | 86 | ctx->md_alg = md_alg; | 
|  | 87 | } | 
|  | 88 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx, | 
|  | 90 | mbedtls_pk_context *key) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 91 | { | 
|  | 92 | ctx->subject_key = key; | 
|  | 93 | } | 
|  | 94 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 95 | void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx, | 
|  | 96 | mbedtls_pk_context *key) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 97 | { | 
|  | 98 | ctx->issuer_key = key; | 
|  | 99 | } | 
|  | 100 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx, | 
|  | 102 | const char *subject_name) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 103 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | return mbedtls_x509_string_to_names(&ctx->subject, subject_name); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 107 | int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx, | 
|  | 108 | const char *issuer_name) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 109 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | return mbedtls_x509_string_to_names(&ctx->issuer, issuer_name); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 111 | } | 
|  | 112 |  | 
| Valerio Setti | 5d164c4 | 2023-01-09 12:19:40 +0100 | [diff] [blame] | 113 | #if defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_DEPRECATED_REMOVED) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx, | 
|  | 115 | const mbedtls_mpi *serial) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 116 | { | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 117 | int ret; | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 118 | size_t tmp_len; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 119 |  | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 120 | /* Ensure that the MPI value fits into the buffer */ | 
|  | 121 | tmp_len = mbedtls_mpi_size(serial); | 
|  | 122 | if (tmp_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) { | 
|  | 123 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | ctx->serial_len = tmp_len; | 
|  | 127 |  | 
| Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 128 | ret = mbedtls_mpi_write_binary(serial, ctx->serial, tmp_len); | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 129 | if (ret < 0) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 130 | return ret; | 
|  | 131 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 132 |  | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 133 | return 0; | 
|  | 134 | } | 
| Valerio Setti | 5d164c4 | 2023-01-09 12:19:40 +0100 | [diff] [blame] | 135 | #endif // MBEDTLS_BIGNUM_C && !MBEDTLS_DEPRECATED_REMOVED | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 136 |  | 
| Valerio Setti | af4815c | 2023-01-26 17:43:09 +0100 | [diff] [blame] | 137 | int mbedtls_x509write_crt_set_serial_raw(mbedtls_x509write_cert *ctx, | 
| Valerio Setti | 746def5 | 2023-01-10 11:46:34 +0100 | [diff] [blame] | 138 | unsigned char *serial, size_t serial_len) | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 139 | { | 
| Valerio Setti | 746def5 | 2023-01-10 11:46:34 +0100 | [diff] [blame] | 140 | if (serial_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) { | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 141 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; | 
|  | 142 | } | 
|  | 143 |  | 
| Valerio Setti | 746def5 | 2023-01-10 11:46:34 +0100 | [diff] [blame] | 144 | ctx->serial_len = serial_len; | 
|  | 145 | memcpy(ctx->serial, serial, serial_len); | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 146 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 147 | return 0; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx, | 
|  | 151 | const char *not_before, | 
|  | 152 | const char *not_after) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 153 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 || | 
|  | 155 | strlen(not_after)  != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) { | 
|  | 156 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 157 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 158 | strncpy(ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN); | 
|  | 159 | 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] | 160 | ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; | 
|  | 161 | ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 162 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 163 | return 0; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 164 | } | 
|  | 165 |  | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 166 | int mbedtls_x509write_crt_set_subject_alternative_name(mbedtls_x509write_cert *ctx, | 
|  | 167 | const mbedtls_x509_san_list *san_list) | 
|  | 168 | { | 
|  | 169 | int ret = 0; | 
|  | 170 | const mbedtls_x509_san_list *cur; | 
|  | 171 | unsigned char *buf; | 
|  | 172 | unsigned char *p; | 
|  | 173 | size_t len; | 
|  | 174 | size_t buflen = 0; | 
|  | 175 |  | 
|  | 176 | /* Determine the maximum size of the SubjectAltName list */ | 
|  | 177 | for (cur = san_list; cur != NULL; cur = cur->next) { | 
|  | 178 | /* Calculate size of the required buffer */ | 
|  | 179 | switch (cur->node.type) { | 
|  | 180 | case MBEDTLS_X509_SAN_DNS_NAME: | 
|  | 181 | case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: | 
|  | 182 | case MBEDTLS_X509_SAN_IP_ADDRESS: | 
| Andrzej Kurek | 908716f | 2023-04-25 04:31:26 -0400 | [diff] [blame] | 183 | case MBEDTLS_X509_SAN_RFC822_NAME: | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 184 | /* length of value for each name entry, | 
|  | 185 | * maximum 4 bytes for the length field, | 
|  | 186 | * 1 byte for the tag/type. | 
|  | 187 | */ | 
| Andrzej Kurek | 7c86974 | 2023-06-02 05:02:41 -0400 | [diff] [blame] | 188 | CHECK_OVERFLOW_ADD(buflen, cur->node.san.unstructured_name.len); | 
|  | 189 | CHECK_OVERFLOW_ADD(buflen, 4 + 1); | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 190 | break; | 
|  | 191 | case MBEDTLS_X509_SAN_DIRECTORY_NAME: | 
| Andrzej Kurek | c6215b0 | 2023-04-04 09:30:12 -0400 | [diff] [blame] | 192 | { | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 193 | const mbedtls_asn1_named_data *chunk = &cur->node.san.directory_name; | 
|  | 194 | while (chunk != NULL) { | 
| Andrzej Kurek | 7c86974 | 2023-06-02 05:02:41 -0400 | [diff] [blame] | 195 | // Max 4 bytes for length, +1 for tag, | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 196 | // additional 4 max for length, +1 for tag. | 
|  | 197 | // See x509_write_name for more information. | 
| Andrzej Kurek | 7c86974 | 2023-06-02 05:02:41 -0400 | [diff] [blame] | 198 | CHECK_OVERFLOW_ADD(buflen, 4 + 1 + 4 + 1); | 
|  | 199 | CHECK_OVERFLOW_ADD(buflen, chunk->oid.len); | 
|  | 200 | CHECK_OVERFLOW_ADD(buflen, chunk->val.len); | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 201 | chunk = chunk->next; | 
|  | 202 | } | 
| Andrzej Kurek | 7c86974 | 2023-06-02 05:02:41 -0400 | [diff] [blame] | 203 | CHECK_OVERFLOW_ADD(buflen, 4 + 1); | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 204 | break; | 
| Andrzej Kurek | c6215b0 | 2023-04-04 09:30:12 -0400 | [diff] [blame] | 205 | } | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 206 | default: | 
| Andrzej Kurek | dc22090 | 2023-04-25 02:29:00 -0400 | [diff] [blame] | 207 | /* Not supported - return. */ | 
|  | 208 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 209 | } | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | /* Add the extra length field and tag */ | 
| Andrzej Kurek | 63a6a26 | 2023-04-27 08:25:41 -0400 | [diff] [blame] | 213 | CHECK_OVERFLOW_ADD(buflen, 4 + 1); | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 214 |  | 
|  | 215 | /* Allocate buffer */ | 
|  | 216 | buf = mbedtls_calloc(1, buflen); | 
|  | 217 | if (buf == NULL) { | 
|  | 218 | return MBEDTLS_ERR_ASN1_ALLOC_FAILED; | 
|  | 219 | } | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 220 | p = buf + buflen; | 
|  | 221 |  | 
|  | 222 | /* Write ASN.1-based structure */ | 
|  | 223 | cur = san_list; | 
|  | 224 | len = 0; | 
|  | 225 | while (cur != NULL) { | 
|  | 226 | size_t single_san_len = 0; | 
|  | 227 | switch (cur->node.type) { | 
|  | 228 | case MBEDTLS_X509_SAN_DNS_NAME: | 
|  | 229 | case MBEDTLS_X509_SAN_RFC822_NAME: | 
|  | 230 | case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER: | 
|  | 231 | case MBEDTLS_X509_SAN_IP_ADDRESS: | 
|  | 232 | { | 
|  | 233 | const unsigned char *unstructured_name = | 
|  | 234 | (const unsigned char *) cur->node.san.unstructured_name.p; | 
|  | 235 | size_t unstructured_name_len = cur->node.san.unstructured_name.len; | 
|  | 236 |  | 
|  | 237 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, | 
|  | 238 | mbedtls_asn1_write_raw_buffer( | 
|  | 239 | &p, buf, | 
|  | 240 | unstructured_name, unstructured_name_len)); | 
|  | 241 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, mbedtls_asn1_write_len( | 
|  | 242 | &p, buf, unstructured_name_len)); | 
|  | 243 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, | 
|  | 244 | mbedtls_asn1_write_tag( | 
|  | 245 | &p, buf, | 
|  | 246 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type)); | 
|  | 247 | } | 
|  | 248 | break; | 
|  | 249 | case MBEDTLS_X509_SAN_DIRECTORY_NAME: | 
|  | 250 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, | 
|  | 251 | mbedtls_x509_write_names(&p, buf, | 
|  | 252 | (mbedtls_asn1_named_data *) & | 
|  | 253 | cur->node | 
|  | 254 | .san.directory_name)); | 
|  | 255 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, | 
|  | 256 | mbedtls_asn1_write_len(&p, buf, single_san_len)); | 
|  | 257 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(single_san_len, | 
|  | 258 | mbedtls_asn1_write_tag(&p, buf, | 
|  | 259 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | | 
|  | 260 | MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 261 | MBEDTLS_X509_SAN_DIRECTORY_NAME)); | 
|  | 262 | break; | 
|  | 263 | default: | 
| Andrzej Kurek | dc22090 | 2023-04-25 02:29:00 -0400 | [diff] [blame] | 264 | /* Error out on an unsupported SAN */ | 
|  | 265 | ret = MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; | 
|  | 266 | goto cleanup; | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 267 | } | 
|  | 268 | cur = cur->next; | 
| Andrzej Kurek | 63a6a26 | 2023-04-27 08:25:41 -0400 | [diff] [blame] | 269 | /* check for overflow */ | 
|  | 270 | if (len > SIZE_MAX - single_san_len) { | 
|  | 271 | ret = MBEDTLS_ERR_X509_BAD_INPUT_DATA; | 
|  | 272 | goto cleanup; | 
|  | 273 | } | 
| Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 274 | len += single_san_len; | 
|  | 275 | } | 
|  | 276 |  | 
|  | 277 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len)); | 
|  | 278 | MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, | 
|  | 279 | mbedtls_asn1_write_tag(&p, buf, | 
|  | 280 | MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 281 | MBEDTLS_ASN1_SEQUENCE)); | 
|  | 282 |  | 
|  | 283 | ret = mbedtls_x509write_crt_set_extension( | 
|  | 284 | ctx, | 
|  | 285 | MBEDTLS_OID_SUBJECT_ALT_NAME, | 
|  | 286 | MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME), | 
|  | 287 | 0, | 
|  | 288 | buf + buflen - len, | 
|  | 289 | len); | 
|  | 290 |  | 
|  | 291 | cleanup: | 
|  | 292 | mbedtls_free(buf); | 
|  | 293 | return ret; | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 297 | int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx, | 
|  | 298 | const char *oid, size_t oid_len, | 
|  | 299 | int critical, | 
|  | 300 | const unsigned char *val, size_t val_len) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 301 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len, | 
|  | 303 | critical, val, val_len); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 304 | } | 
|  | 305 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx, | 
|  | 307 | int is_ca, int max_pathlen) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 308 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 309 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 310 | unsigned char buf[9]; | 
|  | 311 | unsigned char *c = buf + sizeof(buf); | 
|  | 312 | size_t len = 0; | 
|  | 313 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | memset(buf, 0, sizeof(buf)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 315 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 316 | if (is_ca && max_pathlen > 127) { | 
|  | 317 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 318 | } | 
|  | 319 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 320 | if (is_ca) { | 
|  | 321 | if (max_pathlen >= 0) { | 
|  | 322 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, | 
|  | 323 | max_pathlen)); | 
|  | 324 | } | 
|  | 325 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1)); | 
|  | 326 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 327 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 328 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); | 
|  | 329 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, | 
|  | 330 | MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 331 | MBEDTLS_ASN1_SEQUENCE)); | 
|  | 332 |  | 
|  | 333 | return | 
|  | 334 | mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS, | 
|  | 335 | MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS), | 
|  | 336 | is_ca, buf + sizeof(buf) - len, len); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 337 | } | 
|  | 338 |  | 
| Manuel Pégourié-Gonnard | a946489 | 2023-03-17 12:08:50 +0100 | [diff] [blame] | 339 | #if defined(MBEDTLS_MD_CAN_SHA1) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx, | 
|  | 341 | int is_ca, | 
|  | 342 | unsigned char tag) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 343 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 344 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 346 | unsigned char *c = buf + sizeof(buf); | 
|  | 347 | size_t len = 0; | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 348 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 349 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 350 | size_t hash_length; | 
|  | 351 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 352 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 353 | memset(buf, 0, sizeof(buf)); | 
|  | 354 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 355 | mbedtls_pk_write_pubkey(&c, | 
|  | 356 | buf, | 
|  | 357 | is_ca ? | 
|  | 358 | ctx->issuer_key : | 
|  | 359 | ctx->subject_key)); | 
| pespacek | 3015148 | 2022-02-17 15:18:47 +0100 | [diff] [blame] | 360 |  | 
| pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 361 |  | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 362 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 363 | status = psa_hash_compute(PSA_ALG_SHA_1, | 
|  | 364 | buf + sizeof(buf) - len, | 
|  | 365 | len, | 
|  | 366 | buf + sizeof(buf) - 20, | 
|  | 367 | 20, | 
|  | 368 | &hash_length); | 
|  | 369 | if (status != PSA_SUCCESS) { | 
|  | 370 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 371 | } | 
|  | 372 | #else | 
| Manuel Pégourié-Gonnard | 2cd7514 | 2023-02-24 12:37:07 +0100 | [diff] [blame] | 373 | ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), | 
|  | 374 | buf + sizeof(buf) - len, len, | 
|  | 375 | buf + sizeof(buf) - 20); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 376 | if (ret != 0) { | 
|  | 377 | return ret; | 
|  | 378 | } | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 379 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 380 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | c = buf + sizeof(buf) - 20; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 382 | len = 20; | 
|  | 383 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); | 
|  | 385 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag)); | 
| 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 | if (is_ca) { // writes AuthorityKeyIdentifier sequence | 
|  | 388 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); | 
|  | 389 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 390 | mbedtls_asn1_write_tag(&c, | 
|  | 391 | buf, | 
|  | 392 | MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 393 | MBEDTLS_ASN1_SEQUENCE)); | 
| pespacek | 3015148 | 2022-02-17 15:18:47 +0100 | [diff] [blame] | 394 | } | 
| pespacek | d924e55 | 2022-02-28 11:49:54 +0100 | [diff] [blame] | 395 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 396 | if (is_ca) { | 
|  | 397 | return mbedtls_x509write_crt_set_extension(ctx, | 
|  | 398 | MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER, | 
|  | 399 | MBEDTLS_OID_SIZE( | 
|  | 400 | MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER), | 
|  | 401 | 0, buf + sizeof(buf) - len, len); | 
|  | 402 | } else { | 
|  | 403 | return mbedtls_x509write_crt_set_extension(ctx, | 
|  | 404 | MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER, | 
|  | 405 | MBEDTLS_OID_SIZE( | 
|  | 406 | MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER), | 
|  | 407 | 0, buf + sizeof(buf) - len, len); | 
|  | 408 | } | 
| pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 409 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 410 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 411 | int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx) | 
| pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 412 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | return mbedtls_x509write_crt_set_key_identifier(ctx, | 
|  | 414 | 0, | 
|  | 415 | MBEDTLS_ASN1_OCTET_STRING); | 
| pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 416 | } | 
|  | 417 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx) | 
| pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 419 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 420 | return mbedtls_x509write_crt_set_key_identifier(ctx, | 
|  | 421 | 1, | 
|  | 422 | (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 423 | } | 
| Manuel Pégourié-Gonnard | a946489 | 2023-03-17 12:08:50 +0100 | [diff] [blame] | 424 | #endif /* MBEDTLS_MD_CAN_SHA1 */ | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 425 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx, | 
|  | 427 | unsigned int key_usage) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 428 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 429 | unsigned char buf[5] = { 0 }, ku[2] = { 0 }; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 430 | unsigned char *c; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 431 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 432 | const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE | | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 433 | MBEDTLS_X509_KU_NON_REPUDIATION   | | 
|  | 434 | MBEDTLS_X509_KU_KEY_ENCIPHERMENT  | | 
|  | 435 | MBEDTLS_X509_KU_DATA_ENCIPHERMENT | | 
|  | 436 | MBEDTLS_X509_KU_KEY_AGREEMENT     | | 
|  | 437 | MBEDTLS_X509_KU_KEY_CERT_SIGN     | | 
|  | 438 | MBEDTLS_X509_KU_CRL_SIGN          | | 
|  | 439 | MBEDTLS_X509_KU_ENCIPHER_ONLY     | | 
|  | 440 | MBEDTLS_X509_KU_DECIPHER_ONLY; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 441 |  | 
| Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 442 | /* Check that nothing other than the allowed flags is set */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 443 | if ((key_usage & ~allowed_bits) != 0) { | 
|  | 444 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; | 
|  | 445 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 446 |  | 
| Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 447 | c = buf + 5; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0); | 
|  | 449 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9); | 
| Manuel Pégourié-Gonnard | 1cd10ad | 2015-06-23 11:07:37 +0200 | [diff] [blame] | 450 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 451 | if (ret < 0) { | 
|  | 452 | return ret; | 
|  | 453 | } else if (ret < 3 || ret > 5) { | 
|  | 454 | return MBEDTLS_ERR_X509_INVALID_FORMAT; | 
|  | 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 | ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE, | 
|  | 458 | MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE), | 
|  | 459 | 1, c, (size_t) ret); | 
|  | 460 | if (ret != 0) { | 
|  | 461 | return ret; | 
|  | 462 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 463 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 464 | return 0; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 465 | } | 
|  | 466 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx, | 
|  | 468 | const mbedtls_asn1_sequence *exts) | 
| Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 469 | { | 
|  | 470 | unsigned char buf[256]; | 
|  | 471 | unsigned char *c = buf + sizeof(buf); | 
|  | 472 | int ret; | 
|  | 473 | size_t len = 0; | 
| Dave Rodgman | 5f3f0d0 | 2022-08-11 14:38:26 +0100 | [diff] [blame] | 474 | const mbedtls_asn1_sequence *last_ext = NULL; | 
| Dave Rodgman | e2b772d | 2022-08-11 16:04:13 +0100 | [diff] [blame] | 475 | const mbedtls_asn1_sequence *ext; | 
| Dave Rodgman | 5f3f0d0 | 2022-08-11 14:38:26 +0100 | [diff] [blame] | 476 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | memset(buf, 0, sizeof(buf)); | 
| Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 478 |  | 
| Nicholas Wilson | ca841d3 | 2015-11-13 14:22:36 +0000 | [diff] [blame] | 479 | /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 480 | if (exts == NULL) { | 
|  | 481 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; | 
|  | 482 | } | 
| Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 483 |  | 
| Nicholas Wilson | ca841d3 | 2015-11-13 14:22:36 +0000 | [diff] [blame] | 484 | /* 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] | 485 | while (last_ext != exts) { | 
|  | 486 | for (ext = exts; ext->next != last_ext; ext = ext->next) { | 
|  | 487 | } | 
|  | 488 | if (ext->buf.tag != MBEDTLS_ASN1_OID) { | 
|  | 489 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; | 
|  | 490 | } | 
|  | 491 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len)); | 
|  | 492 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len)); | 
|  | 493 | 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] | 494 | last_ext = ext; | 
| Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 495 | } | 
|  | 496 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 497 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); | 
|  | 498 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 499 | mbedtls_asn1_write_tag(&c, buf, | 
|  | 500 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); | 
| Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 501 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | return mbedtls_x509write_crt_set_extension(ctx, | 
|  | 503 | MBEDTLS_OID_EXTENDED_KEY_USAGE, | 
|  | 504 | MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE), | 
|  | 505 | 1, c, len); | 
| Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 506 | } | 
|  | 507 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 508 | int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx, | 
|  | 509 | unsigned char ns_cert_type) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 510 | { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 511 | unsigned char buf[4] = { 0 }; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 512 | unsigned char *c; | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 513 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 514 |  | 
|  | 515 | c = buf + 4; | 
|  | 516 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 517 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8); | 
|  | 518 | if (ret < 3 || ret > 4) { | 
|  | 519 | return ret; | 
|  | 520 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 521 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE, | 
|  | 523 | MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE), | 
|  | 524 | 0, c, (size_t) ret); | 
|  | 525 | if (ret != 0) { | 
|  | 526 | return ret; | 
|  | 527 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 528 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | return 0; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 530 | } | 
|  | 531 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 532 | static int x509_write_time(unsigned char **p, unsigned char *start, | 
|  | 533 | const char *t, size_t size) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 534 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 535 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 536 | size_t len = 0; | 
|  | 537 |  | 
|  | 538 | /* | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 539 | * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 540 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 541 | if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) { | 
|  | 542 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, | 
|  | 543 | (const unsigned char *) t + 2, | 
|  | 544 | size - 2)); | 
|  | 545 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); | 
|  | 546 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, | 
|  | 547 | MBEDTLS_ASN1_UTC_TIME)); | 
|  | 548 | } else { | 
|  | 549 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, | 
|  | 550 | (const unsigned char *) t, | 
|  | 551 | size)); | 
|  | 552 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); | 
|  | 553 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, | 
|  | 554 | MBEDTLS_ASN1_GENERALIZED_TIME)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 555 | } | 
|  | 556 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 557 | return (int) len; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 558 | } | 
|  | 559 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 560 | int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, | 
|  | 561 | unsigned char *buf, size_t size, | 
|  | 562 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 563 | void *p_rng) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 564 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 565 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 566 | const char *sig_oid; | 
|  | 567 | size_t sig_oid_len = 0; | 
|  | 568 | unsigned char *c, *c2; | 
| Gilles Peskine | bf88780 | 2019-11-08 19:21:51 +0100 | [diff] [blame] | 569 | unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 570 | size_t hash_length = 0; | 
| Manuel Pégourié-Gonnard | 8857984 | 2023-03-28 11:20:23 +0200 | [diff] [blame] | 571 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 572 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
|  | 573 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; | 
|  | 574 | psa_algorithm_t psa_algorithm; | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 575 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 576 |  | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 577 | size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len; | 
|  | 578 | size_t len = 0; | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 579 | mbedtls_pk_type_t pk_alg; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 580 |  | 
|  | 581 | /* | 
| Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 582 | * Prepare data to be signed at the end of the target buffer | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 583 | */ | 
| Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 584 | c = buf + size; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 585 |  | 
|  | 586 | /* Signature algorithm needed in TBS, and later for actual signature */ | 
| Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 587 |  | 
|  | 588 | /* There's no direct way of extracting a signature algorithm | 
|  | 589 | * (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] | 590 | if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) { | 
| Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 591 | pk_alg = MBEDTLS_PK_RSA; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 592 | } 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] | 593 | pk_alg = MBEDTLS_PK_ECDSA; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 594 | } else { | 
|  | 595 | return MBEDTLS_ERR_X509_INVALID_ALG; | 
|  | 596 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 597 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 598 | if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg, | 
|  | 599 | &sig_oid, &sig_oid_len)) != 0) { | 
|  | 600 | return ret; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 601 | } | 
|  | 602 |  | 
|  | 603 | /* | 
|  | 604 | *  Extensions  ::=  SEQUENCE SIZE (1..MAX) OF Extension | 
|  | 605 | */ | 
| Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 606 |  | 
|  | 607 | /* Only for v3 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 608 | if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) { | 
|  | 609 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 610 | mbedtls_x509_write_extensions(&c, | 
|  | 611 | buf, ctx->extensions)); | 
|  | 612 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); | 
|  | 613 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 614 | mbedtls_asn1_write_tag(&c, buf, | 
|  | 615 | MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 616 | MBEDTLS_ASN1_SEQUENCE)); | 
|  | 617 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); | 
|  | 618 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 619 | mbedtls_asn1_write_tag(&c, buf, | 
|  | 620 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | | 
|  | 621 | MBEDTLS_ASN1_CONSTRUCTED | 3)); | 
| Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 622 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 623 |  | 
|  | 624 | /* | 
|  | 625 | *  SubjectPublicKeyInfo | 
|  | 626 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 627 | MBEDTLS_ASN1_CHK_ADD(pub_len, | 
|  | 628 | mbedtls_pk_write_pubkey_der(ctx->subject_key, | 
|  | 629 | buf, c - buf)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 630 | c -= pub_len; | 
|  | 631 | len += pub_len; | 
|  | 632 |  | 
|  | 633 | /* | 
|  | 634 | *  Subject  ::=  Name | 
|  | 635 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 636 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 637 | mbedtls_x509_write_names(&c, buf, | 
|  | 638 | ctx->subject)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 639 |  | 
|  | 640 | /* | 
|  | 641 | *  Validity ::= SEQUENCE { | 
|  | 642 | *       notBefore      Time, | 
|  | 643 | *       notAfter       Time } | 
|  | 644 | */ | 
|  | 645 | sub_len = 0; | 
|  | 646 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 647 | MBEDTLS_ASN1_CHK_ADD(sub_len, | 
|  | 648 | x509_write_time(&c, buf, ctx->not_after, | 
|  | 649 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN)); | 
| 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 | MBEDTLS_ASN1_CHK_ADD(sub_len, | 
|  | 652 | x509_write_time(&c, buf, ctx->not_before, | 
|  | 653 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 654 |  | 
|  | 655 | len += sub_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len)); | 
|  | 657 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 658 | mbedtls_asn1_write_tag(&c, buf, | 
|  | 659 | MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 660 | MBEDTLS_ASN1_SEQUENCE)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 661 |  | 
|  | 662 | /* | 
|  | 663 | *  Issuer  ::=  Name | 
|  | 664 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 665 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf, | 
|  | 666 | ctx->issuer)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 667 |  | 
|  | 668 | /* | 
|  | 669 | *  Signature   ::=  AlgorithmIdentifier | 
|  | 670 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 671 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 672 | mbedtls_asn1_write_algorithm_identifier(&c, buf, | 
|  | 673 | sig_oid, strlen(sig_oid), 0)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 674 |  | 
|  | 675 | /* | 
|  | 676 | *  Serial   ::=  INTEGER | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 677 | * | 
|  | 678 | * Written data is: | 
| Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 679 | * - "ctx->serial_len" bytes for the raw serial buffer | 
|  | 680 | *   - if MSb of "serial" is 1, then prepend an extra 0x00 byte | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 681 | * - 1 byte for the length | 
|  | 682 | * - 1 byte for the TAG | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 683 | */ | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 684 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, | 
|  | 685 | ctx->serial, ctx->serial_len)); | 
| Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 686 | if (*c & 0x80) { | 
|  | 687 | if (c - buf < 1) { | 
|  | 688 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; | 
|  | 689 | } | 
| Valerio Setti | 856cec4 | 2023-01-12 14:56:54 +0100 | [diff] [blame] | 690 | *(--c) = 0x0; | 
| Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 691 | len++; | 
|  | 692 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, | 
|  | 693 | ctx->serial_len + 1)); | 
|  | 694 | } else { | 
|  | 695 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, | 
|  | 696 | ctx->serial_len)); | 
|  | 697 | } | 
| Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 698 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, | 
|  | 699 | MBEDTLS_ASN1_INTEGER)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 700 |  | 
|  | 701 | /* | 
|  | 702 | *  Version  ::=  INTEGER  {  v1(0), v2(1), v3(2)  } | 
|  | 703 | */ | 
| Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 704 |  | 
|  | 705 | /* Can be omitted for v1 */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 706 | if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) { | 
| Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 707 | sub_len = 0; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 708 | MBEDTLS_ASN1_CHK_ADD(sub_len, | 
|  | 709 | mbedtls_asn1_write_int(&c, buf, ctx->version)); | 
| Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 710 | len += sub_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 711 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 712 | mbedtls_asn1_write_len(&c, buf, sub_len)); | 
|  | 713 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 714 | mbedtls_asn1_write_tag(&c, buf, | 
|  | 715 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | | 
|  | 716 | MBEDTLS_ASN1_CONSTRUCTED | 0)); | 
| Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 717 | } | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 718 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 719 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); | 
|  | 720 | MBEDTLS_ASN1_CHK_ADD(len, | 
|  | 721 | mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 722 | MBEDTLS_ASN1_SEQUENCE)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 723 |  | 
|  | 724 | /* | 
|  | 725 | * Make signature | 
|  | 726 | */ | 
| Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 727 |  | 
|  | 728 | /* Compute hash of CRT. */ | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 729 | #if defined(MBEDTLS_USE_PSA_CRYPTO) | 
| Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 730 | psa_algorithm = mbedtls_md_psa_alg_from_type(ctx->md_alg); | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 731 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 732 | status = psa_hash_compute(psa_algorithm, | 
|  | 733 | c, | 
|  | 734 | len, | 
|  | 735 | hash, | 
|  | 736 | sizeof(hash), | 
|  | 737 | &hash_length); | 
|  | 738 | if (status != PSA_SUCCESS) { | 
|  | 739 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 740 | } | 
|  | 741 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 742 | if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, | 
|  | 743 | len, hash)) != 0) { | 
|  | 744 | return ret; | 
| Andres Amaya Garcia | 8d8204f | 2017-06-28 11:07:30 +0100 | [diff] [blame] | 745 | } | 
| pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 746 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ | 
|  | 747 |  | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 748 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 749 | if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg, | 
|  | 750 | hash, hash_length, sig, sizeof(sig), &sig_len, | 
|  | 751 | f_rng, p_rng)) != 0) { | 
|  | 752 | return ret; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 753 | } | 
|  | 754 |  | 
| Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 755 | /* Move CRT to the front of the buffer to have space | 
|  | 756 | * for the signature. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 757 | memmove(buf, c, len); | 
| Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 758 | c = buf + len; | 
|  | 759 |  | 
|  | 760 | /* Add signature at the end of the buffer, | 
|  | 761 | * making sure that it doesn't underflow | 
|  | 762 | * into the CRT buffer. */ | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 763 | c2 = buf + size; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 764 | MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c, | 
|  | 765 | sig_oid, sig_oid_len, sig, | 
|  | 766 | sig_len)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 767 |  | 
| Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 768 | /* | 
|  | 769 | * Memory layout after this step: | 
|  | 770 | * | 
|  | 771 | * buf       c=buf+len                c2            buf+size | 
|  | 772 | * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm] | 
|  | 773 | */ | 
| Andres AG | 60dbc93 | 2016-09-02 15:23:48 +0100 | [diff] [blame] | 774 |  | 
| Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 775 | /* Move raw CRT to just before the signature. */ | 
|  | 776 | c = c2 - len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 777 | memmove(c, buf, len); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 778 |  | 
|  | 779 | len += sig_and_oid_len; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 780 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); | 
|  | 781 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, | 
|  | 782 | MBEDTLS_ASN1_CONSTRUCTED | | 
|  | 783 | MBEDTLS_ASN1_SEQUENCE)); | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 784 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | return (int) len; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 786 | } | 
|  | 787 |  | 
|  | 788 | #define PEM_BEGIN_CRT           "-----BEGIN CERTIFICATE-----\n" | 
|  | 789 | #define PEM_END_CRT             "-----END CERTIFICATE-----\n" | 
|  | 790 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 791 | #if defined(MBEDTLS_PEM_WRITE_C) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 792 | int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt, | 
|  | 793 | unsigned char *buf, size_t size, | 
|  | 794 | int (*f_rng)(void *, unsigned char *, size_t), | 
|  | 795 | void *p_rng) | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 796 | { | 
| Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 797 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; | 
| Hanno Becker | 67d4259 | 2019-05-04 08:13:23 +0100 | [diff] [blame] | 798 | size_t olen; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 799 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 800 | if ((ret = mbedtls_x509write_crt_der(crt, buf, size, | 
|  | 801 | f_rng, p_rng)) < 0) { | 
|  | 802 | return ret; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 803 | } | 
|  | 804 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 805 | if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT, | 
|  | 806 | buf + size - ret, ret, | 
|  | 807 | buf, size, &olen)) != 0) { | 
|  | 808 | return ret; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 809 | } | 
|  | 810 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 811 | return 0; | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 812 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 813 | #endif /* MBEDTLS_PEM_WRITE_C */ | 
| Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 814 |  | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 815 | #endif /* MBEDTLS_X509_CRT_WRITE_C */ |