jdk/src/share/classes/sun/security/util/DerValue.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 2065 d0bafa066816
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.util;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Represents a single DER-encoded value.  DER encoding rules are a subset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * of the "Basic" Encoding Rules (BER), but they only support a single way
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * ("Definite" encoding) to encode any given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <P>All DER-encoded data are triples <em>{type, length, data}</em>.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * class represents such tagged values as they have been read (or constructed),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and provides structured access to the encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <P>At this time, this class supports only a subset of the types of DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * data encodings which are defined.  That subset is sufficient for parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * most X.509 certificates, and working with selected additional formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * (such as PKCS #10 certificate requests, and some kinds of PKCS #7 data).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * A note with respect to T61/Teletex strings: From RFC 1617, section 4.1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * and RFC 3280, section 4.1.2.4., we assume that this kind of string will
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * contain ISO-8859-1 characters only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
public class DerValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /** The tag class types */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final byte TAG_UNIVERSAL = (byte)0x000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final byte TAG_APPLICATION = (byte)0x040;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final byte TAG_CONTEXT = (byte)0x080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final byte TAG_PRIVATE = (byte)0x0c0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /** The DER tag of the value; one of the tag_ constants. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public byte                 tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    protected DerInputBuffer    buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * The DER-encoded data of the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public final DerInputStream data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int                 length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * The type starts at the first byte of the encoding, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * is one of these tag_* values.  That may be all the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * data that is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * These tags are the "universal" tags ... they mean the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * in all contexts.  (Mask with 0x1f -- five bits.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** Tag value indicating an ASN.1 "BOOLEAN" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    public final static byte    tag_Boolean = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** Tag value indicating an ASN.1 "INTEGER" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public final static byte    tag_Integer = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /** Tag value indicating an ASN.1 "BIT STRING" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    public final static byte    tag_BitString = 0x03;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /** Tag value indicating an ASN.1 "OCTET STRING" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public final static byte    tag_OctetString = 0x04;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /** Tag value indicating an ASN.1 "NULL" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    public final static byte    tag_Null = 0x05;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /** Tag value indicating an ASN.1 "OBJECT IDENTIFIER" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public final static byte    tag_ObjectId = 0x06;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /** Tag value including an ASN.1 "ENUMERATED" value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public final static byte    tag_Enumerated = 0x0A;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /** Tag value indicating an ASN.1 "UTF8String" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public final static byte    tag_UTF8String = 0x0C;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /** Tag value including a "printable" string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    public final static byte    tag_PrintableString = 0x13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /** Tag value including a "teletype" string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public final static byte    tag_T61String = 0x14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /** Tag value including an ASCII string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    public final static byte    tag_IA5String = 0x16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /** Tag value indicating an ASN.1 "UTCTime" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public final static byte    tag_UtcTime = 0x17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /** Tag value indicating an ASN.1 "GeneralizedTime" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    public final static byte    tag_GeneralizedTime = 0x18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /** Tag value indicating an ASN.1 "GenerallString" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public final static byte    tag_GeneralString = 0x1B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /** Tag value indicating an ASN.1 "UniversalString" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public final static byte    tag_UniversalString = 0x1C;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /** Tag value indicating an ASN.1 "BMPString" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public final static byte    tag_BMPString = 0x1E;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    // CONSTRUCTED seq/set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Tag value indicating an ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * "SEQUENCE" (zero to N elements, order is significant).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public final static byte    tag_Sequence = 0x30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * Tag value indicating an ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * "SEQUENCE OF" (one to N elements, order is significant).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public final static byte    tag_SequenceOf = 0x30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * Tag value indicating an ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * "SET" (zero to N members, order does not matter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public final static byte    tag_Set = 0x31;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * Tag value indicating an ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * "SET OF" (one to N members, order does not matter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    public final static byte    tag_SetOf = 0x31;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * These values are the high order bits for the other kinds of tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns true if the tag class is UNIVERSAL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public boolean isUniversal()      { return ((tag & 0x0c0) == 0x000); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns true if the tag class is APPLICATION.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    public boolean isApplication()    { return ((tag & 0x0c0) == 0x040); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * Returns true iff the CONTEXT SPECIFIC bit is set in the type tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * This is associated with the ASN.1 "DEFINED BY" syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public boolean isContextSpecific() { return ((tag & 0x0c0) == 0x080); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * Returns true iff the CONTEXT SPECIFIC TAG matches the passed tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public boolean isContextSpecific(byte cntxtTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (!isContextSpecific()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        return ((tag & 0x01f) == cntxtTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    boolean isPrivate()        { return ((tag & 0x0c0) == 0x0c0); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /** Returns true iff the CONSTRUCTED bit is set in the type tag. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    public boolean isConstructed()    { return ((tag & 0x020) == 0x020); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * Returns true iff the CONSTRUCTED TAG matches the passed tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    public boolean isConstructed(byte constructedTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (!isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        return ((tag & 0x01f) == constructedTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * Creates a PrintableString or UTF8string DER value from a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public DerValue(String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        boolean isPrintableString = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        for (int i = 0; i < value.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (!isPrintableStringChar(value.charAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                isPrintableString = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        data = init(isPrintableString ? tag_PrintableString : tag_UTF8String, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Creates a string type DER value from a String object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * @param stringTag the tag for the DER value to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param value the String object to use for the DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    public DerValue(byte stringTag, String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        data = init(stringTag, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Creates a DerValue from a tag and some DER-encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * @param tag the DER type tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param data the DER-encoded data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public DerValue(byte tag, byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        this.tag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        buffer = new DerInputBuffer(data.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        length = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        this.data = new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        this.data.mark(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    DerValue(DerInputBuffer in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        // XXX must also parse BER-encoded constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // values such as sequences, sets...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        tag = (byte)in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        byte lenByte = (byte)in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        length = DerInputStream.getLength((lenByte & 0xff), in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (length == -1) {  // indefinite length encoding found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            DerInputBuffer inbuf = in.dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            int readLen = inbuf.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int offset = 2;     // for tag and length bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            byte[] indefData = new byte[readLen + offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            indefData[0] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            indefData[1] = lenByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            DataInputStream dis = new DataInputStream(inbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            dis.readFully(indefData, offset, readLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            DerIndefLenConverter derIn = new DerIndefLenConverter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            inbuf = new DerInputBuffer(derIn.convert(indefData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            if (tag != inbuf.read())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                        ("Indefinite length encoding not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            length = DerInputStream.getLength(inbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            buffer = inbuf.dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            buffer.truncate(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            data = new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // indefinite form is encoded by sending a length field with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // length of 0. - i.e. [1000|0000].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // the object is ended by sending two zero bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            in.skip(length + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            buffer = in.dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            buffer.truncate(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            data = new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            in.skip(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * Get an ASN.1/DER encoded datum from a buffer.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * entire buffer must hold exactly one datum, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * its tag and length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @param buf buffer holding a single DER-encoded datum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    public DerValue(byte[] buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        data = init(true, new ByteArrayInputStream(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * Get an ASN.1/DER encoded datum from part of a buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * That part of the buffer must hold exactly one datum, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * its tag and length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param buf the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param offset start point of the single DER-encoded dataum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param length how many bytes are in the encoded datum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public DerValue(byte[] buf, int offset, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        data = init(true, new ByteArrayInputStream(buf, offset, len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * Get an ASN1/DER encoded datum from an input stream.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * stream may have additional data following the encoded datum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * In case of indefinite length encoded datum, the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * must hold only one datum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * @param in the input stream holding a single DER datum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *  which may be followed by additional data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    public DerValue(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        data = init(false, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    private DerInputStream init(byte stringTag, String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        String enc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        tag = stringTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        switch (stringTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        case tag_PrintableString:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        case tag_IA5String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        case tag_GeneralString:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            enc = "ASCII";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        case tag_T61String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            enc = "ISO-8859-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        case tag_BMPString:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            enc = "UnicodeBigUnmarked";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        case tag_UTF8String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            enc = "UTF8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // TBD: Need encoder for UniversalString before it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            // be handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            throw new IllegalArgumentException("Unsupported DER string type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        byte[] buf = value.getBytes(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        length = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        buffer = new DerInputBuffer(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        DerInputStream result = new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        result.mark(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * helper routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    private DerInputStream init(boolean fullyBuffered, InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        tag = (byte)in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        byte lenByte = (byte)in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        length = DerInputStream.getLength((lenByte & 0xff), in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (length == -1) { // indefinite length encoding found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            int readLen = in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            int offset = 2;     // for tag and length bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            byte[] indefData = new byte[readLen + offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            indefData[0] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            indefData[1] = lenByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            DataInputStream dis = new DataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            dis.readFully(indefData, offset, readLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            DerIndefLenConverter derIn = new DerIndefLenConverter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            in = new ByteArrayInputStream(derIn.convert(indefData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            if (tag != in.read())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                        ("Indefinite length encoding not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            length = DerInputStream.getLength(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (length == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (fullyBuffered && in.available() != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            throw new IOException("extra data given to DerValue constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        byte[] bytes = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // n.b. readFully not needed in normal fullyBuffered case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        DataInputStream dis = new DataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        dis.readFully(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        buffer = new DerInputBuffer(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        return new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Encode an ASN1/DER encoded datum onto a DER output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public void encode(DerOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        out.write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        out.putLength(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        // XXX yeech, excess copies ... DerInputBuffer.write(OutStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            byte[] value = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            // always synchronized on data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            synchronized (data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                buffer.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                if (buffer.read(value) != length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    throw new IOException("short DER value read (encode)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                out.write(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    public final DerInputStream getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public final byte getTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        return tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * Returns an ASN.1 BOOLEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * @return the boolean held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    public boolean getBoolean() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        if (tag != tag_Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
            throw new IOException("DerValue.getBoolean, not a BOOLEAN " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        if (length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            throw new IOException("DerValue.getBoolean, invalid length "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                                        + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        if (buffer.read() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Returns an ASN.1 OBJECT IDENTIFIER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @return the OID held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public ObjectIdentifier getOID() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (tag != tag_ObjectId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            throw new IOException("DerValue.getOID, not an OID " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        return new ObjectIdentifier(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    private byte[] append(byte[] a, byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        byte[] ret = new byte[a.length + b.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        System.arraycopy(a, 0, ret, 0, a.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        System.arraycopy(b, 0, ret, a.length, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Returns an ASN.1 OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @return the octet string held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public byte[] getOctetString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (tag != tag_OctetString && !isConstructed(tag_OctetString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                "DerValue.getOctetString, not an Octet String: " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        bytes = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        if (buffer.read(bytes) != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            throw new IOException("short read on DerValue buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        if (isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            DerInputStream in = new DerInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            while (in.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                bytes = append(bytes, in.getOctetString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Returns an ASN.1 INTEGER value as an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     * @return the integer held in this DER value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    public int getInteger() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        if (tag != tag_Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            throw new IOException("DerValue.getInteger, not an int " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        return buffer.getInteger(data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * Returns an ASN.1 INTEGER value as a BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * @return the integer held in this DER value as a BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    public BigInteger getBigInteger() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        if (tag != tag_Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            throw new IOException("DerValue.getBigInteger, not an int " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        return buffer.getBigInteger(data.available(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Returns an ASN.1 INTEGER value as a positive BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * This is just to deal with implementations that incorrectly encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * some values as negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @return the integer held in this DER value as a BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    public BigInteger getPositiveBigInteger() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        if (tag != tag_Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            throw new IOException("DerValue.getBigInteger, not an int " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
        return buffer.getBigInteger(data.available(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     * Returns an ASN.1 ENUMERATED value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * @return the integer held in this DER value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
    public int getEnumerated() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        if (tag != tag_Enumerated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            throw new IOException("DerValue.getEnumerated, incorrect tag: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                  + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return buffer.getInteger(data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Returns an ASN.1 BIT STRING value.  The bit string must be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @return the bit string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    public byte[] getBitString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (tag != tag_BitString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                "DerValue.getBitString, not a bit string " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return buffer.getBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Returns an ASN.1 BIT STRING value that need not be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @return a BitArray representing the bit string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public BitArray getUnalignedBitString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (tag != tag_BitString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                "DerValue.getBitString, not a bit string " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        return buffer.getUnalignedBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * Returns the name component as a Java string, regardless of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * encoding restrictions (ASCII, T61, Printable, IA5, BMP, UTF8).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    // TBD: Need encoder for UniversalString before it can be handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    public String getAsString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        if (tag == tag_UTF8String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            return getUTF8String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
        else if (tag == tag_PrintableString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            return getPrintableString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
        else if (tag == tag_T61String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            return getT61String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
        else if (tag == tag_IA5String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            return getIA5String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
          else if (tag == tag_UniversalString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
          return getUniversalString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
        else if (tag == tag_BMPString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
            return getBMPString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        else if (tag == tag_GeneralString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            return getGeneralString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * Returns an ASN.1 BIT STRING value, with the tag assumed implicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * based on the parameter.  The bit string must be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @params tagImplicit if true, the tag is assumed implicit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * @return the bit string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
    public byte[] getBitString(boolean tagImplicit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        if (!tagImplicit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if (tag != tag_BitString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                throw new IOException("DerValue.getBitString, not a bit string "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                                       + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        return buffer.getBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * Returns an ASN.1 BIT STRING value, with the tag assumed implicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * based on the parameter.  The bit string need not be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @params tagImplicit if true, the tag is assumed implicit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @return the bit string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    public BitArray getUnalignedBitString(boolean tagImplicit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (!tagImplicit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (tag != tag_BitString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                throw new IOException("DerValue.getBitString, not a bit string "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                                       + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        return buffer.getUnalignedBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * Helper routine to return all the bytes contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * DerInputStream associated with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    public byte[] getDataBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        byte[] retVal = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        synchronized (data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            data.getBytes(retVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * Returns an ASN.1 STRING value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @return the printable string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    public String getPrintableString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
        if (tag != tag_PrintableString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                "DerValue.getPrintableString, not a string " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        return new String(getDataBytes(), "ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * Returns an ASN.1 T61 (Teletype) STRING value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * @return the teletype string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    public String getT61String() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        if (tag != tag_T61String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                "DerValue.getT61String, not T61 " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        return new String(getDataBytes(), "ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * Returns an ASN.1 IA5 (ASCII) STRING value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @return the ASCII string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public String getIA5String() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        if (tag != tag_IA5String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                "DerValue.getIA5String, not IA5 " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        return new String(getDataBytes(), "ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     * Returns the ASN.1 BMP (Unicode) STRING value as a Java string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * @return a string corresponding to the encoded BMPString held in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    public String getBMPString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
        if (tag != tag_BMPString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                "DerValue.getBMPString, not BMP " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        // BMPString is the same as Unicode in big endian, unmarked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        // format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        return new String(getDataBytes(), "UnicodeBigUnmarked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     * Returns the ASN.1 UTF-8 STRING value as a Java String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * @return a string corresponding to the encoded UTF8String held in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     * this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    public String getUTF8String() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        if (tag != tag_UTF8String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                "DerValue.getUTF8String, not UTF-8 " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        return new String(getDataBytes(), "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Returns the ASN.1 GENERAL STRING value as a Java String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * @return a string corresponding to the encoded GeneralString held in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    public String getGeneralString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        if (tag != tag_GeneralString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                "DerValue.getGeneralString, not GeneralString " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        return new String(getDataBytes(), "ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * Returns a Date if the DerValue is UtcTime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @return the Date held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public Date getUTCTime() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        if (tag != tag_UtcTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
            throw new IOException("DerValue.getUTCTime, not a UtcTime: " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        return buffer.getUTCTime(data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Returns a Date if the DerValue is GeneralizedTime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     * @return the Date held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    public Date getGeneralizedTime() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        if (tag != tag_GeneralizedTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                "DerValue.getGeneralizedTime, not a GeneralizedTime: " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        return buffer.getGeneralizedTime(data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Returns true iff the other object is a DER value which
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * is bitwise equal to this one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * @param other the object being compared with this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        if (other instanceof DerValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            return equals((DerValue)other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
     * Bitwise equality comparison.  DER encoded values have a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
     * encoding, so that bitwise equality of the encoded values is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * efficient way to establish equivalence of the unencoded values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * @param other the object being compared with this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    public boolean equals(DerValue other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        if (this == other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        if (tag != other.tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        if (data == other.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        // make sure the order of lock is always consistent to avoid a deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        return (System.identityHashCode(this.data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                > System.identityHashCode(other.data)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                doEquals(this, other):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                doEquals(other, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     * Helper for public method equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    private static boolean doEquals(DerValue d1, DerValue d2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        synchronized (d1.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            synchronized (d2.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                d1.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                d2.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                return d1.buffer.equals(d2.buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * Returns a printable representation of the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * @return printable representation of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            String str = getAsString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            if (str != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                return "\"" + str + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
            if (tag == tag_Null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                return "[DerValue, null]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            if (tag == tag_ObjectId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                return "OID." + getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            // integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                return "[DerValue, tag = " + tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        + ", length = " + length + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            throw new IllegalArgumentException("misformatted DER value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * Returns a DER-encoded value, such that if it's passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * DerValue constructor, a value equivalent to "this" is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     * @return DER-encoded value, including tag and length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    public byte[] toByteArray() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     * For "set" and "sequence" types, this function may be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * to return a DER stream of the members of the set or sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * This operation is not supported for primitive types such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     * integers or bit strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
    public DerInputStream toDerInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
        if (tag == tag_Sequence || tag == tag_Set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            return new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        throw new IOException("toDerInputStream rejects tag type " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Get the length of the encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * Determine if a character is one of the permissible characters for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * PrintableString:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * A-Z, a-z, 0-9, space, apostrophe (39), left and right parentheses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * plus sign, comma, hyphen, period, slash, colon, equals sign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * and question mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * Characters that are *not* allowed in PrintableString include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * exclamation point, quotation mark, number sign, dollar sign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     * percent sign, ampersand, asterisk, semicolon, less than sign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
     * greater than sign, at sign, left and right square brackets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * backslash, circumflex (94), underscore, back quote (96),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     * left and right curly brackets, vertical line, tilde,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * and the control codes (0-31 and 127).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * This list is based on X.680 (the ASN.1 spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
    public static boolean isPrintableStringChar(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            (ch >= '0' && ch <= '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                case ' ':       /* space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                case '\'':      /* apostrophe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                case '(':       /* left paren */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                case ')':       /* right paren */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                case '+':       /* plus */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                case ',':       /* comma */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                case '-':       /* hyphen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                case '.':       /* period */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                case '/':       /* slash */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                case ':':       /* colon */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                case '=':       /* equals */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                case '?':       /* question mark */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * Create the tag of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     * @params class the tag class type, one of UNIVERSAL, CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     *               APPLICATION or PRIVATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
     * @params form if true, the value is constructed, otherwise it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     * is primitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
     * @params val the tag value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    public static byte createTag(byte tagClass, boolean form, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        byte tag = (byte)(tagClass | val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        if (form) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
            tag |= (byte)0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        return (tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * Set the tag of the attribute. Commonly used to reset the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * tag value used for IMPLICIT encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * @params tag the tag value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
    public void resetTag(byte tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        this.tag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
     * Returns a hashcode for this DerValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
     * @return a hashcode for this DerValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        return toString().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
}