Skip to content

Commit 2ed5449

Browse files
olszomalmtrojnar
authored andcommitted
Use TS_TST_INFO struct
1 parent a096aa8 commit 2ed5449

File tree

1 file changed

+42
-63
lines changed

1 file changed

+42
-63
lines changed

osslsigncode.c

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -158,15 +158,10 @@ ASN1_SEQUENCE(CatalogAuthAttr) = {
158158

159159
IMPLEMENT_ASN1_FUNCTIONS(CatalogAuthAttr)
160160

161-
ASN1_SEQUENCE(MessageImprint) = {
162-
ASN1_SIMPLE(MessageImprint, digestAlgorithm, AlgorithmIdentifier),
163-
ASN1_SIMPLE(MessageImprint, digest, ASN1_OCTET_STRING)
164-
} ASN1_SEQUENCE_END(MessageImprint)
165-
166-
IMPLEMENT_ASN1_FUNCTIONS(MessageImprint)
167-
168161
#ifdef ENABLE_CURL
169-
162+
/*
163+
* Structures for Authenticode Timestamp
164+
*/
170165
ASN1_SEQUENCE(TimeStampRequestBlob) = {
171166
ASN1_SIMPLE(TimeStampRequestBlob, type, ASN1_OBJECT),
172167
ASN1_EXP_OPT(TimeStampRequestBlob, signature, ASN1_OCTET_STRING, 0)
@@ -183,29 +178,6 @@ IMPLEMENT_ASN1_FUNCTIONS(TimeStampRequest)
183178

184179
#endif /* ENABLE_CURL */
185180

186-
ASN1_SEQUENCE(TimeStampAccuracy) = {
187-
ASN1_OPT(TimeStampAccuracy, seconds, ASN1_INTEGER),
188-
ASN1_IMP_OPT(TimeStampAccuracy, millis, ASN1_INTEGER, 0),
189-
ASN1_IMP_OPT(TimeStampAccuracy, micros, ASN1_INTEGER, 1)
190-
} ASN1_SEQUENCE_END(TimeStampAccuracy)
191-
192-
IMPLEMENT_ASN1_FUNCTIONS(TimeStampAccuracy)
193-
194-
ASN1_SEQUENCE(TimeStampToken) = {
195-
ASN1_SIMPLE(TimeStampToken, version, ASN1_INTEGER),
196-
ASN1_SIMPLE(TimeStampToken, policy_id, ASN1_OBJECT),
197-
ASN1_SIMPLE(TimeStampToken, messageImprint, MessageImprint),
198-
ASN1_SIMPLE(TimeStampToken, serial, ASN1_INTEGER),
199-
ASN1_SIMPLE(TimeStampToken, time, ASN1_GENERALIZEDTIME),
200-
ASN1_OPT(TimeStampToken, accuracy, TimeStampAccuracy),
201-
ASN1_OPT(TimeStampToken, ordering, ASN1_FBOOLEAN),
202-
ASN1_OPT(TimeStampToken, nonce, ASN1_INTEGER),
203-
ASN1_EXP_OPT(TimeStampToken, tsa, GENERAL_NAME, 0),
204-
ASN1_IMP_SEQUENCE_OF_OPT(TimeStampToken, extensions, X509_EXTENSION, 1)
205-
} ASN1_SEQUENCE_END(TimeStampToken)
206-
207-
IMPLEMENT_ASN1_FUNCTIONS(TimeStampToken)
208-
209181
ASN1_SEQUENCE(CatalogInfo) = {
210182
ASN1_SIMPLE(CatalogInfo, digest, ASN1_OCTET_STRING),
211183
ASN1_SET_OF(CatalogInfo, attributes, CatalogAuthAttr)
@@ -856,7 +828,7 @@ static TS_RESP *get_rfc3161_response(FILE_FORMAT_CTX *ctx, X509 *signer_cert,
856828
if (ctx->options->tsa_time) {
857829
TS_RESP_CTX_set_time_cb(resp_ctx, time_cb, &(ctx->options->tsa_time));
858830
}
859-
/* generate RFC3161 response with embedded TimeStampToken */
831+
/* generate RFC3161 response with embedded TS_TST_INFO structure */
860832
response = TS_RESP_create_response(resp_ctx, bout);
861833
if (!response) {
862834
printf("Failed to create RFC3161 response\n");
@@ -1462,12 +1434,14 @@ static STACK_OF(X509_CRL) *x509_crl_list_get(PKCS7 *p7, X509_CRL *crl)
14621434
return crls;
14631435
}
14641436

1465-
static void print_timestamp_serial_number(const ASN1_INTEGER *serial)
1437+
static void print_timestamp_serial_number(TS_TST_INFO *token)
14661438
{
14671439
BIGNUM *serialbn;
14681440
char *number;
14691441

1470-
serialbn = ASN1_INTEGER_to_BN(serial, NULL);
1442+
if (!token)
1443+
return;
1444+
serialbn = ASN1_INTEGER_to_BN(TS_TST_INFO_get_serial(token), NULL);
14711445
number = BN_bn2hex(serialbn);
14721446
printf("Timestamp serial number: %s\n", number);
14731447
BN_free(serialbn);
@@ -1485,59 +1459,68 @@ static int verify_timestamp_token(PKCS7 *p7, CMS_ContentInfo *timestamp)
14851459
{
14861460
STACK_OF(PKCS7_SIGNER_INFO) *signer_info;
14871461
PKCS7_SIGNER_INFO *si;
1488-
ASN1_OCTET_STRING *hash, **pos;
1489-
TimeStampToken *token = NULL;
1490-
const u_char *p = NULL;
1491-
u_char mdbuf[EVP_MAX_MD_SIZE];
1492-
const EVP_MD *md;
1493-
int md_nid;
1494-
BIO *bhash;
1462+
ASN1_OCTET_STRING **pos;
14951463

14961464
signer_info = PKCS7_get_signer_info(p7);
14971465
if (!signer_info)
14981466
return 0; /* FAILED */
1499-
15001467
si = sk_PKCS7_SIGNER_INFO_value(signer_info, 0);
15011468
if (!si)
15021469
return 0; /* FAILED */
15031470

1471+
/* get the embedded content */
15041472
pos = CMS_get0_content(timestamp);
15051473
if (pos != NULL && *pos != NULL) {
1506-
p = (*pos)->data;
1507-
token = d2i_TimeStampToken(NULL, &p, (*pos)->length);
1474+
const u_char *p = (*pos)->data;
1475+
TS_TST_INFO *token = d2i_TS_TST_INFO(NULL, &p, (*pos)->length);
1476+
15081477
if (token) {
1509-
print_timestamp_serial_number(token->serial);
1510-
/* compute a hash from the encrypted message digest value of the file */
1511-
md_nid = OBJ_obj2nid(token->messageImprint->digestAlgorithm->algorithm);
1478+
BIO *bhash;
1479+
u_char mdbuf[EVP_MAX_MD_SIZE];
1480+
ASN1_OCTET_STRING *hash;
1481+
const ASN1_OBJECT *aoid;
1482+
int md_nid;
1483+
const EVP_MD *md;
1484+
TS_MSG_IMPRINT *msg_imprint = TS_TST_INFO_get_msg_imprint(token);
1485+
const X509_ALGOR *alg = TS_MSG_IMPRINT_get_algo(msg_imprint);
1486+
1487+
X509_ALGOR_get0(&aoid, NULL, NULL, alg);
1488+
md_nid = OBJ_obj2nid(aoid);
15121489
md = EVP_get_digestbynid(md_nid);
1490+
1491+
/* compute a hash from the encrypted message digest value of the file */
15131492
bhash = BIO_new(BIO_f_md());
15141493
if (!BIO_set_md(bhash, md)) {
15151494
printf("Unable to set the message digest of BIO\n");
15161495
BIO_free_all(bhash);
1517-
return 0; /* FAILED */
1496+
TS_TST_INFO_free(token);
1497+
return 0; /* FAILED */
15181498
}
15191499
BIO_push(bhash, BIO_new(BIO_s_null()));
15201500
BIO_write(bhash, si->enc_digest->data, si->enc_digest->length);
15211501
BIO_gets(bhash, (char*)mdbuf, EVP_MD_size(md));
15221502
BIO_free_all(bhash);
1503+
15231504
/* compare the provided hash against the computed hash */
1524-
hash = token->messageImprint->digest;
1525-
/* hash->length == EVP_MD_size(md) */
1505+
hash =TS_MSG_IMPRINT_get_msg(msg_imprint);
15261506
if (memcmp(mdbuf, hash->data, (size_t)hash->length)) {
15271507
printf("Hash value mismatch:\n\tMessage digest algorithm: %s\n",
15281508
(md_nid == NID_undef) ? "UNKNOWN" : OBJ_nid2ln(md_nid));
15291509
print_hash("\tComputed message digest", "", mdbuf, EVP_MD_size(md));
15301510
print_hash("\tReceived message digest", "", hash->data, hash->length);
15311511
printf("\nFile's message digest verification: failed\n");
1532-
TimeStampToken_free(token);
1512+
TS_TST_INFO_free(token);
15331513
return 0; /* FAILED */
15341514
} /* else Computed and received message digests matched */
1535-
TimeStampToken_free(token);
1515+
1516+
print_timestamp_serial_number(token);
1517+
TS_TST_INFO_free(token);
15361518
} else
15371519
/* our CMS_ContentInfo struct created for Authenticode Timestamp
1538-
* does not contain any TimeStampToken as specified in RFC 3161 */
1520+
* does not contain any TS_TST_INFO struct as specified in RFC 3161 */
15391521
ERR_clear_error();
15401522
}
1523+
15411524
return 1; /* OK */
15421525
}
15431526

@@ -2127,28 +2110,24 @@ static time_t time_t_get_si_time(PKCS7_SIGNER_INFO *si)
21272110
*/
21282111
static time_t time_t_get_cms_time(CMS_ContentInfo *cms)
21292112
{
2130-
ASN1_OCTET_STRING **pos;
2131-
const u_char *p = NULL;
2132-
TimeStampToken *token = NULL;
2133-
ASN1_GENERALIZEDTIME *asn1_time = NULL;
21342113
time_t posix_time = INVALID_TIME;
2114+
ASN1_OCTET_STRING **pos = CMS_get0_content(cms);
21352115

2136-
pos = CMS_get0_content(cms);
21372116
if (pos != NULL && *pos != NULL) {
2138-
p = (*pos)->data;
2139-
token = d2i_TimeStampToken(NULL, &p, (*pos)->length);
2117+
const u_char *p = (*pos)->data;
2118+
TS_TST_INFO *token = d2i_TS_TST_INFO(NULL, &p, (*pos)->length);
21402119
if (token) {
2141-
asn1_time = token->time;
2120+
const ASN1_GENERALIZEDTIME *asn1_time = TS_TST_INFO_get_time(token);
21422121
posix_time = time_t_get_asn1_time(asn1_time);
2143-
TimeStampToken_free(token);
2122+
TS_TST_INFO_free(token);
21442123
}
21452124
}
21462125
return posix_time;
21472126
}
21482127

21492128
/*
21502129
* Create new CMS_ContentInfo struct for Authenticode Timestamp.
2151-
* This struct does not contain any TimeStampToken as specified in RFC 3161.
2130+
* This struct does not contain any TS_TST_INFO as specified in RFC 3161.
21522131
* [in] p7_signed: PKCS#7 signedData structure
21532132
* [in] countersignature: Authenticode Timestamp decoded to PKCS7_SIGNER_INFO
21542133
* [returns] pointer to CMS_ContentInfo structure

0 commit comments

Comments
 (0)