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);
 }
Example #2
0
        public static Oid GetContentType(byte[] encodedMessage)
        {
            Oid oid;

            if (encodedMessage == null)
            {
                throw new ArgumentNullException("encodedMessage");
            }
            System.Security.Cryptography.SafeCryptMsgHandle hCryptMsg = System.Security.Cryptography.CAPI.CAPISafe.CryptMsgOpenToDecode(0x10001, 0, 0, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
            if ((hCryptMsg == null) || hCryptMsg.IsInvalid)
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgUpdate(hCryptMsg, encodedMessage, (uint)encodedMessage.Length, true))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            switch (PkcsUtils.GetMessageType(hCryptMsg))
            {
            case 1:
                oid = new Oid("1.2.840.113549.1.7.1");
                break;

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

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

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

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

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

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