diff options
Diffstat (limited to 'bcprov/src/main/java/org/bouncycastle/asn1/DefiniteLengthInputStream.java')
-rw-r--r-- | bcprov/src/main/java/org/bouncycastle/asn1/DefiniteLengthInputStream.java | 40 |
1 files changed, 38 insertions, 2 deletions
diff --git a/bcprov/src/main/java/org/bouncycastle/asn1/DefiniteLengthInputStream.java b/bcprov/src/main/java/org/bouncycastle/asn1/DefiniteLengthInputStream.java index d7b51ded..359bcc25 100644 --- a/bcprov/src/main/java/org/bouncycastle/asn1/DefiniteLengthInputStream.java +++ b/bcprov/src/main/java/org/bouncycastle/asn1/DefiniteLengthInputStream.java @@ -15,13 +15,15 @@ class DefiniteLengthInputStream private static final byte[] EMPTY_BYTES = new byte[0]; private final int _originalLength; + private int _remaining; DefiniteLengthInputStream( InputStream in, - int length) + int length, + int limit) { - super(in, length); + super(in, limit); if (length < 0) { @@ -89,6 +91,33 @@ class DefiniteLengthInputStream return numRead; } + void readAllIntoByteArray(byte[] buf) + throws IOException + { + if (_remaining != buf.length) + { + throw new IllegalArgumentException("buffer length not right for data"); + } + + if (_remaining == 0) + { + return; + } + + // make sure it's safe to do this! + int limit = getLimit(); + if (_remaining >= limit) + { + throw new IOException("corrupted stream - out of bounds length found: " + _remaining + " >= " + limit); + } + + if ((_remaining -= Streams.readFully(_in, buf)) != 0) + { + throw new EOFException("DEF length " + _originalLength + " object truncated by " + _remaining); + } + setParentEofDetect(true); + } + byte[] toByteArray() throws IOException { @@ -97,6 +126,13 @@ class DefiniteLengthInputStream return EMPTY_BYTES; } + // make sure it's safe to do this! + int limit = getLimit(); + if (_remaining >= limit) + { + throw new IOException("corrupted stream - out of bounds length found: " + _remaining + " >= " + limit); + } + byte[] bytes = new byte[_remaining]; if ((_remaining -= Streams.readFully(_in, bytes)) != 0) { |