|
19 | 19 | import java.security.spec.PKCS8EncodedKeySpec; |
20 | 20 | import java.security.spec.X509EncodedKeySpec; |
21 | 21 | import java.util.ArrayList; |
| 22 | +import java.util.Calendar; |
22 | 23 | import java.util.Collection; |
23 | 24 | import java.util.HashMap; |
24 | 25 | import java.util.HashSet; |
25 | 26 | import java.util.Iterator; |
26 | 27 | import java.util.List; |
27 | 28 | import java.util.Map; |
28 | 29 | import java.util.Set; |
| 30 | +import java.util.TimeZone; |
29 | 31 |
|
30 | 32 | import junit.framework.Assert; |
31 | 33 | import junit.framework.Test; |
|
40 | 42 | import org.bouncycastle.asn1.ASN1Primitive; |
41 | 43 | import org.bouncycastle.asn1.ASN1Sequence; |
42 | 44 | import org.bouncycastle.asn1.ASN1TaggedObject; |
| 45 | +import org.bouncycastle.asn1.ASN1UTCTime; |
43 | 46 | import org.bouncycastle.asn1.ASN1Util; |
44 | 47 | import org.bouncycastle.asn1.BERTags; |
45 | 48 | import org.bouncycastle.asn1.DERNull; |
|
54 | 57 | import org.bouncycastle.asn1.cms.ContentInfo; |
55 | 58 | import org.bouncycastle.asn1.cms.SignedData; |
56 | 59 | import org.bouncycastle.asn1.cms.SignerInfo; |
| 60 | +import org.bouncycastle.asn1.cms.Time; |
57 | 61 | import org.bouncycastle.asn1.edec.EdECObjectIdentifiers; |
58 | 62 | import org.bouncycastle.asn1.ess.ESSCertIDv2; |
59 | 63 | import org.bouncycastle.asn1.ess.SigningCertificateV2; |
|
116 | 120 | import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; |
117 | 121 | import org.bouncycastle.test.TestResourceFinder; |
118 | 122 | import org.bouncycastle.util.CollectionStore; |
| 123 | +import org.bouncycastle.util.Properties; |
119 | 124 | import org.bouncycastle.util.Store; |
| 125 | +import org.bouncycastle.util.Strings; |
120 | 126 | import org.bouncycastle.util.encoders.Base64; |
121 | 127 | import org.bouncycastle.util.io.Streams; |
122 | 128 | import org.bouncycastle.util.io.pem.PemObject; |
@@ -1534,6 +1540,138 @@ public void testSHA1WithRSAAndAttributeTableSimple() |
1534 | 1540 | verifyRSASignatures(s, md.digest("Hello world!".getBytes())); |
1535 | 1541 | } |
1536 | 1542 |
|
| 1543 | + public void testSigningTimeMalformedUTCTime() |
| 1544 | + throws Exception |
| 1545 | + { |
| 1546 | + // github #2411: a SignedData in circulation carries the zone-less UTCTime "150612153520" |
| 1547 | + // (seconds present, no trailing "Z") as its signing-time attribute. Since 1.85 the ASN.1 |
| 1548 | + // decoder rejects that as structurally malformed while the signerInfos are being read, so |
| 1549 | + // the whole message fails to load; Properties.ASN1_ALLOW_MALFORMED_TIME restores the legacy |
| 1550 | + // lenient read so the message can still be examined, and getSignerInfos() reports the |
| 1551 | + // strict-mode failure as the CMSException it declares rather than an IllegalStateException. |
| 1552 | + byte[] rawTime = Strings.toByteArray("150612153520"); |
| 1553 | + byte[] utcNoZone = new byte[2 + rawTime.length]; |
| 1554 | + utcNoZone[0] = BERTags.UTC_TIME; |
| 1555 | + utcNoZone[1] = (byte)rawTime.length; |
| 1556 | + System.arraycopy(rawTime, 0, utcNoZone, 2, rawTime.length); |
| 1557 | + |
| 1558 | + CMSTypedData msg = new CMSProcessableByteArray("Hello world!".getBytes()); |
| 1559 | + byte[] encoded; |
| 1560 | + |
| 1561 | + // the lenient read is the only way to obtain such a primitive - use it to build the message |
| 1562 | + // the way the reporter's producer must have. |
| 1563 | + System.setProperty(Properties.ASN1_ALLOW_MALFORMED_TIME, "true"); |
| 1564 | + try |
| 1565 | + { |
| 1566 | + ASN1EncodableVector v = new ASN1EncodableVector(); |
| 1567 | + v.add(new Attribute(CMSAttributes.signingTime, |
| 1568 | + new DERSet(Time.getInstance(ASN1UTCTime.getInstance(utcNoZone))))); |
| 1569 | + |
| 1570 | + List certList = new ArrayList(); |
| 1571 | + certList.add(_origCert); |
| 1572 | + certList.add(_signCert); |
| 1573 | + |
| 1574 | + CMSSignedDataGenerator gen = new CMSSignedDataGenerator(); |
| 1575 | + gen.addSignerInfoGenerator(new JcaSimpleSignerInfoGeneratorBuilder().setProvider(BC) |
| 1576 | + .setSignedAttributeGenerator(new DefaultSignedAttributeTableGenerator(new AttributeTable(v))) |
| 1577 | + .build("SHA256withRSA", _origKP.getPrivate(), _origCert)); |
| 1578 | + gen.addCertificates(new JcaCertStore(certList)); |
| 1579 | + |
| 1580 | + encoded = gen.generate(msg, true).getEncoded(); |
| 1581 | + } |
| 1582 | + finally |
| 1583 | + { |
| 1584 | + System.getProperties().remove(Properties.ASN1_ALLOW_MALFORMED_TIME); |
| 1585 | + } |
| 1586 | + |
| 1587 | + // the attribute went out exactly as supplied |
| 1588 | + assertTrue("zone-less signing time not present in the encoding", indexOf(encoded, utcNoZone) >= 0); |
| 1589 | + |
| 1590 | + // default: the message cannot be loaded, whichever way it is read |
| 1591 | + try |
| 1592 | + { |
| 1593 | + new CMSSignedData(encoded); |
| 1594 | + fail("malformed signing time loaded by default"); |
| 1595 | + } |
| 1596 | + catch (CMSException e) |
| 1597 | + { |
| 1598 | + assertMentions(e, "invalid UTCTime format"); |
| 1599 | + } |
| 1600 | + |
| 1601 | + CMSSignedDataParser sp = new CMSSignedDataParser(new JcaDigestCalculatorProviderBuilder().setProvider(BC).build(), encoded); |
| 1602 | + sp.getSignedContent().drain(); |
| 1603 | + try |
| 1604 | + { |
| 1605 | + sp.getSignerInfos(); |
| 1606 | + fail("malformed signing time loaded by default (parser)"); |
| 1607 | + } |
| 1608 | + catch (CMSException e) |
| 1609 | + { |
| 1610 | + assertMentions(e, "invalid UTCTime format"); |
| 1611 | + } |
| 1612 | + |
| 1613 | + // property set: the message loads and verifies, and the attribute reads as GMT as it did before 1.85 |
| 1614 | + System.setProperty(Properties.ASN1_ALLOW_MALFORMED_TIME, "true"); |
| 1615 | + try |
| 1616 | + { |
| 1617 | + CMSSignedData s = new CMSSignedData(encoded); |
| 1618 | + |
| 1619 | + // verify against the key rather than the certificate: a certificate-bound verifier also |
| 1620 | + // checks validity at the signing time, and 2015 predates the test certificate. |
| 1621 | + SignerInformation signer = (SignerInformation)s.getSignerInfos().getSigners().iterator().next(); |
| 1622 | + assertTrue(signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider(BC).build(_origKP.getPublic()))); |
| 1623 | + |
| 1624 | + Attribute attr = signer.getSignedAttributes().get(CMSAttributes.signingTime); |
| 1625 | + Time signingTime = Time.getInstance(attr.getAttrValues().getObjectAt(0)); |
| 1626 | + |
| 1627 | + assertEquals("150612153520", signingTime.toASN1Primitive().toString()); |
| 1628 | + assertEquals("20150612153520GMT+00:00", signingTime.getTime()); |
| 1629 | + |
| 1630 | + Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); |
| 1631 | + cal.clear(); |
| 1632 | + cal.set(2015, Calendar.JUNE, 12, 15, 35, 20); |
| 1633 | + assertEquals(cal.getTime(), signingTime.getDate()); |
| 1634 | + |
| 1635 | + sp = new CMSSignedDataParser(new JcaDigestCalculatorProviderBuilder().setProvider(BC).build(), encoded); |
| 1636 | + sp.getSignedContent().drain(); |
| 1637 | + signer = (SignerInformation)sp.getSignerInfos().getSigners().iterator().next(); |
| 1638 | + assertTrue(signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider(BC).build(_origKP.getPublic()))); |
| 1639 | + assertEquals("150612153520", signer.getSignedAttributes().get(CMSAttributes.signingTime).getAttrValues().getObjectAt(0).toString()); |
| 1640 | + } |
| 1641 | + finally |
| 1642 | + { |
| 1643 | + System.getProperties().remove(Properties.ASN1_ALLOW_MALFORMED_TIME); |
| 1644 | + } |
| 1645 | + } |
| 1646 | + |
| 1647 | + private static void assertMentions(CMSException e, String text) |
| 1648 | + { |
| 1649 | + String message = e.getMessage(); |
| 1650 | + Exception underlying = e.getUnderlyingException(); |
| 1651 | + if (underlying != null) |
| 1652 | + { |
| 1653 | + message = message + " / " + underlying.getMessage(); |
| 1654 | + } |
| 1655 | + assertTrue(message, message.indexOf(text) >= 0); |
| 1656 | + } |
| 1657 | + |
| 1658 | + private static int indexOf(byte[] data, byte[] pattern) |
| 1659 | + { |
| 1660 | + for (int i = 0; i + pattern.length <= data.length; i++) |
| 1661 | + { |
| 1662 | + int j = 0; |
| 1663 | + while (j != pattern.length && data[i + j] == pattern[j]) |
| 1664 | + { |
| 1665 | + j++; |
| 1666 | + } |
| 1667 | + if (j == pattern.length) |
| 1668 | + { |
| 1669 | + return i; |
| 1670 | + } |
| 1671 | + } |
| 1672 | + return -1; |
| 1673 | + } |
| 1674 | + |
1537 | 1675 | public void testCMSAlgorithmProtection() |
1538 | 1676 | throws Exception |
1539 | 1677 | { |
|
0 commit comments