< prev index next >

src/java.base/share/classes/sun/security/util/DerValue.java

Print this page

        

*** 1,7 **** ! /* ! * Copyright (c) 1996, 2016, 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 --- 1,7 ---- ! /** ! * 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,253 **** */ public DerValue(byte stringTag, String value) throws IOException { data = init(stringTag, 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); } /* * 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(); --- 223,259 ---- */ 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, 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,268 **** 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)); if (tag != inbuf.read()) throw new IOException ("Indefinite length encoding not supported"); length = DerInputStream.getDefiniteLength(inbuf); buffer = inbuf.dup(); --- 264,274 ---- 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), in.allowBER); if (tag != inbuf.read()) throw new IOException ("Indefinite length encoding not supported"); length = DerInputStream.getDefiniteLength(inbuf); buffer = inbuf.dup();
*** 280,300 **** in.skip(length); } } /** * 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)); } /** * 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. * --- 286,319 ---- 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 { ! 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,313 **** * @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)); } /** * 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. --- 320,338 ---- * @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 { ! 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,327 **** * * @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); } ! private DerInputStream init(byte stringTag, String value) throws IOException { String enc = null; tag = stringTag; switch (stringTag) { --- 339,353 ---- * * @param in the input stream holding a single DER datum, * which may be followed by additional data */ public DerValue(InputStream in) throws IOException { ! this(in, true); } ! private DerInputStream init(byte stringTag, String value) ! throws IOException { String enc = null; tag = stringTag; switch (stringTag) {
*** 345,365 **** throw new IllegalArgumentException("Unsupported DER string type"); } byte[] buf = value.getBytes(enc); length = buf.length; ! buffer = new DerInputBuffer(buf); DerInputStream result = new DerInputStream(buffer); result.mark(Integer.MAX_VALUE); return result; } /* * helper routine */ ! private DerInputStream init(boolean fullyBuffered, InputStream in) ! throws IOException { tag = (byte)in.read(); byte lenByte = (byte)in.read(); length = DerInputStream.getLength(lenByte, in); if (length == -1) { // indefinite length encoding found --- 371,391 ---- throw new IllegalArgumentException("Unsupported DER string type"); } byte[] buf = value.getBytes(enc); length = buf.length; ! 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, ! 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,392 **** 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); return new DerInputStream(buffer); } /** * Encode an ASN1/DER encoded datum onto a DER output stream. --- 408,418 ---- 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, allowBER); return new DerInputStream(buffer); } /** * Encode an ASN1/DER encoded datum onto a DER output stream.
*** 477,487 **** return bytes; } if (buffer.read(bytes) != length) throw new IOException("short read on DerValue buffer"); if (isConstructed()) { ! DerInputStream in = new DerInputStream(bytes); bytes = null; while (in.available() != 0) { bytes = append(bytes, in.getOctetString()); } } --- 503,514 ---- return bytes; } if (buffer.read(bytes) != length) throw new IOException("short read on DerValue buffer"); if (isConstructed()) { ! DerInputStream in = new DerInputStream(bytes, 0, bytes.length, ! buffer.allowBER); bytes = null; while (in.available() != 0) { bytes = append(bytes, in.getOctetString()); } }
< prev index next >