jdk/src/share/classes/sun/security/util/DerIndefLenConverter.java
author prr
Tue, 15 Jul 2014 11:22:14 -0700
changeset 25522 10d789df41bb
parent 23010 6dadb192ad81
permissions -rw-r--r--
8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes Reviewed-by: psandoz, prr Contributed-by: otaviopolianasantana@gmail.com
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;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (lenByte > 4)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                throw new IOException("Too much data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            if ((dataSize - dataPos) < (lenByte + 1))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                throw new IOException("Too little data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            for (int i = 0; i < lenByte; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                curLen = (curLen << 8) + (data[dataPos++] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
           curLen = (lenByte & LEN_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        return curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Write the length and if it is an indefinite length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * then calculate the definite length from the positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * of the indefinite length and its matching EOC terminator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Then, write the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private void writeLengthAndValue() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (dataPos == dataSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int curLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        int lenByte = data[dataPos++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (isIndefinite(lenByte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            byte[] lenBytes = (byte[])ndefsList.get(index++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            System.arraycopy(lenBytes, 0, newData, newDataPos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                             lenBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            newDataPos += lenBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (isLongForm(lenByte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            lenByte &= LEN_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            for (int i = 0; i < lenByte; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                curLen = (curLen << 8) + (data[dataPos++] & 0xff);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            curLen = (lenByte & LEN_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        writeLength(curLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        writeValue(curLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    private void writeLength(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (curLen < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else if (curLen < (1 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            newData[newDataPos++] = (byte)0x81;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else if (curLen < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            newData[newDataPos++] = (byte)0x82;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            newData[newDataPos++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        } else if (curLen < (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            newData[newDataPos++] = (byte)0x83;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            newData[newDataPos++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            newData[newDataPos++] = (byte)(curLen >> 8);
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 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            newData[newDataPos++] = (byte)0x84;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            newData[newDataPos++] = (byte)(curLen >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            newData[newDataPos++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            newData[newDataPos++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    private byte[] getLengthBytes(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        byte[] lenBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (curLen < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            lenBytes = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        } else if (curLen < (1 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            lenBytes = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            lenBytes[index++] = (byte)0x81;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        } else if (curLen < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            lenBytes = new byte[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            lenBytes[index++] = (byte)0x82;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            lenBytes[index++] = (byte)(curLen >> 8);
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 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            lenBytes = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            lenBytes[index++] = (byte)0x83;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            lenBytes[index++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            lenBytes[index++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            lenBytes = new byte[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            lenBytes[index++] = (byte)0x84;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            lenBytes[index++] = (byte)(curLen >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            lenBytes[index++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            lenBytes[index++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return lenBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    // Returns the number of bytes needed to represent the given length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    // in ASN.1 notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private int getNumOfLenBytes(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        int numOfLenBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        if (len < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            numOfLenBytes = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        } else if (len < (1 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            numOfLenBytes = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        } else if (len < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            numOfLenBytes = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        } else if (len < (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            numOfLenBytes = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            numOfLenBytes = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return numOfLenBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * Parse the value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    private void parseValue(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        dataPos += curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * Write the value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    private void writeValue(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        for (int i=0; i < curLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            newData[newDataPos++] = data[dataPos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Converts a indefinite length DER encoded byte array to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * a definte length DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param indefData the byte array holding the indefinite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *        length encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @return the byte array containing the definite length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *         DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @exception IOException on parsing or re-writing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    byte[] convert(byte[] indefData) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        data = indefData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        dataPos=0; index=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        dataSize = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        int len=0;
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   314
        int unused = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        // parse and set up the vectors of all the indefinite-lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        while (dataPos < dataSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            parseTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            len = parseLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            parseValue(len);
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   321
            if (unresolved == 0) {
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   322
                unused = dataSize - dataPos;
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   323
                dataSize = dataPos;
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   324
                break;
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   325
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
16082
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   328
        if (unresolved != 0) {
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   329
            throw new IOException("not all indef len BER resolved");
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   330
        }
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   331
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   332
        newData = new byte[dataSize + numOfTotalLenBytes + unused];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        dataPos=0; newDataPos=0; index=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // write out the new byte array replacing all the indefinite-lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // and EOCs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        while (dataPos < dataSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
           writeTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
           writeLengthAndValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   341
        System.arraycopy(indefData, dataSize,
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   342
                         newData, dataSize + numOfTotalLenBytes, unused);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        return newData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
}