summary refs log tree commit diff
path: root/crypto/src/asn1/ess/ESSCertID.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/ess/ESSCertID.cs')
-rw-r--r--crypto/src/asn1/ess/ESSCertID.cs98
1 files changed, 42 insertions, 56 deletions
diff --git a/crypto/src/asn1/ess/ESSCertID.cs b/crypto/src/asn1/ess/ESSCertID.cs
index 7e5fe4c1e..d8a09e22e 100644
--- a/crypto/src/asn1/ess/ESSCertID.cs
+++ b/crypto/src/asn1/ess/ESSCertID.cs
@@ -5,72 +5,58 @@ using Org.BouncyCastle.Utilities;
 
 namespace Org.BouncyCastle.Asn1.Ess
 {
-	public class EssCertID
+    public class EssCertID
 		: Asn1Encodable
 	{
-		private Asn1OctetString certHash;
-		private IssuerSerial issuerSerial;
+        public static EssCertID GetInstance(object o)
+        {
+            if (o == null)
+                return null;
+            if (o is EssCertID essCertID)
+                return essCertID;
+#pragma warning disable CS0618 // Type or member is obsolete
+            return new EssCertID(Asn1Sequence.GetInstance(o));
+#pragma warning restore CS0618 // Type or member is obsolete
+        }
 
-		public static EssCertID GetInstance(
-			object o)
-		{
-			if (o == null || o is EssCertID)
-			{
-				return (EssCertID) o;
-			}
-
-			if (o is Asn1Sequence)
-			{
-				return new EssCertID((Asn1Sequence) o);
-			}
+        public static EssCertID GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
+        {
+#pragma warning disable CS0618 // Type or member is obsolete
+            return new EssCertID(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
+#pragma warning restore CS0618 // Type or member is obsolete
+        }
 
-			throw new ArgumentException(
-				"unknown object in 'EssCertID' factory : "
-                + Platform.GetTypeName(o) + ".");
-		}
+        private readonly Asn1OctetString m_certHash;
+        private readonly IssuerSerial m_issuerSerial;
 
-		/**
-		 * constructor
-		 */
-		public EssCertID(
-			Asn1Sequence seq)
+        [Obsolete("Use 'GetInstance' instead")]
+        public EssCertID(Asn1Sequence seq)
 		{
-			if (seq.Count < 1 || seq.Count > 2)
-			{
-				throw new ArgumentException("Bad sequence size: " + seq.Count);
-			}
+            int count = seq.Count, pos = 0;
+            if (count < 1 || count > 2)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-			this.certHash = Asn1OctetString.GetInstance(seq[0]);
+            m_certHash = Asn1OctetString.GetInstance(seq[pos++]);
+            m_issuerSerial = Asn1Utilities.ReadOptional(seq, ref pos, IssuerSerial.GetOptional);
 
-			if (seq.Count > 1)
-			{
-				issuerSerial = IssuerSerial.GetInstance(seq[1]);
-			}
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
 		}
 
-		public EssCertID(
-			byte[] hash)
-		{
-			certHash = new DerOctetString(hash);
-		}
+        public EssCertID(byte[] hash)
+            : this(hash, null)
+        {
+        }
 
-		public EssCertID(
-			byte[]			hash,
-			IssuerSerial	issuerSerial)
-		{
-			this.certHash = new DerOctetString(hash);
-			this.issuerSerial = issuerSerial;
-		}
+        public EssCertID(byte[] hash, IssuerSerial issuerSerial)
+        {
+            m_certHash = new DerOctetString(hash);
+            m_issuerSerial = issuerSerial;
+        }
 
-		public byte[] GetCertHash()
-		{
-			return certHash.GetOctets();
-		}
+        public byte[] GetCertHash() => Arrays.Clone(m_certHash.GetOctets());
 
-		public IssuerSerial IssuerSerial
-		{
-			get { return issuerSerial; }
-		}
+        public IssuerSerial IssuerSerial => m_issuerSerial;
 
 		/**
 		 * <pre>
@@ -81,9 +67,9 @@ namespace Org.BouncyCastle.Asn1.Ess
 		 */
 		public override Asn1Object ToAsn1Object()
 		{
-			Asn1EncodableVector v = new Asn1EncodableVector(certHash);
-            v.AddOptional(issuerSerial);
-			return new DerSequence(v);
+			return m_issuerSerial == null
+				?  new DerSequence(m_certHash)
+				:  new DerSequence(m_certHash, m_issuerSerial);
 		}
 	}
 }