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