Merge pull request #2964 from ludovic-henry/sgen-monocontext
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / x509certificates / safex509handles.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6
7 //
8 // SafeX509Handles.cs
9 //
10
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;
17
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
21
22     [System.Security.SecurityCritical]  // auto-generated
23     internal sealed class SafeCertContextHandle : SafeHandleZeroOrMinusOneIsInvalid {
24         private SafeCertContextHandle() : base (true) {}
25
26         // 0 is an Invalid Handle
27         internal SafeCertContextHandle(IntPtr handle) : base (true) {
28             SetHandle(handle);
29         }
30
31         internal static SafeCertContextHandle InvalidHandle {
32             get { return new SafeCertContextHandle(IntPtr.Zero); }
33         }
34
35         internal IntPtr pCertContext {
36             get {
37                 if (handle == IntPtr.Zero)
38                     return IntPtr.Zero;
39
40                 return Marshal.ReadIntPtr(handle);
41             }
42         }
43
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);
49
50         [System.Security.SecurityCritical]
51         override protected bool ReleaseHandle()
52         {
53             _FreePCertContext(handle);
54             return true;
55         }
56     }
57
58     [System.Security.SecurityCritical]  // auto-generated
59     internal sealed class SafeCertStoreHandle : SafeHandleZeroOrMinusOneIsInvalid {
60         private SafeCertStoreHandle() : base (true) {}
61
62         // 0 is an Invalid Handle
63         internal SafeCertStoreHandle(IntPtr handle) : base (true) {
64             SetHandle(handle);
65         }
66
67         internal static SafeCertStoreHandle InvalidHandle {
68             get { return new SafeCertStoreHandle(IntPtr.Zero); }
69         }
70
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);
76
77         [System.Security.SecurityCritical]
78         override protected bool ReleaseHandle()
79         {
80             _FreeCertStoreContext(handle);
81             return true;
82         }
83     }
84 }