0N/A/*
2362N/A * Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4853305
0N/A * @summary Test signing/verifying using all the signature algorithms
0N/A * @author Andreas Sterbenz
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/Aimport java.security.*;
0N/Aimport java.security.interfaces.*;
0N/A
0N/Apublic class TestSignatures {
0N/A
0N/A private final static String BASE = System.getProperty("test.src", ".");
0N/A
0N/A private static final char[] password = "test12".toCharArray();
0N/A
0N/A private static Provider provider;
0N/A
0N/A private static byte[] data;
0N/A
0N/A static KeyStore getKeyStore() throws Exception {
0N/A InputStream in = new FileInputStream(new File(BASE, "rsakeys.ks"));
0N/A KeyStore ks = KeyStore.getInstance("JKS");
0N/A ks.load(in, password);
0N/A in.close();
0N/A return ks;
0N/A }
0N/A
0N/A private static void testSignature(String algorithm, PrivateKey privateKey, PublicKey publicKey) throws Exception {
0N/A System.out.println("Testing " + algorithm + "...");
0N/A Signature s = Signature.getInstance(algorithm, provider);
0N/A s.initSign(privateKey);
0N/A s.update(data);
0N/A byte[] sig = s.sign();
0N/A s.initVerify(publicKey);
0N/A s.update(data);
0N/A boolean result;
0N/A result = s.verify(sig);
0N/A if (result == false) {
0N/A throw new Exception("Verification 1 failed");
0N/A }
0N/A s.update(data);
0N/A result = s.verify(sig);
0N/A if (result == false) {
0N/A throw new Exception("Verification 2 failed");
0N/A }
0N/A result = s.verify(sig);
0N/A if (result == true) {
0N/A throw new Exception("Verification 3 succeeded");
0N/A }
0N/A }
0N/A
0N/A private static void test(PrivateKey privateKey, PublicKey publicKey) throws Exception {
0N/A testSignature("MD2withRSA", privateKey, publicKey);
0N/A testSignature("MD5withRSA", privateKey, publicKey);
0N/A testSignature("SHA1withRSA", privateKey, publicKey);
0N/A testSignature("SHA256withRSA", privateKey, publicKey);
0N/A RSAPublicKey rsaKey = (RSAPublicKey)publicKey;
0N/A if (rsaKey.getModulus().bitLength() > 512) {
0N/A // for SHA384 and SHA512 the data is too long for 512 bit keys
0N/A testSignature("SHA384withRSA", privateKey, publicKey);
0N/A testSignature("SHA512withRSA", privateKey, publicKey);
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A long start = System.currentTimeMillis();
0N/A provider = Security.getProvider("SunRsaSign");
0N/A data = new byte[2048];
0N/A new Random().nextBytes(data);
0N/A KeyStore ks = getKeyStore();
0N/A KeyFactory kf = KeyFactory.getInstance("RSA", provider);
0N/A for (Enumeration e = ks.aliases(); e.hasMoreElements(); ) {
0N/A String alias = (String)e.nextElement();
0N/A if (ks.isKeyEntry(alias)) {
0N/A System.out.println("* Key " + alias + "...");
0N/A PrivateKey privateKey = (PrivateKey)ks.getKey(alias, password);
0N/A PublicKey publicKey = ks.getCertificate(alias).getPublicKey();
0N/A privateKey = (PrivateKey)kf.translateKey(privateKey);
0N/A publicKey = (PublicKey)kf.translateKey(publicKey);
0N/A test(privateKey, publicKey);
0N/A }
0N/A }
0N/A long stop = System.currentTimeMillis();
0N/A System.out.println("All tests passed (" + (stop - start) + " ms).");
0N/A }
0N/A}