dervalue.java

来自「This is a resource based on j2me embedde」· Java 代码 · 共 933 行 · 第 1/2 页

JAVA
933
字号
            return b;        }        byte[] ret = new byte[a.length + b.length];        System.arraycopy(a, 0, ret, 0, a.length);        System.arraycopy(b, 0, ret, a.length, b.length);        return ret;    }    /**     * Returns an ASN.1 OCTET STRING     *     * @return the octet string held in this DER value     */    public byte[] getOctetString() throws IOException {        byte[] bytes;        if (tag != tag_OctetString && !isConstructed(tag_OctetString)) {            throw new IOException(                "DerValue.getOctetString, not an Octet String: " + tag);        }        bytes = new byte[length];        if (buffer.read(bytes) != length) {            throw new IOException("short read on DerValue buffer");        }        if (isConstructed()) {            DerInputStream in = new DerInputStream(bytes);            bytes = null;            while (in.available() != 0) {                bytes = append(bytes, in.getOctetString());            }        }        return bytes;    }    /**     * Returns an ASN.1 INTEGER value as an integer.     *     * @return the integer held in this DER value.     */    public int getInteger() throws IOException {        if (tag != tag_Integer) {            throw new IOException("DerValue.getInteger, not an int " + tag);        }        return buffer.getInteger(data.available());    }    /**     * Returns an ASN.1 INTEGER value as a BigInteger.     *     * @return the integer held in this DER value as a BigInteger.     */    public BigInteger getBigInteger() throws IOException {        if (tag != tag_Integer) {            throw new IOException("DerValue.getBigInteger, not an int " + tag);        }        return buffer.getBigInteger(data.available(), false);    }    /**     * Returns an ASN.1 INTEGER value as a positive BigInteger.     * This is just to deal with implementations that incorrectly encode     * some values as negative.     *     * @return the integer held in this DER value as a BigInteger.     */    public BigInteger getPositiveBigInteger() throws IOException {        if (tag != tag_Integer) {            throw new IOException("DerValue.getBigInteger, not an int " + tag);        }        return buffer.getBigInteger(data.available(), true);    }        /**     * Returns an ASN.1 ENUMERATED value.     *     * @return the integer held in this DER value.     */    public int getEnumerated() throws IOException {        if (tag != tag_Enumerated) {            throw new IOException("DerValue.getEnumerated, incorrect tag: "                                  + tag);        }        return buffer.getInteger(data.available());    }    /**     * Returns an ASN.1 BIT STRING value.  The bit string must be byte-aligned.     *     * @return the bit string held in this value     */    public byte[] getBitString() throws IOException {        if (tag != tag_BitString) {            throw new IOException(                "DerValue.getBitString, not a bit string " + tag);        }        return buffer.getBitString();    }    /**     * Returns the name component as a Java string, regardless of its     * encoding restrictions (ASCII, T61, Printable, IA5, BMP, UTF8).     */    public String getAsString() throws IOException {        if (tag == tag_UTF8String) {            return getUTF8String();        } else if (tag == tag_PrintableString) {            return getPrintableString();        } else if (tag == tag_T61String) {            return getT61String();        } else if (tag == tag_IA5String) {            return getIA5String();        }        /*          IMPL_NOTE: Need encoder for UniversalString before it can be handled.          else if (tag == tag_UniversalString)          return getUniversalString();        */        else if (tag == tag_BMPString) {            return getBMPString();        } else if (tag == tag_GeneralString) {            return getGeneralString();        } else {            return null;        }    }    /**     * Returns an ASN.1 BIT STRING value, with the tag assumed implicit     * based on the parameter.  The bit string must be byte-aligned.     *     * @params tagImplicit if true, the tag is assumed implicit.     * @return the bit string held in this value     */    public byte[] getBitString(boolean tagImplicit) throws IOException {        if (!tagImplicit) {            if (tag != tag_BitString) {                throw new IOException("DerValue.getBitString, not a bit string "                                       + tag);            }        }        return buffer.getBitString();    }    /**     * Helper routine to return all the bytes contained in the     * DerInputStream associated with this object.     */    public byte[] getDataBytes() throws IOException {        byte[] retVal = new byte[length];        synchronized (data) {            data.reset();            data.getBytes(retVal);        }        return retVal;    }    /**     * Returns an ASN.1 STRING value     *     * @return the printable string held in this value     */    public String getPrintableString()            throws IOException {        if (tag != tag_PrintableString) {            throw new IOException(                "DerValue.getPrintableString, not a string " + tag);        }        return new String(getDataBytes(), "US_ASCII");    }    /**     * Returns an ASN.1 T61 (Teletype) STRING value     *     * @return the teletype string held in this value     */    public String getT61String() throws IOException {        if (tag != tag_T61String) {            throw new IOException(                "DerValue.getT61String, not T61 " + tag);        }        return new String(getDataBytes(), "ISO-8859-1");    }    /**     * Returns an ASN.1 IA5 (ASCII) STRING value     *     * @return the ASCII string held in this value     */    public String getIA5String() throws IOException {        if (tag != tag_IA5String) {            throw new IOException(                "DerValue.getIA5String, not IA5 " + tag);        }        return new String(getDataBytes(), "US_ASCII");    }    /**     * Returns the ASN.1 BMP (Unicode) STRING value as a Java string.     *     * @return a string corresponding to the encoded BMPString held in     * this value     */    public String getBMPString() throws IOException {        if (tag != tag_BMPString) {            throw new IOException(                "DerValue.getBMPString, not BMP " + tag);        }        // BMPString is the same as Unicode in big endian, unmarked        // format.        return new String(getDataBytes(), "UnicodeBigUnmarked");    }    /**     * Returns the ASN.1 UTF-8 STRING value as a Java String.     *     * @return a string corresponding to the encoded UTF8String held in     * this value     */    public String getUTF8String() throws IOException {        if (tag != tag_UTF8String) {            throw new IOException(                "DerValue.getUTF8String, not UTF-8 " + tag);        }        return new String(getDataBytes(), "UTF-8");    }    /**     * Returns the ASN.1 GENERAL STRING value as a Java String.     *     * @return a string corresponding to the encoded GeneralString held in     * this value     */    public String getGeneralString() throws IOException {        if (tag != tag_GeneralString) {            throw new IOException(                "DerValue.getGeneralString, not GeneralString " + tag);        }        return new String(getDataBytes(), "US_ASCII");    }    /**     * Returns a Date if the DerValue is UtcTime.     *     * @return the Date held in this DER value     */    public Date getUTCTime() throws IOException {        if (tag != tag_UtcTime) {            throw new IOException("DerValue.getUTCTime, not a UtcTime: " + tag);        }        return buffer.getUTCTime(data.available());    }    /**     * Returns a Date if the DerValue is GeneralizedTime.     *     * @return the Date held in this DER value     */    public Date getGeneralizedTime() throws IOException {        if (tag != tag_GeneralizedTime) {            throw new IOException(                "DerValue.getGeneralizedTime, not a GeneralizedTime: " + tag);        }        return buffer.getGeneralizedTime(data.available());    }    /**     * Returns true iff the other object is a DER value which     * is bitwise equal to this one.     *     * @param other the object being compared with this one     */    public boolean equals(Object other) {        if (other instanceof DerValue) {            return equals((DerValue)other);        } else {            return false;        }    }    /**     * Bitwise equality comparison.  DER encoded values have a single     * encoding, so that bitwise equality of the encoded values is an     * efficient way to establish equivalence of the unencoded values.     *     * @param other the object being compared with this one     */    public boolean equals(DerValue other) {        if (this == other) {            return true;        }        if (tag != other.tag) {            return false;        }        if (data == other.data) {            return true;        }        // make sure the order of lock is always consistent to avoid a deadlock        return (System.identityHashCode(this.data)                > System.identityHashCode(other.data)) ?                doEquals(this, other):                doEquals(other, this);    }    /**     * Helper for public method equals()     */    private static boolean doEquals(DerValue d1, DerValue d2) {        synchronized (d1.data) {            synchronized (d2.data) {                d1.data.reset();                d2.data.reset();                return d1.buffer.equals(d2.buffer);            }        }    }    /**     * Returns a printable representation of the value.     *     * @return printable representation of the value     */    public String toString() {        try {            String str = getAsString();            if (str != null) {                return "\"" + str + "\"";            }            if (tag == tag_Null) {                return "[DerValue, null]";            }            if (tag == tag_ObjectId) {                return "OID." + getOID();            }            // integers            else {                return "[DerValue, tag = " + tag                        + ", length = " + length + "]";            }        } catch (IOException e) {            throw new IllegalArgumentException("misformatted DER value");        }    }    /**     * Returns a DER-encoded value, such that if it's passed to the     * DerValue constructor, a value equivalent to "this" is returned.     *     * @return DER-encoded value, including tag and length.     */    public byte[] toByteArray() throws IOException {        DerOutputStream out = new DerOutputStream();        encode(out);        data.reset();        return out.toByteArray();    }    /**     * For "set" and "sequence" types, this function may be used     * to return a DER stream of the members of the set or sequence.     * This operation is not supported for primitive types such as     * integers or bit strings.     */    public DerInputStream toDerInputStream() throws IOException {        if (tag == tag_Sequence || tag == tag_Set) {            return new DerInputStream(buffer);        }        throw new IOException("toDerInputStream rejects tag type " + tag);    }    /**     * Get the length of the encoded value.     */    public int length() {        return length;    }    /**     * Determine if a character is one of the permissible characters for     * PrintableString:     * A-Z, a-z, 0-9, space, apostrophe (39), left and right parentheses,     * plus sign, comma, hyphen, period, slash, colon, equals sign,     * and question mark.     *     * Characters that are *not* allowed in PrintableString include     * exclamation point, quotation mark, number sign, dollar sign,     * percent sign, ampersand, asterisk, semicolon, less than sign,     * greater than sign, at sign, left and right square brackets,     * backslash, circumflex (94), underscore, back quote (96),     * left and right curly brackets, vertical line, tilde,     * and the control codes (0-31 and 127).     *     * This list is based on X.680 (the ASN.1 spec).     */    public static boolean isPrintableStringChar(char ch) {        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||            (ch >= '0' && ch <= '9')) {            return true;        } else {            switch (ch) {                case ' ':       /* space */                case '\'':      /* apostrophe */                case '(':       /* left paren */                case ')':       /* right paren */                case '+':       /* plus */                case ',':       /* comma */                case '-':       /* hyphen */                case '.':       /* period */                case '/':       /* slash */                case ':':       /* colon */                case '=':       /* equals */                case '?':       /* question mark */                    return true;                default:                    return false;            }        }    }    /**     * Create the tag of the attribute.     *     * @params class the tag class type, one of UNIVERSAL, CONTEXT,     *               APPLICATION or PRIVATE     * @params form if true, the value is constructed, otherwise it     * is primitive.     * @params val the tag value     */    public static byte createTag(byte tagClass, boolean form, byte val) {        byte tag = (byte)(tagClass | val);        if (form) {            tag |= (byte)0x20;        }        return (tag);    }    /**     * Set the tag of the attribute. Commonly used to reset the     * tag value used for IMPLICIT encodings.     *     * @params tag the tag value     */    public void resetTag(byte tag) {        this.tag = tag;    }    /**     * Returns a hashcode for this DerValue.     *     * @return a hashcode for this DerValue.     */    public int hashCode() {        return toString().hashCode();    }}

⌨️ 快捷键说明

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