【问题标题】:Why would SslStream.AuthenticateAsServer fail from Ubuntu but not from Windows 10为什么 SslStream.AuthenticateAsServer 会从 Ubuntu 而不是 Windows 10 失败
【发布时间】:2019-07-05 06:19:51
【问题描述】:

SslStream.AuthenticateAsServer 方法在 Ubuntu 中失败,但在 Windows 10 中失败。

在实现来自https://docs.microsoft.com/en-us/dotnet/api/system.net.security.sslstream?view=netframework-4.8 的 SslStrem 示例时 它在 Windows 10 上执行时运行良好,但在 Ubuntu 上执行时,当调用 sslStream.AuthenticateAsServer 时出现以下异常:

System.NotSupportedException:服务器模式 SSL 必须使用 具有相关私钥的证书。

我尝试使用serverCertificate = X509Certificate2.CreateFromCertFile(certificate) 代替serverCertificate = X509Certificate.CreateFromCertFile(certificate),但无济于事。

证书是使用 OpenSSL 生成的,在执行模数检查时,所有密钥和证书确实匹配。服务器证书使用 CA 证书进行签名。证书是使用jww'sHow do you sign a Certificate Signing Request with your Certification Authority? 的冗长答案生成的

总结一下:在 Ubuntu(但不在 Windows 10 上)调用 sslStream.AuthenticateAsServer 时,会引发以下异常:System.NotSupportedException:服务器模式 SSL 必须使用具有关联私有的证书键。

我正在使用 .net-core 环境(显然需要在 Linux 上运行):-)

它与建议的可能重复的X509Certificate2 the server mode SSL must use a certificate with the associated private key 不同,因为它回答了证书的生成方式。不过drake7707's回复Mark Yuan's的回答给了我提示。

所以问题解决如下: 替换代码行

serverCertificate = X509Certificate.CreateFromCertFile(certificate);

serverCertificate = new X509Certificate2(certificate);

并更改其声明

static X509Certificate serverCertificate = null;

static X509Certificate2 serverCertificate = null;

总而言之,使用X509Certificate2 确实可以解决问题。

【问题讨论】:

标签: c# ubuntu authentication .net-core sslstream


【解决方案1】:

所以问题解决如下: 替换代码行

serverCertificate = X509Certificate.CreateFromCertFile(certificate);

serverCertificate = new X509Certificate2(certificate);

并更改其声明

static X509Certificate serverCertificate = null;

static X509Certificate2 serverCertificate = null;

总而言之,使用X509Certificate2 确实可以解决问题。

【讨论】:

  • 将答案发布为 answer。很少有人(如果有的话)会注意到问题的第二部分实际上是答案。检查Can I answer my own question?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-31
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多