summary refs log tree commit diff
path: root/crypto/src/asn1/ocsp/Signature.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/ocsp/Signature.cs')
-rw-r--r--crypto/src/asn1/ocsp/Signature.cs106
1 files changed, 39 insertions, 67 deletions
diff --git a/crypto/src/asn1/ocsp/Signature.cs b/crypto/src/asn1/ocsp/Signature.cs
index c6f149d62..fd77b87b1 100644
--- a/crypto/src/asn1/ocsp/Signature.cs
+++ b/crypto/src/asn1/ocsp/Signature.cs
@@ -1,94 +1,65 @@
 using System;
 
 using Org.BouncyCastle.Asn1.X509;
-using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Ocsp
 {
     public class Signature
         : Asn1Encodable
     {
-        internal AlgorithmIdentifier	signatureAlgorithm;
-        internal DerBitString			signatureValue;
-        internal Asn1Sequence			certs;
-
-		public static Signature GetInstance(
-			Asn1TaggedObject	obj,
-			bool				explicitly)
-		{
-			return GetInstance(Asn1Sequence.GetInstance(obj, explicitly));
+        public static Signature GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is Signature signature)
+                return signature;
+            return new Signature(Asn1Sequence.GetInstance(obj));
 		}
 
-		public static Signature GetInstance(
-			object  obj)
-		{
-			if (obj == null || obj is Signature)
-			{
-				return (Signature)obj;
-			}
-
-			if (obj is Asn1Sequence)
-			{
-				return new Signature((Asn1Sequence)obj);
-			}
+        public static Signature GetInstance(Asn1TaggedObject obj, bool explicitly)
+        {
+            return new Signature(Asn1Sequence.GetInstance(obj, explicitly));
+        }
 
-            throw new ArgumentException("unknown object in factory: " + Platform.GetTypeName(obj), "obj");
-		}
+        private readonly AlgorithmIdentifier m_signatureAlgorithm;
+        private readonly DerBitString m_signatureValue;
+        private readonly Asn1Sequence m_certs;
 
-		public Signature(
-            AlgorithmIdentifier signatureAlgorithm,
-            DerBitString        signatureValue)
-			: this(signatureAlgorithm, signatureValue, null)
+        public Signature(AlgorithmIdentifier signatureAlgorithm, DerBitString signatureValue)
+            : this(signatureAlgorithm, signatureValue, null)
         {
         }
 
-		public Signature(
-            AlgorithmIdentifier	signatureAlgorithm,
-            DerBitString		signatureValue,
-            Asn1Sequence		certs)
+        public Signature(AlgorithmIdentifier signatureAlgorithm, DerBitString signatureValue, Asn1Sequence certs)
         {
-			if (signatureAlgorithm == null)
-				throw new ArgumentException("signatureAlgorithm");
-			if (signatureValue == null)
-				throw new ArgumentException("signatureValue");
-
-			this.signatureAlgorithm = signatureAlgorithm;
-            this.signatureValue = signatureValue;
-            this.certs = certs;
+            m_signatureAlgorithm = signatureAlgorithm ?? throw new ArgumentNullException(nameof(signatureAlgorithm));
+            m_signatureValue = signatureValue ?? throw new ArgumentNullException(nameof(signatureValue));
+            m_certs = certs;
         }
 
-		private Signature(
-            Asn1Sequence seq)
+        private Signature(Asn1Sequence seq)
         {
-            signatureAlgorithm = AlgorithmIdentifier.GetInstance(seq[0]);
-            signatureValue = (DerBitString)seq[1];
+            int count = seq.Count;
+            if (count < 2 || count > 3)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-			if (seq.Count == 3)
-            {
-                certs = Asn1Sequence.GetInstance(
-					(Asn1TaggedObject)seq[2], true);
-            }
+            int pos = 0;
+
+            m_signatureAlgorithm = AlgorithmIdentifier.GetInstance(seq[pos++]);
+            m_signatureValue = DerBitString.GetInstance(seq[pos++]);
+            m_certs = Asn1Utilities.ReadOptionalContextTagged(seq, ref pos, 0, true, Asn1Sequence.GetInstance);
+
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-		public AlgorithmIdentifier SignatureAlgorithm
-		{
-			get { return signatureAlgorithm; }
-		}
+        public AlgorithmIdentifier SignatureAlgorithm => m_signatureAlgorithm;
 
-		public DerBitString SignatureValue
-		{
-			get { return signatureValue; }
-		}
+        public DerBitString SignatureValue => m_signatureValue;
 
-        public byte[] GetSignatureOctets()
-        {
-            return signatureValue.GetOctets();
-        }
+        public byte[] GetSignatureOctets() => m_signatureValue.GetOctets();
 
-        public Asn1Sequence Certs
-		{
-			get { return certs; }
-		}
+        public Asn1Sequence Certs => m_certs;
 
 		/**
          * Produce an object suitable for an Asn1OutputStream.
@@ -101,8 +72,9 @@ namespace Org.BouncyCastle.Asn1.Ocsp
          */
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(signatureAlgorithm, signatureValue);
-            v.AddOptionalTagged(true, 0, certs);
+            Asn1EncodableVector v = new Asn1EncodableVector(3);
+            v.Add(m_signatureAlgorithm, m_signatureValue);
+            v.AddOptionalTagged(true, 0, m_certs);
             return new DerSequence(v);
         }
     }