Path: blob/master/test/jdk/sun/security/pkcs11/rsa/TestSignatures.java
41154 views
/*1* Copyright (c) 2003, 2020, 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 485696626* @summary Test signing/verifying using all the signature algorithms27* @author Andreas Sterbenz28* @library /test/lib ..29* @key randomness30* @modules jdk.crypto.cryptoki31* @run main/othervm TestSignatures32* @run main/othervm -Djava.security.manager=allow TestSignatures sm rsakeys.ks.policy33*/3435import java.io.File;36import java.io.FileInputStream;37import java.io.InputStream;38import java.security.KeyFactory;39import java.security.KeyStore;40import java.security.PrivateKey;41import java.security.Provider;42import java.security.PublicKey;43import java.security.Signature;44import java.security.interfaces.RSAPublicKey;45import java.util.Enumeration;46import java.util.Random;4748public class TestSignatures extends PKCS11Test {4950private static final char[] password = "test12".toCharArray();5152private static Provider provider;5354private static byte[] data;5556static KeyStore getKeyStore() throws Exception {57KeyStore ks;58try (InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"))) {59ks = KeyStore.getInstance("JKS");60ks.load(in, password);61}62return ks;63}6465private static void testSignature(String algorithm, PrivateKey privateKey,66PublicKey publicKey) throws Exception {67System.out.println("Testing " + algorithm + "...");68Signature s = Signature.getInstance(algorithm, provider);69s.initSign(privateKey);70s.update(data);71byte[] sig = s.sign();72s.initVerify(publicKey);73s.update(data);74boolean result;75result = s.verify(sig);76if (result == false) {77throw new Exception("Verification 1 failed");78}79s.update(data);80result = s.verify(sig);81if (result == false) {82throw new Exception("Verification 2 failed");83}84result = s.verify(sig);85if (result == true) {86throw new Exception("Verification 3 succeeded");87}88}8990private static void test(PrivateKey privateKey, PublicKey publicKey)91throws Exception {92testSignature("MD2withRSA", privateKey, publicKey);93testSignature("MD5withRSA", privateKey, publicKey);94testSignature("SHA1withRSA", privateKey, publicKey);95testSignature("SHA224withRSA", privateKey, publicKey);96testSignature("SHA256withRSA", privateKey, publicKey);97RSAPublicKey rsaKey = (RSAPublicKey)publicKey;98if (rsaKey.getModulus().bitLength() > 512) {99// for SHA384 and SHA512 the data is too long for 512 bit keys100testSignature("SHA384withRSA", privateKey, publicKey);101testSignature("SHA512withRSA", privateKey, publicKey);102}103}104105public static void main(String[] args) throws Exception {106main(new TestSignatures(), args);107}108109@Override110public void main(Provider p) throws Exception {111112long start = System.currentTimeMillis();113provider = p;114data = new byte[2048];115new Random().nextBytes(data);116KeyStore ks = getKeyStore();117KeyFactory kf = KeyFactory.getInstance("RSA", provider);118for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {119String alias = (String)e.nextElement();120if (ks.isKeyEntry(alias)) {121System.out.println("* Key " + alias + "...");122PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);123PublicKey publicKey = ks.getCertificate(alias).getPublicKey();124privateKey = (PrivateKey)kf.translateKey(privateKey);125publicKey = (PublicKey)kf.translateKey(publicKey);126test(privateKey, publicKey);127}128}129long stop = System.currentTimeMillis();130System.out.println("All tests passed (" + (stop - start) + " ms).");131}132}133134135