summary refs log tree commit diff
path: root/crypto/src/openpgp/PGPKeyRing.cs
blob: 52db80209b333702d1fddb73c9faf4a568937f8e (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
using System;
using System.Collections.Generic;
using System.IO;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Bcpg.OpenPgp
{
	public abstract class PgpKeyRing
		: PgpObject
	{
		internal PgpKeyRing()
		{
		}

        internal static TrustPacket ReadOptionalTrustPacket(BcpgInputStream pIn)
		{
            PacketTag tag = pIn.SkipMarkerPackets();

            return tag == PacketTag.Trust ? (TrustPacket)pIn.ReadPacket() : null;
		}

		internal static IList<PgpSignature> ReadSignaturesAndTrust(BcpgInputStream pIn)
		{
            try
            {
				var sigList = new List<PgpSignature>();

				while (pIn.SkipMarkerPackets() == PacketTag.Signature)
				{
					SignaturePacket signaturePacket = (SignaturePacket)pIn.ReadPacket();
					TrustPacket trustPacket = ReadOptionalTrustPacket(pIn);

					sigList.Add(new PgpSignature(signaturePacket, trustPacket));
				}

				return sigList;
			}
			catch (PgpException e)
			{
				throw new IOException("can't create signature object: " + e.Message, e);
			}
		}

		internal static void ReadUserIDs(BcpgInputStream pIn, out IList<object> ids, out IList<TrustPacket> idTrusts,
			out IList<IList<PgpSignature>> idSigs)
		{
			ids = new List<object>();
			idTrusts = new List<TrustPacket>();
			idSigs = new List<IList<PgpSignature>>();

            while (IsUserTag(pIn.SkipMarkerPackets()))
			{
				Packet obj = pIn.ReadPacket();
				if (obj is UserIdPacket)
				{
					UserIdPacket id = (UserIdPacket)obj;
					ids.Add(id.GetId());
				}
				else
				{
					UserAttributePacket user = (UserAttributePacket)obj;
					ids.Add(new PgpUserAttributeSubpacketVector(user.GetSubpackets()));
				}

				idTrusts.Add(ReadOptionalTrustPacket(pIn));
				idSigs.Add(ReadSignaturesAndTrust(pIn));
			}
		}

        private static bool IsUserTag(PacketTag tag)
        {
            switch (tag)
            {
                case PacketTag.UserAttribute:
                case PacketTag.UserId:
                    return true;
                default:
                    return false;
            }
        }
	}
}