3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // <OWNER>Microsoft</OWNER>
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
;
21 /// Safe handle representing a mscorwks!CRYPT_PROV_CTX
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
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) {
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()
57 #if FEATURE_CRYPTO || FEATURE_LEGACYNETCFCRYPTO
59 /// Safe handle representing a mscorkws!CRYPT_KEY_CTX
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
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) {
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()
98 /// SafeHandle representing a mscorwks!CRYPT_HASH_CTX
101 /// See code:System.Security.Cryptography.SafeKeyHandle for information about the release process
102 /// for a CRYPT_HASH_CTX.
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) {
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()
132 #endif // #if FEATURE_CRYPTO || FEATURE_LEGACYNETCFCRYPTO