public InfoCardKeyedHashAlgorithm(SymmetricCryptoHandle cryptoHandle)
        {
            InternalRefCountedHandle nativeHashHandle = null;

            try
            {
                int status = CardSpaceSelector.GetShim().m_csShimGetKeyedHash(cryptoHandle.InternalHandle, out nativeHashHandle);
                if (status != 0)
                {
                    InfoCardTrace.CloseInvalidOutSafeHandle(nativeHashHandle);
                    ExceptionHelper.ThrowIfCardSpaceException(status);
                    throw InfoCardTrace.ThrowHelperError(new Win32Exception(status));
                }
                this.m_cryptoHandle = (HashCryptoHandle)CryptoHandle.Create(nativeHashHandle);
                this.m_param        = (RpcHashCryptoParameters)this.m_cryptoHandle.Parameters;
            }
            catch
            {
                if (this.m_cryptoHandle != null)
                {
                    this.m_cryptoHandle.Dispose();
                }
                throw;
            }
        }
 protected override void HashCore(byte[] array, int ibStart, int cbSize)
 {
     if (this.m_cachedBlock != null)
     {
         using (HGlobalSafeHandle handle = null)
         {
             if (this.m_cachedBlock.Length != 0)
             {
                 handle = HGlobalSafeHandle.Construct(this.m_cachedBlock.Length);
                 Marshal.Copy(this.m_cachedBlock, 0, handle.DangerousGetHandle(), this.m_cachedBlock.Length);
             }
             int status = CardSpaceSelector.GetShim().m_csShimHashCore(this.m_cryptoHandle.InternalHandle, this.m_cachedBlock.Length, (handle != null) ? handle : HGlobalSafeHandle.Construct());
             if (status != 0)
             {
                 ExceptionHelper.ThrowIfCardSpaceException(status);
                 throw InfoCardTrace.ThrowHelperError(new Win32Exception(status));
             }
         }
     }
     if (this.m_cachedBlock != null)
     {
         Array.Clear(this.m_cachedBlock, 0, this.m_cachedBlock.Length);
     }
     this.m_cachedBlock = DiagnosticUtility.Utility.AllocateByteArray(cbSize);
     Array.Copy(array, ibStart, this.m_cachedBlock, 0, cbSize);
 }
        public byte[] Decrypt(byte[] inData, bool fAOEP)
        {
            GlobalAllocSafeHandle pOutData = null;
            int cbOutData = 0;

            byte[] outData;
            IDT.ThrowInvalidArgumentConditional(null == inData, "indata");
            using (HGlobalSafeHandle pInData = HGlobalSafeHandle.Construct(inData.Length))
            {
                Marshal.Copy(inData, 0, pInData.DangerousGetHandle(), inData.Length);
                int status = CardSpaceSelector.GetShim().m_csShimDecrypt(m_cryptoHandle.InternalHandle,
                                                                         fAOEP,
                                                                         inData.Length,
                                                                         pInData,
                                                                         out cbOutData,
                                                                         out pOutData);

                if (0 != status)
                {
                    ExceptionHelper.ThrowIfCardSpaceException(status);
                    throw IDT.ThrowHelperError(new Win32Exception(status));
                }
                pOutData.Length = cbOutData;
                outData         = DiagnosticUtility.Utility.AllocateByteArray(pOutData.Length);
                using (pOutData)
                {
                    Marshal.Copy(pOutData.DangerousGetHandle(), outData, 0, pOutData.Length);
                }
            }

            return(outData);
        }
        public byte[] Decrypt(byte[] inData, bool fAOEP)
        {
            GlobalAllocSafeHandle pOutData = null;

            byte[] buffer;
            int    pcbOutData = 0;

            InfoCardTrace.ThrowInvalidArgumentConditional(null == inData, "indata");
            using (HGlobalSafeHandle handle2 = HGlobalSafeHandle.Construct(inData.Length))
            {
                Marshal.Copy(inData, 0, handle2.DangerousGetHandle(), inData.Length);
                int status = CardSpaceSelector.GetShim().m_csShimDecrypt(this.m_cryptoHandle.InternalHandle, fAOEP, inData.Length, handle2, out pcbOutData, out pOutData);
                if (status != 0)
                {
                    ExceptionHelper.ThrowIfCardSpaceException(status);
                    throw InfoCardTrace.ThrowHelperError(new Win32Exception(status));
                }
                pOutData.Length = pcbOutData;
                buffer          = DiagnosticUtility.Utility.AllocateByteArray(pOutData.Length);
                using (pOutData)
                {
                    Marshal.Copy(pOutData.DangerousGetHandle(), buffer, 0, pOutData.Length);
                }
            }
            return(buffer);
        }
        //
        // Summary:
        //  Marshals the PolicyElement to it's native format.
        //
        // Parameters:
        //  ptr  - A pointer to native memory in which to place the native format of the PolicyElement.  Must be
        //         a buffer atleast as large as this.Size.
        //
        public void DoMarshal(IntPtr ptr)
        {
            string target = m_element.Target.OuterXml;
            string issuer = "";

            IDT.DebugAssert(IntPtr.Zero == m_nativePtr, "Pointer already assigned");

            m_nativePtr = ptr;
            if (m_element.Issuer != null)
            {
                issuer = m_element.Issuer.OuterXml;
            }
            string tokenParameters = string.Empty;

            if (null != m_element.Parameters)
            {
                tokenParameters = CardSpaceSelector.XmlToString(m_element.Parameters);
            }

            m_nativeElement.targetEndpointAddress = target;
            m_nativeElement.issuerEndpointAddress = issuer;
            m_nativeElement.issuedTokenParameters = tokenParameters;
            m_nativeElement.policyNoticeLink      = null != m_element.PolicyNoticeLink ? m_element.PolicyNoticeLink.ToString() : null;
            m_nativeElement.policyNoticeVersion   = m_element.PolicyNoticeVersion;
            m_nativeElement.isManagedCardProvider = m_element.IsManagedIssuer;

            Marshal.StructureToPtr(m_nativeElement, ptr, false);

            return;
        }
        public bool VerifyHash(byte[] hash, string hashAlgOid, byte[] sig)
        {
            InfoCardTrace.ThrowInvalidArgumentConditional((hash == null) || (0 == hash.Length), "hash");
            InfoCardTrace.ThrowInvalidArgumentConditional(string.IsNullOrEmpty(hashAlgOid), "hashAlgOid");
            InfoCardTrace.ThrowInvalidArgumentConditional((sig == null) || (0 == sig.Length), "sig");
            bool verified = false;

            using (HGlobalSafeHandle handle = HGlobalSafeHandle.Construct(hash.Length))
            {
                using (HGlobalSafeHandle handle2 = HGlobalSafeHandle.Construct(hashAlgOid))
                {
                    Marshal.Copy(hash, 0, handle.DangerousGetHandle(), hash.Length);
                    int status = 0;
                    using (HGlobalSafeHandle handle3 = HGlobalSafeHandle.Construct(sig.Length))
                    {
                        Marshal.Copy(sig, 0, handle3.DangerousGetHandle(), sig.Length);
                        status = CardSpaceSelector.GetShim().m_csShimVerifyHash(this.m_cryptoHandle.InternalHandle, hash.Length, handle, handle2, sig.Length, handle3, out verified);
                    }
                    if (status != 0)
                    {
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw InfoCardTrace.ThrowHelperError(new Win32Exception(status));
                    }
                }
            }
            return(verified);
        }
            //
            // Summary:
            //  Special function for transforming the last block or partial block in the stream.  The
            //  return value is an array containting the remaining transformed bytes.
            //  We return a new array here because the amount of information we send back at the end could
            //  be larger than a single block once padding is accounted for.
            //
            // Parameters:
            //  inputBuffer  - The input for which to compute the transform.
            //  inputOffset  - The offset into the byte array from which to begin using data.
            //  inputCount   - The number of bytes in the byte array to use as data.
            //
            // Returns:
            //  The computed transform.
            //
            public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
            {
                IDT.DebugAssert(null != inputBuffer && 0 != inputBuffer.Length, "null input buffer");
                IDT.DebugAssert(0 != inputCount, "0 input count");
                GlobalAllocSafeHandle pOutData = null;
                int cbOutData = 0;

                byte[] outData;

                using (HGlobalSafeHandle pInData = HGlobalSafeHandle.Construct(inputCount))
                {
                    Marshal.Copy(inputBuffer, inputOffset, pInData.DangerousGetHandle(), inputCount);

                    int status = CardSpaceSelector.GetShim().m_csShimTransformFinalBlock(m_transCryptoHandle.InternalHandle,
                                                                                         inputCount,
                                                                                         pInData,
                                                                                         out cbOutData,
                                                                                         out pOutData);

                    if (0 != status)
                    {
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw IDT.ThrowHelperError(new Win32Exception(status));
                    }
                    pOutData.Length = cbOutData;
                    outData         = DiagnosticUtility.Utility.AllocateByteArray(pOutData.Length);
                    using (pOutData)
                    {
                        Marshal.Copy(pOutData.DangerousGetHandle(), outData, 0, pOutData.Length);
                    }
                }

                return(outData);
            }
        public byte[] SignHash(byte[] hash, string hashAlgOid)
        {
            InfoCardTrace.ThrowInvalidArgumentConditional((hash == null) || (0 == hash.Length), "hash");
            InfoCardTrace.ThrowInvalidArgumentConditional(string.IsNullOrEmpty(hashAlgOid), "hashAlgOid");
            int pcbSig = 0;
            GlobalAllocSafeHandle pSig = null;

            using (HGlobalSafeHandle handle2 = HGlobalSafeHandle.Construct(hash.Length))
            {
                using (HGlobalSafeHandle handle3 = HGlobalSafeHandle.Construct(hashAlgOid))
                {
                    Marshal.Copy(hash, 0, handle2.DangerousGetHandle(), hash.Length);
                    RuntimeHelpers.PrepareConstrainedRegions();
                    int status = CardSpaceSelector.GetShim().m_csShimSignHash(this.m_cryptoHandle.InternalHandle, hash.Length, handle2, handle3, out pcbSig, out pSig);
                    if (status != 0)
                    {
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw InfoCardTrace.ThrowHelperError(new Win32Exception(status));
                    }
                    pSig.Length = pcbSig;
                    byte[] destination = DiagnosticUtility.Utility.AllocateByteArray(pSig.Length);
                    using (pSig)
                    {
                        Marshal.Copy(pSig.DangerousGetHandle(), destination, 0, pSig.Length);
                    }
                    return(destination);
                }
            }
        }
            public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
            {
                GlobalAllocSafeHandle pOutData = null;

                byte[] buffer;
                int    cbOutData = 0;

                using (HGlobalSafeHandle handle2 = HGlobalSafeHandle.Construct(inputCount))
                {
                    Marshal.Copy(inputBuffer, inputOffset, handle2.DangerousGetHandle(), inputCount);
                    int status = CardSpaceSelector.GetShim().m_csShimTransformFinalBlock(this.m_transCryptoHandle.InternalHandle, inputCount, handle2, out cbOutData, out pOutData);
                    if (status != 0)
                    {
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw InfoCardTrace.ThrowHelperError(new Win32Exception(status));
                    }
                    pOutData.Length = cbOutData;
                    buffer          = DiagnosticUtility.Utility.AllocateByteArray(pOutData.Length);
                    using (pOutData)
                    {
                        Marshal.Copy(pOutData.DangerousGetHandle(), buffer, 0, pOutData.Length);
                    }
                }
                return(buffer);
            }
