summary refs log tree commit diff
path: root/crypto/src/asn1/crmf/SinglePubInfo.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-06-04 23:44:11 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-06-04 23:44:11 +0700
commitcea0366cdf7450e96b663a2ee18ad37c2fdeb5cc (patch)
tree4226642a7547dc373400e385b04a3d2e6f12eed6 /crypto/src/asn1/crmf/SinglePubInfo.cs
parentASN.1: Add support methods for optional elements (diff)
downloadBouncyCastle.NET-ed25519-cea0366cdf7450e96b663a2ee18ad37c2fdeb5cc.tar.xz
Refactoring in Asn1.Crmf
Diffstat (limited to '')
-rw-r--r--crypto/src/asn1/crmf/SinglePubInfo.cs54
1 files changed, 31 insertions, 23 deletions
diff --git a/crypto/src/asn1/crmf/SinglePubInfo.cs b/crypto/src/asn1/crmf/SinglePubInfo.cs
index 5205ce366..7d5d742fd 100644
--- a/crypto/src/asn1/crmf/SinglePubInfo.cs
+++ b/crypto/src/asn1/crmf/SinglePubInfo.cs
@@ -1,42 +1,50 @@
 using System;
 
 using Org.BouncyCastle.Asn1.X509;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Crmf
 {
     public class SinglePubInfo
         : Asn1Encodable
     {
-        private readonly DerInteger pubMethod;
-        private readonly GeneralName pubLocation;
-
-        private SinglePubInfo(Asn1Sequence seq)
+        public static SinglePubInfo GetInstance(object obj)
         {
-            pubMethod = DerInteger.GetInstance(seq[0]);
+            if (obj == null)
+                return null;
+            if (obj is SinglePubInfo singlePubInfo)
+                return singlePubInfo;
+            return new SinglePubInfo(Asn1Sequence.GetInstance(obj));
+        }
 
-            if (seq.Count == 2)
-            {
-                pubLocation = GeneralName.GetInstance(seq[1]);
-            }
+        public static SinglePubInfo GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
+        {
+            return new SinglePubInfo(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
         }
 
-        public static SinglePubInfo GetInstance(object obj)
+        private readonly DerInteger m_pubMethod;
+        private readonly GeneralName m_pubLocation;
+
+        private SinglePubInfo(Asn1Sequence seq)
         {
-            if (obj is SinglePubInfo)
-                return (SinglePubInfo)obj;
+            int count = seq.Count;
+            if (count < 1 || count > 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-            if (obj is Asn1Sequence)
-                return new SinglePubInfo((Asn1Sequence)obj);
+            int pos = 0;
 
-            throw new ArgumentException("Invalid object: " + Platform.GetTypeName(obj), "obj");
-        }
+            m_pubMethod = DerInteger.GetInstance(seq[pos++]);
 
-        public virtual GeneralName PubLocation
-        {
-            get { return pubLocation; }
+            if (pos < count)
+            {
+                m_pubLocation = GeneralName.GetInstance(seq[pos++]);
+            }
+
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
+        public virtual GeneralName PubLocation => m_pubLocation;
+
         /**
          * <pre>
          * SinglePubInfo ::= SEQUENCE {
@@ -51,9 +59,9 @@ namespace Org.BouncyCastle.Asn1.Crmf
          */
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(pubMethod);
-            v.AddOptional(pubLocation);
-            return new DerSequence(v);
+            return m_pubLocation == null
+                ?  new DerSequence(m_pubMethod)
+                :  new DerSequence(m_pubMethod, m_pubLocation);
         }
     }
 }