Code:
/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / clr / src / BCL / System / Security / Cryptography / SafeCryptoHandles.cs / 1 / SafeCryptoHandles.cs
// ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // // SafeCryptoHandles.cs // namespace System.Security.Cryptography { using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using Microsoft.Win32.SafeHandles; // 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 internal sealed class SafeProvHandle : SafeHandleZeroOrMinusOneIsInvalid { // 0 is an Invalid Handle private SafeProvHandle(IntPtr handle) : base (true) { SetHandle(handle); } internal static SafeProvHandle InvalidHandle { get { return new SafeProvHandle(IntPtr.Zero); } } // This method handles the case where pProvCtx == NULL [MethodImplAttribute(MethodImplOptions.InternalCall)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] // The handle here is actually a pointer to a CRYPT_PROV_CTX unmanaged structure private static extern void _FreeCSP(IntPtr pProvCtx); override protected bool ReleaseHandle() { _FreeCSP(handle); return true; } } // 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 loose a ---- 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 strucutres are defined in COMCryptography.h internal sealed class SafeKeyHandle : SafeHandleZeroOrMinusOneIsInvalid { // 0 is an Invalid Handle private SafeKeyHandle(IntPtr handle) : base (true) { SetHandle(handle); } internal static SafeKeyHandle InvalidHandle { get { return new SafeKeyHandle(IntPtr.Zero); } } [MethodImplAttribute(MethodImplOptions.InternalCall)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] // The handle here is actually a pointer to a CRYPT_KEY_CTX unmanaged structure private static extern void _FreeHKey(IntPtr pKeyCtx); override protected bool ReleaseHandle() { _FreeHKey(handle); return true; } } internal sealed class SafeHashHandle : SafeHandleZeroOrMinusOneIsInvalid { // 0 is an Invalid Handle private SafeHashHandle(IntPtr handle) : base (true) { SetHandle(handle); } internal static SafeHashHandle InvalidHandle { get { return new SafeHashHandle(IntPtr.Zero); } } [MethodImplAttribute(MethodImplOptions.InternalCall)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] // The handle here is actually a pointer to a CRYPT_HASH_CTX unmanaged structure private static extern void _FreeHash(IntPtr pHashCtx); override protected bool ReleaseHandle() { _FreeHash(handle); return true; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // ==++== // // Copyright (c) Microsoft Corporation. All rights reserved. // // ==--== // // SafeCryptoHandles.cs // namespace System.Security.Cryptography { using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using Microsoft.Win32.SafeHandles; // 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 internal sealed class SafeProvHandle : SafeHandleZeroOrMinusOneIsInvalid { // 0 is an Invalid Handle private SafeProvHandle(IntPtr handle) : base (true) { SetHandle(handle); } internal static SafeProvHandle InvalidHandle { get { return new SafeProvHandle(IntPtr.Zero); } } // This method handles the case where pProvCtx == NULL [MethodImplAttribute(MethodImplOptions.InternalCall)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] // The handle here is actually a pointer to a CRYPT_PROV_CTX unmanaged structure private static extern void _FreeCSP(IntPtr pProvCtx); override protected bool ReleaseHandle() { _FreeCSP(handle); return true; } } // 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 loose a ---- 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 strucutres are defined in COMCryptography.h internal sealed class SafeKeyHandle : SafeHandleZeroOrMinusOneIsInvalid { // 0 is an Invalid Handle private SafeKeyHandle(IntPtr handle) : base (true) { SetHandle(handle); } internal static SafeKeyHandle InvalidHandle { get { return new SafeKeyHandle(IntPtr.Zero); } } [MethodImplAttribute(MethodImplOptions.InternalCall)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] // The handle here is actually a pointer to a CRYPT_KEY_CTX unmanaged structure private static extern void _FreeHKey(IntPtr pKeyCtx); override protected bool ReleaseHandle() { _FreeHKey(handle); return true; } } internal sealed class SafeHashHandle : SafeHandleZeroOrMinusOneIsInvalid { // 0 is an Invalid Handle private SafeHashHandle(IntPtr handle) : base (true) { SetHandle(handle); } internal static SafeHashHandle InvalidHandle { get { return new SafeHashHandle(IntPtr.Zero); } } [MethodImplAttribute(MethodImplOptions.InternalCall)] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] // The handle here is actually a pointer to a CRYPT_HASH_CTX unmanaged structure private static extern void _FreeHash(IntPtr pHashCtx); override protected bool ReleaseHandle() { _FreeHash(handle); return true; } } } // 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
- GridViewRowPresenter.cs
- LocalizationParserHooks.cs
- CreateParams.cs
- ElementNotAvailableException.cs
- Adorner.cs
- CoreSwitches.cs
- BinaryWriter.cs
- HttpResponse.cs
- TimeoutHelper.cs
- PackageRelationshipCollection.cs
- GorillaCodec.cs
- NotificationContext.cs
- WindowsStartMenu.cs
- SafeMILHandle.cs
- NativeMethods.cs
- FactoryGenerator.cs
- EarlyBoundInfo.cs
- Select.cs
- SqlProfileProvider.cs
- HtmlTableRow.cs
- IDispatchConstantAttribute.cs
- WindowsStatusBar.cs
- DefaultHttpHandler.cs
- AsyncOperation.cs
- DESCryptoServiceProvider.cs
- IndexedEnumerable.cs
- AlgoModule.cs
- And.cs
- UnsafeNativeMethods.cs
- EncodingNLS.cs
- ClientConvert.cs
- LostFocusEventManager.cs
- SettingsBase.cs
- Dictionary.cs
- DateTimeOffsetConverter.cs
- QueryContinueDragEvent.cs
- RenderContext.cs
- AstTree.cs
- ScriptReferenceBase.cs
- ObjectQueryProvider.cs
- TrimSurroundingWhitespaceAttribute.cs
- KeyEvent.cs
- SoapEnumAttribute.cs
- GridEntry.cs
- ContentType.cs
- NamedObject.cs
- AppearanceEditorPart.cs
- OdbcEnvironmentHandle.cs
- NativeWrapper.cs
- BaseValidator.cs
- EdmValidator.cs
- XmlHierarchicalEnumerable.cs
- ResourceDisplayNameAttribute.cs
- ColorPalette.cs
- RSAOAEPKeyExchangeFormatter.cs
- SqlNodeAnnotations.cs
- BaseDataListComponentEditor.cs
- WSHttpBindingCollectionElement.cs
- newitemfactory.cs
- SelectedDatesCollection.cs
- ThaiBuddhistCalendar.cs
- SortFieldComparer.cs
- WpfGeneratedKnownProperties.cs
- GridViewAutoFormat.cs
- TraceUtils.cs
- NegatedConstant.cs
- DoubleAnimationUsingPath.cs
- ComponentEditorForm.cs
- InvariantComparer.cs
- Debug.cs
- _HTTPDateParse.cs
- documentation.cs
- ScriptServiceAttribute.cs
- XmlDataLoader.cs
- CheckBoxField.cs
- formatstringdialog.cs
- NumberSubstitution.cs
- EventItfInfo.cs
- TableCellCollection.cs
- DataSourceXmlSerializationAttribute.cs
- GacUtil.cs
- DataGridColumnDropSeparator.cs
- OwnerDrawPropertyBag.cs
- PreProcessor.cs
- WindowsAuthenticationModule.cs
- IdlingCommunicationPool.cs
- RowTypeElement.cs
- XmlSchemaAny.cs
- ColumnReorderedEventArgs.cs
- LinearKeyFrames.cs
- SortDescription.cs
- RoleService.cs
- SymmetricKey.cs
- METAHEADER.cs
- PeerNameRecordCollection.cs
- TakeQueryOptionExpression.cs
- oledbmetadatacollectionnames.cs
- CollectionChangeEventArgs.cs
- TypeElementCollection.cs
- ClientRoleProvider.cs