summary refs log tree commit diff
path: root/crypto/src/asn1/ocsp/Request.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/ocsp/Request.cs')
-rw-r--r--crypto/src/asn1/ocsp/Request.cs84
1 files changed, 33 insertions, 51 deletions
diff --git a/crypto/src/asn1/ocsp/Request.cs b/crypto/src/asn1/ocsp/Request.cs
index 21121cb28..c6fb6ae47 100644
--- a/crypto/src/asn1/ocsp/Request.cs
+++ b/crypto/src/asn1/ocsp/Request.cs
@@ -1,72 +1,53 @@
 using System;
 
-using Org.BouncyCastle.Asn1;
 using Org.BouncyCastle.Asn1.X509;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Ocsp
 {
     public class Request
         : Asn1Encodable
     {
-        private readonly CertID			reqCert;
-        private readonly X509Extensions	singleRequestExtensions;
-
-		public static Request GetInstance(
-			Asn1TaggedObject	obj,
-			bool				explicitly)
-		{
-			return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
-		}
-
-		public static Request GetInstance(
-			object obj)
-		{
-			if (obj == null || obj is Request)
-			{
-				return (Request)obj;
-			}
+        public static Request GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is Request request)
+                return request;
+            return new Request(Asn1Sequence.GetInstance(obj));
+        }
 
-			if (obj is Asn1Sequence)
-			{
-				return new Request((Asn1Sequence)obj);
-			}
+        public static Request GetInstance(Asn1TaggedObject obj, bool explicitly)
+        {
+            return new Request(Asn1Sequence.GetInstance(obj, explicitly));
+        }
 
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
-		}
+        private readonly CertID m_reqCert;
+        private readonly X509Extensions m_singleRequestExtensions;
 
-		public Request(
-            CertID			reqCert,
-            X509Extensions	singleRequestExtensions)
+        public Request(CertID reqCert, X509Extensions singleRequestExtensions)
         {
-			if (reqCert == null)
-				throw new ArgumentNullException("reqCert");
-
-			this.reqCert = reqCert;
-            this.singleRequestExtensions = singleRequestExtensions;
+			m_reqCert = reqCert ?? throw new ArgumentNullException(nameof(reqCert));
+            m_singleRequestExtensions = singleRequestExtensions;
         }
 
-		private Request(
-			Asn1Sequence seq)
+		private Request(Asn1Sequence seq)
         {
-			reqCert = CertID.GetInstance(seq[0]);
+            int count = seq.Count;
+            if (count < 1 || count > 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
+
+            int pos = 0;
+
+            m_reqCert = CertID.GetInstance(seq[pos++]);
+            m_singleRequestExtensions = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, X509Extensions.GetInstance);
 
-			if (seq.Count == 2)
-            {
-                singleRequestExtensions = X509Extensions.GetInstance(
-					(Asn1TaggedObject)seq[1], true);
-            }
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-		public CertID ReqCert
-		{
-			get { return reqCert; }
-		}
+        public CertID ReqCert => m_reqCert;
 
-		public X509Extensions SingleRequestExtensions
-		{
-			get { return singleRequestExtensions; }
-		}
+        public X509Extensions SingleRequestExtensions => m_singleRequestExtensions;
 
 		/**
          * Produce an object suitable for an Asn1OutputStream.
@@ -78,8 +59,9 @@ namespace Org.BouncyCastle.Asn1.Ocsp
          */
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(reqCert);
-            v.AddOptionalTagged(true, 0, singleRequestExtensions);
+            Asn1EncodableVector v = new Asn1EncodableVector(2);
+            v.Add(m_reqCert);
+            v.AddOptionalTagged(true, 0, m_singleRequestExtensions);
             return new DerSequence(v);
         }
     }