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