Sco*_*ock 17 c# ssl sslstream ssl-certificate
我需要在使用TCP/IP套接字进行通信的各种进程之间提供安全通信.我想要身份验证和加密.我不想重新发明轮子,而是真的想使用SSL和SslStream类以及自签名证书.我想要做的是根据本地应用程序中的已知副本验证远程进程的证书.(不需要是证书颁发机构,因为我打算手动复制证书).
为此,我希望应用程序能够在第一次运行时自动生成新的证书.除了makecert.exe之外,看起来此链接显示了一种自动生成自签名证书的方法,因此这是一个开始.
我查看了SslStream的AuthenticateAsServer和AuthenticateAsClient方法.您可以提供回电验证,因此看起来可能.但是现在我已经了解了它的细节,我真的不认为这样做是可能的.
我正朝着正确的方向前进吗?还有更好的选择吗?有没有人之前做过这样的事情(基本上是点对点SSL而不是客户端服务器)?
Sco*_*ock 28
第1步:生成自签名证书:
我使用此代码生成.pfx证书文件:
byte[] c = Certificate.CreateSelfSignCertificatePfx(
"CN=yourhostname.com", //host name
DateTime.Parse("2000-01-01"), //not valid before
DateTime.Parse("2010-01-01"), //not valid after
"mypassword"); //password to encrypt key file
using (BinaryWriter binWriter = new BinaryWriter(
File.Open(@"testcert.pfx", FileMode.Create)))
{
binWriter.Write(c);
}
Run Code Online (Sandbox Code Playgroud)第2步:加载证书
X509Certificate cert = new X509Certificate2(
@"testcert.pfx",
"mypassword");
Run Code Online (Sandbox Code Playgroud)
第3步:把它放在一起
我使用步骤2中的行替换了Server Program.cs文件中的这一行:
X509Certificate cert = getServerCert();
在Client Program.cs文件中,确保设置serverName = yourhostname.com(并且它与证书中的名称匹配)
第4步:客户端身份验证
这是我的客户端验证的方式(它与服务器有点不同):
TcpClient client = new TcpClient();
client.Connect(hostName, port);
SslStream sslStream = new SslStream(client.GetStream(), false,
new RemoteCertificateValidationCallback(CertificateValidationCallback),
new LocalCertificateSelectionCallback(CertificateSelectionCallback));
bool authenticationPassed = true;
try
{
string serverName = System.Environment.MachineName;
X509Certificate cert = GetServerCert(SERVER_CERT_FILENAME, SERVER_CERT_PASSWORD);
X509CertificateCollection certs = new X509CertificateCollection();
certs.Add(cert);
sslStream.AuthenticateAsClient(
serverName,
certs,
SslProtocols.Default,
false); // check cert revokation
}
catch (AuthenticationException)
{
authenticationPassed = false;
}
if (authenticationPassed)
{
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
CertificateValidationCallback与服务器案例中的相同,但请注意AuthenticateAsClient如何获取证书集合,而不仅仅是一个证书.所以,你必须添加一个LocalCertificateSelectionCallback,就像这样(在这种情况下,我只有一个客户端证书,所以我只返回集合中的第一个):
static X509Certificate CertificateSelectionCallback(object sender,
string targetHost,
X509CertificateCollection localCertificates,
X509Certificate remoteCertificate,
string[] acceptableIssuers)
{
return localCertificates[0];
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28076 次 |
最近记录: |