XCMS Toolkit

schemajic.encoder
Class DEREncoder

java.lang.Object
  extended by schemajic.codec.Encoder
      extended by schemajic.codec.ASN1Encoder
          extended by schemajic.encoder.DEREncoder
All Implemented Interfaces:
ValueVisitor

public class DEREncoder
extends ASN1Encoder
implements ValueVisitor

Encoder for Distinguished Encoding Rules (DER) of ASN.1.

Tag-Length_Value (TLV) encoding is as follows:


          [T][L][V:[T][L][V:[T][L][V]]]
           1        2        3

          L1 is the total of the encoded lengths
          of T2 + L2 + T3 + L3 + V3.
In order to produce an encoding of this nature, the decoded value's structure must be recursively processed, encoding the tags, calculating the lengths, encoding the lengths, and building a list of encoded TLVs.

When processing of the decoded structure has been completed, the encoding-list is then processed, writing the encoded TLVs to an output stream.

Encoding an ASN.1 SEQUENCE:

 Create a list to hold our EncodableEntry objects.
 
 Create a CompositValue 's' for this sequence and append
 it to the encodable-list.
 
 For each field in the sequence:

      If the field is primitive:
           Construct an AtomicValue f for the field and
           append it to the encodable-list.
 
           Encode the value into a byte[].

           Set f.value to byte[].

           Add f.getTotalLength() to s.

      If the field is a sequence :
           recurse.
 
Processing a list of Encodable objects:
 Encode the first taglist to the stream
   
 Encode the length of the following data, including
 the length of the encoded tag(s) to the stream

      if the next encodable is atomic :
            encode the next encodable to the stream

      if the next encodable is composite :
            encodeComposite()
 

Version:
1.00 - 2006/01/26
Author:
support@phillipgriffin.com

