Path: blob/master/test/jdk/sun/security/tools/keytool/PKCS12Passwd.java
41152 views
/*1* Copyright (c) 2017, 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*/2223/*24* @test25* @bug 8192988 826622026* @summary keytool should support -storepasswd for pkcs12 keystores27* @library /test/lib28* @build jdk.test.lib.SecurityTools29* jdk.test.lib.Utils30* jdk.test.lib.Asserts31* jdk.test.lib.JDKToolFinder32* jdk.test.lib.JDKToolLauncher33* jdk.test.lib.Platform34* jdk.test.lib.process.*35* @run main PKCS12Passwd36*/3738import jdk.test.lib.Asserts;39import jdk.test.lib.SecurityTools;40import jdk.test.lib.process.OutputAnalyzer;4142import java.io.File;43import java.security.KeyStore;44import java.util.Collections;4546public class PKCS12Passwd {4748public static void main(String[] args) throws Exception {4950// A PrivateKeyEntry51kt("-genkeypair -alias a -dname CN=A -keyalg DSA")52.shouldHaveExitValue(0);5354// A TrustedCertificateEntry (genkeypair, export, delete, import)55kt("-genkeypair -alias b -dname CN=B -keyalg DSA")56.shouldHaveExitValue(0);57kt("-exportcert -alias b -file b.cert")58.shouldHaveExitValue(0);59kt("-delete -alias b")60.shouldHaveExitValue(0);61kt("-list -alias b")62.shouldHaveExitValue(1);63kt("-importcert -alias b -file b.cert -noprompt")64.shouldHaveExitValue(0);6566// A SecretKeyEntry67kt("-genseckey -keyalg AES -keysize 256 -alias c")68.shouldHaveExitValue(0);6970// Change password7172// 1. Using -importkeystore73ktFull("-importkeystore -srckeystore ks -destkeystore ks2 "74+ "-srcstoretype pkcs12 -deststoretype pkcs12 "75+ "-srcstorepass changeit -deststorepass newpass")76.shouldHaveExitValue(0);7778check("ks2", "newpass", "newpass");7980// 2. Using -storepasswd81kt("-storepasswd -new newpass")82.shouldHaveExitValue(0)83.shouldNotContain("Ignoring user-specified");8485check("ks", "newpass", "newpass");8687// Other facts. Not necessarily the correct thing.8889// A PKCS12 keystore can be loaded as a JKS, and it follows JKS rules90// which means the storepass and keypass can be changed separately!9192ktFull("-genkeypair -alias a -dname CN=A -storetype pkcs12 -keyalg DSA "93+ "-storepass changeit -keypass changeit -keystore p12")94.shouldHaveExitValue(0);9596// Only storepass is changed97ktFull("-storepasswd -storepass changeit -new newpass "98+ "-keystore p12 -storetype jks")99.shouldHaveExitValue(0);100101check("p12", "newpass", "changeit");102103// Only keypass is changed104ktFull("-keypasswd -storepass newpass -keypass changeit -new newpass "105+ "-keystore p12 -storetype jks -alias a")106.shouldHaveExitValue(0);107108check("p12", "newpass", "newpass");109110// Conversely, a JKS keystore can be laoded as a PKCS12, and it follows111// PKCS12 rules that both passwords are changed at the same time and112// some commands are rejected.113114ktFull("-genkeypair -alias a -dname CN=A -storetype jks -keyalg DSA "115+ "-storepass changeit -keypass changeit -keystore jks")116.shouldHaveExitValue(0);117118// Both storepass and keypass changed.119ktFull("-storepasswd -storepass changeit -new newpass "120+ "-keystore jks -storetype pkcs12")121.shouldHaveExitValue(0);122123check("jks", "newpass", "newpass");124125// -keypasswd is not available for pkcs12126ktFull("-keypasswd -storepass newpass -keypass newpass -new newerpass "127+ "-keystore jks -storetype pkcs12 -alias a")128.shouldHaveExitValue(1);129130// but available for JKS131ktFull("-keypasswd -storepass newpass -keypass newpass -new newerpass "132+ "-keystore jks -alias a")133.shouldHaveExitValue(0);134135check("jks", "newpass", "newerpass");136137// A password-less keystore138ktFull("-keystore nopass -genkeypair -keyalg EC "139+ "-storepass changeit -alias no -dname CN=no "140+ "-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE "141+ "-J-Dkeystore.pkcs12.macAlgorithm=NONE")142.shouldHaveExitValue(0);143144ktFull("-keystore nopass -list")145.shouldHaveExitValue(0)146.shouldNotContain("Enter keystore password:");147148ktFull("-keystore nopass -list -storetype pkcs12")149.shouldHaveExitValue(0)150.shouldNotContain("Enter keystore password:");151}152153// Makes sure we can load entries in a keystore154static void check(String file, String storePass, String keyPass)155throws Exception {156157KeyStore ks = KeyStore.getInstance(158new File(file), storePass.toCharArray());159160for (String a : Collections.list(ks.aliases())) {161if (ks.isCertificateEntry(a)) {162ks.getCertificate(a);163} else {164ks.getEntry(a,165new KeyStore.PasswordProtection(keyPass.toCharArray()));166}167}168}169170static OutputAnalyzer kt(String arg) throws Exception {171return ktFull("-keystore ks -storepass changeit " + arg);172}173174static OutputAnalyzer ktFull(String arg) throws Exception {175return SecurityTools.keytool("-debug " + arg);176}177}178179180