2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* X.509 Certificate Signing Request (CSR) parsing
|
|
|
|
*
|
2020-08-07 13:07:28 +02:00
|
|
|
* Copyright The Mbed TLS Contributors
|
2023-11-02 20:47:20 +01:00
|
|
|
* SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The ITU-T X.509 standard defines a certificate format for PKI.
|
|
|
|
*
|
2014-06-12 22:34:55 +02:00
|
|
|
* http://www.ietf.org/rfc/rfc5280.txt (Certificates and CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc3279.txt (Alg IDs for CRLs)
|
|
|
|
* http://www.ietf.org/rfc/rfc2986.txt (CSRs, aka PKCS#10)
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.680-0207.pdf
|
|
|
|
* http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
|
|
|
|
*/
|
|
|
|
|
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_PARSE_C)
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509_csr.h"
|
2019-12-18 16:07:04 +01:00
|
|
|
#include "mbedtls/error.h"
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/oid.h"
|
2018-04-17 16:51:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2015-02-06 14:43:58 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/pem.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#endif
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO) || defined(EFIX64) || defined(EFI32)
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0) }
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int x509_csr_get_version(unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
int *ver)
|
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
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_int(p, end, ver)) != 0) {
|
|
|
|
if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
2013-09-16 13:49:26 +02:00
|
|
|
*ver = 0;
|
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
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_VERSION, 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
|
|
|
}
|
|
|
|
|
2019-10-29 23:03:37 +01:00
|
|
|
/*
|
|
|
|
* Parse CSR extension requests in DER format
|
|
|
|
*/
|
|
|
|
static int x509_csr_parse_extensions(mbedtls_x509_csr *csr,
|
2023-10-18 13:20:59 +02:00
|
|
|
unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_csr_ext_cb_t cb,
|
|
|
|
void *p_ctx)
|
2019-10-29 23:03:37 +01:00
|
|
|
{
|
2023-10-17 16:02:20 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2019-10-29 23:03:37 +01:00
|
|
|
size_t len;
|
2023-10-18 13:20:59 +02:00
|
|
|
unsigned char *end_ext_data, *end_ext_octet;
|
2023-10-17 12:36:23 +02:00
|
|
|
|
2019-10-29 23:03:37 +01:00
|
|
|
while (*p < end) {
|
|
|
|
mbedtls_x509_buf extn_oid = { 0, 0, NULL };
|
2023-10-17 16:02:20 +02:00
|
|
|
int is_critical = 0; /* DEFAULT FALSE */
|
2019-10-29 23:03:37 +01:00
|
|
|
int ext_type = 0;
|
|
|
|
|
|
|
|
/* Read sequence tag */
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
end_ext_data = *p + len;
|
|
|
|
|
|
|
|
/* Get extension ID */
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &extn_oid.len,
|
|
|
|
MBEDTLS_ASN1_OID)) != 0) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
extn_oid.tag = MBEDTLS_ASN1_OID;
|
|
|
|
extn_oid.p = *p;
|
|
|
|
*p += extn_oid.len;
|
|
|
|
|
2023-10-17 16:02:20 +02:00
|
|
|
/* Get optional critical */
|
|
|
|
if ((ret = mbedtls_asn1_get_bool(p, end_ext_data, &is_critical)) != 0 &&
|
|
|
|
(ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG)) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
2023-10-17 11:50:50 +02:00
|
|
|
|
2019-10-29 23:03:37 +01:00
|
|
|
/* Data should be octet string type */
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end_ext_data, &len,
|
|
|
|
MBEDTLS_ASN1_OCTET_STRING)) != 0) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
2023-01-24 09:45:19 +01:00
|
|
|
|
2023-10-18 13:20:59 +02:00
|
|
|
end_ext_octet = *p + len;
|
|
|
|
|
|
|
|
if (end_ext_octet != end_ext_data) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-12 12:58:02 +01:00
|
|
|
/*
|
2023-01-25 11:08:32 +01:00
|
|
|
* Detect supported extensions and skip unsupported extensions
|
2023-01-12 12:58:02 +01:00
|
|
|
*/
|
|
|
|
ret = mbedtls_oid_get_x509_ext_type(&extn_oid, &ext_type);
|
|
|
|
|
2023-10-18 13:20:59 +02:00
|
|
|
if (ret != 0) {
|
|
|
|
/* Give the callback (if any) a chance to handle the extension */
|
|
|
|
if (cb != NULL) {
|
|
|
|
ret = cb(p_ctx, csr, &extn_oid, is_critical, *p, end_ext_octet);
|
|
|
|
if (ret != 0 && is_critical) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
*p = end_ext_octet;
|
|
|
|
continue;
|
2023-01-25 11:08:32 +01:00
|
|
|
}
|
|
|
|
|
2023-10-18 13:20:59 +02:00
|
|
|
/* No parser found, skip extension */
|
|
|
|
*p = end_ext_octet;
|
|
|
|
|
2023-10-17 16:02:20 +02:00
|
|
|
if (is_critical) {
|
|
|
|
/* Data is marked as critical: fail */
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
2023-01-25 11:08:32 +01:00
|
|
|
}
|
2023-10-18 13:20:59 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Forbid repeated extensions */
|
|
|
|
if ((csr->ext_types & ext_type) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
2023-10-19 16:09:08 +02:00
|
|
|
MBEDTLS_ERR_ASN1_INVALID_DATA);
|
2023-10-18 13:20:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
csr->ext_types |= ext_type;
|
|
|
|
|
|
|
|
switch (ext_type) {
|
|
|
|
case MBEDTLS_X509_EXT_KEY_USAGE:
|
|
|
|
/* Parse key usage */
|
|
|
|
if ((ret = mbedtls_x509_get_key_usage(p, end_ext_data,
|
2023-10-19 16:09:08 +02:00
|
|
|
&csr->key_usage)) != 0) {
|
2023-10-18 13:20:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_X509_EXT_SUBJECT_ALT_NAME:
|
|
|
|
/* Parse subject alt name */
|
|
|
|
if ((ret = mbedtls_x509_get_subject_alt_name(p, end_ext_data,
|
2023-10-19 16:09:08 +02:00
|
|
|
&csr->subject_alt_names)) != 0) {
|
2023-10-18 13:20:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MBEDTLS_X509_EXT_NS_CERT_TYPE:
|
|
|
|
/* Parse netscape certificate type */
|
|
|
|
if ((ret = mbedtls_x509_get_ns_cert_type(p, end_ext_data,
|
2023-10-19 16:09:08 +02:00
|
|
|
&csr->ns_cert_type)) != 0) {
|
2023-10-18 13:20:59 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/*
|
2023-10-19 16:09:08 +02:00
|
|
|
* If this is a non-critical extension, which the oid layer
|
|
|
|
* supports, but there isn't an x509 parser for it,
|
|
|
|
* skip the extension.
|
|
|
|
*/
|
2023-10-18 13:20:59 +02:00
|
|
|
if (is_critical) {
|
|
|
|
return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
|
|
|
|
} else {
|
|
|
|
*p = end_ext_octet;
|
|
|
|
}
|
2023-01-24 09:45:19 +01:00
|
|
|
}
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*p != end) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse CSR attributes in DER format
|
|
|
|
*/
|
|
|
|
static int x509_csr_parse_attributes(mbedtls_x509_csr *csr,
|
2023-10-18 13:20:59 +02:00
|
|
|
const unsigned char *start, const unsigned char *end,
|
|
|
|
mbedtls_x509_csr_ext_cb_t cb,
|
|
|
|
void *p_ctx)
|
2019-10-29 23:03:37 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
size_t len;
|
|
|
|
unsigned char *end_attr_data;
|
|
|
|
unsigned char **p = (unsigned char **) &start;
|
|
|
|
|
|
|
|
while (*p < end) {
|
|
|
|
mbedtls_x509_buf attr_oid = { 0, 0, NULL };
|
|
|
|
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
end_attr_data = *p + len;
|
|
|
|
|
|
|
|
/* Get attribute ID */
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end_attr_data, &attr_oid.len,
|
|
|
|
MBEDTLS_ASN1_OID)) != 0) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
attr_oid.tag = MBEDTLS_ASN1_OID;
|
|
|
|
attr_oid.p = *p;
|
|
|
|
*p += attr_oid.len;
|
|
|
|
|
|
|
|
/* Check that this is an extension-request attribute */
|
|
|
|
if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS9_CSR_EXT_REQ, &attr_oid) == 0) {
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) !=
|
|
|
|
0) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
2023-10-18 13:20:59 +02:00
|
|
|
if ((ret = x509_csr_parse_extensions(csr, p, *p + len, cb, p_ctx)) != 0) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return ret;
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (*p != end_attr_data) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = end_attr_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p != end) {
|
2023-01-24 09:45:19 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
2023-01-26 22:30:45 +01:00
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
2019-10-29 23:03:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
2014-06-16 18:06:48 +02:00
|
|
|
* Parse a CSR in DER format
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2023-10-18 13:20:59 +02:00
|
|
|
static int mbedtls_x509_csr_parse_der_internal(mbedtls_x509_csr *csr,
|
2023-10-19 16:09:08 +02:00
|
|
|
const unsigned char *buf, size_t buflen,
|
|
|
|
mbedtls_x509_csr_ext_cb_t cb,
|
|
|
|
void *p_ctx)
|
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 len;
|
|
|
|
unsigned char *p, *end;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf sig_params;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(&sig_params, 0, sizeof(mbedtls_x509_buf));
|
2014-06-05 17:02:24 +02:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (csr == NULL || buf == NULL || buflen == 0) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_csr_init(csr);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-06-16 18:06:48 +02:00
|
|
|
/*
|
|
|
|
* first copy the raw DER data
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
p = mbedtls_calloc(1, len = buflen);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, buf, buflen);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
csr->raw.p = p;
|
|
|
|
csr->raw.len = len;
|
|
|
|
end = p + len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CertificationRequest ::= SEQUENCE {
|
|
|
|
* certificationRequestInfo CertificationRequestInfo,
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signature BIT STRING
|
|
|
|
* }
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_FORMAT;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len != (size_t) (end - p)) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CertificationRequestInfo ::= SEQUENCE {
|
|
|
|
*/
|
|
|
|
csr->cri.p = p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
end = p + len;
|
|
|
|
csr->cri.len = end - csr->cri.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Version ::= INTEGER { v1(0) }
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = x509_csr_get_version(&p, end, &csr->version)) != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (csr->version != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return MBEDTLS_ERR_X509_UNKNOWN_VERSION;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2017-02-17 14:54:43 +01:00
|
|
|
csr->version++;
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* subject Name
|
|
|
|
*/
|
|
|
|
csr->subject_raw.p = p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509_get_name(&p, p + len, &csr->subject)) != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
csr->subject_raw.len = p - csr->subject_raw.p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* subjectPKInfo SubjectPublicKeyInfo
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_pk_parse_subpubkey(&p, end, &csr->pk)) != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* attributes [0] Attributes
|
2016-02-24 15:36:05 +01:00
|
|
|
*
|
|
|
|
* The list of possible attributes is open-ended, though RFC 2985
|
|
|
|
* (PKCS#9) defines a few in section 5.4. We currently don't support any,
|
|
|
|
* so we just ignore them. This is a safe thing to do as the worst thing
|
|
|
|
* that could happen is that we issue a certificate that does not match
|
|
|
|
* the requester's expectations - this cannot cause a violation of our
|
|
|
|
* signature policies.
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
|
|
|
|
0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT, ret);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-10-18 13:20:59 +02:00
|
|
|
if ((ret = x509_csr_parse_attributes(csr, p, p + len, cb, p_ctx)) != 0) {
|
2019-10-29 23:03:37 +01:00
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
p += len;
|
|
|
|
|
|
|
|
end = csr->raw.p + csr->raw.len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* signatureAlgorithm AlgorithmIdentifier,
|
|
|
|
* signature BIT STRING
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509_get_alg(&p, end, &csr->sig_oid, &sig_params)) != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509_get_sig_alg(&csr->sig_oid, &sig_params,
|
|
|
|
&csr->sig_md, &csr->sig_pk,
|
|
|
|
&csr->sig_opts)) != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509_get_sig(&p, end, &csr->sig)) != 0) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
mbedtls_x509_csr_free(csr);
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_FORMAT,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
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-10-18 13:20:59 +02:00
|
|
|
/*
|
|
|
|
* Parse a CSR in DER format
|
|
|
|
*/
|
|
|
|
int mbedtls_x509_csr_parse_der(mbedtls_x509_csr *csr,
|
|
|
|
const unsigned char *buf, size_t buflen)
|
|
|
|
{
|
|
|
|
return mbedtls_x509_csr_parse_der_internal(csr, buf, buflen, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a CSR in DER format with callback for unknown extensions
|
|
|
|
*/
|
|
|
|
int mbedtls_x509_csr_parse_der_with_ext_cb(mbedtls_x509_csr *csr,
|
2023-10-19 16:09:08 +02:00
|
|
|
const unsigned char *buf, size_t buflen,
|
|
|
|
mbedtls_x509_csr_ext_cb_t cb,
|
|
|
|
void *p_ctx)
|
2023-10-18 13:20:59 +02:00
|
|
|
{
|
|
|
|
return mbedtls_x509_csr_parse_der_internal(csr, buf, buflen, cb, p_ctx);
|
|
|
|
}
|
|
|
|
|
2014-06-16 18:06:48 +02:00
|
|
|
/*
|
|
|
|
* Parse a CSR, allowing for PEM or raw DER encoding
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_csr_parse(mbedtls_x509_csr *csr, const unsigned char *buf, size_t buflen)
|
2014-06-16 18:06:48 +02:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-06-16 18:06:48 +02:00
|
|
|
size_t use_len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pem_context pem;
|
2014-06-16 18:06:48 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for valid input
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (csr == NULL || buf == NULL || buflen == 0) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
2014-06-16 18:06:48 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
2015-05-12 11:20:10 +02:00
|
|
|
/* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (buf[buflen - 1] == '\0') {
|
|
|
|
mbedtls_pem_init(&pem);
|
|
|
|
ret = mbedtls_pem_read_buffer(&pem,
|
|
|
|
"-----BEGIN CERTIFICATE REQUEST-----",
|
|
|
|
"-----END CERTIFICATE REQUEST-----",
|
|
|
|
buf, NULL, 0, &use_len);
|
|
|
|
if (ret == MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
|
|
|
|
ret = mbedtls_pem_read_buffer(&pem,
|
|
|
|
"-----BEGIN NEW CERTIFICATE REQUEST-----",
|
|
|
|
"-----END NEW CERTIFICATE REQUEST-----",
|
|
|
|
buf, NULL, 0, &use_len);
|
2018-09-30 16:36:50 +02:00
|
|
|
}
|
2018-10-07 18:48:37 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
2018-06-14 07:35:11 +02:00
|
|
|
/*
|
|
|
|
* Was PEM encoded, parse the result
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_csr_parse_der(csr, pem.buf, pem.buflen);
|
2018-09-30 16:36:50 +02:00
|
|
|
}
|
2014-06-16 18:06:48 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pem_free(&pem);
|
|
|
|
if (ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-06-16 18:06:48 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_PEM_PARSE_C */
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_x509_csr_parse_der(csr, buf, buflen);
|
2014-06-16 18:06:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_FS_IO)
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Load a CSR into the structure
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_csr_parse_file(mbedtls_x509_csr *csr, const char *path)
|
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 n;
|
|
|
|
unsigned char *buf;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_pk_load_file(path, &buf, &n)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_csr_parse(csr, buf, n);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(buf, n);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_FS_IO */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2013-09-16 13:49:26 +02:00
|
|
|
#define BEFORE_COLON 14
|
|
|
|
#define BC "14"
|
|
|
|
/*
|
|
|
|
* Return an informational string about the CSR.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_csr_info(char *buf, size_t size, const char *prefix,
|
|
|
|
const mbedtls_x509_csr *csr)
|
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 n;
|
|
|
|
char *p;
|
|
|
|
char key_size_str[BEFORE_COLON];
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "%sCSR version : %d",
|
|
|
|
prefix, csr->version);
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "\n%ssubject name : ", prefix);
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_dn_gets(p, n, &csr->subject);
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "\n%ssigned using : ", prefix);
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_sig_alg_gets(p, n, &csr->sig_oid, csr->sig_pk, csr->sig_md,
|
|
|
|
csr->sig_opts);
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509_key_size_helper(key_size_str, BEFORE_COLON,
|
|
|
|
mbedtls_pk_get_name(&csr->pk))) != 0) {
|
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "\n%s%-" BC "s: %d bits\n", prefix, key_size_str,
|
|
|
|
(int) mbedtls_pk_get_bitlen(&csr->pk));
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-12 12:58:02 +01:00
|
|
|
/*
|
|
|
|
* Optional extensions
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (csr->ext_types & MBEDTLS_X509_EXT_SUBJECT_ALT_NAME) {
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%ssubject alt name :", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-16 08:47:49 +01:00
|
|
|
if ((ret = mbedtls_x509_info_subject_alt_name(&p, &n,
|
|
|
|
&csr->subject_alt_names,
|
|
|
|
prefix)) != 0) {
|
2023-01-12 12:58:02 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (csr->ext_types & MBEDTLS_X509_EXT_NS_CERT_TYPE) {
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%scert. type : ", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-16 08:47:49 +01:00
|
|
|
if ((ret = mbedtls_x509_info_cert_type(&p, &n, csr->ns_cert_type)) != 0) {
|
2023-01-12 12:58:02 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (csr->ext_types & MBEDTLS_X509_EXT_KEY_USAGE) {
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%skey usage : ", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-01-16 08:47:49 +01:00
|
|
|
if ((ret = mbedtls_x509_info_key_usage(&p, &n, csr->key_usage)) != 0) {
|
2023-01-12 12:58:02 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (csr->ext_types != 0) {
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n");
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) (size - n);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
2020-10-09 10:19:39 +02:00
|
|
|
#endif /* MBEDTLS_X509_REMOVE_INFO */
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2013-09-18 11:58:25 +02:00
|
|
|
/*
|
|
|
|
* Initialize a CSR
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_csr_init(mbedtls_x509_csr *csr)
|
2013-09-18 11:58:25 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(csr, 0, sizeof(mbedtls_x509_csr));
|
2013-09-18 11:58:25 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Unallocate all CSR data
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_x509_csr_free(mbedtls_x509_csr *csr)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
if (csr == NULL) {
|
2013-09-16 13:49:26 +02:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pk_free(&csr->pk);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(csr->sig_opts);
|
2014-06-05 15:14:28 +02:00
|
|
|
#endif
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_free_named_data_list_shallow(csr->subject.next);
|
2023-01-24 15:19:47 +01:00
|
|
|
mbedtls_asn1_sequence_free(csr->subject_alt_names.next);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (csr->raw.p != NULL) {
|
2023-07-17 16:17:40 +02:00
|
|
|
mbedtls_zeroize_and_free(csr->raw.p, csr->raw.len);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_platform_zeroize(csr, sizeof(mbedtls_x509_csr));
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_CSR_PARSE_C */
|