Esempio n. 1
0
        public static void DecodeRecipients3_RoundTrip()
        {
            ContentInfo            contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
            EnvelopedCms           ecms        = new EnvelopedCms(contentInfo);
            CmsRecipientCollection recipients  = new CmsRecipientCollection();

            foreach (X509Certificate2 cert in s_certs)
            {
                recipients.Add(new CmsRecipient(cert));
            }
            ecms.Encrypt(recipients);
            byte[] encodedMessage = ecms.Encode();

            VerifyRecipients3(encodedMessage);
        }
Esempio n. 2
0
        public void TestArgumentExceptions()
        {
            Assert.Throws <ArgumentNullException> (() => new CmsRecipient((X509Certificate2)null));
            Assert.Throws <ArgumentNullException> (() => new CmsRecipient((X509Certificate)null));
            Assert.Throws <ArgumentNullException> (() => new CmsRecipient((Stream)null));
            Assert.Throws <ArgumentNullException> (() => new CmsRecipient((string)null));

            var recipients = new CmsRecipientCollection();

            Assert.AreEqual(0, recipients.Count);
            Assert.IsFalse(recipients.IsReadOnly);
            Assert.Throws <ArgumentNullException> (() => recipients.Add(null));
            Assert.Throws <ArgumentNullException> (() => recipients.Contains(null));
            Assert.Throws <ArgumentNullException> (() => recipients.CopyTo(null, 0));
            Assert.Throws <ArgumentOutOfRangeException> (() => recipients.CopyTo(new CmsRecipient[1], -1));
            Assert.Throws <ArgumentOutOfRangeException> (() => recipients.CopyTo(new CmsRecipient[1], 2));
            Assert.Throws <ArgumentNullException> (() => recipients.Remove(null));
        }
        public static byte[] Encrypt(byte[] data, params X509Certificate2[] certs)
        {
            var envelopedCms = new EnvelopedCms(new ContentInfo(data));

            if (envelopedCms.ContentEncryptionAlgorithm.Oid.FriendlyName != "aes256")
            {
                // After .NET Core 3 aes256 is the standard ContentEncryptionAlgorithm, before it was 3des.
                throw new Exception("Only aes256 is allowed.");
            }

            var cmsRecipientCollection = new CmsRecipientCollection();

            foreach (var x509Certificate2 in certs)
            {
                cmsRecipientCollection.Add(new CmsRecipient(x509Certificate2, RSAEncryptionPadding.OaepSHA512));
            }

            envelopedCms.Encrypt(cmsRecipientCollection);

            return(envelopedCms.Encode());
        }
Esempio n. 4
0
        public static CmsRecipientCollection DeepCopy(this CmsRecipientCollection recipients)
        {
            CmsRecipientCollection recipientsCopy = new CmsRecipientCollection();

            foreach (CmsRecipient recipient in recipients)
            {
                X509Certificate2 originalCert = recipient.Certificate;
                X509Certificate2 certCopy     = new X509Certificate2(originalCert.Handle);
                CmsRecipient     recipientCopy;

                if (recipient.RSAEncryptionPadding is null)
                {
                    recipientCopy = new CmsRecipient(recipient.RecipientIdentifierType, certCopy);
                }
                else
                {
                    recipientCopy = new CmsRecipient(recipient.RecipientIdentifierType, certCopy, recipient.RSAEncryptionPadding);
                }

                recipientsCopy.Add(recipientCopy);
                GC.KeepAlive(originalCert);
            }
            return(recipientsCopy);
        }
Esempio n. 5
0
        public void TestCollectionAddRemove()
        {
            var path       = Path.Combine("..", "..", "TestData", "smime", "certificate-authority.crt");
            var recipients = new CmsRecipientCollection();
            var recipient  = new CmsRecipient(path);
            var array      = new CmsRecipient[1];

            Assert.IsFalse(recipients.Contains(recipient), "Contains: False");
            Assert.IsFalse(recipients.Remove(recipient), "Remove: False");

            recipients.Add(recipient);

            Assert.AreEqual(1, recipients.Count, "Count");
            Assert.IsTrue(recipients.Contains(recipient), "Contains: True");

            recipients.CopyTo(array, 0);
            Assert.AreEqual(recipient, array[0], "CopyTo");

            Assert.IsTrue(recipients.Remove(recipient), "Remove: True");

            Assert.AreEqual(0, recipients.Count, "Count");

            recipients.Clear();
        }
         static void Main (string[] args)
 		{
 			//  Select a binary file
 			var dialog = new OpenFileDialog {
 				Filter = "All files (*.*)|*.*",
 				InitialDirectory = "./",
 				Title = "Select a text file"
 			};
 			var filename = (dialog.ShowDialog () == DialogResult.OK) ? dialog.FileName : null;
             var certificate2 = new X509Certificate2 ("c:/temp1/cert.pfx", "password");
             MimeEntity body;
 
 			using (var content = new MemoryStream (File.ReadAllBytes (filename)))
 			    var part = new MimePart (MimeTypes.GetMimeType (filename)) {
 				    ContentDisposition = new ContentDisposition (ContentDisposition.Attachment),
 				    ContentTransferEncoding = ContentEncoding.Binary,
 				    FileName = Path.GetFileName (filename),
 				    Content = new MimeContent (content)
 			    };
 
 			    var recipient = new CmsRecipient (certificate2) {
                     EncryptionAlgorithms = new EncryptionAlgorithm[] { EncryptionAlgorithm.TripleDes }
                 };
                 var recipients = new CmsRecipientCollection ();
 			    recipients.Add (recipient);
 
                 var signer = new CmsSigner (certificate2) {
 				    DigestAlgorithm = DigestAlgorithm.Sha256
 			    };
 
 			    using (var ctx = new TemporarySecureMimeContext ()) {
                     using (var stream = File.OpenRead ("c:/temp1/cert.pfx"))
                         ctx.Import (stream, "password");
 				    body = ApplicationPkcs7Mime.SignAndEncrypt (ctx, signer, recipients, part);
                 }
 			}
