Class DerValueInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
com.microej.util.appverifier.util.DerValueInputStream
All Implemented Interfaces:
Closeable, AutoCloseable

public class DerValueInputStream extends FilterInputStream
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
  • Constructor Details

    • DerValueInputStream

      public DerValueInputStream(InputStream in) throws IOException, DerFormatException
      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:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class FilterInputStream
    • onStreamEnd

      protected void onStreamEnd() throws IOException
      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 throw IOException on I/O errors.
      RuntimeException - As this method can be user-defined, it may throw only IOException, Error or RuntimeException.
    • 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 only IOException, Error or RuntimeException.
    • 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 only IOException, Error or RuntimeException.
    • available

      public int available() throws IOException
      Overrides:
      available in class FilterInputStream
      Throws:
      IOException
    • read

      public int read(byte[] b, int off, int len) throws IOException

      If len is bigger than the number of remaining bytes, it is clamped to the remaining bytes.

      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • read

      public int read() throws IOException
      Overrides:
      read in class FilterInputStream
      Throws:
      IOException
    • checkStreamEnd

      protected void checkStreamEnd() throws IOException
      Checks if the end of the stream is reached. If so, triggers signature verification.
      Throws:
      IOException - if an I/O error occurs.
      RuntimeException - Any RuntimeException could be thrown by this process.
    • skip

      public long skip(long n) throws IOException
      Consumes, and drops, a given number of bytes. These bytes are consumed instead of just dropped in order to update the hash.
      Overrides:
      skip in class FilterInputStream
      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.