簡體   English   中英

帶有SSL證書的HttpListener掛在GetContext()上

[英]HttpListener with SSL certificate hanging on GetContext()

我創建了一個Windows服務,它使用HttpListener響應使用.NET Framework 4.0的某些第三方請求。 偵聽器通過以下方式綁定到https前綴:

listener.Prefixes.Add("https://+:" + Properties.Settings.Default.ListeningPort.ToString() + "/");

該服務還通過以下方式在計算機商店中自行注冊https證書:

X509Certificate2 certificate = new X509Certificate2(Properties.Settings.Default.HttpsCertPath, "", X509KeyStorageFlags.MachineKeySet);
X509Store store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(certificate);
store.Close();

此外,該服務還使用以下命令在Http API中注冊證書-ip:port binding:

ProcessStartInfo psi = new ProcessStartInfo();            
psi.FileName = "netsh";
psi.Arguments = "http add sslcert ipport=0.0.0.0:" + Properties.Settings.Default.ListeningPort.ToString() + " certhash=" + certificate.Thumbprint + " appid=" + appId;
Process proc = Process.Start(psi);                
proc.WaitForExit();
psi.Arguments = "http add sslcert ipport=[::]:" + Properties.Settings.Default.ListeningPort.ToString() + " certhash=" + certificate.Thumbprint + " appid=" + appId;
Process proc = Process.Start(psi);
proc.WaitForExit();

一切運作良好,如預期的那樣......除了......(這里是EVIL部分):運行一段時間后,一小時左右,listener.GetContext()不再返回,客戶端被“連接”刪除重置“像錯誤。

經過漫長而痛苦的調查后,我發現錯誤發生在HTTP API級別,並且在SSL握手期間會發生故障,因此listener.GetContext()根本不會返回。 更進一步,我發現每次失敗的請求,在Windows事件日志的系統日志中,都會記錄以下錯誤:

Source Schannel: A fatal error occurred when attempting to access the SSL server credential private key. The error code returned from the cryptographic module is 0x8009030D. The internal error state is 10001.

Source HttpEvent: An error occurred while using SSL configuration for endpoint 0.0.0.0:9799.  The error status code is contained within the returned data.

進一步深入研究這個問題我注意到,一旦私鑰從安裝的證書中消失了。 這與我在這個鏈接上讀到的內容相結合,讓我有所不同。 所以我嘗試了一下:

X509Certificate2 certificate = new X509Certificate2(Properties.Settings.Default.HttpsCertPath, "", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);

基本上,我已經放入了X509KeyStorageFlags.Exportable (如果你沒有在IIS X509KeyStorageFlags.PersistKeySet證書標記為可導出,請記住創建SSL綁定時遇到的錯誤嗎?)和X509KeyStorageFlags.PersistKeySet標志。 這似乎完成了這項工作。 該服務現在已經運行了近兩天,仍然樂於接受傳入的連接。

希望這能使我免受麻煩。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM