-
Notifications
You must be signed in to change notification settings - Fork 6
Feature: Mutual TLS (mTLS) support with client certificate authentication #62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
crazyrokr
wants to merge
12
commits into
JavaSaBr:develop
Choose a base branch
from
crazyrokr:mtls-support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1bab899
propagate connection close to client
crazyrokr 67589e2
implement mutual tls support
crazyrokr 2a22bcc
fix mutual tls flow
crazyrokr 29f606c
add mutual tls tests
crazyrokr 438425c
update javadocs
crazyrokr b6d4e2a
fix tls flow
crazyrokr c05edbd
apply formatting
crazyrokr 2b99385
remove unnecessary changes
crazyrokr 5ae68fd
Update comment
crazyrokr ea3f1ca
fix potential dead-loop
crazyrokr e01bf41
return SKIP_READ_PACKETS instead of total
crazyrokr 4bb44f3
improve StringSslNetworkTest
crazyrokr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
12 changes: 12 additions & 0 deletions
12
rlib-network/src/main/java/javasabr/rlib/network/exception/ConnectionClosedException.java
This file contains hidden or 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,12 @@ | ||
| package javasabr.rlib.network.exception; | ||
|
|
||
| public class ConnectionClosedException extends NetworkException { | ||
|
|
||
| public ConnectionClosedException(String remoteAddress) { | ||
| super("Connection closed: %s".formatted(remoteAddress)); | ||
| } | ||
|
|
||
| public ConnectionClosedException(String remoteAddress, Throwable cause) { | ||
| super("Connection closed: %s".formatted(remoteAddress), cause); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
28 changes: 28 additions & 0 deletions
28
rlib-network/src/main/java/javasabr/rlib/network/impl/StringDataMtlsServerConnection.java
This file contains hidden or 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,28 @@ | ||
| package javasabr.rlib.network.impl; | ||
|
|
||
| import javasabr.rlib.network.BufferAllocator; | ||
| import javasabr.rlib.network.Network; | ||
| import javasabr.rlib.network.packet.impl.StringReadableNetworkPacket; | ||
|
|
||
| import javax.net.ssl.SSLContext; | ||
| import java.nio.channels.AsynchronousSocketChannel; | ||
|
|
||
| /** | ||
| * @author crazyrokr | ||
| */ | ||
| public class StringDataMtlsServerConnection extends DefaultDataSslConnection<StringDataMtlsServerConnection> { | ||
|
|
||
| public StringDataMtlsServerConnection( | ||
| Network<StringDataMtlsServerConnection> network, | ||
| AsynchronousSocketChannel channel, | ||
| BufferAllocator bufferAllocator, | ||
| SSLContext sslContext) { | ||
| super(network, channel, bufferAllocator, sslContext, 100, 2, false); | ||
| sslEngine.setNeedClientAuth(true); | ||
| } | ||
|
|
||
| @Override | ||
| protected StringReadableNetworkPacket<StringDataMtlsServerConnection> createReadablePacket() { | ||
| return new StringReadableNetworkPacket<>(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -76,6 +76,7 @@ protected AbstractSslNetworkPacketReader( | |
| protected void handleReceivedData(int receivedBytes, ByteBuffer readingBuffer) { | ||
| if (receivedBytes == -1) { | ||
| doHandshake(sslNetworkBuffer(), -1); | ||
| connection.close(); | ||
| return; | ||
| } | ||
| super.handleReceivedData(receivedBytes, readingBuffer); | ||
|
|
@@ -159,6 +160,9 @@ protected int doHandshake(ByteBuffer networkBuffer, int receivedBytes) { | |
| case NEED_WRAP: { | ||
| log.debug(remoteAddress, "[%s] Send command to wrap data"::formatted); | ||
| packetWriter.accept(SslWrapRequestNetworkPacket.getInstance()); | ||
| if (networkBuffer.hasRemaining()) { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
| return decryptAndRead(networkBuffer); | ||
| } | ||
| NetworkUtils.cleanNetworkBuffer(networkBuffer); | ||
| return SKIP_READ_PACKETS; | ||
| } | ||
|
|
@@ -203,6 +207,10 @@ protected int decryptAndRead(ByteBuffer receivedBuffer) { | |
| } | ||
| switch (result.getStatus()) { | ||
| case OK: { | ||
| if (result.bytesConsumed() == 0 && result.bytesProduced() == 0) { | ||
| log.debug(remoteAddress, "[%s] No progress during decryption, stop processing"::formatted); | ||
| return SKIP_READ_PACKETS; | ||
| } | ||
| sslDataBuffer.flip(); | ||
| logDataAfterDecrypt(remoteAddress, sslDataBuffer); | ||
| total += readPackets(sslDataBuffer, sslDataPendingBuffer); | ||
|
|
||
This file contains hidden or 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
49 changes: 49 additions & 0 deletions
49
rlib-network/src/test/java/javasabr/rlib/network/ConnectionCloseTest.java
This file contains hidden or 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,49 @@ | ||
| package javasabr.rlib.network; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.TimeUnit; | ||
| import javasabr.rlib.network.exception.ConnectionClosedException; | ||
| import javasabr.rlib.network.impl.AbstractConnection; | ||
| import javasabr.rlib.network.impl.DefaultConnection; | ||
| import javasabr.rlib.network.packet.impl.DefaultReadableNetworkPacket; | ||
| import javasabr.rlib.network.packet.registry.ReadableNetworkPacketRegistry; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ConnectionCloseTest extends BaseNetworkTest { | ||
|
|
||
| @Test | ||
| void shouldPropagateConnectionCloseToClient() throws InterruptedException { | ||
| // given | ||
| var packetRegistry = ReadableNetworkPacketRegistry.of( | ||
| DefaultReadableNetworkPacket.class, | ||
| DefaultConnection.class, | ||
| DefaultNetworkTest.ServerPackets.RequestEchoMessage.class, | ||
| DefaultNetworkTest.ServerPackets.RequestServerTime.class); | ||
| var serverNetwork = NetworkFactory.defaultServerNetwork(packetRegistry); | ||
| InetSocketAddress serverAddress = serverNetwork.start(); | ||
| serverNetwork.onAccept(AbstractConnection::close); | ||
| var clientNetwork = NetworkFactory.defaultClientNetwork(packetRegistry); | ||
| CountDownLatch closeLatch = new CountDownLatch(1); | ||
|
|
||
| // when | ||
| clientNetwork | ||
| .connectReactive(serverAddress) | ||
| .flatMapMany(AbstractConnection::receivedEvents) | ||
| .doOnError(e -> { | ||
| if (e instanceof ConnectionClosedException) { | ||
| closeLatch.countDown(); | ||
| } | ||
| }) | ||
| .subscribe(); | ||
|
|
||
| // then | ||
| assertThat(closeLatch.await(5000, TimeUnit.MILLISECONDS)) | ||
| .as("Client should be notified that connection is closed") | ||
| .isTrue(); | ||
| clientNetwork.shutdown(); | ||
| serverNetwork.shutdown(); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.