com.ibm.security12.sun.security.util
Class DerValue

java.lang.Object
  |
  +--com.ibm.security12.sun.security.util.DerValue

public class DerValue
extends Object

Represents 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).

Version:
1.47
Author:
David Brownell, Amit Kapoor, Hemma Prafullchandra

Field Summary
protected  com.ibm.security12.sun.security.util.DerInputBuffer buffer
           
 DerInputStream data
          The DER-encoded data of the value.
 byte tag
          The DER tag of the value; one of the tag_ constants.
static byte TAG_APPLICATION
           
static byte tag_BitString
          Tag value indicating an ASN.1 "BIT STRING" value.
static byte tag_BMPString
          Tag value indicating an ASN.1 "BMPString" value.
static byte tag_Boolean
          Tag value indicating an ASN.1 "BOOLEAN" value.
static byte TAG_CONTEXT
           
static byte tag_Enumerated
          Tag value including an ASN.1 "ENUMERATED" value
static byte tag_GeneralizedTime
          Tag value indicating an ASN.1 "GeneralizedTime" value.
static byte tag_IA5String
          Tag value including an ASCII string
static byte tag_Integer
          Tag value indicating an ASN.1 "INTEGER" value.
static byte tag_Null
          Tag value indicating an ASN.1 "NULL" value.
static byte tag_ObjectId
          Tag value indicating an ASN.1 "OBJECT IDENTIFIER" value.
static byte tag_OctetString
          Tag value indicating an ASN.1 "OCTET STRING" value.
static byte tag_PrintableString
          Tag value including a "printable" string
static byte TAG_PRIVATE
           
static byte tag_Sequence
          Tag value indicating an ASN.1 "SEQUENCE" (zero to N elements, order is significant).
static byte tag_SequenceOf
          Tag value indicating an ASN.1 "SEQUENCE OF" (one to N elements, order is significant).
static byte tag_Set
          Tag value indicating an ASN.1 "SET" (zero to N members, order does not matter).
static byte tag_SetOf
          Tag value indicating an ASN.1 "SET OF" (one to N members, order does not matter).
static byte tag_T61String
          Tag value including a "teletype" string
static byte TAG_UNIVERSAL
          The tag class types
static byte tag_UniversalString
          Tag value indicating an ASN.1 "UniversalString" value.
static byte tag_UtcTime
          Tag value indicating an ASN.1 "UTCTime" value.
 
Constructor Summary
DerValue(byte[] buf)
          Get an ASN.1/DER encoded datum from a buffer.
DerValue(byte[] buf, int offset, int len)
          Get an ASN.1/DER encoded datum from part of a buffer.
DerValue(byte tag, byte[] data)
          Creates a DerValue from a tag and some DER-encoded data.
DerValue(InputStream in)
          Get an ASN1/DER encoded datum from an input stream.
DerValue(String value)
          Creates a PrintableString DER value from a string
 
Method Summary
static byte createTag(byte tagClass, boolean form, byte val)
          Create the tag of the attribute.
 void encode(DerOutputStream out)
          Encode an ASN1/DER encoded datum onto a DER output stream.
 boolean equals(DerValue other)
          Bitwise equality comparison.
 boolean equals(Object other)
          Returns true iff the other object is a DER value which is bitwise equal to this one.
 String getAsString()
          Returns the name component as a Java string, regardless of its encoding restrictions (ASCII, T61, Printable, etc).
 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.
 boolean getBoolean()
          Returns an ASN.1 BOOLEAN
 BigInt getEnumerated()
          Returns an ASN.1 ENUMERATED value.
 String getIA5String()
          Returns an ASN.1 IA5 (ASCII) STRING value
 BigInt getInteger()
          Returns an ASN.1 unsigned INTEGER value.
 BigInt getInteger(boolean tagImplicit)
          Returns an ASN.1 unsigned INTEGER value, the parameter determining if the tag is implicit.
 byte[] getOctetString()
          Returns an ASN.1 OCTET STRING
 ObjectIdentifier getOID()
          Returns an ASN.1 OBJECT IDENTIFIER.
 String getPrintableString()
          Returns an ASN.1 STRING value
 String getT61String()
          Returns an ASN.1 T61 (Teletype) STRING value
 BitArray getUnalignedBitString()
          Returns an ASN.1 BIT STRING value that need not be byte-aligned.
 BitArray getUnalignedBitString(boolean tagImplicit)
          Returns an ASN.1 BIT STRING value, with the tag assumed implicit based on the parameter.
 boolean isConstructed()
          Returns true iff the CONSTRUCTED bit is set in the type tag.
 boolean isContextSpecific()
          Returns true iff the CONTEXT SPECIFIC bit is set in the type tag.
 boolean isContextSpecific(byte cntxtTag)
          Returns true iff the CONTEXT SPECIFIC TAG matches the passed tag.
 int length()
          Get the length of the encoded value.
 void resetTag(byte tag)
          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.
 DerInputStream toDerInputStream()
          For "set" and "sequence" types, this function may be used to return a DER stream of the members of the set or sequence.
 String toString()
          Returns a printable representation of the value.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TAG_UNIVERSAL

