2015-11-11 07:42:20 +00:00
|
|
|
/*
|
2016-02-02 19:27:18 +00:00
|
|
|
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
2015-11-11 07:42:20 +00:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import java.security.MessageDigest;
|
2016-03-21 18:54:23 +00:00
|
|
|
import java.security.KeyFactory;
|
2015-11-11 07:42:20 +00:00
|
|
|
import java.security.NoSuchAlgorithmException;
|
|
|
|
import java.security.Security;
|
2016-05-19 23:05:33 +00:00
|
|
|
import java.security.Signature;
|
2016-03-21 18:54:23 +00:00
|
|
|
import java.security.Provider;
|
2015-11-11 07:42:20 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import javax.crypto.Cipher;
|
2016-05-19 23:05:33 +00:00
|
|
|
import javax.crypto.Mac;
|
2015-11-11 07:42:20 +00:00
|
|
|
import javax.crypto.NoSuchPaddingException;
|
|
|
|
|
2016-03-21 18:54:23 +00:00
|
|
|
/**
|
|
|
|
* @test
|
2016-05-19 23:05:33 +00:00
|
|
|
* @bug 8076359 8133151 8145344 8150512 8155847
|
2016-03-21 18:54:23 +00:00
|
|
|
* @summary Test the value for new jdk.security.provider.preferred
|
|
|
|
* security property
|
2016-07-02 20:51:20 +00:00
|
|
|
* @run main/othervm PreferredProviderTest
|
2016-03-21 18:54:23 +00:00
|
|
|
*/
|
2015-11-11 07:42:20 +00:00
|
|
|
public class PreferredProviderTest {
|
|
|
|
|
2016-03-21 18:54:23 +00:00
|
|
|
public void RunTest(String type, String os)
|
2015-11-11 07:42:20 +00:00
|
|
|
throws NoSuchAlgorithmException, NoSuchPaddingException {
|
2016-03-21 18:54:23 +00:00
|
|
|
|
2015-11-11 07:42:20 +00:00
|
|
|
String actualProvider = null;
|
2016-03-21 18:54:23 +00:00
|
|
|
boolean solaris = os.contains("sun");
|
|
|
|
String preferredProp
|
|
|
|
= "AES/GCM/NoPadding:SunJCE, MessageDigest.SHA-256:SUN";
|
|
|
|
System.out.printf("%nExecuting test for the platform '%s'%n", os);
|
|
|
|
if (!solaris) {
|
|
|
|
//For other platform it will try to set the preferred algorithm and
|
|
|
|
//Provider and verify the usage of it.
|
|
|
|
Security.setProperty(
|
|
|
|
"jdk.security.provider.preferred", preferredProp);
|
|
|
|
verifyPreferredProviderProperty(os, type, preferredProp);
|
|
|
|
|
|
|
|
verifyDigestProvider(os, type, Arrays.asList(
|
|
|
|
new DataTuple("SHA-256", "SUN")));
|
2015-11-11 07:42:20 +00:00
|
|
|
} else {
|
2016-07-02 20:51:20 +00:00
|
|
|
//Solaris has different providers that support the same algorithm
|
|
|
|
//which makes for better testing.
|
2016-03-21 18:54:23 +00:00
|
|
|
switch (type) {
|
|
|
|
case "sparcv9":
|
2016-05-19 23:05:33 +00:00
|
|
|
preferredProp = "AES:SunJCE, SHA1:SUN, Group.SHA2:SUN, " +
|
|
|
|
"HmacSHA1:SunJCE, Group.HmacSHA2:SunJCE";
|
2016-07-02 20:51:20 +00:00
|
|
|
Security.setProperty(
|
|
|
|
"jdk.security.provider.preferred", preferredProp);
|
2016-03-21 18:54:23 +00:00
|
|
|
verifyPreferredProviderProperty(os, type, preferredProp);
|
|
|
|
|
|
|
|
verifyDigestProvider(os, type, Arrays.asList(
|
|
|
|
new DataTuple("SHA1", "SUN"),
|
|
|
|
new DataTuple("SHA-1", "SUN"),
|
|
|
|
new DataTuple("SHA-224", "SUN"),
|
|
|
|
new DataTuple("SHA-256", "SUN"),
|
|
|
|
new DataTuple("SHA-384", "SUN"),
|
2016-05-19 23:05:33 +00:00
|
|
|
new DataTuple("SHA-512", "SUN"),
|
|
|
|
new DataTuple("SHA-512/224", "SUN"),
|
|
|
|
new DataTuple("SHA-512/256", "SUN")));
|
|
|
|
|
|
|
|
verifyMacProvider(os, type, Arrays.asList(
|
|
|
|
new DataTuple("HmacSHA1", "SunJCE"),
|
|
|
|
new DataTuple("HmacSHA224", "SunJCE"),
|
|
|
|
new DataTuple("HmacSHA256", "SunJCE"),
|
|
|
|
new DataTuple("HmacSHA384", "SunJCE"),
|
|
|
|
new DataTuple("HmacSHA512", "SunJCE")));
|
2016-03-21 18:54:23 +00:00
|
|
|
break;
|
|
|
|
case "amd64":
|
2016-05-19 23:05:33 +00:00
|
|
|
preferredProp = "AES:SunJCE, SHA1:SUN, Group.SHA2:SUN, " +
|
|
|
|
"HmacSHA1:SunJCE, Group.HmacSHA2:SunJCE, " +
|
|
|
|
"RSA:SunRsaSign, SHA1withRSA:SunRsaSign, " +
|
|
|
|
"Group.SHA2RSA:SunRsaSign";
|
2016-07-02 20:51:20 +00:00
|
|
|
Security.setProperty(
|
|
|
|
"jdk.security.provider.preferred", preferredProp);
|
2016-03-21 18:54:23 +00:00
|
|
|
verifyPreferredProviderProperty(os, type, preferredProp);
|
|
|
|
|
|
|
|
verifyKeyFactoryProvider(os, type, Arrays.asList(
|
|
|
|
new DataTuple("RSA", "SunRsaSign")));
|
2016-05-19 23:05:33 +00:00
|
|
|
|
|
|
|
verifyDigestProvider(os, type, Arrays.asList(
|
|
|
|
new DataTuple("SHA1", "SUN"),
|
|
|
|
new DataTuple("SHA-1", "SUN"),
|
|
|
|
new DataTuple("SHA-224", "SUN"),
|
|
|
|
new DataTuple("SHA-256", "SUN"),
|
|
|
|
new DataTuple("SHA-384", "SUN"),
|
|
|
|
new DataTuple("SHA-512", "SUN"),
|
|
|
|
new DataTuple("SHA-512/224", "SUN"),
|
|
|
|
new DataTuple("SHA-512/256", "SUN")));
|
|
|
|
|
|
|
|
verifyMacProvider(os, type, Arrays.asList(
|
|
|
|
new DataTuple("HmacSHA1", "SunJCE"),
|
|
|
|
new DataTuple("HmacSHA224", "SunJCE"),
|
|
|
|
new DataTuple("HmacSHA256", "SunJCE"),
|
|
|
|
new DataTuple("HmacSHA384", "SunJCE"),
|
|
|
|
new DataTuple("HmacSHA512", "SunJCE")));
|
|
|
|
|
|
|
|
verifySignatureProvider(os, type, Arrays.asList(
|
|
|
|
new DataTuple("SHA1withRSA", "SunRsaSign"),
|
|
|
|
new DataTuple("SHA224withRSA", "SunRsaSign"),
|
|
|
|
new DataTuple("SHA256withRSA", "SunRsaSign"),
|
|
|
|
new DataTuple("SHA384withRSA", "SunRsaSign"),
|
|
|
|
new DataTuple("SHA512withRSA", "SunRsaSign")));
|
2016-03-21 18:54:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
verifyDigestProvider(os, type, Arrays.asList(
|
|
|
|
new DataTuple("MD5", "OracleUcrypto")));
|
2015-11-11 07:42:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
|
|
|
actualProvider = cipher.getProvider().getName();
|
|
|
|
if (!actualProvider.equals("SunJCE")) {
|
2016-03-21 18:54:23 +00:00
|
|
|
throw new RuntimeException(String.format("Test Failed:Got wrong "
|
|
|
|
+ "provider from %s-%s platform, Expected Provider: SunJCE,"
|
|
|
|
+ " Returned Provider: %s", os, type, actualProvider));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void verifyPreferredProviderProperty(String os, String arch,
|
|
|
|
String preferred) {
|
|
|
|
String preferredProvider
|
|
|
|
= Security.getProperty("jdk.security.provider.preferred");
|
|
|
|
if (!preferredProvider.equals(preferred)) {
|
2016-05-19 23:05:33 +00:00
|
|
|
System.out.println("Expected: " + preferred + "\nResult: " +
|
|
|
|
preferredProvider);
|
2015-11-11 07:42:20 +00:00
|
|
|
throw new RuntimeException(String.format(
|
2016-03-21 18:54:23 +00:00
|
|
|
"Test Failed: wrong jdk.security.provider.preferred value "
|
|
|
|
+ "on %s-%s", os, arch));
|
|
|
|
}
|
|
|
|
System.out.println(
|
|
|
|
"Preferred provider security property verification complete.");
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void verifyDigestProvider(String os, String arch,
|
|
|
|
List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
|
|
|
|
for (DataTuple dataTuple : algoProviders) {
|
|
|
|
System.out.printf(
|
|
|
|
"Verifying MessageDigest for '%s'%n", dataTuple.algorithm);
|
|
|
|
MessageDigest md = MessageDigest.getInstance(dataTuple.algorithm);
|
|
|
|
matchProvider(md.getProvider(), dataTuple.provider,
|
|
|
|
dataTuple.algorithm, os, arch);
|
|
|
|
}
|
|
|
|
System.out.println(
|
|
|
|
"Preferred MessageDigest algorithm verification successful.");
|
|
|
|
}
|
|
|
|
|
2016-05-19 23:05:33 +00:00
|
|
|
private static void verifyMacProvider(String os, String arch,
|
|
|
|
List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
|
|
|
|
for (DataTuple dataTuple : algoProviders) {
|
|
|
|
System.out.printf(
|
|
|
|
"Verifying Mac for '%s'%n", dataTuple.algorithm);
|
|
|
|
Mac mac = Mac.getInstance(dataTuple.algorithm);
|
|
|
|
matchProvider(mac.getProvider(), dataTuple.provider,
|
|
|
|
dataTuple.algorithm, os, arch);
|
|
|
|
}
|
|
|
|
System.out.println(
|
|
|
|
"Preferred Mac algorithm verification successful.");
|
|
|
|
}
|
|
|
|
|
2016-03-21 18:54:23 +00:00
|
|
|
private static void verifyKeyFactoryProvider(String os, String arch,
|
|
|
|
List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
|
|
|
|
for (DataTuple dataTuple : algoProviders) {
|
|
|
|
System.out.printf(
|
|
|
|
"Verifying KeyFactory for '%s'%n", dataTuple.algorithm);
|
|
|
|
KeyFactory kf = KeyFactory.getInstance(dataTuple.algorithm);
|
|
|
|
matchProvider(kf.getProvider(), dataTuple.provider,
|
|
|
|
dataTuple.algorithm, os, arch);
|
2015-11-11 07:42:20 +00:00
|
|
|
}
|
2016-03-21 18:54:23 +00:00
|
|
|
System.out.println(
|
|
|
|
"Preferred KeyFactory algorithm verification successful.");
|
|
|
|
}
|
2015-11-11 07:42:20 +00:00
|
|
|
|
2016-05-19 23:05:33 +00:00
|
|
|
private static void verifySignatureProvider(String os, String arch,
|
|
|
|
List<DataTuple> algoProviders) throws NoSuchAlgorithmException {
|
|
|
|
for (DataTuple dataTuple : algoProviders) {
|
|
|
|
System.out.printf(
|
|
|
|
"Verifying Signature for '%s'%n", dataTuple.algorithm);
|
|
|
|
Signature si = Signature.getInstance(dataTuple.algorithm);
|
|
|
|
matchProvider(si.getProvider(), dataTuple.provider,
|
|
|
|
dataTuple.algorithm, os, arch);
|
|
|
|
}
|
|
|
|
System.out.println(
|
|
|
|
"Preferred Signature algorithm verification successful.");
|
|
|
|
}
|
|
|
|
|
2016-03-21 18:54:23 +00:00
|
|
|
private static void matchProvider(Provider provider, String expected,
|
|
|
|
String algo, String os, String arch) {
|
|
|
|
if (!provider.getName().equals(expected)) {
|
2015-11-11 07:42:20 +00:00
|
|
|
throw new RuntimeException(String.format(
|
2016-03-21 18:54:23 +00:00
|
|
|
"Test Failed:Got wrong provider from %s-%s platform, "
|
|
|
|
+ "for algorithm %s. Expected Provider: %s,"
|
|
|
|
+ " Returned Provider: %s", os, arch, algo,
|
|
|
|
expected, provider.getName()));
|
2015-11-11 07:42:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static class DataTuple {
|
2016-03-21 18:54:23 +00:00
|
|
|
|
2015-11-11 07:42:20 +00:00
|
|
|
private final String provider;
|
|
|
|
private final String algorithm;
|
|
|
|
|
|
|
|
private DataTuple(String algorithm, String provider) {
|
|
|
|
this.algorithm = algorithm;
|
|
|
|
this.provider = provider;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args)
|
|
|
|
throws NoSuchAlgorithmException, NoSuchPaddingException {
|
2016-03-21 18:54:23 +00:00
|
|
|
String os = System.getProperty("os.name").toLowerCase();
|
|
|
|
String arch = System.getProperty("os.arch").toLowerCase();
|
2015-11-11 07:42:20 +00:00
|
|
|
PreferredProviderTest pp = new PreferredProviderTest();
|
2016-03-21 18:54:23 +00:00
|
|
|
pp.RunTest(arch, os);
|
2015-11-11 07:42:20 +00:00
|
|
|
}
|
|
|
|
}
|