简体   繁体   English

使用 C# 读取证书签名请求

[英]Reading a certificate signing request with C#

I want to read the contents of a CSR in C#. However, I haven't found any way to do it in C#. What I've found was the namespace System.Security.Cryptography.X509Certificates , but it only handles existing certificates, not certificate requests.我想阅读 C# 中 CSR 的内容。但是,我在 C# 中没有找到任何方法。我发现的是命名空间System.Security.Cryptography.X509Certificates ,但它只处理现有证书,不是证书请求。

Can anyone give me an hint about it?任何人都可以给我一个提示吗? Thanks in advance.提前致谢。

There is a way, the CertEnroll library which comes with Windows (although I can't say how far back it's been there) allows you to load certificate requests and have them parsed.有一种方法,Windows 附带的 CertEnroll 库(虽然我不能说它已经存在多久了)允许您加载证书请求并解析它们。

First you need to import a reference to the CERTENROLLLib COM library into your project.首先,您需要将CERTENROLLLib COM 库的引用导入到您的项目中。 This will create a CERTENROLLLib name space you can then use.这将创建一个您可以使用的CERTENROLLLib名称空间。

Then you do something like this;然后你做这样的事情;

string csr = "-----BEGIN CERTIFICATE REQUEST-----\r\n" +
             "MIIBnTCCAQYCAQAwXTELMAkGA1UEBhMCU0cxETAPBgNVBAoTCE0yQ3J5cHRvMRIw\r\n" +
             "EAYDVQQDEwlsb2NhbGhvc3QxJzAlBgkqhkiG9w0BCQEWGGFkbWluQHNlcnZlci5l\r\n" +
             "eGFtcGxlLmRvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAr1nYY1Qrll1r\r\n" +
             "uB/FqlCRrr5nvupdIN+3wF7q915tvEQoc74bnu6b8IbbGRMhzdzmvQ4SzFfVEAuM\r\n" +
             "MuTHeybPq5th7YDrTNizKKxOBnqE2KYuX9X22A1Kh49soJJFg6kPb9MUgiZBiMlv\r\n" +
             "tb7K3CHfgw5WagWnLl8Lb+ccvKZZl+8CAwEAAaAAMA0GCSqGSIb3DQEBBAUAA4GB\r\n" +
             "AHpoRp5YS55CZpy+wdigQEwjL/wSluvo+WjtpvP0YoBMJu4VMKeZi405R7o8oEwi\r\n" +
             "PdlrrliKNknFmHKIaCKTLRcU59ScA6ADEIWUzqmUzP5Cs6jrSRo3NKfg1bd09D1K\r\n" +
             "9rsQkRc9Urv9mRBIsredGnYECNeRaK5R1yzpOowninXC\r" + 
             "-----END CERTIFICATE REQUEST-----";

CX509CertificateRequestPkcs10 request = new CX509CertificateRequestPkcs10();
request.InitializeDecode(csr, EncodingType.XCN_CRYPT_STRING_BASE64_ANY);
request.CheckSignature();

Console.WriteLine(((CX500DistinguishedName)request.Subject).Name);
Console.WriteLine(request.PublicKey.Length);
Console.WriteLine(request.HashAlgorithm.FriendlyName);

You can see the only fun part is getting the subject name out, as you need to cast it to a CX500DistinguishedName instance first.您可以看到唯一有趣的部分是获取主题名称,因为您需要CX500DistinguishedName其转换为CX500DistinguishedName实例。

Look at BouncyCastle's C# implementation .查看BouncyCastle 的 C# 实现 Used it for PGP stuff in the past, worked great.过去将它用于 PGP 的东西,效果很好。 Something like this should get you started (not tested):这样的事情应该让你开始(未测试):

var textReader = File.OpenText(...);
var reader = new Org.BouncyCastle.OpenSsl.PEMReader(textReader);
var req = reader.ReadObject() as Org.BouncyCastle.Pkcs.Pkcs10CertificationRequest;
var info = req.GetCertificationRequestInfo();
Console.WriteLine(info.Subject);

