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