iaik.me.asn1
Class ASN1

java.lang.Object
  |
  +--iaik.me.asn1.ASN1

public class ASN1
extends Object

This class implements decoding and encoding of BER/DER encoded ASN.1 data. It also serves as the registry for ObjectIDs mapping them to (algorithm) names and vice versa.

Decoding is able to handle any valid BER encoding that comes along. Encoding always generates DER. Both processes require at least as much memory as the size of the encoded structure.


Field Summary
static int CS0
          Tag for [0] (context specific 0).
static int CS1
          Tag for [1] (context specific 1).
static int CS2
          Tag for [2] (context specific 2).
static int CS3
          Tag for [3] (context specific 3).
static String OID_AES_128_CBC
          Object id 2.16.840.1.101.3.4.1.2 identifying AES/CBC/PKCS5Padding (with 128 bit key).
static String OID_DES_EDE_CBC
          Object id 1.2.840.113549.3.7 identifying DES/CBC/PKCS5Padding.
static String OID_DH
          Object id 1.2.840.113549.1.3.1 identifying DSA.
static String OID_DSA
          Object id 1.2.840.10040.4.1 identifying DSA.
static String OID_HMAC_SHA1
          Object id 1.2.840.113549.2.7 identifying HmacSHA1.
static String OID_MD2
          Object id 1.2.840.113549.2.2 identifying MD2.
static String OID_MD2_WITH_RSA_ENCRYPTION
          Object id 1.2.840.113549.1.1.2 identifying MD2withRSA signatures.
static String OID_MD5
          Object id 1.2.840.113549.2.5 identifying MD5.
static String OID_MD5_WITH_RSA_ENCRYPTION
          Object id 1.2.840.113549.1.1.4 identifying MD5withRSA signatures.
static String OID_NAME_C
          Object id 2.5.4.6 for the country attribute.
static String OID_NAME_CN
          Object id 2.5.4.3 for the commonName attribute.
static String OID_NAME_EMAIL
          Object id 1.2.840.113549.1.9.1 for the emailAddress attribute.
static String OID_NAME_L
          Object id 2.5.4.7 for the locality attribute.
static String OID_NAME_O
          Object id 2.5.4.10 for the organization attribute.
static String OID_NAME_OU
          Object id 2.5.4.11 for the organizationalUnit attribute.
static String OID_NAME_SERIAL
          Object id 2.5.4.5 for the serialNumber attribute.
static String OID_NAME_SN
          Object id 2.5.4.4 for the surName attribute.
static String OID_NAME_ST
          Object id 2.5.4.8 for the stateOrProvince attribute.
static String OID_NAME_STREET
          Object id 2.5.4.9 for the street attribute.
static String OID_NAME_T
          Object id 2.5.4.12 for the title attribute.
static String OID_RSA
          Object id 1.2.840.113549.1.1.1 identifying RSA.
static String OID_SHA
          Object id 1.3.14.3.2.26 identifying SHA-1.
static String OID_SHA1_WITH_DSA
          Object id 1.2.840.10040.4.3 identifying SHA1withDSA signatures.
static String OID_SHA1_WITH_RSA_ENCRYPTION
          Object id 1.2.840.113549.1.1.5 identifying SHA1withRSA signatures.
static String OID_SHA224
          Object id 2.16.840.1.101.3.4.2.4 identifying SHA-224.
static String OID_SHA256
          Object id 2.16.840.1.101.3.4.2.1 identifying SHA-256.
static int TAG_APPLICATION
          Mask for tag class application.
static int TAG_CONTEXT_SPECIFIC
          Mask for tag class context specific .
static int TAG_PRIVATE
          Mask for tag class private.
static int TAG_UNIVERSAL
          Mask for tag class universal.
static int TYPE_BIT_STRING
          Tag for [UNIVERSAL 3], the bit string type.
static int TYPE_BMP_STRING
          Tag for [UNIVERSAL 30], the BMP (Unicode) string type.
static int TYPE_BOOLEAN
          Tag for [UNIVERSAL 1], the boolean type.
static int TYPE_CHARACTER_STRING
          Tag for [UNIVERSAL 29], the character string type.
static int TYPE_EMBEDDED_PDV
          Tag for [UNIVERSAL 11], the embedded pdv type.
