summary refs log tree commit diff
path: root/crypto/src/asn1/crmf/CertRequest.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/crmf/CertRequest.cs')
-rw-r--r--crypto/src/asn1/crmf/CertRequest.cs78
1 files changed, 36 insertions, 42 deletions
diff --git a/crypto/src/asn1/crmf/CertRequest.cs b/crypto/src/asn1/crmf/CertRequest.cs
index bf6182f25..dea7ea23c 100644
--- a/crypto/src/asn1/crmf/CertRequest.cs
+++ b/crypto/src/asn1/crmf/CertRequest.cs
@@ -1,68 +1,61 @@
 using System;
-using Org.BouncyCastle.Crmf;
 
 namespace Org.BouncyCastle.Asn1.Crmf
 {
     public class CertRequest
         : Asn1Encodable
     {
-        private readonly DerInteger certReqId;
-        private readonly CertTemplate certTemplate;
-        private readonly Controls controls;
+        public static CertRequest GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is CertRequest certRequest)
+                return certRequest;
+            return new CertRequest(Asn1Sequence.GetInstance(obj));
+        }
 
-        private CertRequest(Asn1Sequence seq)
+        public static CertRequest GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-            certReqId = DerInteger.GetInstance(seq[0]);
-            certTemplate = CertTemplate.GetInstance(seq[1]);
-            if (seq.Count > 2)
-            {
-                controls = Controls.GetInstance(seq[2]);
-            }
+            return new CertRequest(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
         }
 
-        public static CertRequest GetInstance(object obj)
+        private readonly DerInteger m_certReqId;
+        private readonly CertTemplate m_certTemplate;
+        private readonly Controls m_controls;
+
+        private CertRequest(Asn1Sequence seq)
         {
-            if (obj is CertRequest)
-                return (CertRequest)obj;
+            int count = seq.Count;
+            if (count < 2 || count > 3)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-            if (obj != null)
-                return new CertRequest(Asn1Sequence.GetInstance(obj));
+            int pos = 0;
 
-            return null;
+            m_certReqId = DerInteger.GetInstance(seq[pos++]);
+            m_certTemplate = CertTemplate.GetInstance(seq[pos++]);
+            m_controls = Asn1Utilities.ReadOptional(seq, ref pos, Controls.GetOptional);
+
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-        public CertRequest(
-            int certReqId,
-            CertTemplate certTemplate,
-            Controls controls)
+        public CertRequest(int certReqId, CertTemplate certTemplate, Controls controls)
             : this(new DerInteger(certReqId), certTemplate, controls)
         {
         }
 
-        public CertRequest(
-            DerInteger certReqId,
-            CertTemplate certTemplate,
-            Controls controls)
+        public CertRequest(DerInteger certReqId, CertTemplate certTemplate, Controls controls)
         {
-            this.certReqId = certReqId;
-            this.certTemplate = certTemplate;
-            this.controls = controls;
+            m_certReqId = certReqId ?? throw new ArgumentNullException(nameof(certReqId));
+            m_certTemplate = certTemplate ?? throw new ArgumentNullException(nameof(certTemplate));
+            m_controls = controls;
         }
 
-        public virtual DerInteger CertReqID
-        {
-            get { return certReqId; }
-        }
+        public virtual DerInteger CertReqID => m_certReqId;
 
-        public virtual CertTemplate CertTemplate
-        {
-            get { return certTemplate; }
-        }
+        public virtual CertTemplate CertTemplate => m_certTemplate;
 
-        public virtual Controls Controls
-        {
-            get { return controls; }
-        }
+        public virtual Controls Controls => m_controls;
 
         /**
          * <pre>
@@ -75,8 +68,9 @@ namespace Org.BouncyCastle.Asn1.Crmf
          */
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(certReqId, certTemplate);
-            v.AddOptional(controls);
+            Asn1EncodableVector v = new Asn1EncodableVector(3);
+            v.Add(m_certReqId, m_certTemplate);
+            v.AddOptional(m_controls);
             return new DerSequence(v);
         }
     }