Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * X.509 certificate writing |
| 3 | * |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 45 | */ |
| 46 | /* |
| 47 | * References: |
| 48 | * - certificates: RFC 5280, updated by RFC 6818 |
| 49 | * - CSRs: PKCS#10 v1.7 aka RFC 2986 |
| 50 | * - attributes: PKCS#9 v2.0 aka RFC 2985 |
| 51 | */ |
| 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 55 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 57 | #endif |
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 | #if defined(MBEDTLS_X509_CRT_WRITE_C) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 60 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 61 | #include "mbedtls/x509_crt.h" |
| 62 | #include "mbedtls/oid.h" |
| 63 | #include "mbedtls/asn1write.h" |
| 64 | #include "mbedtls/sha1.h" |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 65 | #include "mbedtls/platform_util.h" |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 66 | |
Rich Evans | 00ab470 | 2015-02-06 13:43:58 +0000 | [diff] [blame] | 67 | #include <string.h> |
| 68 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 69 | #if defined(MBEDTLS_PEM_WRITE_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 70 | #include "mbedtls/pem.h" |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 72 | |
k-stachowiak | e79c939 | 2019-05-31 20:11:26 +0200 | [diff] [blame] | 73 | /* |
| 74 | * For the currently used signature algorithms the buffer to store any signature |
| 75 | * must be at least of size MAX(MBEDTLS_ECDSA_MAX_LEN, MBEDTLS_MPI_MAX_SIZE) |
| 76 | */ |
| 77 | #if MBEDTLS_ECDSA_MAX_LEN > MBEDTLS_MPI_MAX_SIZE |
| 78 | #define SIGNATURE_MAX_SIZE MBEDTLS_ECDSA_MAX_LEN |
| 79 | #else |
| 80 | #define SIGNATURE_MAX_SIZE MBEDTLS_MPI_MAX_SIZE |
| 81 | #endif |
| 82 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 84 | { |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 85 | memset( ctx, 0, sizeof( mbedtls_x509write_cert ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 86 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 87 | mbedtls_mpi_init( &ctx->serial ); |
| 88 | ctx->version = MBEDTLS_X509_CRT_VERSION_3; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 89 | } |
| 90 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 91 | void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 92 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 93 | mbedtls_mpi_free( &ctx->serial ); |
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 | mbedtls_asn1_free_named_data_list( &ctx->subject ); |
| 96 | mbedtls_asn1_free_named_data_list( &ctx->issuer ); |
| 97 | mbedtls_asn1_free_named_data_list( &ctx->extensions ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 98 | |
Andres Amaya Garcia | 1f6301b | 2018-04-17 09:51:09 -0500 | [diff] [blame] | 99 | mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_cert ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 100 | } |
| 101 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 102 | void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx, |
| 103 | int version ) |
Paul Bakker | 5191e92 | 2013-10-11 10:54:28 +0200 | [diff] [blame] | 104 | { |
| 105 | ctx->version = version; |
| 106 | } |
| 107 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 108 | void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx, |
| 109 | mbedtls_md_type_t md_alg ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 110 | { |
| 111 | ctx->md_alg = md_alg; |
| 112 | } |
| 113 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 114 | void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx, |
| 115 | mbedtls_pk_context *key ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 116 | { |
| 117 | ctx->subject_key = key; |
| 118 | } |
| 119 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 120 | void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx, |
| 121 | mbedtls_pk_context *key ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 122 | { |
| 123 | ctx->issuer_key = key; |
| 124 | } |
| 125 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 127 | const char *subject_name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 128 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 129 | return mbedtls_x509_string_to_names( &ctx->subject, subject_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 132 | int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 133 | const char *issuer_name ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 134 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 136 | } |
| 137 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 138 | int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx, |
| 139 | const mbedtls_mpi *serial ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 140 | { |
| 141 | int ret; |
| 142 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 143 | if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 144 | return( ret ); |
| 145 | |
| 146 | return( 0 ); |
| 147 | } |
| 148 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 149 | int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx, |
| 150 | const char *not_before, |
| 151 | const char *not_after ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 152 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 || |
| 154 | strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 155 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 157 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN ); |
| 159 | strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN ); |
| 160 | ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
| 161 | ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z'; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 162 | |
| 163 | return( 0 ); |
| 164 | } |
| 165 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 167 | const char *oid, size_t oid_len, |
| 168 | int critical, |
| 169 | const unsigned char *val, size_t val_len ) |
| 170 | { |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 171 | return( mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len, |
| 172 | critical, val, val_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 | int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 176 | int is_ca, int max_pathlen ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 177 | { |
| 178 | int ret; |
| 179 | unsigned char buf[9]; |
| 180 | unsigned char *c = buf + sizeof(buf); |
| 181 | size_t len = 0; |
| 182 | |
| 183 | memset( buf, 0, sizeof(buf) ); |
| 184 | |
| 185 | if( is_ca && max_pathlen > 127 ) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | return( MBEDTLS_ERR_X509_BAD_INPUT_DATA ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 187 | |
| 188 | if( is_ca ) |
| 189 | { |
| 190 | if( max_pathlen >= 0 ) |
| 191 | { |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 192 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf, |
| 193 | max_pathlen ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 194 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) ); |
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 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 199 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, |
| 200 | MBEDTLS_ASN1_CONSTRUCTED | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 202 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 203 | return( |
| 204 | mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS, |
| 205 | MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ), |
Darren Krahn | ef7730c | 2020-09-21 17:40:50 -0700 | [diff] [blame] | 206 | is_ca, buf + sizeof(buf) - len, len ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 207 | } |
| 208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | #if defined(MBEDTLS_SHA1_C) |
| 210 | int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 211 | { |
| 212 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 213 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 214 | unsigned char *c = buf + sizeof(buf); |
| 215 | size_t len = 0; |
| 216 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 217 | memset( buf, 0, sizeof(buf) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 218 | MBEDTLS_ASN1_CHK_ADD( len, |
| 219 | mbedtls_pk_write_pubkey( &c, buf, ctx->subject_key ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 220 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 221 | ret = mbedtls_sha1_ret( buf + sizeof( buf ) - len, len, |
Andres Amaya Garcia | 8d8204f | 2017-06-28 11:07:30 +0100 | [diff] [blame] | 222 | buf + sizeof( buf ) - 20 ); |
| 223 | if( ret != 0 ) |
| 224 | return( ret ); |
| 225 | c = buf + sizeof( buf ) - 20; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 226 | len = 20; |
| 227 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 228 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 229 | MBEDTLS_ASN1_CHK_ADD( len, |
| 230 | mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OCTET_STRING ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 231 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 232 | return mbedtls_x509write_crt_set_extension( ctx, |
| 233 | MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER, |
| 234 | MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ), |
| 235 | 0, buf + sizeof(buf) - len, len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 239 | { |
| 240 | int ret; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */ |
Hanno Becker | 81535d0 | 2017-09-13 15:39:59 +0100 | [diff] [blame] | 242 | unsigned char *c = buf + sizeof( buf ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 243 | size_t len = 0; |
| 244 | |
Paul Bakker | 66d5d07 | 2014-06-17 16:39:18 +0200 | [diff] [blame] | 245 | memset( buf, 0, sizeof(buf) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 246 | MBEDTLS_ASN1_CHK_ADD( len, |
| 247 | mbedtls_pk_write_pubkey( &c, buf, ctx->issuer_key ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 248 | |
Gilles Peskine | 9e4f77c | 2018-01-22 11:48:08 +0100 | [diff] [blame] | 249 | ret = mbedtls_sha1_ret( buf + sizeof( buf ) - len, len, |
Andres Amaya Garcia | 8d8204f | 2017-06-28 11:07:30 +0100 | [diff] [blame] | 250 | buf + sizeof( buf ) - 20 ); |
| 251 | if( ret != 0 ) |
| 252 | return( ret ); |
| 253 | c = buf + sizeof( buf ) - 20; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 254 | len = 20; |
| 255 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 256 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 257 | MBEDTLS_ASN1_CHK_ADD( len, |
| 258 | mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0 ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 259 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 261 | MBEDTLS_ASN1_CHK_ADD( len, |
| 262 | mbedtls_asn1_write_tag( &c, buf, |
| 263 | MBEDTLS_ASN1_CONSTRUCTED | |
| 264 | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 265 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 266 | return mbedtls_x509write_crt_set_extension( |
| 267 | ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER, |
| 268 | MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ), |
| 269 | 0, buf + sizeof( buf ) - len, len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 270 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 271 | #endif /* MBEDTLS_SHA1_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 272 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 273 | static size_t crt_get_unused_bits_for_named_bitstring( unsigned char bitstring, |
| 274 | size_t bit_offset ) |
| 275 | { |
| 276 | size_t unused_bits; |
| 277 | |
| 278 | /* Count the unused bits removing trailing 0s */ |
| 279 | for( unused_bits = bit_offset; unused_bits < 8; unused_bits++ ) |
| 280 | if( ( ( bitstring >> unused_bits ) & 0x1 ) != 0 ) |
| 281 | break; |
| 282 | |
| 283 | return( unused_bits ); |
| 284 | } |
| 285 | |
Manuel Pégourié-Gonnard | 1cd10ad | 2015-06-23 11:07:37 +0200 | [diff] [blame] | 286 | int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx, |
| 287 | unsigned int key_usage ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 288 | { |
Manuel Pégourié-Gonnard | 1cd10ad | 2015-06-23 11:07:37 +0200 | [diff] [blame] | 289 | unsigned char buf[4], ku; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 290 | unsigned char *c; |
| 291 | int ret; |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 292 | size_t unused_bits; |
| 293 | const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE | |
| 294 | MBEDTLS_X509_KU_NON_REPUDIATION | |
| 295 | MBEDTLS_X509_KU_KEY_ENCIPHERMENT | |
| 296 | MBEDTLS_X509_KU_DATA_ENCIPHERMENT | |
| 297 | MBEDTLS_X509_KU_KEY_AGREEMENT | |
| 298 | MBEDTLS_X509_KU_KEY_CERT_SIGN | |
| 299 | MBEDTLS_X509_KU_CRL_SIGN; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 300 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 301 | /* Check that nothing other than the allowed flags is set */ |
| 302 | if( ( key_usage & ~allowed_bits ) != 0 ) |
Manuel Pégourié-Gonnard | 1cd10ad | 2015-06-23 11:07:37 +0200 | [diff] [blame] | 303 | return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 304 | |
Manuel Pégourié-Gonnard | 1cd10ad | 2015-06-23 11:07:37 +0200 | [diff] [blame] | 305 | c = buf + 4; |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 306 | ku = (unsigned char)key_usage; |
| 307 | unused_bits = crt_get_unused_bits_for_named_bitstring( ku, 1 ); |
| 308 | ret = mbedtls_asn1_write_bitstring( &c, buf, &ku, 8 - unused_bits ); |
Manuel Pégourié-Gonnard | 1cd10ad | 2015-06-23 11:07:37 +0200 | [diff] [blame] | 309 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 310 | if( ret < 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 311 | return( ret ); |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 312 | else if( ret < 3 || ret > 4 ) |
| 313 | return( MBEDTLS_ERR_X509_INVALID_FORMAT ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 314 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 315 | ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 316 | MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ), |
| 317 | 1, c, (size_t)ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 318 | if( ret != 0 ) |
| 319 | return( ret ); |
| 320 | |
| 321 | return( 0 ); |
| 322 | } |
| 323 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 324 | int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 325 | unsigned char ns_cert_type ) |
| 326 | { |
| 327 | unsigned char buf[4]; |
| 328 | unsigned char *c; |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 329 | size_t unused_bits; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 330 | int ret; |
| 331 | |
| 332 | c = buf + 4; |
| 333 | |
Andres Amaya Garcia | d60e378 | 2018-09-26 10:48:24 +0100 | [diff] [blame] | 334 | unused_bits = crt_get_unused_bits_for_named_bitstring( ns_cert_type, 0 ); |
| 335 | ret = mbedtls_asn1_write_bitstring( &c, |
| 336 | buf, |
| 337 | &ns_cert_type, |
| 338 | 8 - unused_bits ); |
| 339 | if( ret < 3 || ret > 4 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 340 | return( ret ); |
| 341 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 342 | ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 343 | MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ), |
| 344 | 0, c, (size_t)ret ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 345 | if( ret != 0 ) |
| 346 | return( ret ); |
| 347 | |
| 348 | return( 0 ); |
| 349 | } |
| 350 | |
| 351 | static int x509_write_time( unsigned char **p, unsigned char *start, |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 352 | const char *t, size_t size ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 353 | { |
| 354 | int ret; |
| 355 | size_t len = 0; |
| 356 | |
| 357 | /* |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 359 | */ |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 360 | if( t[0] == '2' && t[1] == '0' && t[2] < '5' ) |
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 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 363 | (const unsigned char *) t + 2, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 364 | size - 2 ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 365 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 366 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 367 | MBEDTLS_ASN1_UTC_TIME ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 368 | } |
| 369 | else |
| 370 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 371 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start, |
Hanno Becker | 61937d4 | 2017-04-26 15:01:23 +0100 | [diff] [blame] | 372 | (const unsigned char *) t, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 373 | size ) ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 374 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 375 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start, |
| 376 | MBEDTLS_ASN1_GENERALIZED_TIME ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 377 | } |
| 378 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 379 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 380 | } |
| 381 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 382 | int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx, |
| 383 | unsigned char *buf, size_t size, |
| 384 | int (*f_rng)(void *, unsigned char *, size_t), |
| 385 | void *p_rng ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 386 | { |
| 387 | int ret; |
| 388 | const char *sig_oid; |
| 389 | size_t sig_oid_len = 0; |
| 390 | unsigned char *c, *c2; |
| 391 | unsigned char hash[64]; |
k-stachowiak | e79c939 | 2019-05-31 20:11:26 +0200 | [diff] [blame] | 392 | unsigned char sig[SIGNATURE_MAX_SIZE]; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 393 | size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len; |
| 394 | size_t len = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 395 | mbedtls_pk_type_t pk_alg; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 396 | |
| 397 | /* |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 398 | * Prepare data to be signed at the end of the target buffer |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 399 | */ |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 400 | c = buf + size; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 401 | |
| 402 | /* Signature algorithm needed in TBS, and later for actual signature */ |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 403 | |
| 404 | /* There's no direct way of extracting a signature algorithm |
| 405 | * (represented as an element of mbedtls_pk_type_t) from a PK instance. */ |
| 406 | if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_RSA ) ) |
| 407 | pk_alg = MBEDTLS_PK_RSA; |
| 408 | 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] | 409 | pk_alg = MBEDTLS_PK_ECDSA; |
Hanno Becker | fc77144 | 2017-09-13 08:45:48 +0100 | [diff] [blame] | 410 | else |
Hanno Becker | d8a6f7c | 2017-09-22 16:05:43 +0100 | [diff] [blame] | 411 | return( MBEDTLS_ERR_X509_INVALID_ALG ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 412 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 413 | 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] | 414 | &sig_oid, &sig_oid_len ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 415 | { |
| 416 | return( ret ); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension |
| 421 | */ |
Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 422 | |
| 423 | /* Only for v3 */ |
Hanno Becker | a20e33a | 2017-09-22 15:40:01 +0100 | [diff] [blame] | 424 | if( ctx->version == MBEDTLS_X509_CRT_VERSION_3 ) |
Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 425 | { |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 426 | MBEDTLS_ASN1_CHK_ADD( len, |
| 427 | mbedtls_x509_write_extensions( &c, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 428 | buf, ctx->extensions ) ); |
| 429 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 430 | MBEDTLS_ASN1_CHK_ADD( len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 431 | mbedtls_asn1_write_tag( &c, buf, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 432 | MBEDTLS_ASN1_CONSTRUCTED | |
| 433 | MBEDTLS_ASN1_SEQUENCE ) ); |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 434 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 435 | MBEDTLS_ASN1_CHK_ADD( len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 436 | mbedtls_asn1_write_tag( &c, buf, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 437 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 438 | MBEDTLS_ASN1_CONSTRUCTED | 3 ) ); |
Hanno Becker | d7f3520 | 2017-09-13 12:00:15 +0100 | [diff] [blame] | 439 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 440 | |
| 441 | /* |
| 442 | * SubjectPublicKeyInfo |
| 443 | */ |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 444 | MBEDTLS_ASN1_CHK_ADD( pub_len, |
| 445 | mbedtls_pk_write_pubkey_der( ctx->subject_key, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 446 | buf, c - buf ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 447 | c -= pub_len; |
| 448 | len += pub_len; |
| 449 | |
| 450 | /* |
| 451 | * Subject ::= Name |
| 452 | */ |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 453 | MBEDTLS_ASN1_CHK_ADD( len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 454 | mbedtls_x509_write_names( &c, buf, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 455 | ctx->subject ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 456 | |
| 457 | /* |
| 458 | * Validity ::= SEQUENCE { |
| 459 | * notBefore Time, |
| 460 | * notAfter Time } |
| 461 | */ |
| 462 | sub_len = 0; |
| 463 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 464 | MBEDTLS_ASN1_CHK_ADD( sub_len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 465 | x509_write_time( &c, buf, ctx->not_after, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 466 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 467 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 468 | MBEDTLS_ASN1_CHK_ADD( sub_len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 469 | x509_write_time( &c, buf, ctx->not_before, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 470 | MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 471 | |
| 472 | len += sub_len; |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 473 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, sub_len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 474 | MBEDTLS_ASN1_CHK_ADD( len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 475 | mbedtls_asn1_write_tag( &c, buf, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 476 | MBEDTLS_ASN1_CONSTRUCTED | |
| 477 | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * Issuer ::= Name |
| 481 | */ |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 482 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 483 | ctx->issuer ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 484 | |
| 485 | /* |
| 486 | * Signature ::= AlgorithmIdentifier |
| 487 | */ |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 488 | MBEDTLS_ASN1_CHK_ADD( len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 489 | mbedtls_asn1_write_algorithm_identifier( &c, buf, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 490 | sig_oid, strlen( sig_oid ), 0 ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 491 | |
| 492 | /* |
| 493 | * Serial ::= INTEGER |
| 494 | */ |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 495 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 496 | &ctx->serial ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 497 | |
| 498 | /* |
| 499 | * Version ::= INTEGER { v1(0), v2(1), v3(2) } |
| 500 | */ |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 501 | |
| 502 | /* Can be omitted for v1 */ |
Hanno Becker | a20e33a | 2017-09-22 15:40:01 +0100 | [diff] [blame] | 503 | if( ctx->version != MBEDTLS_X509_CRT_VERSION_1 ) |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 504 | { |
| 505 | sub_len = 0; |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 506 | MBEDTLS_ASN1_CHK_ADD( sub_len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 507 | mbedtls_asn1_write_int( &c, buf, ctx->version ) ); |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 508 | len += sub_len; |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 509 | MBEDTLS_ASN1_CHK_ADD( len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 510 | mbedtls_asn1_write_len( &c, buf, sub_len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 511 | MBEDTLS_ASN1_CHK_ADD( len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 512 | mbedtls_asn1_write_tag( &c, buf, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 513 | MBEDTLS_ASN1_CONTEXT_SPECIFIC | |
| 514 | MBEDTLS_ASN1_CONSTRUCTED | 0 ) ); |
Hanno Becker | 4769865 | 2017-09-13 11:59:26 +0100 | [diff] [blame] | 515 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 516 | |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 517 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 518 | MBEDTLS_ASN1_CHK_ADD( len, |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 519 | mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 520 | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 521 | |
| 522 | /* |
| 523 | * Make signature |
| 524 | */ |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 525 | |
| 526 | /* Compute hash of CRT. */ |
Andres Amaya Garcia | 8d8204f | 2017-06-28 11:07:30 +0100 | [diff] [blame] | 527 | if( ( ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c, |
| 528 | len, hash ) ) != 0 ) |
| 529 | { |
| 530 | return( ret ); |
| 531 | } |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 532 | |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 533 | if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg, |
| 534 | hash, 0, sig, &sig_len, |
| 535 | f_rng, p_rng ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 536 | { |
| 537 | return( ret ); |
| 538 | } |
| 539 | |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 540 | /* Move CRT to the front of the buffer to have space |
| 541 | * for the signature. */ |
| 542 | memmove( buf, c, len ); |
| 543 | c = buf + len; |
| 544 | |
| 545 | /* Add signature at the end of the buffer, |
| 546 | * making sure that it doesn't underflow |
| 547 | * into the CRT buffer. */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 548 | c2 = buf + size; |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 549 | 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] | 550 | sig_oid, sig_oid_len, sig, sig_len ) ); |
| 551 | |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 552 | /* |
| 553 | * Memory layout after this step: |
| 554 | * |
| 555 | * buf c=buf+len c2 buf+size |
| 556 | * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm] |
| 557 | */ |
Andres AG | 60dbc93 | 2016-09-02 15:23:48 +0100 | [diff] [blame] | 558 | |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 559 | /* Move raw CRT to just before the signature. */ |
| 560 | c = c2 - len; |
| 561 | memmove( c, buf, len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 562 | |
| 563 | len += sig_and_oid_len; |
Hanno Becker | eeea9ea | 2019-05-04 07:54:36 +0100 | [diff] [blame] | 564 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) ); |
| 565 | MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 566 | MBEDTLS_ASN1_CONSTRUCTED | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 567 | MBEDTLS_ASN1_SEQUENCE ) ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 568 | |
Paul Bakker | b9cfaa0 | 2013-10-11 18:58:55 +0200 | [diff] [blame] | 569 | return( (int) len ); |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | #define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n" |
| 573 | #define PEM_END_CRT "-----END CERTIFICATE-----\n" |
| 574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | #if defined(MBEDTLS_PEM_WRITE_C) |
Hanno Becker | beaf396 | 2019-05-04 07:37:58 +0100 | [diff] [blame] | 576 | int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt, |
| 577 | unsigned char *buf, size_t size, |
| 578 | int (*f_rng)(void *, unsigned char *, size_t), |
| 579 | void *p_rng ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 580 | { |
| 581 | int ret; |
Hanno Becker | c33e921 | 2019-05-04 08:13:23 +0100 | [diff] [blame] | 582 | size_t olen; |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 583 | |
Hanno Becker | c33e921 | 2019-05-04 08:13:23 +0100 | [diff] [blame] | 584 | if( ( ret = mbedtls_x509write_crt_der( crt, buf, size, |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 585 | f_rng, p_rng ) ) < 0 ) |
| 586 | { |
| 587 | return( ret ); |
| 588 | } |
| 589 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 590 | if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT, |
Hanno Becker | c33e921 | 2019-05-04 08:13:23 +0100 | [diff] [blame] | 591 | buf + size - ret, ret, |
| 592 | buf, size, &olen ) ) != 0 ) |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 593 | { |
| 594 | return( ret ); |
| 595 | } |
| 596 | |
| 597 | return( 0 ); |
| 598 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 599 | #endif /* MBEDTLS_PEM_WRITE_C */ |
Paul Bakker | 7c6b2c3 | 2013-09-16 13:49:26 +0200 | [diff] [blame] | 600 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 601 | #endif /* MBEDTLS_X509_CRT_WRITE_C */ |