Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Signature/KeyAndParamCheckForPSS.java
38855 views
/*1* Copyright (c) 2019, 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 808046229* @summary Ensure that PSS key and params check are implemented properly30* regardless of call sequence31* @library ..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}57// NOTE: key length >= (digest length + 2) in bytes58// otherwise, even salt length = 0 would not work59runTest(p, 1024, "SHA-384");60runTest(p, 1040, "SHA-512");61}6263private void runTest(Provider p, int keySize, String hashAlg)64throws Exception {65System.out.println("Testing [" + keySize + " " + hashAlg + "]");6667// create a key pair with the supplied size68KeyPairGenerator kpg = KeyPairGenerator.getInstance(KEYALG, p);69kpg.initialize(keySize);70KeyPair kp = kpg.generateKeyPair();7172int bigSaltLen = keySize/8 - 14;73AlgorithmParameterSpec paramsBad = new PSSParameterSpec(hashAlg,74"MGF1", new MGF1ParameterSpec(hashAlg), bigSaltLen, 1);75AlgorithmParameterSpec paramsGood = new PSSParameterSpec(hashAlg,76"MGF1", new MGF1ParameterSpec(hashAlg), 0, 1);7778PrivateKey priv = kp.getPrivate();79PublicKey pub = kp.getPublic();8081// test#1 - setParameter then initSign82Signature sig = Signature.getInstance("RSASSA-PSS", p);83sig.setParameter(paramsBad);84try {85sig.initSign(priv);86throw new RuntimeException("Expected IKE not thrown");87} catch (InvalidKeyException ike) {88System.out.println("test#1: got expected IKE");89}90sig.setParameter(paramsGood);91sig.initSign(priv);92System.out.println("test#1: pass");9394// test#2 - setParameter then initVerify95sig = Signature.getInstance("RSASSA-PSS", p);96sig.setParameter(paramsBad);97try {98sig.initVerify(pub);99throw new RuntimeException("Expected IKE not thrown");100} catch (InvalidKeyException ike) {101System.out.println("test#2: got expected IKE");102}103sig.setParameter(paramsGood);104sig.initVerify(pub);105System.out.println("test#2: pass");106107// test#3 - initSign, then setParameter108sig = Signature.getInstance("RSASSA-PSS", p);109sig.initSign(priv);110try {111sig.setParameter(paramsBad);112throw new RuntimeException("Expected IAPE not thrown");113} catch (InvalidAlgorithmParameterException iape) {114System.out.println("test#3: got expected IAPE");115}116sig.setParameter(paramsGood);117System.out.println("test#3: pass");118119// test#4 - initVerify, then setParameter120sig = Signature.getInstance("RSASSA-PSS", p);121sig.initVerify(pub);122try {123sig.setParameter(paramsBad);124throw new RuntimeException("Expected IAPE not thrown");125} catch (InvalidAlgorithmParameterException iape) {126System.out.println("test#4: got expected IAPE");127}128sig.setParameter(paramsGood);129System.out.println("test#4: pass");130}131}132133134