private unsafe void RemoveCounterSignature(int parentIndex, int childIndex)
 {
     if (parentIndex < 0)
     {
         throw new ArgumentOutOfRangeException("parentIndex");
     }
     if (childIndex < 0)
     {
         throw new ArgumentOutOfRangeException("childIndex");
     }
     uint cbData = 0;
     System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
     uint num2 = 0;
     System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
     uint num3 = 0;
     uint cAttr = 0;
     IntPtr zero = IntPtr.Zero;
     System.Security.Cryptography.SafeCryptMsgHandle cryptMsgHandle = this.m_signedCms.GetCryptMsgHandle();
     if (PkcsUtils.CmsSupported())
     {
         PkcsUtils.GetParam(cryptMsgHandle, 0x27, (uint) parentIndex, out invalidHandle, out cbData);
         System.Security.Cryptography.CAPI.CMSG_CMS_SIGNER_INFO cmsg_cms_signer_info = (System.Security.Cryptography.CAPI.CMSG_CMS_SIGNER_INFO) Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_CMS_SIGNER_INFO));
         cAttr = cmsg_cms_signer_info.UnauthAttrs.cAttr;
         zero = new IntPtr((long) cmsg_cms_signer_info.UnauthAttrs.rgAttr);
     }
     else
     {
         PkcsUtils.GetParam(cryptMsgHandle, 6, (uint) parentIndex, out pvData, out num2);
         System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO cmsg_signer_info = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO) Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO));
         cAttr = cmsg_signer_info.UnauthAttrs.cAttr;
         zero = new IntPtr((long) cmsg_signer_info.UnauthAttrs.rgAttr);
     }
     for (num3 = 0; num3 < cAttr; num3++)
     {
         System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE crypt_attribute = (System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE) Marshal.PtrToStructure(zero, typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE));
         if ((string.Compare(crypt_attribute.pszObjId, "1.2.840.113549.1.9.6", StringComparison.OrdinalIgnoreCase) == 0) && (crypt_attribute.cValue > 0))
         {
             if (childIndex < crypt_attribute.cValue)
             {
                 System.Security.Cryptography.CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA cmsg_ctrl_del_signer_unauth_attr_para = new System.Security.Cryptography.CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA))) {
                     dwSignerIndex = (uint) parentIndex,
                     dwUnauthAttrIndex = num3
                 };
                 if (!System.Security.Cryptography.CAPI.CryptMsgControl(cryptMsgHandle, 0, 9, new IntPtr((void*) &cmsg_ctrl_del_signer_unauth_attr_para)))
                 {
                     throw new CryptographicException(Marshal.GetLastWin32Error());
                 }
                 if (crypt_attribute.cValue > 1)
                 {
                     try
                     {
                         byte[] buffer;
                         uint num5 = (uint) ((crypt_attribute.cValue - 1) * Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)));
                         System.Security.Cryptography.SafeLocalAllocHandle handle4 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long) num5));
                         System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB* rgValue = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB*) crypt_attribute.rgValue;
                         System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB* handle = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB*) handle4.DangerousGetHandle();
                         int num6 = 0;
                         while (num6 < crypt_attribute.cValue)
                         {
                             if (num6 != childIndex)
                             {
                                 handle[0] = rgValue[0];
                             }
                             num6++;
                             rgValue++;
                             handle++;
                         }
                         System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE structure = new System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE {
                             pszObjId = crypt_attribute.pszObjId,
                             cValue = crypt_attribute.cValue - 1,
                             rgValue = handle4.DangerousGetHandle()
                         };
                         System.Security.Cryptography.SafeLocalAllocHandle handle5 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE))));
                         Marshal.StructureToPtr(structure, handle5.DangerousGetHandle(), false);
                         try
                         {
                             if (!System.Security.Cryptography.CAPI.EncodeObject(new IntPtr(0x16L), handle5.DangerousGetHandle(), out buffer))
                             {
                                 throw new CryptographicException(Marshal.GetLastWin32Error());
                             }
                         }
                         finally
                         {
                             Marshal.DestroyStructure(handle5.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE));
                             handle5.Dispose();
                         }
                         fixed (byte* numRef = buffer)
                         {
                             System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA cmsg_ctrl_add_signer_unauth_attr_para = new System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA))) {
                                 dwSignerIndex = (uint) parentIndex
                             };
                             cmsg_ctrl_add_signer_unauth_attr_para.blob.cbData = (uint) buffer.Length;
                             cmsg_ctrl_add_signer_unauth_attr_para.blob.pbData = new IntPtr((void*) numRef);
                             if (!System.Security.Cryptography.CAPI.CryptMsgControl(cryptMsgHandle, 0, 8, new IntPtr((void*) &cmsg_ctrl_add_signer_unauth_attr_para)))
                             {
                                 throw new CryptographicException(Marshal.GetLastWin32Error());
                             }
                         }
                         handle4.Dispose();
                     }
                     catch (CryptographicException)
                     {
                         byte[] buffer2;
                         if (System.Security.Cryptography.CAPI.EncodeObject(new IntPtr(0x16L), zero, out buffer2))
                         {
                             fixed (byte* numRef2 = buffer2)
                             {
                                 System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA cmsg_ctrl_add_signer_unauth_attr_para2 = new System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA))) {
                                     dwSignerIndex = (uint) parentIndex
                                 };
                                 cmsg_ctrl_add_signer_unauth_attr_para2.blob.cbData = (uint) buffer2.Length;
                                 cmsg_ctrl_add_signer_unauth_attr_para2.blob.pbData = new IntPtr((void*) numRef2);
                                 System.Security.Cryptography.CAPI.CryptMsgControl(cryptMsgHandle, 0, 8, new IntPtr((void*) &cmsg_ctrl_add_signer_unauth_attr_para2));
                             }
                         }
                         throw;
                     }
                 }
                 return;
             }
             childIndex -= (int) crypt_attribute.cValue;
         }
         zero = new IntPtr(((long) zero) + Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE)));
     }
     if ((invalidHandle != null) && !invalidHandle.IsInvalid)
     {
         invalidHandle.Dispose();
     }
     if ((pvData != null) && !pvData.IsInvalid)
     {
         pvData.Dispose();
     }
     throw new CryptographicException(-2146885618);
 }
