internal static unsafe SafeHandle CryptDecodeObjectToMemory(CryptDecodeObjectStructType lpszStructType, byte[] pbEncoded)
 {
     fixed(byte *pbEncodedPointer = pbEncoded)
     {
         return(CryptDecodeObjectToMemory(lpszStructType, (IntPtr)pbEncodedPointer, pbEncoded.Length));
     }
 }
 internal static SafeHandle CryptDecodeObjectToMemory(CryptDecodeObjectStructType lpszStructType, byte[] pbEncoded)
 {
     unsafe
     {
         fixed (byte* pbEncodedPointer = pbEncoded)
         {
             return CryptDecodeObjectToMemory(lpszStructType, (IntPtr)pbEncodedPointer, pbEncoded.Length);
         }
     }
 }
Example #3
0
        private static byte[] DecodeKeyBlob(CryptDecodeObjectStructType lpszStructType, byte[] encodedKeyValue)
        {
            int cbDecoded = 0;
            if (!Interop.crypt32.CryptDecodeObject(CertEncodingType.All, lpszStructType, encodedKeyValue, encodedKeyValue.Length, CryptDecodeObjectFlags.None, null, ref cbDecoded))
                throw new CryptographicException(Marshal.GetLastWin32Error());

            byte[] keyBlob = new byte[cbDecoded];
            if (!Interop.crypt32.CryptDecodeObject(CertEncodingType.All, lpszStructType, encodedKeyValue, encodedKeyValue.Length, CryptDecodeObjectFlags.None, keyBlob, ref cbDecoded))
                throw new CryptographicException(Marshal.GetLastWin32Error());

            return keyBlob;
        }
        internal static unsafe byte[] CryptEncodeObjectToByteArray(CryptDecodeObjectStructType lpszStructType, void* decoded)
        {
            int cb = 0;
            if (!CryptEncodeObject(MsgEncodingType.All, lpszStructType, decoded, null, ref cb))
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            byte[] encoded = new byte[cb];
            if (!CryptEncodeObject(MsgEncodingType.All, lpszStructType, decoded, encoded, ref cb))
                throw Marshal.GetLastWin32Error().ToCryptographicException();

            return encoded.Resize(cb);
        }
        internal static SafeHandle CryptDecodeObjectToMemory(CryptDecodeObjectStructType lpszStructType, IntPtr pbEncoded, int cbEncoded)
        {
            int cbRequired = 0;
            unsafe
            {
                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;
            }
        }
Example #6
0
        public static unsafe bool DecodeObjectNoThrow <TState, TResult>(
            this ReadOnlySpan <byte> encoded,
            CryptDecodeObjectStructType lpszStructType,
            TState state,
            DecodedObjectReceiver <TState, TResult> receiver,
            out TResult?result)
        {
            int cb = 0;

            if (!Interop.crypt32.CryptDecodeObjectPointer(
                    Interop.Crypt32.CertEncodingType.All,
                    lpszStructType,
                    encoded,
                    Interop.Crypt32.CryptDecodeObjectFlags.None,
                    null,
                    ref cb))
            {
                result = default;
                return(false);
            }

            const int   MaxStackAllocSize = 256;
            Span <byte> decoded           = stackalloc byte[MaxStackAllocSize];

            if ((uint)cb > MaxStackAllocSize)
            {
                decoded = new byte[cb];
            }

            fixed(byte *pDecoded = decoded)
            {
                if (!Interop.crypt32.CryptDecodeObjectPointer(
                        Interop.Crypt32.CertEncodingType.All,
                        lpszStructType,
                        encoded,
                        Interop.Crypt32.CryptDecodeObjectFlags.None,
                        pDecoded,
                        ref cb))
                {
                    result = default;
                    return(false);
                }

                result = receiver(pDecoded, cb, state);
            }

            return(true);
        }
Example #7
0
        public static void DecodeObject(this byte[] encoded, CryptDecodeObjectStructType lpszStructType, DecodedObjectReceiver receiver)
        {
            unsafe
            {
                int cb = 0;

                if (!Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, null, ref cb))
                    throw Marshal.GetLastWin32Error().ToCryptographicException();

                byte* decoded = stackalloc byte[cb];
                if (!Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, (byte*)decoded, ref cb))
                    throw Marshal.GetLastWin32Error().ToCryptographicException();

                receiver(decoded);
            }
        }
