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

using Org.BouncyCastle.Asn1.X509;

namespace Org.BouncyCastle.Asn1.Ocsp
{
    public class ResponderID
        : Asn1Encodable, IAsn1Choice
    {
		public static ResponderID GetInstance(object obj)
		{
			if (obj == null)
				return null;

			if (obj is ResponderID responderID)
				return responderID;

			if (obj is Asn1OctetString asn1OctetString)
				return new ResponderID(asn1OctetString);

			if (obj is Asn1TaggedObject taggedObject)
			{
				if (taggedObject.HasContextTag(1))
					return new ResponderID(X509Name.GetInstance(taggedObject, true));

				return new ResponderID(Asn1OctetString.GetInstance(taggedObject, true));
			}

			return new ResponderID(X509Name.GetInstance(obj));
		}

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

        private readonly Asn1Encodable m_id;

        public ResponderID(Asn1OctetString id)
        {
			m_id = id ?? throw new ArgumentNullException(nameof(id));
        }

		public ResponderID(X509Name id)
        {
            m_id = id ?? throw new ArgumentNullException(nameof(id));
        }

		public virtual byte[] GetKeyHash()
		{
			if (m_id is Asn1OctetString asn1OctetString)
				return asn1OctetString.GetOctets();

			return null;
		}

		public virtual X509Name Name
		{
			get
			{
				if (m_id is Asn1OctetString)
					return null;

				return X509Name.GetInstance(m_id);
			}
		}

		/**
         * Produce an object suitable for an Asn1OutputStream.
         * <pre>
         * ResponderID ::= CHOICE {
         *      byName          [1] Name,
         *      byKey           [2] KeyHash }
         * </pre>
         */
        public override Asn1Object ToAsn1Object()
        {
            if (m_id is Asn1OctetString asn1OctetString)
                return new DerTaggedObject(true, 2, asn1OctetString);

			return new DerTaggedObject(true, 1, m_id);
        }
    }
}