Esempio n. 1
0
        public void Remove(CmsRecipient recipient)
        {
            if (recipient == null)
                throw new ArgumentNullException(nameof(recipient));

            _recipients.Remove(recipient);
        }
		public CmsRecipientCollection (SubjectIdentifierType recipientIdentifierType, X509Certificate2Collection certificates) : base () 
		{
			foreach (X509Certificate2 x509 in certificates) {
				CmsRecipient p7r = new CmsRecipient (recipientIdentifierType, x509);
				_list.Add (p7r);
			}
		}
		public void IssuerAndSerialNumber () 
		{
			X509Certificate2 x509 = GetCertificate (true);
			CmsRecipient p7r = new CmsRecipient (SubjectIdentifierType.IssuerAndSerialNumber, x509);
			AssertEquals ("RecipientIdentifierType", SubjectIdentifierType.IssuerAndSerialNumber, p7r.RecipientIdentifierType);
			AssertEquals ("Certificate", x509.Thumbprint, p7r.Certificate.Thumbprint);
		}
Esempio n. 4
0
		public void SubjectKeyIdentifier () 
		{
			X509Certificate2 x509 = GetCertificate (true);
			CmsRecipient p7r = new CmsRecipient (SubjectIdentifierType.SubjectKeyIdentifier, x509);
			Assert.AreEqual (SubjectIdentifierType.SubjectKeyIdentifier, p7r.RecipientIdentifierType, "RecipientIdentifierType");
			Assert.AreEqual (x509.Thumbprint, p7r.Certificate.Thumbprint, "Certificate");
		}
Esempio n. 5
0
		public CmsRecipientCollection (SubjectIdentifierType recipientIdentifierType, X509Certificate2Collection certificates)
		{
			// no null check, MS throws a NullReferenceException here
			foreach (X509Certificate2 x509 in certificates) {
				CmsRecipient p7r = new CmsRecipient (recipientIdentifierType, x509);
				_list.Add (p7r);
			}
		}
 public void Remove(CmsRecipient recipient)
 {
     if (recipient == null)
     {
         throw new ArgumentNullException("recipient");
     }
     this.m_recipients.Remove(recipient);
 }
 public int Add(CmsRecipient recipient)
 {
     if (recipient == null)
     {
         throw new ArgumentNullException("recipient");
     }
     return(this.m_recipients.Add(recipient));
 }
 public void Remove(CmsRecipient recipient)
 {
     if (recipient == null)
     {
         throw new ArgumentNullException("recipient");
     }
     this.m_recipients.Remove(recipient);
 }
 public int Add(CmsRecipient recipient)
 {
     if (recipient == null)
     {
         throw new ArgumentNullException("recipient");
     }
     return this.m_recipients.Add(recipient);
 }
Esempio n. 10
0
        public int Add(CmsRecipient recipient)
        {
            if (recipient == null)
                throw new ArgumentNullException(nameof(recipient));

            int indexOfNewItem = _recipients.Count;
            _recipients.Add(recipient);
            return indexOfNewItem;
        }
Esempio n. 11
0
        public byte[] encrypt(byte[] plainTest)
        {
            envelopedContentInfo = new ContentInfo(plainTest);
            envelopedCms = new EnvelopedCms(envelopedContentInfo);
            envelopeCmsResipient = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, cryptographyClientCert);

            envelopedCms.Encrypt(envelopeCmsResipient);
            return envelopedCms.Encode();
        }
Esempio n. 12
0
        public void Remove(CmsRecipient recipient)
        {
            if (recipient == null)
            {
                throw new ArgumentNullException(nameof(recipient));
            }

            _recipients.Remove(recipient);
        }
Esempio n. 13
0
 public void Encrypt(CmsRecipient recipient)
 {
     if (recipient == null)
     {
         throw new ArgumentNullException("recipient");
     }
     // TODO
     Encrypt();
 }
