internal static unsafe IntPtr CreateCryptAttributes(CryptographicAttributeObjectCollection attributes)
        {
            if (attributes.Count == 0)
            {
                return(IntPtr.Zero);
            }
            uint num  = 0;
            uint num2 = AlignedLength((uint)Marshal.SizeOf(typeof(I_CRYPT_ATTRIBUTE)));
            uint num3 = AlignedLength((uint)Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB)));
            CryptographicAttributeObjectEnumerator enumerator = attributes.GetEnumerator();

            while (enumerator.MoveNext())
            {
                CryptographicAttributeObject current = enumerator.Current;
                num += num2;
                num += AlignedLength((uint)(current.Oid.Value.Length + 1));
                AsnEncodedDataEnumerator enumerator2 = current.Values.GetEnumerator();
                while (enumerator2.MoveNext())
                {
                    AsnEncodedData data = enumerator2.Current;
                    num += num3;
                    num += AlignedLength((uint)data.RawData.Length);
                }
            }
            System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)num));
            I_CRYPT_ATTRIBUTE *i_crypt_attributePtr = (I_CRYPT_ATTRIBUTE *)handle.DangerousGetHandle();
            IntPtr             ptr = new IntPtr(((long)handle.DangerousGetHandle()) + (num2 * attributes.Count));
            CryptographicAttributeObjectEnumerator enumerator3 = attributes.GetEnumerator();

            while (enumerator3.MoveNext())
            {
                CryptographicAttributeObject obj3 = enumerator3.Current;
                byte * numPtr = (byte *)ptr;
                byte[] bytes  = new byte[obj3.Oid.Value.Length + 1];
                System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *cryptoapi_blobPtr = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *)(numPtr + AlignedLength((uint)bytes.Length));
                i_crypt_attributePtr->pszObjId = (IntPtr)numPtr;
                i_crypt_attributePtr->cValue   = (uint)obj3.Values.Count;
                i_crypt_attributePtr->rgValue  = (IntPtr)cryptoapi_blobPtr;
                Encoding.ASCII.GetBytes(obj3.Oid.Value, 0, obj3.Oid.Value.Length, bytes, 0);
                Marshal.Copy(bytes, 0, i_crypt_attributePtr->pszObjId, bytes.Length);
                IntPtr destination = new IntPtr(((long)((ulong)cryptoapi_blobPtr)) + (obj3.Values.Count * num3));
                AsnEncodedDataEnumerator enumerator4 = obj3.Values.GetEnumerator();
                while (enumerator4.MoveNext())
                {
                    byte[] rawData = enumerator4.Current.RawData;
                    if (rawData.Length > 0)
                    {
                        cryptoapi_blobPtr->cbData = (uint)rawData.Length;
                        cryptoapi_blobPtr->pbData = destination;
                        Marshal.Copy(rawData, 0, destination, rawData.Length);
                        destination = new IntPtr(((long)destination) + AlignedLength((uint)rawData.Length));
                    }
                    cryptoapi_blobPtr++;
                }
                i_crypt_attributePtr++;
                ptr = destination;
            }
            GC.SuppressFinalize(handle);
            return(handle.DangerousGetHandle());
        }
 internal static unsafe System.Security.Cryptography.SafeLocalAllocHandle CreateEncodedCertBlob(X509Certificate2Collection certificates)
 {
     System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
     if (certificates.Count > 0)
     {
         invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr(certificates.Count * Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB))));
         System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *handle = (System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB *)invalidHandle.DangerousGetHandle();
         X509Certificate2Enumerator enumerator = certificates.GetEnumerator();
         while (enumerator.MoveNext())
         {
             System.Security.Cryptography.CAPI.CERT_CONTEXT cert_context = *((System.Security.Cryptography.CAPI.CERT_CONTEXT *)System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(enumerator.Current).DangerousGetHandle());
             handle->cbData = cert_context.cbCertEncoded;
             handle->pbData = cert_context.pbCertEncoded;
             handle++;
         }
     }
     return(invalidHandle);
 }
Exemplo n.º 3
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);
        }