2010-07-12 111 views
5

我試圖製作一個簡單的服務器,它偵聽端口並使用SSL進行身份驗證。我有文件設置帶簽名證書的SSL服務器套接字

server.crt 
server.key 
my-ca.crt 

用openssl教程(http://www.vanemery.com/Linux/Apache/apache-SSL.html)獲得。 my-ca.crt是我自己的CA證書,server.crt包含x509服務器證書(用my-ca.crt簽名),server.key是對應的私鑰。

我現在不知道如何在c#中加載這三個文件;我有一些像

serverCertificate = new X509Certificate2("server.crt", "secret_password"); 
sslStream.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true); 

不工作(我得到一個

Unhandled Exception: System.NotSupportedException: The server mode SSL must use a certificate with the associated private key. 

) ,但我不知道如何添加server.key和/或my-ca.crt

回答

3

要在形式

serverCertificate = new X509Certificate2("server.pfx", "secret_password"); 

你應該保存證書PKCS12格式使用X509Certificate2。見http://www.madboa.com/geek/openssl/#cert-pkcs12http://www.openssl.org/docs/apps/pkcs12.html

+0

我一直試圖做這樣的: 'OpenSSL的REQ -x509 -nodes -days 365 -newkey RSA:1024 -keyout 「TEMP \ signkey.key」 退房手續「TEMP \ signkey.key 「-config」openssl.cfg「'和 'openssl pkcs12 -export -out」temp \ certificate.pfx「-in」temp \ signkey.key「-name」NAME「'但是仍然存在異常。任何想法? – 2011-12-01 01:28:30

+0

@Christoph您需要在導出過程中包含私鑰,如下所示:'openssl pkcs12 -export -out certificate.pfx -in certificate.cer -inkey certificate.key -name「SSL stream 帶'-inkey'參數的證書''' 。 – Joseph 2016-06-27 15:32:01