summary refs log tree commit diff
path: root/crypto/src/asn1/esf/SignerLocation.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-05-26 22:58:57 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-05-26 22:58:57 +0700
commit46c46cecd4f62abf854576314751e44b320168ba (patch)
tree6bafc67dbf0f5480d9814e7764e770faad233f66 /crypto/src/asn1/esf/SignerLocation.cs
parentAuto-adjust DateTime precision for GeneralizedTime in X.509 (and OCSP) (diff)
downloadBouncyCastle.NET-ed25519-46c46cecd4f62abf854576314751e44b320168ba.tar.xz
Refactoring in Asn1.Esf
Diffstat (limited to 'crypto/src/asn1/esf/SignerLocation.cs')
-rw-r--r--crypto/src/asn1/esf/SignerLocation.cs149
1 files changed, 65 insertions, 84 deletions
diff --git a/crypto/src/asn1/esf/SignerLocation.cs b/crypto/src/asn1/esf/SignerLocation.cs
index 7b1300045..c4f5e6521 100644
--- a/crypto/src/asn1/esf/SignerLocation.cs
+++ b/crypto/src/asn1/esf/SignerLocation.cs
@@ -4,7 +4,7 @@ using Org.BouncyCastle.Asn1.X500;
 
 namespace Org.BouncyCastle.Asn1.Esf
 {
-	/**
+    /**
 	* Signer-Location attribute (RFC3126).
 	*
 	* <pre>
@@ -16,100 +16,81 @@ namespace Org.BouncyCastle.Asn1.Esf
 	*   PostalAddress ::= SEQUENCE SIZE(1..6) OF DirectoryString
 	* </pre>
 	*/
-	public class SignerLocation
-		: Asn1Encodable
-	{
-        private readonly DirectoryString countryName;
-        private readonly DirectoryString localityName;
-        private readonly Asn1Sequence postalAddress;
-
-		public SignerLocation(Asn1Sequence seq)
-		{
-			foreach (var element in seq)
-			{
-				var obj = Asn1TaggedObject.GetInstance(element, Asn1Tags.ContextSpecific);
-
-				switch (obj.TagNo)
-				{
-				case 0:
-					this.countryName = DirectoryString.GetInstance(obj, true);
-					break;
-				case 1:
-                    this.localityName = DirectoryString.GetInstance(obj, true);
-					break;
-				case 2:
-					bool isExplicit = obj.IsExplicit();	// handle erroneous implicitly tagged sequences
-					this.postalAddress = Asn1Sequence.GetInstance(obj, isExplicit);
-					if (postalAddress != null && postalAddress.Count > 6)
-						throw new ArgumentException("postal address must contain less than 6 strings");
-					break;
-				default:
-					throw new ArgumentException("illegal tag");
-				}
-			}
-		}
-
-        private SignerLocation(
-            DirectoryString countryName,
-            DirectoryString localityName,
-            Asn1Sequence postalAddress)
+    public class SignerLocation
+        : Asn1Encodable
+    {
+        public static SignerLocation GetInstance(object obj)
         {
-            if (postalAddress != null && postalAddress.Count > 6)
-                throw new ArgumentException("postal address must contain less than 6 strings");
-
-            this.countryName = countryName;
-            this.localityName = localityName;
-            this.postalAddress = postalAddress;
+            if (obj == null)
+                return null;
+            if (obj is SignerLocation signerLocation)
+                return signerLocation;
+            return new SignerLocation(Asn1Sequence.GetInstance(obj));
         }
 
-        public SignerLocation(
-            DirectoryString countryName,
-            DirectoryString localityName,
-            DirectoryString[] postalAddress)
-            : this(countryName, localityName, new DerSequence(postalAddress))
+        public static SignerLocation GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
+            return new SignerLocation(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
         }
 
-        public SignerLocation(
-            DerUtf8String countryName,
-            DerUtf8String localityName,
-            Asn1Sequence postalAddress)
-            : this(DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), postalAddress)
+        private readonly DirectoryString m_countryName;
+        private readonly DirectoryString m_localityName;
+        private readonly Asn1Sequence m_postalAddress;
+
+        public SignerLocation(Asn1Sequence seq)
         {
-        }
+            int count = seq.Count;
+            if (count < 0 || count > 3)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-        public static SignerLocation GetInstance(object obj)
-		{
-			if (obj == null || obj is SignerLocation)
-				return (SignerLocation) obj;
+            int pos = 0;
 
-			return new SignerLocation(Asn1Sequence.GetInstance(obj));
-		}
+            m_countryName = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, DirectoryString.GetInstance);
+            m_localityName = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, DirectoryString.GetInstance);
 
