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