UnsupportedOperationException使用KeyStore.getEntry()时?
问题描述:
我试图从Mac OSX 10.6上的Java KeyStore检索条目。我的代码运行正常在Windows和Linux,但是当我在OSX上运行它,我得到以下异常:UnsupportedOperationException使用KeyStore.getEntry()时?
java.lang.UnsupportedOperationException
at java.security.KeyStoreSpi.engineGetEntry(KeyStoreSpi.java:466)
at java.security.KeyStore.getEntry(KeyStore.java:1261)
这里是我的代码:
String keyStorePath = ...
PasswordProtection pp = new PasswordProtection("password".toCharArray());
CallbackHandlerProtection chp = new CallbackHandlerProtection(
new CallbackHandler() {
@Override
public void handle(Callback[] callbacks)
throws IOException, UnsupportedCallbackException {
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callbacks[i];
pc.setPassword("password".toCharArray());
}
}
}
});
try {
KeyStore.Builder kb = Builder.newInstance("JCEKS", null, new File(
keyStorePath), chp);
KeyStore ks = kb.getKeyStore();
Enumeration<String> aliases = ks.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
KeyStore.Entry entry = ks.getEntry(alias, chp);
}
} catch (Exception e) {
e.printStackTrace();
}
任何想法,为什么这个异常被OSX上抛出?这是操作系统上的JVM中的错误吗?有没有人见过这个?
答
看起来是苹果JVM实现中的一个bug。我提交了一个错误报告。谢谢你的帮助!
答
综观KeyStoreSpi.java
在管线466执行揭示了以下内容:
public KeyStore.Entry engineGetEntry(String alias, ...) throws ... {
...
if (protParam instanceof KeyStore.PasswordProtection) {
if (engineIsCertificateEntry(alias)) {
throw new UnsupportedOperationException
("trusted certificate entries are not password-protected");
} else if ...
}
...
}
有你有它抛出异常的确切条件。
那么engineIsCertificateEntry(alias)
何时会返回true?
根据它这样做的documentation ...
... 如果给定别名标识的条目是通过调用创建
setCertificateEntry
,或者创建通过调用setEntry
与TrustedCertificateEntry
但是该方法是抽象的,因此如果不知道所使用的确切实现方式,就很难深入挖掘。根据你的描述,这些逻辑在实现上似乎略有不同。