Powershell 私钥在Windows Server 2008 R2中意外删除

Powershell 私钥在Windows Server 2008 R2中意外删除,powershell,certificate,windows-server-2008-r2,private-key,Powershell,Certificate,Windows Server 2008 R2,Private Key,我在开发安装时遇到了一个奇怪的问题,该安装应该在其中一个步骤中安装证书 该问题与在Windows Server 2008 R2上为帐户(例如IIS_IUSRS)授予证书的私钥访问权限有关。私钥存储在位置C:\Users\All Users\Microsoft\Crypto\RSA\MachineKeys中 自定义C#安装项目导入证书,并在安装过程中通过证书的私钥为帐户提供访问权限。一段时间(2-3秒)后,私钥文件将自动从MachineKeys文件夹中删除。因此,安装的Web应用程序无法访问特定证

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

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

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

“System.Security.Cryptography.CryptographyException:密钥集不存在”。此错误仅在Windows Server 2008 R2上发生,而对于Windows Server 2003,所有操作都正常工作

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

Thx

更新日期:2012年5月17日

我还没有找到所描述问题的解决方案,在我询问的其他论坛(forums.asp.net、social.msdn.microsoft.com)上也没有任何回应。那么,有人能为进一步解决此问题提供其他资源或建议吗


再次感谢

非常清楚,这是一个安全问题“
系统安全
”。并且您没有进行安装的权限。您需要设置私钥的权限以允许该服务帐户

以后编辑:转到开始->运行->命令->键入mmc->选择文件->添加/删除->选择证书->添加->计算机帐户->本地。我附上了一个西班牙文屏幕截图,但我指出了字段:

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


另外,请检查此项以了解此功能在Windows Server 2008中的工作方式。然后,请在您尝试后,回来告诉我是否可以用我告诉您的内容解决此问题。

我也遇到过这种情况-我的安装脚本将添加证书并授予对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"
显示测试PersistKeySet标志的位置。PersistKeySet标志在中记录为短语“导入证书时,与PFX文件关联的密钥将被持久化”。我的techno babble英语翻译告诉我这意味着“如果调用X509Certificate2构造函数,并且证书可能已经安装在计算机上,则必须包含PersistKeySet标志。”这可能也适用于.Import调用。powershell Import-PfxCertificate cmdlet可能已经执行了此操作。但是如果你正在做被接受的答案所显示的或者OP所要求的,你需要包括特殊的密钥。我们在解决方案中使用了ejegg脚本的变体。我们有一个流程,每3分钟运行一次,以检查所有已配置的证书是否已安装,现在似乎工作正常

我们在powershell中看到的症状是HasPrivateKey属性为true,但PrivateKey值本身为null。并且删除了C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys中证书的密钥文件。位于的FindPrivateKey实用程序帮助我们监视文件被删除


所以,对于这个迟来的问题,祝你4岁生日快乐。

谢谢你的回答。但是,我的问题不是应该读取私钥的进程(即组IIS_IUSR)没有足够的访问权限来读取私钥。相反,问题是私钥甚至不存在。让我再次描述这一系列事件:进程(web应用程序的安装程序)创建一些私钥,然后在C:\Users\All Users\Microsoft\Crypto\RSA\MachineKeys机器文件夹中创建适当的文件,几秒钟后(安装程序进程终止之前)这些文件意外删除。我已阅读您的更新评论。让我再次澄清这个问题,因为我认为它还没有被理解:私钥是使用正确的访问权限成功创建的。问题是,在成功创建私钥几秒钟后,私钥会被删除。我不明白是什么程序删除了这些私钥。我也不知道“为什么?”但我们也遇到了这个问题。同样的修复工作-添加PersistKeySet和MachineKeySet似乎就是修复方法。我们的上下文是来自chef进程的powershell脚本块。guard脚本工作正常,但减去附加参数后,私钥受到重击,证书无法使用。希望这对其他人有帮助。谢谢!当$cert.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName为null且$keyExists在应为false时计算为true时,我做了一个小编辑来修复$keyExists测试