summary refs log tree commit diff
path: root/crypto/src/asn1/esf/SignaturePolicyId.cs
blob: 18ea1634a5ba9e2a5c319180ae31268ba6769b42 (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
using System;
using System.Collections.Generic;

namespace Org.BouncyCastle.Asn1.Esf
{
    /// <remarks>
    /// <code>
    /// SignaturePolicyId ::= SEQUENCE {
    /// 	sigPolicyIdentifier		SigPolicyId,
    /// 	sigPolicyHash			SigPolicyHash,
    /// 	sigPolicyQualifiers		SEQUENCE SIZE (1..MAX) OF SigPolicyQualifierInfo OPTIONAL
    /// }
    /// 
    /// SigPolicyId ::= OBJECT IDENTIFIER
    /// 
    /// SigPolicyHash ::= OtherHashAlgAndValue
    /// </code>
    /// </remarks>
    public class SignaturePolicyId
		: Asn1Encodable
	{
		public static SignaturePolicyId GetInstance(object obj)
		{
			if (obj == null)
				return null;
			if (obj is SignaturePolicyId signaturePolicyId)
				return signaturePolicyId;
			return new SignaturePolicyId(Asn1Sequence.GetInstance(obj));
		}

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

        private readonly DerObjectIdentifier m_sigPolicyIdentifier;
        private readonly OtherHashAlgAndValue m_sigPolicyHash;
        private readonly Asn1Sequence m_sigPolicyQualifiers;

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

			m_sigPolicyIdentifier = DerObjectIdentifier.GetInstance(seq[0]);
			m_sigPolicyHash = OtherHashAlgAndValue.GetInstance(seq[1]);

			if (count > 2)
			{
				m_sigPolicyQualifiers = Asn1Sequence.GetInstance(seq[2]);
			}
		}

		public SignaturePolicyId(DerObjectIdentifier sigPolicyIdentifier, OtherHashAlgAndValue sigPolicyHash)
			: this(sigPolicyIdentifier, sigPolicyHash, null)
		{
		}

		public SignaturePolicyId(DerObjectIdentifier sigPolicyIdentifier, OtherHashAlgAndValue sigPolicyHash,
			params SigPolicyQualifierInfo[]	sigPolicyQualifiers)
		{
			m_sigPolicyIdentifier = sigPolicyIdentifier ?? throw new ArgumentNullException(nameof(sigPolicyIdentifier));
            m_sigPolicyHash = sigPolicyHash ?? throw new ArgumentNullException(nameof(sigPolicyHash));

            if (sigPolicyQualifiers != null)
			{
				m_sigPolicyQualifiers = DerSequence.FromElements(sigPolicyQualifiers);
			}
		}

		public SignaturePolicyId(DerObjectIdentifier sigPolicyIdentifier, OtherHashAlgAndValue sigPolicyHash,
			IEnumerable<SigPolicyQualifierInfo> sigPolicyQualifiers)
		{
			m_sigPolicyIdentifier = sigPolicyIdentifier ?? throw new ArgumentNullException(nameof(sigPolicyIdentifier));
            m_sigPolicyHash = sigPolicyHash ?? throw new ArgumentNullException(nameof(sigPolicyHash));

            if (sigPolicyQualifiers != null)
			{
				m_sigPolicyQualifiers = DerSequence.FromVector(Asn1EncodableVector.FromEnumerable(sigPolicyQualifiers));
			}
		}

		public DerObjectIdentifier SigPolicyIdentifier => m_sigPolicyIdentifier;

		public OtherHashAlgAndValue SigPolicyHash => m_sigPolicyHash;

		public SigPolicyQualifierInfo[] GetSigPolicyQualifiers() =>
			m_sigPolicyQualifiers?.MapElements(SigPolicyQualifierInfo.GetInstance);

		public override Asn1Object ToAsn1Object()
		{
			Asn1EncodableVector v = new Asn1EncodableVector(3);
			v.Add(m_sigPolicyIdentifier, m_sigPolicyHash);
			v.AddOptional(m_sigPolicyQualifiers);
			return new DerSequence(v);
		}
	}
}