Esempio n. 14
0
 public static void CmsRecipientPassUnknown()
 {
     using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
     {
         CmsRecipient r = new CmsRecipient(SubjectIdentifierType.Unknown, cert);
         Assert.Equal(SubjectIdentifierType.IssuerAndSerialNumber, r.RecipientIdentifierType);
         Assert.Same(cert, r.Certificate);
     }
 }
 private byte[] EncryptedBytes(byte[] bytes)
 {
     var contentInfo = new ContentInfo(bytes);
     var encryptAlgoOid = new Oid("2.16.840.1.101.3.4.1.42"); // AES-256-CBC            
     var envelopedCms = new EnvelopedCms(contentInfo, new AlgorithmIdentifier(encryptAlgoOid));
     var recipient = new CmsRecipient(CryptographicCertificate);
     envelopedCms.Encrypt(recipient);
     return envelopedCms.Encode();
 }
Esempio n. 16
0
        //
        // Encrypt() overloads. Senders invoke this to encrypt and encode a CMS. Afterward, invoke the Encode() method to retrieve the actual encoding.
        //
        public void Encrypt(CmsRecipient recipient)
        {
            if (recipient == null)
            {
                throw new ArgumentNullException(nameof(recipient));
            }

            Encrypt(new CmsRecipientCollection(recipient));
        }
Esempio n. 17
0
 public CmsRecipientCollection(SubjectIdentifierType recipientIdentifierType, X509Certificate2Collection certificates)
 {
     // no null check, MS throws a NullReferenceException here
     foreach (X509Certificate2 x509 in certificates)
     {
         CmsRecipient p7r = new CmsRecipient(recipientIdentifierType, x509);
         _list.Add(p7r);
     }
 }
        /// <summary>
        /// Encrypts the specified string.
        /// </summary>
        /// <param name="plaintext">The plaintext to be encrypted.</param>
        /// <param name="certificate">The certificate to be used for encryption.</param>
        /// <returns>The encrypted text.</returns>
        public static string Encrypt(this string plaintext, X509Certificate2 certificate)
        {
            var contentInfo = new ContentInfo(Encoding.UTF8.GetBytes(plaintext));
            var envelopedCms = new EnvelopedCms(contentInfo);

            var cmsRecipient = new CmsRecipient(certificate);
            envelopedCms.Encrypt(cmsRecipient);

            return Convert.ToBase64String(envelopedCms.Encode());
        }
Esempio n. 19
0
    private byte[] Envelope(byte[] contentBytes)
    {
        Pkcs.ContentInfo  content   = new Pkcs.ContentInfo(contentBytes);
        Pkcs.EnvelopedCms envMsg    = new Pkcs.EnvelopedCms(content);
        Pkcs.CmsRecipient recipient = new Pkcs.CmsRecipient(Pkcs.SubjectIdentifierType.IssuerAndSerialNumber, _recipientCert);
        envMsg.Encrypt(recipient);
        byte[] encryptedBytes = envMsg.Encode();

        return(encryptedBytes);
    }
Esempio n. 20
0
        public int Add(CmsRecipient recipient)
        {
            if (recipient == null)
            {
                throw new ArgumentNullException(nameof(recipient));
            }

            int indexOfNewItem = _recipients.Count;

            _recipients.Add(recipient);
            return(indexOfNewItem);
        }
 public static void DecodeAlgorithmDes_RoundTrip()
 {
     AlgorithmIdentifier algorithm = new AlgorithmIdentifier(new Oid(Oids.Des));
     ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
     EnvelopedCms ecms = new EnvelopedCms(contentInfo, algorithm);
     using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
     {
         CmsRecipient cmsRecipient = new CmsRecipient(cert);
         ecms.Encrypt(cmsRecipient);
     }
     byte[] encodedMessage = ecms.Encode();
     VerifyAlgorithmDes(encodedMessage);
 }
Esempio n. 22
0
        public static void DecodeCertificates0_RoundTrip()
        {
            ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
            EnvelopedCms ecms = new EnvelopedCms(contentInfo);
            using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
            {
                CmsRecipient cmsRecipient = new CmsRecipient(cert);
                ecms.Encrypt(cmsRecipient);
            }
            byte[] encodedMessage = ecms.Encode();

            VerifyCertificates0(encodedMessage);
        }
Esempio n. 23
0
 public static void ZeroLengthContent_RoundTrip()
 {
     ContentInfo contentInfo = new ContentInfo(Array.Empty<byte>());
     EnvelopedCms ecms = new EnvelopedCms(contentInfo);
     using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
     {
         CmsRecipient cmsRecipient = new CmsRecipient(cert);
         try
         {
             ecms.Encrypt(cmsRecipient);
         }
         catch (CryptographicException e)
         {
             throw new Exception("ecms.Encrypt() threw " + e.Message + ".\nIf you're running on the desktop CLR, this is actually an expected result.");
         }
     }
     byte[] encodedMessage = ecms.Encode();
     ValidateZeroLengthContent(encodedMessage);
 }
Esempio n. 24
0
        public static void ReuseEnvelopeCmsDecodeThenEncode()
        {
            byte[] encodedMessage =
                ("3082010c06092a864886f70d010703a081fe3081fb0201003181c83081c5020100302e301a311830160603550403130f5253"
                + "414b65795472616e7366657231021031d935fb63e8cfab48a0bf7b397b67c0300d06092a864886f70d01010105000481805e"
                + "bb2d08773594be9ec5d30c0707cf339f2b982a4f0797b74d520a0c973d668a9a6ad9d28066ef36e5b5620fef67f4d79ee50c"
                + "25eb999f0c656548347d5676ac4b779f8fce2b87e6388fbe483bb0fcf78ab1f1ff29169600401fded7b2803a0bf96cc160c4"
                + "96726216e986869eed578bda652855c85604a056201538ee56b6c4302b06092a864886f70d010701301406082a864886f70d"
                + "030704083adadf63cd297a86800835edc437e31d0b70").HexToByteArray();

            EnvelopedCms ecms = new EnvelopedCms();
            ecms.Decode(encodedMessage);
            using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
            {
                CmsRecipient cmsRecipient = new CmsRecipient(cert);
                ecms.Encrypt(cmsRecipient);
            }

            encodedMessage = ecms.Encode();
            ecms.Decode(encodedMessage);

            RecipientInfoCollection recipients = ecms.RecipientInfos;
            Assert.Equal(1, recipients.Count);
            RecipientInfo recipientInfo = recipients[0];
            KeyTransRecipientInfo recipient = recipientInfo as KeyTransRecipientInfo;
            Assert.NotNull(recipientInfo);

            SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
            object value = subjectIdentifier.Value;
            Assert.True(value is X509IssuerSerial);
            X509IssuerSerial xis = (X509IssuerSerial)value;
            Assert.Equal("CN=RSAKeyTransfer1", xis.IssuerName);
            Assert.Equal("31D935FB63E8CFAB48A0BF7B397B67C0", xis.SerialNumber);
        }
Esempio n. 25
0
 public CmsRecipientCollection(CmsRecipient recipient)
 {
     _list.Add(recipient);
 }
Esempio n. 26
0
        private byte[] Envelope(byte[] contentBytes)
        {
            Pkcs.ContentInfo content = new Pkcs.ContentInfo(contentBytes);
            Pkcs.EnvelopedCms envMsg = new Pkcs.EnvelopedCms(content);
            Pkcs.CmsRecipient recipient = new Pkcs.CmsRecipient(Pkcs.SubjectIdentifierType.IssuerAndSerialNumber, _recipientCert);
            envMsg.Encrypt(recipient);
            byte[] encryptedBytes = envMsg.Encode();

            return encryptedBytes;
        }
Esempio n. 27
0
        public static void ReuseEnvelopeCmsEncodeThenDecode()
        {
            // Test ability to encrypt, encode and decode all in one EnvelopedCms instance.

            ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
            EnvelopedCms ecms = new EnvelopedCms(contentInfo);
            using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
            {
                CmsRecipient cmsRecipient = new CmsRecipient(cert);
                ecms.Encrypt(cmsRecipient);
            }

            byte[] encodedMessage = ecms.Encode();
            ecms.Decode(encodedMessage);

            RecipientInfoCollection recipients = ecms.RecipientInfos;
            Assert.Equal(1, recipients.Count);
            RecipientInfo recipientInfo = recipients[0];
            KeyTransRecipientInfo recipient = recipientInfo as KeyTransRecipientInfo;
            Assert.NotNull(recipientInfo);

            SubjectIdentifier subjectIdentifier = recipient.RecipientIdentifier;
            object value = subjectIdentifier.Value;
            Assert.True(value is X509IssuerSerial);
            X509IssuerSerial xis = (X509IssuerSerial)value;
            Assert.Equal("CN=RSAKeyTransfer1", xis.IssuerName);
            Assert.Equal("31D935FB63E8CFAB48A0BF7B397B67C0", xis.SerialNumber);
        }
