Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Signature/SigInteropPSS.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*/2223import java.security.*;24import java.security.spec.*;25import java.security.interfaces.*;2627/*28* @test29* @bug 808046230* @summary testing interoperability of PSS signatures of PKCS11 provider31* against SunRsaSign provider32* @library ..33* @modules jdk.crypto.cryptoki34* @run main/othervm SigInteropPSS35*/36public class SigInteropPSS extends PKCS11Test {3738private static final byte[] MSG =39"Interoperability test between SunRsaSign and SunPKCS11".getBytes();4041private static final String[] DIGESTS = {42"SHA-224", "SHA-256", "SHA-384", "SHA-512"43};4445public static void main(String[] args) throws Exception {46main(new SigInteropPSS(), args);47}4849@Override50public void main(Provider p) throws Exception {51Signature sigPkcs11;52try {53sigPkcs11 = Signature.getInstance("RSASSA-PSS", p);54} catch (NoSuchAlgorithmException e) {55System.out.println("Skip testing RSASSA-PSS" +56" due to no support");57return;58}5960Signature sigSunRsaSign =61Signature.getInstance("RSASSA-PSS", "SunRsaSign");6263KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);64kpg.initialize(3072);65KeyPair kp = kpg.generateKeyPair();66boolean status;67try {68status = runTest(sigSunRsaSign, sigPkcs11, kp);69status &= runTest(sigPkcs11, sigSunRsaSign, kp);70} catch (Exception e) {71System.out.println("Unexpected exception: " + e);72e.printStackTrace(System.out);73status = false;74}7576if (!status) {77throw new RuntimeException("One or more test failed");78}79System.out.println("Test passed");80}8182static boolean runTest(Signature signer, Signature verifier, KeyPair kp) throws Exception {83System.out.println("\tSign using " + signer.getProvider().getName());84System.out.println("\tVerify using " + verifier.getProvider().getName());8586boolean status;87for (String digestAlg : DIGESTS) {88System.out.println("\tDigest = " + digestAlg);89PSSParameterSpec params = new PSSParameterSpec(digestAlg, "MGF1",90new MGF1ParameterSpec(digestAlg), 0, 1);91try {92signer.setParameter(params);93signer.initSign(kp.getPrivate());94verifier.setParameter(params);95verifier.initVerify(kp.getPublic());96} catch (Exception e) {97System.out.println("\tERROR: unexpected ex during init" + e);98status = false;99continue;100}101try {102signer.update(MSG);103byte[] sigBytes = signer.sign();104verifier.update(MSG);105boolean isValid = verifier.verify(sigBytes);106if (isValid) {107System.out.println("\tPSS Signature verified");108} else {109System.out.println("\tERROR verifying PSS Signature");110status = false;111}112} catch (Exception e) {113System.out.println("\tERROR: unexpected ex" + e);114e.printStackTrace();115status = false;116}117}118return true;119}120}121122123