codec.asn1
Class ASN1

java.lang.Object
  |
  +--codec.asn1.ASN1

public final class ASN1
extends Object

Defines various constants used with ASN.1 such as the tag and type class identifiers. The classes in this package are modelled along the lines of ITU-T Recommendations X.680, X.681, X.682, X.690, and X.691. From now on we assume the reader is familiar with ASN.1, BER, and DER.

This package defines a number of primitive types as specified by the basic syntax in X.680. Based on these primitive types more complex types can be created. We refer to these types as compound types or structures. Below, we discuss how such types are constructed, encoded and decoded using the classes in this package.

For instance the type PrivateKeyInfo is defined in PKCS#8 as follows using ASN.1:

 PrivateKeyInfo ::= SEQUENCE (
   version Version,
   privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
   privateKey PrivateKey,
   attributes [0] IMPLICIT Attributes OPTIONAL
 }
 Version ::= INTEGER
 PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
 PrivateKey ::= OCTET STRING
 Attributes ::= SET OF Attribute
 
This type can be created as follows based on the classes in this package:
 public class PrivateKeyInfo extends ASN1Sequence
  {
    public PrivateKeyInfo()
     {
       add(new ASN1Integer());
       add(new AlgorithmIdentifier()); // Detailed below
       add(new ASN1OctetString());
       add(new ASN1TaggedType(
         0, new ASN1SetOf(Attribute.class), false, true);
     }
    ...
  }
 
The tagged type allows to define types of the ASN.1 tag class UNIVERSAL, CONTEXT SPECIFIC, APPLICATION, and PRIVATE. The constructor shown above is a convenience constructor that assumes the class is CONTEXT SPECIFIC. The third parameter specifies that the tagging is not EXPLICIT (hence IMPLICIT as required by the definition above), and the fourth parameter specifies that attributes is OPTIONAL.

The interface ASN1Type specifies a number of methods to declare types as OPTIONAL, IMPLICIT or EXPLICIT. In principle, ASN.1 structures can be modelled almost one to one using the classes in this package.

Individual types also offer setter methods and constructors that allow to preset certain values as the values of the ASN.1 types from native Java types such as String, int, byte[] etc.

Once, a primitive type or a compound type x has been defined and initialized it can be encoded in a number of ways. The first step is to choose an encoder. One example encoder is the DEREncoder. This encoder encodes types according to the Distinguished Encoding Rules, a subset of the Basic Encoding Rules defined in the ITU-T Recommendations. Encoding to a file is simple, the following code snippet shows how to do it:

 DEREncoder enc;
 enc = new DEREncoder(new FileOutputStream("code.der"));
 enc.writeType(x);
 enc.close();
 
There is nothing more to it. However, only types not declared as OPTIONAL are written to the stream. Hence, the type attributes mentioned above is not written.

Why then is it declared as OPTIONAL? Answer, by convention in this package (and by reason) the default constructor is meant to initialize a type for decoding. In a code the attributes type can be encountered. The decoder clears the OPTIONAL flag of types it encounters during decoding so it is clear which types were present and which were not. If a type is only used for encoding and not for decoding then the OPTIONAL types not required can be omitted.

 AlgorithmIdentifier  ::= SEQUENCE{
   algorithm  OBJECT IDENTIFIER,
   parameters ANY DEFINED BY algorithm OPTIONAL
 }
 

Version:
"$Id: ASN1.java,v 1.4 2005/03/22 16:04:37 flautens Exp $"
Author:
Volker Roth

Field Summary
static int CLASS_APPLICATION
          DOCUMENT ME!
static int CLASS_CONTEXT
          DOCUMENT ME!
static int CLASS_MASK
          DOCUMENT ME!
static int CLASS_PRIVATE
          DOCUMENT ME!
static int CLASS_UNIVERSAL
          DOCUMENT ME!
static int CONSTRUCTED
          DOCUMENT ME!
static int LENGTH_LONGFORM
          DOCUMENT ME!
static int LENGTH_MASK
          DOCUMENT ME!
static int PRIMITIVE
          DOCUMENT ME!
static int TAG_BITSTRING
          DOCUMENT ME!
static int TAG_BMPSTRING
          DOCUMENT ME!
static int TAG_BOOLEAN
          DOCUMENT ME!
static int TAG_ENUMERATED
          DOCUMENT ME!
static int TAG_EOC
          DOCUMENT ME!
static int TAG_GENERALIZEDTIME
          DOCUMENT ME!
static int TAG_GENERALSTRING
          DOCUMENT ME!
static int TAG_GRAPHICSTRING
          DOCUMENT ME!
static int TAG_IA5STRING
          DOCUMENT ME!
static int TAG_INTEGER
          DOCUMENT ME!
static int TAG_LONGFORM
          DOCUMENT ME!
static int TAG_MASK
          DOCUMENT ME!
static int TAG_NULL
          DOCUMENT ME!
static int TAG_NUMERICSTRING
          DOCUMENT ME!
static int TAG_OCTETSTRING
          DOCUMENT ME!
static int TAG_OID
          DOCUMENT ME!
static int TAG_PRINTABLESTRING
          DOCUMENT ME!
static int TAG_REAL
          DOCUMENT ME!
static int TAG_SEQUENCE
          DOCUMENT ME!
static int TAG_SET
          DOCUMENT ME!
static int TAG_T61STRING
          DOCUMENT ME!
static int TAG_UNIVERSALSTRING
          DOCUMENT ME!
static int TAG_UTCTIME
          DOCUMENT ME!
static int TAG_UTF8STRING
          DOCUMENT ME!
static int TAG_VIDEOTEXTSTRING
          DOCUMENT ME!
static int TAG_VISIBLESTRING
          DOCUMENT ME!
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

TAG_EOC

public static final int TAG_EOC
DOCUMENT ME!

TAG_BOOLEAN

public static final int TAG_BOOLEAN
DOCUMENT ME!

TAG_INTEGER

public static final int TAG_INTEGER
DOCUMENT ME!

TAG_BITSTRING

public static final int TAG_BITSTRING
DOCUMENT ME!

TAG_OCTETSTRING

public static final int TAG_OCTETSTRING
DOCUMENT ME!

TAG_NULL

public static final int TAG_NULL
DOCUMENT ME!

TAG_OID

public static final int TAG_OID
DOCUMENT ME!

TAG_REAL

public static final int TAG_REAL
DOCUMENT ME!

TAG_ENUMERATED

public static final int TAG_ENUMERATED
DOCUMENT ME!

TAG_UTF8STRING

public static final int TAG_UTF8STRING
DOCUMENT ME!

TAG_SEQUENCE

public static final int TAG_SEQUENCE
DOCUMENT ME!

TAG_SET

public static final int TAG_SET
DOCUMENT ME!

TAG_NUMERICSTRING

public static final int TAG_NUMERICSTRING
DOCUMENT ME!

TAG_PRINTABLESTRING

public static final int TAG_PRINTABLESTRING
DOCUMENT ME!

TAG_T61STRING

public static final int TAG_T61STRING
DOCUMENT ME!

TAG_VIDEOTEXTSTRING

public static final int TAG_VIDEOTEXTSTRING
DOCUMENT ME!

TAG_IA5STRING

public static final int TAG_IA5STRING
DOCUMENT ME!

TAG_UTCTIME

public static final int TAG_UTCTIME
DOCUMENT ME!

TAG_GENERALIZEDTIME

public static final int TAG_GENERALIZEDTIME
DOCUMENT ME!

TAG_GRAPHICSTRING

public static final int TAG_GRAPHICSTRING
DOCUMENT ME!

TAG_VISIBLESTRING

public static final int TAG_VISIBLESTRING
DOCUMENT ME!

TAG_GENERALSTRING

public static final int TAG_GENERALSTRING
DOCUMENT ME!

TAG_UNIVERSALSTRING

public static final int TAG_UNIVERSALSTRING
DOCUMENT ME!

TAG_BMPSTRING

public static final int TAG_BMPSTRING
DOCUMENT ME!

TAG_MASK

public static final int TAG_MASK
DOCUMENT ME!

TAG_LONGFORM

public static final int TAG_LONGFORM
DOCUMENT ME!

CLASS_UNIVERSAL

public static final int CLASS_UNIVERSAL
DOCUMENT ME!

CLASS_APPLICATION

public static final int CLASS_APPLICATION
DOCUMENT ME!

CLASS_CONTEXT

public static final int CLASS_CONTEXT
DOCUMENT ME!

CLASS_PRIVATE

public static final int CLASS_PRIVATE
DOCUMENT ME!

CLASS_MASK

public static final int CLASS_MASK
DOCUMENT ME!

PRIMITIVE

public static final int PRIMITIVE
DOCUMENT ME!

CONSTRUCTED

public static final int CONSTRUCTED
DOCUMENT ME!

LENGTH_LONGFORM

public static final int LENGTH_LONGFORM
DOCUMENT ME!

LENGTH_MASK

public static final int LENGTH_MASK
DOCUMENT ME!


Copyright © Fraunhofer Gesellschaft. All Rights Reserved.