summary refs log tree commit diff
path: root/crypto/src/openpgp/PgpEncryptedDataList.cs
blob: 33e7eee2ce754784c3d55ec0c6864d6c03d12e2e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
using System;
using System.Collections;
using System.IO;

using Org.BouncyCastle.Utilities;
using Org.BouncyCastle.Utilities.Collections;

namespace Org.BouncyCastle.Bcpg.OpenPgp
{
	/// <remarks>A holder for a list of PGP encryption method packets.</remarks>
    public class PgpEncryptedDataList
		: PgpObject
    {
        private readonly IList list = Platform.CreateArrayList();
        private readonly InputStreamPacket data;

        public PgpEncryptedDataList(
            BcpgInputStream bcpgInput)
        {
            while (bcpgInput.NextPacketTag() == PacketTag.PublicKeyEncryptedSession
                || bcpgInput.NextPacketTag() == PacketTag.SymmetricKeyEncryptedSessionKey)
            {
                list.Add(bcpgInput.ReadPacket());
            }

            Packet packet = bcpgInput.ReadPacket();
            if (!(packet is InputStreamPacket))
                throw new IOException("unexpected packet in stream: " + packet);

            this.data = (InputStreamPacket)packet;

            for (int i = 0; i != list.Count; i++)
            {
                if (list[i] is SymmetricKeyEncSessionPacket)
                {
                    list[i] = new PgpPbeEncryptedData((SymmetricKeyEncSessionPacket) list[i], data);
                }
                else
                {
                    list[i] = new PgpPublicKeyEncryptedData((PublicKeyEncSessionPacket) list[i], data);
                }
            }
        }

		public PgpEncryptedData this[int index]
		{
			get { return (PgpEncryptedData) list[index]; }
		}

		public int Count
		{
			get { return list.Count; }
		}

		public bool IsEmpty
        {
			get { return list.Count == 0; }
        }

		public IEnumerable GetEncryptedDataObjects()
        {
            return new EnumerableProxy(list);
        }
    }
}