-        public DirectoryString Country
-        {
-            get { return countryName; }
+            m_postalAddress = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 2, true, Asn1Sequence.GetInstance);
+            if (m_postalAddress != null)
+            {
+                if (m_postalAddress.Count > 6)
+                    throw new ArgumentException("postal address must contain less than 6 strings");
+
+                m_postalAddress.MapElements(element => DirectoryString.GetInstance(element.ToAsn1Object()));
+            }
+
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-        public DirectoryString Locality
+        private SignerLocation(DirectoryString countryName, DirectoryString localityName, Asn1Sequence postalAddress)
         {
-            get { return localityName; }
+            if (postalAddress != null && postalAddress.Count > 6)
+                throw new ArgumentException("postal address must contain less than 6 strings");
+
+            m_countryName = countryName;
+            m_localityName = localityName;
+            m_postalAddress = postalAddress;
         }
 
-        public DirectoryString[] GetPostal()
+        public SignerLocation(DirectoryString countryName, DirectoryString localityName, DirectoryString[] postalAddress)
+            : this(countryName, localityName, new DerSequence(postalAddress))
         {
-            if (postalAddress == null)
-                return null;
+        }
 
-			return postalAddress.MapElements(element => DirectoryString.GetInstance(element.ToAsn1Object()));
+        public SignerLocation(DerUtf8String countryName, DerUtf8String localityName, Asn1Sequence postalAddress)
+            : this(DirectoryString.GetInstance(countryName), DirectoryString.GetInstance(localityName), postalAddress)
+        {
         }
 
-		public Asn1Sequence PostalAddress
-		{
-			get { return postalAddress; }
-		}
+        public DirectoryString Country => m_countryName;
+
+        public DirectoryString Locality => m_localityName;
+
+        public DirectoryString[] GetPostal() =>
+            m_postalAddress?.MapElements(element => DirectoryString.GetInstance(element.ToAsn1Object()));
 
-		/**
+        public Asn1Sequence PostalAddress => m_postalAddress;
+
+        /**
 		* <pre>
 		*   SignerLocation ::= SEQUENCE {
 		*       countryName        [0] DirectoryString OPTIONAL,
@@ -126,13 +107,13 @@ namespace Org.BouncyCastle.Asn1.Esf
 		*         bmpString               BMPString (SIZE (1..MAX)) }
 		* </pre>
 		*/
-		public override Asn1Object ToAsn1Object()
-		{
-			Asn1EncodableVector v = new Asn1EncodableVector(3);
-            v.AddOptionalTagged(true, 0, countryName);
-            v.AddOptionalTagged(true, 1, localityName);
-            v.AddOptionalTagged(true, 2, postalAddress);
-			return new DerSequence(v);
-		}
-	}
+        public override Asn1Object ToAsn1Object()
+        {
+            Asn1EncodableVector v = new Asn1EncodableVector(3);
+            v.AddOptionalTagged(true, 0, m_countryName);
+            v.AddOptionalTagged(true, 1, m_localityName);
+            v.AddOptionalTagged(true, 2, m_postalAddress);
+            return DerSequence.FromVector(v);
+        }
+    }
 }