Beispiel #1
0
        public void TestMultipartEncryptedSignAndEncryptAlgorithm()
        {
            var body = new TextPart("plain")
            {
                Text = "This is some cleartext that we'll end up signing and encrypting..."
            };
            var self = new SecureMailboxAddress("MimeKit UnitTests", "*****@*****.**", "AB0821A2");
            DigitalSignatureCollection signatures;

            var encrypted = MultipartEncrypted.SignAndEncrypt(self, DigestAlgorithm.Sha1, EncryptionAlgorithm.Cast5, new [] { self }, body);

            //using (var file = File.Create ("pgp-signed-encrypted.asc"))
            //	encrypted.WriteTo (file);

            var decrypted = encrypted.Decrypt(out signatures);

            Assert.IsInstanceOf <TextPart> (decrypted, "Decrypted part is not the expected type.");
            Assert.AreEqual(body.Text, ((TextPart)decrypted).Text, "Decrypted content is not the same as the original.");

            Assert.AreEqual(1, signatures.Count, "Verify returned an unexpected number of signatures.");
            foreach (var signature in signatures)
            {
                try {
                    bool valid = signature.Verify();

                    Assert.IsTrue(valid, "Bad signature from {0}", signature.SignerCertificate.Email);
                } catch (DigitalSignatureVerifyException ex) {
                    Assert.Fail("Failed to verify signature: {0}", ex);
                }
            }
        }
Beispiel #2
0
        public void TestMimeMessageEncrypt()
        {
            var body = new TextPart("plain")
            {
                Text = "This is some cleartext that we'll end up encrypting..."
            };
            var self    = new SecureMailboxAddress("MimeKit UnitTests", "*****@*****.**", "44CD48EEC90D8849961F36BA50DCD107AB0821A2");
            var message = new MimeMessage {
                Subject = "Test of signing with OpenPGP"
            };

            message.From.Add(self);
            message.To.Add(self);
            message.Body = body;

            using (var ctx = new DummyOpenPgpContext()) {
                message.Encrypt(ctx);

                Assert.IsInstanceOf <MultipartEncrypted> (message.Body);

                var encrypted = (MultipartEncrypted)message.Body;

                //using (var file = File.Create ("pgp-encrypted.asc"))
                //	encrypted.WriteTo (file);

                var decrypted = encrypted.Decrypt(ctx);

                Assert.IsInstanceOf <TextPart> (decrypted, "Decrypted part is not the expected type.");
                Assert.AreEqual(body.Text, ((TextPart)decrypted).Text, "Decrypted content is not the same as the original.");
            }
        }
Beispiel #3
0
        public void TestMultipartSignedSignUsingKeys()
        {
            var body = new TextPart("plain")
            {
                Text = "This is some cleartext that we'll end up signing..."
            };
            var          self = new SecureMailboxAddress("MimeKit UnitTests", "*****@*****.**", "44CD48EEC90D8849961F36BA50DCD107AB0821A2");
            PgpSecretKey signer;

            using (var ctx = new DummyOpenPgpContext()) {
                signer = ctx.GetSigningKey(self);

                foreach (DigestAlgorithm digest in Enum.GetValues(typeof(DigestAlgorithm)))
                {
                    if (digest == DigestAlgorithm.None ||
                        digest == DigestAlgorithm.DoubleSha ||
                        digest == DigestAlgorithm.Tiger192 ||
                        digest == DigestAlgorithm.Haval5160 ||
                        digest == DigestAlgorithm.MD4)
                    {
                        continue;
                    }

                    var multipart = MultipartSigned.Create(signer, digest, body);

                    Assert.AreEqual(2, multipart.Count, "The multipart/signed has an unexpected number of children.");

                    var protocol = multipart.ContentType.Parameters["protocol"];
                    Assert.AreEqual("application/pgp-signature", protocol, "The multipart/signed protocol does not match.");

                    var micalg    = multipart.ContentType.Parameters["micalg"];
                    var algorithm = ctx.GetDigestAlgorithm(micalg);

                    Assert.AreEqual(digest, algorithm, "The multipart/signed micalg does not match.");

                    Assert.IsInstanceOf <TextPart> (multipart[0], "The first child is not a text part.");
                    Assert.IsInstanceOf <ApplicationPgpSignature> (multipart[1], "The second child is not a detached signature.");

                    var signatures = multipart.Verify();
                    Assert.AreEqual(1, signatures.Count, "Verify returned an unexpected number of signatures.");
                    foreach (var signature in signatures)
                    {
                        try {
                            bool valid = signature.Verify();

                            Assert.IsTrue(valid, "Bad signature from {0}", signature.SignerCertificate.Email);
                        } catch (DigitalSignatureVerifyException ex) {
                            Assert.Fail("Failed to verify signature: {0}", ex);
                        }
                    }
                }
            }
        }
