Class DerInputStream
java.lang.Object
org.mozilla.jss.netscape.security.util.DerInputStream
A DER input stream, used for parsing ASN.1 DER-encoded data such as
that found in X.509 certificates. DER is a subset of BER/1, which has
the advantage that it allows only a single encoding of primitive data.
(High level data such as dates still support many encodings.) That is,
it uses the "Definite" Encoding Rules (DER) not the "Basic" ones (BER).
Note that, like BER/1, DER streams are streams of explicitly tagged data values. Accordingly, this programming interface does not expose any variant of the java.io.InputStream interface, since that kind of input stream holds untagged data values and using that I/O model could prevent correct parsing of the DER data.
At this time, this class supports only a subset of the types of DER data encodings which are defined. That subset is sufficient for parsing most X.509 certificates.
- Version:
- 1.35
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionDerInputStream
(byte[] data) Create a DER input stream from a data buffer.DerInputStream
(byte[] data, int offset, int len) Create a DER input stream from part of a data buffer. -
Method Summary
Modifier and TypeMethodDescriptionint
Returns the number of bytes available for reading.byte[]
Get a bit string from the input stream.(package private) int
getByte()
void
getBytes
(byte[] val) Returns the asked number of bytes from the input stream.Get a single DER-encoded value from the input stream.Get a Generalized encoded time value from the input stream.Get an (unsigned) integer from the input stream.(package private) int
(package private) static int
getLength
(InputStream in) void
getNull()
Reads an encoded null value from the input stream.byte[]
Returns an ASN.1 OCTET STRING from the input stream.getOID()
Reads an X.200 style Object Identifier from the stream.DerValue[]
getSequence
(int startLen) Return a sequence of encoded entities.DerValue[]
getSet
(int startLen) Return a set of encoded entities.DerValue[]
getSet
(int startLen, boolean implicit) Return a set of encoded entities.Get a bit string from the input stream.Get a UTC encoded time value from the input stream.void
mark
(int value) Mark the current position in the buffer, so that a later call toreset
will return here.int
peekByte()
protected DerValue[]
readVector
(int startLen) void
reset()
Return to the position of the lastmark
call.void
skipSequence
(int startLen) subStream
(int len, boolean do_skip) Creates a new DER input stream from part of this input stream.byte[]
Return what has been written to this DerInputStream as a byte array.
-
Field Details
-
buffer
DerInputBuffer buffer
-
-
Constructor Details
-
DerInputStream
public DerInputStream(byte[] data) Create a DER input stream from a data buffer. The buffer is not copied, it is shared. Accordingly, the buffer should be treated as read-only.- Parameters:
data
- the buffer from which to create the string (CONSUMED)
-
DerInputStream
public DerInputStream(byte[] data, int offset, int len) Create a DER input stream from part of a data buffer. The buffer is not copied, it is shared. Accordingly, the buffer should be treated as read-only.- Parameters:
data
- the buffer from which to create the string (CONSUMED)offset
- the first index of data which will be read as DER input in the new streamlen
- how long a chunk of the buffer to use, starting at "offset"
-
DerInputStream
DerInputStream(DerInputBuffer buf)
-
-
Method Details
-
subStream
Creates a new DER input stream from part of this input stream.- Parameters:
len
- how long a chunk of the current input stream to use, starting at the current position.do_skip
- true if the existing data in the input stream should be skipped. If this value is false, the next data read on this stream and the newly created stream will be the same.- Throws:
IOException
-
toByteArray
Return what has been written to this DerInputStream as a byte array. Useful for debugging.- Throws:
IOException
-
getInteger
Get an (unsigned) integer from the input stream.- Throws:
IOException
-
getBitString
Get a bit string from the input stream. Only octet-aligned bitstrings (multiples of eight bits in length) are handled by this method.- Throws:
IOException
-
getUnalignedBitString
Get a bit string from the input stream. The bit string need not be byte-aligned.- Throws:
IOException
-
getOctetString
Returns an ASN.1 OCTET STRING from the input stream.- Throws:
IOException
-
getBytes
Returns the asked number of bytes from the input stream.- Throws:
IOException
-
getNull
Reads an encoded null value from the input stream.- Throws:
IOException
-
getOID
Reads an X.200 style Object Identifier from the stream.- Throws:
IOException
-
getSequence
Return a sequence of encoded entities. ASN.1 sequences are ordered, and they are often used, like a "struct" in C or C++, to group data values. They may have optional or context specific values.- Parameters:
startLen
- guess about how long the sequence will be (used to initialize an auto-growing data structure)- Returns:
- array of the values in the sequence
- Throws:
IOException
-
skipSequence
- Throws:
IOException
-
getSet
Return a set of encoded entities. ASN.1 sets are unordered, though DER may specify an order for some kinds of sets (such as the attributes in an X.500 relative distinguished name) to facilitate binary comparisons of encoded values.- Parameters:
startLen
- guess about how large the set will be (used to initialize an auto-growing data structure)- Returns:
- array of the values in the sequence
- Throws:
IOException
-
getSet
Return a set of encoded entities. ASN.1 sets are unordered, though DER may specify an order for some kinds of sets (such as the attributes in an X.500 relative distinguished name) to facilitate binary comparisons of encoded values.- Parameters:
startLen
- guess about how large the set will be (used to initialize an auto-growing data structure)implicit
- if true tag is assumed implicit.- Returns:
- array of the values in the sequence
- Throws:
IOException
-
readVector
- Throws:
IOException
-
getDerValue
Get a single DER-encoded value from the input stream. It can often be useful to pull a value from the stream and defer parsing it. For example, you can pull a nested sequence out with one call, and only examine its elements later when you really need to.- Throws:
IOException
-
getPrintableString
- Throws:
IOException
-
getT61String
- Throws:
IOException
-
getIA5String
- Throws:
IOException
-
getBMPString
- Throws:
IOException
-
getUniversalString
- Throws:
IOException
-
getDirectoryString
- Throws:
IOException
-
getUTCTime
Get a UTC encoded time value from the input stream.- Throws:
IOException
-
getGeneralizedTime
Get a Generalized encoded time value from the input stream.- Throws:
IOException
-
getByte
- Throws:
IOException
-
peekByte
- Throws:
IOException
-
getLength
- Throws:
IOException
-
getLength
- Throws:
IOException
-
mark
public void mark(int value) Mark the current position in the buffer, so that a later call toreset
will return here. -
reset
public void reset()Return to the position of the lastmark
call. A mark is implicitly set at the beginning of the stream when it is created. -
available
public int available()Returns the number of bytes available for reading. This is most useful for testing whether the stream is empty.
-