6804045: DerValue does not accept empty OCTET STRING

Reviewed-by: xuelei
This commit is contained in:
Weijun Wang 2009-02-23 10:04:52 +08:00
parent 967dd884ac
commit 9081658837
2 changed files with 51 additions and 4 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
* Copyright 1996-2009 Sun Microsystems, Inc. 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
@ -65,7 +65,7 @@ public class DerValue {
protected DerInputBuffer buffer;
/**
* The DER-encoded data of the value.
* The DER-encoded data of the value, never null
*/
public final DerInputStream data;
@ -378,8 +378,6 @@ public class DerValue {
("Indefinite length encoding not supported");
length = DerInputStream.getLength(in);
}
if (length == 0)
return null;
if (fullyBuffered && in.available() != length)
throw new IOException("extra data given to DerValue constructor");
@ -477,6 +475,11 @@ public class DerValue {
"DerValue.getOctetString, not an Octet String: " + tag);
}
bytes = new byte[length];
// Note: do not tempt to call buffer.read(bytes) at all. There's a
// known bug that it returns -1 instead of 0.
if (length == 0) {
return bytes;
}
if (buffer.read(bytes) != length)
throw new IOException("short read on DerValue buffer");
if (isConstructed()) {

View File

@ -0,0 +1,44 @@
/*
* Copyright 2009 Sun Microsystems, Inc. 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.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/
/*
* @test
* @bug 6804045
* @summary DerValue does not accept empty OCTET STRING
*/
import sun.security.util.DerValue;
public class EmptyValue {
public static void main(String[] args) throws Exception {
DerValue v = new DerValue(new byte[]{4,0});
if (v.getOctetString().length != 0) {
throw new Exception("Get octet string error");
}
v = new DerValue(new byte[]{0x30,0});
if (v.data.available() != 0) {
throw new Exception("Get sequence error");
}
}
}