简体   繁体   English

Java keytool不喜欢OpenSSL CSR

[英]Java keytool doesn't like OpenSSL CSR

I'm trying to use OpenSSL to create a self-signed SSL certificate and then add that certificate to a JKS file (Java keystore) so I can have a Jetty-based web service serve that self-signed certificate to HTTP clients over HTTPS. 我正在尝试使用OpenSSL创建自签名SSL证书,然后将该证书添加到JKS文件(Java密钥库)中,这样我就可以让基于Jetty的Web服务通过HTTPS向HTTP客户端提供该自签名证书。

I created the OpenSSL self-signed cert: 我创建了OpenSSL自签名证书:

openssl req -x509 -newkey rsa:4096 -keyout mykey-dev.pem -out mycsr-dev.pem -days 3650

I then created the JKS: 然后,我创建了JKS:

keytool -alias myorg -keyalg RSA -keystore myapp.jks -keysize 2048

I believe I now need to import the CSR ( mycsr-dev.pem ) into the JKS: 相信我现在需要将CSR( mycsr-dev.pem )导入到JKS中:

keytool -importcert -trustcacerts -file mycsr-dev.pem -alias myorg -keystore myapp.jks

This produces the following error: 这将产生以下错误:

keytool error: java.lang.Exception: Public keys in reply and keystore don't match

Any idea what the problem is? 知道是什么问题吗?

openssl create PEM format file, while keytool will jks format. openssl创建PEM格式文件,而keytool将jks格式。

this is how to convert certificate from pem to jks: 这是将证书从pem转换为jks的方法:

cat cert_public_key.pem cert_private_key.pem | 猫cert_public_key.pem cert_private_key.pem | openssl pkcs12 -export -out cert.p12 openssl pkcs12 -export -out cert.p12

keytool -importkeystore -srckeystore cert.p12 -srcstoretype pkcs12 -destkeystore cert.jks keytool -importkeystore -srckeystore cert.p12 -srcstoretype pkcs12 -destkeystore cert.jks

The problem occurred because keytool genkey -alias myorg ... created a keypair and the openssl req command also creates an unrelated keypair. 发生问题是因为keytool genkey -alias myorg ...创建了一个密钥对,而openssl req命令也创建了一个不相关的密钥对。 Trying to import the cert from the openssl req command into the JKS keystore under the myorg alias therefore causes a conflict between the two different public keys. 因此,尝试将openssl req命令中的cert导入myorg别名下的JKS密钥库中会导致两个不同的公共密钥之间发生冲突。 If you intend to import a trusted certificate into the keystore then simply do the import under the desired alias, there is no need to create the alias ahead of time with keytool genkey ... . 如果您打算将受信任的证书导入密钥库,则只需在所需别名下进行导入,则无需使用keytool genkey ...提前创建别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM