summary refs log tree commit diff
path: root/crypto/src/asn1/x509/Target.cs
blob: eff74014342fa31b2fca7f151fa58e0647b2bcf8 (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
125
126
127
128
129
130
131
132
133
134
135
136
using System;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.X509
{
	/**
	 * Target structure used in target information extension for attribute
	 * certificates from RFC 3281.
	 * 
	 * <pre>
	 *     Target  ::= CHOICE {
	 *       targetName          [0] GeneralName,
	 *       targetGroup         [1] GeneralName,
	 *       targetCert          [2] TargetCert
	 *     }
	 * </pre>
	 * 
	 * <p>
	 * The targetCert field is currently not supported and must not be used
	 * according to RFC 3281.</p>
	 */
	public class Target
		: Asn1Encodable, IAsn1Choice
	{
		public enum Choice
		{
			Name = 0,
			Group = 1
		};

		private readonly GeneralName targetName;
		private readonly GeneralName targetGroup;

        /**
		* Creates an instance of a Target from the given object.
		* <p>
		* <code>obj</code> can be a Target or a {@link Asn1TaggedObject}</p>
		* 
		* @param obj The object.
		* @return A Target instance.
		* @throws ArgumentException if the given object cannot be
		*             interpreted as Target.
		*/
        public static Target GetInstance(object obj)
        {
            if (obj is Target target)
                return target;

            if (obj is Asn1TaggedObject taggedObject)
                return new Target(taggedObject);

            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
        }

        /**
		 * Constructor from Asn1TaggedObject.
		 * 
		 * @param tagObj The tagged object.
		 * @throws ArgumentException if the encoding is wrong.
		 */
        private Target(
			Asn1TaggedObject tagObj)
		{
			switch ((Choice) tagObj.TagNo)
			{
				case Choice.Name:	// GeneralName is already a choice so explicit
					targetName = GeneralName.GetInstance(tagObj, true);
					break;
				case Choice.Group:
					targetGroup = GeneralName.GetInstance(tagObj, true);
					break;
				default:
					throw new ArgumentException("unknown tag: " + tagObj.TagNo);
			}
		}

		/**
		 * Constructor from given details.
		 * <p>
		 * Exactly one of the parameters must be not <code>null</code>.</p>
		 *
		 * @param type the choice type to apply to the name.
		 * @param name the general name.
		 * @throws ArgumentException if type is invalid.
		 */
		public Target(
			Choice		type,
			GeneralName	name)
			: this(new DerTaggedObject((int) type, name))
		{
		}

		/**
		 * @return Returns the targetGroup.
		 */
		public virtual GeneralName TargetGroup
		{
			get { return targetGroup; }
		}

		/**
		 * @return Returns the targetName.
		 */
		public virtual GeneralName TargetName
		{
			get { return targetName; }
		}

		/**
		 * Produce an object suitable for an Asn1OutputStream.
		 * 
		 * Returns:
		 * 
		 * <pre>
		 *     Target  ::= CHOICE {
		 *       targetName          [0] GeneralName,
		 *       targetGroup         [1] GeneralName,
		 *       targetCert          [2] TargetCert
		 *     }
		 * </pre>
		 * 
		 * @return an Asn1Object
		 */
		public override Asn1Object ToAsn1Object()
		{
			// GeneralName is a choice already so most be explicitly tagged
			if (targetName != null)
			{
				return new DerTaggedObject(true, 0, targetName);
			}

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