summary refs log tree commit diff
path: root/crypto/src/asn1/ocsp/ResponseData.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/ocsp/ResponseData.cs')
-rw-r--r--crypto/src/asn1/ocsp/ResponseData.cs173
1 files changed, 66 insertions, 107 deletions
diff --git a/crypto/src/asn1/ocsp/ResponseData.cs b/crypto/src/asn1/ocsp/ResponseData.cs
index b79301997..08b80d673 100644
--- a/crypto/src/asn1/ocsp/ResponseData.cs
+++ b/crypto/src/asn1/ocsp/ResponseData.cs
@@ -1,126 +1,85 @@
 using System;
 
 using Org.BouncyCastle.Asn1.X509;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Ocsp
 {
-	public class ResponseData
+    public class ResponseData
 		: Asn1Encodable
 	{
-		private static readonly DerInteger V1 = new DerInteger(0);
-
-		private readonly bool                versionPresent;
-		private readonly DerInteger          version;
-		private readonly ResponderID         responderID;
-		private readonly Asn1GeneralizedTime producedAt;
-		private readonly Asn1Sequence        responses;
-		private readonly X509Extensions      responseExtensions;
-
-		public static ResponseData GetInstance(
-			Asn1TaggedObject	obj,
-			bool				explicitly)
-		{
-			return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
-		}
+        public static ResponseData GetInstance(object obj)
+        {
+			if (obj == null)
+				return null;
+            if (obj is ResponseData responseData)
+                return responseData;
+			return new ResponseData(Asn1Sequence.GetInstance(obj));
+        }
 
-		public static ResponseData GetInstance(
-			object  obj)
-		{
-			if (obj == null || obj is ResponseData)
-			{
-				return (ResponseData)obj;
-			}
+        public static ResponseData GetInstance(Asn1TaggedObject obj, bool explicitly)
+        {
+            return new ResponseData(Asn1Sequence.GetInstance(obj, explicitly));
+        }
 
-			if (obj is Asn1Sequence)
-			{
-				return new ResponseData((Asn1Sequence)obj);
-			}
+        private static readonly DerInteger V1 = new DerInteger(0);
 
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
-		}
+        private readonly DerInteger m_version;
+        private readonly bool m_versionPresent;
+        private readonly ResponderID m_responderID;
+        private readonly Asn1GeneralizedTime m_producedAt;
+        private readonly Asn1Sequence m_responses;
+        private readonly X509Extensions m_responseExtensions;
 
-		public ResponseData(
-			DerInteger          version,
-			ResponderID         responderID,
-            Asn1GeneralizedTime producedAt,
-			Asn1Sequence        responses,
-			X509Extensions      responseExtensions)
-		{
-			this.version = version;
-			this.responderID = responderID;
-			this.producedAt = producedAt;
-			this.responses = responses;
-			this.responseExtensions = responseExtensions;
-		}
+        public ResponseData(ResponderID responderID, Asn1GeneralizedTime producedAt, Asn1Sequence responses,
+            X509Extensions responseExtensions)
+            : this(V1, responderID, producedAt, responses, responseExtensions)
+        {
+        }
 
-		public ResponseData(
-			ResponderID         responderID,
-            Asn1GeneralizedTime producedAt,
-			Asn1Sequence        responses,
-			X509Extensions      responseExtensions)
-			: this(V1, responderID, producedAt, responses, responseExtensions)
-		{
+        public ResponseData(DerInteger version, ResponderID responderID, Asn1GeneralizedTime producedAt,
+            Asn1Sequence responses, X509Extensions responseExtensions)
+        {
+            m_version = version ?? V1;
+			m_versionPresent = false;
+			m_responderID = responderID ?? throw new ArgumentNullException(nameof(responderID));
+			m_producedAt = producedAt ?? throw new ArgumentNullException(nameof(producedAt));
+			m_responses = responses ?? throw new ArgumentNullException(nameof(responses));
+			m_responseExtensions = responseExtensions;
 		}
 
-		private ResponseData(Asn1Sequence seq)
+        private ResponseData(Asn1Sequence seq)
 		{
-			int index = 0;
-
-			Asn1Encodable enc = seq[0];
-			if (enc is Asn1TaggedObject o)
-			{
-				if (o.TagNo == 0)
-				{
-					this.versionPresent = true;
-					this.version = DerInteger.GetInstance(o, true);
-					index++;
-				}
-				else
-				{
-					this.version = V1;
-				}
-			}
-			else
-			{
-				this.version = V1;
-			}
-
-			this.responderID = ResponderID.GetInstance(seq[index++]);
-			this.producedAt = (Asn1GeneralizedTime)seq[index++];
-			this.responses = (Asn1Sequence)seq[index++];
-
-			if (seq.Count > index)
-			{
-				this.responseExtensions = X509Extensions.GetInstance(
-					(Asn1TaggedObject)seq[index], true);
-			}
-		}
+            int count = seq.Count;
+            if (count < 3 || count > 5)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-		public DerInteger Version
-		{
-			get { return version; }
-		}
+            int pos = 0;
 
-		public ResponderID ResponderID
-		{
-			get { return responderID; }
-		}
+            {
+                DerInteger version = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, DerInteger.GetInstance);
 
-		public Asn1GeneralizedTime ProducedAt
-		{
-			get { return producedAt; }
-		}
+                m_version = version ?? V1;
+                m_versionPresent = version != null;
+            }
 
-		public Asn1Sequence Responses
-		{
-			get { return responses; }
-		}
+            m_responderID = ResponderID.GetInstance(seq[pos++]);
+            m_producedAt = Asn1GeneralizedTime.GetInstance(seq[pos++]);
+            m_responses = Asn1Sequence.GetInstance(seq[pos++]);
+            m_responseExtensions = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 1, true, X509Extensions.GetInstance);
 
-		public X509Extensions ResponseExtensions
-		{
-			get { return responseExtensions; }
-		}
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
+        }
+
+        public DerInteger Version => m_version;
+
+		public ResponderID ResponderID => m_responderID;
+
+		public Asn1GeneralizedTime ProducedAt => m_producedAt;
+
+		public Asn1Sequence Responses => m_responses;
+
+		public X509Extensions ResponseExtensions => m_responseExtensions;
 
 		/**
          * Produce an object suitable for an Asn1OutputStream.
@@ -135,15 +94,15 @@ namespace Org.BouncyCastle.Asn1.Ocsp
          */
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(3);
+            Asn1EncodableVector v = new Asn1EncodableVector(5);
 
-            if (versionPresent || !version.Equals(V1))
+            if (m_versionPresent || !V1.Equals(m_version))
             {
-                v.Add(new DerTaggedObject(true, 0, version));
+                v.Add(new DerTaggedObject(true, 0, m_version));
             }
 
-            v.Add(responderID, producedAt, responses);
-            v.AddOptionalTagged(true, 1, responseExtensions);
+            v.Add(m_responderID, m_producedAt, m_responses);
+            v.AddOptionalTagged(true, 1, m_responseExtensions);
             return new DerSequence(v);
         }
     }