Path: blob/master/test/jdk/sun/security/pkcs12/KeytoolOpensslInteropTest.java
66644 views
/*1* Copyright (c) 2018, 2022, 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*/2223/*24* @test25* @bug 8076190 8242151 8153005 826618226* @summary This is java keytool <-> openssl interop test. This test generates27* some openssl keystores on the fly, java operates on it and28* vice versa.29*30* Note: This test executes some openssl command, so need to set31* openssl path using system property "test.openssl.path" or it should32* be available in /usr/bin or /usr/local/bin33* Required OpenSSL version : OpenSSL 1.1.*34*35* @modules java.base/sun.security.pkcs36* java.base/sun.security.util37* @library /test/lib38* @library /sun/security/pkcs11/39* @run main/othervm/timeout=600 KeytoolOpensslInteropTest40*/4142import jdk.test.lib.Asserts;43import jdk.test.lib.SecurityTools;44import jdk.test.lib.process.ProcessTools;45import jdk.test.lib.process.OutputAnalyzer;46import jdk.test.lib.security.OpensslArtifactFetcher;4748import java.io.File;49import java.io.FileInputStream;50import java.io.FileOutputStream;51import java.io.IOException;52import java.io.InputStream;53import java.io.OutputStream;54import java.io.UncheckedIOException;55import java.nio.file.DirectoryStream;56import java.nio.file.Files;57import java.nio.file.Path;58import java.security.KeyStore;59import java.util.Base64;60import java.util.Objects;6162import static jdk.test.lib.security.DerUtils.*;63import static sun.security.util.KnownOIDs.*;64import static sun.security.pkcs.ContentInfo.*;6566public class KeytoolOpensslInteropTest {6768public static void main(String[] args) throws Throwable {69String opensslPath = OpensslArtifactFetcher.getOpenssl1dot1dotStar();70if (opensslPath != null) {71// if preferred version of openssl is available perform all72// keytool <-> openssl interop tests73generateInitialKeystores(opensslPath);74testWithJavaCommands();75testWithOpensslCommands(opensslPath);76} else {77// since preferred version of openssl is not available skip all78// openssl command dependent tests with a warning79System.out.println("\n\u001B[31mWarning: Can't find openssl "80+ "(version 1.1.*) binary on this machine, please install"81+ " and set openssl path with property "82+ "'test.openssl.path'. Now running only half portion of "83+ "the test, skipping all tests which depends on openssl "84+ "commands.\u001B[0m\n");85// De-BASE64 textual files in ./params to `pwd`86try (DirectoryStream<Path> stream = Files.newDirectoryStream(87Path.of(System.getProperty("test.src"), "params"),88p -> !p.getFileName().toString().equals("README"))) {89stream.forEach(p -> {90try (InputStream is = Files.newInputStream(p);91OutputStream os = Files.newOutputStream(92p.getFileName())) {93Base64.getMimeDecoder().wrap(is).transferTo(os);94} catch (IOException e) {95throw new UncheckedIOException(e);96}97});98}99testWithJavaCommands();100}101}102103private static void generateInitialKeystores(String opensslPath)104throws Throwable {105keytool("-keystore ks -keyalg ec -genkeypair -storepass"106+ " changeit -alias a -dname CN=A").shouldHaveExitValue(0);107108ProcessTools.executeCommand(opensslPath, "pkcs12", "-in", "ks",109"-nodes", "-out", "kandc", "-passin", "pass:changeit")110.shouldHaveExitValue(0);111112ProcessTools.executeCommand(opensslPath, "pkcs12", "-export", "-in",113"kandc", "-out", "os2", "-name", "a", "-passout",114"pass:changeit", "-certpbe", "NONE", "-nomac")115.shouldHaveExitValue(0);116117ProcessTools.executeCommand(opensslPath, "pkcs12", "-export", "-in",118"kandc", "-out", "os3", "-name", "a", "-passout",119"pass:changeit", "-certpbe", "NONE")120.shouldHaveExitValue(0);121122ProcessTools.executeCommand(opensslPath, "pkcs12", "-export", "-in",123"kandc", "-out", "os4", "-name", "a", "-passout",124"pass:changeit", "-certpbe", "PBE-SHA1-RC4-128", "-keypbe",125"PBE-SHA1-RC4-128", "-macalg", "SHA224")126.shouldHaveExitValue(0);127128ProcessTools.executeCommand(opensslPath, "pkcs12", "-export", "-in",129"kandc", "-out", "os5", "-name", "a", "-passout",130"pass:changeit", "-certpbe", "AES-256-CBC", "-keypbe",131"AES-256-CBC", "-macalg", "SHA512")132.shouldHaveExitValue(0);133}134135private static void testWithJavaCommands() throws Throwable {136byte[] data;137138// openssl -> keytool interop check139// os2. no cert pbe, no mac.140check("os2", "a", null, "changeit", true, true, true);141check("os2", "a", "changeit", "changeit", true, true, true);142// You can even load it with a wrong storepass, controversial143check("os2", "a", "wrongpass", "changeit", true, true, true);144145// os3. no cert pbe, has mac. just like JKS146check("os3", "a", null, "changeit", true, true, true);147check("os3", "a", "changeit", "changeit", true, true, true);148// Cannot load with a wrong storepass, same as JKS149check("os3", "a", "wrongpass", "-", IOException.class, "-", "-");150151// os4. non default algs152check("os4", "a", "changeit", "changeit", true, true, true);153check("os4", "a", "wrongpass", "-", IOException.class, "-", "-");154// no storepass no cert155check("os4", "a", null, "changeit", true, false, true);156157// os5. strong non default algs158check("os5", "a", "changeit", "changeit", true, true, true);159check("os5", "a", "wrongpass", "-", IOException.class, "-", "-");160// no storepass no cert161check("os5", "a", null, "changeit", true, false, true);162163// keytool164165// Current default pkcs12 setting166keytool("-importkeystore -srckeystore ks -srcstorepass changeit "167+ "-destkeystore ksnormal -deststorepass changeit");168169data = Files.readAllBytes(Path.of("ksnormal"));170checkInt(data, "22", 10000); // Mac ic171checkAlg(data, "2000", SHA_256); // Mac alg172checkAlg(data, "110c010c01000", PBES2); // key alg173checkInt(data, "110c010c01001011", 10000); // key ic174checkAlg(data, "110c10", ENCRYPTED_DATA_OID);175checkAlg(data, "110c110110", PBES2); // cert alg176check("ksnormal", "a", "changeit", "changeit", true, true, true);177check("ksnormal", "a", null, "changeit", true, false, true);178check("ksnormal", "a", "wrongpass", "-", IOException.class, "-", "-");179180// Import it into a new keystore with legacy algorithms181keytool("-importkeystore -srckeystore ksnormal -srcstorepass changeit "182+ "-destkeystore kslegacyimp -deststorepass changeit "183+ "-J-Dkeystore.pkcs12.legacy");184data = Files.readAllBytes(Path.of("kslegacyimp"));185checkInt(data, "22", 100000); // Mac ic186checkAlg(data, "2000", SHA_1); // Mac alg187checkAlg(data, "110c010c01000", PBEWithSHA1AndDESede); // key alg188checkInt(data, "110c010c010011", 50000); // key ic189checkAlg(data, "110c110110", PBEWithSHA1AndRC2_40); // cert alg190checkInt(data, "110c1101111", 50000); // cert ic191192// Add a new entry with password-less settings, still has a storepass193keytool("-keystore ksnormal -genkeypair -keyalg DSA "194+ "-storepass changeit -alias b -dname CN=b "195+ "-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE "196+ "-J-Dkeystore.pkcs12.macAlgorithm=NONE");197data = Files.readAllBytes(Path.of("ksnormal"));198checkInt(data, "22", 10000); // Mac ic199checkAlg(data, "2000", SHA_256); // Mac alg200checkAlg(data, "110c010c01000", PBES2); // key alg201checkInt(data, "110c010c01001011", 10000); // key ic202checkAlg(data, "110c010c11000", PBES2); // new key alg203checkInt(data, "110c010c11001011", 10000); // new key ic204checkAlg(data, "110c10", ENCRYPTED_DATA_OID);205checkAlg(data, "110c110110", PBES2); // cert alg206check("ksnormal", "b", null, "changeit", true, false, true);207check("ksnormal", "b", "changeit", "changeit", true, true, true);208209// Different keypbe alg, no cert pbe and no mac210keytool("-importkeystore -srckeystore ks -srcstorepass changeit "211+ "-destkeystore ksnopass -deststorepass changeit "212+ "-J-Dkeystore.pkcs12.keyProtectionAlgorithm=PBEWithSHA1AndRC4_128 "213+ "-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE "214+ "-J-Dkeystore.pkcs12.macAlgorithm=NONE");215data = Files.readAllBytes(Path.of("ksnopass"));216shouldNotExist(data, "2"); // no Mac217checkAlg(data, "110c010c01000", PBEWithSHA1AndRC4_128);218checkInt(data, "110c010c010011", 10000);219checkAlg(data, "110c10", DATA_OID);220check("ksnopass", "a", null, "changeit", true, true, true);221check("ksnopass", "a", "changeit", "changeit", true, true, true);222check("ksnopass", "a", "wrongpass", "changeit", true, true, true);223224// Add a new entry with normal settings, still password-less225keytool("-keystore ksnopass -genkeypair -keyalg DSA "226+ "-storepass changeit -alias b -dname CN=B");227data = Files.readAllBytes(Path.of("ksnopass"));228shouldNotExist(data, "2"); // no Mac229checkAlg(data, "110c010c01000", PBEWithSHA1AndRC4_128);230checkInt(data, "110c010c010011", 10000);231checkAlg(data, "110c010c11000", PBES2);232checkInt(data, "110c010c11001011", 10000);233checkAlg(data, "110c10", DATA_OID);234check("ksnopass", "a", null, "changeit", true, true, true);235check("ksnopass", "b", null, "changeit", true, true, true);236237keytool("-importkeystore -srckeystore ks -srcstorepass changeit "238+ "-destkeystore ksnewic -deststorepass changeit "239+ "-J-Dkeystore.pkcs12.macIterationCount=5555 "240+ "-J-Dkeystore.pkcs12.certPbeIterationCount=6666 "241+ "-J-Dkeystore.pkcs12.keyPbeIterationCount=7777");242data = Files.readAllBytes(Path.of("ksnewic"));243checkInt(data, "22", 5555); // Mac ic244checkAlg(data, "2000", SHA_256); // Mac alg245checkAlg(data, "110c010c01000", PBES2); // key alg246checkInt(data, "110c010c01001011", 7777); // key ic247checkAlg(data, "110c110110", PBES2); // cert alg248checkInt(data, "110c110111011", 6666); // cert ic249250// keypbe alg cannot be NONE251keytool("-keystore ksnewic -genkeypair -keyalg DSA "252+ "-storepass changeit -alias b -dname CN=B "253+ "-J-Dkeystore.pkcs12.keyProtectionAlgorithm=NONE")254.shouldContain("NONE AlgorithmParameters not available")255.shouldHaveExitValue(1);256257// new entry new keypbe alg (and default ic), else unchanged258keytool("-keystore ksnewic -genkeypair -keyalg DSA "259+ "-storepass changeit -alias b -dname CN=B "260+ "-J-Dkeystore.pkcs12.keyProtectionAlgorithm=PBEWithSHA1AndRC4_128");261data = Files.readAllBytes(Path.of("ksnewic"));262checkInt(data, "22", 5555); // Mac ic263checkAlg(data, "2000", SHA_256); // Mac alg264checkAlg(data, "110c010c01000", PBES2); // key alg265checkInt(data, "110c010c01001011", 7777); // key ic266checkAlg(data, "110c010c11000", PBEWithSHA1AndRC4_128); // new key alg267checkInt(data, "110c010c110011", 10000); // new key ic268checkAlg(data, "110c110110", PBES2); // cert alg269checkInt(data, "110c110111011", 6666); // cert ic270271// Check KeyStore loading multiple keystores272KeyStore ks = KeyStore.getInstance("pkcs12");273try (FileInputStream fis = new FileInputStream("ksnormal");274FileOutputStream fos = new FileOutputStream("ksnormaldup")) {275ks.load(fis, "changeit".toCharArray());276ks.store(fos, "changeit".toCharArray());277}278data = Files.readAllBytes(Path.of("ksnormaldup"));279checkInt(data, "22", 10000); // Mac ic280checkAlg(data, "2000", SHA_256); // Mac alg281checkAlg(data, "110c010c01000", PBES2); // key alg282checkInt(data, "110c010c01001011", 10000); // key ic283checkAlg(data, "110c010c11000", PBES2); // new key alg284checkInt(data, "110c010c11001011", 10000); // new key ic285checkAlg(data, "110c10", ENCRYPTED_DATA_OID);286checkAlg(data, "110c110110", PBES2); // cert alg287checkInt(data, "110c110111011", 10000); // cert ic288289try (FileInputStream fis = new FileInputStream("ksnopass");290FileOutputStream fos = new FileOutputStream("ksnopassdup")) {291ks.load(fis, "changeit".toCharArray());292ks.store(fos, "changeit".toCharArray());293}294data = Files.readAllBytes(Path.of("ksnopassdup"));295shouldNotExist(data, "2"); // no Mac296checkAlg(data, "110c010c01000", PBEWithSHA1AndRC4_128);297checkInt(data, "110c010c010011", 10000);298checkAlg(data, "110c010c11000", PBES2);299checkInt(data, "110c010c11001011", 10000);300checkAlg(data, "110c10", DATA_OID);301302try (FileInputStream fis = new FileInputStream("ksnewic");303FileOutputStream fos = new FileOutputStream("ksnewicdup")) {304ks.load(fis, "changeit".toCharArray());305ks.store(fos, "changeit".toCharArray());306}307data = Files.readAllBytes(Path.of("ksnewicdup"));308checkInt(data, "22", 5555); // Mac ic309checkAlg(data, "2000", SHA_256); // Mac alg310checkAlg(data, "110c010c01000", PBES2); // key alg311checkInt(data, "110c010c01001011", 7777); // key ic312checkAlg(data, "110c010c11000", PBEWithSHA1AndRC4_128); // new key alg313checkInt(data, "110c010c110011", 10000); // new key ic314checkAlg(data, "110c110110", PBES2); // cert alg315checkInt(data, "110c110111011", 6666); // cert ic316317// Check keytool behavior318319// ksnormal has password320321keytool("-list -keystore ksnormal")322.shouldContain("WARNING WARNING WARNING")323.shouldContain("Certificate chain length: 0");324325SecurityTools.setResponse("changeit");326keytool("-list -keystore ksnormal")327.shouldNotContain("WARNING WARNING WARNING")328.shouldContain("Certificate fingerprint");329330// ksnopass is password-less331332keytool("-list -keystore ksnopass")333.shouldNotContain("WARNING WARNING WARNING")334.shouldContain("Certificate fingerprint");335336// -certreq prompts for keypass337SecurityTools.setResponse("changeit");338keytool("-certreq -alias a -keystore ksnopass")339.shouldContain("Enter key password for <a>")340.shouldContain("-----BEGIN NEW CERTIFICATE REQUEST-----")341.shouldHaveExitValue(0);342343// -certreq -storepass works fine344keytool("-certreq -alias a -keystore ksnopass -storepass changeit")345.shouldNotContain("Enter key password for <a>")346.shouldContain("-----BEGIN NEW CERTIFICATE REQUEST-----")347.shouldHaveExitValue(0);348349// -certreq -keypass also works fine350keytool("-certreq -alias a -keystore ksnopass -keypass changeit")351.shouldNotContain("Enter key password for <a>")352.shouldContain("-----BEGIN NEW CERTIFICATE REQUEST-----")353.shouldHaveExitValue(0);354355// -importkeystore prompts for srckeypass356SecurityTools.setResponse("changeit", "changeit");357keytool("-importkeystore -srckeystore ksnopass "358+ "-destkeystore jks3 -deststorepass changeit")359.shouldContain("Enter key password for <a>")360.shouldContain("Enter key password for <b>")361.shouldContain("2 entries successfully imported");362363// ksnopass2 is ksnopass + 2 cert entries364365ks = KeyStore.getInstance(new File("ksnopass"), (char[])null);366ks.setCertificateEntry("aa", ks.getCertificate("a"));367ks.setCertificateEntry("bb", ks.getCertificate("b"));368try (FileOutputStream fos = new FileOutputStream("ksnopass2")) {369ks.store(fos, null);370}371372// -importkeystore prompts for srckeypass for private keys373// and no prompt for certs374SecurityTools.setResponse("changeit", "changeit");375keytool("-importkeystore -srckeystore ksnopass2 "376+ "-destkeystore jks5 -deststorepass changeit")377.shouldContain("Enter key password for <a>")378.shouldContain("Enter key password for <b>")379.shouldNotContain("Enter key password for <aa>")380.shouldNotContain("Enter key password for <bb>")381.shouldContain("4 entries successfully imported");382383// ksonlycert has only cert entries384385ks.deleteEntry("a");386ks.deleteEntry("b");387try (FileOutputStream fos = new FileOutputStream("ksonlycert")) {388ks.store(fos, null);389}390391// -importkeystore does not prompt at all392keytool("-importkeystore -srckeystore ksonlycert "393+ "-destkeystore jks6 -deststorepass changeit")394.shouldNotContain("Enter key password for <aa>")395.shouldNotContain("Enter key password for <bb>")396.shouldContain("2 entries successfully imported");397398// create a new password-less keystore399keytool("-keystore ksnopass -exportcert -alias a -file a.cert -rfc");400401// Normally storepass is prompted for402keytool("-keystore kscert1 -importcert -alias a -file a.cert -noprompt")403.shouldContain("Enter keystore password:");404keytool("-keystore kscert2 -importcert -alias a -file a.cert -noprompt "405+ "-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE")406.shouldContain("Enter keystore password:");407keytool("-keystore kscert3 -importcert -alias a -file a.cert -noprompt "408+ "-J-Dkeystore.pkcs12.macAlgorithm=NONE")409.shouldContain("Enter keystore password:");410// ... but not if it's password-less411keytool("-keystore kscert4 -importcert -alias a -file a.cert -noprompt "412+ "-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE "413+ "-J-Dkeystore.pkcs12.macAlgorithm=NONE")414.shouldNotContain("Enter keystore password:");415416// still prompt for keypass for genkeypair and certreq417SecurityTools.setResponse("changeit", "changeit");418keytool("-keystore ksnopassnew -genkeypair -keyalg DSA "419+ "-alias a -dname CN=A "420+ "-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE "421+ "-J-Dkeystore.pkcs12.macAlgorithm=NONE")422.shouldNotContain("Enter keystore password:")423.shouldContain("Enter key password for <a>");424keytool("-keystore ksnopassnew -certreq -alias a")425.shouldNotContain("Enter keystore password:")426.shouldContain("Enter key password for <a>");427keytool("-keystore ksnopassnew -list -v -alias a")428.shouldNotContain("Enter keystore password:")429.shouldNotContain("Enter key password for <a>");430431// params only read on demand432433// keyPbeIterationCount is used by -genkeypair434keytool("-keystore ksgenbadkeyic -genkeypair -keyalg DSA "435+ "-alias a -dname CN=A "436+ "-storepass changeit "437+ "-J-Dkeystore.pkcs12.keyPbeIterationCount=abc")438.shouldContain("keyPbeIterationCount is not a number: abc")439.shouldHaveExitValue(1);440441keytool("-keystore ksnopassnew -exportcert -alias a -file a.cert");442443// but not used by -importcert444keytool("-keystore ksimpbadkeyic -importcert -alias a -file a.cert "445+ "-noprompt -storepass changeit "446+ "-J-Dkeystore.pkcs12.keyPbeIterationCount=abc")447.shouldHaveExitValue(0);448449// None is used by -list450keytool("-keystore ksnormal -storepass changeit -list "451+ "-J-Dkeystore.pkcs12.keyPbeIterationCount=abc "452+ "-J-Dkeystore.pkcs12.certPbeIterationCount=abc "453+ "-J-Dkeystore.pkcs12.macIterationCount=abc")454.shouldHaveExitValue(0);455}456457private static void testWithOpensslCommands(String opensslPath)458throws Throwable {459460OutputAnalyzer output1 = ProcessTools.executeCommand(opensslPath,461"pkcs12", "-in", "ksnormal", "-passin", "pass:changeit",462"-info", "-nokeys", "-nocerts");463output1.shouldHaveExitValue(0)464.shouldMatch("MAC:.*sha256.*Iteration 10000")465.shouldContain("Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC,"466+ " Iteration 10000, PRF hmacWithSHA256")467.shouldContain("PKCS7 Encrypted data: PBES2, PBKDF2, AES-256-CBC,"468+ " Iteration 10000, PRF hmacWithSHA256");469470OutputAnalyzer output2 = ProcessTools.executeCommand(opensslPath,471"pkcs12", "-in", "ksnormaldup", "-passin", "pass:changeit",472"-info", "-nokeys", "-nocerts");473output2.shouldHaveExitValue(0);474if(!output1.getStderr().equals(output2.getStderr())) {475throw new RuntimeException("Duplicate pkcs12 keystores"476+ " ksnormal & ksnormaldup show different info");477}478479output1 = ProcessTools.executeCommand(opensslPath, "pkcs12", "-in",480"ksnopass", "-passin", "pass:changeit", "-info", "-nokeys",481"-nocerts");482output1.shouldNotHaveExitValue(0);483484output1 = ProcessTools.executeCommand(opensslPath, "pkcs12", "-in",485"ksnopass", "-passin", "pass:changeit", "-info", "-nokeys",486"-nocerts", "-nomacver");487output1.shouldHaveExitValue(0)488.shouldNotContain("PKCS7 Encrypted data:")489.shouldContain("Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC,"490+ " Iteration 10000, PRF hmacWithSHA256")491.shouldContain("Shrouded Keybag: pbeWithSHA1And128BitRC4,"492+ " Iteration 10000");493494output2 = ProcessTools.executeCommand(opensslPath, "pkcs12", "-in",495"ksnopassdup", "-passin", "pass:changeit", "-info", "-nokeys",496"-nocerts", "-nomacver");497output2.shouldHaveExitValue(0);498if(!output1.getStderr().equals(output2.getStderr())) {499throw new RuntimeException("Duplicate pkcs12 keystores"500+ " ksnopass & ksnopassdup show different info");501}502503output1 = ProcessTools.executeCommand(opensslPath, "pkcs12", "-in",504"ksnewic", "-passin", "pass:changeit", "-info", "-nokeys",505"-nocerts");506output1.shouldHaveExitValue(0)507.shouldMatch("MAC:.*sha256.*Iteration 5555")508.shouldContain("Shrouded Keybag: PBES2, PBKDF2, AES-256-CBC,"509+ " Iteration 7777, PRF hmacWithSHA256")510.shouldContain("Shrouded Keybag: pbeWithSHA1And128BitRC4,"511+ " Iteration 10000")512.shouldContain("PKCS7 Encrypted data: PBES2, PBKDF2, AES-256-CBC,"513+ " Iteration 6666, PRF hmacWithSHA256");514515output2 = ProcessTools.executeCommand(opensslPath, "pkcs12", "-in",516"ksnewicdup", "-passin", "pass:changeit", "-info", "-nokeys",517"-nocerts");518output2.shouldHaveExitValue(0);519if(!output1.getStderr().equals(output2.getStderr())) {520throw new RuntimeException("Duplicate pkcs12 keystores"521+ " ksnewic & ksnewicdup show different info");522}523}524525/**526* Check keystore loading and key/cert reading.527*528* @param keystore the file name of keystore529* @param alias the key/cert to read530* @param storePass store pass to try out, can be null531* @param keypass key pass to try, can not be null532* @param expectedLoad expected result of keystore loading, true if non533* null, false if null, exception class if exception534* @param expectedCert expected result of cert reading535* @param expectedKey expected result of key reading536*/537private static void check(538String keystore,539String alias,540String storePass,541String keypass,542Object expectedLoad,543Object expectedCert,544Object expectedKey) {545KeyStore ks = null;546Object actualLoad, actualCert, actualKey;547String label = keystore + "-" + alias + "-" + storePass + "-" + keypass;548try {549ks = KeyStore.getInstance(new File(keystore),550storePass == null ? null : storePass.toCharArray());551actualLoad = ks != null;552} catch (Exception e) {553e.printStackTrace(System.out);554actualLoad = e.getClass();555}556Asserts.assertEQ(expectedLoad, actualLoad, label + "-load");557558// If not loaded correctly, skip cert/key reading559if (!Objects.equals(actualLoad, true)) {560return;561}562563try {564actualCert = (ks.getCertificate(alias) != null);565} catch (Exception e) {566e.printStackTrace(System.out);567actualCert = e.getClass();568}569Asserts.assertEQ(expectedCert, actualCert, label + "-cert");570571try {572actualKey = (ks.getKey(alias, keypass.toCharArray()) != null);573} catch (Exception e) {574e.printStackTrace(System.out);575actualKey = e.getClass();576}577Asserts.assertEQ(expectedKey, actualKey, label + "-key");578}579580private static OutputAnalyzer keytool(String s) throws Throwable {581return SecurityTools.keytool(s);582}583}584585586