internal static unsafe X509Certificate2Collection GetCertificates(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle)
        {
            uint num  = 0;
            uint num2 = (uint)Marshal.SizeOf(typeof(uint));
            X509Certificate2Collection certificates = new X509Certificate2Collection();

            if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 11, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2)))
            {
                checkErr(Marshal.GetLastWin32Error());
            }
            for (uint i = 0; i < num; i++)
            {
                uint cbData = 0;
                System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
                GetParam(safeCryptMsgHandle, 12, i, out invalidHandle, out cbData);
                if (cbData > 0)
                {
                    System.Security.Cryptography.SafeCertContextHandle handle2 = System.Security.Cryptography.CAPI.CAPISafe.CertCreateCertificateContext(0x10001, invalidHandle, cbData);
                    if ((handle2 == null) || handle2.IsInvalid)
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    certificates.Add(new X509Certificate2(handle2.DangerousGetHandle()));
                    handle2.Dispose();
                }
            }
            return(certificates);
        }
 private static System.Security.Cryptography.SafeCryptMsgHandle OpenToDecode(byte[] encodedMessage, System.Security.Cryptography.Pkcs.ContentInfo contentInfo, bool detached)
 {
     System.Security.Cryptography.SafeCryptMsgHandle hCryptMsg = System.Security.Cryptography.CAPI.CAPISafe.CryptMsgOpenToDecode(0x10001, detached ? 4 : 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());
     }
     if (2 != PkcsUtils.GetMessageType(hCryptMsg))
     {
         throw new CryptographicException(-2146889724);
     }
     if (detached)
     {
         byte[] content = contentInfo.Content;
         if (((content != null) && (content.Length > 0)) && !System.Security.Cryptography.CAPI.CAPISafe.CryptMsgUpdate(hCryptMsg, content, (uint)content.Length, true))
         {
             throw new CryptographicException(Marshal.GetLastWin32Error());
         }
     }
     return(hCryptMsg);
 }
        internal unsafe SignerInfoCollection(SignedCms signedCms)
        {
            uint num  = 0;
            uint num2 = (uint)Marshal.SizeOf(typeof(uint));

            System.Security.Cryptography.SafeCryptMsgHandle cryptMsgHandle = signedCms.GetCryptMsgHandle();
            if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(cryptMsgHandle, 5, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2)))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            SignerInfo[] infoArray = new SignerInfo[num];
            for (int i = 0; i < num; i++)
            {
                uint num4 = 0;
                if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(cryptMsgHandle, 6, (uint)i, IntPtr.Zero, new IntPtr((void *)&num4)))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)num4));
                if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(cryptMsgHandle, 6, (uint)i, pvData, new IntPtr((void *)&num4)))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
                infoArray[i] = new SignerInfo(signedCms, pvData);
            }
            this.m_signerInfos = infoArray;
        }
        internal static unsafe int GetSignerIndex(System.Security.Cryptography.SafeCryptMsgHandle safeCrytpMsgHandle, SignerInfo signerInfo, int startIndex)
        {
            uint num  = 0;
            uint num2 = (uint)Marshal.SizeOf(typeof(uint));

            if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 5, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2)))
            {
                checkErr(Marshal.GetLastWin32Error());
            }
            for (int i = startIndex; i < num; i++)
            {
                uint num4 = 0;
                if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 6, (uint)i, IntPtr.Zero, new IntPtr((void *)&num4)))
                {
                    checkErr(Marshal.GetLastWin32Error());
                }
                if (num4 > 0)
                {
                    System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)num4));
                    if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle, 6, (uint)i, pvData, new IntPtr((void *)&num4)))
                    {
                        checkErr(Marshal.GetLastWin32Error());
                    }
                    System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO cmsgSignerInfo    = signerInfo.GetCmsgSignerInfo();
                    System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO cmsg_signer_info2 = (System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_SIGNER_INFO));
                    if (System.Security.Cryptography.X509Certificates.X509Utils.MemEqual((byte *)cmsgSignerInfo.Issuer.pbData, cmsgSignerInfo.Issuer.cbData, (byte *)cmsg_signer_info2.Issuer.pbData, cmsg_signer_info2.Issuer.cbData) && System.Security.Cryptography.X509Certificates.X509Utils.MemEqual((byte *)cmsgSignerInfo.SerialNumber.pbData, cmsgSignerInfo.SerialNumber.cbData, (byte *)cmsg_signer_info2.SerialNumber.pbData, cmsg_signer_info2.SerialNumber.cbData))
                    {
                        return(i);
                    }
                    pvData.Dispose();
                }
            }
            throw new CryptographicException(-2146889714);
        }
 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);
 }
        internal static byte[] GetMessage(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle)
        {
            uint cbData = 0;

            byte[] pvData = new byte[0];
            GetParam(safeCryptMsgHandle, 0x1d, 0, out pvData, out cbData);
            return(pvData);
        }
 internal void ReopenToDecode()
 {
     byte[] message = PkcsUtils.GetMessage(this.m_safeCryptMsgHandle);
     if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid)
     {
         this.m_safeCryptMsgHandle.Dispose();
     }
     this.m_safeCryptMsgHandle = OpenToDecode(message, this.ContentInfo, this.Detached);
 }
        internal static unsafe uint GetVersion(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle)
        {
            uint num  = 0;
            uint num2 = (uint)Marshal.SizeOf(typeof(uint));

            if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 30, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2)))
            {
                checkErr(Marshal.GetLastWin32Error());
            }
            return(num);
        }
        internal static Oid GetContentType(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle)
        {
            uint cbData = 0;

            byte[] pvData = new byte[0];
            GetParam(safeCryptMsgHandle, 4, 0, out pvData, out cbData);
            if ((pvData.Length > 0) && (pvData[pvData.Length - 1] == 0))
            {
                byte[] destinationArray = new byte[pvData.Length - 1];
                Array.Copy(pvData, 0, destinationArray, 0, destinationArray.Length);
                pvData = destinationArray;
            }
            return(new Oid(Encoding.ASCII.GetString(pvData)));
        }
