summary refs log tree commit diff
path: root/crypto/src/openpgp/PgpEncryptedDataList.cs
blob: b756147f1444bd1fbc1c4e58f8adfeff90024d47 (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
66
67
68
using System;
using System.Collections.Generic;
using System.IO;

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 List<PgpEncryptedData> list = new List<PgpEncryptedData>();
        private readonly InputStreamPacket data;

        public PgpEncryptedDataList(BcpgInputStream bcpgInput)
        {
            var packets = new List<Packet>();
            while (bcpgInput.NextPacketTag() == PacketTag.PublicKeyEncryptedSession
                || bcpgInput.NextPacketTag() == PacketTag.SymmetricKeyEncryptedSessionKey)
            {
                packets.Add(bcpgInput.ReadPacket());
            }

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

            this.data = inputStreamPacket;

            foreach (var packet in packets)
            {
                if (packet is SymmetricKeyEncSessionPacket symmetricKey)
                {
                    list.Add(new PgpPbeEncryptedData(symmetricKey, data));
                }
                else if (packet is PublicKeyEncSessionPacket publicKey)
                {
                    list.Add(new PgpPublicKeyEncryptedData(publicKey, data));
                }
                else
                {
                    throw new InvalidOperationException();
                }
            }
        }

		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<PgpEncryptedData> GetEncryptedDataObjects()
        {
            return CollectionUtilities.Proxy(list);
        }
    }
}