summary refs log tree commit diff
path: root/crypto/src/asn1/cms/Evidence.cs
blob: baa93d6b9fa18630c7bba73d1583f858eb610a61 (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
using System;

using Org.BouncyCastle.Asn1.Tsp;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.Cms
{
	public class Evidence
		: Asn1Encodable, IAsn1Choice
	{
        public static Evidence GetInstance(object obj)
        {
            if (obj == null)
                return null;
            if (obj is Evidence evidence)
                return evidence;
            if (obj is Asn1TaggedObject taggedObject)
                return new Evidence(Asn1Utilities.CheckContextTagClass(taggedObject));

            throw new ArgumentException("Unknown object in GetInstance: " + Platform.GetTypeName(obj), nameof(obj));
        }

        public static Evidence GetInstance(Asn1TaggedObject obj, bool isExplicit)
        {
            return Asn1Utilities.GetInstanceFromChoice(obj, isExplicit, GetInstance);
        }

        private readonly TimeStampTokenEvidence m_tstEvidence;
        private readonly EvidenceRecord m_ersEvidence;
        private readonly Asn1Sequence m_otherEvidence;

        public Evidence(TimeStampTokenEvidence tstEvidence)
		{
			m_tstEvidence = tstEvidence;
		}

        public Evidence(EvidenceRecord ersEvidence)
        {
            m_ersEvidence = ersEvidence;
        }

        private Evidence(Asn1TaggedObject tagged)
		{
            if (tagged.TagNo == 0)
            {
                m_tstEvidence = TimeStampTokenEvidence.GetInstance(tagged, false);
            }
            else if (tagged.TagNo == 1)
            {
                m_ersEvidence = EvidenceRecord.GetInstance(tagged, false);
            }
            else if (tagged.TagNo == 2)
            {
                m_otherEvidence = Asn1Sequence.GetInstance(tagged, false);
            }
            else
            {
                throw new ArgumentException("unknown tag in Evidence", nameof(tagged));
            }
        }

        public virtual TimeStampTokenEvidence TstEvidence => m_tstEvidence;

        public virtual EvidenceRecord ErsEvidence => m_ersEvidence;

        public override Asn1Object ToAsn1Object()
        {
            if (m_tstEvidence != null)
                return new DerTaggedObject(false, 0, m_tstEvidence);
            if (m_ersEvidence != null)
                return new DerTaggedObject(false, 1, m_ersEvidence);
            return new DerTaggedObject(false, 2, m_otherEvidence);
        }
    }
}