6804045: DerValue does not accept empty OCTET STRING
authorweijun
Mon, 23 Feb 2009 10:04:52 +0800
changeset 2065 d0bafa066816
parent 2064 d690c8a2acea
child 2066 4230d224f27b
6804045: DerValue does not accept empty OCTET STRING Reviewed-by: xuelei
jdk/src/share/classes/sun/security/util/DerValue.java
jdk/test/sun/security/util/DerValue/EmptyValue.java
--- a/jdk/src/share/classes/sun/security/util/DerValue.java	Mon Feb 23 10:04:25 2009 +0800
+++ b/jdk/src/share/classes/sun/security/util/DerValue.java	Mon Feb 23 10:04:52 2009 +0800
@@ -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 @@
     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 @@
                         ("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 @@
                 "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()) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/sun/security/util/DerValue/EmptyValue.java	Mon Feb 23 10:04:52 2009 +0800
@@ -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");
+        }
+    }
+}