protected override void Dispose(bool disposing)
            {
                if (disposing)
                {
                    _out.Dispose();

                    // TODO Parent context(s) should really be be closed explicitly

                    _eiGen.Close();

                    if (_outer.unprotectedAttributeGenerator != null)
                    {
                        Asn1.Cms.AttributeTable attrTable = _outer.unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());

                        Asn1Set unprotectedAttrs = new BerSet(attrTable.ToAsn1EncodableVector());

                        _envGen.AddObject(new DerTaggedObject(false, 1, unprotectedAttrs));
                    }

                    _envGen.Close();
                    _cGen.Close();
                }

                base.Dispose(disposing);
            }
        private CmsEnvelopedData doGenerate(
            ICmsTypedData content,
            ICipherBuilderWithKey<AlgorithmIdentifier> contentEncryptor)
        {
            Asn1EncodableVector recipientInfos = new Asn1EncodableVector();
            AlgorithmIdentifier encAlgId;
            Asn1OctetString encContent;

            MemoryOutputStream bOut = new MemoryOutputStream();

            try
            {
                ICipher cOut = contentEncryptor.BuildCipher(bOut);

                content.Write(cOut.Stream);

                cOut.Stream.Close();
            }
            catch (IOException e)
            {
                throw new CmsException(e.Message, e);
            }

            byte[] encryptedContent = bOut.ToArray();

            encAlgId = contentEncryptor.AlgorithmDetails;

            encContent = new BerOctetString(encryptedContent);

            ISymmetricKey encKey = contentEncryptor.Key;

            for (IEnumerator<IRecipientInfoGenerator> it = recipientInfoGenerators.GetEnumerator(); it.MoveNext();)
            {
                IRecipientInfoGenerator recipient = (IRecipientInfoGenerator)it.Current;

                recipientInfos.Add(recipient.Generate(encKey));
            }

            EncryptedContentInfo eci = new EncryptedContentInfo(
                            content.ContentType,
                            encAlgId,
                            encContent);

            Asn1Set unprotectedAttrSet = null;
            if (unprotectedAttributeGenerator != null)
            {
                Asn1.Cms.AttributeTable attrTable = unprotectedAttributeGenerator.GetAttributes(new Dictionary<string, object>());

                unprotectedAttrSet = new BerSet(attrTable.ToAsn1EncodableVector());
            }

            ContentInfo contentInfo = new ContentInfo(
                    CmsObjectIdentifiers.EnvelopedData,
                    new EnvelopedData(originatorInfo, new DerSet(recipientInfos), eci, unprotectedAttrSet));

            return new CmsEnvelopedData(contentInfo);
        }
 public override void Close()
 {
     Platform.Dispose((Stream)(object)_out);
     _eiGen.Close();
     if (_outer.unprotectedAttributeGenerator != null)
     {
         Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = _outer.unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());
         Asn1Set obj = new BerSet(attributes.ToAsn1EncodableVector());
         _envGen.AddObject(new DerTaggedObject(explicitly: false, 1, obj));
     }
     _envGen.Close();
     _cGen.Close();
     base.Close();
 }
 public override void Close()
 {
     this._out.Close();
     this._eiGen.Close();
     if (this._outer.unprotectedAttributeGenerator != null)
     {
         Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = this._outer.unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());
         Asn1Set obj = new BerSet(attributes.ToAsn1EncodableVector());
         this._envGen.AddObject(new DerTaggedObject(false, 1, obj));
     }
     this._envGen.Close();
     this._cGen.Close();
     base.Close();
 }
        private CmsEncryptedData doGenerate(
            ICmsTypedData content,
            ICipherBuilder <AlgorithmIdentifier> contentEncryptor)
        {
            AlgorithmIdentifier encAlgId;
            Asn1OctetString     encContent;

            MemoryOutputStream bOut = new MemoryOutputStream();

            try
            {
                ICipher cipher = contentEncryptor.BuildCipher(bOut);

                content.Write(cipher.Stream);

                cipher.Stream.Close();
            }
            catch (IOException)
            {
                throw new CmsException("");
            }

            byte[] encryptedContent = bOut.ToArray();

            encAlgId = contentEncryptor.AlgorithmDetails;

            encContent = new BerOctetString(encryptedContent);

            EncryptedContentInfo eci = new EncryptedContentInfo(
                content.ContentType,
                encAlgId,
                encContent);

            Asn1Set unprotectedAttrSet = null;

            if (unprotectedAttributeGenerator != null)
            {
                Asn1.Cms.AttributeTable attrTable = unprotectedAttributeGenerator.GetAttributes(new Dictionary <string, object>());

                unprotectedAttrSet = new BerSet(attrTable.ToAsn1EncodableVector());
            }

            ContentInfo contentInfo = new ContentInfo(
                CmsObjectIdentifiers.EncryptedData,
                new EncryptedData(eci, unprotectedAttrSet));

            return(new CmsEncryptedData(contentInfo));
        }
            public override void Close()
            {
                BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(_out);

                // TODO Parent context(s) should really be closed explicitly

                _eiGen.Close();

                if (_outer.unprotectedAttributeGenerator != null)
                {
                    Asn1.Cms.AttributeTable attrTable = _outer.unprotectedAttributeGenerator.GetAttributes(BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateHashtable());

                    Asn1Set unprotectedAttrs = new BerSet(attrTable.ToAsn1EncodableVector());

                    _envGen.AddObject(new DerTaggedObject(false, 1, unprotectedAttrs));
                }

                _envGen.Close();
                _cGen.Close();
                base.Close();
            }
        private static AttributeTable GenerateSignedAttributesTable(byte[] codeDirectoryBytes, byte[] alternativeCodeDirectory1Bytes)
        {
            DerObjectIdentifier hashesAttributeIdentifier = new DerObjectIdentifier("1.2.840.113635.100.9.2");
            DerObjectIdentifier sha256Identifier          = new DerObjectIdentifier("2.16.840.1.101.3.4.2.1");
            DerObjectIdentifier sha1Identifier            = new DerObjectIdentifier("1.3.14.3.2.26");
            DerObjectIdentifier hashesPlistIdentifier     = new DerObjectIdentifier("1.2.840.113635.100.9.1");

            byte[] codeDirectory1Sha256Hash = new SHA256Managed().ComputeHash(alternativeCodeDirectory1Bytes);
            byte[] codeDirectorySha1Hash    = new SHA1Managed().ComputeHash(codeDirectoryBytes);

            BerSequence sha256Sequence       = new BerSequence(sha256Identifier, new DerOctetString(codeDirectory1Sha256Hash));
            BerSequence sha1Sequence         = new BerSequence(sha1Identifier, new DerOctetString(codeDirectorySha1Hash));
            BerSet      codeDirectoryHashSet = new BerSet(new Asn1EncodableVector(sha256Sequence, sha1Sequence));

            byte[] hashesPListBytes = GenerateHashesPList(codeDirectoryBytes, alternativeCodeDirectory1Bytes);
            DerSet hashesPlistSet   = new DerSet(new DerOctetString(hashesPListBytes));

            Asn1EncodableVector signedAttributes = new Asn1EncodableVector();

            signedAttributes.Add(new Org.BouncyCastle.Asn1.Cms.Attribute(hashesAttributeIdentifier, codeDirectoryHashSet));
            signedAttributes.Add(new Org.BouncyCastle.Asn1.Cms.Attribute(hashesPlistIdentifier, hashesPlistSet));
            return(new AttributeTable(signedAttributes));
        }