Ejemplo n.º 10
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);
        }
 public void Decode(byte[] encodedMessage)
 {
     if (encodedMessage == null)
     {
         throw new ArgumentNullException("encodedMessage");
     }
     if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid)
     {
         this.m_safeCryptMsgHandle.Dispose();
     }
     this.m_safeCryptMsgHandle = OpenToDecode(encodedMessage, this.ContentInfo, this.Detached);
     if (!this.Detached)
     {
         Oid    contentType = PkcsUtils.GetContentType(this.m_safeCryptMsgHandle);
         byte[] content     = PkcsUtils.GetContent(this.m_safeCryptMsgHandle);
         this.m_contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(contentType, content);
     }
 }
 public SignedCms(SubjectIdentifierType signerIdentifierType, System.Security.Cryptography.Pkcs.ContentInfo contentInfo, bool detached)
 {
     if (contentInfo == null)
     {
         throw new ArgumentNullException("contentInfo");
     }
     if (contentInfo.Content == null)
     {
         throw new ArgumentNullException("contentInfo.Content");
     }
     if (((signerIdentifierType != SubjectIdentifierType.SubjectKeyIdentifier) && (signerIdentifierType != SubjectIdentifierType.IssuerAndSerialNumber)) && (signerIdentifierType != SubjectIdentifierType.NoSignature))
     {
         signerIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber;
     }
     this.m_safeCryptMsgHandle   = System.Security.Cryptography.SafeCryptMsgHandle.InvalidHandle;
     this.m_signerIdentifierType = signerIdentifierType;
     this.m_version     = 0;
     this.m_contentInfo = contentInfo;
     this.m_detached    = detached;
 }
 public SignedCms(SubjectIdentifierType signerIdentifierType, System.Security.Cryptography.Pkcs.ContentInfo contentInfo, bool detached)
 {
     if (contentInfo == null)
     {
         throw new ArgumentNullException("contentInfo");
     }
     if (contentInfo.Content == null)
     {
         throw new ArgumentNullException("contentInfo.Content");
     }
     if (((signerIdentifierType != SubjectIdentifierType.SubjectKeyIdentifier) && (signerIdentifierType != SubjectIdentifierType.IssuerAndSerialNumber)) && (signerIdentifierType != SubjectIdentifierType.NoSignature))
     {
         signerIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber;
     }
     this.m_safeCryptMsgHandle = System.Security.Cryptography.SafeCryptMsgHandle.InvalidHandle;
     this.m_signerIdentifierType = signerIdentifierType;
     this.m_version = 0;
     this.m_contentInfo = contentInfo;
     this.m_detached = detached;
 }
 internal static unsafe void GetParam(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle, uint paramType, uint index, out System.Security.Cryptography.SafeLocalAllocHandle pvData, out uint cbData)
 {
     cbData = 0;
     pvData = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
     fixed(uint *numRef = ((uint *)cbData))
     {
         if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, pvData, new IntPtr((void *)numRef)))
         {
             checkErr(Marshal.GetLastWin32Error());
         }
         if (cbData > 0)
         {
             pvData = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)((ulong)cbData)));
             if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, pvData, new IntPtr((void *)numRef)))
             {
                 checkErr(Marshal.GetLastWin32Error());
             }
         }
     }
 }
        internal static unsafe AlgorithmIdentifier GetAlgorithmIdentifier(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle)
        {
            AlgorithmIdentifier identifier = new AlgorithmIdentifier();
            uint num = 0;

            if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15, 0, IntPtr.Zero, new IntPtr((void *)&num)))
            {
                checkErr(Marshal.GetLastWin32Error());
            }
            if (num > 0)
            {
                System.Security.Cryptography.SafeLocalAllocHandle pvData = System.Security.Cryptography.CAPI.LocalAlloc(0, new IntPtr((long)num));
                if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 15, 0, pvData, new IntPtr((void *)&num)))
                {
                    checkErr(Marshal.GetLastWin32Error());
                }
                System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER algorithmIdentifier = (System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CRYPT_ALGORITHM_IDENTIFIER));
                identifier = new AlgorithmIdentifier(algorithmIdentifier);
                pvData.Dispose();
            }
            return(identifier);
        }
 public EnvelopedCms(SubjectIdentifierType recipientIdentifierType, System.Security.Cryptography.Pkcs.ContentInfo contentInfo, AlgorithmIdentifier encryptionAlgorithm)
 {
     if (contentInfo == null)
     {
         throw new ArgumentNullException("contentInfo");
     }
     if (contentInfo.Content == null)
     {
         throw new ArgumentNullException("contentInfo.Content");
     }
     if (encryptionAlgorithm == null)
     {
         throw new ArgumentNullException("encryptionAlgorithm");
     }
     this.m_safeCryptMsgHandle = System.Security.Cryptography.SafeCryptMsgHandle.InvalidHandle;
     this.m_version = (recipientIdentifierType == SubjectIdentifierType.SubjectKeyIdentifier) ? 2 : 0;
     this.m_recipientIdentifierType = recipientIdentifierType;
     this.m_contentInfo = contentInfo;
     this.m_encryptionAlgorithm = encryptionAlgorithm;
     this.m_encryptionAlgorithm.Parameters = new byte[0];
     this.m_certificates = new X509Certificate2Collection();
     this.m_unprotectedAttributes = new CryptographicAttributeObjectCollection();
 }
 internal static unsafe void GetParam(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle, uint paramType, uint index, out byte[] pvData, out uint cbData)
 {
     cbData = 0;
     pvData = new byte[0];
     fixed(uint *numRef = ((uint *)cbData))
     {
         if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, IntPtr.Zero, new IntPtr((void *)numRef)))
         {
             checkErr(Marshal.GetLastWin32Error());
         }
         if (cbData > 0)
         {
             pvData = new byte[cbData];
             fixed(byte *numRef2 = pvData)
             {
                 if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, paramType, index, new IntPtr((void *)numRef2), new IntPtr((void *)numRef)))
                 {
                     checkErr(Marshal.GetLastWin32Error());
                 }
             }
         }
     }
 }
        internal static unsafe uint AddCertsToMessage(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle, X509Certificate2Collection bagOfCerts, X509Certificate2Collection chainOfCerts)
        {
            uint num = 0;
            X509Certificate2Enumerator enumerator = chainOfCerts.GetEnumerator();

            while (enumerator.MoveNext())
            {
                X509Certificate2 current = enumerator.Current;
                if (bagOfCerts.Find(X509FindType.FindByThumbprint, current.Thumbprint, false).Count == 0)
                {
                    System.Security.Cryptography.CAPI.CERT_CONTEXT   cert_context   = *((System.Security.Cryptography.CAPI.CERT_CONTEXT *)System.Security.Cryptography.X509Certificates.X509Utils.GetCertContext(current).DangerousGetHandle());
                    System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB cryptoapi_blob = new System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB {
                        cbData = cert_context.cbCertEncoded,
                        pbData = cert_context.pbCertEncoded
                    };
                    if (!System.Security.Cryptography.CAPI.CryptMsgControl(safeCryptMsgHandle, 0, 10, new IntPtr((long)((ulong)((IntPtr) & cryptoapi_blob)))))
                    {
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    }
                    num++;
                }
            }
            return(num);
        }
