< prev index next >
src/java.base/share/classes/sun/security/util/DerValue.java
Print this page
@@ -1,7 +1,7 @@
-/*
- * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
+/**
+ * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
@@ -223,31 +223,37 @@
*/
public DerValue(byte stringTag, String value) throws IOException {
data = init(stringTag, value);
}
+ // Creates a DerValue from a tag and some DER-encoded data w/ additional
+ // arg to control whether DER checks are enforced.
+ DerValue(byte tag, byte[] data, boolean allowBER) {
+ this.tag = tag;
+ buffer = new DerInputBuffer(data.clone(), allowBER);
+ length = data.length;
+ this.data = new DerInputStream(buffer);
+ this.data.mark(Integer.MAX_VALUE);
+ }
+
/**
* Creates a DerValue from a tag and some DER-encoded data.
*
* @param tag the DER type tag
* @param data the DER-encoded data
*/
public DerValue(byte tag, byte[] data) {
- this.tag = tag;
- buffer = new DerInputBuffer(data.clone());
- length = data.length;
- this.data = new DerInputStream(buffer);
- this.data.mark(Integer.MAX_VALUE);
+ this(tag, data, true);
}
/*
* package private
*/
DerValue(DerInputBuffer in) throws IOException {
+
// XXX must also parse BER-encoded constructed
// values such as sequences, sets...
-
tag = (byte)in.read();
byte lenByte = (byte)in.read();
length = DerInputStream.getLength(lenByte, in);
if (length == -1) { // indefinite length encoding found
DerInputBuffer inbuf = in.dup();
@@ -258,11 +264,11 @@
indefData[1] = lenByte;
DataInputStream dis = new DataInputStream(inbuf);
dis.readFully(indefData, offset, readLen);
dis.close();
DerIndefLenConverter derIn = new DerIndefLenConverter();
- inbuf = new DerInputBuffer(derIn.convert(indefData));
+ inbuf = new DerInputBuffer(derIn.convert(indefData), in.allowBER);
if (tag != inbuf.read())
throw new IOException
("Indefinite length encoding not supported");
length = DerInputStream.getDefiniteLength(inbuf);
buffer = inbuf.dup();
@@ -280,21 +286,34 @@
in.skip(length);
}
}
+ // Get an ASN.1/DER encoded datum from a buffer w/ additional
+ // arg to control whether DER checks are enforced.
+ DerValue(byte[] buf, boolean allowBER) throws IOException {
+ data = init(true, new ByteArrayInputStream(buf), allowBER);
+ }
+
/**
* Get an ASN.1/DER encoded datum from a buffer. The
* entire buffer must hold exactly one datum, including
* its tag and length.
*
* @param buf buffer holding a single DER-encoded datum.
*/
public DerValue(byte[] buf) throws IOException {
- data = init(true, new ByteArrayInputStream(buf));
+ this(buf, true);
}
+ // Get an ASN.1/DER encoded datum from part of a buffer w/ additional
+ // arg to control whether DER checks are enforced.
+ DerValue(byte[] buf, int offset, int len, boolean allowBER)
+ throws IOException {
+ data = init(true, new ByteArrayInputStream(buf, offset, len), allowBER);
+ }
+
/**
* Get an ASN.1/DER encoded datum from part of a buffer.
* That part of the buffer must hold exactly one datum, including
* its tag and length.
*
@@ -301,13 +320,19 @@
* @param buf the buffer
* @param offset start point of the single DER-encoded dataum
* @param len how many bytes are in the encoded datum
*/
public DerValue(byte[] buf, int offset, int len) throws IOException {
- data = init(true, new ByteArrayInputStream(buf, offset, len));
+ this(buf, offset, len, true);
}
+ // Get an ASN1/DER encoded datum from an input stream w/ additional
+ // arg to control whether DER checks are enforced.
+ DerValue(InputStream in, boolean allowBER) throws IOException {
+ data = init(false, in, allowBER);
+ }
+
/**
* Get an ASN1/DER encoded datum from an input stream. The
* stream may have additional data following the encoded datum.
* In case of indefinite length encoded datum, the input stream
* must hold only one datum.
@@ -314,14 +339,15 @@
*
* @param in the input stream holding a single DER datum,
* which may be followed by additional data
*/
public DerValue(InputStream in) throws IOException {
- data = init(false, in);
+ this(in, true);
}
- private DerInputStream init(byte stringTag, String value) throws IOException {
+ private DerInputStream init(byte stringTag, String value)
+ throws IOException {
String enc = null;
tag = stringTag;
switch (stringTag) {
@@ -345,21 +371,21 @@
throw new IllegalArgumentException("Unsupported DER string type");
}
byte[] buf = value.getBytes(enc);
length = buf.length;
- buffer = new DerInputBuffer(buf);
+ buffer = new DerInputBuffer(buf, true);
DerInputStream result = new DerInputStream(buffer);
result.mark(Integer.MAX_VALUE);
return result;
}
/*
* helper routine
*/
- private DerInputStream init(boolean fullyBuffered, InputStream in)
- throws IOException {
+ private DerInputStream init(boolean fullyBuffered, InputStream in,
+ boolean allowBER) throws IOException {
tag = (byte)in.read();
byte lenByte = (byte)in.read();
length = DerInputStream.getLength(lenByte, in);
if (length == -1) { // indefinite length encoding found
@@ -382,11 +408,11 @@
if (fullyBuffered && in.available() != length)
throw new IOException("extra data given to DerValue constructor");
byte[] bytes = IOUtils.readFully(in, length, true);
- buffer = new DerInputBuffer(bytes);
+ buffer = new DerInputBuffer(bytes, allowBER);
return new DerInputStream(buffer);
}
/**
* Encode an ASN1/DER encoded datum onto a DER output stream.
@@ -477,11 +503,12 @@
return bytes;
}
if (buffer.read(bytes) != length)
throw new IOException("short read on DerValue buffer");
if (isConstructed()) {
- DerInputStream in = new DerInputStream(bytes);
+ DerInputStream in = new DerInputStream(bytes, 0, bytes.length,
+ buffer.allowBER);
bytes = null;
while (in.available() != 0) {
bytes = append(bytes, in.getOctetString());
}
}
< prev index next >