Package org.wildfly.security.asn1
Class DERDecoder
- java.lang.Object
-
- org.wildfly.security.asn1.DERDecoder
-
- All Implemented Interfaces:
ASN1Decoder
public class DERDecoder extends Object implements ASN1Decoder
A class used to decode ASN.1 values that have been encoded using the Distinguished Encoding Rules (DER).- Author:
- Farah Juma
-
-
Constructor Summary
Constructors Constructor Description DERDecoder(byte[] buf)Create a DER decoder that will decode values from the given byte array.DERDecoder(byte[] buf, int offset, int length)Create a DER decoder that will decode values from the given byte array.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description byte[]decodeBitString()Decode the next ASN.1 element as a bit string.BigIntegerdecodeBitStringAsInteger()Decode the next ASN.1 element as a bit string where the value is a ASN.1 INTEGER.StringdecodeBitStringAsString()Decode the next ASN.1 element as a bit string.StringdecodeBMPString()Decode the next ASN.1 element as an BMPString.byte[]decodeBMPStringAsBytes()Decode the next ASN.1 element as an BMPString.booleandecodeBoolean()Decode the next ASN.1 element as a boolean value.StringdecodeIA5String()Decode the next ASN.1 element as an IA5 string.byte[]decodeIA5StringAsBytes()Decode the next ASN.1 element as an IA5 string.voiddecodeImplicit(int number)Indicate that the next ASN.1 element has the given implicit, context-specific tag.voiddecodeImplicit(int clazz, int number)Indicate that the next ASN.1 element has the given implicit tag.BigIntegerdecodeInteger()Decode the next ASN.1 element as an integer.voiddecodeNull()Decode the next ASN.1 element as a null element.StringdecodeObjectIdentifier()Decode the next ASN.1 element as an object identifier.StringdecodeObjectIdentifierAsKeyAlgorithm()Decodes an OID and resolve its corresponding key algorithm.byte[]decodeOctetString()Decode the next ASN.1 element as an octet string.StringdecodeOctetStringAsString()Decode the next ASN.1 element as an octet string.StringdecodeOctetStringAsString(String charSet)Decode the next ASN.1 element as an octet string.StringdecodePrintableString()Decode the next ASN.1 element as a PrintableString.byte[]decodePrintableStringAsBytes()Decode the next ASN.1 element as a PrintableString.StringdecodeUniversalString()Decode the next ASN.1 element as an UniversalString.byte[]decodeUniversalStringAsBytes()Decode the next ASN.1 element as an UniversalString.StringdecodeUtf8String()Decode the next ASN.1 element as an UTF8String.byte[]decodeUtf8StringAsBytes()Decode the next ASN.1 element as an UTF8String.byte[]drain()Drain all of the remaining bytes from the input stream.byte[]drainElement()Drain all of the bytes from the next ASN.1 element.byte[]drainElementValue()Drain the value bytes from the next ASN.1 element.voidendExplicit()Advance to the end of an explicitly tagged element.voidendSequence()Advance to the end of a sequence.voidendSet()Advance to the end of a set.voidendSetOf()Advance to the end of a "set of" element.booleanhasNextElement()Determine if there is at least one more ASN.1 element that can be read.booleanisNextType(int clazz, int number, boolean isConstructed)Determine if the type of the next ASN.1 element matches the given type without actually decoding the next element.intpeekType()Retrieve the type of the next ASN.1 element without actually decoding the next element.voidskipElement()Skip over the next ASN.1 element.voidstartExplicit(int number)Start decoding an ASN.1 explicitly tagged element.voidstartExplicit(int clazz, int number)Start decoding an ASN.1 explicitly tagged element.voidstartSequence()Start decoding an ASN.1 sequence.voidstartSet()Start decoding an ASN.1 set.voidstartSetOf()Start decoding an ASN.1 "set of" element.
-
-
-
Constructor Detail
-
DERDecoder
public DERDecoder(byte[] buf)
Create a DER decoder that will decode values from the given byte array.- Parameters:
buf- the byte array to decode
-
DERDecoder
public DERDecoder(byte[] buf, int offset, int length)Create a DER decoder that will decode values from the given byte array.- Parameters:
buf- the byte array to decodeoffset- the offset in the byte array of the first byte to readlength- the maximum number of bytes to read from the byte array
-
-
Method Detail
-
startSequence
public void startSequence() throws ASN1ExceptionDescription copied from interface:ASN1DecoderStart decoding an ASN.1 sequence. All subsequent decode operations will decode elements from this sequence untilASN1Decoder.endSequence()is called.- Specified by:
startSequencein interfaceASN1Decoder- Throws:
ASN1Exception- if the next element is not a sequence
-
endSequence
public void endSequence() throws ASN1ExceptionDescription copied from interface:ASN1DecoderAdvance to the end of a sequence. If there are any elements in the sequence that have not yet been decoded, they will be discarded.- Specified by:
endSequencein interfaceASN1Decoder- Throws:
ASN1Exception- if an error occurs while advancing to the end of the sequence
-
startSet
public void startSet() throws ASN1ExceptionDescription copied from interface:ASN1DecoderStart decoding an ASN.1 set. All subsequent decode operations will decode elements from this set untilASN1Decoder.endSet()is called.- Specified by:
startSetin interfaceASN1Decoder- Throws:
ASN1Exception- if the next element is not a set
-
endSet
public void endSet() throws ASN1ExceptionDescription copied from interface:ASN1DecoderAdvance to the end of a set. If there are any elements in the set that have not yet been decoded, they will be discarded.- Specified by:
endSetin interfaceASN1Decoder- Throws:
ASN1Exception- if an error occurs while advancing to the end of the set
-
startSetOf
public void startSetOf() throws ASN1ExceptionDescription copied from interface:ASN1DecoderStart decoding an ASN.1 "set of" element. All subsequent decode operations will decode elements from this set untilASN1Decoder.endSetOf()is called.- Specified by:
startSetOfin interfaceASN1Decoder- Throws:
ASN1Exception- if the next element is not a set
-
endSetOf
public void endSetOf() throws ASN1ExceptionDescription copied from interface:ASN1DecoderAdvance to the end of a "set of" element. If there are any elements in the set that have not yet been decoded, they will be discarded.- Specified by:
endSetOfin interfaceASN1Decoder- Throws:
ASN1Exception- if an error occurs while advancing to the end of the set
-
startExplicit
public void startExplicit(int number) throws ASN1ExceptionDescription copied from interface:ASN1DecoderStart decoding an ASN.1 explicitly tagged element. All subsequent decode operations will decode elements from this explicitly tagged element untilASN1Decoder.endExplicit()is called.- Specified by:
startExplicitin interfaceASN1Decoder- Parameters:
number- the tag number for the explicit, context-specific tag- Throws:
ASN1Exception- if the next element's type does not match the given type
-
startExplicit
public void startExplicit(int clazz, int number) throws ASN1ExceptionDescription copied from interface:ASN1DecoderStart decoding an ASN.1 explicitly tagged element. All subsequent decode operations will decode elements from this explicitly tagged element untilASN1Decoder.endExplicit()is called.- Specified by:
startExplicitin interfaceASN1Decoder- Parameters:
clazz- the class for the explicit tagnumber- the tag number for the explicit tag- Throws:
ASN1Exception- if the next element's type does not match the given type
-
endExplicit
public void endExplicit() throws ASN1ExceptionDescription copied from interface:ASN1DecoderAdvance to the end of an explicitly tagged element. If there are any elements within the explicitly tagged element that have not yet been decoded, they will be discarded.- Specified by:
endExplicitin interfaceASN1Decoder- Throws:
ASN1Exception- if an error occurs while advancing to the end of the explicitly tagged element
-
decodeOctetString
public byte[] decodeOctetString() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDecode the next ASN.1 element as an octet string.- Specified by:
decodeOctetStringin interfaceASN1Decoder- Returns:
- the decoded octet string, as a byte array
- Throws:
ASN1Exception- if the next element is not an octet string
-
decodeOctetStringAsString
public String decodeOctetStringAsString() throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as an octet string.- Specified by:
decodeOctetStringAsStringin interfaceASN1Decoder- Returns:
- the decoded octet string, as a UTF-8 string
- Throws:
ASN1Exception- if the next element is not an octet string
-
decodeOctetStringAsString
public String decodeOctetStringAsString(String charSet) throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as an octet string.- Specified by:
decodeOctetStringAsStringin interfaceASN1Decoder- Parameters:
charSet- the character set to use when decoding- Returns:
- the decoded octet string
- Throws:
ASN1Exception- if the next element is not an octet string
-
decodeIA5String
public String decodeIA5String() throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as an IA5 string.- Specified by:
decodeIA5Stringin interfaceASN1Decoder- Returns:
- the decoded IA5 string
- Throws:
ASN1Exception- if the next element is not an IA5 string
-
decodeIA5StringAsBytes
public byte[] decodeIA5StringAsBytes() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDecode the next ASN.1 element as an IA5 string.- Specified by:
decodeIA5StringAsBytesin interfaceASN1Decoder- Returns:
- the decoded IA5 string, as a byte array
- Throws:
ASN1Exception- if the next element is not an IA5 string
-
decodeBitString
public byte[] decodeBitString() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDecode the next ASN.1 element as a bit string.- Specified by:
decodeBitStringin interfaceASN1Decoder- Returns:
- the decoded bit string as a byte array, with any unused bits removed
- Throws:
ASN1Exception- if the next element is not a bit string
-
decodeBitStringAsInteger
public BigInteger decodeBitStringAsInteger()
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as a bit string where the value is a ASN.1 INTEGER.- Specified by:
decodeBitStringAsIntegerin interfaceASN1Decoder- Returns:
- a
BigIntegerdecoded from the bit string
-
decodeBitStringAsString
public String decodeBitStringAsString() throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as a bit string.- Specified by:
decodeBitStringAsStringin interfaceASN1Decoder- Returns:
- the decoded bit string as a binary string, with any unused bits removed
- Throws:
ASN1Exception- if the next element is not a bit string
-
decodePrintableString
public String decodePrintableString() throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as a PrintableString.- Specified by:
decodePrintableStringin interfaceASN1Decoder- Returns:
- the decoded PrintableString as a string
- Throws:
ASN1Exception- if the next element is not a PrintableString
-
decodePrintableStringAsBytes
public byte[] decodePrintableStringAsBytes() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDecode the next ASN.1 element as a PrintableString.- Specified by:
decodePrintableStringAsBytesin interfaceASN1Decoder- Returns:
- the decoded PrintableString as a byte array
- Throws:
ASN1Exception- if the next element is not a PrintableString
-
decodeUniversalString
public String decodeUniversalString() throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as an UniversalString.- Specified by:
decodeUniversalStringin interfaceASN1Decoder- Returns:
- the decoded UniversalString as a string
- Throws:
ASN1Exception- if the next element is not a UniversalString
-
decodeUniversalStringAsBytes
public byte[] decodeUniversalStringAsBytes() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDecode the next ASN.1 element as an UniversalString.- Specified by:
decodeUniversalStringAsBytesin interfaceASN1Decoder- Returns:
- the decoded UniversalString as a byte array
- Throws:
ASN1Exception- if the next element is not a UniversalString
-
decodeUtf8String
public String decodeUtf8String() throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as an UTF8String.- Specified by:
decodeUtf8Stringin interfaceASN1Decoder- Returns:
- the decoded UTF8String as a string
- Throws:
ASN1Exception- if the next element is not a UTF8String
-
decodeUtf8StringAsBytes
public byte[] decodeUtf8StringAsBytes() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDecode the next ASN.1 element as an UTF8String.- Specified by:
decodeUtf8StringAsBytesin interfaceASN1Decoder- Returns:
- the decoded UTF8String as a byte array
- Throws:
ASN1Exception- if the next element is not a UTF8String
-
decodeBMPString
public String decodeBMPString() throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as an BMPString.- Specified by:
decodeBMPStringin interfaceASN1Decoder- Returns:
- the decoded BMPString as a string
- Throws:
ASN1Exception- if the next element is not a BMPString
-
decodeBMPStringAsBytes
public byte[] decodeBMPStringAsBytes() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDecode the next ASN.1 element as an BMPString.- Specified by:
decodeBMPStringAsBytesin interfaceASN1Decoder- Returns:
- the decoded BMPString as a byte array
- Throws:
ASN1Exception- if the next element is not a BMPString
-
decodeObjectIdentifier
public String decodeObjectIdentifier() throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as an object identifier.- Specified by:
decodeObjectIdentifierin interfaceASN1Decoder- Returns:
- the object identifier as a string
- Throws:
ASN1Exception- if the next element is not a bit string
-
decodeInteger
public BigInteger decodeInteger() throws ASN1Exception
Description copied from interface:ASN1DecoderDecode the next ASN.1 element as an integer.- Specified by:
decodeIntegerin interfaceASN1Decoder- Returns:
- an integer decoded from the next element
- Throws:
ASN1Exception- if the next element is not an integer
-
decodeNull
public void decodeNull() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDecode the next ASN.1 element as a null element.- Specified by:
decodeNullin interfaceASN1Decoder- Throws:
ASN1Exception- if the next element is not null
-
decodeImplicit
public void decodeImplicit(int number)
Description copied from interface:ASN1DecoderIndicate that the next ASN.1 element has the given implicit, context-specific tag.- Specified by:
decodeImplicitin interfaceASN1Decoder- Parameters:
number- the tag number for the implicit tag
-
decodeImplicit
public void decodeImplicit(int clazz, int number)Description copied from interface:ASN1DecoderIndicate that the next ASN.1 element has the given implicit tag.- Specified by:
decodeImplicitin interfaceASN1Decoder- Parameters:
clazz- the class for the implicit tagnumber- the tag number for the implicit tag
-
decodeBoolean
public boolean decodeBoolean() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDecode the next ASN.1 element as a boolean value.- Specified by:
decodeBooleanin interfaceASN1Decoder- Returns:
- the decoded boolean value
- Throws:
ASN1Exception- if the next element is not a boolean value
-
isNextType
public boolean isNextType(int clazz, int number, boolean isConstructed)Description copied from interface:ASN1DecoderDetermine if the type of the next ASN.1 element matches the given type without actually decoding the next element. This method can be used to determine if an optional ASN.1 value has been included in the encoding or not.- Specified by:
isNextTypein interfaceASN1Decoder- Parameters:
clazz- the tag class to match againstnumber- the tag number to match againstisConstructed- whether or not the next element should be constructed- Returns:
trueif the type of the next ASN.1 element matches the given type andfalseotherwise
-
peekType
public int peekType() throws ASN1ExceptionDescription copied from interface:ASN1DecoderRetrieve the type of the next ASN.1 element without actually decoding the next element.- Specified by:
peekTypein interfaceASN1Decoder- Returns:
- the type of the next ASN.1 element
- Throws:
ASN1Exception- if an error occurs while determining the type of the next element
-
skipElement
public void skipElement() throws ASN1ExceptionDescription copied from interface:ASN1DecoderSkip over the next ASN.1 element.- Specified by:
skipElementin interfaceASN1Decoder- Throws:
ASN1Exception- if the next element cannot be skipped
-
hasNextElement
public boolean hasNextElement()
Description copied from interface:ASN1DecoderDetermine if there is at least one more ASN.1 element that can be read. If called while decoding a constructed element (i.e., while decoding a sequence, set, or explicitly tagged element), this method will return whether the constructed element has at least one more ASN.1 element that can be read. Otherwise, this method will return whether at least one more ASN.1 element can be read from the input stream.- Specified by:
hasNextElementin interfaceASN1Decoder- Returns:
trueif there is at least one more ASN.1 element that can be read andfalseotherwise
-
drainElementValue
public byte[] drainElementValue() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDrain the value bytes from the next ASN.1 element.- Specified by:
drainElementValuein interfaceASN1Decoder- Returns:
- the value bytes from the next ASN.1 element, as a byte array
- Throws:
ASN1Exception- if the value bytes from the next ASN.1 element cannot be obtained
-
drainElement
public byte[] drainElement() throws ASN1ExceptionDescription copied from interface:ASN1DecoderDrain all of the bytes from the next ASN.1 element.- Specified by:
drainElementin interfaceASN1Decoder- Returns:
- all of the bytes from the next ASN.1 element
- Throws:
ASN1Exception- if the bytes from the next ASN.1 element cannot be obtained
-
drain
public byte[] drain()
Description copied from interface:ASN1DecoderDrain all of the remaining bytes from the input stream.- Specified by:
drainin interfaceASN1Decoder- Returns:
- all of the remaining bytes from the input stream
-
decodeObjectIdentifierAsKeyAlgorithm
public String decodeObjectIdentifierAsKeyAlgorithm()
Decodes an OID and resolve its corresponding key algorithm.- Returns:
- the key algorithm associated with the OID or null if no algorithm could be resolved
-
-