Ejemplo n.º 10
0
        //
        // Summary:
        //  Creates a new InfoCardKeyedHashAlgorithm based on a SymmetricCryptoHandle.
        //
        // Parameters:
        //  cryptoHandle  - The handle to the symmetric key on which to base the keyed hash.
        //
        public InfoCardKeyedHashAlgorithm(SymmetricCryptoHandle cryptoHandle)
        {
            InternalRefCountedHandle nativeHandle = null;

            try
            {
                //
                // Call native api to get a hashCryptoHandle.
                //
                int status = CardSpaceSelector.GetShim().m_csShimGetKeyedHash(cryptoHandle.InternalHandle, out nativeHandle);

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

                m_cryptoHandle = (HashCryptoHandle)CryptoHandle.Create(nativeHandle);

                m_param = (RpcHashCryptoParameters)m_cryptoHandle.Parameters;
            }
            catch
            {
                if (null != m_cryptoHandle)
                {
                    m_cryptoHandle.Dispose();
                }
                throw;
            }
        }
        public bool VerifyHash(byte[] hash, string hashAlgOid, byte[] sig)
        {
            IDT.ThrowInvalidArgumentConditional(null == hash || 0 == hash.Length, "hash");
            IDT.ThrowInvalidArgumentConditional(String.IsNullOrEmpty(hashAlgOid), "hashAlgOid");
            IDT.ThrowInvalidArgumentConditional(null == sig || 0 == sig.Length, "sig");
            bool verified = false;

            using (HGlobalSafeHandle pHash = HGlobalSafeHandle.Construct(hash.Length))
            {
                using (HGlobalSafeHandle pHashAlgOid = HGlobalSafeHandle.Construct(hashAlgOid))
                {
                    Marshal.Copy(hash, 0, pHash.DangerousGetHandle(), hash.Length);
                    int status = 0;
                    using (HGlobalSafeHandle pSig = HGlobalSafeHandle.Construct(sig.Length))
                    {
                        Marshal.Copy(sig, 0, pSig.DangerousGetHandle(), sig.Length);

                        status = CardSpaceSelector.GetShim().m_csShimVerifyHash(m_cryptoHandle.InternalHandle,
                                                                                hash.Length,
                                                                                pHash,
                                                                                pHashAlgOid,
                                                                                sig.Length,
                                                                                pSig,
                                                                                out verified);
                    }
                    if (0 != status)
                    {
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw IDT.ThrowHelperError(new Win32Exception(status));
                    }
                }
            }

            return(verified);
        }
 public override byte[] GenerateDerivedKey(string algorithmUri, byte[] label, byte[] nonce, int derivedKeyLength, int offset)
 {
     if (!this.IsSupportedAlgorithm(algorithmUri))
     {
         throw InfoCardTrace.ThrowHelperError(new NotSupportedException(Microsoft.InfoCards.SR.GetString("ClientUnsupportedCryptoAlgorithm", new object[] { algorithmUri })));
     }
     byte[] destination = null;
     using (HGlobalSafeHandle handle = HGlobalSafeHandle.Construct(label.Length))
     {
         using (HGlobalSafeHandle handle2 = HGlobalSafeHandle.Construct(nonce.Length))
         {
             GlobalAllocSafeHandle pDerivedKey = null;
             int cbDerivedKey = 0;
             Marshal.Copy(label, 0, handle.DangerousGetHandle(), label.Length);
             Marshal.Copy(nonce, 0, handle2.DangerousGetHandle(), nonce.Length);
             int error = CardSpaceSelector.GetShim().m_csShimGenerateDerivedKey(this.m_cryptoHandle.InternalHandle, label.Length, handle, nonce.Length, handle2, derivedKeyLength, offset, algorithmUri, out cbDerivedKey, out pDerivedKey);
             if (error != 0)
             {
                 throw InfoCardTrace.ThrowHelperError(new Win32Exception(error));
             }
             pDerivedKey.Length = cbDerivedKey;
             destination        = new byte[pDerivedKey.Length];
             using (pDerivedKey)
             {
                 Marshal.Copy(pDerivedKey.DangerousGetHandle(), destination, 0, pDerivedKey.Length);
             }
             return(destination);
         }
     }
 }
