4

我在开发应该在其中一个步骤中安装证书的安装时遇到了一个奇怪的问题。

该问题与在 Windows Server 2008 R2 上为帐户(例如 IIS_IUSRS)授予证书的私钥访问权限有关。私钥存储在位置 C:\Users\All Users\Microsoft\Crypto\RSA\MachineKeys。

自定义 C# 安装项目会在安装过程中导入证书并授予对证书私钥帐户的访问权限。一段时间后(2-3 秒),私钥文件会自动从 MachineKeys 文件夹中删除。因此已安装的 Web 应用程序无法访问特定证书并显示以下错误消息:

“System.Security.Cryptography.CryptographicException:密钥集不存在”。此错误仅在 Windows Server 2008 R2 上发生,而对于 Windows Server 2003,一切正常。

我的问题是,为什么私钥会被删除,哪个进程会这样做?

谢谢

2012 年 17 月 5 日更新

我还没有找到所描述问题的解决方案,并且在我询问的其他论坛(forums.asp.net、social.msdn.microsoft.com)上也没有发布任何回复。那么,任何人都可以建议任何其他资源或建议以进一步解决此问题吗?

再次感谢

4

3 回答 3

6

这也发生在我身上 - 我的安装脚本将添加证书并授予对 PK 文件的访问权限,并且该应用程序可以正常工作。后来,在我关闭 PowerShell 编辑器后,我重新启动了该应用程序,但由于找不到密钥集而失败。

在导入证书时添加 PersistKeySet 标志可解决问题。这是用于添加具有持久性的证书和私钥的 PowerShell 代码:

param(
    [string]$certStore = "LocalMachine\TrustedPeople",
    [string]$filename = "sp.pfx",
    [string]$password = "password",
    [string]$username = "$Env:COMPUTERNAME\WebSiteUser"
)

    function getKeyUniqueName($cert) {
         return $cert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
    }
    
    function getKeyFilePath($cert) {             
         return "$ENV:ProgramData\Microsoft\Crypto\RSA\MachineKeys\$(getKeyUniqueName($cert))"
    }

$certFromFile = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($filename, $password)
$certFromStore = Get-ChildItem "Cert:\$certStore" | Where-Object {$_.Thumbprint -eq $certFromFile.Thumbprint}
$certExistsInStore = $certFromStore.Count -gt 0
$keyExists = $certExistsInStore -and ($certFromStore.PrivateKey -ne $null) -and (getKeyUniqueName($cert) -ne $null) -and (Test-Path(getKeyFilePath($certFromStore)))

if ((!$certExistsInStore) -or (!$keyExists)) {

    $keyFlags = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet 
    $keyFlags = $keyFlags -bor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet
    $certFromFile.Import($filename, $password, $keyFlags)

    $store = Get-Item "Cert:\$certStore"
    $store.Open("ReadWrite")

    if ($certExistsInStore) {
        #Cert is in the store, but we have no persisted private key
        #Remove it so we can add the one we just imported with the key file
        $store.Remove($certFromStore)
    }

    $store.Add($certFromFile)
    $store.Close()

    $certFromStore = $certFromFile
    "Installed x509 certificate"
}

$pkFile = Get-Item(getKeyFilePath($certFromStore))
$pkAcl = $pkFile.GetAccessControl("Access")
$readPermission = $username,"Read","Allow"
$readAccessRule = new-object System.Security.AccessControl.FileSystemAccessRule $readPermission
$pkAcl.AddAccessRule($readAccessRule)
Set-Acl $pkFile.FullName $pkAcl
"Granted read permission on private key to web user"
于 2013-05-10T20:51:05.537 回答
1

很明显就是安全问题“<strong> System.Security.”。并且您没有权限进行安装。您需要设置私钥的权限以允许该服务帐户访问它

稍后编辑:转到开始->运行->cmd->键入 mmc->选择文件->添加/删除->选择证书->添加->计算机帐户->本地。,我附上一个截图是西班牙语,但我表示字段:

在此处输入图像描述

打开->证书->个人->证书->右键单击证书->所有任务->管理私钥->添加网络服务。

还请查看此条以了解此功能在 Windows Server 2008 中的工作原理。然后请在您尝试后,回来说是否可以按照我告诉您的内容解决问题。

于 2012-05-08T12:38:19.727 回答
1

http://referencesource.microsoft.com/#System/security/system/security/cryptography/x509/x509certificate2collection.cs,256显示了测试 PersistKeySet 标志的位置。PersistKeySet 标志记录在https://msdn.microsoft.com/en-us/library/system.security.cryptography.x509certificates.x509keystorageflags%28v=vs.110%29.aspx带有短语“与 PFX 文件关联的密钥在导入证书时被保留。” 我的英语翻译器告诉我这意味着“如果您调用 X509Certificate2 构造函数并且证书可能已经安装在机器上,则必须包含 PersistKeySet 标志。” 这可能也适用于 .Import 调用。很可能 powershell Import-PfxCertificate cmdlet 已经这样做了。但是,如果您正在执行已接受的答案显示或 OP 要求的内容,则需要包含特殊密钥。我们在解决方案中使用了 ejegg 脚本的变体。我们有一个每 3 分钟运行一次的进程来检查是否安装了所有配置的证书,现在这似乎工作正常。

我们在 powershell 中看到的症状是 HasPrivateKey 属性为 true 但 PrivateKey 值本身为空。并且 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 中证书的密钥文件已被删除。https://msdn.microsoft.com/en-us/library/aa717039(v=vs.110).aspx上的 FindPrivateKey 实用程序帮助我们观察文件被删除。

很晚才回答这个问题,祝你 4 岁生日快乐。

于 2016-06-02T14:49:50.010 回答