blob: 52942a9e8dba43f2a6d7a9c75eca388e0be8421c [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
Przemek Stekiel40afdd22022-09-06 13:08:28 +020048#include "hash_info.h"
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Przemek Stekielfd183662022-08-02 15:29:20 +020050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052{
Hanno Becker81535d02017-09-13 15:39:59 +010053 memset( ctx, 0, sizeof( mbedtls_x509write_cert ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 mbedtls_mpi_init( &ctx->serial );
56 ctx->version = MBEDTLS_X509_CRT_VERSION_3;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020057}
58
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_mpi_free( &ctx->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_asn1_free_named_data_list( &ctx->subject );
64 mbedtls_asn1_free_named_data_list( &ctx->issuer );
65 mbedtls_asn1_free_named_data_list( &ctx->extensions );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020066
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050067 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_cert ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020068}
69
Hanno Becker6ad3fd12019-05-04 07:37:58 +010070void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx,
71 int version )
Paul Bakker5191e922013-10-11 10:54:28 +020072{
73 ctx->version = version;
74}
75
Hanno Becker6ad3fd12019-05-04 07:37:58 +010076void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx,
77 mbedtls_md_type_t md_alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020078{
79 ctx->md_alg = md_alg;
80}
81
Hanno Becker6ad3fd12019-05-04 07:37:58 +010082void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx,
83 mbedtls_pk_context *key )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020084{
85 ctx->subject_key = key;
86}
87
Hanno Becker6ad3fd12019-05-04 07:37:58 +010088void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx,
89 mbedtls_pk_context *key )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090{
91 ctx->issuer_key = key;
92}
93
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Hanno Becker6ad3fd12019-05-04 07:37:58 +010095 const char *subject_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020098}
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100101 const char *issuer_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104}
105
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100106int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx,
107 const mbedtls_mpi *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200108{
Janos Follath865b3eb2019-12-16 11:46:15 +0000109 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112 return( ret );
113
114 return( 0 );
115}
116
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100117int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx,
118 const char *not_before,
119 const char *not_after )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121 if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
122 strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200125 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
127 strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
128 ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
129 ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200130
131 return( 0 );
132}
133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200135 const char *oid, size_t oid_len,
136 int critical,
137 const unsigned char *val, size_t val_len )
138{
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100139 return( mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
140 critical, val, val_len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200141}
142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100144 int is_ca, int max_pathlen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200145{
Janos Follath865b3eb2019-12-16 11:46:15 +0000146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147 unsigned char buf[9];
148 unsigned char *c = buf + sizeof(buf);
149 size_t len = 0;
150
151 memset( buf, 0, sizeof(buf) );
152
153 if( is_ca && max_pathlen > 127 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200155
156 if( is_ca )
157 {
158 if( max_pathlen >= 0 )
159 {
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100160 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf,
161 max_pathlen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200164 }
165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100167 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
168 MBEDTLS_ASN1_CONSTRUCTED |
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200170
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100171 return(
172 mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
173 MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ),
Darren Krahne560be32020-09-21 17:40:50 -0700174 is_ca, buf + sizeof(buf) - len, len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200175}
176
Przemek Stekiel76b753b2022-08-09 10:54:45 +0200177#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
pespacekb9ca22d2022-03-07 13:30:01 +0100178static int mbedtls_x509write_crt_set_key_identifier( mbedtls_x509write_cert *ctx,
pespaceka6e955e2022-02-14 15:20:57 +0100179 int is_ca,
180 unsigned char tag )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200181{
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;
pespacek7599a772022-02-07 14:40:55 +0100186#if defined(MBEDTLS_USE_PSA_CRYPTO)
187 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
188 size_t hash_length;
189#endif /* MBEDTLS_USE_PSA_CRYPTO */
190
Paul Bakker66d5d072014-06-17 16:39:18 +0200191 memset( buf, 0, sizeof(buf) );
pespacekd924e552022-02-28 11:49:54 +0100192 MBEDTLS_ASN1_CHK_ADD( len,
193 mbedtls_pk_write_pubkey( &c,
194 buf,
195 is_ca ?
196 ctx->issuer_key :
197 ctx->subject_key ) );
pespacek30151482022-02-17 15:18:47 +0100198
pespaceka6e955e2022-02-14 15:20:57 +0100199
pespacek7599a772022-02-07 14:40:55 +0100200#if defined(MBEDTLS_USE_PSA_CRYPTO)
201 status = psa_hash_compute( PSA_ALG_SHA_1,
pespacekb9f07a72022-02-14 15:13:26 +0100202 buf + sizeof(buf) - len,
pespacek7599a772022-02-07 14:40:55 +0100203 len,
pespacekb9f07a72022-02-14 15:13:26 +0100204 buf + sizeof(buf) - 20,
pespacek30151482022-02-17 15:18:47 +0100205 20,
pespacek7599a772022-02-07 14:40:55 +0100206 &hash_length );
207 if( status != PSA_SUCCESS )
208 {
pespacek3110c7b2022-02-14 15:07:41 +0100209 return( MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED );
pespacek7599a772022-02-07 14:40:55 +0100210 }
211#else
TRodziewicz26371e42021-06-08 16:45:41 +0200212 ret = mbedtls_sha1( buf + sizeof( buf ) - len, len,
pespacek7599a772022-02-07 14:40:55 +0100213 buf + sizeof( buf ) - 20 );
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100214 if( ret != 0 )
215 return( ret );
pespacek7599a772022-02-07 14:40:55 +0100216#endif /* MBEDTLS_USE_PSA_CRYPTO */
217
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100218 c = buf + sizeof( buf ) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219 len = 20;
220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
pespacekd924e552022-02-28 11:49:54 +0100222 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf, tag ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200223
pespaceka6e955e2022-02-14 15:20:57 +0100224 if( is_ca ) // writes AuthorityKeyIdentifier sequence
225 {
226 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ));
227 MBEDTLS_ASN1_CHK_ADD( len,
228 mbedtls_asn1_write_tag( &c,
229 buf,
230 MBEDTLS_ASN1_CONSTRUCTED |
231 MBEDTLS_ASN1_SEQUENCE ) );
pespacek30151482022-02-17 15:18:47 +0100232 }
pespacekd924e552022-02-28 11:49:54 +0100233
234 if( is_ca )
pespacekb9ca22d2022-03-07 13:30:01 +0100235 return( mbedtls_x509write_crt_set_extension( ctx,
pespacekd924e552022-02-28 11:49:54 +0100236 MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
237 MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
pespacekb9ca22d2022-03-07 13:30:01 +0100238 0, buf + sizeof(buf) - len, len ) );
239 else
240 return( mbedtls_x509write_crt_set_extension( ctx,
241 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
242 MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
243 0, buf + sizeof(buf) - len, len ) );
pespaceka6e955e2022-02-14 15:20:57 +0100244}
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200245
pespaceka6e955e2022-02-14 15:20:57 +0100246int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
247{
248 return mbedtls_x509write_crt_set_key_identifier( ctx,
249 0,
250 MBEDTLS_ASN1_OCTET_STRING );
251}
252
253int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
254{
255 return mbedtls_x509write_crt_set_key_identifier( ctx,
256 1,
257 (MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200258}
Przemek Stekiel41465252022-08-18 12:43:07 +0200259#endif /* MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200260
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200261int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
262 unsigned int key_usage )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200263{
Shawn Careyaa13e932021-05-06 15:11:30 -0400264 unsigned char buf[5] = {0}, ku[2] = {0};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200265 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000266 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100267 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
268 MBEDTLS_X509_KU_NON_REPUDIATION |
269 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
270 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
271 MBEDTLS_X509_KU_KEY_AGREEMENT |
272 MBEDTLS_X509_KU_KEY_CERT_SIGN |
273 MBEDTLS_X509_KU_CRL_SIGN |
274 MBEDTLS_X509_KU_ENCIPHER_ONLY |
275 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200276
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100277 /* Check that nothing other than the allowed flags is set */
278 if( ( key_usage & ~allowed_bits ) != 0 )
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200279 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200280
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100281 c = buf + 5;
Joe Subbiani6dd73642021-07-19 11:56:54 +0100282 MBEDTLS_PUT_UINT16_LE( key_usage, ku, 0 );
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100283 ret = mbedtls_asn1_write_named_bitstring( &c, buf, ku, 9 );
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200284
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100285 if( ret < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200286 return( ret );
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100287 else if( ret < 3 || ret > 5 )
288 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100291 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
292 1, c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200293 if( ret != 0 )
294 return( ret );
295
296 return( 0 );
297}
298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200300 unsigned char ns_cert_type )
301{
Shawn Careyaa13e932021-05-06 15:11:30 -0400302 unsigned char buf[4] = {0};
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200303 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200305
306 c = buf + 4;
307
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100308 ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
309 if( ret < 3 || ret > 4 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200310 return( ret );
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100313 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
314 0, c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200315 if( ret != 0 )
316 return( ret );
317
318 return( 0 );
319}
320
321static int x509_write_time( unsigned char **p, unsigned char *start,
Hanno Becker61937d42017-04-26 15:01:23 +0100322 const char *t, size_t size )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323{
Janos Follath865b3eb2019-12-16 11:46:15 +0000324 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200325 size_t len = 0;
326
327 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200328 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329 */
Werner Lewisacd01e52022-05-10 12:23:13 +0100330 if( t[0] < '2' || ( t[0] == '2' && t[1] == '0' && t[2] < '5' ) )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200332 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Hanno Becker61937d42017-04-26 15:01:23 +0100333 (const unsigned char *) t + 2,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200334 size - 2 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100336 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
337 MBEDTLS_ASN1_UTC_TIME ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200338 }
339 else
340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Hanno Becker61937d42017-04-26 15:01:23 +0100342 (const unsigned char *) t,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200343 size ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100345 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
346 MBEDTLS_ASN1_GENERALIZED_TIME ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200347 }
348
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200349 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200350}
351
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100352int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx,
353 unsigned char *buf, size_t size,
354 int (*f_rng)(void *, unsigned char *, size_t),
355 void *p_rng )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200356{
Janos Follath865b3eb2019-12-16 11:46:15 +0000357 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 const char *sig_oid;
359 size_t sig_oid_len = 0;
360 unsigned char *c, *c2;
Gilles Peskinebf887802019-11-08 19:21:51 +0100361 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100362 size_t hash_length = 0;
Przemek Stekiel40afdd22022-09-06 13:08:28 +0200363 unsigned char hash[MBEDTLS_HASH_MAX_SIZE];
pespacek7599a772022-02-07 14:40:55 +0100364#if defined(MBEDTLS_USE_PSA_CRYPTO)
365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
366 psa_algorithm_t psa_algorithm;
pespacek7599a772022-02-07 14:40:55 +0100367#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)
Manuel Pégourié-Gonnardabac0372022-07-18 13:41:11 +0200504 psa_algorithm = mbedtls_hash_info_psa_from_md( ctx->md_alg );
pespacek7599a772022-02-07 14:40:55 +0100505
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 */