Ejemplo n.º 13
0
        //
        // Summary:
        //  Implements the HashFinal method of the KeyedHashAlgorithm class by calling the InfoCard native client.
        //
        protected override byte[] HashFinal()
        {
            byte[] outData   = null;
            int    cbOutData = 0;

            IDT.DebugAssert(null != m_cachedBlock, "null cached block");

            HGlobalSafeHandle     pInData  = null;
            GlobalAllocSafeHandle pOutData = null;

            try
            {
                if (null != m_cachedBlock)
                {
                    if (0 != m_cachedBlock.Length)
                    {
                        pInData = HGlobalSafeHandle.Construct(m_cachedBlock.Length);
                        Marshal.Copy(m_cachedBlock, 0, pInData.DangerousGetHandle(), m_cachedBlock.Length);
                    }

                    int status = CardSpaceSelector.GetShim().m_csShimHashFinal(m_cryptoHandle.InternalHandle,
                                                                               m_cachedBlock.Length,
                                                                               null != pInData ? pInData : HGlobalSafeHandle.Construct(),
                                                                               out cbOutData,
                                                                               out pOutData);
                    if (0 != status)
                    {
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw IDT.ThrowHelperError(new Win32Exception(status));
                    }
                    pOutData.Length = cbOutData;
                    outData         = DiagnosticUtility.Utility.AllocateByteArray(pOutData.Length);
                    using (pOutData)
                    {
                        Marshal.Copy(pOutData.DangerousGetHandle(), outData, 0, pOutData.Length);
                    }
                }
            }
            finally
            {
                if (null != pInData)
                {
                    pInData.Dispose();
                }
                Array.Clear(m_cachedBlock, 0, m_cachedBlock.Length);
                m_cachedBlock = null;
            }

            return(outData);
        }
