Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Secmod/TestNssDbSqlite.java
38855 views
/*1* Copyright (c) 2017, 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 ../31* @run main/othervm/timeout=120 TestNssDbSqlite32* @author Martin Balao ([email protected])33*/3435import java.security.PrivateKey;36import java.security.cert.Certificate;37import java.security.KeyStore;38import java.security.Provider;39import java.security.Signature;4041import sun.security.rsa.SunRsaSign;42import sun.security.jca.ProviderList;43import sun.security.jca.Providers;44import sun.security.tools.keytool.CertAndKeyGen;45import sun.security.x509.X500Name;4647public final class TestNssDbSqlite extends SecmodTest {4849private static final boolean enableDebug = true;5051private static Provider sunPKCS11NSSProvider;52private static Provider sunRsaSignProvider;53private static Provider sunJCEProvider;54private static KeyStore ks;55private static char[] passphrase = "test12".toCharArray();56private static PrivateKey privateKey;57private static Certificate certificate;5859public static void main(String[] args) throws Exception {6061initialize();6263if (enableDebug) {64System.out.println("SunPKCS11 provider: " +65sunPKCS11NSSProvider);66}6768testRetrieveKeysFromKeystore();6970System.out.println("Test PASS - OK");71}7273private static void testRetrieveKeysFromKeystore() throws Exception {7475String plainText = "known plain text";7677ks.setKeyEntry("root_ca_1", privateKey, passphrase,78new Certificate[]{certificate});79PrivateKey k1 = (PrivateKey) ks.getKey("root_ca_1", passphrase);8081Signature sS = Signature.getInstance(82"SHA256withRSA", sunPKCS11NSSProvider);83sS.initSign(k1);84sS.update(plainText.getBytes());85byte[] generatedSignature = sS.sign();8687if (enableDebug) {88System.out.println("Generated signature: ");89for (byte b : generatedSignature) {90System.out.printf("0x%02x, ", (int)(b) & 0xFF);91}92System.out.println("");93}9495Signature sV = Signature.getInstance("SHA256withRSA", sunRsaSignProvider);96sV.initVerify(certificate);97sV.update(plainText.getBytes());98if(!sV.verify(generatedSignature)){99throw new Exception("Couldn't verify signature");100}101}102103private static void initialize() throws Exception {104initializeProvider();105}106107private static void initializeProvider () throws Exception {108useSqlite(true);109if (!initSecmod()) {110return;111}112113sunPKCS11NSSProvider = getSunPKCS11(BASE + SEP + "nss-sqlite.cfg");114sunJCEProvider = new com.sun.crypto.provider.SunJCE();115sunRsaSignProvider = new SunRsaSign();116Providers.setProviderList(ProviderList.newList(117sunJCEProvider, sunPKCS11NSSProvider,118new sun.security.provider.Sun(), sunRsaSignProvider));119120ks = KeyStore.getInstance("PKCS11-NSS-Sqlite", sunPKCS11NSSProvider);121ks.load(null, passphrase);122123CertAndKeyGen gen = new CertAndKeyGen("RSA", "SHA256withRSA");124gen.generate(2048);125privateKey = gen.getPrivateKey();126certificate = gen.getSelfCertificate(new X500Name("CN=Me"), 365);127}128}129130131