2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* X.509 common functions for parsing and verification
|
|
|
|
*
|
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
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* The ITU-T X.509 standard defines a certificate format for PKI.
|
|
|
|
*
|
|
|
|
* 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)
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
#include "common.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_USE_C)
|
|
|
|
|
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/asn1.h"
|
2021-12-21 12:54:05 +01:00
|
|
|
#include "mbedtls/error.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
#include "mbedtls/oid.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_PEM_PARSE_C)
|
|
|
|
#include "mbedtls/pem.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
#include "mbedtls/platform_time.h"
|
|
|
|
#endif
|
2018-07-28 11:16:41 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME_DATE)
|
2019-02-16 17:19:46 +01:00
|
|
|
#include "mbedtls/platform_util.h"
|
2018-02-08 19:04:43 +01:00
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2023-08-04 18:09:03 +02:00
|
|
|
#define CHECK(code) \
|
|
|
|
do { \
|
|
|
|
if ((ret = (code)) != 0) { \
|
|
|
|
return ret; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
#define CHECK_RANGE(min, max, val) \
|
2023-08-04 18:09:03 +02:00
|
|
|
do { \
|
|
|
|
if ((val) < (min) || (val) > (max)) { \
|
|
|
|
return ret; \
|
2019-07-10 16:15:01 +02:00
|
|
|
} \
|
2023-04-18 10:38:24 +02:00
|
|
|
} while (0)
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CertificateSerialNumber ::= INTEGER
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *serial)
|
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
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (**p != (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_PRIMITIVE | 2) &&
|
|
|
|
**p != MBEDTLS_ASN1_INTEGER) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
serial->tag = *(*p)++;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
serial->p = *p;
|
|
|
|
*p += serial->len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get an algorithm identifier without parameters (eg for signatures)
|
|
|
|
*
|
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg)
|
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
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, 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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-11-12 08:55:43 +01:00
|
|
|
* Parse an algorithm identifier with (optional) parameters
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
|
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
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, 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
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
/*
|
|
|
|
* HashAlgorithm ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
|
|
|
*
|
|
|
|
* For HashAlgorithm, parameters MUST be NULL or absent.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg)
|
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 *p;
|
|
|
|
const unsigned char *end;
|
|
|
|
mbedtls_x509_buf md_oid;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Make sure we got a SEQUENCE and setup bounds */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2021-12-21 12:54:05 +01:00
|
|
|
p = alg->p;
|
2018-02-08 19:04:43 +01:00
|
|
|
end = p + alg->len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p >= end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Parse md_oid */
|
|
|
|
md_oid.tag = *p;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &md_oid.len, MBEDTLS_ASN1_OID)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
md_oid.p = p;
|
|
|
|
p += md_oid.len;
|
|
|
|
|
|
|
|
/* Get md_alg from md_oid */
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Make sure params is absent of NULL */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_NULL)) != 0 || len != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RSASSA-PSS-params ::= SEQUENCE {
|
|
|
|
* hashAlgorithm [0] HashAlgorithm DEFAULT sha1Identifier,
|
|
|
|
* maskGenAlgorithm [1] MaskGenAlgorithm DEFAULT mgf1SHA1Identifier,
|
|
|
|
* saltLength [2] INTEGER DEFAULT 20,
|
|
|
|
* trailerField [3] INTEGER DEFAULT 1 }
|
|
|
|
* -- Note that the tags in this Sequence are explicit.
|
|
|
|
*
|
|
|
|
* RFC 4055 (which defines use of RSASSA-PSS in PKIX) states that the value
|
|
|
|
* of trailerField MUST be 1, and PKCS#1 v2.2 doesn't even define any other
|
2022-12-21 12:05:54 +01:00
|
|
|
* option. Enforce this at parsing time.
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_get_rsassa_pss_params(const mbedtls_x509_buf *params,
|
|
|
|
mbedtls_md_type_t *md_alg, mbedtls_md_type_t *mgf_md,
|
|
|
|
int *salt_len)
|
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 *p;
|
|
|
|
const unsigned char *end, *end2;
|
|
|
|
size_t len;
|
|
|
|
mbedtls_x509_buf alg_id, alg_params;
|
|
|
|
|
|
|
|
/* First set everything to defaults */
|
|
|
|
*md_alg = MBEDTLS_MD_SHA1;
|
|
|
|
*mgf_md = MBEDTLS_MD_SHA1;
|
|
|
|
*salt_len = 20;
|
|
|
|
|
|
|
|
/* Make sure params is a SEQUENCE and setup bounds */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
p = (unsigned char *) params->p;
|
|
|
|
end = p + params->len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* HashAlgorithm
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
0)) == 0) {
|
2018-02-08 19:04:43 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
|
|
|
/* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p != end2) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
|
|
|
} else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MaskGenAlgorithm
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
1)) == 0) {
|
2018-02-08 19:04:43 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
|
|
|
/* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Only MFG1 is recognised for now */
|
2023-04-18 10:38:24 +02:00
|
|
|
if (MBEDTLS_OID_CMP(MBEDTLS_OID_MGF1, &alg_id) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE,
|
|
|
|
MBEDTLS_ERR_OID_NOT_FOUND);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Parse HashAlgorithm */
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p != end2) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
|
|
|
} else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* salt_len
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
2)) == 0) {
|
2018-02-08 19:04:43 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p != end2) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
|
|
|
} else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* trailer_field (if present, must be 1)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
3)) == 0) {
|
2018-02-08 19:04:43 +01:00
|
|
|
int trailer_field;
|
|
|
|
|
|
|
|
end2 = p + len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p != end2) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (trailer_field != 1) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_ALG;
|
|
|
|
}
|
|
|
|
} else if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (p != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
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_X509_RSASSA_PSS_SUPPORT */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int x509_get_attr_type_value(unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
mbedtls_x509_name *cur)
|
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;
|
|
|
|
mbedtls_x509_buf *oid;
|
|
|
|
mbedtls_x509_buf *val;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
end = *p + len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
oid = &cur->oid;
|
|
|
|
oid->tag = **p;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &oid->len, MBEDTLS_ASN1_OID)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
oid->p = *p;
|
|
|
|
*p += oid->len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
|
2018-02-08 19:04:43 +01:00
|
|
|
**p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
|
|
|
|
**p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
|
2023-04-18 10:38:24 +02:00
|
|
|
**p != MBEDTLS_ASN1_BIT_STRING) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
val = &cur->val;
|
|
|
|
val->tag = *(*p)++;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
val->p = *p;
|
|
|
|
*p += val->len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*p != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
2019-07-10 16:15:01 +02:00
|
|
|
}
|
|
|
|
|
2018-02-08 19:04:43 +01:00
|
|
|
cur->next = NULL;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Name ::= CHOICE { -- only one possibility for now --
|
|
|
|
* rdnSequence RDNSequence }
|
|
|
|
*
|
|
|
|
* RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
|
|
|
|
*
|
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*
|
|
|
|
* The data structure is optimized for the common case where each RDN has only
|
|
|
|
* one element, which is represented as a list of AttributeTypeAndValue.
|
|
|
|
* For the general case we still use a flat list, but we mark elements of the
|
|
|
|
* same set so that they are "merged" together in the functions that consume
|
|
|
|
* this list, eg mbedtls_x509_dn_gets().
|
2022-12-21 12:05:54 +01:00
|
|
|
*
|
|
|
|
* On success, this function may allocate a linked list starting at cur->next
|
|
|
|
* that must later be free'd by the caller using mbedtls_free(). In error
|
|
|
|
* cases, this function frees all allocated memory internally and the caller
|
|
|
|
* has no freeing responsibilities.
|
2018-02-08 19:04:43 +01:00
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_name *cur)
|
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 set_len;
|
|
|
|
const unsigned char *end_set;
|
2022-12-21 12:05:54 +01:00
|
|
|
mbedtls_x509_name *head = cur;
|
|
|
|
mbedtls_x509_name *prev, *allocated;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* don't use recursion, we'd risk stack overflow if not optimized */
|
2023-04-18 10:38:24 +02:00
|
|
|
while (1) {
|
2018-02-08 19:04:43 +01:00
|
|
|
/*
|
|
|
|
* parse SET
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &set_len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SET)) != 0) {
|
|
|
|
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
|
2022-12-21 12:05:54 +01:00
|
|
|
goto error;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
end_set = *p + set_len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
while (1) {
|
|
|
|
if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
|
2022-12-21 12:05:54 +01:00
|
|
|
goto error;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*p == end_set) {
|
2018-02-08 19:04:43 +01:00
|
|
|
break;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/* Mark this item as being no the only one in a set */
|
|
|
|
cur->next_merged = 1;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (cur->next == NULL) {
|
2022-12-21 12:05:54 +01:00
|
|
|
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* continue until end of SEQUENCE is reached
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (cur->next == NULL) {
|
2022-12-21 12:05:54 +01:00
|
|
|
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
2022-12-21 12:05:54 +01:00
|
|
|
|
|
|
|
error:
|
|
|
|
/* Skip the first element as we did not allocate it */
|
|
|
|
allocated = head->next;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
while (allocated != NULL) {
|
2022-12-21 12:05:54 +01:00
|
|
|
prev = allocated;
|
|
|
|
allocated = allocated->next;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_platform_zeroize(prev, sizeof(*prev));
|
|
|
|
mbedtls_free(prev);
|
2022-12-21 12:05:54 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_platform_zeroize(head, sizeof(*head));
|
2022-12-21 12:05:54 +01:00
|
|
|
|
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
|
|
|
static int x509_parse_int(unsigned char **p, size_t n, int *res)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
*res = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (; n > 0; --n) {
|
|
|
|
if ((**p < '0') || (**p > '9')) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_DATE;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
*res *= 10;
|
2023-04-18 10:38:24 +02:00
|
|
|
*res += (*(*p)++ - '0');
|
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_date_is_valid(const mbedtls_x509_time *t)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_X509_INVALID_DATE;
|
|
|
|
int month_len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
CHECK_RANGE(0, 9999, t->year);
|
|
|
|
CHECK_RANGE(0, 23, t->hour);
|
|
|
|
CHECK_RANGE(0, 59, t->min);
|
|
|
|
CHECK_RANGE(0, 59, t->sec);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
switch (t->mon) {
|
2018-02-08 19:04:43 +01:00
|
|
|
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
|
|
|
|
month_len = 31;
|
|
|
|
break;
|
|
|
|
case 4: case 6: case 9: case 11:
|
|
|
|
month_len = 30;
|
|
|
|
break;
|
|
|
|
case 2:
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((!(t->year % 4) && t->year % 100) ||
|
|
|
|
!(t->year % 400)) {
|
2018-02-08 19:04:43 +01:00
|
|
|
month_len = 29;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else {
|
2018-02-08 19:04:43 +01:00
|
|
|
month_len = 28;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
break;
|
|
|
|
default:
|
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
|
|
|
CHECK_RANGE(1, month_len, t->day);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
|
|
|
|
* field.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int x509_parse_time(unsigned char **p, size_t len, size_t yearlen,
|
|
|
|
mbedtls_x509_time *tm)
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* Minimum length is 10 or 12 depending on yearlen
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (len < yearlen + 8) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_DATE;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
len -= yearlen + 8;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse year, month, day, hour, minute
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
CHECK(x509_parse_int(p, yearlen, &tm->year));
|
|
|
|
if (2 == yearlen) {
|
|
|
|
if (tm->year < 50) {
|
2018-02-08 19:04:43 +01:00
|
|
|
tm->year += 100;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
tm->year += 1900;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
CHECK(x509_parse_int(p, 2, &tm->mon));
|
|
|
|
CHECK(x509_parse_int(p, 2, &tm->day));
|
|
|
|
CHECK(x509_parse_int(p, 2, &tm->hour));
|
|
|
|
CHECK(x509_parse_int(p, 2, &tm->min));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse seconds if present
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (len >= 2) {
|
|
|
|
CHECK(x509_parse_int(p, 2, &tm->sec));
|
2018-02-08 19:04:43 +01:00
|
|
|
len -= 2;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_DATE;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse trailing 'Z' if present
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (1 == len && 'Z' == **p) {
|
2018-02-08 19:04:43 +01:00
|
|
|
(*p)++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We should have parsed all characters at this point
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if (0 != len) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_DATE;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
CHECK(x509_date_is_valid(tm));
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Time ::= CHOICE {
|
|
|
|
* utcTime UTCTime,
|
|
|
|
* generalTime GeneralizedTime }
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_time *tm)
|
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, year_len;
|
|
|
|
unsigned char tag;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
tag = **p;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (tag == MBEDTLS_ASN1_UTC_TIME) {
|
2018-02-08 19:04:43 +01:00
|
|
|
year_len = 2;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
|
2018-02-08 19:04:43 +01:00
|
|
|
year_len = 4;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
(*p)++;
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_asn1_get_len(p, end, &len);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return x509_parse_time(p, len, year_len, tm);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
|
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;
|
|
|
|
int tag_type;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
tag_type = **p;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
sig->tag = tag_type;
|
|
|
|
sig->len = len;
|
|
|
|
sig->p = *p;
|
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get signature algorithm from alg OID and optional parameters
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_get_sig_alg(const mbedtls_x509_buf *sig_oid, const mbedtls_x509_buf *sig_params,
|
|
|
|
mbedtls_md_type_t *md_alg, mbedtls_pk_type_t *pk_alg,
|
|
|
|
void **sig_opts)
|
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
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*sig_opts != NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_oid_get_sig_alg(sig_oid, md_alg, pk_alg)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
|
2018-02-08 19:04:43 +01:00
|
|
|
mbedtls_pk_rsassa_pss_options *pss_opts;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
pss_opts = mbedtls_calloc(1, sizeof(mbedtls_pk_rsassa_pss_options));
|
|
|
|
if (pss_opts == NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = mbedtls_x509_get_rsassa_pss_params(sig_params,
|
|
|
|
md_alg,
|
|
|
|
&pss_opts->mgf1_hash_id,
|
|
|
|
&pss_opts->expected_salt_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_free(pss_opts);
|
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*sig_opts = (void *) pss_opts;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else
|
2018-02-08 19:04:43 +01:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
|
|
|
{
|
|
|
|
/* Make sure parameters are absent or NULL */
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
|
|
|
|
sig_params->len != 0) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_ALG;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 Extensions (No parsing of extensions, pointer should
|
|
|
|
* be either manually updated or extensions should be parsed!)
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *ext, int 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
|
|
|
size_t len;
|
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
/* Extension structure use EXPLICIT tagging. That is, the actual
|
|
|
|
* `Extensions` structure is wrapped by a tag-length pair using
|
|
|
|
* the respective context-specific tag. */
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_asn1_get_tag(p, end, &ext->len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
|
|
|
|
ext->p = *p;
|
|
|
|
end = *p + ext->len;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (end != *p + len) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the name in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2022-07-18 14:48:00 +02:00
|
|
|
size_t i, j, n;
|
2018-02-08 19:04:43 +01:00
|
|
|
unsigned char c, merge = 0;
|
|
|
|
const mbedtls_x509_name *name;
|
|
|
|
const char *short_name = NULL;
|
|
|
|
char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
memset(s, 0, sizeof(s));
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
name = dn;
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
while (name != NULL) {
|
|
|
|
if (!name->oid.p) {
|
2018-02-08 19:04:43 +01:00
|
|
|
name = name->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (name != dn) {
|
|
|
|
ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
|
2018-02-08 19:04:43 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (ret == 0) {
|
|
|
|
ret = mbedtls_snprintf(p, n, "%s=", short_name);
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_snprintf(p, n, "\?\?=");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 0, j = 0; i < name->val.len; i++, j++) {
|
|
|
|
if (j >= sizeof(s) - 1) {
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
|
|
|
c = name->val.p[i];
|
2022-07-18 14:48:00 +02:00
|
|
|
// Special characters requiring escaping, RFC 1779
|
2023-04-18 10:38:24 +02:00
|
|
|
if (c && strchr(",=+<>#;\"\\", c)) {
|
|
|
|
if (j + 1 >= sizeof(s) - 1) {
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2022-07-18 14:48:00 +02:00
|
|
|
s[j++] = '\\';
|
|
|
|
}
|
2023-04-18 10:38:24 +02:00
|
|
|
if (c < 32 || c >= 127) {
|
|
|
|
s[j] = '?';
|
|
|
|
} else {
|
|
|
|
s[j] = c;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
2022-07-18 14:48:00 +02:00
|
|
|
s[j] = '\0';
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_snprintf(p, n, "%s", s);
|
2018-02-08 19:04:43 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
|
|
|
merge = name->next_merged;
|
|
|
|
name = name->next;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) (size - n);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the serial in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
|
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 i, n, nr;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
nr = (serial->len <= 32)
|
2018-02-08 19:04:43 +01:00
|
|
|
? serial->len : 28;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
|
2018-02-08 19:04:43 +01:00
|
|
|
continue;
|
2023-04-18 10:38:24 +02:00
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_snprintf(p, n, "%02X%s",
|
|
|
|
serial->p[i], (i < nr - 1) ? ":" : "");
|
2018-02-08 19:04:43 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (nr != serial->len) {
|
|
|
|
ret = mbedtls_snprintf(p, n, "....");
|
2018-02-08 19:04:43 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) (size - n);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for writing signature algorithms
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_sig_alg_gets(char *buf, size_t size, const mbedtls_x509_buf *sig_oid,
|
|
|
|
mbedtls_pk_type_t pk_alg, mbedtls_md_type_t md_alg,
|
|
|
|
const void *sig_opts)
|
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
|
|
|
char *p = buf;
|
|
|
|
size_t n = size;
|
|
|
|
const char *desc = NULL;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_oid_get_sig_alg_desc(sig_oid, &desc);
|
|
|
|
if (ret != 0) {
|
|
|
|
ret = mbedtls_snprintf(p, n, "???");
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_snprintf(p, n, "%s", desc);
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2023-04-18 10:38:24 +02:00
|
|
|
if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
|
2018-02-08 19:04:43 +01:00
|
|
|
const mbedtls_pk_rsassa_pss_options *pss_opts;
|
|
|
|
const mbedtls_md_info_t *md_info, *mgf_md_info;
|
|
|
|
|
|
|
|
pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
md_info = mbedtls_md_info_from_type(md_alg);
|
|
|
|
mgf_md_info = mbedtls_md_info_from_type(pss_opts->mgf1_hash_id);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
|
|
|
|
md_info ? mbedtls_md_get_name(md_info) : "???",
|
|
|
|
mgf_md_info ? mbedtls_md_get_name(mgf_md_info) : "???",
|
|
|
|
(unsigned int) pss_opts->expected_salt_len);
|
2018-02-08 19:04:43 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) pk_alg);
|
|
|
|
((void) md_alg);
|
|
|
|
((void) sig_opts);
|
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return (int) (size - n);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper for writing "RSA key size", "EC key size", etc
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
size_t n = buf_size;
|
2021-12-21 12:54:05 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_snprintf(p, n, "%s key size", name);
|
2018-02-08 19:04:43 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME_DATE)
|
|
|
|
/*
|
|
|
|
* Set the time structure to the current time.
|
|
|
|
* Return 0 on success, non-zero on failure.
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int x509_get_current_time(mbedtls_x509_time *now)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2018-07-28 11:16:41 +02:00
|
|
|
struct tm *lt, tm_buf;
|
2018-02-08 19:04:43 +01:00
|
|
|
mbedtls_time_t tt;
|
|
|
|
int ret = 0;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
tt = mbedtls_time(NULL);
|
|
|
|
lt = mbedtls_platform_gmtime_r(&tt, &tm_buf);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (lt == NULL) {
|
2018-02-08 19:04:43 +01:00
|
|
|
ret = -1;
|
2023-04-18 10:38:24 +02:00
|
|
|
} else {
|
2018-02-08 19:04:43 +01:00
|
|
|
now->year = lt->tm_year + 1900;
|
|
|
|
now->mon = lt->tm_mon + 1;
|
|
|
|
now->day = lt->tm_mday;
|
|
|
|
now->hour = lt->tm_hour;
|
|
|
|
now->min = lt->tm_min;
|
|
|
|
now->sec = lt->tm_sec;
|
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return 0 if before <= after, 1 otherwise
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
static int x509_check_time(const mbedtls_x509_time *before, const mbedtls_x509_time *after)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2023-04-18 10:38:24 +02:00
|
|
|
if (before->year > after->year) {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (before->year == after->year &&
|
|
|
|
before->mon > after->mon) {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (before->year == after->year &&
|
2018-02-08 19:04:43 +01:00
|
|
|
before->mon == after->mon &&
|
2023-04-18 10:38:24 +02:00
|
|
|
before->day > after->day) {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (before->year == after->year &&
|
2018-02-08 19:04:43 +01:00
|
|
|
before->mon == after->mon &&
|
|
|
|
before->day == after->day &&
|
2023-04-18 10:38:24 +02:00
|
|
|
before->hour > after->hour) {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (before->year == after->year &&
|
2018-02-08 19:04:43 +01:00
|
|
|
before->mon == after->mon &&
|
|
|
|
before->day == after->day &&
|
|
|
|
before->hour == after->hour &&
|
2023-04-18 10:38:24 +02:00
|
|
|
before->min > after->min) {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (before->year == after->year &&
|
2018-02-08 19:04:43 +01:00
|
|
|
before->mon == after->mon &&
|
|
|
|
before->day == after->day &&
|
|
|
|
before->hour == after->hour &&
|
|
|
|
before->min == after->min &&
|
2023-04-18 10:38:24 +02:00
|
|
|
before->sec > after->sec) {
|
|
|
|
return 1;
|
|
|
|
}
|
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
|
|
|
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
mbedtls_x509_time now;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (x509_get_current_time(&now) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return x509_check_time(&now, to);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
mbedtls_x509_time now;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (x509_get_current_time(&now) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
return x509_check_time(from, &now);
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#else /* MBEDTLS_HAVE_TIME_DATE */
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
((void) to);
|
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
|
|
|
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
|
|
|
((void) from);
|
2023-04-18 10:38:24 +02:00
|
|
|
return 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_HAVE_TIME_DATE */
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_SELF_TEST)
|
|
|
|
|
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/certs.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checkup routine
|
|
|
|
*/
|
2023-04-18 10:38:24 +02:00
|
|
|
int mbedtls_x509_self_test(int verbose)
|
2018-02-08 19:04:43 +01:00
|
|
|
{
|
2019-07-10 16:15:01 +02:00
|
|
|
int ret = 0;
|
2018-02-08 19:04:43 +01:00
|
|
|
#if defined(MBEDTLS_CERTS_C) && defined(MBEDTLS_SHA256_C)
|
|
|
|
uint32_t flags;
|
|
|
|
mbedtls_x509_crt cacert;
|
|
|
|
mbedtls_x509_crt clicert;
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf(" X.509 certificate load: ");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_x509_crt_init(&cacert);
|
|
|
|
mbedtls_x509_crt_init(&clicert);
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_x509_crt_parse(&clicert, (const unsigned char *) mbedtls_test_cli_crt,
|
|
|
|
mbedtls_test_cli_crt_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("failed\n");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
goto cleanup;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_x509_crt_parse(&cacert, (const unsigned char *) mbedtls_test_ca_crt,
|
|
|
|
mbedtls_test_ca_crt_len);
|
|
|
|
if (ret != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("failed\n");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
goto cleanup;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n X.509 signature verify: ");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
ret = mbedtls_x509_crt_verify(&clicert, &cacert, NULL, NULL, &flags, NULL, NULL);
|
|
|
|
if (ret != 0) {
|
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("failed\n");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
goto cleanup;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 10:38:24 +02:00
|
|
|
if (verbose != 0) {
|
|
|
|
mbedtls_printf("passed\n\n");
|
|
|
|
}
|
2018-02-08 19:04:43 +01:00
|
|
|
|
2019-07-10 16:15:01 +02:00
|
|
|
cleanup:
|
2023-04-18 10:38:24 +02:00
|
|
|
mbedtls_x509_crt_free(&cacert);
|
|
|
|
mbedtls_x509_crt_free(&clicert);
|
2018-02-08 19:04:43 +01:00
|
|
|
#else
|
|
|
|
((void) verbose);
|
2020-04-21 10:37:14 +02:00
|
|
|
#endif /* MBEDTLS_CERTS_C && MBEDTLS_SHA256_C */
|
2023-04-18 10:38:24 +02:00
|
|
|
return ret;
|
2018-02-08 19:04:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_SELF_TEST */
|
|
|
|
|
|
|
|
#endif /* MBEDTLS_X509_USE_C */
|