Mat*_*zuk 5 c# iis openssl http.sys
我正在构建一个C#控制台应用程序,它将:
[1.]生成自签名证书.
[2.]将其添加到个人(本地计算机商店)
[3.]最后使用netsh命令将该证书分配给计算机上的端口号.
到目前为止,我有部分[1.]和[2.]完美地工作,但在[3.]我受到无用且非正式的错误消息的困扰:
SSL证书添加失败,错误:1312指定的登录会话不存在.它可能已经被终止了.
当我查看微软有关此问题的官方网页时:https: //support.microsoft.com/en-us/kb/981506
它基本上告诉我这是一个Windows操作系统错误,我应该请求一个修补程序.
我的Hack解决这个问题的方法:
我能够最终绕过此错误的一种方法是打开IIS主页>打开"服务器证书"功能>然后导入我的.pfx证书.
通过将.pfx导入IIS,我似乎能够毫无困难地解决问题.我只需要按顺序运行这两个命令来生成.pfx
1.) C:\OpenSSL-Win32\bin>openssl req -x509 -sha256 -nodes -days 365 -subj "/C=US/ST=Denial/L=Springfield/O=Dis/CN=www.example.com" -newkey rsa:2048 -keyout privateKey.key -out certificate.crt
2.) C:\OpenSSL-Win32\bin>openssl pkcs12 -export -out cert.pfx -inkey privateKey.key -in certificate.crt -passout pass:
因此,如果我现在将这两个命令运行到openssl,并通过IIS将它们导入我的个人本地计算机证书存储区,我就没有SSL Certificate add failed, Error: 1312问题了.
但是,如果我以编程方式将新生成的证书添加到我的个人本地计算机证书存储区,那么我确实会收到错误:1312问题.
这是我的代码:
using CERTENROLLLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Diagnostics;
namespace Generate_X509_Certificate
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Guid.NewGuid().ToString());
Console.ReadLine();
// Launch OpenSSL:
string cPath = @"C:\OpenSSL-Win32\bin\";
string filename = Path.Combine(cPath, @"openssl.exe");
// Generate a .crt file
Console.WriteLine(@"Generating SSL Certificate...");
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\OpenSSL-Win32\bin\openssl.exe", @"req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout privateKey.key -out certificate.crt -subj ""/C=US/ST=California/L=SanFrancisco/CN=SecurityEncryption""");
Process.Start(startInfo);
// Combine the .crt with the .key to form a more Windows friendly .pfx
Console.WriteLine(@"Combining Private Key With Certificate...");
Process proc2 = Process.Start(filename, "pkcs12 -export -out cert.pfx -inkey privateKey.key -in certificate.crt -passout pass:");
proc2.Close();
// Store our newly created .pfx file as a variable
X509Certificate2 cert = new X509Certificate2(Directory.GetCurrentDirectory()+@"\cert.pfx");
// Add our .pfx file into the Personal Local Computer store:
var store = new X509Store(StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Add(cert);
store.Close();
// Finally, use netsh to assign this newly generated certificate to port 6613
string s1 = "netsh http add sslcert ipport=0.0.0.0:6613 certhash=???" + cert.GetCertHashString() + " appid={" + Guid.NewGuid().ToString() + "}";
Process p1 = new Process();
p1.StartInfo.FileName = "netsh.exe";
p1.StartInfo.Arguments = s1;
p1.StartInfo.UseShellExecute = false;
p1.StartInfo.RedirectStandardOutput = true;
// this is where I get the error "SSL Certificate add failed, Error: 1312"
p1.Start();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是netsh命令,只要我没有在这个C#程序中执行它就可以正常工作:
netsh http add sslcert ipport = 0.0.0.0:6613 certhash = 8834efd403687b331363ce9e5657ba4ffba0075b appid = {e604f84f-e666-4ccf-af52-fdeca666b5e9}
令人困惑的部分
因此,如果你openssl要从这个线程逐字地执行我的命令,那么将.pfxopenssl生成的文件导入到IIS中,最后使用netssh上面带有正确证书哈希的命令,这样整个过程就完美无缺.但是,当你在上面的C#代码中执行我刚才所说的内容时,我得到了错误.
另外一点,生成的.pfx比从此代码导入到商店中的内容在尝试netsh通过命令行手动执行时根本不起作用.
有没有人有任何想法?
如果使用 C#,请在存储之前尝试将 pfx 显式包含到证书中
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
//ensure pfx in cert.
byte[] pfx = cert.Export(X509ContentType.Pfx);
cert = new X509Certificate2(pfx, (string)null, X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.MachineKeySet);
//then store
store.Add(cert);
store.Close();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3808 次 |
| 最近记录: |