jdk/src/java.base/share/classes/sun/security/util/DerInputBuffer.java
author juh
Tue, 07 Oct 2014 22:23:19 -0700
changeset 26967 c182469301ee
parent 25859 3317bb8137f4
child 43214 3dd7af9b9e05
permissions -rw-r--r--
8037550: Update RFC references in javadoc to RFC 5280 Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
     2
 * Copyright (c) 1996, 2014, 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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import sun.util.calendar.CalendarDate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.util.calendar.CalendarSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * DER input buffer ... this is the main abstraction in the DER library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * which actively works with the "untyped byte stream" abstraction.  It
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * does so with impunity, since it's not intended to be exposed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * anyone who could violate the "typed value stream" DER model and hence
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * corrupt the input stream of DER values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class DerInputBuffer extends ByteArrayInputStream implements Cloneable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    DerInputBuffer(byte[] buf) { super(buf); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    DerInputBuffer(byte[] buf, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        super(buf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    DerInputBuffer dup() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            DerInputBuffer retval = (DerInputBuffer)clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            retval.mark(Integer.MAX_VALUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } catch (CloneNotSupportedException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            throw new IllegalArgumentException(e.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    byte[] toByteArray() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int     len = available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (len <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        byte[]  retval = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        System.arraycopy(buf, pos, retval, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    int peek() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (pos >= count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            throw new IOException("out of data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            return buf[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Compares this DerInputBuffer for equality with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public boolean equals(Object other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (other instanceof DerInputBuffer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return equals((DerInputBuffer)other);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    boolean equals(DerInputBuffer other) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (this == other)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        int max = this.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (other.available() != max)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        for (int i = 0; i < max; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            if (this.buf[this.pos + i] != other.buf[other.pos + i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Returns a hashcode for this DerInputBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @return a hashcode for this DerInputBuffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int retval = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        int len = available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        int p = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        for (int i = 0; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            retval += buf[p + i] * i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    void truncate(int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (len > available())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            throw new IOException("insufficient data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        count = pos + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * Returns the integer which takes up the specified number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * of bytes in this buffer as a BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param len the number of bytes to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param makePositive whether to always return a positive value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *   irrespective of actual encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * @return the integer as a BigInteger.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    BigInteger getBigInteger(int len, boolean makePositive) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (len > available())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new IOException("short read of integer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            throw new IOException("Invalid encoding: zero length Int value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        byte[] bytes = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        System.arraycopy(buf, pos, bytes, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        skip(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        if (makePositive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            return new BigInteger(1, bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            return new BigInteger(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Returns the integer which takes up the specified number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * of bytes in this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws IOException if the result is not within the valid
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * range for integer, i.e. between Integer.MIN_VALUE and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * Integer.MAX_VALUE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * @param len the number of bytes to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @return the integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public int getInteger(int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        BigInteger result = getBigInteger(len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (result.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            throw new IOException("Integer below minimum valid value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        if (result.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            throw new IOException("Integer exceeds maximum valid value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return result.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * Returns the bit string which takes up the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * number of bytes in this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    public byte[] getBitString(int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (len > available())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            throw new IOException("short read of bit string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            throw new IOException("Invalid encoding: zero length bit string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        int numOfPadBits = buf[pos];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        if ((numOfPadBits < 0) || (numOfPadBits > 7)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new IOException("Invalid number of padding bits");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // minus the first byte which indicates the number of padding bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        byte[] retval = new byte[len - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        System.arraycopy(buf, pos + 1, retval, 0, len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        if (numOfPadBits != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            // get rid of the padding bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            retval[len - 2] &= (0xff << numOfPadBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        skip(len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return retval;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Returns the bit string which takes up the rest of this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    byte[] getBitString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        return getBitString(available());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * Returns the bit string which takes up the rest of this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * The bit string need not be byte-aligned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    BitArray getUnalignedBitString() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (pos >= count)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
         * Just copy the data into an aligned, padded octet buffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
         * and consume the rest of the buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        int len = available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        int unusedBits = buf[pos] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (unusedBits > 7 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            throw new IOException("Invalid value for unused bits: " + unusedBits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        byte[] bits = new byte[len - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        // number of valid bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        int length = (bits.length == 0) ? 0 : bits.length * 8 - unusedBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        System.arraycopy(buf, pos + 1, bits, 0, len - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        BitArray bitArray = new BitArray(length, bits);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        pos = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        return bitArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Returns the UTC Time value that takes up the specified number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * of bytes in this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param len the number of bytes to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public Date getUTCTime(int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (len > available())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            throw new IOException("short read of DER UTC Time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if (len < 11 || len > 17)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            throw new IOException("DER UTC Time length error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return getTime(len, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns the Generalized Time value that takes up the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * number of bytes in this buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @param len the number of bytes to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public Date getGeneralizedTime(int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        if (len > available())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            throw new IOException("short read of DER Generalized Time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (len < 13 || len > 23)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            throw new IOException("DER Generalized Time length error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return getTime(len, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Private helper routine to extract time from the der value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param len the number of bytes to use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @param generalized true if Generalized Time is to be read, false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * if UTC Time is to be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    private Date getTime(int len, boolean generalized) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
         * UTC time encoded as ASCII chars:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
         *       YYMMDDhhmmZ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
         *       YYMMDDhhmmssZ
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
         *       YYMMDDhhmm+hhmm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
         *       YYMMDDhhmm-hhmm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
         *       YYMMDDhhmmss+hhmm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
         *       YYMMDDhhmmss-hhmm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
         * UTC Time is broken in storing only two digits of year.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
         * If YY < 50, we assume 20YY;
26967
c182469301ee 8037550: Update RFC references in javadoc to RFC 5280
juh
parents: 25859
diff changeset
   288
         * if YY >= 50, we assume 19YY, as per RFC 5280.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
         * Generalized time has a four-digit year and allows any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
         * precision specified in ISO 8601. However, for our purposes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
         * we will only allow the same format as UTC time, except that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
         * fractional seconds (millisecond precision) are supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        int year, month, day, hour, minute, second, millis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        String type = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (generalized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            type = "Generalized";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            year = 1000 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            year += 100 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            year += 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            year += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            len -= 2; // For the two extra YY
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            type = "UTC";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            year = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            year += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            if (year < 50)              // origin 2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                year += 2000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
                year += 1900;   // origin 1900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        month = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        month += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        day = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        day += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        hour = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        hour += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        minute = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        minute += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        len -= 10; // YYMMDDhhmm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
         * We allow for non-encoded seconds, even though the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
         * IETF-PKIX specification says that the seconds should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
         * always be encoded even if it is zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        millis = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (len > 2 && len < 12) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            second = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            second += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            len -= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            // handle fractional seconds (if present)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            if (buf[pos] == '.' || buf[pos] == ',') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                len --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                // handle upto milisecond precision only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                int precision = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                int peek = pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                while (buf[peek] != 'Z' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                       buf[peek] != '+' &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                       buf[peek] != '-') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    peek++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    precision++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                switch (precision) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                case 3:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    millis += 100 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    millis += 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    millis += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    millis += 100 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    millis += 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    millis += 100 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                        throw new IOException("Parse " + type +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                            " time, unsupported precision for seconds value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                len -= precision;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        } else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            second = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        if (month == 0 || day == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            || month > 12 || day > 31
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            || hour >= 24 || minute >= 60 || second >= 60)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new IOException("Parse " + type + " time, invalid format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         * Generalized time can theoretically allow any precision,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
         * but we're not supporting that.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        CalendarSystem gcal = CalendarSystem.getGregorianCalendar();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        CalendarDate date = gcal.newCalendarDate(null); // no time zone
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        date.setDate(year, month, day);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        date.setTimeOfDay(hour, minute, second, millis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        long time = gcal.getTime(date);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
         * Finally, "Z" or "+hhmm" or "-hhmm" ... offsets change hhmm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        if (! (len == 1 || len == 5))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throw new IOException("Parse " + type + " time, invalid offset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        int hr, min;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        switch (buf[pos++]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        case '+':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            hr = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            hr += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            min = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            min += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            if (hr >= 24 || min >= 60)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                throw new IOException("Parse " + type + " time, +hhmm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            time -= ((hr * 60) + min) * 60 * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        case '-':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            hr = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            hr += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            min = 10 * Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            min += Character.digit((char)buf[pos++], 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            if (hr >= 24 || min >= 60)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                throw new IOException("Parse " + type + " time, -hhmm");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            time += ((hr * 60) + min) * 60 * 1000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        case 'Z':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            throw new IOException("Parse " + type + " time, garbage offset");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
        return new Date(time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
}