Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / infocard / Client / System / IdentityModel / Selectors / CryptoHandle.cs / 1305376 / CryptoHandle.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- // // Presharp uses the c# pragma mechanism to supress its warnings. // These are not recognised by the base compiler so we need to explictly // disable the following warnings. See http://winweb/cse/Tools/PREsharp/userguide/default.asp // for details. // #pragma warning disable 1634, 1691 // unknown message, unknown pragma namespace System.IdentityModel.Selectors { using System; using System.Runtime.InteropServices; using System.Threading; using System.Security; using System.Runtime.ConstrainedExecution; using System.Runtime.CompilerServices; using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace; // // For common & resources // using Microsoft.InfoCards; // // Summary: // Wraps and manages the lifetime of a native crypto handle passed back from the native InfoCard API. // internal abstract class CryptoHandle : IDisposable { bool m_isDisposed; InternalRefCountedHandle m_internalHandle; // // Summary: // Creates a new CryptoHandle. ParamType has information as to what // nativeParameters has to be marshaled into. // protected CryptoHandle( InternalRefCountedHandle nativeHandle, DateTime expiration, IntPtr nativeParameters, Type paramType ) { m_internalHandle = nativeHandle; m_internalHandle.Initialize( expiration, Marshal.PtrToStructure( nativeParameters, paramType ) ); } // // Summary: // This constructor creates a new CryptoHandle instance with the same InternalRefCountedHandle and adds // a ref count to that InternalRefCountedHandle. // protected CryptoHandle( InternalRefCountedHandle internalHandle ) { m_internalHandle = internalHandle; m_internalHandle.AddRef(); } public InternalRefCountedHandle InternalHandle { get { ThrowIfDisposed(); return m_internalHandle; } } public DateTime Expiration { get { ThrowIfDisposed(); return m_internalHandle.Expiration; } } public object Parameters { get { ThrowIfDisposed(); return m_internalHandle.Parameters; } } // // Summary: // Creates a new CryptoHandle with same InternalRefCountedCryptoHandle. // public CryptoHandle Duplicate() { ThrowIfDisposed(); return OnDuplicate(); } // // Summary: // Allows subclasses to create a duplicate of their particular class. // protected abstract CryptoHandle OnDuplicate(); protected void ThrowIfDisposed() { if ( m_isDisposed ) { throw IDT.ThrowHelperError( new ObjectDisposedException( SR.GetString( SR.ClientCryptoSessionDisposed ) ) ); } } public void Dispose() { if ( m_isDisposed ) { return; } m_internalHandle.Release(); m_internalHandle = null; m_isDisposed = true; } // // Summary: // Given a pointer to a native cryptosession this method creates the appropriate CryptoHandle type. // static internal CryptoHandle Create( InternalRefCountedHandle nativeHandle ) { CryptoHandle handle = null; bool mustRelease = false; RuntimeHelpers.PrepareConstrainedRegions(); try { nativeHandle.DangerousAddRef( ref mustRelease ); RpcInfoCardCryptoHandle hCrypto = (RpcInfoCardCryptoHandle)Marshal.PtrToStructure( nativeHandle.DangerousGetHandle(), typeof( RpcInfoCardCryptoHandle ) ); DateTime expiration = DateTime.FromFileTimeUtc( hCrypto.expiration ); switch ( hCrypto.type ) { case RpcInfoCardCryptoHandle.HandleType.Asymmetric: handle = new AsymmetricCryptoHandle( nativeHandle, expiration, hCrypto.cryptoParameters ); break; case RpcInfoCardCryptoHandle.HandleType.Symmetric: handle = new SymmetricCryptoHandle( nativeHandle, expiration, hCrypto.cryptoParameters ); break; case RpcInfoCardCryptoHandle.HandleType.Transform: handle = new TransformCryptoHandle( nativeHandle, expiration, hCrypto.cryptoParameters ); break; case RpcInfoCardCryptoHandle.HandleType.Hash: handle = new HashCryptoHandle( nativeHandle, expiration, hCrypto.cryptoParameters ); break; default: IDT.DebugAssert( false, "Invalid crypto operation type" ); throw IDT.ThrowHelperError( new InvalidOperationException( SR.GetString( SR.GeneralExceptionMessage ) ) ); } return handle; } finally { if( mustRelease ) { nativeHandle.DangerousRelease(); } } } } // // Summary: // This class manages the lifetime of a native crypto handle through ref counts. Any number of CryptoHandles // may refer to a single InternalRefCountedHandle, but once they are all disposed this object will dispose // itself as well. // internal class InternalRefCountedHandle : SafeHandle { int m_refcount = 0; DateTime m_expiration; object m_parameters = null; [DllImport( "infocardapi.dll", EntryPoint ="CloseCryptoHandle", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true ) ] [SuppressUnmanagedCodeSecurity] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] private static extern bool CloseCryptoHandle( [In] IntPtr hKey ); private InternalRefCountedHandle() : base( IntPtr.Zero, true ) { m_refcount = 1; } public void Initialize( DateTime expiration, object parameters ) { m_expiration = expiration; m_parameters = parameters; } // // Summary: // The deserialized parameters specific to a particular type of CryptoHandle. // public object Parameters { get { ThrowIfInvalid(); return m_parameters; } } // // Summary: // The expiration of this CryptoHandle // public DateTime Expiration { get { ThrowIfInvalid(); return m_expiration; } } public void AddRef() { ThrowIfInvalid(); Interlocked.Increment( ref m_refcount ); } public void Release() { ThrowIfInvalid(); int refcount = Interlocked.Decrement( ref m_refcount ); if ( 0 == refcount ) { Dispose(); } } private void ThrowIfInvalid() { if ( IsInvalid ) { throw IDT.ThrowHelperError( new ObjectDisposedException( "InternalRefCountedHandle" ) ); } } public override bool IsInvalid { get { return ( IntPtr.Zero == base.handle ); } } protected override bool ReleaseHandle() { #pragma warning suppress 56523 return CloseCryptoHandle( base.handle ); } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- // // Presharp uses the c# pragma mechanism to supress its warnings. // These are not recognised by the base compiler so we need to explictly // disable the following warnings. See http://winweb/cse/Tools/PREsharp/userguide/default.asp // for details. // #pragma warning disable 1634, 1691 // unknown message, unknown pragma namespace System.IdentityModel.Selectors { using System; using System.Runtime.InteropServices; using System.Threading; using System.Security; using System.Runtime.ConstrainedExecution; using System.Runtime.CompilerServices; using IDT=Microsoft.InfoCards.Diagnostics.InfoCardTrace; // // For common & resources // using Microsoft.InfoCards; // // Summary: // Wraps and manages the lifetime of a native crypto handle passed back from the native InfoCard API. // internal abstract class CryptoHandle : IDisposable { bool m_isDisposed; InternalRefCountedHandle m_internalHandle; // // Summary: // Creates a new CryptoHandle. ParamType has information as to what // nativeParameters has to be marshaled into. // protected CryptoHandle( InternalRefCountedHandle nativeHandle, DateTime expiration, IntPtr nativeParameters, Type paramType ) { m_internalHandle = nativeHandle; m_internalHandle.Initialize( expiration, Marshal.PtrToStructure( nativeParameters, paramType ) ); } // // Summary: // This constructor creates a new CryptoHandle instance with the same InternalRefCountedHandle and adds // a ref count to that InternalRefCountedHandle. // protected CryptoHandle( InternalRefCountedHandle internalHandle ) { m_internalHandle = internalHandle; m_internalHandle.AddRef(); } public InternalRefCountedHandle InternalHandle { get { ThrowIfDisposed(); return m_internalHandle; } } public DateTime Expiration { get { ThrowIfDisposed(); return m_internalHandle.Expiration; } } public object Parameters { get { ThrowIfDisposed(); return m_internalHandle.Parameters; } } // // Summary: // Creates a new CryptoHandle with same InternalRefCountedCryptoHandle. // public CryptoHandle Duplicate() { ThrowIfDisposed(); return OnDuplicate(); } // // Summary: // Allows subclasses to create a duplicate of their particular class. // protected abstract CryptoHandle OnDuplicate(); protected void ThrowIfDisposed() { if ( m_isDisposed ) { throw IDT.ThrowHelperError( new ObjectDisposedException( SR.GetString( SR.ClientCryptoSessionDisposed ) ) ); } } public void Dispose() { if ( m_isDisposed ) { return; } m_internalHandle.Release(); m_internalHandle = null; m_isDisposed = true; } // // Summary: // Given a pointer to a native cryptosession this method creates the appropriate CryptoHandle type. // static internal CryptoHandle Create( InternalRefCountedHandle nativeHandle ) { CryptoHandle handle = null; bool mustRelease = false; RuntimeHelpers.PrepareConstrainedRegions(); try { nativeHandle.DangerousAddRef( ref mustRelease ); RpcInfoCardCryptoHandle hCrypto = (RpcInfoCardCryptoHandle)Marshal.PtrToStructure( nativeHandle.DangerousGetHandle(), typeof( RpcInfoCardCryptoHandle ) ); DateTime expiration = DateTime.FromFileTimeUtc( hCrypto.expiration ); switch ( hCrypto.type ) { case RpcInfoCardCryptoHandle.HandleType.Asymmetric: handle = new AsymmetricCryptoHandle( nativeHandle, expiration, hCrypto.cryptoParameters ); break; case RpcInfoCardCryptoHandle.HandleType.Symmetric: handle = new SymmetricCryptoHandle( nativeHandle, expiration, hCrypto.cryptoParameters ); break; case RpcInfoCardCryptoHandle.HandleType.Transform: handle = new TransformCryptoHandle( nativeHandle, expiration, hCrypto.cryptoParameters ); break; case RpcInfoCardCryptoHandle.HandleType.Hash: handle = new HashCryptoHandle( nativeHandle, expiration, hCrypto.cryptoParameters ); break; default: IDT.DebugAssert( false, "Invalid crypto operation type" ); throw IDT.ThrowHelperError( new InvalidOperationException( SR.GetString( SR.GeneralExceptionMessage ) ) ); } return handle; } finally { if( mustRelease ) { nativeHandle.DangerousRelease(); } } } } // // Summary: // This class manages the lifetime of a native crypto handle through ref counts. Any number of CryptoHandles // may refer to a single InternalRefCountedHandle, but once they are all disposed this object will dispose // itself as well. // internal class InternalRefCountedHandle : SafeHandle { int m_refcount = 0; DateTime m_expiration; object m_parameters = null; [DllImport( "infocardapi.dll", EntryPoint ="CloseCryptoHandle", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true ) ] [SuppressUnmanagedCodeSecurity] [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] private static extern bool CloseCryptoHandle( [In] IntPtr hKey ); private InternalRefCountedHandle() : base( IntPtr.Zero, true ) { m_refcount = 1; } public void Initialize( DateTime expiration, object parameters ) { m_expiration = expiration; m_parameters = parameters; } // // Summary: // The deserialized parameters specific to a particular type of CryptoHandle. // public object Parameters { get { ThrowIfInvalid(); return m_parameters; } } // // Summary: // The expiration of this CryptoHandle // public DateTime Expiration { get { ThrowIfInvalid(); return m_expiration; } } public void AddRef() { ThrowIfInvalid(); Interlocked.Increment( ref m_refcount ); } public void Release() { ThrowIfInvalid(); int refcount = Interlocked.Decrement( ref m_refcount ); if ( 0 == refcount ) { Dispose(); } } private void ThrowIfInvalid() { if ( IsInvalid ) { throw IDT.ThrowHelperError( new ObjectDisposedException( "InternalRefCountedHandle" ) ); } } public override bool IsInvalid { get { return ( IntPtr.Zero == base.handle ); } } protected override bool ReleaseHandle() { #pragma warning suppress 56523 return CloseCryptoHandle( base.handle ); } } } // 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
- UrlPath.cs
- CellParagraph.cs
- SchemaElement.cs
- DataGridViewTextBoxColumn.cs
- ContentPresenter.cs
- XmlSerializer.cs
- AssemblyName.cs
- XhtmlConformanceSection.cs
- xsdvalidator.cs
- PageSettings.cs
- UserControl.cs
- CLSCompliantAttribute.cs
- Stack.cs
- ColorMatrix.cs
- _AcceptOverlappedAsyncResult.cs
- BitmapDecoder.cs
- LineBreak.cs
- FillBehavior.cs
- VarRefManager.cs
- UrlAuthFailedErrorFormatter.cs
- CompiledQuery.cs
- InkCanvasSelection.cs
- SafeFindHandle.cs
- SecurityBindingElementImporter.cs
- SchemaImporter.cs
- BindingOperations.cs
- VariantWrapper.cs
- PrivilegedConfigurationManager.cs
- Pkcs9Attribute.cs
- HttpClientCertificate.cs
- DictionaryBase.cs
- OperatingSystem.cs
- PartialCachingControl.cs
- XmlElementList.cs
- BooleanSwitch.cs
- ErrorWebPart.cs
- WebReferencesBuildProvider.cs
- BamlWriter.cs
- SiteMapPath.cs
- TransformationRules.cs
- Encoder.cs
- Section.cs
- PageFunction.cs
- util.cs
- ListSortDescription.cs
- DynamicFilter.cs
- TextLine.cs
- MultiPropertyDescriptorGridEntry.cs
- PolicyImporterElement.cs
- ParameterCollection.cs
- CursorEditor.cs
- BindingSource.cs
- TextDecoration.cs
- PrimaryKeyTypeConverter.cs
- Internal.cs
- SafeArrayTypeMismatchException.cs
- NavigationPropertyEmitter.cs
- RuntimeConfigurationRecord.cs
- TextLine.cs
- RoleManagerModule.cs
- FrameSecurityDescriptor.cs
- TransformerInfo.cs
- BinaryFormatter.cs
- XsltFunctions.cs
- HasCopySemanticsAttribute.cs
- BinaryObjectWriter.cs
- ObjectDesignerDataSourceView.cs
- Int32CAMarshaler.cs
- Button.cs
- ControlTemplate.cs
- Hyperlink.cs
- EditorZoneBase.cs
- HtmlContainerControl.cs
- SynchronizationLockException.cs
- MemberInfoSerializationHolder.cs
- SQLInt16Storage.cs
- SimpleApplicationHost.cs
- CqlIdentifiers.cs
- AssemblyCollection.cs
- ExcludeFromCodeCoverageAttribute.cs
- ResourceProviderFactory.cs
- SchemaNames.cs
- SafeHandles.cs
- PersistChildrenAttribute.cs
- ModelEditingScope.cs
- InternalBufferOverflowException.cs
- AssemblyBuilder.cs
- figurelengthconverter.cs
- ClientTarget.cs
- SizeValueSerializer.cs
- SmtpCommands.cs
- ToolStripItemCollection.cs
- EnterpriseServicesHelper.cs
- GenerateScriptTypeAttribute.cs
- LinearGradientBrush.cs
- MessageHeaderAttribute.cs
- XsltCompileContext.cs
- Queue.cs
- Track.cs
- MailDefinition.cs