我有一个带有自签名证书的服务器,但也需要客户端证书身份验证。我正在尝试获取原始CA服务器证书,因此我可以将其导入密钥库。任何人都有一些关于如何轻松做到这一点的建议?感谢。
答案 0 :(得分:76)
正在研究如何在使用jenkins cli时信任证书,并找到了 https://issues.jenkins-ci.org/browse/JENKINS-12629有一些配方。
这将为您提供证书:
openssl s_client -connect ${HOST}:${PORT} </dev/null
如果您只对证书部分感兴趣,请将其剪切为:
| sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
并重定向到文件:
> ${HOST}.cert
然后使用keytool导入它:
keytool -import -noprompt -trustcacerts -alias ${HOST} -file ${HOST}.cert \
-keystore ${KEYSTOREFILE} -storepass ${KEYSTOREPASS}
一气呵成:
HOST=myhost.example.com
PORT=443
KEYSTOREFILE=dest_keystore
KEYSTOREPASS=changeme
# get the SSL certificate
openssl s_client -connect ${HOST}:${PORT} </dev/null \
| sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ${HOST}.cert
# create a keystore and import certificate
keytool -import -noprompt -trustcacerts \
-alias ${HOST} -file ${HOST}.cert \
-keystore ${KEYSTOREFILE} -storepass ${KEYSTOREPASS}
# verify we've got it.
keytool -list -v -keystore ${KEYSTOREFILE} -storepass ${KEYSTOREPASS} -alias ${HOST}
答案 1 :(得分:21)
我发现有几种方法可以做到这一点:
java InstallCert [host]:[port] keytool -exportcert -keystore jssecacerts -storepass changeit -file output.cert keytool -importcert -keystore [DESTINATION_KEYSTORE] -file output.cert
答案 2 :(得分:6)
我使用openssl,但是如果您不喜欢使用opensl,或者在没有它的系统上(尤其是Windows),自2011年Java 7 keytool
起,您就可以完成整个工作:
keytool -printcert -sslserver host[:port] -rfc >tempfile
keytool -import [-noprompt] -alias nm -keystore file [-storepass pw] [-storetype ty] <tempfile
# or with noprompt and storepass (so nothing on stdin besides the cert) piping works:
keytool -printcert -sslserver host[:port] -rfc | keytool -import -noprompt -alias nm -keystore file -storepass pw [-storetype ty]
相反,对于Java 9 up up,在许多情况下,对于早期版本,Java可以将PKCS12文件用作密钥库,而不是传统的JKS文件,并且 OpenSSL可以创建PKCS12 而无需任何操作来自keytool的帮助:
openssl s_client -connect host:port </dev/null | openssl pkcs12 -export -nokeys [-name nm] [-passout option] -out p12file
# <NUL on Windows
# default is to prompt for password, but -passout supports several options
# including actual value, envvar, or file; see the openssl(1ssl) man page
答案 3 :(得分:4)
答案 4 :(得分:1)
只需公开dnozay对函数的回答,以便我们可以同时导入多个证书。
#!/usr/bin/env sh
KEYSTORE_FILE=dest_keystore
KEYSTORE_PASS=changeit
import_cert() {
local HOST=$1
local PORT=$2
# get the SSL certificate
openssl s_client -connect ${HOST}:${PORT} </dev/null | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > ${HOST}.cert
# delete the old alias and then import the new one
keytool -delete -keystore ${KEYSTORE_FILE} -storepass ${KEYSTORE_PASS} -alias ${HOST} &> /dev/null
# create a keystore and import certificate
keytool -import -noprompt -trustcacerts \
-alias ${HOST} -file ${HOST}.cert \
-keystore ${KEYSTORE_FILE} -storepass ${KEYSTORE_PASS}
rm ${HOST}.cert
}
import_cert stackoverflow.com 443
import_cert www.google.com 443
import_cert 172.217.194.104 443 # google