How to import an existing X.509 certificate and private key in Java keystore to use in SSL?

前端 未结 15 860
说谎
说谎 2020-11-22 08:05

I have this in an ActiveMQ config:


        

        
相关标签:
15条回答
  • 2020-11-22 08:27

    If you have a PEM file (e.g. server.pem) containing:

    • the trusted certificate
    • the private key

    then you can import the certificate and key into a JKS keystore like this:

    1) Copy the private key from the PEM file into an ascii file (e.g. server.key)

    2) Copy the cert from the PEM file into an ascii file (e.g. server.crt)

    3) Export the cert and key into a PKCS12 file:

    $ openssl pkcs12 -export -in server.crt -inkey server.key \
                     -out server.p12 -name [some-alias] -CAfile server.pem -caname root
    
    • the PEM file can be used as the argument to the -CAfile option.
    • you are prompted for an 'export' password.
    • if doing this in git bash then add winpty to the start of the command so the export password can be entered.

    4) Convert the PKCS12 file to a JKS keystore:

    $ keytool -importkeystore -deststorepass changeit -destkeypass changeit \
              -destkeystore keystore.jks  -srckeystore server.p12 -srcstoretype PKCS12 \
              -srcstorepass changeit
    
    • the srcstorepass password should match the export password from step 3)
    0 讨论(0)
  • 2020-11-22 08:28

    in a case of Elliptic Curve and answer the question import an existing x509 certificate and private key in Java keystore, you may want to have a look also to this thread How to read EC Private key in java which is in .pem file format

    0 讨论(0)
  • 2020-11-22 08:30

    First convert to p12:

    openssl pkcs12 -export -in [filename-certificate] -inkey [filename-key] -name [host] -out [filename-new-PKCS-12.p12]
    

    Create new JKS from p12:

    keytool -importkeystore -deststorepass [password] -destkeystore [filename-new-keystore.jks] -srckeystore [filename-new-PKCS-12.p12] -srcstoretype PKCS12
    
    0 讨论(0)
  • 2020-11-22 08:30

    Just make a PKCS12 keystore, Java can use it directly now. In fact, if you list a Java-style keystore, keytool itself alerts you to the fact that PKCS12 is now the preferred format.

    openssl pkcs12 -export -in server.crt -inkey server.key \
                   -out server.p12 -name [some-alias] \
                   -CAfile ca.crt -caname root -chain
    

    You should have received all three files (server.crt, server.key, ca.crt) from your certificate provider. I am not sure what "-caname root" actually means, but it seems to have to be specified that way.

    In the Java code, make sure to specify the right keystore type.

    KeyStore.getInstance("PKCS12")
    

    I got my comodo.com-issued SSL certificate working fine in NanoHTTPD this way.

    0 讨论(0)
  • 2020-11-22 08:31

    Believe or not, keytool does not provide such basic functionality like importing private key to keystore. You can try this workaround with merging PKSC12 file with private key to a keystore:

    keytool -importkeystore \
      -deststorepass storepassword \
      -destkeypass keypassword \
      -destkeystore my-keystore.jks \
      -srckeystore cert-and-key.p12 \
      -srcstoretype PKCS12 \
      -srcstorepass p12password \
      -alias 1
    

    Or just use more user-friendly KeyMan from IBM for keystore handling instead of keytool.

    0 讨论(0)
  • 2020-11-22 08:31

    Previous answers point out correctly that you can only do this with the standard JDK tools by converting the JKS file into PKCS #12 format first. If you're interested, I put together a compact utility to import OpenSSL-derived keys into a JKS-formatted keystore without having to convert the keystore to PKCS #12 first: http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art049

    You would use the linked utility like this:

    $ openssl req -x509 -newkey rsa:2048 -keyout localhost.key -out localhost.csr -subj "/CN=localhost"
    

    (sign the CSR, get back localhost.cer)

    $ openssl rsa -in localhost.key -out localhost.rsa
    Enter pass phrase for localhost.key:
    writing RSA key
    $ java -classpath . KeyImport -keyFile localhost.rsa -alias localhost -certificateFile localhost.cer -keystore localhost.jks -keystorePassword changeit -keystoreType JKS -keyPassword changeit
    
    0 讨论(0)
提交回复
热议问题