changeset 1751:59d04ebbc600

6804045: DerValue does not accept empty OCTET STRING Reviewed-by: xuelei
author weijun
date Mon, 23 Feb 2009 10:04:52 +0800
parents d4ca9895762d
children 3442abfe4092
files src/share/classes/sun/security/util/DerValue.java test/sun/security/util/DerValue/EmptyValue.java
diffstat 2 files changed, 51 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/sun/security/util/DerValue.java	Mon Aug 07 19:57:21 2017 -0700
+++ b/src/share/classes/sun/security/util/DerValue.java	Mon Feb 23 10:04:52 2009 +0800
@@ -1,5 +1,5 @@
 /**
- * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2018, 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
@@ -66,7 +66,7 @@
     protected DerInputBuffer    buffer;
 
     /**
-     * The DER-encoded data of the value.
+     * The DER-encoded data of the value, never null
      */
     public final DerInputStream data;
 
@@ -405,8 +405,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");
@@ -500,6 +498,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/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");
+        }
+    }
+}