summary refs log tree commit diff
path: root/crypto/src/asn1/bc/LinkedCertificate.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/bc/LinkedCertificate.cs')
-rw-r--r--crypto/src/asn1/bc/LinkedCertificate.cs91
1 files changed, 39 insertions, 52 deletions
diff --git a/crypto/src/asn1/bc/LinkedCertificate.cs b/crypto/src/asn1/bc/LinkedCertificate.cs
index c8d05d8f5..e530cd82c 100644
--- a/crypto/src/asn1/bc/LinkedCertificate.cs
+++ b/crypto/src/asn1/bc/LinkedCertificate.cs
@@ -18,11 +18,24 @@ namespace Org.BouncyCastle.Asn1.BC
     public class LinkedCertificate
         : Asn1Encodable
     {
-        private readonly DigestInfo mDigest;
-        private readonly GeneralName mCertLocation;
+        public static LinkedCertificate GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is LinkedCertificate linkedCertificate)
+                return linkedCertificate;
+            return new LinkedCertificate(Asn1Sequence.GetInstance(obj));
+        }
 
-        private X509Name mCertIssuer;
-        private GeneralNames mCACerts;
+        public static LinkedCertificate GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
+        {
+            return new LinkedCertificate(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
+        }
+
+        private readonly DigestInfo m_digest;
+        private readonly GeneralName m_certLocation;
+        private readonly X509Name m_certIssuer;
+        private readonly GeneralNames m_cACerts;
 
         public LinkedCertificate(DigestInfo digest, GeneralName certLocation)
             : this(digest, certLocation, null, null)
@@ -31,69 +44,43 @@ namespace Org.BouncyCastle.Asn1.BC
 
         public LinkedCertificate(DigestInfo digest, GeneralName certLocation, X509Name certIssuer, GeneralNames caCerts)
         {
-            this.mDigest = digest;
-            this.mCertLocation = certLocation;
-            this.mCertIssuer = certIssuer;
-            this.mCACerts = caCerts;
+            m_digest = digest ?? throw new ArgumentNullException(nameof(digest));
+            m_certLocation = certLocation ?? throw new ArgumentNullException(nameof(certLocation));
+            m_certIssuer = certIssuer;
+            m_cACerts = caCerts;
         }
 
         private LinkedCertificate(Asn1Sequence seq)
         {
-            this.mDigest = DigestInfo.GetInstance(seq[0]);
-            this.mCertLocation = GeneralName.GetInstance(seq[1]);
+            int count = seq.Count;
+            if (count < 2 || count > 4)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-            for (int i = 2; i < seq.Count; ++i)
-            {
-                Asn1TaggedObject tagged =  Asn1TaggedObject.GetInstance(seq[i]);
+            int pos = 0;
 
-                switch (tagged.TagNo)
-                {
-                case 0:
-                    this.mCertIssuer = X509Name.GetInstance(tagged, false);
-                    break;
-                case 1:
-                    this.mCACerts = GeneralNames.GetInstance(tagged, false);
-                    break;
-                default:
-                    throw new ArgumentException("unknown tag in tagged field");
-                }
-            }
-        }
+            m_digest = DigestInfo.GetInstance(seq[pos++]);
+            m_certLocation = GeneralName.GetInstance(seq[pos++]);
+            m_certIssuer = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, false, X509Name.GetInstance);
+            m_cACerts = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, false, GeneralNames.GetInstance);
 
-        public static LinkedCertificate GetInstance(object obj)
-        {
-            if (obj is LinkedCertificate)
-                return (LinkedCertificate)obj;
-            if (obj != null)
-                return new LinkedCertificate(Asn1Sequence.GetInstance(obj));
-            return null;
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-        public virtual DigestInfo Digest
-        {
-            get { return mDigest; }
-        }
+        public virtual DigestInfo Digest => m_digest;
 
-        public virtual GeneralName CertLocation
-        {
-            get { return mCertLocation; }
-        }
+        public virtual GeneralName CertLocation => m_certLocation;
 
-        public virtual X509Name CertIssuer
-        {
-            get { return mCertIssuer; }
-        }
+        public virtual X509Name CertIssuer => m_certIssuer;
 
-        public virtual GeneralNames CACerts
-        {
-            get { return mCACerts; }
-        }
+        public virtual GeneralNames CACerts => m_cACerts;
 
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(mDigest, mCertLocation);
-            v.AddOptionalTagged(false, 0, mCertIssuer);
-            v.AddOptionalTagged(false, 1, mCACerts);
+            Asn1EncodableVector v = new Asn1EncodableVector(4);
+            v.Add(m_digest, m_certLocation);
+            v.AddOptionalTagged(false, 0, m_certIssuer);
+            v.AddOptionalTagged(false, 1, m_cACerts);
             return new DerSequence(v);
         }
     }