iaik.asn1.structures
Class AVA

java.lang.Object
  |
  +--iaik.asn1.structures.AVA

public class AVA
extends java.lang.Object
implements ASN1Type

This class implements the ASN.1 type AttributeValueAssertion. An AttributeValueAssertion gives one component of a RelativeDistinguishedName structure:

 RelativeDistinguishedName ::= SET OF AttributeValueAssertion

 AttributeValueAssertion ::= SEQUENCE {
  AttributeType    OBJECT IDENTIFIER,
  AttributeValue   ANY
 }
 

When creating a new AVA object, the attribute type has to be specified as ObjectID, and the value has to be supplied as a Java object of matching type (i.e. compatible to the type expected by the setValue method of the corresponding ASN.1 type), e.g.:

 AVA ava = new AVA(ObjectID.commonName, "John Doe");
 
The example above will create an AVA for the X.500 attribute type commonName. Since, per default, the commonName value will be encoded as PrintableString, the value has to be specified as java.lang.String object since the setValue method of class PrintableString expects a String object. Alternatively immediately a PrintableString ASN1Object may be supplied (and will be encoded unchanged):
 AVA ava = new AVA(ObjectID.commonName, new PrintableString("John Doe"));
 

When DER encoding an AVA object where the value is not immediately given as ASN1Object, the inherent value per default will be encoded as ASN.1 character string of type PrintableString, except for the X.500 attribute type uniqueIdentifier and the PKCS#9 attribute type emailAddress which per default will be encoded as BIT_STRING, and IA5String, respectively.

An application wishing to use another encoding type than PrintableString for encoding some specific attribute value, may register the ASN.1 encoding type in mind by means of the static defineEncoding(ObjectID type, ASN encodingType) method, e.g.:

 AVA.defineEncoding(ObjectID.title, ASN.VisibleString);
 
will enforce that any AVA value of X.500 attribute type title will be encoded as VisibleString instead of PrintableString}.

If you want to use a different encoding type only for one specific AVA object you may use the non static setEncoding method, e.g.:

 AVA ava = new AVA(ObjectID.commonName, "John Doe");
 ava.setEncoding(ASN.IA5String);
 
will enforce that the value of this specific AVA object will be encoded as IA5String.

Special care has to taken when using an attribute of type type uniqueIdentifier. Since the uniqueIdentifier value has to be encoded as BIT_STRING, per default the value has to be specified as byte array as expected by the setValue method of the BIT_STRING class:

 byte[] value = ...;
 AVA ava = new AVA(ObjectID.uniqueIdentifier, value);
 
However, since some certificates use a DER encoded PrintableString as value for the BIT_STRING uniqueIdentifier alternatively a string value may be supplied:
 String s = ...;
 AVA ava = new AVA(ObjectID.uniqueIdentifier, s);
 
In such cases, the AVA will encode the uniqueIdentifier value as BIT_STRING having a DER encoded PrintableString as its value.


Constructor Summary
AVA(ASN1Object obj)
          Creates an AttributeValueAssertion from an ASN1Object.
AVA(ObjectID type, java.lang.Object value)
          Creates a new AttributeValueAssertion from a type and a value.
AVA(java.lang.String avaName)
          Creates a new AttributeValueAssertion from a string formatted version, such as cn=John Doe.
 
Method Summary
 void decode(ASN1Object obj)
          Decodes an AVA from the given ASN1Object.
static void defineEncoding(ObjectID type, ASN encodingType)
          Defines the ASN.1 encoding for a specified Attribute type.
 boolean equals(java.lang.Object obj)
          Compares two AttributeValueAssertions.
 ASN1Object getASN1Value()
          Returns an ASN.1 representation of the value of this AttributeValueAssertion.
 ObjectID getType()
          Returns the type of this AttributeValueAssertion.
 java.lang.Object getValue()
          Returns the value of this AttributeValueAssertion.
 int hashCode()
          Returns the hashcode for this AttributeValueAssertion.
 void setEncoding(ASN encodingType)
          Sets the ASN.1 encoding for this AVA.
 ASN1Object toASN1Object()
          Returns this AttributeValueAssertion as an ASN1Object.
 java.lang.String toString()
          Returns a string that represents the contents of this RDN.
 java.lang.String toString(boolean detailed)
          Returns a string that represents the contents of this AVA.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AVA

public AVA(ObjectID type,
           java.lang.Object value)
    throws java.lang.IllegalArgumentException
Creates a new AttributeValueAssertion from a type and a value. The value has to be supplied as a Java object of matching type (i.e. compatible to the type expected by the setValue method of the corresponding ASN.1 type), e.g.:
 AVA ava = new AVA(ObjectID.commonName, "John Doe");
 