Field Summary
protected  GeneralizedTimeFormat _gtFormat
           
 
Constructor Summary
DEREncoder()
           
 
Method Summary
 void encode(ASN1Value value, java.io.OutputStream out)
          Encode a value according to the Distinguished Encoding Rules to an OutputStream.
 void encodeBitString(byte[] value, int sigBits, java.io.OutputStream out)
           
 void encodeBoolean(boolean value, java.io.OutputStream out)
           
 void encodeEnumerated(int value, java.io.OutputStream out)
           
 void encodeInteger(int value, java.io.OutputStream out)
           
 void encodeObjectIdentifier(int[] value, java.io.OutputStream out)
           
 void encodeOctetString(byte[] value, java.io.OutputStream out)
           
 void encodeRelativeObjectIdentifier(int[] value, java.io.OutputStream out)
           
 java.lang.Object visit(ASN1BitString value, java.lang.Object object)
          From X.690 8.6 Encoding of a bitstring value 8.6.1 The encoding of a bitstring value shall be either primitive or constructed at the option of the sender.
 java.lang.Object visit(ASN1BMPString value, java.lang.Object object)
          An ASN.1 type BMPString visit method with value and object parameters.
 java.lang.Object visit(ASN1Boolean value, java.lang.Object object)
          All bits in the single octet shall be set to 1 X680 - 11.1 ( DER )
 java.lang.Object visit(ASN1Choice value, java.lang.Object object)
          If the Tag of a Choice is EXPLICIT, the tag for the Choice itself must be encoded.
 java.lang.Object visit(ASN1Enumerated value, java.lang.Object object)
          Encode an Enumerated.
 java.lang.Object visit(ASN1GeneralizedTime value, java.lang.Object object)
          GeneralizedTime is encoded as a VisibleString.
 java.lang.Object visit(ASN1GeneralString value, java.lang.Object object)
          An ASN.1 type GeneralString visit method with value and object parameters.
 java.lang.Object visit(ASN1IA5String value, java.lang.Object object)
          An ASN.1 type IA5String visit method with value and object parameters.
 java.lang.Object visit(ASN1Integer value, java.lang.Object object)
          Encode an Integer.
 java.lang.Object visit(ASN1ISO646String value, java.lang.Object object)
          An ASN.1 type ISO646String visit method with value and object parameters.
 java.lang.Object visit(ASN1Null value, java.lang.Object object)
          An ASN.1 type NULL visit method with value and object parameters.
 java.lang.Object visit(ASN1NumericString value, java.lang.Object object)
          An ASN.1 type NumericString visit method with value and object parameters.
 java.lang.Object visit(ASN1ObjectIdentifier value, java.lang.Object object)
          Encode an ObjectIdentifier.
 java.lang.Object visit(ASN1OctetString value, java.lang.Object object)
          Encode an OctetString.
 java.lang.Object visit(ASN1OpenType value, java.lang.Object object)
          If the OpenType contains a decoded ASN.1 value, then encode the contained value, otherwise assume that the user has provided a correctly encoded value and copy that value into the output stream.
 java.lang.Object visit(ASN1PrintableString value, java.lang.Object object)
          An ASN.1 type PrintableString visit method with value and object parameters.
 java.lang.Object visit(ASN1RelativeOID value, java.lang.Object object)
          Encode each subidentifier as 7-bits.
 java.lang.Object visit(ASN1Sequence value, java.lang.Object object)
          Encode each field of the Sequence.
 java.lang.Object visit(ASN1SequenceOf value, java.lang.Object object)
          An ASN.1 SEQUENCE OF type visit method with value and object parameters.
 java.lang.Object visit(ASN1SetOf value, java.lang.Object object)
          An ASN.1 SET OF type visit method with value and object parameters.
 java.lang.Object visit(ASN1T61String value, java.lang.Object object)
          An ASN.1 type T61String visit method with value and object parameters.
 java.lang.Object visit(ASN1TeletexString value, java.lang.Object object)
          An ASN.1 type TeletexString visit method with value and object parameters.
 java.lang.Object visit(ASN1UniversalString value, java.lang.Object object)
          An ASN.1 type UniversalString visit method with value and object parameters.
 java.lang.Object visit(ASN1UTF8String value, java.lang.Object object)
          An ASN.1 type UTF8String visit method with value and object parameters.
 java.lang.Object visit(ASN1VideotexString value, java.lang.Object object)
          An ASN.1 type VideotexString visit method with value and object parameters.
 java.lang.Object visit(ASN1VisibleString value, java.lang.Object object)
          An ASN.1 type VisibleString visit method with value and object parameters.
 java.lang.Object visit(XEROctetString value, java.lang.Object object)
          TODO: description goes here
 
Methods inherited from class schemajic.codec.ASN1Encoder
getCurrentTags, getOutputStream, setCurrentTags, setOutputStream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_gtFormat

protected GeneralizedTimeFormat _gtFormat
Constructor Detail

DEREncoder

public DEREncoder()
Method Detail

encode

public void encode(ASN1Value value,
                   java.io.OutputStream out)
            throws EncodingException
Encode a value according to the Distinguished Encoding Rules to an OutputStream.

Specified by:
encode in class Encoder
Parameters:
value - the value of an ASN.1 type to be encoded
out - target of an encoding transform
Throws:
EncodingException

visit

public java.lang.Object visit(ASN1BitString value,
                              java.lang.Object object)
                       throws VisitorException
