Esempio n. 1
0
        public IntPtr Alloc(int cbSize)
        {
            if (cbSize < 0)
            {
                throw new OverflowException();
            }
            SafeHeapAllocHandle hBlock = SafeHeapAllocHandle.Alloc(cbSize);

            _blocks.Add(hBlock);
            return(hBlock.DangerousGetHandle());
        }
Esempio n. 2
0
        // Used for binary blobs with internal pointers.
        public static SafeHandle GetMsgParamAsMemory(this SafeCryptMsgHandle hCryptMsg, CryptMsgParamType paramType, int index = 0)
        {
            int cbData = 0;

            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, null, ref cbData))
            {
                throw Interop.CPError.GetLastWin32Error().ToCryptographicException();
            }

            SafeHandle pvData = SafeHeapAllocHandle.Alloc(cbData);

            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, pvData.DangerousGetHandle(), ref cbData))
            {
                throw Interop.CPError.GetLastWin32Error().ToCryptographicException();
            }

            return(pvData);
        }
        internal static unsafe SafeHandle CryptDecodeObjectToMemory(CryptDecodeObjectStructType lpszStructType, IntPtr pbEncoded, int cbEncoded)
        {
            int cbRequired = 0;

            if (!CryptDecodeObject(MsgEncodingType.All, (IntPtr)lpszStructType, pbEncoded, cbEncoded, 0, null, ref cbRequired))
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            SafeHandle sh = SafeHeapAllocHandle.Alloc(cbRequired);

            if (!CryptDecodeObject(MsgEncodingType.All, (IntPtr)lpszStructType, pbEncoded, cbEncoded, 0, (void *)sh.DangerousGetHandle(), ref cbRequired))
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            return(sh);
        }
Esempio n. 4
0
        // Used for binary blobs with internal pointers.
        public static SafeHandle GetMsgParamAsMemory(this SafeCryptMsgHandle hCryptMsg, CryptMsgParamType paramType, int index = 0)
        {
            int cbData = 0;

            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, IntPtr.Zero, ref cbData))
            {
                throw Marshal.GetLastPInvokeError().ToCryptographicException();
            }

            SafeHandle pvData = SafeHeapAllocHandle.Alloc(cbData);

            if (!Interop.Crypt32.CryptMsgGetParam(hCryptMsg, paramType, index, pvData.DangerousGetHandle(), ref cbData))
            {
                Exception e = Marshal.GetLastPInvokeError().ToCryptographicException();
                pvData.Dispose();
                throw e;
            }

            return(pvData);
        }