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

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