Example #8
0
        /// <summary>
        /// Generate an enveloped object that contains a CMS Enveloped Data
        /// object using the passed in key generator.
        /// </summary>
        private CmsEnvelopedData Generate(
            CmsProcessable content,
            string encryptionOid,
            CipherKeyGenerator keyGen)
        {
            AlgorithmIdentifier encAlgId = null;
            KeyParameter        encKey;
            Asn1OctetString     encContent;

            try
            {
                byte[] encKeyBytes = keyGen.GenerateKey();
                encKey = ParameterUtilities.CreateKeyParameter(encryptionOid, encKeyBytes);

                Asn1Encodable asn1Params = GenerateAsn1Parameters(encryptionOid, encKeyBytes);

                ICipherParameters cipherParameters;
                encAlgId = GetAlgorithmIdentifier(
                    encryptionOid, encKey, asn1Params, out cipherParameters);

                IBufferedCipher cipher = CipherUtilities.GetCipher(encryptionOid);
                cipher.Init(true, new ParametersWithRandom(cipherParameters, rand));

                MemoryStream bOut = new MemoryStream();
                CipherStream cOut = new CipherStream(bOut, null, cipher);

                content.Write(cOut);

                Platform.Dispose(cOut);

                encContent = new BerOctetString(bOut.ToArray());
            }
            catch (SecurityUtilityException e)
            {
                throw new CmsException("couldn't create cipher.", e);
            }
            catch (InvalidKeyException e)
            {
                throw new CmsException("key invalid in message.", e);
            }
            catch (IOException e)
            {
                throw new CmsException("exception decoding algorithm parameters.", e);
            }


            Asn1EncodableVector recipientInfos = new Asn1EncodableVector();

            foreach (RecipientInfoGenerator rig in recipientInfoGenerators)
            {
                try
                {
                    recipientInfos.Add(rig.Generate(encKey, rand));
                }
                catch (InvalidKeyException e)
                {
                    throw new CmsException("key inappropriate for algorithm.", e);
                }
                catch (GeneralSecurityException e)
                {
                    throw new CmsException("error making encrypted content.", e);
                }
            }

            EncryptedContentInfo eci = new EncryptedContentInfo(
                CmsObjectIdentifiers.Data,
                encAlgId,
                encContent);

            Asn1Set unprotectedAttrSet = null;

            if (unprotectedAttributeGenerator != null)
            {
                Asn1.Cms.AttributeTable attrTable = unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());

                unprotectedAttrSet = new BerSet(attrTable.ToAsn1EncodableVector());
            }

            ContentInfo contentInfo = new ContentInfo(
                CmsObjectIdentifiers.EnvelopedData,
                new EnvelopedData(null, new DerSet(recipientInfos), eci, unprotectedAttrSet));

            return(new CmsEnvelopedData(contentInfo));
        }
