jdk/src/java.base/share/classes/sun/security/util/DerIndefLenConverter.java
author weijun
Wed, 08 Oct 2014 19:13:57 +0800
changeset 28551 6533404b7ce1
parent 25859 3317bb8137f4
permissions -rw-r--r--
8059485: Resolve parsing ambiguity Reviewed-by: mullan, vinnie
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 16082
diff changeset
     2
 * Copyright (c) 1998, 2012, 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: 1093
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: 1093
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: 1093
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1093
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1093
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.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A package private utility class to convert indefinite length DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * encoded byte arrays to definite length DER encoded byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * This assumes that the basic data structure is "tag, length, value"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * triplet. In the case where the length is "indefinite", terminating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * end-of-contents bytes are expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
class DerIndefLenConverter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final int TAG_MASK            = 0x1f; // bits 5-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final int FORM_MASK           = 0x20; // bits 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final int CLASS_MASK          = 0xC0; // bits 8 and 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int LEN_LONG            = 0x80; // bit 8 set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final int LEN_MASK            = 0x7f; // bits 7 - 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int SKIP_EOC_BYTES      = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private byte[] data, newData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int newDataPos, dataPos, dataSize, index;
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
    53
    private int unresolved = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private ArrayList<Object> ndefsList = new ArrayList<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private int numOfTotalLenBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean isEOC(int tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        return (((tag & TAG_MASK) == 0x00) &&  // EOC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                ((tag & FORM_MASK) == 0x00) && // primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                ((tag & CLASS_MASK) == 0x00)); // universal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // if bit 8 is set then it implies either indefinite length or long form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static boolean isLongForm(int lengthByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return ((lengthByte & LEN_LONG) == LEN_LONG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Default package private constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    DerIndefLenConverter() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Checks whether the given length byte is of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * <em>Indefinite</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param lengthByte the length byte from a DER encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *        object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @return true if the byte is of Indefinite form otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *         returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    static boolean isIndefinite(int lengthByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        return (isLongForm(lengthByte) && ((lengthByte & LEN_MASK) == 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Parse the tag and if it is an end-of-contents tag then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * add the current position to the <code>eocList</code> vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private void parseTag() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (dataPos == dataSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (isEOC(data[dataPos]) && (data[dataPos + 1] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            int numOfEncapsulatedLenBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            Object elem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            int index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            for (index = ndefsList.size()-1; index >= 0; index--) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                // Determine the first element in the vector that does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                // have a matching EOC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                elem = ndefsList.get(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                if (elem instanceof Integer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                    numOfEncapsulatedLenBytes += ((byte[])elem).length - 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (index < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                throw new IOException("EOC does not have matching " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                                      "indefinite-length tag");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            int sectionLen = dataPos - ((Integer)elem).intValue() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                             numOfEncapsulatedLenBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            byte[] sectionLenBytes = getLengthBytes(sectionLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            ndefsList.set(index, sectionLenBytes);
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   117
            unresolved--;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // Add the number of bytes required to represent this section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // to the total number of length bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // and subtract the indefinite-length tag (1 byte) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            // EOC bytes (2 bytes) for this section
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            numOfTotalLenBytes += (sectionLenBytes.length - 3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        dataPos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * Write the tag and if it is an end-of-contents tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * then skip the tag and its 1 byte length of zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private void writeTag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (dataPos == dataSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int tag = data[dataPos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (isEOC(tag) && (data[dataPos] == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            dataPos++;  // skip length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            writeTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            newData[newDataPos++] = (byte)tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Parse the length and if it is an indefinite length then add
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * the current position to the <code>ndefsList</code> vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private int parseLength() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int curLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (dataPos == dataSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            return curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int lenByte = data[dataPos++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        if (isIndefinite(lenByte)) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   153
            ndefsList.add(dataPos);
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   154
            unresolved++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            return curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        if (isLongForm(lenByte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            lenByte &= LEN_MASK;
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   159
            if (lenByte > 4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                throw new IOException("Too much data");
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   161
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   162
            if ((dataSize - dataPos) < (lenByte + 1)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw new IOException("Too little data");
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   164
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   165
            for (int i = 0; i < lenByte; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                curLen = (curLen << 8) + (data[dataPos++] & 0xff);
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   167
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   168
            if (curLen < 0) {
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   169
                throw new IOException("Invalid length bytes");
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   170
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
           curLen = (lenByte & LEN_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Write the length and if it is an indefinite length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * then calculate the definite length from the positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * of the indefinite length and its matching EOC terminator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * Then, write the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private void writeLengthAndValue() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (dataPos == dataSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        int curLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        int lenByte = data[dataPos++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (isIndefinite(lenByte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            byte[] lenBytes = (byte[])ndefsList.get(index++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            System.arraycopy(lenBytes, 0, newData, newDataPos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                             lenBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            newDataPos += lenBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        if (isLongForm(lenByte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            lenByte &= LEN_MASK;
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   197
            for (int i = 0; i < lenByte; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                curLen = (curLen << 8) + (data[dataPos++] & 0xff);
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   199
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   200
            if (curLen < 0) {
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   201
                throw new IOException("Invalid length bytes");
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   202
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   203
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            curLen = (lenByte & LEN_MASK);
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   205
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        writeLength(curLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        writeValue(curLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    private void writeLength(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        if (curLen < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        } else if (curLen < (1 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            newData[newDataPos++] = (byte)0x81;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        } else if (curLen < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            newData[newDataPos++] = (byte)0x82;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            newData[newDataPos++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        } else if (curLen < (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            newData[newDataPos++] = (byte)0x83;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            newData[newDataPos++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            newData[newDataPos++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            newData[newDataPos++] = (byte)0x84;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            newData[newDataPos++] = (byte)(curLen >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            newData[newDataPos++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            newData[newDataPos++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    private byte[] getLengthBytes(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        byte[] lenBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        if (curLen < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            lenBytes = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        } else if (curLen < (1 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            lenBytes = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            lenBytes[index++] = (byte)0x81;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        } else if (curLen < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            lenBytes = new byte[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            lenBytes[index++] = (byte)0x82;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            lenBytes[index++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else if (curLen < (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            lenBytes = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            lenBytes[index++] = (byte)0x83;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            lenBytes[index++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            lenBytes[index++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            lenBytes = new byte[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            lenBytes[index++] = (byte)0x84;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            lenBytes[index++] = (byte)(curLen >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            lenBytes[index++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            lenBytes[index++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        return lenBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // Returns the number of bytes needed to represent the given length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // in ASN.1 notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    private int getNumOfLenBytes(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        int numOfLenBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        if (len < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            numOfLenBytes = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        } else if (len < (1 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            numOfLenBytes = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        } else if (len < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            numOfLenBytes = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } else if (len < (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            numOfLenBytes = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            numOfLenBytes = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return numOfLenBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * Parse the value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    private void parseValue(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        dataPos += curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * Write the value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    private void writeValue(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        for (int i=0; i < curLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            newData[newDataPos++] = data[dataPos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * Converts a indefinite length DER encoded byte array to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * a definte length DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param indefData the byte array holding the indefinite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *        length encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @return the byte array containing the definite length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *         DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @exception IOException on parsing or re-writing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    byte[] convert(byte[] indefData) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        data = indefData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        dataPos=0; index=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        dataSize = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        int len=0;
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   325
        int unused = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        // parse and set up the vectors of all the indefinite-lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        while (dataPos < dataSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            parseTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            parseValue(len);
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   332
            if (unresolved == 0) {
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   333
                unused = dataSize - dataPos;
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   334
                dataSize = dataPos;
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   335
                break;
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   336
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
16082
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   339
        if (unresolved != 0) {
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   340
            throw new IOException("not all indef len BER resolved");
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   341
        }
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   342
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   343
        newData = new byte[dataSize + numOfTotalLenBytes + unused];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        dataPos=0; newDataPos=0; index=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        // write out the new byte array replacing all the indefinite-lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        // and EOCs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        while (dataPos < dataSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
           writeTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
           writeLengthAndValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        }
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   352
        System.arraycopy(indefData, dataSize,
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   353
                         newData, dataSize + numOfTotalLenBytes, unused);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return newData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
}