summary refs log tree commit diff
path: root/crypto/src/bcpg/SignatureSubpacket.cs
blob: 12c74bd6e4050508c63ef922e4cf852dcef891d9 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.IO;

using Org.BouncyCastle.Crypto.Utilities;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Bcpg
{
	/// <remarks>Basic type for a PGP Signature sub-packet.</remarks>
    public class SignatureSubpacket
    {
        private readonly SignatureSubpacketTag type;
        private readonly bool critical;
        private readonly bool isLongLength;
		internal byte[] data;

		protected internal SignatureSubpacket(
            SignatureSubpacketTag	type,
            bool					critical,
            bool                    isLongLength,
            byte[]					data)
        {
            this.type = type;
            this.critical = critical;
            this.isLongLength = isLongLength;
            this.data = data;
        }

        public SignatureSubpacketTag SubpacketType
        {
			get { return type; }
        }

        public bool IsCritical()
        {
            return critical;
        }

        public bool IsLongLength()
        {
            return isLongLength;
        }

        /// <summary>Return the generic data making up the packet.</summary>
        public byte[] GetData()
        {
            return (byte[])data.Clone();
        }

		public void Encode(
            Stream os)
        {
            int bodyLen = data.Length + 1;

            if (isLongLength || bodyLen > 8383)
            {
#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER
                Span<byte> buf = stackalloc byte[5];
                buf[0] = 0xFF;
                Pack.UInt32_To_BE((uint)bodyLen, buf, 1);
                os.Write(buf);
#else
                os.WriteByte(0xff);
                os.WriteByte((byte)(bodyLen >> 24));
                os.WriteByte((byte)(bodyLen >> 16));
                os.WriteByte((byte)(bodyLen >> 8));
                os.WriteByte((byte)bodyLen);
#endif
            }
            else if (bodyLen < 192)
            {
                os.WriteByte((byte)bodyLen);
            }
            else
            {
                bodyLen -= 192;

                os.WriteByte((byte)(((bodyLen >> 8) & 0xff) + 192));
                os.WriteByte((byte)bodyLen);
            }

            if (critical)
            {
                os.WriteByte((byte)(0x80 | (int) type));
            }
            else
            {
                os.WriteByte((byte) type);
            }

            os.Write(data, 0, data.Length);
        }

        public override int GetHashCode()
        {
            return (critical ? 1 : 0) + 7 * (int)type + 49 * Arrays.GetHashCode(data);
        }

        public override bool Equals(object obj)
        {
            if (obj == this)
                return true;

            if (!(obj is SignatureSubpacket other))
                return false;

            return this.type == other.type
                && this.critical == other.critical
                && Arrays.AreEqual(this.data, other.data);
        }
    }
}