Ejemplo n.º 19
0
        internal static unsafe int GetSignerIndex (SafeCryptMsgHandle safeCrytpMsgHandle, SignerInfo signerInfo, int startIndex) {
            uint dwSigners = 0;
            uint cbCount = (uint) Marshal.SizeOf(typeof(uint));

            if (!CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle,
                                                CAPI.CMSG_SIGNER_COUNT_PARAM,
                                                0,
                                                new IntPtr(&dwSigners),
                                                new IntPtr(&cbCount)))
                checkErr(Marshal.GetLastWin32Error());

            for (int index = startIndex; index < (int) dwSigners; index++) {
                uint cbCmsgSignerInfo = 0;

                if (!CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle,
                                                    CAPI.CMSG_SIGNER_INFO_PARAM,
                                                    (uint)index,
                                                    IntPtr.Zero,
                                                    new IntPtr(&cbCmsgSignerInfo)))
                    checkErr(Marshal.GetLastWin32Error());

                if (cbCmsgSignerInfo > 0) {
                    SafeLocalAllocHandle pbCmsgSignerInfo = CAPI.LocalAlloc(CAPI.LMEM_FIXED, new IntPtr(cbCmsgSignerInfo));

                    if (!CAPI.CAPISafe.CryptMsgGetParam(safeCrytpMsgHandle,
                                                        CAPI.CMSG_SIGNER_INFO_PARAM,
                                                        (uint)index,
                                                        pbCmsgSignerInfo,
                                                        new IntPtr(&cbCmsgSignerInfo)))
                        checkErr(Marshal.GetLastWin32Error());

                    CAPI.CMSG_SIGNER_INFO cmsgSignerInfo1 = signerInfo.GetCmsgSignerInfo();
                    CAPI.CMSG_SIGNER_INFO cmsgSignerInfo2 = (CAPI.CMSG_SIGNER_INFO) Marshal.PtrToStructure(pbCmsgSignerInfo.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO));

                    if (X509Utils.MemEqual((byte *) cmsgSignerInfo1.Issuer.pbData,
                                  cmsgSignerInfo1.Issuer.cbData,
                                  (byte *) cmsgSignerInfo2.Issuer.pbData,
                                  cmsgSignerInfo2.Issuer.cbData) &&
                        X509Utils.MemEqual((byte *) cmsgSignerInfo1.SerialNumber.pbData,
                                  cmsgSignerInfo1.SerialNumber.cbData,
                                  (byte *) cmsgSignerInfo2.SerialNumber.pbData,
                                  cmsgSignerInfo2.SerialNumber.cbData)) {
                        return index; // Signer's index is found.
                    }

                    // Keep alive.
                    pbCmsgSignerInfo.Dispose();
                }
            }

            throw new CryptographicException(CAPI.CRYPT_E_SIGNER_NOT_FOUND);
        }
 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);
 }
 internal void ReopenToDecode()
 {
     byte[] message = PkcsUtils.GetMessage(this.m_safeCryptMsgHandle);
     if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid)
     {
         this.m_safeCryptMsgHandle.Dispose();
     }
     this.m_safeCryptMsgHandle = OpenToDecode(message, this.ContentInfo, this.Detached);
 }
Ejemplo n.º 22
0
        internal static unsafe uint GetVersion (SafeCryptMsgHandle safeCryptMsgHandle) {
            uint dwVersion = 0;
            uint cbCount = (uint) Marshal.SizeOf(typeof(uint));
            if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                CAPI.CMSG_VERSION_PARAM,
                                                0,
                                                new IntPtr(&dwVersion),
                                                new IntPtr(&cbCount)))
                checkErr(Marshal.GetLastWin32Error());

            return dwVersion;
        }
Ejemplo n.º 23
0
        internal unsafe RecipientInfoCollection (SafeCryptMsgHandle safeCryptMsgHandle) {
            bool cmsSupported = PkcsUtils.CmsSupported();
            uint dwRecipients = 0;
            uint cbCount = (uint) Marshal.SizeOf(typeof(uint));

            // Use CMS if supported.
            if (cmsSupported) {
                // CMS.
                if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                    CAPI.CMSG_CMS_RECIPIENT_COUNT_PARAM,
                                                    0,
                                                    new IntPtr(&dwRecipients),
                                                    new IntPtr(&cbCount)))
                    throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            else {
                // PKCS7.
                if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                    CAPI.CMSG_RECIPIENT_COUNT_PARAM,
                                                    0,
                                                    new IntPtr(&dwRecipients),
                                                    new IntPtr(&cbCount)))
                    throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            m_recipientInfos = new ArrayList();
            
            for (uint index = 0; index < dwRecipients; index++) {
                if (cmsSupported) {
                    uint cbCmsRecipientInfo;
                    SafeLocalAllocHandle pbCmsRecipientInfo;

                    PkcsUtils.GetParam(safeCryptMsgHandle, CAPI.CMSG_CMS_RECIPIENT_INFO_PARAM, index, out pbCmsRecipientInfo, out cbCmsRecipientInfo);
                    CAPI.CMSG_CMS_RECIPIENT_INFO cmsRecipientInfo = (CAPI.CMSG_CMS_RECIPIENT_INFO) Marshal.PtrToStructure(pbCmsRecipientInfo.DangerousGetHandle(), typeof(CAPI.CMSG_CMS_RECIPIENT_INFO));

                    switch (cmsRecipientInfo.dwRecipientChoice) {
                    case CAPI.CMSG_KEY_TRANS_RECIPIENT:
                        CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO keyTrans = (CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO) Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO));
                        m_recipientInfos.Add(new KeyTransRecipientInfo(pbCmsRecipientInfo, keyTrans, index));
                        break;
                    case CAPI.CMSG_KEY_AGREE_RECIPIENT:
                        CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO keyAgree = (CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO) Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO));
                        switch (keyAgree.dwOriginatorChoice) {
                        case CAPI.CMSG_KEY_AGREE_ORIGINATOR_CERT:
                            CAPI.CMSG_KEY_AGREE_CERT_ID_RECIPIENT_INFO certIdRecipient = (CAPI.CMSG_KEY_AGREE_CERT_ID_RECIPIENT_INFO) Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_AGREE_CERT_ID_RECIPIENT_INFO));
                            for (uint cRecipient = 0; cRecipient < certIdRecipient.cRecipientEncryptedKeys; cRecipient++) {
                                m_recipientInfos.Add(new KeyAgreeRecipientInfo(pbCmsRecipientInfo, certIdRecipient, index, cRecipient));
                            }
                            break;
                        case CAPI.CMSG_KEY_AGREE_ORIGINATOR_PUBLIC_KEY:
                            CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO publicKeyRecipient = (CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO) Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO));
                            for (uint cRecipient = 0; cRecipient < publicKeyRecipient.cRecipientEncryptedKeys; cRecipient++) {
                                m_recipientInfos.Add(new KeyAgreeRecipientInfo(pbCmsRecipientInfo, publicKeyRecipient, index, cRecipient));
                            }
                            break;
                        default:
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Invalid_Originator_Identifier_Choice"), keyAgree.dwOriginatorChoice.ToString(CultureInfo.CurrentCulture));
                        }
                        break;
                    default: 
                        throw new CryptographicException(CAPI.E_NOTIMPL);
                    }
                }
                else {
                    uint cbCertInfo;
                    SafeLocalAllocHandle pbCertInfo;

                    PkcsUtils.GetParam(safeCryptMsgHandle, CAPI.CMSG_RECIPIENT_INFO_PARAM, index, out pbCertInfo, out cbCertInfo);
                    CAPI.CERT_INFO certInfo = (CAPI.CERT_INFO) Marshal.PtrToStructure(pbCertInfo.DangerousGetHandle(), typeof(CAPI.CERT_INFO));

                    m_recipientInfos.Add(new KeyTransRecipientInfo(pbCertInfo, certInfo, index));
                }
            }

            m_safeCryptMsgHandle = safeCryptMsgHandle;
        }
