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