summary refs log tree commit diff
path: root/crypto/src/asn1/isismtt/x509/ProcurationSyntax.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/isismtt/x509/ProcurationSyntax.cs')
-rw-r--r--crypto/src/asn1/isismtt/x509/ProcurationSyntax.cs174
1 files changed, 73 insertions, 101 deletions
diff --git a/crypto/src/asn1/isismtt/x509/ProcurationSyntax.cs b/crypto/src/asn1/isismtt/x509/ProcurationSyntax.cs
index 96047f9bd..4b5a4693a 100644
--- a/crypto/src/asn1/isismtt/x509/ProcurationSyntax.cs
+++ b/crypto/src/asn1/isismtt/x509/ProcurationSyntax.cs
@@ -1,4 +1,5 @@
 using System;
+using System.Diagnostics;
 
 using Org.BouncyCastle.Asn1.X500;
 using Org.BouncyCastle.Asn1.X509;
@@ -6,7 +7,7 @@ using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.IsisMtt.X509
 {
-	/**
+    /**
 	* Attribute to indicate that the certificate holder may sign in the name of a
 	* third person.
 	* <p>
@@ -40,26 +41,29 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509
 	* </pre>
 	* 
 	*/
-	public class ProcurationSyntax
+    public class ProcurationSyntax
 		: Asn1Encodable
 	{
-		private readonly string				country;
-		private readonly DirectoryString	typeOfSubstitution;
-		private readonly GeneralName		thirdPerson;
-		private readonly IssuerSerial		certRef;
-
-		public static ProcurationSyntax GetInstance(object obj)
-		{
-			if (obj == null || obj is ProcurationSyntax)
-				return (ProcurationSyntax) obj;
-
-			if (obj is Asn1Sequence seq)
-				return new ProcurationSyntax(seq);
-
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
-		}
-
-		/**
+        public static ProcurationSyntax GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is ProcurationSyntax procurationSyntax)
+                return procurationSyntax;
+            return new ProcurationSyntax(Asn1Sequence.GetInstance(obj));
+        }
+
+        public static ProcurationSyntax GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new ProcurationSyntax(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
+
+        public static ProcurationSyntax GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new ProcurationSyntax(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
+
+        private readonly DerPrintableString m_country;
+        private readonly DirectoryString m_typeOfSubstitution;
+		private readonly Asn1Encodable m_signingFor;
+
+        /**
 		* Constructor from Asn1Sequence.
 		* <p/>
 		* The sequence is of type ProcurationSyntax:
@@ -80,40 +84,21 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509
 		*
 		* @param seq The ASN.1 sequence.
 		*/
-		private ProcurationSyntax(Asn1Sequence seq)
+        private ProcurationSyntax(Asn1Sequence seq)
 		{
-			if (seq.Count < 1 || seq.Count > 3)
-				throw new ArgumentException("Bad sequence size: " + seq.Count);
-
-			foreach (var element in seq)
-			{
-				Asn1TaggedObject o = Asn1TaggedObject.GetInstance(element, Asn1Tags.ContextSpecific);
-				switch (o.TagNo)
-				{
-				case 1:
-					country = DerPrintableString.GetInstance(o, true).GetString();
-					break;
-				case 2:
-					typeOfSubstitution = DirectoryString.GetInstance(o, true);
-					break;
-				case 3:
-					Asn1Encodable signingFor = o.GetExplicitBaseObject();
-					if (signingFor is Asn1TaggedObject)
-					{
-						thirdPerson = GeneralName.GetInstance(signingFor);
-					}
-					else
-					{
-						certRef = IssuerSerial.GetInstance(signingFor);
-					}
-					break;
-				default:
-					throw new ArgumentException("Bad tag number: " + o.TagNo);
-				}
-			}
+            int count = seq.Count, pos = 0;
+            if (count < 1 || count > 3)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
+
+			m_country = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, DerPrintableString.GetTagged);
+            m_typeOfSubstitution = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 2, true, DirectoryString.GetTagged);
+            m_signingFor = Asn1Utilities.ReadContextTagged(seq, ref pos, 3, true, GetTaggedSigningFor);
+
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
 		}
 
-		/**
+        /**
 		* Constructor from a given details.
 		* <p/>
 		* <p/>
@@ -124,18 +109,14 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509
 		* @param typeOfSubstitution The type of procuration.
 		* @param certRef            Reference to certificate of the person who is represented.
 		*/
