Hi,
ManifestDigester fails to digest manifests that end with \r with an
array index out of bounds exception and such jar files cannot be
signed.
I cannot create a bug myself because I don't have the privileges and
could therefore not add the proper @bug tag to the test yet.
Philipp

Following up in smaller parts
https://mail.openjdk.java.net/pipermail/security-dev/2019-January/01919
3.html

Possibly related:
https://bugs.openjdk.java.net/browse/JDK-8200530
https://bugs.openjdk.java.net/browse/JDK-6954621
diff -r 683a112e0e1e src/java.base/share/classes/sun/security/util/ManifestDigester.java
--- a/src/java.base/share/classes/sun/security/util/ManifestDigester.java	Sat Jan 19 11:20:01 2019 +0100
+++ b/src/java.base/share/classes/sun/security/util/ManifestDigester.java	Sun Jan 20 09:17:34 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2019, 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
@@ -84,7 +84,7 @@
             case '\r':
                 if (pos.endOfFirstLine == -1)
                     pos.endOfFirstLine = i-1;
-                if ((i < len) &&  (rawBytes[i+1] == '\n'))
+                if (i + 1 < len && rawBytes[i + 1] == '\n')
                     i++;
                 /* fall through */
             case '\n':
diff -r 683a112e0e1e test/jdk/sun/security/util/ManifestDigester/LineBreaks.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/sun/security/util/ManifestDigester/LineBreaks.java	Sun Jan 20 09:17:34 2019 +0100
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2019, 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.
+ *
+ * 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.function.Function;
+import java.util.jar.Attributes;
+import java.util.jar.Attributes.Name;
+import java.util.jar.Manifest;
+import sun.security.util.ManifestDigester;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.testng.Assert.*;
+
+/**
+ * @test
+ * @modules java.base/sun.security.util
+ * @run testng LineBreaks
+ * @summary Verify {@code ManifestDigester} reads different line breaks well.
+ * The specifications state:
+ * <q><pre>newline: CR LF | LF | CR (not followed by LF)</pre></q>.
+ * This test does not verify that the digests are correct.
+ */
+public class LineBreaks {
+
+    static final String KEY = "Key";
+    static final String VALUE = "Value";
+    static final String SECTION = "Section";
+    static final String FOO = "Foo";
+    static final String BAR = "Bar";
+
+    byte[] createTestManifest(String lineBreak, boolean onlyMainAtts) {
+        System.out.println("lineBreak = " +
+                byteArrayToIntList(lineBreak.getBytes(UTF_8)));
+        System.out.println("onlyMainAtts = " + onlyMainAtts);
+        String mf = "";
+        mf += Name.MANIFEST_VERSION + ": 1.0" + lineBreak;
+        mf += KEY + ": " + VALUE + lineBreak;
+        mf += lineBreak;
+        if (!onlyMainAtts) {
+            mf += "Name: " + SECTION + lineBreak;
+            mf += FOO + ": " + BAR + lineBreak;
+            mf += lineBreak;
+        }
+        System.out.println("-".repeat(72));
+        System.out.println(mf);
+        System.out.println("-".repeat(72));
+        byte[] mfBytes = mf.getBytes(UTF_8);
+        System.out.println("binary manifest = " + byteArrayToIntList(mfBytes));
+        return mfBytes;
+    }
+
+    @DataProvider(name = "parameters")
+    public static Object[][] parameters() {
+        List<Object[]> tests = new ArrayList<>();
+        for (String lineBreak : new String[] { "\n", "\r", "\r\n" }) {
+            for (boolean onlyMainAtts : new boolean[] { false, true }) {
+                tests.add(new Object[] { lineBreak, onlyMainAtts });
+            }
+        }
+        return tests.toArray(new Object[tests.size()][]);
+    }
+
+    @Test(dataProvider = "parameters")
+    void test(String lineBreak, boolean onlyMainAtts) throws IOException {
+        byte[] mfBytes = createTestManifest(lineBreak, onlyMainAtts);
+
+        // self-test: make sure the manifest is valid and represents the
+        // values as expected before attempting to digest it
+        Manifest mf = new Manifest(new ByteArrayInputStream(mfBytes));
+        assertEquals(mf.getMainAttributes().getValue(KEY), VALUE);
+        if (onlyMainAtts) {
+            assertNull(mf.getAttributes(SECTION));
+        } else {
+            assertEquals(mf.getAttributes(SECTION).getValue(FOO), BAR);
+        }
+
+        // verify that ManifestDigester has actually found the individual
+        // section if and only if it was present thereby also implying based
+        // on ManifestDigester implementation that the main attributes were
+        // found before
+        ManifestDigester md = new ManifestDigester(mfBytes);
+        assertTrue((md.get(SECTION, false) != null) != onlyMainAtts);
+    }
+
+    static List<Integer> byteArrayToIntList(byte[] bytes) {
+        List<Integer> list = new ArrayList<>();
+        for (int i = 0; i < bytes.length; i++) {
+            list.add((int) bytes[i]);
+        }
+        return list;
+    }
+
+}

Reply via email to