Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Core / Microsoft / Win32 / SafeHandles / BCryptSafeHandles.cs / 1305376 / BCryptSafeHandles.cs
// ==++==
//
// 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 {
///
/// SafeHandle representing a BCRYPT_ALG_HANDLE
///
//
//
//
#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;
}
}
///
/// Safe handle representing a BCRYPT_HASH_HANDLE and the associated buffer holding the hash object
///
//
//
//
#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) {
}
///
/// Buffer holding the hash object. This buffer should be allocated with Marshal.AllocCoTaskMem.
///
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;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++==
//
// 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 {
///
/// SafeHandle representing a BCRYPT_ALG_HANDLE
///
//
//
//
#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;
}
}
///
/// Safe handle representing a BCRYPT_HASH_HANDLE and the associated buffer holding the hash object
///
//
//
//
#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) {
}
///
/// Buffer holding the hash object. This buffer should be allocated with Marshal.AllocCoTaskMem.
///
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;
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- WebCategoryAttribute.cs
- InfiniteIntConverter.cs
- SystemColorTracker.cs
- HtmlInputImage.cs
- WrappedReader.cs
- XmlWriterDelegator.cs
- InstrumentationTracker.cs
- OdbcError.cs
- AdministrationHelpers.cs
- ProtectedProviderSettings.cs
- WindowsFormsHost.cs
- Processor.cs
- DetailsViewCommandEventArgs.cs
- GeometryGroup.cs
- FaultContractInfo.cs
- UnmanagedHandle.cs
- Ipv6Element.cs
- SpecialTypeDataContract.cs
- Select.cs
- DataGridViewImageColumn.cs
- RuntimeResourceSet.cs
- OleDbInfoMessageEvent.cs
- NavigationProperty.cs
- ClientSideProviderDescription.cs
- HtmlInputReset.cs
- EdmItemError.cs
- StreamGeometry.cs
- Comparer.cs
- ResourceReferenceExpression.cs
- EngineSite.cs
- Baml2006KeyRecord.cs
- TreeViewImageIndexConverter.cs
- TagMapInfo.cs
- ActivityXRefPropertyEditor.cs
- Schema.cs
- CryptographicAttribute.cs
- System.Data_BID.cs
- GlyphTypeface.cs
- InputMethodStateChangeEventArgs.cs
- COM2ICategorizePropertiesHandler.cs
- DetailsViewPageEventArgs.cs
- Drawing.cs
- PriorityItem.cs
- OleDbConnectionInternal.cs
- UpdatePanel.cs
- EventListenerClientSide.cs
- _ReceiveMessageOverlappedAsyncResult.cs
- Rijndael.cs
- AmbientValueAttribute.cs
- CustomAssemblyResolver.cs
- BinaryUtilClasses.cs
- Evidence.cs
- SeekableReadStream.cs
- SupportsEventValidationAttribute.cs
- TargetPerspective.cs
- PriorityRange.cs
- Tag.cs
- ControlUtil.cs
- SqlBooleanMismatchVisitor.cs
- VirtualPathUtility.cs
- FontWeightConverter.cs
- HeaderUtility.cs
- SqlTypeSystemProvider.cs
- GeneratedContractType.cs
- EventHandlerList.cs
- GreenMethods.cs
- CryptoConfig.cs
- ListenerElementsCollection.cs
- WebScriptMetadataMessageEncoderFactory.cs
- DetailsView.cs
- ViewStateException.cs
- ProcessModelInfo.cs
- EmbossBitmapEffect.cs
- XmlReflectionMember.cs
- EnumerableValidator.cs
- SafeRegistryKey.cs
- HashMembershipCondition.cs
- Interfaces.cs
- HttpClientCertificate.cs
- CustomAttributeBuilder.cs
- PersonalizationStateQuery.cs
- DataRelation.cs
- DesignTimeResourceProviderFactoryAttribute.cs
- DependencyPropertyHelper.cs
- RegisteredHiddenField.cs
- TabControlEvent.cs
- DataService.cs
- PageThemeCodeDomTreeGenerator.cs
- Header.cs
- NegotiateStream.cs
- PkcsMisc.cs
- Line.cs
- ToggleButton.cs
- ToolBarOverflowPanel.cs
- ButtonField.cs
- SafeFileHandle.cs
- ResourceDisplayNameAttribute.cs
- TakeOrSkipQueryOperator.cs
- RoamingStoreFileUtility.cs
- TableLayoutSettings.cs