static int TYPE_ENUMERATED
          Tag for [UNIVERSAL 10], the enumerated type.
static int TYPE_EXTERNAL
          Tag for [UNIVERSAL 8], the external type.
static int TYPE_GENERAL_STRING
          Tag for [UNIVERSAL 27], the general string type.
static int TYPE_GENERALIZED_TIME
          Tag for [UNIVERSAL 24], the generalized time type.
static int TYPE_GRAPHIC_STRING
          Tag for [UNIVERSAL 25], the graphic string type.
static int TYPE_IA5_STRING
          Tag for [UNIVERSAL 22], the IA5 string type.
static int TYPE_INTEGER
          Tag for [UNIVERSAL 2], the integer type.
static int TYPE_NULL
          Tag for [UNIVERSAL 5], the null type.
static int TYPE_NUMERIC_STRING
          Tag for [UNIVERSAL 18], the numeric string type.
static int TYPE_OBJECT_DESCRIPTOR
          Tag for [UNIVERSAL 7], the object descriptor type.
static int TYPE_OBJECT_ID
          Tag for [UNIVERSAL 6], the object id type.
static int TYPE_OCTET_STRING
          Tag for [UNIVERSAL 4], the octet string type.
static int TYPE_PRINTABLE_STRING
          Tag for [UNIVERSAL 19], the printable string type.
static int TYPE_REAL
          Tag for [UNIVERSAL 9], the real type.
static int TYPE_RELATIVE_OID
          Tag for [UNIVERSAL 13], the relative object id type.
static int TYPE_SEQUENCE
          Tag for [UNIVERSAL 16], the sequence type.
static int TYPE_SET
          Tag for [UNIVERSAL 17], the set type.
static int TYPE_T61_STRING
          Tag for [UNIVERSAL 20], the T61 (teletex) string type.
static int TYPE_UNIVERSAL_STRING
          Tag for [UNIVERSAL 28], the universal string type.
static int TYPE_UTCTIME
          Tag for [UNIVERSAL 23], the UTC time type.
static int TYPE_UTF8_STRING
          Tag for [UNIVERSAL 12], the UTF8 string type.
static int TYPE_VIDEOTEX_STRING
          Tag for [UNIVERSAL 21], the videotex string type.
static int TYPE_VISIBLE_STRING
          Tag for [UNIVERSAL 26], the visible string type.
 
Constructor Summary
ASN1(byte[] data)
           
ASN1(byte[] data, int saveEncoding, boolean expectImplicit)
           
ASN1(InputStream in)
           
ASN1(InputStream in, int saveEncoding, boolean expectImplicit)
           
ASN1(int type, Object value)
           
ASN1(int type, Object value, boolean implicit)
           
 
Method Summary
 void addElement(ASN1 asn1)
          Add an element to a structured type.
 void clearEncoding(boolean recursive)
          Clear the cached encoding.
 boolean equals(Object obj)
          Compare this and another ASN1 object.
 ASN1 getElementAt(int n)
          Get an element of a structured type.
 byte[] getEncoded()
          Get the encoding of this object.
static String getName(String objectId)
          Get the algorithm name associated with the given object id.
static String getObjectId(String name)
          Return the ObjectId associated with the given algorithm name.
 int getSize()
          Get the number of elements in this structured type.
 int getTagClass()
          Get the tag class for this object.
 int getType()
          Get the tag of this ASN.1 object.
 int getTypeOnly()
          Get the type of this ASN.1 object without the tag class.
 int getUnusedBits()
          Returns the number of unused bits if the ANS.1 type is a BIT STRING.
 String gvAlgorithm()
          Get the algorithm value of this AlgorithmID object.
 ASN1 gvASN1()
          Get the value as an ASN1 object.
 BigInteger gvBigInteger()
          Get the value as BigInteger.
 boolean gvBoolean()
          Get the value as a boolean.
 byte[] gvByteArray()
          Get the value as a byte array.
 Date gvDate()
          Get the value as a Date.
 int gvInt()
          Get the value as an int.
 Object gvObject()
          Get the value as Object.
 String gvObjectId()
          Get the value as a object id.
 String gvString()
          Get the value as a String.
 Vector gvVector()
          Get the value as a Vector.
 int hashCode()
          Get a hashCode for this ASN1 object.
 boolean isConstructed()
          Check whether this type uses constructed encoding.
