Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

### 2.17 (unreleased)

**Other user-visible changes**

- fixed memory leaks in signature processing, built-in timestamping and
private key loading, and incorrect success reports when TSA parameters
cannot be loaded
- fixed allocation-failure crashes when decoding Base64 script signatures
and timestamp responses, or creating signature content

### 2.16 (2026.11.13)

**Security fixes**
Expand Down
38 changes: 26 additions & 12 deletions appx.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ IMPLEMENT_ASN1_FUNCTIONS(AppxSpcSipInfo)

struct appx_ctx_st {
ZIP_FILE *zip;
/* Hash buffers are owned by this context and freed by appx_ctx_cleanup(). */
u_char *calculatedBMHash;
u_char *calculatedCTHash;
u_char *calculatedCDHash;
Expand Down Expand Up @@ -730,13 +731,13 @@ static int appx_append_pkcs7(FILE_FORMAT_CTX *ctx, BIO *outdata, PKCS7 *p7)
blob = OPENSSL_malloc((size_t)(len + 4));
memcpy(blob, PKCX_SIGNATURE, 4);
memcpy(blob + 4, der, (size_t)len);
OPENSSL_free(der);
len += 4;
if (!zipAppendSignatureFile(outdata, zip, blob, (uint64_t)len)) {
OPENSSL_free(blob);
fprintf(stderr, "Failed to append zip file\n");
return 1; /* FAILED */
}
OPENSSL_free(der);
OPENSSL_free(blob);
if (!get_current_position(outdata, &cdOffset)) {
fprintf(stderr, "Unable to get offset\n");
Expand Down Expand Up @@ -798,6 +799,11 @@ static BIO *appx_calculate_hashes(FILE_FORMAT_CTX *ctx)
{
uint64_t cdOffset = 0;

OPENSSL_free(ctx->appx_ctx->calculatedBMHash);
OPENSSL_free(ctx->appx_ctx->calculatedCTHash);
OPENSSL_free(ctx->appx_ctx->calculatedDataHash);
OPENSSL_free(ctx->appx_ctx->calculatedCDHash);
OPENSSL_free(ctx->appx_ctx->calculatedCIHash);
ctx->appx_ctx->calculatedBMHash = zipCalcDigest(ctx->appx_ctx->zip, BLOCK_MAP_FILENAME, ctx->appx_ctx->md);
ctx->appx_ctx->calculatedCTHash = zipCalcDigest(ctx->appx_ctx->zip, CONTENT_TYPES_FILENAME, ctx->appx_ctx->md);
ctx->appx_ctx->calculatedDataHash = appx_calc_zip_data_hash(&cdOffset, ctx->appx_ctx->zip, ctx->appx_ctx->md);
Expand Down Expand Up @@ -873,6 +879,8 @@ static uint8_t *appx_calc_zip_central_directory_hash(ZIP_FILE *zip, const EVP_MD
u_char *mdbuf = NULL;
BIO *bhash = BIO_new(BIO_f_md());

if (!bhash)
return NULL; /* FAILED */
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
Expand Down Expand Up @@ -1007,6 +1015,8 @@ static uint8_t *appx_calc_zip_data_hash(uint64_t *cdOffset, ZIP_FILE *zip, const
BIO *bhash = BIO_new(BIO_f_md());
uint64_t noEntries = 0;

if (!bhash)
return NULL; /* FAILED */
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
Expand Down Expand Up @@ -1092,25 +1102,29 @@ static int appx_extract_hashes(FILE_FORMAT_CTX *ctx, SpcIndirectDataContent *con
return 0; /* FAILED */
}
while (pos + mdlen + 4 <= len) {
u_char **hash;

if (!memcmp(data + pos, AXPC_SIGNATURE, 4)) {
ctx->appx_ctx->existingDataHash = OPENSSL_malloc((size_t)mdlen);
memcpy(ctx->appx_ctx->existingDataHash, data + pos + 4, (size_t)mdlen);
hash = &ctx->appx_ctx->existingDataHash;
} else if (!memcmp(data + pos, AXCD_SIGNATURE, 4)) {
ctx->appx_ctx->existingCDHash = OPENSSL_malloc((size_t)mdlen);
memcpy(ctx->appx_ctx->existingCDHash, data + pos + 4, (size_t)mdlen);
hash = &ctx->appx_ctx->existingCDHash;
} else if (!memcmp(data + pos, AXCT_SIGNATURE, 4)) {
ctx->appx_ctx->existingCTHash = OPENSSL_malloc((size_t)mdlen);
memcpy(ctx->appx_ctx->existingCTHash, data + pos + 4, (size_t)mdlen);
hash = &ctx->appx_ctx->existingCTHash;
} else if (!memcmp(data + pos, AXBM_SIGNATURE, 4)) {
ctx->appx_ctx->existingBMHash = OPENSSL_malloc((size_t)mdlen);
memcpy(ctx->appx_ctx->existingBMHash, data + pos + 4, (size_t)mdlen);
hash = &ctx->appx_ctx->existingBMHash;
} else if (!memcmp(data + pos, AXCI_SIGNATURE, 4)) {
ctx->appx_ctx->existingCIHash = OPENSSL_malloc((size_t)mdlen);
memcpy(ctx->appx_ctx->existingCIHash, data + pos + 4, (size_t)mdlen);
hash = &ctx->appx_ctx->existingCIHash;
} else {
fprintf(stderr, "Invalid hash signature\n");
return 0; /* FAILED */
}
/* The digest size is fixed for this context; reuse buffers for repeated tags. */
if (!*hash) {
*hash = OPENSSL_malloc((size_t)mdlen);
if (!*hash)
return 0; /* FAILED */
}
memcpy(*hash, data + pos + 4, (size_t)mdlen);
pos += mdlen + 4;
}
if (!ctx->appx_ctx->existingDataHash) {
Expand Down Expand Up @@ -1780,7 +1794,7 @@ static u_char *zipCalcDigest(ZIP_FILE *zip, const char *fileName, const EVP_MD *
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#endif
if (!BIO_set_md(bhash, md)) {
if (!bhash || !BIO_set_md(bhash, md)) {
fprintf(stderr, "Unable to set the message digest of BIO\n");
OPENSSL_free(data);
BIO_free_all(bhash);
Expand Down
10 changes: 7 additions & 3 deletions cab.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static u_char *cab_digest_calc(FILE_FORMAT_CTX *ctx, const EVP_MD *md)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
#endif
if (!BIO_set_md(bhash, md)) {
if (!bhash || !BIO_set_md(bhash, md)) {
fprintf(stderr, "Unable to set the message digest of BIO\n");
BIO_free_all(bhash);
return 0; /* FAILED */
Expand Down Expand Up @@ -493,6 +493,7 @@ static PKCS7 *cab_pkcs7_signature_new(FILE_FORMAT_CTX *ctx, BIO *hash)
content = spc_indirect_data_content_get(hash, ctx);
if (!content) {
fprintf(stderr, "Failed to get spcIndirectDataContent\n");
PKCS7_free(p7);
return NULL; /* FAILED */
}
if (!sign_spc_indirect_data_content(p7, content)) {
Expand Down Expand Up @@ -723,8 +724,11 @@ static int cab_add_jp_attribute(PKCS7 *p7, int jp)
if (attrs) {
astr = ASN1_STRING_new();
ASN1_STRING_set(astr, attrs, sizeof java_attrs_low);
return PKCS7_add_signed_attribute(si, OBJ_txt2nid(MS_JAVA_SOMETHING),
V_ASN1_SEQUENCE, astr);
if (!PKCS7_add_signed_attribute(si, OBJ_txt2nid(MS_JAVA_SOMETHING),
V_ASN1_SEQUENCE, astr)) {
ASN1_STRING_free(astr);
return 0; /* FAILED */
}
}
return 1; /* OK */
}
Expand Down
8 changes: 7 additions & 1 deletion cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ static CAT_CTX *cat_ctx_get(char *indata, uint32_t filesize)
static int cat_add_content_type(PKCS7 *p7, PKCS7 *cursig)
{
const char *content_type;
ASN1_OBJECT *object;
STACK_OF(PKCS7_SIGNER_INFO) *signer_info;
PKCS7_SIGNER_INFO *si;

Expand All @@ -275,9 +276,14 @@ static int cat_add_content_type(PKCS7 *p7, PKCS7 *cursig)
si = sk_PKCS7_SIGNER_INFO_value(signer_info, 0);
if (!si)
return 0; /* FAILED */
object = OBJ_txt2obj(content_type, 1);
if (!object)
return 0; /* FAILED */
if (!PKCS7_add_signed_attribute(si, NID_pkcs9_contentType,
V_ASN1_OBJECT, OBJ_txt2obj(content_type, 1)))
V_ASN1_OBJECT, object)) {
ASN1_OBJECT_free(object);
return 0; /* FAILED */
}
return 1; /* OK */
}

Expand Down
Loading
Loading