2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* X.509 Certificate Signing Request writing
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2015-09-04 14:21:07 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02: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 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
2015-09-04 14:21:07 +02: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 13:49:26 +02:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* References:
|
|
|
|
* - CSRs: PKCS#10 v1.7 aka RFC 2986
|
|
|
|
* - attributes: PKCS#9 v2.0 aka RFC 2985
|
|
|
|
*/
|
|
|
|
|
2020-06-03 01:43:33 +02:00
|
|
|
#include "common.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_CSR_WRITE_C)
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-02-24 13:12:55 +01:00
|
|
|
#include "mbedtls/x509.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509_csr.h"
|
|
|
|
#include "mbedtls/asn1write.h"
|
2019-12-18 16:07:04 +01:00
|
|
|
#include "mbedtls/error.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2018-10-31 11:18:39 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
#include "psa/crypto.h"
|
|
|
|
#include "mbedtls/psa_util.h"
|
2023-03-28 12:33:20 +02:00
|
|
|
#include "md_psa.h"
|
2022-02-07 14:40:55 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2018-10-31 11:18:39 +01:00
|
|
|
|
2015-02-06 14:43:58 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pem.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
|
|
|
|
2018-12-14 20:08:35 +01:00
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509write_csr_init(mbedtls_x509write_csr *ctx)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(ctx, 0, sizeof(mbedtls_x509write_csr));
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509write_csr_free(mbedtls_x509write_csr *ctx)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_free_named_data_list(&ctx->subject);
|
|
|
|
mbedtls_asn1_free_named_data_list(&ctx->extensions);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(ctx, sizeof(mbedtls_x509write_csr));
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509write_csr_set_md_alg(mbedtls_x509write_csr *ctx, mbedtls_md_type_t md_alg)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
ctx->md_alg = md_alg;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509write_csr_set_key(mbedtls_x509write_csr *ctx, mbedtls_pk_context *key)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
ctx->key = key;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509write_csr_set_subject_name(mbedtls_x509write_csr *ctx,
|
|
|
|
const char *subject_name)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_x509_string_to_names(&ctx->subject, subject_name);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509write_csr_set_extension(mbedtls_x509write_csr *ctx,
|
|
|
|
const char *oid, size_t oid_len,
|
|
|
|
int critical,
|
|
|
|
const unsigned char *val, size_t val_len)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_x509_set_extension(&ctx->extensions, oid, oid_len,
|
|
|
|
critical, val, val_len);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2022-12-28 18:38:53 +01:00
|
|
|
int mbedtls_x509write_csr_set_subject_alternative_name(mbedtls_x509write_csr *ctx,
|
|
|
|
const mbedtls_x509_san_list *san_list)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
2023-03-09 08:18:30 +01:00
|
|
|
const mbedtls_x509_san_list *cur;
|
2022-12-28 18:38:53 +01:00
|
|
|
unsigned char *buf;
|
|
|
|
unsigned char *p;
|
|
|
|
size_t len;
|
|
|
|
size_t buflen = 0;
|
|
|
|
|
|
|
|
/* Determine the maximum size of the SubjectAltName list */
|
2023-03-10 12:40:41 +01:00
|
|
|
for (cur = san_list; cur != NULL; cur = cur->next) {
|
2023-02-24 13:12:55 +01:00
|
|
|
/* Calculate size of the required buffer */
|
2023-02-24 14:03:30 +01:00
|
|
|
switch (cur->node.type) {
|
2023-02-24 13:12:55 +01:00
|
|
|
case MBEDTLS_X509_SAN_DNS_NAME:
|
|
|
|
case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
|
|
|
|
case MBEDTLS_X509_SAN_IP_ADDRESS:
|
2023-03-09 08:18:30 +01:00
|
|
|
/* length of value for each name entry,
|
|
|
|
* maximum 4 bytes for the length field,
|
|
|
|
* 1 byte for the tag/type.
|
2023-02-24 13:12:55 +01:00
|
|
|
*/
|
|
|
|
buflen += cur->node.san.unstructured_name.len + 4 + 1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Not supported - skip. */
|
|
|
|
break;
|
2022-12-28 18:38:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the extra length field and tag */
|
|
|
|
buflen += 4 + 1;
|
|
|
|
|
|
|
|
/* Allocate buffer */
|
|
|
|
buf = mbedtls_calloc(1, buflen);
|
|
|
|
if (buf == NULL) {
|
|
|
|
return MBEDTLS_ERR_ASN1_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
mbedtls_platform_zeroize(buf, buflen);
|
|
|
|
p = buf + buflen;
|
|
|
|
|
|
|
|
/* Write ASN.1-based structure */
|
|
|
|
cur = san_list;
|
|
|
|
len = 0;
|
|
|
|
while (cur != NULL) {
|
2023-02-14 11:54:37 +01:00
|
|
|
switch (cur->node.type) {
|
|
|
|
case MBEDTLS_X509_SAN_DNS_NAME:
|
|
|
|
case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
|
|
|
|
case MBEDTLS_X509_SAN_IP_ADDRESS:
|
2023-03-10 12:40:41 +01:00
|
|
|
{
|
|
|
|
const unsigned char *unstructured_name =
|
|
|
|
(const unsigned char *) cur->node.san.unstructured_name.p;
|
|
|
|
size_t unstructured_name_len = cur->node.san.unstructured_name.len;
|
|
|
|
|
2023-02-24 14:03:30 +01:00
|
|
|
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
|
2023-03-10 12:40:41 +01:00
|
|
|
mbedtls_asn1_write_raw_buffer(
|
|
|
|
&p, buf,
|
|
|
|
unstructured_name, unstructured_name_len));
|
|
|
|
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(
|
|
|
|
&p, buf, unstructured_name_len));
|
2023-02-24 14:03:30 +01:00
|
|
|
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
|
2023-03-10 12:40:41 +01:00
|
|
|
mbedtls_asn1_write_tag(
|
|
|
|
&p, buf,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | cur->node.type));
|
|
|
|
}
|
|
|
|
break;
|
2023-02-14 11:54:37 +01:00
|
|
|
default:
|
|
|
|
/* Skip unsupported names. */
|
|
|
|
break;
|
|
|
|
}
|
2022-12-28 18:38:53 +01:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:03:30 +01:00
|
|
|
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
|
|
|
|
MBEDTLS_ASN1_CHK_CLEANUP_ADD(len,
|
|
|
|
mbedtls_asn1_write_tag(&p, buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
MBEDTLS_ASN1_SEQUENCE));
|
2022-12-28 18:38:53 +01:00
|
|
|
|
|
|
|
ret = mbedtls_x509write_csr_set_extension(
|
|
|
|
ctx,
|
|
|
|
MBEDTLS_OID_SUBJECT_ALT_NAME,
|
|
|
|
MBEDTLS_OID_SIZE(MBEDTLS_OID_SUBJECT_ALT_NAME),
|
|
|
|
0,
|
|
|
|
buf + buflen - len,
|
|
|
|
len);
|
|
|
|
|
2023-03-07 15:43:38 +01:00
|
|
|
/* If we exceeded the allocated buffer it means that maximum size of the SubjectAltName list
|
|
|
|
* was incorrectly calculated and memory is corrupted. */
|
2023-03-10 12:40:41 +01:00
|
|
|
if (p < buf) {
|
2023-03-07 15:43:38 +01:00
|
|
|
ret = MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
|
|
|
|
}
|
|
|
|
|
2023-02-24 14:03:30 +01:00
|
|
|
cleanup:
|
2022-12-28 18:38:53 +01:00
|
|
|
mbedtls_free(buf);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509write_csr_set_key_usage(mbedtls_x509write_csr *ctx, unsigned char key_usage)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char buf[4] = { 0 };
|
2013-09-16 13:49:26 +02:00
|
|
|
unsigned char *c;
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
c = buf + 4;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_write_named_bitstring(&c, buf, &key_usage, 8);
|
|
|
|
if (ret < 3 || ret > 4) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509write_csr_set_extension(ctx, MBEDTLS_OID_KEY_USAGE,
|
|
|
|
MBEDTLS_OID_SIZE(MBEDTLS_OID_KEY_USAGE),
|
|
|
|
0, c, (size_t) ret);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509write_csr_set_ns_cert_type(mbedtls_x509write_csr *ctx,
|
|
|
|
unsigned char ns_cert_type)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
unsigned char buf[4] = { 0 };
|
2013-09-16 13:49:26 +02:00
|
|
|
unsigned char *c;
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
c = buf + 4;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_write_named_bitstring(&c, buf, &ns_cert_type, 8);
|
|
|
|
if (ret < 3 || ret > 4) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509write_csr_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;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int x509write_csr_der_internal(mbedtls_x509write_csr *ctx,
|
|
|
|
unsigned char *buf,
|
|
|
|
size_t size,
|
|
|
|
unsigned char *sig, size_t sig_size,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 13:49:26 +02:00
|
|
|
const char *sig_oid;
|
|
|
|
size_t sig_oid_len = 0;
|
|
|
|
unsigned char *c, *c2;
|
2023-03-28 11:20:23 +02:00
|
|
|
unsigned char hash[MBEDTLS_MD_MAX_SIZE];
|
2013-09-16 13:49:26 +02:00
|
|
|
size_t pub_len = 0, sig_and_oid_len = 0, sig_len;
|
|
|
|
size_t len = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_type_t pk_alg;
|
2018-10-31 11:18:39 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
|
|
|
size_t hash_len;
|
2023-03-28 11:38:08 +02:00
|
|
|
psa_algorithm_t hash_alg = mbedtls_md_psa_alg_from_type(ctx->md_alg);
|
2018-10-31 11:18:39 +01:00
|
|
|
#endif /* MBEDTLS_USE_PSA_CRYPTO */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2020-06-26 23:23:32 +02:00
|
|
|
/* Write the CSR backwards starting from the end of buf */
|
2018-12-14 20:08:35 +01:00
|
|
|
c = buf + size;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_extensions(&c, buf,
|
|
|
|
ctx->extensions));
|
|
|
|
|
|
|
|
if (len) {
|
|
|
|
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_CONSTRUCTED | MBEDTLS_ASN1_SET));
|
|
|
|
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_oid(
|
|
|
|
&c, buf, MBEDTLS_OID_PKCS9_CSR_EXT_REQ,
|
|
|
|
MBEDTLS_OID_SIZE(MBEDTLS_OID_PKCS9_CSR_EXT_REQ)));
|
|
|
|
|
|
|
|
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));
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01: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_CONTEXT_SPECIFIC));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(pub_len, mbedtls_pk_write_pubkey_der(ctx->key,
|
|
|
|
buf, c - buf));
|
2013-09-16 13:49:26 +02:00
|
|
|
c -= pub_len;
|
|
|
|
len += pub_len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Subject ::= Name
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_x509_write_names(&c, buf,
|
|
|
|
ctx->subject));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0), v2(1), v3(2) }
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_int(&c, buf, 0));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01: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));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
/*
|
2020-06-26 23:23:32 +02:00
|
|
|
* Sign the written CSR data into the sig buffer
|
2018-10-31 11:18:39 +01:00
|
|
|
* Note: hash errors can happen only after an internal error
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2018-10-31 11:18:39 +01:00
|
|
|
#if defined(MBEDTLS_USE_PSA_CRYPTO)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (psa_hash_compute(hash_alg,
|
|
|
|
c,
|
|
|
|
len,
|
|
|
|
hash,
|
|
|
|
sizeof(hash),
|
|
|
|
&hash_len) != PSA_SUCCESS) {
|
|
|
|
return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
|
2018-10-31 11:18:39 +01:00
|
|
|
}
|
|
|
|
#else /* MBEDTLS_USE_PSA_CRYPTO */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_md(mbedtls_md_info_from_type(ctx->md_alg), c, len, hash);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-10-31 11:18:39 +01:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_pk_sign(ctx->key, ctx->md_alg, hash, 0,
|
|
|
|
sig, sig_size, &sig_len,
|
|
|
|
f_rng, p_rng)) != 0) {
|
|
|
|
return ret;
|
2017-09-13 09:45:48 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_RSA)) {
|
2017-09-13 09:45:48 +02:00
|
|
|
pk_alg = MBEDTLS_PK_RSA;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (mbedtls_pk_can_do(ctx->key, MBEDTLS_PK_ECDSA)) {
|
2015-04-08 12:49:31 +02:00
|
|
|
pk_alg = MBEDTLS_PK_ECDSA;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_ALG;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_oid_get_oid_by_sig_alg(pk_alg, ctx->md_alg,
|
|
|
|
&sig_oid, &sig_oid_len)) != 0) {
|
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-06-26 23:23:32 +02:00
|
|
|
* Move the written CSR data to the start of buf to create space for
|
|
|
|
* writing the signature into buf.
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
memmove(buf, c, len);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2020-06-26 23:23:32 +02:00
|
|
|
/*
|
|
|
|
* Write sig and its OID into buf backwards from the end of buf.
|
|
|
|
* Note: mbedtls_x509_write_sig will check for c2 - ( buf + len ) < sig_len
|
|
|
|
* and return MBEDTLS_ERR_ASN1_BUF_TOO_SMALL if needed.
|
|
|
|
*/
|
2018-12-14 20:08:35 +01:00
|
|
|
c2 = buf + size;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(sig_and_oid_len,
|
|
|
|
mbedtls_x509_write_sig(&c2, buf + len, sig_oid, sig_oid_len,
|
|
|
|
sig, sig_len));
|
2016-09-02 16:23:48 +02:00
|
|
|
|
2020-06-26 23:23:32 +02:00
|
|
|
/*
|
|
|
|
* Compact the space between the CSR data and signature by moving the
|
|
|
|
* CSR data to the start of the signature.
|
|
|
|
*/
|
2013-09-16 13:49:26 +02:00
|
|
|
c2 -= len;
|
2023-01-11 14:50:10 +01:00
|
|
|
memmove(c2, buf, len);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2020-06-26 23:23:32 +02:00
|
|
|
/* ASN encode the total size and tag the CSR data with it. */
|
2013-09-16 13:49:26 +02:00
|
|
|
len += sig_and_oid_len;
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&c2, buf, len));
|
|
|
|
MBEDTLS_ASN1_CHK_ADD(len,
|
|
|
|
mbedtls_asn1_write_tag(
|
|
|
|
&c2, buf,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE));
|
2020-06-26 23:23:32 +02:00
|
|
|
|
|
|
|
/* Zero the unused bytes at the start of buf */
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(buf, 0, c2 - buf);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) len;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509write_csr_der(mbedtls_x509write_csr *ctx, unsigned char *buf,
|
|
|
|
size_t size,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2018-12-14 20:08:35 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
unsigned char *sig;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((sig = mbedtls_calloc(1, MBEDTLS_PK_SIGNATURE_MAX_SIZE)) == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
2018-12-14 20:08:35 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = x509write_csr_der_internal(ctx, buf, size,
|
|
|
|
sig, MBEDTLS_PK_SIGNATURE_MAX_SIZE,
|
|
|
|
f_rng, p_rng);
|
2018-12-14 20:08:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(sig);
|
2018-12-14 20:08:35 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2018-12-14 20:08:35 +01:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
#define PEM_BEGIN_CSR "-----BEGIN CERTIFICATE REQUEST-----\n"
|
|
|
|
#define PEM_END_CSR "-----END CERTIFICATE REQUEST-----\n"
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_WRITE_C)
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509write_csr_pem(mbedtls_x509write_csr *ctx, unsigned char *buf, size_t size,
|
|
|
|
int (*f_rng)(void *, unsigned char *, size_t),
|
|
|
|
void *p_rng)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2013-09-16 13:49:26 +02:00
|
|
|
size_t olen = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509write_csr_der(ctx, buf, size,
|
|
|
|
f_rng, p_rng)) < 0) {
|
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_pem_write_buffer(PEM_BEGIN_CSR, PEM_END_CSR,
|
|
|
|
buf + size - ret,
|
|
|
|
ret, buf, size, &olen)) != 0) {
|
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PEM_WRITE_C */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CSR_WRITE_C */
|