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