1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using System; 6 using System.Diagnostics; 7 using System.Security; 8 using System.Runtime.InteropServices; 9 10 namespace Microsoft.Win32.SafeHandles 11 { 12 internal sealed class SafeEcKeyHandle : SafeHandle 13 { SafeEcKeyHandle()14 private SafeEcKeyHandle() : 15 base(IntPtr.Zero, ownsHandle: true) 16 { 17 } 18 ReleaseHandle()19 protected override bool ReleaseHandle() 20 { 21 Interop.Crypto.EcKeyDestroy(handle); 22 SetHandle(IntPtr.Zero); 23 return true; 24 } 25 26 public override bool IsInvalid 27 { 28 get { return handle == IntPtr.Zero; } 29 } 30 DuplicateHandle(IntPtr handle)31 internal static SafeEcKeyHandle DuplicateHandle(IntPtr handle) 32 { 33 Debug.Assert(handle != IntPtr.Zero); 34 35 // Reliability: Allocate the SafeHandle before calling EC_KEY_up_ref so 36 // that we don't lose a tracked reference in low-memory situations. 37 SafeEcKeyHandle safeHandle = new SafeEcKeyHandle(); 38 39 if (!Interop.Crypto.EcKeyUpRef(handle)) 40 { 41 throw Interop.Crypto.CreateOpenSslCryptographicException(); 42 } 43 44 safeHandle.SetHandle(handle); 45 return safeHandle; 46 } 47 } 48 } 49