public static final byte TAG_UNIVERSAL
The tag class types

TAG_APPLICATION

public static final byte TAG_APPLICATION

TAG_CONTEXT

public static final byte TAG_CONTEXT

TAG_PRIVATE

public static final byte TAG_PRIVATE

tag

public byte tag
The DER tag of the value; one of the tag_ constants.

buffer

protected com.ibm.security12.sun.security.util.DerInputBuffer buffer

data

public DerInputStream data
The DER-encoded data of the value.

tag_Boolean

public static final byte tag_Boolean
Tag value indicating an ASN.1 "BOOLEAN" value.

tag_Integer

public static final byte tag_Integer
Tag value indicating an ASN.1 "INTEGER" value.

tag_BitString

public static final byte tag_BitString
Tag value indicating an ASN.1 "BIT STRING" value.

tag_OctetString

public static final byte tag_OctetString
Tag value indicating an ASN.1 "OCTET STRING" value.

tag_Null

public static final byte tag_Null
Tag value indicating an ASN.1 "NULL" value.

tag_ObjectId

public static final byte tag_ObjectId
Tag value indicating an ASN.1 "OBJECT IDENTIFIER" value.

tag_Enumerated

public static final byte tag_Enumerated
Tag value including an ASN.1 "ENUMERATED" value

tag_PrintableString

public static final byte tag_PrintableString
Tag value including a "printable" string

tag_T61String

public static final byte tag_T61String
Tag value including a "teletype" string

tag_IA5String

public static final byte tag_IA5String
Tag value including an ASCII string

tag_UtcTime

public static final byte tag_UtcTime
Tag value indicating an ASN.1 "UTCTime" value.

tag_GeneralizedTime

public static final byte tag_GeneralizedTime
Tag value indicating an ASN.1 "GeneralizedTime" value.

tag_BMPString

public static final byte tag_BMPString
Tag value indicating an ASN.1 "BMPString" value.

tag_UniversalString

public static final byte tag_UniversalString
Tag value indicating an ASN.1 "UniversalString" value.

tag_Sequence

public static final byte tag_Sequence
Tag value indicating an ASN.1 "SEQUENCE" (zero to N elements, order is significant).

tag_SequenceOf

public static final byte tag_SequenceOf
Tag value indicating an ASN.1 "SEQUENCE OF" (one to N elements, order is significant).

tag_Set

public static final byte tag_Set
Tag value indicating an ASN.1 "SET" (zero to N members, order does not matter).

tag_SetOf

public static final byte tag_SetOf
Tag value indicating an ASN.1 "SET OF" (one to N members, order does not matter).
Constructor Detail

DerValue

public DerValue(String value)
         throws IOException
Creates a PrintableString DER value from a string

DerValue

public DerValue(byte tag,
                byte[] data)
Creates a DerValue from a tag and some DER-encoded data.
Parameters:
tag - the DER type tag
data - the DER-encoded data

DerValue

public DerValue(byte[] buf)
         throws IOException
