Code:
/ Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / whidbey / NetFXspW7 / 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
- PagesChangedEventArgs.cs
- NumberFunctions.cs
- ReaderWriterLockWrapper.cs
- PropertyChangeTracker.cs
- MissingMethodException.cs
- ConsoleCancelEventArgs.cs
- GPRECTF.cs
- XmlSchemaAttributeGroupRef.cs
- PropertyFilterAttribute.cs
- SvcMapFileSerializer.cs
- FrugalList.cs
- ResolveCriteriaCD1.cs
- MessagePropertyFilter.cs
- JsonWriterDelegator.cs
- WindowsMenu.cs
- ConfigXmlAttribute.cs
- _AuthenticationState.cs
- HostingEnvironmentException.cs
- FileLogRecordStream.cs
- TypeListConverter.cs
- AttachedPropertyBrowsableForChildrenAttribute.cs
- PackageRelationshipSelector.cs
- DropShadowBitmapEffect.cs
- HelloMessage11.cs
- TimeSpanStorage.cs
- ProviderUtil.cs
- KeyTimeConverter.cs
- Bidi.cs
- CultureInfoConverter.cs
- DesignerSerializationVisibilityAttribute.cs
- RtfFormatStack.cs
- DataKeyCollection.cs
- _CommandStream.cs
- CodeCatchClause.cs
- BuildProvidersCompiler.cs
- MachineKeySection.cs
- DockPanel.cs
- WebHttpElement.cs
- AuthStoreRoleProvider.cs
- httpstaticobjectscollection.cs
- ManagementNamedValueCollection.cs
- IsolatedStorageException.cs
- AddInServer.cs
- HttpCookiesSection.cs
- ClientOptions.cs
- LogicalExpr.cs
- DataGridViewEditingControlShowingEventArgs.cs
- BCLDebug.cs
- DelayedRegex.cs
- TypeDescriptor.cs
- OdbcCommandBuilder.cs
- SectionVisual.cs
- AdapterUtil.cs
- SectionXmlInfo.cs
- ContentTextAutomationPeer.cs
- Rectangle.cs
- OleDbConnectionPoolGroupProviderInfo.cs
- EntityDataSourceEntityTypeFilterConverter.cs
- ParameterBuilder.cs
- QueryAccessibilityHelpEvent.cs
- ColorKeyFrameCollection.cs
- DataGridColumnsPage.cs
- ClientBuildManager.cs
- AttributeCollection.cs
- DataChangedEventManager.cs
- TemplateBindingExpressionConverter.cs
- Menu.cs
- MimeMultiPart.cs
- FontCollection.cs
- NotifyIcon.cs
- ELinqQueryState.cs
- SystemTcpConnection.cs
- ComponentCollection.cs
- TextEndOfLine.cs
- FlowLayout.cs
- XmlIlGenerator.cs
- TableStyle.cs
- BaseProcessor.cs
- GenericsInstances.cs
- MultiBindingExpression.cs
- EncryptedType.cs
- XmlSchemaAttributeGroup.cs
- MailAddressCollection.cs
- TraceHandlerErrorFormatter.cs
- ViewManager.cs
- CodeDirectoryCompiler.cs
- NumberEdit.cs
- ReadWriteSpinLock.cs
- TransactionManager.cs
- RelationshipType.cs
- InvalidDataException.cs
- AggregationMinMaxHelpers.cs
- SerializationEventsCache.cs
- QueryExpression.cs
- OracleRowUpdatingEventArgs.cs
- RequestCacheManager.cs
- CharacterBuffer.cs
- AdPostCacheSubstitution.cs
- UpdateRecord.cs
- StringFreezingAttribute.cs