Azure Key Vault 和过期证书

Azure Key Valut and Expiring Certificate

我希望我能正确解释这一点。我继承了几个 windows 应用程序,这些应用程序需要将证书安装到本地证书存储区才能访问 Azure Key Vault 的 Secret 来执行应用程序的操作。目前一切正常。 Azure 中的证书将于 2019 年 10 月 31 日到期。

已创建新证书,将于 2020 年 9 月到期。

当我将这些应用程序转储给我时,我得到了可以使用的证书,但它有一个 .p12 扩展名。我只能将新的 Azure 证书导出为 .cer 或 .pfx。

当我将新导出的证书安装为 .pfx 或 .cer 时,应用程序失败。如果我安装带有 .p12 扩展名的旧证书,它们就可以工作。

这两个应用程序都使用下面的代码通过 "Issuer" 获取(我认为)当前的本地证书,即 CN = Value。我检查了 "Issuer/CN =" 的旧值和新值,它们是相同的。

在 Azure 中导出的证书是否需要具有 .p12 扩展名?如果是这样,我该怎么做。

如果 Azure 中导出的证书作为 .pfx 没问题,我的问题可能出在哪里?

获取本地证书的应用程序中的 C# 代码依次获取完成工作所需的 Azure 机密:

private static X509Certificate2 ReadCertificateFromStore(string certName)
    {
        X509Certificate2 cert = null;

        try
        {
            using (X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
            {
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certCollection = store.Certificates;

                // Find unexpired certificates.
                X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);

                // From the collection of unexpired certificates, find the ones with the correct name.
                X509Certificate2Collection signingCert = currentCerts.Find(X509FindType.FindBySubjectDistinguishedName, certName, false);

                // Return the first certificate in the collection, has the right name and is current.
                cert = signingCert.OfType<X509Certificate2>().OrderByDescending(c => c.NotBefore).FirstOrDefault();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        return cert;
    }

首先.p12.pfx是PKCS#12格式的扩展。

Both apps use the code below to get (I think) the local cert that is current via the "Issuer" which is CN = Value. I've checked both the old and new values of "Issuer/CN =" and they are identical.

根据您的代码不正确

// From the collection of unexpired certificates, find the ones with the correct name.
X509Certificate2Collection signingCert =
   currentCerts.Find(X509FindType.FindBySubjectDistinguishedName, certName, false);

它说 FindBySubjectDistinguishedName 这意味着两个证书的 subject 需要完全相同。这是一个例子:

还有一个在主题中有多个元素:

您也可以同时安装这两个证书并尝试计算参数以获得正确的证书。我将您的部分代码转换为 PowerShell:

$store = 
  new-object System.Security.Cryptography.X509Certificates.X509Store( `
    [System.Security.Cryptography.X509Certificates.StoreName]::My, `
    [System.Security.Cryptography.X509Certificates.StoreLocation]::CurrentUser);

$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly);

$signingCert = 
   $store.Certificates.Find(
     [System.Security.Cryptography.X509Certificates.X509FindType]::FindBySubjectDistinguishedName, 
     "CN=...", `
     $false);

$signingCert

谜团解开了。除了在相关机器上安装证书之外,您还需要在 Azure 的应用程序注册中注册证书(.cer 部分)。