Beispiel #1
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);
        }
Beispiel #2
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);
            }
        }
Beispiel #3
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));
                }
            }
        }
Beispiel #4
0
        public static void DecodeObject(this byte[] encoded, string 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);
            }
        }
Beispiel #5
0
        public static bool DecodeObjectNoThrow(
            this byte[] encoded,
            CryptDecodeObjectStructType lpszStructType,
            DecodedObjectReceiver receiver)
        {
            unsafe
            {
                int cb = 0;

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

                byte *decoded = stackalloc byte[cb];

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

                receiver(decoded, cb);
            }

            return(true);
        }
Beispiel #6
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.GetLastWin32Error().ToCryptographicException();
                }

                byte *decoded = stackalloc byte[cb];

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

                return(receiver(decoded, cb));
            }
        }
Beispiel #7
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;
        }