Ejemplo n.º 24
0
        internal static byte[] GetMessage (SafeCryptMsgHandle safeCryptMsgHandle) {
            uint cbMessage = 0;
            byte[] message = new byte[0];

            GetParam(safeCryptMsgHandle, CAPI.CMSG_ENCODED_MESSAGE, 0, out message, out cbMessage);
            return message;
        }
Ejemplo n.º 25
0
        private unsafe void EncryptContent (CmsRecipientCollection recipients) {
            CMSG_ENCRYPT_PARAM encryptParam = new CMSG_ENCRYPT_PARAM();

            if (recipients.Count < 1)
                throw new CryptographicException(CAPI.CRYPT_E_RECIPIENT_NOT_FOUND);

            foreach (CmsRecipient recipient in recipients) {
                if (recipient.Certificate == null)
                    throw new ArgumentNullException(SecurityResources.GetResourceString("Cryptography_Cms_RecipientCertificateNotFound"));

                if ((PkcsUtils.GetRecipientInfoType(recipient.Certificate) == RecipientInfoType.KeyAgreement) ||
                    (recipient.RecipientIdentifierType == SubjectIdentifierType.SubjectKeyIdentifier))
                    encryptParam.useCms = true;
            }

            if (!encryptParam.useCms) {
                if (this.Certificates.Count > 0 || this.UnprotectedAttributes.Count > 0) {
                    encryptParam.useCms = true;
                }
            }

            if (encryptParam.useCms && !PkcsUtils.CmsSupported()) {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Not_Supported"));
            }

            CAPI.CMSG_ENVELOPED_ENCODE_INFO encodeInfo = new CAPI.CMSG_ENVELOPED_ENCODE_INFO(Marshal.SizeOf(typeof(CAPI.CMSG_ENVELOPED_ENCODE_INFO)));
            SafeLocalAllocHandle ceei = CAPI.LocalAlloc(CAPI.LPTR, new IntPtr(Marshal.SizeOf(typeof(CAPI.CMSG_ENVELOPED_ENCODE_INFO))));

            SetCspParams(this.ContentEncryptionAlgorithm, ref encryptParam);
            encodeInfo.ContentEncryptionAlgorithm.pszObjId = this.ContentEncryptionAlgorithm.Oid.Value;
            //encodeInfo.hCryptProv = encryptParam.safeCryptProvHandle.DangerousGetHandle(); 
            if (encryptParam.pvEncryptionAuxInfo != null && !encryptParam.pvEncryptionAuxInfo.IsInvalid) {
                encodeInfo.pvEncryptionAuxInfo = encryptParam.pvEncryptionAuxInfo.DangerousGetHandle();
            }

            encodeInfo.cRecipients = (uint) recipients.Count;

            List<SafeCertContextHandle> certContexts = null;
            if (encryptParam.useCms) {
                SetCmsRecipientParams(recipients, this.Certificates, this.UnprotectedAttributes, this.ContentEncryptionAlgorithm, ref encryptParam);
                encodeInfo.rgCmsRecipients = encryptParam.rgpRecipients.DangerousGetHandle();
                if (encryptParam.rgCertEncoded != null && !encryptParam.rgCertEncoded.IsInvalid) {
                    encodeInfo.cCertEncoded = (uint) this.Certificates.Count;
                    encodeInfo.rgCertEncoded = encryptParam.rgCertEncoded.DangerousGetHandle();
                }
                if (encryptParam.rgUnprotectedAttr != null && !encryptParam.rgUnprotectedAttr.IsInvalid) {
                    encodeInfo.cUnprotectedAttr = (uint) this.UnprotectedAttributes.Count;
                    encodeInfo.rgUnprotectedAttr = encryptParam.rgUnprotectedAttr.DangerousGetHandle();
                }
            }
            else {
                SetPkcs7RecipientParams(recipients, ref encryptParam, out certContexts);
                encodeInfo.rgpRecipients = encryptParam.rgpRecipients.DangerousGetHandle();
            }

            Marshal.StructureToPtr(encodeInfo, ceei.DangerousGetHandle(), false);

            try {
                SafeCryptMsgHandle safeCryptMsgHandle = CAPI.CryptMsgOpenToEncode(CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                                                                                  0,
                                                                                  CAPI.CMSG_ENVELOPED,
                                                                                  ceei.DangerousGetHandle(),
                                                                                  this.ContentInfo.ContentType.Value,
                                                                                  IntPtr.Zero);
                if (safeCryptMsgHandle == null || safeCryptMsgHandle.IsInvalid)
                    throw new CryptographicException(Marshal.GetLastWin32Error());

                if (m_safeCryptMsgHandle != null && !m_safeCryptMsgHandle.IsInvalid) {
                    m_safeCryptMsgHandle.Dispose();
                }

                m_safeCryptMsgHandle = safeCryptMsgHandle;
            }
            finally {
                Marshal.DestroyStructure(ceei.DangerousGetHandle(), typeof(CAPI.CMSG_ENVELOPED_ENCODE_INFO));
                ceei.Dispose();
            }

            byte[] encodedContent = new byte[0];
            if (String.Compare(this.ContentInfo.ContentType.Value, CAPI.szOID_RSA_data, StringComparison.OrdinalIgnoreCase) == 0) {
                byte[] content = this.ContentInfo.Content;
                fixed (byte * pbContent = content) {
                    CAPI.CRYPTOAPI_BLOB dataBlob = new CAPI.CRYPTOAPI_BLOB();
                    dataBlob.cbData = (uint) content.Length;
                    dataBlob.pbData = new IntPtr(pbContent);
                    if (!CAPI.EncodeObject(new IntPtr(CAPI.X509_OCTET_STRING), new IntPtr(&dataBlob), out encodedContent))
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
            else {
                encodedContent = this.ContentInfo.Content;
            }
            if (encodedContent.Length > 0) {
                if (!CAPI.CAPISafe.CryptMsgUpdate(m_safeCryptMsgHandle, encodedContent, (uint) encodedContent.Length, true))
                    throw new CryptographicException(Marshal.GetLastWin32Error());
            }

            // Keep alive
            GC.KeepAlive(encryptParam);
            GC.KeepAlive(recipients);
            GC.KeepAlive(certContexts);
        }
Ejemplo n.º 26
0
        internal static unsafe uint GetMessageType (SafeCryptMsgHandle safeCryptMsgHandle) {
            uint dwMsgType = 0;
            uint cbMsgType = (uint) Marshal.SizeOf(typeof(uint));
            if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                CAPI.CMSG_TYPE_PARAM,
                                                0,
                                                new IntPtr(&dwMsgType),
                                                new IntPtr(&cbMsgType)))
                checkErr(Marshal.GetLastWin32Error());

            return dwMsgType;
        }