Example #9
0
        public override void PerformTest()
        {
            Asn1EncodableVector v = new Asn1EncodableVector();

            byte[] data = new byte[10];

            v.Add(new DerOctetString(data));
            v.Add(new DerBitString(data));
            v.Add(new DerInteger(100));
            v.Add(DerBoolean.True);

            checkSortedSet(0, new DerSet(v));

            v = new Asn1EncodableVector();
            v.Add(new DerInteger(100));
            v.Add(DerBoolean.True);
            v.Add(new DerOctetString(data));
            v.Add(new DerBitString(data));

            checkSortedSet(1, new DerSet(v));

            v = new Asn1EncodableVector();
            v.Add(DerBoolean.True);
            v.Add(new DerOctetString(data));
            v.Add(new DerBitString(data));
            v.Add(new DerInteger(100));


            checkSortedSet(2, new DerSet(v));

            v = new Asn1EncodableVector();
            v.Add(new DerBitString(data));
            v.Add(new DerOctetString(data));
            v.Add(new DerInteger(100));
            v.Add(DerBoolean.True);

            checkSortedSet(3, new DerSet(v));

            v = new Asn1EncodableVector();
            v.Add(new DerOctetString(data));
            v.Add(new DerBitString(data));
            v.Add(new DerInteger(100));
            v.Add(DerBoolean.True);

            Asn1Set s = new BerSet(v);

            if (!(s[0] is DerOctetString))
            {
                Fail("BER set sort order changed.");
            }

            // create an implicitly tagged "set" without sorting
            Asn1TaggedObject tag = new DerTaggedObject(false, 1, new DerSequence(v));

            s = Asn1Set.GetInstance(tag, false);

            if (s[0] is DerBoolean)
            {
                Fail("sorted when shouldn't be.");
            }

            // equality test
            v = new Asn1EncodableVector();

            v.Add(DerBoolean.True);
            v.Add(DerBoolean.True);
            v.Add(DerBoolean.True);

            s = new DerSet(v);
        }
