Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/security/util/DerInputBuffer.java
38830 views
/*1* Copyright (c) 1996, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.util;2627import java.io.ByteArrayInputStream;28import java.io.IOException;29import java.math.BigInteger;30import java.util.Date;31import sun.util.calendar.CalendarDate;32import sun.util.calendar.CalendarSystem;3334/**35* DER input buffer ... this is the main abstraction in the DER library36* which actively works with the "untyped byte stream" abstraction. It37* does so with impunity, since it's not intended to be exposed to38* anyone who could violate the "typed value stream" DER model and hence39* corrupt the input stream of DER values.40*41* @author David Brownell42*/43class DerInputBuffer extends ByteArrayInputStream implements Cloneable {4445boolean allowBER = true;4647// used by sun/security/util/DerInputBuffer/DerInputBufferEqualsHashCode.java48DerInputBuffer(byte[] buf) {49this(buf, true);50}5152DerInputBuffer(byte[] buf, boolean allowBER) {53super(buf);54this.allowBER = allowBER;55}5657DerInputBuffer(byte[] buf, int offset, int len, boolean allowBER) {58super(buf, offset, len);59this.allowBER = allowBER;60}6162DerInputBuffer dup() {63try {64DerInputBuffer retval = (DerInputBuffer)clone();65retval.mark(Integer.MAX_VALUE);66return retval;67} catch (CloneNotSupportedException e) {68throw new IllegalArgumentException(e.toString());69}70}7172byte[] toByteArray() {73int len = available();74if (len <= 0)75return null;76byte[] retval = new byte[len];7778System.arraycopy(buf, pos, retval, 0, len);79return retval;80}8182int peek() throws IOException {83if (pos >= count)84throw new IOException("out of data");85else86return buf[pos];87}8889/**90* Compares this DerInputBuffer for equality with the specified91* object.92*/93public boolean equals(Object other) {94if (other instanceof DerInputBuffer)95return equals((DerInputBuffer)other);96else97return false;98}99100boolean equals(DerInputBuffer other) {101if (this == other)102return true;103104int max = this.available();105if (other.available() != max)106return false;107for (int i = 0; i < max; i++) {108if (this.buf[this.pos + i] != other.buf[other.pos + i]) {109return false;110}111}112return true;113}114115/**116* Returns a hashcode for this DerInputBuffer.117*118* @return a hashcode for this DerInputBuffer.119*/120public int hashCode() {121int retval = 0;122123int len = available();124int p = pos;125126for (int i = 0; i < len; i++)127retval += buf[p + i] * i;128return retval;129}130131void truncate(int len) throws IOException {132if (len > available())133throw new IOException("insufficient data");134count = pos + len;135}136137/**138* Returns the integer which takes up the specified number139* of bytes in this buffer as a BigInteger.140* @param len the number of bytes to use.141* @param makePositive whether to always return a positive value,142* irrespective of actual encoding143* @return the integer as a BigInteger.144*/145BigInteger getBigInteger(int len, boolean makePositive) throws IOException {146if (len > available())147throw new IOException("short read of integer");148149if (len == 0) {150throw new IOException("Invalid encoding: zero length Int value");151}152153byte[] bytes = new byte[len];154155System.arraycopy(buf, pos, bytes, 0, len);156skip(len);157158// BER allows leading 0s but DER does not159if (!allowBER && (len >= 2 && (bytes[0] == 0) && (bytes[1] >= 0))) {160throw new IOException("Invalid encoding: redundant leading 0s");161}162163if (makePositive) {164return new BigInteger(1, bytes);165} else {166return new BigInteger(bytes);167}168}169170/**171* Returns the integer which takes up the specified number172* of bytes in this buffer.173* @throws IOException if the result is not within the valid174* range for integer, i.e. between Integer.MIN_VALUE and175* Integer.MAX_VALUE.176* @param len the number of bytes to use.177* @return the integer.178*/179public int getInteger(int len) throws IOException {180181BigInteger result = getBigInteger(len, false);182if (result.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) < 0) {183throw new IOException("Integer below minimum valid value");184}185if (result.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) > 0) {186throw new IOException("Integer exceeds maximum valid value");187}188return result.intValue();189}190191/**192* Returns the bit string which takes up the specified193* number of bytes in this buffer.194*/195public byte[] getBitString(int len) throws IOException {196if (len > available())197throw new IOException("short read of bit string");198199if (len == 0) {200throw new IOException("Invalid encoding: zero length bit string");201}202203int numOfPadBits = buf[pos];204if ((numOfPadBits < 0) || (numOfPadBits > 7)) {205throw new IOException("Invalid number of padding bits");206}207// minus the first byte which indicates the number of padding bits208byte[] retval = new byte[len - 1];209System.arraycopy(buf, pos + 1, retval, 0, len - 1);210if (numOfPadBits != 0) {211// get rid of the padding bits212retval[len - 2] &= (0xff << numOfPadBits);213}214skip(len);215return retval;216}217218/**219* Returns the bit string which takes up the rest of this buffer.220*/221byte[] getBitString() throws IOException {222return getBitString(available());223}224225/**226* Returns the bit string which takes up the rest of this buffer.227* The bit string need not be byte-aligned.228*/229BitArray getUnalignedBitString() throws IOException {230if (pos >= count)231return null;232/*233* Just copy the data into an aligned, padded octet buffer,234* and consume the rest of the buffer.235*/236int len = available();237int unusedBits = buf[pos] & 0xff;238if (unusedBits > 7 ) {239throw new IOException("Invalid value for unused bits: " + unusedBits);240}241byte[] bits = new byte[len - 1];242// number of valid bits243int length = (bits.length == 0) ? 0 : bits.length * 8 - unusedBits;244245System.arraycopy(buf, pos + 1, bits, 0, len - 1);246247BitArray bitArray = new BitArray(length, bits);248pos = count;249return bitArray;250}251252/**253* Returns the UTC Time value that takes up the specified number254* of bytes in this buffer.255* @param len the number of bytes to use256*/257public Date getUTCTime(int len) throws IOException {258if (len > available())259throw new IOException("short read of DER UTC Time");260261if (len < 11 || len > 17)262throw new IOException("DER UTC Time length error");263264return getTime(len, false);265}266267/**268* Returns the Generalized Time value that takes up the specified269* number of bytes in this buffer.270* @param len the number of bytes to use271*/272public Date getGeneralizedTime(int len) throws IOException {273if (len > available())274throw new IOException("short read of DER Generalized Time");275276if (len < 13)277throw new IOException("DER Generalized Time length error");278279return getTime(len, true);280281}282283/**284* Private helper routine to extract time from the der value.285* @param len the number of bytes to use286* @param generalized true if Generalized Time is to be read, false287* if UTC Time is to be read.288*/289private Date getTime(int len, boolean generalized) throws IOException {290291/*292* UTC time encoded as ASCII chars:293* YYMMDDhhmmZ294* YYMMDDhhmmssZ295* YYMMDDhhmm+hhmm296* YYMMDDhhmm-hhmm297* YYMMDDhhmmss+hhmm298* YYMMDDhhmmss-hhmm299* UTC Time is broken in storing only two digits of year.300* If YY < 50, we assume 20YY;301* if YY >= 50, we assume 19YY, as per RFC 5280.302*303* Generalized time has a four-digit year and allows any304* precision specified in ISO 8601. However, for our purposes,305* we will only allow the same format as UTC time, except that306* fractional seconds (millisecond precision) are supported.307*/308309int year, month, day, hour, minute, second, millis;310String type = null;311312if (generalized) {313type = "Generalized";314year = 1000 * toDigit(buf[pos++], type);315year += 100 * toDigit(buf[pos++], type);316year += 10 * toDigit(buf[pos++], type);317year += toDigit(buf[pos++], type);318len -= 2; // For the two extra YY319} else {320type = "UTC";321year = 10 * toDigit(buf[pos++], type);322year += toDigit(buf[pos++], type);323324if (year < 50) // origin 2000325year += 2000;326else327year += 1900; // origin 1900328}329330month = 10 * toDigit(buf[pos++], type);331month += toDigit(buf[pos++], type);332333day = 10 * toDigit(buf[pos++], type);334day += toDigit(buf[pos++], type);335336hour = 10 * toDigit(buf[pos++], type);337hour += toDigit(buf[pos++], type);338339minute = 10 * toDigit(buf[pos++], type);340minute += toDigit(buf[pos++], type);341342len -= 10; // YYMMDDhhmm343344/*345* We allow for non-encoded seconds, even though the346* IETF-PKIX specification says that the seconds should347* always be encoded even if it is zero.348*/349350millis = 0;351if (len > 2) {352second = 10 * toDigit(buf[pos++], type);353second += toDigit(buf[pos++], type);354len -= 2;355// handle fractional seconds (if present)356if (generalized && (buf[pos] == '.' || buf[pos] == ',')) {357len --;358if (len == 0) {359throw new IOException("Parse " + type +360" time, empty fractional part");361}362pos++;363int precision = 0;364while (buf[pos] != 'Z' &&365buf[pos] != '+' &&366buf[pos] != '-') {367// Validate all digits in the fractional part but368// store millisecond precision only369int thisDigit = toDigit(buf[pos], type);370precision++;371len--;372if (len == 0) {373throw new IOException("Parse " + type +374" time, invalid fractional part");375}376pos++;377switch (precision) {378case 1:379millis += 100 * thisDigit;380break;381case 2:382millis += 10 * thisDigit;383break;384case 3:385millis += thisDigit;386break;387}388}389if (precision == 0) {390throw new IOException("Parse " + type +391" time, empty fractional part");392}393}394} else395second = 0;396397if (month == 0 || day == 0398|| month > 12 || day > 31399|| hour >= 24 || minute >= 60 || second >= 60)400throw new IOException("Parse " + type + " time, invalid format");401402/*403* Generalized time can theoretically allow any precision,404* but we're not supporting that.405*/406CalendarSystem gcal = CalendarSystem.getGregorianCalendar();407CalendarDate date = gcal.newCalendarDate(null); // no time zone408date.setDate(year, month, day);409date.setTimeOfDay(hour, minute, second, millis);410long time = gcal.getTime(date);411412/*413* Finally, "Z" or "+hhmm" or "-hhmm" ... offsets change hhmm414*/415if (! (len == 1 || len == 5))416throw new IOException("Parse " + type + " time, invalid offset");417418int hr, min;419420switch (buf[pos++]) {421case '+':422if (len != 5) {423throw new IOException("Parse " + type + " time, invalid offset");424}425hr = 10 * toDigit(buf[pos++], type);426hr += toDigit(buf[pos++], type);427min = 10 * toDigit(buf[pos++], type);428min += toDigit(buf[pos++], type);429430if (hr >= 24 || min >= 60)431throw new IOException("Parse " + type + " time, +hhmm");432433time -= ((hr * 60) + min) * 60 * 1000;434break;435436case '-':437if (len != 5) {438throw new IOException("Parse " + type + " time, invalid offset");439}440hr = 10 * toDigit(buf[pos++], type);441hr += toDigit(buf[pos++], type);442min = 10 * toDigit(buf[pos++], type);443min += toDigit(buf[pos++], type);444445if (hr >= 24 || min >= 60)446throw new IOException("Parse " + type + " time, -hhmm");447448time += ((hr * 60) + min) * 60 * 1000;449break;450451case 'Z':452if (len != 1) {453throw new IOException("Parse " + type + " time, invalid format");454}455break;456457default:458throw new IOException("Parse " + type + " time, garbage offset");459}460return new Date(time);461}462463/**464* Converts byte (represented as a char) to int.465* @throws IOException if integer is not a valid digit in the specified466* radix (10)467*/468private static int toDigit(byte b, String type) throws IOException {469if (b < '0' || b > '9') {470throw new IOException("Parse " + type + " time, invalid format");471}472return b - '0';473}474}475476477