Ejemplo n.º 14
0
        // ISymmetricCrypto

        public override byte[] GenerateDerivedKey(string algorithmUri,
                                                  byte[] label,
                                                  byte[] nonce,
                                                  int derivedKeyLength,
                                                  int offset)
        {
            IDT.DebugAssert(!String.IsNullOrEmpty(algorithmUri), "null alg uri");
            IDT.DebugAssert(null != label && 0 != label.Length, "null label");
            IDT.DebugAssert(null != nonce && 0 != nonce.Length, "null nonce");

            if (!IsSupportedAlgorithm(algorithmUri))
            {
                throw IDT.ThrowHelperError(new NotSupportedException(SR.GetString(SR.ClientUnsupportedCryptoAlgorithm, algorithmUri)));
            }
            byte[] derivedKey = null;
            using (HGlobalSafeHandle pLabel = HGlobalSafeHandle.Construct(label.Length))
            {
                using (HGlobalSafeHandle pNonce = HGlobalSafeHandle.Construct(nonce.Length))
                {
                    GlobalAllocSafeHandle pDerivedKey = null;
                    int cbDerivedKey = 0;

                    Marshal.Copy(label, 0, pLabel.DangerousGetHandle(), label.Length);
                    Marshal.Copy(nonce, 0, pNonce.DangerousGetHandle(), nonce.Length);

                    int status = CardSpaceSelector.GetShim().m_csShimGenerateDerivedKey(m_cryptoHandle.InternalHandle,
                                                                                        label.Length,
                                                                                        pLabel,
                                                                                        nonce.Length,
                                                                                        pNonce,
                                                                                        derivedKeyLength,
                                                                                        offset,
                                                                                        algorithmUri,
                                                                                        out cbDerivedKey,
                                                                                        out pDerivedKey);

                    if (0 != status)
                    {
                        throw IDT.ThrowHelperError(new Win32Exception(status));
                    }
                    pDerivedKey.Length = cbDerivedKey;
                    derivedKey         = new byte[pDerivedKey.Length];
                    using (pDerivedKey)
                    {
                        Marshal.Copy(pDerivedKey.DangerousGetHandle(), derivedKey, 0, pDerivedKey.Length);
                    }
                }
            }
            return(derivedKey);
        }
