Path: blob/master/test/jdk/sun/security/rsa/pss/SignatureTest2.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.RSAPrivateKey;24import java.security.interfaces.RSAPublicKey;25import java.security.spec.*;26import java.util.Arrays;27import java.util.stream.IntStream;28import static javax.crypto.Cipher.PRIVATE_KEY;29import static javax.crypto.Cipher.PUBLIC_KEY;3031/**32* @test33* @bug 8146293 8238448 817236634* @summary Create a signature for RSASSA-PSS and get its signed data.35* re-initiate the signature with the public key. The signature36* can be verified by acquired signed data.37* @run main SignatureTest2 76838* @run main SignatureTest2 102439* @run main SignatureTest2 102540* @run main SignatureTest2 204841* @run main SignatureTest2 204942* @run main/timeout=240 SignatureTest2 409643*/44public class SignatureTest2 {45/**46* ALGORITHM name, fixed as RSA.47*/48private static final String KEYALG = "RSASSA-PSS";4950/**51* JDK default RSA Provider.52*/53private static final String PROVIDER = "SunRsaSign";5455/**56* How much times signature updated.57*/58private static final int UPDATE_TIMES_TWO = 2;5960/**61* How much times signature initial updated.62*/63private static final int UPDATE_TIMES_TEN = 10;6465/**66* Digest algorithms to test w/ RSASSA-PSS signature algorithms67*/68private static final String[] DIGEST_ALG = {69"SHA-1", "SHA-224", "SHA-256", "SHA-384",70"SHA-512", "SHA-512/224", "SHA-512/256",71"SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512"72};7374private static final String SIG_ALG = "RSASSA-PSS";7576private static PSSParameterSpec genPSSParameter(String digestAlgo,77int digestLen, int keySize) {78// pick a salt length based on the key length and digestAlgo79int saltLength = keySize/8 - digestLen - 2;80if (saltLength < 0) {81System.out.println("keysize: " + keySize/8 + ", digestLen: " + digestLen);82return null;83}84return new PSSParameterSpec(digestAlgo, "MGF1",85new MGF1ParameterSpec(digestAlgo), saltLength, 1);86}8788public static void main(String[] args) throws Exception {89final int testSize = Integer.parseInt(args[0]);9091byte[] data = new byte[100];92IntStream.range(0, data.length).forEach(j -> {93data[j] = (byte) j;94});9596// create a key pair97KeyPair kpair = generateKeys(KEYALG, testSize);98Key[] privs = manipulateKey(PRIVATE_KEY, kpair.getPrivate());99Key[] pubs = manipulateKey(PUBLIC_KEY, kpair.getPublic());100101// For messsage digest algorithm, create and verify a RSASSA-PSS signature102Arrays.stream(privs).forEach(priv103-> Arrays.stream(pubs).forEach(pub104-> Arrays.stream(DIGEST_ALG).forEach(testAlg -> {105checkSignature(data, (PublicKey) pub, (PrivateKey) priv,106testAlg, testSize);107}108)));109110}111112private static KeyPair generateKeys(String keyalg, int size)113throws NoSuchAlgorithmException {114KeyPairGenerator kpg = KeyPairGenerator.getInstance(keyalg);115kpg.initialize(size);116return kpg.generateKeyPair();117}118119private static Key[] manipulateKey(int type, Key key)120throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {121KeyFactory kf = KeyFactory.getInstance(KEYALG, PROVIDER);122123switch (type) {124case PUBLIC_KEY:125return new Key[]{126kf.generatePublic(kf.getKeySpec(key, RSAPublicKeySpec.class)),127kf.generatePublic(new X509EncodedKeySpec(key.getEncoded())),128kf.generatePublic(new RSAPublicKeySpec(129((RSAPublicKey) key).getModulus(),130((RSAPublicKey) key).getPublicExponent()))131};132case PRIVATE_KEY:133return new Key[]{134kf.generatePrivate(kf.getKeySpec(key,135RSAPrivateKeySpec.class)),136kf.generatePrivate(new PKCS8EncodedKeySpec(137key.getEncoded())),138kf.generatePrivate(new RSAPrivateKeySpec(((RSAPrivateKey) key).getModulus(),139((RSAPrivateKey) key).getPrivateExponent()))140};141}142throw new RuntimeException("We shouldn't reach here");143}144145private static void checkSignature(byte[] data, PublicKey pub,146PrivateKey priv, String digestAlg, int keySize) throws RuntimeException {147try {148Signature sig = Signature.getInstance(SIG_ALG, PROVIDER);149int digestLen = MessageDigest.getInstance(digestAlg).getDigestLength();150PSSParameterSpec params = genPSSParameter(digestAlg, digestLen, keySize);151if (params == null) {152System.out.println("Skip test due to short key size");153return;154}155sig.setParameter(params);156sig.initSign(priv);157for (int i = 0; i < UPDATE_TIMES_TEN; i++) {158sig.update(data);159}160byte[] signedDataTen = sig.sign();161162// Make sure signature can be generated without re-init163sig.update(data);164byte[] signedDataOne = sig.sign();165166// Make sure signature verifies with original data167System.out.println("Verify using params " + sig.getParameters());168sig.initVerify(pub);169sig.setParameter(params);170for (int i = 0; i < UPDATE_TIMES_TEN; i++) {171sig.update(data);172}173if (!sig.verify(signedDataTen)) {174throw new RuntimeException("Signature verification test#1 failed w/ "175+ digestAlg);176}177178// Make sure signature can verify without re-init179sig.update(data);180if (!sig.verify(signedDataOne)) {181throw new RuntimeException("Signature verification test#2 failed w/ "182+ digestAlg);183}184185// Make sure signature does NOT verify when the original data186// has changed187for (int i = 0; i < UPDATE_TIMES_TWO; i++) {188sig.update(data);189}190191if (sig.verify(signedDataOne)) {192throw new RuntimeException("Bad signature accepted w/ "193+ digestAlg);194}195} catch (NoSuchAlgorithmException | InvalidKeyException |196SignatureException | NoSuchProviderException |197InvalidAlgorithmParameterException e) {198e.printStackTrace();199throw new RuntimeException(e);200}201}202}203204205