summary refs log tree commit diff
path: root/crypto/src/openpgp/PgpEncryptedDataList.cs
blob: c768c804ab45d2f9d2b3a9bd3c7b36aca7cd3b3a (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
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> m_list = new List<PgpEncryptedData>();
        private readonly InputStreamPacket m_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);

            m_data = inputStreamPacket;

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

		public PgpEncryptedData this[int index] => m_list[index];

		public int Count => m_list.Count;

        public bool IsEmpty => m_list.Count == 0;

		public IEnumerable<PgpEncryptedData> GetEncryptedDataObjects()
        {
            return CollectionUtilities.Proxy(m_list);
        }
    }
}