如何从openssl生成的密钥生成X.509证书

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何从openssl生成的密钥生成X.509证书相关的知识,希望对你有一定的参考价值。

我有一个在ec2-instance上运行的Web服务器,它在内部调用使用Spring Boot构建的REST服务器。现在,我试图让这个REST服务器在SSL下运行。这是我到目前为止所做的:

1)使用此命令创建CSR和密钥文件

openssl req -newkey rsa:2048 -nodes -keyout mydomain.key -out mydomain.csr

2)Copied 'csr' to get SSL certificate from GoDaddy.

3)在我的ec2-instance上成功安装了nginx下的证书。

4)当我点击https下的主页时,它可以工作。我不再从浏览器收到“不安全”消息。

5)登录失败,因为它进行了REST调用但是REST服务器没有在SSL下运行,所以我试图让它在SSL下运行。

6)遵循以下命令:

keytool -import -alias mydomain -keystore tomcat.keystore -trustcacerts -file mydomain.com.chained.crt
keytool -import -alias mydomain-key -keystore tomcat.keystore -trustcacerts -file mydomain.key

上一个命令给出了一条错误消息:“keytool error:java.lang.Exception:输入不是X.509证书”

但这是在上面的步骤1中创建的那个&同一文件在Nginx下工作。我缺少什么(除了我对设置SSL很少知道的事实!)?我相信,我需要第二个命令在application.properties中指定'server.ssl.keyAlias'的值。

答案

不是一个答案,而是满溢的评论。

您不需要“生成”X.509证书;你已经从GoDaddy获得了这个。如果(且仅当)SpringBoot服务器使用与(外部)nginx相同的名称访问 - 我不清楚 - 您需要将私钥和证书CHAIN对从PEM格式转换为格式Java使用。看到: How to import an existing x509 certificate and private key in Java keystore to use in SSL? How can I set up a letsencrypt SSL certificate and use it in a Spring Boot application? How to use .key and .crt file in java that generated by openssl? Importing the private-key/public-certificate pair in the Java KeyStore 也许Import key and SSL Certificate into java keystore

另一答案

谢谢@ Dave_thompson_085。遵循2个命令就可以了!

openssl pkcs12 -export -in mydomain.com.chained.crt -inkey mydomain.key -out keystore.p12 -name my-alias -caname root

keytool -importkeystore -deststorepass mypassword -destkeypass mypassword -destkeystore keystore.jks -srckeystore keystore.p12 -srcstoretype PKCS12 -srcstorepass mypassword -alias my-alias

然后在application.properties中我指定了以下属性:

server.port=8443
server.ssl.enabled=true
security.require-ssl=true
server.ssl.key-store=/etc/nginx/ssl/keystore.jks
server.ssl.key-store-password=mypassword
server.ssl.keyStoreType=JKS
server.ssl.keyAlias=my-alias

以上是关于如何从openssl生成的密钥生成X.509证书的主要内容,如果未能解决你的问题,请参考以下文章

使用 openssl 生成证书

使用 openssl 生成证书

使用 openssl 生成证书

用openssl.exe 来生成证书

联盟链系列 - 用Openssl颁发X.509证书

linux下生成https的crt和key证书