jdk/src/java.base/share/classes/sun/security/util/ObjectIdentifier.java
author ascarpino
Thu, 17 Nov 2016 09:51:10 -0800
changeset 43216 c0f498d6a804
parent 37361 a790f7bc3878
permissions -rw-r--r--
8168705: Better ObjectIdentifier validation Reviewed-by: mullan, asmotrak, ahgross
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 32649
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: 2584
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: 2584
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: 2584
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2584
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2584
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.*;
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    29
import java.math.BigInteger;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    30
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * Represent an ISO Object Identifier.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <P>Object Identifiers are arbitrary length hierarchical identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The individual components are numbers, and they define paths from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * root of an ISO-managed identifier space.  You will sometimes see a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * string name used instead of (or in addition to) the numerical id.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * These are synonyms for the numerical IDs, but are not widely used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * since most sites do not know all the requisite strings, while all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * sites can parse the numeric forms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <P>So for example, JavaSoft has the sole authority to assign the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * meaning to identifiers below the 1.3.6.1.4.1.42.2.17 node in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * hierarchy, and other organizations can easily acquire the ability
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * to assign such unique identifiers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author Amit Kapoor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    52
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31538
diff changeset
    53
public final
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
class ObjectIdentifier implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
{
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    56
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    57
     * We use the DER value (no tag, no length) as the internal format
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    58
     * @serial
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    59
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    60
    private byte[] encoding = null;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    61
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    62
    private transient volatile String stringForm;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    63
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    64
    /*
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    65
     * IMPORTANT NOTES FOR CODE CHANGES (bug 4811968) IN JDK 1.7.0
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    66
     * ===========================================================
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    67
     *
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    68
     * (Almost) serialization compatibility with old versions:
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    69
     *
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    70
     * serialVersionUID is unchanged. Old field "component" is changed to
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    71
     * type Object so that "poison" (unknown object type for old versions)
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    72
     * can be put inside if there are huge components that cannot be saved
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    73
     * as integers.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    74
     *
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    75
     * New version use the new filed "encoding" only.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    76
     *
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    77
     * Below are all 4 cases in a serialization/deserialization process:
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    78
     *
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    79
     * 1. old -> old: Not covered here
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    80
     * 2. old -> new: There's no "encoding" field, new readObject() reads
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    81
     *    "components" and "componentLen" instead and inits correctly.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    82
     * 3. new -> new: "encoding" field exists, new readObject() uses it
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    83
     *    (ignoring the other 2 fields) and inits correctly.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    84
     * 4. new -> old: old readObject() only recognizes "components" and
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    85
     *    "componentLen" fields. If no huge components are involved, they
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    86
     *    are serialized as legal values and old object can init correctly.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    87
     *    Otherwise, old object cannot recognize the form (component not int[])
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    88
     *    and throw a ClassNotFoundException at deserialization time.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    89
     *
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    90
     * Therfore, for the first 3 cases, exact compatibility is preserved. In
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    91
     * the 4th case, non-huge OID is still supportable in old versions, while
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    92
     * huge OID is not.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    93
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static final long serialVersionUID = 8697030238860181294L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    /**
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    97
     * Changed to Object
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    98
     * @serial
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
    99
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   100
    private Object      components   = null;          // path from root
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   101
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   102
     * @serial
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   103
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   104
    private int         componentLen = -1;            // how much is used.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   105
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   106
    // Is the components field calculated?
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31538
diff changeset
   107
    private transient boolean   componentsCalculated = false;
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   108
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   109
    private void readObject(ObjectInputStream is)
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   110
            throws IOException, ClassNotFoundException {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   111
        is.defaultReadObject();
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   112
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   113
        if (encoding == null) {  // from an old version
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   114
            init((int[])components, componentLen);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   115
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   116
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   117
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   118
    private void writeObject(ObjectOutputStream os)
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   119
            throws IOException {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   120
        if (!componentsCalculated) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   121
            int[] comps = toIntArray();
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   122
            if (comps != null) {    // every one understands this
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   123
                components = comps;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   124
                componentLen = comps.length;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   125
            } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   126
                components = HugeOidNotSupportedByOldJDK.theOne;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   127
            }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   128
            componentsCalculated = true;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   129
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   130
        os.defaultWriteObject();
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   131
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   132
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   133
    static class HugeOidNotSupportedByOldJDK implements Serializable {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   134
        private static final long serialVersionUID = 1L;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   135
        static HugeOidNotSupportedByOldJDK theOne = new HugeOidNotSupportedByOldJDK();
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   136
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   137
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   138
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   139
     * Constructs, from a string.  This string should be of the form 1.23.56.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   140
     * Validity check included.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public ObjectIdentifier (String oid) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        int ch = '.';
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   145
        int start = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int end = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   148
        int pos = 0;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   149
        byte[] tmp = new byte[oid.length()];
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   150
        int first = 0, second;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   151
        int count = 0;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   152
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   153
        try {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   154
            String comp = null;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   155
            do {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   156
                int length = 0; // length of one section
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   157
                end = oid.indexOf(ch,start);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   158
                if (end == -1) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   159
                    comp = oid.substring(start);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   160
                    length = oid.length() - start;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   161
                } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   162
                    comp = oid.substring(start,end);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   163
                    length = end - start;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   164
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   166
                if (length > 9) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   167
                    BigInteger bignum = new BigInteger(comp);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   168
                    if (count == 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   169
                        checkFirstComponent(bignum);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   170
                        first = bignum.intValue();
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   171
                    } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   172
                        if (count == 1) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   173
                            checkSecondComponent(first, bignum);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   174
                            bignum = bignum.add(BigInteger.valueOf(40*first));
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   175
                        } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   176
                            checkOtherComponent(count, bignum);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   177
                        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   178
                        pos += pack7Oid(bignum, tmp, pos);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   179
                    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   180
                } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   181
                    int num = Integer.parseInt(comp);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   182
                    if (count == 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   183
                        checkFirstComponent(num);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   184
                        first = num;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   185
                    } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   186
                        if (count == 1) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   187
                            checkSecondComponent(first, num);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   188
                            num += 40 * first;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   189
                        } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   190
                            checkOtherComponent(count, num);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   191
                        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   192
                        pos += pack7Oid(num, tmp, pos);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   193
                    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   194
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                start = end + 1;
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   196
                count++;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   197
            } while (end != -1);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   198
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   199
            checkCount(count);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   200
            encoding = new byte[pos];
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   201
            System.arraycopy(tmp, 0, encoding, 0, pos);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   202
            this.stringForm = oid;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   203
        } catch (IOException ioe) { // already detected by checkXXX
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   204
            throw ioe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new IOException("ObjectIdentifier() -- Invalid format: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    + e.toString(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   212
     * Constructor, from an array of integers.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   213
     * Validity check included.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   215
    public ObjectIdentifier(int[] values) throws IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    {
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   217
        checkCount(values.length);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   218
        checkFirstComponent(values[0]);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   219
        checkSecondComponent(values[0], values[1]);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   220
        for (int i=2; i<values.length; i++)
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   221
            checkOtherComponent(i, values[i]);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   222
        init(values, values.length);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /**
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   226
     * Constructor, from an ASN.1 encoded input stream.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   227
     * Validity check NOT included.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * The encoding of the ID in the stream uses "DER", a BER/1 subset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * In this case, that means a triple { typeId, length, data }.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <P><STRONG>NOTE:</STRONG>  When an exception is thrown, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * input stream has not been returned to its "initial" state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param in DER-encoded data holding an object ID
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @exception IOException indicates a decoding error
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     */
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   237
    public ObjectIdentifier (DerInputStream in) throws IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        byte    type_id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int     bufferEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         * Object IDs are a "universal" type, and their tag needs only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
         * one byte of encoding.  Verify that the tag of this datum
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
         * is that of an object ID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
         * Then get and check the length of the ID's encoding.  We set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
         * up so that we can use in.available() to check for the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
         * this value in the data stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        type_id = (byte) in.getByte ();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        if (type_id != DerValue.tag_ObjectId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            throw new IOException (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                "ObjectIdentifier() -- data isn't an object ID"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                + " (tag = " +  type_id + ")"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
43216
c0f498d6a804 8168705: Better ObjectIdentifier validation
ascarpino
parents: 37361
diff changeset
   258
        int len = in.getDefiniteLength();
c0f498d6a804 8168705: Better ObjectIdentifier validation
ascarpino
parents: 37361
diff changeset
   259
        if (len > in.available()) {
c0f498d6a804 8168705: Better ObjectIdentifier validation
ascarpino
parents: 37361
diff changeset
   260
            throw new IOException("ObjectIdentifier() -- length exceeds" +
c0f498d6a804 8168705: Better ObjectIdentifier validation
ascarpino
parents: 37361
diff changeset
   261
                    "data available.  Length: " + len + ", Available: " +
c0f498d6a804 8168705: Better ObjectIdentifier validation
ascarpino
parents: 37361
diff changeset
   262
                    in.available());
c0f498d6a804 8168705: Better ObjectIdentifier validation
ascarpino
parents: 37361
diff changeset
   263
        }
c0f498d6a804 8168705: Better ObjectIdentifier validation
ascarpino
parents: 37361
diff changeset
   264
        encoding = new byte[len];
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   265
        in.getBytes(encoding);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   266
        check(encoding);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /*
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   270
     * Constructor, from the rest of a DER input buffer;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   271
     * the tag and length have been removed/verified
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   272
     * Validity check NOT included.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     */
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   274
    ObjectIdentifier (DerInputBuffer buf) throws IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    {
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   276
        DerInputStream in = new DerInputStream(buf);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   277
        encoding = new byte[in.available()];
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   278
        in.getBytes(encoding);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   279
        check(encoding);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   280
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   282
    private void init(int[] components, int length) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   283
        int pos = 0;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   284
        byte[] tmp = new byte[length*5+1];  // +1 for empty input
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   286
        if (components[1] < Integer.MAX_VALUE - components[0]*40)
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   287
            pos += pack7Oid(components[0]*40+components[1], tmp, pos);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   288
        else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   289
            BigInteger big = BigInteger.valueOf(components[1]);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   290
            big = big.add(BigInteger.valueOf(components[0]*40));
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   291
            pos += pack7Oid(big, tmp, pos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   294
        for (int i=2; i<length; i++) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   295
            pos += pack7Oid(components[i], tmp, pos);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   296
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   297
        encoding = new byte[pos];
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   298
        System.arraycopy(tmp, 0, encoding, 0, pos);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   299
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   301
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   302
     * This method is kept for compatibility reasons. The new implementation
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   303
     * does the check and conversion. All around the JDK, the method is called
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   304
     * in static blocks to initialize pre-defined ObjectIdentifieies. No
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   305
     * obvious performance hurt will be made after this change.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   306
     *
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   307
     * Old doc: Create a new ObjectIdentifier for internal use. The values are
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   308
     * neither checked nor cloned.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   309
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   310
    public static ObjectIdentifier newInternal(int[] values) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   311
        try {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   312
            return new ObjectIdentifier(values);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   313
        } catch (IOException ex) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   314
            throw new RuntimeException(ex);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   315
            // Should not happen, internal calls always uses legal values.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * n.b. the only public interface is DerOutputStream.putOID()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    void encode (DerOutputStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    {
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   324
        out.write (DerValue.tag_ObjectId, encoding);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * Compares this identifier with another, for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * @return true iff the names are identical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     */
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   332
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (obj instanceof ObjectIdentifier == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        ObjectIdentifier other = (ObjectIdentifier)obj;
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   341
        return Arrays.equals(encoding, other.encoding);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   342
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   343
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   344
    @Override
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   345
    public int hashCode() {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   346
        return Arrays.hashCode(encoding);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   347
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   348
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   349
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   350
     * Private helper method for serialization. To be compatible with old
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   351
     * versions of JDK.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   352
     * @return components in an int array, if all the components are less than
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   353
     *         Integer.MAX_VALUE. Otherwise, null.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   354
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   355
    private int[] toIntArray() {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   356
        int length = encoding.length;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   357
        int[] result = new int[20];
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   358
        int which = 0;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   359
        int fromPos = 0;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   360
        for (int i = 0; i < length; i++) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   361
            if ((encoding[i] & 0x80) == 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   362
                // one section [fromPos..i]
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   363
                if (i - fromPos + 1 > 4) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   364
                    BigInteger big = new BigInteger(pack(encoding, fromPos, i-fromPos+1, 7, 8));
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   365
                    if (fromPos == 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   366
                        result[which++] = 2;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   367
                        BigInteger second = big.subtract(BigInteger.valueOf(80));
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   368
                        if (second.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == 1) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   369
                            return null;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   370
                        } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   371
                            result[which++] = second.intValue();
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   372
                        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   373
                    } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   374
                        if (big.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) == 1) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   375
                            return null;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   376
                        } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   377
                            result[which++] = big.intValue();
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   378
                        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   379
                    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   380
                } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   381
                    int retval = 0;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   382
                    for (int j = fromPos; j <= i; j++) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   383
                        retval <<= 7;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   384
                        byte tmp = encoding[j];
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   385
                        retval |= (tmp & 0x07f);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   386
                    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   387
                    if (fromPos == 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   388
                        if (retval < 80) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   389
                            result[which++] = retval / 40;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   390
                            result[which++] = retval % 40;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   391
                        } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   392
                            result[which++] = 2;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   393
                            result[which++] = retval - 80;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   394
                        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   395
                    } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   396
                        result[which++] = retval;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   397
                    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   398
                }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   399
                fromPos = i+1;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   400
            }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   401
            if (which >= result.length) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   402
                result = Arrays.copyOf(result, which + 10);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   405
        return Arrays.copyOf(result, which);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * Returns a string form of the object ID.  The format is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * conventional "dot" notation for such IDs, without any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * user-friendly descriptive strings, since those strings
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * will not be understood everywhere.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     */
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   414
    @Override
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        String s = stringForm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (s == null) {
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   418
            int length = encoding.length;
24969
afa6934dd8e8 8041679: Replace uses of StringBuffer with StringBuilder within core library classes
psandoz
parents: 23345
diff changeset
   419
            StringBuilder sb = new StringBuilder(length * 4);
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   420
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   421
            int fromPos = 0;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   422
            for (int i = 0; i < length; i++) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   423
                if ((encoding[i] & 0x80) == 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   424
                    // one section [fromPos..i]
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   425
                    if (fromPos != 0) {  // not the first segment
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   426
                        sb.append('.');
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   427
                    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   428
                    if (i - fromPos + 1 > 4) { // maybe big integer
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   429
                        BigInteger big = new BigInteger(pack(encoding, fromPos, i-fromPos+1, 7, 8));
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   430
                        if (fromPos == 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   431
                            // first section encoded with more than 4 bytes,
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   432
                            // must be 2.something
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   433
                            sb.append("2.");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   434
                            sb.append(big.subtract(BigInteger.valueOf(80)));
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   435
                        } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   436
                            sb.append(big);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   437
                        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   438
                    } else { // small integer
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   439
                        int retval = 0;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   440
                        for (int j = fromPos; j <= i; j++) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   441
                            retval <<= 7;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   442
                            byte tmp = encoding[j];
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   443
                            retval |= (tmp & 0x07f);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   444
                        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   445
                        if (fromPos == 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   446
                            if (retval < 80) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   447
                                sb.append(retval/40);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   448
                                sb.append('.');
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   449
                                sb.append(retval%40);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   450
                            } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   451
                                sb.append("2.");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   452
                                sb.append(retval - 80);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   453
                            }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   454
                        } else {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   455
                            sb.append(retval);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   456
                        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   457
                    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   458
                    fromPos = i+1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
            s = sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            stringForm = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   467
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   468
     * Repack all bits from input to output. On the both sides, only a portion
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   469
     * (from the least significant bit) of the 8 bits in a byte is used. This
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   470
     * number is defined as the number of useful bits (NUB) for the array. All the
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   471
     * used bits from the input byte array and repacked into the output in the
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   472
     * exactly same order. The output bits are aligned so that the final bit of
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   473
     * the input (the least significant bit in the last byte), when repacked as
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   474
     * the final bit of the output, is still at the least significant position.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   475
     * Zeroes will be padded on the left side of the first output byte if
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   476
     * necessary. All unused bits in the output are also zeroed.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   477
     *
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   478
     * For example: if the input is 01001100 with NUB 8, the output which
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   479
     * has a NUB 6 will look like:
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   480
     *      00000001 00001100
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   481
     * The first 2 bits of the output bytes are unused bits. The other bits
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   482
     * turn out to be 000001 001100. While the 8 bits on the right are from
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   483
     * the input, the left 4 zeroes are padded to fill the 6 bits space.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   484
     *
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   485
     * @param in        the input byte array
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   486
     * @param ioffset   start point inside <code>in</code>
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   487
     * @param ilength   number of bytes to repack
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   488
     * @param iw        NUB for input
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   489
     * @param ow        NUB for output
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   490
     * @return          the repacked bytes
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   491
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   492
    private static byte[] pack(byte[] in, int ioffset, int ilength, int iw, int ow) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   493
        assert (iw > 0 && iw <= 8): "input NUB must be between 1 and 8";
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   494
        assert (ow > 0 && ow <= 8): "output NUB must be between 1 and 8";
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   495
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   496
        if (iw == ow) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   497
            return in.clone();
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   498
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   499
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   500
        int bits = ilength * iw;    // number of all used bits
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   501
        byte[] out = new byte[(bits+ow-1)/ow];
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   502
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   503
        // starting from the 0th bit in the input
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   504
        int ipos = 0;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   505
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   506
        // the number of padding 0's needed in the output, skip them
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   507
        int opos = (bits+ow-1)/ow*ow-bits;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   508
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   509
        while(ipos < bits) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   510
            int count = iw - ipos%iw;   // unpacked bits in current input byte
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   511
            if (count > ow - opos%ow) { // free space available in output byte
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   512
                count = ow - opos%ow;   // choose the smaller number
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   513
            }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   514
            // and move them!
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   515
            out[opos/ow] |=                         // paste!
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   516
                (((in[ioffset+ipos/iw]+256)         // locate the byte (+256 so that it's never negative)
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   517
                    >> (iw-ipos%iw-count))          // move to the end of a byte
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   518
                        & ((1 << (count))-1))       // zero out all other bits
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   519
                            << (ow-opos%ow-count);  // move to the output position
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   520
            ipos += count;  // advance
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   521
            opos += count;  // advance
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   522
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   523
        return out;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   524
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   525
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   526
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   527
     * Repack from NUB 8 to a NUB 7 OID sub-identifier, remove all
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   528
     * unnecessary 0 headings, set the first bit of all non-tail
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   529
     * output bytes to 1 (as ITU-T Rec. X.690 8.19.2 says), and
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   530
     * paste it into an existing byte array.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   531
     * @param out the existing array to be pasted into
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   532
     * @param ooffset the starting position to paste
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   533
     * @return the number of bytes pasted
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   534
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   535
    private static int pack7Oid(byte[] in, int ioffset, int ilength, byte[] out, int ooffset) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   536
        byte[] pack = pack(in, ioffset, ilength, 8, 7);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   537
        int firstNonZero = pack.length-1;   // paste at least one byte
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   538
        for (int i=pack.length-2; i>=0; i--) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   539
            if (pack[i] != 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   540
                firstNonZero = i;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   541
            }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   542
            pack[i] |= 0x80;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   543
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   544
        System.arraycopy(pack, firstNonZero, out, ooffset, pack.length-firstNonZero);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   545
        return pack.length-firstNonZero;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   546
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   547
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   548
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   549
     * Repack from NUB 7 to NUB 8, remove all unnecessary 0
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   550
     * headings, and paste it into an existing byte array.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   551
     * @param out the existing array to be pasted into
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   552
     * @param ooffset the starting position to paste
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   553
     * @return the number of bytes pasted
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     */
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   555
    private static int pack8(byte[] in, int ioffset, int ilength, byte[] out, int ooffset) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   556
        byte[] pack = pack(in, ioffset, ilength, 7, 8);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   557
        int firstNonZero = pack.length-1;   // paste at least one byte
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   558
        for (int i=pack.length-2; i>=0; i--) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   559
            if (pack[i] != 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   560
                firstNonZero = i;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   561
            }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   562
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   563
        System.arraycopy(pack, firstNonZero, out, ooffset, pack.length-firstNonZero);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   564
        return pack.length-firstNonZero;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   565
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   566
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   567
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   568
     * Pack the int into a OID sub-identifier DER encoding
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   569
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   570
    private static int pack7Oid(int input, byte[] out, int ooffset) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   571
        byte[] b = new byte[4];
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   572
        b[0] = (byte)(input >> 24);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   573
        b[1] = (byte)(input >> 16);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   574
        b[2] = (byte)(input >> 8);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   575
        b[3] = (byte)(input);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   576
        return pack7Oid(b, 0, 4, out, ooffset);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   577
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   578
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   579
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   580
     * Pack the BigInteger into a OID subidentifier DER encoding
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   581
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   582
    private static int pack7Oid(BigInteger input, byte[] out, int ooffset) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   583
        byte[] b = input.toByteArray();
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   584
        return pack7Oid(b, 0, b.length, out, ooffset);
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   585
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   586
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   587
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   588
     * Private methods to check validity of OID. They must be --
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   589
     * 1. at least 2 components
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   590
     * 2. all components must be non-negative
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   591
     * 3. the first must be 0, 1 or 2
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   592
     * 4. if the first is 0 or 1, the second must be <40
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   593
     */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   595
    /**
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   596
     * Check the DER encoding. Since DER encoding defines that the integer bits
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   597
     * are unsigned, so there's no need to check the MSB.
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   598
     */
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   599
    private static void check(byte[] encoding) throws IOException {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   600
        int length = encoding.length;
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   601
        if (length < 1 ||      // too short
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   602
                (encoding[length - 1] & 0x80) != 0) {  // not ended
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   603
            throw new IOException("ObjectIdentifier() -- " +
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   604
                    "Invalid DER encoding, not ended");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   605
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   606
        for (int i=0; i<length; i++) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   607
            // 0x80 at the beginning of a subidentifier
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   608
            if (encoding[i] == (byte)0x80 &&
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   609
                    (i==0 || (encoding[i-1] & 0x80) == 0)) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   610
                throw new IOException("ObjectIdentifier() -- " +
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   611
                        "Invalid DER encoding, useless extra octet detected");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   612
            }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   613
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   614
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   615
    private static void checkCount(int count) throws IOException {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   616
        if (count < 2) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   617
            throw new IOException("ObjectIdentifier() -- " +
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   618
                    "Must be at least two oid components ");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   619
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   620
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   621
    private static void checkFirstComponent(int first) throws IOException {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   622
        if (first < 0 || first > 2) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   623
            throw new IOException("ObjectIdentifier() -- " +
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   624
                    "First oid component is invalid ");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   625
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   626
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   627
    private static void checkFirstComponent(BigInteger first) throws IOException {
37361
a790f7bc3878 8072452: Support DHE sizes up to 8192-bits and DSA sizes up to 3072-bits
xuelei
parents: 32649
diff changeset
   628
        if (first.signum() == -1 || first.compareTo(BigInteger.TWO) > 0) {
2584
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   629
            throw new IOException("ObjectIdentifier() -- " +
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   630
                    "First oid component is invalid ");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   631
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   632
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   633
    private static void checkSecondComponent(int first, int second) throws IOException {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   634
        if (second < 0 || first != 2 && second > 39) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   635
            throw new IOException("ObjectIdentifier() -- " +
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   636
                    "Second oid component is invalid ");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   637
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   638
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   639
    private static void checkSecondComponent(int first, BigInteger second) throws IOException {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   640
        if (second.signum() == -1 ||
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   641
                first != 2 &&
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   642
                second.compareTo(BigInteger.valueOf(39)) == 1) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   643
            throw new IOException("ObjectIdentifier() -- " +
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   644
                    "Second oid component is invalid ");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   645
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   646
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   647
    private static void checkOtherComponent(int i, int num) throws IOException {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   648
        if (num < 0) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   649
            throw new IOException("ObjectIdentifier() -- " +
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   650
                    "oid component #" + (i+1) + " must be non-negative ");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   651
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   652
    }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   653
    private static void checkOtherComponent(int i, BigInteger num) throws IOException {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   654
        if (num.signum() == -1) {
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   655
            throw new IOException("ObjectIdentifier() -- " +
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   656
                    "oid component #" + (i+1) + " must be non-negative ");
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   657
        }
a89e7cabf6fd 4811968: ASN.1 (X509Certificate) implementations don't handle large OID components
weijun
parents: 2
diff changeset
   658
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
}