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" |
| 47 | #include "mbedtls/psa_util.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 | |
Andrzej Kurek | 1c8ecbe | 2023-07-07 05:12:52 -0400 | [diff] [blame] | 291 | /* If we exceeded the allocated buffer it means that maximum size of the SubjectAltName list |
| 292 | * was incorrectly calculated and memory is corrupted. */ |
| 293 | if (p < buf) { |
| 294 | ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH; |
| 295 | } |
Andrzej Kurek | 67fdb33 | 2023-04-04 06:56:14 -0400 | [diff] [blame] | 296 | cleanup: |
| 297 | mbedtls_free(buf); |
| 298 | return ret; |
| 299 | } |
| 300 | |
| 301 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx, |
| 303 | const char *oid, size_t oid_len, |
| 304 | int critical, |
| 305 | const unsigned char *val, size_t val_len) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 306 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len, |
| 308 | critical, val, val_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 309 | } |
| 310 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 311 | int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx, |
| 312 | int is_ca, int max_pathlen) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 313 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 314 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 315 | unsigned char buf[9]; |
| 316 | unsigned char *c = buf + sizeof(buf); |
| 317 | size_t len = 0; |
| 318 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 319 | memset(buf, 0, sizeof(buf)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 320 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 321 | if (is_ca && max_pathlen > 127) { |
| 322 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 323 | } |
| 324 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 325 | if (is_ca) { |
| 326 | if (max_pathlen >= 0) { |
| 327 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, |
| 328 | max_pathlen)); |
| 329 | } |
| 330 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1)); |
| 331 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 332 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 333 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 334 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, |
| 335 | MBEDTLS_ASN1_CONSTRUCTED | |
| 336 | MBEDTLS_ASN1_SEQUENCE)); |
| 337 | |
| 338 | return |
| 339 | mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS, |
| 340 | MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS), |
| 341 | is_ca, buf + sizeof(buf) - len, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 342 | } |
| 343 | |
Manuel Pégourié-Gonnard | a946489 | 2023-03-17 12:08:50 +0100 | [diff] [blame] | 344 | #if defined(MBEDTLS_MD_CAN_SHA1) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx, |
| 346 | int is_ca, |
| 347 | unsigned char tag) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 348 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 349 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 350 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 351 | unsigned char *c = buf + sizeof(buf); |
| 352 | size_t len = 0; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 353 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 354 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 355 | size_t hash_length; |
| 356 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 357 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 358 | memset(buf, 0, sizeof(buf)); |
| 359 | MBEDTLS_ASN1_CHK_ADD(len, |
| 360 | mbedtls_pk_write_pubkey(&c, |
| 361 | buf, |
| 362 | is_ca ? |
| 363 | ctx->issuer_key : |
| 364 | ctx->subject_key)); |
pespacek | 3015148 | 2022-02-17 15:18:47 +0100 | [diff] [blame] | 365 | |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 366 | |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 367 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | status = psa_hash_compute(PSA_ALG_SHA_1, |
| 369 | buf + sizeof(buf) - len, |
| 370 | len, |
| 371 | buf + sizeof(buf) - 20, |
| 372 | 20, |
| 373 | &hash_length); |
| 374 | if (status != PSA_SUCCESS) { |
| 375 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 376 | } |
| 377 | #else |
Manuel Pégourié-Gonnard | 2cd7514 | 2023-02-24 12:37:07 +0100 | [diff] [blame] | 378 | ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), |
| 379 | buf + sizeof(buf) - len, len, |
| 380 | buf + sizeof(buf) - 20); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 381 | if (ret != 0) { |
| 382 | return ret; |
| 383 | } |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 384 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 385 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 386 | c = buf + sizeof(buf) - 20; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 387 | len = 20; |
| 388 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 389 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 390 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 391 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 392 | if (is_ca) { // writes AuthorityKeyIdentifier sequence |
| 393 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 394 | MBEDTLS_ASN1_CHK_ADD(len, |
| 395 | mbedtls_asn1_write_tag(&c, |
| 396 | buf, |
| 397 | MBEDTLS_ASN1_CONSTRUCTED | |
| 398 | MBEDTLS_ASN1_SEQUENCE)); |
pespacek | 3015148 | 2022-02-17 15:18:47 +0100 | [diff] [blame] | 399 | } |
pespacek | d924e55 | 2022-02-28 11:49:54 +0100 | [diff] [blame] | 400 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 401 | if (is_ca) { |
| 402 | return mbedtls_x509write_crt_set_extension(ctx, |
| 403 | MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER, |
| 404 | MBEDTLS_OID_SIZE( |
| 405 | MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER), |
| 406 | 0, buf + sizeof(buf) - len, len); |
| 407 | } else { |
| 408 | return mbedtls_x509write_crt_set_extension(ctx, |
| 409 | MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER, |
| 410 | MBEDTLS_OID_SIZE( |
| 411 | MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER), |
| 412 | 0, buf + sizeof(buf) - len, len); |
| 413 | } |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 414 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 415 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 416 | int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx) |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 417 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 418 | return mbedtls_x509write_crt_set_key_identifier(ctx, |
| 419 | 0, |
| 420 | MBEDTLS_ASN1_OCTET_STRING); |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 421 | } |
| 422 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 423 | int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx) |
pespacek | a6e955e | 2022-02-14 15:20:57 +0100 | [diff] [blame] | 424 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | return mbedtls_x509write_crt_set_key_identifier(ctx, |
| 426 | 1, |
| 427 | (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 428 | } |
Manuel Pégourié-Gonnard | a946489 | 2023-03-17 12:08:50 +0100 | [diff] [blame] | 429 | #endif /* MBEDTLS_MD_CAN_SHA1 */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx, |
| 432 | unsigned int key_usage) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 433 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 434 | unsigned char buf[5] = { 0 }, ku[2] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 435 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 436 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 437 | const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 438 | MBEDTLS_X509_KU_NON_REPUDIATION | |
| 439 | MBEDTLS_X509_KU_KEY_ENCIPHERMENT | |
| 440 | MBEDTLS_X509_KU_DATA_ENCIPHERMENT | |
| 441 | MBEDTLS_X509_KU_KEY_AGREEMENT | |
| 442 | MBEDTLS_X509_KU_KEY_CERT_SIGN | |
| 443 | MBEDTLS_X509_KU_CRL_SIGN | |
| 444 | MBEDTLS_X509_KU_ENCIPHER_ONLY | |
| 445 | MBEDTLS_X509_KU_DECIPHER_ONLY; |
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 | /* Check that nothing other than the allowed flags is set */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | if ((key_usage & ~allowed_bits) != 0) { |
| 449 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 450 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 451 | |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 452 | c = buf + 5; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0); |
| 454 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9); |
Manuel Pégourié-Gonnard | 1cd10ad | 2015-06-23 11:07:37 +0200 | [diff] [blame] | 455 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 456 | if (ret < 0) { |
| 457 | return ret; |
| 458 | } else if (ret < 3 || ret > 5) { |
| 459 | return MBEDTLS_ERR_X509_INVALID_FORMAT; |
| 460 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 461 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE, |
| 463 | MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE), |
| 464 | 1, c, (size_t) ret); |
| 465 | if (ret != 0) { |
| 466 | return ret; |
| 467 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 468 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 469 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 470 | } |
| 471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx, |
| 473 | const mbedtls_asn1_sequence *exts) |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 474 | { |
| 475 | unsigned char buf[256]; |
| 476 | unsigned char *c = buf + sizeof(buf); |
| 477 | int ret; |
| 478 | size_t len = 0; |
Dave Rodgman | 5f3f0d0 | 2022-08-11 14:38:26 +0100 | [diff] [blame] | 479 | const mbedtls_asn1_sequence *last_ext = NULL; |
Dave Rodgman | e2b772d | 2022-08-11 16:04:13 +0100 | [diff] [blame] | 480 | const mbedtls_asn1_sequence *ext; |
Dave Rodgman | 5f3f0d0 | 2022-08-11 14:38:26 +0100 | [diff] [blame] | 481 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 482 | memset(buf, 0, sizeof(buf)); |
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 | /* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 485 | if (exts == NULL) { |
| 486 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 487 | } |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 488 | |
Nicholas Wilson | ca841d3 | 2015-11-13 14:22:36 +0000 | [diff] [blame] | 489 | /* 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] | 490 | while (last_ext != exts) { |
| 491 | for (ext = exts; ext->next != last_ext; ext = ext->next) { |
| 492 | } |
| 493 | if (ext->buf.tag != MBEDTLS_ASN1_OID) { |
| 494 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
| 495 | } |
| 496 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len)); |
| 497 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len)); |
| 498 | 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] | 499 | last_ext = ext; |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 500 | } |
| 501 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 502 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 503 | MBEDTLS_ASN1_CHK_ADD(len, |
| 504 | mbedtls_asn1_write_tag(&c, buf, |
| 505 | MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 506 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 507 | return mbedtls_x509write_crt_set_extension(ctx, |
| 508 | MBEDTLS_OID_EXTENDED_KEY_USAGE, |
| 509 | MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE), |
| 510 | 1, c, len); |
Nicholas Wilson | 8e5bdfb | 2015-09-09 19:03:34 +0100 | [diff] [blame] | 511 | } |
| 512 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx, |
| 514 | unsigned char ns_cert_type) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 515 | { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 516 | unsigned char buf[4] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 517 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 518 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 519 | |
| 520 | c = buf + 4; |
| 521 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 522 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8); |
| 523 | if (ret < 3 || ret > 4) { |
| 524 | return ret; |
| 525 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 526 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE, |
| 528 | MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE), |
| 529 | 0, c, (size_t) ret); |
| 530 | if (ret != 0) { |
| 531 | return ret; |
| 532 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 533 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 537 | static int x509_write_time(unsigned char **p, unsigned char *start, |
| 538 | const char *t, size_t size) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 539 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 540 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 541 | size_t len = 0; |
| 542 | |
| 543 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 544 | * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 545 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 546 | if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) { |
| 547 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, |
| 548 | (const unsigned char *) t + 2, |
| 549 | size - 2)); |
| 550 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 551 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 552 | MBEDTLS_ASN1_UTC_TIME)); |
| 553 | } else { |
| 554 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, |
| 555 | (const unsigned char *) t, |
| 556 | size)); |
| 557 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 558 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 559 | MBEDTLS_ASN1_GENERALIZED_TIME)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 560 | } |
| 561 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 563 | } |
| 564 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 565 | int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, |
| 566 | unsigned char *buf, size_t size, |
| 567 | int (*f_rng)(void *, unsigned char *, size_t), |
| 568 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 569 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 570 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 571 | const char *sig_oid; |
| 572 | size_t sig_oid_len = 0; |
| 573 | unsigned char *c, *c2; |
Gilles Peskine | bf88780 | 2019-11-08 19:21:51 +0100 | [diff] [blame] | 574 | unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 575 | size_t hash_length = 0; |
Manuel Pégourié-Gonnard | 8857984 | 2023-03-28 11:20:23 +0200 | [diff] [blame] | 576 | unsigned char hash[MBEDTLS_MD_MAX_SIZE]; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 577 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
| 578 | psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED; |
| 579 | psa_algorithm_t psa_algorithm; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 580 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 581 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 582 | size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len; |
| 583 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 584 | mbedtls_pk_type_t pk_alg; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 585 | |
| 586 | /* |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 587 | * Prepare data to be signed at the end of the target buffer |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 588 | */ |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 589 | c = buf + size; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 590 | |
| 591 | /* Signature algorithm needed in TBS, and later for actual signature */ |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 592 | |
| 593 | /* There's no direct way of extracting a signature algorithm |
| 594 | * (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] | 595 | if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) { |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 596 | pk_alg = MBEDTLS_PK_RSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 597 | } 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] | 598 | pk_alg = MBEDTLS_PK_ECDSA; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 599 | } else { |
| 600 | return MBEDTLS_ERR_X509_INVALID_ALG; |
| 601 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 602 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 603 | if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg, |
| 604 | &sig_oid, &sig_oid_len)) != 0) { |
| 605 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* |
| 609 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 610 | */ |
Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 611 | |
| 612 | /* Only for v3 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 613 | if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) { |
| 614 | MBEDTLS_ASN1_CHK_ADD(len, |
| 615 | mbedtls_x509_write_extensions(&c, |
| 616 | buf, ctx->extensions)); |
| 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_CONSTRUCTED | |
| 621 | MBEDTLS_ASN1_SEQUENCE)); |
| 622 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 623 | MBEDTLS_ASN1_CHK_ADD(len, |
| 624 | mbedtls_asn1_write_tag(&c, buf, |
| 625 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 626 | MBEDTLS_ASN1_CONSTRUCTED | 3)); |
Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 627 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 628 | |
| 629 | /* |
| 630 | * SubjectPublicKeyInfo |
| 631 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 632 | MBEDTLS_ASN1_CHK_ADD(pub_len, |
| 633 | mbedtls_pk_write_pubkey_der(ctx->subject_key, |
| 634 | buf, c - buf)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 635 | c -= pub_len; |
| 636 | len += pub_len; |
| 637 | |
| 638 | /* |
| 639 | * Subject ::= Name |
| 640 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 641 | MBEDTLS_ASN1_CHK_ADD(len, |
| 642 | mbedtls_x509_write_names(&c, buf, |
| 643 | ctx->subject)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 644 | |
| 645 | /* |
| 646 | * Validity ::= SEQUENCE { |
| 647 | * notBefore Time, |
| 648 | * notAfter Time } |
| 649 | */ |
| 650 | sub_len = 0; |
| 651 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 652 | MBEDTLS_ASN1_CHK_ADD(sub_len, |
| 653 | x509_write_time(&c, buf, ctx->not_after, |
| 654 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 655 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 656 | MBEDTLS_ASN1_CHK_ADD(sub_len, |
| 657 | x509_write_time(&c, buf, ctx->not_before, |
| 658 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 659 | |
| 660 | len += sub_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 661 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len)); |
| 662 | MBEDTLS_ASN1_CHK_ADD(len, |
| 663 | mbedtls_asn1_write_tag(&c, buf, |
| 664 | MBEDTLS_ASN1_CONSTRUCTED | |
| 665 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 666 | |
| 667 | /* |
| 668 | * Issuer ::= Name |
| 669 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 670 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf, |
| 671 | ctx->issuer)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 672 | |
| 673 | /* |
| 674 | * Signature ::= AlgorithmIdentifier |
| 675 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 676 | MBEDTLS_ASN1_CHK_ADD(len, |
| 677 | mbedtls_asn1_write_algorithm_identifier(&c, buf, |
| 678 | sig_oid, strlen(sig_oid), 0)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 679 | |
| 680 | /* |
| 681 | * Serial ::= INTEGER |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 682 | * |
| 683 | * Written data is: |
Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 684 | * - "ctx->serial_len" bytes for the raw serial buffer |
| 685 | * - if MSb of "serial" is 1, then prepend an extra 0x00 byte |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 686 | * - 1 byte for the length |
| 687 | * - 1 byte for the TAG |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 688 | */ |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 689 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, |
| 690 | ctx->serial, ctx->serial_len)); |
Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 691 | if (*c & 0x80) { |
| 692 | if (c - buf < 1) { |
| 693 | return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL; |
| 694 | } |
Valerio Setti | 856cec4 | 2023-01-12 14:56:54 +0100 | [diff] [blame] | 695 | *(--c) = 0x0; |
Valerio Setti | 4752aac | 2023-01-10 16:00:15 +0100 | [diff] [blame] | 696 | len++; |
| 697 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, |
| 698 | ctx->serial_len + 1)); |
| 699 | } else { |
| 700 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, |
| 701 | ctx->serial_len)); |
| 702 | } |
Valerio Setti | da0afcc | 2023-01-05 16:46:59 +0100 | [diff] [blame] | 703 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, |
| 704 | MBEDTLS_ASN1_INTEGER)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 705 | |
| 706 | /* |
| 707 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 708 | */ |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 709 | |
| 710 | /* Can be omitted for v1 */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 711 | if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) { |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 712 | sub_len = 0; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 713 | MBEDTLS_ASN1_CHK_ADD(sub_len, |
| 714 | mbedtls_asn1_write_int(&c, buf, ctx->version)); |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 715 | len += sub_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 716 | MBEDTLS_ASN1_CHK_ADD(len, |
| 717 | mbedtls_asn1_write_len(&c, buf, sub_len)); |
| 718 | MBEDTLS_ASN1_CHK_ADD(len, |
| 719 | mbedtls_asn1_write_tag(&c, buf, |
| 720 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 721 | MBEDTLS_ASN1_CONSTRUCTED | 0)); |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 722 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 723 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 724 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 725 | MBEDTLS_ASN1_CHK_ADD(len, |
| 726 | mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
| 727 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 728 | |
| 729 | /* |
| 730 | * Make signature |
| 731 | */ |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 732 | |
| 733 | /* Compute hash of CRT. */ |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 734 | #if defined(MBEDTLS_USE_PSA_CRYPTO) |
Manuel Pégourié-Gonnard | 2d6d993 | 2023-03-28 11:38:08 +0200 | [diff] [blame] | 735 | psa_algorithm = mbedtls_md_psa_alg_from_type(ctx->md_alg); |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 736 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 737 | status = psa_hash_compute(psa_algorithm, |
| 738 | c, |
| 739 | len, |
| 740 | hash, |
| 741 | sizeof(hash), |
| 742 | &hash_length); |
| 743 | if (status != PSA_SUCCESS) { |
| 744 | return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED; |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 745 | } |
| 746 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 747 | if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, |
| 748 | len, hash)) != 0) { |
| 749 | return ret; |
Andres Amaya Garcia | 8d8204f | 2017-06-28 11:07:30 +0100 | [diff] [blame] | 750 | } |
pespacek | 7599a77 | 2022-02-07 14:40:55 +0100 | [diff] [blame] | 751 | #endif /* MBEDTLS_USE_PSA_CRYPTO */ |
| 752 | |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 753 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 754 | if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg, |
| 755 | hash, hash_length, sig, sizeof(sig), &sig_len, |
| 756 | f_rng, p_rng)) != 0) { |
| 757 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 758 | } |
| 759 | |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 760 | /* Move CRT to the front of the buffer to have space |
| 761 | * for the signature. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 762 | memmove(buf, c, len); |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 763 | c = buf + len; |
| 764 | |
| 765 | /* Add signature at the end of the buffer, |
| 766 | * making sure that it doesn't underflow |
| 767 | * into the CRT buffer. */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 768 | c2 = buf + size; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 769 | MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c, |
| 770 | sig_oid, sig_oid_len, sig, |
| 771 | sig_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 772 | |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 773 | /* |
| 774 | * Memory layout after this step: |
| 775 | * |
| 776 | * buf c=buf+len c2 buf+size |
| 777 | * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm] |
| 778 | */ |
Andres AG | 60dbc93 | 2016-09-02 15:23:48 +0100 | [diff] [blame] | 779 | |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 780 | /* Move raw CRT to just before the signature. */ |
| 781 | c = c2 - len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 782 | memmove(c, buf, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 783 | |
| 784 | len += sig_and_oid_len; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 785 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 786 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, |
| 787 | MBEDTLS_ASN1_CONSTRUCTED | |
| 788 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 789 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 790 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n" |
| 794 | #define PEM_END_CRT "-----END CERTIFICATE-----\n" |
| 795 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 796 | #if defined(MBEDTLS_PEM_WRITE_C) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 797 | int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt, |
| 798 | unsigned char *buf, size_t size, |
| 799 | int (*f_rng)(void *, unsigned char *, size_t), |
| 800 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 801 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 802 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 67d4259 | 2019-05-04 08:13:23 +0100 | [diff] [blame] | 803 | size_t olen; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 804 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 805 | if ((ret = mbedtls_x509write_crt_der(crt, buf, size, |
| 806 | f_rng, p_rng)) < 0) { |
| 807 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 808 | } |
| 809 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 810 | if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT, |
| 811 | buf + size - ret, ret, |
| 812 | buf, size, &olen)) != 0) { |
| 813 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 814 | } |
| 815 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 816 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 817 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 818 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 819 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 820 | #endif /* MBEDTLS_X509_CRT_WRITE_C */ |