private unsafe void Sign(CmsSigner signer, bool silent)
 {
     System.Security.Cryptography.SafeCryptMsgHandle           hCryptMsg = null;
     System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO cmsg_signed_encode_info = new System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO)));
     System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO structure = PkcsUtils.CreateSignerEncodeInfo(signer, silent);
     byte[] encodedMessage = null;
     try
     {
         System.Security.Cryptography.SafeLocalAllocHandle handle2 = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO))));
         try
         {
             Marshal.StructureToPtr(structure, handle2.DangerousGetHandle(), false);
             X509Certificate2Collection certificates = PkcsUtils.CreateBagOfCertificates(signer);
             System.Security.Cryptography.SafeLocalAllocHandle handle3 = PkcsUtils.CreateEncodedCertBlob(certificates);
             cmsg_signed_encode_info.cSigners     = 1;
             cmsg_signed_encode_info.rgSigners    = handle2.DangerousGetHandle();
             cmsg_signed_encode_info.cCertEncoded = (uint)certificates.Count;
             if (certificates.Count > 0)
             {
                 cmsg_signed_encode_info.rgCertEncoded = handle3.DangerousGetHandle();
             }
             if (string.Compare(this.ContentInfo.ContentType.Value, "1.2.840.113549.1.7.1", StringComparison.OrdinalIgnoreCase) == 0)
             {
                 hCryptMsg = System.Security.Cryptography.CAPI.CryptMsgOpenToEncode(0x10001, this.Detached ? 4 : 0, 2, new IntPtr((void *)&cmsg_signed_encode_info), IntPtr.Zero, IntPtr.Zero);
             }
             else
             {
                 hCryptMsg = System.Security.Cryptography.CAPI.CryptMsgOpenToEncode(0x10001, this.Detached ? 4 : 0, 2, new IntPtr((void *)&cmsg_signed_encode_info), this.ContentInfo.ContentType.Value, IntPtr.Zero);
             }
             if ((hCryptMsg == null) || hCryptMsg.IsInvalid)
             {
                 throw new CryptographicException(Marshal.GetLastWin32Error());
             }
             if ((this.ContentInfo.Content.Length > 0) && !System.Security.Cryptography.CAPI.CAPISafe.CryptMsgUpdate(hCryptMsg, this.ContentInfo.pContent, (uint)this.ContentInfo.Content.Length, true))
             {
                 throw new CryptographicException(Marshal.GetLastWin32Error());
             }
             encodedMessage = PkcsUtils.GetContent(hCryptMsg);
             hCryptMsg.Dispose();
             handle3.Dispose();
         }
         finally
         {
             Marshal.DestroyStructure(handle2.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO));
             handle2.Dispose();
         }
     }
     finally
     {
         structure.Dispose();
     }
     hCryptMsg = OpenToDecode(encodedMessage, this.ContentInfo, this.Detached);
     if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid)
     {
         this.m_safeCryptMsgHandle.Dispose();
     }
     this.m_safeCryptMsgHandle = hCryptMsg;
     GC.KeepAlive(signer);
 }
 private unsafe void Sign(CmsSigner signer, bool silent)
 {
     System.Security.Cryptography.SafeCryptMsgHandle hCryptMsg = null;
     System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO cmsg_signed_encode_info = new System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNED_ENCODE_INFO)));
     System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO structure = PkcsUtils.CreateSignerEncodeInfo(signer, silent);
     byte[] encodedMessage = null;
     try
     {
         System.Security.Cryptography.SafeLocalAllocHandle handle2 = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO))));
         try
         {
             Marshal.StructureToPtr(structure, handle2.DangerousGetHandle(), false);
             X509Certificate2Collection certificates = PkcsUtils.CreateBagOfCertificates(signer);
             System.Security.Cryptography.SafeLocalAllocHandle handle3 = PkcsUtils.CreateEncodedCertBlob(certificates);
             cmsg_signed_encode_info.cSigners = 1;
             cmsg_signed_encode_info.rgSigners = handle2.DangerousGetHandle();
             cmsg_signed_encode_info.cCertEncoded = (uint) certificates.Count;
             if (certificates.Count > 0)
             {
                 cmsg_signed_encode_info.rgCertEncoded = handle3.DangerousGetHandle();
             }
             if (string.Compare(this.ContentInfo.ContentType.Value, "1.2.840.113549.1.7.1", StringComparison.OrdinalIgnoreCase) == 0)
             {
                 hCryptMsg = System.Security.Cryptography.CAPI.CryptMsgOpenToEncode(0x10001, this.Detached ? 4 : 0, 2, new IntPtr((void*) &cmsg_signed_encode_info), IntPtr.Zero, IntPtr.Zero);
             }
             else
             {
                 hCryptMsg = System.Security.Cryptography.CAPI.CryptMsgOpenToEncode(0x10001, this.Detached ? 4 : 0, 2, new IntPtr((void*) &cmsg_signed_encode_info), this.ContentInfo.ContentType.Value, IntPtr.Zero);
             }
             if ((hCryptMsg == null) || hCryptMsg.IsInvalid)
             {
                 throw new CryptographicException(Marshal.GetLastWin32Error());
             }
             if ((this.ContentInfo.Content.Length > 0) && !System.Security.Cryptography.CAPI.CAPISafe.CryptMsgUpdate(hCryptMsg, this.ContentInfo.pContent, (uint) this.ContentInfo.Content.Length, true))
             {
                 throw new CryptographicException(Marshal.GetLastWin32Error());
             }
             encodedMessage = PkcsUtils.GetContent(hCryptMsg);
             hCryptMsg.Dispose();
             handle3.Dispose();
         }
         finally
         {
             Marshal.DestroyStructure(handle2.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_ENCODE_INFO));
             handle2.Dispose();
         }
     }
     finally
     {
         structure.Dispose();
     }
     hCryptMsg = OpenToDecode(encodedMessage, this.ContentInfo, this.Detached);
     if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid)
     {
         this.m_safeCryptMsgHandle.Dispose();
     }
     this.m_safeCryptMsgHandle = hCryptMsg;
     GC.KeepAlive(signer);
 }