How to create a certificate into a PKCS12 keystore with keytool?

后端 未结 2 1732
遇见更好的自我
遇见更好的自我 2021-01-31 19:16

I wanted to create a certificate into a PKCS12 keystore format with keytool program.

The keystore has extension .pfx.

How do I achieve this?

相关标签:
2条回答
  • 2021-01-31 19:26

    Additional answer to the key of the question.

    With JDK 8 (1.8.0_121-b13) you don't get an exception if you remove -storetype pkcs12 but the keytool creates a JKS keystore instead, and the .pfx extension is ignored.

    It also asks for a -keypass mykeypassword which the keytool doesn't support for PKCS12.

    %JAVA_HOME%/bin/keytool -genkeypair -alias mykey -keyalg EC -dname "cn=CN, ou=OU, o=O, c=C" -validity 365 -keystore keystore.pfx -keypass mykeypassword -storepass mystorepassword -v
    
    (translated)
    Generating keypair (Type EC, 256 Bit) and self-signed certificate (SHA256withECDSA) with a validity of 365 days
        for: CN=CN, OU=OU, O=O, C=C
    [keystore.pfx saved]
    

    List the contents:

    %JAVA_HOME%/bin/keytool -list -keystore keystore.pfx -storepass mystorepassword 
    
    (translated)
    Keystore-Type: JKS
    Keystore-Provider: SUN
    
    Keystore contains 1 entry.
    
    mykey, 25.04.2017, PrivateKeyEntry,
    Certificate-Fingerprint (SHA1): A1:6C:5F:8F:43:37:1A:B6:43:69:08:DE:6B:B9:4D:DB:05:C9:D5:84
    

    You see it's a Java keystore.

    The next problem is, that even if you specify -storetype pkcs12 when you -list the keystore, the keytool will still display the store as a JKS keystore!

    Let's try that:

    %JAVA_HOME%/bin/keytool -genkeypair -alias mykey -keyalg EC -dname "cn=CN, ou=OU, o=O, c=C" -validity 365 -storetype pkcs12 -keystore keystore.pkx -keypass mykeypassword -storepass mystorepassword -v
    
    (translated)
    Warning: No support for different keystore and key password for PKCS12 keystores. The value of -keypass will be ignored.
    Generating keypair (Type EC, 256 Bit) and self signed certificate (SHA256withECDSA) with a validity of 365 Days
            für: CN=CN, OU=OU, O=O, C=C
    [keystore.pkx saved]
    

    Now list the contents:

    %JAVA_HOME%/bin/keytool -list -keystore keystore.pkx -storepass mystorepassword
    
    (translated)
    Keystore-Type: JKS // ??
    Keystore-Provider: SUN
    
    Keystore contains 1 entry
    
    mykey, 25.04.2017, PrivateKeyEntry,
    Certificate Fingerprint (SHA1): EA:C2:36:C6:55:69:CB:32:22:C7:14:83:67:47:D2:7E:06:8E:13:14
    
    0 讨论(0)
  • 2021-01-31 19:38

    If the keystore is PKCS12 type (.pfx) you have to specify it with -storetype PKCS12 (line breaks added for readability):

    keytool -genkey -alias <desired certificate alias> 
        -keystore <path to keystore.pfx>
        -storetype PKCS12 
        -keyalg RSA 
        -storepass <password> 
        -validity 730 
        -keysize 2048 
    
    0 讨论(0)
提交回复
热议问题