File: BCryptSafeHandles.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (94 lines) | stat: -rw-r--r-- 3,437 bytes parent folder | download | duplicates (7)
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
// ==++==
// 
//   Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// ==--==

using System;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Cryptography;
using System.Diagnostics.Contracts;

namespace Microsoft.Win32.SafeHandles {
    /// <summary>
    ///     SafeHandle representing a BCRYPT_ALG_HANDLE
    /// </summary>
#pragma warning disable 618    // Have not migrated to v4 transparency yet
    [System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
#pragma warning restore 618
    internal sealed class SafeBCryptAlgorithmHandle : SafeHandleZeroOrMinusOneIsInvalid {
        private SafeBCryptAlgorithmHandle() : base(true) {
        }

        [DllImport("bcrypt")]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]
        private static extern BCryptNative.ErrorCode BCryptCloseAlgorithmProvider(IntPtr hAlgorithm, int flags);

        protected override bool ReleaseHandle() {
            return BCryptCloseAlgorithmProvider(handle, 0) == BCryptNative.ErrorCode.Success;
        }
    }

    /// <summary>
    ///     Safe handle representing a BCRYPT_HASH_HANDLE and the associated buffer holding the hash object
    /// </summary>
#pragma warning disable 618    // Have not migrated to v4 transparency yet
    [System.Security.SecurityCritical(System.Security.SecurityCriticalScope.Everything)]
#pragma warning restore 618
    internal sealed class SafeBCryptHashHandle : SafeHandleZeroOrMinusOneIsInvalid {
        private IntPtr m_hashObject;

        private SafeBCryptHashHandle() : base(true) {
        }

        /// <summary>
        ///     Buffer holding the hash object. This buffer should be allocated with Marshal.AllocCoTaskMem.
        /// </summary>
        internal IntPtr HashObject {
            get { return m_hashObject; }

            set {
                Contract.Requires(value != IntPtr.Zero);
                m_hashObject = value;
            }
        }


        [DllImport("bcrypt")]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]
        private static extern BCryptNative.ErrorCode BCryptDestroyHash(IntPtr hHash);

        protected override bool ReleaseHandle() {
            bool success = BCryptDestroyHash(handle) == BCryptNative.ErrorCode.Success;

            // The hash object buffer must be released only after destroying the hash handle
            if (m_hashObject != IntPtr.Zero) {
                Marshal.FreeCoTaskMem(m_hashObject);
            }

            return success;
        }
    }

    /// <summary>
    ///     SafeHandle for a native BCRYPT_KEY_HANDLE.
    /// </summary>
    [SecuritySafeCritical]
    internal sealed class SafeBCryptKeyHandle : SafeHandleZeroOrMinusOneIsInvalid {
        internal SafeBCryptKeyHandle(): base(true){ }

        [DllImport("bcrypt.dll")]
        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        [SuppressUnmanagedCodeSecurity]        
        internal static extern BCryptNative.ErrorCode BCryptDestroyKey(IntPtr hKey);

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        protected override bool ReleaseHandle() {
            return BCryptDestroyKey(handle) == BCryptNative.ErrorCode.Success;
        }
    }
}