Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / safecryptohandles.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // <OWNER>[....]</OWNER>
7 // 
8
9 //
10 // SafeCryptoHandles.cs
11 //
12
13 namespace System.Security.Cryptography {
14     using System.Runtime.InteropServices;
15     using System.Runtime.CompilerServices;
16     using System.Runtime.ConstrainedExecution;
17     using System.Runtime.Versioning;
18     using Microsoft.Win32.SafeHandles;
19
20     /// <summary>
21     ///     Safe handle representing a mscorwks!CRYPT_PROV_CTX
22     /// </summary>
23     /// <remarks>
24     ///     Since we need sometimes to delete the key container created in the context of the CSP, the handle
25     ///     used in this class is actually a pointer to a CRYPT_PROV_CTX unmanaged structure defined in
26     ///     COMCryptography.h
27     /// </remarks>
28     [System.Security.SecurityCritical]  // auto-generated
29     internal sealed class SafeProvHandle : SafeHandleZeroOrMinusOneIsInvalid {
30
31         private SafeProvHandle() : base(true) {
32             SetHandle(IntPtr.Zero);
33         }
34
35         private SafeProvHandle(IntPtr handle) : base (true) {
36             SetHandle(handle);
37         }
38
39         internal static SafeProvHandle InvalidHandle {
40             get { return new SafeProvHandle(); }
41         }
42
43         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
44         [ResourceExposure(ResourceScope.None)]
45         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
46         [SuppressUnmanagedCodeSecurity]
47         private static extern void FreeCsp(IntPtr pProviderContext);
48
49         [System.Security.SecurityCritical]
50         protected override bool ReleaseHandle()
51         {
52             FreeCsp(handle);
53             return true;
54         }
55     }
56
57 #if FEATURE_CRYPTO || FEATURE_LEGACYNETCFCRYPTO
58     /// <summary>
59     ///     Safe handle representing a mscorkws!CRYPT_KEY_CTX
60     /// </summary>
61     /// <summary>
62     ///     Since we need to delete the key handle before the provider is released we need to actually hold a
63     ///     pointer to a CRYPT_KEY_CTX unmanaged structure whose destructor decrements a refCount. Only when
64     ///     the provider refCount is 0 it is deleted. This way, we loose a ---- in the critical finalization
65     ///     of the key handle and provider handle. This also applies to hash handles, which point to a 
66     ///     CRYPT_HASH_CTX. Those strucutres are defined in COMCryptography.h
67     /// </summary>
68     [System.Security.SecurityCritical]  // auto-generated
69     internal sealed class SafeKeyHandle : SafeHandleZeroOrMinusOneIsInvalid {
70
71         private SafeKeyHandle() : base(true) {
72             SetHandle(IntPtr.Zero);
73         }
74
75         private SafeKeyHandle(IntPtr handle) : base (true) {
76             SetHandle(handle);
77         }
78
79         internal static SafeKeyHandle InvalidHandle {
80             get { return new SafeKeyHandle(); }
81         }
82
83         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
84         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
85         [ResourceExposure(ResourceScope.None)]
86         [SuppressUnmanagedCodeSecurity]
87         private static extern void FreeKey(IntPtr pKeyCotext);
88
89         [System.Security.SecurityCritical]
90         protected override bool ReleaseHandle()
91         {
92             FreeKey(handle);
93             return true;
94         }
95     }
96
97     /// <summary>
98     ///     SafeHandle representing a mscorwks!CRYPT_HASH_CTX
99     /// </summary>
100     /// <remarks>
101     ///     See code:System.Security.Cryptography.SafeKeyHandle for information about the release process
102     ///     for a CRYPT_HASH_CTX.
103     /// </remarks>
104     [System.Security.SecurityCritical]  // auto-generated
105     internal sealed class SafeHashHandle : SafeHandleZeroOrMinusOneIsInvalid {
106
107         private SafeHashHandle() : base(true) {
108             SetHandle(IntPtr.Zero);
109         }
110
111         private SafeHashHandle(IntPtr handle) : base (true) {
112             SetHandle(handle);
113         }
114
115         internal static SafeHashHandle InvalidHandle {
116             get { return new SafeHashHandle(); }
117         }
118
119         [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
120         [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
121         [ResourceExposure(ResourceScope.None)]
122         [SuppressUnmanagedCodeSecurity]
123         private static extern void FreeHash(IntPtr pHashContext);
124
125         [System.Security.SecurityCritical]
126         protected override bool ReleaseHandle()
127         {
128             FreeHash(handle);
129             return true;
130         }
131     }
132 #endif // #if FEATURE_CRYPTO || FEATURE_LEGACYNETCFCRYPTO
133 }
134