Merge pull request #6816 from nick-child-ibm/pkcs7_coverage
Pkcs7 coverage
This commit is contained in:
commit
a22749e749
13 changed files with 3620 additions and 128 deletions
|
@ -26,9 +26,6 @@
|
|||
#include "mbedtls/oid.h"
|
||||
#include "mbedtls/error.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
@ -94,6 +91,7 @@ static int pkcs7_get_version(unsigned char **p, unsigned char *end, int *ver)
|
|||
* [0] EXPLICIT ANY DEFINED BY contentType OPTIONAL }
|
||||
**/
|
||||
static int pkcs7_get_content_info_type(unsigned char **p, unsigned char *end,
|
||||
unsigned char **seq_end,
|
||||
mbedtls_pkcs7_buf *pkcs7)
|
||||
{
|
||||
size_t len = 0;
|
||||
|
@ -106,8 +104,8 @@ static int pkcs7_get_content_info_type(unsigned char **p, unsigned char *end,
|
|||
*p = start;
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO, ret);
|
||||
}
|
||||
|
||||
ret = mbedtls_asn1_get_tag(p, end, &len, MBEDTLS_ASN1_OID);
|
||||
*seq_end = *p + len;
|
||||
ret = mbedtls_asn1_get_tag(p, *seq_end, &len, MBEDTLS_ASN1_OID);
|
||||
if (ret != 0) {
|
||||
*p = start;
|
||||
return MBEDTLS_ERROR_ADD(MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO, ret);
|
||||
|
@ -289,7 +287,7 @@ static void pkcs7_free_signer_info(mbedtls_pkcs7_signer_info *signer)
|
|||
static int pkcs7_get_signer_info(unsigned char **p, unsigned char *end,
|
||||
mbedtls_pkcs7_signer_info *signer)
|
||||
{
|
||||
unsigned char *end_signer;
|
||||
unsigned char *end_signer, *end_issuer_and_sn;
|
||||
int asn1_ret = 0, ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
size_t len = 0;
|
||||
|
||||
|
@ -312,10 +310,11 @@ static int pkcs7_get_signer_info(unsigned char **p, unsigned char *end,
|
|||
goto out;
|
||||
}
|
||||
|
||||
end_issuer_and_sn = *p + len;
|
||||
/* Parsing IssuerAndSerialNumber */
|
||||
signer->issuer_raw.p = *p;
|
||||
|
||||
asn1_ret = mbedtls_asn1_get_tag(p, end_signer, &len,
|
||||
asn1_ret = mbedtls_asn1_get_tag(p, end_issuer_and_sn, &len,
|
||||
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE);
|
||||
if (asn1_ret != 0) {
|
||||
goto out;
|
||||
|
@ -328,11 +327,17 @@ static int pkcs7_get_signer_info(unsigned char **p, unsigned char *end,
|
|||
|
||||
signer->issuer_raw.len = *p - signer->issuer_raw.p;
|
||||
|
||||
ret = mbedtls_x509_get_serial(p, end_signer, &signer->serial);
|
||||
ret = mbedtls_x509_get_serial(p, end_issuer_and_sn, &signer->serial);
|
||||
if (ret != 0) {
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* ensure no extra or missing bytes */
|
||||
if (*p != end_issuer_and_sn) {
|
||||
ret = MBEDTLS_ERR_PKCS7_INVALID_SIGNER_INFO;
|
||||
goto out;
|
||||
}
|
||||
|
||||
ret = pkcs7_get_digest_algorithm(p, end_signer, &signer->alg_identifier);
|
||||
if (ret != 0) {
|
||||
goto out;
|
||||
|
@ -449,7 +454,7 @@ static int pkcs7_get_signed_data(unsigned char *buf, size_t buflen,
|
|||
{
|
||||
unsigned char *p = buf;
|
||||
unsigned char *end = buf + buflen;
|
||||
unsigned char *end_set;
|
||||
unsigned char *end_set, *end_content_info;
|
||||
size_t len = 0;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
mbedtls_md_type_t md_alg;
|
||||
|
@ -481,11 +486,29 @@ static int pkcs7_get_signed_data(unsigned char *buf, size_t buflen,
|
|||
}
|
||||
|
||||
/* Do not expect any content */
|
||||
ret = pkcs7_get_content_info_type(&p, end_set, &signed_data->content.oid);
|
||||
ret = pkcs7_get_content_info_type(&p, end_set, &end_content_info,
|
||||
&signed_data->content.oid);
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
if (p != end_content_info) {
|
||||
/* Determine if valid content is present */
|
||||
ret = mbedtls_asn1_get_tag(&p,
|
||||
end_content_info,
|
||||
&len,
|
||||
MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_CONTEXT_SPECIFIC);
|
||||
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;
|
||||
}
|
||||
|
||||
if (MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_DATA, &signed_data->content.oid)) {
|
||||
return MBEDTLS_ERR_PKCS7_INVALID_CONTENT_INFO;
|
||||
}
|
||||
|
@ -527,7 +550,7 @@ int mbedtls_pkcs7_parse_der(mbedtls_pkcs7 *pkcs7, const unsigned char *buf,
|
|||
const size_t buflen)
|
||||
{
|
||||
unsigned char *p;
|
||||
unsigned char *end;
|
||||
unsigned char *end, *end_content_info;
|
||||
size_t len = 0;
|
||||
int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
|
||||
int isoidset = 0;
|
||||
|
@ -546,14 +569,20 @@ int mbedtls_pkcs7_parse_der(mbedtls_pkcs7 *pkcs7, const unsigned char *buf,
|
|||
pkcs7->raw.len = buflen;
|
||||
end = p + buflen;
|
||||
|
||||
ret = pkcs7_get_content_info_type(&p, end, &pkcs7->content_type_oid);
|
||||
ret = pkcs7_get_content_info_type(&p, end, &end_content_info,
|
||||
&pkcs7->content_type_oid);
|
||||
if (ret != 0) {
|
||||
len = buflen;
|
||||
goto try_data;
|
||||
}
|
||||
|
||||
/* Ensure PKCS7 data uses the exact number of bytes specified in buflen */
|
||||
if (end_content_info != end) {
|
||||
ret = MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (!MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_DATA, &pkcs7->content_type_oid)
|
||||
|| !MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_ENCRYPTED_DATA, &pkcs7->content_type_oid)
|
||||
|| !MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_ENVELOPED_DATA, &pkcs7->content_type_oid)
|
||||
|| !MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_SIGNED_AND_ENVELOPED_DATA, &pkcs7->content_type_oid)
|
||||
|| !MBEDTLS_OID_CMP(MBEDTLS_OID_PKCS7_DIGESTED_DATA, &pkcs7->content_type_oid)
|
||||
|
@ -574,6 +603,12 @@ int mbedtls_pkcs7_parse_der(mbedtls_pkcs7 *pkcs7, const unsigned char *buf,
|
|||
goto out;
|
||||
}
|
||||
|
||||
/* ensure no extra/missing data */
|
||||
if (p + len != end) {
|
||||
ret = MBEDTLS_ERR_PKCS7_BAD_INPUT_DATA;
|
||||
goto out;
|
||||
}
|
||||
|
||||
try_data:
|
||||
ret = pkcs7_get_signed_data(p, len, &pkcs7->signed_data);
|
||||
if (ret != 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue