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);
        }
            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);
            }
        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 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);
         }
     }
 }
            //
            // 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[] 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);
        }
Ejemplo n.º 7
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.º 8
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);
        }
        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);
        }