54

我有一个带有自签名证书的服务器,但也需要客户端证书身份验证。我在尝试获取原始 CA 服务器证书时遇到了困难,因此我可以将其导入密钥库。有人对如何轻松做到这一点有一些建议吗?谢谢。

4

5 回答 5

97

正在研究如何在使用 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}
于 2014-04-18T20:56:45.393 回答
27

我使用 openssl,但如果您不喜欢,或者在没有它的系统(尤其是 Windows)上,因为 2011 年的 java 7keytool可以完成整个工作

 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,以及在许多情况下的早期版本,Java 可以使用 PKCS12 文件而不是传统的 JKS 文件作为密钥库, OpenSSL 可以在没有 keytool 帮助的情况下创建 PKCS12 :

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 
于 2018-12-04T17:34:08.697 回答
23

我发现有几种方法可以做到这一点:

    java InstallCert [主机]:[端口]
    keytool -exportcert -keystore jssecacerts -storepass changeit -file output.cert
    keytool -importcert -keystore [DESTINATION_KEYSTORE] -file output.cert
于 2010-09-10T15:41:01.843 回答
4

您可以使用 Firefox 导出证书,此站点有说明。然后使用keytool添加证书。

于 2010-09-10T14:50:19.600 回答
4

只需将dnozay的答案暴露给一个函数,以便我们可以同时导入多个证书。

将其保存到 .sh 文件中,然后运行它。

#!/usr/bin/env sh

KEYSTORE_FILE=/path/to/keystore.jks
KEYSTORE_PASS=changeit


import_cert() {
  local HOST=$1
  local PORT=$2

  if [[ -z $PORT ]]; then
    PORT=443
  fi

  # 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 (or update) and import certificate
  keytool -import -noprompt -trustcacerts \
      -alias ${HOST} -file ${HOST}.cert \
      -keystore ${KEYSTORE_FILE} -storepass ${KEYSTORE_PASS}

  # remove temp file
  rm ${HOST}.cert
}

# Change your sites here
import_cert stackoverflow.com 443
import_cert www.google.com # default port 443
import_cert 172.217.194.104 443 # google
于 2018-12-04T16:47:04.850 回答