Blame SOURCES/jdk8165996-pr3506-rh1760437-nss_sqlite_db.patch

5781ba
# HG changeset patch
5781ba
# User weijun
5781ba
# Date 1513099798 -28800
5781ba
#      Wed Dec 13 01:29:58 2017 +0800
5781ba
# Node ID aa8f2e25f003feddf362892b2820fa2839c854b6
5781ba
# Parent  9ebb70cb99a472b5fee9ac08240b7979468c2fa5
5781ba
8165996: PKCS11 using NSS throws an error regarding secmod.db when NSS uses sqlite
5781ba
Reviewed-by: weijun
5781ba
Contributed-by: Martin Balao <mbalao@redhat.com>
5781ba
5781ba
diff --git openjdk.orig/jdk/src/share/classes/sun/security/pkcs11/Secmod.java openjdk/jdk/src/share/classes/sun/security/pkcs11/Secmod.java
5781ba
--- openjdk.orig/jdk/src/share/classes/sun/security/pkcs11/Secmod.java
5781ba
+++ openjdk/jdk/src/share/classes/sun/security/pkcs11/Secmod.java
5781ba
@@ -196,13 +196,23 @@
5781ba
         }
5781ba
 
5781ba
         if (configDir != null) {
5781ba
-            File configBase = new File(configDir);
5781ba
-            if (configBase.isDirectory() == false ) {
5781ba
-                throw new IOException("configDir must be a directory: " + configDir);
5781ba
+            String configDirPath = null;
5781ba
+            String sqlPrefix = "sql:/";
5781ba
+            if (!configDir.startsWith(sqlPrefix)) {
5781ba
+                configDirPath = configDir;
5781ba
+            } else {
5781ba
+                StringBuilder configDirPathSB = new StringBuilder(configDir);
5781ba
+                configDirPath = configDirPathSB.substring(sqlPrefix.length());
5781ba
             }
5781ba
-            File secmodFile = new File(configBase, "secmod.db");
5781ba
-            if (secmodFile.isFile() == false) {
5781ba
-                throw new FileNotFoundException(secmodFile.getPath());
5781ba
+            File configBase = new File(configDirPath);
5781ba
+            if (configBase.isDirectory() == false ) {
5781ba
+                throw new IOException("configDir must be a directory: " + configDirPath);
5781ba
+            }
5781ba
+            if (!configDir.startsWith(sqlPrefix)) {
5781ba
+                File secmodFile = new File(configBase, "secmod.db");
5781ba
+                if (secmodFile.isFile() == false) {
5781ba
+                    throw new FileNotFoundException(secmodFile.getPath());
5781ba
+                }
5781ba
             }
5781ba
         }
5781ba
 
5781ba
diff --git openjdk.orig/jdk/test/sun/security/pkcs11/Secmod/README-SQLITE openjdk/jdk/test/sun/security/pkcs11/Secmod/README-SQLITE
5781ba
new file mode 100644
5781ba
--- /dev/null
5781ba
+++ openjdk/jdk/test/sun/security/pkcs11/Secmod/README-SQLITE
5781ba
@@ -0,0 +1,8 @@
5781ba
+// How to create key4.db and cert9.db
5781ba
+cd <path-for-db>
5781ba
+echo "" > 1
5781ba
+echo "test12" > 2
5781ba
+modutil -create -force -dbdir sql:/$(pwd)
5781ba
+modutil -list "NSS Internal PKCS #11 Module" -dbdir sql:/$(pwd)
5781ba
+modutil -changepw "NSS Certificate DB" -force -dbdir sql:/$(pwd) -pwfile $(pwd)/1 -newpwfile $(pwd)/2
5781ba
+
5781ba
diff --git openjdk.orig/jdk/test/sun/security/pkcs11/Secmod/TestNssDbSqlite.java openjdk/jdk/test/sun/security/pkcs11/Secmod/TestNssDbSqlite.java
5781ba
new file mode 100644
5781ba
--- /dev/null
5781ba
+++ openjdk/jdk/test/sun/security/pkcs11/Secmod/TestNssDbSqlite.java
5781ba
@@ -0,0 +1,134 @@
5781ba
+/*
5781ba
+ * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
5781ba
+ *
5781ba
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5781ba
+ *
5781ba
+ * This code is free software; you can redistribute it and/or modify it
5781ba
+ * under the terms of the GNU General Public License version 2 only, as
5781ba
+ * published by the Free Software Foundation.
5781ba
+ *
5781ba
+ * This code is distributed in the hope that it will be useful, but WITHOUT
5781ba
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5781ba
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5781ba
+ * version 2 for more details (a copy is included in the LICENSE file that
5781ba
+ * accompanied this code).
5781ba
+ *
5781ba
+ * You should have received a copy of the GNU General Public License version
5781ba
+ * 2 along with this work; if not, write to the Free Software Foundation,
5781ba
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5781ba
+ *
5781ba
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5781ba
+ * or visit www.oracle.com if you need additional information or have any
5781ba
+ * questions.
5781ba
+ */
5781ba
+
5781ba
+/*
5781ba
+ * @test
5781ba
+ * @bug 8165996
5781ba
+ * @summary Test NSS DB Sqlite
5781ba
+ * @library ../
5781ba
+ * @modules java.base/sun.security.rsa
5781ba
+ *          java.base/sun.security.provider
5781ba
+ *          java.base/sun.security.jca
5781ba
+ *          java.base/sun.security.tools.keytool
5781ba
+ *          java.base/sun.security.x509
5781ba
+ *          java.base/com.sun.crypto.provider
5781ba
+ *          jdk.crypto.cryptoki/sun.security.pkcs11:+open
5781ba
+ * @run main/othervm/timeout=120 TestNssDbSqlite
5781ba
+ * @author Martin Balao (mbalao@redhat.com)
5781ba
+ */
5781ba
+
5781ba
+import java.security.PrivateKey;
5781ba
+import java.security.cert.Certificate;
5781ba
+import java.security.KeyStore;
5781ba
+import java.security.Provider;
5781ba
+import java.security.Signature;
5781ba
+
5781ba
+import sun.security.rsa.SunRsaSign;
5781ba
+import sun.security.jca.ProviderList;
5781ba
+import sun.security.jca.Providers;
5781ba
+import sun.security.tools.keytool.CertAndKeyGen;
5781ba
+import sun.security.x509.X500Name;
5781ba
+
5781ba
+public final class TestNssDbSqlite extends SecmodTest {
5781ba
+
5781ba
+    private static final boolean enableDebug = true;
5781ba
+
5781ba
+    private static Provider sunPKCS11NSSProvider;
5781ba
+    private static Provider sunRsaSignProvider;
5781ba
+    private static Provider sunJCEProvider;
5781ba
+    private static KeyStore ks;
5781ba
+    private static char[] passphrase = "test12".toCharArray();
5781ba
+    private static PrivateKey privateKey;
5781ba
+    private static Certificate certificate;
5781ba
+
5781ba
+    public static void main(String[] args) throws Exception {
5781ba
+
5781ba
+        initialize();
5781ba
+
5781ba
+        if (enableDebug) {
5781ba
+            System.out.println("SunPKCS11 provider: " +
5781ba
+                sunPKCS11NSSProvider);
5781ba
+        }
5781ba
+
5781ba
+        testRetrieveKeysFromKeystore();
5781ba
+
5781ba
+        System.out.println("Test PASS - OK");
5781ba
+    }
5781ba
+
5781ba
+    private static void testRetrieveKeysFromKeystore() throws Exception {
5781ba
+
5781ba
+        String plainText = "known plain text";
5781ba
+
5781ba
+        ks.setKeyEntry("root_ca_1", privateKey, passphrase,
5781ba
+                new Certificate[]{certificate});
5781ba
+        PrivateKey k1 = (PrivateKey) ks.getKey("root_ca_1", passphrase);
5781ba
+
5781ba
+        Signature sS = Signature.getInstance(
5781ba
+                "SHA256withRSA", sunPKCS11NSSProvider);
5781ba
+        sS.initSign(k1);
5781ba
+        sS.update(plainText.getBytes());
5781ba
+        byte[] generatedSignature = sS.sign();
5781ba
+
5781ba
+        if (enableDebug) {
5781ba
+            System.out.println("Generated signature: ");
5781ba
+            for (byte b : generatedSignature) {
5781ba
+                System.out.printf("0x%02x, ", (int)(b) & 0xFF);
5781ba
+            }
5781ba
+            System.out.println("");
5781ba
+        }
5781ba
+
5781ba
+        Signature sV = Signature.getInstance("SHA256withRSA", sunRsaSignProvider);
5781ba
+        sV.initVerify(certificate);
5781ba
+        sV.update(plainText.getBytes());
5781ba
+        if(!sV.verify(generatedSignature)){
5781ba
+            throw new Exception("Couldn't verify signature");
5781ba
+        }
5781ba
+    }
5781ba
+
5781ba
+    private static void initialize() throws Exception {
5781ba
+        initializeProvider();
5781ba
+    }
5781ba
+
5781ba
+    private static void initializeProvider () throws Exception {
5781ba
+        useSqlite(true);
5781ba
+        if (!initSecmod()) {
5781ba
+            return;
5781ba
+        }
5781ba
+
5781ba
+        sunPKCS11NSSProvider = getSunPKCS11(BASE + SEP + "nss-sqlite.cfg");
5781ba
+        sunJCEProvider = new com.sun.crypto.provider.SunJCE();
5781ba
+        sunRsaSignProvider = new SunRsaSign();
5781ba
+        Providers.setProviderList(ProviderList.newList(
5781ba
+                sunJCEProvider, sunPKCS11NSSProvider,
5781ba
+                new sun.security.provider.Sun(), sunRsaSignProvider));
5781ba
+
5781ba
+        ks = KeyStore.getInstance("PKCS11-NSS-Sqlite", sunPKCS11NSSProvider);
5781ba
+        ks.load(null, passphrase);
5781ba
+
5781ba
+        CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA256withRSA");
5781ba
+        gen.generate(2048);
5781ba
+        privateKey = gen.getPrivateKey();
5781ba
+        certificate = gen.getSelfCertificate(new X500Name("CN=Me"), 365);
5781ba
+    }
5781ba
+}
5781ba
diff --git openjdk.orig/jdk/test/sun/security/pkcs11/Secmod/nss-sqlite.cfg openjdk/jdk/test/sun/security/pkcs11/Secmod/nss-sqlite.cfg
5781ba
new file mode 100644
5781ba
--- /dev/null
5781ba
+++ openjdk/jdk/test/sun/security/pkcs11/Secmod/nss-sqlite.cfg
5781ba
@@ -0,0 +1,13 @@
5781ba
+# config file for secmod KeyStore access using sqlite backend
5781ba
+
5781ba
+name = NSS-Sqlite
5781ba
+
5781ba
+nssLibraryDirectory = ${pkcs11test.nss.libdir}
5781ba
+
5781ba
+nssDbMode = readWrite
5781ba
+
5781ba
+nssModule =  keystore
5781ba
+
5781ba
+nssSecmodDirectory = ${pkcs11test.nss.db}
5781ba
+
5781ba
+attributes = compatibility
5781ba
diff --git openjdk.orig/jdk/test/sun/security/pkcs11/SecmodTest.java openjdk/jdk/test/sun/security/pkcs11/SecmodTest.java
5781ba
--- openjdk.orig/jdk/test/sun/security/pkcs11/SecmodTest.java
5781ba
+++ openjdk/jdk/test/sun/security/pkcs11/SecmodTest.java
5781ba
@@ -34,6 +34,11 @@
5781ba
     static String DBDIR;
5781ba
     static char[] password = "test12".toCharArray();
5781ba
     static String keyAlias = "mykey";
5781ba
+    static boolean useSqlite = false;
5781ba
+
5781ba
+    static void useSqlite(boolean b) {
5781ba
+        useSqlite = b;
5781ba
+    }
5781ba
 
5781ba
     static boolean initSecmod() throws Exception {
5781ba
         useNSS();
5781ba
@@ -49,14 +54,24 @@
5781ba
         safeReload(LIBPATH + System.mapLibraryName("nssckbi"));
5781ba
 
5781ba
         DBDIR = System.getProperty("test.classes", ".") + SEP + "tmpdb";
5781ba
-        System.setProperty("pkcs11test.nss.db", DBDIR);
5781ba
+        if (useSqlite) {
5781ba
+            System.setProperty("pkcs11test.nss.db", "sql:/" + DBDIR);
5781ba
+        } else {
5781ba
+            System.setProperty("pkcs11test.nss.db", DBDIR);
5781ba
+        }
5781ba
         File dbdirFile = new File(DBDIR);
5781ba
         if (dbdirFile.exists() == false) {
5781ba
             dbdirFile.mkdir();
5781ba
         }
5781ba
-        copyFile("secmod.db", BASE, DBDIR);
5781ba
-        copyFile("key3.db", BASE, DBDIR);
5781ba
-        copyFile("cert8.db", BASE, DBDIR);
5781ba
+
5781ba
+        if (useSqlite) {
5781ba
+            copyFile("key4.db", BASE, DBDIR);
5781ba
+            copyFile("cert9.db", BASE, DBDIR);
5781ba
+        } else {
5781ba
+            copyFile("secmod.db", BASE, DBDIR);
5781ba
+            copyFile("key3.db", BASE, DBDIR);
5781ba
+            copyFile("cert8.db", BASE, DBDIR);
5781ba
+        }
5781ba
         return true;
5781ba
     }
5781ba