3 // Copyright (c) Microsoft Corporation. All rights reserved.
11 namespace System.Security.Cryptography.X509Certificates {
12 using System.Runtime.InteropServices;
13 using System.Runtime.CompilerServices;
14 using System.Runtime.ConstrainedExecution;
15 using System.Runtime.Versioning;
16 using Microsoft.Win32.SafeHandles;
18 // Since we need sometimes to delete the key container associated with a cert
19 // context, the handle used in this class is actually a pointer
20 // to a CERT_CTX unmanaged structure defined in COMX509Certificate.h
22 [System.Security.SecurityCritical] // auto-generated
23 internal sealed class SafeCertContextHandle : SafeHandleZeroOrMinusOneIsInvalid {
24 private SafeCertContextHandle() : base (true) {}
26 // 0 is an Invalid Handle
27 internal SafeCertContextHandle(IntPtr handle) : base (true) {
31 internal static SafeCertContextHandle InvalidHandle {
32 get { return new SafeCertContextHandle(IntPtr.Zero); }
35 internal IntPtr pCertContext {
37 if (handle == IntPtr.Zero)
40 return Marshal.ReadIntPtr(handle);
44 // This method handles the case where pCert == NULL
45 [ResourceExposure(ResourceScope.None)]
46 [MethodImplAttribute(MethodImplOptions.InternalCall)]
47 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
48 private static extern void _FreePCertContext(IntPtr pCert);
50 [System.Security.SecurityCritical]
51 override protected bool ReleaseHandle()
53 _FreePCertContext(handle);
58 [System.Security.SecurityCritical] // auto-generated
59 internal sealed class SafeCertStoreHandle : SafeHandleZeroOrMinusOneIsInvalid {
60 private SafeCertStoreHandle() : base (true) {}
62 // 0 is an Invalid Handle
63 internal SafeCertStoreHandle(IntPtr handle) : base (true) {
67 internal static SafeCertStoreHandle InvalidHandle {
68 get { return new SafeCertStoreHandle(IntPtr.Zero); }
71 // This method handles the case where hCertStore == NULL
72 [ResourceExposure(ResourceScope.None)]
73 [MethodImplAttribute(MethodImplOptions.InternalCall)]
74 [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
75 private static extern void _FreeCertStoreContext(IntPtr hCertStore);
77 [System.Security.SecurityCritical]
78 override protected bool ReleaseHandle()
80 _FreeCertStoreContext(handle);