Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/GetPrivateKey.java
41153 views
/*1* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6273877 6322208 627552326* @summary make sure we can access the NSS softtoken KeyStore27* and use a private key28* @author Andreas Sterbenz29* @library /test/lib ..30* @modules jdk.crypto.cryptoki31* @run main/othervm GetPrivateKey32* @run main/othervm -Djava.security.manager=allow GetPrivateKey sm policy33*/3435import java.io.File;36import java.security.KeyStore;37import java.security.PrivateKey;38import java.security.Provider;39import java.security.Security;40import java.security.Signature;41import java.security.cert.X509Certificate;42import java.util.Collection;43import java.util.Collections;44import java.util.TreeSet;4546public class GetPrivateKey extends SecmodTest {4748public static void main(String[] args) throws Exception {49if (args.length > 1 && "sm".equals(args[0])) {50System.setProperty("java.security.policy",51BASE + File.separator + args[1]);52}5354if (initSecmod() == false) {55return;56}5758String configName = BASE + SEP + "nss.cfg";59Provider p = getSunPKCS11(configName);6061System.out.println(p);62Security.addProvider(p);6364if (args.length > 1 && "sm".equals(args[0])) {65System.setSecurityManager(new SecurityManager());66}6768KeyStore ks = KeyStore.getInstance(PKCS11, p);69ks.load(null, password);70Collection<String> aliases = new TreeSet<>(71Collections.list(ks.aliases()));72System.out.println("entries: " + aliases.size());73System.out.println(aliases);7475PrivateKey privateKey = (PrivateKey)ks.getKey(keyAlias, password);76System.out.println(privateKey);7778byte[] data = generateData(1024);7980System.out.println("Signing...");81Signature signature = Signature.getInstance("MD5withRSA");82signature.initSign(privateKey);83signature.update(data);84byte[] sig = signature.sign();8586X509Certificate[] chain =87(X509Certificate[]) ks.getCertificateChain(keyAlias);88signature.initVerify(chain[0].getPublicKey());89signature.update(data);90boolean ok = signature.verify(sig);91if (ok == false) {92throw new Exception("Signature verification error");93}9495System.out.println("OK");9697}9899}100101102