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
- CheckBoxStandardAdapter.cs
- DelayDesigner.cs
- GacUtil.cs
- Point3DCollection.cs
- AvTrace.cs
- SqlBuilder.cs
- Comparer.cs
- FileVersion.cs
- FrameworkEventSource.cs
- SelectionWordBreaker.cs
- TypeSystem.cs
- TdsRecordBufferSetter.cs
- DataGridViewCellValueEventArgs.cs
- ToolStripItemTextRenderEventArgs.cs
- ListViewTableRow.cs
- EnumerationRangeValidationUtil.cs
- XmlDataSource.cs
- SrgsToken.cs
- parserscommon.cs
- ConfigurationElementProperty.cs
- MsmqBindingElementBase.cs
- ELinqQueryState.cs
- GetPageCompletedEventArgs.cs
- AutoFocusStyle.xaml.cs
- TypeConverterHelper.cs
- ConfigurationElement.cs
- DataReceivedEventArgs.cs
- XmlArrayItemAttributes.cs
- SR.cs
- ColumnResizeUndoUnit.cs
- InputLanguage.cs
- XsltSettings.cs
- ScriptControlManager.cs
- SmiMetaData.cs
- ConfigXmlCDataSection.cs
- Knowncolors.cs
- TCPClient.cs
- DetailsViewDeleteEventArgs.cs
- tooltip.cs
- Window.cs
- PermissionToken.cs
- XmlMembersMapping.cs
- Cursor.cs
- GeneralTransform2DTo3DTo2D.cs
- DispatcherObject.cs
- CodeNamespaceImportCollection.cs
- ExtendedProperty.cs
- Attribute.cs
- DataGridGeneralPage.cs
- Transform3DGroup.cs
- WebPartAddingEventArgs.cs
- DataPagerFieldItem.cs
- WebPartsPersonalizationAuthorization.cs
- UnmanagedBitmapWrapper.cs
- PersonalizationAdministration.cs
- UndoManager.cs
- SerTrace.cs
- DecodeHelper.cs
- SynchronizedDispatch.cs
- SignatureDescription.cs
- SystemIcmpV6Statistics.cs
- PageAsyncTaskManager.cs
- KeyGestureValueSerializer.cs
- TextDecorationLocationValidation.cs
- KeyBinding.cs
- XamlRtfConverter.cs
- HttpTransportSecurity.cs
- ConnectionStringsExpressionBuilder.cs
- Matrix.cs
- RealizationContext.cs
- WebPartDisplayMode.cs
- EntityCommandCompilationException.cs
- VScrollBar.cs
- QilTargetType.cs
- TextRangeBase.cs
- SwitchAttribute.cs
- WizardForm.cs
- GridViewCellAutomationPeer.cs
- Convert.cs
- ComponentChangedEvent.cs
- ConcurrentDictionary.cs
- FileVersion.cs
- LineServices.cs
- SendMailErrorEventArgs.cs
- MyContact.cs
- ActivityDesignerHighlighter.cs
- SupportsEventValidationAttribute.cs
- JpegBitmapEncoder.cs
- SafeArrayTypeMismatchException.cs
- WorkflowViewManager.cs
- SqlClientMetaDataCollectionNames.cs
- DataGridCaption.cs
- Transform3DGroup.cs
- DataGridViewRowHeightInfoNeededEventArgs.cs
- ReceiveMessageRecord.cs
- PagesSection.cs
- Size3D.cs
- DataGridViewImageCell.cs
- ImportCatalogPart.cs
- ComponentResourceKeyConverter.cs