Package org.wildfly.security.asn1
Class DEREncoder
- java.lang.Object
-
- org.wildfly.security.asn1.DEREncoder
-
- All Implemented Interfaces:
Flushable
,ASN1Encoder
public class DEREncoder extends Object implements ASN1Encoder
A class used to encode ASN.1 values using the Distinguished Encoding Rules (DER), as specified in ITU-T X.690.- Author:
- Farah Juma
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
DEREncoder.EncoderState
A class used to maintain state information during DER encoding.private static class
DEREncoder.LexicographicComparator
A class that compares DER encodings using lexicographic order.private static class
DEREncoder.TagComparator
A class that compares DER encodings based on their tags.
-
Field Summary
Fields Modifier and Type Field Description private static int[]
BITS
private static byte[]
BOOLEAN_FALSE_AS_BYTES
private static byte[]
BOOLEAN_TRUE_AS_BYTES
private ArrayList<org.wildfly.common.bytes.ByteStringBuilder>
buffers
private org.wildfly.common.bytes.ByteStringBuilder
currentBuffer
private int
currentBufferPos
private static BigInteger[]
digits
private static DateTimeFormatter
GENERALIZED_TIME_FORMAT
private int
implicitTag
private static long
LARGEST_UNSHIFTED_LONG
private static DEREncoder.LexicographicComparator
LEXICOGRAPHIC_COMPARATOR
private static byte[]
NULL_CONTENTS
private ArrayDeque<DEREncoder.EncoderState>
states
private static DEREncoder.TagComparator
TAG_COMPARATOR
private org.wildfly.common.bytes.ByteStringBuilder
target
private static Charset
UTF_32BE
-
Constructor Summary
Constructors Constructor Description DEREncoder()
Create a DER encoder.DEREncoder(org.wildfly.common.bytes.ByteStringBuilder target)
Create a DER encoder that writes its output to the givenByteStringBuilder
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
encodeBitString(byte[] str)
Encode an ASN.1 bit string value.void
encodeBitString(byte[] str, int numUnusedBits)
Encode an ASN.1 bit string value.void
encodeBitString(String binaryStr)
Encode an ASN.1 bit string value.void
encodeBitString(BigInteger integer)
Encode an ASN.1 bit string where the value represents the binary form of the giveninteger
.void
encodeBitString(BitSet bitSet)
Encode an ASN.1 bit string value.void
encodeBitString(EnumSet<?> enumSet)
Encode an enum set as an ASN.1 bit string, with each ordinal value of the set representing a single bit.void
encodeBMPString(String str)
Encode an ASN.1 BMPString string value.void
encodeBoolean(boolean value)
Encode an ASN.1boolean
value.void
encodeGeneralizedTime(ZonedDateTime time)
Encode an ASN.1 GeneralizedTime type.void
encodeIA5String(byte[] str)
Encode an ASN.1 IA5 string value.void
encodeIA5String(String str)
Encode an ASN.1 IA5 string value.(package private) void
encodeIA5String(org.wildfly.common.bytes.ByteStringBuilder str)
void
encodeImplicit(int number)
Indicate that the next encode operation should encode an ASN.1 value using the given implicit, context-specific tag.void
encodeImplicit(int clazz, int number)
Indicate that the next encode operation should encode an ASN.1 value using the given implicit tag.void
encodeInteger(BigInteger integer)
Encode an ASN.1integer
value.void
encodeNull()
Encode an ASN.1 null value.void
encodeObjectIdentifier(String objectIdentifier)
Encode an ASN.1 object identifier value.void
encodeOctetString(byte[] str)
Encode an ASN.1 octet string value.void
encodeOctetString(String str)
Encode an ASN.1 octet string value.(package private) void
encodeOctetString(org.wildfly.common.bytes.ByteStringBuilder str)
private void
encodeOIDComponent(long value, org.wildfly.common.bytes.ByteStringBuilder contents)
private void
encodeOIDComponent(long value, org.wildfly.common.bytes.ByteStringBuilder contents, int numComponents, int first)
private void
encodeOIDComponent(BigInteger value, org.wildfly.common.bytes.ByteStringBuilder contents)
private void
encodeOIDComponent(BigInteger value, org.wildfly.common.bytes.ByteStringBuilder contents, int numComponents, int first)
void
encodePrintableString(byte[] str)
Encode an ASN.1 printable string value.void
encodePrintableString(String str)
Encode an ASN.1 printable string value.void
encodeUniversalString(String str)
Encode an ASN.1 UniversalString string value.void
encodeUTF8String(String str)
Encode an ASN.1 UTF8String string value.private void
endConstructedElement()
void
endExplicit()
Finish encoding an ASN.1 explicitly tagged element.void
endSequence()
Finish encoding an ASN.1 sequence.void
endSet()
Finish encoding an ASN.1 set.private void
endSet(Comparator<DEREncoder.EncoderState> comparator)
void
endSetOf()
Finish encoding an ASN.1 "set of" element.void
flush()
Flush the encoder, writing any saved ASN.1 encoded values to the target destination.byte[]
getEncoded()
Get the ASN.1 encoded bytes.private void
startConstructedElement(int tag)
void
startExplicit(int number)
Start encoding an ASN.1 explicitly tagged element.void
startExplicit(int clazz, int number)
Start encoding an ASN.1 explicitly tagged element.void
startSequence()
Start encoding an ASN.1 sequence.void
startSet()
Start encoding an ASN.1 set.void
startSetOf()
Start encoding an ASN.1 "set of" element.private void
updateCurrentBuffer()
private int
validateFirstOIDComponent(long value)
private int
validateFirstOIDComponent(BigInteger value)
private void
validateSecondOIDComponent(long second, int first)
private void
validateSecondOIDComponent(BigInteger second, int first)
private void
writeElement(int tag, byte[] contents)
private void
writeElement(int tag, org.wildfly.common.bytes.ByteStringBuilder contents)
void
writeEncoded(byte[] encoded)
Write an already encoded ASN.1 value to the target destination.private int
writeLength(int length, org.wildfly.common.bytes.ByteStringBuilder dest)
private void
writeTag(int tag, org.wildfly.common.bytes.ByteStringBuilder dest)
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.wildfly.security.asn1.ASN1Encoder
encodeInteger
-
-
-
-
Field Detail
-
BITS
private static final int[] BITS
-
LARGEST_UNSHIFTED_LONG
private static final long LARGEST_UNSHIFTED_LONG
- See Also:
- Constant Field Values
-
NULL_CONTENTS
private static final byte[] NULL_CONTENTS
-
TAG_COMPARATOR
private static final DEREncoder.TagComparator TAG_COMPARATOR
-
LEXICOGRAPHIC_COMPARATOR
private static final DEREncoder.LexicographicComparator LEXICOGRAPHIC_COMPARATOR
-
BOOLEAN_TRUE_AS_BYTES
private static final byte[] BOOLEAN_TRUE_AS_BYTES
-
BOOLEAN_FALSE_AS_BYTES
private static final byte[] BOOLEAN_FALSE_AS_BYTES
-
states
private final ArrayDeque<DEREncoder.EncoderState> states
-
buffers
private final ArrayList<org.wildfly.common.bytes.ByteStringBuilder> buffers
-
currentBuffer
private org.wildfly.common.bytes.ByteStringBuilder currentBuffer
-
currentBufferPos
private int currentBufferPos
-
target
private final org.wildfly.common.bytes.ByteStringBuilder target
-
implicitTag
private int implicitTag
-
UTF_32BE
private static final Charset UTF_32BE
-
GENERALIZED_TIME_FORMAT
private static final DateTimeFormatter GENERALIZED_TIME_FORMAT
-
digits
private static final BigInteger[] digits
-
-
Constructor Detail
-
DEREncoder
public DEREncoder()
Create a DER encoder.
-
DEREncoder
DEREncoder(org.wildfly.common.bytes.ByteStringBuilder target)
Create a DER encoder that writes its output to the givenByteStringBuilder
.- Parameters:
target
- theByteStringBuilder
to which the DER encoded values are written
-
-
Method Detail
-
startSequence
public void startSequence()
Description copied from interface:ASN1Encoder
Start encoding an ASN.1 sequence. All subsequent encode operations will be part of this sequence untilASN1Encoder.endSequence()
is called.- Specified by:
startSequence
in interfaceASN1Encoder
-
startSet
public void startSet()
Description copied from interface:ASN1Encoder
Start encoding an ASN.1 set. All subsequent encode operations will be part of this set untilASN1Encoder.endSet()
is called.- Specified by:
startSet
in interfaceASN1Encoder
-
startSetOf
public void startSetOf()
Description copied from interface:ASN1Encoder
Start encoding an ASN.1 "set of" element. All subsequent encode operations will be part of this set untilASN1Encoder.endSetOf()
is called.- Specified by:
startSetOf
in interfaceASN1Encoder
-
startExplicit
public void startExplicit(int number)
Description copied from interface:ASN1Encoder
Start encoding an ASN.1 explicitly tagged element. All subsequent encode operations will be part of this explicitly tagged element untilASN1Encoder.endExplicit()
is called.- Specified by:
startExplicit
in interfaceASN1Encoder
- Parameters:
number
- the tag number for the explicit, context-specific tag
-
startExplicit
public void startExplicit(int clazz, int number)
Description copied from interface:ASN1Encoder
Start encoding an ASN.1 explicitly tagged element. All subsequent encode operations will be part of this explicitly tagged element untilASN1Encoder.endExplicit()
is called.- Specified by:
startExplicit
in interfaceASN1Encoder
- Parameters:
clazz
- the class for the explicit tagnumber
- the tag number for the explicit tag
-
startConstructedElement
private void startConstructedElement(int tag)
-
endSequence
public void endSequence() throws IllegalStateException
Description copied from interface:ASN1Encoder
Finish encoding an ASN.1 sequence.- Specified by:
endSequence
in interfaceASN1Encoder
- Throws:
IllegalStateException
- if there is no sequence to end
-
endExplicit
public void endExplicit() throws IllegalStateException
Description copied from interface:ASN1Encoder
Finish encoding an ASN.1 explicitly tagged element.- Specified by:
endExplicit
in interfaceASN1Encoder
- Throws:
IllegalStateException
- if there is no explicitly tagged element to end
-
endConstructedElement
private void endConstructedElement()
-
endSet
public void endSet() throws IllegalStateException
Description copied from interface:ASN1Encoder
Finish encoding an ASN.1 set.- Specified by:
endSet
in interfaceASN1Encoder
- Throws:
IllegalStateException
- if there is no set to end
-
endSetOf
public void endSetOf() throws IllegalStateException
Description copied from interface:ASN1Encoder
Finish encoding an ASN.1 "set of" element.- Specified by:
endSetOf
in interfaceASN1Encoder
- Throws:
IllegalStateException
- if there is no set to end
-
endSet
private void endSet(Comparator<DEREncoder.EncoderState> comparator)
-
encodeOctetString
public void encodeOctetString(String str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 octet string value.- Specified by:
encodeOctetString
in interfaceASN1Encoder
- Parameters:
str
- the octet string to encode
-
encodeOctetString
public void encodeOctetString(byte[] str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 octet string value.- Specified by:
encodeOctetString
in interfaceASN1Encoder
- Parameters:
str
- the byte array containing the octet string to encode
-
encodeOctetString
void encodeOctetString(org.wildfly.common.bytes.ByteStringBuilder str)
-
encodeIA5String
public void encodeIA5String(String str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 IA5 string value.- Specified by:
encodeIA5String
in interfaceASN1Encoder
- Parameters:
str
- the IA5 string to encode
-
encodeIA5String
public void encodeIA5String(byte[] str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 IA5 string value.- Specified by:
encodeIA5String
in interfaceASN1Encoder
- Parameters:
str
- the byte array containing IA5 string to encode
-
encodeIA5String
void encodeIA5String(org.wildfly.common.bytes.ByteStringBuilder str)
-
encodePrintableString
public void encodePrintableString(byte[] str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 printable string value.- Specified by:
encodePrintableString
in interfaceASN1Encoder
- Parameters:
str
- the byte array containing the printable string to encode
-
encodePrintableString
public void encodePrintableString(String str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 printable string value.- Specified by:
encodePrintableString
in interfaceASN1Encoder
- Parameters:
str
- the printable string to encode
-
encodeUTF8String
public void encodeUTF8String(String str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 UTF8String string value.- Specified by:
encodeUTF8String
in interfaceASN1Encoder
- Parameters:
str
- the string to encode
-
encodeBMPString
public void encodeBMPString(String str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 BMPString string value.- Specified by:
encodeBMPString
in interfaceASN1Encoder
- Parameters:
str
- the string to encode
-
encodeUniversalString
public void encodeUniversalString(String str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 UniversalString string value.- Specified by:
encodeUniversalString
in interfaceASN1Encoder
- Parameters:
str
- the string to encode
-
encodeBitString
public void encodeBitString(byte[] str)
Description copied from interface:ASN1Encoder
Encode an ASN.1 bit string value.- Specified by:
encodeBitString
in interfaceASN1Encoder
- Parameters:
str
- the byte array containing the bit string to encode (all bits in the bit string will be used)
-
encodeBitString
public void encodeBitString(byte[] str, int numUnusedBits)
Description copied from interface:ASN1Encoder
Encode an ASN.1 bit string value.- Specified by:
encodeBitString
in interfaceASN1Encoder
- Parameters:
str
- the byte array containing the bit string to encodenumUnusedBits
- the number of unused bits in the byte array
-
encodeBitString
public void encodeBitString(EnumSet<?> enumSet)
Description copied from interface:ASN1Encoder
Encode an enum set as an ASN.1 bit string, with each ordinal value of the set representing a single bit.- Specified by:
encodeBitString
in interfaceASN1Encoder
- Parameters:
enumSet
- the enum set (must not benull
)
-
encodeBitString
public void encodeBitString(BitSet bitSet)
Description copied from interface:ASN1Encoder
Encode an ASN.1 bit string value.- Specified by:
encodeBitString
in interfaceASN1Encoder
- Parameters:
bitSet
- the bit set (must not benull
)
-
encodeBitString
public void encodeBitString(String binaryStr)
Description copied from interface:ASN1Encoder
Encode an ASN.1 bit string value.- Specified by:
encodeBitString
in interfaceASN1Encoder
- Parameters:
binaryStr
- the bit string to encode, as a binary string
-
encodeBitString
public void encodeBitString(BigInteger integer)
Description copied from interface:ASN1Encoder
Encode an ASN.1 bit string where the value represents the binary form of the giveninteger
.- Specified by:
encodeBitString
in interfaceASN1Encoder
- Parameters:
integer
- the integer to encode as a binary string
-
encodeGeneralizedTime
public void encodeGeneralizedTime(ZonedDateTime time)
Description copied from interface:ASN1Encoder
Encode an ASN.1 GeneralizedTime type.- Specified by:
encodeGeneralizedTime
in interfaceASN1Encoder
- Parameters:
time
- the time to encode (must not benull
)
-
encodeObjectIdentifier
public void encodeObjectIdentifier(String objectIdentifier) throws ASN1Exception
Description copied from interface:ASN1Encoder
Encode an ASN.1 object identifier value.- Specified by:
encodeObjectIdentifier
in interfaceASN1Encoder
- Parameters:
objectIdentifier
- the object identifier to encode- Throws:
ASN1Exception
- if the given object identifier is invalid
-
encodeNull
public void encodeNull()
Description copied from interface:ASN1Encoder
Encode an ASN.1 null value.- Specified by:
encodeNull
in interfaceASN1Encoder
-
encodeImplicit
public void encodeImplicit(int number)
Description copied from interface:ASN1Encoder
Indicate that the next encode operation should encode an ASN.1 value using the given implicit, context-specific tag.- Specified by:
encodeImplicit
in interfaceASN1Encoder
- Parameters:
number
- the tag number for the implicit, context-specific tag
-
encodeImplicit
public void encodeImplicit(int clazz, int number)
Description copied from interface:ASN1Encoder
Indicate that the next encode operation should encode an ASN.1 value using the given implicit tag.- Specified by:
encodeImplicit
in interfaceASN1Encoder
- Parameters:
clazz
- the class for the implicit tagnumber
- the tag number for the implicit tag
-
encodeBoolean
public void encodeBoolean(boolean value)
Description copied from interface:ASN1Encoder
Encode an ASN.1boolean
value.- Specified by:
encodeBoolean
in interfaceASN1Encoder
- Parameters:
value
- the boolean to encode
-
encodeInteger
public void encodeInteger(BigInteger integer)
Description copied from interface:ASN1Encoder
Encode an ASN.1integer
value.- Specified by:
encodeInteger
in interfaceASN1Encoder
- Parameters:
integer
- the integer to encode
-
writeEncoded
public void writeEncoded(byte[] encoded)
Description copied from interface:ASN1Encoder
Write an already encoded ASN.1 value to the target destination.- Specified by:
writeEncoded
in interfaceASN1Encoder
- Parameters:
encoded
- the encoded ASN.1 value to write
-
flush
public void flush()
Description copied from interface:ASN1Encoder
Flush the encoder, writing any saved ASN.1 encoded values to the target destination. Any unfinished sequences or sets will be ended.- Specified by:
flush
in interfaceASN1Encoder
- Specified by:
flush
in interfaceFlushable
-
getEncoded
public byte[] getEncoded()
Description copied from interface:ASN1Encoder
Get the ASN.1 encoded bytes.- Specified by:
getEncoded
in interfaceASN1Encoder
- Returns:
- the ASN.1 encoded bytes
-
validateFirstOIDComponent
private int validateFirstOIDComponent(long value) throws ASN1Exception
- Throws:
ASN1Exception
-
validateFirstOIDComponent
private int validateFirstOIDComponent(BigInteger value) throws ASN1Exception
- Throws:
ASN1Exception
-
validateSecondOIDComponent
private void validateSecondOIDComponent(long second, int first) throws ASN1Exception
- Throws:
ASN1Exception
-
validateSecondOIDComponent
private void validateSecondOIDComponent(BigInteger second, int first) throws ASN1Exception
- Throws:
ASN1Exception
-
encodeOIDComponent
private void encodeOIDComponent(long value, org.wildfly.common.bytes.ByteStringBuilder contents, int numComponents, int first) throws ASN1Exception
- Throws:
ASN1Exception
-
encodeOIDComponent
private void encodeOIDComponent(BigInteger value, org.wildfly.common.bytes.ByteStringBuilder contents, int numComponents, int first) throws ASN1Exception
- Throws:
ASN1Exception
-
encodeOIDComponent
private void encodeOIDComponent(long value, org.wildfly.common.bytes.ByteStringBuilder contents)
-
encodeOIDComponent
private void encodeOIDComponent(BigInteger value, org.wildfly.common.bytes.ByteStringBuilder contents)
-
writeTag
private void writeTag(int tag, org.wildfly.common.bytes.ByteStringBuilder dest)
-
writeLength
private int writeLength(int length, org.wildfly.common.bytes.ByteStringBuilder dest) throws ASN1Exception
- Throws:
ASN1Exception
-
updateCurrentBuffer
private void updateCurrentBuffer()
-
writeElement
private void writeElement(int tag, byte[] contents)
-
writeElement
private void writeElement(int tag, org.wildfly.common.bytes.ByteStringBuilder contents)
-
-