jdk/src/java.base/share/classes/sun/security/util/DerValue.java
author valeriep
Thu, 17 Nov 2016 02:08:53 +0000
changeset 43214 3dd7af9b9e05
parent 32933 83d50f1247bb
child 44260 dd947f766e11
permissions -rw-r--r--
8168714: Tighten ECDSA validation Summary: Added additional checks to DER parsing code Reviewed-by: vinnie, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 32933
diff changeset
     2
 * Copyright (c) 1996, 2016, 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;
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
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
    47
 * 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
    48
 * ISO-8859-1 characters only.
2
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
    /**
2065
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
    68
     * The DER-encoded data of the value, never null
2
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. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
    86
    public static final byte    tag_Boolean = 0x01;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /** Tag value indicating an ASN.1 "INTEGER" value. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
    89
    public static final byte    tag_Integer = 0x02;
2
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. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
    92
    public static final byte    tag_BitString = 0x03;
2
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. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
    95
    public static final byte    tag_OctetString = 0x04;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /** Tag value indicating an ASN.1 "NULL" value. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
    98
    public static final byte    tag_Null = 0x05;
2
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. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   101
    public static final byte    tag_ObjectId = 0x06;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /** Tag value including an ASN.1 "ENUMERATED" value */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   104
    public static final byte    tag_Enumerated = 0x0A;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    /** Tag value indicating an ASN.1 "UTF8String" value. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   107
    public static final byte    tag_UTF8String = 0x0C;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /** Tag value including a "printable" string */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   110
    public static final byte    tag_PrintableString = 0x13;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /** Tag value including a "teletype" string */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   113
    public static final byte    tag_T61String = 0x14;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /** Tag value including an ASCII string */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   116
    public static final byte    tag_IA5String = 0x16;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /** Tag value indicating an ASN.1 "UTCTime" value. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   119
    public static final byte    tag_UtcTime = 0x17;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    /** Tag value indicating an ASN.1 "GeneralizedTime" value. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   122
    public static final byte    tag_GeneralizedTime = 0x18;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /** Tag value indicating an ASN.1 "GenerallString" value. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   125
    public static final byte    tag_GeneralString = 0x1B;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /** Tag value indicating an ASN.1 "UniversalString" value. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   128
    public static final byte    tag_UniversalString = 0x1C;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    /** Tag value indicating an ASN.1 "BMPString" value. */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   131
    public static final byte    tag_BMPString = 0x1E;
2
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
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   139
    public static final byte    tag_Sequence = 0x30;
2
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
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   145
    public static final byte    tag_SequenceOf = 0x30;
2
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
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   151
    public static final byte    tag_Set = 0x31;
2
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
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 30374
diff changeset
   157
    public static final byte    tag_SetOf = 0x31;
2
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();
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 32933
diff changeset
   251
        length = DerInputStream.getLength(lenByte, in);
2
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");
23345
534d74068ee0 8028591: NegativeArraySizeException in sun.security.util.DerInputStream.getUnalignedBitString()
asmotrak
parents: 22563
diff changeset
   267
            length = DerInputStream.getDefiniteLength(inbuf);
2
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
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   303
     * @param len how many bytes are in the encoded datum
2
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();
43214
3dd7af9b9e05 8168714: Tighten ECDSA validation
valeriep
parents: 32933
diff changeset
   364
        length = DerInputStream.getLength(lenByte, in);
