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