示例#1
0
        private void ParseEnveloped(
            byte[] data)
        {
            Asn1StreamParser aIn = new Asn1StreamParser(data);

            ContentInfoParser cP = new ContentInfoParser((Asn1SequenceParser)aIn.ReadObject());

            EnvelopedDataParser eP = new EnvelopedDataParser((Asn1SequenceParser)cP.GetContent(Asn1Tags.Sequence));

            eP.GetRecipientInfos().ToAsn1Object();             // Must drain the parser!

            EncryptedContentInfoParser ecP = eP.GetEncryptedContentInfo();

            Asn1OctetStringParser content = (Asn1OctetStringParser)ecP.GetEncryptedContent(Asn1Tags.OctetString);

            Streams.Drain(content.GetOctetStream());
        }
        /// <summary>
        /// Return the MAC calculated for the recipient. Note: this call is only meaningful once all
        /// the content has been read.
        /// </summary>
        /// <returns>byte array containing the mac.</returns>
        public byte[] GetMac()
        {
            if (resultMac == null)
            {
                if (op.IsMacBased)
                {
                    if (additionalData != null)
                    {
                        try
                        {
                            Streams.Drain(op.GetStream(new MemoryInputStream(additionalData.AuthAttributes.GetEncoded(Asn1Encodable.Der))));
                        }
                        catch (IOException e)
                        {
                            throw new InvalidOperationException("unable to drain input: " + e.Message, e);
                        }
                    }
                    resultMac = op.GetMac();
                }
            }

            return(resultMac);
        }
 public void Drain()
 {
     Streams.Drain(_in);
     _in.Close();
 }
 public void Drain()
 {
     Streams.Drain(_in);
     _in.Dispose();
 }
示例#5
0
        private ITestResult EnvelopedTest()
        {
            try
            {
                // Key trans
                ContentInfo info = ContentInfo.GetInstance(
                    Asn1Object.FromByteArray(envDataKeyTrns));
                EnvelopedData envData = EnvelopedData.GetInstance(info.Content);
                Asn1Set       s       = envData.RecipientInfos;

                if (s.Count != 1)
                {
                    return(new SimpleTestResult(false, Name + ": CMS KeyTrans enveloped, wrong number of recipients"));
                }

                RecipientInfo recip = RecipientInfo.GetInstance(s[0]);

                if (recip.Info is KeyTransRecipientInfo)
                {
                    KeyTransRecipientInfo inf = KeyTransRecipientInfo.GetInstance(recip.Info);

                    inf = new KeyTransRecipientInfo(inf.RecipientIdentifier, inf.KeyEncryptionAlgorithm, inf.EncryptedKey);

                    s = new DerSet(new RecipientInfo(inf));
                }
                else
                {
                    return(new SimpleTestResult(false, Name + ": CMS KeyTrans enveloped, wrong recipient type"));
                }

                envData = new EnvelopedData(envData.OriginatorInfo, s, envData.EncryptedContentInfo, envData.UnprotectedAttrs);
                info    = new ContentInfo(CmsObjectIdentifiers.EnvelopedData, envData);

                if (!Arrays.AreEqual(info.GetEncoded(), envDataKeyTrns))
                {
                    return(new SimpleTestResult(false, Name + ": CMS KeyTrans enveloped failed to re-encode"));
                }


                // KEK
                info = ContentInfo.GetInstance(
                    Asn1Object.FromByteArray(envDataKEK));
                envData = EnvelopedData.GetInstance(info.Content);
                s       = envData.RecipientInfos;

                if (s.Count != 1)
                {
                    return(new SimpleTestResult(false, Name + ": CMS KEK enveloped, wrong number of recipients"));
                }

                recip = RecipientInfo.GetInstance(s[0]);

                if (recip.Info is KekRecipientInfo)
                {
                    KekRecipientInfo inf = KekRecipientInfo.GetInstance(recip.Info);

                    inf = new KekRecipientInfo(inf.KekID, inf.KeyEncryptionAlgorithm, inf.EncryptedKey);

                    s = new DerSet(new RecipientInfo(inf));
                }
                else
                {
                    return(new SimpleTestResult(false, Name + ": CMS KEK enveloped, wrong recipient type"));
                }

                envData = new EnvelopedData(envData.OriginatorInfo, s, envData.EncryptedContentInfo, envData.UnprotectedAttrs);
                info    = new ContentInfo(CmsObjectIdentifiers.EnvelopedData, envData);

                if (!Arrays.AreEqual(info.GetEncoded(), envDataKEK))
                {
                    return(new SimpleTestResult(false, Name + ": CMS KEK enveloped failed to re-encode"));
                }

                // Nested NDEF problem
                Asn1StreamParser    asn1In = new Asn1StreamParser(new MemoryStream(envDataNestedNDEF, false));
                ContentInfoParser   ci     = new ContentInfoParser((Asn1SequenceParser)asn1In.ReadObject());
                EnvelopedDataParser ed     = new EnvelopedDataParser((Asn1SequenceParser)ci
                                                                     .GetContent(Asn1Tags.Sequence));
                Touch(ed.Version);
                ed.GetOriginatorInfo();
                ed.GetRecipientInfos().ToAsn1Object();
                EncryptedContentInfoParser eci = ed.GetEncryptedContentInfo();
                Touch(eci.ContentType);
                Touch(eci.ContentEncryptionAlgorithm);

                Stream dataIn = ((Asn1OctetStringParser)eci.GetEncryptedContent(Asn1Tags.OctetString))
                                .GetOctetStream();
                Streams.Drain(dataIn);
                dataIn.Close();

                // Test data doesn't have unprotected attrs, bug was being thrown by this call
                Asn1SetParser upa = ed.GetUnprotectedAttrs();
                if (upa != null)
                {
                    upa.ToAsn1Object();
                }

                return(new SimpleTestResult(true, Name + ": Okay"));
            }
            catch (Exception e)
            {
                return(new SimpleTestResult(false, Name + ": CMS enveloped failed - " + e.ToString(), e));
            }
        }
示例#6
0
 public void Drain()
 {
     Streams.Drain(_in);
     Platform.Dispose(_in);
 }
示例#7
0
 public void Drain()
 {
     Streams.Drain(_in);
     BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.Dispose(_in);
 }
示例#8
0
 public void Drain()
 {
     Streams.Drain(this._in);
     this._in.Close();
 }