Ejemplo n.º 27
0
 internal static bool CryptMsgCountersign([In] SafeCryptMsgHandle hCryptMsg, [In] uint dwIndex, [In] uint cCountersigners, [In] IntPtr rgCountersigners)
 {
     return(CAPIUnsafe.CryptMsgCountersign(hCryptMsg, dwIndex, cCountersigners, rgCountersigners));
 }
Ejemplo n.º 28
0
 internal static extern bool CryptMsgUpdate([In] SafeCryptMsgHandle hCryptMsg, [In] IntPtr pbData, [In] uint cbData, [In] bool fFinal);
Ejemplo n.º 29
0
        internal static unsafe AlgorithmIdentifier GetAlgorithmIdentifier (SafeCryptMsgHandle safeCryptMsgHandle) {
            AlgorithmIdentifier algorithmIdentifier = new AlgorithmIdentifier();

            uint cbAlgorithm = 0;
            if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                CAPI.CMSG_ENVELOPE_ALGORITHM_PARAM,
                                                0,
                                                IntPtr.Zero,
                                                new IntPtr(&cbAlgorithm)))
                checkErr(Marshal.GetLastWin32Error());

            if (cbAlgorithm > 0) {
                SafeLocalAllocHandle pbAlgorithm = CAPI.LocalAlloc(CAPI.LMEM_FIXED, new IntPtr(cbAlgorithm));
                if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                    CAPI.CMSG_ENVELOPE_ALGORITHM_PARAM,
                                                    0,
                                                    pbAlgorithm,
                                                    new IntPtr(&cbAlgorithm)))
                    checkErr(Marshal.GetLastWin32Error());

                CAPI.CRYPT_ALGORITHM_IDENTIFIER cryptAlgorithmIdentifier = (CAPI.CRYPT_ALGORITHM_IDENTIFIER) Marshal.PtrToStructure(pbAlgorithm.DangerousGetHandle(), typeof(CAPI.CRYPT_ALGORITHM_IDENTIFIER));
                algorithmIdentifier = new AlgorithmIdentifier(cryptAlgorithmIdentifier);
                pbAlgorithm.Dispose();
            }

            return algorithmIdentifier;
        }
Ejemplo n.º 30
0
        internal static unsafe void GetParam (SafeCryptMsgHandle safeCryptMsgHandle,
                                              uint paramType,
                                              uint index,
                                              out SafeLocalAllocHandle pvData,
                                              out uint cbData) {
            cbData = 0;
            pvData = SafeLocalAllocHandle.InvalidHandle;

            fixed (uint * pcbData = &cbData) {
                if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                    paramType,
                                                    index,
                                                    pvData,
                                                    new IntPtr(pcbData)))
                    checkErr(Marshal.GetLastWin32Error());

                if (cbData > 0) {
                    pvData = CAPI.LocalAlloc(CAPI.LPTR, new IntPtr(cbData));

                    if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                        paramType,
                                                        index,
                                                        pvData,
                                                        new IntPtr(pcbData)))
                        checkErr(Marshal.GetLastWin32Error());
                }
            }
        }
Ejemplo n.º 31
0
        internal static unsafe void GetParam (SafeCryptMsgHandle safeCryptMsgHandle,
                                              uint paramType,
                                              uint index,
                                              out byte[] pvData,
                                              out uint cbData) {
            cbData = 0;
            pvData = new byte[0];

            fixed (uint * pcbData = &cbData) {
                if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                    paramType,
                                                    index,
                                                    IntPtr.Zero,
                                                    new IntPtr(pcbData)))
                    checkErr(Marshal.GetLastWin32Error());

                if (cbData > 0) {
                    pvData = new byte[cbData];

                    fixed (byte * ppvData = &pvData[0]) {
                        if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                            paramType,
                                                            index,
                                                            new IntPtr(ppvData),
                                                            new IntPtr(pcbData)))
                            checkErr(Marshal.GetLastWin32Error());
                    }
                }
            }
        }
Ejemplo n.º 32
0
        internal static unsafe X509Certificate2Collection GetCertificates (SafeCryptMsgHandle safeCryptMsgHandle) {
            uint dwCount = 0;
            uint cbCount = (uint) Marshal.SizeOf(typeof(uint));
            X509Certificate2Collection certificates = new X509Certificate2Collection();

            if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                CAPI.CMSG_CERT_COUNT_PARAM,
                                                0,
                                                new IntPtr(&dwCount),
                                                new IntPtr(&cbCount)))
                checkErr(Marshal.GetLastWin32Error());

            for (uint index = 0; index < dwCount; index++) {
                uint cbEncoded = 0;
                SafeLocalAllocHandle pbEncoded = SafeLocalAllocHandle.InvalidHandle;

                GetParam(safeCryptMsgHandle, CAPI.CMSG_CERT_PARAM, index, out pbEncoded, out cbEncoded);
                if (cbEncoded > 0) {
                    SafeCertContextHandle safeCertContextHandle = CAPI.CAPISafe.CertCreateCertificateContext(CAPI.X509_ASN_ENCODING | CAPI.PKCS_7_ASN_ENCODING,
                                                                                                             pbEncoded,
                                                                                                             cbEncoded);
                    if (safeCertContextHandle == null || safeCertContextHandle.IsInvalid)
                        throw new CryptographicException(Marshal.GetLastWin32Error());

                    certificates.Add(new X509Certificate2(safeCertContextHandle.DangerousGetHandle()));
                    safeCertContextHandle.Dispose();
                }
            }

            return certificates;
        }
Ejemplo n.º 33
0
 protected internal static extern bool CryptMsgControl([In] SafeCryptMsgHandle hCryptMsg, [In] uint dwFlags, [In] uint dwCtrlType, [In] IntPtr pvCtrlPara);
Ejemplo n.º 34
0
 protected internal static extern bool CryptMsgCountersign([In] SafeCryptMsgHandle hCryptMsg, [In] uint dwIndex, [In] uint cCountersigners, [In] IntPtr rgCountersigners);
Ejemplo n.º 35
0
        internal static unsafe CryptographicAttributeObjectCollection GetUnprotectedAttributes (SafeCryptMsgHandle safeCryptMsgHandle) {
            uint cbUnprotectedAttr = 0;
            CryptographicAttributeObjectCollection attributes = new CryptographicAttributeObjectCollection();
            SafeLocalAllocHandle pbUnprotectedAttr = SafeLocalAllocHandle.InvalidHandle;
            if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                CAPI.CMSG_UNPROTECTED_ATTR_PARAM,
                                                0,
                                                pbUnprotectedAttr,
                                                new IntPtr(&cbUnprotectedAttr))) {
                int lastWin32Error = Marshal.GetLastWin32Error();
                if (lastWin32Error != CAPI.CRYPT_E_ATTRIBUTES_MISSING)
                    checkErr(Marshal.GetLastWin32Error());
            }

            if (cbUnprotectedAttr > 0) {
                using (pbUnprotectedAttr = CAPI.LocalAlloc(CAPI.LPTR, new IntPtr(cbUnprotectedAttr))) {
                    if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle,
                                                        CAPI.CMSG_UNPROTECTED_ATTR_PARAM,
                                                        0,
                                                        pbUnprotectedAttr,
                                                        new IntPtr(&cbUnprotectedAttr)))
                        checkErr(Marshal.GetLastWin32Error());

                    attributes = new CryptographicAttributeObjectCollection(pbUnprotectedAttr);
                }
            }
            return attributes;
        }
