【发布时间】: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 确实可以解决问题。
【问题讨论】:
-
感谢您解决了该问题,但查看异常消息(“System.NotSupportedException:服务器模式 SSL 必须使用具有关联私钥的证书。 " - 重点是我的)结合apisof page for AuthenticateAsServer 表明它不支持 Windows 以外的操作系统
-
我猜这就是为什么写
X509Certificate2因为X509Certificate不支持其他操作系统。
标签: c# ubuntu authentication .net-core sslstream