Code:
/ FX-1434 / FX-1434 / 1.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
- HttpProfileGroupBase.cs
- XmlDocumentSerializer.cs
- CodeParameterDeclarationExpression.cs
- HeaderUtility.cs
- XPathConvert.cs
- SendKeys.cs
- LineInfo.cs
- StatusBarPanel.cs
- DataGridViewCellStyleConverter.cs
- UnknownWrapper.cs
- HtmlShimManager.cs
- SettingsSection.cs
- COM2PropertyDescriptor.cs
- DbReferenceCollection.cs
- NonBatchDirectoryCompiler.cs
- XmlCodeExporter.cs
- AlignmentYValidation.cs
- ControlUtil.cs
- DataGridViewSelectedRowCollection.cs
- WpfKnownType.cs
- ClientApiGenerator.cs
- TransformPattern.cs
- IsolatedStorageException.cs
- Config.cs
- DataGridViewBand.cs
- DetailsViewPageEventArgs.cs
- AddInStore.cs
- WorkflowServiceOperationListItem.cs
- MdiWindowListStrip.cs
- EmptyElement.cs
- PrintPreviewControl.cs
- AppSettings.cs
- ClientSettingsStore.cs
- ScrollViewer.cs
- EntityCommandExecutionException.cs
- PromptStyle.cs
- HandlerMappingMemo.cs
- TriggerCollection.cs
- CellCreator.cs
- TrackingProfileDeserializationException.cs
- TreeNodeBinding.cs
- BaseTemplateParser.cs
- ActiveXHelper.cs
- DeclarativeCatalogPart.cs
- QueryOperatorEnumerator.cs
- BackgroundFormatInfo.cs
- WebPartEditorApplyVerb.cs
- HelpEvent.cs
- Aes.cs
- ProcessThread.cs
- _FtpDataStream.cs
- CollectionBuilder.cs
- ResXResourceReader.cs
- NativeMethods.cs
- TextBoxView.cs
- HttpDictionary.cs
- Ops.cs
- Number.cs
- Pair.cs
- FrameworkEventSource.cs
- NoneExcludedImageIndexConverter.cs
- Setter.cs
- HierarchicalDataBoundControl.cs
- SetStateEventArgs.cs
- FontNamesConverter.cs
- CounterCreationDataCollection.cs
- PrivilegedConfigurationManager.cs
- EdmComplexTypeAttribute.cs
- HandlerWithFactory.cs
- AppSettingsExpressionBuilder.cs
- BindableAttribute.cs
- ProxyGenerator.cs
- CustomBinding.cs
- TreeView.cs
- ClearTypeHintValidation.cs
- RoleManagerEventArgs.cs
- PathGeometry.cs
- TabItemWrapperAutomationPeer.cs
- HttpCapabilitiesSectionHandler.cs
- HandleExceptionArgs.cs
- DocumentSequence.cs
- SessionStateUtil.cs
- HttpListenerResponse.cs
- InfoCardPolicy.cs
- IPEndPoint.cs
- SelectionWordBreaker.cs
- InstanceLockLostException.cs
- X509CertificateCollection.cs
- NavigationProgressEventArgs.cs
- TimeoutStream.cs
- CircleHotSpot.cs
- Function.cs
- UriSectionData.cs
- SQLUtility.cs
- MetadataArtifactLoaderFile.cs
- SolidBrush.cs
- Trigger.cs
- TemplatePropertyEntry.cs
- Array.cs
- MessagePropertyVariants.cs