static boolean isObjectId(String s)
          Return whether or not the string represents an ObjectId.
static ASN1 makeAlgorithmId(String objectId, ASN1 params)
          Creates a ASN.1 AlgorithmID structure.
static ASN1 makeSequence(int size)
          Make an ASN.1 sequence.
static ASN1 makeSignatureAlgId(String objectId, ASN1 params)
          Creates a ASN.1 AlgorithmID structure {@see makeAlgorithmId( String, ASN1 ); } for digital signatures.
static ASN1 makeString(String s)
          Make an ASN.1 string type from s suitable for use in a X.500 name.
static void register(String objectId, String name)
          Register an algorithm name for an ObjectId.
 void setImplicitTag(int tag)
           
 void setUnusedBits(int bits)
          Sets the number of unused bits for encoding a ASN.1 BIT STRING object.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OID_RSA

public static final String OID_RSA
Object id 1.2.840.113549.1.1.1 identifying RSA. The alias 2.5.8.1.1 is recognized as well.

OID_DSA

public static final String OID_DSA
Object id 1.2.840.10040.4.1 identifying DSA. The alias 1.3.14.3.2.12 is recognized as well.

OID_DH

public static final String OID_DH
Object id 1.2.840.113549.1.3.1 identifying DSA.

OID_MD2_WITH_RSA_ENCRYPTION

public static final String OID_MD2_WITH_RSA_ENCRYPTION
Object id 1.2.840.113549.1.1.2 identifying MD2withRSA signatures.

OID_MD5_WITH_RSA_ENCRYPTION

public static final String OID_MD5_WITH_RSA_ENCRYPTION
Object id 1.2.840.113549.1.1.4 identifying MD5withRSA signatures.

OID_SHA1_WITH_RSA_ENCRYPTION

public static final String OID_SHA1_WITH_RSA_ENCRYPTION
Object id 1.2.840.113549.1.1.5 identifying SHA1withRSA signatures. The alias 1.3.14.3.2.29 is recognized as well.

OID_SHA1_WITH_DSA

public static final String OID_SHA1_WITH_DSA
Object id 1.2.840.10040.4.3 identifying SHA1withDSA signatures. The aliases 1.3.14.3.2.27 and 1.3.14.3.2.13 are recognized as well.

OID_MD2

public static final String OID_MD2
Object id 1.2.840.113549.2.2 identifying MD2.

OID_MD5

public static final String OID_MD5
Object id 1.2.840.113549.2.5 identifying MD5.

OID_SHA

public static final String OID_SHA
Object id 1.3.14.3.2.26 identifying SHA-1.

OID_SHA256

public static final String OID_SHA256
Object id 2.16.840.1.101.3.4.2.1 identifying SHA-256.

OID_SHA224

public static final String OID_SHA224
Object id 2.16.840.1.101.3.4.2.4 identifying SHA-224.

OID_DES_EDE_CBC

public static final String OID_DES_EDE_CBC
Object id 1.2.840.113549.3.7 identifying DES/CBC/PKCS5Padding.

OID_AES_128_CBC

public static final String OID_AES_128_CBC
Object id 2.16.840.1.101.3.4.1.2 identifying AES/CBC/PKCS5Padding (with 128 bit key).

OID_HMAC_SHA1

public static final String OID_HMAC_SHA1
Object id 1.2.840.113549.2.7 identifying HmacSHA1.

OID_NAME_CN

public static final String OID_NAME_CN
Object id 2.5.4.3 for the commonName attribute.

OID_NAME_SN

public static final String OID_NAME_SN
Object id 2.5.4.4 for the surName attribute.

OID_NAME_SERIAL

public static final String OID_NAME_SERIAL
Object id 2.5.4.5 for the serialNumber attribute.

OID_NAME_C

public static final String OID_NAME_C
Object id 2.5.4.6 for the country attribute.

OID_NAME_L

public static final String OID_NAME_L
Object id 2.5.4.7 for the locality attribute.

OID_NAME_ST

public static final String OID_NAME_ST
Object id 2.5.4.8 for the stateOrProvince attribute.

OID_NAME_STREET

