예제 #1
0
        private void Reset(SubjectIdentifierType recipientIdentifierType, X509Certificate2 certificate)
        {
            if (certificate == null)
            {
                throw new ArgumentNullException("certificate");
            }
            switch (recipientIdentifierType)
            {
            case SubjectIdentifierType.Unknown:
                recipientIdentifierType = SubjectIdentifierType.IssuerAndSerialNumber;
                goto case 1;

            case SubjectIdentifierType.IssuerAndSerialNumber:
                this.m_recipientIdentifierType = recipientIdentifierType;
                this.m_certificate             = certificate;
                break;

            case SubjectIdentifierType.SubjectKeyIdentifier:
                if (!PkcsUtils.CmsSupported())
                {
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Not_Supported"));
                }
                else
                {
                    goto case 1;
                }

            default:
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Invalid_Subject_Identifier_Type"), ((object)recipientIdentifierType).ToString());
            }
        }
예제 #2
0
        internal void Reset(SubjectIdentifierOrKeyType type, object value)
        {
            switch (type)
            {
            case SubjectIdentifierOrKeyType.Unknown:
                this.m_type  = type;
                this.m_value = value;
                break;

            case SubjectIdentifierOrKeyType.IssuerAndSerialNumber:
                if (value.GetType() != typeof(X509IssuerSerial))
                {
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Invalid_Subject_Identifier_Type_Value_Mismatch"), value.GetType().ToString());
                }
                else
                {
                    goto case 0;
                }

            case SubjectIdentifierOrKeyType.SubjectKeyIdentifier:
                if (!PkcsUtils.CmsSupported())
                {
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Not_Supported"));
                }
                if (value.GetType() != typeof(string))
                {
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Invalid_Subject_Identifier_Type_Value_Mismatch"), value.GetType().ToString());
                }
                else
                {
                    goto case 0;
                }

            case SubjectIdentifierOrKeyType.PublicKeyInfo:
                if (!PkcsUtils.CmsSupported())
                {
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Not_Supported"));
                }
                if (value.GetType() != typeof(PublicKeyInfo))
                {
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Invalid_Subject_Identifier_Type_Value_Mismatch"), value.GetType().ToString());
                }
                else
                {
                    goto case 0;
                }

            default:
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Invalid_Subject_Identifier_Type"), ((object)type).ToString());
            }
        }
예제 #3
0
        internal RecipientInfoCollection(SafeCryptMsgHandle safeCryptMsgHandle)
        {
            bool flag = PkcsUtils.CmsSupported();
            uint num1 = 0U;
            uint num2 = (uint)Marshal.SizeOf(typeof(uint));

            if (flag)
            {
                if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 33U, 0U, new IntPtr((void *)&num1), new IntPtr((void *)&num2)))
                {
                    throw new CryptographicException(Marshal.GetLastWin32Error());
                }
            }
            else if (!CAPI.CAPISafe.CryptMsgGetParam(safeCryptMsgHandle, 17U, 0U, new IntPtr((void *)&num1), new IntPtr((void *)&num2)))
            {
                throw new CryptographicException(Marshal.GetLastWin32Error());
            }
            this.m_recipientInfos = new ArrayList();
            for (uint index = 0U; index < num1; ++index)
            {
                if (flag)
                {
                    SafeLocalAllocHandle pvData;
                    uint cbData;
                    PkcsUtils.GetParam(safeCryptMsgHandle, 36U, index, out pvData, out cbData);
                    CAPI.CMSG_CMS_RECIPIENT_INFO cmsRecipientInfo = (CAPI.CMSG_CMS_RECIPIENT_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(CAPI.CMSG_CMS_RECIPIENT_INFO));
                    switch (cmsRecipientInfo.dwRecipientChoice)
                    {
                    case 1U:
                        CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO keyTrans = (CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO)Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_TRANS_RECIPIENT_INFO));
                        this.m_recipientInfos.Add((object)new KeyTransRecipientInfo(pvData, keyTrans, index));
                        continue;

                    case 2U:
                        CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO agreeRecipientInfo = (CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO)Marshal.PtrToStructure(cmsRecipientInfo.pRecipientInfo, typeof(CAPI.CMSG_KEY_AGREE_RECIPIENT_INFO));
                        switch (agreeRecipientInfo.dwOriginatorChoice)
                        {
                        case 1U:
                            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 subIndex = 0U; subIndex < certIdRecipient.cRecipientEncryptedKeys; ++subIndex)
                            {
                                this.m_recipientInfos.Add((object)new KeyAgreeRecipientInfo(pvData, certIdRecipient, index, subIndex));
                            }
                            continue;

                        case 2U:
                            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 subIndex = 0U; subIndex < publicKeyRecipient.cRecipientEncryptedKeys; ++subIndex)
                            {
                                this.m_recipientInfos.Add((object)new KeyAgreeRecipientInfo(pvData, publicKeyRecipient, index, subIndex));
                            }
                            continue;

                        default:
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Cms_Invalid_Originator_Identifier_Choice"), agreeRecipientInfo.dwOriginatorChoice.ToString((IFormatProvider)CultureInfo.CurrentCulture));
                        }

                    default:
                        throw new CryptographicException(-2147483647);
                    }
                }
                else
                {
                    SafeLocalAllocHandle pvData;
                    uint cbData;
                    PkcsUtils.GetParam(safeCryptMsgHandle, 19U, index, out pvData, out cbData);
                    CAPI.CERT_INFO certInfo = (CAPI.CERT_INFO)Marshal.PtrToStructure(pvData.DangerousGetHandle(), typeof(CAPI.CERT_INFO));
                    this.m_recipientInfos.Add((object)new KeyTransRecipientInfo(pvData, certInfo, index));
                }
            }
            this.m_safeCryptMsgHandle = safeCryptMsgHandle;
        }
