Skip to content

Commit aa08566

Browse files
olszomalmtrojnar
authored andcommitted
Use TS_REQ struct
1 parent c04b229 commit aa08566

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

osslsigncode.c

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,6 @@ ASN1_SEQUENCE(TimeStampRequest) = {
181181

182182
IMPLEMENT_ASN1_FUNCTIONS(TimeStampRequest)
183183

184-
/* RFC3161 Time stamping */
185-
186-
ASN1_SEQUENCE(TimeStampReq) = {
187-
ASN1_SIMPLE(TimeStampReq, version, ASN1_INTEGER),
188-
ASN1_SIMPLE(TimeStampReq, messageImprint, MessageImprint),
189-
ASN1_OPT (TimeStampReq, reqPolicy, ASN1_OBJECT),
190-
ASN1_OPT (TimeStampReq, nonce, ASN1_INTEGER),
191-
ASN1_SIMPLE(TimeStampReq, certReq, ASN1_FBOOLEAN),
192-
ASN1_IMP_SEQUENCE_OF_OPT(TimeStampReq, extensions, X509_EXTENSION, 0)
193-
} ASN1_SEQUENCE_END(TimeStampReq)
194-
195-
IMPLEMENT_ASN1_FUNCTIONS(TimeStampReq)
196-
197184
#endif /* ENABLE_CURL */
198185

199186
ASN1_SEQUENCE(TimeStampAccuracy) = {
@@ -302,48 +289,71 @@ static BIO *bio_encode_rfc3161_request(PKCS7 *p7, const EVP_MD *md)
302289
STACK_OF(PKCS7_SIGNER_INFO) *signer_info;
303290
PKCS7_SIGNER_INFO *si;
304291
u_char mdbuf[EVP_MAX_MD_SIZE];
305-
TimeStampReq *req;
306-
BIO *bout, *bhash;
292+
TS_MSG_IMPRINT *msg_imprint = NULL;
293+
X509_ALGOR *alg = NULL;
294+
TS_REQ *req = NULL;
295+
BIO *bout = NULL, *bhash = NULL;
307296
u_char *p;
308297
int len;
309298

310299
signer_info = PKCS7_get_signer_info(p7);
311300
if (!signer_info)
312-
return NULL; /* FAILED */
301+
goto out;
313302

314303
si = sk_PKCS7_SIGNER_INFO_value(signer_info, 0);
315304
if (!si)
316-
return NULL; /* FAILED */
305+
goto out;
317306

318307
bhash = BIO_new(BIO_f_md());
319308
if (!BIO_set_md(bhash, md)) {
320309
printf("Unable to set the message digest of BIO\n");
321-
BIO_free_all(bhash);
322-
return NULL; /* FAILED */
310+
goto out;
323311
}
324312
BIO_push(bhash, BIO_new(BIO_s_null()));
325313
BIO_write(bhash, si->enc_digest->data, si->enc_digest->length);
326314
BIO_gets(bhash, (char*)mdbuf, EVP_MD_size(md));
327-
BIO_free_all(bhash);
328315

329-
req = TimeStampReq_new();
330-
ASN1_INTEGER_set(req->version, 1);
331-
req->messageImprint->digestAlgorithm->algorithm = OBJ_nid2obj(EVP_MD_nid(md));
332-
req->messageImprint->digestAlgorithm->parameters = ASN1_TYPE_new();
333-
req->messageImprint->digestAlgorithm->parameters->type = V_ASN1_NULL;
334-
ASN1_OCTET_STRING_set(req->messageImprint->digest, mdbuf, EVP_MD_size(md));
335-
req->certReq = 0xFF;
316+
req = TS_REQ_new();
317+
if (!req)
318+
goto out;
319+
if (!TS_REQ_set_version(req, 1))
320+
goto out;
321+
322+
msg_imprint = TS_MSG_IMPRINT_new();
323+
if (!msg_imprint)
324+
goto out;
325+
alg = X509_ALGOR_new();
326+
if (!alg)
327+
goto out;
328+
X509_ALGOR_set_md(alg, md);
329+
if (!X509_ALGOR_set0(alg, OBJ_nid2obj(EVP_MD_nid(md)), V_ASN1_NULL, NULL))
330+
goto out;
331+
if (!TS_MSG_IMPRINT_set_algo(msg_imprint, alg))
332+
goto out;
333+
if (!TS_MSG_IMPRINT_set_msg(msg_imprint, mdbuf, EVP_MD_size(md)))
334+
goto out;
335+
if (!TS_REQ_set_msg_imprint(req, msg_imprint))
336+
goto out;
337+
/* TSA is expected to include its signing certificate in the response, flag 0xFF */
338+
if (!TS_REQ_set_cert_req(req, 1))
339+
goto out;
336340

337-
len = i2d_TimeStampReq(req, NULL);
341+
len = i2d_TS_REQ(req, NULL);
338342
p = OPENSSL_malloc((size_t)len);
339-
len = i2d_TimeStampReq(req, &p);
343+
len = i2d_TS_REQ(req, &p);
340344
p -= len;
341-
TimeStampReq_free(req);
342345

343346
bout = BIO_new(BIO_s_mem());
344347
BIO_write(bout, p, len);
345348
OPENSSL_free(p);
346349
(void)BIO_flush(bout);
350+
351+
out:
352+
BIO_free_all(bhash);
353+
TS_MSG_IMPRINT_free(msg_imprint);
354+
X509_ALGOR_free(alg);
355+
TS_REQ_free(req);
356+
347357
return bout;
348358
}
349359

0 commit comments

Comments
 (0)