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
- DataColumnMappingCollection.cs
- Pair.cs
- Classification.cs
- FieldAccessException.cs
- PropertyGroupDescription.cs
- TreeViewEvent.cs
- FontCollection.cs
- KeyEventArgs.cs
- CompilerTypeWithParams.cs
- SharedPerformanceCounter.cs
- Statements.cs
- SelectingProviderEventArgs.cs
- ExpressionLexer.cs
- SizeIndependentAnimationStorage.cs
- EnumMember.cs
- AssemblyInfo.cs
- Serializer.cs
- BaseServiceProvider.cs
- MaterialGroup.cs
- Parser.cs
- FullTextBreakpoint.cs
- LowerCaseStringConverter.cs
- EntityViewGenerationConstants.cs
- UpdateRecord.cs
- SendMailErrorEventArgs.cs
- MatrixTransform.cs
- DiagnosticsConfiguration.cs
- SoapInteropTypes.cs
- JsonStringDataContract.cs
- XNameConverter.cs
- RadioButtonAutomationPeer.cs
- controlskin.cs
- SQlBooleanStorage.cs
- CryptoApi.cs
- AddressUtility.cs
- Int32EqualityComparer.cs
- PartBasedPackageProperties.cs
- RtfControls.cs
- GlyphCache.cs
- VarRefManager.cs
- TraceRecord.cs
- ParserOptions.cs
- DetailsViewRowCollection.cs
- MessageHeaderDescriptionCollection.cs
- TraceContextRecord.cs
- EntitySqlQueryState.cs
- WebPartActionVerb.cs
- OleDbRowUpdatedEvent.cs
- ByteAnimation.cs
- PermissionToken.cs
- TypeBinaryExpression.cs
- OleDbConnection.cs
- ListArgumentProvider.cs
- ConstraintEnumerator.cs
- XmlSchemaSimpleContentExtension.cs
- Transform.cs
- CodeGenerator.cs
- DoubleAnimationUsingKeyFrames.cs
- SemaphoreSlim.cs
- ThreadStateException.cs
- CollectionViewSource.cs
- CompilerResults.cs
- InputLanguageEventArgs.cs
- BooleanStorage.cs
- GcHandle.cs
- ItemAutomationPeer.cs
- CompositeControl.cs
- glyphs.cs
- GridViewItemAutomationPeer.cs
- SimpleHandlerBuildProvider.cs
- DeflateStream.cs
- SerialReceived.cs
- UnitySerializationHolder.cs
- SecurityKeyEntropyMode.cs
- XhtmlConformanceSection.cs
- UIPermission.cs
- MessageParameterAttribute.cs
- UserMapPath.cs
- OdbcException.cs
- SystemTcpConnection.cs
- EmissiveMaterial.cs
- ConditionCollection.cs
- ImportCatalogPart.cs
- ProfileManager.cs
- SqlCommandBuilder.cs
- OracleEncoding.cs
- RayHitTestParameters.cs
- InkCanvasAutomationPeer.cs
- ipaddressinformationcollection.cs
- SqlGenerator.cs
- MemberProjectedSlot.cs
- TextDecoration.cs
- TableLayoutPanel.cs
- KeyValueSerializer.cs
- TdsParserStateObject.cs
- ActivityDesignerAccessibleObject.cs
- AssociatedControlConverter.cs
- keycontainerpermission.cs
- cookieexception.cs
- AutomationProperties.cs