summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Security/Cryptography/SafeCryptoHandles.cs
blob: 7cf85f683fb8a2f0e7184cbe27d5b587d522242d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace System.Security.Cryptography {
    using System.Runtime.InteropServices;
    using System.Runtime.CompilerServices;
    using System.Runtime.ConstrainedExecution;
    using System.Runtime.Versioning;
    using Microsoft.Win32.SafeHandles;

    /// <summary>
    ///     Safe handle representing a mscorwks!CRYPT_PROV_CTX
    /// </summary>
    /// <remarks>
    ///     Since we need sometimes to delete the key container created in the context of the CSP, the handle
    ///     used in this class is actually a pointer to a CRYPT_PROV_CTX unmanaged structure defined in
    ///     COMCryptography.h
    /// </remarks>
    [System.Security.SecurityCritical]  // auto-generated
    internal sealed class SafeProvHandle : SafeHandleZeroOrMinusOneIsInvalid {

        private SafeProvHandle() : base(true) {
            SetHandle(IntPtr.Zero);
        }

        private SafeProvHandle(IntPtr handle) : base (true) {
            SetHandle(handle);
        }

        internal static SafeProvHandle InvalidHandle {
            get { return new SafeProvHandle(); }
        }

        [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]
        private static extern void FreeCsp(IntPtr pProviderContext);

        [System.Security.SecurityCritical]
        protected override bool ReleaseHandle()
        {
            FreeCsp(handle);
            return true;
        }
    }

    /// <summary>
    ///     Safe handle representing a mscorkws!CRYPT_KEY_CTX
    /// </summary>
    /// <summary>
    ///     Since we need to delete the key handle before the provider is released we need to actually hold a
    ///     pointer to a CRYPT_KEY_CTX unmanaged structure whose destructor decrements a refCount. Only when
    ///     the provider refCount is 0 it is deleted. This way, we raced and lost in the critical finalization
    ///     of the key handle and provider handle. This also applies to hash handles, which point to a 
    ///     CRYPT_HASH_CTX. Those structures are defined in COMCryptography.h
    /// </summary>
    [System.Security.SecurityCritical]  // auto-generated
    internal sealed class SafeKeyHandle : SafeHandleZeroOrMinusOneIsInvalid {

        private SafeKeyHandle() : base(true) {
            SetHandle(IntPtr.Zero);
        }

        private SafeKeyHandle(IntPtr handle) : base (true) {
            SetHandle(handle);
        }

        internal static SafeKeyHandle InvalidHandle {
            get { return new SafeKeyHandle(); }
        }

        [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]
        private static extern void FreeKey(IntPtr pKeyCotext);

        [System.Security.SecurityCritical]
        protected override bool ReleaseHandle()
        {
            FreeKey(handle);
            return true;
        }
    }

    /// <summary>
    ///     SafeHandle representing a mscorwks!CRYPT_HASH_CTX
    /// </summary>
    /// <remarks>
    ///     See code:System.Security.Cryptography.SafeKeyHandle for information about the release process
    ///     for a CRYPT_HASH_CTX.
    /// </remarks>
    [System.Security.SecurityCritical]  // auto-generated
    internal sealed class SafeHashHandle : SafeHandleZeroOrMinusOneIsInvalid {

        private SafeHashHandle() : base(true) {
            SetHandle(IntPtr.Zero);
        }

        private SafeHashHandle(IntPtr handle) : base (true) {
            SetHandle(handle);
        }

        internal static SafeHashHandle InvalidHandle {
            get { return new SafeHashHandle(); }
        }

        [DllImport(JitHelpers.QCall, CharSet = CharSet.Unicode)]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]
        private static extern void FreeHash(IntPtr pHashContext);

        [System.Security.SecurityCritical]
        protected override bool ReleaseHandle()
        {
            FreeHash(handle);
            return true;
        }
    }
}