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