Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/rsa/SignatureTest.java
38839 views
/*1* Copyright (c) 2015, 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*/22import java.security.*;23import java.security.interfaces.RSAPrivateKey;24import java.security.interfaces.RSAPublicKey;25import java.security.spec.*;26import java.util.*;27import static javax.crypto.Cipher.PRIVATE_KEY;28import static javax.crypto.Cipher.PUBLIC_KEY;29import jdk.testlibrary.RandomFactory;3031import jdk.test.lib.SigTestUtil;32import static jdk.test.lib.SigTestUtil.SignatureType;3334/**35* @test36* @bug 8044199 814629337* @summary Create a signature for RSA and get its signed data. re-initiate38* the signature with the public key. The signature can be verified39* by acquired signed data.40* @library /lib41* @key randomness42* @library ../../../lib/testlibrary43* @run main SignatureTest MD2withRSA 51244* @run main SignatureTest MD5withRSA 51245* @run main SignatureTest SHA1withRSA 51246* @run main SignatureTest SHA256withRSA 51247* @run main SignatureTest MD2withRSA 76848* @run main SignatureTest MD5withRSA 76849* @run main SignatureTest SHA1withRSA 76850* @run main SignatureTest SHA256withRSA 76851* @run main SignatureTest MD2withRSA 102452* @run main SignatureTest MD5withRSA 102453* @run main SignatureTest SHA1withRSA 102454* @run main SignatureTest SHA256withRSA 102455* @run main SignatureTest MD2withRSA 204856* @run main SignatureTest MD5withRSA 204857* @run main SignatureTest SHA1withRSA 204858* @run main SignatureTest SHA256withRSA 204859* @run main/timeout=240 SignatureTest MD2withRSA 409660* @run main/timeout=240 SignatureTest MD5withRSA 409661* @run main/timeout=240 SignatureTest SHA1withRSA 409662* @run main/timeout=240 SignatureTest SHA256withRSA 409663* @run main/timeout=240 SignatureTest MD2withRSA 512064* @run main/timeout=240 SignatureTest MD5withRSA 512065* @run main/timeout=240 SignatureTest SHA1withRSA 512066* @run main/timeout=240 SignatureTest SHA256withRSA 512067* @run main/timeout=480 SignatureTest MD2withRSA 614468* @run main/timeout=480 SignatureTest MD5withRSA 614469* @run main/timeout=480 SignatureTest SHA1withRSA 614470* @run main/timeout=480 SignatureTest SHA256withRSA 614471*/72public class SignatureTest {73/**74* ALGORITHM name, fixed as RSA.75*/76private static final String KEYALG = "RSA";7778/**79* JDK default RSA Provider.80*/81private static final String PROVIDER = "SunRsaSign";8283/**84* How much times signature updated.85*/86private static final int UPDATE_TIMES_FIFTY = 50;8788/**89* How much times signature initial updated.90*/91private static final int UPDATE_TIMES_HUNDRED = 100;9293public static void main(String[] args) throws Exception {94String testAlg = args[0];95int keySize = Integer.parseInt(args[1]);96Iterable<String> md_alg_pkcs15 =97SigTestUtil.getDigestAlgorithms(SignatureType.RSA, keySize);9899Iterable<String> md_alg_pss =100SigTestUtil.getDigestAlgorithms(SignatureType.RSASSA_PSS, keySize);101102byte[] data = new byte[100];103RandomFactory.getRandom().nextBytes(data);104105// create a key pair106KeyPair kpair = generateKeys(KEYALG, keySize);107Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());108Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());109110test(SignatureType.RSA, md_alg_pkcs15, privs, pubs, data);111test(SignatureType.RSASSA_PSS, md_alg_pss, privs, pubs, data);112}113114private static void test(SignatureType type, Iterable<String> digestAlgs,115Key[] privs, Key[] pubs, byte[] data) throws RuntimeException {116117// For signature algorithm, create and verify a signature118119Arrays.stream(privs).forEach(priv120-> Arrays.stream(pubs).forEach(pub121-> digestAlgs.forEach(digestAlg -> {122try {123AlgorithmParameterSpec sigParams =124SigTestUtil.generateDefaultParameter(type, digestAlg);125String sigAlg = SigTestUtil.generateSigAlg(type, digestAlg);126checkSignature(data, (PublicKey) pub, (PrivateKey) priv,127sigAlg, sigParams);128} catch (NoSuchAlgorithmException | InvalidKeyException |129SignatureException | NoSuchProviderException |130InvalidAlgorithmParameterException ex) {131throw new RuntimeException(ex);132}133}134)));135}136137private static KeyPair generateKeys(String keyalg, int size)138throws NoSuchAlgorithmException {139KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg);140kpg.initialize(size);141return kpg.generateKeyPair();142}143144private static Key[] manipulateKey(int type, Key key)145throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {146KeyFactory kf = KeyFactory.getInstance(KEYALG, PROVIDER);147148switch (type) {149case PUBLIC_KEY:150try {151kf.getKeySpec(key, RSAPrivateKeySpec.class);152throw new RuntimeException("Expected InvalidKeySpecException "153+ "not thrown");154} catch (InvalidKeySpecException expected) {155}156157return new Key[]{158kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),159kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),160kf.generatePublic(new RSAPublicKeySpec(161((RSAPublicKey) key).getModulus(),162((RSAPublicKey) key).getPublicExponent()))163};164case PRIVATE_KEY:165try {166kf.getKeySpec(key, RSAPublicKeySpec.class);167throw new RuntimeException("Expected InvalidKeySpecException"168+ " not thrown");169} catch (InvalidKeySpecException expected) {170}171return new Key[]{172kf.generatePrivate(kf.getKeySpec(key,173RSAPrivateKeySpec.class)),174kf.generatePrivate(new PKCS8EncodedKeySpec(175key.getEncoded())),176kf.generatePrivate(new RSAPrivateKeySpec(((RSAPrivateKey) key).getModulus(),177((RSAPrivateKey) key).getPrivateExponent()))178};179}180throw new RuntimeException("We shouldn't reach here");181}182183private static void checkSignature(byte[] data, PublicKey pub,184PrivateKey priv, String sigAlg, AlgorithmParameterSpec sigParams)185throws NoSuchAlgorithmException, InvalidKeyException,186SignatureException, NoSuchProviderException,187InvalidAlgorithmParameterException {188System.out.println("Testing " + sigAlg);189Signature sig = Signature.getInstance(sigAlg, PROVIDER);190sig.setParameter(sigParams);191192sig.initSign(priv);193for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {194sig.update(data);195}196byte[] signedData = sig.sign();197198// Make sure signature verifies with original data199sig.setParameter(sigParams);200sig.initVerify(pub);201for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {202sig.update(data);203}204if (!sig.verify(signedData)) {205throw new RuntimeException("Failed to verify " + sigAlg206+ " signature");207}208209// Make sure signature does NOT verify when the original data210// has changed211sig.initVerify(pub);212for (int i = 0; i < UPDATE_TIMES_FIFTY; i++) {213sig.update(data);214}215216if (sig.verify(signedData)) {217throw new RuntimeException("Failed to detect bad " + sigAlg218+ " signature");219}220}221}222223224