예제 #4
0
        private unsafe void RemoveCounterSignature(int parentIndex, int childIndex)
        {
            if (parentIndex < 0)
            {
                throw new ArgumentOutOfRangeException("parentIndex");
            }
            if (childIndex < 0)
            {
                throw new ArgumentOutOfRangeException("childIndex");
            }
            uint cbData1 = 0U;
            SafeLocalAllocHandle pvData1 = SafeLocalAllocHandle.InvalidHandle;
            uint cbData2 = 0U;
            SafeLocalAllocHandle pvData2      = SafeLocalAllocHandle.InvalidHandle;
            IntPtr             num1           = IntPtr.Zero;
            SafeCryptMsgHandle cryptMsgHandle = this.m_signedCms.GetCryptMsgHandle();
            uint num2;

            if (PkcsUtils.CmsSupported())
            {
                PkcsUtils.GetParam(cryptMsgHandle, 39U, (uint)parentIndex, out pvData1, out cbData1);
                CAPI.CMSG_CMS_SIGNER_INFO cmsgCmsSignerInfo = (CAPI.CMSG_CMS_SIGNER_INFO)Marshal.PtrToStructure(pvData1.DangerousGetHandle(), typeof(CAPI.CMSG_CMS_SIGNER_INFO));
                num2 = cmsgCmsSignerInfo.UnauthAttrs.cAttr;
                num1 = new IntPtr((long)cmsgCmsSignerInfo.UnauthAttrs.rgAttr);
            }
            else
            {
                PkcsUtils.GetParam(cryptMsgHandle, 6U, (uint)parentIndex, out pvData2, out cbData2);
                CAPI.CMSG_SIGNER_INFO cmsgSignerInfo = (CAPI.CMSG_SIGNER_INFO)Marshal.PtrToStructure(pvData2.DangerousGetHandle(), typeof(CAPI.CMSG_SIGNER_INFO));
                num2 = cmsgSignerInfo.UnauthAttrs.cAttr;
                num1 = new IntPtr((long)cmsgSignerInfo.UnauthAttrs.rgAttr);
            }
            for (uint index = 0U; index < num2; ++index)
            {
                CAPI.CRYPT_ATTRIBUTE cryptAttribute1 = (CAPI.CRYPT_ATTRIBUTE)Marshal.PtrToStructure(num1, typeof(CAPI.CRYPT_ATTRIBUTE));
                if (string.Compare(cryptAttribute1.pszObjId, "1.2.840.113549.1.9.6", StringComparison.OrdinalIgnoreCase) == 0 && cryptAttribute1.cValue > 0U)
                {
                    if (childIndex < (int)cryptAttribute1.cValue)
                    {
                        if (!CAPI.CryptMsgControl(cryptMsgHandle, 0U, 9U, new IntPtr((void *)&new CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(CAPI.CMSG_CTRL_DEL_SIGNER_UNAUTH_ATTR_PARA)))
                        {
                            dwSignerIndex = (uint)parentIndex,
                            dwUnauthAttrIndex = index
                        })))
                        {
                            throw new CryptographicException(Marshal.GetLastWin32Error());
                        }
                        if (cryptAttribute1.cValue <= 1U)
                        {
                            return;
                        }
                        try
                        {
                            SafeLocalAllocHandle localAllocHandle1 = CAPI.LocalAlloc(64U, new IntPtr((long)(uint)((ulong)(cryptAttribute1.cValue - 1U) * (ulong)Marshal.SizeOf(typeof(CAPI.CRYPTOAPI_BLOB)))));
                            CAPI.CRYPTOAPI_BLOB *cryptoapiBlobPtr1 = (CAPI.CRYPTOAPI_BLOB *)(void *) cryptAttribute1.rgValue;
                            CAPI.CRYPTOAPI_BLOB *cryptoapiBlobPtr2 = (CAPI.CRYPTOAPI_BLOB *)(void *) localAllocHandle1.DangerousGetHandle();
                            int num3 = 0;
                            while (num3 < (int)cryptAttribute1.cValue)
                            {
                                if (num3 != childIndex)
                                {
                                    *cryptoapiBlobPtr2 = *cryptoapiBlobPtr1;
                                }
                                ++num3;
                                ++cryptoapiBlobPtr1;
                                ++cryptoapiBlobPtr2;
                            }
                            CAPI.CRYPT_ATTRIBUTE cryptAttribute2 = new CAPI.CRYPT_ATTRIBUTE();
                            cryptAttribute2.pszObjId = cryptAttribute1.pszObjId;
                            cryptAttribute2.cValue   = cryptAttribute1.cValue - 1U;
                            cryptAttribute2.rgValue  = localAllocHandle1.DangerousGetHandle();
                            SafeLocalAllocHandle localAllocHandle2 = CAPI.LocalAlloc(64U, new IntPtr(Marshal.SizeOf(typeof(CAPI.CRYPT_ATTRIBUTE))));
                            Marshal.StructureToPtr((object)cryptAttribute2, localAllocHandle2.DangerousGetHandle(), false);
                            byte[] encodedData;
                            try
                            {
                                if (!CAPI.EncodeObject(new IntPtr(22L), localAllocHandle2.DangerousGetHandle(), out encodedData))
                                {
                                    throw new CryptographicException(Marshal.GetLastWin32Error());
                                }
                            }
                            finally
                            {
                                Marshal.DestroyStructure(localAllocHandle2.DangerousGetHandle(), typeof(CAPI.CRYPT_ATTRIBUTE));
                                localAllocHandle2.Dispose();
                            }
                            fixed(byte *numPtr = &encodedData[0])
                            {
                                if (!CAPI.CryptMsgControl(cryptMsgHandle, 0U, 8U, new IntPtr((void *)&new CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA)))
                                {
                                    dwSignerIndex = (uint)parentIndex,
                                    blob =
                                    {
                                        cbData = (uint)encodedData.Length,
                                        pbData = new IntPtr((void *)numPtr)
                                    }
                                })))
                                {
                                    throw new CryptographicException(Marshal.GetLastWin32Error());
                                }
                            }
                            localAllocHandle1.Dispose();
                            return;
                        }
                        catch (CryptographicException ex)
                        {
                            byte[] encodedData;
                            if (CAPI.EncodeObject(new IntPtr(22L), num1, out encodedData))
                            {
                                fixed(byte *numPtr = &encodedData[0])
                                CAPI.CryptMsgControl(cryptMsgHandle, 0U, 8U, new IntPtr((void *)&new CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA(Marshal.SizeOf(typeof(CAPI.CMSG_CTRL_ADD_SIGNER_UNAUTH_ATTR_PARA)))
                                {
                                    dwSignerIndex = (uint)parentIndex,
                                    blob          =
                                    {
                                        cbData = (uint)encodedData.Length,
                                        pbData = new IntPtr((void *)numPtr)
                                    }
                                }));
                            }
                            throw;
                        }
                    }
                    else
                    {
                        childIndex -= (int)cryptAttribute1.cValue;
                    }
                }
                num1 = new IntPtr((long)num1 + (long)Marshal.SizeOf(typeof(CAPI.CRYPT_ATTRIBUTE)));
            }
            if (pvData1 != null && !pvData1.IsInvalid)
            {
                pvData1.Dispose();
            }
            if (pvData2 != null && !pvData2.IsInvalid)
            {
                pvData2.Dispose();
            }
            throw new CryptographicException(-2146885618);
        }