summary refs log tree commit diff
path: root/crypto/src/asn1/icao/LDSSecurityObject.cs
blob: 9fcb4e5b9e164222f3fbfa77a9e403de627fecce (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
113
114
115
using System;

using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Math;

namespace Org.BouncyCastle.Asn1.Icao
{
    /**
	 * The LDSSecurityObject object (V1.8).
	 * <pre>
	 * LDSSecurityObject ::= SEQUENCE {
	 *   version                LDSSecurityObjectVersion,
	 *   hashAlgorithm          DigestAlgorithmIdentifier,
	 *   dataGroupHashValues    SEQUENCE SIZE (2..ub-DataGroups) OF DataHashGroup,
	 *   ldsVersionInfo         LDSVersionInfo OPTIONAL
	 *     -- if present, version MUST be v1 }
	 *
	 * DigestAlgorithmIdentifier ::= AlgorithmIdentifier,
	 *
	 * LDSSecurityObjectVersion :: INTEGER {V0(0)}
	 * </pre>
	 */
    public class LdsSecurityObject
		: Asn1Encodable
	{
		public const int UBDataGroups = 16;

        public static LdsSecurityObject GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is LdsSecurityObject ldsSecurityObject)
                return ldsSecurityObject;
            return new LdsSecurityObject(Asn1Sequence.GetInstance(obj));
        }

        public static LdsSecurityObject GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
        {
            return new LdsSecurityObject(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
        }

		private readonly DerInteger m_version;
        private readonly AlgorithmIdentifier m_hashAlgorithm;
        private readonly DataGroupHash[] m_datagroupHashValues;
        private readonly LdsVersionInfo m_ldsVersionInfo;

        private LdsSecurityObject(Asn1Sequence seq)
		{
            int count = seq.Count, pos = 0;
            if (count < 3 || count > 4)
                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));

			m_version = DerInteger.GetInstance(seq[pos++]);
			m_hashAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]);
			m_datagroupHashValues = ConvertDataGroupHash(Asn1Sequence.GetInstance(seq[pos++]));
			m_ldsVersionInfo = Asn1Utilities.ReadOptional(seq, ref pos, LdsVersionInfo.GetOptional);

            if (pos != count)
                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));

			if (m_ldsVersionInfo != null && !m_version.HasValue(1))
				throw new ArgumentException("'ldsVersionInfo' is present, but 'version' is NOT 'v1'");
        }

        public LdsSecurityObject(AlgorithmIdentifier digestAlgorithmIdentifier, DataGroupHash[] datagroupHash)
        {
            m_version = new DerInteger(0);
			m_hashAlgorithm = digestAlgorithmIdentifier ?? throw new ArgumentNullException(nameof(digestAlgorithmIdentifier)); ;
			m_datagroupHashValues = datagroupHash ?? throw new ArgumentNullException(nameof(datagroupHash));
            m_ldsVersionInfo = null;

			CheckDatagroupHashCount(m_datagroupHashValues.Length);
		}

        public LdsSecurityObject(AlgorithmIdentifier digestAlgorithmIdentifier, DataGroupHash[] datagroupHash,
            LdsVersionInfo versionInfo)
        {
            m_version = new DerInteger(1);
            m_hashAlgorithm = digestAlgorithmIdentifier ?? throw new ArgumentNullException(nameof(digestAlgorithmIdentifier)); ;
            m_datagroupHashValues = datagroupHash ?? throw new ArgumentNullException(nameof(datagroupHash));
            m_ldsVersionInfo = versionInfo;

            CheckDatagroupHashCount(m_datagroupHashValues.Length);
        }

        public BigInteger Version => m_version.Value;

		public AlgorithmIdentifier DigestAlgorithmIdentifier => m_hashAlgorithm;

		public DataGroupHash[] GetDatagroupHash() => m_datagroupHashValues;

		public LdsVersionInfo VersionInfo => m_ldsVersionInfo;

        public override Asn1Object ToAsn1Object()
        {
            Asn1EncodableVector v = new Asn1EncodableVector(4);
            v.Add(m_version, m_hashAlgorithm, DerSequence.FromElements(m_datagroupHashValues));
            v.AddOptional(m_ldsVersionInfo);
            return new DerSequence(v);
        }

        private static void CheckDatagroupHashCount(int count)
        {
            if (count < 2 || count > UBDataGroups)
                throw new ArgumentException("wrong size in DataGroupHashValues : not in (2.." + UBDataGroups + ")");
        }

		private static DataGroupHash[] ConvertDataGroupHash(Asn1Sequence seq)
		{
            CheckDatagroupHashCount(seq.Count);

			return seq.MapElements(DataGroupHash.GetInstance);
		}
    }
}