Path: blob/master/test/jdk/sun/security/rsa/TestSignatures.java
41152 views
/*1* Copyright (c) 2003, 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 4853305 4963723 814629326* @summary Test signing/verifying using all the signature algorithms27* @library /test/lib28* @build jdk.test.lib.SigTestUtil29* @run main TestSignatures30* @author Andreas Sterbenz31* @key randomness32*/3334import java.io.*;35import java.util.*;3637import java.security.*;38import java.security.interfaces.*;3940import jdk.test.lib.SigTestUtil;41import static jdk.test.lib.SigTestUtil.SignatureType;4243public class TestSignatures {4445private final static String BASE = System.getProperty("test.src", ".");4647private static final char[] password = "test12".toCharArray();4849private static Provider provider;5051private static byte[] data;5253static KeyStore getKeyStore() throws Exception {54InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));55KeyStore ks = KeyStore.getInstance("JKS");56ks.load(in, password);57in.close();58return ks;59}6061private static void testSignature(String mdAlg, PrivateKey privateKey,62PublicKey publicKey) throws NoSuchAlgorithmException,63InvalidKeyException, SignatureException {64System.out.println("Testing against " + mdAlg + "...");65String sigAlg = SigTestUtil.generateSigAlg(SignatureType.RSA, mdAlg);66Signature s = Signature.getInstance(sigAlg, provider);67s.initSign(privateKey);68s.update(data);69byte[] sig = s.sign();70s.initVerify(publicKey);71s.update(data);72boolean result;73result = s.verify(sig);74if (result == false) {75throw new RuntimeException("Verification 1 failed");76}77s.update(data);78result = s.verify(sig);79if (result == false) {80throw new RuntimeException("Verification 2 failed");81}82result = s.verify(sig);83if (result == true) {84throw new RuntimeException("Verification 3 succeeded");85}86}8788private static void test(PrivateKey privateKey, PublicKey publicKey)89throws Exception {9091int testSize = ((RSAPublicKey)publicKey).getModulus().bitLength();92System.out.println("modulus size = " + testSize);93// work around a corner case where the key size is one bit short94if ((testSize & 0x07) != 0) {95testSize += (8 - (testSize & 0x07));96System.out.println("adjusted modulus size = " + testSize);97}98Iterable<String> sign_alg_pkcs15 =99SigTestUtil.getDigestAlgorithms(SignatureType.RSA, testSize);100sign_alg_pkcs15.forEach(testAlg -> {101try {102testSignature(testAlg, privateKey, publicKey);103} catch (NoSuchAlgorithmException | InvalidKeyException |104SignatureException ex) {105throw new RuntimeException(ex);106}107}108);109}110111public static void main(String[] args) throws Exception {112long start = System.currentTimeMillis();113provider = Security.getProvider("SunRsaSign");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