Skip to content

Commit 59ccfb0

Browse files
committed
modified ERSInputStreamData to explicitly recognize files - relates to #1859
1 parent 4998af4 commit 59ccfb0

File tree

2 files changed

+38
-26
lines changed

2 files changed

+38
-26
lines changed

pkix/src/main/java/org/bouncycastle/tsp/ers/ERSEvidenceRecord.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -396,20 +396,10 @@ public TimeStampRequest generateHashRenewalRequest(DigestCalculator digCalc, ERS
396396
public TimeStampRequest generateHashRenewalRequest(DigestCalculator digCalc, ERSData data, TimeStampRequestGenerator tspReqGen, BigInteger nonce)
397397
throws ERSException, TSPException, IOException
398398
{
399-
ERSData copy;
400-
if (data instanceof ERSInputStreamData)
401-
{
402-
copy = ((ERSInputStreamData) data).toByteData();
403-
404-
}
405-
else
406-
{
407-
copy = data;
408-
}
409399
// check old data present
410400
try
411401
{
412-
firstArchiveTimeStamp.validatePresent(copy, new Date());
402+
firstArchiveTimeStamp.validatePresent(data, new Date());
413403
}
414404
catch (Exception e)
415405
{
@@ -418,7 +408,7 @@ public TimeStampRequest generateHashRenewalRequest(DigestCalculator digCalc, ERS
418408

419409
ERSArchiveTimeStampGenerator atsGen = new ERSArchiveTimeStampGenerator(digCalc);
420410

421-
atsGen.addData(copy);
411+
atsGen.addData(data);
422412

423413
atsGen.addPreviousChains(evidenceRecord.getArchiveTimeStampSequence());
424414

pkix/src/main/java/org/bouncycastle/tsp/ers/ERSInputStreamData.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package org.bouncycastle.tsp.ers;
22

3-
import java.io.ByteArrayOutputStream;
43
import java.io.File;
54
import java.io.FileInputStream;
65
import java.io.FileNotFoundException;
7-
import java.io.InputStream;
86
import java.io.IOException;
7+
import java.io.InputStream;
98

109
import org.bouncycastle.operator.DigestCalculator;
1110
import org.bouncycastle.util.io.Streams;
@@ -16,7 +15,8 @@
1615
public class ERSInputStreamData
1716
extends ERSCachingData
1817
{
19-
private final InputStream content;
18+
private final File contentFile;
19+
private final byte[] contentBytes;
2020

2121
public ERSInputStreamData(File content)
2222
throws FileNotFoundException
@@ -25,25 +25,47 @@ public ERSInputStreamData(File content)
2525
{
2626
throw new IllegalArgumentException("directory not allowed");
2727
}
28-
this.content = new FileInputStream(content);
28+
if (!content.exists())
29+
{
30+
throw new FileNotFoundException(content + " not found");
31+
}
32+
this.contentBytes = null;
33+
this.contentFile = content;
2934
}
3035

3136
public ERSInputStreamData(InputStream content)
3237
{
33-
this.content = content;
34-
}
35-
36-
protected ERSByteData toByteData()
37-
throws IOException
38-
{
39-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
40-
Streams.pipeAll(this.content, baos);
41-
return new ERSByteData(baos.toByteArray());
38+
try
39+
{
40+
this.contentBytes = Streams.readAll(content);
41+
}
42+
catch (IOException e)
43+
{
44+
throw ExpUtil.createIllegalState("unable to open content: " + e.getMessage(), e);
45+
}
46+
this.contentFile = null;
4247
}
4348

4449
protected byte[] calculateHash(DigestCalculator digestCalculator, byte[] previousChainHash)
4550
{
46-
byte[] hash = ERSUtil.calculateDigest(digestCalculator, content);
51+
byte[] hash;
52+
if (contentBytes != null)
53+
{
54+
hash = ERSUtil.calculateDigest(digestCalculator, contentBytes);
55+
}
56+
else
57+
{
58+
try
59+
{
60+
InputStream content = new FileInputStream(contentFile);
61+
hash = ERSUtil.calculateDigest(digestCalculator, content);
62+
content.close();
63+
}
64+
catch (IOException e)
65+
{
66+
throw ExpUtil.createIllegalState("unable to open content: " + e.getMessage(), e);
67+
}
68+
}
4769

4870
if (previousChainHash != null)
4971
{

0 commit comments

Comments
 (0)