我们有一个与设备通讯的c#应用。现在,我们只需将证书安装在Windows应用商店中即可。
现在,我们需要一种类似于“ Windows远程桌面”的行为。一旦我们连接到设备,我们将获得(自签名)证书。然后,我们询问用户他是否信任证书。使用SslStream
非常简单。
初始化SSL流:
SslStream sslStream = new SslStream(.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
处理程序:
static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors != SslPolicyErrors.None)
{
string certificateHash = CreateHash(certificate);
//pseudo code -> go to database and check if the certificate is already trusted
if(database.TrustedCertificates.Contains(certificateHash))
{
return true;
}
//TODO ask for permission
bool isTrusted = true;
if (isTrusted)
{
//TODO add certificate (hash) to store
database.TrustedCertificates.Add(certificateHash);
}
}
return true;
}
我们不想在商店内置的窗口中添加手动信任的证书。相反,我们将在数据库中拥有自己的证书存储。因此,受信任的证书仅对我们的应用程序有效。
现在,我想知道是否最好的方法是将受信任的证书存储在自己的存储中,而不是将其添加到Windows证书存储(certlm.msc)。是否足够保存以将它们存储在应用程序的数据库中(只需创建一个表TrustedCertificates)?证书是否需要像密码一样进行哈希处理?在任何情况下都应该避免这种情况,仅使用Windows证书存储区吗?
有什么想法吗?