@@ -449,6 +449,9 @@ public PdfPKCS7(byte[] contentsKey, PdfName filterSubtype, String provider) {
449
449
IASN1Set attributeValues = ts .getAttrValues ();
450
450
IASN1Sequence tokenSequence =
451
451
BOUNCY_CASTLE_FACTORY .createASN1SequenceInstance (attributeValues .getObjectAt (0 ));
452
+ this .timestampSignatureContainer = new PdfPKCS7 (tokenSequence .getEncoded (),
453
+ PdfName .ETSI_RFC3161 , BOUNCY_CASTLE_FACTORY .getProviderName ());
454
+ this .timestampSignatureContainer .update (signatureValue , 0 , signatureValue .length );
452
455
this .timestampCerts = SignUtils .readAllCerts (tokenSequence .getEncoded ());
453
456
IContentInfo contentInfo = BOUNCY_CASTLE_FACTORY .createContentInfo (tokenSequence );
454
457
this .timeStampTokenInfo = BOUNCY_CASTLE_FACTORY .createTSTInfo (contentInfo );
@@ -461,6 +464,7 @@ public PdfPKCS7(byte[] contentsKey, PdfName filterSubtype, String provider) {
461
464
this .timestampCerts = this .certs ;
462
465
String algOID = timeStampTokenInfo .getMessageImprint ().getHashAlgorithm ().getAlgorithm ().getId ();
463
466
messageDigest = DigestAlgorithms .getMessageDigestFromOid (algOID , null );
467
+ encContDigest = DigestAlgorithms .getMessageDigest (getDigestAlgorithmName (), provider );
464
468
} else {
465
469
if (this .encapMessageContent != null || digestAttr != null ) {
466
470
if (PdfName .Adbe_pkcs7_sha1 .equals (getFilterSubtype ())) {
@@ -1323,32 +1327,39 @@ public boolean verifySignatureIntegrityAndAuthenticity() throws GeneralSecurityE
1323
1327
if (verified ) {
1324
1328
return verifyResult ;
1325
1329
}
1326
- if (isTsp ) {
1327
- IMessageImprint imprint = timeStampTokenInfo .getMessageImprint ();
1328
- byte [] md = messageDigest .digest ();
1329
- byte [] imphashed = imprint .getHashedMessage ();
1330
- verifyResult = Arrays .equals (md , imphashed );
1331
- } else {
1332
- if (sigAttr != null || sigAttrDer != null ) {
1333
- final byte [] msgDigestBytes = messageDigest .digest ();
1334
- boolean verifySignedMessageContent = true ;
1335
- // Stefan Santesson fixed a bug, keeping the code backward compatible
1336
- boolean encContDigestCompare = false ;
1337
- if (encapMessageContent != null ) {
1330
+ if (sigAttr != null || sigAttrDer != null ) {
1331
+ final byte [] msgDigestBytes = messageDigest .digest ();
1332
+ boolean verifySignedMessageContent = true ;
1333
+ // Stefan Santesson fixed a bug, keeping the code backward compatible
1334
+ boolean encContDigestCompare = false ;
1335
+ if (encapMessageContent != null ) {
1336
+ if (isTsp ) {
1337
+ byte [] tstInfo = new byte [0 ];
1338
+ try {
1339
+ tstInfo = timeStampTokenInfo .toASN1Primitive ().getEncoded ();
1340
+ } catch (IOException e ) {
1341
+ // Ignore.
1342
+ }
1343
+ // Check that encapMessageContent is TSTInfo
1344
+ boolean isTSTInfo = Arrays .equals (tstInfo , encapMessageContent );
1345
+ IMessageImprint imprint = timeStampTokenInfo .getMessageImprint ();
1346
+ byte [] imphashed = imprint .getHashedMessage ();
1347
+ verifySignedMessageContent = isTSTInfo && Arrays .equals (msgDigestBytes , imphashed );
1348
+ } else {
1338
1349
verifySignedMessageContent = Arrays .equals (msgDigestBytes , encapMessageContent );
1339
- encContDigest .update (encapMessageContent );
1340
- encContDigestCompare = Arrays .equals (encContDigest .digest (), digestAttr );
1341
- }
1342
- boolean absentEncContDigestCompare = Arrays .equals (msgDigestBytes , digestAttr );
1343
- boolean concludingDigestCompare = absentEncContDigestCompare || encContDigestCompare ;
1344
- boolean sigVerify = verifySigAttributes (sigAttr ) || verifySigAttributes (sigAttrDer );
1345
- verifyResult = concludingDigestCompare && sigVerify && verifySignedMessageContent ;
1346
- } else {
1347
- if (encapMessageContent != null ) {
1348
- SignUtils .updateVerifier (sig , messageDigest .digest ());
1349
1350
}
1350
- verifyResult = sig .verify (signatureValue );
1351
+ encContDigest .update (encapMessageContent );
1352
+ encContDigestCompare = Arrays .equals (encContDigest .digest (), digestAttr );
1353
+ }
1354
+ boolean absentEncContDigestCompare = Arrays .equals (msgDigestBytes , digestAttr );
1355
+ boolean concludingDigestCompare = absentEncContDigestCompare || encContDigestCompare ;
1356
+ boolean sigVerify = verifySigAttributes (sigAttr ) || verifySigAttributes (sigAttrDer );
1357
+ verifyResult = concludingDigestCompare && sigVerify && verifySignedMessageContent ;
1358
+ } else {
1359
+ if (encapMessageContent != null ) {
1360
+ SignUtils .updateVerifier (sig , messageDigest .digest ());
1351
1361
}
1362
+ verifyResult = sig .verify (signatureValue );
1352
1363
}
1353
1364
verified = true ;
1354
1365
return verifyResult ;
@@ -1623,11 +1634,15 @@ private void findOcsp(IASN1Sequence seq) throws IOException {
1623
1634
*/
1624
1635
private boolean isCades ;
1625
1636
1637
+ /**
1638
+ * Inner timestamp signature container.
1639
+ */
1640
+ private PdfPKCS7 timestampSignatureContainer ;
1641
+
1626
1642
/**
1627
1643
* BouncyCastle TSTInfo.
1628
1644
*/
1629
1645
private ITSTInfo timeStampTokenInfo ;
1630
-
1631
1646
/**
1632
1647
* Check if it's a PAdES-LTV time stamp.
1633
1648
*
@@ -1637,6 +1652,15 @@ public boolean isTsp() {
1637
1652
return isTsp ;
1638
1653
}
1639
1654
1655
+ /**
1656
+ * Retrieves inner timestamp signature container if there is one.
1657
+ *
1658
+ * @return timestamp signature container or null.
1659
+ */
1660
+ public PdfPKCS7 getTimestampSignatureContainer () {
1661
+ return timestampSignatureContainer ;
1662
+ }
1663
+
1640
1664
/**
1641
1665
* Gets the timestamp token info if there is one.
1642
1666
*
0 commit comments