From X.690 8.6 Encoding of a bitstring value 8.6.1 The encoding of a bitstring value shall be either primitive or constructed at the option of the sender. NOTE – Where it is necessary to transfer part of a bit string before the entire bitstring is available, the constructed encoding is used. 8.6.2 The contents octets for the primitive encoding shall contain an initial octet followed by zero, one or more subsequent octets. 8.6.2.1 The bits in the bitstring value, commencing with the leading bit and proceeding to the trailing bit, shall be placed in bits 8 to 1 of the first subsequent octet, followed by bits 8 to 1 of the second subsequent octet, followed by bits 8 to 1 of each octet in turn, followed by as many bits as are needed of the final subsequent octet, commencing with bit 8. NOTE – The terms "leading bit" and "trailing bit" are defined in ITU-T Rec. X.680 | ISO/IEC 8824-1, 21.2. 8.6.2.2 The initial octet shall encode, as an unsigned binary integer with bit 1 as the least significant bit, the number of unused bits in the final subsequent octet. The number shall be in the range zero to seven. ISO/IEC 8825-1:2003 (E) ITU-T Rec. X.690 (07/2002) 9 8.6.2.3 If the bitstring is empty, there shall be no subsequent octets, and the initial octet shall be zero. DER 11.2 Unused bits 11.2.1 Each unused bit in the final octet of the encoding of a bit string value shall be set to zero. 11.2.2 Where ITU-T Rec. X.680 | ISO/IEC 8824-1, 21.7, applies, the bitstring shall have all trailing 0 bits removed before it is encoded. NOTE 1 – In the case where a size constraint has been applied, the abstract value delivered by a decoder to the application will be one of those satisfying the size constraint and differing from the transmitted value only in the number of trailing 0 bits. NOTE 2 – If a bitstring value has no 1 bits, then an encoder shall encode the value with a length of 1 and an initial octet set to 0.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type BIT STRING in its encoded form
object - visit an object of ASN.1 type BIT STRING
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1Boolean value,
                              java.lang.Object object)
                       throws VisitorException
All bits in the single octet shall be set to 1 X680 - 11.1 ( DER )

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of an ASN.1 type BOOLEAN in its encoded form
object - visit an object of ASN.1 type BOOLEAN
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1Choice value,
                              java.lang.Object object)
                       throws VisitorException
If the Tag of a Choice is EXPLICIT, the tag for the Choice itself must be encoded. If the Tag of a Choice is CONTEXT-SPECIFIC, then the Tag and length of the encoding of the chosen value should be encoded.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of some ASN.1 CHOICE type in its encoded form
object - visit an object of the ASN.1 CHOICE type
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1Enumerated value,
                              java.lang.Object object)
                       throws VisitorException
Encode an Enumerated.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type ENUMERATED in its encoded form
object - visit an object of ASN.1 type ENUMERATED
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1GeneralizedTime value,
                              java.lang.Object object)
                       throws VisitorException
GeneralizedTime is encoded as a VisibleString. We write the single-byte characters of a String returned by GeneralizedTimeFormat.format() to the output stream.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type GeneralizedTime in its encoded form
object - visit an object of ASN.1 type GeneralizedTime
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1Integer value,
                              java.lang.Object object)
                       throws VisitorException
Encode an Integer.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type INTEGER in its encoded form
object - visit an object of ASN.1 type INTEGER
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1Null value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type NULL visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type NULL in its encoded form
object - visit an object of ASN.1 type NULL
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1ObjectIdentifier value,
                              java.lang.Object object)
                       throws VisitorException
Encode an ObjectIdentifier.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type OBJECT IDENTIFIER in its encoded form
object - visit an object of ASN.1 type OBJECT IDENTIFIER
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1OctetString value,
                              java.lang.Object object)
                       throws VisitorException
Encode an OctetString.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type OCTET STRING in its encoded form
object - visit an object of ASN.1 type OCTET STRING
Throws:
VisitorException

visit

public java.lang.Object visit(XEROctetString value,
                              java.lang.Object object)
                       throws VisitorException
TODO: description goes here

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type OCTET STRING in its XER encoded form
object - visit an object of ASN.1 type OCTET STRING
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1RelativeOID value,
                              java.lang.Object object)
                       throws VisitorException
Encode each subidentifier as 7-bits. If a subidentifer has a 1 in bit 8, that bit must be moved to bit 7 of the next byte. Each byte in the encoding except the last will have a 1 in bit 8.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type RELATIVE_OID in its encoded form
object - visit an object of ASN.1 type RELATIVE_OID
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1Sequence value,
                              java.lang.Object object)
                       throws VisitorException
