Class DERValue
- java.lang.Object
-
- com.github.toolarium.security.rsa.der.DERValue
-
public class DERValue extends java.lang.ObjectRepresents a single DER-encoded value. DER encoding rules are a subset of the "Basic" Encoding Rules (BER), but they only support a single way ("Definite" encoding) to encode any given value.All DER-encoded data are triples {type, length, data}. This class represents such tagged values as they have been read (or constructed), and provides structured access to the encoded 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, and working with selected additional formats (such as PKCS #10 certificate requests, and some kinds of PKCS #7 data).
-
-
Field Summary
Fields Modifier and Type Field Description static byteTAG_APPLICATIONthe application tagstatic bytetag_BitStringTag value indicating an ASN.1 "BIT STRING" value.static bytetag_BMPStringTag value indicating an ASN.1 "BMPString" value.static bytetag_BooleanTag value indicating an ASN.1 "BOOLEAN" value.static byteTAG_CONTEXTthe context tagstatic bytetag_EnumeratedTag value including an ASN.1 "ENUMERATED" valuestatic bytetag_GeneralizedTimeTag value indicating an ASN.1 "GeneralizedTime" value.static bytetag_IA5StringTag value including an ASCII stringstatic bytetag_IntegerTag value indicating an ASN.1 "INTEGER" value.static bytetag_NullTag value indicating an ASN.1 "NULL" value.static bytetag_ObjectIdTag value indicating an ASN.1 "OBJECT IDENTIFIER" value.static bytetag_OctetStringTag value indicating an ASN.1 "OCTET STRING" value.static bytetag_PrintableStringTag value including a "printable" stringstatic byteTAG_PRIVATEthe private tagstatic bytetag_SequenceTag value indicating an ASN.1 "SEQUENCE" (zero to N elements, order is significant).static bytetag_SequenceOfTag value indicating an ASN.1 "SEQUENCE OF" (one to N elements, order is significant).static bytetag_SetTag value indicating an ASN.1 "SET" (zero to N members, order does not matter).static bytetag_SetOfTag value indicating an ASN.1 "SET OF" (one to N members, order does not matter).static bytetag_T61StringTag value including a "teletype" stringstatic byteTAG_UNIVERSALThe tag class typesstatic bytetag_UniversalStringTag value indicating an ASN.1 "UniversalString" value.static bytetag_UtcTimeTag value indicating an ASN.1 "UTCTime" value.static bytetag_UTF8StringTag value indicating an ASN.1 "UTF8String" value.
-
Constructor Summary
Constructors Constructor Description DERValue(byte tag, byte[] data)Creates a DERValue from a tag and some DER-encoded data.DERValue(java.io.InputStream in)Get an ASN1/DER encoded datum from an input stream.DERValue(java.lang.String value)Creates a PrintableString DER value from a string
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static bytecreateTag(byte tagClass, boolean form, byte val)Create the tag of the attribute.voidencode(DEROutputStream out)Encode an ASN1/DER encoded datum onto a DER output stream.booleanequals(DERValue other)Bitwise equality comparison.booleanequals(java.lang.Object other)java.lang.StringgetAsString()Returns the name component as a Java string, regardless of its encoding restrictions (ASCII, T61, Printable, IA5, BMP, UTF8).byte[]getBitString()Returns an ASN.1 BIT STRING value.byte[]getBitString(boolean tagImplicit)Returns an ASN.1 BIT STRING value, with the tag assumed implicit based on the parameter.java.lang.StringgetBMPString()Returns the ASN.1 BMP (Unicode) STRING value as a Java string.booleangetBoolean()Returns an ASN.1 BOOLEANBigIntgetEnumerated()Returns an ASN.1 ENUMERATED value.java.lang.StringgetIA5String()Returns an ASN.1 IA5 (ASCII) STRING valueBigIntgetInteger()Returns an ASN.1 unsigned INTEGER value.BigIntgetInteger(boolean tagImplicit)Returns an ASN.1 unsigned INTEGER value, the parameter determining if the tag is implicit.byte[]getOctetString()Returns an ASN.1 OCTET STRINGObjectIdentifiergetOID()Returns an ASN.1 OBJECT IDENTIFIER.java.lang.StringgetPrintableString()Returns an ASN.1 STRING valuejava.lang.StringgetT61String()Returns an ASN.1 T61 (Teletype) STRING valueBitArraygetUnalignedBitString()Returns an ASN.1 BIT STRING value that need not be byte-aligned.BitArraygetUnalignedBitString(boolean tagImplicit)Returns an ASN.1 BIT STRING value, with the tag assumed implicit based on the parameter.java.lang.StringgetUTF8String()Returns the ASN.1 UTF-8 STRING value as a Java String.inthashCode()Returns a hashcode for this DERValue.booleanisConstructed()Returns true iff the CONSTRUCTED bit is set in the type tag.booleanisContextSpecific()Returns true iff the CONTEXT SPECIFIC bit is set in the type tag.booleanisContextSpecific(byte cntxtTag)Returns true iff the CONTEXT SPECIFIC TAG matches the passed tag.intlength()Get the length of the encoded value.voidresetTag(byte t)Set the tag of the attribute.byte[]toByteArray()Returns a DER-encoded value, such that if it's passed to the DERValue constructor, a value equivalent to "this" is returned.DERInputStreamtoDERInputStream()For "set" and "sequence" types, this function may be used to return a DER stream of the members of the set or sequence.java.lang.StringtoString()Returns a printable representation of the value.
-
-
-
Field Detail
-
tag_Boolean
public static final byte tag_Boolean
Tag value indicating an ASN.1 "BOOLEAN" value.- See Also:
- Constant Field Values
-
tag_Integer
public static final byte tag_Integer
Tag value indicating an ASN.1 "INTEGER" value.- See Also:
- Constant Field Values
-
tag_BitString
public static final byte tag_BitString
Tag value indicating an ASN.1 "BIT STRING" value.- See Also:
- Constant Field Values
-
tag_OctetString
public static final byte tag_OctetString
Tag value indicating an ASN.1 "OCTET STRING" value.- See Also:
- Constant Field Values
-
tag_Null
public static final byte tag_Null
Tag value indicating an ASN.1 "NULL" value.- See Also:
- Constant Field Values
-
tag_ObjectId
public static final byte tag_ObjectId
Tag value indicating an ASN.1 "OBJECT IDENTIFIER" value.- See Also:
- Constant Field Values
-
tag_Enumerated
public static final byte tag_Enumerated
Tag value including an ASN.1 "ENUMERATED" value- See Also:
- Constant Field Values
-
tag_UTF8String
public static final byte tag_UTF8String
Tag value indicating an ASN.1 "UTF8String" value.- See Also:
- Constant Field Values
-
tag_PrintableString
public static final byte tag_PrintableString
Tag value including a "printable" string- See Also:
- Constant Field Values
-
tag_T61String
public static final byte tag_T61String
Tag value including a "teletype" string- See Also:
- Constant Field Values
-
tag_IA5String
public static final byte tag_IA5String
Tag value including an ASCII string- See Also:
- Constant Field Values
-
tag_UtcTime
public static final byte tag_UtcTime
Tag value indicating an ASN.1 "UTCTime" value.- See Also:
- Constant Field Values
-
tag_GeneralizedTime
public static final byte tag_GeneralizedTime
Tag value indicating an ASN.1 "GeneralizedTime" value.- See Also:
- Constant Field Values
-
tag_UniversalString
public static final byte tag_UniversalString
Tag value indicating an ASN.1 "UniversalString" value.- See Also:
- Constant Field Values
-
tag_BMPString
public static final byte tag_BMPString
Tag value indicating an ASN.1 "BMPString" value.- See Also:
- Constant Field Values
-
tag_Sequence
public static final byte tag_Sequence
Tag value indicating an ASN.1 "SEQUENCE" (zero to N elements, order is significant).- See Also:
- Constant Field Values
-
tag_SequenceOf
public static final byte tag_SequenceOf
Tag value indicating an ASN.1 "SEQUENCE OF" (one to N elements, order is significant).- See Also:
- Constant Field Values
-
tag_Set
public static final byte tag_Set
Tag value indicating an ASN.1 "SET" (zero to N members, order does not matter).- See Also:
- Constant Field Values
-
tag_SetOf
public static final byte tag_SetOf
Tag value indicating an ASN.1 "SET OF" (one to N members, order does not matter).- See Also:
- Constant Field Values
-
TAG_UNIVERSAL
public static final byte TAG_UNIVERSAL
The tag class types- See Also:
- Constant Field Values
-
TAG_APPLICATION
public static final byte TAG_APPLICATION
the application tag- See Also:
- Constant Field Values
-
TAG_CONTEXT
public static final byte TAG_CONTEXT
the context tag- See Also:
- Constant Field Values
-
TAG_PRIVATE
public static final byte TAG_PRIVATE
the private tag- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DERValue
public DERValue(java.lang.String value)
Creates a PrintableString DER value from a string- Parameters:
value- the value
-
DERValue
public DERValue(byte tag, byte[] data)Creates a DERValue from a tag and some DER-encoded data.- Parameters:
tag- the DER type tagdata- the DER-encoded data
-
DERValue
public DERValue(java.io.InputStream in) throws java.io.IOExceptionGet an ASN1/DER encoded datum from an input stream. The stream may have additional data following the encoded datum. In case of indefinite length encoded datum, the input stream must hold only one datum.- Parameters:
in- the input stream holding a single DER datum, which may be followed by additional data- Throws:
java.io.IOException- in case of error
-
-
Method Detail
-
isContextSpecific
public boolean isContextSpecific()
Returns true iff the CONTEXT SPECIFIC bit is set in the type tag. This is associated with the ASN.1 "DEFINED BY" syntax.- Returns:
- result
-
isContextSpecific
public boolean isContextSpecific(byte cntxtTag)
Returns true iff the CONTEXT SPECIFIC TAG matches the passed tag.- Parameters:
cntxtTag- the tag- Returns:
- result
-
isConstructed
public boolean isConstructed()
Returns true iff the CONSTRUCTED bit is set in the type tag.- Returns:
- result
-
encode
public void encode(DEROutputStream out) throws java.io.IOException
Encode an ASN1/DER encoded datum onto a DER output stream.- Parameters:
out- the stream- Throws:
java.io.IOException- in case of error
-
getBoolean
public boolean getBoolean() throws java.io.IOExceptionReturns an ASN.1 BOOLEAN- Returns:
- the boolean held in this DER value
- Throws:
java.io.IOException- in case of error
-
getOID
public ObjectIdentifier getOID() throws java.io.IOException
Returns an ASN.1 OBJECT IDENTIFIER.- Returns:
- the OID held in this DER value
- Throws:
java.io.IOException- in case of error
-
getOctetString
public byte[] getOctetString() throws java.io.IOExceptionReturns an ASN.1 OCTET STRING- Returns:
- the octet string held in this DER value
- Throws:
java.io.IOException- in case of error
-
getInteger
public BigInt getInteger() throws java.io.IOException
Returns an ASN.1 unsigned INTEGER value.- Returns:
- the (unsigned) integer held in this DER value
- Throws:
java.io.IOException- in case of error
-
getInteger
public BigInt getInteger(boolean tagImplicit) throws java.io.IOException
Returns an ASN.1 unsigned INTEGER value, the parameter determining if the tag is implicit.- Parameters:
tagImplicit- if true, ignores the tag value as it is assumed implicit.- Returns:
- the (unsigned) integer held in this DER value
- Throws:
java.io.IOException- in case of error
-
getEnumerated
public BigInt getEnumerated() throws java.io.IOException
Returns an ASN.1 ENUMERATED value.- Returns:
- the integer held in this DER value
- Throws:
java.io.IOException- in case of error
-
getBitString
public byte[] getBitString() throws java.io.IOExceptionReturns an ASN.1 BIT STRING value. The bit string must be byte-aligned.- Returns:
- the bit string held in this value
- Throws:
java.io.IOException- in case of error
-
getBitString
public byte[] getBitString(boolean tagImplicit) throws java.io.IOExceptionReturns an ASN.1 BIT STRING value, with the tag assumed implicit based on the parameter. The bit string must be byte-aligned.- Parameters:
tagImplicit- if true, the tag is assumed implicit.- Returns:
- the bit string held in this value
- Throws:
java.io.IOException- in case of error
-
getUnalignedBitString
public BitArray getUnalignedBitString() throws java.io.IOException
Returns an ASN.1 BIT STRING value that need not be byte-aligned.- Returns:
- a BitArray representing the bit string held in this value
- Throws:
java.io.IOException- in case of error
-
getUnalignedBitString
public BitArray getUnalignedBitString(boolean tagImplicit) throws java.io.IOException
Returns an ASN.1 BIT STRING value, with the tag assumed implicit based on the parameter. The bit string need not be byte-aligned.- Parameters:
tagImplicit- if true, the tag is assumed implicit.- Returns:
- the bit string held in this value
- Throws:
java.io.IOException- in case of error
-
getAsString
public java.lang.String getAsString() throws java.io.IOExceptionReturns the name component as a Java string, regardless of its encoding restrictions (ASCII, T61, Printable, IA5, BMP, UTF8).- Returns:
- the string
- Throws:
java.io.IOException- in case of error
-
getPrintableString
public java.lang.String getPrintableString() throws java.io.IOExceptionReturns an ASN.1 STRING value- Returns:
- the printable string held in this value
- Throws:
java.io.IOException- in case of error
-
getT61String
public java.lang.String getT61String() throws java.io.IOExceptionReturns an ASN.1 T61 (Teletype) STRING value- Returns:
- the teletype string held in this value
- Throws:
java.io.IOException- in case of error
-
getIA5String
public java.lang.String getIA5String() throws java.io.IOExceptionReturns an ASN.1 IA5 (ASCII) STRING value- Returns:
- the ASCII string held in this value
- Throws:
java.io.IOException- in case of error
-
getBMPString
public java.lang.String getBMPString() throws java.io.IOExceptionReturns the ASN.1 BMP (Unicode) STRING value as a Java string.- Returns:
- a string corresponding to the encoded BMPString held in this value
- Throws:
java.io.IOException- in case of error
-
getUTF8String
public java.lang.String getUTF8String() throws java.io.IOExceptionReturns the ASN.1 UTF-8 STRING value as a Java String.- Returns:
- a string corresponding to the encoded UTF8String held in this value
- Throws:
java.io.IOException- in case of error
-
equals
public boolean equals(java.lang.Object other)
- Overrides:
equalsin classjava.lang.Object- See Also:
Object.equals(java.lang.Object)
-
equals
public boolean equals(DERValue other)
Bitwise equality comparison. DER encoded values have a single encoding, so that bitwise equality of the encoded values is an efficient way to establish equivalence of the unencoded values.- Parameters:
other- the object being compared with this one- Returns:
- true if they are the equals
-
toString
public java.lang.String toString()
Returns a printable representation of the value.- Overrides:
toStringin classjava.lang.Object- Returns:
- printable representation of the value
-
toByteArray
public byte[] toByteArray() throws java.io.IOExceptionReturns a DER-encoded value, such that if it's passed to the DERValue constructor, a value equivalent to "this" is returned.- Returns:
- DER-encoded value, including tag and length.
- Throws:
java.io.IOException- in case of error
-
toDERInputStream
public DERInputStream toDERInputStream() throws java.io.IOException
For "set" and "sequence" types, this function may be used to return a DER stream of the members of the set or sequence. This operation is not supported for primitive types such as integers or bit strings.- Returns:
- the stream
- Throws:
java.io.IOException- in case of error
-
length
public int length()
Get the length of the encoded value.- Returns:
- the length
-
createTag
public static byte createTag(byte tagClass, boolean form, byte val)Create the tag of the attribute.- Parameters:
tagClass- the tag class type, one of UNIVERSAL, CONTEXT, APPLICATION or PRIVATEform- if true, the value is constructed, otherwise it is primitive.val- the tag value- Returns:
- the created byte
-
resetTag
public void resetTag(byte t)
Set the tag of the attribute. Commonly used to reset the tag value used for IMPLICIT encodings.- Parameters:
t- the tag value
-
hashCode
public int hashCode()
Returns a hashcode for this DERValue.- Overrides:
hashCodein classjava.lang.Object- Returns:
- a hashcode for this DERValue.
-
-