1 using Microsoft.Win32.SafeHandles;
12 private struct X509ChainErrorMapping
14 public readonly uint Win32Flag;
16 public readonly
int Win32ErrorCode;
20 public X509ChainErrorMapping(uint win32Flag,
int win32ErrorCode,
X509ChainStatusFlags chainStatusFlag)
22 Win32Flag = win32Flag;
23 Win32ErrorCode = win32ErrorCode;
24 ChainStatusFlag = chainStatusFlag;
28 private uint m_status;
37 private SafeX509ChainHandle m_safeCertChainHandle;
39 private bool m_useMachineContext;
41 private readonly
object m_syncRoot =
new object();
43 private static readonly X509ChainErrorMapping[] s_x509ChainErrorMappings =
new X509ChainErrorMapping[23]
55 new X509ChainErrorMapping(4096u, -2146762476,
X509ChainStatusFlags.HasNotSupportedNameConstraint),
57 new X509ChainErrorMapping(16384u, -2146762476,
X509ChainStatusFlags.HasNotPermittedNameConstraint),
66 new X509ChainErrorMapping(134217728u, -2146762491,
X509ChainStatusFlags.HasNotSupportedCriticalExtension),
78 return m_safeCertChainHandle.DangerousGetHandle();
91 return m_safeCertChainHandle;
102 if (m_chainPolicy ==
null)
106 return m_chainPolicy;
114 m_chainPolicy = value;
124 if (m_chainStatus ==
null)
132 m_chainStatus = GetChainStatusInformation(m_status);
135 return m_chainStatus;
153 : this(useMachineContext: false)
164 m_chainPolicy =
null;
165 m_chainStatus =
null;
167 m_safeCertChainHandle = SafeX509ChainHandle.InvalidHandle;
168 m_useMachineContext = useMachineContext;
183 m_safeCertChainHandle = CAPISafe.CertDuplicateCertificateChain(chainContext);
184 if (m_safeCertChainHandle ==
null || m_safeCertChainHandle == SafeX509ChainHandle.InvalidHandle)
203 if (certificate ==
null || certificate.CertContext.IsInvalid)
205 throw new ArgumentException(SR.GetString(
"Cryptography_InvalidContextHandle"),
"certificate");
223 CAPIBase.CERT_CHAIN_POLICY_PARA pPolicyPara =
new CAPIBase.CERT_CHAIN_POLICY_PARA(
Marshal.
SizeOf(typeof(CAPIBase.CERT_CHAIN_POLICY_PARA)));
224 CAPIBase.CERT_CHAIN_POLICY_STATUS pPolicyStatus =
new CAPIBase.CERT_CHAIN_POLICY_STATUS(
Marshal.
SizeOf(typeof(CAPIBase.CERT_CHAIN_POLICY_STATUS)));
226 if (!CAPISafe.CertVerifyCertificateChainPolicy(
new IntPtr(1
L), m_safeCertChainHandle, ref pPolicyPara, ref pPolicyStatus))
230 CAPISafe.SetLastError(pPolicyStatus.dwError);
231 return pPolicyStatus.dwError == 0;
242 m_chainStatus =
null;
244 if (!m_safeCertChainHandle.IsInvalid)
246 m_safeCertChainHandle.Dispose();
247 m_safeCertChainHandle = SafeX509ChainHandle.InvalidHandle;
252 [SecuritySafeCritical]
262 [SecuritySafeCritical]
263 protected virtual void Dispose(
bool disposing)
272 private unsafe
void Init()
274 using (SafeX509ChainHandle safeX509ChainHandle = CAPISafe.CertDuplicateCertificateChain(m_safeCertChainHandle))
276 CAPIBase.CERT_CHAIN_CONTEXT cERT_CHAIN_CONTEXT =
new CAPIBase.CERT_CHAIN_CONTEXT(
Marshal.
SizeOf(typeof(CAPIBase.CERT_CHAIN_CONTEXT)));
277 uint num = (uint)
Marshal.
ReadInt32(safeX509ChainHandle.DangerousGetHandle());
283 m_status = cERT_CHAIN_CONTEXT.dwErrorStatus;
284 m_chainElementCollection =
new X509ChainElementCollection(
Marshal.
ReadIntPtr(cERT_CHAIN_CONTEXT.rgpChain));
288 internal static X509ChainStatus[] GetChainStatusInformation(uint dwStatus)
292 return new X509ChainStatus[0];
295 for (uint num2 = dwStatus; num2 != 0; num2 >>= 1)
302 X509ChainStatus[] array =
new X509ChainStatus[num];
304 X509ChainErrorMapping[] array2 = s_x509ChainErrorMappings;
305 foreach (X509ChainErrorMapping x509ChainErrorMapping
in array2)
307 if ((dwStatus & x509ChainErrorMapping.Win32Flag) != 0)
310 array[num3].Status = x509ChainErrorMapping.ChainStatusFlag;
312 dwStatus &= ~x509ChainErrorMapping.Win32Flag;
316 for (uint num5 = dwStatus; num5 != 0; num5 >>= 1)
321 array[num3].StatusInformation = SR.GetString(
"Unknown_Error");
332 if (pCertContext ==
null || pCertContext.IsInvalid)
334 throw new ArgumentException(SR.GetString(
"Cryptography_InvalidContextHandle"),
"pCertContext");
337 if (extraStore !=
null && extraStore.Count > 0)
341 CAPIBase.CERT_CHAIN_PARA pChainPara =
default(CAPIBase.CERT_CHAIN_PARA);
342 pChainPara.cbSize = (uint)
Marshal.
SizeOf((
object)pChainPara);
343 SafeLocalAllocHandle safeLocalAllocHandle = SafeLocalAllocHandle.InvalidHandle;
344 SafeLocalAllocHandle safeLocalAllocHandle2 = SafeLocalAllocHandle.InvalidHandle;
347 if (applicationPolicy !=
null && applicationPolicy.Count > 0)
349 pChainPara.RequestedUsage.dwType = 0u;
350 pChainPara.RequestedUsage.Usage.cUsageIdentifier = (uint)applicationPolicy.Count;
352 pChainPara.RequestedUsage.Usage.rgpszUsageIdentifier = safeLocalAllocHandle.DangerousGetHandle();
354 if (certificatePolicy !=
null && certificatePolicy.Count > 0)
356 pChainPara.RequestedIssuancePolicy.dwType = 0u;
357 pChainPara.RequestedIssuancePolicy.Usage.cUsageIdentifier = (uint)certificatePolicy.Count;
359 pChainPara.RequestedIssuancePolicy.Usage.rgpszUsageIdentifier = safeLocalAllocHandle2.DangerousGetHandle();
361 pChainPara.dwUrlRetrievalTimeout = (uint)Math.Floor(timeout.TotalMilliseconds);
363 *(
long*)(&pTime) = verificationTime.ToFileTime();
365 if (!CAPISafe.CertGetCertificateChain(hChainEngine, pCertContext, ref pTime, hAdditionalStore, ref pChainPara, dwFlags, IntPtr.Zero, ref ppChainContext))
372 safeLocalAllocHandle.Dispose();
373 safeLocalAllocHandle2.Dispose();
The exception that is thrown when an error occurs during a cryptographic operation.
static int ReadInt32([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
Reads a 32-bit signed integer at a given offset from unmanaged memory.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
Describes a set of security permissions applied to code. This class cannot be inherited.
void Dispose()
Releases all of the resources used by this T:System.Security.Cryptography.X509Certificates....
X509Chain(bool useMachineContext)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
X509Certificate2Collection ExtraStore
Represents an additional collection of certificates that can be searched by the chaining engine when ...
static void SuppressFinalize(object obj)
Requests that the common language runtime not call the finalizer for the specified object.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
Provides a simple structure for storing X509 chain status and error information.
virtual void Dispose(bool disposing)
Releases the unmanaged resources used by this T:System.Security.Cryptography.X509Certificates....
Provides a mechanism for releasing unmanaged resources.To browse the .NET Framework source code for t...
static object CreateFromName(string name, params object[] args)
Creates a new instance of the specified cryptographic object with the specified arguments.
static int SizeOf(object structure)
Returns the unmanaged size of an object in bytes.
X509ChainPolicy ChainPolicy
Gets or sets the T:System.Security.Cryptography.X509Certificates.X509ChainPolicy to use when building...
Represents a collection of T:System.Security.Cryptography.X509Certificates.X509ChainElement objects....
static int GetHRForLastWin32Error()
Returns the HRESULT corresponding to the last error incurred by Win32 code executed using T:System....
X509RevocationFlag RevocationFlag
Gets or sets values for X509 revocation flags.
X509RevocationMode
Specifies the mode used to check for X509 certificate revocation.
IPermission AddPermission(IPermission perm)
Adds a specified permission to the T:System.Security.PermissionSet.
Represents a wrapper class for operating system handles. This class must be inherited.
DateTime VerificationTime
The time that the certificate was verified expressed in local time.
SecurityAction
Specifies the security actions that can be performed using declarative security.
X509ExtensionCollection Extensions
Gets a collection of T:System.Security.Cryptography.X509Certificates.X509Extension objects.
Represents a collection that can contain many different types of permissions.
Accesses the cryptography configuration information.
bool Build(X509Certificate2 certificate)
Builds an X.509 chain using the policy specified in T:System.Security.Cryptography....
OidCollection CertificatePolicy
Gets a collection of object identifiers (OIDs) specifying which certificate policies the certificate ...
Controls rights to access HTTP Internet resources.
Controls access to stores containing X.509 certificates. This class cannot be inherited.
A platform-specific type that is used to represent a pointer or a handle.
X509VerificationFlags VerificationFlags
Gets verification flags for the certificate.
Represents the chain policy to be applied when building an X509 certificate chain....
Provides a collection of methods for allocating unmanaged memory, copying unmanaged memory blocks,...
Represents a chain-building engine for T:System.Security.Cryptography.X509Certificates....
X509ChainStatus [] ChainStatus
Gets the status of each element in an T:System.Security.Cryptography.X509Certificates....
Controls the system garbage collector, a service that automatically reclaims unused memory.
static X509Chain Create()
Creates an T:System.Security.Cryptography.X509Certificates.X509Chain object after querying for the ma...
StorePermissionFlags
Specifies the permitted access to X.509 certificate stores.
The exception that is thrown when one of the arguments provided to a method is not valid.
void Demand()
Forces a T:System.Security.SecurityException at run time if all callers higher in the call stack have...
Represents the number of 100-nanosecond intervals since January 1, 1601. This structure is a 64-bit v...
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
static readonly IntPtr Zero
A read-only field that represents a pointer or handle that has been initialized to zero.
IntPtr ChainContext
Gets a handle to an X.509 chain.
X509Chain()
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
void Reset()
Clears the current T:System.Security.Cryptography.X509Certificates.X509Chain object.
TimeSpan UrlRetrievalTimeout
Gets the time span that elapsed during online revocation verification or downloading the certificate ...
X509ChainStatusFlags
Defines the status of an X509 chain.
static int GetLastWin32Error()
Returns the error code returned by the last unmanaged function that was called using platform invoke ...
X509RevocationMode RevocationMode
Gets or sets values for X509 certificate revocation mode.
SecurityPermissionFlag
Specifies access flags for the security permission object.
X509ChainElementCollection ChainElements
Gets a collection of T:System.Security.Cryptography.X509Certificates.X509ChainElement objects.
X509RevocationFlag
Specifies which X509 certificates in the chain should be checked for revocation.
X509Chain(IntPtr chainContext)
Initializes a new instance of the T:System.Security.Cryptography.X509Certificates....
OidCollection ApplicationPolicy
Gets a collection of object identifiers (OIDs) specifying which application policies or enhanced key ...
static IntPtr ReadIntPtr([In] [MarshalAs(UnmanagedType.AsAny)] object ptr, int ofs)
Reads a processor native sized integer from unmanaged memory.
Represents an X.509 certificate.