It seems to me the best way for you is usage unmanaged CryptoAPI or P/Invoke.在我看来,对您来说最好的方法是使用非托管的 CryptoAPI 或 P/Invoke。 CryptoAPI has CERT_REQUEST_INFO data struct and CryptSignAndEncodeCertificate function which can be used with X509_CERT_REQUEST_TO_BE_SIGNED parameter. CryptoAPI 具有CERT_REQUEST_INFO数据结构和CryptSignAndEncodeCertificate函数,可与X509_CERT_REQUEST_TO_BE_SIGNED参数一起使用。 Of cause theoretically it's possible to encode request manually with respect of AsnEncodedData , because CSR is not complex (see http://en.wikipedia.org/wiki/Certificate_signing_request and http://www.rfc-editor.org/rfc/rfc2311.txt ), but I don't think that it has a sense if an implementation already exist in CryptoAPI.当然,理论上可以针对AsnEncodedData手动编码请求,因为 CSR 并不复杂(参见http://en.wikipedia.org/wiki/Certificate_signing_requesthttp://www.rfc-editor.org/rfc/rfc2311 .txt ),但我认为如果 CryptoAPI 中已经存在实现,则没有意义。

A good examples to create CSR with respect of CryptoAPI you will find in http://msdn.microsoft.com/en-us/library/aa382364(VS.85).aspx and http://msdn.microsoft.com/en-us/library/ms867026.aspx .您可以在http://msdn.microsoft.com/en-us/library/aa382364(VS.85).aspxhttp://msdn.microsoft.com/en 中找到关于 CryptoAPI 创建 CSR 的一个很好的例子-us/library/ms867026.aspx

This is how you do it with OpenSSL.NET library:这是您使用OpenSSL.NET库的方式:

// Load the CSR file
var csr = new X509Request(BIO.File("C:/temp/test.csr", "r"));
OR
var csr = new X509Request(@"-----BEGIN CERTIFICATE REQUEST-----...");

// Read CSR file properties
Console.WriteLine(csr.PublicKey.GetRSA().PublicKeyAsPEM);
Console.WriteLine(csr.Subject.SerialNumber);
Console.WriteLine(csr.Subject.Organization);
.
.
.

X509Request type has properties to get everything out of your CSR file text. X509Request类型具有从 CSR 文件文本中获取所有内容的属性。

To read Certificate Signing request, you can use method LoadSigningRequestPem availaible in class - "System.Security.Cryptography.X509Certificates.CertificateRequest".要读取证书签名请求,您可以使用 class 中可用的方法 LoadSigningRequestPem - “System.Security.Cryptography.X509Certificates.CertificateRequest”。

using System.Security.Cryptography.X509Certificates;
string csr = @"-----BEGIN CERTIFICATE REQUEST-----
MIICvDCCAaQCAQAwdzELMAkGA1UEBhMCVVMxDTALBgNVBAgMBFV0YWgxDzANBgNV
BAcMBkxpbmRvbjEWMBQGA1UECgwNRGlnaUNlcnQgSW5jLjERMA8GA1UECwwIRGln
aUNlcnQxHTAbBgNVBAMMFGV4YW1wbGUuZGlnaWNlcnQuY29tMIIBIjANBgkqhkiG
9w0BAQEFAAOCAQ8AMIIBCgKCAQEA8+To7d+2kPWeBv/orU3LVbJwDrSQbeKamCmo
wp5bqDxIwV20zqRb7APUOKYoVEFFOEQs6T6gImnIolhbiH6m4zgZ/CPvWBOkZc+c
1Po2EmvBz+AD5sBdT5kzGQA6NbWyZGldxRthNLOs1efOhdnWFuhI162qmcflgpiI
WDuwq4C9f+YkeJhNn9dF5+owm8cOQmDrV8NNdiTqin8q3qYAHHJRW28glJUCZkTZ
wIaSR6crBQ8TbYNE0dc+Caa3DOIkz1EOsHWzTx+n0zKfqcbgXi4DJx+C1bjptYPR
BPZL8DAeWuA8ebudVT44yEp82G96/Ggcf7F33xMxe0yc+Xa6owIDAQABoAAwDQYJ
KoZIhvcNAQEFBQADggEBAB0kcrFccSmFDmxox0Ne01UIqSsDqHgL+XmHTXJwre6D
hJSZwbvEtOK0G3+dr4Fs11WuUNt5qcLsx5a8uk4G6AKHMzuhLsJ7XZjgmQXGECpY
Q4mC3yT3ZoCGpIXbw+iP3lmEEXgaQL0Tx5LFl/okKbKYwIqNiyKWOMj7ZR/wxWg/
ZDGRs55xuoeLDJ/ZRFf9bI+IaCUd1YrfYcHIl3G87Av+r49YVwqRDT0VDV7uLgqn
29XI1PpVUNCPQGn9p/eX6Qo7vpDaPybRtA2R7XLKjQaF9oXWeCUqy1hvJac9QFO2
97Ob1alpHPoZ7mWiEuJwjBPii6a9M9G30nUo39lBi1w=
-----END CERTIFICATE REQUEST-----";
CertificateRequest certReq = CertificateRequest.LoadSigningRequestPem(csr,HashAlgorithmName.SHA256);
Console.WriteLine("Subject Name: {0}",certReq.SubjectName.Name);

I had the same issue.我遇到过同样的问题。 I didn;t find a solution so "invented" ;) on a work around.我没有找到一个如此“发明”的解决方案;) 在变通方法上。 CertUtil.exe is microsoft's command line utility to create, read,submit, accept and install certs. CertUtil.exe 是微软的命令行实用程序,用于创建、读取、提交、接受和安装证书。 I used System.Diagnostics.Process to create external process and passed the CSR request file as argument to read the file into a stream.我使用 System.Diagnostics.Process 创建外部进程并将 CSR 请求文件作为参数传递以将文件读入流。 Heres the code for it.这是它的代码。

using (System.Diagnostics.Process extProc = new System.Diagnostics.Process())
{
      extProc.StartInfo.CreateNoWindow = true;
      extProc.StartInfo.UseShellExecute = false;
      extProc.StartInfo.RedirectStandardOutput = true;

      extProc.StartInfo.FileName = @"C:\certtest\Util_xpVersion\certutil.exe";
      extProc.StartInfo.Arguments = "-dump \"C:\\certtest\\Util_xpVersion\\ToolCSR.crq\"";

      extProc.Start();
      extProc.WaitForExit();
      string sTemp = extProc.StandardOutput.ReadToEnd();
      extProc.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM