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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
DERDecoder.DecoderState
A class used to maintain state information during DER decoding.
-
Field Summary
Fields Modifier and Type Field Description private org.wildfly.common.iteration.ByteIterator
bi
private static int
BOOLEAN_FALSE
private int
implicitTag
private ArrayDeque<DERDecoder.DecoderState>
states
private static Charset
UTF_32BE
-
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.DERDecoder(org.wildfly.common.iteration.ByteIterator bi)
Create a DER decoder that will decode values from the givenByteIterator
.
-
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.BigInteger
decodeBitStringAsInteger()
Decode the next ASN.1 element as a bit string where the value is a ASN.1 INTEGER.String
decodeBitStringAsString()
Decode the next ASN.1 element as a bit string.String
decodeBMPString()
Decode the next ASN.1 element as an BMPString.byte[]
decodeBMPStringAsBytes()
Decode the next ASN.1 element as an BMPString.boolean
decodeBoolean()
Decode the next ASN.1 element as a boolean value.String
decodeIA5String()
Decode the next ASN.1 element as an IA5 string.byte[]
decodeIA5StringAsBytes()
Decode the next ASN.1 element as an IA5 string.void
decodeImplicit(int number)
Indicate that the next ASN.1 element has the given implicit, context-specific tag.void
decodeImplicit(int clazz, int number)
Indicate that the next ASN.1 element has the given implicit tag.BigInteger
decodeInteger()
Decode the next ASN.1 element as an integer.void
decodeNull()
Decode the next ASN.1 element as a null element.String
decodeObjectIdentifier()
Decode the next ASN.1 element as an object identifier.String
decodeObjectIdentifierAsKeyAlgorithm()
Decodes an OID and resolve its corresponding key algorithm.byte[]
decodeOctetString()
Decode the next ASN.1 element as an octet string.String
decodeOctetStringAsString()
Decode the next ASN.1 element as an octet string.String
decodeOctetStringAsString(String charSet)
Decode the next ASN.1 element as an octet string.String
decodePrintableString()
Decode the next ASN.1 element as a PrintableString.byte[]
decodePrintableStringAsBytes()
Decode the next ASN.1 element as a PrintableString.private byte[]
decodeUncheckedStringAsBytes()
String
decodeUniversalString()
Decode the next ASN.1 element as an UniversalString.byte[]
decodeUniversalStringAsBytes()
Decode the next ASN.1 element as an UniversalString.String
decodeUtf8String()
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.private void
endConstructedElement(long nextElementIndex)
void
endExplicit()
Advance to the end of an explicitly tagged element.void
endSequence()
Advance to the end of a sequence.void
endSet()
Advance to the end of a set.void
endSetOf()
Advance to the end of a "set of" element.private boolean
hasCompleteElement()
boolean
hasNextElement()
Determine if there is at least one more ASN.1 element that can be read.boolean
isNextType(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.int
peekType()
Retrieve the type of the next ASN.1 element without actually decoding the next element.private int
readLength()
private int
readTag()
private void
readTag(int expectedTag)
void
skipElement()
Skip over the next ASN.1 element.void
startExplicit(int number)
Start decoding an ASN.1 explicitly tagged element.void
startExplicit(int clazz, int number)
Start decoding an ASN.1 explicitly tagged element.void
startSequence()
Start decoding an ASN.1 sequence.void
startSet()
Start decoding an ASN.1 set.void
startSetOf()
Start decoding an ASN.1 "set of" element.
-
-
-
Field Detail
-
BOOLEAN_FALSE
private static final int BOOLEAN_FALSE
- See Also:
- Constant Field Values
-
UTF_32BE
private static final Charset UTF_32BE
-
bi
private final org.wildfly.common.iteration.ByteIterator bi
-
states
private final ArrayDeque<DERDecoder.DecoderState> states
-
implicitTag
private int implicitTag
-
-
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
-
DERDecoder
DERDecoder(org.wildfly.common.iteration.ByteIterator bi)
Create a DER decoder that will decode values from the givenByteIterator
.- Parameters:
bi
- theByteIterator
from which DER encoded values will be decoded
-
-
Method Detail
-
startSequence
public void startSequence() throws ASN1Exception
Description copied from interface:ASN1Decoder
Start decoding an ASN.1 sequence. All subsequent decode operations will decode elements from this sequence untilASN1Decoder.endSequence()
is called.- Specified by:
startSequence
in interfaceASN1Decoder
- Throws:
ASN1Exception
- if the next element is not a sequence
-
endSequence
public void endSequence() throws ASN1Exception
Description copied from interface:ASN1Decoder
Advance 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:
endSequence
in interfaceASN1Decoder
- Throws:
ASN1Exception
- if an error occurs while advancing to the end of the sequence
-
startSet
public void startSet() throws ASN1Exception
Description copied from interface:ASN1Decoder
Start decoding an ASN.1 set. All subsequent decode operations will decode elements from this set untilASN1Decoder.endSet()
is called.- Specified by:
startSet
in interfaceASN1Decoder
- Throws:
ASN1Exception
- if the next element is not a set
-
endSet
public void endSet() throws ASN1Exception
Description copied from interface:ASN1Decoder
Advance 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:
endSet
in interfaceASN1Decoder
- Throws:
ASN1Exception
- if an error occurs while advancing to the end of the set
-
startSetOf
public void startSetOf() throws ASN1Exception
Description copied from interface:ASN1Decoder
Start decoding an ASN.1 "set of" element. All subsequent decode operations will decode elements from this set untilASN1Decoder.endSetOf()
is called.- Specified by:
startSetOf
in interfaceASN1Decoder
- Throws:
ASN1Exception
- if the next element is not a set
-
endSetOf
public void endSetOf() throws ASN1Exception
Description copied from interface:ASN1Decoder
Advance 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:
endSetOf
in interfaceASN1Decoder
- Throws:
ASN1Exception
- if an error occurs while advancing to the end of the set
-
startExplicit
public void startExplicit(int number) throws ASN1Exception
Description copied from interface:ASN1Decoder
Start 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:
startExplicit
in 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 ASN1Exception
Description copied from interface:ASN1Decoder
Start 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:
startExplicit
in 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 ASN1Exception
Description copied from interface:ASN1Decoder
Advance 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:
endExplicit
in interfaceASN1Decoder
- Throws:
ASN1Exception
- if an error occurs while advancing to the end of the explicitly tagged element
-
endConstructedElement
private void endConstructedElement(long nextElementIndex) throws ASN1Exception
- Throws:
ASN1Exception
-
decodeOctetString
public byte[] decodeOctetString() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as an octet string.- Specified by:
decodeOctetString
in 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:ASN1Decoder
Decode the next ASN.1 element as an octet string.- Specified by:
decodeOctetStringAsString
in 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:ASN1Decoder
Decode the next ASN.1 element as an octet string.- Specified by:
decodeOctetStringAsString
in 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:ASN1Decoder
Decode the next ASN.1 element as an IA5 string.- Specified by:
decodeIA5String
in interfaceASN1Decoder
- Returns:
- the decoded IA5 string
- Throws:
ASN1Exception
- if the next element is not an IA5 string
-
decodeIA5StringAsBytes
public byte[] decodeIA5StringAsBytes() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as an IA5 string.- Specified by:
decodeIA5StringAsBytes
in 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 ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as a bit string.- Specified by:
decodeBitString
in 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:ASN1Decoder
Decode the next ASN.1 element as a bit string where the value is a ASN.1 INTEGER.- Specified by:
decodeBitStringAsInteger
in interfaceASN1Decoder
- Returns:
- a
BigInteger
decoded from the bit string
-
decodeBitStringAsString
public String decodeBitStringAsString() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as a bit string.- Specified by:
decodeBitStringAsString
in 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:ASN1Decoder
Decode the next ASN.1 element as a PrintableString.- Specified by:
decodePrintableString
in interfaceASN1Decoder
- Returns:
- the decoded PrintableString as a string
- Throws:
ASN1Exception
- if the next element is not a PrintableString
-
decodePrintableStringAsBytes
public byte[] decodePrintableStringAsBytes() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as a PrintableString.- Specified by:
decodePrintableStringAsBytes
in 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:ASN1Decoder
Decode the next ASN.1 element as an UniversalString.- Specified by:
decodeUniversalString
in interfaceASN1Decoder
- Returns:
- the decoded UniversalString as a string
- Throws:
ASN1Exception
- if the next element is not a UniversalString
-
decodeUniversalStringAsBytes
public byte[] decodeUniversalStringAsBytes() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as an UniversalString.- Specified by:
decodeUniversalStringAsBytes
in 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:ASN1Decoder
Decode the next ASN.1 element as an UTF8String.- Specified by:
decodeUtf8String
in interfaceASN1Decoder
- Returns:
- the decoded UTF8String as a string
- Throws:
ASN1Exception
- if the next element is not a UTF8String
-
decodeUtf8StringAsBytes
public byte[] decodeUtf8StringAsBytes() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as an UTF8String.- Specified by:
decodeUtf8StringAsBytes
in 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:ASN1Decoder
Decode the next ASN.1 element as an BMPString.- Specified by:
decodeBMPString
in interfaceASN1Decoder
- Returns:
- the decoded BMPString as a string
- Throws:
ASN1Exception
- if the next element is not a BMPString
-
decodeBMPStringAsBytes
public byte[] decodeBMPStringAsBytes() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as an BMPString.- Specified by:
decodeBMPStringAsBytes
in interfaceASN1Decoder
- Returns:
- the decoded BMPString as a byte array
- Throws:
ASN1Exception
- if the next element is not a BMPString
-
decodeUncheckedStringAsBytes
private byte[] decodeUncheckedStringAsBytes() throws ASN1Exception
- Throws:
ASN1Exception
-
decodeObjectIdentifier
public String decodeObjectIdentifier() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as an object identifier.- Specified by:
decodeObjectIdentifier
in 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:ASN1Decoder
Decode the next ASN.1 element as an integer.- Specified by:
decodeInteger
in interfaceASN1Decoder
- Returns:
- an integer decoded from the next element
- Throws:
ASN1Exception
- if the next element is not an integer
-
decodeNull
public void decodeNull() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as a null element.- Specified by:
decodeNull
in interfaceASN1Decoder
- Throws:
ASN1Exception
- if the next element is not null
-
decodeImplicit
public void decodeImplicit(int number)
Description copied from interface:ASN1Decoder
Indicate that the next ASN.1 element has the given implicit, context-specific tag.- Specified by:
decodeImplicit
in interfaceASN1Decoder
- Parameters:
number
- the tag number for the implicit tag
-
decodeImplicit
public void decodeImplicit(int clazz, int number)
Description copied from interface:ASN1Decoder
Indicate that the next ASN.1 element has the given implicit tag.- Specified by:
decodeImplicit
in interfaceASN1Decoder
- Parameters:
clazz
- the class for the implicit tagnumber
- the tag number for the implicit tag
-
decodeBoolean
public boolean decodeBoolean() throws ASN1Exception
Description copied from interface:ASN1Decoder
Decode the next ASN.1 element as a boolean value.- Specified by:
decodeBoolean
in 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:ASN1Decoder
Determine 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:
isNextType
in 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:
true
if the type of the next ASN.1 element matches the given type andfalse
otherwise
-
peekType
public int peekType() throws ASN1Exception
Description copied from interface:ASN1Decoder
Retrieve the type of the next ASN.1 element without actually decoding the next element.- Specified by:
peekType
in 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 ASN1Exception
Description copied from interface:ASN1Decoder
Skip over the next ASN.1 element.- Specified by:
skipElement
in interfaceASN1Decoder
- Throws:
ASN1Exception
- if the next element cannot be skipped
-
hasNextElement
public boolean hasNextElement()
Description copied from interface:ASN1Decoder
Determine 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:
hasNextElement
in interfaceASN1Decoder
- Returns:
true
if there is at least one more ASN.1 element that can be read andfalse
otherwise
-
hasCompleteElement
private boolean hasCompleteElement()
-
drainElementValue
public byte[] drainElementValue() throws ASN1Exception
Description copied from interface:ASN1Decoder
Drain the value bytes from the next ASN.1 element.- Specified by:
drainElementValue
in 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 ASN1Exception
Description copied from interface:ASN1Decoder
Drain all of the bytes from the next ASN.1 element.- Specified by:
drainElement
in 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:ASN1Decoder
Drain all of the remaining bytes from the input stream.- Specified by:
drain
in interfaceASN1Decoder
- Returns:
- all of the remaining bytes from the input stream
-
readTag
private int readTag() throws ASN1Exception
- Throws:
ASN1Exception
-
readTag
private void readTag(int expectedTag) throws ASN1Exception
- Throws:
ASN1Exception
-
readLength
private int readLength() throws ASN1Exception
- Throws:
ASN1Exception
-
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
-
-