Example #10
0
        private CmsEnvelopedData Generate(CmsProcessable content, string encryptionOid, CipherKeyGenerator keyGen)
        {
            //IL_0045: Unknown result type (might be due to invalid IL or missing references)
            //IL_004c: Expected O, but got Unknown
            //IL_0096: Expected O, but got Unknown
            AlgorithmIdentifier algorithmIdentifier = null;
            KeyParameter        keyParameter;
            Asn1OctetString     encryptedContent;

            try
            {
                byte[] array = keyGen.GenerateKey();
                keyParameter = ParameterUtilities.CreateKeyParameter(encryptionOid, array);
                Asn1Encodable asn1Params = GenerateAsn1Parameters(encryptionOid, array);
                algorithmIdentifier = GetAlgorithmIdentifier(encryptionOid, keyParameter, asn1Params, out var cipherParameters);
                IBufferedCipher cipher = CipherUtilities.GetCipher(encryptionOid);
                cipher.Init(forEncryption: true, new ParametersWithRandom(cipherParameters, rand));
                MemoryStream val          = new MemoryStream();
                CipherStream cipherStream = new CipherStream((Stream)(object)val, null, cipher);
                content.Write((Stream)(object)cipherStream);
                Platform.Dispose((Stream)(object)cipherStream);
                encryptedContent = new BerOctetString(val.ToArray());
            }
            catch (SecurityUtilityException e)
            {
                throw new CmsException("couldn't create cipher.", e);
            }
            catch (InvalidKeyException e2)
            {
                throw new CmsException("key invalid in message.", e2);
            }
            catch (IOException val2)
            {
                IOException e3 = val2;
                throw new CmsException("exception decoding algorithm parameters.", (global::System.Exception)(object) e3);
            }
            Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();

            global::System.Collections.IEnumerator enumerator = ((global::System.Collections.IEnumerable)recipientInfoGenerators).GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    RecipientInfoGenerator recipientInfoGenerator = (RecipientInfoGenerator)enumerator.get_Current();
                    try
                    {
                        asn1EncodableVector.Add(recipientInfoGenerator.Generate(keyParameter, rand));
                    }
                    catch (InvalidKeyException e4)
                    {
                        throw new CmsException("key inappropriate for algorithm.", e4);
                    }
                    catch (GeneralSecurityException e5)
                    {
                        throw new CmsException("error making encrypted content.", e5);
                    }
                }
            }
            finally
            {
                global::System.IDisposable disposable = enumerator as global::System.IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            EncryptedContentInfo encryptedContentInfo = new EncryptedContentInfo(CmsObjectIdentifiers.Data, algorithmIdentifier, encryptedContent);
            Asn1Set unprotectedAttrs = null;

            if (unprotectedAttributeGenerator != null)
            {
                Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());
                unprotectedAttrs = new BerSet(attributes.ToAsn1EncodableVector());
            }
            ContentInfo contentInfo = new ContentInfo(CmsObjectIdentifiers.EnvelopedData, new EnvelopedData(null, new DerSet(asn1EncodableVector), encryptedContentInfo, unprotectedAttrs));

            return(new CmsEnvelopedData(contentInfo));
        }
        public CmsEnvelopedData Generate(CmsProcessable content, ICipherBuilderWithKey cipherBuilder)
        {
            //AlgorithmIdentifier encAlgId = null;
            KeyParameter    encKey;
            Asn1OctetString encContent;

            try
            {
                encKey = (KeyParameter)cipherBuilder.Key;

                MemoryStream collector = new MemoryStream();
                Stream       bOut      = cipherBuilder.BuildCipher(collector).Stream;
                content.Write(bOut);
                Platform.Dispose(bOut);
                encContent = new BerOctetString(collector.ToArray());
            }
            catch (SecurityUtilityException e)
            {
                throw new CmsException("couldn't create cipher.", e);
            }
            catch (InvalidKeyException e)
            {
                throw new CmsException("key invalid in message.", e);
            }
            catch (IOException e)
            {
                throw new CmsException("exception decoding algorithm parameters.", e);
            }


            Asn1EncodableVector recipientInfos = new Asn1EncodableVector();

            foreach (RecipientInfoGenerator rig in recipientInfoGenerators)
            {
                try
                {
                    recipientInfos.Add(rig.Generate(encKey, rand));
                }
                catch (InvalidKeyException e)
                {
                    throw new CmsException("key inappropriate for algorithm.", e);
                }
                catch (GeneralSecurityException e)
                {
                    throw new CmsException("error making encrypted content.", e);
                }
            }

            EncryptedContentInfo eci = new EncryptedContentInfo(
                CmsObjectIdentifiers.Data,
                (AlgorithmIdentifier)cipherBuilder.AlgorithmDetails,
                encContent);

            Asn1Set unprotectedAttrSet = null;

            if (unprotectedAttributeGenerator != null)
            {
                Asn1.Cms.AttributeTable attrTable = unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());

                unprotectedAttrSet = new BerSet(attrTable.ToAsn1EncodableVector());
            }

            ContentInfo contentInfo = new ContentInfo(
                CmsObjectIdentifiers.EnvelopedData,
                new EnvelopedData(null, new DerSet(recipientInfos), eci, unprotectedAttrSet));

            return(new CmsEnvelopedData(contentInfo));
        }