Esempio n. 2
0
        private unsafe void RemoveCounterSignature(int parentIndex, int childIndex)
        {
            if (parentIndex < 0)
            {
                throw new ArgumentOutOfRangeException("parentIndex");
            }
            if (childIndex < 0)
            {
                throw new ArgumentOutOfRangeException("childIndex");
            }
            uint cbData = 0;

            System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
            uint num2 = 0;

            System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
            uint   num3  = 0;
            uint   cAttr = 0;
            IntPtr zero  = IntPtr.Zero;

            System.Security.Cryptography.SafeCryptMsgHandle cryptMsgHandle = this.m_signedCms.GetCryptMsgHandle();
            if (PkcsUtils.CmsSupported())
            {
                PkcsUtils.GetParam(cryptMsgHandle, 0x27, (uint)parentIndex, out invalidHandle, out cbData);
                System.Security.Cryptography.CAPI.CMSG_CMS_SIGNER_INFO cmsg_cms_signer_info = (System.Security.Cryptography.CAPI.CMSG_CMS_SIGNER_INFO)Marshal.PtrToStructure(invalidHandle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_CMS_SIGNER_INFO));
                cAttr = cmsg_cms_signer_info.UnauthAttrs.cAttr;
                zero  = new IntPtr((long)cmsg_cms_signer_info.UnauthAttrs.rgAttr);
            }
            else
            {
                PkcsUtils.GetParam(cryptMsgHandle, 6, (uint)parentIndex, out pvData, out num2);
                System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO cmsg_signer_info = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO));
                cAttr = cmsg_signer_info.UnauthAttrs.cAttr;
                zero  = new IntPtr((long)cmsg_signer_info.UnauthAttrs.rgAttr);
            }
            for (num3 = 0; num3 < cAttr; num3++)
            {
                System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE crypt_attribute = (System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE)Marshal.PtrToStructure(zero, typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE));
                if ((string.Compare(crypt_attribute.pszObjId, "1.2.840.113549.1.9.6", StringComparison.OrdinalIgnoreCase) == 0) && (crypt_attribute.cValue > 0))
                {
                    if (childIndex < crypt_attribute.cValue)
                    {
                        System.Security.Cryptography.CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA cmsg_ctrl_del_signer_unauth_attr_para = new System.Security.Cryptography.CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA)))
                        {
                            dwSignerIndex     = (uint)parentIndex,
                            dwUnauthAttrIndex = num3
                        };
                        if (!System.Security.Cryptography.CAPI.CryptMsgControl(cryptMsgHandle, 0, 9, new IntPtr((void *)&cmsg_ctrl_del_signer_unauth_attr_para)))
                        {
                            throw new CryptographicException(Marshal.GetLastWin32Error());
                        }
                        if (crypt_attribute.cValue > 1)
                        {
                            try
                            {
                                byte[] buffer;
                                uint   num5 = (uint)((crypt_attribute.cValue - 1) * Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)));
                                System.Security.Cryptography.SafeLocalAllocHandle handle4 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)num5));
                                System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *rgValue = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *)crypt_attribute.rgValue;
                                System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *handle  = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *)handle4.DangerousGetHandle();
                                int num6 = 0;
                                while (num6 < crypt_attribute.cValue)
                                {
                                    if (num6 != childIndex)
                                    {
                                        handle[0] = rgValue[0];
                                    }
                                    num6++;
                                    rgValue++;
                                    handle++;
                                }
                                System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE structure = new System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE {
                                    pszObjId = crypt_attribute.pszObjId,
                                    cValue   = crypt_attribute.cValue - 1,
                                    rgValue  = handle4.DangerousGetHandle()
                                };
                                System.Security.Cryptography.SafeLocalAllocHandle handle5 = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE))));
                                Marshal.StructureToPtr(structure, handle5.DangerousGetHandle(), false);
                                try
                                {
                                    if (!System.Security.Cryptography.CAPI.EncodeObject(new IntPtr(0x16L), handle5.DangerousGetHandle(), out buffer))
                                    {
                                        throw new CryptographicException(Marshal.GetLastWin32Error());
                                    }
                                }
                                finally
                                {
                                    Marshal.DestroyStructure(handle5.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE));
                                    handle5.Dispose();
                                }
                                fixed(byte *numRef = buffer)
                                {
                                    System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA cmsg_ctrl_add_signer_unauth_attr_para = new System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA)))
                                    {
                                        dwSignerIndex = (uint)parentIndex
                                    };
                                    cmsg_ctrl_add_signer_unauth_attr_para.blob.cbData = (uint)buffer.Length;
                                    cmsg_ctrl_add_signer_unauth_attr_para.blob.pbData = new IntPtr((void *)numRef);
                                    if (!System.Security.Cryptography.CAPI.CryptMsgControl(cryptMsgHandle, 0, 8, new IntPtr((void *)&cmsg_ctrl_add_signer_unauth_attr_para)))
                                    {
                                        throw new CryptographicException(Marshal.GetLastWin32Error());
                                    }
                                }
                                handle4.Dispose();
                            }
                            catch (CryptographicException)
                            {
                                byte[] buffer2;
                                if (System.Security.Cryptography.CAPI.EncodeObject(new IntPtr(0x16L), zero, out buffer2))
                                {
                                    fixed(byte *numRef2 = buffer2)
                                    {
                                        System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA cmsg_ctrl_add_signer_unauth_attr_para2 = new System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA)))
                                        {
                                            dwSignerIndex = (uint)parentIndex
                                        };
                                        cmsg_ctrl_add_signer_unauth_attr_para2.blob.cbData = (uint)buffer2.Length;
                                        cmsg_ctrl_add_signer_unauth_attr_para2.blob.pbData = new IntPtr((void *)numRef2);
                                        System.Security.Cryptography.CAPI.CryptMsgControl(cryptMsgHandle, 0, 8, new IntPtr((void *)&cmsg_ctrl_add_signer_unauth_attr_para2));
                                    }
                                }
                                throw;
                            }
                        }
                        return;
                    }
                    childIndex -= (int)crypt_attribute.cValue;
                }
                zero = new IntPtr(((long)zero) + Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPT_ATTRIBUTE)));
            }
            if ((invalidHandle != null) && !invalidHandle.IsInvalid)
            {
                invalidHandle.Dispose();
            }
            if ((pvData != null) && !pvData.IsInvalid)
            {
                pvData.Dispose();
            }
            throw new CryptographicException(-2146885618);
        }