Encode each field of the Sequence. get the type of this value get the tag for this value for each field in value : get the tags for this field if field is atomic : length = return encodeAtomicValue( tags, field ) if field is composite : length = encodeCompositeValue( tags, field ) if tags == null : # This is a top-level value. push Length(length) onto tlvStack add length(Length) to length push Tag(tag) onto tlvStack add length(Tag) to length return length

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type SEQUENCE in its encoded form
object - visit an object of ASN.1 type SEQUENCE
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1SequenceOf value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 SEQUENCE OF type visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type SEQUENCE OF in its encoded form
object - visit an object of ASN.1 type SEQUENCE OF
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1SetOf value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 SET OF type visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type SET OF in its encoded form
object - visit an object of ASN.1 type SET OF
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1OpenType value,
                              java.lang.Object object)
                       throws VisitorException
If the OpenType contains a decoded ASN.1 value, then encode the contained value, otherwise assume that the user has provided a correctly encoded value and copy that value into the output stream.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of an ASN.1 type in its encoded form (an open type)
object - visit an object of the ASN.1 open type
Throws:
VisitorException

encodeBoolean

public void encodeBoolean(boolean value,
                          java.io.OutputStream out)
                   throws EncodingException
Throws:
EncodingException

encodeBitString

public void encodeBitString(byte[] value,
                            int sigBits,
                            java.io.OutputStream out)
                     throws EncodingException
Throws:
EncodingException

encodeEnumerated

public void encodeEnumerated(int value,
                             java.io.OutputStream out)
                      throws EncodingException
Throws:
EncodingException

encodeInteger

public void encodeInteger(int value,
                          java.io.OutputStream out)
                   throws EncodingException
Throws:
EncodingException

encodeObjectIdentifier

public void encodeObjectIdentifier(int[] value,
                                   java.io.OutputStream out)
                            throws EncodingException
Throws:
EncodingException

encodeOctetString

public void encodeOctetString(byte[] value,
                              java.io.OutputStream out)
                       throws EncodingException
Throws:
EncodingException

encodeRelativeObjectIdentifier

public void encodeRelativeObjectIdentifier(int[] value,
                                           java.io.OutputStream out)
                                    throws EncodingException
Throws:
EncodingException

visit

public java.lang.Object visit(ASN1VisibleString value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type VisibleString visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type VisibleString in its encoded form
object - visit an object of ASN.1 type VisibleString
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1BMPString value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type BMPString visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type BMPString in its encoded form
object - visit an object of ASN.1 type BMPString
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1GeneralString value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type GeneralString visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type GeneralString in its encoded form
object - visit an object of ASN.1 type GeneralString
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1IA5String value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type IA5String visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type IA5String in its encoded form
object - visit an object of ASN.1 type IA5String
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1ISO646String value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type ISO646String visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type ISO646String in its encoded form
object - visit an object of ASN.1 type ISO646String
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1NumericString value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type NumericString visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type NumericString in its encoded form
object - visit an object of ASN.1 type NumericString
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1PrintableString value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type PrintableString visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type PrintableString in its encoded form
object - visit an object of ASN.1 type PrintableString
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1T61String value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type T61String visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type T61String in its encoded form
object - visit an object of ASN.1 type T61String
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1TeletexString value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type TeletexString visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type TeletexString in its encoded form
object - visit an object of ASN.1 type TeletexString
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1UniversalString value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type UniversalString visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type UniversalString in its encoded form
object - visit an object of ASN.1 type UniversalString
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1UTF8String value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type UTF8String visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type UTF8String in its encoded form
object - visit an object of ASN.1 type UTF8String
Throws:
VisitorException

visit

public java.lang.Object visit(ASN1VideotexString value,
                              java.lang.Object object)
                       throws VisitorException
Description copied from interface: ValueVisitor
An ASN.1 type VideotexString visit method with value and object parameters.

Specified by:
visit in interface ValueVisitor
Parameters:
value - a value of ASN.1 type VideotexString in its encoded form
object - visit an object of ASN.1 type VideotexString
Throws:
VisitorException

XCMS Toolkit


Copyright © 2005-2006 GRIFFIN Consulting. All Rights Reserved.