summary refs log tree commit diff
path: root/crypto/src/asn1/pkcs/DHParameter.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/pkcs/DHParameter.cs')
-rw-r--r--crypto/src/asn1/pkcs/DHParameter.cs76
1 files changed, 41 insertions, 35 deletions
diff --git a/crypto/src/asn1/pkcs/DHParameter.cs b/crypto/src/asn1/pkcs/DHParameter.cs
index 7a07a18b0..43a6129d4 100644
--- a/crypto/src/asn1/pkcs/DHParameter.cs
+++ b/crypto/src/asn1/pkcs/DHParameter.cs
@@ -1,3 +1,5 @@
+using System;
+
 using Org.BouncyCastle.Math;
 
 namespace Org.BouncyCastle.Asn1.Pkcs
@@ -5,59 +7,63 @@ namespace Org.BouncyCastle.Asn1.Pkcs
     public class DHParameter
         : Asn1Encodable
     {
-        internal DerInteger p, g, l;
+        public static DHParameter GetInstance(object obj)
+        {
+            if (obj == null)
+                return null;
+            if (obj is DHParameter dhParameter)
+                return dhParameter;
+#pragma warning disable CS0618 // Type or member is obsolete
+            return new DHParameter(Asn1Sequence.GetInstance(obj));
+#pragma warning restore CS0618 // Type or member is obsolete
+        }
 
-		public DHParameter(
-            BigInteger	p,
-            BigInteger	g,
-            int			l)
+        public static DHParameter GetInstance(Asn1TaggedObject taggedObject, bool declaredExplicit)
         {
-            this.p = new DerInteger(p);
-            this.g = new DerInteger(g);
+#pragma warning disable CS0618 // Type or member is obsolete
+            return new DHParameter(Asn1Sequence.GetInstance(taggedObject, declaredExplicit));
+#pragma warning restore CS0618 // Type or member is obsolete
+        }
+
+        private readonly DerInteger m_p, m_g, m_l;
+
+        public DHParameter(BigInteger p, BigInteger g, int l)
+        {
+            m_p = new DerInteger(p);
+            m_g = new DerInteger(g);
 
 			if (l != 0)
             {
-                this.l = new DerInteger(l);
+                m_l = new DerInteger(l);
             }
         }
 
-		public DHParameter(
-            Asn1Sequence seq)
+        [Obsolete("Use 'GetInstance' instead")]
+        public DHParameter(Asn1Sequence seq)
         {
-            var e = seq.GetEnumerator();
-
-			e.MoveNext();
-            p = (DerInteger)e.Current;
+            int count = seq.Count, pos = 0;
+            if (count < 2 || count > 3)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
 
-			e.MoveNext();
-            g = (DerInteger)e.Current;
+            m_p = DerInteger.GetInstance(seq[pos++]);
+            m_g = DerInteger.GetInstance(seq[pos++]);
+            m_l = Asn1Utilities.ReadOptional(seq, ref pos, DerInteger.GetOptional);
 
-			if (e.MoveNext())
-            {
-                l = (DerInteger) e.Current;
-            }
+            if (pos != count)
+                throw new ArgumentException("Unexpected elements in sequence", nameof(seq));
         }
 
-		public BigInteger P
-		{
-			get { return p.PositiveValue; }
-		}
+        public BigInteger P => m_p.PositiveValue;
 
-		public BigInteger G
-		{
-			get { return g.PositiveValue; }
-		}
+        public BigInteger G => m_g.PositiveValue;
 
-		public BigInteger L
-		{
-            get { return l == null ? null : l.PositiveValue; }
-        }
+        public BigInteger L => m_l?.PositiveValue;
 
         public override Asn1Object ToAsn1Object()
         {
-            Asn1EncodableVector v = new Asn1EncodableVector(p, g);
-            v.AddOptional(l);
-            return new DerSequence(v);
+            return m_l == null
+                ?  new DerSequence(m_p, m_g)
+                :  new DerSequence(m_p, m_g, m_l);
         }
     }
 }