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

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

I have this in an ActiveMQ config:


        

        
15条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 08:26

    I used the following two steps which I found in the comments/posts linked in the other answers:

    Step one: Convert the x.509 cert and key to a pkcs12 file

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

    Note: Make sure you put a password on the pkcs12 file - otherwise you'll get a null pointer exception when you try to import it. (In case anyone else had this headache). (Thanks jocull!)

    Note 2: You might want to add the -chain option to preserve the full certificate chain. (Thanks Mafuba)

    Step two: Convert the pkcs12 file to a Java keystore

    keytool -importkeystore \
            -deststorepass [changeit] -destkeypass [changeit] -destkeystore server.keystore \
            -srckeystore server.p12 -srcstoretype PKCS12 -srcstorepass some-password \
            -alias [some-alias]
    

    Finished

    OPTIONAL Step zero: Create self-signed certificate

    openssl genrsa -out server.key 2048
    openssl req -new -out server.csr -key server.key
    openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
    

    Cheers!

提交回复
热议问题