summary refs log tree commit diff
path: root/crypto/src/asn1/cms/OriginatorIdentifierOrKey.cs
blob: 6d127ccc3f8eea54484a910b011ffe5bdf064bff (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 Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.Cms
{
    public class OriginatorIdentifierOrKey
        : Asn1Encodable, IAsn1Choice
    {
        public static OriginatorIdentifierOrKey GetInstance(object o)
        {
            if (o == null)
                return null;

            if (o is OriginatorIdentifierOrKey originatorIdentifierOrKey)
                return originatorIdentifierOrKey;

            if (o is IssuerAndSerialNumber issuerAndSerialNumber)
                return new OriginatorIdentifierOrKey(issuerAndSerialNumber);

            if (o is Asn1Sequence sequence)
                return new OriginatorIdentifierOrKey(IssuerAndSerialNumber.GetInstance(sequence));

            if (o is Asn1TaggedObject taggedObject)
            {
                if (taggedObject.HasContextTag(0))
                    return new OriginatorIdentifierOrKey(SubjectKeyIdentifier.GetInstance(taggedObject, false));

                if (taggedObject.HasContextTag(1))
                    return new OriginatorIdentifierOrKey(OriginatorPublicKey.GetInstance(taggedObject, false));
            }

            throw new ArgumentException("Invalid OriginatorIdentifierOrKey: " + Platform.GetTypeName(o), nameof(o));
        }

        public static OriginatorIdentifierOrKey GetInstance(Asn1TaggedObject o, bool explicitly)
        {
            return Asn1Utilities.GetInstanceFromChoice(o, explicitly, GetInstance);
        }

        private readonly Asn1Encodable m_id;

        public OriginatorIdentifierOrKey(IssuerAndSerialNumber id)
        {
            m_id = id;
        }

        public OriginatorIdentifierOrKey(SubjectKeyIdentifier id)
        {
            m_id = new DerTaggedObject(false, 0, id);
        }

        public OriginatorIdentifierOrKey(OriginatorPublicKey id)
        {
            m_id = new DerTaggedObject(false, 1, id);
        }

        public Asn1Encodable ID => m_id;

        public IssuerAndSerialNumber IssuerAndSerialNumber => m_id as IssuerAndSerialNumber;

		public SubjectKeyIdentifier SubjectKeyIdentifier
		{
			get
			{
                if (m_id is Asn1TaggedObject taggedObject && taggedObject.HasContextTag(0))
                    return SubjectKeyIdentifier.GetInstance(taggedObject, false);

				return null;
			}
		}

		public OriginatorPublicKey OriginatorPublicKey
		{
			get
			{
                if (m_id is Asn1TaggedObject taggedObject && taggedObject.HasContextTag(1))
					return OriginatorPublicKey.GetInstance(taggedObject, false);

				return null;
			}
		}

		/**
         * Produce an object suitable for an Asn1OutputStream.
         * <pre>
         * OriginatorIdentifierOrKey ::= CHOICE {
         *     issuerAndSerialNumber IssuerAndSerialNumber,
         *     subjectKeyIdentifier [0] SubjectKeyIdentifier,
         *     originatorKey [1] OriginatorPublicKey
         * }
         *
         * SubjectKeyIdentifier ::= OCTET STRING
         * </pre>
         */
        public override Asn1Object ToAsn1Object() => m_id.ToAsn1Object();
    }
}