-		public ProcurationSyntax(
-			string			country,
-			DirectoryString	typeOfSubstitution,
-			IssuerSerial	certRef)
-		{
-			this.country = country;
-			this.typeOfSubstitution = typeOfSubstitution;
-			this.thirdPerson = null;
-			this.certRef = certRef;
+        public ProcurationSyntax(string country, DirectoryString typeOfSubstitution, IssuerSerial certRef)
+        {
+            m_country = country == null ? null : new DerPrintableString(country, true);
+			m_typeOfSubstitution = typeOfSubstitution;
+			m_signingFor = certRef ?? throw new ArgumentNullException(nameof(certRef));
 		}
 
-		/**
+        /**
 		 * Constructor from a given details.
 		 * <p/>
 		 * <p/>
@@ -146,36 +127,20 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509
 		 * @param typeOfSubstitution The type of procuration.
 		 * @param thirdPerson        The GeneralName of the person who is represented.
 		 */
-		public ProcurationSyntax(
-			string			country,
-			DirectoryString	typeOfSubstitution,
-			GeneralName		thirdPerson)
-		{
-			this.country = country;
-			this.typeOfSubstitution = typeOfSubstitution;
-			this.thirdPerson = thirdPerson;
-			this.certRef = null;
+        public ProcurationSyntax(string country, DirectoryString typeOfSubstitution, GeneralName thirdPerson)
+        {
+            m_country = country == null ? null : new DerPrintableString(country, true);
+            m_typeOfSubstitution = typeOfSubstitution;
+            m_signingFor = thirdPerson ?? throw new ArgumentNullException(nameof(thirdPerson));
 		}
 
-		public virtual string Country
-		{
-			get { return country; }
-		}
+		public virtual string Country => m_country?.GetString();
 
-		public virtual DirectoryString TypeOfSubstitution
-		{
-			get { return typeOfSubstitution; }
-		}
+		public virtual DirectoryString TypeOfSubstitution => m_typeOfSubstitution;
 
-		public virtual GeneralName ThirdPerson
-		{
-			get { return thirdPerson; }
-		}
+		public virtual GeneralName ThirdPerson => m_signingFor as GeneralName;
 
-		public virtual IssuerSerial CertRef
-		{
-			get { return certRef; }
-		}
+		public virtual IssuerSerial CertRef => m_signingFor as IssuerSerial;
 
 		/**
 		* Produce an object suitable for an Asn1OutputStream.
@@ -201,24 +166,31 @@ namespace Org.BouncyCastle.Asn1.IsisMtt.X509
 		public override Asn1Object ToAsn1Object()
 		{
             Asn1EncodableVector v = new Asn1EncodableVector(3);
+			v.AddOptionalTagged(true, 1, m_country);
+            v.AddOptionalTagged(true, 2, m_typeOfSubstitution);
+			v.Add(new DerTaggedObject(true, 3, m_signingFor));
+            return new DerSequence(v);
+		}
 
-            if (country != null)
-            {
-                v.Add(new DerTaggedObject(true, 1, new DerPrintableString(country, true)));
-            }
+        private static Asn1Encodable GetInstanceSigningFor(Asn1Encodable obj)
+        {
+			var generalName = GeneralName.GetOptional(obj);
+			if (generalName != null)
+				return generalName;
 
-            v.AddOptionalTagged(true, 2, typeOfSubstitution);
+			var issuerSerial = IssuerSerial.GetOptional(obj);
+			if (issuerSerial != null)
+				return issuerSerial;
 
-            if (thirdPerson != null)
-            {
-                v.Add(new DerTaggedObject(true, 3, thirdPerson));
-            }
-            else
-            {
-                v.Add(new DerTaggedObject(true, 3, certRef));
-            }
+            throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), nameof(obj));
+        }
 
-            return new DerSequence(v);
-		}
-	}
+		private static Asn1Encodable GetTaggedSigningFor(Asn1TaggedObject taggedObject, bool declaredExplicit)
+		{
+			Debug.Assert(taggedObject != null);
+			Debug.Assert(declaredExplicit);
+
+            return GetInstanceSigningFor(taggedObject.GetExplicitBaseObject());
+        }
+    }
 }