X509 SSL: No name matching <hostname> found

Viewed 1218

I am trying to configure Kafka with SSL using openssl and keytool command. I configured the certificates to accept a specific CN and some alternative names; here is what I did:

openssl req -x509 -nodes -newkey rsa:2048 -days 3650 -sha256 -keyout ca-key -out ca-cert -reqexts SAN -extensions SAN -subj '/CN=kafkabroker' -config <(cat /etc/ssl/openssl.cnf; printf "[SAN]\nsubjectAltName=DNS:kafkabroker,DNS:kafka-broker,DNS:localhost,DNS:host.docker.internal,IP:127.0.0.1,IP:1.1.1.1, IP:2.2.2.2")


keytool -keystore kafka.server.keystore.jks -alias kafkabroker -validity 365 -genkey -storepass passw -keypass passw -ext SAN=DNS:kafkabroker,DNS:localhost,IP:1.1.1.1,DNS:juliet,DNS:host.docker.internal,IP:2.2.2.2,IP:127.0.0.1


keytool -keystore kafka.client.truststore.jks -alias CARoot -import -file ca-cert
keytool -keystore kafka.server.keystore.jks -alias kafkabroker -certreq -file cert-file
openssl x509 -req -CA ca-cert -CAkey ca-key -in cert-file -out cert-signed -days 3650 -CAcreateserial -passin pass:passw 
keytool -keystore kafka.server.keystore.jks -alias CARoot -import -file ca-cert
keytool -keystore kafka.server.keystore.jks -alias kafkabroker -import -file cert-signed
keytool -keystore kafka.client.keystore.jks -alias kafkabroker -validity 365 -genkey -storepass passw -keypass passw -ext SAN=DNS:kafkabroker,DNS:localhost,IP:1.1.1.1,DNS:juliet,DNS:host.docker.internal,IP:2.2.2.2,IP:127.0.0.1
keytool -keystore kafka.client.keystore.jks -alias kafkabroker -certreq -file cert-file
openssl x509 -req -CA ca-cert -CAkey ca-key -in cert-file -out cert-signed -days 3650 -CAcreateserial -passin pass:passw 
keytool -keystore kafka.client.keystore.jks -alias CARoot -import -file ca-cert
keytool -keystore kafka.client.keystore.jks -alias kafkabroker -import -file cert-signed

keytool -keystore kafka.server.truststore.jks -alias CARoot -import -file ca-cert

In a local environment, having the hostname "kafkabroker" it works but when i run kafka with the same certificates on another environment which has an IP configured during the certificate creation, it says:

java.security.cert.CertificateException: No subject alternative names present

I don't understand why. I checked the jks files which are the truststore and keystore and I can see cleary the section "alternative names" fully populated with the above names and IP addresses.

1 Answers

The main problem, that kafka requires SAN in signed SERVER certificate (which is in server.keystore.jks). As I can see, in your request section you do not have SAN names added:

openssl x509 -req -CA ca-cert -CAkey ca-key -in cert-file -out cert-signed -days 3650 -CAcreateserial -passin pass:passw -extfile openssl-sign.cnf -extensions server_cert

Be ready to add/edit your /etc/pki/tls/openssl.cnf (CentOS/RHEL catalog) or /etc/ssl/openssl.cnf (others) - server_cert (or analogue) section. Another way is to create your own copy of openssl.cnf as shown in my example (file openssl-sigh.cnf). server_cert section can be something like this (anywhere in cnf file):

[ server_cert ]
# Extensions for server certificates (`man x509v3_config`).
basicConstraints = CA:FALSE
nsCertType = server
nsComment = "Openssl Server generated Certificate"
subjectKeyIdentifier = hash
authorityKeyIdentifier = keyid,issuer:always
keyUsage = critical, digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth,clientAuth
subjectAltName = DNS:kafkabroker,DNS:localhost,IP:1.1.1.1,DNS:juliet,DNS:host.docker.internal,IP:2.2.2.2,IP:127.0.0.1

Then check your results by:

openssl req -text -noout -verify -in cert-file
openssl x509 -text -noout -in cert-signed

And you should see SAN text lines in x509 extensions.

I'm not sure about the client.keystore - you create another pair with another certs with the same names. Try first server.keystore.jks check.

Related