Class DerInputStreamHelper
java.lang.Object
com.microej.util.appverifier.util.DerInputStreamHelper
A DER format parsing helper class.
- Since:
- 1.0.0
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final byteMask for constructed types.static final byteMask for explicit tagging.static final byteTag value indicating an ASN.1 "BIT STRING" value.static final byteTag value indicating an ASN.1 "BMPString" value.static final byteTag value indicating an ASN.1 "BOOLEAN" value.static final byteTag value including an ASN.1 "ENUMERATED" valuestatic final byteTag value indicating an ASN.1 "GenerallString" value.static final byteTag value indicating an ASN.1 "GeneralizedTime" value.static final byteTag value including an ASCII stringstatic final byteTag value indicating an ASN.1 "INTEGER" value.static final byteTag value indicating an ASN.1 "NULL" value.static final byteTag value indicating an ASN.1 "OBJECT IDENTIFIER" value.static final byteTag value indicating an ASN.1 "OCTET STRING" value.static final byteTag value including a "printable" stringstatic final byteTag value indicating an ASN.1 "SEQUENCE" (zero to N elements, order is significant).static final byteTag value indicating an ASN.1 "SEQUENCE OF" (one to N elements, order is significant).static final byteTag value indicating an ASN.1 "SET" (zero to N members, order does not matter).static final byteTag value indicating an ASN.1 "SET OF" (one to N members, order does not matter).static final byteTag value including a "teletype" stringstatic final byteTag value indicating an ASN.1 "UniversalString" value.static final byteTag value indicating an ASN.1 "UTCTime" value.static final byteTag value indicating an ASN.1 "UTF8String" value. -
Method Summary
Modifier and TypeMethodDescriptionstatic intexpectTag(byte expectedTag, InputStream stream) Reads next byte from the InputStream and verifies if the tag matches the expected tag.static byte[]Reads the next DER-encoded BitString value from the specified InputStream, and returns it as a byte[].static booleanReads the next DER-encoded Boolean value from the specified InputStream, and returns it as a boolean.static intReads a DER-encoded value length from an input stream.static voidreadFully(InputStream in, byte[] buffer, int off, int len) See the general contract of thereadFullymethod ofDataInput.static intReads the next DER-encoded Integer value from the specified InputStream, and returns it as an integer.static voidreadNull(InputStream is) Reads the next DER-encoded NULL value from the specified InputStream, as a null value.static StringReads the next DER-encoded ObjectID value from the specified InputStream, and returns it as a String representation in dotted-decimal notation (for example1.2.840.113549.1.1.11).static StringReads the next DER-encoded value from the specified InputStream, and returns it as a String.static StringReads the next DER-encoded UTF8String value from the specified InputStream, and returns it as a String.
-
Field Details
-
TAG_BOOLEAN
public static final byte TAG_BOOLEANTag value indicating an ASN.1 "BOOLEAN" value.- See Also:
-
TAG_INTEGER
public static final byte TAG_INTEGERTag value indicating an ASN.1 "INTEGER" value.- See Also:
-
TAG_BIT_STRING
public static final byte TAG_BIT_STRINGTag value indicating an ASN.1 "BIT STRING" value.- See Also:
-
TAG_OCTET_STRING
public static final byte TAG_OCTET_STRINGTag value indicating an ASN.1 "OCTET STRING" value.- See Also:
-
TAG_NULL
public static final byte TAG_NULLTag value indicating an ASN.1 "NULL" value.- See Also:
-
TAG_OBJECT_ID
public static final byte TAG_OBJECT_IDTag value indicating an ASN.1 "OBJECT IDENTIFIER" value.- See Also:
-
TAG_ENUMERATED
public static final byte TAG_ENUMERATEDTag value including an ASN.1 "ENUMERATED" value- See Also:
-
TAG_UTF8_STRING
public static final byte TAG_UTF8_STRINGTag value indicating an ASN.1 "UTF8String" value.- See Also:
-
TAG_PRINTABLE_STRING
public static final byte TAG_PRINTABLE_STRINGTag value including a "printable" string- See Also:
-
TAG_T61_STRING
public static final byte TAG_T61_STRINGTag value including a "teletype" string- See Also:
-
TAG_IA5_STRING
public static final byte TAG_IA5_STRINGTag value including an ASCII string- See Also:
-
TAG_UTC_TIME
public static final byte TAG_UTC_TIMETag value indicating an ASN.1 "UTCTime" value.- See Also:
-
TAG_GENERALIZED_TIME
public static final byte TAG_GENERALIZED_TIMETag value indicating an ASN.1 "GeneralizedTime" value.- See Also:
-
TAG_GENERAL_STRING
public static final byte TAG_GENERAL_STRINGTag value indicating an ASN.1 "GenerallString" value.- See Also:
-
TAG_UNIVERSAL_STRING
public static final byte TAG_UNIVERSAL_STRINGTag value indicating an ASN.1 "UniversalString" value.- See Also:
-
TAG_BMP_STRING
public static final byte TAG_BMP_STRINGTag value indicating an ASN.1 "BMPString" value.- See Also:
-
TAG_SEQUENCE
public static final byte TAG_SEQUENCETag value indicating an ASN.1 "SEQUENCE" (zero to N elements, order is significant).- See Also:
-
TAG_SEQUENCE_OF
public static final byte TAG_SEQUENCE_OFTag value indicating an ASN.1 "SEQUENCE OF" (one to N elements, order is significant).- See Also:
-
TAG_SET
public static final byte TAG_SETTag value indicating an ASN.1 "SET" (zero to N members, order does not matter).- See Also:
-
TAG_SET_OF
public static final byte TAG_SET_OFTag value indicating an ASN.1 "SET OF" (one to N members, order does not matter).- See Also:
-
MASK_CONTEXT
public static final byte MASK_CONTEXTMask for explicit tagging.- See Also:
-
MASK_CONSTRUCTED
public static final byte MASK_CONSTRUCTEDMask for constructed types.- See Also:
-
-
Method Details
-
readFully
See the general contract of thereadFullymethod ofDataInput.Bytes for this operation are read from the contained input stream.
- Parameters:
in- input stream from which to read.buffer- the buffer into which the data is read.off- the start offset of the data.len- the number of bytes to read.- Throws:
EOFException- if this input stream reaches the end before reading all the bytes.IOException- the stream has been closed and the contained input stream does not support reading after close, or another I/O error occurs.
-
readInteger
Reads the next DER-encoded Integer value from the specified InputStream, and returns it as an integer.- Parameters:
is- the InputStream to parse- Returns:
- the read value, as an int
- Throws:
DerFormatException- if the value is not correctly formatted.IOException- if an I/O error occurs.
-
readUTF8String
Reads the next DER-encoded UTF8String value from the specified InputStream, and returns it as a String.- Parameters:
is- the InputStream to parse- Returns:
- the read value, as a String
- Throws:
DerFormatException- if the value is not correctly formatted.IOException- if an I/O error occurs.
-
readString
Reads the next DER-encoded value from the specified InputStream, and returns it as a String. Supported string types are:- UTF8String
- IA5String
- PrintableString
- Parameters:
is- the InputStream to parse- Returns:
- the read value, as a String
- Throws:
DerFormatException- if the value is not correctly formatted.IOException- if an I/O error occurs.
-
readBoolean
Reads the next DER-encoded Boolean value from the specified InputStream, and returns it as a boolean.- Parameters:
is- the InputStream to parse- Returns:
- the read value, as a boolean
- Throws:
DerFormatException- if the value is not correctly formatted.IOException- if an I/O error occurs.
-
readOIDString
Reads the next DER-encoded ObjectID value from the specified InputStream, and returns it as a String representation in dotted-decimal notation (for example1.2.840.113549.1.1.11).This method does not check the encoding.
- Parameters:
is- the InputStream to parse- Returns:
- the read value, as String representing the OID.
- Throws:
DerFormatException- if the value is not correctly formatted.IOException- if an I/O error occurs.
-
readNull
Reads the next DER-encoded NULL value from the specified InputStream, as a null value. Obviously returns nothing.- Parameters:
is- the InputStream to parse- Throws:
DerFormatException- if the value is not correctly formatted.IOException- if an I/O error occurs.
-
readBitString
Reads the next DER-encoded BitString value from the specified InputStream, and returns it as a byte[].- Parameters:
is- the InputStream to parse- Returns:
- the read value, as a byte[]
- Throws:
DerFormatException- if the value is not correctly formatted.IOException- if an I/O error occurs.
-
expectTag
public static int expectTag(byte expectedTag, InputStream stream) throws IOException, DerFormatException Reads next byte from the InputStream and verifies if the tag matches the expected tag. If so returns length of the value, otherwise throws an IOException- Parameters:
expectedTag- tag expected according to the stream format.stream- stream to be parsed.- Returns:
- lengths of the value if the read tag matches the expected tag.
- Throws:
IOException- if an I/O error occurs.DerFormatException- if an error occurs while parsing.
-
readDERLength
Reads a DER-encoded value length from an input stream. The length is encoded as follow- Short form: only one byte, between 0 and 127
- Long form: 1 first byte, whose 8th bit is set to 1, and 7 other bits indicate the following number of bytes storing the length as a big endian number
- Parameters:
in- the InputStream to read from.- Returns:
- the encoded length, as an integer.
- Throws:
IOException- if an I/O error occurs.DerFormatException- if an error occurs while parsing.
-