Example #1
0
        private static SafeCryptMsgHandle OpenToDecode(byte[] encodedMessage, ContentInfo contentInfo, bool detached)
        {
            SafeCryptMsgHandle safeCryptMsgHandle = CAPI.CAPISafe.CryptMsgOpenToDecode(65537U, detached ? 4U : 0U, 0U, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            if (safeCryptMsgHandle == null || safeCryptMsgHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            if (!CAPI.CAPISafe.CryptMsgUpdate(safeCryptMsgHandle, encodedMessage, (uint)encodedMessage.Length, true))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            if (2 != (int)PkcsUtils.GetMessageType(safeCryptMsgHandle))
            {
                throw new CryptographicException(-2146889724);
            }
            if (detached)
            {
                byte[] content = contentInfo.Content;
                if (content != null && content.Length > 0 && !CAPI.CAPISafe.CryptMsgUpdate(safeCryptMsgHandle, content, (uint)content.Length, true))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
            return(safeCryptMsgHandle);
        }
Example #2
0
        public static Oid GetContentType(byte[] encodedMessage)
        {
            if (encodedMessage == null)
            {
                throw new ArgumentNullException("encodedMessage");
            }
            SafeCryptMsgHandle safeCryptMsgHandle = CAPI.CAPISafe.CryptMsgOpenToDecode(65537U, 0U, 0U, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);

            if (safeCryptMsgHandle == null || safeCryptMsgHandle.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            if (!CAPI.CAPISafe.CryptMsgUpdate(safeCryptMsgHandle, encodedMessage, (uint)encodedMessage.Length, true))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            Oid oid;

            switch (PkcsUtils.GetMessageType(safeCryptMsgHandle))
            {
            case 1U:
                oid = new Oid("1.2.840.113549.1.7.1");
                break;

            case 2U:
                oid = new Oid("1.2.840.113549.1.7.2");
                break;

            case 3U:
                oid = new Oid("1.2.840.113549.1.7.3");
                break;

            case 4U:
                oid = new Oid("1.2.840.113549.1.7.4");
                break;

            case 5U:
                oid = new Oid("1.2.840.113549.1.7.5");
                break;

            case 6U:
                oid = new Oid("1.2.840.113549.1.7.6");
                break;

            default:
                throw new CryptographicException(-2146889724);
            }
            safeCryptMsgHandle.Dispose();
            return(oid);
        }