Example #12
0
    private CmsEnvelopedData Generate(CmsProcessable content, string encryptionOid, CipherKeyGenerator keyGen)
    {
        AlgorithmIdentifier algorithmIdentifier = null;
        KeyParameter        keyParameter;
        Asn1OctetString     encryptedContent;

        try
        {
            byte[] array = keyGen.GenerateKey();
            keyParameter = ParameterUtilities.CreateKeyParameter(encryptionOid, array);
            Asn1Encodable asn1Params = GenerateAsn1Parameters(encryptionOid, array);
            algorithmIdentifier = GetAlgorithmIdentifier(encryptionOid, keyParameter, asn1Params, out ICipherParameters cipherParameters);
            IBufferedCipher cipher = CipherUtilities.GetCipher(encryptionOid);
            cipher.Init(forEncryption: true, new ParametersWithRandom(cipherParameters, rand));
            MemoryStream memoryStream = new MemoryStream();
            CipherStream cipherStream = new CipherStream(memoryStream, null, cipher);
            content.Write(cipherStream);
            Platform.Dispose(cipherStream);
            encryptedContent = new BerOctetString(memoryStream.ToArray());
        }
        catch (SecurityUtilityException e)
        {
            throw new CmsException("couldn't create cipher.", e);
        }
        catch (InvalidKeyException e2)
        {
            throw new CmsException("key invalid in message.", e2);
        }
        catch (IOException e3)
        {
            throw new CmsException("exception decoding algorithm parameters.", e3);
        }
        Asn1EncodableVector asn1EncodableVector = new Asn1EncodableVector();

        foreach (RecipientInfoGenerator recipientInfoGenerator in recipientInfoGenerators)
        {
            try
            {
                asn1EncodableVector.Add(recipientInfoGenerator.Generate(keyParameter, rand));
            }
            catch (InvalidKeyException e4)
            {
                throw new CmsException("key inappropriate for algorithm.", e4);
            }
            catch (GeneralSecurityException e5)
            {
                throw new CmsException("error making encrypted content.", e5);
            }
        }
        EncryptedContentInfo encryptedContentInfo = new EncryptedContentInfo(CmsObjectIdentifiers.Data, algorithmIdentifier, encryptedContent);
        Asn1Set unprotectedAttrs = null;

        if (unprotectedAttributeGenerator != null)
        {
            Org.BouncyCastle.Asn1.Cms.AttributeTable attributes = unprotectedAttributeGenerator.GetAttributes(Platform.CreateHashtable());
            unprotectedAttrs = new BerSet(attributes.ToAsn1EncodableVector());
        }
        ContentInfo contentInfo = new ContentInfo(CmsObjectIdentifiers.EnvelopedData, new EnvelopedData(null, new DerSet(asn1EncodableVector), encryptedContentInfo, unprotectedAttrs));

        return(new CmsEnvelopedData(contentInfo));
    }
Example #13
0
		public override void PerformTest()
		{
			Asn1EncodableVector v = new Asn1EncodableVector();
			byte[] data = new byte[10];

			v.Add(new DerOctetString(data));
			v.Add(new DerBitString(data));
			v.Add(new DerInteger(100));
			v.Add(DerBoolean.True);

			checkSortedSet(0, new DerSet(v));

			v = new Asn1EncodableVector();
			v.Add(new DerInteger(100));
			v.Add(DerBoolean.True);
			v.Add(new DerOctetString(data));
			v.Add(new DerBitString(data));

			checkSortedSet(1, new DerSet(v));

			v = new Asn1EncodableVector();
			v.Add(DerBoolean.True);
			v.Add(new DerOctetString(data));
			v.Add(new DerBitString(data));
			v.Add(new DerInteger(100));


			checkSortedSet(2, new DerSet(v));

			v = new Asn1EncodableVector();
			v.Add(new DerBitString(data));
			v.Add(new DerOctetString(data));
			v.Add(new DerInteger(100));
			v.Add(DerBoolean.True);

			checkSortedSet(3, new DerSet(v));

			v = new Asn1EncodableVector();
			v.Add(new DerOctetString(data));
			v.Add(new DerBitString(data));
			v.Add(new DerInteger(100));
			v.Add(DerBoolean.True);

			Asn1Set s = new BerSet(v);

			if (!(s[0] is DerOctetString))
			{
				Fail("BER set sort order changed.");
			}

			// create an implicitly tagged "set" without sorting
			Asn1TaggedObject tag = new DerTaggedObject(false, 1, new DerSequence(v));
			s = Asn1Set.GetInstance(tag, false);

			if (s[0] is DerBoolean)
			{
				Fail("sorted when shouldn't be.");
			}

			// equality test
			v = new Asn1EncodableVector();

			v.Add(DerBoolean.True);
			v.Add(DerBoolean.True);
			v.Add(DerBoolean.True);

			s = new DerSet(v);
		}