Example #8
0
        public static TResult DecodeObject <TResult>(
            this byte[] encoded,
            CryptDecodeObjectStructType lpszStructType,
            DecodedObjectReceiver <TResult> receiver)
        {
            unsafe
            {
                int cb = 0;

                if (!Interop.crypt32.CryptDecodeObjectPointer(
                        Interop.Crypt32.CertEncodingType.All,
                        lpszStructType,
                        encoded,
                        encoded.Length,
                        Interop.Crypt32.CryptDecodeObjectFlags.None,
                        null,
                        ref cb))
                {
                    throw Marshal.GetLastPInvokeError().ToCryptographicException();
                }

                int         MaxStackAllocSize = 256;
                Span <byte> decoded           = stackalloc byte[MaxStackAllocSize];

                if ((uint)cb > MaxStackAllocSize)
                {
                    decoded = new byte[cb];
                }

                fixed(byte *pDecoded = decoded)
                {
                    if (!Interop.crypt32.CryptDecodeObjectPointer(
                            Interop.Crypt32.CertEncodingType.All,
                            lpszStructType,
                            encoded,
                            encoded.Length,
                            Interop.Crypt32.CryptDecodeObjectFlags.None,
                            pDecoded,
                            ref cb))
                    {
                        throw Marshal.GetLastPInvokeError().ToCryptographicException();
                    }

                    return(receiver(pDecoded, cb));
                }
            }
        }
Example #9
0
        private static byte[] DecodeKeyBlob(CryptDecodeObjectStructType lpszStructType, byte[] encodedKeyValue)
        {
            int cbDecoded = 0;

            if (!Interop.crypt32.CryptDecodeObject(CertEncodingType.All, lpszStructType, encodedKeyValue, encodedKeyValue.Length, CryptDecodeObjectFlags.None, null, ref cbDecoded))
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            byte[] keyBlob = new byte[cbDecoded];
            if (!Interop.crypt32.CryptDecodeObject(CertEncodingType.All, lpszStructType, encodedKeyValue, encodedKeyValue.Length, CryptDecodeObjectFlags.None, keyBlob, ref cbDecoded))
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            return(keyBlob);
        }
        internal static unsafe byte[] CryptEncodeObjectToByteArray(CryptDecodeObjectStructType lpszStructType, void *decoded)
        {
            int cb = 0;

            if (!CryptEncodeObject(MsgEncodingType.All, lpszStructType, decoded, null, ref cb))
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            byte[] encoded = new byte[cb];
            if (!CryptEncodeObject(MsgEncodingType.All, lpszStructType, decoded, encoded, ref cb))
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            return(encoded.Resize(cb));
        }
Example #11
0
        public static unsafe byte[] EncodeObject(CryptDecodeObjectStructType lpszStructType, void *decoded)
        {
            int cb = 0;

            if (!Interop.crypt32.CryptEncodeObject(CertEncodingType.All, lpszStructType, decoded, null, ref cb))
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            byte[] encoded = new byte[cb];
            if (!Interop.crypt32.CryptEncodeObject(CertEncodingType.All, lpszStructType, decoded, encoded, ref cb))
            {
                throw Marshal.GetLastWin32Error().ToCryptographicException();
            }

            return(encoded);
        }
        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);
        }
Example #13
0
        public static void DecodeObject(this byte[] encoded, CryptDecodeObjectStructType lpszStructType, DecodedObjectReceiver receiver)
        {
            unsafe
            {
                int cb = 0;

                if (!Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, null, ref cb))
                {
                    throw Marshal.GetLastWin32Error().ToCryptographicException();
                }

                byte *decoded = stackalloc byte[cb];
                if (!Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, (byte *)decoded, ref cb))
                {
                    throw Marshal.GetLastWin32Error().ToCryptographicException();
                }

                receiver(decoded);
            }
        }
Example #14
0
        public static bool DecodeObjectNoThrow(this byte[] encoded, CryptDecodeObjectStructType lpszStructType, DecodedObjectReceiver receiver)
        {
            unsafe
            {
                int cb = 0;

                if (!Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, null, ref cb))
                {
                    return(false);
                }

                byte *decoded = stackalloc byte[cb];
                if (!Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, (byte *)decoded, ref cb))
                {
                    return(false);
                }

                receiver(decoded);
            }
            return(true);
        }
 internal static unsafe bool CryptDecodeObject(CryptDecodeObjectStructType lpszStructType, IntPtr pbEncoded, int cbEncoded, void* pvStructInfo, ref int pcbStructInfo)
 {
     return CryptDecodeObject(MsgEncodingType.All, (IntPtr)lpszStructType, pbEncoded, cbEncoded, 0, pvStructInfo, ref pcbStructInfo);
 }
