diff --git a/src/interfaces/libpq/fe-secure-openssl.c b/src/interfaces/libpq/fe-secure-openssl.c index 00d43f3eff..67feaedc4e 100644 --- a/src/interfaces/libpq/fe-secure-openssl.c +++ b/src/interfaces/libpq/fe-secure-openssl.c @@ -1087,20 +1087,24 @@ initialize_SSL(PGconn *conn) * Per RFC 6066, do not set it if the host is a literal IP address (IPv4 * or IPv6). */ - if (conn->sslsni && conn->sslsni[0] && - !(strspn(conn->pghost, "0123456789.") == strlen(conn->pghost) || - strchr(conn->pghost, ':'))) + if (conn->sslsni && conn->sslsni[0]) { - if (SSL_set_tlsext_host_name(conn->ssl, conn->pghost) != 1) - { - char *err = SSLerrmessage(ERR_get_error()); + const char *host = conn->connhost[conn->whichhost].host; - appendPQExpBuffer(&conn->errorMessage, - libpq_gettext("could not set SSL Server Name Indication (SNI): %s\n"), - err); - SSLerrfree(err); - SSL_CTX_free(SSL_context); - return -1; + if (host && host[0] && + !(strspn(host, "0123456789.") == strlen(host) || + strchr(host, ':'))) + { + if (SSL_set_tlsext_host_name(conn->ssl, host) != 1) + { + char *err = SSLerrmessage(ERR_get_error()); + + appendPQExpBuffer(&conn->errorMessage, + libpq_gettext("could not set SSL Server Name Indication (SNI): %s\n"), + err); + SSLerrfree(err); + return -1; + } } }