Path: blob/master/test/jdk/sun/security/rsa/pss/PSSParametersTest.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 8242556 817236634* @summary Test RSASSA-PSS AlgorithmParameters impl of SunRsaSign provider.35* @run main PSSParametersTest36*/37public class PSSParametersTest {38/**39* JDK default RSA Provider.40*/41private static final String PROVIDER = "SunRsaSign";4243private static final String PSS_ALGO = "RSASSA-PSS";44private static final String PSS_OID = "1.2.840.113549.1.1.10";4546public static void main(String[] args) throws Exception {47System.out.println("Testing against DEFAULT parameters");48test(PSSParameterSpec.DEFAULT);49System.out.println("Testing against custom parameters");50test(new PSSParameterSpec("SHA-512/224", "MGF1",51MGF1ParameterSpec.SHA384, 100, 1));52test(new PSSParameterSpec("SHA3-256", "MGF1",53new MGF1ParameterSpec("SHA3-256"), 256>>3, 1));54System.out.println("Test Passed");55}5657// test the given spec by first initializing w/ it, generate the DER58// bytes, then initialize w/ the DER bytes, retrieve the spec.59// compare both spec for equality and throw exception if the check failed.60private static void test(PSSParameterSpec spec) throws Exception {61System.out.println("Testing PSS spec: " + spec);62String ALGORITHMS[] = { PSS_ALGO, PSS_OID };63for (String alg : ALGORITHMS) {64AlgorithmParameters params = AlgorithmParameters.getInstance65(alg, PROVIDER);66params.init(spec);67byte[] encoded = params.getEncoded();68AlgorithmParameters params2 = AlgorithmParameters.getInstance69(alg, PROVIDER);70params2.init(encoded);71PSSParameterSpec spec2 = params2.getParameterSpec72(PSSParameterSpec.class);73if (!isEqual(spec, spec2)) {74throw new RuntimeException("Spec check Failed for " + alg);75}76}77}7879private static boolean isEqual(PSSParameterSpec spec,80PSSParameterSpec spec2) throws Exception {81if (spec == spec2) return true;82if (spec == null || spec2 == null) return false;8384if (!spec.getDigestAlgorithm().equals(spec2.getDigestAlgorithm())) {85System.out.println("Different digest algorithms: " +86spec.getDigestAlgorithm() + " vs " + spec2.getDigestAlgorithm());87return false;88}89if (!spec.getMGFAlgorithm().equals(spec2.getMGFAlgorithm())) {90System.out.println("Different MGF algorithms: " +91spec.getMGFAlgorithm() + " vs " + spec2.getMGFAlgorithm());92return false;93}94if (spec.getSaltLength() != spec2.getSaltLength()) {95System.out.println("Different Salt Length: " +96spec.getSaltLength() + " vs " + spec2.getSaltLength());97return false;98}99if (spec.getTrailerField() != spec2.getTrailerField()) {100System.out.println("Different TrailerField: " +101spec.getTrailerField() + " vs " + spec2.getTrailerField());102return false;103}104// continue checking MGF Parameters105AlgorithmParameterSpec mgfParams = spec.getMGFParameters();106AlgorithmParameterSpec mgfParams2 = spec2.getMGFParameters();107if (mgfParams == mgfParams2) return true;108if (mgfParams == null || mgfParams2 == null) {109System.out.println("Different MGF Parameters: " +110mgfParams + " vs " + mgfParams2);111return false;112}113if (mgfParams instanceof MGF1ParameterSpec) {114if (mgfParams2 instanceof MGF1ParameterSpec) {115boolean result =116((MGF1ParameterSpec)mgfParams).getDigestAlgorithm().equals117(((MGF1ParameterSpec)mgfParams2).getDigestAlgorithm());118if (!result) {119System.out.println("Different MGF1 digest algorithms: " +120((MGF1ParameterSpec)mgfParams).getDigestAlgorithm() +121" vs " +122((MGF1ParameterSpec)mgfParams2).getDigestAlgorithm());123}124return result;125} else {126System.out.println("Different MGF Parameters types: " +127mgfParams.getClass() + " vs " + mgfParams2.getClass());128return false;129}130}131throw new RuntimeException("Unrecognized MGFParameters: " + mgfParams);132}133}134135136