summary refs log tree commit diff
path: root/crypto/src/asn1/x500/AttributeTypeAndValue.cs
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2024-07-01 19:41:12 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2024-07-01 19:41:12 +0700
commit6e5206da62f412be2b27298c146b41271210e0d9 (patch)
tree89d91dfc59813e8fb0c2ffd12572f89ea5613570 /crypto/src/asn1/x500/AttributeTypeAndValue.cs
parentRefactoring in Asn1.Tsp (diff)
downloadBouncyCastle.NET-ed25519-6e5206da62f412be2b27298c146b41271210e0d9.tar.xz
Refactoring in Asn1.X500
Diffstat (limited to 'crypto/src/asn1/x500/AttributeTypeAndValue.cs')
-rw-r--r--crypto/src/asn1/x500/AttributeTypeAndValue.cs57
1 files changed, 28 insertions, 29 deletions
diff --git a/crypto/src/asn1/x500/AttributeTypeAndValue.cs b/crypto/src/asn1/x500/AttributeTypeAndValue.cs
index eb6b3ca30..d7e684d86 100644
--- a/crypto/src/asn1/x500/AttributeTypeAndValue.cs
+++ b/crypto/src/asn1/x500/AttributeTypeAndValue.cs
@@ -8,42 +8,44 @@ namespace Org.BouncyCastle.Asn1.X500
     public class AttributeTypeAndValue
         : Asn1Encodable
     {
-        private readonly DerObjectIdentifier type;
-        private readonly Asn1Encodable value;
-
-        private AttributeTypeAndValue(Asn1Sequence seq)
-        {
-            type = (DerObjectIdentifier)seq[0];
-            value = seq[1];
-        }
-
         public static AttributeTypeAndValue GetInstance(object obj)
         {
-            if (obj is AttributeTypeAndValue)
-                return (AttributeTypeAndValue)obj;
-            if (null != obj)
-                return new AttributeTypeAndValue(Asn1Sequence.GetInstance(obj));
-            throw new ArgumentNullException("obj");
+            if (obj == null)
+                return null;
+            if (obj is AttributeTypeAndValue attributeTypeAndValue)
+                return attributeTypeAndValue;
+            return new AttributeTypeAndValue(Asn1Sequence.GetInstance(obj));
         }
 
-        public AttributeTypeAndValue(
-            DerObjectIdentifier type,
-            Asn1Encodable value)
-        {
-            this.type = type;
-            this.value = value;
-        }
+        public static AttributeTypeAndValue GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new AttributeTypeAndValue(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
+
+        public static AttributeTypeAndValue GetTagged(Asn1TaggedObject taggedObject, bool declaredExplicit) =>
+            new AttributeTypeAndValue(Asn1Sequence.GetTagged(taggedObject, declaredExplicit));
 
-        public virtual DerObjectIdentifier Type
+        private readonly DerObjectIdentifier m_type;
+        private readonly Asn1Encodable m_value;
+
+        private AttributeTypeAndValue(Asn1Sequence seq)
         {
-            get { return type; }
+            int count = seq.Count;
+            if (count != 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
+
+            m_type = DerObjectIdentifier.GetInstance(seq[0]);
+            m_value = seq[1];
         }
 
-        public virtual Asn1Encodable Value
+        public AttributeTypeAndValue(DerObjectIdentifier type, Asn1Encodable value)
         {
-            get { return value; }
+            m_type = type ?? throw new ArgumentNullException(nameof(type));
+            m_value = value ?? throw new ArgumentNullException(nameof(value));
         }
 
+        public virtual DerObjectIdentifier Type => m_type;
+
+        public virtual Asn1Encodable Value => m_value;
+
         /**
          * <pre>
          * AttributeTypeAndValue ::= SEQUENCE {
@@ -52,9 +54,6 @@ namespace Org.BouncyCastle.Asn1.X500
          * </pre>
          * @return a basic ASN.1 object representation.
          */
-        public override Asn1Object ToAsn1Object()
-        {
-            return new DerSequence(type, value);
-        }
+        public override Asn1Object ToAsn1Object() => new DerSequence(m_type, m_value);
     }
 }