public void Dispose()
 {
     if (this.m_transCryptoHandle != null)
     {
         this.m_transCryptoHandle.Dispose();
         this.m_transCryptoHandle = null;
     }
 }
 public void Dispose()
 {
     if (null != m_transCryptoHandle)
     {
         m_transCryptoHandle.Dispose();
         m_transCryptoHandle = null;
     }
 }
        //
        // 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();
                }
            }
        }
        internal static CryptoHandle Create(InternalRefCountedHandle nativeHandle)
        {
            CryptoHandle handle = null;
            CryptoHandle handle3;
            bool         success = false;

            RuntimeHelpers.PrepareConstrainedRegions();
            try
            {
                nativeHandle.DangerousAddRef(ref success);
                RpcInfoCardCryptoHandle handle2 = (RpcInfoCardCryptoHandle)Marshal.PtrToStructure(nativeHandle.DangerousGetHandle(), typeof(RpcInfoCardCryptoHandle));
                DateTime expiration             = DateTime.FromFileTimeUtc(handle2.expiration);
                switch (handle2.type)
                {
                case RpcInfoCardCryptoHandle.HandleType.Asymmetric:
                    handle = new AsymmetricCryptoHandle(nativeHandle, expiration, handle2.cryptoParameters);
                    break;

                case RpcInfoCardCryptoHandle.HandleType.Symmetric:
                    handle = new SymmetricCryptoHandle(nativeHandle, expiration, handle2.cryptoParameters);
                    break;

                case RpcInfoCardCryptoHandle.HandleType.Transform:
                    handle = new TransformCryptoHandle(nativeHandle, expiration, handle2.cryptoParameters);
                    break;

                case RpcInfoCardCryptoHandle.HandleType.Hash:
                    handle = new HashCryptoHandle(nativeHandle, expiration, handle2.cryptoParameters);
                    break;

                default:
                    throw InfoCardTrace.ThrowHelperError(new InvalidOperationException(Microsoft.InfoCards.SR.GetString("GeneralExceptionMessage")));
                }
                handle3 = handle;
            }
            finally
            {
                if (success)
                {
                    nativeHandle.DangerousRelease();
                }
            }
            return(handle3);
        }
            public CryptoTransform(InfoCardSymmetricAlgorithm symAlgo, Direction cryptoDirection)
            {
                InternalRefCountedHandle nativeTransformHandle = null;

                byte[] iV = symAlgo.IV;
                using (HGlobalSafeHandle handle2 = HGlobalSafeHandle.Construct(iV.Length))
                {
                    Marshal.Copy(iV, 0, handle2.DangerousGetHandle(), iV.Length);
                    int status = CardSpaceSelector.GetShim().m_csShimGetCryptoTransform(symAlgo.m_cryptoHandle.InternalHandle, (int)symAlgo.Mode, (int)symAlgo.Padding, symAlgo.FeedbackSize, (int)cryptoDirection, iV.Length, handle2, out nativeTransformHandle);
                    if (status != 0)
                    {
                        InfoCardTrace.CloseInvalidOutSafeHandle(nativeTransformHandle);
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw InfoCardTrace.ThrowHelperError(new Win32Exception(status));
                    }
                    this.m_transCryptoHandle = (TransformCryptoHandle)CryptoHandle.Create(nativeTransformHandle);
                    this.m_param             = (RpcTransformCryptoParameters)this.m_transCryptoHandle.Parameters;
                }
            }
            //
            // Parameters:
            //  symAlgo  - the algorithm being requested.
            //  cryptoDirection - determines whether the transform will encrypt or decrypt.
            //
            public CryptoTransform(InfoCardSymmetricAlgorithm symAlgo, Direction cryptoDirection)
            {
                InternalRefCountedHandle nativeHandle = null;

                byte[] iv = symAlgo.IV;
                using (HGlobalSafeHandle pIV = HGlobalSafeHandle.Construct(iv.Length))
                {
                    //
                    // Marshal the initialization vector.
                    //
                    Marshal.Copy(iv, 0, pIV.DangerousGetHandle(), iv.Length);

                    //
                    // Call native method to get a handle to a native transform.
                    //
                    int status = CardSpaceSelector.GetShim().m_csShimGetCryptoTransform(symAlgo.m_cryptoHandle.InternalHandle,
                                                                                        (int)symAlgo.Mode,
                                                                                        (int)symAlgo.Padding,
                                                                                        symAlgo.FeedbackSize,
                                                                                        (int)cryptoDirection,
                                                                                        iv.Length,
                                                                                        pIV,
                                                                                        out nativeHandle);

                    if (0 != status)
                    {
                        IDT.CloseInvalidOutSafeHandle(nativeHandle);
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw IDT.ThrowHelperError(new Win32Exception(status));
                    }

                    m_transCryptoHandle = (TransformCryptoHandle)CryptoHandle.Create(nativeHandle);

                    m_param = (RpcTransformCryptoParameters)m_transCryptoHandle.Parameters;
                }
            }
 public CryptoTransform(InfoCardSymmetricAlgorithm symAlgo, Direction cryptoDirection)
 {
     InternalRefCountedHandle nativeTransformHandle = null;
     byte[] iV = symAlgo.IV;
     using (HGlobalSafeHandle handle2 = HGlobalSafeHandle.Construct(iV.Length))
     {
         Marshal.Copy(iV, 0, handle2.DangerousGetHandle(), iV.Length);
         int status = CardSpaceSelector.GetShim().m_csShimGetCryptoTransform(symAlgo.m_cryptoHandle.InternalHandle, (int) symAlgo.Mode, (int) symAlgo.Padding, symAlgo.FeedbackSize, (int) cryptoDirection, iV.Length, handle2, out nativeTransformHandle);
         if (status != 0)
         {
             InfoCardTrace.CloseInvalidOutSafeHandle(nativeTransformHandle);
             ExceptionHelper.ThrowIfCardSpaceException(status);
             throw InfoCardTrace.ThrowHelperError(new Win32Exception(status));
         }
         this.m_transCryptoHandle = (TransformCryptoHandle) CryptoHandle.Create(nativeTransformHandle);
         this.m_param = (RpcTransformCryptoParameters) this.m_transCryptoHandle.Parameters;
     }
 }
 public void Dispose()
 {
     if (this.m_transCryptoHandle != null)
     {
         this.m_transCryptoHandle.Dispose();
         this.m_transCryptoHandle = null;
     }
 }
 public void Dispose()
 {
     if (null != m_transCryptoHandle)
     {
         m_transCryptoHandle.Dispose();
         m_transCryptoHandle = null;
     }
 }
            //
            // Parameters:
            //  symAlgo  - the algorithm being requested.
            //  cryptoDirection - determines whether the transform will encrypt or decrypt.
            //
            public CryptoTransform(InfoCardSymmetricAlgorithm symAlgo, Direction cryptoDirection)
            {
                InternalRefCountedHandle nativeHandle = null;
                byte[] iv = symAlgo.IV;
                using (HGlobalSafeHandle pIV = HGlobalSafeHandle.Construct(iv.Length))
                {
                    //
                    // Marshal the initialization vector.
                    //
                    Marshal.Copy(iv, 0, pIV.DangerousGetHandle(), iv.Length);

                    //
                    // Call native method to get a handle to a native transform.
                    //
                    int status = CardSpaceSelector.GetShim().m_csShimGetCryptoTransform(symAlgo.m_cryptoHandle.InternalHandle,
                                                                   (int)symAlgo.Mode,
                                                                   (int)symAlgo.Padding,
                                                                   symAlgo.FeedbackSize,
                                                                   (int)cryptoDirection,
                                                                   iv.Length,
                                                                   pIV,
                                                                   out nativeHandle);

                    if (0 != status)
                    {
                        IDT.CloseInvalidOutSafeHandle(nativeHandle);
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw IDT.ThrowHelperError(new Win32Exception(status));
                    }

                    m_transCryptoHandle = (TransformCryptoHandle)CryptoHandle.Create(nativeHandle);

                    m_param = (RpcTransformCryptoParameters)m_transCryptoHandle.Parameters;
                }

            }
        //
        // 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();
                }
            }
        }