Example #16
0
 public static unsafe bool CryptEncodeObject(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, void *pvStructInfo, byte[]?pbEncoded, ref int pcbEncoded)
 {
     return(CryptEncodeObject(dwCertEncodingType, (IntPtr)lpszStructType, pvStructInfo, pbEncoded, ref pcbEncoded));
 }
Example #17
0
 public static unsafe bool CryptDecodeObjectPointer(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, void *pvStructInfo, ref int pcbStructInfo)
 {
     return(CryptDecodeObjectPointer(dwCertEncodingType, (IntPtr)lpszStructType, pbEncoded, cbEncoded, dwFlags, pvStructInfo, ref pcbStructInfo));
 }
Example #18
0
 public static bool CryptDecodeObject(Interop.Crypt32.CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, byte[] pbEncoded, int cbEncoded, Interop.Crypt32.CryptDecodeObjectFlags dwFlags, byte[]?pvStructInfo, ref int pcbStructInfo)
 {
     return(Interop.Crypt32.CryptDecodeObject(dwCertEncodingType, (IntPtr)lpszStructType, pbEncoded, cbEncoded, dwFlags, pvStructInfo, ref pcbStructInfo));
 }
Example #19
0
            public static unsafe byte[] EncodeObject(CryptDecodeObjectStructType lpszStructType, void* decoded)
            {
                int cb = 0;
                if (!Interop.crypt32.CryptEncodeObject(CertEncodingType.All, lpszStructType, decoded, null, ref cb))
                    throw new CryptographicException(Marshal.GetLastWin32Error());

                byte[] encoded = new byte[cb];
                if (!Interop.crypt32.CryptEncodeObject(CertEncodingType.All, lpszStructType, decoded, encoded, ref cb))
                    throw new CryptographicException(Marshal.GetLastWin32Error());

                return encoded;
            }
Example #20
0
 public static unsafe bool CryptEncodeObject(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, void* pvStructInfo, byte[] pbEncoded, ref int pcbEncoded)
 {
     return CryptEncodeObject(dwCertEncodingType, (IntPtr)lpszStructType, pvStructInfo, pbEncoded, ref pcbEncoded);
 }
Example #21
0
 public static unsafe bool CryptDecodeObjectPointer(CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, byte[] pbEncoded, int cbEncoded, CryptDecodeObjectFlags dwFlags, void* pvStructInfo, ref int pcbStructInfo)
 {
     return CryptDecodeObjectPointer(dwCertEncodingType, (IntPtr)lpszStructType, pbEncoded, cbEncoded, dwFlags, pvStructInfo, ref pcbStructInfo);
 }
 public static unsafe bool CryptDecodeObjectPointer(Interop.Crypt32.CertEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, ReadOnlySpan <byte> encoded, Interop.Crypt32.CryptDecodeObjectFlags dwFlags, void *pvStructInfo, ref int pcbStructInfo)
 {
     fixed(byte *pEncoded = encoded)
     {
         return(Interop.Crypt32.CryptDecodeObjectPointer(dwCertEncodingType, (IntPtr)lpszStructType, pEncoded, encoded.Length, dwFlags, pvStructInfo, ref pcbStructInfo));
     }
 }
Example #23
0
 internal static unsafe bool CryptDecodeObject(CryptDecodeObjectStructType lpszStructType, IntPtr pbEncoded, int cbEncoded, void *pvStructInfo, ref int pcbStructInfo)
 {
     return(CryptDecodeObject(MsgEncodingType.All, (IntPtr)lpszStructType, pbEncoded, cbEncoded, 0, pvStructInfo, ref pcbStructInfo));
 }
Example #24
0
        public static bool DecodeObjectNoThrow(this byte[] encoded, CryptDecodeObjectStructType lpszStructType, DecodedObjectReceiver receiver)
        {
            unsafe
            {
                int cb = 0;

                if (!Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, null, ref cb))
                    return false;

                byte* decoded = stackalloc byte[cb];
                if (!Interop.crypt32.CryptDecodeObjectPointer(CertEncodingType.All, lpszStructType, encoded, encoded.Length, CryptDecodeObjectFlags.None, (byte*)decoded, ref cb))
                    return false;

                receiver(decoded);
            }
            return true;
        }
Example #25
0
 internal static unsafe bool CryptEncodeObject(MsgEncodingType dwCertEncodingType, CryptDecodeObjectStructType lpszStructType, void *pvStructInfo, byte[]?pbEncoded, ref int pcbEncoded)
 {
     return(CryptEncodeObject(dwCertEncodingType, (nint)lpszStructType, pvStructInfo, pbEncoded, ref pcbEncoded));
 }