Skip to content

Commit

Permalink
Use supressed StacklessSSLHandshakeException and revert tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hyperxpro committed Jan 23, 2023
1 parent c16f371 commit cf14ccc
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 13 deletions.
14 changes: 7 additions & 7 deletions handler/src/main/java/io/netty/handler/ssl/SslHandler.java
Expand Up @@ -1062,13 +1062,13 @@ private SSLEngineResult wrap(ByteBufAllocator alloc, SSLEngine engine, ByteBuf i
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
boolean handshakeFailed = handshakePromise.cause() != null;

final Exception exception;
if (handshakePromise.isDone()) {
exception = new ClosedChannelException();
} else {
// Closed before the handshake was done.
exception = new SSLHandshakeException("Connection closed during SSL handshake");
exception.initCause(new ClosedChannelException());
// Channel closed, we will generate 'ClosedChannelException' now.
ClosedChannelException exception = new ClosedChannelException();

// Add a supressed exception if the handshake was not completed yet.
if (!isStateSet(STATE_HANDSHAKE_STARTED) || handshakePromise.isDone()) {
exception.addSuppressed(new StacklessSSLHandshakeException("Connection closed before " +
"SSL/TLS handshake completed"));
}

// Make sure to release SSLEngine,
Expand Down
@@ -0,0 +1,43 @@
/*
* Copyright 2023 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.ssl;

import javax.net.ssl.SSLHandshakeException;

/**
* A {@link SSLHandshakeException} that does not fill in the stack trace.
*/
public final class StacklessSSLHandshakeException extends SSLHandshakeException {

private static final long serialVersionUID = -1244781947804415549L;

/**
* Constructs an exception reporting an error found by
* an SSL subsystem during handshaking.
*
* @param reason describes the problem.
*/
public StacklessSSLHandshakeException(String reason) {
super(reason);
}

@Override
public Throwable fillInStackTrace() {
// This is a performance optimization to not fill in the
// stack trace as this is a stackless exception.
return this;
}
}
Expand Up @@ -90,7 +90,6 @@
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLEngine;
import javax.net.ssl.SSLException;
import javax.net.ssl.SSLHandshakeException;
import javax.net.ssl.SSLProtocolException;
import javax.net.ssl.X509ExtendedTrustManager;

Expand Down Expand Up @@ -569,8 +568,8 @@ public void testCloseFutureNotified() throws Exception {

assertFalse(ch.finishAndReleaseAll());

assertThat(handler.handshakeFuture().cause(), instanceOf(SSLHandshakeException.class));
assertThat(handler.sslCloseFuture().cause(), instanceOf(SSLHandshakeException.class));
assertThat(handler.handshakeFuture().cause(), instanceOf(ClosedChannelException.class));
assertThat(handler.sslCloseFuture().cause(), instanceOf(ClosedChannelException.class));
}

@Test
Expand All @@ -591,11 +590,11 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc

SslCompletionEvent evt = events.take();
assertTrue(evt instanceof SslHandshakeCompletionEvent);
assertThat(evt.cause(), instanceOf(SSLHandshakeException.class));
assertThat(evt.cause(), instanceOf(ClosedChannelException.class));

evt = events.take();
assertTrue(evt instanceof SslCloseCompletionEvent);
assertThat(evt.cause(), instanceOf(SSLHandshakeException.class));
assertThat(evt.cause(), instanceOf(ClosedChannelException.class));
assertTrue(events.isEmpty());
}

Expand Down
Expand Up @@ -255,7 +255,7 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
assertSame(SslHandshakeCompletionEvent.SUCCESS, evt);
} else {
if (ctx.channel().parent() == null) {
assertTrue(handshakeEvt.cause() instanceof SSLHandshakeException);
assertTrue(handshakeEvt.cause() instanceof ClosedChannelException);
}
}
}
Expand Down

0 comments on commit cf14ccc

Please sign in to comment.