public static final String OID_NAME_STREET
Object id 2.5.4.9 for the street attribute.

OID_NAME_O

public static final String OID_NAME_O
Object id 2.5.4.10 for the organization attribute.

OID_NAME_OU

public static final String OID_NAME_OU
Object id 2.5.4.11 for the organizationalUnit attribute.

OID_NAME_T

public static final String OID_NAME_T
Object id 2.5.4.12 for the title attribute.

OID_NAME_EMAIL

public static final String OID_NAME_EMAIL
Object id 1.2.840.113549.1.9.1 for the emailAddress attribute.

TYPE_BOOLEAN

public static final int TYPE_BOOLEAN
Tag for [UNIVERSAL 1], the boolean type.

TYPE_INTEGER

public static final int TYPE_INTEGER
Tag for [UNIVERSAL 2], the integer type.

TYPE_BIT_STRING

public static final int TYPE_BIT_STRING
Tag for [UNIVERSAL 3], the bit string type.

TYPE_OCTET_STRING

public static final int TYPE_OCTET_STRING
Tag for [UNIVERSAL 4], the octet string type.

TYPE_NULL

public static final int TYPE_NULL
Tag for [UNIVERSAL 5], the null type.

TYPE_OBJECT_ID

public static final int TYPE_OBJECT_ID
Tag for [UNIVERSAL 6], the object id type.

TYPE_OBJECT_DESCRIPTOR

public static final int TYPE_OBJECT_DESCRIPTOR
Tag for [UNIVERSAL 7], the object descriptor type.

TYPE_EXTERNAL

public static final int TYPE_EXTERNAL
Tag for [UNIVERSAL 8], the external type.

TYPE_REAL

public static final int TYPE_REAL
Tag for [UNIVERSAL 9], the real type.

TYPE_ENUMERATED

public static final int TYPE_ENUMERATED
Tag for [UNIVERSAL 10], the enumerated type.

TYPE_EMBEDDED_PDV

public static final int TYPE_EMBEDDED_PDV
Tag for [UNIVERSAL 11], the embedded pdv type.

TYPE_UTF8_STRING

public static final int TYPE_UTF8_STRING
Tag for [UNIVERSAL 12], the UTF8 string type.

TYPE_RELATIVE_OID

public static final int TYPE_RELATIVE_OID
Tag for [UNIVERSAL 13], the relative object id type.

TYPE_SEQUENCE

public static final int TYPE_SEQUENCE
Tag for [UNIVERSAL 16], the sequence type.

TYPE_SET

public static final int TYPE_SET
Tag for [UNIVERSAL 17], the set type.

TYPE_NUMERIC_STRING

public static final int TYPE_NUMERIC_STRING
Tag for [UNIVERSAL 18], the numeric string type.

TYPE_PRINTABLE_STRING

public static final int TYPE_PRINTABLE_STRING
Tag for [UNIVERSAL 19], the printable string type.

TYPE_T61_STRING

public static final int TYPE_T61_STRING
Tag for [UNIVERSAL 20], the T61 (teletex) string type.

TYPE_VIDEOTEX_STRING

public static final int TYPE_VIDEOTEX_STRING
Tag for [UNIVERSAL 21], the videotex string type.

TYPE_IA5_STRING

public static final int TYPE_IA5_STRING
Tag for [UNIVERSAL 22], the IA5 string type.

TYPE_UTCTIME

public static final int TYPE_UTCTIME
Tag for [UNIVERSAL 23], the UTC time type.

TYPE_GENERALIZED_TIME

public static final int TYPE_GENERALIZED_TIME
Tag for [UNIVERSAL 24], the generalized time type.

TYPE_GRAPHIC_STRING

public static final int TYPE_GRAPHIC_STRING
Tag for [UNIVERSAL 25], the graphic string type.

TYPE_VISIBLE_STRING

public static final int TYPE_VISIBLE_STRING
Tag for [UNIVERSAL 26], the visible string type.

TYPE_GENERAL_STRING

public static final int TYPE_GENERAL_STRING
Tag for [UNIVERSAL 27], the general string type.

TYPE_UNIVERSAL_STRING

public static final int TYPE_UNIVERSAL_STRING
Tag for [UNIVERSAL 28], the universal string type.

TYPE_CHARACTER_STRING

