15

我正在试验 OSGi 条件权限机制。更具体地说,我正在尝试使用 org.osgi.service.condpermadmin.BundleSignerCondition 来限制可以启动哪些包。我的文档指出,为了使用此权限,我必须使用 org.osgi.framework.trust.repositories 框架配置属性指定 JKS 密钥库的路径。但是,相同的文档提到此属性中提到的 JKS 不能有密码。那么问题来了:如何创建没有密码的 JKS?Keytool 实用程序拒绝使用空白密码创建 JKS。

4

1 回答 1

20

一段时间以来,您无法使用 keytool 创建具有空白密码的密钥库,但您仍然可以通过编程方式进行操作。

阅读这样的证书:

private static Certificate readCert(String path) throws IOException, CertificateException {
    try (FileInputStream fin = new FileInputStream(path)) {
        return CertificateFactory.getInstance("X.509").generateCertificate(fin);
    }
}

比使用空密码创建密钥库,如下所示:

try {
    // Reading the cert
    Certificate cert = readCert("/tmp/cert.cert");

    // Creating an empty JKS keystore
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    keystore.load(null, null);

    // Adding the cert to the keystore
    keystore.setCertificateEntry("somecert", cert);

    // Saving the keystore with a zero length password
    FileOutputStream fout = new FileOutputStream("/tmp/keystore");
    keystore.store(fout, new char[0]);
} catch (GeneralSecurityException | IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

运行命令:

keytool -list -keystore keystore

它会要求输入密码,但您只需按回车即可。您将收到以下警告,但将列出密钥库的内容:

*****************  WARNING WARNING WARNING  *****************
* The integrity of the information stored in your keystore  *
* has NOT been verified!  In order to verify its integrity, *
* you must provide your keystore password.                  *
*****************  WARNING WARNING WARNING  *****************

这可能对你有用。

于 2014-05-15T18:19:09.640 回答