Ejemplo n.º 15
0
        //
        // Summary
        //  Start the management user interface
        //
        public static void Manage()
        {
            Int32 result = CardSpaceSelector.GetShim().m_csShimManageCardSpace();

            //
            // Convert HRESULTS to errors
            //
            if (0 != result)
            {
                //
                // Convert the HRESULTS to exceptions
                //
                ExceptionHelper.ThrowIfCardSpaceException((int)result);
                throw IDT.ThrowHelperError(new CardSpaceException(SR.GetString(SR.ClientAPIInfocardError)));
            }
        }
Ejemplo n.º 16
0
        //
        // Summary:
        //  Implements the HashCore method of the KeyedHashAlgorithm class by calling the InfoCard native client.
        //
        // Parameters:
        //  array   - the bytes to hash.
        //  ibStart - the index in the array from which to begin hashing.
        //  cbSize  - the number of bytes after the starting index to hash.
        //
        protected override void HashCore(byte[] array, int ibStart, int cbSize)
        {
            //
            // will cache one block and call TransformBlock on the previous block.
            //
            if (null != m_cachedBlock)
            {
                HGlobalSafeHandle pInData = null;
                try
                {
                    if (0 != m_cachedBlock.Length)
                    {
                        pInData = HGlobalSafeHandle.Construct(m_cachedBlock.Length);
                        Marshal.Copy(m_cachedBlock, 0, pInData.DangerousGetHandle(), m_cachedBlock.Length);
                    }

                    int status = CardSpaceSelector.GetShim().m_csShimHashCore(m_cryptoHandle.InternalHandle,
                                                                              m_cachedBlock.Length,
                                                                              null != pInData ? pInData : HGlobalSafeHandle.Construct());

                    if (0 != status)
                    {
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw IDT.ThrowHelperError(new Win32Exception(status));
                    }
                }
                finally
                {
                    if (null != pInData)
                    {
                        pInData.Dispose();
                    }
                }
            }

            //
            // Cache the current block.
            //
            if (null != m_cachedBlock)
            {
                Array.Clear(m_cachedBlock, 0, m_cachedBlock.Length);
            }
            m_cachedBlock = DiagnosticUtility.Utility.AllocateByteArray(cbSize);
            Array.Copy(array, ibStart, m_cachedBlock, 0, cbSize);

            return;
        }
            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;
                }
            }
        protected override byte[] HashFinal()
        {
            byte[]                destination = null;
            int                   cbOutData   = 0;
            HGlobalSafeHandle     handle      = null;
            GlobalAllocSafeHandle pOutData    = null;

            try
            {
                if (this.m_cachedBlock == null)
                {
                    return(destination);
                }
                if (this.m_cachedBlock.Length != 0)
                {
                    handle = HGlobalSafeHandle.Construct(this.m_cachedBlock.Length);
                    Marshal.Copy(this.m_cachedBlock, 0, handle.DangerousGetHandle(), this.m_cachedBlock.Length);
                }
                int status = CardSpaceSelector.GetShim().m_csShimHashFinal(this.m_cryptoHandle.InternalHandle, this.m_cachedBlock.Length, (handle != null) ? handle : HGlobalSafeHandle.Construct(), out cbOutData, out pOutData);
                if (status != 0)
                {
                    ExceptionHelper.ThrowIfCardSpaceException(status);
                    throw InfoCardTrace.ThrowHelperError(new Win32Exception(status));
                }
                pOutData.Length = cbOutData;
                destination     = DiagnosticUtility.Utility.AllocateByteArray(pOutData.Length);
                using (pOutData)
                {
                    Marshal.Copy(pOutData.DangerousGetHandle(), destination, 0, pOutData.Length);
                }
            }
            finally
            {
                if (handle != null)
                {
                    handle.Dispose();
                }
                Array.Clear(this.m_cachedBlock, 0, this.m_cachedBlock.Length);
                this.m_cachedBlock = null;
            }
            return(destination);
        }
        public byte[] SignHash(byte[] hash, string hashAlgOid)
        {
            IDT.ThrowInvalidArgumentConditional(null == hash || 0 == hash.Length, "hash");
            IDT.ThrowInvalidArgumentConditional(String.IsNullOrEmpty(hashAlgOid), "hashAlgOid");
            int cbSig = 0;
            GlobalAllocSafeHandle pSig = null;

            byte[] sig;
            using (HGlobalSafeHandle pHash = HGlobalSafeHandle.Construct(hash.Length))
            {
                using (HGlobalSafeHandle pHashAlgOid = HGlobalSafeHandle.Construct(hashAlgOid))
                {
                    Marshal.Copy(hash, 0, pHash.DangerousGetHandle(), hash.Length);

                    RuntimeHelpers.PrepareConstrainedRegions();
                    int status = CardSpaceSelector.GetShim().m_csShimSignHash(m_cryptoHandle.InternalHandle,
                                                                              hash.Length,
                                                                              pHash,
                                                                              pHashAlgOid,
                                                                              out cbSig,
                                                                              out pSig);

                    if (0 != status)
                    {
                        ExceptionHelper.ThrowIfCardSpaceException(status);
                        throw IDT.ThrowHelperError(new Win32Exception(status));
                    }
                    pSig.Length = cbSig;
                    sig         = DiagnosticUtility.Utility.AllocateByteArray(pSig.Length);
                    using (pSig)
                    {
                        Marshal.Copy(pSig.DangerousGetHandle(), sig, 0, pSig.Length);
                    }
                }
            }

            return(sig);
        }
