- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试更新我的应用程序的 TcpClient
将 TLS 与 SslStream
一起使用而不是正常的Stream
,我为此使用的代码似乎在 Unity 之外工作,但在集成到我的 Unity 2019.1.8(也在 2018 年和 2017 年测试)项目时失败。
建立连接并打开一个新的SslStream
我使用以下代码:
public static void InitClient(string hostName, int port, string certificateName)
{
client = new TcpClient(hostName, port);
if (client.Client.Connected)
{
Debug.LogFormat("Client connected succesfully");
}
else
{
Debug.LogErrorFormat("Client couldn't connect");
return;
}
stream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
try
{
stream.AuthenticateAsClient(certificateName);
}
catch (AuthenticationException e)
{
Debug.LogErrorFormat("Error authenticating: {0}", e);
if (e.InnerException != null)
{
Debug.LogErrorFormat("Inner exception: {0}", e);
}
Debug.LogErrorFormat("Authentication failed - closing connection");
stream.Close();
client.Close();
}
}
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
return true;
Debug.LogErrorFormat("Certificate error: {0}", sslPolicyErrors);
return false;
}
TlsException: Handshake failed - error code: UNITYTLS_INTERNAL_ERROR, verify result: UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED.
ValidateServerCertificate
总是返回
true
让我的客户毫无问题地连接。
true
来自
ValidateServerCertificate
来自
sslPolicyErrors == SslPolicyErrors.None
查看。
最佳答案
为什么 Unity 无法验证选项 1:
尽管证书有效且正确(例如,它在 Web 浏览器中使用时有效),但它不包括到根 CA 的中间证书链。这导致无法形成信任链(Unity 不缓存/检索中间体),导致 UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED
标志被设置。
为了解决这个问题,我需要将证书链附加到我的叶证书,以便 Unity 可以验证整个链直到根 CA。要查找证书的证书链,您可以使用“TLS 证书链编写器”。
根据您使用的软件,您可能需要将链包含在证书中,或者将其保存在单独的文件中。来自 whatsmychaincert (我绝不隶属于这个网站,只是使用它):
Note: some software requires you to put your site's certificate (e.g. example.com.crt) and your chain certificates (e.g. example.com.chain.crt) in separate files, while other software requires you to put your chain certificates after your site's certificate in the same file.
UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED
没有证书已过期的附加信息的错误,因此请确保“有效期至”日期在将来(这也会导致证书在 Web 浏览器中使用时被拒绝)。
In general, SSL/TLS clients will try to validate the server certificate chain as received from the server. If that chain does not please the client, then the client's behaviour depends on the implementation: some clients simply give up; others (especially Windows/Internet Explorer) will try to build another chain using locally known intermediate CA and also downloading certificates from URL found in other certificates (the "authority information access" extension).
We may be able to solve this issue by doing a verification via the system specific TLS api instead of using OpenSSL/MbedTLS to validate against root certificates as we do today, however this solution would then not work cross-platform. So we don't want to implement it today, as it would hide the misconfigured server from the user on some but not all platforms.
UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED
可能有各种原因,这只是其中之一。
关于c# - Unity TlsException : Handshake failed UNITYTLS_X509VERIFY_FLAG_NOT_TRUSTED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59945581/
我是一名优秀的程序员,十分优秀!