summary refs log tree commit diff
path: root/crypto/src/asn1/cms/OriginatorIdentifierOrKey.cs
blob: 12b1f518dbba3ba01affca625a0b105418002ff8 (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
116
117
118
119
120
121
122
123
124
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 SubjectKeyIdentifier subjectKeyIdentifier)
                return new OriginatorIdentifierOrKey(subjectKeyIdentifier);

            if (o is OriginatorPublicKey originatorPublicKey)
                return new OriginatorIdentifierOrKey(originatorPublicKey);

            if (o is Asn1TaggedObject taggedObject)
                return new OriginatorIdentifierOrKey(Asn1Utilities.CheckTagClass(taggedObject, Asn1Tags.ContextSpecific));

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

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

        private readonly Asn1Encodable id;

        public OriginatorIdentifierOrKey(IssuerAndSerialNumber id)
        {
            this.id = id;
        }

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

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

		private OriginatorIdentifierOrKey(Asn1TaggedObject id)
		{
			// TODO Add validation
			this.id = id;
		}

		public Asn1Encodable ID
		{
			get { return id; }
		}

		public IssuerAndSerialNumber IssuerAndSerialNumber
		{
			get
			{
				if (id is IssuerAndSerialNumber)
				{
					return (IssuerAndSerialNumber)id;
				}

				return null;
			}
		}

		public SubjectKeyIdentifier SubjectKeyIdentifier
		{
			get
			{
				if (id is Asn1TaggedObject && ((Asn1TaggedObject)id).TagNo == 0)
				{
					return SubjectKeyIdentifier.GetInstance((Asn1TaggedObject)id, false);
				}

				return null;
			}
		}

		public OriginatorPublicKey OriginatorPublicKey
		{
			get
			{
				if (id is Asn1TaggedObject && ((Asn1TaggedObject)id).TagNo == 1)
				{
					return OriginatorPublicKey.GetInstance((Asn1TaggedObject)id, 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()
        {
            return id.ToAsn1Object();
        }
    }
}