Code:
/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / whidbey / NetFxQFE / 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
- _SingleItemRequestCache.cs
- HttpStaticObjectsCollectionWrapper.cs
- DashStyle.cs
- EntityDataSourceStatementEditor.cs
- Serializer.cs
- BamlLocalizer.cs
- Soap.cs
- selecteditemcollection.cs
- HttpVersion.cs
- StackOverflowException.cs
- ScriptingAuthenticationServiceSection.cs
- ListViewInsertionMark.cs
- RC2CryptoServiceProvider.cs
- MetadataSerializer.cs
- BitmapFrame.cs
- DocumentScope.cs
- InvalidDataContractException.cs
- _TLSstream.cs
- DataTrigger.cs
- EditorPartCollection.cs
- PageCopyCount.cs
- CompensationDesigner.cs
- SQLBoolean.cs
- ISAPIWorkerRequest.cs
- ErrorLog.cs
- DropShadowEffect.cs
- EncodingInfo.cs
- TextTreeInsertUndoUnit.cs
- WizardStepBase.cs
- WeakKeyDictionary.cs
- ProfessionalColors.cs
- StateDesigner.TransitionInfo.cs
- DataBinding.cs
- XNodeSchemaApplier.cs
- AuthenticationException.cs
- LassoSelectionBehavior.cs
- ToolStripSettings.cs
- PlainXmlDeserializer.cs
- MarkupCompilePass2.cs
- TextServicesCompartmentEventSink.cs
- Faults.cs
- DataTemplateKey.cs
- COM2ComponentEditor.cs
- TraceContextEventArgs.cs
- ExceptionValidationRule.cs
- PrimitiveXmlSerializers.cs
- EntitySqlQueryBuilder.cs
- DeferredReference.cs
- Baml2006KnownTypes.cs
- SapiInterop.cs
- XmlAtomicValue.cs
- SplitContainer.cs
- HtmlInputControl.cs
- TaskSchedulerException.cs
- XPathSelfQuery.cs
- Queue.cs
- GroupItem.cs
- Semaphore.cs
- LogWriteRestartAreaState.cs
- PerfCounterSection.cs
- XmlDeclaration.cs
- CircleEase.cs
- RepeaterCommandEventArgs.cs
- CLSCompliantAttribute.cs
- TimeStampChecker.cs
- AttachedAnnotation.cs
- ValueProviderWrapper.cs
- BitmapVisualManager.cs
- TypeForwardedToAttribute.cs
- TemplateInstanceAttribute.cs
- LogRestartAreaEnumerator.cs
- AutoCompleteStringCollection.cs
- StandardToolWindows.cs
- CheckBoxFlatAdapter.cs
- DragEvent.cs
- ThrowHelper.cs
- StyleReferenceConverter.cs
- ExceptionUtil.cs
- TextTreeTextNode.cs
- CodeAttributeArgumentCollection.cs
- SqlWebEventProvider.cs
- SettingsProviderCollection.cs
- TypeUtil.cs
- DataGridViewAutoSizeColumnsModeEventArgs.cs
- formatter.cs
- SchemaElementLookUpTable.cs
- Visual3D.cs
- OleDbConnectionInternal.cs
- FileSystemWatcher.cs
- BindMarkupExtensionSerializer.cs
- EntityTypeEmitter.cs
- TreeNodeMouseHoverEvent.cs
- AssociationSet.cs
- TypeSource.cs
- EditorZoneBase.cs
- UrlMappingCollection.cs
- QuadraticEase.cs
- SourceLineInfo.cs
- XmlSchemaCompilationSettings.cs
- CodeParameterDeclarationExpression.cs