summary refs log tree commit diff
path: root/crypto/src/asn1/pkcs/RSAPrivateKeyStructure.cs
diff options
context:
space:
mode:
Diffstat (limited to 'crypto/src/asn1/pkcs/RSAPrivateKeyStructure.cs')
-rw-r--r--crypto/src/asn1/pkcs/RSAPrivateKeyStructure.cs130
1 files changed, 55 insertions, 75 deletions
diff --git a/crypto/src/asn1/pkcs/RSAPrivateKeyStructure.cs b/crypto/src/asn1/pkcs/RSAPrivateKeyStructure.cs
index 0e7911163..afd642255 100644
--- a/crypto/src/asn1/pkcs/RSAPrivateKeyStructure.cs
+++ b/crypto/src/asn1/pkcs/RSAPrivateKeyStructure.cs
@@ -7,29 +7,29 @@ namespace Org.BouncyCastle.Asn1.Pkcs
     public class RsaPrivateKeyStructure
         : Asn1Encodable
     {
-        private readonly BigInteger modulus;
-        private readonly BigInteger publicExponent;
-        private readonly BigInteger privateExponent;
-        private readonly BigInteger prime1;
-        private readonly BigInteger prime2;
-        private readonly BigInteger exponent1;
-        private readonly BigInteger exponent2;
-        private readonly BigInteger coefficient;
-
-        public static RsaPrivateKeyStructure GetInstance(Asn1TaggedObject obj, bool isExplicit)
-        {
-            return GetInstance(Asn1Sequence.GetInstance(obj, isExplicit));
-        }
-
         public static RsaPrivateKeyStructure GetInstance(object obj)
         {
             if (obj == null)
                 return null;
-            if (obj is RsaPrivateKeyStructure)
-                return (RsaPrivateKeyStructure)obj;
+            if (obj is RsaPrivateKeyStructure rsaPrivateKeyStructure)
+                return rsaPrivateKeyStructure;
             return new RsaPrivateKeyStructure(Asn1Sequence.GetInstance(obj));
         }
 
+        public static RsaPrivateKeyStructure GetInstance(Asn1TaggedObject obj, bool isExplicit)
+        {
+            return new RsaPrivateKeyStructure(Asn1Sequence.GetInstance(obj, isExplicit));
+        }
+
+        private readonly BigInteger m_modulus;
+        private readonly BigInteger m_publicExponent;
+        private readonly BigInteger m_privateExponent;
+        private readonly BigInteger m_prime1;
+        private readonly BigInteger m_prime2;
+        private readonly BigInteger m_exponent1;
+        private readonly BigInteger m_exponent2;
+        private readonly BigInteger m_coefficient;
+
         public RsaPrivateKeyStructure(
             BigInteger modulus,
             BigInteger publicExponent,
@@ -40,71 +40,51 @@ namespace Org.BouncyCastle.Asn1.Pkcs
             BigInteger exponent2,
             BigInteger coefficient)
         {
-            this.modulus = modulus;
-            this.publicExponent = publicExponent;
-            this.privateExponent = privateExponent;
-            this.prime1 = prime1;
-            this.prime2 = prime2;
-            this.exponent1 = exponent1;
-            this.exponent2 = exponent2;
-            this.coefficient = coefficient;
+            m_modulus = modulus ?? throw new ArgumentNullException(nameof(modulus));
+            m_publicExponent = publicExponent ?? throw new ArgumentNullException(nameof(publicExponent));
+            m_privateExponent = privateExponent ?? throw new ArgumentNullException(nameof(privateExponent));
+            m_prime1 = prime1 ?? throw new ArgumentNullException(nameof(prime1));
+            m_prime2 = prime2 ?? throw new ArgumentNullException(nameof(prime2));
+            m_exponent1 = exponent1 ?? throw new ArgumentNullException(nameof(exponent1));
+            m_exponent2 = exponent2 ?? throw new ArgumentNullException(nameof(exponent2));
+            m_coefficient = coefficient ?? throw new ArgumentNullException(nameof(coefficient));
         }
 
         private RsaPrivateKeyStructure(Asn1Sequence seq)
         {
-            BigInteger version = ((DerInteger)seq[0]).Value;
-            if (version.IntValue != 0)
+            int count = seq.Count;
+            if (count != 9)
+                throw new ArgumentException("Bad sequence size: " + count, nameof(seq));
+
+            var version = DerInteger.GetInstance(seq[0]);
+            m_modulus = DerInteger.GetInstance(seq[1]).Value;
+            m_publicExponent = DerInteger.GetInstance(seq[2]).Value;
+            m_privateExponent = DerInteger.GetInstance(seq[3]).Value;
+            m_prime1 = DerInteger.GetInstance(seq[4]).Value;
+            m_prime2 = DerInteger.GetInstance(seq[5]).Value;
+            m_exponent1 = DerInteger.GetInstance(seq[6]).Value;
+            m_exponent2 = DerInteger.GetInstance(seq[7]).Value;
+            m_coefficient = DerInteger.GetInstance(seq[8]).Value;
+
+            if (!version.HasValue(0))
                 throw new ArgumentException("wrong version for RSA private key");
-
-            modulus = ((DerInteger)seq[1]).Value;
-            publicExponent = ((DerInteger)seq[2]).Value;
-            privateExponent = ((DerInteger)seq[3]).Value;
-            prime1 = ((DerInteger)seq[4]).Value;
-            prime2 = ((DerInteger)seq[5]).Value;
-            exponent1 = ((DerInteger)seq[6]).Value;
-            exponent2 = ((DerInteger)seq[7]).Value;
-            coefficient = ((DerInteger)seq[8]).Value;
         }
 
-        public BigInteger Modulus
-        {
-            get { return modulus; }
-        }
+        public BigInteger Modulus => m_modulus;
 
-        public BigInteger PublicExponent
-        {
-            get { return publicExponent; }
-        }
+        public BigInteger PublicExponent => m_publicExponent;
 
-        public BigInteger PrivateExponent
-        {
-            get { return privateExponent; }
-        }
+        public BigInteger PrivateExponent => m_privateExponent;
 
-        public BigInteger Prime1
-        {
-            get { return prime1; }
-        }
+        public BigInteger Prime1 => m_prime1;
 
-        public BigInteger Prime2
-        {
-            get { return prime2; }
-        }
+        public BigInteger Prime2 => m_prime2;
 
-        public BigInteger Exponent1
-        {
-            get { return exponent1; }
-        }
+        public BigInteger Exponent1 => m_exponent1;
 
-        public BigInteger Exponent2
-        {
-            get { return exponent2; }
-        }
+        public BigInteger Exponent2 => m_exponent2;
 
-        public BigInteger Coefficient
-        {
-            get { return coefficient; }
-        }
+        public BigInteger Coefficient => m_coefficient;
 
         /**
          * This outputs the key in Pkcs1v2 format.
@@ -129,14 +109,14 @@ namespace Org.BouncyCastle.Asn1.Pkcs
         {
             return new DerSequence(
                 DerInteger.Zero, // version
-                new DerInteger(Modulus),
-                new DerInteger(PublicExponent),
-                new DerInteger(PrivateExponent),
-                new DerInteger(Prime1),
-                new DerInteger(Prime2),
-                new DerInteger(Exponent1),
-                new DerInteger(Exponent2),
-                new DerInteger(Coefficient));
+                new DerInteger(m_modulus),
+                new DerInteger(m_publicExponent),
+                new DerInteger(m_privateExponent),
+                new DerInteger(m_prime1),
+                new DerInteger(m_prime2),
+                new DerInteger(m_exponent1),
+                new DerInteger(m_exponent2),
+                new DerInteger(m_coefficient));
         }
     }
 }