jdk/src/java.base/share/classes/sun/security/util/DerValue.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
child 30374 2abaf49910ea
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4188
diff changeset
    23
 * questions.
2
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;
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2065
diff changeset
    31
import sun.misc.IOUtils;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Represents a single DER-encoded value.  DER encoding rules are a subset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * of the "Basic" Encoding Rules (BER), but they only support a single way
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * ("Definite" encoding) to encode any given value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <P>All DER-encoded data are triples <em>{type, length, data}</em>.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * class represents such tagged values as they have been read (or constructed),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * and provides structured access to the encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <P>At this time, this class supports only a subset of the types of DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * data encodings which are defined.  That subset is sufficient for parsing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * most X.509 certificates, and working with selected additional formats
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * (such as PKCS #10 certificate requests, and some kinds of PKCS #7 data).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * A note with respect to T61/Teletex strings: From RFC 1617, section 4.1.3
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    48
 * and RFC 5280, section 8, we assume that this kind of string will contain
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    49
 * ISO-8859-1 characters only.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
public class DerValue {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** The tag class types */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final byte TAG_UNIVERSAL = (byte)0x000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final byte TAG_APPLICATION = (byte)0x040;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final byte TAG_CONTEXT = (byte)0x080;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final byte TAG_PRIVATE = (byte)0x0c0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /** The DER tag of the value; one of the tag_ constants. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public byte                 tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected DerInputBuffer    buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
2065
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
    69
     * The DER-encoded data of the value, never null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public final DerInputStream data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private int                 length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * The type starts at the first byte of the encoding, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * is one of these tag_* values.  That may be all the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * data that is needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * These tags are the "universal" tags ... they mean the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * in all contexts.  (Mask with 0x1f -- five bits.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /** Tag value indicating an ASN.1 "BOOLEAN" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public final static byte    tag_Boolean = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /** Tag value indicating an ASN.1 "INTEGER" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public final static byte    tag_Integer = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /** Tag value indicating an ASN.1 "BIT STRING" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public final static byte    tag_BitString = 0x03;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /** Tag value indicating an ASN.1 "OCTET STRING" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public final static byte    tag_OctetString = 0x04;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /** Tag value indicating an ASN.1 "NULL" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public final static byte    tag_Null = 0x05;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /** Tag value indicating an ASN.1 "OBJECT IDENTIFIER" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    public final static byte    tag_ObjectId = 0x06;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /** Tag value including an ASN.1 "ENUMERATED" value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public final static byte    tag_Enumerated = 0x0A;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /** Tag value indicating an ASN.1 "UTF8String" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public final static byte    tag_UTF8String = 0x0C;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /** Tag value including a "printable" string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    public final static byte    tag_PrintableString = 0x13;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /** Tag value including a "teletype" string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public final static byte    tag_T61String = 0x14;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    /** Tag value including an ASCII string */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public final static byte    tag_IA5String = 0x16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /** Tag value indicating an ASN.1 "UTCTime" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public final static byte    tag_UtcTime = 0x17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /** Tag value indicating an ASN.1 "GeneralizedTime" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public final static byte    tag_GeneralizedTime = 0x18;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /** Tag value indicating an ASN.1 "GenerallString" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    public final static byte    tag_GeneralString = 0x1B;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /** Tag value indicating an ASN.1 "UniversalString" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public final static byte    tag_UniversalString = 0x1C;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /** Tag value indicating an ASN.1 "BMPString" value. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public final static byte    tag_BMPString = 0x1E;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    // CONSTRUCTED seq/set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Tag value indicating an ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * "SEQUENCE" (zero to N elements, order is significant).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public final static byte    tag_Sequence = 0x30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * Tag value indicating an ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * "SEQUENCE OF" (one to N elements, order is significant).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public final static byte    tag_SequenceOf = 0x30;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * Tag value indicating an ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * "SET" (zero to N members, order does not matter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public final static byte    tag_Set = 0x31;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Tag value indicating an ASN.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * "SET OF" (one to N members, order does not matter).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    public final static byte    tag_SetOf = 0x31;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * These values are the high order bits for the other kinds of tags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * Returns true if the tag class is UNIVERSAL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    public boolean isUniversal()      { return ((tag & 0x0c0) == 0x000); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Returns true if the tag class is APPLICATION.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public boolean isApplication()    { return ((tag & 0x0c0) == 0x040); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Returns true iff the CONTEXT SPECIFIC bit is set in the type tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * This is associated with the ASN.1 "DEFINED BY" syntax.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public boolean isContextSpecific() { return ((tag & 0x0c0) == 0x080); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Returns true iff the CONTEXT SPECIFIC TAG matches the passed tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public boolean isContextSpecific(byte cntxtTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (!isContextSpecific()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return ((tag & 0x01f) == cntxtTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    boolean isPrivate()        { return ((tag & 0x0c0) == 0x0c0); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /** Returns true iff the CONSTRUCTED bit is set in the type tag. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public boolean isConstructed()    { return ((tag & 0x020) == 0x020); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns true iff the CONSTRUCTED TAG matches the passed tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public boolean isConstructed(byte constructedTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (!isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return ((tag & 0x01f) == constructedTag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Creates a PrintableString or UTF8string DER value from a string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    public DerValue(String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        boolean isPrintableString = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        for (int i = 0; i < value.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (!isPrintableStringChar(value.charAt(i))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                isPrintableString = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        data = init(isPrintableString ? tag_PrintableString : tag_UTF8String, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Creates a string type DER value from a String object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param stringTag the tag for the DER value to create
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param value the String object to use for the DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    public DerValue(byte stringTag, String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        data = init(stringTag, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * Creates a DerValue from a tag and some DER-encoded data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @param tag the DER type tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * @param data the DER-encoded data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    public DerValue(byte tag, byte[] data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        this.tag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        buffer = new DerInputBuffer(data.clone());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        length = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        this.data = new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        this.data.mark(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * package private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    DerValue(DerInputBuffer in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        // XXX must also parse BER-encoded constructed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        // values such as sequences, sets...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        tag = (byte)in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        byte lenByte = (byte)in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        length = DerInputStream.getLength((lenByte & 0xff), in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (length == -1) {  // indefinite length encoding found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            DerInputBuffer inbuf = in.dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            int readLen = inbuf.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            int offset = 2;     // for tag and length bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            byte[] indefData = new byte[readLen + offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            indefData[0] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            indefData[1] = lenByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            DataInputStream dis = new DataInputStream(inbuf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            dis.readFully(indefData, offset, readLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            DerIndefLenConverter derIn = new DerIndefLenConverter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            inbuf = new DerInputBuffer(derIn.convert(indefData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            if (tag != inbuf.read())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                        ("Indefinite length encoding not supported");
23345
534d74068ee0 8028591: NegativeArraySizeException in sun.security.util.DerInputStream.getUnalignedBitString()
asmotrak
parents: 22563
diff changeset
   268
            length = DerInputStream.getDefiniteLength(inbuf);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            buffer = inbuf.dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            buffer.truncate(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            data = new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            // indefinite form is encoded by sending a length field with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            // length of 0. - i.e. [1000|0000].
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            // the object is ended by sending two zero bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            in.skip(length + offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            buffer = in.dup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            buffer.truncate(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            data = new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            in.skip(length);
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * Get an ASN.1/DER encoded datum from a buffer.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * entire buffer must hold exactly one datum, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * its tag and length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @param buf buffer holding a single DER-encoded datum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    public DerValue(byte[] buf) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        data = init(true, new ByteArrayInputStream(buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Get an ASN.1/DER encoded datum from part of a buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * That part of the buffer must hold exactly one datum, including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * its tag and length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param buf the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param offset start point of the single DER-encoded dataum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param length how many bytes are in the encoded datum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    public DerValue(byte[] buf, int offset, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        data = init(true, new ByteArrayInputStream(buf, offset, len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Get an ASN1/DER encoded datum from an input stream.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * stream may have additional data following the encoded datum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * In case of indefinite length encoded datum, the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * must hold only one datum.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @param in the input stream holding a single DER datum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *  which may be followed by additional data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    public DerValue(InputStream in) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        data = init(false, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    private DerInputStream init(byte stringTag, String value) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        String enc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        tag = stringTag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        switch (stringTag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        case tag_PrintableString:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        case tag_IA5String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        case tag_GeneralString:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            enc = "ASCII";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        case tag_T61String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            enc = "ISO-8859-1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        case tag_BMPString:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            enc = "UnicodeBigUnmarked";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        case tag_UTF8String:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            enc = "UTF8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            // TBD: Need encoder for UniversalString before it can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            // be handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            throw new IllegalArgumentException("Unsupported DER string type");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        byte[] buf = value.getBytes(enc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        length = buf.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        buffer = new DerInputBuffer(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        DerInputStream result = new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        result.mark(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * helper routine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    private DerInputStream init(boolean fullyBuffered, InputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        tag = (byte)in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        byte lenByte = (byte)in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        length = DerInputStream.getLength((lenByte & 0xff), in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        if (length == -1) { // indefinite length encoding found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            int readLen = in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            int offset = 2;     // for tag and length bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            byte[] indefData = new byte[readLen + offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            indefData[0] = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            indefData[1] = lenByte;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            DataInputStream dis = new DataInputStream(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            dis.readFully(indefData, offset, readLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            DerIndefLenConverter derIn = new DerIndefLenConverter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            in = new ByteArrayInputStream(derIn.convert(indefData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            if (tag != in.read())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                throw new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                        ("Indefinite length encoding not supported");
23345
534d74068ee0 8028591: NegativeArraySizeException in sun.security.util.DerInputStream.getUnalignedBitString()
asmotrak
parents: 22563
diff changeset
   380
            length = DerInputStream.getDefiniteLength(in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        if (fullyBuffered && in.available() != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            throw new IOException("extra data given to DerValue constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2065
diff changeset
   386
        byte[] bytes = IOUtils.readFully(in, length, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        buffer = new DerInputBuffer(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Encode an ASN1/DER encoded datum onto a DER output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public void encode(DerOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        out.write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        out.putLength(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        // XXX yeech, excess copies ... DerInputBuffer.write(OutStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            byte[] value = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            // always synchronized on data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            synchronized (data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                buffer.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                if (buffer.read(value) != length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    throw new IOException("short DER value read (encode)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                out.write(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    public final DerInputStream getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public final byte getTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * Returns an ASN.1 BOOLEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @return the boolean held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    public boolean getBoolean() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        if (tag != tag_Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
            throw new IOException("DerValue.getBoolean, not a BOOLEAN " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        if (length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            throw new IOException("DerValue.getBoolean, invalid length "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                                        + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
        if (buffer.read() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * Returns an ASN.1 OBJECT IDENTIFIER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @return the OID held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    public ObjectIdentifier getOID() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (tag != tag_ObjectId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            throw new IOException("DerValue.getOID, not an OID " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return new ObjectIdentifier(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    private byte[] append(byte[] a, byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        byte[] ret = new byte[a.length + b.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        System.arraycopy(a, 0, ret, 0, a.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        System.arraycopy(b, 0, ret, a.length, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * Returns an ASN.1 OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @return the octet string held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    public byte[] getOctetString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        if (tag != tag_OctetString && !isConstructed(tag_OctetString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                "DerValue.getOctetString, not an Octet String: " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        bytes = new byte[length];
2065
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
   475
        // Note: do not tempt to call buffer.read(bytes) at all. There's a
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
   476
        // known bug that it returns -1 instead of 0.
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
   477
        if (length == 0) {
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
   478
            return bytes;
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
   479
        }
2
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
     * Bitwise equality comparison.  DER encoded values have a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * encoding, so that bitwise equality of the encoded values is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     * efficient way to establish equivalence of the unencoded values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * @param other the object being compared with this one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     */
22563
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   755
    @Override
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   756
    public boolean equals(Object o) {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   757
        if (this == o) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
22563
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   760
        if (!(o instanceof DerValue)) {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   761
            return false;
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   762
        }
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   763
        DerValue other = (DerValue) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        if (tag != other.tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        if (data == other.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        // make sure the order of lock is always consistent to avoid a deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        return (System.identityHashCode(this.data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                > System.identityHashCode(other.data)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                doEquals(this, other):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                doEquals(other, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     * Helper for public method equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    private static boolean doEquals(DerValue d1, DerValue d2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        synchronized (d1.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
            synchronized (d2.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                d1.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                d2.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                return d1.buffer.equals(d2.buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     * Returns a printable representation of the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     * @return printable representation of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     */
22563
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   796
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            String str = getAsString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            if (str != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                return "\"" + str + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            if (tag == tag_Null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                return "[DerValue, null]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
            if (tag == tag_ObjectId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                return "OID." + getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            // integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                return "[DerValue, tag = " + tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                        + ", length = " + length + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            throw new IllegalArgumentException("misformatted DER value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * Returns a DER-encoded value, such that if it's passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * DerValue constructor, a value equivalent to "this" is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * @return DER-encoded value, including tag and length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    public byte[] toByteArray() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * For "set" and "sequence" types, this function may be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * to return a DER stream of the members of the set or sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * This operation is not supported for primitive types such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * integers or bit strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    public DerInputStream toDerInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        if (tag == tag_Sequence || tag == tag_Set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            return new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        throw new IOException("toDerInputStream rejects tag type " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * Get the length of the encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        return length;
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
     * Determine if a character is one of the permissible characters for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * PrintableString:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * A-Z, a-z, 0-9, space, apostrophe (39), left and right parentheses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * plus sign, comma, hyphen, period, slash, colon, equals sign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * and question mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * Characters that are *not* allowed in PrintableString include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * exclamation point, quotation mark, number sign, dollar sign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * percent sign, ampersand, asterisk, semicolon, less than sign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * greater than sign, at sign, left and right square brackets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * backslash, circumflex (94), underscore, back quote (96),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * left and right curly brackets, vertical line, tilde,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * and the control codes (0-31 and 127).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * This list is based on X.680 (the ASN.1 spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    public static boolean isPrintableStringChar(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            (ch >= '0' && ch <= '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                case ' ':       /* space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                case '\'':      /* apostrophe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                case '(':       /* left paren */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                case ')':       /* right paren */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                case '+':       /* plus */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                case ',':       /* comma */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                case '-':       /* hyphen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                case '.':       /* period */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                case '/':       /* slash */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                case ':':       /* colon */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                case '=':       /* equals */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                case '?':       /* question mark */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     * Create the tag of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     * @params class the tag class type, one of UNIVERSAL, CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     *               APPLICATION or PRIVATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * @params form if true, the value is constructed, otherwise it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * is primitive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @params val the tag value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
    public static byte createTag(byte tagClass, boolean form, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        byte tag = (byte)(tagClass | val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        if (form) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            tag |= (byte)0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        return (tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * Set the tag of the attribute. Commonly used to reset the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     * tag value used for IMPLICIT encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     * @params tag the tag value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
    public void resetTag(byte tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        this.tag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * Returns a hashcode for this DerValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * @return a hashcode for this DerValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     */
22563
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   924
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        return toString().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
}