summary refs log tree commit diff
path: root/crypto/src/asn1/isismtt/x509/DeclarationOfMajority.cs
blob: 5db3df9e78521d4c8aec55f71af3be7e730e1d96 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;

using Org.BouncyCastle.Utilities;

namespace Org.BouncyCastle.Asn1.IsisMtt.X509
{
    /**
	* A declaration of majority.
	* <p/>
	* <pre>
	*           DeclarationOfMajoritySyntax ::= CHOICE
	*           {
	*             notYoungerThan [0] IMPLICIT INTEGER,
	*             fullAgeAtCountry [1] IMPLICIT SEQUENCE
	*             {
	*               fullAge BOOLEAN DEFAULT TRUE,
	*               country PrintableString (SIZE(2))
	*             }
	*             dateOfBirth [2] IMPLICIT GeneralizedTime
	*           }
	* </pre>
	* <p/>
	* fullAgeAtCountry indicates the majority of the owner with respect to the laws
	* of a specific country.
	*/
    public class DeclarationOfMajority
		: Asn1Encodable, IAsn1Choice
	{
		public enum Choice
		{
			NotYoungerThan = 0,
			FullAgeAtCountry = 1,
			DateOfBirth = 2
		};

        public static DeclarationOfMajority GetInstance(object obj)
        {
            if (obj == null)
                return null;

            if (obj is Asn1Encodable element)
            {
                var result = GetOptional(element);
                if (result != null)
                    return result;
            }

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

        public static DeclarationOfMajority GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            Asn1Utilities.GetInstanceChoice(taggedObject, declaredExplicit, GetInstance);

        public static DeclarationOfMajority GetOptional(Asn1Encodable element)
        {
            if (element == null)
                throw new ArgumentNullException(nameof(element));

            if (element is DeclarationOfMajority declarationOfMajority)
                return declarationOfMajority;

            if (element is Asn1TaggedObject taggedObject)
            {
                Asn1Encodable baseObject = GetOptionalBaseObject(taggedObject);
                if (baseObject != null)
                    return new DeclarationOfMajority(taggedObject.TagNo, baseObject);
            }

            return null;
        }

        public static DeclarationOfMajority GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
            Asn1Utilities.GetTaggedChoice(taggedObject, declaredExplicit, GetInstance);

        private static Asn1Encodable GetOptionalBaseObject(Asn1TaggedObject taggedObject)
        {
			if (taggedObject.HasContextTag())
			{
				switch (taggedObject.TagNo)
				{
				case (int)Choice.NotYoungerThan:
                    return DerInteger.GetInstance(taggedObject, false);
				case (int)Choice.FullAgeAtCountry:
					return Asn1Sequence.GetInstance(taggedObject, false);
				case (int)Choice.DateOfBirth:
					return Asn1GeneralizedTime.GetInstance(taggedObject, false);
				}
            }

            return null;
        }

        private readonly int m_tag;
        private readonly Asn1Encodable m_baseObject;

        private DeclarationOfMajority(int tag, Asn1Encodable baseObject)
        {
            m_tag = tag;
            m_baseObject = baseObject;
        }

        public DeclarationOfMajority(int notYoungerThan)
		{
			m_tag = (int)Choice.NotYoungerThan;
			m_baseObject = new DerInteger(notYoungerThan);
		}

		public DeclarationOfMajority(bool fullAge, string country)
		{
			if (country.Length != 2)
				throw new ArgumentException("country can only be 2 characters", nameof(country));

			DerPrintableString countryString = new DerPrintableString(country, true);

			DerSequence seq;
			if (fullAge)
			{
				seq = new DerSequence(countryString);
			}
			else
			{
				seq = new DerSequence(DerBoolean.False, countryString);
			}

			m_tag = (int)Choice.FullAgeAtCountry;
			m_baseObject = seq;
		}

		public DeclarationOfMajority(Asn1GeneralizedTime dateOfBirth)
		{
			m_tag = (int)Choice.DateOfBirth;
			m_baseObject = dateOfBirth ?? throw new ArgumentNullException(nameof(dateOfBirth));
		}

		public Choice Type => (Choice)m_tag;

		/**
		* @return notYoungerThan if that's what we are, -1 otherwise
		*/
		public virtual int NotYoungerThan
		{
			get
			{
				switch (Type)
				{
				case Choice.NotYoungerThan:
                    return DerInteger.GetInstance(m_baseObject).IntValueExact;
				default:
					return -1;
				}
			}
		}

		public virtual Asn1Sequence FullAgeAtCountry
		{
			get
			{
				switch (Type)
				{
				case Choice.FullAgeAtCountry:
					return Asn1Sequence.GetInstance(m_baseObject);
				default:
					return null;
				}
			}
		}

		public virtual Asn1GeneralizedTime DateOfBirth
		{
			get
			{
				switch (Type)
				{
				case Choice.DateOfBirth:
					return Asn1GeneralizedTime.GetInstance(m_baseObject);
				default:
					return null;
				}
			}
		}

		/**
		* Produce an object suitable for an Asn1OutputStream.
		* <p/>
		* Returns:
		* <p/>
		* <pre>
		*           DeclarationOfMajoritySyntax ::= CHOICE
		*           {
		*             notYoungerThan [0] IMPLICIT INTEGER,
		*             fullAgeAtCountry [1] IMPLICIT SEQUENCE
		*             {
		*               fullAge BOOLEAN DEFAULT TRUE,
		*               country PrintableString (SIZE(2))
		*             }
		*             dateOfBirth [2] IMPLICIT GeneralizedTime
		*           }
		* </pre>
		*
		* @return an Asn1Object
		*/
		public override Asn1Object ToAsn1Object() => new DerTaggedObject(false, m_tag, m_baseObject);
	}
}