The example above will create an AVA for the X.500 attribute type commonName. Since, per default, the commonName value will be encoded as PrintableString, the value has to be specified as java.lang.String object since the setValue method of class PrintableString expects a String object. Alternatively immediately a PrintableString ASN1Object may be supplied:
 AVA ava = new AVA(ObjectID.commonName, new PrintableString("John Doe"));
 
Parameters:
type - the attribute type as an ObjectID
value - the attribute value

AVA

public AVA(java.lang.String avaName)
    throws java.lang.IllegalArgumentException
Creates a new AttributeValueAssertion from a string formatted version, such as cn=John Doe. It currently assumes that the value is a string type.

AVA

public AVA(ASN1Object obj)
    throws CodingException
Creates an AttributeValueAssertion from an ASN1Object.

The supplied ASN1Object represents an already existing AVA that may have been created by means of the toASN1Object method.

Parameters:
obj - the AttributeValueAssertion as ASN1Object
Throws:
CodingException - if the AttributeValueAssertion can not be decoded
Method Detail

defineEncoding

public static void defineEncoding(ObjectID type,
                                  ASN encodingType)
Defines the ASN.1 encoding for a specified Attribute type. The default encoding is a PrintableString meaning that per default the inherent attribute value will be encoded as ASN.1 character string of type PrintableString, except for the X.500 attribute type uniqueIdentifier and the PKCS#9 attribute type emailAddress which per default will be encoded as BIT_STRING, and IA5String, respectively.

This method may be used to enforce another encoding scheme for some specific attribute type, e.g.:

 AVA.defineEncoding(ObjectID.title, ASN.VisibleString);
 
will enforce that any AVA value of X.500 attribute type title will be encoded as VisibleString instead of PrintableString}.
Parameters:
type - the attribute type for which a new encoding scheme shall be defined
encodingType - the ASN.1 type for encoding it

setEncoding

public void setEncoding(ASN encodingType)
Sets the ASN.1 encoding for this AVA.

This method may be used to enforce a particular encoding for this AVA object, e.g.:

 AVA ava = new AVA(ObjectID.commonName, "John Doe");
 ava.setEncoding(ASN.IA5String);
 
will enforce that the value of this specific AVA object will be encoded as IA5String.
Parameters:
encodingType - the ASN.1 type specifying the encoding to be used.

decode

public void decode(ASN1Object obj)
            throws CodingException
Decodes an AVA from the given ASN1Object.

The supplied ASN1Object represents an already existing AVA object that may have been created by means of the toASN1Object() method.

Specified by:
decode in interface ASN1Type
Parameters:
obj - the AttributeValueAssertion as ASN1Object
Throws:
CodingException - if the AttributeValueAssertion can not be decoded

toASN1Object

public ASN1Object toASN1Object()
Returns this AttributeValueAssertion as an ASN1Object.
Specified by:
toASN1Object in interface ASN1Type
Returns:
this AttributeValueAssertion as an ASN1Object

getType

public ObjectID getType()
Returns the type of this AttributeValueAssertion.

getValue

public java.lang.Object getValue()
Returns the value of this AttributeValueAssertion.

getASN1Value

public ASN1Object getASN1Value()
                        throws CodingException
Returns an ASN.1 representation of the value of this AttributeValueAssertion.
Returns:
an ASN.1 representation of the value of this AttributeValueAssertion
Throws:
CodingException - if an error occurs when getting the ASN.1 representation

hashCode

public int hashCode()
Returns the hashcode for this AttributeValueAssertion.
Overrides:
hashCode in class java.lang.Object
Returns:
a hashcode for this AttributeValueAssertion

equals

public boolean equals(java.lang.Object obj)
Compares two AttributeValueAssertions.
Overrides:
equals in class java.lang.Object
Parameters:
obj - the other AttributeValueAssertion
Returns:
true, if the two AttributeValueAssertions are equal, false otherwise

toString

public java.lang.String toString()
Returns a string that represents the contents of this RDN.

The string output is compatible to RFC 1779/2253 except for escaping. If desired, an application itself make take care for proper escaping.

Care has to be taken about attributes of type uniqueIdentifier. Since some certificates use a DER encoded PrintableString as value for the BIT_STRING uniqueIdentifier this method prints a string for such a uniqueIdentifier. Otherwise a binary string representation of the bit string value is printed, e.g.: "#'10010'B".

Overrides:
toString in class java.lang.Object
Returns:
the string representation

toString

public java.lang.String toString(boolean detailed)
Returns a string that represents the contents of this AVA.

The string output is compatible to RFC 1779/2253 except for escaping. If desired, an application itself make take care for proper escaping.

Care has to be taken about attributes of type uniqueIdentifier. Since some certificates use a DER encoded PrintableString as value for the BIT_STRING uniqueIdentifier this method prints a string for such a uniqueIdentifier. Otherwise a binary string representation of the bit string value is printed, e.g.: "#'10010'B".

Parameters:
detailed - whether the short name of the full name of the type shall be used
Returns:
the string representation