Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/java.base/share/classes/java/security/KeyStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -1780,6 +1780,7 @@ private static final KeyStore getInstance(File file, char[] password,
}

KeyStore keystore = null;
String matched = null;

try (DataInputStream dataStream =
new DataInputStream(
Expand All @@ -1806,8 +1807,10 @@ private static final KeyStore getInstance(File file, char[] password,
if (CryptoAlgorithmConstraints.permits(
"KEYSTORE", type)) {
keystore = new KeyStore(impl, (Provider)objs[1], type);
break;
} else {
matched = type;
}
break;
}
} catch (NoSuchAlgorithmException | NoSuchProviderException e) {
// ignore
Expand Down Expand Up @@ -1836,8 +1839,14 @@ private static final KeyStore getInstance(File file, char[] password,
}
}

throw new KeyStoreException("Unrecognized keystore format. "
+ "Please load it with a specified type");
if (matched == null) {
throw new KeyStoreException("Unrecognized keystore format. "
+ "Please load it with a specified type");
} else {
throw new KeyStoreException("Keystore format " +
matched +
" disabled by jdk.crypto.disabledAlgorithms property");
}
}

/**
Expand Down
53 changes: 53 additions & 0 deletions test/jdk/java/security/KeyStore/DisabledKnownType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/**
* @test
* @bug 8373690
* @summary verify that the exception message indicates the keystore type
* when the type is disabled instead of being unrecognized
* @run main/othervm -Djdk.crypto.disabledAlgorithms=KeyStore.JKS DisabledKnownType
*/

import java.security.KeyStore;
import java.security.KeyStoreException;

public class DisabledKnownType {
public static void main(String[] args) throws Exception {
String cacertsPath = System.getProperty("java.home") +
"/lib/security/cacerts";
try {
KeyStore ks = KeyStore.getInstance(new java.io.File(cacertsPath),
"changeit".toCharArray());
throw new RuntimeException("Expected KeyStoreException not thrown");
} catch (KeyStoreException kse) {
if (kse.getMessage().contains("JKS")) {
System.out.println("Passed: expected ex thrown: " + kse);
} else {
// pass it up
throw kse;
}
}
}
}