2
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");
23345
534d74068ee0 8028591: NegativeArraySizeException in sun.security.util.DerInputStream.getUnalignedBitString()
asmotrak
parents: 22563
diff changeset
   379
            length = DerInputStream.getDefiniteLength(in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        if (fullyBuffered && in.available() != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            throw new IOException("extra data given to DerValue constructor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
4188
f67abce80f05 6864911: ASN.1/DER input stream parser needs more work
weijun
parents: 2065
diff changeset
   385
        byte[] bytes = IOUtils.readFully(in, length, true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        buffer = new DerInputBuffer(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        return new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * Encode an ASN1/DER encoded datum onto a DER output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    public void encode(DerOutputStream out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        out.write(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        out.putLength(length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        // XXX yeech, excess copies ... DerInputBuffer.write(OutStream)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        if (length > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            byte[] value = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            // always synchronized on data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            synchronized (data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                buffer.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                if (buffer.read(value) != length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    throw new IOException("short DER value read (encode)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                out.write(value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            }
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
    public final DerInputStream getData() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        return data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public final byte getTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        return tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * Returns an ASN.1 BOOLEAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @return the boolean held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public boolean getBoolean() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        if (tag != tag_Boolean) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            throw new IOException("DerValue.getBoolean, not a BOOLEAN " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        if (length != 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            throw new IOException("DerValue.getBoolean, invalid length "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                        + length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        if (buffer.read() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * Returns an ASN.1 OBJECT IDENTIFIER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @return the OID held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    public ObjectIdentifier getOID() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        if (tag != tag_ObjectId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            throw new IOException("DerValue.getOID, not an OID " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        return new ObjectIdentifier(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    private byte[] append(byte[] a, byte[] b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (a == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            return b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        byte[] ret = new byte[a.length + b.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        System.arraycopy(a, 0, ret, 0, a.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        System.arraycopy(b, 0, ret, a.length, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * Returns an ASN.1 OCTET STRING
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * @return the octet string held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    public byte[] getOctetString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        byte[] bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (tag != tag_OctetString && !isConstructed(tag_OctetString)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                "DerValue.getOctetString, not an Octet String: " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        bytes = new byte[length];
2065
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
   474
        // 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
   475
        // known bug that it returns -1 instead of 0.
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
   476
        if (length == 0) {
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
   477
            return bytes;
d0bafa066816 6804045: DerValue does not accept empty OCTET STRING
weijun
parents: 2
diff changeset
   478
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (buffer.read(bytes) != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            throw new IOException("short read on DerValue buffer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        if (isConstructed()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            DerInputStream in = new DerInputStream(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            bytes = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            while (in.available() != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                bytes = append(bytes, in.getOctetString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return bytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * Returns an ASN.1 INTEGER value as an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * @return the integer held in this DER value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public int getInteger() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        if (tag != tag_Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            throw new IOException("DerValue.getInteger, not an int " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
        return buffer.getInteger(data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * Returns an ASN.1 INTEGER value as a BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * @return the integer held in this DER value as a BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public BigInteger getBigInteger() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        if (tag != tag_Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            throw new IOException("DerValue.getBigInteger, not an int " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
        return buffer.getBigInteger(data.available(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * Returns an ASN.1 INTEGER value as a positive BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * This is just to deal with implementations that incorrectly encode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * some values as negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * @return the integer held in this DER value as a BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    public BigInteger getPositiveBigInteger() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        if (tag != tag_Integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
            throw new IOException("DerValue.getBigInteger, not an int " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        return buffer.getBigInteger(data.available(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * Returns an ASN.1 ENUMERATED value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @return the integer held in this DER value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    public int getEnumerated() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        if (tag != tag_Enumerated) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            throw new IOException("DerValue.getEnumerated, incorrect tag: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                                  + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        return buffer.getInteger(data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * Returns an ASN.1 BIT STRING value.  The bit string must be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * @return the bit string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    public byte[] getBitString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        if (tag != tag_BitString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                "DerValue.getBitString, not a bit string " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        return buffer.getBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Returns an ASN.1 BIT STRING value that need not be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return a BitArray representing the bit string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public BitArray getUnalignedBitString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        if (tag != tag_BitString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                "DerValue.getBitString, not a bit string " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
        return buffer.getUnalignedBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * Returns the name component as a Java string, regardless of its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * encoding restrictions (ASCII, T61, Printable, IA5, BMP, UTF8).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    // TBD: Need encoder for UniversalString before it can be handled.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    public String getAsString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        if (tag == tag_UTF8String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            return getUTF8String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        else if (tag == tag_PrintableString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            return getPrintableString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
        else if (tag == tag_T61String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            return getT61String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        else if (tag == tag_IA5String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            return getIA5String();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
          else if (tag == tag_UniversalString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
          return getUniversalString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
        else if (tag == tag_BMPString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            return getBMPString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        else if (tag == tag_GeneralString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            return getGeneralString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * Returns an ASN.1 BIT STRING value, with the tag assumed implicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * based on the parameter.  The bit string must be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   596
     * @param tagImplicit if true, the tag is assumed implicit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * @return the bit string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    public byte[] getBitString(boolean tagImplicit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (!tagImplicit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            if (tag != tag_BitString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                throw new IOException("DerValue.getBitString, not a bit string "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                                       + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        return buffer.getBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * Returns an ASN.1 BIT STRING value, with the tag assumed implicit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * based on the parameter.  The bit string need not be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   612
     * @param tagImplicit if true, the tag is assumed implicit.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * @return the bit string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
    public BitArray getUnalignedBitString(boolean tagImplicit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        if (!tagImplicit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            if (tag != tag_BitString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                throw new IOException("DerValue.getBitString, not a bit string "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                                       + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        return buffer.getUnalignedBitString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * Helper routine to return all the bytes contained in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * DerInputStream associated with this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    public byte[] getDataBytes() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
        byte[] retVal = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        synchronized (data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            data.getBytes(retVal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        return retVal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * Returns an ASN.1 STRING value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * @return the printable string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public String getPrintableString()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        if (tag != tag_PrintableString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                "DerValue.getPrintableString, not a string " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        return new String(getDataBytes(), "ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     * Returns an ASN.1 T61 (Teletype) STRING value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * @return the teletype string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    public String getT61String() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        if (tag != tag_T61String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
                "DerValue.getT61String, not T61 " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return new String(getDataBytes(), "ISO-8859-1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * Returns an ASN.1 IA5 (ASCII) STRING value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @return the ASCII string held in this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public String getIA5String() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        if (tag != tag_IA5String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                "DerValue.getIA5String, not IA5 " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        return new String(getDataBytes(), "ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     * Returns the ASN.1 BMP (Unicode) STRING value as a Java string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @return a string corresponding to the encoded BMPString held in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
    public String getBMPString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (tag != tag_BMPString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                "DerValue.getBMPString, not BMP " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        // BMPString is the same as Unicode in big endian, unmarked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        // format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        return new String(getDataBytes(), "UnicodeBigUnmarked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * Returns the ASN.1 UTF-8 STRING value as a Java String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @return a string corresponding to the encoded UTF8String held in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    public String getUTF8String() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        if (tag != tag_UTF8String)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                "DerValue.getUTF8String, not UTF-8 " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        return new String(getDataBytes(), "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * Returns the ASN.1 GENERAL STRING value as a Java String.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * @return a string corresponding to the encoded GeneralString held in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    public String getGeneralString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        if (tag != tag_GeneralString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                "DerValue.getGeneralString, not GeneralString " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        return new String(getDataBytes(), "ASCII");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     * Returns a Date if the DerValue is UtcTime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @return the Date held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    public Date getUTCTime() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        if (tag != tag_UtcTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
            throw new IOException("DerValue.getUTCTime, not a UtcTime: " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        return buffer.getUTCTime(data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * Returns a Date if the DerValue is GeneralizedTime.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * @return the Date held in this DER value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    public Date getGeneralizedTime() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (tag != tag_GeneralizedTime) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                "DerValue.getGeneralizedTime, not a GeneralizedTime: " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        return buffer.getGeneralizedTime(data.available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
     * Bitwise equality comparison.  DER encoded values have a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * encoding, so that bitwise equality of the encoded values is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
     * efficient way to establish equivalence of the unencoded values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   752
     * @param o the object being compared with this one
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     */
22563
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   754
    @Override
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   755
    public boolean equals(Object o) {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   756
        if (this == o) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
        }
22563
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   759
        if (!(o instanceof DerValue)) {
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   760
            return false;
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   761
        }
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   762
        DerValue other = (DerValue) o;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
        if (tag != other.tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (data == other.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        // make sure the order of lock is always consistent to avoid a deadlock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
        return (System.identityHashCode(this.data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                > System.identityHashCode(other.data)) ?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                doEquals(this, other):
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                doEquals(other, this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * Helper for public method equals()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
    private static boolean doEquals(DerValue d1, DerValue d2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        synchronized (d1.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            synchronized (d2.data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                d1.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                d2.data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                return d1.buffer.equals(d2.buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
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
     * Returns a printable representation of the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
     * @return printable representation of the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     */
22563
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   795
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            String str = getAsString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
            if (str != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                return "\"" + str + "\"";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            if (tag == tag_Null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                return "[DerValue, null]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            if (tag == tag_ObjectId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                return "OID." + getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            // integers
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                return "[DerValue, tag = " + tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                        + ", length = " + length + "]";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            throw new IllegalArgumentException("misformatted DER value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
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
     * Returns a DER-encoded value, such that if it's passed to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
     * DerValue constructor, a value equivalent to "this" is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * @return DER-encoded value, including tag and length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    public byte[] toByteArray() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        encode(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
     * For "set" and "sequence" types, this function may be used
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
     * to return a DER stream of the members of the set or sequence.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * This operation is not supported for primitive types such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     * integers or bit strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    public DerInputStream toDerInputStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
        if (tag == tag_Sequence || tag == tag_Set)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            return new DerInputStream(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        throw new IOException("toDerInputStream rejects tag type " + tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * Get the length of the encoded value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    public int length() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
        return length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     * Determine if a character is one of the permissible characters for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * PrintableString:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * A-Z, a-z, 0-9, space, apostrophe (39), left and right parentheses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * plus sign, comma, hyphen, period, slash, colon, equals sign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * and question mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * Characters that are *not* allowed in PrintableString include
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * exclamation point, quotation mark, number sign, dollar sign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     * percent sign, ampersand, asterisk, semicolon, less than sign,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * greater than sign, at sign, left and right square brackets,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * backslash, circumflex (94), underscore, back quote (96),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     * left and right curly brackets, vertical line, tilde,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * and the control codes (0-31 and 127).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     * This list is based on X.680 (the ASN.1 spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
    public static boolean isPrintableStringChar(char ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
        if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            (ch >= '0' && ch <= '9')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            switch (ch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                case ' ':       /* space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                case '\'':      /* apostrophe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                case '(':       /* left paren */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                case ')':       /* right paren */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                case '+':       /* plus */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                case ',':       /* comma */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                case '-':       /* hyphen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                case '.':       /* period */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                case '/':       /* slash */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                case ':':       /* colon */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                case '=':       /* equals */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                case '?':       /* question mark */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            }
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
     * Create the tag of the attribute.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   894
     * @param tagClass the tag class type, one of UNIVERSAL, CONTEXT,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
     *               APPLICATION or PRIVATE
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   896
     * @param form if true, the value is constructed, otherwise it
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     * is primitive.
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   898
     * @param val the tag value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    public static byte createTag(byte tagClass, boolean form, byte val) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        byte tag = (byte)(tagClass | val);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if (form) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            tag |= (byte)0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        return (tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * Set the tag of the attribute. Commonly used to reset the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     * tag value used for IMPLICIT encodings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
     *
30374
2abaf49910ea 8079478: some docs cleanup for sun.security
avstepan
parents: 26967
diff changeset
   912
     * @param tag the tag value
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    public void resetTag(byte tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        this.tag = tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * Returns a hashcode for this DerValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     * @return a hashcode for this DerValue.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     */
22563
2e5b6242e863 8028431: NullPointerException in DerValue.equals(DerValue)
asmotrak
parents: 5506
diff changeset
   923
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        return toString().hashCode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
}