Esempio n. 28
0
		// methods

		public int Add (CmsRecipient recipient) 
		{
			return _list.Add (recipient);
		}
Esempio n. 29
0
		public CmsRecipientCollection (CmsRecipient recipient)
		{
			_list.Add (recipient);
		}
        private static KeyTransRecipientInfo EncodeKeyTransl(SubjectIdentifierType type = SubjectIdentifierType.IssuerAndSerialNumber)
        {
            ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
            EnvelopedCms ecms = new EnvelopedCms(contentInfo);
            using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
            {
                CmsRecipient cmsRecipient = new CmsRecipient(type, cert);
                ecms.Encrypt(cmsRecipient);
            }
            byte[] encodedMessage = ecms.Encode();

            EnvelopedCms ecms2 = new EnvelopedCms();
            ecms2.Decode(encodedMessage);

            RecipientInfoCollection recipients = ecms2.RecipientInfos;
            Assert.Equal(1, recipients.Count);
            RecipientInfo recipientInfo = recipients[0];
            Assert.True(recipientInfo is KeyTransRecipientInfo);
            return (KeyTransRecipientInfo)recipientInfo;
        }
Esempio n. 31
0
		public void CopyTo (CmsRecipient[] array, int index) 
		{
			_list.CopyTo (array, index);
		}
        private static byte[] CreateEcmsWithAttributes(params AsnEncodedData[] attributes)
        {
            ContentInfo contentInfo = new ContentInfo(new byte[] { 1, 2, 3 });
            EnvelopedCms ecms = new EnvelopedCms(contentInfo);

            foreach (AsnEncodedData attribute in attributes)
            {
                ecms.UnprotectedAttributes.Add(attribute);
            }

            using (X509Certificate2 cert = Certificates.RSAKeyTransfer1.GetCertificate())
            {
                CmsRecipient cmsRecipient = new CmsRecipient(cert);
                ecms.Encrypt(cmsRecipient);
            }
            byte[] encodedMessage = ecms.Encode();
            return encodedMessage;
        }
Esempio n. 33
0
        private static void TestSimpleDecrypt_RoundTrip(CertLoader certLoader, ContentInfo contentInfo, string algorithmOidValue, SubjectIdentifierType type)
        {
            // Deep-copy the contentInfo since the real ContentInfo doesn't do this. This defends against a bad implementation changing
            // our "expectedContentInfo" to match what it produces.
            ContentInfo expectedContentInfo = new ContentInfo(new Oid(contentInfo.ContentType), (byte[])(contentInfo.Content.Clone()));

            string certSubjectName;
            byte[] encodedMessage;
            using (X509Certificate2 certificate = certLoader.GetCertificate())
            {
                certSubjectName = certificate.Subject;
                AlgorithmIdentifier alg = new AlgorithmIdentifier(new Oid(algorithmOidValue));
                EnvelopedCms ecms = new EnvelopedCms(contentInfo, alg);
                CmsRecipient cmsRecipient = new CmsRecipient(type, certificate);
                ecms.Encrypt(cmsRecipient);
                encodedMessage = ecms.Encode();
            }

            // We don't pass "certificate" down because it's expected that the certificate used for encrypting doesn't have a private key (part of the purpose of this test is
            // to ensure that you don't need the recipient's private key to encrypt.) The decrypt phase will have to locate the matching cert with the private key.
            VerifySimpleDecrypt(encodedMessage, certLoader, expectedContentInfo);
        }
Esempio n. 34
0
		public void Remove (CmsRecipient recipient) 
		{
			_list.Remove (recipient);
		}
Esempio n. 35
0
        public static byte[] Encrypt(byte[] data, X509Certificate2 encryptingCert)
        {
            ContentInfo plainContent = new ContentInfo(data);

            EnvelopedCms encryptedData = new EnvelopedCms(plainContent);

            CmsRecipient recipient = new CmsRecipient(encryptingCert);

            encryptedData.Encrypt(recipient);

            byte[] encryptedBytes = encryptedData.Encode();

            return encryptedBytes;
        }
