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" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 34 | #include "mbedtls/platform_util.h" |
Janos Follath | 73c616b | 2019-12-18 15:07:04 +0000 | [diff] [blame] | 35 | #include "mbedtls/sha1.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 36 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 37 | #include <string.h> |
| 38 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if defined(MBEDTLS_PEM_WRITE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 40 | #include "mbedtls/pem.h" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 41 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 42 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 43 | void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 44 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 45 | memset(ctx, 0, sizeof(mbedtls_x509write_cert)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 46 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 47 | mbedtls_mpi_init(&ctx->serial); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | ctx->version = MBEDTLS_X509_CRT_VERSION_3; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 51 | void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 52 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 53 | mbedtls_mpi_free(&ctx->serial); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 54 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 55 | mbedtls_asn1_free_named_data_list(&ctx->subject); |
| 56 | mbedtls_asn1_free_named_data_list(&ctx->issuer); |
| 57 | mbedtls_asn1_free_named_data_list(&ctx->extensions); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 58 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 59 | mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_cert)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 62 | void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx, |
| 63 | int version) |
Paul Bakker | 5191e92 | 2013-10-11 10:54:28 +0200 | [diff] [blame] | 64 | { |
| 65 | ctx->version = version; |
| 66 | } |
| 67 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 68 | void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx, |
| 69 | mbedtls_md_type_t md_alg) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 70 | { |
| 71 | ctx->md_alg = md_alg; |
| 72 | } |
| 73 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 74 | void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx, |
| 75 | mbedtls_pk_context *key) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 76 | { |
| 77 | ctx->subject_key = key; |
| 78 | } |
| 79 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 80 | void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx, |
| 81 | mbedtls_pk_context *key) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 82 | { |
| 83 | ctx->issuer_key = key; |
| 84 | } |
| 85 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 86 | int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx, |
| 87 | const char *subject_name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 88 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 89 | return mbedtls_x509_string_to_names(&ctx->subject, subject_name); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 90 | } |
| 91 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 92 | int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx, |
| 93 | const char *issuer_name) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 94 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 95 | return mbedtls_x509_string_to_names(&ctx->issuer, issuer_name); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 96 | } |
| 97 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 98 | int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx, |
| 99 | const mbedtls_mpi *serial) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 100 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 101 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 102 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 103 | if ((ret = mbedtls_mpi_copy(&ctx->serial, serial)) != 0) { |
| 104 | return ret; |
| 105 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 106 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 107 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 110 | int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx, |
| 111 | const char *not_before, |
| 112 | const char *not_after) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 113 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 114 | if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 || |
| 115 | strlen(not_after) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) { |
| 116 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 117 | } |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 118 | strncpy(ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN); |
| 119 | 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] | 120 | ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
| 121 | ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 122 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 123 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 124 | } |
| 125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 126 | int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx, |
| 127 | const char *oid, size_t oid_len, |
| 128 | int critical, |
| 129 | const unsigned char *val, size_t val_len) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 130 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 131 | return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len, |
| 132 | critical, val, val_len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 135 | int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx, |
| 136 | int is_ca, int max_pathlen) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 137 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 138 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 139 | unsigned char buf[9]; |
| 140 | unsigned char *c = buf + sizeof(buf); |
| 141 | size_t len = 0; |
| 142 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 143 | memset(buf, 0, sizeof(buf)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 144 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 145 | if (is_ca && max_pathlen > 127) { |
| 146 | return MBEDTLS_ERR_X509_BAD_INPUT_DATA; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 149 | if (is_ca) { |
| 150 | if (max_pathlen >= 0) { |
| 151 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, |
| 152 | max_pathlen)); |
| 153 | } |
| 154 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1)); |
| 155 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 156 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 157 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 158 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, |
| 159 | MBEDTLS_ASN1_CONSTRUCTED | |
| 160 | MBEDTLS_ASN1_SEQUENCE)); |
| 161 | |
| 162 | return |
| 163 | mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS, |
| 164 | MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS), |
| 165 | is_ca, buf + sizeof(buf) - len, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | #if defined(MBEDTLS_SHA1_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 169 | int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 170 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 171 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 173 | unsigned char *c = buf + sizeof(buf); |
| 174 | size_t len = 0; |
| 175 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 176 | memset(buf, 0, sizeof(buf)); |
| 177 | MBEDTLS_ASN1_CHK_ADD(len, |
| 178 | mbedtls_pk_write_pubkey(&c, buf, ctx->subject_key)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 179 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 180 | ret = mbedtls_sha1_ret(buf + sizeof(buf) - len, len, |
| 181 | buf + sizeof(buf) - 20); |
| 182 | if (ret != 0) { |
| 183 | return ret; |
| 184 | } |
| 185 | c = buf + sizeof(buf) - 20; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 186 | len = 20; |
| 187 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 188 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 189 | MBEDTLS_ASN1_CHK_ADD(len, |
| 190 | mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OCTET_STRING)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 191 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 192 | return mbedtls_x509write_crt_set_extension(ctx, |
| 193 | MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER, |
| 194 | MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER), |
| 195 | 0, buf + sizeof(buf) - len, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 198 | int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 199 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 200 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 202 | unsigned char *c = buf + sizeof(buf); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 203 | size_t len = 0; |
| 204 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 205 | memset(buf, 0, sizeof(buf)); |
| 206 | MBEDTLS_ASN1_CHK_ADD(len, |
| 207 | mbedtls_pk_write_pubkey(&c, buf, ctx->issuer_key)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 208 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 209 | ret = mbedtls_sha1_ret(buf + sizeof(buf) - len, len, |
| 210 | buf + sizeof(buf) - 20); |
| 211 | if (ret != 0) { |
| 212 | return ret; |
| 213 | } |
| 214 | c = buf + sizeof(buf) - 20; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 215 | len = 20; |
| 216 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 217 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 218 | MBEDTLS_ASN1_CHK_ADD(len, |
| 219 | mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 220 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 221 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 222 | MBEDTLS_ASN1_CHK_ADD(len, |
| 223 | mbedtls_asn1_write_tag(&c, buf, |
| 224 | MBEDTLS_ASN1_CONSTRUCTED | |
| 225 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 226 | |
Hanno Becker | 6ad3fd1 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 227 | return mbedtls_x509write_crt_set_extension( |
| 228 | ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER, |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 229 | MBEDTLS_OID_SIZE(MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER), |
| 230 | 0, buf + sizeof(buf) - len, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 231 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | #endif /* MBEDTLS_SHA1_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 233 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 234 | int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx, |
| 235 | unsigned int key_usage) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 236 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 237 | unsigned char buf[5] = { 0 }, ku[2] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 238 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 239 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 240 | const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 241 | MBEDTLS_X509_KU_NON_REPUDIATION | |
| 242 | MBEDTLS_X509_KU_KEY_ENCIPHERMENT | |
| 243 | MBEDTLS_X509_KU_DATA_ENCIPHERMENT | |
| 244 | MBEDTLS_X509_KU_KEY_AGREEMENT | |
| 245 | MBEDTLS_X509_KU_KEY_CERT_SIGN | |
| 246 | MBEDTLS_X509_KU_CRL_SIGN | |
| 247 | MBEDTLS_X509_KU_ENCIPHER_ONLY | |
| 248 | MBEDTLS_X509_KU_DECIPHER_ONLY; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 249 | |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 250 | /* Check that nothing other than the allowed flags is set */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 251 | if ((key_usage & ~allowed_bits) != 0) { |
| 252 | return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE; |
| 253 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 254 | |
Andres Amaya Garcia | 6e95914 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 255 | c = buf + 5; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 256 | MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0); |
| 257 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9); |
Manuel Pégourié-Gonnard | 1cd10ad | 2015-06-23 11:07:37 +0200 | [diff] [blame] | 258 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 259 | if (ret < 0) { |
| 260 | return ret; |
| 261 | } else if (ret < 3 || ret > 5) { |
| 262 | return MBEDTLS_ERR_X509_INVALID_FORMAT; |
| 263 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 264 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 265 | ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE, |
| 266 | MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE), |
| 267 | 1, c, (size_t) ret); |
| 268 | if (ret != 0) { |
| 269 | return ret; |
| 270 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 271 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 272 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 273 | } |
| 274 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 275 | int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx, |
| 276 | unsigned char ns_cert_type) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 277 | { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 278 | unsigned char buf[4] = { 0 }; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 279 | unsigned char *c; |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 280 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 281 | |
| 282 | c = buf + 4; |
| 283 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 284 | ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8); |
| 285 | if (ret < 3 || ret > 4) { |
| 286 | return ret; |
| 287 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 288 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 289 | ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE, |
| 290 | MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE), |
| 291 | 0, c, (size_t) ret); |
| 292 | if (ret != 0) { |
| 293 | return ret; |
| 294 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 295 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 296 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 299 | static int x509_write_time(unsigned char **p, unsigned char *start, |
| 300 | const char *t, size_t size) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 301 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 302 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 303 | size_t len = 0; |
| 304 | |
| 305 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 306 | * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 307 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 308 | if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) { |
| 309 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, |
| 310 | (const unsigned char *) t + 2, |
| 311 | size - 2)); |
| 312 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 313 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 314 | MBEDTLS_ASN1_UTC_TIME)); |
| 315 | } else { |
| 316 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start, |
| 317 | (const unsigned char *) t, |
| 318 | size)); |
| 319 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len)); |
| 320 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start, |
| 321 | MBEDTLS_ASN1_GENERALIZED_TIME)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 322 | } |
| 323 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 324 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 325 | } |
| 326 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 327 | int mbedtls_x509write_crt_der(mbedtls_x509write_cert *ctx, |
| 328 | unsigned char *buf, size_t size, |
| 329 | int (*f_rng)(void *, unsigned char *, size_t), |
| 330 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 331 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 332 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 333 | const char *sig_oid; |
| 334 | size_t sig_oid_len = 0; |
| 335 | unsigned char *c, *c2; |
| 336 | unsigned char hash[64]; |
Gilles Peskine | bf88780 | 2019-11-08 19:21:51 +0100 | [diff] [blame] | 337 | unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE]; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 338 | size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len; |
| 339 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 340 | mbedtls_pk_type_t pk_alg; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 341 | |
| 342 | /* |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 343 | * Prepare data to be signed at the end of the target buffer |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 344 | */ |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 345 | c = buf + size; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 346 | |
| 347 | /* Signature algorithm needed in TBS, and later for actual signature */ |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 348 | |
| 349 | /* There's no direct way of extracting a signature algorithm |
| 350 | * (represented as an element of mbedtls_pk_type_t) from a PK instance. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 351 | if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) { |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 352 | pk_alg = MBEDTLS_PK_RSA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 353 | } 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] | 354 | pk_alg = MBEDTLS_PK_ECDSA; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 355 | } else { |
| 356 | return MBEDTLS_ERR_X509_INVALID_ALG; |
| 357 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 358 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 359 | if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg, |
| 360 | &sig_oid, &sig_oid_len)) != 0) { |
| 361 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | /* |
| 365 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 366 | */ |
Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 367 | |
| 368 | /* Only for v3 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 369 | if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) { |
| 370 | MBEDTLS_ASN1_CHK_ADD(len, |
| 371 | mbedtls_x509_write_extensions(&c, |
| 372 | buf, ctx->extensions)); |
| 373 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 374 | MBEDTLS_ASN1_CHK_ADD(len, |
| 375 | mbedtls_asn1_write_tag(&c, buf, |
| 376 | MBEDTLS_ASN1_CONSTRUCTED | |
| 377 | MBEDTLS_ASN1_SEQUENCE)); |
| 378 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 379 | MBEDTLS_ASN1_CHK_ADD(len, |
| 380 | mbedtls_asn1_write_tag(&c, buf, |
| 381 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 382 | MBEDTLS_ASN1_CONSTRUCTED | 3)); |
Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 383 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 384 | |
| 385 | /* |
| 386 | * SubjectPublicKeyInfo |
| 387 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 388 | MBEDTLS_ASN1_CHK_ADD(pub_len, |
| 389 | mbedtls_pk_write_pubkey_der(ctx->subject_key, |
| 390 | buf, c - buf)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 391 | c -= pub_len; |
| 392 | len += pub_len; |
| 393 | |
| 394 | /* |
| 395 | * Subject ::= Name |
| 396 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 397 | MBEDTLS_ASN1_CHK_ADD(len, |
| 398 | mbedtls_x509_write_names(&c, buf, |
| 399 | ctx->subject)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 400 | |
| 401 | /* |
| 402 | * Validity ::= SEQUENCE { |
| 403 | * notBefore Time, |
| 404 | * notAfter Time } |
| 405 | */ |
| 406 | sub_len = 0; |
| 407 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 408 | MBEDTLS_ASN1_CHK_ADD(sub_len, |
| 409 | x509_write_time(&c, buf, ctx->not_after, |
| 410 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 411 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 412 | MBEDTLS_ASN1_CHK_ADD(sub_len, |
| 413 | x509_write_time(&c, buf, ctx->not_before, |
| 414 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 415 | |
| 416 | len += sub_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 417 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len)); |
| 418 | MBEDTLS_ASN1_CHK_ADD(len, |
| 419 | mbedtls_asn1_write_tag(&c, buf, |
| 420 | MBEDTLS_ASN1_CONSTRUCTED | |
| 421 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * Issuer ::= Name |
| 425 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 426 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf, |
| 427 | ctx->issuer)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * Signature ::= AlgorithmIdentifier |
| 431 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 432 | MBEDTLS_ASN1_CHK_ADD(len, |
| 433 | mbedtls_asn1_write_algorithm_identifier(&c, buf, |
| 434 | sig_oid, strlen(sig_oid), 0)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 435 | |
| 436 | /* |
| 437 | * Serial ::= INTEGER |
| 438 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 439 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&c, buf, |
| 440 | &ctx->serial)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 444 | */ |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 445 | |
| 446 | /* Can be omitted for v1 */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 447 | if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) { |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 448 | sub_len = 0; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 449 | MBEDTLS_ASN1_CHK_ADD(sub_len, |
| 450 | mbedtls_asn1_write_int(&c, buf, ctx->version)); |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 451 | len += sub_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 452 | MBEDTLS_ASN1_CHK_ADD(len, |
| 453 | mbedtls_asn1_write_len(&c, buf, sub_len)); |
| 454 | MBEDTLS_ASN1_CHK_ADD(len, |
| 455 | mbedtls_asn1_write_tag(&c, buf, |
| 456 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 457 | MBEDTLS_ASN1_CONSTRUCTED | 0)); |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 458 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 459 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 460 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 461 | MBEDTLS_ASN1_CHK_ADD(len, |
| 462 | mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
| 463 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 464 | |
| 465 | /* |
| 466 | * Make signature |
| 467 | */ |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 468 | |
| 469 | /* Compute hash of CRT. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 470 | if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, |
| 471 | len, hash)) != 0) { |
| 472 | return ret; |
Andres Amaya Garcia | 8d8204f | 2017-06-28 11:07:30 +0100 | [diff] [blame] | 473 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 474 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 475 | if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg, |
| 476 | hash, 0, sig, &sig_len, |
| 477 | f_rng, p_rng)) != 0) { |
| 478 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 479 | } |
| 480 | |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 481 | /* Move CRT to the front of the buffer to have space |
| 482 | * for the signature. */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 483 | memmove(buf, c, len); |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 484 | c = buf + len; |
| 485 | |
| 486 | /* Add signature at the end of the buffer, |
| 487 | * making sure that it doesn't underflow |
| 488 | * into the CRT buffer. */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 489 | c2 = buf + size; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 490 | MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c, |
| 491 | sig_oid, sig_oid_len, sig, |
| 492 | sig_len)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 493 | |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 494 | /* |
| 495 | * Memory layout after this step: |
| 496 | * |
| 497 | * buf c=buf+len c2 buf+size |
| 498 | * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm] |
| 499 | */ |
Andres AG | 60dbc93 | 2016-09-02 15:23:48 +0100 | [diff] [blame] | 500 | |
Hanno Becker | def4305 | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 501 | /* Move raw CRT to just before the signature. */ |
| 502 | c = c2 - len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 503 | memmove(c, buf, len); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 504 | |
| 505 | len += sig_and_oid_len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 506 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len)); |
| 507 | MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, |
| 508 | MBEDTLS_ASN1_CONSTRUCTED | |
| 509 | MBEDTLS_ASN1_SEQUENCE)); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 510 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 511 | return (int) len; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n" |
| 515 | #define PEM_END_CRT "-----END CERTIFICATE-----\n" |
| 516 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 517 | #if defined(MBEDTLS_PEM_WRITE_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 518 | int mbedtls_x509write_crt_pem(mbedtls_x509write_cert *crt, |
| 519 | unsigned char *buf, size_t size, |
| 520 | int (*f_rng)(void *, unsigned char *, size_t), |
| 521 | void *p_rng) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 522 | { |
Janos Follath | 865b3eb | 2019-12-16 11:46:15 +0000 | [diff] [blame] | 523 | int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED; |
Hanno Becker | 67d4259 | 2019-05-04 08:13:23 +0100 | [diff] [blame] | 524 | size_t olen; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 525 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 526 | if ((ret = mbedtls_x509write_crt_der(crt, buf, size, |
| 527 | f_rng, p_rng)) < 0) { |
| 528 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 529 | } |
| 530 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 531 | if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT, |
| 532 | buf + size - ret, ret, |
| 533 | buf, size, &olen)) != 0) { |
| 534 | return ret; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 535 | } |
| 536 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame^] | 537 | return 0; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 538 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 539 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 540 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 541 | #endif /* MBEDTLS_X509_CRT_WRITE_C */ |