Skip to content
Open
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
10 changes: 10 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

**Other user-visible changes**

- match SignTool's Authenticode attributes: omit primary signingTime and always
include SpcSpOpusInfo, even without description or URL
- accept `-st` and `-time` as deprecated no-ops when signing, with a warning;
`verify -time` and its compatibility alias `verify -st` continue to select
the verification time
- use empty PE image flags and an empty Unicode file link when not generating
page hashes, matching the PE SIP's default encoding
- generate SHA-384 and SHA-512 PE page hashes with the selected signing digest,
matching SignTool; verify these and legacy SHA-256 page-hash tables
- match the CAB SIP's v2 subject OID and serialized cabinet-size hash
- fixed memory leaks in signature processing, built-in timestamping and
private key loading, and incorrect success reports when TSA parameters
cannot be loaded
Expand Down
79 changes: 63 additions & 16 deletions cab.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct cab_ctx_st {

/* FILE_FORMAT method prototypes */
static FILE_FORMAT_CTX *cab_ctx_new(GLOBAL_OPTIONS *options, BIO *hash, BIO *outdata);
static ASN1_OBJECT *cab_obsolete_link_get(u_char **p, int *plen, FILE_FORMAT_CTX *ctx);
static ASN1_OBJECT *cab_link_get(u_char **p, int *plen, FILE_FORMAT_CTX *ctx);
static PKCS7 *cab_pkcs7_contents_get(FILE_FORMAT_CTX *ctx, BIO *hash, const EVP_MD *md);
static int cab_hash_length_get(FILE_FORMAT_CTX *ctx);
static u_char *cab_digest_calc(FILE_FORMAT_CTX *ctx, const EVP_MD *md);
Expand All @@ -60,7 +60,7 @@ static int cab_is_detaching_supported(void);

FILE_FORMAT file_format_cab = {
.ctx_new = cab_ctx_new,
.data_blob_get = cab_obsolete_link_get,
.data_blob_get = cab_link_get,
.pkcs7_contents_get = cab_pkcs7_contents_get,
.hash_length_get = cab_hash_length_get,
.digest_calc = cab_digest_calc,
Expand Down Expand Up @@ -134,24 +134,71 @@ static FILE_FORMAT_CTX *cab_ctx_new(GLOBAL_OPTIONS *options, BIO *hash, BIO *out
* Allocate and return SpcLink object.
* [out] p: SpcLink data
* [out] plen: SpcLink data length
* [in] ctx: structure holds input and output data (unused)
* [returns] pointer to ASN1_OBJECT structure corresponding to SPC_CAB_DATA_OBJID
* [in] ctx: structure holds input and output data
* [returns] CAB SIP version 2 subject OID, or NULL on failure
*/
static ASN1_OBJECT *cab_obsolete_link_get(u_char **p, int *plen, FILE_FORMAT_CTX *ctx)
static ASN1_OBJECT *cab_link_get(u_char **p, int *plen, FILE_FORMAT_CTX *ctx)
{
ASN1_OBJECT *dtype;
SpcLink *link = spc_link_obsolete_get();

/* squash the unused parameter warning */
(void)ctx;

*plen = i2d_SpcLink(link, NULL);
*p = OPENSSL_malloc((size_t)*plen);
i2d_SpcLink(link, p);
*p -= *plen;
static const u_char classid[] = {
0xa6, 0xb5, 0x86, 0xd5, 0xb4, 0xa1, 0x24, 0x66,
0xae, 0x05, 0xa2, 0x17, 0xda, 0x8e, 0x60, 0xd6
};
u_char size[4], digest[EVP_MAX_MD_SIZE], *serialized = NULL, *q;
unsigned int digest_len;
uint32_t cabinet_size = GET_UINT32_LE(ctx->options->indata + 8);
ASN1_OBJECT *oid = NULL, *dtype = NULL;
X509_ATTRIBUTE *attribute = NULL;
SpcLink *link = NULL;
int len, serialized_len;

/* The SIP hashes cbCabinet after reserving the signature header, not
* the total file length including the PKCS#7. Re-signing keeps it. */
if (!(ctx->cab_ctx->flags & FLAG_RESERVE_PRESENT)) {
if (cabinet_size > UINT32_MAX - 24)
goto cleanup;
cabinet_size += 24;
}
PUT_UINT32_LE(cabinet_size, size);
if (!EVP_Digest(size, sizeof size, digest, &digest_len, ctx->options->md, NULL))
goto cleanup;
oid = OBJ_txt2obj("1.3.6.1.4.1.311.2.5.1", 1);
if (!oid)
goto cleanup;
attribute = X509_ATTRIBUTE_create_by_OBJ(NULL, oid, V_ASN1_OCTET_STRING,
digest, (int)digest_len);
if (!attribute || (len = i2d_X509_ATTRIBUTE(attribute, NULL)) <= 0)
goto cleanup;
/* serializedData is a SET OF attributes, containing the size hash. */
serialized_len = ASN1_object_size(1, len, V_ASN1_SET);
serialized = OPENSSL_malloc((size_t)serialized_len);
link = SpcLink_new();
if (!serialized || !link)
goto cleanup;
q = serialized;
ASN1_put_object(&q, 1, len, V_ASN1_SET, V_ASN1_UNIVERSAL);
if (i2d_X509_ATTRIBUTE(attribute, &q) != len)
goto cleanup;
link->type = 1;
link->value.moniker = SpcSerializedObject_new();
if (!link->value.moniker
|| !ASN1_OCTET_STRING_set(link->value.moniker->classId, classid, sizeof classid)
|| !ASN1_OCTET_STRING_set(link->value.moniker->serializedData, serialized, serialized_len))
goto cleanup;
dtype = OBJ_txt2obj(SPC_CAB_DATA_OBJID, 1);
if (!dtype)
goto cleanup;
*p = NULL;
*plen = i2d_SpcLink(link, p);
if (*plen <= 0) {
ASN1_OBJECT_free(dtype);
dtype = NULL;
}
cleanup:
SpcLink_free(link);
return dtype; /* OK */
X509_ATTRIBUTE_free(attribute);
ASN1_OBJECT_free(oid);
OPENSSL_free(serialized);
return dtype;
}

/*
Expand Down
26 changes: 19 additions & 7 deletions cmake/CMakeTest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ if(Python3_FOUND)
"${CMAKE_CURRENT_SOURCE_DIR}/tests/make_large_pe.py"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/make_crafted_timestamps.py"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/check_pe_checksum.py"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/check_pe_page_hashes.py"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/check_signtool_compat.py"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/check_sip_formats.py"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/check_msi_tree.py"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/start_server.py"
"${CMAKE_CURRENT_SOURCE_DIR}/tests/exec.py"
Expand Down Expand Up @@ -117,14 +120,12 @@ if(Python3_FOUND AND NOT cryptography_error)
### Sign ###

# Sign with PKCS#12 container with private key and certificate encryption algorithm
# Signing time: May 1 00:00:00 2019 GMT (1556668800)
foreach(ext ${extensions_all})
add_test(NAME "signed_${ext}"
COMMAND ${Python3_EXECUTABLE} ${EXEC} ${OSSLSIGNCODE} "sign"
"-pkcs12" "${CERTS}/cert.p12"
"-readpass" "${CERTS}/password.txt"
"-ac" "${CERTS}/CAcross.pem"
"-time" "1556668800"
"-add-msi-dse"
"-comm"
"-ph"
Expand All @@ -146,7 +147,6 @@ if(Python3_FOUND AND NOT cryptography_error)
"-key" "${CERTS}/keyp.pem"
"-readpass" "${CERTS}/password.txt"
"-ac" "${CERTS}/CAcross.pem"
"-time" "1556668800" # Signing time: May 1 00:00:00 2019 GMT
"-add-msi-dse"
"-comm"
"-ph"
Expand Down Expand Up @@ -237,7 +237,6 @@ if(Python3_FOUND AND NOT cryptography_error)
"-key" "${CERTS}/key.der"
"-pass" "passme"
"-ac" "${CERTS}/CAcross.pem"
"-time" "1556755200" # Signing time: May 2 00:00:00 2019 GMT
"-add-msi-dse"
"-comm"
"-ph"
Expand All @@ -253,6 +252,22 @@ if(Python3_FOUND AND NOT cryptography_error)
endforeach(ext ${extensions_all})


### SignTool-compatible default attributes (all supported OpenSSL versions) ###

add_test(NAME "signtool_compat"
COMMAND ${Python3_EXECUTABLE} "${TEST_DIR}/check_signtool_compat.py"
${OSSLSIGNCODE} ${FILES})
list(APPEND ALL_TESTS "signtool_compat")
add_test(NAME "sip_formats"
COMMAND ${Python3_EXECUTABLE} "${TEST_DIR}/check_sip_formats.py"
${OSSLSIGNCODE} "${FILES}")
list(APPEND ALL_TESTS "sip_formats")
add_test(NAME "pe_page_hashes"
COMMAND ${Python3_EXECUTABLE} "${TEST_DIR}/check_pe_page_hashes.py"
${OSSLSIGNCODE} "${FILES}")
list(APPEND ALL_TESTS "pe_page_hashes")


### PE checksum ###

# The CheckSum field of a signed PE file is compared with an independent
Expand Down Expand Up @@ -496,7 +511,6 @@ if(Python3_FOUND AND NOT cryptography_error)
"-pkcs12" "${CERTS}/cert.p12"
"-readpass" "${CERTS}/password.txt"
"-ac" "${CERTS}/CAcross.pem"
"-time" "1556668800" # Signing time: May 1 00:00:00 2019 GMT
"-add-msi-dse"
"-comm"
"-ph"
Expand All @@ -518,7 +532,6 @@ if(Python3_FOUND AND NOT cryptography_error)
"-pkcs12" "${CERTS}/cert.p12"
"-readpass" "${CERTS}/password.txt"
"-ac" "${CERTS}/CAcross.pem"
"-time" "1556668800" # Signing time: May 1 00:00:00 2019 GMT
"-add-msi-dse"
"-comm"
"-ph"
Expand Down Expand Up @@ -615,7 +628,6 @@ if(Python3_FOUND AND NOT cryptography_error)
"-h" "sha384"
"-i" "https://www.osslsigncode.com/"
"-n" "osslsigncode"
"-time" "1556668800" # Signing time: May 1 00:00:00 2019 GMT
"-ts" "FILE ${LOGS}/url.log"
"-in" "${FILES}/unsigned.${ext}"
"-out" "${FILES}/ts_${cert}.${ext}")
Expand Down
109 changes: 53 additions & 56 deletions helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
static SpcSpOpusInfo *spc_sp_opus_info_create(FILE_FORMAT_CTX *ctx);
static int spc_indirect_data_content_create(u_char **blob, int *len, FILE_FORMAT_CTX *ctx);
static int pkcs7_signer_info_add_spc_sp_opus_info(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx);
static int pkcs7_signer_info_add_signing_time(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx);
static int pkcs7_signer_info_add_purpose(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx);
static int pkcs7_signer_info_add_sequence_number(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx);
static STACK_OF(X509) *X509_chain_get_sorted(FILE_FORMAT_CTX *ctx, int signer);
Expand Down Expand Up @@ -200,14 +199,13 @@ PKCS7 *pkcs7_create(FILE_FORMAT_CTX *ctx)
goto err;
}

if (!pkcs7_signer_info_add_signing_time(si, ctx)) {
goto err;
}
if (!pkcs7_signer_info_add_purpose(si, ctx)) {
goto err;
}
if ((ctx->options->desc || ctx->options->url) &&
!pkcs7_signer_info_add_spc_sp_opus_info(si, ctx)) {
/* SignTool's shared I_CreateAuthCodeAttrs/CreateOpusInfo path emits OpusInfo,
* including an empty SEQUENCE when neither -n nor -i was supplied. This
* applies to all SIP formats, not just PE files. */
if (!pkcs7_signer_info_add_spc_sp_opus_info(si, ctx)) {
fprintf(stderr, "Couldn't allocate memory for opus info\n");
goto err;
}
Expand Down Expand Up @@ -501,27 +499,50 @@ ASN1_OCTET_STRING *spc_indirect_data_content_get(BIO *hash, FILE_FORMAT_CTX *ctx
}

/*
* Signs the data and place the signature in p7
* [in, out] p7: new PKCS#7 signature
* Sign the prepared Authenticode content, without synthesizing signingTime.
* Both callers embed the content separately after this function returns;
* data contains the SEQUENCE contents, not its ASN.1 tag and length.
* [in, out] p7: new signedData with signed attributes and signing keys
* [in] data: content data
* [in] len: content length
* [returns] 0 on error or 1 on success
*/
int pkcs7_sign_content(PKCS7 *p7, const u_char *data, int len)
{
BIO *p7bio;
STACK_OF(PKCS7_SIGNER_INFO) *signers = PKCS7_get_signer_info(p7);
int i;

if ((p7bio = PKCS7_dataInit(p7, NULL)) == NULL) {
fprintf(stderr, "PKCS7_dataInit failed\n");
return 0; /* FAILED */
}
BIO_write(p7bio, data, len);
(void)BIO_flush(p7bio);
if (!PKCS7_dataFinal(p7, p7bio)) {
fprintf(stderr, "PKCS7_dataFinal failed\n");
BIO_free_all(p7bio);
/* PKCS7_dataFinal() inserts a missing signingTime by default, unlike
* SignTool. Use the public signer-level API on every supported OpenSSL
* version (including 1.1.1): supply the content digest, then sign the
* existing attributes once. PKCS7_SIGNER_INFO_sign() does not add a time.
* This uses the supplied key and attributes directly, without adding a
* temporary time or signing twice.
*
* This helper only handles newly constructed Authenticode signedData:
* contentType and the other signed attributes are already present, and
* its callers install the encapsulated content. It is not a general
* replacement for PKCS7_dataFinal() (encryption, streaming, no-attrs). */
if (len < 0 || !signers || sk_PKCS7_SIGNER_INFO_num(signers) <= 0)
return 0; /* FAILED */
for (i = 0; i < sk_PKCS7_SIGNER_INFO_num(signers); i++) {
PKCS7_SIGNER_INFO *si = sk_PKCS7_SIGNER_INFO_value(signers, i);
X509_ALGOR *alg;
EVP_PKEY *key;
const EVP_MD *md;
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int digest_len;

PKCS7_SIGNER_INFO_get0_algs(si, &key, &alg, NULL);
md = EVP_get_digestbyobj(alg->algorithm);
if (!key || !md || sk_X509_ATTRIBUTE_num(PKCS7_get_signed_attributes(si)) <= 0 ||
!EVP_Digest(data, (size_t)len, digest, &digest_len, md, NULL) ||
!PKCS7_add1_attrib_digest(si, digest, (int)digest_len) ||
!PKCS7_SIGNER_INFO_sign(si)) {
fprintf(stderr, "Failed to sign Authenticode attributes\n");
return 0; /* FAILED */
}
}
BIO_free_all(p7bio);
return 1; /* OK */
}

Expand Down Expand Up @@ -658,25 +679,27 @@ ASN1_TYPE *catalog_content_get(CatalogAuthAttr *attribute)
}

/*
* PE and CAB format specific
* [in] none
* [returns] pointer to SpcLink
* Build the empty Unicode file link used by the PE SIP.
* [returns] pointer to SpcLink, or NULL on allocation failure
*/
SpcLink *spc_link_obsolete_get(void)
SpcLink *spc_link_file_get(void)
{
const u_char obsolete[] = {
0x00, 0x3c, 0x00, 0x3c, 0x00, 0x3c, 0x00, 0x4f,
0x00, 0x62, 0x00, 0x73, 0x00, 0x6f, 0x00, 0x6c,
0x00, 0x65, 0x00, 0x74, 0x00, 0x65, 0x00, 0x3e,
0x00, 0x3e, 0x00, 0x3e
};
SpcLink *link = SpcLink_new();

if (!link)
return NULL;
link->type = 2;
link->value.file = SpcString_new();
if (!link->value.file)
goto err;
link->value.file->type = 0;
link->value.file->value.unicode = ASN1_BMPSTRING_new();
ASN1_STRING_set(link->value.file->value.unicode, obsolete, sizeof obsolete);
if (!link->value.file->value.unicode)
goto err;
return link;
err:
SpcLink_free(link);
return NULL;
}

/*
Expand Down Expand Up @@ -884,32 +907,6 @@ static int pkcs7_signer_info_add_spc_sp_opus_info(PKCS7_SIGNER_INFO *si, FILE_FO
return 1; /* OK */
}

/*
* Add a custom, non-trusted time to the PKCS7 structure to prevent OpenSSL
* adding the _current_ time. This allows to create a deterministic signature
* when no trusted timestamp server was specified, making osslsigncode
* behaviour closer to signtool.exe (which doesn't include any non-trusted
* time in this case.)
* [in, out] si: PKCS7_SIGNER_INFO structure
* [in] ctx: structure holds input and output data
* [returns] 0 on error or 1 on success
*/
static int pkcs7_signer_info_add_signing_time(PKCS7_SIGNER_INFO *si, FILE_FORMAT_CTX *ctx)
{
ASN1_TIME *time;

if (ctx->options->time == INVALID_TIME) /* -time option was not specified */
return 1; /* SUCCESS */
time = ASN1_TIME_adj(NULL, ctx->options->time, 0, 0);
if (!time)
return 0; /* FAILED */
if (!PKCS7_add_signed_attribute(si, NID_pkcs9_signingTime, V_ASN1_UTCTIME, time)) {
ASN1_TIME_free(time);
return 0; /* FAILED */
}
return 1; /* OK */
}

/*
* [in, out] si: PKCS7_SIGNER_INFO structure
* [in] ctx: structure holds input and output data
Expand Down
2 changes: 1 addition & 1 deletion helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int is_content_type(PKCS7 *p7, const char *objid);
MsCtlContent *ms_ctl_content_get(PKCS7 *p7);
/* Borrows attribute; free a non-NULL result with ASN1_TYPE_free(). */
ASN1_TYPE *catalog_content_get(CatalogAuthAttr *attribute);
SpcLink *spc_link_obsolete_get(void);
SpcLink *spc_link_file_get(void);
int compare_digests(u_char *mdbuf, u_char *cmdbuf, int mdtype);
int spc_indirect_data_content_get_digest(SpcIndirectDataContent *idc, u_char *mdbuf, int *mdtype);

Expand Down
Loading