Skip to content

Commit

Permalink
Convert BER to DER in d2i_PKCS7 if needed
Browse files Browse the repository at this point in the history
  • Loading branch information
WillChilds-Klein committed Aug 20, 2024
1 parent 6aa0b0a commit c97b588
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 281 deletions.
35 changes: 34 additions & 1 deletion crypto/pkcs7/pkcs7_asn1.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "internal.h"
#include "../internal.h"
#include "../bytestring/internal.h"

ASN1_ADB_TEMPLATE(p7default) = ASN1_EXP_OPT(PKCS7, d.other, ASN1_ANY, 0);

Expand All @@ -22,7 +23,39 @@ ASN1_SEQUENCE(PKCS7) = {
ASN1_ADB_OBJECT(PKCS7)
} ASN1_SEQUENCE_END(PKCS7)

IMPLEMENT_ASN1_FUNCTIONS(PKCS7)
IMPLEMENT_ASN1_ALLOC_FUNCTIONS(PKCS7)

PKCS7 *d2i_PKCS7(PKCS7 **a, const unsigned char **in, long len) {
uint8_t *der_bytes = NULL;
PKCS7 *ret = NULL;
CBS cbs, cbs_der;

CBS_init(&cbs, *in, len);
if (!CBS_asn1_ber_to_der(&cbs, &cbs_der, &der_bytes)) {
goto err;
}

if (der_bytes) {
uint8_t *der_bytes_ptr = der_bytes;
ret = (PKCS7 *) ASN1_item_d2i(
(ASN1_VALUE **) a, (const uint8_t**) &der_bytes_ptr,
CBS_len(&cbs_der), ASN1_ITEM_rptr(PKCS7)
);
} else {
ret = (PKCS7 *) ASN1_item_d2i((ASN1_VALUE **) a, in, len, ASN1_ITEM_rptr(PKCS7));
}

err:
OPENSSL_free(der_bytes);
der_bytes = NULL;
return ret;
}

int i2d_PKCS7(PKCS7 *a, unsigned char **out) {
return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(PKCS7));
}

IMPLEMENT_ASN1_DUP_FUNCTION(PKCS7)

ASN1_SEQUENCE(PKCS7_SIGNED) = {
ASN1_SIMPLE(PKCS7_SIGNED, version, ASN1_INTEGER),
Expand Down
Loading

0 comments on commit c97b588

Please sign in to comment.