|
1 | 1 | package org.bouncycastle.tsp.test; |
2 | 2 |
|
| 3 | +import java.io.ByteArrayInputStream; |
3 | 4 | import java.io.ByteArrayOutputStream; |
4 | 5 | import java.io.File; |
5 | 6 | import java.io.FileOutputStream; |
| 7 | +import java.io.IOException; |
6 | 8 | import java.io.OutputStream; |
7 | 9 | import java.math.BigInteger; |
| 10 | +import java.nio.charset.StandardCharsets; |
8 | 11 | import java.security.KeyPair; |
| 12 | +import java.security.NoSuchAlgorithmException; |
9 | 13 | import java.security.MessageDigest; |
10 | 14 | import java.security.PrivateKey; |
11 | 15 | import java.security.Security; |
|
19 | 23 |
|
20 | 24 | import junit.framework.Assert; |
21 | 25 | import junit.framework.TestCase; |
| 26 | +import org.bouncycastle.asn1.ASN1Encoding; |
22 | 27 | import org.bouncycastle.asn1.ASN1ObjectIdentifier; |
23 | 28 | import org.bouncycastle.asn1.nist.NISTObjectIdentifiers; |
24 | 29 | import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
|
31 | 36 | import org.bouncycastle.operator.DigestCalculator; |
32 | 37 | import org.bouncycastle.operator.DigestCalculatorProvider; |
33 | 38 | import org.bouncycastle.operator.OperatorCreationException; |
| 39 | +import org.bouncycastle.operator.bc.BcDigestCalculatorProvider; |
34 | 40 | import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; |
35 | 41 | import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder; |
36 | 42 | import org.bouncycastle.tsp.TSPAlgorithms; |
@@ -1571,4 +1577,79 @@ public DigestCalculator get(AlgorithmIdentifier digestAlgorithmIdentifier) |
1571 | 1577 | } |
1572 | 1578 |
|
1573 | 1579 | } |
| 1580 | + |
| 1581 | + public void testCompareStreamAndByteData () throws TSPException, ERSException, OperatorCreationException, IOException, |
| 1582 | + NoSuchAlgorithmException |
| 1583 | + { |
| 1584 | + // ER for the String "foo" using SHA-256 and a dummy cert/key. |
| 1585 | + String evidenceRecordBase64 = "MIIDCgIBATANMAsGCWCGSAFlA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pY9bbEMuJboe4DkQx2Eqpq1pVaMKRxsjhrnbH8QlkUGtuGztqnZa5AoCth79x70Ch7WhdDcxG3wiFi" |
| 1598 | + + "29pw69obUCh3c61Q2WKl+MKW/tqq7EGYu5+jE="; |
| 1599 | + DigestCalculatorProvider digestProvider = new BcDigestCalculatorProvider(); |
| 1600 | + ERSEvidenceRecord ersEvidenceRecord = new ERSEvidenceRecord( |
| 1601 | + Base64.decode(evidenceRecordBase64), digestProvider); |
| 1602 | + |
| 1603 | + // Sanity check, make sure root hash of ER is what we expect. |
| 1604 | + byte[] sourceData = "foo".getBytes(StandardCharsets.UTF_8); |
| 1605 | + byte[] sourceSha256 = MessageDigest.getInstance("SHA-256").digest(sourceData); |
| 1606 | + assert Arrays.areEqual(sourceSha256, ersEvidenceRecord.getPrimaryRootHash()); |
| 1607 | + |
| 1608 | + |
| 1609 | + // Generate hash renewal request using ERSInputStreamData. |
| 1610 | + ERSData ersStreamData = new ERSInputStreamData(new ByteArrayInputStream(sourceData)); |
| 1611 | + TimeStampRequest streamDataReq = ersEvidenceRecord.generateHashRenewalRequest( |
| 1612 | + digestProvider.get(new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512)), |
| 1613 | + ersStreamData, |
| 1614 | + new TimeStampRequestGenerator(), |
| 1615 | + BigInteger.ZERO); |
| 1616 | + |
| 1617 | + |
| 1618 | + // Generate hash renewal request using ERSByteData to compare against. |
| 1619 | + ERSData ersByteData = new ERSByteData(sourceData); |
| 1620 | + TimeStampRequest byteDataReq = ersEvidenceRecord.generateHashRenewalRequest( |
| 1621 | + digestProvider.get(new AlgorithmIdentifier(NISTObjectIdentifiers.id_sha512)), |
| 1622 | + ersByteData, |
| 1623 | + new TimeStampRequestGenerator(), |
| 1624 | + BigInteger.ZERO); |
| 1625 | + |
| 1626 | + |
| 1627 | + // check ERSByteData and ERSInputStreamData produce same output |
| 1628 | + assert Arrays.areEqual(byteDataReq.getMessageImprintDigest(), |
| 1629 | + streamDataReq.getMessageImprintDigest()); |
| 1630 | + |
| 1631 | + |
| 1632 | + // Generate the digest we expect to see in the requests and compare. |
| 1633 | + byte[] expectedDigest = generateExpectedRequestDigest(sourceData, ersEvidenceRecord, |
| 1634 | + MessageDigest.getInstance("SHA-512")); |
| 1635 | + assert Arrays.areEqual(byteDataReq.getMessageImprintDigest(), expectedDigest); |
| 1636 | + assert Arrays.areEqual(streamDataReq.getMessageImprintDigest(), expectedDigest); |
| 1637 | + } |
| 1638 | + |
| 1639 | + /** Based on RFC 4998 section 5.2. */ |
| 1640 | + private static byte[] generateExpectedRequestDigest (byte[] sourceData, |
| 1641 | + ERSEvidenceRecord evidenceRecord, MessageDigest digest) throws IOException |
| 1642 | + { |
| 1643 | + byte[] atsci = evidenceRecord.toASN1Structure().getArchiveTimeStampSequence().getEncoded(ASN1Encoding.DER); |
| 1644 | + byte[] hi = digest.digest(sourceData); |
| 1645 | + byte[] hai = digest.digest(atsci); |
| 1646 | + byte[] hihai; |
| 1647 | + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { |
| 1648 | + outputStream.write(hi); |
| 1649 | + outputStream.write(hai); |
| 1650 | + hihai = outputStream.toByteArray(); |
| 1651 | + } |
| 1652 | + byte[] hiprime = digest.digest(hihai); |
| 1653 | + return hiprime; |
| 1654 | + } |
1574 | 1655 | } |
0 commit comments