Esempio n. 36
0
 public static void CmsRecipientPassNullCertificate()
 {
     object ignore;
     Assert.Throws<ArgumentNullException>(() => ignore = new CmsRecipient(null));
     Assert.Throws<ArgumentNullException>(() => ignore = new CmsRecipient(SubjectIdentifierType.IssuerAndSerialNumber, null));
 }
Esempio n. 37
0
 public void Remove(CmsRecipient recipient)
 {
     _list.Remove(recipient);
 }
Esempio n. 38
0
		public void EncryptCmsRecipientUnknown () 
		{
			ContentInfo ci = new ContentInfo (asnNull);
			EnvelopedCms ep = new EnvelopedCms (SubjectIdentifierType.IssuerAndSerialNumber, ci);

			X509Certificate2 x509 = GetCertificate (false);
			CmsRecipient p7r = new CmsRecipient (SubjectIdentifierType.Unknown, x509);
			ep.Encrypt (p7r);
			byte[] encoded = ep.Encode ();
#if DEBUG			
			FileStream fs = File.OpenWrite ("EncryptCmsRecipientUnknown.der");
			fs.Write (encoded, 0, encoded.Length);
			fs.Close ();
#endif
			RoundTrip (encoded);
		}
Esempio n. 39
0
 public CmsRecipientCollection(CmsRecipient recipient)
 {
     _recipients = new List <CmsRecipient>(1);
     _recipients.Add(recipient);
 }
Esempio n. 40
0
        // methods

        public int Add(CmsRecipient recipient)
        {
            return(_list.Add(recipient));
        }
Esempio n. 41
0
 public CmsRecipientCollection(CmsRecipient recipient)
 {
     m_recipients = new ArrayList(1);
     m_recipients.Add(recipient);
 }
Esempio n. 42
0
 public int Add(CmsRecipient recipient !!)
 {
Esempio n. 43
0
        /// <summary>
        /// Encrypts the message and envelopes it for one recipient.
        /// </summary>
        /// <param name="recipient">An object containing the recipient's certificate with public key.</param>
        /// <example>
        /// <code>
        /// [C#]
        /// 
        /// X509Certificate2 cert = new X509Ceritificate2("C:\\recipient.cer");
        /// 
        /// CmsRecipient recipient = new CmsRecipient(cert);
        /// 
        /// message.SmimeEnvelopeAndEncryptFor(recipient);
        /// </code>
        /// </example>
#if !PocketPC
        public void SmimeEnvelopeAndEncryptFor(CmsRecipient recipient)
        {
            this.SmimeEnvelopeAndEncryptFor(new CmsRecipientCollection(recipient));
        }
        private void _bSendMessage_Click(object sender, EventArgs e)
        {
            this.AddLogEntry("Creating message.");

            // We create the message object
            ActiveUp.Net.Mail.Message message = new ActiveUp.Net.Mail.Message();

            try
            {
                // We assign the sender email
                message.From.Email = this._tbFromEmail.Text;

                // We assign the recipient email
                message.To.Add(this._tbToEmail.Text);

                // We assign the subject
                message.Subject = this._tbSubject.Text;

                // We assign the body text
                message.BodyText.Text = this._tbBodyText.Text;

                // It is required to build the mime part tree before encrypting
                message.BuildMimePartTree();

                // Encrypt the message. You need the recipient(s) certificate(s) (with public key only).
                X509Certificate2 recipientCertificate = new X509Certificate2(_tbRecipientCertificate.Text);

                CmsRecipient recipient = new CmsRecipient(recipientCertificate);

                message.SmimeEnvelopeAndEncryptFor(recipient);

                // We send the email using the specified SMTP server
                this.AddLogEntry("Sending message.");
                           
                message.Send(this._tbSmtpServer.Text);

                this.AddLogEntry("Message sent successfully.");
            }

            catch (SmtpException ex)
            {
                this.AddLogEntry(string.Format("Smtp Error: {0}", ex.Message));
            }

            catch (Exception ex)
            {
                this.AddLogEntry(string.Format("Failed: {0}", ex.Message));
            }
        }