Esempio n. 7
0
        /// <summary>
        /// Gets the <see cref="CmsRecipient"/>s for the specified <see cref="MimeKit.MailboxAddress"/>es.
        /// </summary>
        /// <returns>The <see cref="CmsRecipient"/>s.</returns>
        /// <param name="mailboxes">The mailboxes.</param>
        /// <exception cref="CertificateNotFoundException">
        /// A certificate for one or more of the specified <paramref name="mailboxes"/> could not be found.
        /// </exception>
        protected CmsRecipientCollection GetCmsRecipients(IEnumerable<MailboxAddress> mailboxes)
        {
            var recipients = new CmsRecipientCollection ();

            foreach (var mailbox in mailboxes)
                recipients.Add (GetCmsRecipient (mailbox));

            return recipients;
        }
Esempio n. 8
0
        public void TestArgumentExceptions()
        {
            var path   = Path.Combine("..", "..", "TestData", "smime", "smime.p12");
            var entity = new TextPart("plain")
            {
                Text = "This is some text..."
            };
            var mailbox    = new MailboxAddress("MimeKit UnitTests", "*****@*****.**");
            var recipients = new CmsRecipientCollection();
            var signer     = new CmsSigner(path, "no.secret");
            var mailboxes  = new [] { mailbox };

            recipients.Add(new CmsRecipient(signer.Certificate));

            using (var ctx = new TemporarySecureMimeContext()) {
                using (var file = File.OpenRead(path))
                    ctx.Import(file, "no.secret");

                // Compress
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Compress(null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Compress(ctx, null));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Compress(null));

                // Encrypt
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt(null, mailboxes, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt(null, recipients, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt(ctx, (IEnumerable <MailboxAddress>)null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt(ctx, (CmsRecipientCollection)null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt(ctx, recipients, null));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt(ctx, mailboxes, null));

                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt((IEnumerable <MailboxAddress>)null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt((CmsRecipientCollection)null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt(recipients, null));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt(mailboxes, null));

                // Sign
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign(null, mailbox, DigestAlgorithm.Sha1, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign(null, signer, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign(ctx, (MailboxAddress)null, DigestAlgorithm.Sha1, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign(ctx, (CmsSigner)null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign(ctx, mailbox, DigestAlgorithm.Sha1, null));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign(ctx, signer, null));

                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign((MailboxAddress)null, DigestAlgorithm.Sha1, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign((CmsSigner)null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign(mailbox, DigestAlgorithm.Sha1, null));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.Sign(signer, null));

                // SignAndEncrypt
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(null, mailbox, DigestAlgorithm.Sha1, mailboxes, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(ctx, null, DigestAlgorithm.Sha1, mailboxes, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(ctx, mailbox, DigestAlgorithm.Sha1, null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(ctx, mailbox, DigestAlgorithm.Sha1, mailboxes, null));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(null, DigestAlgorithm.Sha1, mailboxes, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(mailbox, DigestAlgorithm.Sha1, null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(mailbox, DigestAlgorithm.Sha1, mailboxes, null));

                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(null, signer, recipients, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(ctx, null, recipients, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(ctx, signer, null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(ctx, signer, recipients, null));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(null, recipients, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(signer, null, entity));
                Assert.Throws <ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt(signer, recipients, null));

                var compressed = ApplicationPkcs7Mime.Compress(ctx, entity);
                var encrypted  = ApplicationPkcs7Mime.Encrypt(recipients, entity);
                var signed     = ApplicationPkcs7Mime.Sign(signer, entity);

                // Decompress
                Assert.Throws <ArgumentNullException> (() => compressed.Decompress(null));
                Assert.Throws <InvalidOperationException> (() => encrypted.Decompress(ctx));
                Assert.Throws <InvalidOperationException> (() => signed.Decompress(ctx));

                // Decrypt
                Assert.Throws <ArgumentNullException> (() => encrypted.Decrypt(null));
                Assert.Throws <InvalidOperationException> (() => compressed.Decrypt(ctx));
                Assert.Throws <InvalidOperationException> (() => signed.Decrypt(ctx));

                // Verify
                Assert.Throws <ArgumentNullException> (() => {
                    MimeEntity mime;

                    signed.Verify(null, out mime);
                });
                Assert.Throws <InvalidOperationException> (() => {
                    MimeEntity mime;

                    compressed.Verify(ctx, out mime);
                });
                Assert.Throws <InvalidOperationException> (() => {
                    MimeEntity mime;

                    encrypted.Verify(ctx, out mime);
                });
            }
        }
        public static void AddNegative()
        {
            CmsRecipientCollection c = new CmsRecipientCollection();

            Assert.Throws <ArgumentNullException>(() => c.Add(null));
        }
		public void TestArgumentExceptions ()
		{
			var path = Path.Combine ("..", "..", "TestData", "smime", "smime.p12");
			var entity = new TextPart ("plain") { Text = "This is some text..." };
			var mailbox = new MailboxAddress ("MimeKit UnitTests", "*****@*****.**");
			var recipients = new CmsRecipientCollection ();
			var signer = new CmsSigner (path, "no.secret");
			var mailboxes = new [] { mailbox };

			recipients.Add (new CmsRecipient (signer.Certificate));

			using (var ctx = new TemporarySecureMimeContext ()) {
				using (var file = File.OpenRead (path))
					ctx.Import (file, "no.secret");

				// Compress
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Compress (null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Compress (ctx, null));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Compress (null));

				// Encrypt
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt (null, mailboxes, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt (null, recipients, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt (ctx, (IEnumerable<MailboxAddress>) null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt (ctx, (CmsRecipientCollection) null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt (ctx, recipients, null));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt (ctx, mailboxes, null));

				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt ((IEnumerable<MailboxAddress>) null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt ((CmsRecipientCollection) null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt (recipients, null));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Encrypt (mailboxes, null));

				// Sign
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign (null, mailbox, DigestAlgorithm.Sha1, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign (null, signer, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign (ctx, (MailboxAddress) null, DigestAlgorithm.Sha1, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign (ctx, (CmsSigner) null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign (ctx, mailbox, DigestAlgorithm.Sha1, null));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign (ctx, signer, null));

				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign ((MailboxAddress) null, DigestAlgorithm.Sha1, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign ((CmsSigner) null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign (mailbox, DigestAlgorithm.Sha1, null));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.Sign (signer, null));

				// SignAndEncrypt
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (null, mailbox, DigestAlgorithm.Sha1, mailboxes, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (ctx, null, DigestAlgorithm.Sha1, mailboxes, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (ctx, mailbox, DigestAlgorithm.Sha1, null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (ctx, mailbox, DigestAlgorithm.Sha1, mailboxes, null));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (null, DigestAlgorithm.Sha1, mailboxes, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (mailbox, DigestAlgorithm.Sha1, null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (mailbox, DigestAlgorithm.Sha1, mailboxes, null));

				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (null, signer, recipients, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (ctx, null, recipients, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (ctx, signer, null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (ctx, signer, recipients, null));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (null, recipients, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (signer, null, entity));
				Assert.Throws<ArgumentNullException> (() => ApplicationPkcs7Mime.SignAndEncrypt (signer, recipients, null));

				var compressed = ApplicationPkcs7Mime.Compress (ctx, entity);
				var encrypted = ApplicationPkcs7Mime.Encrypt (recipients, entity);
				var signed = ApplicationPkcs7Mime.Sign (signer, entity);

				// Decompress
				Assert.Throws<ArgumentNullException> (() => compressed.Decompress (null));
				Assert.Throws<InvalidOperationException> (() => encrypted.Decompress (ctx));
				Assert.Throws<InvalidOperationException> (() => signed.Decompress (ctx));

				// Decrypt
				Assert.Throws<ArgumentNullException> (() => encrypted.Decrypt (null));
				Assert.Throws<InvalidOperationException> (() => compressed.Decrypt (ctx));
				Assert.Throws<InvalidOperationException> (() => signed.Decrypt (ctx));

				// Verify
				Assert.Throws<ArgumentNullException> (() => {
					MimeEntity mime;

					signed.Verify (null, out mime);
				});
				Assert.Throws<InvalidOperationException> (() => {
					MimeEntity mime;

					compressed.Verify (ctx, out mime);
				});
				Assert.Throws<InvalidOperationException> (() => {
					MimeEntity mime;

					encrypted.Verify (ctx, out mime);
				});
			}
		}