public GetEnumerator ( ) : System.Security.Cryptography.Pkcs.CmsRecipientEnumerator | ||
return | System.Security.Cryptography.Pkcs.CmsRecipientEnumerator |
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); }
private static void AssertEquals(CmsRecipientCollection c, IList<CmsRecipient> expected) { Assert.Equal(expected.Count, c.Count); for (int i = 0; i < c.Count; i++) { Assert.Equal(expected[i], c[i]); } int index = 0; foreach (CmsRecipient a in c) { Assert.Equal(expected[index++], a); } Assert.Equal(c.Count, index); ValidateEnumerator(c.GetEnumerator(), expected); ValidateEnumerator(((ICollection)c).GetEnumerator(), expected); { CmsRecipient[] dumped = new CmsRecipient[c.Count + 3]; c.CopyTo(dumped, 2); Assert.Equal(null, dumped[0]); Assert.Equal(null, dumped[1]); Assert.Equal(null, dumped[dumped.Length - 1]); Assert.Equal<CmsRecipient>(expected, dumped.Skip(2).Take(c.Count)); } { CmsRecipient[] dumped = new CmsRecipient[c.Count + 3]; ((ICollection)c).CopyTo(dumped, 2); Assert.Equal(null, dumped[0]); Assert.Equal(null, dumped[1]); Assert.Equal(null, dumped[dumped.Length - 1]); Assert.Equal<CmsRecipient>(expected, dumped.Skip(2).Take(c.Count)); } }