Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/rsa/pss/PSSParametersTest.java
38853 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 824255634* @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));52System.out.println("Test Passed");53}5455// test the given spec by first initializing w/ it, generate the DER56// bytes, then initialize w/ the DER bytes, retrieve the spec.57// compare both spec for equality and throw exception if the check failed.58private static void test(PSSParameterSpec spec) throws Exception {59String ALGORITHMS[] = { PSS_ALGO, PSS_OID };60for (String alg : ALGORITHMS) {61AlgorithmParameters params = AlgorithmParameters.getInstance62(alg, PROVIDER);63params.init(spec);64byte[] encoded = params.getEncoded();65AlgorithmParameters params2 = AlgorithmParameters.getInstance66(alg, PROVIDER);67params2.init(encoded);68PSSParameterSpec spec2 = params2.getParameterSpec69(PSSParameterSpec.class);70if (!isEqual(spec, spec2)) {71throw new RuntimeException("Spec check Failed for " + alg);72}73}74}7576private static boolean isEqual(PSSParameterSpec spec,77PSSParameterSpec spec2) throws Exception {78if (spec == spec2) return true;79if (spec == null || spec2 == null) return false;8081if (!spec.getDigestAlgorithm().equals(spec2.getDigestAlgorithm())) {82System.out.println("Different digest algorithms: " +83spec.getDigestAlgorithm() + " vs " + spec2.getDigestAlgorithm());84return false;85}86if (!spec.getMGFAlgorithm().equals(spec2.getMGFAlgorithm())) {87System.out.println("Different MGF algorithms: " +88spec.getMGFAlgorithm() + " vs " + spec2.getMGFAlgorithm());89return false;90}91if (spec.getSaltLength() != spec2.getSaltLength()) {92System.out.println("Different Salt Length: " +93spec.getSaltLength() + " vs " + spec2.getSaltLength());94return false;95}96if (spec.getTrailerField() != spec2.getTrailerField()) {97System.out.println("Different TrailerField: " +98spec.getTrailerField() + " vs " + spec2.getTrailerField());99return false;100}101// continue checking MGF Parameters102AlgorithmParameterSpec mgfParams = spec.getMGFParameters();103AlgorithmParameterSpec mgfParams2 = spec2.getMGFParameters();104if (mgfParams == mgfParams2) return true;105if (mgfParams == null || mgfParams2 == null) {106System.out.println("Different MGF Parameters: " +107mgfParams + " vs " + mgfParams2);108return false;109}110if (mgfParams instanceof MGF1ParameterSpec) {111if (mgfParams2 instanceof MGF1ParameterSpec) {112boolean result =113((MGF1ParameterSpec)mgfParams).getDigestAlgorithm().equals114(((MGF1ParameterSpec)mgfParams2).getDigestAlgorithm());115if (!result) {116System.out.println("Different MGF1 digest algorithms: " +117((MGF1ParameterSpec)mgfParams).getDigestAlgorithm() +118" vs " +119((MGF1ParameterSpec)mgfParams2).getDigestAlgorithm());120}121return result;122} else {123System.out.println("Different MGF Parameters types: " +124mgfParams.getClass() + " vs " + mgfParams2.getClass());125return false;126}127}128throw new RuntimeException("Unrecognized MGFParameters: " + mgfParams);129}130}131132133