blob: 498b8b0a0c1bc3241837cc6d427050fc2d65202e [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020044{
Hanno Becker81535d02017-09-13 15:39:59 +010045 memset( ctx, 0, sizeof( mbedtls_x509write_cert ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 mbedtls_mpi_init( &ctx->serial );
48 ctx->version = MBEDTLS_X509_CRT_VERSION_3;
Paul Bakker7c6b2c32013-09-16 13:49:26 +020049}
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020052{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_mpi_free( &ctx->serial );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 mbedtls_asn1_free_named_data_list( &ctx->subject );
56 mbedtls_asn1_free_named_data_list( &ctx->issuer );
57 mbedtls_asn1_free_named_data_list( &ctx->extensions );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020058
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050059 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_cert ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020060}
61
Hanno Becker6ad3fd12019-05-04 07:37:58 +010062void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx,
63 int version )
Paul Bakker5191e922013-10-11 10:54:28 +020064{
65 ctx->version = version;
66}
67
Hanno Becker6ad3fd12019-05-04 07:37:58 +010068void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx,
69 mbedtls_md_type_t md_alg )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020070{
71 ctx->md_alg = md_alg;
72}
73
Hanno Becker6ad3fd12019-05-04 07:37:58 +010074void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx,
75 mbedtls_pk_context *key )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020076{
77 ctx->subject_key = key;
78}
79
Hanno Becker6ad3fd12019-05-04 07:37:58 +010080void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx,
81 mbedtls_pk_context *key )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020082{
83 ctx->issuer_key = key;
84}
85
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
Hanno Becker6ad3fd12019-05-04 07:37:58 +010087 const char *subject_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020090}
91
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
Hanno Becker6ad3fd12019-05-04 07:37:58 +010093 const char *issuer_name )
Paul Bakker7c6b2c32013-09-16 13:49:26 +020094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095 return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name );
Paul Bakker7c6b2c32013-09-16 13:49:26 +020096}
97
Hanno Becker6ad3fd12019-05-04 07:37:58 +010098int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx,
99 const mbedtls_mpi *serial )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200100{
Janos Follath865b3eb2019-12-16 11:46:15 +0000101 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200104 return( ret );
105
106 return( 0 );
107}
108
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100109int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx,
110 const char *not_before,
111 const char *not_after )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200112{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
114 strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200117 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
119 strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
120 ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
121 ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200122
123 return( 0 );
124}
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200127 const char *oid, size_t oid_len,
128 int critical,
129 const unsigned char *val, size_t val_len )
130{
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100131 return( mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
132 critical, val, val_len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200133}
134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100136 int is_ca, int max_pathlen )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200137{
Janos Follath865b3eb2019-12-16 11:46:15 +0000138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200139 unsigned char buf[9];
140 unsigned char *c = buf + sizeof(buf);
141 size_t len = 0;
142
143 memset( buf, 0, sizeof(buf) );
144
145 if( is_ca && max_pathlen > 127 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200147
148 if( is_ca )
149 {
150 if( max_pathlen >= 0 )
151 {
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100152 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf,
153 max_pathlen ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200154 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200156 }
157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100159 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
160 MBEDTLS_ASN1_CONSTRUCTED |
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200162
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100163 return(
164 mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
165 MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ),
Darren Krahne560be32020-09-21 17:40:50 -0700166 is_ca, buf + sizeof(buf) - len, len ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200167}
168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#if defined(MBEDTLS_SHA1_C)
170int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200171{
Janos Follath865b3eb2019-12-16 11:46:15 +0000172 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200174 unsigned char *c = buf + sizeof(buf);
175 size_t len = 0;
176
Paul Bakker66d5d072014-06-17 16:39:18 +0200177 memset( buf, 0, sizeof(buf) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100178 MBEDTLS_ASN1_CHK_ADD( len,
179 mbedtls_pk_write_pubkey( &c, buf, ctx->subject_key ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200180
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100181 ret = mbedtls_sha1_ret( buf + sizeof( buf ) - len, len,
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100182 buf + sizeof( buf ) - 20 );
183 if( ret != 0 )
184 return( ret );
185 c = buf + sizeof( buf ) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200186 len = 20;
187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100189 MBEDTLS_ASN1_CHK_ADD( len,
190 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OCTET_STRING ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200191
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100192 return mbedtls_x509write_crt_set_extension( ctx,
193 MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
194 MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
195 0, buf + sizeof(buf) - len, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200196}
197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200199{
Janos Follath865b3eb2019-12-16 11:46:15 +0000200 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
Hanno Becker81535d02017-09-13 15:39:59 +0100202 unsigned char *c = buf + sizeof( buf );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200203 size_t len = 0;
204
Paul Bakker66d5d072014-06-17 16:39:18 +0200205 memset( buf, 0, sizeof(buf) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100206 MBEDTLS_ASN1_CHK_ADD( len,
207 mbedtls_pk_write_pubkey( &c, buf, ctx->issuer_key ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200208
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100209 ret = mbedtls_sha1_ret( buf + sizeof( buf ) - len, len,
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100210 buf + sizeof( buf ) - 20 );
211 if( ret != 0 )
212 return( ret );
213 c = buf + sizeof( buf ) - 20;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200214 len = 20;
215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200216 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100217 MBEDTLS_ASN1_CHK_ADD( len,
218 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100221 MBEDTLS_ASN1_CHK_ADD( len,
222 mbedtls_asn1_write_tag( &c, buf,
223 MBEDTLS_ASN1_CONSTRUCTED |
224 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200225
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100226 return mbedtls_x509write_crt_set_extension(
227 ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
228 MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
229 0, buf + sizeof( buf ) - len, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200230}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231#endif /* MBEDTLS_SHA1_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200232
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200233int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
234 unsigned int key_usage )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200235{
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100236 unsigned char buf[5], ku[2];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200237 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100239 const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
240 MBEDTLS_X509_KU_NON_REPUDIATION |
241 MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
242 MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
243 MBEDTLS_X509_KU_KEY_AGREEMENT |
244 MBEDTLS_X509_KU_KEY_CERT_SIGN |
245 MBEDTLS_X509_KU_CRL_SIGN |
246 MBEDTLS_X509_KU_ENCIPHER_ONLY |
247 MBEDTLS_X509_KU_DECIPHER_ONLY;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200248
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100249 /* Check that nothing other than the allowed flags is set */
250 if( ( key_usage & ~allowed_bits ) != 0 )
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200251 return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200252
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100253 c = buf + 5;
254 ku[0] = (unsigned char)( key_usage );
255 ku[1] = (unsigned char)( key_usage >> 8 );
256 ret = mbedtls_asn1_write_named_bitstring( &c, buf, ku, 9 );
Manuel Pégourié-Gonnard1cd10ad2015-06-23 11:07:37 +0200257
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100258 if( ret < 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200259 return( ret );
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100260 else if( ret < 3 || ret > 5 )
261 return( MBEDTLS_ERR_X509_INVALID_FORMAT );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100264 MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
265 1, c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200266 if( ret != 0 )
267 return( ret );
268
269 return( 0 );
270}
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200273 unsigned char ns_cert_type )
274{
275 unsigned char buf[4];
276 unsigned char *c;
Janos Follath865b3eb2019-12-16 11:46:15 +0000277 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200278
279 c = buf + 4;
280
Andres Amaya Garcia6e959142018-09-26 10:48:24 +0100281 ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
282 if( ret < 3 || ret > 4 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200283 return( ret );
284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200285 ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100286 MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
287 0, c, (size_t)ret );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200288 if( ret != 0 )
289 return( ret );
290
291 return( 0 );
292}
293
294static int x509_write_time( unsigned char **p, unsigned char *start,
Hanno Becker61937d42017-04-26 15:01:23 +0100295 const char *t, size_t size )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200296{
Janos Follath865b3eb2019-12-16 11:46:15 +0000297 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200298 size_t len = 0;
299
300 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 * write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200302 */
Hanno Becker61937d42017-04-26 15:01:23 +0100303 if( t[0] == '2' && t[1] == '0' && t[2] < '5' )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Hanno Becker61937d42017-04-26 15:01:23 +0100306 (const unsigned char *) t + 2,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200307 size - 2 ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100309 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
310 MBEDTLS_ASN1_UTC_TIME ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200311 }
312 else
313 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
Hanno Becker61937d42017-04-26 15:01:23 +0100315 (const unsigned char *) t,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200316 size ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100318 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
319 MBEDTLS_ASN1_GENERALIZED_TIME ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200320 }
321
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200322 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200323}
324
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100325int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx,
326 unsigned char *buf, size_t size,
327 int (*f_rng)(void *, unsigned char *, size_t),
328 void *p_rng )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200329{
Janos Follath865b3eb2019-12-16 11:46:15 +0000330 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200331 const char *sig_oid;
332 size_t sig_oid_len = 0;
333 unsigned char *c, *c2;
334 unsigned char hash[64];
Gilles Peskinebf887802019-11-08 19:21:51 +0100335 unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200336 size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
337 size_t len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338 mbedtls_pk_type_t pk_alg;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200339
340 /*
Hanno Beckerdef43052019-05-04 07:54:36 +0100341 * Prepare data to be signed at the end of the target buffer
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200342 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100343 c = buf + size;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200344
345 /* Signature algorithm needed in TBS, and later for actual signature */
Hanno Beckerfc771442017-09-13 08:45:48 +0100346
347 /* There's no direct way of extracting a signature algorithm
348 * (represented as an element of mbedtls_pk_type_t) from a PK instance. */
349 if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_RSA ) )
350 pk_alg = MBEDTLS_PK_RSA;
351 else if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_ECDSA ) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 pk_alg = MBEDTLS_PK_ECDSA;
Hanno Beckerfc771442017-09-13 08:45:48 +0100353 else
Hanno Beckerd8a6f7c2017-09-22 16:05:43 +0100354 return( MBEDTLS_ERR_X509_INVALID_ALG );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
Hanno Becker81535d02017-09-13 15:39:59 +0100357 &sig_oid, &sig_oid_len ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200358 {
359 return( ret );
360 }
361
362 /*
363 * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
364 */
Hanno Beckerd7f35202017-09-13 12:00:15 +0100365
366 /* Only for v3 */
Hanno Beckera20e33a2017-09-22 15:40:01 +0100367 if( ctx->version == MBEDTLS_X509_CRT_VERSION_3 )
Hanno Beckerd7f35202017-09-13 12:00:15 +0100368 {
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100369 MBEDTLS_ASN1_CHK_ADD( len,
370 mbedtls_x509_write_extensions( &c,
Hanno Beckerdef43052019-05-04 07:54:36 +0100371 buf, ctx->extensions ) );
372 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100373 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100374 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100375 MBEDTLS_ASN1_CONSTRUCTED |
376 MBEDTLS_ASN1_SEQUENCE ) );
Hanno Beckerdef43052019-05-04 07:54:36 +0100377 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100378 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100379 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100380 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
381 MBEDTLS_ASN1_CONSTRUCTED | 3 ) );
Hanno Beckerd7f35202017-09-13 12:00:15 +0100382 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200383
384 /*
385 * SubjectPublicKeyInfo
386 */
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100387 MBEDTLS_ASN1_CHK_ADD( pub_len,
388 mbedtls_pk_write_pubkey_der( ctx->subject_key,
Hanno Beckerdef43052019-05-04 07:54:36 +0100389 buf, c - buf ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200390 c -= pub_len;
391 len += pub_len;
392
393 /*
394 * Subject ::= Name
395 */
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100396 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100397 mbedtls_x509_write_names( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100398 ctx->subject ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200399
400 /*
401 * Validity ::= SEQUENCE {
402 * notBefore Time,
403 * notAfter Time }
404 */
405 sub_len = 0;
406
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100407 MBEDTLS_ASN1_CHK_ADD( sub_len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100408 x509_write_time( &c, buf, ctx->not_after,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100409 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200410
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100411 MBEDTLS_ASN1_CHK_ADD( sub_len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100412 x509_write_time( &c, buf, ctx->not_before,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100413 MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200414
415 len += sub_len;
Hanno Beckerdef43052019-05-04 07:54:36 +0100416 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, sub_len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100417 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100418 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100419 MBEDTLS_ASN1_CONSTRUCTED |
420 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200421
422 /*
423 * Issuer ::= Name
424 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100425 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100426 ctx->issuer ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200427
428 /*
429 * Signature ::= AlgorithmIdentifier
430 */
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100431 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100432 mbedtls_asn1_write_algorithm_identifier( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100433 sig_oid, strlen( sig_oid ), 0 ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200434
435 /*
436 * Serial ::= INTEGER
437 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100438 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100439 &ctx->serial ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200440
441 /*
442 * Version ::= INTEGER { v1(0), v2(1), v3(2) }
443 */
Hanno Becker47698652017-09-13 11:59:26 +0100444
445 /* Can be omitted for v1 */
Hanno Beckera20e33a2017-09-22 15:40:01 +0100446 if( ctx->version != MBEDTLS_X509_CRT_VERSION_1 )
Hanno Becker47698652017-09-13 11:59:26 +0100447 {
448 sub_len = 0;
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100449 MBEDTLS_ASN1_CHK_ADD( sub_len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100450 mbedtls_asn1_write_int( &c, buf, ctx->version ) );
Hanno Becker47698652017-09-13 11:59:26 +0100451 len += sub_len;
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100452 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100453 mbedtls_asn1_write_len( &c, buf, sub_len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100454 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100455 mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100456 MBEDTLS_ASN1_CONTEXT_SPECIFIC |
457 MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
Hanno Becker47698652017-09-13 11:59:26 +0100458 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200459
Hanno Beckerdef43052019-05-04 07:54:36 +0100460 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100461 MBEDTLS_ASN1_CHK_ADD( len,
Hanno Beckerdef43052019-05-04 07:54:36 +0100462 mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100463 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200464
465 /*
466 * Make signature
467 */
Hanno Beckerdef43052019-05-04 07:54:36 +0100468
469 /* Compute hash of CRT. */
Andres Amaya Garcia8d8204f2017-06-28 11:07:30 +0100470 if( ( ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c,
471 len, hash ) ) != 0 )
472 {
473 return( ret );
474 }
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200475
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100476 if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg,
477 hash, 0, sig, &sig_len,
478 f_rng, p_rng ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200479 {
480 return( ret );
481 }
482
Hanno Beckerdef43052019-05-04 07:54:36 +0100483 /* Move CRT to the front of the buffer to have space
484 * for the signature. */
485 memmove( buf, c, len );
486 c = buf + len;
487
488 /* Add signature at the end of the buffer,
489 * making sure that it doesn't underflow
490 * into the CRT buffer. */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200491 c2 = buf + size;
Hanno Beckerdef43052019-05-04 07:54:36 +0100492 MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, c,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200493 sig_oid, sig_oid_len, sig, sig_len ) );
494
Hanno Beckerdef43052019-05-04 07:54:36 +0100495 /*
496 * Memory layout after this step:
497 *
498 * buf c=buf+len c2 buf+size
499 * [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
500 */
Andres AG60dbc932016-09-02 15:23:48 +0100501
Hanno Beckerdef43052019-05-04 07:54:36 +0100502 /* Move raw CRT to just before the signature. */
503 c = c2 - len;
504 memmove( c, buf, len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200505
506 len += sig_and_oid_len;
Hanno Beckerdef43052019-05-04 07:54:36 +0100507 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
508 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100509 MBEDTLS_ASN1_CONSTRUCTED |
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200510 MBEDTLS_ASN1_SEQUENCE ) );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200511
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200512 return( (int) len );
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200513}
514
515#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
516#define PEM_END_CRT "-----END CERTIFICATE-----\n"
517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200518#if defined(MBEDTLS_PEM_WRITE_C)
Hanno Becker6ad3fd12019-05-04 07:37:58 +0100519int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt,
520 unsigned char *buf, size_t size,
521 int (*f_rng)(void *, unsigned char *, size_t),
522 void *p_rng )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200523{
Janos Follath865b3eb2019-12-16 11:46:15 +0000524 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker67d42592019-05-04 08:13:23 +0100525 size_t olen;
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200526
Hanno Becker67d42592019-05-04 08:13:23 +0100527 if( ( ret = mbedtls_x509write_crt_der( crt, buf, size,
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200528 f_rng, p_rng ) ) < 0 )
529 {
530 return( ret );
531 }
532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
Hanno Becker67d42592019-05-04 08:13:23 +0100534 buf + size - ret, ret,
535 buf, size, &olen ) ) != 0 )
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200536 {
537 return( ret );
538 }
539
540 return( 0 );
541}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542#endif /* MBEDTLS_PEM_WRITE_C */
Paul Bakker7c6b2c32013-09-16 13:49:26 +0200543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544#endif /* MBEDTLS_X509_CRT_WRITE_C */