public static final int TYPE_CHARACTER_STRING
Tag for [UNIVERSAL 29], the character string type.

TYPE_BMP_STRING

public static final int TYPE_BMP_STRING
Tag for [UNIVERSAL 30], the BMP (Unicode) string type.

TAG_UNIVERSAL

public static final int TAG_UNIVERSAL
Mask for tag class universal.

TAG_APPLICATION

public static final int TAG_APPLICATION
Mask for tag class application.

TAG_CONTEXT_SPECIFIC

public static final int TAG_CONTEXT_SPECIFIC
Mask for tag class context specific .

TAG_PRIVATE

public static final int TAG_PRIVATE
Mask for tag class private.

CS0

public static final int CS0
Tag for [0] (context specific 0). Shorthand for ASN1.TAG_CONTEXT_SPECIFIC | 0.

CS1

public static final int CS1
Tag for [1] (context specific 1). Shorthand for ASN1.TAG_CONTEXT_SPECIFIC | 1.

CS2

public static final int CS2
Tag for [2] (context specific 2). Shorthand for ASN1.TAG_CONTEXT_SPECIFIC | 2.

CS3

public static final int CS3
Tag for [3] (context specific 3). Shorthand for ASN1.TAG_CONTEXT_SPECIFIC | 3.
Constructor Detail

ASN1

public ASN1(InputStream in)
     throws IOException

ASN1

public ASN1(InputStream in,
            int saveEncoding,
            boolean expectImplicit)
     throws IOException

ASN1

public ASN1(byte[] data)
     throws IOException

ASN1

public ASN1(byte[] data,
            int saveEncoding,
            boolean expectImplicit)
     throws IOException

ASN1

public ASN1(int type,
            Object value)

ASN1

public ASN1(int type,
            Object value,
            boolean implicit)
Method Detail

getName

public static String getName(String objectId)
Get the algorithm name associated with the given object id. If objectId does not represent an ObjectId or no name is available it is returned unmodified.

getObjectId

public static String getObjectId(String name)
Return the ObjectId associated with the given algorithm name. If no ObjectId is known the name is returned unmodified.

register

public static void register(String objectId,
                            String name)
Register an algorithm name for an ObjectId.
Parameters:
objectId -  
algorithmname -  

isObjectId

public static boolean isObjectId(String s)
Return whether or not the string represents an ObjectId.

clearEncoding

public void clearEncoding(boolean recursive)
Clear the cached encoding. The cached encoding for this object is deleted, saving up memory. If recursive is true and this is a structured type, the encoding will be recursively cleared in all contained types.

getUnusedBits

public int getUnusedBits()
Returns the number of unused bits if the ANS.1 type is a BIT STRING. The number of unused bits defines how many bits of the last byte have to be padded in order to have multiples of 8. These bits are filled with zeroes, for example: 0x80 means that the last 7 bits are unused ( the method returns 7 in this case). 0x10 means that the last 4 bits are unused and so on. Note: If automatic calculation is used all trailling bytes containing 0 are removed, too. Fo example, if you want to encode a byte array that looks like this {3, 78, 142, 6, 0,0,0,0} the last 4 bytes are removed. The encoded bit string contains: {3, 78, 142, 6}, the number of unused bits in the last byte is 1.
Returns:
integer number between 0 and 7

makeAlgorithmId

public static ASN1 makeAlgorithmId(String objectId,
                                   ASN1 params)
Creates a ASN.1 AlgorithmID structure.
 AlgorithmIdentifier  ::=  SEQUENCE  {
      algorithm			OBJECT IDENTIFIER,
      parameters		ANY DEFINED BY algorithm OPTIONAL  }
 
If the passed parameters are null, they are encoded as the NULL type.

makeSignatureAlgId

public static ASN1 makeSignatureAlgId(String objectId,
                                      ASN1 params)
Creates a ASN.1 AlgorithmID structure {@see makeAlgorithmId( String, ASN1 ); } for digital signatures. If no parameters are provided (i.e. params = null), the parameter field is not encoded within the algorithm identifier.
Parameters:
objectId - OID of the algorithm
params - additional algotrithm parameters
Returns:
a ASN1 object containing an AlgorithmID
Since:
3.04

makeString

