-
Notifications
You must be signed in to change notification settings - Fork 819
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update SCRAM dependency to 3.0 and support channel binding
Signed-off-by: Jorge Solórzano <jorsol@gmail.com>
- Loading branch information
Showing
6 changed files
with
199 additions
and
236 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
pgjdbc/src/main/java/org/postgresql/core/v3/ScramAuthenticator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/* | ||
* Copyright (c) 2017, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.core.v3; | ||
|
||
import org.postgresql.core.PGStream; | ||
import org.postgresql.util.GT; | ||
import org.postgresql.util.PSQLException; | ||
import org.postgresql.util.PSQLState; | ||
|
||
import com.ongres.scram.client.ScramClient; | ||
import com.ongres.scram.common.ClientFinalMessage; | ||
import com.ongres.scram.common.ClientFirstMessage; | ||
import com.ongres.scram.common.StringPreparation; | ||
import com.ongres.scram.common.exception.ScramException; | ||
import com.ongres.scram.common.util.TlsServerEndpoint; | ||
|
||
import java.io.IOException; | ||
import java.net.Socket; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateEncodingException; | ||
import java.security.cert.X509Certificate; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
|
||
import javax.net.ssl.SSLException; | ||
import javax.net.ssl.SSLSession; | ||
import javax.net.ssl.SSLSocket; | ||
|
||
final class ScramAuthenticator { | ||
private static final Logger LOGGER = Logger.getLogger(ScramAuthenticator.class.getName()); | ||
|
||
private final PGStream pgStream; | ||
private final ScramClient scramClient; | ||
|
||
ScramAuthenticator(char[] password, PGStream pgStream) throws PSQLException { | ||
this.pgStream = pgStream; | ||
this.scramClient = initializeScramClient(password, pgStream); | ||
} | ||
|
||
private static ScramClient initializeScramClient(char[] password, PGStream stream) throws PSQLException { | ||
try { | ||
final List<String> advertisedMechanisms = advertisedMechanisms(stream); | ||
final byte[] cbindData = extractChannelBindingData(stream); | ||
|
||
ScramClient client = ScramClient.builder() | ||
.advertisedMechanisms(advertisedMechanisms) | ||
.username("*") // username is ignored by server, startup message is used instead | ||
.password(password) | ||
.channelBinding(TlsServerEndpoint.TLS_SERVER_END_POINT, cbindData) | ||
.stringPreparation(StringPreparation.POSTGRESQL_PREPARATION) | ||
.build(); | ||
|
||
if (LOGGER.isLoggable(Level.FINEST)) { | ||
LOGGER.log(Level.FINEST, " Using SCRAM mechanism: {0}", | ||
client.getScramMechanism().getName()); | ||
} | ||
return client; | ||
} catch (IllegalArgumentException | IOException e) { | ||
throw new PSQLException( | ||
GT.tr("Invalid SCRAM client initialization", e), | ||
PSQLState.CONNECTION_REJECTED); | ||
} | ||
} | ||
|
||
private static List<String> advertisedMechanisms(PGStream stream) throws PSQLException, IOException { | ||
List<String> mechanisms = new ArrayList<>(); | ||
do { | ||
mechanisms.add(stream.receiveString()); | ||
} while (stream.peekChar() != 0); | ||
int c = stream.receiveChar(); | ||
assert c == 0; | ||
if (mechanisms.isEmpty()) { | ||
throw new PSQLException( | ||
GT.tr("Received AuthenticationSASL message with 0 mechanisms!"), | ||
PSQLState.CONNECTION_REJECTED); | ||
} | ||
LOGGER.log(Level.FINEST, " <=BE AuthenticationSASL( {0} )", mechanisms); | ||
return mechanisms; | ||
} | ||
|
||
private static byte[] extractChannelBindingData(PGStream stream) { | ||
Socket socket = stream.getSocket(); | ||
if (socket instanceof SSLSocket) { | ||
SSLSession session = ((SSLSocket) socket).getSession(); | ||
try { | ||
Certificate[] certificates = session.getPeerCertificates(); | ||
if (certificates != null && certificates.length > 0) { | ||
Certificate peerCert = certificates[0]; // First certificate is the peer's certificate | ||
if (peerCert instanceof X509Certificate) { | ||
X509Certificate cert = (X509Certificate) peerCert; | ||
return TlsServerEndpoint.getChannelBindingData(cert); | ||
} | ||
} | ||
} catch (CertificateEncodingException | SSLException e) { | ||
if (LOGGER.isLoggable(Level.FINEST)) { | ||
LOGGER.log(Level.FINEST, "Error extracting channel binding data", e); | ||
} | ||
} | ||
} | ||
return new byte[0]; | ||
} | ||
|
||
void handleAuthenticationSASL() throws IOException { | ||
ClientFirstMessage clientFirstMessage = scramClient.clientFirstMessage(); | ||
LOGGER.log(Level.FINEST, " FE=> SASLInitialResponse( {0} )", clientFirstMessage); | ||
String scramMechanismName = scramClient.getScramMechanism().getName(); | ||
final byte[] scramMechanismNameBytes = scramMechanismName.getBytes(StandardCharsets.UTF_8); | ||
final byte[] clientFirstMessageBytes = | ||
clientFirstMessage.toString().getBytes(StandardCharsets.UTF_8); | ||
sendAuthenticationMessage( | ||
(scramMechanismNameBytes.length + 1) + 4 + clientFirstMessageBytes.length, | ||
pgStream -> { | ||
pgStream.send(scramMechanismNameBytes); | ||
pgStream.sendChar(0); // List terminated in '\0' | ||
pgStream.sendInteger4(clientFirstMessageBytes.length); | ||
pgStream.send(clientFirstMessageBytes); | ||
}); | ||
} | ||
|
||
void handleAuthenticationSASLContinue(int length) throws IOException, PSQLException { | ||
String receivedServerFirstMessage = pgStream.receiveString(length); | ||
LOGGER.log(Level.FINEST, " <=BE AuthenticationSASLContinue( {0} )", receivedServerFirstMessage); | ||
try { | ||
scramClient.serverFirstMessage(receivedServerFirstMessage); | ||
} catch (ScramException | IllegalStateException | IllegalArgumentException e) { | ||
throw new PSQLException( | ||
GT.tr("SCRAM authentication failed: {0}", e.getMessage()), | ||
PSQLState.CONNECTION_REJECTED, | ||
e); | ||
} | ||
|
||
ClientFinalMessage clientFinalMessage = scramClient.clientFinalMessage(); | ||
LOGGER.log(Level.FINEST, " FE=> SASLResponse( {0} )", clientFinalMessage); | ||
final byte[] clientFinalMessageBytes = | ||
clientFinalMessage.toString().getBytes(StandardCharsets.UTF_8); | ||
sendAuthenticationMessage( | ||
clientFinalMessageBytes.length, | ||
pgStream -> pgStream.send(clientFinalMessageBytes) | ||
); | ||
} | ||
|
||
void handleAuthenticationSASLFinal(int length) throws IOException, PSQLException { | ||
String serverFinalMessage = pgStream.receiveString(length); | ||
LOGGER.log(Level.FINEST, " <=BE AuthenticationSASLFinal( {0} )", serverFinalMessage); | ||
try { | ||
scramClient.serverFinalMessage(serverFinalMessage); | ||
} catch (ScramException | IllegalStateException | IllegalArgumentException e) { | ||
throw new PSQLException( | ||
GT.tr("SCRAM authentication failed: {0}", e.getMessage()), | ||
PSQLState.CONNECTION_REJECTED, | ||
e); | ||
} | ||
} | ||
|
||
private interface BodySender { | ||
void sendBody(PGStream pgStream) throws IOException; | ||
} | ||
|
||
private void sendAuthenticationMessage(int bodyLength, BodySender bodySender) | ||
throws IOException { | ||
pgStream.sendChar('p'); | ||
pgStream.sendInteger4(Integer.BYTES + bodyLength); | ||
bodySender.sendBody(pgStream); | ||
pgStream.flush(); | ||
} | ||
} |
Oops, something went wrong.