Ejemplo n.º 36
0
 internal RecipientInfoCollection () {
     m_safeCryptMsgHandle = SafeCryptMsgHandle.InvalidHandle;
     m_recipientInfos = new ArrayList();
 }
Ejemplo n.º 37
0
        internal static Oid GetContentType (SafeCryptMsgHandle safeCryptMsgHandle) {
            uint cbContentType = 0;
            byte[] contentType = new byte[0];

            GetParam(safeCryptMsgHandle, CAPI.CMSG_INNER_CONTENT_TYPE_PARAM, 0, out contentType, out cbContentType);
            if (contentType.Length > 0 && contentType[contentType.Length - 1] == 0) {
                byte[] temp = new byte[contentType.Length - 1];
                Array.Copy(contentType, 0, temp, 0, temp.Length);
                contentType = temp;
            }
            return new Oid(Encoding.ASCII.GetString(contentType));
        }
Ejemplo n.º 38
0
        public void Decode (byte[] encodedMessage) {
            if (encodedMessage == null)
                throw new ArgumentNullException("encodedMessage");

            if (m_safeCryptMsgHandle != null && !m_safeCryptMsgHandle.IsInvalid) {
                m_safeCryptMsgHandle.Dispose();
            }

            // Open to decode.
            m_safeCryptMsgHandle = OpenToDecode(encodedMessage);

            // Get version.
            m_version = (int) PkcsUtils.GetVersion(m_safeCryptMsgHandle);

            // Get contentInfo (content still encrypted).
            Oid contentType = PkcsUtils.GetContentType(m_safeCryptMsgHandle);
            byte[] content = PkcsUtils.GetContent(m_safeCryptMsgHandle);
            m_contentInfo = new ContentInfo(contentType, content); 
            
            // Get encryption algorithm.
            m_encryptionAlgorithm = PkcsUtils.GetAlgorithmIdentifier(m_safeCryptMsgHandle);

            // Get certificates.
            m_certificates = PkcsUtils.GetCertificates(m_safeCryptMsgHandle);

            // Get unprotected attributes.
            m_unprotectedAttributes = PkcsUtils.GetUnprotectedAttributes(m_safeCryptMsgHandle);
        }
 public void Decode(byte[] encodedMessage)
 {
     if (encodedMessage == null)
     {
         throw new ArgumentNullException("encodedMessage");
     }
     if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid)
     {
         this.m_safeCryptMsgHandle.Dispose();
     }
     this.m_safeCryptMsgHandle = OpenToDecode(encodedMessage, this.ContentInfo, this.Detached);
     if (!this.Detached)
     {
         Oid contentType = PkcsUtils.GetContentType(this.m_safeCryptMsgHandle);
         byte[] content = PkcsUtils.GetContent(this.m_safeCryptMsgHandle);
         this.m_contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(contentType, content);
     }
 }
Ejemplo n.º 40
0
 internal RecipientInfoCollection (RecipientInfo recipientInfo) {
     m_safeCryptMsgHandle = SafeCryptMsgHandle.InvalidHandle;
     m_recipientInfos = new ArrayList(1);
     m_recipientInfos.Add(recipientInfo);
 }
Ejemplo n.º 41
0
        internal static unsafe uint AddCertsToMessage (SafeCryptMsgHandle safeCryptMsgHandle, X509Certificate2Collection bagOfCerts, X509Certificate2Collection chainOfCerts) {
            uint certsAdded = 0;

            foreach (X509Certificate2 certificate in chainOfCerts) {
                // Skip it if already in the bag of certs.
                X509Certificate2Collection foundCerts = bagOfCerts.Find(X509FindType.FindByThumbprint, certificate.Thumbprint, false);
                if (foundCerts.Count == 0) {
                    SafeCertContextHandle safeCertContextHandle = X509Utils.GetCertContext(certificate);
                    CAPI.CERT_CONTEXT pCertContext = *((CAPI.CERT_CONTEXT*) safeCertContextHandle.DangerousGetHandle());

                    CAPI.CRYPTOAPI_BLOB certBlob = new CAPI.CRYPTOAPI_BLOB();
                    certBlob.cbData = pCertContext.cbCertEncoded;
                    certBlob.pbData = pCertContext.pbCertEncoded;

                    if (!CAPI.CryptMsgControl(safeCryptMsgHandle,
                                              0,
                                              CAPI.CMSG_CTRL_ADD_CERT,
                                              new IntPtr((long) &certBlob)))
                        throw new CryptographicException(Marshal.GetLastWin32Error());
                    certsAdded++;
                }
            }

            return certsAdded;
        }
 internal RecipientInfoCollection(RecipientInfo recipientInfo)
 {
     this.m_safeCryptMsgHandle = System.Security.Cryptography.SafeCryptMsgHandle.InvalidHandle;
     this.m_recipientInfos     = new ArrayList(1);
     this.m_recipientInfos.Add(recipientInfo);
 }
        internal unsafe RecipientInfoCollection(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle)
        {
            bool flag = PkcsUtils.CmsSupported();
            uint num  = 0;
            uint num2 = (uint)Marshal.SizeOf(typeof(uint));

            if (flag)
            {
                if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 0x21, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2)))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
            else if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 0x11, 0, new IntPtr((void *)&num), new IntPtr((void *)&num2)))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            this.m_recipientInfos = new ArrayList();
            for (uint i = 0; i < num; i++)
            {
                uint num4;
                System.Security.Cryptography.SafeLocalAllocHandle handle;
                System.Security.Cryptography.CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO cmsg_key_agree_public_key_recipient_info;
                uint num7;
                System.Security.Cryptography.SafeLocalAllocHandle handle2;
                if (!flag)
                {
                    goto Label_020B;
                }
                PkcsUtils.GetParam(safeCryptMsgHandle, 0x24, i, out handle, out num4);
                System.Security.Cryptography.CAPI.CMSG_CMS_RECIPIENT_INFO cmsg_cms_recipient_info = (System.Security.Cryptography.CAPI.CMSG_CMS_RECIPIENT_INFO)Marshal.PtrToStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_CMS_RECIPIENT_INFO));
                switch (cmsg_cms_recipient_info.dwRecipientChoice)
                {
                case 1:
                {
                    System.Security.Cryptography.CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO keyTrans = (System.Security.Cryptography.CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO)Marshal.PtrToStructure(cmsg_cms_recipient_info.pRecipientInfo, typeof(System.Security.Cryptography.CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO));
                    this.m_recipientInfos.Add(new KeyTransRecipientInfo(handle, keyTrans, i));
                    continue;
                }

                case 2:
                {
                    System.Security.Cryptography.CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO cmsg_key_agree_recipient_info = (System.Security.Cryptography.CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO)Marshal.PtrToStructure(cmsg_cms_recipient_info.pRecipientInfo, typeof(System.Security.Cryptography.CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO));
                    switch (cmsg_key_agree_recipient_info.dwOriginatorChoice)
                    {
                    case 2:
                        goto Label_0192;
                    }
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Invalid_Originator_Identifier_Choice"), cmsg_key_agree_recipient_info.dwOriginatorChoice.ToString(CultureInfo.CurrentCulture));
                }

                default:
                    throw new CryptographicException(-2147483647);
                }
                System.Security.Cryptography.CAPI.CMSG_KEY_AGREE_CERT_ID_RECIPIENT_INFO certIdRecipient = (System.Security.Cryptography.CAPI.CMSG_KEY_AGREE_CERT_ID_RECIPIENT_INFO)Marshal.PtrToStructure(cmsg_cms_recipient_info.pRecipientInfo, typeof(System.Security.Cryptography.CAPI.CMSG_KEY_AGREE_CERT_ID_RECIPIENT_INFO));
                for (uint j = 0; j < certIdRecipient.cRecipientEncryptedKeys; j++)
                {
                    this.m_recipientInfos.Add(new KeyAgreeRecipientInfo(handle, certIdRecipient, i, j));
                }
                continue;
