2022-02-28 17:09:16 +01: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
|
2020-11-16 20:03:12 +01:00
|
|
|
*/
|
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include "mbedtls/build_info.h"
|
|
|
|
#if defined(MBEDTLS_PKCS7_C)
|
|
|
|
#include "mbedtls/pkcs7.h"
|
2024-01-17 10:55:32 +01:00
|
|
|
#include "x509_internal.h"
|
2020-11-16 20:03:12 +01:00
|
|
|
#include "mbedtls/asn1.h"
|
|
|
|
#include "mbedtls/x509_crt.h"
|
|
|
|
#include "mbedtls/x509_crl.h"
|
|
|
|
#include "mbedtls/oid.h"
|
2022-09-12 23:21:02 +02:00
|
|
|
#include "mbedtls/error.h"
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
#if defined(MBEDTLS_FS_IO)
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "mbedtls/platform.h"
|
|
|
|
#include "mbedtls/platform_util.h"
|
|
|
|
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME)
|
|
|
|
#include "mbedtls/platform_time.h"
|
|
|
|
#endif
|
|
|
|
#if defined(MBEDTLS_HAVE_TIME_DATE)
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2023-03-13 13:15:49 +01:00
|
|
|
* Initializes the mbedtls_pkcs7 structure.
|
2020-11-16 20:03:12 +01:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_pkcs7_init(mbedtls_pkcs7 *pkcs7)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2023-01-11 14:50:10 +01:00
|
|
|
memset(pkcs7, 0, sizeof(*pkcs7));
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_next_content_len(unsigned char **p, unsigned char *end,
|
|
|
|
size_t *len)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(p, end, len, MBEDTLS_ASN1_CONSTRUCTED
|
|
|
|
| MBEDTLS_ASN1_CONTEXT_SPECIFIC);
|
|
|
|
if (ret != 0) {
|
2022-11-28 06:46:00 +01:00
|
|
|
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO, ret);
|
2022-11-28 04:48:55 +01:00
|
|
|
} else if ((size_t) (end - *p) != *len) {
|
2022-11-28 06:46:00 +01:00
|
|
|
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO,
|
2022-11-28 04:48:55 +01:00
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* version Version
|
|
|
|
* Version ::= INTEGER
|
|
|
|
**/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_version(unsigned char **p, unsigned char *end, int *ver)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_int(p, end, ver);
|
|
|
|
if (ret != 0) {
|
|
|
|
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_VERSION, ret);
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
/* If version != 1, return invalid version */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*ver != MBEDTLS_PKCS7_SUPPORTED_VERSION) {
|
2022-09-12 23:21:02 +02:00
|
|
|
ret = MBEDTLS_ERR_PKCS7_INVALID_VERSION;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentInfo ::= SEQUENCE {
|
|
|
|
* contentType ContentType,
|
|
|
|
* content
|
|
|
|
* [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
|
|
|
|
**/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_content_info_type(unsigned char **p, unsigned char *end,
|
2022-12-15 22:54:03 +01:00
|
|
|
unsigned char **seq_end,
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_pkcs7_buf *pkcs7)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
|
|
|
size_t len = 0;
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-12-14 23:44:49 +01:00
|
|
|
unsigned char *start = *p;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED
|
|
|
|
| MBEDTLS_ASN1_SEQUENCE);
|
|
|
|
if (ret != 0) {
|
2021-07-01 21:29:50 +02:00
|
|
|
*p = start;
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO, ret);
|
2021-07-01 21:29:50 +02:00
|
|
|
}
|
2022-12-15 22:54:03 +01:00
|
|
|
*seq_end = *p + len;
|
|
|
|
ret = mbedtls_asn1_get_tag(p, *seq_end, &len, MBEDTLS_ASN1_OID);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2020-12-14 23:44:49 +01:00
|
|
|
*p = start;
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO, ret);
|
2020-12-14 23:44:49 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
pkcs7->tag = MBEDTLS_ASN1_OID;
|
|
|
|
pkcs7->len = len;
|
|
|
|
pkcs7->p = *p;
|
2022-10-28 19:28:54 +02:00
|
|
|
*p += len;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DigestAlgorithmIdentifier ::= AlgorithmIdentifier
|
|
|
|
*
|
|
|
|
* This is from x509.h
|
|
|
|
**/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_digest_algorithm(unsigned char **p, unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_alg_null(p, end, alg)) != 0) {
|
|
|
|
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_ALG, ret);
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DigestAlgorithmIdentifiers :: SET of DigestAlgorithmIdentifier
|
|
|
|
**/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_digest_algorithm_set(unsigned char **p,
|
|
|
|
unsigned char *end,
|
|
|
|
mbedtls_x509_buf *alg)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
|
|
|
size_t len = 0;
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED
|
|
|
|
| MBEDTLS_ASN1_SET);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_ALG, ret);
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
end = *p + len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_alg_null(p, end, alg);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_ALG, ret);
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-09-14 21:44:03 +02:00
|
|
|
/** For now, it assumes there is only one digest algorithm specified **/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p != end) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE;
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* certificates :: SET OF ExtendedCertificateOrCertificate,
|
|
|
|
* ExtendedCertificateOrCertificate ::= CHOICE {
|
|
|
|
* certificate Certificate -- x509,
|
|
|
|
* extendedCertificate[0] IMPLICIT ExtendedCertificate }
|
|
|
|
* Return number of certificates added to the signed data,
|
|
|
|
* 0 or higher is valid.
|
|
|
|
* Return negative error code for failure.
|
|
|
|
**/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_certificates(unsigned char **p, unsigned char *end,
|
|
|
|
mbedtls_x509_crt *certs)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
size_t len1 = 0;
|
|
|
|
size_t len2 = 0;
|
2022-10-28 19:28:54 +02:00
|
|
|
unsigned char *end_set, *end_cert, *start;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-11-28 06:46:00 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(p, end, &len1, MBEDTLS_ASN1_CONSTRUCTED
|
|
|
|
| MBEDTLS_ASN1_CONTEXT_SPECIFIC);
|
|
|
|
if (ret == MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT, ret);
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
start = *p;
|
|
|
|
end_set = *p + len1;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(p, end_set, &len2, MBEDTLS_ASN1_CONSTRUCTED
|
|
|
|
| MBEDTLS_ASN1_SEQUENCE);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CERT, ret);
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
end_cert = *p + len2;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is to verify that there is only one signer certificate. It seems it is
|
|
|
|
* not easy to differentiate between the chain vs different signer's certificate.
|
|
|
|
* So, we support only the root certificate and the single signer.
|
|
|
|
* The behaviour would be improved with addition of multiple signer support.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
if (end_cert != end_set) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE;
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-11-28 06:29:32 +01:00
|
|
|
if ((ret = mbedtls_x509_crt_parse_der(certs, start, len1)) < 0) {
|
2023-01-11 14:50:10 +01:00
|
|
|
return MBEDTLS_ERR_PKCS7_INVALID_CERT;
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-11-28 06:29:32 +01:00
|
|
|
*p = end_cert;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-09-19 17:01:25 +02:00
|
|
|
/*
|
2022-09-12 23:21:02 +02:00
|
|
|
* Since in this version we strictly support single certificate, and reaching
|
2022-09-19 17:01:25 +02:00
|
|
|
* here implies we have parsed successfully, we return 1.
|
2022-09-12 23:21:02 +02:00
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
return 1;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EncryptedDigest ::= OCTET STRING
|
|
|
|
**/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_signature(unsigned char **p, unsigned char *end,
|
|
|
|
mbedtls_pkcs7_buf *signature)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_OCTET_STRING);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
signature->tag = MBEDTLS_ASN1_OCTET_STRING;
|
|
|
|
signature->len = len;
|
|
|
|
signature->p = *p;
|
|
|
|
|
|
|
|
*p = *p + len;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static void pkcs7_free_signer_info(mbedtls_pkcs7_signer_info *signer)
|
2022-12-12 21:37:36 +01:00
|
|
|
{
|
|
|
|
mbedtls_x509_name *name_cur;
|
|
|
|
mbedtls_x509_name *name_prv;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (signer == NULL) {
|
2022-12-12 21:37:36 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-12-12 21:37:36 +01:00
|
|
|
|
|
|
|
name_cur = signer->issuer.next;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (name_cur != NULL) {
|
2022-12-12 21:37:36 +01:00
|
|
|
name_prv = name_cur;
|
|
|
|
name_cur = name_cur->next;
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(name_prv);
|
2022-12-12 21:37:36 +01:00
|
|
|
}
|
|
|
|
signer->issuer.next = NULL;
|
|
|
|
}
|
|
|
|
|
2020-11-16 20:03:12 +01:00
|
|
|
/**
|
|
|
|
* SignerInfo ::= SEQUENCE {
|
|
|
|
* version Version;
|
|
|
|
* issuerAndSerialNumber IssuerAndSerialNumber,
|
|
|
|
* digestAlgorithm DigestAlgorithmIdentifier,
|
|
|
|
* authenticatedAttributes
|
|
|
|
* [0] IMPLICIT Attributes OPTIONAL,
|
|
|
|
* digestEncryptionAlgorithm DigestEncryptionAlgorithmIdentifier,
|
|
|
|
* encryptedDigest EncryptedDigest,
|
|
|
|
* unauthenticatedAttributes
|
|
|
|
* [1] IMPLICIT Attributes OPTIONAL,
|
2020-09-02 06:48:45 +02:00
|
|
|
* Returns 0 if the signerInfo is valid.
|
2020-11-16 20:03:12 +01:00
|
|
|
* Return negative error code for failure.
|
2022-10-28 19:28:54 +02:00
|
|
|
* Structure must not contain vales for authenticatedAttributes
|
|
|
|
* and unauthenticatedAttributes.
|
2020-11-16 20:03:12 +01:00
|
|
|
**/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_signer_info(unsigned char **p, unsigned char *end,
|
2022-11-30 08:06:07 +01:00
|
|
|
mbedtls_pkcs7_signer_info *signer,
|
|
|
|
mbedtls_x509_buf *alg)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2022-12-15 22:54:03 +01:00
|
|
|
unsigned char *end_signer, *end_issuer_and_sn;
|
2022-09-12 23:21:02 +02:00
|
|
|
int asn1_ret = 0, ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
size_t len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
asn1_ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED
|
|
|
|
| MBEDTLS_ASN1_SEQUENCE);
|
|
|
|
if (asn1_ret != 0) {
|
2022-09-12 23:21:02 +02:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2020-09-02 06:48:45 +02:00
|
|
|
end_signer = *p + len;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = pkcs7_get_version(p, end_signer, &signer->version);
|
|
|
|
if (ret != 0) {
|
2022-09-12 23:21:02 +02:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
asn1_ret = mbedtls_asn1_get_tag(p, end_signer, &len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
|
|
|
|
if (asn1_ret != 0) {
|
2022-09-12 23:21:02 +02:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-12-15 22:54:03 +01:00
|
|
|
end_issuer_and_sn = *p + len;
|
2020-11-16 20:03:12 +01:00
|
|
|
/* Parsing IssuerAndSerialNumber */
|
2020-09-02 06:48:45 +02:00
|
|
|
signer->issuer_raw.p = *p;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-31 21:42:26 +01:00
|
|
|
asn1_ret = mbedtls_asn1_get_tag(p, end_issuer_and_sn, &len,
|
2023-01-11 14:50:10 +01:00
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
|
|
|
|
if (asn1_ret != 0) {
|
2022-09-12 23:21:02 +02:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_x509_get_name(p, *p + len, &signer->issuer);
|
|
|
|
if (ret != 0) {
|
2022-09-12 23:21:02 +02:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-11-04 13:20:09 +01:00
|
|
|
signer->issuer_raw.len = (size_t) (*p - signer->issuer_raw.p);
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-31 21:42:26 +01:00
|
|
|
ret = mbedtls_x509_get_serial(p, end_issuer_and_sn, &signer->serial);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-09-12 23:21:02 +02:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-12-15 22:54:03 +01:00
|
|
|
/* ensure no extra or missing bytes */
|
|
|
|
if (*p != end_issuer_and_sn) {
|
|
|
|
ret = MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = pkcs7_get_digest_algorithm(p, end_signer, &signer->alg_identifier);
|
|
|
|
if (ret != 0) {
|
2022-09-12 23:21:02 +02:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-11-30 08:06:07 +01:00
|
|
|
/* Check that the digest algorithm used matches the one provided earlier */
|
|
|
|
if (signer->alg_identifier.tag != alg->tag ||
|
|
|
|
signer->alg_identifier.len != alg->len ||
|
|
|
|
memcmp(signer->alg_identifier.p, alg->p, alg->len) != 0) {
|
|
|
|
ret = MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO;
|
|
|
|
goto out;
|
|
|
|
}
|
2022-10-28 19:28:54 +02:00
|
|
|
|
2023-03-07 12:39:52 +01:00
|
|
|
/* Assume authenticatedAttributes is nonexistent */
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = pkcs7_get_digest_algorithm(p, end_signer, &signer->sig_alg_identifier);
|
|
|
|
if (ret != 0) {
|
2022-09-12 23:21:02 +02:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = pkcs7_get_signature(p, end_signer, &signer->sig);
|
|
|
|
if (ret != 0) {
|
2022-09-12 23:21:02 +02:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2020-09-02 06:48:45 +02:00
|
|
|
/* Do not permit any unauthenticated attributes */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (*p != end_signer) {
|
2022-09-12 23:21:02 +02:00
|
|
|
ret = MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2022-09-12 23:21:02 +02:00
|
|
|
|
|
|
|
out:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (asn1_ret != 0 || ret != 0) {
|
|
|
|
pkcs7_free_signer_info(signer);
|
|
|
|
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO,
|
|
|
|
asn1_ret);
|
2022-12-12 21:37:36 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-09-02 06:48:45 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2020-09-02 06:48:45 +02:00
|
|
|
/**
|
|
|
|
* SignerInfos ::= SET of SignerInfo
|
|
|
|
* Return number of signers added to the signed data,
|
|
|
|
* 0 or higher is valid.
|
|
|
|
* Return negative error code for failure.
|
|
|
|
**/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_signers_info_set(unsigned char **p, unsigned char *end,
|
2022-11-30 08:06:07 +01:00
|
|
|
mbedtls_pkcs7_signer_info *signers_set,
|
|
|
|
mbedtls_x509_buf *digest_alg)
|
2020-09-02 06:48:45 +02:00
|
|
|
{
|
|
|
|
unsigned char *end_set;
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-09-02 06:48:45 +02:00
|
|
|
int count = 0;
|
|
|
|
size_t len = 0;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_CONSTRUCTED
|
|
|
|
| MBEDTLS_ASN1_SET);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO, ret);
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-09-02 06:48:45 +02:00
|
|
|
|
|
|
|
/* Detect zero signers */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-09-02 06:48:45 +02:00
|
|
|
|
|
|
|
end_set = *p + len;
|
|
|
|
|
2022-11-30 08:06:07 +01:00
|
|
|
ret = pkcs7_get_signer_info(p, end_set, signers_set, digest_alg);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-09-02 06:48:45 +02:00
|
|
|
count++;
|
|
|
|
|
2022-11-27 21:46:56 +01:00
|
|
|
mbedtls_pkcs7_signer_info *prev = signers_set;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (*p != end_set) {
|
2022-11-27 21:46:56 +01:00
|
|
|
mbedtls_pkcs7_signer_info *signer =
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_calloc(1, sizeof(mbedtls_pkcs7_signer_info));
|
|
|
|
if (!signer) {
|
2020-09-02 06:48:45 +02:00
|
|
|
ret = MBEDTLS_ERR_PKCS7_ALLOC_FAILED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2022-11-30 08:06:07 +01:00
|
|
|
ret = pkcs7_get_signer_info(p, end_set, signer, digest_alg);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_free(signer);
|
2020-09-02 06:48:45 +02:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
prev->next = signer;
|
|
|
|
prev = signer;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return count;
|
2020-09-02 06:48:45 +02:00
|
|
|
|
|
|
|
cleanup:
|
2023-01-11 14:50:10 +01:00
|
|
|
pkcs7_free_signer_info(signers_set);
|
2022-11-27 21:46:56 +01:00
|
|
|
mbedtls_pkcs7_signer_info *signer = signers_set->next;
|
2023-01-11 14:50:10 +01:00
|
|
|
while (signer != NULL) {
|
2020-09-02 06:48:45 +02:00
|
|
|
prev = signer;
|
|
|
|
signer = signer->next;
|
2023-01-11 14:50:10 +01:00
|
|
|
pkcs7_free_signer_info(prev);
|
|
|
|
mbedtls_free(prev);
|
2020-09-02 06:48:45 +02:00
|
|
|
}
|
2022-11-27 21:28:31 +01:00
|
|
|
signers_set->next = NULL;
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SignedData ::= SEQUENCE {
|
|
|
|
* version Version,
|
|
|
|
* digestAlgorithms DigestAlgorithmIdentifiers,
|
|
|
|
* contentInfo ContentInfo,
|
|
|
|
* certificates
|
|
|
|
* [0] IMPLICIT ExtendedCertificatesAndCertificates
|
|
|
|
* OPTIONAL,
|
|
|
|
* crls
|
|
|
|
* [0] IMPLICIT CertificateRevocationLists OPTIONAL,
|
|
|
|
* signerInfos SignerInfos }
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
static int pkcs7_get_signed_data(unsigned char *buf, size_t buflen,
|
|
|
|
mbedtls_pkcs7_signed_data *signed_data)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
|
|
|
unsigned char *p = buf;
|
|
|
|
unsigned char *end = buf + buflen;
|
2023-02-09 18:55:41 +01:00
|
|
|
unsigned char *end_content_info = NULL;
|
2020-11-16 20:03:12 +01:00
|
|
|
size_t len = 0;
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
mbedtls_md_type_t md_alg;
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED
|
|
|
|
| MBEDTLS_ASN1_SEQUENCE);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT, ret);
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-11-28 06:23:00 +01:00
|
|
|
if (p + len != end) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
/* Get version of signed data */
|
2022-11-28 06:23:00 +01:00
|
|
|
ret = pkcs7_get_version(&p, end, &signed_data->version);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
/* Get digest algorithm */
|
2022-11-28 06:23:00 +01:00
|
|
|
ret = pkcs7_get_digest_algorithm_set(&p, end,
|
2023-01-11 14:50:10 +01:00
|
|
|
&signed_data->digest_alg_identifiers);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = mbedtls_oid_get_md_alg(&signed_data->digest_alg_identifiers, &md_alg);
|
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_INVALID_ALG;
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-12-14 05:50:03 +01:00
|
|
|
mbedtls_pkcs7_buf content_type;
|
2023-02-09 18:55:41 +01:00
|
|
|
memset(&content_type, 0, sizeof(content_type));
|
2022-12-14 05:50:03 +01:00
|
|
|
ret = pkcs7_get_content_info_type(&p, end, &end_content_info, &content_type);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2022-12-14 05:50:03 +01:00
|
|
|
if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_DATA, &content_type)) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO;
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-02-07 20:59:58 +01:00
|
|
|
if (p != end_content_info) {
|
|
|
|
/* Determine if valid content is present */
|
2023-02-09 10:21:14 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(&p,
|
|
|
|
end_content_info,
|
|
|
|
&len,
|
|
|
|
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC);
|
2023-02-07 20:59:58 +01:00
|
|
|
if (ret != 0) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO, ret);
|
|
|
|
}
|
|
|
|
p += len;
|
|
|
|
if (p != end_content_info) {
|
|
|
|
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO, ret);
|
|
|
|
}
|
|
|
|
/* Valid content is present - this is not supported */
|
|
|
|
return MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE;
|
2022-12-15 22:54:03 +01:00
|
|
|
}
|
|
|
|
|
2020-11-16 20:03:12 +01:00
|
|
|
/* Look for certificates, there may or may not be any */
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_init(&signed_data->certs);
|
2022-11-28 06:23:00 +01:00
|
|
|
ret = pkcs7_get_certificates(&p, end, &signed_data->certs);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
signed_data->no_of_certs = ret;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Currently CRLs are not supported. If CRL exist, the parsing will fail
|
|
|
|
* at next step of getting signers info and return error as invalid
|
|
|
|
* signer info.
|
|
|
|
*/
|
|
|
|
|
|
|
|
signed_data->no_of_crls = 0;
|
|
|
|
|
|
|
|
/* Get signers info */
|
2022-11-30 08:06:07 +01:00
|
|
|
ret = pkcs7_get_signers_info_set(&p,
|
|
|
|
end,
|
|
|
|
&signed_data->signers,
|
|
|
|
&signed_data->digest_alg_identifiers);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
signed_data->no_of_signers = ret;
|
|
|
|
|
2020-09-02 06:48:45 +02:00
|
|
|
/* Don't permit trailing data */
|
2023-01-11 14:50:10 +01:00
|
|
|
if (p != end) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_INVALID_FORMAT;
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return 0;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_pkcs7_parse_der(mbedtls_pkcs7 *pkcs7, const unsigned char *buf,
|
|
|
|
const size_t buflen)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2022-10-28 19:28:54 +02:00
|
|
|
unsigned char *p;
|
2022-11-28 06:23:00 +01:00
|
|
|
unsigned char *end;
|
2020-11-16 20:03:12 +01:00
|
|
|
size_t len = 0;
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (pkcs7 == NULL) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
/* make an internal copy of the buffer for parsing */
|
2023-01-11 14:50:10 +01:00
|
|
|
pkcs7->raw.p = p = mbedtls_calloc(1, buflen);
|
|
|
|
if (pkcs7->raw.p == NULL) {
|
2022-09-12 23:21:02 +02:00
|
|
|
ret = MBEDTLS_ERR_PKCS7_ALLOC_FAILED;
|
|
|
|
goto out;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
memcpy(p, buf, buflen);
|
2020-11-16 20:03:12 +01:00
|
|
|
pkcs7->raw.len = buflen;
|
2022-10-28 19:28:54 +02:00
|
|
|
end = p + buflen;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-11-28 06:20:42 +01:00
|
|
|
ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_CONSTRUCTED
|
|
|
|
| MBEDTLS_ASN1_SEQUENCE);
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret != 0) {
|
2022-11-28 06:20:42 +01:00
|
|
|
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT, ret);
|
|
|
|
goto out;
|
2020-12-14 23:44:49 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-11-28 06:20:42 +01:00
|
|
|
if ((size_t) (end - p) != len) {
|
|
|
|
ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_FORMAT,
|
|
|
|
MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
|
2022-12-15 22:54:03 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-11-28 06:20:42 +01:00
|
|
|
if ((ret = mbedtls_asn1_get_tag(&p, end, &len, MBEDTLS_ASN1_OID)) != 0) {
|
|
|
|
if (ret != MBEDTLS_ERR_ASN1_UNEXPECTED_TAG) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
p = pkcs7->raw.p;
|
|
|
|
len = buflen;
|
|
|
|
goto try_data;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
2022-11-28 06:20:42 +01:00
|
|
|
if (MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_SIGNED_DATA, p, len)) {
|
2023-02-20 15:45:09 +01:00
|
|
|
/* OID is not MBEDTLS_OID_PKCS7_SIGNED_DATA, which is the only supported feature */
|
2022-11-28 06:20:42 +01:00
|
|
|
if (!MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_DATA, p, len)
|
|
|
|
|| !MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_ENCRYPTED_DATA, p, len)
|
|
|
|
|| !MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_ENVELOPED_DATA, p, len)
|
|
|
|
|| !MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_SIGNED_AND_ENVELOPED_DATA, p, len)
|
|
|
|
|| !MBEDTLS_OID_CMP_RAW(MBEDTLS_OID_PKCS7_DIGESTED_DATA, p, len)) {
|
2023-02-20 15:45:09 +01:00
|
|
|
/* OID is valid according to the spec, but unsupported */
|
2022-11-28 06:20:42 +01:00
|
|
|
ret = MBEDTLS_ERR_PKCS7_FEATURE_UNAVAILABLE;
|
|
|
|
} else {
|
2023-02-20 15:45:09 +01:00
|
|
|
/* OID is invalid according to the spec */
|
2022-11-28 06:20:42 +01:00
|
|
|
ret = MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2022-11-28 06:20:42 +01:00
|
|
|
p += len;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = pkcs7_get_next_content_len(&p, end, &len);
|
|
|
|
if (ret != 0) {
|
2020-11-16 20:03:12 +01:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-12-15 22:54:03 +01:00
|
|
|
/* ensure no extra/missing data */
|
|
|
|
if (p + len != end) {
|
|
|
|
ret = MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2020-12-14 23:44:49 +01:00
|
|
|
try_data:
|
2023-01-11 14:50:10 +01:00
|
|
|
ret = pkcs7_get_signed_data(p, len, &pkcs7->signed_data);
|
|
|
|
if (ret != 0) {
|
2020-12-14 23:44:49 +01:00
|
|
|
goto out;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-12-14 23:44:49 +01:00
|
|
|
|
|
|
|
ret = MBEDTLS_PKCS7_SIGNED_DATA;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
|
|
|
out:
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret < 0) {
|
|
|
|
mbedtls_pkcs7_free(pkcs7);
|
|
|
|
}
|
2020-12-14 23:44:49 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
static int mbedtls_pkcs7_data_or_hash_verify(mbedtls_pkcs7 *pkcs7,
|
2022-10-28 18:23:15 +02:00
|
|
|
const mbedtls_x509_crt *cert,
|
|
|
|
const unsigned char *data,
|
|
|
|
size_t datalen,
|
2023-01-11 14:50:10 +01:00
|
|
|
const int is_data_hash)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2022-09-12 23:21:02 +02:00
|
|
|
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
2020-11-16 20:03:12 +01:00
|
|
|
unsigned char *hash;
|
|
|
|
mbedtls_pk_context pk_cxt = cert->pk;
|
|
|
|
const mbedtls_md_info_t *md_info;
|
|
|
|
mbedtls_md_type_t md_alg;
|
2020-09-02 06:48:45 +02:00
|
|
|
mbedtls_pkcs7_signer_info *signer;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (pkcs7->signed_data.no_of_signers == 0) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_INVALID_CERT;
|
2022-09-12 23:21:02 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (mbedtls_x509_time_is_past(&cert->valid_to) ||
|
|
|
|
mbedtls_x509_time_is_future(&cert->valid_from)) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_CERT_DATE_INVALID;
|
2022-10-28 18:23:15 +02:00
|
|
|
}
|
|
|
|
|
2022-11-30 08:06:07 +01:00
|
|
|
ret = mbedtls_oid_get_md_alg(&pkcs7->signed_data.digest_alg_identifiers, &md_alg);
|
|
|
|
if (ret != 0) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
md_info = mbedtls_md_info_from_type(md_alg);
|
|
|
|
if (md_info == NULL) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
hash = mbedtls_calloc(mbedtls_md_get_size(md_info), 1);
|
|
|
|
if (hash == NULL) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_ALLOC_FAILED;
|
|
|
|
}
|
|
|
|
|
2023-02-16 17:23:09 +01:00
|
|
|
/* BEGIN must free hash before jumping out */
|
2022-11-30 08:06:07 +01:00
|
|
|
if (is_data_hash) {
|
|
|
|
if (datalen != mbedtls_md_get_size(md_info)) {
|
|
|
|
ret = MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
|
|
|
} else {
|
|
|
|
memcpy(hash, data, datalen);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = mbedtls_md(md_info, data, datalen, hash);
|
|
|
|
}
|
|
|
|
if (ret != 0) {
|
|
|
|
mbedtls_free(hash);
|
|
|
|
return MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* assume failure */
|
|
|
|
ret = MBEDTLS_ERR_PKCS7_VERIFY_FAIL;
|
|
|
|
|
2020-09-02 06:48:45 +02:00
|
|
|
/*
|
|
|
|
* Potential TODOs
|
|
|
|
* Currently we iterate over all signers and return success if any of them
|
|
|
|
* verify.
|
|
|
|
*
|
|
|
|
* However, we could make this better by checking against the certificate's
|
|
|
|
* identification and SignerIdentifier fields first. That would also allow
|
|
|
|
* us to distinguish between 'no signature for key' and 'signature for key
|
|
|
|
* failed to validate'.
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
for (signer = &pkcs7->signed_data.signers; signer; signer = signer->next) {
|
|
|
|
ret = mbedtls_pk_verify(&pk_cxt, md_alg, hash,
|
|
|
|
mbedtls_md_get_size(md_info),
|
|
|
|
signer->sig.p, signer->sig.len);
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (ret == 0) {
|
2020-09-02 06:48:45 +02:00
|
|
|
break;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-09-02 06:48:45 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2022-11-30 08:06:07 +01:00
|
|
|
mbedtls_free(hash);
|
|
|
|
/* END must free hash before jumping out */
|
2023-01-11 14:50:10 +01:00
|
|
|
return ret;
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
2022-11-28 06:46:00 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_pkcs7_signed_data_verify(mbedtls_pkcs7 *pkcs7,
|
|
|
|
const mbedtls_x509_crt *cert,
|
|
|
|
const unsigned char *data,
|
|
|
|
size_t datalen)
|
2022-10-28 18:23:15 +02:00
|
|
|
{
|
2022-11-28 06:46:00 +01:00
|
|
|
if (data == NULL) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_pkcs7_data_or_hash_verify(pkcs7, cert, data, datalen, 0);
|
2022-10-28 18:23:15 +02:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
int mbedtls_pkcs7_signed_hash_verify(mbedtls_pkcs7 *pkcs7,
|
|
|
|
const mbedtls_x509_crt *cert,
|
|
|
|
const unsigned char *hash,
|
|
|
|
size_t hashlen)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2022-11-28 06:46:00 +01:00
|
|
|
if (hash == NULL) {
|
|
|
|
return MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
|
|
|
}
|
2023-01-11 14:50:10 +01:00
|
|
|
return mbedtls_pkcs7_data_or_hash_verify(pkcs7, cert, hash, hashlen, 1);
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unallocate all pkcs7 data
|
|
|
|
*/
|
2023-01-11 14:50:10 +01:00
|
|
|
void mbedtls_pkcs7_free(mbedtls_pkcs7 *pkcs7)
|
2020-11-16 20:03:12 +01:00
|
|
|
{
|
2020-09-02 06:48:45 +02:00
|
|
|
mbedtls_pkcs7_signer_info *signer_cur;
|
|
|
|
mbedtls_pkcs7_signer_info *signer_prev;
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
if (pkcs7 == NULL || pkcs7->raw.p == NULL) {
|
2020-11-16 20:03:12 +01:00
|
|
|
return;
|
2023-01-11 14:50:10 +01:00
|
|
|
}
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_free(pkcs7->raw.p);
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2023-01-11 14:50:10 +01:00
|
|
|
mbedtls_x509_crt_free(&pkcs7->signed_data.certs);
|
|
|
|
mbedtls_x509_crl_free(&pkcs7->signed_data.crl);
|
2020-11-16 20:03:12 +01:00
|
|
|
|
2020-09-02 06:48:45 +02:00
|
|
|
signer_cur = pkcs7->signed_data.signers.next;
|
2023-01-11 14:50:10 +01:00
|
|
|
pkcs7_free_signer_info(&pkcs7->signed_data.signers);
|
|
|
|
while (signer_cur != NULL) {
|
2020-09-02 06:48:45 +02:00
|
|
|
signer_prev = signer_cur;
|
|
|
|
signer_cur = signer_prev->next;
|
2023-01-11 14:50:10 +01:00
|
|
|
pkcs7_free_signer_info(signer_prev);
|
|
|
|
mbedtls_free(signer_prev);
|
2020-11-16 20:03:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pkcs7->raw.p = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|