Ich habe einen Server mit einem selbstsignierten Zertifikat, erfordert aber auch eine clientseitige Zertifizierungsauthentifizierung. Ich habe eine schwierige Zeit, um das rohe CA-Serverzertifikat zu erhalten, damit ich es in einen Keystore importieren kann. Hat jemand ein paar Vorschläge, wie das einfach geht? Vielen Dank.
War dabei, wie man einem Zertifikat vertrauen kann, während er jenkins cli verwendet, und fand https://issues.jenkins-ci.org/browse/JENKINS-12629 , das dafür einiges hat.
Dadurch erhalten Sie das Zertifikat:
openssl s_client -connect ${Host}:${PORT} </dev/null
wenn Sie nur an dem Zertifikatsteil interessiert sind, schneiden Sie es aus, indem Sie es an Folgendes leiten:
| sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p'
und umleiten zu einer Datei:
> ${Host}.cert
Dann importiere es mit keytool:
keytool -import -noprompt -trustcacerts -alias ${Host} -file ${Host}.cert \
-keystore ${KEYSTOREFILE} -storepass ${KEYSTOREPASS}
Auf einmal:
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}
Es gab ein paar Möglichkeiten, dies zu tun:
Java InstallCert [Host]: [Anschluss] keytool -exportcert -keystore jssecacerts -storepass change -file output.cert keytool -importcert -keystore [DESTINATION_KEYSTORE] -Datei output.cert
Sie können ein Zertifikat mit Firefox exportieren, diese Site enthält Anweisungen. Anschließend fügen Sie das Zertifikat mit keytool hinzu.
Ich benutze openssl, aber wenn Sie es vorziehen, auf einem System (insbesondere Windows) zu sein, das es nicht hat, da Java 7 2011 keytool
die ganze Arbeit erledigen kann :
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]
Umgekehrt kann Java für Java 9 immer und für frühere Versionen in vielen Fällen eine PKCS12-Datei für einen Keystore anstelle der herkömmlichen JKS-Datei verwenden, und OpenSSL kann ein PKCS12 ohne Hilfe von keytool erstellen:
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
Setzen Sie einfach die Antwort von dnozay auf eine Funktion, damit wir mehrere Zertifikate gleichzeitig importieren können.
#!/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