test/jdk/sun/security/rsa/pss/PSSParametersTest.java
branchJDK-8145252-TLS13-branch
changeset 56542 56aaa6cb3693
equal deleted inserted replaced
56541:92cbbfc996f3 56542:56aaa6cb3693
       
     1 /*
       
     2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     7  * published by the Free Software Foundation.
       
     8  *
       
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    12  * version 2 for more details (a copy is included in the LICENSE file that
       
    13  * accompanied this code).
       
    14  *
       
    15  * You should have received a copy of the GNU General Public License version
       
    16  * 2 along with this work; if not, write to the Free Software Foundation,
       
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    18  *
       
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 import java.security.*;
       
    24 import java.security.interfaces.RSAPrivateKey;
       
    25 import java.security.interfaces.RSAPublicKey;
       
    26 import java.security.spec.*;
       
    27 import java.util.Arrays;
       
    28 import java.util.stream.IntStream;
       
    29 import static javax.crypto.Cipher.PRIVATE_KEY;
       
    30 import static javax.crypto.Cipher.PUBLIC_KEY;
       
    31 
       
    32 /**
       
    33  * @test
       
    34  * @bug 8146293
       
    35  * @summary Test RSASSA-PSS AlgorithmParameters impl of SunRsaSign provider.
       
    36  * @run main PSSParametersTest
       
    37  */
       
    38 public class PSSParametersTest {
       
    39     /**
       
    40      * JDK default RSA Provider.
       
    41      */
       
    42     private static final String PROVIDER = "SunRsaSign";
       
    43 
       
    44     private static final String ALGO = "RSASSA-PSS";
       
    45 
       
    46     public static void main(String[] args) throws Exception {
       
    47         System.out.println("Testing against DEFAULT parameters");
       
    48         test(PSSParameterSpec.DEFAULT);
       
    49         System.out.println("Testing against custom parameters");
       
    50         test(new PSSParameterSpec("SHA-512/224", "MGF1", MGF1ParameterSpec.SHA384,
       
    51             100, 1));
       
    52         System.out.println("Test Passed");
       
    53     }
       
    54 
       
    55     // test against the given spec by initializing w/ it, generate the DER bytes,
       
    56     // then initialize another instance w/ the DER bytes, retrieve the spec.
       
    57     // compare both spec for equality and throw exception if the comparison failed.
       
    58     private static void test(PSSParameterSpec spec) throws Exception {
       
    59         AlgorithmParameters params = AlgorithmParameters.getInstance(ALGO, PROVIDER);
       
    60         params.init(spec);
       
    61         byte[] encoded = params.getEncoded();
       
    62         AlgorithmParameters params2 = AlgorithmParameters.getInstance(ALGO, PROVIDER);
       
    63         params2.init(encoded);
       
    64         PSSParameterSpec spec2 = params2.getParameterSpec(PSSParameterSpec.class);
       
    65         if (!isEqual(spec, spec2)) {
       
    66             throw new RuntimeException("Spec check Failed");
       
    67         }
       
    68     }
       
    69 
       
    70     private static boolean isEqual(PSSParameterSpec spec, PSSParameterSpec spec2)
       
    71         throws Exception {
       
    72         if (spec == spec2) return true;
       
    73         if (spec == null || spec2 == null) return false;
       
    74 
       
    75         if (!spec.getDigestAlgorithm().equals(spec2.getDigestAlgorithm())) {
       
    76             System.out.println("Different digest algorithms: " +
       
    77                 spec.getDigestAlgorithm() + " vs " + spec2.getDigestAlgorithm());
       
    78             return false;
       
    79         }
       
    80         if (!spec.getMGFAlgorithm().equals(spec2.getMGFAlgorithm())) {
       
    81             System.out.println("Different MGF algorithms: " +
       
    82                 spec.getMGFAlgorithm() + " vs " + spec2.getMGFAlgorithm());
       
    83             return false;
       
    84         }
       
    85         if (spec.getSaltLength() != spec2.getSaltLength()) {
       
    86             System.out.println("Different Salt Length: " +
       
    87                 spec.getSaltLength() + " vs " + spec2.getSaltLength());
       
    88             return false;
       
    89         }
       
    90         if (spec.getTrailerField() != spec2.getTrailerField()) {
       
    91             System.out.println("Different TrailerField: " +
       
    92                 spec.getTrailerField() + " vs " + spec2.getTrailerField());
       
    93             return false;
       
    94         }
       
    95         // continue checking MGF Parameters
       
    96         AlgorithmParameterSpec mgfParams = spec.getMGFParameters();
       
    97         AlgorithmParameterSpec mgfParams2 = spec2.getMGFParameters();
       
    98         if (mgfParams == mgfParams2) return true;
       
    99         if (mgfParams == null || mgfParams2 == null) {
       
   100             System.out.println("Different MGF Parameters: " +
       
   101                 mgfParams + " vs " + mgfParams2);
       
   102             return false;
       
   103         }
       
   104         if (mgfParams instanceof MGF1ParameterSpec) {
       
   105             if (mgfParams2 instanceof MGF1ParameterSpec) {
       
   106                 boolean result =
       
   107                     ((MGF1ParameterSpec)mgfParams).getDigestAlgorithm().equals
       
   108                          (((MGF1ParameterSpec)mgfParams2).getDigestAlgorithm());
       
   109                 if (!result) {
       
   110                     System.out.println("Different Digest algo in MGF Parameters: " +
       
   111                         ((MGF1ParameterSpec)mgfParams).getDigestAlgorithm() + " vs " +
       
   112                         ((MGF1ParameterSpec)mgfParams2).getDigestAlgorithm());
       
   113                 }
       
   114                 return result;
       
   115             } else {
       
   116                 System.out.println("Different MGF Parameters types: " +
       
   117                     mgfParams.getClass() + " vs " + mgfParams2.getClass());
       
   118                 return false;
       
   119             }
       
   120         }
       
   121         throw new RuntimeException("Unrecognized MGFParameters: " + mgfParams);
       
   122     }
       
   123 }