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
- AutomationPropertyInfo.cs
- StreamSecurityUpgradeAcceptorBase.cs
- __Error.cs
- AnchoredBlock.cs
- StrokeNodeEnumerator.cs
- WebPartsSection.cs
- LongSumAggregationOperator.cs
- StringAnimationBase.cs
- WebPartDisplayModeCancelEventArgs.cs
- ToolStripContainerActionList.cs
- CompositeControl.cs
- XPathExpr.cs
- XmlSchemaSet.cs
- HttpClientChannel.cs
- ReachPageContentSerializerAsync.cs
- ProcessRequestAsyncResult.cs
- PropertyRef.cs
- CSharpCodeProvider.cs
- WizardForm.cs
- _IPv6Address.cs
- RtfToXamlReader.cs
- ScriptReference.cs
- RegistrySecurity.cs
- EventSetterHandlerConverter.cs
- FieldNameLookup.cs
- DrawListViewItemEventArgs.cs
- SiteMapSection.cs
- Executor.cs
- ArraySet.cs
- UmAlQuraCalendar.cs
- Int32EqualityComparer.cs
- InternalResources.cs
- BrowserCapabilitiesFactory35.cs
- Executor.cs
- infer.cs
- UInt32.cs
- SurrogateDataContract.cs
- XmlSchemaAnnotation.cs
- RightsDocument.cs
- StylusPointProperties.cs
- BindingContext.cs
- StaticFileHandler.cs
- SqlFlattener.cs
- SqlNamer.cs
- Matrix3DStack.cs
- FacetDescriptionElement.cs
- GeneralTransform3D.cs
- XamlStyleSerializer.cs
- SizeF.cs
- RoleManagerEventArgs.cs
- PixelFormatConverter.cs
- GrammarBuilderWildcard.cs
- ConstructorExpr.cs
- ActiveXSite.cs
- EventItfInfo.cs
- BooleanAnimationBase.cs
- StrokeRenderer.cs
- RangeValuePattern.cs
- JournalEntryStack.cs
- TemplateKey.cs
- CultureMapper.cs
- XmlSchemaInfo.cs
- LinqDataSourceContextEventArgs.cs
- DataGridItem.cs
- DefaultAssemblyResolver.cs
- NavigationCommands.cs
- SafeNativeMethods.cs
- PageVisual.cs
- _Events.cs
- ListBindingConverter.cs
- ActivityDesignerHighlighter.cs
- AppLevelCompilationSectionCache.cs
- ResourceDictionary.cs
- FixUpCollection.cs
- CommandExpr.cs
- MimeTypeAttribute.cs
- TypedTableBase.cs
- RequestCache.cs
- PeerServiceMessageContracts.cs
- StaticSiteMapProvider.cs
- LinkArea.cs
- DisplayMemberTemplateSelector.cs
- FixedTextSelectionProcessor.cs
- SiteMapDesignerDataSourceView.cs
- NoneExcludedImageIndexConverter.cs
- TypeSystem.cs
- KeyManager.cs
- SystemGatewayIPAddressInformation.cs
- PeerCollaboration.cs
- OdbcTransaction.cs
- AnimationStorage.cs
- Choices.cs
- WebPartsSection.cs
- CodeMethodInvokeExpression.cs
- SplitterEvent.cs
- IFlowDocumentViewer.cs
- Misc.cs
- XmlText.cs
- HttpCapabilitiesSectionHandler.cs
- DependencyObjectProvider.cs