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