Skip to content

Fix encryption alert write during TLS handshake #91

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 3 additions & 2 deletions src/DotNetty.Handlers/Tls/TlsHandler.Handshake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace DotNetty.Handlers.Tls
partial class TlsHandler
{
private static readonly Action<object, object> s_handshakeCompletionCallback = (t, s) => HandleHandshakeCompleted((Task)t, (TlsHandler)s);
private static readonly TaskCanceledException s_taskCanceledException = new TaskCanceledException();
public static readonly AttributeKey<SslStream> SslStreamAttrKey = AttributeKey<SslStream>.ValueOf("SSLSTREAM");

private bool EnsureAuthenticated(IChannelHandlerContext ctx)
Expand Down Expand Up @@ -215,9 +216,9 @@ private static void HandleHandshakeCompleted(Task task, TlsHandler self)
var cause = taskExc.Unwrap();
try
{
if (self._handshakePromise.TrySetException(taskExc))
if (task.IsFaulted ? self._handshakePromise.TrySetException(taskExc) : self._handshakePromise.TrySetCanceled())
{
TlsUtils.NotifyHandshakeFailure(capturedContext, cause, true);
TlsUtils.NotifyHandshakeFailure(capturedContext, task.IsFaulted ? cause : s_taskCanceledException, true);
}
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ private int ReadFromInput(Memory<byte> destination) // byte[] destination, int d
}

public override void Write(ReadOnlySpan<byte> buffer)
=> _owner.FinishWrap(buffer, _owner._lastContextWritePromise);
=> _owner.FinishWrap(buffer, _owner._lastContextWritePromise ?? _owner.CapturedContext.NewPromise());

public override void Write(byte[] buffer, int offset, int count)
=> _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise);
=> _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise ?? _owner.CapturedContext.NewPromise());

public override ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private int ReadFromInput(byte[] destination, int destinationOffset, int destina
return length;
}

public override void Write(byte[] buffer, int offset, int count) => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise);
public override void Write(byte[] buffer, int offset, int count) => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise ?? _owner.CapturedContext.NewPromise());

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> _owner.FinishWrapNonAppDataAsync(buffer, offset, count, _owner.CapturedContext.NewPromise());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private int ReadFromInput(byte[] destination, int destinationOffset, int destina
return length;
}

public override void Write(byte[] buffer, int offset, int count) => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise);
public override void Write(byte[] buffer, int offset, int count) => _owner.FinishWrap(buffer, offset, count, _owner._lastContextWritePromise ?? _owner.CapturedContext.NewPromise());

public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
=> _owner.FinishWrapNonAppDataAsync(buffer, offset, count, _owner.CapturedContext.NewPromise());
Expand Down