JavaKeyTool从url/端口添加服务器证书的简单方法

JavaKeyTool从url/端口添加服务器证书的简单方法,java,ssl-certificate,keytool,Java,Ssl Certificate,Keytool,我有一个具有自签名证书的服务器,但也需要客户端证书身份验证。我在尝试获取原始CA服务器证书以便将其导入密钥库时遇到了困难。有人对如何轻松做到这一点有什么建议吗?谢谢。您可以使用Firefox导出证书,并提供相关说明。然后使用添加证书。我发现有几种方法可以做到这一点: Firefox:添加异常->获取证书->查看->详细信息->导出 KeyMan()您可以直接从文件->导入菜单获取SSL证书 InstallCert() java InstallCert[主机]:[端口] keytool-exp

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

您可以使用Firefox导出证书,并提供相关说明。然后使用添加证书。

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

  • Firefox:添加异常->获取证书->查看->详细信息->导出
  • KeyMan()您可以直接从文件->导入菜单获取SSL证书
  • InstallCert()
java InstallCert[主机]:[端口] keytool-exportcert-keystore jssecacerts-storepass changeit-file output.cert keytool-importcert-keystore[DESTINATION_keystore]-file output.cert
正在研究如何在使用jenkins cli时信任证书,发现 这有一些诀窍

这将为您提供以下证书:

openssl s_client -connect ${HOST}:${PORT} </dev/null
并重定向到一个文件:

> ${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}
HOST=myhost.example.com
端口=443
KEYSTOREFILE=dest_密钥库
keystrepass=changeme
#获取SSL证书
openssl s_客户端-连接${HOST}:${PORT}${HOST}.cert
#创建密钥库并导入证书
keytool-import-noprompt-trustcacerts\
-别名${HOST}-文件${HOST}.cert\
-keystore${KEYSTOREFILE}-storepass${KEYSTOREPASS}
#确认我们找到了。
keytool-list-v-keystore${KEYSTOREFILE}-storepass${KEYSTOREPASS}-alias${HOST}
只需将的答案公开给一个函数,这样我们就可以同时导入多个证书

将其保存到.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
#/副秘书长/垃圾箱/环境卫生
KEYSTORE_FILE=/path/to/KEYSTORE.jks
KEYSTORE_PASS=changeit
导入证书(){
本地主机=$1
本地端口=$2
如果[[-z$PORT]];则
端口=443
fi
#获取SSL证书
openssl s_客户端-连接${HOST}:${PORT}${HOST}.cert
#删除旧别名,然后导入新别名
keytool-delete-keystore${keystore\u FILE}-storepass${keystore\u PASS}-alias${HOST}&>/dev/null
#创建密钥库(或更新)并导入证书
keytool-import-noprompt-trustcacerts\
-别名${HOST}-文件${HOST}.cert\
-密钥库${keystore\u FILE}-storepass${keystore\u PASS}
#删除临时文件
rm${HOST}.cert
}
#在此处更改您的站点
导入证书stackoverflow.com 443
导入证书www.google.com默认端口443
导入证书172.217.194.104 443#谷歌

我使用openssl,但是如果您不喜欢,或者您所在的系统(尤其是Windows)没有openssl,因为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]
keytool-printcert-sslserver主机[:端口]-rfc>tempfile

keytool-import[-noprompt]-alias nm-keystore file[-storepass pw][-storetype ty]我以前也使用过Andreas Sterbenz的InstallCert类,如果您需要一个不接受HTTP GET请求的主机的证书,它会很有用Hanks Wunte,这对我很有帮助,因为我这里有库存::D tyCan I put host=*.example.com,KEYSTOREPASS=changeit不是'changeme',从2012年的j7开始,keytool(通常是CertificateFactory)会忽略证书的PEM文件中的无关文本,因此您不需要sed。另外,使用
-noprompt
-storepass
您可以不使用临时文件进行管道传输:
openssl s_客户端-连接主机:port | keytool-import-noprompt-alias nm-keystore文件-storepass pw
非常好!谢谢
#!/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
 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]
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