Skip to content

Commit f2d880c

Browse files
author
Hai-May Chao
committed
8266400: importkeystore fails to a password less pkcs12 keystore
Reviewed-by: weijun
1 parent f5562f1 commit f2d880c

File tree

2 files changed

+92
-3
lines changed

2 files changed

+92
-3
lines changed

src/java.base/share/classes/sun/security/tools/keytool/Main.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2429,8 +2429,15 @@ private int doImportKeyStoreSingle(KeyStore srckeystore, String alias)
24292429
newPass = destKeyPass;
24302430
pp = new PasswordProtection(destKeyPass);
24312431
} else if (objs.snd != null) {
2432-
newPass = P12KEYSTORE.equalsIgnoreCase(storetype) ?
2433-
storePass : objs.snd;
2432+
if (P12KEYSTORE.equalsIgnoreCase(storetype)) {
2433+
if (isPasswordlessKeyStore) {
2434+
newPass = objs.snd;
2435+
} else {
2436+
newPass = storePass;
2437+
}
2438+
} else {
2439+
newPass = objs.snd;
2440+
}
24342441
pp = new PasswordProtection(newPass);
24352442
}
24362443

@@ -2442,7 +2449,7 @@ private int doImportKeyStoreSingle(KeyStore srckeystore, String alias)
24422449
keyStore.setEntry(newAlias, entry, pp);
24432450
// Place the check so that only successful imports are blocked.
24442451
// For example, we don't block a failed SecretEntry import.
2445-
if (P12KEYSTORE.equalsIgnoreCase(storetype)) {
2452+
if (P12KEYSTORE.equalsIgnoreCase(storetype) && !isPasswordlessKeyStore) {
24462453
if (newPass != null && !Arrays.equals(newPass, storePass)) {
24472454
throw new Exception(rb.getString(
24482455
"The.destination.pkcs12.keystore.has.different.storepass.and.keypass.Please.retry.with.destkeypass.specified."));
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
/*
25+
* @test
26+
* @bug 8266400
27+
* @summary Test importkeystore to a password less PKCS12 keystore
28+
* @library /test/lib
29+
*/
30+
31+
import jdk.test.lib.SecurityTools;
32+
import jdk.test.lib.process.OutputAnalyzer;
33+
34+
public class ImportToPwordlessPK12 {
35+
36+
static OutputAnalyzer kt(String cmd, String ks) throws Exception {
37+
return SecurityTools.keytool("-storepass changeit " + cmd +
38+
" -keystore " + ks);
39+
}
40+
41+
public static void main(String[] args) throws Exception {
42+
43+
kt("-genkeypair -keyalg EC -alias testcert -dname CN=EE " +
44+
"-storetype jks -keypass pass123 ", "ks.jks");
45+
46+
/*
47+
* Test by setting the responses for source keystore password and
48+
* key password for alias
49+
*/
50+
SecurityTools.setResponse("changeit", "pass123");
51+
SecurityTools.keytool("-importkeystore -srckeystore ks.jks " +
52+
"-destkeystore ks.p12 " +
53+
"-J-Dkeystore.pkcs12.macAlgorithm=NONE " +
54+
"-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE")
55+
.shouldHaveExitValue(0);
56+
57+
SecurityTools.keytool("-list -keystore ks.p12 -debug")
58+
.shouldContain("Keystore type: PKCS12")
59+
.shouldContain("keystore contains 1 entry")
60+
.shouldNotContain("Enter keystore password:")
61+
.shouldHaveExitValue(0);
62+
63+
kt("-genkeypair -keyalg EC -alias testcert -dname CN=EE " +
64+
"-storetype jks -keypass pass123 ", "ks1.jks");
65+
66+
// Test with all of options specified on command line
67+
SecurityTools.keytool("-importkeystore -srckeystore ks1.jks " +
68+
"-destkeystore ks1.p12 " +
69+
"-srcstorepass changeit " +
70+
"-srcalias testcert " +
71+
"-srckeypass pass123 " +
72+
"-J-Dkeystore.pkcs12.macAlgorithm=NONE " +
73+
"-J-Dkeystore.pkcs12.certProtectionAlgorithm=NONE")
74+
.shouldHaveExitValue(0);
75+
76+
SecurityTools.keytool("-list -keystore ks1.p12 -debug")
77+
.shouldContain("Keystore type: PKCS12")
78+
.shouldContain("keystore contains 1 entry")
79+
.shouldNotContain("Enter keystore password:")
80+
.shouldHaveExitValue(0);
81+
}
82+
}

0 commit comments

Comments
 (0)