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 SafeHandle DoMarshal() { if (null == m_nativeChain) { int elementSize = InternalPolicyElement.Size; int chainLength = m_chain.Length; m_nativeChain = HGlobalSafeHandle.Construct(chainLength * elementSize); IntPtr pos = m_nativeChain.DangerousGetHandle(); foreach (InternalPolicyElement element in m_chain) { element.DoMarshal(pos); unsafe { // // All this just to do pos += elementSize // pos = new IntPtr((long)(((ulong)pos.ToPointer()) + (ulong)elementSize)); } } } return(m_nativeChain); }
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); }
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); } } }
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 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); }
// // 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; 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); }
// 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: // 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); }
public SafeHandle DoMarshal() { if (this.m_nativeChain == null) { int size = InternalPolicyElement.Size; int length = this.m_chain.Length; this.m_nativeChain = HGlobalSafeHandle.Construct((int)(length * size)); IntPtr handle = this.m_nativeChain.DangerousGetHandle(); foreach (InternalPolicyElement element in this.m_chain) { element.DoMarshal(handle); handle = new IntPtr(((long)((ulong)handle.ToPointer())) + size); } } return(this.m_nativeChain); }
// // 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); }
// // 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; } }