Path: blob/master/test/jdk/sun/security/pkcs11/Signature/KeyAndParamCheckForPSS.java
41153 views
/*1* Copyright (c) 2019, 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.*;2526/**27* @test28* @bug 8080462 8226651 824233229* @summary Ensure that PSS key and params check are implemented properly30* regardless of call sequence31* @library /test/lib ..32* @modules jdk.crypto.cryptoki33* @run main KeyAndParamCheckForPSS34*/35public class KeyAndParamCheckForPSS extends PKCS11Test {3637/**38* ALGORITHM name, fixed as RSA for PKCS1139*/40private static final String KEYALG = "RSA";41private static final String SIGALG = "RSASSA-PSS";4243public static void main(String[] args) throws Exception {44main(new KeyAndParamCheckForPSS(), args);45}4647@Override48public void main(Provider p) throws Exception {49Signature sig;50try {51sig = Signature.getInstance(SIGALG, p);52} catch (NoSuchAlgorithmException e) {53System.out.println("Skip testing RSASSA-PSS" +54" due to no support");55return;56}5758// NOTE: key length >= (digest length + 2) in bytes59// otherwise, even salt length = 0 would not work60runTest(p, 1024, "SHA-256", "SHA-256");61runTest(p, 1024, "SHA-256", "SHA-384");62runTest(p, 1024, "SHA-256", "SHA-512");63runTest(p, 1024, "SHA-384", "SHA-256");64runTest(p, 1024, "SHA-384", "SHA-384");65runTest(p, 1024, "SHA-384", "SHA-512");66runTest(p, 1040, "SHA-512", "SHA-256");67runTest(p, 1040, "SHA-512", "SHA-384");68runTest(p, 1040, "SHA-512", "SHA-512");69runTest(p, 1024, "SHA3-256", "SHA3-256");70runTest(p, 1024, "SHA3-256", "SHA3-384");71runTest(p, 1024, "SHA3-256", "SHA3-512");72runTest(p, 1024, "SHA3-384", "SHA3-256");73runTest(p, 1024, "SHA3-384", "SHA3-384");74runTest(p, 1024, "SHA3-384", "SHA3-512");75runTest(p, 1040, "SHA3-512", "SHA3-256");76runTest(p, 1040, "SHA3-512", "SHA3-384");77runTest(p, 1040, "SHA3-512", "SHA3-512");78}7980private void runTest(Provider p, int keySize, String hashAlg,81String mgfHashAlg) throws Exception {8283// skip further test if this provider does not support hashAlg or84// mgfHashAlg85try {86MessageDigest.getInstance(hashAlg, p);87MessageDigest.getInstance(mgfHashAlg, p);88} catch (NoSuchAlgorithmException nsae) {89System.out.println("No support for " + hashAlg + ", skip");90return;91}9293System.out.println("Testing [" + keySize + " " + hashAlg + "]");9495// create a key pair with the supplied size96KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEYALG, p);97kpg.initialize(keySize);98KeyPair kp = kpg.generateKeyPair();99100int bigSaltLen = keySize/8 - 14;101AlgorithmParameterSpec paramsBad = new PSSParameterSpec(hashAlg,102"MGF1", new MGF1ParameterSpec(mgfHashAlg), bigSaltLen, 1);103AlgorithmParameterSpec paramsGood = new PSSParameterSpec(hashAlg,104"MGF1", new MGF1ParameterSpec(mgfHashAlg), 0, 1);105106PrivateKey priv = kp.getPrivate();107PublicKey pub = kp.getPublic();108109// test#1 - setParameter then initSign110Signature sig = Signature.getInstance("RSASSA-PSS", p);111sig.setParameter(paramsBad);112try {113sig.initSign(priv);114throw new RuntimeException("Expected IKE not thrown");115} catch (InvalidKeyException ike) {116System.out.println("test#1: got expected IKE");117}118119sig.setParameter(paramsGood);120sig.initSign(priv);121System.out.println("test#1: pass");122123// test#2 - setParameter then initVerify124sig = Signature.getInstance("RSASSA-PSS", p);125sig.setParameter(paramsBad);126try {127sig.initVerify(pub);128throw new RuntimeException("Expected IKE not thrown");129} catch (InvalidKeyException ike) {130System.out.println("test#2: got expected IKE");131}132133sig.setParameter(paramsGood);134sig.initVerify(pub);135136System.out.println("test#2: pass");137138// test#3 - initSign, then setParameter139sig = Signature.getInstance("RSASSA-PSS", p);140sig.initSign(priv);141try {142sig.setParameter(paramsBad);143throw new RuntimeException("Expected IAPE not thrown");144} catch (InvalidAlgorithmParameterException iape) {145System.out.println("test#3: got expected IAPE");146}147148sig.setParameter(paramsGood);149System.out.println("test#3: pass");150151// test#4 - initVerify, then setParameter152sig = Signature.getInstance("RSASSA-PSS", p);153sig.initVerify(pub);154try {155sig.setParameter(paramsBad);156throw new RuntimeException("Expected IAPE not thrown");157} catch (InvalidAlgorithmParameterException iape) {158System.out.println("test#4: got expected IAPE");159}160161sig.setParameter(paramsGood);162System.out.println("test#4: pass");163}164}165166167