Beispiel #4
0
        public void TestMimeMessageSignAndEncrypt()
        {
            var body = new TextPart("plain")
            {
                Text = "This is some cleartext that we'll end up signing and encrypting..."
            };
            var self    = new SecureMailboxAddress("MimeKit UnitTests", "*****@*****.**", "AB0821A2");
            var message = new MimeMessage {
                Subject = "Test of signing with OpenPGP"
            };
            DigitalSignatureCollection signatures;

            using (var ctx = new DummyOpenPgpContext()) {
                // throws because no Body has been set
                Assert.Throws <InvalidOperationException> (() => message.SignAndEncrypt(ctx));

                message.Body = body;

                // throws because no sender has been set
                Assert.Throws <InvalidOperationException> (() => message.SignAndEncrypt(ctx));

                message.From.Add(self);
                message.To.Add(self);

                message.SignAndEncrypt(ctx);

                Assert.IsInstanceOf <MultipartEncrypted> (message.Body);

                var encrypted = (MultipartEncrypted)message.Body;

                //using (var file = File.Create ("pgp-signed-encrypted.asc"))
                //	encrypted.WriteTo (file);

                var decrypted = encrypted.Decrypt(ctx, out signatures);

                Assert.IsInstanceOf <TextPart> (decrypted, "Decrypted part is not the expected type.");
                Assert.AreEqual(body.Text, ((TextPart)decrypted).Text, "Decrypted content is not the same as the original.");

                Assert.AreEqual(1, signatures.Count, "Verify returned an unexpected number of signatures.");
                foreach (var signature in signatures)
                {
                    try {
                        bool valid = signature.Verify();

                        Assert.IsTrue(valid, "Bad signature from {0}", signature.SignerCertificate.Email);
                    } catch (DigitalSignatureVerifyException ex) {
                        Assert.Fail("Failed to verify signature: {0}", ex);
                    }
                }
            }
        }
Beispiel #5
0
        public virtual void TestSecureMimeSignAndEncrypt()
        {
            var body = new TextPart("plain")
            {
                Text = "This is some cleartext that we'll end up signing and encrypting..."
            };
            var self    = new SecureMailboxAddress("MimeKit UnitTests", "*****@*****.**", "2c29c66e281c9c515cc16a91ac87c4da988dbadf");
            var message = new MimeMessage {
                Subject = "Test of signing and encrypting with S/MIME"
            };
            ApplicationPkcs7Mime encrypted;

            message.From.Add(self);
            message.To.Add(self);
            message.Body = body;

            using (var ctx = CreateContext()) {
                message.SignAndEncrypt(ctx);

                Assert.IsInstanceOf <ApplicationPkcs7Mime> (message.Body, "The message body should be an application/pkcs7-mime part.");

                encrypted = (ApplicationPkcs7Mime)message.Body;

                Assert.AreEqual(SecureMimeType.EnvelopedData, encrypted.SecureMimeType, "S/MIME type did not match.");
            }

            using (var ctx = CreateContext()) {
                var decrypted = encrypted.Decrypt(ctx);

                // The decrypted part should be a multipart/signed
                Assert.IsInstanceOf <MultipartSigned> (decrypted, "Expected the decrypted part to be a multipart/signed.");
                var signed = (MultipartSigned)decrypted;

                Assert.IsInstanceOf <TextPart> (signed[0], "Expected the first part of the multipart/signed to be a multipart.");
                Assert.IsInstanceOf <ApplicationPkcs7Signature> (signed[1], "Expected second part of the multipart/signed to be a pkcs7-signature.");

                var extracted = (TextPart)signed[0];
                Assert.AreEqual(body.Text, extracted.Text, "The decrypted text part's text does not match the original.");

                var signatures = signed.Verify(ctx);

                Assert.AreEqual(1, signatures.Count, "Verify returned an unexpected number of signatures.");
                foreach (var signature in signatures)
                {
                    try {
                        bool valid = signature.Verify();

                        Assert.IsTrue(valid, "Bad signature from {0}", signature.SignerCertificate.Email);
                    } catch (DigitalSignatureVerifyException ex) {
                        Assert.Fail("Failed to verify signature: {0}", ex);
                    }

                    var algorithms = ((SecureMimeDigitalSignature)signature).EncryptionAlgorithms;
                    int i          = 0;

                    Assert.AreEqual(EncryptionAlgorithm.Aes256, algorithms[i++], "Expected AES-256 capability");
                    Assert.AreEqual(EncryptionAlgorithm.Aes192, algorithms[i++], "Expected AES-192 capability");
                    Assert.AreEqual(EncryptionAlgorithm.Aes128, algorithms[i++], "Expected AES-128 capability");
                    if (ctx.IsEnabled(EncryptionAlgorithm.Idea))
                    {
                        Assert.AreEqual(EncryptionAlgorithm.Idea, algorithms[i++], "Expected IDEA capability");
                    }
                    if (ctx.IsEnabled(EncryptionAlgorithm.Cast5))
                    {
                        Assert.AreEqual(EncryptionAlgorithm.Cast5, algorithms[i++], "Expected Cast5 capability");
                    }
                    if (ctx.IsEnabled(EncryptionAlgorithm.Camellia256))
                    {
                        Assert.AreEqual(EncryptionAlgorithm.Camellia256, algorithms[i++], "Expected Camellia-256 capability");
                    }
                    if (ctx.IsEnabled(EncryptionAlgorithm.Camellia192))
                    {
                        Assert.AreEqual(EncryptionAlgorithm.Camellia192, algorithms[i++], "Expected Camellia-192 capability");
                    }
                    if (ctx.IsEnabled(EncryptionAlgorithm.Camellia128))
                    {
                        Assert.AreEqual(EncryptionAlgorithm.Camellia128, algorithms[i++], "Expected Camellia-128 capability");
                    }
                    Assert.AreEqual(EncryptionAlgorithm.TripleDes, algorithms[i++], "Expected Triple-DES capability");
                    //Assert.AreEqual (EncryptionAlgorithm.RC2128, algorithms[i++], "Expected RC2-128 capability");
                    //Assert.AreEqual (EncryptionAlgorithm.RC264, algorithms[i++], "Expected RC2-64 capability");
                    //Assert.AreEqual (EncryptionAlgorithm.Des, algorithms[i++], "Expected DES capability");
                    //Assert.AreEqual (EncryptionAlgorithm.RC240, algorithms[i++], "Expected RC2-40 capability");
                }
            }
        }