Path: blob/master/test/jdk/sun/security/rsa/pss/SignatureTestPSS.java
41153 views
/*1* Copyright (c) 2018, 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.*;24import java.security.spec.*;25import java.util.Arrays;26import java.util.stream.IntStream;27import jdk.test.lib.SigTestUtil;28import static jdk.test.lib.SigTestUtil.SignatureType;29import static javax.crypto.Cipher.PRIVATE_KEY;30import static javax.crypto.Cipher.PUBLIC_KEY;3132/**33* @test34* @bug 8146293 823844835* @summary Create a signature for RSASSA-PSS and get its signed data.36* re-initiate the signature with the public key. The signature37* can be verified by acquired signed data.38* @library /test/lib39* @build jdk.test.lib.SigTestUtil40* @run main SignatureTestPSS 51241* @run main SignatureTestPSS 76842* @run main SignatureTestPSS 102443* @run main SignatureTestPSS 102544* @run main SignatureTestPSS 204845* @run main SignatureTestPSS 204946* @run main/timeout=240 SignatureTestPSS 409647* @run main/timeout=240 SignatureTestPSS 512048* @run main/timeout=480 SignatureTestPSS 614449*/50public class SignatureTestPSS {51/**52* ALGORITHM name, fixed as RSASSA-PSS.53*/54private static final String KEYALG = SignatureType.RSASSA_PSS.toString();5556/**57* JDK default RSA Provider.58*/59private static final String PROVIDER = "SunRsaSign";6061/**62* How much times signature updated.63*/64private static final int UPDATE_TIMES_FIFTY = 50;6566/**67* How much times signature initial updated.68*/69private static final int UPDATE_TIMES_HUNDRED = 100;7071public static void main(String[] args) throws Exception {72int testSize = Integer.parseInt(args[0]);7374Iterable<String> md_alg_pss =75SigTestUtil.getDigestAlgorithms(SignatureType.RSASSA_PSS, testSize);7677byte[] data = new byte[100];78IntStream.range(0, data.length).forEach(j -> {79data[j] = (byte) j;80});8182// create a key pair83KeyPair kpair = generateKeys(KEYALG, testSize);84Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());85Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());8687test(md_alg_pss, privs, pubs, data);88}8990private static void test(Iterable<String> testAlgs, Key[] privs,91Key[] pubs, byte[] data) throws RuntimeException {92// For signature algorithm, create and verify a signature93Arrays.stream(privs).forEach(priv94-> Arrays.stream(pubs).forEach(pub95-> testAlgs.forEach(testAlg -> {96try {97checkSignature(data, (PublicKey) pub, (PrivateKey) priv,98testAlg);99} catch (NoSuchAlgorithmException | InvalidKeyException |100SignatureException | NoSuchProviderException |101InvalidAlgorithmParameterException ex) {102throw new RuntimeException(ex);103}104}105)));106107}108109private static KeyPair generateKeys(String keyalg, int size)110throws NoSuchAlgorithmException {111KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg);112kpg.initialize(size);113return kpg.generateKeyPair();114}115116private static Key[] manipulateKey(int type, Key key)117throws NoSuchAlgorithmException, InvalidKeySpecException,118NoSuchProviderException {119KeyFactory kf = KeyFactory.getInstance(KEYALG, PROVIDER);120switch (type) {121case PUBLIC_KEY:122try {123kf.getKeySpec(key, RSAPrivateKeySpec.class);124throw new RuntimeException("Expected InvalidKeySpecException"125+ " not thrown");126} catch (InvalidKeySpecException expected) {127System.out.println("Expected IKSE thrown for PublicKey");128}129return new Key[]{130kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),131kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),132kf.generatePublic(new RSAPublicKeySpec(133((RSAPublicKey)key).getModulus(),134((RSAPublicKey)key).getPublicExponent(),135((RSAPublicKey)key).getParams()))136};137case PRIVATE_KEY:138try {139kf.getKeySpec(key, RSAPublicKeySpec.class);140throw new RuntimeException("Expected InvalidKeySpecException"141+ " not thrown");142} catch (InvalidKeySpecException expected) {143System.out.println("Expected IKSE thrown for PrivateKey");144}145return new Key[]{146kf.generatePrivate(kf.getKeySpec(key, RSAPrivateKeySpec.class)),147kf.generatePrivate(new PKCS8EncodedKeySpec(key.getEncoded())),148kf.generatePrivate(new RSAPrivateKeySpec(149((RSAPrivateKey)key).getModulus(),150((RSAPrivateKey)key).getPrivateExponent(),151((RSAPrivateKey)key).getParams()))152};153}154throw new RuntimeException("We shouldn't reach here");155}156157private static void checkSignature(byte[] data, PublicKey pub,158PrivateKey priv, String mdAlg) throws NoSuchAlgorithmException,159InvalidKeyException, SignatureException, NoSuchProviderException,160InvalidAlgorithmParameterException {161System.out.println("Testing against " + mdAlg);162Signature sig = Signature.getInstance163(SignatureType.RSASSA_PSS.toString(), PROVIDER);164AlgorithmParameterSpec params =165SigTestUtil.generateDefaultParameter(SignatureType.RSASSA_PSS, mdAlg);166sig.setParameter(params);167sig.initSign(priv);168for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {169sig.update(data);170}171byte[] signedData = sig.sign();172173// Make sure signature verifies with original data174// do we need to call sig.setParameter(params) again?175sig.initVerify(pub);176for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {177sig.update(data);178}179if (!sig.verify(signedData)) {180throw new RuntimeException("Failed to verify signature");181}182183// Make sure signature does NOT verify when the original data184// has changed185sig.initVerify(pub);186for (int i = 0; i < UPDATE_TIMES_FIFTY; i++) {187sig.update(data);188}189190if (sig.verify(signedData)) {191throw new RuntimeException("Failed to detect bad signature");192}193}194}195196197