Path: blob/master/test/jdk/sun/security/jca/PreferredProviderTest.java
41152 views
/*1* Copyright (c) 2015, 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*/2223import java.security.MessageDigest;24import java.security.KeyFactory;25import java.security.NoSuchAlgorithmException;26import java.security.Security;27import java.security.Signature;28import java.security.Provider;29import java.util.Arrays;30import java.util.List;31import javax.crypto.Cipher;32import javax.crypto.Mac;33import javax.crypto.NoSuchPaddingException;3435/**36* @test37* @bug 8076359 8133151 8145344 8150512 815584738* @summary Test the value for new jdk.security.provider.preferred39* security property40* @run main/othervm PreferredProviderTest41*/42public class PreferredProviderTest {4344public void RunTest(String type, String os)45throws NoSuchAlgorithmException, NoSuchPaddingException {4647String actualProvider = null;48String preferredProp49= "AES/GCM/NoPadding:SunJCE, MessageDigest.SHA-256:SUN";50System.out.printf("%nExecuting test for the platform '%s'%n", os);5152// Try to set the preferred algorithm and Provider and verify53// the usage of it.54Security.setProperty(55"jdk.security.provider.preferred", preferredProp);56verifyPreferredProviderProperty(os, type, preferredProp);5758verifyDigestProvider(os, type, Arrays.asList(59new DataTuple("SHA-256", "SUN")));6061Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");62actualProvider = cipher.getProvider().getName();63if (!actualProvider.equals("SunJCE")) {64throw new RuntimeException(String.format("Test Failed:Got wrong "65+ "provider from %s-%s platform, Expected Provider: SunJCE,"66+ " Returned Provider: %s", os, type, actualProvider));67}68}6970private static void verifyPreferredProviderProperty(String os, String arch,71String preferred) {72String preferredProvider73= Security.getProperty("jdk.security.provider.preferred");74if (!preferredProvider.equals(preferred)) {75System.out.println("Expected: " + preferred + "\nResult: " +76preferredProvider);77throw new RuntimeException(String.format(78"Test Failed: wrong jdk.security.provider.preferred value "79+ "on %s-%s", os, arch));80}81System.out.println(82"Preferred provider security property verification complete.");83}8485private static void verifyDigestProvider(String os, String arch,86List<DataTuple> algoProviders) throws NoSuchAlgorithmException {87for (DataTuple dataTuple : algoProviders) {88System.out.printf(89"Verifying MessageDigest for '%s'%n", dataTuple.algorithm);90MessageDigest md = MessageDigest.getInstance(dataTuple.algorithm);91matchProvider(md.getProvider(), dataTuple.provider,92dataTuple.algorithm, os, arch);93}94System.out.println(95"Preferred MessageDigest algorithm verification successful.");96}9798private static void verifyMacProvider(String os, String arch,99List<DataTuple> algoProviders) throws NoSuchAlgorithmException {100for (DataTuple dataTuple : algoProviders) {101System.out.printf(102"Verifying Mac for '%s'%n", dataTuple.algorithm);103Mac mac = Mac.getInstance(dataTuple.algorithm);104matchProvider(mac.getProvider(), dataTuple.provider,105dataTuple.algorithm, os, arch);106}107System.out.println(108"Preferred Mac algorithm verification successful.");109}110111private static void verifyKeyFactoryProvider(String os, String arch,112List<DataTuple> algoProviders) throws NoSuchAlgorithmException {113for (DataTuple dataTuple : algoProviders) {114System.out.printf(115"Verifying KeyFactory for '%s'%n", dataTuple.algorithm);116KeyFactory kf = KeyFactory.getInstance(dataTuple.algorithm);117matchProvider(kf.getProvider(), dataTuple.provider,118dataTuple.algorithm, os, arch);119}120System.out.println(121"Preferred KeyFactory algorithm verification successful.");122}123124private static void verifySignatureProvider(String os, String arch,125List<DataTuple> algoProviders) throws NoSuchAlgorithmException {126for (DataTuple dataTuple : algoProviders) {127System.out.printf(128"Verifying Signature for '%s'%n", dataTuple.algorithm);129Signature si = Signature.getInstance(dataTuple.algorithm);130matchProvider(si.getProvider(), dataTuple.provider,131dataTuple.algorithm, os, arch);132}133System.out.println(134"Preferred Signature algorithm verification successful.");135}136137private static void matchProvider(Provider provider, String expected,138String algo, String os, String arch) {139if (!provider.getName().equals(expected)) {140throw new RuntimeException(String.format(141"Test Failed:Got wrong provider from %s-%s platform, "142+ "for algorithm %s. Expected Provider: %s,"143+ " Returned Provider: %s", os, arch, algo,144expected, provider.getName()));145}146}147148private static class DataTuple {149150private final String provider;151private final String algorithm;152153private DataTuple(String algorithm, String provider) {154this.algorithm = algorithm;155this.provider = provider;156}157}158159public static void main(String[] args)160throws NoSuchAlgorithmException, NoSuchPaddingException {161String os = System.getProperty("os.name").toLowerCase();162String arch = System.getProperty("os.arch").toLowerCase();163PreferredProviderTest pp = new PreferredProviderTest();164pp.RunTest(arch, os);165}166}167168169