⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 asn1object.java

📁 JAVA开源LDAP浏览器jxplorer的源码!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

package com.ca.commons.security.asn1;


import com.ca.commons.cbutil.CBParse;

import java.util.Vector;
import java.util.Hashtable;
import java.io.*;

/**
 * This class is an abstract representation of ASN.1 types. It is
 * the superclass of all classes representing actual ASN.1 types.
 * It also defines necessary interfaces for its subclasses. For more
 * information about ASN.1, please refer to:
 * <pre>CCITT. Recommendation X.208: Specification of Abstract Syntax
 * Notation One (ASN.1). 1988</pre>
 * <P> ASN.1 types are encoded using Basic Encoding Rules (BER) and
 * Distinguished Encoding Rules (DER). For more information about
 * these rules, please refer to:
 * <pre>CCITT. Recommendation X.209: Specification of Basic Encoding
 * Rules for Abstract Notation One (ASN.1). 1988</pre>
 * <P> To create an ASN1Object of a specific ASN.1 type, the static
 * method create() is called which takes an ASN1Type object as the
 * parameter and returns a suitable subclass of ASN1Object.
 * <P> This class provides a set of methods which converts ASN1Objects
 * to/from DER encoded byte arrays, Privacy Enhanced Mail (PEM) formatted
 * files and character strings, etc. It also has a set of methods which
 * converts ASN1Object to/from encrypted form. For more information about
 * PEM format and encryption, please refer to com.ca.commons.security.pkcs5.Envelope.
 * Ref: http://www.rsa.com/rsalabs/pubs/PKCS/
 *
 * @author who
 */
public class ASN1Object implements java.io.Serializable
{

    /** The ASN.1 type of the ASN1Object. */
    protected ASN1Type asn1Type;

    /** DER encoded byte array representation of the ASN1Object. */
    protected byte [] byteArray;

    /** The mapping table of ASN.1 type and implementation class. */
    private static Hashtable asn1ToJava = new Hashtable(27);

    static{
        Class p = (new Primitive()).getClass();
        register(ASN1Type.BOOLEAN, p);
        register(ASN1Type.INTEGER, p);
        register(ASN1Type.OCTET_STRING, p);
        register(ASN1Type.NULL, p);
        register(ASN1Type.OBJECT_ID, p);
        register(ASN1Type.BIT_STRING, p);
        register(ASN1Type.IA5String, p);
        register(ASN1Type.T61String, p);
        register(ASN1Type.PrintableString, p);
        register(ASN1Type.UTCTime, p);

        register(ASN1Type.GENERALIZEDTIME,p);
        register(ASN1Type.ENUMERATED,p);
        register(ASN1Type.UniversalString, p);
        register(ASN1Type.BMPString, p);

        Class s = (new Sequence()).getClass();
        register(ASN1Type.SEQUENCE, s);
        register(ASN1Type.SET, s);

        Class c = new Context().getClass();
        register(ASN1Type.ContextSpecific, c);
    }

    /**
     * Constructs an empty ASN1Object.
     */
    public ASN1Object()
    {}

    /**
     * Initializes the ASN1Object with a specific ASN1Type.
     */
    protected void init(ASN1Type type)
    {
        asn1Type = type;
        byteArray = null;
    }

    /**
     * Gets the ASN1Type of the ASN1Object.
     */
    public ASN1Type getASN1Type()
    {
        return asn1Type;
    }

    /**
     * Gets the DER encoded byte array of the ASN1Object.
     */
    public byte [] getByteArray()
    {
        return byteArray;
    }

    /**
     * Sets the DER encoded byte array of the ASN1Object.
     * @param	b the DER encoded byte array.
     */
    void setByteArray(byte [] b)
    {
        byteArray = b;
    }

    /**
     * Checks the type of this ASN1Object against the given ASN1Type.
     */
    public boolean isASN1Type(ASN1Type type)
    {
        return asn1Type.equals(type);
    }

    /**
     * Compares the 'len' byte sequences of 2 arrays, x and y starting at
     * offsets 'xoff' and 'yoff' respectively.
     */
    public boolean compByteArray(byte [] x, int xoff, byte [] y,
                                 int yoff, int len)
    {
        if (len <= 0 || x == null || xoff < 0 || x.length < xoff + len
                || y == null || yoff < 0 || y.length < yoff + len)
        {
            return false;
        }
        for (int i = 0; i < len; i++)
        {
            if (x[xoff+i] != y[yoff+i])
            {
                return false;
            }
        }
        return true;
    }

    /**
     * Compares byte sequences of 2 arrays, x and y.
     */
    public boolean compByteArray(byte [] x, byte [] y)
    {
        if (x == null || y == null || x.length != y.length)
        {
            return false;
        }
        return compByteArray(x, 0, y, 0, x.length);
    }

