2013-09-16 13:49:26 +02:00
|
|
|
/*
|
2014-06-12 22:34:55 +02:00
|
|
|
* X.509 common functions for parsing and verification
|
2013-09-16 13:49:26 +02:00
|
|
|
*
|
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_USE_C)
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/x509.h"
|
|
|
|
#include "mbedtls/asn1.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"
|
2015-02-06 14:43:58 +01:00
|
|
|
|
2015-02-12 19:27:14 +01:00
|
|
|
#include <stdio.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
|
|
|
|
|
2023-07-26 16:51:49 +02:00
|
|
|
#include "mbedtls/asn1write.h"
|
|
|
|
|
2015-03-09 18:05:11 +01:00
|
|
|
#include "mbedtls/platform.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2016-07-13 15:46:18 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
#include "mbedtls/platform_time.h"
|
|
|
|
#endif
|
2017-12-05 13:07:33 +01:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME_DATE)
|
2018-08-16 22:42:09 +02:00
|
|
|
#include "mbedtls/platform_util.h"
|
2013-09-16 13:49:26 +02:00
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
2022-12-04 10:24:22 +01:00
|
|
|
#define CHECK(code) \
|
|
|
|
do { \
|
|
|
|
if ((ret = (code)) != 0) { \
|
|
|
|
return ret; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
|
|
|
|
2018-10-15 13:01:35 +02:00
|
|
|
#define CHECK_RANGE(min, max, val) \
|
2022-12-04 10:24:22 +01:00
|
|
|
do { \
|
|
|
|
if ((val) < (min) || (val) > (max)) { \
|
|
|
|
return ret; \
|
2018-10-15 13:01:35 +02:00
|
|
|
} \
|
2023-01-11 14:50:10 +01:00
|
|
|
} while (0)
|
2015-02-13 12:48:02 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* CertificateSerialNumber ::= INTEGER
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_get_serial(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *serial)
|
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 ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
serial->tag = *(*p)++;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_len(p, end, &serial->len)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SERIAL, ret);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
serial->p = *p;
|
|
|
|
*p += serial->len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get an algorithm identifier without parameters (eg for signatures)
|
|
|
|
*
|
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_get_alg_null(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg)
|
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_alg_null(p, end, alg)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, 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
|
|
|
}
|
|
|
|
|
2014-01-22 10:12:57 +01:00
|
|
|
/*
|
2019-01-23 15:24:37 +01:00
|
|
|
* Parse an algorithm identifier with (optional) parameters
|
2014-01-22 10:12:57 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_get_alg(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg, mbedtls_x509_buf *params)
|
2014-01-22 10:12:57 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-01-22 10:12:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_alg(p, end, alg, params)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2014-01-22 10:12:57 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-01-22 10:12:57 +01:00
|
|
|
}
|
|
|
|
|
2022-06-27 11:19:04 +02:00
|
|
|
/*
|
|
|
|
* Convert md type to string
|
|
|
|
*/
|
2023-06-28 17:22:50 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static inline const char *md_type_to_string(mbedtls_md_type_t md_alg)
|
2022-06-27 11:19:04 +02:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (md_alg) {
|
2023-03-16 11:39:20 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_MD5)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_MD5:
|
|
|
|
return "MD5";
|
2022-06-27 11:19:04 +02:00
|
|
|
#endif
|
2023-03-16 11:39:20 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA1)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_SHA1:
|
|
|
|
return "SHA1";
|
2022-06-27 11:19:04 +02:00
|
|
|
#endif
|
2023-03-16 11:39:20 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA224)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_SHA224:
|
|
|
|
return "SHA224";
|
2022-06-27 11:19:04 +02:00
|
|
|
#endif
|
2023-03-16 11:39:20 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA256)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_SHA256:
|
|
|
|
return "SHA256";
|
2022-06-27 11:19:04 +02:00
|
|
|
#endif
|
2023-03-16 11:39:20 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA384)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_SHA384:
|
|
|
|
return "SHA384";
|
2022-06-27 11:19:04 +02:00
|
|
|
#endif
|
2023-03-16 11:39:20 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_SHA512)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_SHA512:
|
|
|
|
return "SHA512";
|
2022-06-27 11:19:04 +02:00
|
|
|
#endif
|
2023-03-16 11:39:20 +01:00
|
|
|
#if defined(MBEDTLS_MD_CAN_RIPEMD160)
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_RIPEMD160:
|
|
|
|
return "RIPEMD160";
|
2022-06-27 11:19:04 +02:00
|
|
|
#endif
|
2023-01-11 14:50:10 +01:00
|
|
|
case MBEDTLS_MD_NONE:
|
|
|
|
return NULL;
|
|
|
|
default:
|
|
|
|
return NULL;
|
2022-06-27 11:19:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 13:09:27 +02:00
|
|
|
#endif /* !defined(MBEDTLS_X509_REMOVE_INFO) && defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) */
|
2023-06-28 17:22:50 +02:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2014-01-23 19:15:29 +01:00
|
|
|
/*
|
|
|
|
* HashAlgorithm ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* AlgorithmIdentifier ::= SEQUENCE {
|
|
|
|
* algorithm OBJECT IDENTIFIER,
|
|
|
|
* parameters ANY DEFINED BY algorithm OPTIONAL }
|
|
|
|
*
|
|
|
|
* For HashAlgorithm, parameters MUST be NULL or absent.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int x509_get_hash_alg(const mbedtls_x509_buf *alg, mbedtls_md_type_t *md_alg)
|
2014-01-23 19:15:29 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-01-23 19:15:29 +01:00
|
|
|
unsigned char *p;
|
|
|
|
const unsigned char *end;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf md_oid;
|
2014-01-23 19:15:29 +01:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Make sure we got a SEQUENCE and setup bounds */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (alg->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
2020-07-16 10:37:41 +02:00
|
|
|
p = alg->p;
|
2014-01-23 19:15:29 +01:00
|
|
|
end = p + alg->len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p >= end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
/* Parse md_oid */
|
|
|
|
md_oid.tag = *p;
|
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
md_oid.p = p;
|
|
|
|
p += md_oid.len;
|
|
|
|
|
|
|
|
/* Get md_alg from md_oid */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_oid_get_md_alg(&md_oid, md_alg)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
/* Make sure params is absent of NULL */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-01-23 19:15:29 +01:00
|
|
|
}
|
|
|
|
|
2014-01-23 16:24:44 +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.
|
2014-05-31 17:08:16 +02:00
|
|
|
*
|
|
|
|
* 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-04 18:19:59 +01:00
|
|
|
* option. Enforce this at parsing time.
|
2014-01-23 16:24:44 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01: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)
|
2014-01-23 16:24:44 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-01-23 16:24:44 +01:00
|
|
|
unsigned char *p;
|
2014-01-24 14:15:20 +01:00
|
|
|
const unsigned char *end, *end2;
|
2014-01-23 16:24:44 +01:00
|
|
|
size_t len;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf alg_id, alg_params;
|
2014-01-23 16:24:44 +01:00
|
|
|
|
|
|
|
/* First set everything to defaults */
|
2015-04-08 12:49:31 +02:00
|
|
|
*md_alg = MBEDTLS_MD_SHA1;
|
|
|
|
*mgf_md = MBEDTLS_MD_SHA1;
|
2014-01-23 16:24:44 +01:00
|
|
|
*salt_len = 20;
|
|
|
|
|
|
|
|
/* Make sure params is a SEQUENCE and setup bounds */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (params->tag != (MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2014-01-23 16:24:44 +01:00
|
|
|
|
|
|
|
p = (unsigned char *) params->p;
|
|
|
|
end = p + params->len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-23 16:24:44 +01:00
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
/*
|
|
|
|
* HashAlgorithm
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
0)) == 0) {
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 19:15:29 +01:00
|
|
|
/* HashAlgorithm ::= AlgorithmIdentifier (without parameters) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509_get_alg_null(&p, end2, &alg_id)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_oid_get_md_alg(&alg_id, md_alg)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2014-01-24 14:15:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-24 14:15:20 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* MaskGenAlgorithm
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
1)) == 0) {
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2014-01-23 19:15:29 +01:00
|
|
|
/* MaskGenAlgorithm ::= AlgorithmIdentifier (params = HashAlgorithm) */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_x509_get_alg(&p, end2, &alg_id, &alg_params)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
/* Only MFG1 is recognised for now */
|
2023-01-11 14:50:10 +01: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);
|
|
|
|
}
|
2014-01-23 19:15:29 +01:00
|
|
|
|
|
|
|
/* Parse HashAlgorithm */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = x509_get_hash_alg(&alg_params, mgf_md)) != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2014-01-24 14:15:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-23 16:24:44 +01:00
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
/*
|
|
|
|
* salt_len
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
2)) == 0) {
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_int(&p, end2, salt_len)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2014-01-24 14:15:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-01-23 16:24:44 +01:00
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
/*
|
2014-05-31 17:08:16 +02:00
|
|
|
* trailer_field (if present, must be 1)
|
2014-01-24 14:15:20 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED |
|
|
|
|
3)) == 0) {
|
2014-05-31 17:08:16 +02:00
|
|
|
int trailer_field;
|
|
|
|
|
2014-01-24 14:15:20 +01:00
|
|
|
end2 = p + len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_int(&p, end2, &trailer_field)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG, ret);
|
|
|
|
}
|
2014-01-24 14:15:20 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end2) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2014-05-31 17:08:16 +02:00
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_ALG,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2014-01-23 16:24:44 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-01-23 16:24:44 +01:00
|
|
|
}
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2014-01-23 16:24:44 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int x509_get_attr_type_value(unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
mbedtls_x509_name *cur)
|
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;
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_buf *oid;
|
|
|
|
mbedtls_x509_buf *val;
|
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_SEQUENCE)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
Obey bounds of ASN.1 substructures
When parsing a substructure of an ASN.1 structure, no field within
the substructure must exceed the bounds of the substructure.
Concretely, the `end` pointer passed to the ASN.1 parsing routines
must be updated to point to the end of the substructure while parsing
the latter.
This was previously not the case for the routines
- x509_get_attr_type_and_value(),
- mbedtls_x509_get_crt_ext(),
- mbedtls_x509_get_crl_ext().
These functions kept using the end of the parent structure as the
`end` pointer and would hence allow substructure fields to cross
the substructure boundary. This could lead to successful parsing
of ill-formed X.509 CRTs.
This commit fixes this.
Care has to be taken when adapting `mbedtls_x509_get_crt_ext()`
and `mbedtls_x509_get_crl_ext()`, as the underlying function
`mbedtls_x509_get_ext()` returns `0` if no extensions are present
but doesn't set the variable which holds the bounds of the Extensions
structure in case the latter is present. This commit addresses
this by returning early from `mbedtls_x509_get_crt_ext()` and
`mbedtls_x509_get_crl_ext()` if parsing has reached the end of
the input buffer.
The following X.509 parsing tests need to be adapted:
- "TBSCertificate, issuer two inner set datas"
This test exercises the X.509 CRT parser with a Subject name
which has two empty `AttributeTypeAndValue` structures.
This is supposed to fail with `MBEDTLS_ERR_ASN1_OUT_OF_DATA`
because the parser should attempt to parse the first structure
and fail because of a lack of data. Previously, it failed to
obey the (0-length) bounds of the first AttributeTypeAndValue
structure and would try to interpret the beginning of the second
AttributeTypeAndValue structure as the first field of the first
AttributeTypeAndValue structure, returning an UNEXPECTED_TAG error.
- "TBSCertificate, issuer, no full following string"
This test exercises the parser's behaviour on an AttributeTypeAndValue
structure which contains more data than expected; it should therefore
fail with MBEDTLS_ERR_ASN1_LENGTH_MISMATCH. Because of the missing bounds
check, it previously failed with UNEXPECTED_TAG because it interpreted
the remaining byte in the first AttributeTypeAndValue structure as the
first byte in the second AttributeTypeAndValue structure.
- "SubjectAltName repeated"
This test should exercise two SubjectAltNames extensions in succession,
but a wrong length values makes the second SubjectAltNames extension appear
outside of the Extensions structure. With the new bounds in place, this
therefore fails with a LENGTH_MISMATCH error. This commit adapts the test
data to put the 2nd SubjectAltNames extension inside the Extensions
structure, too.
2019-02-12 18:22:36 +01:00
|
|
|
end = *p + len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
oid = &cur->oid;
|
|
|
|
oid->tag = **p;
|
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
oid->p = *p;
|
|
|
|
*p += oid->len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (**p != MBEDTLS_ASN1_BMP_STRING && **p != MBEDTLS_ASN1_UTF8_STRING &&
|
2015-04-08 12:49:31 +02:00
|
|
|
**p != MBEDTLS_ASN1_T61_STRING && **p != MBEDTLS_ASN1_PRINTABLE_STRING &&
|
|
|
|
**p != MBEDTLS_ASN1_IA5_STRING && **p != MBEDTLS_ASN1_UNIVERSAL_STRING &&
|
2023-01-11 14:50:10 +01:00
|
|
|
**p != MBEDTLS_ASN1_BIT_STRING) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
val = &cur->val;
|
|
|
|
val->tag = *(*p)++;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_len(p, end, &val->len)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME, ret);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
val->p = *p;
|
|
|
|
*p += val->len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_NAME,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
Obey bounds of ASN.1 substructures
When parsing a substructure of an ASN.1 structure, no field within
the substructure must exceed the bounds of the substructure.
Concretely, the `end` pointer passed to the ASN.1 parsing routines
must be updated to point to the end of the substructure while parsing
the latter.
This was previously not the case for the routines
- x509_get_attr_type_and_value(),
- mbedtls_x509_get_crt_ext(),
- mbedtls_x509_get_crl_ext().
These functions kept using the end of the parent structure as the
`end` pointer and would hence allow substructure fields to cross
the substructure boundary. This could lead to successful parsing
of ill-formed X.509 CRTs.
This commit fixes this.
Care has to be taken when adapting `mbedtls_x509_get_crt_ext()`
and `mbedtls_x509_get_crl_ext()`, as the underlying function
`mbedtls_x509_get_ext()` returns `0` if no extensions are present
but doesn't set the variable which holds the bounds of the Extensions
structure in case the latter is present. This commit addresses
this by returning early from `mbedtls_x509_get_crt_ext()` and
`mbedtls_x509_get_crl_ext()` if parsing has reached the end of
the input buffer.
The following X.509 parsing tests need to be adapted:
- "TBSCertificate, issuer two inner set datas"
This test exercises the X.509 CRT parser with a Subject name
which has two empty `AttributeTypeAndValue` structures.
This is supposed to fail with `MBEDTLS_ERR_ASN1_OUT_OF_DATA`
because the parser should attempt to parse the first structure
and fail because of a lack of data. Previously, it failed to
obey the (0-length) bounds of the first AttributeTypeAndValue
structure and would try to interpret the beginning of the second
AttributeTypeAndValue structure as the first field of the first
AttributeTypeAndValue structure, returning an UNEXPECTED_TAG error.
- "TBSCertificate, issuer, no full following string"
This test exercises the parser's behaviour on an AttributeTypeAndValue
structure which contains more data than expected; it should therefore
fail with MBEDTLS_ERR_ASN1_LENGTH_MISMATCH. Because of the missing bounds
check, it previously failed with UNEXPECTED_TAG because it interpreted
the remaining byte in the first AttributeTypeAndValue structure as the
first byte in the second AttributeTypeAndValue structure.
- "SubjectAltName repeated"
This test should exercise two SubjectAltNames extensions in succession,
but a wrong length values makes the second SubjectAltNames extension appear
outside of the Extensions structure. With the new bounds in place, this
therefore fails with a LENGTH_MISMATCH error. This commit adapts the test
data to put the 2nd SubjectAltNames extension inside the Extensions
structure, too.
2019-02-12 18:22:36 +01:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
cur->next = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2015-02-04 18:11:55 +01:00
|
|
|
* Name ::= CHOICE { -- only one possibility for now --
|
|
|
|
* rdnSequence RDNSequence }
|
|
|
|
*
|
|
|
|
* RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
|
|
|
|
*
|
2013-09-16 13:49:26 +02:00
|
|
|
* RelativeDistinguishedName ::=
|
|
|
|
* SET OF AttributeTypeAndValue
|
|
|
|
*
|
|
|
|
* AttributeTypeAndValue ::= SEQUENCE {
|
|
|
|
* type AttributeType,
|
|
|
|
* value AttributeValue }
|
|
|
|
*
|
|
|
|
* AttributeType ::= OBJECT IDENTIFIER
|
|
|
|
*
|
|
|
|
* AttributeValue ::= ANY DEFINED BY AttributeType
|
2014-10-17 12:41:41 +02:00
|
|
|
*
|
2015-02-04 18:11:55 +01:00
|
|
|
* 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
|
2015-04-08 12:49:31 +02:00
|
|
|
* this list, eg mbedtls_x509_dn_gets().
|
2022-10-04 19:12:06 +02:00
|
|
|
*
|
2022-10-17 19:10:23 +02:00
|
|
|
* On success, this function may allocate a linked list starting at cur->next
|
2022-10-04 19:12:06 +02:00
|
|
|
* 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.
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_get_name(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_name *cur)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-10-17 12:41:41 +02:00
|
|
|
size_t set_len;
|
|
|
|
const unsigned char *end_set;
|
2022-10-04 19:12:06 +02:00
|
|
|
mbedtls_x509_name *head = cur;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-12 01:25:31 +01:00
|
|
|
/* don't use recursion, we'd risk stack overflow if not optimized */
|
2023-01-11 14:50:10 +01:00
|
|
|
while (1) {
|
2014-11-12 01:25:31 +01:00
|
|
|
/*
|
2015-02-04 18:11:55 +01:00
|
|
|
* parse SET
|
2014-11-12 01:25:31 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01: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-10-04 19:12:06 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-12 01:25:31 +01:00
|
|
|
end_set = *p + set_len;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (1) {
|
|
|
|
if ((ret = x509_get_attr_type_value(p, end_set, cur)) != 0) {
|
2022-10-04 19:12:06 +02:00
|
|
|
goto error;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-02-04 18:11:55 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p == end_set) {
|
2015-02-04 18:11:55 +01:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-05-12 12:56:41 +02:00
|
|
|
/* Mark this item as being no the only one in a set */
|
2015-02-04 18:11:55 +01:00
|
|
|
cur->next_merged = 1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
|
2015-02-04 18:11:55 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cur->next == NULL) {
|
2022-10-04 19:12:06 +02:00
|
|
|
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
2015-02-04 18:11:55 +01:00
|
|
|
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-12 01:25:31 +01:00
|
|
|
/*
|
|
|
|
* continue until end of SEQUENCE is reached
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p == end) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
cur->next = mbedtls_calloc(1, sizeof(mbedtls_x509_name));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (cur->next == NULL) {
|
2022-10-04 19:12:06 +02:00
|
|
|
ret = MBEDTLS_ERR_X509_ALLOC_FAILED;
|
|
|
|
goto error;
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2014-11-12 01:25:31 +01:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
2022-10-04 19:12:06 +02:00
|
|
|
|
|
|
|
error:
|
|
|
|
/* Skip the first element as we did not allocate it */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_asn1_free_named_data_list_shallow(head->next);
|
2022-06-27 01:32:09 +02:00
|
|
|
head->next = NULL;
|
2022-10-04 19:12:06 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int x509_date_is_valid(const mbedtls_x509_time *t)
|
2016-09-23 14:16:02 +02:00
|
|
|
{
|
2023-07-05 10:58:03 +02:00
|
|
|
unsigned int month_days;
|
|
|
|
unsigned int year;
|
2023-01-11 14:50:10 +01:00
|
|
|
switch (t->mon) {
|
2016-09-23 14:16:02 +02:00
|
|
|
case 1: case 3: case 5: case 7: case 8: case 10: case 12:
|
2023-07-05 10:58:03 +02:00
|
|
|
month_days = 31;
|
2016-09-23 14:16:02 +02:00
|
|
|
break;
|
|
|
|
case 4: case 6: case 9: case 11:
|
2023-07-05 10:58:03 +02:00
|
|
|
month_days = 30;
|
2016-09-23 14:16:02 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
2023-07-05 10:58:03 +02:00
|
|
|
year = (unsigned int) t->year;
|
|
|
|
month_days = ((year & 3) || (!(year % 100)
|
|
|
|
&& (year % 400)))
|
|
|
|
? 28 : 29;
|
2016-09-23 14:16:02 +02:00
|
|
|
break;
|
|
|
|
default:
|
2022-06-30 19:07:55 +02:00
|
|
|
return MBEDTLS_ERR_X509_INVALID_DATE;
|
|
|
|
}
|
|
|
|
|
2023-07-05 11:00:31 +02:00
|
|
|
if ((unsigned int) (t->day - 1) >= month_days || /* (1 - days in month) */
|
2023-07-05 12:15:08 +02:00
|
|
|
/* (unsigned int) (t->mon - 1) >= 12 || */ /* (1 - 12) checked above */
|
2023-07-05 11:00:31 +02:00
|
|
|
(unsigned int) t->year > 9999 || /* (0 - 9999) */
|
|
|
|
(unsigned int) t->hour > 23 || /* (0 - 23) */
|
|
|
|
(unsigned int) t->min > 59 || /* (0 - 59) */
|
|
|
|
(unsigned int) t->sec > 59) { /* (0 - 59) */
|
2022-06-30 19:07:55 +02:00
|
|
|
return MBEDTLS_ERR_X509_INVALID_DATE;
|
2016-09-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2016-09-23 14:16:02 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 19:07:55 +02:00
|
|
|
static int x509_parse2_int(const unsigned char *p)
|
|
|
|
{
|
|
|
|
uint32_t d1 = p[0] - '0';
|
|
|
|
uint32_t d2 = p[1] - '0';
|
|
|
|
return (d1 < 10 && d2 < 10) ? (int) (d1 * 10 + d2) : -1;
|
|
|
|
}
|
|
|
|
|
2017-02-03 13:36:59 +01:00
|
|
|
/*
|
|
|
|
* Parse an ASN1_UTC_TIME (yearlen=2) or ASN1_GENERALIZED_TIME (yearlen=4)
|
|
|
|
* field.
|
|
|
|
*/
|
2022-06-30 19:07:55 +02:00
|
|
|
static int x509_parse_time(const unsigned char *p, mbedtls_x509_time *tm,
|
|
|
|
size_t yearlen)
|
2017-02-03 13:36:59 +01:00
|
|
|
{
|
2022-06-30 19:07:55 +02:00
|
|
|
int x;
|
2017-02-03 13:36:59 +01:00
|
|
|
|
|
|
|
/*
|
2022-06-30 19:07:55 +02:00
|
|
|
* Parse year, month, day, hour, minute, second
|
2017-02-03 13:36:59 +01:00
|
|
|
*/
|
2022-06-30 19:07:55 +02:00
|
|
|
tm->year = x509_parse2_int(p);
|
|
|
|
if (tm->year < 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_X509_INVALID_DATE;
|
|
|
|
}
|
2017-02-03 13:36:59 +01:00
|
|
|
|
2022-06-30 19:07:55 +02:00
|
|
|
if (4 == yearlen) {
|
|
|
|
x = tm->year * 100;
|
|
|
|
p += 2;
|
|
|
|
tm->year = x509_parse2_int(p);
|
|
|
|
if (tm->year < 0) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_DATE;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
|
|
|
} else {
|
2022-06-30 19:07:55 +02:00
|
|
|
x = (tm->year < 50) ? 2000 : 1900;
|
2017-02-03 13:36:59 +01:00
|
|
|
}
|
2022-06-30 19:07:55 +02:00
|
|
|
tm->year += x;
|
2017-02-03 13:36:59 +01:00
|
|
|
|
2022-06-30 19:07:55 +02:00
|
|
|
tm->mon = x509_parse2_int(p + 2);
|
|
|
|
tm->day = x509_parse2_int(p + 4);
|
|
|
|
tm->hour = x509_parse2_int(p + 6);
|
|
|
|
tm->min = x509_parse2_int(p + 8);
|
|
|
|
tm->sec = x509_parse2_int(p + 10);
|
2017-02-03 13:36:59 +01:00
|
|
|
|
2022-06-30 19:07:55 +02:00
|
|
|
return x509_date_is_valid(tm);
|
2017-02-03 13:36:59 +01:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Time ::= CHOICE {
|
|
|
|
* utcTime UTCTime,
|
|
|
|
* generalTime GeneralizedTime }
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_get_time(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_time *tm)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2017-02-03 13:36:59 +01:00
|
|
|
size_t len, year_len;
|
2013-09-16 13:49:26 +02:00
|
|
|
unsigned char tag;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
tag = **p;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (tag == MBEDTLS_ASN1_UTC_TIME) {
|
2017-02-03 13:36:59 +01:00
|
|
|
year_len = 2;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else if (tag == MBEDTLS_ASN1_GENERALIZED_TIME) {
|
2017-02-03 13:36:59 +01:00
|
|
|
year_len = 4;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
2017-02-03 13:36:59 +01:00
|
|
|
|
|
|
|
(*p)++;
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_len(p, end, &len);
|
2017-02-03 13:36:59 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_DATE, ret);
|
|
|
|
}
|
2017-02-03 13:36:59 +01:00
|
|
|
|
2022-06-30 19:07:55 +02:00
|
|
|
/* len is 12 or 14 depending on year_len, plus optional trailing 'Z' */
|
|
|
|
if (len != year_len + 10 &&
|
|
|
|
!(len == year_len + 11 && (*p)[(len - 1)] == 'Z')) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_DATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*p) += len;
|
|
|
|
return x509_parse_time(*p - len, tm, year_len);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_get_sig(unsigned char **p, const unsigned char *end, mbedtls_x509_buf *sig)
|
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;
|
2016-09-19 17:58:45 +02:00
|
|
|
int tag_type;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((end - *p) < 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE,
|
|
|
|
MBEDTLS_ERR_ASN1_OUT_OF_DATA);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2016-09-19 17:58:45 +02:00
|
|
|
tag_type = **p;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_bitstring_null(p, end, &len)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_SIGNATURE, ret);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2016-09-19 17:58:45 +02:00
|
|
|
sig->tag = tag_type;
|
2013-09-16 13:49:26 +02:00
|
|
|
sig->len = len;
|
|
|
|
sig->p = *p;
|
|
|
|
|
|
|
|
*p += len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2014-01-24 19:28:43 +01:00
|
|
|
/*
|
|
|
|
* Get signature algorithm from alg OID and optional parameters
|
|
|
|
*/
|
2023-01-11 14:50:10 +01: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)
|
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 (*sig_opts != NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
2014-06-05 15:14:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
|
|
|
}
|
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
|
|
|
if (*pk_alg == MBEDTLS_PK_RSASSA_PSS) {
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_pk_rsassa_pss_options *pss_opts;
|
2014-06-05 15:14:28 +02:00
|
|
|
|
2023-01-11 14:50:10 +01: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;
|
2014-06-05 15:14:28 +02:00
|
|
|
}
|
2014-01-24 19:28:43 +01:00
|
|
|
|
2014-06-05 15:14:28 +02:00
|
|
|
*sig_opts = (void *) pss_opts;
|
2023-01-11 14:50:10 +01:00
|
|
|
} else
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2014-01-24 19:28:43 +01:00
|
|
|
{
|
|
|
|
/* Make sure parameters are absent or NULL */
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((sig_params->tag != MBEDTLS_ASN1_NULL && sig_params->tag != 0) ||
|
|
|
|
sig_params->len != 0) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_ALG;
|
|
|
|
}
|
2014-01-24 19:28:43 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* X.509 Extensions (No parsing of extensions, pointer should
|
2016-11-06 13:45:15 +01:00
|
|
|
* be either manually updated or extensions should be parsed!)
|
2013-09-16 13:49:26 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_get_ext(unsigned char **p, const unsigned char *end,
|
|
|
|
mbedtls_x509_buf *ext, int tag)
|
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;
|
|
|
|
|
2019-02-11 15:33:36 +01: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-01-11 14:50:10 +01: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);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
Always return a high-level error code from X.509 module
Some functions within the X.509 module return an ASN.1 low level
error code where instead this error code should be wrapped by a
high-level X.509 error code as in the bulk of the module.
Specifically, the following functions are affected:
- mbedtls_x509_get_ext()
- x509_get_version()
- x509_get_uid()
This commit modifies these functions to always return an
X.509 high level error code.
Care has to be taken when adapting `mbetls_x509_get_ext()`:
Currently, the callers `mbedtls_x509_crt_ext()` treat the
return code `MBEDTLS_ERR_ASN1_UNEXPECTED_TAG` specially to
gracefully detect and continue if the extension structure is not
present. Wrapping the ASN.1 error with
`MBEDTLS_ERR_X509_INVALID_EXTENSIONS` and adapting the check
accordingly would mean that an unexpected tag somewhere
down the extension parsing would be ignored by the caller.
The way out of this is the following: Luckily, the extension
structure is always the last field in the surrounding structure,
so if there is some data remaining, it must be an Extension
structure, so we don't need to deal with a tag mismatch gracefully
in the first place.
We may therefore wrap the return code from the initial call to
`mbedtls_asn1_get_tag()` in `mbedtls_x509_get_ext()` by
`MBEDTLS_ERR_X509_INVALID_EXTENSIONS` and simply remove
the special treatment of `MBEDTLS_ERR_ASN1_UNEXPECTED_TAG`
in the callers `x509_crl_get_ext()` and `x509_crt_get_ext()`.
This renders `mbedtls_x509_get_ext()` unsuitable if it ever
happened that an Extension structure is optional and does not
occur at the end of its surrounding structure, but for CRTs
and CRLs, it's fine.
The following tests need to be adapted:
- "TBSCertificate v3, issuerID wrong tag"
The issuerID is optional, so if we look for its presence
but find a different tag, we silently continue and try
parsing the subjectID, and then the extensions. The tag '00'
used in this test doesn't match either of these, and the
previous code would hence return LENGTH_MISMATCH after
unsucessfully trying issuerID, subjectID and Extensions.
With the new code, any data remaining after issuerID and
subjectID _must_ be Extension data, so we fail with
UNEXPECTED_TAG when trying to parse the Extension data.
- "TBSCertificate v3, UIDs, invalid length"
The test hardcodes the expectation of
MBEDTLS_ERR_ASN1_INVALID_LENGTH, which needs to be
wrapped in MBEDTLS_ERR_X509_INVALID_FORMAT now.
Fixes #2431.
2019-02-12 12:52:10 +01:00
|
|
|
ext->tag = MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_ASN1_CONSTRUCTED | tag;
|
|
|
|
ext->p = *p;
|
|
|
|
end = *p + ext->len;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
|
|
|
|
*/
|
2023-01-11 14:50:10 +01: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);
|
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end != *p + len) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
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-07-24 17:35:57 +02:00
|
|
|
static char nibble_to_hex_digit(int i)
|
2023-07-21 16:47:47 +02:00
|
|
|
{
|
2023-07-24 17:43:36 +02:00
|
|
|
return (i < 10) ? (i + '0') : (i - 10 + 'A');
|
2023-07-21 16:47:47 +02:00
|
|
|
}
|
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Store the name in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_dn_gets(char *buf, size_t size, const mbedtls_x509_name *dn)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-08-10 16:50:57 +02:00
|
|
|
size_t i, j, n, asn1_len_size, asn1_tag_size, asn1_tag_len_buf_start;
|
2023-09-12 18:54:43 +02:00
|
|
|
/* 6 is enough as our asn1 write functions only write one byte for the tag and at most five bytes for the length*/
|
|
|
|
unsigned char asn1_tag_len_buf[6];
|
2023-07-26 16:51:49 +02:00
|
|
|
unsigned char *asn1_len_p;
|
2015-02-04 18:11:55 +01:00
|
|
|
unsigned char c, merge = 0;
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_x509_name *name;
|
2013-09-16 13:49:26 +02:00
|
|
|
const char *short_name = NULL;
|
2023-08-02 18:08:52 +02:00
|
|
|
char lowbits, highbits;
|
2015-04-08 12:49:31 +02:00
|
|
|
char s[MBEDTLS_X509_MAX_DN_NAME_SIZE], *p;
|
2023-07-31 18:25:44 +02:00
|
|
|
int print_hexstring;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(s, 0, sizeof(s));
|
2013-09-16 13:49:26 +02:00
|
|
|
|
|
|
|
name = dn;
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
while (name != NULL) {
|
|
|
|
if (!name->oid.p) {
|
2013-09-16 13:49:26 +02:00
|
|
|
name = name->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (name != dn) {
|
|
|
|
ret = mbedtls_snprintf(p, n, merge ? " + " : ", ");
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:05:57 +02:00
|
|
|
print_hexstring = (name->val.tag != MBEDTLS_ASN1_UTF8_STRING) &&
|
|
|
|
(name->val.tag != MBEDTLS_ASN1_PRINTABLE_STRING) &&
|
|
|
|
(name->val.tag != MBEDTLS_ASN1_IA5_STRING);
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-07-31 18:25:44 +02:00
|
|
|
if ((ret = mbedtls_oid_get_attr_short_name(&name->oid, &short_name)) == 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "%s=", short_name);
|
|
|
|
} else {
|
2023-08-10 17:39:23 +02:00
|
|
|
if ((ret = mbedtls_oid_get_numeric_string(p, n, &name->oid)) > 0) {
|
2023-08-22 17:37:11 +02:00
|
|
|
n -= ret;
|
|
|
|
p += ret;
|
2023-08-10 17:39:23 +02:00
|
|
|
ret = mbedtls_snprintf(p, n, "=");
|
2023-07-31 18:25:44 +02:00
|
|
|
print_hexstring = 1;
|
2023-08-22 17:42:27 +02:00
|
|
|
} else if (ret == MBEDTLS_ERR_OID_BUF_TOO_SMALL) {
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
2023-08-22 18:50:00 +02:00
|
|
|
} else {
|
2023-07-26 16:51:49 +02:00
|
|
|
ret = mbedtls_snprintf(p, n, "\?\?=");
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-08-01 12:10:52 +02:00
|
|
|
if (print_hexstring) {
|
2023-07-26 16:51:49 +02:00
|
|
|
s[0] = '#';
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-09-12 18:54:43 +02:00
|
|
|
asn1_len_p = asn1_tag_len_buf + sizeof(asn1_tag_len_buf);
|
2023-08-22 18:50:00 +02:00
|
|
|
if ((ret = mbedtls_asn1_write_len(&asn1_len_p, asn1_tag_len_buf, name->val.len)) < 0) {
|
2023-08-22 17:29:39 +02:00
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
}
|
|
|
|
asn1_len_size = ret;
|
2023-08-22 18:50:00 +02:00
|
|
|
if ((ret = mbedtls_asn1_write_tag(&asn1_len_p, asn1_tag_len_buf, name->val.tag)) < 0) {
|
2023-08-22 17:29:39 +02:00
|
|
|
return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
}
|
|
|
|
asn1_tag_size = ret;
|
2023-09-12 18:54:43 +02:00
|
|
|
asn1_tag_len_buf_start = sizeof(asn1_tag_len_buf) - asn1_len_size - asn1_tag_size;
|
2023-08-10 16:50:57 +02:00
|
|
|
for (i = 0, j = 1; i < asn1_len_size + asn1_tag_size; i++) {
|
2023-01-11 14:50:10 +01:00
|
|
|
if (j + 1 >= sizeof(s) - 1) {
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2023-08-10 16:50:57 +02:00
|
|
|
c = asn1_tag_len_buf[asn1_tag_len_buf_start+i];
|
|
|
|
lowbits = (c & 0x0F);
|
|
|
|
highbits = c >> 4;
|
|
|
|
s[j++] = nibble_to_hex_digit(highbits);
|
|
|
|
s[j++] = nibble_to_hex_digit(lowbits);
|
2022-05-20 13:48:46 +02:00
|
|
|
}
|
2023-08-10 16:50:57 +02:00
|
|
|
for (i = 0; i < name->val.len; i++) {
|
|
|
|
if (j + 1 >= sizeof(s) - 1) {
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
2023-07-26 16:51:49 +02:00
|
|
|
}
|
2023-08-10 16:50:57 +02:00
|
|
|
c = name->val.p[i];
|
2023-08-02 18:08:52 +02:00
|
|
|
lowbits = (c & 0x0F);
|
2023-08-10 15:37:00 +02:00
|
|
|
highbits = c >> 4;
|
2023-07-24 17:35:57 +02:00
|
|
|
s[j++] = nibble_to_hex_digit(highbits);
|
2023-08-10 16:50:57 +02:00
|
|
|
s[j++] = nibble_to_hex_digit(lowbits);
|
2023-07-26 16:51:49 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0, j = 0; i < name->val.len; i++, j++) {
|
|
|
|
if (j >= sizeof(s) - 1) {
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
c = name->val.p[i];
|
|
|
|
// Special characters requiring escaping, RFC 4514 Section 2.4
|
2023-08-22 17:56:04 +02:00
|
|
|
if (c == '\0') {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_NAME;
|
2023-08-22 18:50:00 +02:00
|
|
|
} else {
|
2023-08-10 17:03:27 +02:00
|
|
|
if (strchr(",=+<>;\"\\", c) ||
|
2023-07-26 16:51:49 +02:00
|
|
|
((i == 0) && strchr("# ", c)) ||
|
|
|
|
((i == name->val.len-1) && (c == ' '))) {
|
|
|
|
if (j + 1 >= sizeof(s) - 1) {
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
s[j++] = '\\';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (c < 32 || c >= 127) {
|
|
|
|
if (j + 3 >= sizeof(s) - 1) {
|
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
s[j++] = '\\';
|
2023-08-02 18:08:52 +02:00
|
|
|
lowbits = (c & 0x0F);
|
2023-08-10 15:37:00 +02:00
|
|
|
highbits = c >> 4;
|
2023-07-26 16:51:49 +02:00
|
|
|
s[j++] = nibble_to_hex_digit(highbits);
|
|
|
|
s[j] = nibble_to_hex_digit(lowbits);
|
|
|
|
} else {
|
|
|
|
s[j] = c;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
2022-05-20 13:48:46 +02:00
|
|
|
s[j] = '\0';
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "%s", s);
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2015-02-04 18:11:55 +01:00
|
|
|
|
|
|
|
merge = name->next_merged;
|
2013-09-16 13:49:26 +02:00
|
|
|
name = name->next;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) (size - n);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Store the serial in printable form into buf; no more
|
|
|
|
* than size characters will be written
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_serial_gets(char *buf, size_t size, const mbedtls_x509_buf *serial)
|
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 i, n, nr;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
p = buf;
|
|
|
|
n = size;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
nr = (serial->len <= 32)
|
2013-09-16 13:49:26 +02:00
|
|
|
? serial->len : 28;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
for (i = 0; i < nr; i++) {
|
|
|
|
if (i == 0 && nr > 1 && serial->p[i] == 0x0) {
|
2013-09-16 13:49:26 +02:00
|
|
|
continue;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "%02X%s",
|
|
|
|
serial->p[i], (i < nr - 1) ? ":" : "");
|
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 (nr != serial->len) {
|
|
|
|
ret = mbedtls_snprintf(p, n, "....");
|
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
|
|
|
return (int) (size - n);
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2020-10-09 10:19:39 +02:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
2014-01-25 11:50:59 +01:00
|
|
|
/*
|
2014-06-05 15:41:39 +02:00
|
|
|
* Helper for writing signature algorithms
|
2014-01-25 11:50:59 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01: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)
|
2014-01-25 11:50:59 +01:00
|
|
|
{
|
2019-12-16 12:46:15 +01:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2014-01-25 11:50:59 +01:00
|
|
|
char *p = buf;
|
|
|
|
size_t n = size;
|
|
|
|
const char *desc = NULL;
|
|
|
|
|
2023-01-11 14:50:10 +01: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);
|
|
|
|
}
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
|
2023-01-11 14:50:10 +01:00
|
|
|
if (pk_alg == MBEDTLS_PK_RSASSA_PSS) {
|
2015-04-08 12:49:31 +02:00
|
|
|
const mbedtls_pk_rsassa_pss_options *pss_opts;
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2015-04-08 12:49:31 +02:00
|
|
|
pss_opts = (const mbedtls_pk_rsassa_pss_options *) sig_opts;
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
const char *name = md_type_to_string(md_alg);
|
|
|
|
const char *mgf_name = md_type_to_string(pss_opts->mgf1_hash_id);
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, " (%s, MGF1-%s, 0x%02X)",
|
|
|
|
name ? name : "???",
|
|
|
|
mgf_name ? mgf_name : "???",
|
|
|
|
(unsigned int) pss_opts->expected_salt_len);
|
2015-06-22 11:12:02 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2014-01-25 11:50:59 +01:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
((void) pk_alg);
|
2014-06-05 15:41:39 +02:00
|
|
|
((void) md_alg);
|
|
|
|
((void) sig_opts);
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return (int) (size - n);
|
2014-01-25 11:50:59 +01:00
|
|
|
}
|
2020-10-09 10:19:39 +02:00
|
|
|
#endif /* MBEDTLS_X509_REMOVE_INFO */
|
2014-01-25 11:50:59 +01:00
|
|
|
|
2013-09-16 13:49:26 +02:00
|
|
|
/*
|
|
|
|
* Helper for writing "RSA key size", "EC key size", etc
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_key_size_helper(char *buf, size_t buf_size, const char *name)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
char *p = buf;
|
2015-06-18 16:25:56 +02:00
|
|
|
size_t n = buf_size;
|
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
|
|
|
ret = mbedtls_snprintf(p, n, "%s key size", name);
|
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
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 06:38:53 +02:00
|
|
|
int mbedtls_x509_time_cmp(const mbedtls_x509_time *t1,
|
|
|
|
const mbedtls_x509_time *t2)
|
2015-06-22 18:59:21 +02:00
|
|
|
{
|
2022-06-30 10:38:02 +02:00
|
|
|
int x;
|
2015-05-29 10:11:03 +02:00
|
|
|
|
2022-06-30 10:38:02 +02:00
|
|
|
x = (((t1->year << 9) | (t1->mon << 5) | (t1->day)) -
|
|
|
|
((t2->year << 9) | (t2->mon << 5) | (t2->day)));
|
|
|
|
if (x != 0) {
|
|
|
|
return x;
|
2015-06-22 18:59:21 +02:00
|
|
|
}
|
2015-05-29 10:11:03 +02:00
|
|
|
|
2022-06-30 10:38:02 +02:00
|
|
|
x = (((t1->hour << 12) | (t1->min << 6) | (t1->sec)) -
|
|
|
|
((t2->hour << 12) | (t2->min << 6) | (t2->sec)));
|
|
|
|
return x;
|
2014-03-10 12:26:11 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2015-06-18 20:52:58 +02:00
|
|
|
#if defined(MBEDTLS_HAVE_TIME_DATE)
|
2022-06-30 11:25:56 +02:00
|
|
|
int mbedtls_x509_time_gmtime(mbedtls_time_t tt, mbedtls_x509_time *now)
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
2022-06-30 11:28:50 +02:00
|
|
|
struct tm tm;
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2022-06-30 11:28:50 +02:00
|
|
|
if (mbedtls_platform_gmtime_r(&tt, &tm) == NULL) {
|
|
|
|
return -1;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2013-09-16 13:49:26 +02:00
|
|
|
|
2022-06-30 11:28:50 +02:00
|
|
|
now->year = tm.tm_year + 1900;
|
|
|
|
now->mon = tm.tm_mon + 1;
|
|
|
|
now->day = tm.tm_mday;
|
|
|
|
now->hour = tm.tm_hour;
|
|
|
|
now->min = tm.tm_min;
|
|
|
|
now->sec = tm.tm_sec;
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2022-06-30 11:25:56 +02:00
|
|
|
static int x509_get_current_time(mbedtls_x509_time *now)
|
|
|
|
{
|
|
|
|
return mbedtls_x509_time_gmtime(mbedtls_time(NULL), now);
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_time now;
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (x509_get_current_time(&now) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2022-06-30 06:38:53 +02:00
|
|
|
return mbedtls_x509_time_cmp(to, &now) < 0;
|
2014-03-10 12:26:11 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
2015-04-08 12:49:31 +02:00
|
|
|
mbedtls_x509_time now;
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (x509_get_current_time(&now) != 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2022-06-30 06:38:53 +02:00
|
|
|
return mbedtls_x509_time_cmp(from, &now) > 0;
|
2014-03-10 12:26:11 +01:00
|
|
|
}
|
|
|
|
|
2015-06-18 20:52:58 +02:00
|
|
|
#else /* MBEDTLS_HAVE_TIME_DATE */
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_time_is_past(const mbedtls_x509_time *to)
|
2013-09-16 13:49:26 +02:00
|
|
|
{
|
|
|
|
((void) to);
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2013-09-16 13:49:26 +02:00
|
|
|
}
|
2014-03-10 12:26:11 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_x509_time_is_future(const mbedtls_x509_time *from)
|
2014-03-10 12:26:11 +01:00
|
|
|
{
|
|
|
|
((void) from);
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2014-03-10 12:26:11 +01:00
|
|
|
}
|
2015-06-18 20:52:58 +02:00
|
|
|
#endif /* MBEDTLS_HAVE_TIME_DATE */
|
2023-01-17 10:26:40 +01:00
|
|
|
|
|
|
|
/* Common functions for parsing CRT and CSR. */
|
|
|
|
#if defined(MBEDTLS_X509_CRT_PARSE_C) || defined(MBEDTLS_X509_CSR_PARSE_C)
|
|
|
|
/*
|
|
|
|
* OtherName ::= SEQUENCE {
|
|
|
|
* type-id OBJECT IDENTIFIER,
|
|
|
|
* value [0] EXPLICIT ANY DEFINED BY type-id }
|
|
|
|
*
|
|
|
|
* HardwareModuleName ::= SEQUENCE {
|
|
|
|
* hwType OBJECT IDENTIFIER,
|
|
|
|
* hwSerialNum OCTET STRING }
|
|
|
|
*
|
|
|
|
* NOTE: we currently only parse and use otherName of type HwModuleName,
|
|
|
|
* as defined in RFC 4108.
|
|
|
|
*/
|
|
|
|
static int x509_get_other_name(const mbedtls_x509_buf *subject_alt_name,
|
|
|
|
mbedtls_x509_san_other_name *other_name)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
size_t len;
|
|
|
|
unsigned char *p = subject_alt_name->p;
|
|
|
|
const unsigned char *end = p + subject_alt_name->len;
|
|
|
|
mbedtls_x509_buf cur_oid;
|
|
|
|
|
|
|
|
if ((subject_alt_name->tag &
|
|
|
|
(MBEDTLS_ASN1_TAG_CLASS_MASK | MBEDTLS_ASN1_TAG_VALUE_MASK)) !=
|
|
|
|
(MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME)) {
|
|
|
|
/*
|
|
|
|
* The given subject alternative name is not of type "othername".
|
|
|
|
*/
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_OID)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
cur_oid.tag = MBEDTLS_ASN1_OID;
|
|
|
|
cur_oid.p = p;
|
|
|
|
cur_oid.len = len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Only HwModuleName is currently supported.
|
|
|
|
*/
|
|
|
|
if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME, &cur_oid) != 0) {
|
|
|
|
return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2023-08-18 19:36:02 +02:00
|
|
|
other_name->type_id = cur_oid;
|
2023-01-17 10:26:40 +01:00
|
|
|
|
|
|
|
p += len;
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC)) !=
|
|
|
|
0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
|
|
|
|
2019-09-13 15:21:13 +02:00
|
|
|
if (end != p + len) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:26:40 +01: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);
|
|
|
|
}
|
|
|
|
|
2019-09-13 15:21:13 +02:00
|
|
|
if (end != p + len) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:26:40 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
other_name->value.hardware_module_name.oid.tag = MBEDTLS_ASN1_OID;
|
|
|
|
other_name->value.hardware_module_name.oid.p = p;
|
|
|
|
other_name->value.hardware_module_name.oid.len = len;
|
|
|
|
|
|
|
|
p += len;
|
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
|
|
|
|
MBEDTLS_ASN1_OCTET_STRING)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
other_name->value.hardware_module_name.val.tag = MBEDTLS_ASN1_OCTET_STRING;
|
|
|
|
other_name->value.hardware_module_name.val.p = p;
|
|
|
|
other_name->value.hardware_module_name.val.len = len;
|
|
|
|
p += len;
|
|
|
|
if (p != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-21 08:32:37 +01:00
|
|
|
/* Check mbedtls_x509_get_subject_alt_name for detailed description.
|
2023-01-17 10:26:40 +01:00
|
|
|
*
|
2023-02-21 08:32:37 +01:00
|
|
|
* In some cases while parsing subject alternative names the sequence tag is optional
|
|
|
|
* (e.g. CertSerialNumber). This function is designed to handle such case.
|
2023-04-04 22:49:44 +02:00
|
|
|
*/
|
2023-02-21 08:32:37 +01:00
|
|
|
int mbedtls_x509_get_subject_alt_name_ext(unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
mbedtls_x509_sequence *subject_alt_name)
|
2023-01-17 10:26:40 +01:00
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2023-02-21 08:32:37 +01:00
|
|
|
size_t tag_len;
|
2023-01-17 10:26:40 +01:00
|
|
|
mbedtls_asn1_sequence *cur = subject_alt_name;
|
|
|
|
|
|
|
|
while (*p < end) {
|
2023-04-30 20:11:49 +02:00
|
|
|
mbedtls_x509_subject_alternative_name tmp_san_name;
|
2019-09-13 13:24:56 +02:00
|
|
|
mbedtls_x509_buf tmp_san_buf;
|
2023-04-30 20:11:49 +02:00
|
|
|
memset(&tmp_san_name, 0, sizeof(tmp_san_name));
|
2023-01-17 10:26:40 +01:00
|
|
|
|
2019-09-13 13:24:56 +02:00
|
|
|
tmp_san_buf.tag = **p;
|
2023-01-17 10:26:40 +01:00
|
|
|
(*p)++;
|
2019-09-13 13:24:56 +02:00
|
|
|
|
2023-01-17 10:26:40 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_len(p, end, &tag_len)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
|
|
|
|
2019-09-13 13:24:56 +02:00
|
|
|
tmp_san_buf.p = *p;
|
|
|
|
tmp_san_buf.len = tag_len;
|
|
|
|
|
|
|
|
if ((tmp_san_buf.tag & MBEDTLS_ASN1_TAG_CLASS_MASK) !=
|
2023-01-17 10:26:40 +01:00
|
|
|
MBEDTLS_ASN1_CONTEXT_SPECIFIC) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_UNEXPECTED_TAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2023-04-30 20:11:49 +02:00
|
|
|
* Check that the SAN is structured correctly by parsing it.
|
|
|
|
* The SAN structure is discarded afterwards.
|
2023-01-17 10:26:40 +01:00
|
|
|
*/
|
2023-04-30 20:11:49 +02:00
|
|
|
ret = mbedtls_x509_parse_subject_alt_name(&tmp_san_buf, &tmp_san_name);
|
2023-01-17 10:26:40 +01:00
|
|
|
/*
|
|
|
|
* In case the extension is malformed, return an error,
|
|
|
|
* and clear the allocated sequences.
|
|
|
|
*/
|
|
|
|
if (ret != 0 && ret != MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
|
|
|
|
mbedtls_asn1_sequence_free(subject_alt_name->next);
|
|
|
|
subject_alt_name->next = NULL;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-04-30 20:11:49 +02:00
|
|
|
mbedtls_x509_free_subject_alt_name(&tmp_san_name);
|
2023-01-17 10:26:40 +01:00
|
|
|
/* Allocate and assign next pointer */
|
|
|
|
if (cur->buf.p != NULL) {
|
|
|
|
if (cur->next != NULL) {
|
|
|
|
return MBEDTLS_ERR_X509_INVALID_EXTENSIONS;
|
|
|
|
}
|
|
|
|
|
|
|
|
cur->next = mbedtls_calloc(1, sizeof(mbedtls_asn1_sequence));
|
|
|
|
|
|
|
|
if (cur->next == NULL) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_ALLOC_FAILED);
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
2019-09-13 13:24:56 +02:00
|
|
|
cur->buf = tmp_san_buf;
|
|
|
|
*p += tmp_san_buf.len;
|
2023-01-17 10:26:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set final sequence entry's next pointer to NULL */
|
|
|
|
cur->next = NULL;
|
|
|
|
|
|
|
|
if (*p != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-21 08:32:37 +01:00
|
|
|
/*
|
|
|
|
* SubjectAltName ::= GeneralNames
|
|
|
|
*
|
|
|
|
* GeneralNames ::= SEQUENCE SIZE (1..MAX) OF GeneralName
|
|
|
|
*
|
|
|
|
* GeneralName ::= CHOICE {
|
|
|
|
* otherName [0] OtherName,
|
|
|
|
* rfc822Name [1] IA5String,
|
|
|
|
* dNSName [2] IA5String,
|
|
|
|
* x400Address [3] ORAddress,
|
|
|
|
* directoryName [4] Name,
|
|
|
|
* ediPartyName [5] EDIPartyName,
|
|
|
|
* uniformResourceIdentifier [6] IA5String,
|
|
|
|
* iPAddress [7] OCTET STRING,
|
|
|
|
* registeredID [8] OBJECT IDENTIFIER }
|
|
|
|
*
|
|
|
|
* OtherName ::= SEQUENCE {
|
|
|
|
* type-id OBJECT IDENTIFIER,
|
|
|
|
* value [0] EXPLICIT ANY DEFINED BY type-id }
|
|
|
|
*
|
|
|
|
* EDIPartyName ::= SEQUENCE {
|
|
|
|
* nameAssigner [0] DirectoryString OPTIONAL,
|
|
|
|
* partyName [1] DirectoryString }
|
|
|
|
*
|
|
|
|
* We list all types, but use the following GeneralName types from RFC 5280:
|
|
|
|
* "dnsName", "uniformResourceIdentifier" and "hardware_module_name"
|
|
|
|
* of type "otherName", as defined in RFC 4108.
|
|
|
|
*/
|
|
|
|
int mbedtls_x509_get_subject_alt_name(unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
mbedtls_x509_sequence *subject_alt_name)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t len;
|
|
|
|
|
|
|
|
/* Get main sequence tag */
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p + len != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mbedtls_x509_get_subject_alt_name_ext(p, end, subject_alt_name);
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:26:40 +01:00
|
|
|
int mbedtls_x509_get_ns_cert_type(unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
unsigned char *ns_cert_type)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
mbedtls_x509_bitstring bs = { 0, 0, NULL };
|
|
|
|
|
|
|
|
if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
|
|
|
|
2023-01-25 14:26:15 +01:00
|
|
|
/* A bitstring with no flags set is still technically valid, as it will mean
|
|
|
|
that the certificate has no designated purpose at the time of creation. */
|
2023-01-17 10:26:40 +01:00
|
|
|
if (bs.len == 0) {
|
|
|
|
*ns_cert_type = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bs.len != 1) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS,
|
|
|
|
MBEDTLS_ERR_ASN1_INVALID_LENGTH);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get actual bitstring */
|
|
|
|
*ns_cert_type = *bs.p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_x509_get_key_usage(unsigned char **p,
|
|
|
|
const unsigned char *end,
|
|
|
|
unsigned int *key_usage)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t i;
|
|
|
|
mbedtls_x509_bitstring bs = { 0, 0, NULL };
|
|
|
|
|
|
|
|
if ((ret = mbedtls_asn1_get_bitstring(p, end, &bs)) != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_X509_INVALID_EXTENSIONS, ret);
|
|
|
|
}
|
|
|
|
|
2023-01-25 14:26:15 +01:00
|
|
|
/* A bitstring with no flags set is still technically valid, as it will mean
|
|
|
|
that the certificate has no designated purpose at the time of creation. */
|
2023-01-17 10:26:40 +01:00
|
|
|
if (bs.len == 0) {
|
|
|
|
*key_usage = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get actual bitstring */
|
|
|
|
*key_usage = 0;
|
|
|
|
for (i = 0; i < bs.len && i < sizeof(unsigned int); i++) {
|
|
|
|
*key_usage |= (unsigned int) bs.p[i] << (8*i);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int mbedtls_x509_parse_subject_alt_name(const mbedtls_x509_buf *san_buf,
|
|
|
|
mbedtls_x509_subject_alternative_name *san)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
switch (san_buf->tag &
|
|
|
|
(MBEDTLS_ASN1_TAG_CLASS_MASK |
|
|
|
|
MBEDTLS_ASN1_TAG_VALUE_MASK)) {
|
|
|
|
/*
|
|
|
|
* otherName
|
|
|
|
*/
|
|
|
|
case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_OTHER_NAME):
|
|
|
|
{
|
|
|
|
mbedtls_x509_san_other_name other_name;
|
|
|
|
|
|
|
|
ret = x509_get_other_name(san_buf, &other_name);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
|
|
|
|
san->type = MBEDTLS_X509_SAN_OTHER_NAME;
|
|
|
|
memcpy(&san->san.other_name,
|
|
|
|
&other_name, sizeof(other_name));
|
|
|
|
|
|
|
|
}
|
|
|
|
break;
|
2023-02-13 16:03:07 +01:00
|
|
|
/*
|
|
|
|
* uniformResourceIdentifier
|
|
|
|
*/
|
|
|
|
case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER):
|
|
|
|
{
|
|
|
|
memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
|
|
|
|
san->type = MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER;
|
|
|
|
|
|
|
|
memcpy(&san->san.unstructured_name,
|
|
|
|
san_buf, sizeof(*san_buf));
|
2023-01-17 10:26:40 +01:00
|
|
|
|
2023-02-13 16:03:07 +01:00
|
|
|
}
|
|
|
|
break;
|
2023-01-17 10:26:40 +01:00
|
|
|
/*
|
|
|
|
* dNSName
|
|
|
|
*/
|
|
|
|
case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DNS_NAME):
|
|
|
|
{
|
|
|
|
memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
|
|
|
|
san->type = MBEDTLS_X509_SAN_DNS_NAME;
|
|
|
|
|
|
|
|
memcpy(&san->san.unstructured_name,
|
|
|
|
san_buf, sizeof(*san_buf));
|
2023-02-09 14:43:49 +01:00
|
|
|
}
|
|
|
|
break;
|
2023-05-29 16:30:50 +02:00
|
|
|
/*
|
|
|
|
* IP address
|
|
|
|
*/
|
|
|
|
case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_IP_ADDRESS):
|
|
|
|
{
|
|
|
|
memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
|
|
|
|
san->type = MBEDTLS_X509_SAN_IP_ADDRESS;
|
2023-06-05 16:38:13 +02:00
|
|
|
// Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
|
|
|
|
if (san_buf->len == 4 || san_buf->len == 16) {
|
|
|
|
memcpy(&san->san.unstructured_name,
|
|
|
|
san_buf, sizeof(*san_buf));
|
|
|
|
} else {
|
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
|
|
|
}
|
2023-05-29 16:30:50 +02:00
|
|
|
}
|
|
|
|
break;
|
2023-02-09 14:43:49 +01:00
|
|
|
/*
|
2023-04-30 20:11:49 +02:00
|
|
|
* rfc822Name
|
2023-02-09 14:43:49 +01:00
|
|
|
*/
|
|
|
|
case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_RFC822_NAME):
|
|
|
|
{
|
|
|
|
memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
|
|
|
|
san->type = MBEDTLS_X509_SAN_RFC822_NAME;
|
|
|
|
memcpy(&san->san.unstructured_name, san_buf, sizeof(*san_buf));
|
2023-01-17 10:26:40 +01:00
|
|
|
}
|
|
|
|
break;
|
2023-01-10 12:47:38 +01:00
|
|
|
/*
|
|
|
|
* directoryName
|
|
|
|
*/
|
|
|
|
case (MBEDTLS_ASN1_CONTEXT_SPECIFIC | MBEDTLS_X509_SAN_DIRECTORY_NAME):
|
|
|
|
{
|
|
|
|
size_t name_len;
|
|
|
|
unsigned char *p = san_buf->p;
|
|
|
|
memset(san, 0, sizeof(mbedtls_x509_subject_alternative_name));
|
|
|
|
san->type = MBEDTLS_X509_SAN_DIRECTORY_NAME;
|
|
|
|
|
|
|
|
ret = mbedtls_asn1_get_tag(&p, p + san_buf->len, &name_len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
|
|
|
|
|
|
|
|
if (ret != 0) {
|
2023-02-13 13:13:30 +01:00
|
|
|
return ret;
|
2023-01-10 12:47:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = mbedtls_x509_get_name(&p, p + name_len,
|
|
|
|
&san->san.directory_name)) != 0) {
|
2023-02-13 13:13:30 +01:00
|
|
|
return ret;
|
2023-01-10 12:47:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-01-17 10:26:40 +01:00
|
|
|
/*
|
|
|
|
* Type not supported
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
return MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:01:59 +01:00
|
|
|
void mbedtls_x509_free_subject_alt_name(mbedtls_x509_subject_alternative_name *san)
|
|
|
|
{
|
|
|
|
if (san->type == MBEDTLS_X509_SAN_DIRECTORY_NAME) {
|
|
|
|
mbedtls_asn1_free_named_data_list_shallow(san->san.directory_name.next);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:26:40 +01:00
|
|
|
#if !defined(MBEDTLS_X509_REMOVE_INFO)
|
|
|
|
int mbedtls_x509_info_subject_alt_name(char **buf, size_t *size,
|
|
|
|
const mbedtls_x509_sequence
|
|
|
|
*subject_alt_name,
|
|
|
|
const char *prefix)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t i;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
|
|
|
const mbedtls_x509_sequence *cur = subject_alt_name;
|
|
|
|
mbedtls_x509_subject_alternative_name san;
|
|
|
|
int parse_ret;
|
|
|
|
|
|
|
|
while (cur != NULL) {
|
|
|
|
memset(&san, 0, sizeof(san));
|
|
|
|
parse_ret = mbedtls_x509_parse_subject_alt_name(&cur->buf, &san);
|
|
|
|
if (parse_ret != 0) {
|
|
|
|
if (parse_ret == MBEDTLS_ERR_X509_FEATURE_UNAVAILABLE) {
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%s <malformed>", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
}
|
|
|
|
cur = cur->next;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (san.type) {
|
|
|
|
/*
|
|
|
|
* otherName
|
|
|
|
*/
|
|
|
|
case MBEDTLS_X509_SAN_OTHER_NAME:
|
|
|
|
{
|
|
|
|
mbedtls_x509_san_other_name *other_name = &san.san.other_name;
|
|
|
|
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%s otherName :", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
|
|
|
if (MBEDTLS_OID_CMP(MBEDTLS_OID_ON_HW_MODULE_NAME,
|
2023-08-18 20:12:59 +02:00
|
|
|
&other_name->type_id) == 0) {
|
2023-01-17 10:26:40 +01:00
|
|
|
ret = mbedtls_snprintf(p, n, "\n%s hardware module name :", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
ret =
|
|
|
|
mbedtls_snprintf(p, n, "\n%s hardware type : ", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
|
|
|
ret = mbedtls_oid_get_numeric_string(p,
|
|
|
|
n,
|
|
|
|
&other_name->value.hardware_module_name.oid);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
|
|
|
ret =
|
|
|
|
mbedtls_snprintf(p, n, "\n%s hardware serial number : ", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
|
|
|
|
for (i = 0; i < other_name->value.hardware_module_name.val.len; i++) {
|
|
|
|
ret = mbedtls_snprintf(p,
|
|
|
|
n,
|
|
|
|
"%02X",
|
|
|
|
other_name->value.hardware_module_name.val.p[i]);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
}
|
|
|
|
}/* MBEDTLS_OID_ON_HW_MODULE_NAME */
|
|
|
|
}
|
|
|
|
break;
|
2023-02-13 16:03:07 +01:00
|
|
|
/*
|
|
|
|
* uniformResourceIdentifier
|
|
|
|
*/
|
|
|
|
case MBEDTLS_X509_SAN_UNIFORM_RESOURCE_IDENTIFIER:
|
|
|
|
{
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%s uniformResourceIdentifier : ", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
if (san.san.unstructured_name.len >= n) {
|
2023-06-05 16:38:13 +02:00
|
|
|
if (n > 0) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2023-02-13 16:03:07 +01:00
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
2023-01-17 10:26:40 +01:00
|
|
|
|
2023-02-13 16:03:07 +01:00
|
|
|
memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
|
|
|
|
p += san.san.unstructured_name.len;
|
|
|
|
n -= san.san.unstructured_name.len;
|
|
|
|
}
|
|
|
|
break;
|
2023-01-17 10:26:40 +01:00
|
|
|
/*
|
|
|
|
* dNSName
|
2023-02-15 12:56:37 +01:00
|
|
|
* RFC822 Name
|
2023-01-17 10:26:40 +01:00
|
|
|
*/
|
|
|
|
case MBEDTLS_X509_SAN_DNS_NAME:
|
2023-02-15 12:56:37 +01:00
|
|
|
case MBEDTLS_X509_SAN_RFC822_NAME:
|
2023-01-17 10:26:40 +01:00
|
|
|
{
|
2023-02-15 15:00:50 +01:00
|
|
|
const char *dns_name = "dNSName";
|
|
|
|
const char *rfc822_name = "rfc822Name";
|
|
|
|
|
|
|
|
ret = mbedtls_snprintf(p, n,
|
|
|
|
"\n%s %s : ",
|
|
|
|
prefix,
|
2023-06-02 10:11:32 +02:00
|
|
|
san.type ==
|
|
|
|
MBEDTLS_X509_SAN_DNS_NAME ? dns_name : rfc822_name);
|
2023-01-17 10:26:40 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
if (san.san.unstructured_name.len >= n) {
|
2023-06-05 16:38:13 +02:00
|
|
|
if (n > 0) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2023-01-17 10:26:40 +01:00
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
|
2023-06-02 10:11:32 +02:00
|
|
|
memcpy(p, san.san.unstructured_name.p, san.san.unstructured_name.len);
|
|
|
|
p += san.san.unstructured_name.len;
|
|
|
|
n -= san.san.unstructured_name.len;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
/*
|
|
|
|
* iPAddress
|
|
|
|
*/
|
|
|
|
case MBEDTLS_X509_SAN_IP_ADDRESS:
|
|
|
|
{
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%s %s : ",
|
|
|
|
prefix, "iPAddress");
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
if (san.san.unstructured_name.len >= n) {
|
2023-06-05 16:38:13 +02:00
|
|
|
if (n > 0) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2023-06-02 10:11:32 +02:00
|
|
|
return MBEDTLS_ERR_X509_BUFFER_TOO_SMALL;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *ip = san.san.unstructured_name.p;
|
|
|
|
// Only IPv6 (16 bytes) and IPv4 (4 bytes) types are supported
|
|
|
|
if (san.san.unstructured_name.len == 4) {
|
2023-06-06 11:40:32 +02:00
|
|
|
ret = mbedtls_snprintf(p, n, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2023-06-02 10:11:32 +02:00
|
|
|
} else if (san.san.unstructured_name.len == 16) {
|
2023-06-06 11:40:32 +02:00
|
|
|
ret = mbedtls_snprintf(p, n,
|
2023-06-05 16:38:13 +02:00
|
|
|
"%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X:%X%X",
|
|
|
|
ip[0], ip[1], ip[2], ip[3], ip[4], ip[5], ip[6],
|
2023-06-06 11:40:32 +02:00
|
|
|
ip[7], ip[8], ip[9], ip[10], ip[11], ip[12], ip[13],
|
2023-06-05 16:38:13 +02:00
|
|
|
ip[14], ip[15]);
|
2023-06-06 11:40:32 +02:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
2023-05-29 16:30:50 +02:00
|
|
|
} else {
|
2023-06-05 16:38:13 +02:00
|
|
|
if (n > 0) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2023-06-02 10:11:32 +02:00
|
|
|
return MBEDTLS_ERR_X509_BAD_INPUT_DATA;
|
2023-05-29 16:30:50 +02:00
|
|
|
}
|
2023-01-17 10:26:40 +01:00
|
|
|
}
|
|
|
|
break;
|
2023-01-10 12:47:38 +01:00
|
|
|
/*
|
|
|
|
* directoryName
|
|
|
|
*/
|
|
|
|
case MBEDTLS_X509_SAN_DIRECTORY_NAME:
|
|
|
|
{
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%s directoryName : ", prefix);
|
2023-02-27 22:03:41 +01:00
|
|
|
if (ret < 0 || (size_t) ret >= n) {
|
|
|
|
mbedtls_x509_free_subject_alt_name(&san);
|
|
|
|
}
|
|
|
|
|
2023-01-10 12:47:38 +01:00
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
ret = mbedtls_x509_dn_gets(p, n, &san.san.directory_name);
|
2023-02-13 13:01:59 +01:00
|
|
|
|
2023-01-10 12:47:38 +01:00
|
|
|
if (ret < 0) {
|
2023-02-27 22:03:41 +01:00
|
|
|
mbedtls_x509_free_subject_alt_name(&san);
|
2023-06-05 16:38:13 +02:00
|
|
|
if (n > 0) {
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2023-01-10 12:47:38 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
p += ret;
|
|
|
|
n -= ret;
|
|
|
|
}
|
|
|
|
break;
|
2023-01-17 10:26:40 +01:00
|
|
|
/*
|
|
|
|
* Type not supported, skip item.
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
ret = mbedtls_snprintf(p, n, "\n%s <unsupported>", prefix);
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-02-13 13:01:59 +01:00
|
|
|
/* So far memory is freed only in the case of directoryName
|
2023-02-27 22:03:41 +01:00
|
|
|
* parsing succeeding, as mbedtls_x509_get_name allocates memory. */
|
2023-02-13 13:01:59 +01:00
|
|
|
mbedtls_x509_free_subject_alt_name(&san);
|
2023-01-17 10:26:40 +01:00
|
|
|
cur = cur->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = '\0';
|
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-04 10:24:22 +01:00
|
|
|
#define PRINT_ITEM(i) \
|
|
|
|
do { \
|
|
|
|
ret = mbedtls_snprintf(p, n, "%s" i, sep); \
|
|
|
|
MBEDTLS_X509_SAFE_SNPRINTF; \
|
|
|
|
sep = ", "; \
|
|
|
|
} while (0)
|
2023-01-17 10:26:40 +01:00
|
|
|
|
2022-12-04 10:24:22 +01:00
|
|
|
#define CERT_TYPE(type, name) \
|
|
|
|
do { \
|
|
|
|
if (ns_cert_type & (type)) { \
|
|
|
|
PRINT_ITEM(name); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2023-01-17 10:26:40 +01:00
|
|
|
|
|
|
|
int mbedtls_x509_info_cert_type(char **buf, size_t *size,
|
|
|
|
unsigned char ns_cert_type)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
|
|
|
const char *sep = "";
|
|
|
|
|
|
|
|
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CLIENT, "SSL Client");
|
|
|
|
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_SERVER, "SSL Server");
|
|
|
|
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL, "Email");
|
|
|
|
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING, "Object Signing");
|
|
|
|
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_RESERVED, "Reserved");
|
|
|
|
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_SSL_CA, "SSL CA");
|
|
|
|
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_EMAIL_CA, "Email CA");
|
|
|
|
CERT_TYPE(MBEDTLS_X509_NS_CERT_TYPE_OBJECT_SIGNING_CA, "Object Signing CA");
|
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-12-04 10:24:22 +01:00
|
|
|
#define KEY_USAGE(code, name) \
|
|
|
|
do { \
|
|
|
|
if ((key_usage) & (code)) { \
|
|
|
|
PRINT_ITEM(name); \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2023-01-17 10:26:40 +01:00
|
|
|
|
|
|
|
int mbedtls_x509_info_key_usage(char **buf, size_t *size,
|
|
|
|
unsigned int key_usage)
|
|
|
|
{
|
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
|
|
|
size_t n = *size;
|
|
|
|
char *p = *buf;
|
|
|
|
const char *sep = "";
|
|
|
|
|
|
|
|
KEY_USAGE(MBEDTLS_X509_KU_DIGITAL_SIGNATURE, "Digital Signature");
|
|
|
|
KEY_USAGE(MBEDTLS_X509_KU_NON_REPUDIATION, "Non Repudiation");
|
|
|
|
KEY_USAGE(MBEDTLS_X509_KU_KEY_ENCIPHERMENT, "Key Encipherment");
|
|
|
|
KEY_USAGE(MBEDTLS_X509_KU_DATA_ENCIPHERMENT, "Data Encipherment");
|
|
|
|
KEY_USAGE(MBEDTLS_X509_KU_KEY_AGREEMENT, "Key Agreement");
|
|
|
|
KEY_USAGE(MBEDTLS_X509_KU_KEY_CERT_SIGN, "Key Cert Sign");
|
|
|
|
KEY_USAGE(MBEDTLS_X509_KU_CRL_SIGN, "CRL Sign");
|
|
|
|
KEY_USAGE(MBEDTLS_X509_KU_ENCIPHER_ONLY, "Encipher Only");
|
|
|
|
KEY_USAGE(MBEDTLS_X509_KU_DECIPHER_ONLY, "Decipher Only");
|
|
|
|
|
|
|
|
*size = n;
|
|
|
|
*buf = p;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* MBEDTLS_X509_REMOVE_INFO */
|
|
|
|
#endif /* MBEDTLS_X509_CRT_PARSE_C || MBEDTLS_X509_CSR_PARSE_C */
|
2015-04-08 12:49:31 +02:00
|
|
|
#endif /* MBEDTLS_X509_USE_C */
|