Label_0192:
                cmsg_key_agree_public_key_recipient_info = (System.Security.Cryptography.CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO)Marshal.PtrToStructure(cmsg_cms_recipient_info.pRecipientInfo, typeof(System.Security.Cryptography.CAPI.CMSG_KEY_AGREE_PUBLIC_KEY_RECIPIENT_INFO));
                for (uint k = 0; k < cmsg_key_agree_public_key_recipient_info.cRecipientEncryptedKeys; k++)
                {
                    this.m_recipientInfos.Add(new KeyAgreeRecipientInfo(handle, cmsg_key_agree_public_key_recipient_info, i, k));
                }
                continue;
Label_020B:
                PkcsUtils.GetParam(safeCryptMsgHandle, 0x13, i, out handle2, out num7);
                System.Security.Cryptography.CAPI.CERT_INFO certInfo = (System.Security.Cryptography.CAPI.CERT_INFO)Marshal.PtrToStructure(handle2.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CERT_INFO));
                this.m_recipientInfos.Add(new KeyTransRecipientInfo(handle2, certInfo, i));
            }
            this.m_safeCryptMsgHandle = safeCryptMsgHandle;
        }
        internal static unsafe CryptographicAttributeObjectCollection GetUnprotectedAttributes(System.Security.Cryptography.SafeCryptMsgHandle safeCryptMsgHandle)
        {
            uint num = 0;
            CryptographicAttributeObjectCollection objects = new CryptographicAttributeObjectCollection();

            System.Security.Cryptography.SafeLocalAllocHandle invalidHandle = System.Security.Cryptography.SafeLocalAllocHandle.InvalidHandle;
            if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 0x25, 0, invalidHandle, new IntPtr((void *)&num)) && (Marshal.GetLastWin32Error() != -2146889713))
            {
                checkErr(Marshal.GetLastWin32Error());
            }
            if (num <= 0)
            {
                return(objects);
            }
            using (invalidHandle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr((long)num)))
            {
                if (!System.Security.Cryptography.CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 0x25, 0, invalidHandle, new IntPtr((void *)&num)))
                {
                    checkErr(Marshal.GetLastWin32Error());
                }
                return(new CryptographicAttributeObjectCollection(invalidHandle));
            }
        }
Ejemplo n.º 45
0
        internal static byte[] GetContent (SafeCryptMsgHandle safeCryptMsgHandle) {
            uint cbContent = 0;
            byte[] content = new byte[0];

            GetParam(safeCryptMsgHandle, CAPI.CMSG_CONTENT_PARAM, 0, out content, out cbContent);

            return content;
        }
Ejemplo n.º 46
0
 internal static extern bool CryptMsgGetParam([In] SafeCryptMsgHandle hCryptMsg, [In] uint dwParamType, [In] uint dwIndex, [In, Out] SafeLocalAllocHandle pvData, [In, Out] IntPtr pcbData);
 public void Decode(byte[] encodedMessage)
 {
     if (encodedMessage == null)
     {
         throw new ArgumentNullException("encodedMessage");
     }
     if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid)
     {
         this.m_safeCryptMsgHandle.Dispose();
     }
     this.m_safeCryptMsgHandle = OpenToDecode(encodedMessage);
     this.m_version = (int) PkcsUtils.GetVersion(this.m_safeCryptMsgHandle);
     Oid contentType = PkcsUtils.GetContentType(this.m_safeCryptMsgHandle);
     byte[] content = PkcsUtils.GetContent(this.m_safeCryptMsgHandle);
     this.m_contentInfo = new System.Security.Cryptography.Pkcs.ContentInfo(contentType, content);
     this.m_encryptionAlgorithm = PkcsUtils.GetAlgorithmIdentifier(this.m_safeCryptMsgHandle);
     this.m_certificates = PkcsUtils.GetCertificates(this.m_safeCryptMsgHandle);
     this.m_unprotectedAttributes = PkcsUtils.GetUnprotectedAttributes(this.m_safeCryptMsgHandle);
 }
