src/java.base/share/classes/sun/security/util/DerIndefLenConverter.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 55714 e17ec6bc670a
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2019, 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;
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
    29
import java.io.InputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.ArrayList;
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
    31
import java.util.Arrays;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * A package private utility class to convert indefinite length DER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * encoded byte arrays to definite length DER encoded byte arrays.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This assumes that the basic data structure is "tag, length, value"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * triplet. In the case where the length is "indefinite", terminating
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * end-of-contents bytes are expected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author Hemma Prafullchandra
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
class DerIndefLenConverter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final int TAG_MASK            = 0x1f; // bits 5-1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final int FORM_MASK           = 0x20; // bits 6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final int CLASS_MASK          = 0xC0; // bits 8 and 7
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private static final int LEN_LONG            = 0x80; // bit 8 set
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final int LEN_MASK            = 0x7f; // bits 7 - 1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final int SKIP_EOC_BYTES      = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private byte[] data, newData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private int newDataPos, dataPos, dataSize, index;
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
    55
    private int unresolved = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private ArrayList<Object> ndefsList = new ArrayList<Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private int numOfTotalLenBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private boolean isEOC(int tag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        return (((tag & TAG_MASK) == 0x00) &&  // EOC
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                ((tag & FORM_MASK) == 0x00) && // primitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                ((tag & CLASS_MASK) == 0x00)); // universal
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // if bit 8 is set then it implies either indefinite length or long form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    static boolean isLongForm(int lengthByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        return ((lengthByte & LEN_LONG) == LEN_LONG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Default package private constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    DerIndefLenConverter() { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Checks whether the given length byte is of the form
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <em>Indefinite</em>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param lengthByte the length byte from a DER encoded
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *        object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @return true if the byte is of Indefinite form otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *         returns false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static boolean isIndefinite(int lengthByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        return (isLongForm(lengthByte) && ((lengthByte & LEN_MASK) == 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Parse the tag and if it is an end-of-contents tag then
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * add the current position to the <code>eocList</code> vector.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private void parseTag() throws IOException {
55714
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
    95
        if (isEOC(data[dataPos]) && (data[dataPos + 1] == 0)) {
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
    96
            int numOfEncapsulatedLenBytes = 0;
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
    97
            Object elem = null;
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
    98
            int index;
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
    99
            for (index = ndefsList.size()-1; index >= 0; index--) {
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   100
                // Determine the first element in the vector that does not
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   101
                // have a matching EOC
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   102
                elem = ndefsList.get(index);
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   103
                if (elem instanceof Integer) {
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   104
                    break;
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   105
                } else {
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   106
                    numOfEncapsulatedLenBytes += ((byte[])elem).length - 3;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                }
55714
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   108
            }
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   109
            if (index < 0) {
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   110
                throw new IOException("EOC does not have matching " +
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   111
                                      "indefinite-length tag");
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   112
            }
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   113
            int sectionLen = dataPos - ((Integer)elem).intValue() +
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   114
                             numOfEncapsulatedLenBytes;
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   115
            byte[] sectionLenBytes = getLengthBytes(sectionLen);
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   116
            ndefsList.set(index, sectionLenBytes);
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   117
            unresolved--;
55707
b8a12f53226e 8219775: Certificate validation improvements
jnimeh
parents: 54372
diff changeset
   118
55714
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   119
            // Add the number of bytes required to represent this section
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   120
            // to the total number of length bytes,
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   121
            // and subtract the indefinite-length tag (1 byte) and
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   122
            // EOC bytes (2 bytes) for this section
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   123
            numOfTotalLenBytes += (sectionLenBytes.length - 3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
55714
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   125
        dataPos++;
2
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.
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   146
     *
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   147
     * @return the length of definite length data next, or -1 if there is
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   148
     *         not enough bytes to determine it
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   149
     * @throws IOException if invalid data is read
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private int parseLength() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int curLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (dataPos == dataSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            return curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        int lenByte = data[dataPos++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (isIndefinite(lenByte)) {
25522
10d789df41bb 8049892: Replace uses of 'new Integer()' with appropriate alternative across core classes
prr
parents: 23010
diff changeset
   157
            ndefsList.add(dataPos);
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   158
            unresolved++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            return curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (isLongForm(lenByte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            lenByte &= LEN_MASK;
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   163
            if (lenByte > 4) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                throw new IOException("Too much data");
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   165
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   166
            if ((dataSize - dataPos) < (lenByte + 1)) {
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   167
                return -1;
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   168
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   169
            for (int i = 0; i < lenByte; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                curLen = (curLen << 8) + (data[dataPos++] & 0xff);
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   171
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   172
            if (curLen < 0) {
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   173
                throw new IOException("Invalid length bytes");
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   174
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
           curLen = (lenByte & LEN_MASK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        return curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * Write the length and if it is an indefinite length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * then calculate the definite length from the positions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * of the indefinite length and its matching EOC terminator.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * Then, write the value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private void writeLengthAndValue() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (dataPos == dataSize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int curLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        int lenByte = data[dataPos++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        if (isIndefinite(lenByte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            byte[] lenBytes = (byte[])ndefsList.get(index++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            System.arraycopy(lenBytes, 0, newData, newDataPos,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                             lenBytes.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            newDataPos += lenBytes.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        if (isLongForm(lenByte)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            lenByte &= LEN_MASK;
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   201
            for (int i = 0; i < lenByte; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                curLen = (curLen << 8) + (data[dataPos++] & 0xff);
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   203
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   204
            if (curLen < 0) {
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   205
                throw new IOException("Invalid length bytes");
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   206
            }
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   207
        } else {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            curLen = (lenByte & LEN_MASK);
28551
6533404b7ce1 8059485: Resolve parsing ambiguity
weijun
parents: 25859
diff changeset
   209
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        writeLength(curLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        writeValue(curLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private void writeLength(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (curLen < 128) {
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 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            newData[newDataPos++] = (byte)0x81;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        } else if (curLen < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            newData[newDataPos++] = (byte)0x82;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            newData[newDataPos++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        } else if (curLen < (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            newData[newDataPos++] = (byte)0x83;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            newData[newDataPos++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            newData[newDataPos++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            newData[newDataPos++] = (byte)0x84;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            newData[newDataPos++] = (byte)(curLen >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            newData[newDataPos++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            newData[newDataPos++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            newData[newDataPos++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private byte[] getLengthBytes(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        byte[] lenBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        int index = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (curLen < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            lenBytes = new byte[1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        } else if (curLen < (1 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            lenBytes = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            lenBytes[index++] = (byte)0x81;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        } else if (curLen < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            lenBytes = new byte[3];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            lenBytes[index++] = (byte)0x82;
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
        } else if (curLen < (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            lenBytes = new byte[4];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            lenBytes[index++] = (byte)0x83;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            lenBytes[index++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            lenBytes[index++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            lenBytes = new byte[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            lenBytes[index++] = (byte)0x84;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            lenBytes[index++] = (byte)(curLen >> 24);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            lenBytes[index++] = (byte)(curLen >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            lenBytes[index++] = (byte)(curLen >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            lenBytes[index++] = (byte)curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        return lenBytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    // Returns the number of bytes needed to represent the given length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    // in ASN.1 notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    private int getNumOfLenBytes(int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int numOfLenBytes = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        if (len < 128) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            numOfLenBytes = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        } else if (len < (1 << 8)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            numOfLenBytes = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        } else if (len < (1 << 16)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            numOfLenBytes = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        } else if (len < (1 << 24)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            numOfLenBytes = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            numOfLenBytes = 5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        return numOfLenBytes;
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
     * Parse the value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    private void parseValue(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        dataPos += curLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Write the value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    private void writeValue(int curLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        for (int i=0; i < curLen; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            newData[newDataPos++] = data[dataPos++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * Converts a indefinite length DER encoded byte array to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * a definte length DER encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @param indefData the byte array holding the indefinite
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *        length encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @return the byte array containing the definite length
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   321
     *         DER encoding, or null if there is not enough data.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * @exception IOException on parsing or re-writing errors.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     */
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   324
    byte[] convertBytes(byte[] indefData) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        data = indefData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        dataPos=0; index=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        dataSize = data.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        int len=0;
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   329
        int unused = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        // parse and set up the vectors of all the indefinite-lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        while (dataPos < dataSize) {
55714
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   333
            if (dataPos + 2 > dataSize) {
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   334
                // There should be at least one tag and one length
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   335
                return null;
e17ec6bc670a 8222751: closed/test/jdk/sun/security/util/DerIndefLenConverter/IndefBerPkcs12.java fail
weijun
parents: 55707
diff changeset
   336
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
            parseTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            len = parseLength();
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   339
            if (len < 0) {
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   340
                return null;
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   341
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            parseValue(len);
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   343
            if (unresolved == 0) {
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   344
                unused = dataSize - dataPos;
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   345
                dataSize = dataPos;
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   346
                break;
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   347
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
16082
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   350
        if (unresolved != 0) {
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   351
            return null;
16082
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   352
        }
a042f1412a78 8000210: Improve JarFile code quality
weijun
parents: 5506
diff changeset
   353
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   354
        newData = new byte[dataSize + numOfTotalLenBytes + unused];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        dataPos=0; newDataPos=0; index=0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // write out the new byte array replacing all the indefinite-lengths
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        // and EOCs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        while (dataPos < dataSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
           writeTag();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
           writeLengthAndValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
1093
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   363
        System.arraycopy(indefData, dataSize,
b7d502a05abf 6731685: CertificateFactory.generateCertificates throws IOException on PKCS7 cert chain
weijun
parents: 2
diff changeset
   364
                         newData, dataSize + numOfTotalLenBytes, unused);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        return newData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    }
54372
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   368
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   369
    /**
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   370
     * Read the input stream into a DER byte array. If an indef len BER is
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   371
     * not resolved this method will try to read more data until EOF is reached.
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   372
     * This may block.
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   373
     *
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   374
     * @param in the input stream with tag and lenByte already read
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   375
     * @param lenByte the length of the length field to remember
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   376
     * @param tag the tag to remember
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   377
     * @return a DER byte array
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   378
     * @throws IOException if not all indef len BER
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   379
     *         can be resolved or another I/O error happens
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   380
     */
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   381
    public static byte[] convertStream(InputStream in, byte lenByte, byte tag)
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   382
            throws IOException {
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   383
        int offset = 2;     // for tag and length bytes
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   384
        int readLen = in.available();
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   385
        byte[] indefData = new byte[readLen + offset];
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   386
        indefData[0] = tag;
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   387
        indefData[1] = lenByte;
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   388
        while (true) {
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   389
            int bytesRead = in.readNBytes(indefData, offset, readLen);
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   390
            if (bytesRead != readLen) {
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   391
                readLen = bytesRead;
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   392
                indefData = Arrays.copyOf(indefData, offset + bytesRead);
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   393
            }
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   394
            DerIndefLenConverter derIn = new DerIndefLenConverter();
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   395
            byte[] result = derIn.convertBytes(indefData);
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   396
            if (result == null) {
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   397
                int next = in.read(); // This could block, but we need more
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   398
                if (next == -1) {
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   399
                    throw new IOException("not all indef len BER resolved");
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   400
                }
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   401
                int more = in.available();
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   402
                // expand array to include next and more
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   403
                indefData = Arrays.copyOf(indefData, offset + readLen + 1 + more);
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   404
                indefData[offset + readLen] = (byte)next;
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   405
                offset = offset + readLen + 1;
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   406
                readLen = more;
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   407
            } else {
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   408
                return result;
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   409
            }
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   410
        }
9ac5d41abf68 8157404: Unable to read certain PKCS12 keystores from SequenceInputStream
weijun
parents: 47216
diff changeset
   411
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
}