Skip to content

Commit

Permalink
ssl: Work around missing remote hostname for authentication (#5988)
Browse files Browse the repository at this point in the history
* ssl: Retrieve remote hostnames if the provided hostname is empty

 This avoids crashing with an AuthenticationException.

* ssl: Remove unused variable from RetrieveHostName
  • Loading branch information
TSRBerry authored Jan 25, 2024
1 parent 371e6fa commit 43705c2
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Ryujinx.HLE.HOS.Services.Ssl.Types;
using System;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
Expand Down Expand Up @@ -83,10 +84,40 @@ private SslProtocols TranslateSslVersion(SslVersion version)
}
#pragma warning restore SYSLIB0039

/// <summary>
/// Retrieve the hostname of the current remote in case the provided hostname is null or empty.
/// </summary>
/// <param name="hostName">The current hostname</param>
/// <returns>Either the resolved or provided hostname</returns>
/// <remarks>
/// This is done to avoid getting an <see cref="System.Security.Authentication.AuthenticationException"/>
/// as the remote certificate will be rejected with <c>RemoteCertificateNameMismatch</c> due to an empty hostname.
/// This is not what the switch does!
/// It might just skip remote hostname verification if the hostname wasn't set with <see cref="ISslConnection.SetHostName"/> before.
/// TODO: Remove this as soon as we know how the switch deals with empty hostnames
/// </remarks>
private string RetrieveHostName(string hostName)
{
if (!string.IsNullOrEmpty(hostName))
{
return hostName;
}

try
{
return Dns.GetHostEntry(Socket.RemoteEndPoint.Address).HostName;
}
catch (SocketException)
{
return hostName;
}
}

public ResultCode Handshake(string hostName)
{
StartSslOperation();
_stream = new SslStream(new NetworkStream(((ManagedSocket)Socket).Socket, false), false, null, null);
hostName = RetrieveHostName(hostName);
_stream.AuthenticateAsClient(hostName, null, TranslateSslVersion(_sslVersion), false);
EndSslOperation();

Expand Down

0 comments on commit 43705c2

Please sign in to comment.