简体   繁体   English

在.Net中创建并验证x509证书

[英]Create and verify x509 certificates in .Net

In connection with my enterprise project (intranet only) I came across a question with the verification of certificates using .net System.Security.Cryptography.X509Certificates 关于我的企业项目(仅限内联网),我遇到了一个使用.net System.Security.Cryptography.X509Certificates验证证书的问题。

Step 1: Creating a root certificate 第1步: 创建根证书

Use makecert to create a root certificate and install it into the Trusted Root Certification Authorities 使用makecert创建根证书并将其安装到受信任的根证书颁发机构中

makecert -r -pe -n "CN=Test Labs (CA)" -ss CA -sr CurrentUser -a sha256 -cy authority -sky signature -sv TestLabCA.pvk TestLabCA.cer
pvk2pfx -pvk TestLabCA.pvk -spc TestLabCA.cer -pfx TestLabCA.pfx

Step 2: Creating a certificate and sign it with the root certificate 步骤2: 创建证书并使用根证书对其进行签名

Use makecert to create a certificate, sign it with the root certificate and install it to the Trusted Publishers 使用makecert创建证书,使用根证书对其进行签名并将其安装到受信任的发布者

makecert -pe -n "CN=Test Labs (SPC)" -a sha256 -cy end -sky signature -ic TestLabCA.cer -iv TestLabCA.pvk -sv TestLabSPC.pvk TestLabSPC.cer
pvk2pfx -pvk TestLabSPC.pvk -spc TestLabSPC.cer -pfx TestLabSPC.pfx

Step 3: Verify in code 第3步: 在代码中验证

This is the C# code sample to verify the certificates: 这是验证证书的C#代码示例:

X509Certificate2 rootCertificate = new X509Certificate2("TestLabCA.cer");
X509Certificate2 certificate = new X509Certificate2("TestLabSPC.cer");

// will return true
Console.WriteLine("{0}, verified = {1}", rootCertificate.GetName(), rootCertificate.Verify());

// will return false
Console.WriteLine("{0}, verified = {1}", certificate.GetName(), certificate.Verify());

// validate the chain
var chain = new X509Chain();
chain.Build(certificate);
Console.WriteLine("{0}, verified root of chain = {1}", certificate.GetName(), chain.ChainElements[chain.ChainElements.Count-1].Certificate.Verify());

Question: 题:

If I want to verify the certificate, do I have to check the chain and verify the last one in the chain, assuming that this is a root certificate? 如果我想验证证书,我是否必须检查链并验证链中的最后一个,假设这是一个根证书?

Is there a better way to do it? 有没有更好的方法呢?

You have to create a chain of trust, yes. 你必须创建一个信任链,是的。 This means you have to trace it back to a certificate that is trusted by you. 这意味着您必须将其追溯到您信任的证书。 This does not have to be a root certificate, but it is common to trust the root certificate and not an intermediate certificate. 这不一定是根证书,但通常信任根证书而不是中间证书。 Sometimes you only want to allow a single entity with a single certificate. 有时您只希望允许单个实体使用单个证书。 As long as you are sure it is the correct certificate, you could simply trust that single leaf certificate, so the "chain" would consist of a single certificate. 只要您确定它是正确的证书,您就可以简单地信任单叶证书,因此“链”将包含单个证书。 An example of this are self-signed root certificates that are sometimes used by web-services. 一个例子是有时由Web服务使用的自签名根证书。

Note that the verification of the certificate chain is only part of the total validation of a certificate. 请注意,证书链的验证只是证书总验证的一部分。 You need to make sure that the certificate has not been revoked, that the certificate is still valid (between the effective date and date of expiry). 您需要确保证书尚未被撤销,证书仍然有效(在生效日期和到期日期之间)。 Sometimes there are also additional, proprietary rules for validating certificates, eg presence of the certificate ID in a DB - whitelisting/blacklisting. 有时还有用于验证证书的附加专有规则,例如在DB中存在证书ID - 白名单/黑名单。

In addition to Maarten Bodewes' answer: 除了Maarten Bodewes的回答:

To easily verify if the chain is valid you can use the following code: 要轻松验证链是否有效,您可以使用以下代码:

X509Certificate2 Certificate = new X509Certificate2( "Certificate.pfx" );
X509Chain CertificateChain = new X509Chain();
//If you do not provide revokation information, use the following line.
CertificateChain.ChainPolicy.RevocationMode = X509RevocationMode.NoCheck;
bool IsCertificateChainValid = CertificateChain.Build( Certificate );

IsCertificateChainValid then returns true when the certificate chain is correct. 当证书链正确时,IsCertificateChainValid返回true。 In cases it returns false you can use 如果它返回false,你可以使用

CertificateChain.ChainStatus

for information about why the status is not valid. 有关状态无效的原因的信息。 Possible values include NotTimeValid, UntrustedRoot. 可能的值包括NotTimeValid,UntrustedRoot。

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

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