mscorlib(4.0.0.0) API with additions
CspKeyContainerInfo.cs
4 
6 {
8  [ComVisible(true)]
9  public sealed class CspKeyContainerInfo
10  {
11  private CspParameters m_parameters;
12 
13  private bool m_randomKeyContainer;
14 
18  public bool MachineKeyStore
19  {
20  get
21  {
22  if ((m_parameters.Flags & CspProviderFlags.UseMachineKeyStore) != CspProviderFlags.UseMachineKeyStore)
23  {
24  return false;
25  }
26  return true;
27  }
28  }
29 
32  public string ProviderName => m_parameters.ProviderName;
33 
36  public int ProviderType => m_parameters.ProviderType;
37 
40  public string KeyContainerName => m_parameters.KeyContainerName;
41 
46  public string UniqueKeyContainerName
47  {
48  [SecuritySafeCritical]
49  get
50  {
51  SafeProvHandle hProv = SafeProvHandle.InvalidHandle;
52  if (Utils._OpenCSP(m_parameters, 64u, ref hProv) != 0)
53  {
54  throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_NotFound"));
55  }
56  string result = (string)Utils._GetProviderParameter(hProv, m_parameters.KeyNumber, 8u);
57  hProv.Dispose();
58  return result;
59  }
60  }
61 
64  public KeyNumber KeyNumber => (KeyNumber)m_parameters.KeyNumber;
65 
71  public bool Exportable
72  {
73  [SecuritySafeCritical]
74  get
75  {
76  if (HardwareDevice)
77  {
78  return false;
79  }
80  SafeProvHandle hProv = SafeProvHandle.InvalidHandle;
81  if (Utils._OpenCSP(m_parameters, 64u, ref hProv) != 0)
82  {
83  throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_NotFound"));
84  }
85  byte[] array = (byte[])Utils._GetProviderParameter(hProv, m_parameters.KeyNumber, 3u);
86  hProv.Dispose();
87  return array[0] == 1;
88  }
89  }
90 
95  public bool HardwareDevice
96  {
97  [SecuritySafeCritical]
98  get
99  {
100  SafeProvHandle hProv = SafeProvHandle.InvalidHandle;
101  CspParameters cspParameters = new CspParameters(m_parameters);
102  cspParameters.KeyContainerName = null;
103  cspParameters.Flags = (((cspParameters.Flags & CspProviderFlags.UseMachineKeyStore) != 0) ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags);
104  uint flags = 4026531840u;
105  if (Utils._OpenCSP(cspParameters, flags, ref hProv) != 0)
106  {
107  throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_NotFound"));
108  }
109  byte[] array = (byte[])Utils._GetProviderParameter(hProv, cspParameters.KeyNumber, 5u);
110  hProv.Dispose();
111  return array[0] == 1;
112  }
113  }
114 
119  public bool Removable
120  {
121  [SecuritySafeCritical]
122  get
123  {
124  SafeProvHandle hProv = SafeProvHandle.InvalidHandle;
125  CspParameters cspParameters = new CspParameters(m_parameters);
126  cspParameters.KeyContainerName = null;
127  cspParameters.Flags = (((cspParameters.Flags & CspProviderFlags.UseMachineKeyStore) != 0) ? CspProviderFlags.UseMachineKeyStore : CspProviderFlags.NoFlags);
128  uint flags = 4026531840u;
129  if (Utils._OpenCSP(cspParameters, flags, ref hProv) != 0)
130  {
131  throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_NotFound"));
132  }
133  byte[] array = (byte[])Utils._GetProviderParameter(hProv, cspParameters.KeyNumber, 4u);
134  hProv.Dispose();
135  return array[0] == 1;
136  }
137  }
138 
143  public bool Accessible
144  {
145  [SecuritySafeCritical]
146  get
147  {
148  SafeProvHandle hProv = SafeProvHandle.InvalidHandle;
149  if (Utils._OpenCSP(m_parameters, 64u, ref hProv) != 0)
150  {
151  return false;
152  }
153  byte[] array = (byte[])Utils._GetProviderParameter(hProv, m_parameters.KeyNumber, 6u);
154  hProv.Dispose();
155  return array[0] == 1;
156  }
157  }
158 
164  public bool Protected
165  {
166  [SecuritySafeCritical]
167  get
168  {
169  if (HardwareDevice)
170  {
171  return true;
172  }
173  SafeProvHandle hProv = SafeProvHandle.InvalidHandle;
174  if (Utils._OpenCSP(m_parameters, 64u, ref hProv) != 0)
175  {
176  throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_NotFound"));
177  }
178  byte[] array = (byte[])Utils._GetProviderParameter(hProv, m_parameters.KeyNumber, 7u);
179  hProv.Dispose();
180  return array[0] == 1;
181  }
182  }
183 
189  {
190  [SecuritySafeCritical]
191  get
192  {
193  KeyContainerPermission keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
195  keyContainerPermission.AccessEntries.Add(accessEntry);
196  keyContainerPermission.Demand();
197  SafeProvHandle hProv = SafeProvHandle.InvalidHandle;
198  if (Utils._OpenCSP(m_parameters, 64u, ref hProv) != 0)
199  {
200  throw new CryptographicException(Environment.GetResourceString("Cryptography_CSP_NotFound"));
201  }
202  using (hProv)
203  {
204  return Utils.GetKeySetSecurityInfo(hProv, AccessControlSections.All);
205  }
206  }
207  }
208 
212  public bool RandomlyGenerated => m_randomKeyContainer;
213 
214  private CspKeyContainerInfo()
215  {
216  }
217 
218  [SecurityCritical]
219  internal CspKeyContainerInfo(CspParameters parameters, bool randomKeyContainer)
220  {
221  if (!CompatibilitySwitches.IsAppEarlierThanWindowsPhone8)
222  {
223  KeyContainerPermission keyContainerPermission = new KeyContainerPermission(KeyContainerPermissionFlags.NoFlags);
225  keyContainerPermission.AccessEntries.Add(accessEntry);
226  keyContainerPermission.Demand();
227  }
228  m_parameters = new CspParameters(parameters);
229  if (m_parameters.KeyNumber == -1)
230  {
231  if (m_parameters.ProviderType == 1 || m_parameters.ProviderType == 24)
232  {
233  m_parameters.KeyNumber = 1;
234  }
235  else if (m_parameters.ProviderType == 13)
236  {
237  m_parameters.KeyNumber = 2;
238  }
239  }
240  m_randomKeyContainer = randomKeyContainer;
241  }
242 
245  [SecuritySafeCritical]
247  : this(parameters, randomKeyContainer: false)
248  {
249  }
250  }
251 }
The exception that is thrown when an error occurs during a cryptographic operation.
int ProviderType
Gets the provider type of a key.
int KeyNumber
Specifies whether an asymmetric key is created as a signature key or an exchange key.
string KeyContainerName
Represents the key container name for T:System.Security.Cryptography.CspParameters.
Definition: __Canon.cs:3
bool MachineKeyStore
Gets a value indicating whether a key is from a machine key set.
Specifies access rights for specific key containers. This class cannot be inherited.
CspProviderFlags
Specifies flags that modify the behavior of the cryptographic service providers (CSP).
Controls the ability to access key containers. This class cannot be inherited.
bool? HardwareDevice
Gets a value indicating whether a key is a hardware key.
string ProviderName
Gets the provider name of a key.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
KeyNumber KeyNumber
Gets a value that describes whether an asymmetric key was created as a signature key or an exchange k...
string UniqueKeyContainerName
Gets a unique key container name.
int Add(KeyContainerPermissionAccessEntry accessEntry)
Adds a T:System.Security.Permissions.KeyContainerPermissionAccessEntry object to the collection.
KeyContainerPermissionAccessEntryCollection AccessEntries
Gets the collection of T:System.Security.Permissions.KeyContainerPermissionAccessEntry objects associ...
Contains parameters that are passed to the cryptographic service provider (CSP) that performs cryptog...
Definition: CspParameters.cs:8
KeyContainerPermissionFlags
Specifies the type of key container access allowed.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
bool Exportable
Gets a value indicating whether a key can be exported from a key container.
KeyNumber
Specifies whether to create an asymmetric signature key or an asymmetric exchange key.
Definition: KeyNumber.cs:8
Provides the ability to control access to a cryptographic key object without direct manipulation of a...
Provides additional information about a cryptographic key pair. This class cannot be inherited.
string KeyContainerName
Gets a key container name.
AccessControlSections
Specifies which sections of a security descriptor to save or load.
CspKeyContainerInfo(CspParameters parameters)
Initializes a new instance of the T:System.Security.Cryptography.CspKeyContainerInfo class using the ...
CspProviderFlags Flags
Represents the flags for T:System.Security.Cryptography.CspParameters that modify the behavior of the...