Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / mscorlib / system / security / cryptography / safecryptohandles.cs
blobe7dc76c0944ad4f5f7ae8ead91c32cb868f6ddff
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 // <OWNER>Microsoft</OWNER>
7 //
9 //
10 // SafeCryptoHandles.cs
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;
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 {
31 private SafeProvHandle() : base(true) {
32 SetHandle(IntPtr.Zero);
35 private SafeProvHandle(IntPtr handle) : base (true) {
36 SetHandle(handle);
39 internal static SafeProvHandle InvalidHandle {
40 get { return new SafeProvHandle(); }
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);
49 [System.Security.SecurityCritical]
50 protected override bool ReleaseHandle()
52 FreeCsp(handle);
53 return true;
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 {
71 private SafeKeyHandle() : base(true) {
72 SetHandle(IntPtr.Zero);
75 private SafeKeyHandle(IntPtr handle) : base (true) {
76 SetHandle(handle);
79 internal static SafeKeyHandle InvalidHandle {
80 get { return new SafeKeyHandle(); }
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);
89 [System.Security.SecurityCritical]
90 protected override bool ReleaseHandle()
92 FreeKey(handle);
93 return true;
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 {
107 private SafeHashHandle() : base(true) {
108 SetHandle(IntPtr.Zero);
111 private SafeHashHandle(IntPtr handle) : base (true) {
112 SetHandle(handle);
115 internal static SafeHashHandle InvalidHandle {
116 get { return new SafeHashHandle(); }
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);
125 [System.Security.SecurityCritical]
126 protected override bool ReleaseHandle()
128 FreeHash(handle);
129 return true;
132 #endif // #if FEATURE_CRYPTO || FEATURE_LEGACYNETCFCRYPTO