2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* X.509 certificate writing
|
|
|
|
*
|
2020-09-05 12:53:20 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2024-01-30 14:09:13 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2018-02-08 19:04:43 +01: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
|
|
|
|
*/
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#include "common.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_CRT_WRITE_C)
|
|
|
|
|
|
|
|
#include "mbedtls/x509_crt.h"
|
2023-09-25 05:04:06 +02:00
|
|
|
#include "x509_internal.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
#include "mbedtls/asn1write.h"
|
2021-12-21 12:54:05 +01:00
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2023-09-25 05:04:06 +02:00
|
|
|
#include "mbedtls/platform.h"
|
2018-06-07 16:25:01 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2023-09-25 05:04:06 +02:00
|
|
|
#include "mbedtls/md.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
2023-09-25 05:04:06 +02:00
|
|
|
#include <stdint.h>
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
|
|
|
#include "mbedtls/pem.h"
|
|
|
|
#endif /* MBEDTLS_PEM_WRITE_C */
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include "psa_util_internal.h"
|
|
|
|
#include "mbedtls/psa_util.h"
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_x509write_crt_init(mbedtls_x509write_cert *ctx)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
memset(ctx, 0, sizeof(mbedtls_x509write_cert));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
ctx->version = MBEDTLS_X509_CRT_VERSION_3;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_x509write_crt_free(mbedtls_x509write_cert *ctx)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2024-08-31 15:25:10 +02:00
|
|
|
if (ctx == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02: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);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_cert));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_x509write_crt_set_version(mbedtls_x509write_cert *ctx,
|
|
|
|
int version)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
ctx->version = version;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_x509write_crt_set_md_alg(mbedtls_x509write_cert *ctx,
|
|
|
|
mbedtls_md_type_t md_alg)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
ctx->md_alg = md_alg;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_x509write_crt_set_subject_key(mbedtls_x509write_cert *ctx,
|
|
|
|
mbedtls_pk_context *key)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
ctx->subject_key = key;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
void mbedtls_x509write_crt_set_issuer_key(mbedtls_x509write_cert *ctx,
|
|
|
|
mbedtls_pk_context *key)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
ctx->issuer_key = key;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509write_crt_set_subject_name(mbedtls_x509write_cert *ctx,
|
|
|
|
const char *subject_name)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509write_crt_set_issuer_name(mbedtls_x509write_cert *ctx,
|
|
|
|
const char *issuer_name)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_x509_string_to_names(&ctx->issuer, issuer_name);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_BIGNUM_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509write_crt_set_serial(mbedtls_x509write_cert *ctx,
|
|
|
|
const mbedtls_mpi *serial)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-09-25 05:04:06 +02:00
|
|
|
int ret;
|
|
|
|
size_t tmp_len;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
/* Ensure that the MPI value fits into the buffer */
|
|
|
|
tmp_len = mbedtls_mpi_size(serial);
|
|
|
|
if (tmp_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
|
2023-04-18 10:38:24 +02:00
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
ctx->serial_len = tmp_len;
|
|
|
|
|
|
|
|
ret = mbedtls_mpi_write_binary(serial, ctx->serial, tmp_len);
|
|
|
|
if (ret < 0) {
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif // MBEDTLS_BIGNUM_C && !MBEDTLS_DEPRECATED_REMOVED
|
|
|
|
|
|
|
|
int mbedtls_x509write_crt_set_serial_raw(mbedtls_x509write_cert *ctx,
|
|
|
|
unsigned char *serial, size_t serial_len)
|
|
|
|
{
|
|
|
|
if (serial_len > MBEDTLS_X509_RFC5280_MAX_SERIAL_LEN) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx->serial_len = serial_len;
|
|
|
|
memcpy(ctx->serial, serial, serial_len);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509write_crt_set_validity(mbedtls_x509write_cert *ctx,
|
|
|
|
const char *not_before,
|
|
|
|
const char *not_after)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (strlen(not_before) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1 ||
|
|
|
|
strlen(not_after) != MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2023-04-18 10:38:24 +02: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);
|
2018-02-08 19:04:43 +01:00
|
|
|
ctx->not_before[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
|
|
|
|
ctx->not_after[MBEDTLS_X509_RFC5280_UTC_TIME_LEN - 1] = 'Z';
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
int mbedtls_x509write_crt_set_subject_alternative_name(mbedtls_x509write_cert *ctx,
|
|
|
|
const mbedtls_x509_san_list *san_list)
|
|
|
|
{
|
|
|
|
return mbedtls_x509_write_set_san_common(&ctx->extensions, san_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509write_crt_set_extension(mbedtls_x509write_cert *ctx,
|
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
int critical,
|
|
|
|
const unsigned char *val, size_t val_len)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
|
|
|
|
critical, val, val_len);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509write_crt_set_basic_constraints(mbedtls_x509write_cert *ctx,
|
|
|
|
int is_ca, int max_pathlen)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
unsigned char buf[9];
|
|
|
|
unsigned char *c = buf + sizeof(buf);
|
|
|
|
size_t len = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
memset(buf, 0, sizeof(buf));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (is_ca && max_pathlen > 127) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (is_ca) {
|
|
|
|
if (max_pathlen >= 0) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf,
|
|
|
|
max_pathlen));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_bool(&c, buf, 1));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return
|
|
|
|
mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_BASIC_CONSTRAINTS,
|
|
|
|
MBEDTLS_OID_SIZE(MBEDTLS_OID_BASIC_CONSTRAINTS),
|
|
|
|
is_ca, buf + sizeof(buf) - len, len);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA1)
|
|
|
|
static int mbedtls_x509write_crt_set_key_identifier(mbedtls_x509write_cert *ctx,
|
|
|
|
int is_ca,
|
|
|
|
unsigned char tag)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
unsigned char buf[MBEDTLS_MPI_MAX_SIZE * 2 + 20]; /* tag, length + 2xMPI */
|
|
|
|
unsigned char *c = buf + sizeof(buf);
|
|
|
|
size_t len = 0;
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t hash_length;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
2023-09-25 05:04:06 +02:00
|
|
|
mbedtls_pk_write_pubkey(&c,
|
|
|
|
buf,
|
|
|
|
is_ca ?
|
|
|
|
ctx->issuer_key :
|
|
|
|
ctx->subject_key));
|
|
|
|
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
status = psa_hash_compute(PSA_ALG_SHA_1,
|
|
|
|
buf + sizeof(buf) - len,
|
|
|
|
len,
|
|
|
|
buf + sizeof(buf) - 20,
|
|
|
|
20,
|
|
|
|
&hash_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
ret = mbedtls_md(mbedtls_md_info_from_type(MBEDTLS_MD_SHA1),
|
|
|
|
buf + sizeof(buf) - len, len,
|
|
|
|
buf + sizeof(buf) - 20);
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
c = buf + sizeof(buf) - 20;
|
2018-02-08 19:04:43 +01:00
|
|
|
len = 20;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, tag));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
if (is_ca) { // writes AuthorityKeyIdentifier sequence
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_tag(&c,
|
|
|
|
buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
if (is_ca) {
|
|
|
|
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);
|
|
|
|
} else {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
int mbedtls_x509write_crt_set_subject_key_identifier(mbedtls_x509write_cert *ctx)
|
|
|
|
{
|
|
|
|
return mbedtls_x509write_crt_set_key_identifier(ctx,
|
|
|
|
0,
|
|
|
|
MBEDTLS_ASN1_OCTET_STRING);
|
|
|
|
}
|
2020-09-05 12:53:20 +02:00
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
int mbedtls_x509write_crt_set_authority_key_identifier(mbedtls_x509write_cert *ctx)
|
|
|
|
{
|
|
|
|
return mbedtls_x509write_crt_set_key_identifier(ctx,
|
|
|
|
1,
|
|
|
|
(MBEDTLS_ASN1_CONTEXT_SPECIFIC | 0));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif /* MBEDTLS_MD_CAN_SHA1 */
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509write_crt_set_key_usage(mbedtls_x509write_cert *ctx,
|
|
|
|
unsigned int key_usage)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
unsigned char buf[5] = { 0 }, ku[2] = { 0 };
|
2018-02-08 19:04:43 +01:00
|
|
|
unsigned char *c;
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-07-10 16:15:01 +02:00
|
|
|
const unsigned int allowed_bits = MBEDTLS_X509_KU_DIGITAL_SIGNATURE |
|
2023-04-18 10:38:24 +02:00
|
|
|
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;
|
2019-07-10 16:15:01 +02:00
|
|
|
|
|
|
|
/* Check that nothing other than the allowed flags is set */
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((key_usage & ~allowed_bits) != 0) {
|
|
|
|
return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
c = buf + 5;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_PUT_UINT16_LE(key_usage, ku, 0);
|
|
|
|
ret = mbedtls_asn1_write_named_bitstring(&c, buf, ku, 9);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
} else if (ret < 3 || ret > 5) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_FORMAT;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
|
|
|
|
MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
|
|
|
|
1, c, (size_t) ret);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-09-25 05:04:06 +02:00
|
|
|
int mbedtls_x509write_crt_set_ext_key_usage(mbedtls_x509write_cert *ctx,
|
|
|
|
const mbedtls_asn1_sequence *exts)
|
|
|
|
{
|
|
|
|
unsigned char buf[256];
|
|
|
|
unsigned char *c = buf + sizeof(buf);
|
|
|
|
int ret;
|
|
|
|
size_t len = 0;
|
|
|
|
const mbedtls_asn1_sequence *last_ext = NULL;
|
|
|
|
const mbedtls_asn1_sequence *ext;
|
|
|
|
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
|
|
|
|
/* We need at least one extension: SEQUENCE SIZE (1..MAX) OF KeyPurposeId */
|
|
|
|
if (exts == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Iterate over exts backwards, so we write them out in the requested order */
|
|
|
|
while (last_ext != exts) {
|
|
|
|
for (ext = exts; ext->next != last_ext; ext = ext->next) {
|
|
|
|
}
|
|
|
|
if (ext->buf.tag != MBEDTLS_ASN1_OID) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf, ext->buf.p, ext->buf.len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, ext->buf.len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_OID));
|
|
|
|
last_ext = ext;
|
|
|
|
}
|
|
|
|
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
|
|
|
|
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_EXTENDED_KEY_USAGE,
|
|
|
|
MBEDTLS_OID_SIZE(MBEDTLS_OID_EXTENDED_KEY_USAGE),
|
|
|
|
1, c, len);
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509write_crt_set_ns_cert_type(mbedtls_x509write_cert *ctx,
|
|
|
|
unsigned char ns_cert_type)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
unsigned char buf[4] = { 0 };
|
2018-02-08 19:04:43 +01:00
|
|
|
unsigned char *c;
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
c = buf + 4;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
|
|
|
|
if (ret < 3 || ret > 4) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_x509write_crt_set_extension(ctx, MBEDTLS_OID_NS_CERT_TYPE,
|
|
|
|
MBEDTLS_OID_SIZE(MBEDTLS_OID_NS_CERT_TYPE),
|
|
|
|
0, c, (size_t) ret);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
static int x509_write_time(unsigned char **p, unsigned char *start,
|
|
|
|
const char *t, size_t size)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* write MBEDTLS_ASN1_UTC_TIME if year < 2050 (2 bytes shorter)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (t[0] < '2' || (t[0] == '2' && t[1] == '0' && t[2] < '5')) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
|
|
|
|
(const unsigned char *) t + 2,
|
|
|
|
size - 2));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
|
|
|
|
MBEDTLS_ASN1_UTC_TIME));
|
|
|
|
} else {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(p, start,
|
|
|
|
(const unsigned char *) t,
|
|
|
|
size));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(p, start, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(p, start,
|
|
|
|
MBEDTLS_ASN1_GENERALIZED_TIME));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02: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)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
const char *sig_oid;
|
|
|
|
size_t sig_oid_len = 0;
|
|
|
|
unsigned char *c, *c2;
|
2021-12-21 12:54:05 +01:00
|
|
|
unsigned char sig[MBEDTLS_PK_SIGNATURE_MAX_SIZE];
|
2023-09-25 05:04:06 +02:00
|
|
|
size_t hash_length = 0;
|
|
|
|
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
|
|
|
|
psa_algorithm_t psa_algorithm;
|
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
size_t sub_len = 0, pub_len = 0, sig_and_oid_len = 0, sig_len;
|
|
|
|
size_t len = 0;
|
|
|
|
mbedtls_pk_type_t pk_alg;
|
2023-10-21 14:06:23 +02:00
|
|
|
int write_sig_null_par;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
2020-09-05 12:53:20 +02:00
|
|
|
* Prepare data to be signed at the end of the target buffer
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2020-09-05 12:53:20 +02:00
|
|
|
c = buf + size;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Signature algorithm needed in TBS, and later for actual signature */
|
|
|
|
|
|
|
|
/* There's no direct way of extracting a signature algorithm
|
|
|
|
* (represented as an element of mbedtls_pk_type_t) from a PK instance. */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_RSA)) {
|
2018-02-08 19:04:43 +01:00
|
|
|
pk_alg = MBEDTLS_PK_RSA;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else if (mbedtls_pk_can_do(ctx->issuer_key, MBEDTLS_PK_ECDSA)) {
|
2018-02-08 19:04:43 +01:00
|
|
|
pk_alg = MBEDTLS_PK_ECDSA;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_ALG;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
|
|
|
|
&sig_oid, &sig_oid_len)) != 0) {
|
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Only for v3 */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ctx->version == MBEDTLS_X509_CRT_VERSION_3) {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_x509_write_extensions(&c,
|
|
|
|
buf, ctx->extensions));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_tag(&c, buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_tag(&c, buf,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC |
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | 3));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SubjectPublicKeyInfo
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(pub_len,
|
|
|
|
mbedtls_pk_write_pubkey_der(ctx->subject_key,
|
2023-09-25 05:04:06 +02:00
|
|
|
buf, (size_t) (c - buf)));
|
2018-02-08 19:04:43 +01:00
|
|
|
c -= pub_len;
|
|
|
|
len += pub_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Subject ::= Name
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_x509_write_names(&c, buf,
|
|
|
|
ctx->subject));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Validity ::= SEQUENCE {
|
|
|
|
* notBefore Time,
|
|
|
|
* notAfter Time }
|
|
|
|
*/
|
|
|
|
sub_len = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(sub_len,
|
|
|
|
x509_write_time(&c, buf, ctx->not_after,
|
|
|
|
MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(sub_len,
|
|
|
|
x509_write_time(&c, buf, ctx->not_before,
|
|
|
|
MBEDTLS_X509_RFC5280_UTC_TIME_LEN));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
len += sub_len;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, sub_len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_tag(&c, buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Issuer ::= Name
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
|
|
|
|
ctx->issuer));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Signature ::= AlgorithmIdentifier
|
|
|
|
*/
|
2023-10-21 14:06:23 +02:00
|
|
|
if (pk_alg == MBEDTLS_PK_ECDSA) {
|
|
|
|
/*
|
|
|
|
* The AlgorithmIdentifier's parameters field must be absent for DSA/ECDSA signature
|
|
|
|
* algorithms, see https://www.rfc-editor.org/rfc/rfc5480#page-17 and
|
|
|
|
* https://www.rfc-editor.org/rfc/rfc5758#section-3.
|
|
|
|
*/
|
|
|
|
write_sig_null_par = 0;
|
|
|
|
} else {
|
|
|
|
write_sig_null_par = 1;
|
|
|
|
}
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
2023-10-21 14:06:23 +02:00
|
|
|
mbedtls_asn1_write_algorithm_identifier_ext(&c, buf,
|
|
|
|
sig_oid, strlen(sig_oid),
|
|
|
|
0, write_sig_null_par));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Serial ::= INTEGER
|
2023-09-25 05:04:06 +02:00
|
|
|
*
|
|
|
|
* Written data is:
|
|
|
|
* - "ctx->serial_len" bytes for the raw serial buffer
|
|
|
|
* - if MSb of "serial" is 1, then prepend an extra 0x00 byte
|
|
|
|
* - 1 byte for the length
|
|
|
|
* - 1 byte for the TAG
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-09-25 05:04:06 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_raw_buffer(&c, buf,
|
|
|
|
ctx->serial, ctx->serial_len));
|
|
|
|
if (*c & 0x80) {
|
|
|
|
if (c - buf < 1) {
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
*(--c) = 0x0;
|
|
|
|
len++;
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
|
|
|
|
ctx->serial_len + 1));
|
|
|
|
} else {
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf,
|
|
|
|
ctx->serial_len));
|
|
|
|
}
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
|
|
|
|
MBEDTLS_ASN1_INTEGER));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0), v2(1), v3(2) }
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Can be omitted for v1 */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ctx->version != MBEDTLS_X509_CRT_VERSION_1) {
|
2018-02-08 19:04:43 +01:00
|
|
|
sub_len = 0;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(sub_len,
|
|
|
|
mbedtls_asn1_write_int(&c, buf, ctx->version));
|
2018-02-08 19:04:43 +01:00
|
|
|
len += sub_len;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_len(&c, buf, sub_len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_tag(&c, buf,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC |
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | 0));
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_tag(&c, buf, MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make signature
|
|
|
|
*/
|
2020-09-05 12:53:20 +02:00
|
|
|
|
|
|
|
/* Compute hash of CRT. */
|
2023-09-25 05:04:06 +02:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
psa_algorithm = mbedtls_md_psa_alg_from_type(ctx->md_alg);
|
|
|
|
|
|
|
|
status = psa_hash_compute(psa_algorithm,
|
|
|
|
c,
|
|
|
|
len,
|
|
|
|
hash,
|
|
|
|
sizeof(hash),
|
|
|
|
&hash_length);
|
|
|
|
if (status != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
|
|
|
}
|
|
|
|
#else
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c,
|
|
|
|
len, hash)) != 0) {
|
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2023-09-25 05:04:06 +02:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_pk_sign(ctx->issuer_key, ctx->md_alg,
|
2023-09-25 05:04:06 +02:00
|
|
|
hash, hash_length, sig, sizeof(sig), &sig_len,
|
2023-04-18 10:38:24 +02:00
|
|
|
f_rng, p_rng)) != 0) {
|
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2020-09-05 12:53:20 +02:00
|
|
|
/* Move CRT to the front of the buffer to have space
|
|
|
|
* for the signature. */
|
2023-04-18 10:38:24 +02:00
|
|
|
memmove(buf, c, len);
|
2020-09-05 12:53:20 +02:00
|
|
|
c = buf + len;
|
|
|
|
|
|
|
|
/* Add signature at the end of the buffer,
|
|
|
|
* making sure that it doesn't underflow
|
|
|
|
* into the CRT buffer. */
|
2018-02-08 19:04:43 +01:00
|
|
|
c2 = buf + size;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len, mbedtls_x509_write_sig(&c2, c,
|
2023-10-21 14:06:23 +02:00
|
|
|
sig_oid, sig_oid_len,
|
|
|
|
sig, sig_len, pk_alg));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2020-09-05 12:53:20 +02:00
|
|
|
/*
|
|
|
|
* Memory layout after this step:
|
|
|
|
*
|
|
|
|
* buf c=buf+len c2 buf+size
|
|
|
|
* [CRT0,...,CRTn, UNUSED, ..., UNUSED, SIG0, ..., SIGm]
|
|
|
|
*/
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2020-09-05 12:53:20 +02:00
|
|
|
/* Move raw CRT to just before the signature. */
|
|
|
|
c = c2 - len;
|
2023-04-18 10:38:24 +02:00
|
|
|
memmove(c, buf, len);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
len += sig_and_oid_len;
|
2023-04-18 10:38:24 +02:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c, buf, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&c, buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) len;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define PEM_BEGIN_CRT "-----BEGIN CERTIFICATE-----\n"
|
|
|
|
#define PEM_END_CRT "-----END CERTIFICATE-----\n"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
2023-04-18 10:38:24 +02: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)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-09-05 12:53:20 +02:00
|
|
|
size_t olen;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_x509write_crt_der(crt, buf, size,
|
|
|
|
f_rng, p_rng)) < 0) {
|
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CRT, PEM_END_CRT,
|
|
|
|
buf + size - ret, ret,
|
|
|
|
buf, size, &olen)) != 0) {
|
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_PEM_WRITE_C */
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_X509_CRT_WRITE_C */
|