Class DerValueInputStream
java.lang.Object
java.io.InputStream
java.io.FilterInputStream
com.microej.util.appverifier.util.DerValueInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
This InputStream implementation is used to limit the reading operations to a defined length of a DER encoded stream.
The input stream passed as the parameter of the constructor MUST be positioned right be before the Tag byte of a DER
value. This
DerValueInputStream can be considered as a limited, narrowed, input stream. If this
DerValueInputStream can only be read for the length of the DER value the InputStream passed as the
parameter of the constructor may contain more data.
In addition, a method is called after each read: read() will automatically call
onDataRead(byte), read(byte[], int, int) will automatically
call onDataRead(byte[], int, int), and skip(long) performs
read() operations. Finally onStreamEnd() is called right
after the last byte of the DER value has been returned.
A classic usage of this class is a signed DER Value, as shown in the following code example. A hash is updated in
onDataRead(byte[], int, int) and onDataRead(byte) and the
signature is verified in onStreamEnd().
final Signature signature;
[...]
DerValueInputStream filteredInputStream = new DerValueInputStream(signedApplication) {
@Override
protected void onStreamEnd() throws IOException {
validateSignature(signature);
}
@Override
protected void onDataRead(byte[] data, int off, int len) {
try {
signature.update(data, off, len);
} catch (SignatureException e) {
throw new IllegalStateException(e);
}
}
@Override
protected void onDataRead(byte b) {
try {
signature.update(b);
} catch (SignatureException e) {
throw new IllegalStateException(e);
}
}
};
- Since:
- 1.0.0
-
Field Summary
Fields inherited from class java.io.FilterInputStream
in -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a fixed length input stream of a DER encoded stream. -
Method Summary
Modifier and TypeMethodDescriptionintprotected voidChecks if the end of the stream is reached.voidclose()Does nothing: closing this stream must not close the underlying signed stream, which is still being read.intgetTag()Returns the tag of the stream, e.g.protected voidonDataRead(byte b) Called after a data read to apply a process on it, such as an update of hash.protected voidonDataRead(byte[] data, int off, int len) Called after a data read to apply a process on it, such as an update of hash.protected voidCalled once the end of the stream is reached.intread()intread(byte[] b, int off, int len) intReturns the number of bytes that remains in the stream.longskip(long n) Consumes, and drops, a given number of bytes.Methods inherited from class java.io.FilterInputStream
mark, markSupported, read, resetMethods inherited from class java.io.InputStream
nullInputStream, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Constructor Details
-
DerValueInputStream
Constructs a fixed length input stream of a DER encoded stream. The first bytes of the stream are automatically read in the constructor in order to determine the tag value as well as the length of the stream.- Parameters:
in- the input stream to limit.- Throws:
IOException- if an I/O error occurs.DerFormatException- if an error occurs while parsing.
-
-
Method Details
-
close
public void close()Does nothing: closing this stream must not close the underlying signed stream, which is still being read.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classFilterInputStream
-
onStreamEnd
Called once the end of the stream is reached. This allows, for instance, to retrieve a hash or perform a signature verification.- Throws:
IOException- as this method may read further in the input stream passed as parameter of the constructor, it may throwIOExceptionon I/O errors.RuntimeException- As this method can be user-defined, it may throw onlyIOException,ErrororRuntimeException.
-
onDataRead
protected void onDataRead(byte[] data, int off, int len) Called after a data read to apply a process on it, such as an update of hash.- Parameters:
data- the array of bytes.off- the offset to start from in the array of bytes.len- the number of bytes to use, starting at offset.- Throws:
RuntimeException- As this method can be user-defined, it may throw onlyIOException,ErrororRuntimeException.
-
onDataRead
protected void onDataRead(byte b) Called after a data read to apply a process on it, such as an update of hash.- Parameters:
b- the byte to use for the update.- Throws:
RuntimeException- As this method can be user-defined, it may throw onlyIOException,ErrororRuntimeException.
-
available
- Overrides:
availablein classFilterInputStream- Throws:
IOException
-
read
If
lenis bigger than the number of remaining bytes, it is clamped to the remaining bytes.- Overrides:
readin classFilterInputStream- Throws:
IOException
-
read
- Overrides:
readin classFilterInputStream- Throws:
IOException
-
checkStreamEnd
Checks if the end of the stream is reached. If so, triggers signature verification.- Throws:
IOException- if an I/O error occurs.RuntimeException- AnyRuntimeExceptioncould be thrown by this process.
-
skip
Consumes, and drops, a given number of bytes. These bytes are consumed instead of just dropped in order to update the hash.- Overrides:
skipin classFilterInputStream- Parameters:
n- the number of bytes to skip.- Returns:
- the number of bytes actually skipped.
- Throws:
IOException- if an I/O error occurs.
-
remainingLength
public int remainingLength()Returns the number of bytes that remains in the stream.- Returns:
- the remaining number of bytes.
-
getTag
public int getTag()Returns the tag of the stream, e.g. first byte read.- Returns:
- the DER tag of the stream.
-