Code:
/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / 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 race 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; } } }
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- ExtendedTransformFactory.cs
- RawStylusSystemGestureInputReport.cs
- ToolStripControlHost.cs
- HwndProxyElementProvider.cs
- ILGenerator.cs
- SrgsToken.cs
- ChangeBlockUndoRecord.cs
- DataGridViewCellToolTipTextNeededEventArgs.cs
- ToolStripItemTextRenderEventArgs.cs
- ConfigurationStrings.cs
- XmlC14NWriter.cs
- DesignTimeTemplateParser.cs
- Table.cs
- StrokeCollection.cs
- CodeDOMUtility.cs
- DataGridViewCellErrorTextNeededEventArgs.cs
- HttpStaticObjectsCollectionBase.cs
- ClientApiGenerator.cs
- ComPersistableTypeElementCollection.cs
- RC2.cs
- DesignerOptionService.cs
- ScrollBar.cs
- ImageMapEventArgs.cs
- RowToParametersTransformer.cs
- Debugger.cs
- Processor.cs
- CollectionDataContract.cs
- XmlElementAttribute.cs
- SoapInteropTypes.cs
- ToolTipService.cs
- ElasticEase.cs
- DoubleStorage.cs
- TagPrefixCollection.cs
- ImageField.cs
- MexHttpBindingCollectionElement.cs
- DebugInfoGenerator.cs
- SchemaImporterExtensionElement.cs
- ProgressBar.cs
- AccessedThroughPropertyAttribute.cs
- CodeBinaryOperatorExpression.cs
- WindowsProgressbar.cs
- Stacktrace.cs
- GridViewCancelEditEventArgs.cs
- Icon.cs
- OdbcEnvironmentHandle.cs
- EncodingInfo.cs
- ValuePattern.cs
- Pair.cs
- SharedStatics.cs
- ComAdminWrapper.cs
- RichTextBox.cs
- VolatileEnlistmentState.cs
- WorkflowViewManager.cs
- DataGridViewBand.cs
- SamlAttribute.cs
- SchemaImporterExtension.cs
- ConfigurationException.cs
- XmlReader.cs
- PackageDocument.cs
- Item.cs
- BaseDataList.cs
- DataConnectionHelper.cs
- TypeSystemHelpers.cs
- FileSecurity.cs
- OSFeature.cs
- OracleFactory.cs
- RegexGroup.cs
- AliasExpr.cs
- ObjectViewEntityCollectionData.cs
- ObjectDataSourceFilteringEventArgs.cs
- XmlIgnoreAttribute.cs
- AnnotationResourceChangedEventArgs.cs
- Regex.cs
- BitmapData.cs
- WinFormsSecurity.cs
- XPathNodePointer.cs
- MachineKeySection.cs
- ButtonField.cs
- SafeSystemMetrics.cs
- Color.cs
- ITreeGenerator.cs
- MemoryRecordBuffer.cs
- WinHttpWebProxyFinder.cs
- PersonalizationProviderCollection.cs
- TypeBuilderInstantiation.cs
- DetailsViewPageEventArgs.cs
- Interfaces.cs
- TextEffect.cs
- ResourceManagerWrapper.cs
- ConfigurationPropertyAttribute.cs
- SqlUdtInfo.cs
- StrongTypingException.cs
- ObjectStateManagerMetadata.cs
- InstanceHandleReference.cs
- ExtentCqlBlock.cs
- NameValueFileSectionHandler.cs
- BuildDependencySet.cs
- BinaryObjectInfo.cs
- ClientScriptManager.cs
- SiteMapHierarchicalDataSourceView.cs