Skip to content
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

Correctly handle optional client auth on the client #567

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ long[] handle(long ssl, byte[] keyTypeBytes, byte[][] asn1DerEncodedPrincipals,

try {
if (keyManager == null) {
engineMap.remove(ssl);
return null;
return new long[] { 0, 0 };

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there is a keyManager but it doesn't contain applicable valid key for the requested issuers (selectKeyMaterialClientSide below returning null)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch... fixing as we speak

}
if (engine.getUseClientMode()) {
final Set<String> keyTypesSet = supportedClientKeyTypes(keyTypeBytes);
Expand Down
5 changes: 5 additions & 0 deletions codec-native-quic/src/main/c/netty_quic_boringssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,11 @@ static int quic_certificate_cb(SSL* ssl, void* arg) {
SSL_set_ex_data(ssl, sslTaskIdx, NULL);
netty_boringssl_ssl_task_free(e, ssl_task);

if (pkey == NULL && cchain == NULL) {
// No key material found.
return 1;
}

int numCerts = sk_CRYPTO_BUFFER_num(cchain);
if (numCerts == 0) {
goto done;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.Future;
import io.netty.util.concurrent.ImmediateEventExecutor;
import io.netty.util.concurrent.ImmediateExecutor;
import org.hamcrest.CoreMatchers;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Timeout;
Expand Down Expand Up @@ -930,23 +929,46 @@ InsecureQuicTokenHandler.INSTANCE, new ChannelInboundHandlerAdapter(),

@ParameterizedTest
@MethodSource("newSslTaskExecutors")
public void testConnectMutualAuthSuccess(Executor executor) throws Throwable {
public void testConnectMutualAuthRequiredSuccess(Executor executor) throws Throwable {
testConnectMutualAuthSuccess(executor, MutalAuthTestMode.REQUIRED);
}

@ParameterizedTest
@MethodSource("newSslTaskExecutors")
public void testConnectMutualAuthOptionalWithCertSuccess(Executor executor) throws Throwable {
testConnectMutualAuthSuccess(executor, MutalAuthTestMode.OPTIONAL_CERT);
}

@ParameterizedTest
@MethodSource("newSslTaskExecutors")
public void testConnectMutualAuthOptionalWithoutCertSuccess(Executor executor) throws Throwable {
testConnectMutualAuthSuccess(executor, MutalAuthTestMode.OPTIONAL_NO_CERT);
}

private void testConnectMutualAuthSuccess(Executor executor, MutalAuthTestMode mode) throws Throwable {
Channel server = QuicTestUtils.newServer(QuicTestUtils.newQuicServerBuilder(executor,
QuicSslContextBuilder.forServer(
QuicTestUtils.SELF_SIGNED_CERTIFICATE.privateKey(), null,
QuicTestUtils.SELF_SIGNED_CERTIFICATE.privateKey(), null,
QuicTestUtils.SELF_SIGNED_CERTIFICATE.certificate()).trustManager(
InsecureTrustManagerFactory.INSTANCE)
.applicationProtocols(QuicTestUtils.PROTOS).clientAuth(ClientAuth.REQUIRE).build()),
InsecureTrustManagerFactory.INSTANCE)
.applicationProtocols(QuicTestUtils.PROTOS)
.clientAuth(mode == MutalAuthTestMode.REQUIRED ?
ClientAuth.REQUIRE : ClientAuth.OPTIONAL).build()),
InsecureQuicTokenHandler.INSTANCE, new ChannelInboundHandlerAdapter(),
new ChannelInboundHandlerAdapter());
InetSocketAddress address = (InetSocketAddress) server.localAddress();

QuicSslContextBuilder clientSslCtxBuilder = QuicSslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.applicationProtocols(QuicTestUtils.PROTOS);
if (mode == MutalAuthTestMode.OPTIONAL_CERT || mode == MutalAuthTestMode.REQUIRED) {
clientSslCtxBuilder.keyManager(
QuicTestUtils.SELF_SIGNED_CERTIFICATE.privateKey(), null,
QuicTestUtils.SELF_SIGNED_CERTIFICATE.certificate());
}

Channel channel = QuicTestUtils.newClient(QuicTestUtils.newQuicClientBuilder(executor,
QuicSslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE).keyManager(
QuicTestUtils.SELF_SIGNED_CERTIFICATE.privateKey(), null,
QuicTestUtils.SELF_SIGNED_CERTIFICATE.certificate())
.applicationProtocols(QuicTestUtils.PROTOS).build()));
clientSslCtxBuilder.build()));
try {
ChannelActiveVerifyHandler clientQuicChannelHandler = new ChannelActiveVerifyHandler();
QuicChannel quicChannel = QuicTestUtils.newQuicChannelBootstrap(channel)
Expand All @@ -968,32 +990,60 @@ InsecureQuicTokenHandler.INSTANCE, new ChannelInboundHandlerAdapter(),
}
}

private enum MutalAuthTestMode {
REQUIRED,
OPTIONAL_CERT,
OPTIONAL_NO_CERT
}

@ParameterizedTest
@MethodSource("newSslTaskExecutors")
public void testConnectMutualAuthFailsIfClientNotSendCertificate(Executor executor) throws Throwable {
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Throwable> causeRef = new AtomicReference<>();

Channel server = QuicTestUtils.newServer(QuicTestUtils.newQuicServerBuilder(executor,
QuicSslContextBuilder.forServer(
QuicTestUtils.SELF_SIGNED_CERTIFICATE.privateKey(), null,
QuicTestUtils.SELF_SIGNED_CERTIFICATE.certificate())
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.applicationProtocols(QuicTestUtils.PROTOS).clientAuth(ClientAuth.REQUIRE).build()),
InsecureQuicTokenHandler.INSTANCE, new ChannelInboundHandlerAdapter(),
InsecureQuicTokenHandler.INSTANCE, new ChannelInboundHandlerAdapter() {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
causeRef.compareAndSet(null, cause);
latch.countDown();
ctx.close();
}
},
new ChannelInboundHandlerAdapter());
InetSocketAddress address = (InetSocketAddress) server.localAddress();
Channel channel = QuicTestUtils.newClient(QuicTestUtils.newQuicClientBuilder(executor,
QuicSslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.applicationProtocols(QuicTestUtils.PROTOS).build()));
QuicChannel client = null;
try {
Throwable cause = QuicTestUtils.newQuicChannelBootstrap(channel)
.handler(new ChannelInboundHandlerAdapter())
client = QuicTestUtils.newQuicChannelBootstrap(channel)
.handler(new ChannelInboundHandlerAdapter() {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
}
})
.streamHandler(new ChannelInboundHandlerAdapter())
.remoteAddress(address)
.connect()
.await().cause();
assertThat(cause, Matchers.instanceOf(SSLException.class));
.get();
latch.await();

assertThat(causeRef.get(), Matchers.instanceOf(SSLHandshakeException.class));
} finally {
server.close().sync();

if (client != null) {
client.close().sync();
}
// Close the parent Datagram channel as well.
channel.close().sync();

Expand Down