Ejemplo n.º 48
0
 internal static bool CryptMsgControl([In] SafeCryptMsgHandle hCryptMsg, [In] uint dwFlags, [In] uint dwCtrlType, [In] IntPtr pvCtrlPara)
 {
     return(CAPIUnsafe.CryptMsgControl(hCryptMsg, dwFlags, dwCtrlType, pvCtrlPara));
 }
 private unsafe void EncryptContent(CmsRecipientCollection recipients)
 {
     CMSG_ENCRYPT_PARAM encryptParam = new CMSG_ENCRYPT_PARAM();
     if (recipients.Count < 1)
     {
         throw new CryptographicException(-2146889717);
     }
     CmsRecipientEnumerator enumerator = recipients.GetEnumerator();
     while (enumerator.MoveNext())
     {
         CmsRecipient current = enumerator.Current;
         if (current.Certificate == null)
         {
             throw new ArgumentNullException(SecurityResources.GetResourceString("Cryptography_Cms_RecipientCertificateNotFound"));
         }
         if ((PkcsUtils.GetRecipientInfoType(current.Certificate) == RecipientInfoType.KeyAgreement) || (current.RecipientIdentifierType == SubjectIdentifierType.SubjectKeyIdentifier))
         {
             encryptParam.useCms = true;
         }
     }
     if (!encryptParam.useCms && ((this.Certificates.Count > 0) || (this.UnprotectedAttributes.Count > 0)))
     {
         encryptParam.useCms = true;
     }
     if (encryptParam.useCms && !PkcsUtils.CmsSupported())
     {
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Not_Supported"));
     }
     System.Security.Cryptography.CAPI.CMSG_ENVELOPED_ENCODE_INFO structure = new System.Security.Cryptography.CAPI.CMSG_ENVELOPED_ENCODE_INFO(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_ENVELOPED_ENCODE_INFO)));
     System.Security.Cryptography.SafeLocalAllocHandle handle = System.Security.Cryptography.CAPI.LocalAlloc(0x40, new IntPtr(Marshal.SizeOf(typeof(System.Security.Cryptography.CAPI.CMSG_ENVELOPED_ENCODE_INFO))));
     SetCspParams(this.ContentEncryptionAlgorithm, ref encryptParam);
     structure.ContentEncryptionAlgorithm.pszObjId = this.ContentEncryptionAlgorithm.Oid.Value;
     if ((encryptParam.pvEncryptionAuxInfo != null) && !encryptParam.pvEncryptionAuxInfo.IsInvalid)
     {
         structure.pvEncryptionAuxInfo = encryptParam.pvEncryptionAuxInfo.DangerousGetHandle();
     }
     structure.cRecipients = (uint) recipients.Count;
     List<System.Security.Cryptography.SafeCertContextHandle> certContexts = null;
     if (encryptParam.useCms)
     {
         SetCmsRecipientParams(recipients, this.Certificates, this.UnprotectedAttributes, this.ContentEncryptionAlgorithm, ref encryptParam);
         structure.rgCmsRecipients = encryptParam.rgpRecipients.DangerousGetHandle();
         if ((encryptParam.rgCertEncoded != null) && !encryptParam.rgCertEncoded.IsInvalid)
         {
             structure.cCertEncoded = (uint) this.Certificates.Count;
             structure.rgCertEncoded = encryptParam.rgCertEncoded.DangerousGetHandle();
         }
         if ((encryptParam.rgUnprotectedAttr != null) && !encryptParam.rgUnprotectedAttr.IsInvalid)
         {
             structure.cUnprotectedAttr = (uint) this.UnprotectedAttributes.Count;
             structure.rgUnprotectedAttr = encryptParam.rgUnprotectedAttr.DangerousGetHandle();
         }
     }
     else
     {
         SetPkcs7RecipientParams(recipients, ref encryptParam, out certContexts);
         structure.rgpRecipients = encryptParam.rgpRecipients.DangerousGetHandle();
     }
     Marshal.StructureToPtr(structure, handle.DangerousGetHandle(), false);
     try
     {
         System.Security.Cryptography.SafeCryptMsgHandle handle2 = System.Security.Cryptography.CAPI.CryptMsgOpenToEncode(0x10001, 0, 3, handle.DangerousGetHandle(), this.ContentInfo.ContentType.Value, IntPtr.Zero);
         if ((handle2 == null) || handle2.IsInvalid)
         {
             throw new CryptographicException(Marshal.GetLastWin32Error());
         }
         if ((this.m_safeCryptMsgHandle != null) && !this.m_safeCryptMsgHandle.IsInvalid)
         {
             this.m_safeCryptMsgHandle.Dispose();
         }
         this.m_safeCryptMsgHandle = handle2;
     }
     finally
     {
         Marshal.DestroyStructure(handle.DangerousGetHandle(), typeof(System.Security.Cryptography.CAPI.CMSG_ENVELOPED_ENCODE_INFO));
         handle.Dispose();
     }
     byte[] encodedData = new byte[0];
     if (string.Compare(this.ContentInfo.ContentType.Value, "1.2.840.113549.1.7.1", StringComparison.OrdinalIgnoreCase) == 0)
     {
         byte[] content = this.ContentInfo.Content;
         fixed (byte* numRef = content)
         {
             System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB cryptoapi_blob = new System.Security.Cryptography.CAPI.CRYPTOAPI_BLOB {
                 cbData = (uint) content.Length,
                 pbData = new IntPtr((void*) numRef)
             };
             if (!System.Security.Cryptography.CAPI.EncodeObject(new IntPtr(0x19L), new IntPtr((void*) &cryptoapi_blob), out encodedData))
             {
                 throw new CryptographicException(Marshal.GetLastWin32Error());
             }
         }
     }
     else
     {
         encodedData = this.ContentInfo.Content;
     }
     if ((encodedData.Length > 0) && !System.Security.Cryptography.CAPI.CAPISafe.CryptMsgUpdate(this.m_safeCryptMsgHandle, encodedData, (uint) encodedData.Length, true))
     {
         throw new CryptographicException(Marshal.GetLastWin32Error());
     }
     GC.KeepAlive(encryptParam);
     GC.KeepAlive(recipients);
     GC.KeepAlive(certContexts);
 }
Ejemplo n.º 50
0
        private static SafeCertContextHandle GetSignerInPKCS7Store(SafeCertStoreHandle hCertStore, SafeCryptMsgHandle hCryptMsg)
        {
            // make sure that there is at least one signer of the certificate store
            int dwSigners;
            int cbSigners = sizeof(int);
            if (!Interop.crypt32.CryptMsgGetParam(hCryptMsg, CryptMessageParameterType.CMSG_SIGNER_COUNT_PARAM, 0, out dwSigners, ref cbSigners))
                throw Marshal.GetHRForLastWin32Error().ToCryptographicException();;
            if (dwSigners == 0)
                throw ErrorCode.CRYPT_E_SIGNER_NOT_FOUND.ToCryptographicException();

            // get the first signer from the store, and use that as the loaded certificate
            int cbData = 0;
            if (!Interop.crypt32.CryptMsgGetParam(hCryptMsg, CryptMessageParameterType.CMSG_SIGNER_INFO_PARAM, 0, null, ref cbData))
                throw Marshal.GetHRForLastWin32Error().ToCryptographicException();;

            byte[] cmsgSignerBytes = new byte[cbData];
            if (!Interop.crypt32.CryptMsgGetParam(hCryptMsg, CryptMessageParameterType.CMSG_SIGNER_INFO_PARAM, 0, cmsgSignerBytes, ref cbData))
                throw Marshal.GetHRForLastWin32Error().ToCryptographicException();;

            CERT_INFO certInfo = default(CERT_INFO);
            unsafe
            {
                fixed (byte* pCmsgSignerBytes = cmsgSignerBytes)
                {
                    CMSG_SIGNER_INFO_Partial* pCmsgSignerInfo = (CMSG_SIGNER_INFO_Partial*)pCmsgSignerBytes;
                    certInfo.Issuer.cbData = pCmsgSignerInfo->Issuer.cbData;
                    certInfo.Issuer.pbData = pCmsgSignerInfo->Issuer.pbData;
                    certInfo.SerialNumber.cbData = pCmsgSignerInfo->SerialNumber.cbData;
                    certInfo.SerialNumber.pbData = pCmsgSignerInfo->SerialNumber.pbData;
                }

                SafeCertContextHandle pCertContext = null;
                if (!Interop.crypt32.CertFindCertificateInStore(hCertStore, CertFindType.CERT_FIND_SUBJECT_CERT, &certInfo, ref pCertContext))
                    throw Marshal.GetHRForLastWin32Error().ToCryptographicException();;

                return pCertContext;
            }
        }
Ejemplo n.º 51
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);
        }