public static ASN1 makeString(String s)
Make an ASN.1 string type from s suitable for use in a X.500 name. If the string contains any non-ASCII characters a T61 string is created, if it contains an @ character, an IA5 string is created, otherwise a printable string.

makeSequence

public static ASN1 makeSequence(int size)
Make an ASN.1 sequence. Identical to new ASN(ASN1.TYPE_SEQUENCE, new Vector(size)).gvVector(). The exception is that if size is 0, a Vector with the default size is created.

addElement

public void addElement(ASN1 asn1)
Add an element to a structured type. Identical to gvVector().addElement(asn1).

getType

public int getType()
Get the tag of this ASN.1 object.

getTypeOnly

public int getTypeOnly()
Get the type of this ASN.1 object without the tag class. This means [APPLICATION 1], [1], etc. will all return 1. Occasionally useful for context specific tags.

getTagClass

public int getTagClass()
Get the tag class for this object. One of the TAG_* constants defined in this class.

isConstructed

public boolean isConstructed()
Check whether this type uses constructed encoding.

getEncoded

public byte[] getEncoded()
Get the encoding of this object. If the encoding is not cached, it is calculated anew. Note that in that case the encoding is cached for this types a potential subtypes. If you are low on memory you can clear the cached encoding by calling clearEncoding(). If you know it advance that you will later need the encoding of a type you are decoding you can also save it during decoding, see the constructors.

gvObject

public Object gvObject()
Get the value as Object.

gvBigInteger

public BigInteger gvBigInteger()
                        throws IOException
Get the value as BigInteger. If this is not an integer type, an IOException is thrown.

gvInt

public int gvInt()
          throws IOException
Get the value as an int. If this is not an integer type or it does not fit into an int value, an IOException is thrown.

gvByteArray

public byte[] gvByteArray()
                   throws IOException
Get the value as a byte array. If this is not an byte[] type, an IOException is thrown.

gvVector

public Vector gvVector()
                throws IOException
Get the value as a Vector. If this is not an Vector type, an IOException is thrown.

gvDate

public Date gvDate()
            throws IOException
Get the value as a Date. If this is not an Date type, an IOException is thrown.

gvBoolean

public boolean gvBoolean()
                  throws IOException
Get the value as a boolean. If this is not an boolean type, an IOException is thrown.

gvString

public String gvString()
                throws IOException
Get the value as a String. If this is not an String type, an IOException is thrown.

gvObjectId

public String gvObjectId()
                  throws IOException
Get the value as a object id. If this is not an object id type, an IOException is thrown.

gvASN1

public ASN1 gvASN1()
            throws IOException
Get the value as an ASN1 object. If this is not an ASN1 type, an IOException is thrown.

setImplicitTag

public void setImplicitTag(int tag)
                    throws IOException

getElementAt

public ASN1 getElementAt(int n)
                  throws IOException
Get an element of a structured type. If this is not a structured type or the index is invalid an IOException is thrown.

getSize

public int getSize()
            throws IOException
Get the number of elements in this structured type.

gvAlgorithm

public String gvAlgorithm()
                   throws IOException
Get the algorithm value of this AlgorithmID object.

hashCode

public int hashCode()
Get a hashCode for this ASN1 object. It is calculated from the encoding.
Overrides:
hashCode in class Object

equals

public boolean equals(Object obj)
Compare this and another ASN1 object. Two objects are equal iff they have the same encoding.
Overrides:
equals in class Object

setUnusedBits

public void setUnusedBits(int bits)
Sets the number of unused bits for encoding a ASN.1 BIT STRING object. This values defines how many bits of the last byte have to be padded in order to have multiples of 8. The bits are filled with zeroes. The value for bits ranges from -1 to 7. If bits is set to -1 the number of unused bits is calculated automatically based on the provided encoding data. Furthermore, setting bits to -1 implies that all unset bits in the last byte are not used, so for example: 0x80 means that the last 7 bits are unused. 0x10 means that the last 4 bits are unused and so on.
Parameters:
bits - number of unused bits

This Javadoc may contain text parts from IETF Internet Standard specifications, see copyright note) and RSA Data Security Public-Key Cryptography Standards (see copyright note).

IAIK-JCE ME 3.04, (c) 2002 IAIK, (c) 2003 to 2006 Stiftung SIC