934 lines · 29.9 KB · Java
RawDownload
        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 an ASN.1 BIT STRING value that need not be byte-aligned.     *     * @return a BitArray representing the bit string held in this value     */    public BitArray getUnalignedBitString() throws IOException {        if (tag != tag_BitString)            throw new IOException(                "DerValue.getBitString, not a bit string " + tag);         return buffer.getUnalignedBitString();    }     /**     * Returns the name component as a Java string, regardless of its     * encoding restrictions (ASCII, T61, Printable, IA5, BMP, UTF8).     */    // TBD: Need encoder for UniversalString before it can be handled.    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();        /*          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();    }     /**     * Returns an ASN.1 BIT STRING value, with the tag assumed implicit     * based on the parameter.  The bit string need not be byte-aligned.     *     * @params tagImplicit if true, the tag is assumed implicit.     * @return the bit string held in this value     */    public BitArray getUnalignedBitString(boolean tagImplicit)    throws IOException {        if (!tagImplicit) {            if (tag != tag_BitString)                throw new IOException("DerValue.getBitString, not a bit string "                                       + tag);            }        return buffer.getUnalignedBitString();    }     /**     * 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(), "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(), "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(), "UTF8");    }     /**     * 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(), "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();    }}

Lines 501–934 of 934. Previous lines