jdk/test/sun/security/jca/PreferredProviderTest.java
changeset 38435 292ad46c1bf1
parent 36647 26bac33028c6
child 39339 3a9850ed77e8
equal deleted inserted replaced
38434:a38df2210d1f 38435:292ad46c1bf1
    23 
    23 
    24 import java.security.MessageDigest;
    24 import java.security.MessageDigest;
    25 import java.security.KeyFactory;
    25 import java.security.KeyFactory;
    26 import java.security.NoSuchAlgorithmException;
    26 import java.security.NoSuchAlgorithmException;
    27 import java.security.Security;
    27 import java.security.Security;
       
    28 import java.security.Signature;
    28 import java.security.Provider;
    29 import java.security.Provider;
    29 import java.util.Arrays;
    30 import java.util.Arrays;
    30 import java.util.List;
    31 import java.util.List;
    31 import javax.crypto.Cipher;
    32 import javax.crypto.Cipher;
       
    33 import javax.crypto.Mac;
    32 import javax.crypto.NoSuchPaddingException;
    34 import javax.crypto.NoSuchPaddingException;
    33 
    35 
    34 /**
    36 /**
    35  * @test
    37  * @test
    36  * @bug 8076359 8133151 8145344 8150512
    38  * @bug 8076359 8133151 8145344 8150512 8155847
    37  * @summary Test the value for new jdk.security.provider.preferred
    39  * @summary Test the value for new jdk.security.provider.preferred
    38  *          security property
    40  *          security property
    39  */
    41  */
    40 public class PreferredProviderTest {
    42 public class PreferredProviderTest {
    41 
    43 
    59         } else {
    61         } else {
    60             //For solaris the preferred algorithm/provider is already set in
    62             //For solaris the preferred algorithm/provider is already set in
    61             //java.security file which will be verified.
    63             //java.security file which will be verified.
    62             switch (type) {
    64             switch (type) {
    63                 case "sparcv9":
    65                 case "sparcv9":
    64                     preferredProp = "AES:SunJCE, SHA1:SUN, SHA-224:SUN,"
    66                     preferredProp = "AES:SunJCE, SHA1:SUN, Group.SHA2:SUN, " +
    65                             + " SHA-256:SUN, SHA-384:SUN, SHA-512:SUN";
    67                             "HmacSHA1:SunJCE, Group.HmacSHA2:SunJCE";
    66                     verifyPreferredProviderProperty(os, type, preferredProp);
    68                     verifyPreferredProviderProperty(os, type, preferredProp);
    67 
    69 
    68                     verifyDigestProvider(os, type, Arrays.asList(
    70                     verifyDigestProvider(os, type, Arrays.asList(
    69                             new DataTuple("SHA1", "SUN"),
    71                             new DataTuple("SHA1", "SUN"),
    70                             new DataTuple("SHA-1", "SUN"),
    72                             new DataTuple("SHA-1", "SUN"),
    71                             new DataTuple("SHA-224", "SUN"),
    73                             new DataTuple("SHA-224", "SUN"),
    72                             new DataTuple("SHA-256", "SUN"),
    74                             new DataTuple("SHA-256", "SUN"),
    73                             new DataTuple("SHA-384", "SUN"),
    75                             new DataTuple("SHA-384", "SUN"),
    74                             new DataTuple("SHA-512", "SUN")));
    76                             new DataTuple("SHA-512", "SUN"),
       
    77                             new DataTuple("SHA-512/224", "SUN"),
       
    78                             new DataTuple("SHA-512/256", "SUN")));
       
    79 
       
    80                     verifyMacProvider(os, type, Arrays.asList(
       
    81                             new DataTuple("HmacSHA1", "SunJCE"),
       
    82                             new DataTuple("HmacSHA224", "SunJCE"),
       
    83                             new DataTuple("HmacSHA256", "SunJCE"),
       
    84                             new DataTuple("HmacSHA384", "SunJCE"),
       
    85                             new DataTuple("HmacSHA512", "SunJCE")));
    75                     break;
    86                     break;
    76                 case "amd64":
    87                 case "amd64":
    77                     preferredProp = "AES:SunJCE, RSA:SunRsaSign";
    88                     preferredProp = "AES:SunJCE, SHA1:SUN, Group.SHA2:SUN, " +
       
    89                             "HmacSHA1:SunJCE, Group.HmacSHA2:SunJCE, " +
       
    90                             "RSA:SunRsaSign, SHA1withRSA:SunRsaSign, " +
       
    91                             "Group.SHA2RSA:SunRsaSign";
       
    92 
    78                     verifyPreferredProviderProperty(os, type, preferredProp);
    93                     verifyPreferredProviderProperty(os, type, preferredProp);
    79 
    94 
    80                     verifyKeyFactoryProvider(os, type, Arrays.asList(
    95                     verifyKeyFactoryProvider(os, type, Arrays.asList(
    81                             new DataTuple("RSA", "SunRsaSign")));
    96                             new DataTuple("RSA", "SunRsaSign")));
       
    97 
       
    98                     verifyDigestProvider(os, type, Arrays.asList(
       
    99                             new DataTuple("SHA1", "SUN"),
       
   100                             new DataTuple("SHA-1", "SUN"),
       
   101                             new DataTuple("SHA-224", "SUN"),
       
   102                             new DataTuple("SHA-256", "SUN"),
       
   103                             new DataTuple("SHA-384", "SUN"),
       
   104                             new DataTuple("SHA-512", "SUN"),
       
   105                             new DataTuple("SHA-512/224", "SUN"),
       
   106                             new DataTuple("SHA-512/256", "SUN")));
       
   107 
       
   108                     verifyMacProvider(os, type, Arrays.asList(
       
   109                             new DataTuple("HmacSHA1", "SunJCE"),
       
   110                             new DataTuple("HmacSHA224", "SunJCE"),
       
   111                             new DataTuple("HmacSHA256", "SunJCE"),
       
   112                             new DataTuple("HmacSHA384", "SunJCE"),
       
   113                             new DataTuple("HmacSHA512", "SunJCE")));
       
   114 
       
   115                     verifySignatureProvider(os, type, Arrays.asList(
       
   116                             new DataTuple("SHA1withRSA", "SunRsaSign"),
       
   117                             new DataTuple("SHA224withRSA", "SunRsaSign"),
       
   118                             new DataTuple("SHA256withRSA", "SunRsaSign"),
       
   119                             new DataTuple("SHA384withRSA", "SunRsaSign"),
       
   120                             new DataTuple("SHA512withRSA", "SunRsaSign")));
    82                     break;
   121                     break;
    83             }
   122             }
    84             verifyDigestProvider(os, type, Arrays.asList(
   123             verifyDigestProvider(os, type, Arrays.asList(
    85                     new DataTuple("MD5", "OracleUcrypto")));
   124                     new DataTuple("MD5", "OracleUcrypto")));
    86         }
   125         }
    97     private static void verifyPreferredProviderProperty(String os, String arch,
   136     private static void verifyPreferredProviderProperty(String os, String arch,
    98             String preferred) {
   137             String preferred) {
    99         String preferredProvider
   138         String preferredProvider
   100                 = Security.getProperty("jdk.security.provider.preferred");
   139                 = Security.getProperty("jdk.security.provider.preferred");
   101         if (!preferredProvider.equals(preferred)) {
   140         if (!preferredProvider.equals(preferred)) {
       
   141             System.out.println("Expected: " + preferred + "\nResult: " +
       
   142                     preferredProvider);
   102             throw new RuntimeException(String.format(
   143             throw new RuntimeException(String.format(
   103                     "Test Failed: wrong jdk.security.provider.preferred value "
   144                     "Test Failed: wrong jdk.security.provider.preferred value "
   104                     + "on %s-%s", os, arch));
   145                     + "on %s-%s", os, arch));
   105         }
   146         }
   106         System.out.println(
   147         System.out.println(
   118         }
   159         }
   119         System.out.println(
   160         System.out.println(
   120                 "Preferred MessageDigest algorithm verification successful.");
   161                 "Preferred MessageDigest algorithm verification successful.");
   121     }
   162     }
   122 
   163 
       
   164     private static void verifyMacProvider(String os, String arch,
       
   165             List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
       
   166         for (DataTuple dataTuple : algoProviders) {
       
   167             System.out.printf(
       
   168                     "Verifying Mac for '%s'%n", dataTuple.algorithm);
       
   169             Mac mac = Mac.getInstance(dataTuple.algorithm);
       
   170             matchProvider(mac.getProvider(), dataTuple.provider,
       
   171                     dataTuple.algorithm, os, arch);
       
   172         }
       
   173         System.out.println(
       
   174                 "Preferred Mac algorithm verification successful.");
       
   175     }
       
   176 
   123     private static void verifyKeyFactoryProvider(String os, String arch,
   177     private static void verifyKeyFactoryProvider(String os, String arch,
   124             List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
   178             List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
   125         for (DataTuple dataTuple : algoProviders) {
   179         for (DataTuple dataTuple : algoProviders) {
   126             System.out.printf(
   180             System.out.printf(
   127                     "Verifying KeyFactory for '%s'%n", dataTuple.algorithm);
   181                     "Verifying KeyFactory for '%s'%n", dataTuple.algorithm);
   129             matchProvider(kf.getProvider(), dataTuple.provider,
   183             matchProvider(kf.getProvider(), dataTuple.provider,
   130                     dataTuple.algorithm, os, arch);
   184                     dataTuple.algorithm, os, arch);
   131         }
   185         }
   132         System.out.println(
   186         System.out.println(
   133                 "Preferred KeyFactory algorithm verification successful.");
   187                 "Preferred KeyFactory algorithm verification successful.");
       
   188     }
       
   189 
       
   190     private static void verifySignatureProvider(String os, String arch,
       
   191             List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
       
   192         for (DataTuple dataTuple : algoProviders) {
       
   193             System.out.printf(
       
   194                     "Verifying Signature for '%s'%n", dataTuple.algorithm);
       
   195             Signature si = Signature.getInstance(dataTuple.algorithm);
       
   196             matchProvider(si.getProvider(), dataTuple.provider,
       
   197                     dataTuple.algorithm, os, arch);
       
   198         }
       
   199         System.out.println(
       
   200                 "Preferred Signature algorithm verification successful.");
   134     }
   201     }
   135 
   202 
   136     private static void matchProvider(Provider provider, String expected,
   203     private static void matchProvider(Provider provider, String expected,
   137             String algo, String os, String arch) {
   204             String algo, String os, String arch) {
   138         if (!provider.getName().equals(expected)) {
   205         if (!provider.getName().equals(expected)) {