Path: blob/master/test/jdk/sun/security/pkcs11/Secmod/Crypto.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 632900626* @summary verify that NSS no-db mode works correctly27* @author Andreas Sterbenz28* @library /test/lib ..29* @modules jdk.crypto.cryptoki30* @run main/othervm Crypto31* @run main/othervm -Djava.security.manager=allow Crypto sm policy32*/3334import java.io.File;35import java.security.KeyPair;36import java.security.KeyPairGenerator;37import java.security.Provider;38import java.security.Signature;3940public class Crypto extends SecmodTest {4142public static void main(String[] args) throws Exception {43if (initSecmod() == false) {44return;45}4647String configName = BASE + SEP + "nsscrypto.cfg";48Provider p = getSunPKCS11(configName);4950if (args.length > 1 && "sm".equals(args[0])) {51System.setProperty("java.security.policy",52BASE + File.separator + args[1]);53System.setSecurityManager(new SecurityManager());54}5556KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);57KeyPair kp = kpg.generateKeyPair();5859System.out.println(kp.getPublic());60System.out.println(kp.getPrivate());6162byte[] data = generateData(2048);6364Signature sig = Signature.getInstance("SHA1withRSA", p);65sig.initSign(kp.getPrivate());6667sig.update(data);68byte[] s = sig.sign();69System.out.println("signature: " + toString(s));7071sig.initVerify(kp.getPublic());72sig.update(data);73boolean ok = sig.verify(s);74if (ok == false) {75throw new Exception("Signature verification failed");76}7778System.out.println("OK");79}8081}828384