private Stream Open(Stream outStream, string encryptionOid, CipherKeyGenerator keyGen)
        {
            byte[]              array        = keyGen.GenerateKey();
            KeyParameter        keyParameter = ParameterUtilities.CreateKeyParameter(encryptionOid, array);
            Asn1Encodable       asn1Params   = GenerateAsn1Parameters(encryptionOid, array);
            ICipherParameters   cipherParameters;
            AlgorithmIdentifier algorithmIdentifier = GetAlgorithmIdentifier(encryptionOid, keyParameter, asn1Params, out cipherParameters);
            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 e)
                    {
                        throw new CmsException("key inappropriate for algorithm.", e);
                    }
                    catch (GeneralSecurityException e2)
                    {
                        throw new CmsException("error making encrypted content.", e2);
                    }
                }
            }
            finally
            {
                global::System.IDisposable disposable = enumerator as global::System.IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            return(Open(outStream, algorithmIdentifier, cipherParameters, asn1EncodableVector));
        }
예제 #2
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));
        }
예제 #3
0
        private CmsAuthenticatedData Generate(CmsProcessable content, string macOid, CipherKeyGenerator keyGen)
        {
            //IL_0039: Unknown result type (might be due to invalid IL or missing references)
            //IL_0040: Expected O, but got Unknown
            //IL_009f: Expected O, but got Unknown
            KeyParameter        keyParameter;
            AlgorithmIdentifier algorithmIdentifier;
            Asn1OctetString     content2;
            Asn1OctetString     mac2;

            try
            {
                byte[] array = keyGen.GenerateKey();
                keyParameter = ParameterUtilities.CreateKeyParameter(macOid, array);
                Asn1Encodable asn1Params = GenerateAsn1Parameters(macOid, array);
                algorithmIdentifier = GetAlgorithmIdentifier(macOid, keyParameter, asn1Params, out var _);
                IMac mac = MacUtilities.GetMac(macOid);
                mac.Init(keyParameter);
                MemoryStream val  = new MemoryStream();
                Stream       val2 = (Stream)(object)new TeeOutputStream((Stream)(object)val, (Stream)(object)new MacOutputStream(mac));
                content.Write(val2);
                Platform.Dispose(val2);
                content2 = new BerOctetString(val.ToArray());
                byte[] str = MacUtilities.DoFinal(mac);
                mac2 = new DerOctetString(str);
            }
            catch (SecurityUtilityException e)
            {
                throw new CmsException("couldn't create cipher.", e);
            }
            catch (InvalidKeyException e2)
            {
                throw new CmsException("key invalid in message.", e2);
            }
            catch (IOException val3)
            {
                IOException e3 = val3;
                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();
                }
            }
            ContentInfo encapsulatedContent = new ContentInfo(CmsObjectIdentifiers.Data, content2);
            ContentInfo contentInfo         = new ContentInfo(CmsObjectIdentifiers.AuthenticatedData, new AuthenticatedData(null, new DerSet(asn1EncodableVector), algorithmIdentifier, null, encapsulatedContent, null, mac2, null));

            return(new CmsAuthenticatedData(contentInfo));
        }