blob: 07f5851bb4726b998f240b9ead233183be5cf67a [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)
pespaceka6e955e2022-02-14 15:20:57 +0100175static int mbedtls_x509write_crt_set_key_identifier( mbedtls_x509write_cert
176*ctx,
177 int is_ca,
178 unsigned char tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200179{
Janos Follath865b3eb2019-12-16 11:46:15 +0000180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200182 unsigned char *c = buf + sizeof(buf);
183 size_t len = 0;
pespacek7599a772022-02-07 14:40:55 +0100184#if defined(MBEDTLS_USE_PSA_CRYPTO)
185 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
186 size_t hash_length;
187#endif /* MBEDTLS_USE_PSA_CRYPTO */
188
Paul Bakker66d5d072014-06-17 16:39:18 +0200189 memset( buf, 0, sizeof(buf) );
pespacek30151482022-02-17 15:18:47 +0100190 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c,
191 buf,
192 is_ca ?
193 ctx->issuer_key :
194 ctx->subject_key ) );
195
pespaceka6e955e2022-02-14 15:20:57 +0100196
pespacek7599a772022-02-07 14:40:55 +0100197#if defined(MBEDTLS_USE_PSA_CRYPTO)
198 status = psa_hash_compute( PSA_ALG_SHA_1,
pespacekb9f07a72022-02-14 15:13:26 +0100199 buf + sizeof(buf) - len,
pespacek7599a772022-02-07 14:40:55 +0100200 len,
pespacekb9f07a72022-02-14 15:13:26 +0100201 buf + sizeof(buf) - 20,
pespacek30151482022-02-17 15:18:47 +0100202 20,
pespacek7599a772022-02-07 14:40:55 +0100203 &hash_length );
204 if( status != PSA_SUCCESS )
205 {
pespacek3110c7b2022-02-14 15:07:41 +0100206 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
pespacek7599a772022-02-07 14:40:55 +0100207 }
208#else
TRodziewicz26371e42021-06-08 16:45:41 +0200209 ret = mbedtls_sha1( buf + sizeof( buf ) - len, len,
pespacek7599a772022-02-07 14:40:55 +0100210 buf + sizeof( buf ) - 20 );
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100211 if( ret != 0 )
212 return( ret );
pespacek7599a772022-02-07 14:40:55 +0100213#endif /* MBEDTLS_USE_PSA_CRYPTO */
214
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100215 c = buf + sizeof( buf ) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200216 len = 20;
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100219 MBEDTLS_ASN1_CHK_ADD( len,
pespaceka6e955e2022-02-14 15:20:57 +0100220 mbedtls_asn1_write_tag( &c, buf, tag ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200221
pespaceka6e955e2022-02-14 15:20:57 +0100222 if( is_ca ) // writes AuthorityKeyIdentifier sequence
223 {
224 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ));
225 MBEDTLS_ASN1_CHK_ADD( len,
226 mbedtls_asn1_write_tag( &c,
227 buf,
228 MBEDTLS_ASN1_CONSTRUCTED |
229 MBEDTLS_ASN1_SEQUENCE ) );
pespacek30151482022-02-17 15:18:47 +0100230 }
231 return mbedtls_x509write_crt_set_extension(
pespaceka6e955e2022-02-14 15:20:57 +0100232 ctx,
pespacek30151482022-02-17 15:18:47 +0100233 is_ca ? MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER :
234 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
235 is_ca ? MBEDTLS_OID_SIZE(
236 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ) :
237 MBEDTLS_OID_SIZE(
238 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
pespaceka6e955e2022-02-14 15:20:57 +0100239 0,
240 buf + sizeof( buf ) - len,
241 len );
pespaceka6e955e2022-02-14 15:20:57 +0100242}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200243
pespaceka6e955e2022-02-14 15:20:57 +0100244int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
245{
246 return mbedtls_x509write_crt_set_key_identifier( ctx,
247 0,
248 MBEDTLS_ASN1_OCTET_STRING );
249}
250
251int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
252{
253 return mbedtls_x509write_crt_set_key_identifier( ctx,
254 1,
255 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200256}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200259int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
260 unsigned int key_usage )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200261{
Shawn Careyaa13e932021-05-06 15:11:30 -0400262 unsigned char buf[5] = {0}, ku[2] = {0};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100265 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
266 MBEDTLS_X509_KU_NON_REPUDIATION |
267 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
268 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
269 MBEDTLS_X509_KU_KEY_AGREEMENT |
270 MBEDTLS_X509_KU_KEY_CERT_SIGN |
271 MBEDTLS_X509_KU_CRL_SIGN |
272 MBEDTLS_X509_KU_ENCIPHER_ONLY |
273 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200274
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100275 /* Check that nothing other than the allowed flags is set */
276 if( ( key_usage & ~allowed_bits ) != 0 )
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200277 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100279 c = buf + 5;
Joe Subbiani6dd73642021-07-19 11:56:54 +0100280 MBEDTLS_PUT_UINT16_LE( key_usage, ku, 0 );
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100281 ret = mbedtls_asn1_write_named_bitstring( &c, buf, ku, 9 );
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200282
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100283 if( ret < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200284 return( ret );
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100285 else if( ret < 3 || ret > 5 )
286 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100289 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
290 1, c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200291 if( ret != 0 )
292 return( ret );
293
294 return( 0 );
295}
296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298 unsigned char ns_cert_type )
299{
Shawn Careyaa13e932021-05-06 15:11:30 -0400300 unsigned char buf[4] = {0};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200301 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200303
304 c = buf + 4;
305
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100306 ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
307 if( ret < 3 || ret > 4 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200308 return( ret );
309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100311 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
312 0, c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200313 if( ret != 0 )
314 return( ret );
315
316 return( 0 );
317}
318
319static int x509_write_time( unsigned char **p, unsigned char *start,
Hanno Becker61937d42017-04-26 15:01:23 +0100320 const char *t, size_t size )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200321{
Janos Follath865b3eb2019-12-16 11:46:15 +0000322 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323 size_t len = 0;
324
325 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200327 */
Hanno Becker61937d42017-04-26 15:01:23 +0100328 if( t[0] == '2' && t[1] == '0' && t[2] < '5' )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Hanno Becker61937d42017-04-26 15:01:23 +0100331 (const unsigned char *) t + 2,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200332 size - 2 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100334 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
335 MBEDTLS_ASN1_UTC_TIME ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336 }
337 else
338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Hanno Becker61937d42017-04-26 15:01:23 +0100340 (const unsigned char *) t,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200341 size ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100343 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
344 MBEDTLS_ASN1_GENERALIZED_TIME ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200345 }
346
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200347 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200348}
349
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100350int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx,
351 unsigned char *buf, size_t size,
352 int (*f_rng)(void *, unsigned char *, size_t),
353 void *p_rng )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200354{
Janos Follath865b3eb2019-12-16 11:46:15 +0000355 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356 const char *sig_oid;
357 size_t sig_oid_len = 0;
358 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100359 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100360 size_t hash_length = 0;
361#if defined(MBEDTLS_USE_PSA_CRYPTO)
362 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
363 psa_algorithm_t psa_algorithm;
364 unsigned char hash[PSA_HASH_MAX_SIZE];
365#else
366 unsigned char hash[64];
367#endif /* MBEDTLS_USE_PSA_CRYPTO */
368
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200369 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
370 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_pk_type_t pk_alg;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200372
373 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100374 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200375 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100376 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200377
378 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100379
380 /* There's no direct way of extracting a signature algorithm
381 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
382 if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_RSA ) )
383 pk_alg = MBEDTLS_PK_RSA;
384 else if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200385 pk_alg = MBEDTLS_PK_ECDSA;
Hanno Beckerfc771442017-09-13 08:45:48 +0100386 else
Hanno Beckerd8a6f7c2017-09-22 16:05:43 +0100387 return( MBEDTLS_ERR_X509_INVALID_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
Hanno Becker81535d02017-09-13 15:39:59 +0100390 &sig_oid, &sig_oid_len ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200391 {
392 return( ret );
393 }
394
395 /*
396 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
397 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100398
399 /* Only for v3 */
Hanno Beckera20e33a2017-09-22 15:40:01 +0100400 if( ctx->version == MBEDTLS_X509_CRT_VERSION_3 )
Hanno Beckerd7f35202017-09-13 12:00:15 +0100401 {
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100402 MBEDTLS_ASN1_CHK_ADD( len,
403 mbedtls_x509_write_extensions( &c,
Hanno Beckerdef43052019-05-04 07:54:36 +0100404 buf, ctx->extensions ) );
405 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100406 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100407 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100408 MBEDTLS_ASN1_CONSTRUCTED |
409 MBEDTLS_ASN1_SEQUENCE ) );
Hanno Beckerdef43052019-05-04 07:54:36 +0100410 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100411 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100412 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100413 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
414 MBEDTLS_ASN1_CONSTRUCTED | 3 ) );
Hanno Beckerd7f35202017-09-13 12:00:15 +0100415 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200416
417 /*
418 * SubjectPublicKeyInfo
419 */
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100420 MBEDTLS_ASN1_CHK_ADD( pub_len,
421 mbedtls_pk_write_pubkey_der( ctx->subject_key,
Hanno Beckerdef43052019-05-04 07:54:36 +0100422 buf, c - buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200423 c -= pub_len;
424 len += pub_len;
425
426 /*
427 * Subject ::= Name
428 */
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100429 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100430 mbedtls_x509_write_names( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100431 ctx->subject ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200432
433 /*
434 * Validity ::= SEQUENCE {
435 * notBefore Time,
436 * notAfter Time }
437 */
438 sub_len = 0;
439
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100440 MBEDTLS_ASN1_CHK_ADD( sub_len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100441 x509_write_time( &c, buf, ctx->not_after,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100442 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200443
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100444 MBEDTLS_ASN1_CHK_ADD( sub_len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100445 x509_write_time( &c, buf, ctx->not_before,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100446 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200447
448 len += sub_len;
Hanno Beckerdef43052019-05-04 07:54:36 +0100449 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, sub_len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100450 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100451 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100452 MBEDTLS_ASN1_CONSTRUCTED |
453 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200454
455 /*
456 * Issuer ::= Name
457 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100458 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100459 ctx->issuer ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200460
461 /*
462 * Signature ::= AlgorithmIdentifier
463 */
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100464 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100465 mbedtls_asn1_write_algorithm_identifier( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100466 sig_oid, strlen( sig_oid ), 0 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200467
468 /*
469 * Serial ::= INTEGER
470 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100471 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100472 &ctx->serial ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200473
474 /*
475 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
476 */
Hanno Becker47698652017-09-13 11:59:26 +0100477
478 /* Can be omitted for v1 */
Hanno Beckera20e33a2017-09-22 15:40:01 +0100479 if( ctx->version != MBEDTLS_X509_CRT_VERSION_1 )
Hanno Becker47698652017-09-13 11:59:26 +0100480 {
481 sub_len = 0;
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100482 MBEDTLS_ASN1_CHK_ADD( sub_len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100483 mbedtls_asn1_write_int( &c, buf, ctx->version ) );
Hanno Becker47698652017-09-13 11:59:26 +0100484 len += sub_len;
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100485 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100486 mbedtls_asn1_write_len( &c, buf, sub_len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100487 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100488 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100489 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
490 MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Hanno Becker47698652017-09-13 11:59:26 +0100491 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200492
Hanno Beckerdef43052019-05-04 07:54:36 +0100493 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100494 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100495 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100496 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200497
498 /*
499 * Make signature
500 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100501
502 /* Compute hash of CRT. */
pespacek7599a772022-02-07 14:40:55 +0100503#if defined(MBEDTLS_USE_PSA_CRYPTO)
504 psa_algorithm = mbedtls_psa_translate_md( ctx->md_alg );
505
506 status = psa_hash_compute( psa_algorithm,
507 c,
508 len,
509 hash,
pespacekb9f07a72022-02-14 15:13:26 +0100510 sizeof( hash ),
pespacek7599a772022-02-07 14:40:55 +0100511 &hash_length );
512 if( status != PSA_SUCCESS )
513 {
pespacek3110c7b2022-02-14 15:07:41 +0100514 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
pespacek7599a772022-02-07 14:40:55 +0100515 }
516#else
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100517 if( ( ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c,
518 len, hash ) ) != 0 )
519 {
520 return( ret );
521 }
pespacek7599a772022-02-07 14:40:55 +0100522#endif /* MBEDTLS_USE_PSA_CRYPTO */
523
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200524
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100525 if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg,
pespacek7599a772022-02-07 14:40:55 +0100526 hash, hash_length, sig, sizeof( sig ), &sig_len,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100527 f_rng, p_rng ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528 {
529 return( ret );
530 }
531
Hanno Beckerdef43052019-05-04 07:54:36 +0100532 /* Move CRT to the front of the buffer to have space
533 * for the signature. */
534 memmove( buf, c, len );
535 c = buf + len;
536
537 /* Add signature at the end of the buffer,
538 * making sure that it doesn't underflow
539 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200540 c2 = buf + size;
Hanno Beckerdef43052019-05-04 07:54:36 +0100541 MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, c,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200542 sig_oid, sig_oid_len, sig, sig_len ) );
543
Hanno Beckerdef43052019-05-04 07:54:36 +0100544 /*
545 * Memory layout after this step:
546 *
547 * buf c=buf+len c2 buf+size
548 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
549 */
Andres AG60dbc932016-09-02 15:23:48 +0100550
Hanno Beckerdef43052019-05-04 07:54:36 +0100551 /* Move raw CRT to just before the signature. */
552 c = c2 - len;
553 memmove( c, buf, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200554
555 len += sig_and_oid_len;
Hanno Beckerdef43052019-05-04 07:54:36 +0100556 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
557 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100558 MBEDTLS_ASN1_CONSTRUCTED |
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200559 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200560
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200561 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200562}
563
564#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
565#define PEM_END_CRT "-----END CERTIFICATE-----\n"
566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#if defined(MBEDTLS_PEM_WRITE_C)
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100568int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt,
569 unsigned char *buf, size_t size,
570 int (*f_rng)(void *, unsigned char *, size_t),
571 void *p_rng )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200572{
Janos Follath865b3eb2019-12-16 11:46:15 +0000573 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100574 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200575
Hanno Becker67d42592019-05-04 08:13:23 +0100576 if( ( ret = mbedtls_x509write_crt_der( crt, buf, size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200577 f_rng, p_rng ) ) < 0 )
578 {
579 return( ret );
580 }
581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
Hanno Becker67d42592019-05-04 08:13:23 +0100583 buf + size - ret, ret,
584 buf, size, &olen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200585 {
586 return( ret );
587 }
588
589 return( 0 );
590}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#endif /* MBEDTLS_X509_CRT_WRITE_C */