Get an ASN.1/DER encoded datum from a buffer. The entire buffer must hold exactly one datum, including its tag and length.
Parameters:
buf - buffer holding a single DER-encoded datum.

DerValue

public DerValue(byte[] buf,
                int offset,
                int len)
         throws IOException
Get an ASN.1/DER encoded datum from part of a buffer. That part of the buffer must hold exactly one datum, including its tag and length.
Parameters:
buf - the buffer
offset - start point of the single DER-encoded dataum
length - how many bytes are in the encoded datum

DerValue

public DerValue(InputStream in)
         throws IOException
Get 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
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.

isContextSpecific

public boolean isContextSpecific(byte cntxtTag)
Returns true iff the CONTEXT SPECIFIC TAG matches the passed tag.

isConstructed

public boolean isConstructed()
Returns true iff the CONSTRUCTED bit is set in the type tag.

encode

public void encode(DerOutputStream out)
            throws IOException
Encode an ASN1/DER encoded datum onto a DER output stream.

getBoolean

public boolean getBoolean()
                   throws IOException
Returns an ASN.1 BOOLEAN
Returns:
the boolean held in this DER value

getOID

public ObjectIdentifier getOID()
                        throws IOException
Returns an ASN.1 OBJECT IDENTIFIER.
Returns:
the OID held in this DER value

getOctetString

public byte[] getOctetString()
                      throws IOException
Returns an ASN.1 OCTET STRING
Returns:
the octet string held in this DER value

getInteger

public BigInt getInteger()
                  throws IOException
Returns an ASN.1 unsigned INTEGER value.
Returns:
the (unsigned) integer held in this DER value

getInteger

public BigInt getInteger(boolean tagImplicit)
                  throws IOException
Returns an ASN.1 unsigned INTEGER value, the parameter determining if the tag is implicit.
Returns:
the (unsigned) integer held in this DER value

getEnumerated

public BigInt getEnumerated()
                     throws IOException
Returns an ASN.1 ENUMERATED value.
Returns:
the integer held in this DER value

getBitString

public byte[] getBitString()
                    throws IOException
Returns an ASN.1 BIT STRING value. The bit string must be byte-aligned.
Returns:
the bit string held in this value

getUnalignedBitString

public BitArray getUnalignedBitString()
                               throws 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

getAsString

public String getAsString()
                   throws IOException
Returns the name component as a Java string, regardless of its encoding restrictions (ASCII, T61, Printable, etc).

getBitString

public byte[] getBitString(boolean tagImplicit)
                    throws IOException
Returns an ASN.1 BIT STRING value, with the tag assumed implicit based on the parameter. The bit string must be byte-aligned.
Returns:
the bit string held in this value

getUnalignedBitString

public BitArray getUnalignedBitString(boolean tagImplicit)
                               throws 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.
Returns:
the bit string held in this value

getPrintableString

public String getPrintableString()
                          throws IOException
Returns an ASN.1 STRING value
Returns:
the printable string held in this value

getT61String

public String getT61String()
                    throws IOException
Returns an ASN.1 T61 (Teletype) STRING value
Returns:
the teletype string held in this value

getIA5String

public String getIA5String()
                    throws IOException
Returns an ASN.1 IA5 (ASCII) STRING value
Returns:
the ASCII string held in this value

equals

public boolean equals(Object other)
Returns true iff the other object is a DER value which is bitwise equal to this one.
Parameters:
other - the object being compared with this one
Overrides:
equals in class 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

toString

public String toString()
Returns a printable representation of the value.
Returns:
printable representation of the value
Overrides:
toString in class Object

toByteArray

public byte[] toByteArray()
                   throws IOException
Returns 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.

toDerInputStream

public DerInputStream toDerInputStream()
                                throws 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.

length

public int length()
Get the length of the encoded value.

createTag

public static byte createTag(byte tagClass,
                             boolean form,
                             byte val)
Create the tag of the attribute.

resetTag

public void resetTag(byte tag)
Set the tag of the attribute. Commonly used to reset the tag value used for IMPLICIT encodings.