私钥权限在程序退出时中断
Private Key Permissions Breaking on Program Exit
我有一个小应用程序作为一个更大程序的 'certificate manager',这样用户就不必手动安装和配置证书。
它相当简单 - 它有一些证书作为嵌入式资源,将其加载到适当的存储中,然后设置适当配置的权限。
这似乎在程序 运行ning 时正常工作。使用 MMC,我可以看到证书已安装。如果我管理私钥,它会正确添加新权限。但是,一旦我关闭证书管理器,权限就会中断。证书仍然安装,但点击管理私钥弹出类似于 "Key does not exist."
的错误
此外,如果程序是 运行 第二次,程序退出后权限将 'stick' 正确。
这是程序用来验证权限是否已添加的代码。每次都使用此方法 returns 'true',即使之后权限中断也是如此。
private bool GetSecurityStatus(X509Certificate2 cert, X509Store store)
{
store.Open(OpenFlags.ReadOnly);
//add Authenticated Users to private cert
RSACryptoServiceProvider privKeyRSA = cert.PrivateKey as RSACryptoServiceProvider;
string keyFilePath = FindKeyLocation(privKeyRSA.CspKeyContainerInfo.UniqueKeyContainerName);
FileInfo privateKeyFileInfo = new FileInfo(keyFilePath + "\" + privKeyRSA.CspKeyContainerInfo.UniqueKeyContainerName);
FileSecurity privateKeyFileSecurity = privateKeyFileInfo.GetAccessControl();
AuthorizationRuleCollection rules = privateKeyFileSecurity.GetAccessRules(true, true, typeof(NTAccount));
foreach (FileSystemAccessRule fsar in rules)
{
if(fsar.IdentityReference.Value.Contains("Authenticated Users") && fsar.AccessControlType == AccessControlType.Allow && fsar.FileSystemRights == FileSystemRights.FullControl){
store.Close();return true;
}
}
//Close Private Cert store
store.Close();
return false;
}
FindKeyLocation returns appdata\Microsoft\Crypto\RSA\ 私钥的路径。
我认为它必须以某种方式改变私钥文件本身的程序退出,但我不确定为什么它会在第二次工作。
我相信我已根据此处的回复找到问题的解决方案:
Import certificate with private key programmatically
并从这里的 MSDN
我必须在证书参数中传递两个标志,而不仅仅是 MachineKey
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet
我有一个小应用程序作为一个更大程序的 'certificate manager',这样用户就不必手动安装和配置证书。
它相当简单 - 它有一些证书作为嵌入式资源,将其加载到适当的存储中,然后设置适当配置的权限。
这似乎在程序 运行ning 时正常工作。使用 MMC,我可以看到证书已安装。如果我管理私钥,它会正确添加新权限。但是,一旦我关闭证书管理器,权限就会中断。证书仍然安装,但点击管理私钥弹出类似于 "Key does not exist."
的错误此外,如果程序是 运行 第二次,程序退出后权限将 'stick' 正确。
这是程序用来验证权限是否已添加的代码。每次都使用此方法 returns 'true',即使之后权限中断也是如此。
private bool GetSecurityStatus(X509Certificate2 cert, X509Store store)
{
store.Open(OpenFlags.ReadOnly);
//add Authenticated Users to private cert
RSACryptoServiceProvider privKeyRSA = cert.PrivateKey as RSACryptoServiceProvider;
string keyFilePath = FindKeyLocation(privKeyRSA.CspKeyContainerInfo.UniqueKeyContainerName);
FileInfo privateKeyFileInfo = new FileInfo(keyFilePath + "\" + privKeyRSA.CspKeyContainerInfo.UniqueKeyContainerName);
FileSecurity privateKeyFileSecurity = privateKeyFileInfo.GetAccessControl();
AuthorizationRuleCollection rules = privateKeyFileSecurity.GetAccessRules(true, true, typeof(NTAccount));
foreach (FileSystemAccessRule fsar in rules)
{
if(fsar.IdentityReference.Value.Contains("Authenticated Users") && fsar.AccessControlType == AccessControlType.Allow && fsar.FileSystemRights == FileSystemRights.FullControl){
store.Close();return true;
}
}
//Close Private Cert store
store.Close();
return false;
}
FindKeyLocation returns appdata\Microsoft\Crypto\RSA\ 私钥的路径。
我认为它必须以某种方式改变私钥文件本身的程序退出,但我不确定为什么它会在第二次工作。
我相信我已根据此处的回复找到问题的解决方案:
Import certificate with private key programmatically
并从这里的 MSDN
我必须在证书参数中传递两个标志,而不仅仅是 MachineKey
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet