简体   繁体   English

如何创建一个空的 Java 信任库?

[英]How to create an empty java trust store?

I want to make a https client in java which initially does not have any CA certs to trust.我想在 Java 中创建一个 https 客户端,它最初没有任何可信任的 CA 证书。 Since I don't want the JVM to use the default cacerts file I should make an empty trust store and point it to the JVM.由于我不希望 JVM 使用默认的 cacerts 文件,因此我应该创建一个空的信任存储并将其指向 JVM。
How can I make an empty trust store?我怎样才能建立一个空的信托商店?

Using keytool, create a random key pair:使用 keytool,创建一个随机密钥对:

keytool -genkeypair -alias boguscert -storepass storePassword -keypass secretPassword -keystore emptyStore.keystore -dname "CN=Developer, OU=Department, O=Company, L=City, ST=State, C=CA"

then delete it然后删除它

keytool -delete -alias boguscert -storepass storePassword -keystore emptyStore.keystore

review its contents:查看其内容:

$ keytool -list -keystore emptyStore.keystore -storepass storePassword
Keystore type: JKS
Keystore provider: SUN
Your keystore contains 0 entries

One possible solution I found is to import some random certificate into a newly created trust store with keytool import and then delete the imported certificate from it.我发现的一种可能的解决方案是使用keytool import将一些随机证书导入到新创建的信任库中,然后从中删除导入的证书。 This leaves you with an empty key/trust store.这会给您留下一个空的密钥/信任库。 Unfortunately the JVM is not happy with an empty trust store and throws an exception upon that.不幸的是,JVM 对空的信任存储并不满意,并在此基础上抛出异常。 So at least one certificate should be present there which could be any invalid or expired one in order to achieve the goal.因此,至少应存在一个证书,该证书可能是任何无效或过期的证书,以实现目标。

if someone eventually reaches here again:如果有人最终再次到达这里:

  public static void main (String[] args) {
    String storePassword = "storePassword";
    String storeName = "emptyStore.jks";
    String storeType = "jks";
    try (FileOutputStream fileOutputStream = new FileOutputStream(storeName)) {
      KeyStore keystore = KeyStore.getInstance(storeType);
      keystore.load(null, storePassword.toCharArray());
      keystore.store(fileOutputStream, storePassword.toCharArray());
    } catch (CertificateException | NoSuchAlgorithmException | IOException | KeyStoreException e) {
      e.printStackTrace();
  }

then check the content with keytool:然后用keytool检查内容:

$ keytool -list -keystore emptyStore.jks -storepass storePassword
Keystore type: JKS
Keystore provider: SUN
Your keystore contains 0 entries

You may pass a null argument to KeyStore::load to create an empty keystore.您可以将空参数传递给KeyStore::load以创建一个空的密钥库。 See https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html#load-java.io.InputStream-char:A-请参阅https://docs.oracle.com/javase/8/docs/api/java/security/KeyStore.html#load-java.io.InputStream-char:A-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM