Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/rsa/TestSignatures.java
38855 views
/*1* Copyright (c) 2003, 2016, 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 ..29* @key randomness30* @run main/othervm TestSignatures31* @run main/othervm TestSignatures sm rsakeys.ks.policy32*/3334import java.io.File;35import java.io.FileInputStream;36import java.io.InputStream;37import java.security.KeyFactory;38import java.security.KeyStore;39import java.security.PrivateKey;40import java.security.Provider;41import java.security.PublicKey;42import java.security.Signature;43import java.security.interfaces.RSAPublicKey;44import java.util.Enumeration;45import java.util.Random;4647public class TestSignatures extends PKCS11Test {4849private static final char[] password = "test12".toCharArray();5051private static Provider provider;5253private static byte[] data;5455static KeyStore getKeyStore() throws Exception {56KeyStore ks;57try (InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"))) {58ks = KeyStore.getInstance("JKS");59ks.load(in, password);60}61return ks;62}6364private static void testSignature(String algorithm, PrivateKey privateKey,65PublicKey publicKey) throws Exception {66System.out.println("Testing " + algorithm + "...");67Signature s = Signature.getInstance(algorithm, provider);68s.initSign(privateKey);69s.update(data);70byte[] sig = s.sign();71s.initVerify(publicKey);72s.update(data);73boolean result;74result = s.verify(sig);75if (result == false) {76throw new Exception("Verification 1 failed");77}78s.update(data);79result = s.verify(sig);80if (result == false) {81throw new Exception("Verification 2 failed");82}83result = s.verify(sig);84if (result == true) {85throw new Exception("Verification 3 succeeded");86}87}8889private static void test(PrivateKey privateKey, PublicKey publicKey)90throws Exception {91testSignature("MD2withRSA", privateKey, publicKey);92testSignature("MD5withRSA", privateKey, publicKey);93testSignature("SHA1withRSA", privateKey, publicKey);94testSignature("SHA224withRSA", privateKey, publicKey);95testSignature("SHA256withRSA", privateKey, publicKey);96RSAPublicKey rsaKey = (RSAPublicKey)publicKey;97if (rsaKey.getModulus().bitLength() > 512) {98// for SHA384 and SHA512 the data is too long for 512 bit keys99testSignature("SHA384withRSA", privateKey, publicKey);100testSignature("SHA512withRSA", privateKey, publicKey);101}102}103104public static void main(String[] args) throws Exception {105main(new TestSignatures(), args);106}107108@Override109public void main(Provider p) throws Exception {110long start = System.currentTimeMillis();111provider = p;112data = new byte[2048];113new Random().nextBytes(data);114KeyStore ks = getKeyStore();115KeyFactory kf = KeyFactory.getInstance("RSA", provider);116for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {117String alias = (String)e.nextElement();118if (ks.isKeyEntry(alias)) {119System.out.println("* Key " + alias + "...");120PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);121PublicKey publicKey = ks.getCertificate(alias).getPublicKey();122privateKey = (PrivateKey)kf.translateKey(privateKey);123publicKey = (PublicKey)kf.translateKey(publicKey);124test(privateKey, publicKey);125}126}127long stop = System.currentTimeMillis();128System.out.println("All tests passed (" + (stop - start) + " ms).");129}130}131132133