Ejemplo n.º 20
0
        //
        // Summary
        //  Start the import card user interface
        //
        public static void Import(string fileName)
        {
            if (String.IsNullOrEmpty(fileName))
            {
                throw IDT.ThrowHelperArgumentNull("fileName");
            }


            IDT.TraceDebug("Import Infocard has been called");
            Int32 result = CardSpaceSelector.GetShim().m_csShimImportInformationCard(fileName);

            //
            // Convert HRESULTS to errors
            //
            if (0 != result)
            {
                //
                // Convert the HRESULTS to exceptions
                //
                ExceptionHelper.ThrowIfCardSpaceException((int)result);
                throw IDT.ThrowHelperError(new CardSpaceException(SR.GetString(SR.ClientAPIInfocardError)));
            }
        }
            //
            // 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;
                }
            }
Ejemplo n.º 22
0
        public void DoMarshal(IntPtr ptr)
        {
            string outerXml = this.m_element.Target.OuterXml;
            string str2     = "";

            this.m_nativePtr = ptr;
            if (this.m_element.Issuer != null)
            {
                str2 = this.m_element.Issuer.OuterXml;
            }
            string str3 = string.Empty;

            if (this.m_element.Parameters != null)
            {
                str3 = CardSpaceSelector.XmlToString(this.m_element.Parameters);
            }
            this.m_nativeElement.targetEndpointAddress = outerXml;
            this.m_nativeElement.issuerEndpointAddress = str2;
            this.m_nativeElement.issuedTokenParameters = str3;
            this.m_nativeElement.policyNoticeLink      = (null != this.m_element.PolicyNoticeLink) ? this.m_element.PolicyNoticeLink.ToString() : null;
            this.m_nativeElement.policyNoticeVersion   = this.m_element.PolicyNoticeVersion;
            this.m_nativeElement.isManagedCardProvider = this.m_element.IsManagedIssuer;
            Marshal.StructureToPtr(this.m_nativeElement, ptr, false);
        }