2013-09-16 11:49:26 +00:00
|
|
|
/*
|
|
|
|
* X.509 certificate writing
|
|
|
|
*
|
2020-08-07 11:07:28 +00:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 12:21:07 +00:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
* not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-16 11:49:26 +00:00
|
|
|
*
|
2015-09-04 12:21:07 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* References:
|
|
|
|
* - certificates: RFC 5280, updated by RFC 6818
|
|
|
|
* - CSRs: PKCS#10 v1.7 aka RFC 2986
|
|
|
|
* - attributes: PKCS#9 v2.0 aka RFC 2985
|
|
|
|
*/
|
|
|
|
|
2020-06-02 23:43:33 +00:00
|
|
|
#include "common.h"
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_X509_CRT_WRITE_C)
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/asn1write.h"
|
2019-12-18 15:07:04 +00:00
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2018-04-17 14:51:09 +00:00
|
|
|
#include "mbedtls/platform_util.h"
|
2019-12-18 15:07:04 +00:00
|
|
|
#include "mbedtls/sha1.h"
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-02-06 13:43:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
2015-03-09 17:05:11 +00:00
|
|
|
#include "mbedtls/pem.h"
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PEM_WRITE_C */
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_x509write_crt_init( mbedtls_x509write_cert *ctx )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2017-09-13 14:39:59 +00:00
|
|
|
memset( ctx, 0, sizeof( mbedtls_x509write_cert ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_mpi_init( &ctx->serial );
|
|
|
|
ctx->version = MBEDTLS_X509_CRT_VERSION_3;
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
void mbedtls_x509write_crt_free( mbedtls_x509write_cert *ctx )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_mpi_free( &ctx->serial );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_asn1_free_named_data_list( &ctx->subject );
|
|
|
|
mbedtls_asn1_free_named_data_list( &ctx->issuer );
|
|
|
|
mbedtls_asn1_free_named_data_list( &ctx->extensions );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2018-04-17 14:51:09 +00:00
|
|
|
mbedtls_platform_zeroize( ctx, sizeof( mbedtls_x509write_cert ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
void mbedtls_x509write_crt_set_version( mbedtls_x509write_cert *ctx,
|
|
|
|
int version )
|
2013-10-11 08:54:28 +00:00
|
|
|
{
|
|
|
|
ctx->version = version;
|
|
|
|
}
|
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
void mbedtls_x509write_crt_set_md_alg( mbedtls_x509write_cert *ctx,
|
|
|
|
mbedtls_md_type_t md_alg )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
ctx->md_alg = md_alg;
|
|
|
|
}
|
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
void mbedtls_x509write_crt_set_subject_key( mbedtls_x509write_cert *ctx,
|
|
|
|
mbedtls_pk_context *key )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
ctx->subject_key = key;
|
|
|
|
}
|
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
void mbedtls_x509write_crt_set_issuer_key( mbedtls_x509write_cert *ctx,
|
|
|
|
mbedtls_pk_context *key )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
ctx->issuer_key = key;
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509write_crt_set_subject_name( mbedtls_x509write_cert *ctx,
|
2019-05-04 06:37:58 +00:00
|
|
|
const char *subject_name )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_x509_string_to_names( &ctx->subject, subject_name );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509write_crt_set_issuer_name( mbedtls_x509write_cert *ctx,
|
2019-05-04 06:37:58 +00:00
|
|
|
const char *issuer_name )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return mbedtls_x509_string_to_names( &ctx->issuer, issuer_name );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
int mbedtls_x509write_crt_set_serial( mbedtls_x509write_cert *ctx,
|
|
|
|
const mbedtls_mpi *serial )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_mpi_copy( &ctx->serial, serial ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
int mbedtls_x509write_crt_set_validity( mbedtls_x509write_cert *ctx,
|
|
|
|
const char *not_before,
|
|
|
|
const char *not_after )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
if( strlen( not_before ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
|
|
|
|
strlen( not_after ) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
strncpy( ctx->not_before, not_before, MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
|
|
|
|
strncpy( ctx->not_after , not_after , MBEDTLS_X509_RFC5280_UTC_TIME_LEN );
|
|
|
|
ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
|
|
|
|
ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509write_crt_set_extension( mbedtls_x509write_cert *ctx,
|
2013-09-16 11:49:26 +00:00
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
int critical,
|
|
|
|
const unsigned char *val, size_t val_len )
|
|
|
|
{
|
2019-05-04 06:37:58 +00:00
|
|
|
return( mbedtls_x509_set_extension( &ctx->extensions, oid, oid_len,
|
|
|
|
critical, val, val_len ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509write_crt_set_basic_constraints( mbedtls_x509write_cert *ctx,
|
2019-05-04 06:37:58 +00:00
|
|
|
int is_ca, int max_pathlen )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 11:49:26 +00:00
|
|
|
unsigned char buf[9];
|
|
|
|
unsigned char *c = buf + sizeof(buf);
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
memset( buf, 0, sizeof(buf) );
|
|
|
|
|
|
|
|
if( is_ca && max_pathlen > 127 )
|
2015-04-08 10:49:31 +00:00
|
|
|
return( MBEDTLS_ERR_X509_BAD_INPUT_DATA );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
if( is_ca )
|
|
|
|
{
|
|
|
|
if( max_pathlen >= 0 )
|
|
|
|
{
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_int( &c, buf,
|
|
|
|
max_pathlen ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_bool( &c, buf, 1 ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
return(
|
|
|
|
mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
|
|
|
|
MBEDTLS_OID_SIZE( MBEDTLS_OID_BASIC_CONSTRAINTS ),
|
2020-09-22 00:40:50 +00:00
|
|
|
is_ca, buf + sizeof(buf) - len, len ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_SHA1_C)
|
|
|
|
int mbedtls_x509write_crt_set_subject_key_identifier( mbedtls_x509write_cert *ctx )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
|
2013-09-16 11:49:26 +00:00
|
|
|
unsigned char *c = buf + sizeof(buf);
|
|
|
|
size_t len = 0;
|
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
memset( buf, 0, sizeof(buf) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
|
|
|
mbedtls_pk_write_pubkey( &c, buf, ctx->subject_key ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2021-06-08 14:45:41 +00:00
|
|
|
ret = mbedtls_sha1( buf + sizeof( buf ) - len, len,
|
2017-06-28 10:07:30 +00:00
|
|
|
buf + sizeof( buf ) - 20 );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
c = buf + sizeof( buf ) - 20;
|
2013-09-16 11:49:26 +00:00
|
|
|
len = 20;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
|
|
|
mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_OCTET_STRING ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
return mbedtls_x509write_crt_set_extension( ctx,
|
|
|
|
MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER,
|
|
|
|
MBEDTLS_OID_SIZE( MBEDTLS_OID_SUBJECT_KEY_IDENTIFIER ),
|
|
|
|
0, buf + sizeof(buf) - len, len );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509write_crt_set_authority_key_identifier( mbedtls_x509write_cert *ctx )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2015-04-08 10:49:31 +00:00
|
|
|
unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
|
2017-09-13 14:39:59 +00:00
|
|
|
unsigned char *c = buf + sizeof( buf );
|
2013-09-16 11:49:26 +00:00
|
|
|
size_t len = 0;
|
|
|
|
|
2014-06-17 14:39:18 +00:00
|
|
|
memset( buf, 0, sizeof(buf) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
|
|
|
mbedtls_pk_write_pubkey( &c, buf, ctx->issuer_key ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2021-06-08 14:45:41 +00:00
|
|
|
ret = mbedtls_sha1( buf + sizeof( buf ) - len, len,
|
2017-06-28 10:07:30 +00:00
|
|
|
buf + sizeof( buf ) - 20 );
|
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
c = buf + sizeof( buf ) - 20;
|
2013-09-16 11:49:26 +00:00
|
|
|
len = 20;
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
|
|
|
mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0 ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
|
|
|
mbedtls_asn1_write_tag( &c, buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
|
|
|
|
|
|
|
return mbedtls_x509write_crt_set_extension(
|
|
|
|
ctx, MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER,
|
|
|
|
MBEDTLS_OID_SIZE( MBEDTLS_OID_AUTHORITY_KEY_IDENTIFIER ),
|
|
|
|
0, buf + sizeof( buf ) - len, len );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_SHA1_C */
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-06-23 09:07:37 +00:00
|
|
|
int mbedtls_x509write_crt_set_key_usage( mbedtls_x509write_cert *ctx,
|
|
|
|
unsigned int key_usage )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2021-05-06 19:11:30 +00:00
|
|
|
unsigned char buf[5] = {0}, ku[2] = {0};
|
2013-09-16 11:49:26 +00:00
|
|
|
unsigned char *c;
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-09-26 09:48:24 +00:00
|
|
|
const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
|
|
|
|
MBEDTLS_X509_KU_NON_REPUDIATION |
|
|
|
|
MBEDTLS_X509_KU_KEY_ENCIPHERMENT |
|
|
|
|
MBEDTLS_X509_KU_DATA_ENCIPHERMENT |
|
|
|
|
MBEDTLS_X509_KU_KEY_AGREEMENT |
|
|
|
|
MBEDTLS_X509_KU_KEY_CERT_SIGN |
|
|
|
|
MBEDTLS_X509_KU_CRL_SIGN |
|
|
|
|
MBEDTLS_X509_KU_ENCIPHER_ONLY |
|
|
|
|
MBEDTLS_X509_KU_DECIPHER_ONLY;
|
|
|
|
|
|
|
|
/* Check that nothing other than the allowed flags is set */
|
|
|
|
if( ( key_usage & ~allowed_bits ) != 0 )
|
2015-06-23 09:07:37 +00:00
|
|
|
return( MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE );
|
|
|
|
|
2018-09-26 09:48:24 +00:00
|
|
|
c = buf + 5;
|
2021-07-19 10:56:54 +00:00
|
|
|
MBEDTLS_PUT_UINT16_LE( key_usage, ku, 0 );
|
2018-09-26 09:48:24 +00:00
|
|
|
ret = mbedtls_asn1_write_named_bitstring( &c, buf, ku, 9 );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2018-09-26 09:48:24 +00:00
|
|
|
if( ret < 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
2018-09-26 09:48:24 +00:00
|
|
|
else if( ret < 3 || ret > 5 )
|
|
|
|
return( MBEDTLS_ERR_X509_INVALID_FORMAT );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_KEY_USAGE,
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_OID_SIZE( MBEDTLS_OID_KEY_USAGE ),
|
|
|
|
1, c, (size_t)ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
int mbedtls_x509write_crt_set_ns_cert_type( mbedtls_x509write_cert *ctx,
|
2013-09-16 11:49:26 +00:00
|
|
|
unsigned char ns_cert_type )
|
|
|
|
{
|
2021-05-06 19:11:30 +00:00
|
|
|
unsigned char buf[4] = {0};
|
2013-09-16 11:49:26 +00:00
|
|
|
unsigned char *c;
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
c = buf + 4;
|
|
|
|
|
2018-09-26 09:48:24 +00:00
|
|
|
ret = mbedtls_asn1_write_named_bitstring( &c, buf, &ns_cert_type, 8 );
|
|
|
|
if( ret < 3 || ret > 4 )
|
2013-09-16 11:49:26 +00:00
|
|
|
return( ret );
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
ret = mbedtls_x509write_crt_set_extension( ctx, MBEDTLS_OID_NS_CERT_TYPE,
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_OID_SIZE( MBEDTLS_OID_NS_CERT_TYPE ),
|
|
|
|
0, c, (size_t)ret );
|
2013-09-16 11:49:26 +00:00
|
|
|
if( ret != 0 )
|
|
|
|
return( ret );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int x509_write_time( unsigned char **p, unsigned char *start,
|
2017-04-26 14:01:23 +00:00
|
|
|
const char *t, size_t size )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 11:49:26 +00:00
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
/*
|
2015-04-08 10:49:31 +00:00
|
|
|
* write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
2017-04-26 14:01:23 +00:00
|
|
|
if( t[0] == '2' && t[1] == '0' && t[2] < '5' )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
|
2017-04-26 14:01:23 +00:00
|
|
|
(const unsigned char *) t + 2,
|
2013-09-16 11:49:26 +00:00
|
|
|
size - 2 ) );
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
|
|
|
|
MBEDTLS_ASN1_UTC_TIME ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_raw_buffer( p, start,
|
2017-04-26 14:01:23 +00:00
|
|
|
(const unsigned char *) t,
|
2013-09-16 11:49:26 +00:00
|
|
|
size ) );
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( p, start, len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( p, start,
|
|
|
|
MBEDTLS_ASN1_GENERALIZED_TIME ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
int mbedtls_x509write_crt_der( mbedtls_x509write_cert *ctx,
|
|
|
|
unsigned char *buf, size_t size,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 11:49:26 +00:00
|
|
|
const char *sig_oid;
|
|
|
|
size_t sig_oid_len = 0;
|
|
|
|
unsigned char *c, *c2;
|
|
|
|
unsigned char hash[64];
|
2019-11-08 18:21:51 +00:00
|
|
|
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
|
2013-09-16 11:49:26 +00:00
|
|
|
size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
|
|
|
|
size_t len = 0;
|
2015-04-08 10:49:31 +00:00
|
|
|
mbedtls_pk_type_t pk_alg;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
2019-05-04 06:54:36 +00:00
|
|
|
* Prepare data to be signed at the end of the target buffer
|
2013-09-16 11:49:26 +00:00
|
|
|
*/
|
2019-05-04 06:54:36 +00:00
|
|
|
c = buf + size;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/* Signature algorithm needed in TBS, and later for actual signature */
|
2017-09-13 07:45:48 +00:00
|
|
|
|
|
|
|
/* There's no direct way of extracting a signature algorithm
|
|
|
|
* (represented as an element of mbedtls_pk_type_t) from a PK instance. */
|
|
|
|
if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_RSA ) )
|
|
|
|
pk_alg = MBEDTLS_PK_RSA;
|
|
|
|
else if( mbedtls_pk_can_do( ctx->issuer_key, MBEDTLS_PK_ECDSA ) )
|
2015-04-08 10:49:31 +00:00
|
|
|
pk_alg = MBEDTLS_PK_ECDSA;
|
2017-09-13 07:45:48 +00:00
|
|
|
else
|
2017-09-22 15:05:43 +00:00
|
|
|
return( MBEDTLS_ERR_X509_INVALID_ALG );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_oid_get_oid_by_sig_alg( pk_alg, ctx->md_alg,
|
2017-09-13 14:39:59 +00:00
|
|
|
&sig_oid, &sig_oid_len ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
|
|
|
|
*/
|
2017-09-13 11:00:15 +00:00
|
|
|
|
|
|
|
/* Only for v3 */
|
2017-09-22 14:40:01 +00:00
|
|
|
if( ctx->version == MBEDTLS_X509_CRT_VERSION_3 )
|
2017-09-13 11:00:15 +00:00
|
|
|
{
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
|
|
|
mbedtls_x509_write_extensions( &c,
|
2019-05-04 06:54:36 +00:00
|
|
|
buf, ctx->extensions ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
2019-05-04 06:54:36 +00:00
|
|
|
mbedtls_asn1_write_tag( &c, buf,
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
2019-05-04 06:54:36 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
2019-05-04 06:54:36 +00:00
|
|
|
mbedtls_asn1_write_tag( &c, buf,
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC |
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | 3 ) );
|
2017-09-13 11:00:15 +00:00
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* SubjectPublicKeyInfo
|
|
|
|
*/
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( pub_len,
|
|
|
|
mbedtls_pk_write_pubkey_der( ctx->subject_key,
|
2019-05-04 06:54:36 +00:00
|
|
|
buf, c - buf ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
c -= pub_len;
|
|
|
|
len += pub_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Subject ::= Name
|
|
|
|
*/
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
2019-05-04 06:54:36 +00:00
|
|
|
mbedtls_x509_write_names( &c, buf,
|
2019-05-04 06:37:58 +00:00
|
|
|
ctx->subject ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Validity ::= SEQUENCE {
|
|
|
|
* notBefore Time,
|
|
|
|
* notAfter Time }
|
|
|
|
*/
|
|
|
|
sub_len = 0;
|
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( sub_len,
|
2019-05-04 06:54:36 +00:00
|
|
|
x509_write_time( &c, buf, ctx->not_after,
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( sub_len,
|
2019-05-04 06:54:36 +00:00
|
|
|
x509_write_time( &c, buf, ctx->not_before,
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_X509_RFC5280_UTC_TIME_LEN ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
len += sub_len;
|
2019-05-04 06:54:36 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, sub_len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
2019-05-04 06:54:36 +00:00
|
|
|
mbedtls_asn1_write_tag( &c, buf,
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Issuer ::= Name
|
|
|
|
*/
|
2019-05-04 06:54:36 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_x509_write_names( &c, buf,
|
2019-05-04 06:37:58 +00:00
|
|
|
ctx->issuer ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Signature ::= AlgorithmIdentifier
|
|
|
|
*/
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
2019-05-04 06:54:36 +00:00
|
|
|
mbedtls_asn1_write_algorithm_identifier( &c, buf,
|
2019-05-04 06:37:58 +00:00
|
|
|
sig_oid, strlen( sig_oid ), 0 ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Serial ::= INTEGER
|
|
|
|
*/
|
2019-05-04 06:54:36 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_mpi( &c, buf,
|
2019-05-04 06:37:58 +00:00
|
|
|
&ctx->serial ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0), v2(1), v3(2) }
|
|
|
|
*/
|
2017-09-13 10:59:26 +00:00
|
|
|
|
|
|
|
/* Can be omitted for v1 */
|
2017-09-22 14:40:01 +00:00
|
|
|
if( ctx->version != MBEDTLS_X509_CRT_VERSION_1 )
|
2017-09-13 10:59:26 +00:00
|
|
|
{
|
|
|
|
sub_len = 0;
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( sub_len,
|
2019-05-04 06:54:36 +00:00
|
|
|
mbedtls_asn1_write_int( &c, buf, ctx->version ) );
|
2017-09-13 10:59:26 +00:00
|
|
|
len += sub_len;
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
2019-05-04 06:54:36 +00:00
|
|
|
mbedtls_asn1_write_len( &c, buf, sub_len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
2019-05-04 06:54:36 +00:00
|
|
|
mbedtls_asn1_write_tag( &c, buf,
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC |
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | 0 ) );
|
2017-09-13 10:59:26 +00:00
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-05-04 06:54:36 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len,
|
2019-05-04 06:54:36 +00:00
|
|
|
mbedtls_asn1_write_tag( &c, buf, MBEDTLS_ASN1_CONSTRUCTED |
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make signature
|
|
|
|
*/
|
2019-05-04 06:54:36 +00:00
|
|
|
|
|
|
|
/* Compute hash of CRT. */
|
2017-06-28 10:07:30 +00:00
|
|
|
if( ( ret = mbedtls_md( mbedtls_md_info_from_type( ctx->md_alg ), c,
|
|
|
|
len, hash ) ) != 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-05-04 06:37:58 +00:00
|
|
|
if( ( ret = mbedtls_pk_sign( ctx->issuer_key, ctx->md_alg,
|
2021-06-21 22:09:00 +00:00
|
|
|
hash, 0, sig, sizeof( sig ), &sig_len,
|
2019-05-04 06:37:58 +00:00
|
|
|
f_rng, p_rng ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2019-05-04 06:54:36 +00:00
|
|
|
/* Move CRT to the front of the buffer to have space
|
|
|
|
* for the signature. */
|
|
|
|
memmove( buf, c, len );
|
|
|
|
c = buf + len;
|
|
|
|
|
|
|
|
/* Add signature at the end of the buffer,
|
|
|
|
* making sure that it doesn't underflow
|
|
|
|
* into the CRT buffer. */
|
2013-09-16 11:49:26 +00:00
|
|
|
c2 = buf + size;
|
2019-05-04 06:54:36 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( sig_and_oid_len, mbedtls_x509_write_sig( &c2, c,
|
2013-09-16 11:49:26 +00:00
|
|
|
sig_oid, sig_oid_len, sig, sig_len ) );
|
|
|
|
|
2019-05-04 06:54:36 +00:00
|
|
|
/*
|
|
|
|
* Memory layout after this step:
|
|
|
|
*
|
|
|
|
* buf c=buf+len c2 buf+size
|
|
|
|
* [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
|
|
|
|
*/
|
2016-09-02 14:23:48 +00:00
|
|
|
|
2019-05-04 06:54:36 +00:00
|
|
|
/* Move raw CRT to just before the signature. */
|
|
|
|
c = c2 - len;
|
|
|
|
memmove( c, buf, len );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
|
|
|
len += sig_and_oid_len;
|
2019-05-04 06:54:36 +00:00
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_len( &c, buf, len ) );
|
|
|
|
MBEDTLS_ASN1_CHK_ADD( len, mbedtls_asn1_write_tag( &c, buf,
|
2019-05-04 06:37:58 +00:00
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
2015-04-08 10:49:31 +00:00
|
|
|
MBEDTLS_ASN1_SEQUENCE ) );
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2013-10-11 16:58:55 +00:00
|
|
|
return( (int) len );
|
2013-09-16 11:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
|
|
|
|
#define PEM_END_CRT "-----END CERTIFICATE-----\n"
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
2019-05-04 06:37:58 +00:00
|
|
|
int mbedtls_x509write_crt_pem( mbedtls_x509write_cert *crt,
|
|
|
|
unsigned char *buf, size_t size,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
2019-12-16 11:46:15 +00:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-05-04 07:13:23 +00:00
|
|
|
size_t olen;
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2019-05-04 07:13:23 +00:00
|
|
|
if( ( ret = mbedtls_x509write_crt_der( crt, buf, size,
|
2013-09-16 11:49:26 +00:00
|
|
|
f_rng, p_rng ) ) < 0 )
|
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
if( ( ret = mbedtls_pem_write_buffer( PEM_BEGIN_CRT, PEM_END_CRT,
|
2019-05-04 07:13:23 +00:00
|
|
|
buf + size - ret, ret,
|
|
|
|
buf, size, &olen ) ) != 0 )
|
2013-09-16 11:49:26 +00:00
|
|
|
{
|
|
|
|
return( ret );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_PEM_WRITE_C */
|
2013-09-16 11:49:26 +00:00
|
|
|
|
2015-04-08 10:49:31 +00:00
|
|
|
#endif /* MBEDTLS_X509_CRT_WRITE_C */
|