Path: blob/master/test/jdk/sun/security/pkcs12/ParamsPreferences.java
41149 views
/*1* Copyright (c) 2018, 2021, 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 jdk.test.lib.SecurityTools;24import sun.security.util.KnownOIDs;2526import java.nio.file.Files;27import java.nio.file.Path;28import java.util.ArrayList;29import java.util.List;30import java.util.Map;3132import static jdk.test.lib.security.DerUtils.*;33import static sun.security.util.KnownOIDs.*;3435/*36* @test37* @bug 8076190 8242151 8153005 826629338* @library /test/lib39* @modules java.base/sun.security.pkcs40* java.base/sun.security.util41* @summary Checks the preferences order of pkcs12 params, whether it's42* a system property or a security property, whether the name has43* "pkcs12" or "PKCS12", whether the legacy property is set.44*/45public class ParamsPreferences {4647public static final void main(String[] args) throws Exception {48int c = 0;4950// default51test(c++,52Map.of(),53Map.of(),54PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,55PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,56SHA_256, 10000);5758// legacy settings59test(c++,60Map.of("keystore.pkcs12.legacy", ""),61Map.of(),62PBEWithSHA1AndRC2_40, 50000,63PBEWithSHA1AndDESede, 50000,64SHA_1, 100000);6566// legacy override everything else67test(c++,68Map.of("keystore.pkcs12.legacy", "",69"keystore.pkcs12.certProtectionAlgorithm", "PBEWithHmacSHA256AndAES_128",70"keystore.pkcs12.certPbeIterationCount", 3000,71"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithHmacSHA256AndAES_128",72"keystore.pkcs12.keyPbeIterationCount", 4000,73"keystore.pkcs12.macAlgorithm", "HmacPBESHA384",74"keystore.pkcs12.macIterationCount", 2000),75Map.of(),76PBEWithSHA1AndRC2_40, 50000,77PBEWithSHA1AndDESede, 50000,78SHA_1, 100000);7980// password-less with system property81test(c++,82Map.of("keystore.pkcs12.certProtectionAlgorithm", "NONE",83"keystore.pkcs12.macAlgorithm", "NONE"),84Map.of(),85null,86PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,87null);8889// password-less with security property90test(c++,91Map.of(),92Map.of("keystore.pkcs12.certProtectionAlgorithm", "NONE",93"keystore.pkcs12.macAlgorithm", "NONE"),94null,95PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,96null);9798// back to with storepass by overriding security property with system property99test(c++,100Map.of("keystore.pkcs12.certProtectionAlgorithm", "PBEWithSHA1AndDESede",101"keystore.pkcs12.macAlgorithm", "HmacPBESHA256"),102Map.of("keystore.pkcs12.certProtectionAlgorithm", "NONE",103"keystore.pkcs12.macAlgorithm", "NONE"),104PBEWithSHA1AndDESede, 10000,105PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,106SHA_256, 10000);107108// back to with storepass by using "" to force hardcoded default109test(c++,110Map.of("keystore.pkcs12.certProtectionAlgorithm", "",111"keystore.pkcs12.keyProtectionAlgorithm", "",112"keystore.pkcs12.macAlgorithm", ""),113Map.of("keystore.pkcs12.certProtectionAlgorithm", "NONE",114"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40",115"keystore.pkcs12.macAlgorithm", "NONE"),116PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,117PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,118SHA_256, 10000);119120// change everything with system property121test(c++,122Map.of("keystore.pkcs12.certProtectionAlgorithm", "PBEWithSHA1AndDESede",123"keystore.pkcs12.certPbeIterationCount", 3000,124"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40",125"keystore.pkcs12.keyPbeIterationCount", 4000,126"keystore.pkcs12.macAlgorithm", "HmacPBESHA256",127"keystore.pkcs12.macIterationCount", 2000),128Map.of(),129PBEWithSHA1AndDESede, 3000,130PBEWithSHA1AndRC2_40, 4000,131SHA_256, 2000);132133// change everything with security property134test(c++,135Map.of(),136Map.of("keystore.pkcs12.certProtectionAlgorithm", "PBEWithSHA1AndDESede",137"keystore.pkcs12.certPbeIterationCount", 3000,138"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40",139"keystore.pkcs12.keyPbeIterationCount", 4000,140"keystore.pkcs12.macAlgorithm", "HmacPBESHA256",141"keystore.pkcs12.macIterationCount", 2000),142PBEWithSHA1AndDESede, 3000,143PBEWithSHA1AndRC2_40, 4000,144SHA_256, 2000);145146// override security property with system property147test(c++,148Map.of("keystore.pkcs12.certProtectionAlgorithm", "PBEWithSHA1AndDESede",149"keystore.pkcs12.certPbeIterationCount", 13000,150"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40",151"keystore.pkcs12.keyPbeIterationCount", 14000,152"keystore.pkcs12.macAlgorithm", "HmacPBESHA256",153"keystore.pkcs12.macIterationCount", 12000),154Map.of("keystore.pkcs12.certProtectionAlgorithm", "PBEWithSHA1AndRC2_40",155"keystore.pkcs12.certPbeIterationCount", 3000,156"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndDESede",157"keystore.pkcs12.keyPbeIterationCount", 4000,158"keystore.pkcs12.macAlgorithm", "HmacPBESHA1",159"keystore.pkcs12.macIterationCount", 2000),160PBEWithSHA1AndDESede, 13000,161PBEWithSHA1AndRC2_40, 14000,162SHA_256, 12000);163164// check keyProtectionAlgorithm old behavior. Preferences of165// 4 different settings.166167test(c++,168Map.of(),169Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_128"),170PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,171PBEWithSHA1AndRC2_128, 10000,172SHA_256, 10000);173test(c++,174Map.of(),175Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_128",176"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40"),177PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,178PBEWithSHA1AndRC2_40, 10000,179SHA_256, 10000);180test(c++,181Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC4_128"),182Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_128",183"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40"),184PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,185PBEWithSHA1AndRC4_128, 10000,186SHA_256, 10000);187test(c++,188Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC4_128",189"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC4_40"),190Map.of("keystore.PKCS12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_128",191"keystore.pkcs12.keyProtectionAlgorithm", "PBEWithSHA1AndRC2_40"),192PBES2, HmacSHA256, AES_256$CBC$NoPadding, 10000,193PBEWithSHA1AndRC4_40, 10000,194SHA_256, 10000);195196// 8266293197test(c++,198Map.of("keystore.pkcs12.keyProtectionAlgorithm", "PBEWithMD5AndDES",199"keystore.pkcs12.certProtectionAlgorithm", "PBEWithMD5AndDES"),200Map.of(),201PBEWithMD5AndDES, 10000,202PBEWithMD5AndDES, 10000,203SHA_256, 10000);204}205206/**207* Run once.208*209* @param sysProps system properties210* @param secProps security properties211* @param args an array expected certPbeAlg (sub algs), certPbeIC,212* keyPbeAlg (sub algs), keyPbeIc, macAlg, macIC.213*/214static void test(int n, Map<String, ?> sysProps,215Map<String, ?> secProps,216Object... args) throws Exception {217218String cmd = "-keystore ks" + n + " -genkeypair -keyalg EC "219+ "-alias a -dname CN=A -storepass changeit "220+ "-J-Djava.security.properties=" + n + ".conf";221222for (var p : sysProps.entrySet()) {223cmd += " -J-D" + p.getKey() + "=" + p.getValue();224}225226List<String> jsConf = new ArrayList<>();227for (var p : secProps.entrySet()) {228jsConf.add(p.getKey() + "=" + p.getValue());229}230Files.write(Path.of(n + ".conf"), jsConf);231System.out.println("--------- test starts ----------");232System.out.println(jsConf);233SecurityTools.keytool(cmd).shouldHaveExitValue(0);234235int i = 0;236byte[] data = Files.readAllBytes(Path.of("ks" + n));237238// cert pbe alg + ic239KnownOIDs certAlg = (KnownOIDs)args[i++];240if (certAlg == null) {241checkAlg(data, "110c10", Data);242} else {243checkAlg(data, "110c10", EncryptedData);244checkAlg(data, "110c110110", certAlg);245if (certAlg == PBES2) {246checkAlg(data, "110c11011100", PBKDF2WithHmacSHA1);247checkAlg(data, "110c1101110130", (KnownOIDs)args[i++]);248checkAlg(data, "110c11011110", (KnownOIDs)args[i++]);249checkInt(data, "110c110111011", (int) args[i++]);250} else {251checkInt(data, "110c1101111", (int) args[i++]);252}253}254255// key pbe alg + ic256KnownOIDs keyAlg = (KnownOIDs)args[i++];257checkAlg(data, "110c010c01000", keyAlg);258if (keyAlg == PBES2) {259checkAlg(data, "110c010c0100100", PBKDF2WithHmacSHA1);260checkAlg(data, "110c010c010010130", (KnownOIDs)args[i++]);261checkAlg(data, "110c010c0100110", (KnownOIDs)args[i++]);262checkInt(data, "110c010c01001011", (int) args[i++]);263} else {264checkInt(data, "110c010c010011", (int) args[i++]);265}266267// mac alg + ic268KnownOIDs macAlg = (KnownOIDs)args[i++];269if (macAlg == null) {270shouldNotExist(data, "2");271} else {272checkAlg(data, "2000", macAlg);273checkInt(data, "22", (int) args[i++]);274}275}276}277278279