`

Keystores and Truststores

    博客分类:
  • J2SE
阅读更多

Keystores and Truststores

A  keystore   is a database of key material. Key material is used for a variety of purposes, including authentication and data integrity. There are various types of keystores available, including "PKCS12" and Sun's "JKS."

Generally speaking, keystore information can be grouped into two different categories: key entries and trusted certificate entries. A key entry consists of an entity's identity and its private key, and can be used for a variety of cryptographic purposes. In contrast, a trusted certificate entry only contains a public key in addition to the entity's identity. Thus, a trusted certificate entry can not be used where a private key is required, such as in a  javax.net.ssl.KeyManager . In the JDK implementation of "JKS", a keystore may contain both key entries and trusted certificate entries.

A  truststore   is a keystore which is used when making decisions about what to trust. If you receive some data from an entity that you already trust, and if you can verify that the entity is the one it claims to be, then you can assume that the data really came from that entity.

An entry should only be added to a truststore if the user makes a decision to trust that entity. By either generating a keypair or by importing a certificate, the user has given trust to that entry, and thus any entry in the keystore is considered a trusted entry.

It may be useful to have two different keystore files: one containing just your key entries, and the other containing your trusted certificate entries, including Certification Authority (CA) certificates. The former contains private information, while the latter does not. Using two different files instead of a single keystore file provides for a cleaner separation of the logical distinction between your own certificates (and corresponding private keys) and others' certificates. You could provide more protection for your private keys if you store them in a keystore with restricted access, while providing the trusted certificates in a more publicly accessible keystore if needed.

 

 Creating a Simple Keystore and Truststore

   

  1. Create a new keystore and self-signed certificate with corresponding public/private keys.
    % keytool -genkey -alias duke -keyalg RSA \
      -validity 7 -keystore keystore 
    
     Enter keystore password:  password
     What is your first and last name?
     [Unknown]:  Duke
     What is the name of your organizational unit?
     [Unknown]:  Java Software
     What is the name of your organization?
     [Unknown]:  Sun Microsystems, Inc.
     What is the name of your City or Locality?
     [Unknown]:  Palo Alto
     What is the name of your State or Province?
     [Unknown]:  CA
     What is the two-letter country code for this unit?
     [Unknown]:  US 
     Is CN=Duke, OU=Java Software, O="Sun Microsystems, Inc.",
     L=Palo Alto, ST=CA, C=US correct?
     [no]:  yes
    
     Enter key password for <duke>
      (RETURN if same as keystore password):  <CR>
          
    This is the keystore that the server will use.

     

  2. Examine the keystore. Notice the entry type is keyEntry which means that this entry has a private key associated with it (shown in red).
    % keytool -list -v -keystore keystore
    Enter keystore password:  password
    
    Keystore type: jks
    Keystore provider: SUN
    
    Your keystore contains 1 entry
    
    Alias name: duke
    Creation date: Dec 20, 2001
    Entry type: keyEntry
    Certificate chain length: 1
    Certificate[1]:
    Owner: CN=Duke, OU=Java Software, O="Sun Microsystems, Inc.", 
    L=Palo Alto, ST=CA, C=US
    Issuer: CN=Duke, OU=Java Software, O="Sun Microsystems, Inc.", L=Palo Alto, ST=CA, C=US
    Serial number: 3c22adc1
    Valid from: Thu Dec 20 19:34:25 PST 2001 until: Thu Dec 27 19:34:25 PST 2001
    Certificate fingerprints:
        MD5: F1:5B:9B:A1:F7:16:CF:25:CF:F4:FF:35:3F:4C:9C:F0
        SHA1: B2:00:50:DD:B6:CC:35:66:21:45:0F:96:AA:AF:6A:3D:E4:03:7C:74
          

     

  3. Export and examine the self-signed certificate.
    % keytool -export -alias duke -keystore keystore -rfc \
      -file duke.cer
    Enter keystore password:  password
    Certificate stored in file <duke.cer>
    % cat duke.cer
    -----BEGIN CERTIFICATE-----       
    MIICXjCCAccCBDwircEwDQYJKoZIhvcNAQEEBQAwdjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNB  
    MRIwEAYDVQQHEwlQYWxvIEFsdG8xHzAdBgNVBAoTFlN1biBNaWNyb3N5c3RlbXMsIEluYy4xFjAU
    BgNVBAsTDUphdmEgU29mdHdhcmUxDTALBgNVBAMTBER1a2UwHhcNMDExMjIxMDMzNDI1WhcNMDEx
    MjI4MDMzNDI1WjB2MQswCQYDVQQGEwJVUzELMAkGA1UECBMCQ0ExEjAQBgNVBAcTCVBhbG8gQWx0
    bzEfMB0GA1UEChMWU3VuIE1pY3Jvc3lzdGVtcywgSW5jLjEWMBQGA1UECxMNSmF2YSBTb2Z0d2Fy
    ZTENMAsGA1UEAxMERHVrZTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA1loObJzNXsi5aSr8
    N4XzDksD6GjTHFeqG9DUFXKEOQetfYXvA8F9uWtz8WInrqskLTNzwXgmNeWkoM7mrPpK6Rf5M3G1
    NXtYzvxyi473Gh1h9k7tjJvqSVKO7E1oFkQYeUPYifxmjbSMVirWZgvo2UmA1c76oNK+NhoHJ4qj
    eCUCAwEAATANBgkqhkiG9w0BAQQFAAOBgQCRPoQYw9rWWvfLPQuPXowvFmuebsTc28qI7iFWm6BJ
    TT/qdmzti7B5MHOt9BeVEft3mMeBU0CS2guaBjDpGlf+zsK/UUi1w9C4mnwGDZzqY/NKKWtLxabZ
    5M+4MAKLZ92ePPKGpobM2CPLfM8ap4IgAzCbBKd8+CMp8yFmifze9Q==
    -----END CERTIFICATE-----
          
    Alternatively, you could generate Certificate Signing Request (CSR) with -certreq and send that to a Certificate Authority (CA) for signing, but again, that's beyond the scope of this example.

     

  4. Import the certificate into a new truststore.
    % keytool -import -alias dukecert -file duke.cer \
      -keystore truststore
    Enter keystore password:  trustword
    Owner: CN=Duke, OU=Java Software, O="Sun Microsystems, Inc.", L=Palo Alto, ST=CA, C=US
    Issuer: CN=Duke, OU=Java Software, O="Sun Microsystems, Inc.", L=Palo Alto, ST=CA, C=US
    Serial number: 3c22adc1
    Valid from: Thu Dec 20 19:34:25 PST 2001 until: Thu Dec 27 19:34:25 PST 2001
    Certificate fingerprints:
        MD5: F1:5B:9B:A1:F7:16:CF:25:CF:F4:FF:35:3F:4C:9C:F0
        SHA1: B2:00:50:DD:B6:CC:35:66:21:45:0F:96:AA:AF:6A:3D:E4:03:7C:74
    Trust this certificate? [no]:  yes
    Certificate was added to keystore
          
  5. Examine the truststore. Note that the entry type is trustedCertEntry, which means that a private key is not available for this entry (shown in red). It also means that this file is not suitable as a KeyManager's keystore.
    % keytool -list -v -keystore truststore 
    Enter keystore password:  trustword
    
    Keystore type: jks
    Keystore provider: SUN
    
    Your keystore contains 1 entry
    
    Alias name: dukecert
    Creation date: Dec 20, 2001
    Entry type: trustedCertEntry
    
    Owner: CN=Duke, OU=Java Software, O="Sun Microsystems, Inc.", L=Palo Alto, ST=CA, C=US
    Issuer: CN=Duke, OU=Java Software, O="Sun Microsystems, Inc.", L=Palo Alto, ST=CA, C=US
    Serial number: 3c22adc1
    Valid from: Thu Dec 20 19:34:25 PST 2001 until: Thu Dec 27 19:34:25 PST 2001
    Certificate fingerprints:
        MD5: F1:5B:9B:A1:F7:16:CF:25:CF:F4:FF:35:3F:4C:9C:F0
        SHA1: B2:00:50:DD:B6:CC:35:66:21:45:0F:96:AA:AF:6A:3D:E4:03:7C:74
          
    Now run your applications with the appropriate key stores. This example assumes the default X509KeyManager and X509TrustManager are used, thus we will select the keystores using the system properties described in Customization.
    % java -Djavax.net.ssl.keyStore=keystore \
      -Djavax.net.ssl.keyStorePassword=password Server
    
    % java -Djavax.net.ssl.trustStore=truststore \
      -Djavax.net.ssl.trustStorePassword=trustword Client

 

分享到:
评论

相关推荐

    为高级 JSSE 开发人员定制 SSL

    1. **KeyStores和TrustStores**:JSSE使用KeyStore存储私钥和证书,用于身份验证;TrustStore则存储信任的CA证书或服务器证书,用于验证对方的身份。开发者可以通过系统属性`javax.net.ssl.keyStore`和`javax.net....

    Apache Geronimo 2.1_ Quick Reference.pdf

    Downloading and running Apache Geronimo 12 Geronimo Administration Console 14 Information portlet 15 Java System Info portlet 15 Server Logs portlet 15 Web Server portlet 16 JMS Server portlet ...

    Android代码-FrostforFacebook

    Note Some keystores are public for the sake of automatic builds and consistent signing across devices. This means that others can build apps with the same signature. The only valid download sources ...

    安装weblogic设置ssl归纳.pdf

    在“安全”-&gt;“Keystores and Certificates”中配置密钥库信息,导入CA签发的证书。 5.5 配置密钥库信息 在管理控制台中,配置SSL监听端口(如7443),选择刚才创建的密钥库和私钥别名,启用SSL。 5.6 测试 配置...

    Oracle Service Bus下载的配置

    在 Environment -&gt; Services -&gt; AdminServer (admin) 下,你会看到 KeyStores 和 SSL 卡片。KeyStores 存储了服务器的私钥和证书,而 SSL 卡片则用于配置 SSL 安全套接层,确保通信的安全性。在这里,你可以配置 ...

    WebLogic 集群中SSL 配置说明

    - 在弹出的对话框中选择`Custom Identity and Custom Trust`。 - 输入keystore路径、类型以及密码。 - 输入私钥的别名和keystore的密码。 - 根据需要配置其他高级选项,然后点击【Apply】按钮完成设置。 ##### 3. ...

    ansible-tls-klusters

    ansible-tls-klusters 用法 依存关系 在每个库存文件夹中列出了依赖关系 安装依赖项: pip install -r &lt; my&gt; /requirements/python_...ansible-playbook -i inventories/test/docker playbooks/tls/keystores.yml

    java_sec_demo:Java安全演示的源代码和文档

    5. **密钥和证书管理**:对于加密和数字签名,项目可能会包含如何管理和使用密钥库(Keystores)和信任库(Truststores)的示例。 通过分析和学习"java_sec_demo"中的代码和文档,开发者可以深入理解Java安全模型的...

    solidhabits-android

    Solidhabits应用程序。... 并将两个密钥库都放在app/keystores/目录下: playstore.keystore stage.keystore 构建变体 使用Android Studio Build Variants按钮在生产和过渡版本以及调试和发行版本类型之间进行

    MyRSVPApp-React-Natove:RSVP React本机应用程序-首次尝试

    通过浏览到React Native项目的android文件夹在Android Studio中打开您的应用程序转到Build&gt;生成签名的包/ APK选择APK,然后单击下一步在密钥库路径下,单击创建新的选择一个路径,例如/ home / karl / keystores / ...

    Weblogic_8.1_SSL_配置详细图解.rar

    - 打开WebLogic管理控制台,登录后选择“Security” &gt; “Realms” &gt; 你的realm名 &gt; "Providers" &gt; "KeyStores"。 - 添加一个新的KeyStore,输入keystore的文件路径和密码。 - 在“SSL”部分,配置SSL端口(默认为...

    portecle-1.11

    1. **创建和管理Keystores**:你可以通过Portecle创建新的keystore文件,设置keystore的密码,以及管理keystore中的条目。这对于在HTTPS服务器配置、SSL/TLS连接以及代码签名等领域非常有用。 2. **生成和导入密钥...

    JKS 密钥库使用专用格式。建议使用 “keytool -importkeystore -srckeystore E:\xxxxxx- pkcs12” 迁移到行业标准格式PKCS12

    错误: Key was created with errors: Warning: JKS 密钥库使用专用格式。建议使用 “keytool -importkeystore -srckeystore E:\androidstudio\androidstudio_work\CommonDemo\app\fast_keystore.jks -destkeystore E...

    weblogic开启ssl功能.docx

    - 标识和信任位置【Identity and Trust Locations】:选择“密钥库”【keystores】 - 私有密钥别名【Private Key Alias】:`app_server` - 私有密钥密码短语【Private Key Passphrase】:`boncme` - 确认私有密钥...

    Java_2平台安全技术-结构、api设计和实现

    2. Certificates和KeyStores:用于管理数字证书,存储公钥和私钥,支持X.509标准。 3. Permissions和Policy:表示代码执行的权限,并定义安全策略。`java.security.Permission`类是所有权限的基类,而`java....

    4.CXF安全访问之单向SSL或者双向SSL(三)

    1. **配置Keystores**:为服务器和客户端创建keystore文件,存储各自的私钥和证书。可以使用Keytool工具来生成这些文件。 2. **设置CXF配置**:在CXF的配置文件(如cxf.xml或Spring配置文件)中,指定keystore和...

    【计算机专业-Andorid项目源码100套之】Android 打包签名 从生成keystore到完成签名

    - **Multiple keystores**:如果你有多个应用,或者需要在不同环境中发布应用(如测试和生产),可能需要生成多个keystore文件。 - **Key recovery**:如果丢失了keystore或密码,你将无法更新应用,因此最好备份...

Global site tag (gtag.js) - Google Analytics