    /**
     * Returns whether two ASN1Objects are equal.
     */
    public boolean equals(Object o)
    {
        if (!(o instanceof com.ca.commons.security.asn1.ASN1Object))
        {
            return false;
        }
        try
        {
            byte [] der = ASN1Util.toByteArrayDER(this);
            byte [] der1 = ASN1Util.toByteArrayDER((ASN1Object) o);
            return compByteArray(der, der1);
        }
        catch(ASN1Exception asn1e)
        {
            asn1e.printStackTrace(System.out);
            return false;
        }
    }

    /**
     * Returns a string representation of the ASN1Object.
     */
    public String toString()
    {
        return asn1Type.toString();
    }


    /* interface for primitive and context specific ASN.1 types */

    private Object o = null;

    /**
     * Returns the actual value of the ASN1Object. The ASN1Object cannot
     * be of a constructed ASN.1 type.
     * Subclasses should overwrite this method.
     */
    public Object getValue()
    {
        return o;
        // throw new IllegalArgumentException("method not supported");
    }

    /**
     * Sets the value of the ASN1Object. The ASN1Object cannot be of a
     * constructed ASN.1 type.
     * Subclass should overwrite this method.
     */
    public void setValue(Object o)
    {
        this.o = o;
        // throw new IllegalArgumentException("method not supported");
    }


    /* interface for composite ASN.1 classes */

    Vector comps = new Vector();

    /**
     * Adds a component to the ASN1Object. The ASN1Object must be of a
     * constructed ASN.1 type.
     * Subclass should overwrite this method.
     */
    public void addComponent(ASN1Object o)
    {
        comps.addElement(o);
        // throw new IllegalArgumentException("method not supported");
    }

    /**
     * Adds a component to the ASN1Object at a given index. The ASN1Object
     * must be of a constructed ASN.1 type.
     * Subclass should overwrite this method.
     */
    public void addComponent(ASN1Object o, int index)
    {
        throw new IllegalArgumentException("method not supported");
    }

    /**
     * Gets a component from the ASN1Object. The ASN1Object must be of a
     * constructed ASN.1 type to allow this operation.
     * Subclass should overwrite this method.
     */
    public ASN1Object getComponent()
    {
        throw new IllegalArgumentException("method not supported");
    }

    /**
     * Gets a component from the ASN1Object at the given index. The
     * ASN1Object must be of a constructed ASN.1 type.
     * Subclass should overwrite this method.
     */
    public ASN1Object getComponent(int index)
    {
        return (ASN1Object) comps.elementAt(index);
        // throw new IllegalArgumentException("method not supported");
    }

    /**
     * Returns the number of components in the ASN1Object. The ASN1Object
     * must be of a constructed ASN.1 type.
     * Subclass should overwrite this method.
     */
    public int size()
    {
        return comps.size();
        // throw new IllegalArgumentException("method not supported");
    }


    /* interface for context specific ASN.1 class */

    /**
     * Returns whether the ASN1Object is using implicit ASN.1 tagging.
     * The ASN1Object must be of the context specific ASN.1 type.
     * Subclass should overwrite this method.
     */
    public boolean implicit()
    {
        throw new IllegalArgumentException("method not supported");
    }

    /**
     * Returns the context specific tag of the ASN1Object. The ASN1Object
     * must be of the context specific ASN.1 type.
     * Subclass should overwrite this method.
     */
    public int getTag()
    {
        if (asn1Type != null)
            return asn1Type.getTag();
        else
            throw new IllegalArgumentException("Object not initialised, does not have a type");
    }


    /* class methods */

    /**
     * Registers the implementation class of the ASN1Type.
     */
    private static void register(ASN1Type type, Class c)
    {
        asn1ToJava.put(type, c);
    }

    /**
     * This is the main method of creating an ASN1Object of the given
     * ASN1Type, without assigning it any initial value.
     * @exception	com.ca.commons.security.asn1.ASN1Exception if no implementation
     * class of the ASN1Type is available or cannot create an instance of
     * the implementation class.
     */
    public static ASN1Object create(ASN1Type type)
    throws ASN1Exception
    {
        Class impl = null;
        try
        {
            ASN1Object o;
            impl  = (Class) asn1ToJava.get(type);
            if (impl == null)
            {
                throw new ASN1Exception(type.toString()+
                                        " : no implementation class available");
            }
            o = (ASN1Object) impl.newInstance();
            o.init(type);
            return o;
        }
        catch (InstantiationException e)
        {
            throw new ASN1Exception("Cannot create instance for " +

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -