Different types of keystore in Java -- DKS

English 简体中文 繁体中文 ภาษาไทย Tiếng Việt
Summary

Domain KeyStore (DKS), introduced in Java 8, functions as a "keystore of keystores," abstracting a collection of individual keystores into a single logical unit. Developers configure DKS by defining domains in a configuration file, specifying each contained keystore with properties such as its URI and type. Loading a DKS instance involves using KeyStore.getInstance("DKS") and passing a DomainLoadStoreParameter object, which includes the configuration URI and a map of passwords for the underlying keystores. DKS also supports loading a single JKS or default-type keystore directly from an InputStream. Once loaded, DKS behaves like any other KeyStore for subsequent cryptographic operations.

域密钥库 (DKS) 是密钥库的密钥库。它将多个密钥库抽象为单个逻辑密钥库。它本身实际上并不是密钥库。这种新的密钥库类型在Java 8中引入。有一个新的类DomainLoadStoreParameter与DKS密切相关。

要将不同的密钥库加载到单个逻辑密钥库中,需要一些配置。以下是用于分组不同密钥库的配置格式。

domain  [ ...] {
         keystore  [ ...] ;
         ...
};

下面是域domain的一个示例配置。

domain app1 {
     keystore app1-truststore
         keystoreURI="file:///app1/etc/truststore.jks";

     keystore system-truststore
         keystoreURI="${java.home}/lib/security/cacerts";

     keystore app1-keystore
         keystoreType="PKCS12"
         keystoreURI="file:///app1/etc/keystore.p12";
 };

此配置定义了一个域app1,它包含三个密钥库。每个包含的密钥库 (app1-truststore、system-truststore、app1-keystore) 都指定了 keystoreName。请注意,每个条目的 keystoreName 必须唯一。

在 keystoreName 后面是一组密钥库属性。这些属性包括 keystoreURI、keystoreType、keystoreProviderName、keystorePasswordEnv、entryNameSeparator。属性名称不言自明。

加载 DKS 密钥库时,将构造一个DomainLoadStoreParameter对象并将其传递给 KeyStore.load()。下面是加载 DKS 密钥库的示例代码片段。

Map<String, KeyStore.ProtectionParameter> PASSWORDS =
        new HashMap<String, KeyStore.ProtectionParameter>() {{
            put("keystore",
                new KeyStore.PasswordProtection("test123".toCharArray()));
            put("policy_keystore",
                new KeyStore.PasswordProtection(
                    "Alias.password".toCharArray()));
            put("pw_keystore",
                new KeyStore.PasswordProtection("test12".toCharArray()));
            put("eckeystore1",
                new KeyStore.PasswordProtection("password".toCharArray()));
            put("eckeystore2",
                new KeyStore.PasswordProtection("password".toCharArray()));
            put("truststore",
                new KeyStore.PasswordProtection("changeit".toCharArray()));
            put("empty",
                new KeyStore.PasswordProtection("passphrase".toCharArray()));
}};
URI config = new URI(CONFIG + "#system");
KeyStore keystore = KeyStore.getInstance("DKS");
keystore.load(new DomainLoadStoreParameter(config, PASSWORDS));

config 是 DKS 密钥库的配置路径,PASSWORDS 是包含配置文件中定义的密钥库的相应密码的映射。

DKS 还支持加载 InputStream,在这种情况下,InputStream 必须是单个密钥库流,其密钥库类型为 JKS 或 KeyStore.getDefaultType()。例如,下面的代码片段将使用 DKS 加载 JKS 密钥库。

char[] PASSWORD = <PASSWORD>;
KeyStore keyStore = KeyStore.getInstance("DKS");, 
keyStore.load(new FileInputStream("keystore.jks"), PASSWORD);

加载密钥库后,其他密钥库操作与其他密钥库类型(如 JKS)的操作相同。有关创建密钥和证书的操作,请阅读Java 中不同类型的密钥库——JKS

JAVA TUTORIAL KEYSTORE DKS

  RELATED

  COMMENTS

0

No comment for this article.