Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/TestNssDbSqlite.java
41153 views
/*1* Copyright (c) 2017, 2018, Red Hat, Inc. and/or its affiliates.2*3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/*25* @test26* @bug 816599627* @summary Test NSS DB Sqlite28* @comment There is no NSS on Aix.29* @requires os.family != "aix"30* @library /test/lib ../31* @modules java.base/sun.security.rsa32* java.base/sun.security.provider33* java.base/sun.security.jca34* java.base/sun.security.tools.keytool35* java.base/sun.security.x50936* java.base/com.sun.crypto.provider37* jdk.crypto.cryptoki/sun.security.pkcs11:+open38* @run main/othervm/timeout=120 TestNssDbSqlite39* @author Martin Balao ([email protected])40*/4142import java.security.PrivateKey;43import java.security.cert.Certificate;44import java.security.KeyStore;45import java.security.Provider;46import java.security.Signature;4748import sun.security.rsa.SunRsaSign;49import sun.security.jca.ProviderList;50import sun.security.jca.Providers;51import sun.security.tools.keytool.CertAndKeyGen;52import sun.security.x509.X500Name;5354public final class TestNssDbSqlite extends SecmodTest {5556private static final boolean enableDebug = true;5758private static Provider sunPKCS11NSSProvider;59private static Provider sunRsaSignProvider;60private static Provider sunJCEProvider;61private static KeyStore ks;62private static char[] passphrase = "test12".toCharArray();63private static PrivateKey privateKey;64private static Certificate certificate;6566public static void main(String[] args) throws Exception {6768if (!initialize()) {69return;70}7172if (enableDebug) {73System.out.println("SunPKCS11 provider: " +74sunPKCS11NSSProvider);75}7677testRetrieveKeysFromKeystore();7879System.out.println("Test PASS - OK");80}8182private static void testRetrieveKeysFromKeystore() throws Exception {8384String plainText = "known plain text";8586ks.setKeyEntry("root_ca_1", privateKey, passphrase,87new Certificate[]{certificate});88PrivateKey k1 = (PrivateKey) ks.getKey("root_ca_1", passphrase);8990Signature sS = Signature.getInstance(91"SHA256withRSA", sunPKCS11NSSProvider);92sS.initSign(k1);93sS.update(plainText.getBytes());94byte[] generatedSignature = sS.sign();9596if (enableDebug) {97System.out.println("Generated signature: ");98for (byte b : generatedSignature) {99System.out.printf("0x%02x, ", (int)(b) & 0xFF);100}101System.out.println("");102}103104Signature sV = Signature.getInstance("SHA256withRSA", sunRsaSignProvider);105sV.initVerify(certificate);106sV.update(plainText.getBytes());107if(!sV.verify(generatedSignature)){108throw new Exception("Couldn't verify signature");109}110}111112private static boolean initialize() throws Exception {113return initializeProvider();114}115116private static boolean initializeProvider() throws Exception {117useSqlite(true);118if (!initSecmod()) {119System.out.println("Cannot init security module database, skipping");120return false;121}122123sunPKCS11NSSProvider = getSunPKCS11(BASE + SEP + "nss-sqlite.cfg");124sunJCEProvider = new com.sun.crypto.provider.SunJCE();125sunRsaSignProvider = new SunRsaSign();126Providers.setProviderList(ProviderList.newList(127sunJCEProvider, sunPKCS11NSSProvider,128new sun.security.provider.Sun(), sunRsaSignProvider));129130ks = KeyStore.getInstance("PKCS11-NSS-Sqlite", sunPKCS11NSSProvider);131ks.load(null, passphrase);132133CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA256withRSA");134gen.generate(2048);135privateKey = gen.getPrivateKey();136certificate = gen.getSelfCertificate(new X500Name("CN=Me"), 365);137138return true;139}140}141142143