blob: 6703474bdccfec79de59fc492a7074168063bd52 [file] [log] [blame]
Paul Bakker7c6b2c32013-09-16 13:49:26 +02001/*
2 * X.509 certificate writing
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * 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 Bakker7c6b2c32013-09-16 13:49:26 +020018 */
19/*
20 * References:
21 * - certificates: RFC 5280, updated by RFC 6818
22 * - CSRs: PKCS#10 v1.7 aka RFC 2986
23 * - attributes: PKCS#9 v2.0 aka RFC 2985
24 */
25
Gilles Peskinedb09ef62020-06-03 01:43:33 +020026#include "common.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_X509_CRT_WRITE_C)
Paul Bakker7c6b2c32013-09-16 13:49:26 +020029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/x509_crt.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/asn1write.h"
Janos Follath73c616b2019-12-18 15:07:04 +000032#include "mbedtls/error.h"
33#include "mbedtls/oid.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050034#include "mbedtls/platform_util.h"
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/sha1.h"
Paul Bakker7c6b2c32013-09-16 13:49:26 +020036
Rich Evans00ab4702015-02-06 13:43:58 +000037#include <string.h>
38
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if defined(MBEDTLS_PEM_WRITE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/pem.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +020042
pespacek7599a772022-02-07 14:40:55 +010043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "psa/crypto.h"
45#include "mbedtls/psa_util.h"
46#endif /* MBEDTLS_USE_PSA_CRYPTO */
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049{
Hanno Becker81535d02017-09-13 15:39:59 +010050 memset( ctx, 0, sizeof( mbedtls_x509write_cert ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_mpi_init( &ctx->serial );
53 ctx->version = MBEDTLS_X509_CRT_VERSION_3;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054}
55
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_mpi_free( &ctx->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 mbedtls_asn1_free_named_data_list( &ctx->subject );
61 mbedtls_asn1_free_named_data_list( &ctx->issuer );
62 mbedtls_asn1_free_named_data_list( &ctx->extensions );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020063
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050064 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_cert ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020065}
66
Hanno Becker6ad3fd12019-05-04 07:37:58 +010067void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx,
68 int version )
Paul Bakker5191e922013-10-11 10:54:28 +020069{
70 ctx->version = version;
71}
72
Hanno Becker6ad3fd12019-05-04 07:37:58 +010073void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx,
74 mbedtls_md_type_t md_alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020075{
76 ctx->md_alg = md_alg;
77}
78
Hanno Becker6ad3fd12019-05-04 07:37:58 +010079void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx,
80 mbedtls_pk_context *key )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020081{
82 ctx->subject_key = key;
83}
84
Hanno Becker6ad3fd12019-05-04 07:37:58 +010085void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx,
86 mbedtls_pk_context *key )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020087{
88 ctx->issuer_key = key;
89}
90
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Hanno Becker6ad3fd12019-05-04 07:37:58 +010092 const char *subject_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020095}
96
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Hanno Becker6ad3fd12019-05-04 07:37:58 +010098 const char *issuer_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020099{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200101}
102
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100103int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx,
104 const mbedtls_mpi *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200105{
Janos Follath865b3eb2019-12-16 11:46:15 +0000106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200109 return( ret );
110
111 return( 0 );
112}
113
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100114int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx,
115 const char *not_before,
116 const char *not_after )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
119 strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
124 strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
125 ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
126 ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127
128 return( 0 );
129}
130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200132 const char *oid, size_t oid_len,
133 int critical,
134 const unsigned char *val, size_t val_len )
135{
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100136 return( mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
137 critical, val, val_len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200138}
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100141 int is_ca, int max_pathlen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200142{
Janos Follath865b3eb2019-12-16 11:46:15 +0000143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200144 unsigned char buf[9];
145 unsigned char *c = buf + sizeof(buf);
146 size_t len = 0;
147
148 memset( buf, 0, sizeof(buf) );
149
150 if( is_ca && max_pathlen > 127 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200152
153 if( is_ca )
154 {
155 if( max_pathlen >= 0 )
156 {
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100157 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf,
158 max_pathlen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200159 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200161 }
162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100164 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
165 MBEDTLS_ASN1_CONSTRUCTED |
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100168 return(
169 mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
170 MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ),
Darren Krahne560be32020-09-21 17:40:50 -0700171 is_ca, buf + sizeof(buf) - len, len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200172}
173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174#if defined(MBEDTLS_SHA1_C)
175int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200176{
pespacek7599a772022-02-07 14:40:55 +0100177#if defined(MBEDTLS_USE_PSA_CRYPTO)
178 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
179 size_t hash_length;
180#endif /* MBEDTLS_USE_PSA_CRYPTO */
181
Janos Follath865b3eb2019-12-16 11:46:15 +0000182 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200184 unsigned char *c = buf + sizeof(buf);
185 size_t len = 0;
186
Paul Bakker66d5d072014-06-17 16:39:18 +0200187 memset( buf, 0, sizeof(buf) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100188 MBEDTLS_ASN1_CHK_ADD( len,
189 mbedtls_pk_write_pubkey( &c, buf, ctx->subject_key ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200190
pespacek7599a772022-02-07 14:40:55 +0100191#if defined(MBEDTLS_USE_PSA_CRYPTO)
192 status = psa_hash_compute( PSA_ALG_SHA_1,
193 buf + sizeof( buf ) - len,
194 len,
195 buf + sizeof( buf ) - 20 ,
196 PSA_HASH_LENGTH(PSA_ALG_SHA_1),
197 &hash_length );
198 if( status != PSA_SUCCESS )
199 {
200 return( MBEDTLS_ERR_ERROR_GENERIC_ERROR );
201 }
202#else
TRodziewicz26371e42021-06-08 16:45:41 +0200203 ret = mbedtls_sha1( buf + sizeof( buf ) - len, len,
pespacek7599a772022-02-07 14:40:55 +0100204 buf + sizeof( buf ) - 20 );
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100205 if( ret != 0 )
206 return( ret );
pespacek7599a772022-02-07 14:40:55 +0100207#endif /* MBEDTLS_USE_PSA_CRYPTO */
208
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100209 c = buf + sizeof( buf ) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200210 len = 20;
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100213 MBEDTLS_ASN1_CHK_ADD( len,
214 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200215
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100216 return mbedtls_x509write_crt_set_extension( ctx,
217 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
218 MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
219 0, buf + sizeof(buf) - len, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200220}
221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223{
pespacek7599a772022-02-07 14:40:55 +0100224#if defined(MBEDTLS_USE_PSA_CRYPTO)
225 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
226 size_t hash_length;
227#endif /* MBEDTLS_USE_PSA_CRYPTO */
228
Janos Follath865b3eb2019-12-16 11:46:15 +0000229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Hanno Becker81535d02017-09-13 15:39:59 +0100231 unsigned char *c = buf + sizeof( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232 size_t len = 0;
233
Paul Bakker66d5d072014-06-17 16:39:18 +0200234 memset( buf, 0, sizeof(buf) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100235 MBEDTLS_ASN1_CHK_ADD( len,
236 mbedtls_pk_write_pubkey( &c, buf, ctx->issuer_key ) );
pespacek7599a772022-02-07 14:40:55 +0100237#if defined(MBEDTLS_USE_PSA_CRYPTO)
238 status = psa_hash_compute( PSA_ALG_SHA_1,
pespacekb9f07a72022-02-14 15:13:26 +0100239 buf + sizeof(buf) - len,
pespacek7599a772022-02-07 14:40:55 +0100240 len,
pespacekb9f07a72022-02-14 15:13:26 +0100241 buf + sizeof(buf) - 20,
pespacek7599a772022-02-07 14:40:55 +0100242 PSA_HASH_LENGTH(PSA_ALG_SHA_1),
243 &hash_length );
244 if( status != PSA_SUCCESS )
245 {
pespacek3110c7b2022-02-14 15:07:41 +0100246 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
pespacek7599a772022-02-07 14:40:55 +0100247 }
248#else
TRodziewicz26371e42021-06-08 16:45:41 +0200249 ret = mbedtls_sha1( buf + sizeof( buf ) - len, len,
pespacek7599a772022-02-07 14:40:55 +0100250 buf + sizeof( buf ) - 20 );
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100251 if( ret != 0 )
252 return( ret );
pespacek7599a772022-02-07 14:40:55 +0100253#endif /* MBEDTLS_USE_PSA_CRYPTO */
254
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100255 c = buf + sizeof( buf ) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256 len = 20;
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100259 MBEDTLS_ASN1_CHK_ADD( len,
260 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100263 MBEDTLS_ASN1_CHK_ADD( len,
264 mbedtls_asn1_write_tag( &c, buf,
265 MBEDTLS_ASN1_CONSTRUCTED |
266 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200267
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100268 return mbedtls_x509write_crt_set_extension(
269 ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
270 MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
271 0, buf + sizeof( buf ) - len, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200272}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200273#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200275int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
276 unsigned int key_usage )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200277{
Shawn Careyaa13e932021-05-06 15:11:30 -0400278 unsigned char buf[5] = {0}, ku[2] = {0};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200279 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100281 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
282 MBEDTLS_X509_KU_NON_REPUDIATION |
283 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
284 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
285 MBEDTLS_X509_KU_KEY_AGREEMENT |
286 MBEDTLS_X509_KU_KEY_CERT_SIGN |
287 MBEDTLS_X509_KU_CRL_SIGN |
288 MBEDTLS_X509_KU_ENCIPHER_ONLY |
289 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200290
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100291 /* Check that nothing other than the allowed flags is set */
292 if( ( key_usage & ~allowed_bits ) != 0 )
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200293 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200294
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100295 c = buf + 5;
Joe Subbiani6dd73642021-07-19 11:56:54 +0100296 MBEDTLS_PUT_UINT16_LE( key_usage, ku, 0 );
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100297 ret = mbedtls_asn1_write_named_bitstring( &c, buf, ku, 9 );
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200298
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100299 if( ret < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 return( ret );
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100301 else if( ret < 3 || ret > 5 )
302 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100305 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
306 1, c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307 if( ret != 0 )
308 return( ret );
309
310 return( 0 );
311}
312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200314 unsigned char ns_cert_type )
315{
Shawn Careyaa13e932021-05-06 15:11:30 -0400316 unsigned char buf[4] = {0};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200317 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200319
320 c = buf + 4;
321
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100322 ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
323 if( ret < 3 || ret > 4 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200324 return( ret );
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100327 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
328 0, c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 if( ret != 0 )
330 return( ret );
331
332 return( 0 );
333}
334
335static int x509_write_time( unsigned char **p, unsigned char *start,
Hanno Becker61937d42017-04-26 15:01:23 +0100336 const char *t, size_t size )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200337{
Janos Follath865b3eb2019-12-16 11:46:15 +0000338 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339 size_t len = 0;
340
341 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343 */
Hanno Becker61937d42017-04-26 15:01:23 +0100344 if( t[0] == '2' && t[1] == '0' && t[2] < '5' )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Hanno Becker61937d42017-04-26 15:01:23 +0100347 (const unsigned char *) t + 2,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348 size - 2 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100350 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
351 MBEDTLS_ASN1_UTC_TIME ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200352 }
353 else
354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Hanno Becker61937d42017-04-26 15:01:23 +0100356 (const unsigned char *) t,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200357 size ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100359 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
360 MBEDTLS_ASN1_GENERALIZED_TIME ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200361 }
362
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200363 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200364}
365
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100366int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx,
367 unsigned char *buf, size_t size,
368 int (*f_rng)(void *, unsigned char *, size_t),
369 void *p_rng )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200370{
Janos Follath865b3eb2019-12-16 11:46:15 +0000371 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372 const char *sig_oid;
373 size_t sig_oid_len = 0;
374 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100375 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100376 size_t hash_length = 0;
377#if defined(MBEDTLS_USE_PSA_CRYPTO)
378 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
379 psa_algorithm_t psa_algorithm;
380 unsigned char hash[PSA_HASH_MAX_SIZE];
381#else
382 unsigned char hash[64];
383#endif /* MBEDTLS_USE_PSA_CRYPTO */
384
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200385 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
386 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_pk_type_t pk_alg;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388
389 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100390 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100392 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200393
394 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100395
396 /* There's no direct way of extracting a signature algorithm
397 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
398 if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_RSA ) )
399 pk_alg = MBEDTLS_PK_RSA;
400 else if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 pk_alg = MBEDTLS_PK_ECDSA;
Hanno Beckerfc771442017-09-13 08:45:48 +0100402 else
Hanno Beckerd8a6f7c2017-09-22 16:05:43 +0100403 return( MBEDTLS_ERR_X509_INVALID_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
Hanno Becker81535d02017-09-13 15:39:59 +0100406 &sig_oid, &sig_oid_len ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200407 {
408 return( ret );
409 }
410
411 /*
412 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
413 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100414
415 /* Only for v3 */
Hanno Beckera20e33a2017-09-22 15:40:01 +0100416 if( ctx->version == MBEDTLS_X509_CRT_VERSION_3 )
Hanno Beckerd7f35202017-09-13 12:00:15 +0100417 {
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100418 MBEDTLS_ASN1_CHK_ADD( len,
419 mbedtls_x509_write_extensions( &c,
Hanno Beckerdef43052019-05-04 07:54:36 +0100420 buf, ctx->extensions ) );
421 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100422 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100423 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100424 MBEDTLS_ASN1_CONSTRUCTED |
425 MBEDTLS_ASN1_SEQUENCE ) );
Hanno Beckerdef43052019-05-04 07:54:36 +0100426 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100427 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100428 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100429 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
430 MBEDTLS_ASN1_CONSTRUCTED | 3 ) );
Hanno Beckerd7f35202017-09-13 12:00:15 +0100431 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 /*
434 * SubjectPublicKeyInfo
435 */
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100436 MBEDTLS_ASN1_CHK_ADD( pub_len,
437 mbedtls_pk_write_pubkey_der( ctx->subject_key,
Hanno Beckerdef43052019-05-04 07:54:36 +0100438 buf, c - buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200439 c -= pub_len;
440 len += pub_len;
441
442 /*
443 * Subject ::= Name
444 */
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100445 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100446 mbedtls_x509_write_names( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100447 ctx->subject ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200448
449 /*
450 * Validity ::= SEQUENCE {
451 * notBefore Time,
452 * notAfter Time }
453 */
454 sub_len = 0;
455
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100456 MBEDTLS_ASN1_CHK_ADD( sub_len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100457 x509_write_time( &c, buf, ctx->not_after,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100458 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100460 MBEDTLS_ASN1_CHK_ADD( sub_len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100461 x509_write_time( &c, buf, ctx->not_before,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100462 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200463
464 len += sub_len;
Hanno Beckerdef43052019-05-04 07:54:36 +0100465 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, sub_len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100466 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100467 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100468 MBEDTLS_ASN1_CONSTRUCTED |
469 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200470
471 /*
472 * Issuer ::= Name
473 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100474 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100475 ctx->issuer ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200476
477 /*
478 * Signature ::= AlgorithmIdentifier
479 */
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100480 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100481 mbedtls_asn1_write_algorithm_identifier( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100482 sig_oid, strlen( sig_oid ), 0 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200483
484 /*
485 * Serial ::= INTEGER
486 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100487 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100488 &ctx->serial ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200489
490 /*
491 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
492 */
Hanno Becker47698652017-09-13 11:59:26 +0100493
494 /* Can be omitted for v1 */
Hanno Beckera20e33a2017-09-22 15:40:01 +0100495 if( ctx->version != MBEDTLS_X509_CRT_VERSION_1 )
Hanno Becker47698652017-09-13 11:59:26 +0100496 {
497 sub_len = 0;
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100498 MBEDTLS_ASN1_CHK_ADD( sub_len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100499 mbedtls_asn1_write_int( &c, buf, ctx->version ) );
Hanno Becker47698652017-09-13 11:59:26 +0100500 len += sub_len;
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100501 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100502 mbedtls_asn1_write_len( &c, buf, sub_len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100503 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100504 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100505 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
506 MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Hanno Becker47698652017-09-13 11:59:26 +0100507 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200508
Hanno Beckerdef43052019-05-04 07:54:36 +0100509 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100510 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100511 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100512 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513
514 /*
515 * Make signature
516 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100517
518 /* Compute hash of CRT. */
pespacek7599a772022-02-07 14:40:55 +0100519#if defined(MBEDTLS_USE_PSA_CRYPTO)
520 psa_algorithm = mbedtls_psa_translate_md( ctx->md_alg );
521
522 status = psa_hash_compute( psa_algorithm,
523 c,
524 len,
525 hash,
pespacekb9f07a72022-02-14 15:13:26 +0100526 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +0100527 &hash_length );
528 if( status != PSA_SUCCESS )
529 {
pespacek3110c7b2022-02-14 15:07:41 +0100530 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
pespacek7599a772022-02-07 14:40:55 +0100531 }
532#else
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100533 if( ( ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c,
534 len, hash ) ) != 0 )
535 {
536 return( ret );
537 }
pespacek7599a772022-02-07 14:40:55 +0100538#endif /* MBEDTLS_USE_PSA_CRYPTO */
539
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100541 if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg,
pespacek7599a772022-02-07 14:40:55 +0100542 hash, hash_length, sig, sizeof( sig ), &sig_len,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100543 f_rng, p_rng ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200544 {
545 return( ret );
546 }
547
Hanno Beckerdef43052019-05-04 07:54:36 +0100548 /* Move CRT to the front of the buffer to have space
549 * for the signature. */
550 memmove( buf, c, len );
551 c = buf + len;
552
553 /* Add signature at the end of the buffer,
554 * making sure that it doesn't underflow
555 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200556 c2 = buf + size;
Hanno Beckerdef43052019-05-04 07:54:36 +0100557 MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, c,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200558 sig_oid, sig_oid_len, sig, sig_len ) );
559
Hanno Beckerdef43052019-05-04 07:54:36 +0100560 /*
561 * Memory layout after this step:
562 *
563 * buf c=buf+len c2 buf+size
564 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
565 */
Andres AG60dbc932016-09-02 15:23:48 +0100566
Hanno Beckerdef43052019-05-04 07:54:36 +0100567 /* Move raw CRT to just before the signature. */
568 c = c2 - len;
569 memmove( c, buf, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200570
571 len += sig_and_oid_len;
Hanno Beckerdef43052019-05-04 07:54:36 +0100572 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
573 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100574 MBEDTLS_ASN1_CONSTRUCTED |
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200576
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200577 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200578}
579
580#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
581#define PEM_END_CRT "-----END CERTIFICATE-----\n"
582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#if defined(MBEDTLS_PEM_WRITE_C)
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100584int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt,
585 unsigned char *buf, size_t size,
586 int (*f_rng)(void *, unsigned char *, size_t),
587 void *p_rng )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200588{
Janos Follath865b3eb2019-12-16 11:46:15 +0000589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100590 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200591
Hanno Becker67d42592019-05-04 08:13:23 +0100592 if( ( ret = mbedtls_x509write_crt_der( crt, buf, size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200593 f_rng, p_rng ) ) < 0 )
594 {
595 return( ret );
596 }
597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
Hanno Becker67d42592019-05-04 08:13:23 +0100599 buf + size - ret, ret,
600 buf, size, &olen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200601 {
602 return( ret );
603 }
604
605 return( 0 );
606}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200609#endif /* MBEDTLS_X509_CRT_WRITE_C */