summary refs log tree commit diff
path: root/crypto/src
diff options
context:
space:
mode:
authorPeter Dettman <peter.dettman@bouncycastle.org>2017-09-04 17:30:13 +0700
committerPeter Dettman <peter.dettman@bouncycastle.org>2017-09-04 17:30:13 +0700
commitea97e77307f5e2b6f119250a1542ccea24446fc7 (patch)
treeba63b4bab444a76ec70ae7663457b6ef5e43b1ad /crypto/src
parentAvoid integer overflow in argument checks (diff)
downloadBouncyCastle.NET-ed25519-ea97e77307f5e2b6f119250a1542ccea24446fc7.tar.xz
Support INTEGER encoding when reading GOST private keys
- see https://github.com/bcgit/bc-csharp/pull/86
Diffstat (limited to 'crypto/src')
-rw-r--r--crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs11
-rw-r--r--crypto/src/security/PrivateKeyFactory.cs23
2 files changed, 17 insertions, 17 deletions
diff --git a/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs b/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs
index 10c45ba4d..ea42a1ec4 100644
--- a/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs
+++ b/crypto/src/asn1/cryptopro/GOST3410PublicKeyAlgParameters.cs
@@ -22,16 +22,9 @@ namespace Org.BouncyCastle.Asn1.CryptoPro
             object obj)
         {
             if (obj == null || obj is Gost3410PublicKeyAlgParameters)
-            {
-                return (Gost3410PublicKeyAlgParameters) obj;
-            }
-
-			if (obj is Asn1Sequence)
-            {
-                return new Gost3410PublicKeyAlgParameters((Asn1Sequence) obj);
-            }
+                return (Gost3410PublicKeyAlgParameters)obj;
 
-            throw new ArgumentException("Invalid GOST3410Parameter: " + Platform.GetTypeName(obj));
+            return new Gost3410PublicKeyAlgParameters(Asn1Sequence.GetInstance((obj)));
         }
 
 		public Gost3410PublicKeyAlgParameters(
diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs
index 8c2ecfdb0..c9e19cc7d 100644
--- a/crypto/src/security/PrivateKeyFactory.cs
+++ b/crypto/src/security/PrivateKeyFactory.cs
@@ -143,8 +143,7 @@ namespace Org.BouncyCastle.Security
 
                 if (privKey is DerInteger)
                 {
-                    // TODO Do we need to pass any parameters here?
-                    ec = new ECPrivateKeyStructure(ecP.N.BitLength, ((DerInteger)privKey).Value);
+                    ec = new ECPrivateKeyStructure(ecP.N.BitLength, ((DerInteger)privKey).PositiveValue);
                 }
                 else
                 {
@@ -155,14 +154,22 @@ namespace Org.BouncyCastle.Security
             }
             else if (algOid.Equals(CryptoProObjectIdentifiers.GostR3410x94))
             {
-                Gost3410PublicKeyAlgParameters gostParams = new Gost3410PublicKeyAlgParameters(
-                    Asn1Sequence.GetInstance(algID.Parameters.ToAsn1Object()));
+                Gost3410PublicKeyAlgParameters gostParams = Gost3410PublicKeyAlgParameters.GetInstance(algID.Parameters);
 
-                DerOctetString derX = (DerOctetString)keyInfo.ParsePrivateKey();
-                BigInteger x = new BigInteger(1, Arrays.Reverse(derX.GetOctets()));
+                Asn1Object privKey = keyInfo.ParsePrivateKey();
+                BigInteger x;
 
-                return new Gost3410PrivateKeyParameters(x, gostParams.PublicKeyParamSet);
-            }
+                if (privKey is DerInteger)
+                {
+                    x = DerInteger.GetInstance(privKey).PositiveValue;
+				}
+                else
+                {
+                    x = new BigInteger(1, Arrays.Reverse(Asn1OctetString.GetInstance(privKey).GetOctets()));
+				}
+
+				return new Gost3410PrivateKeyParameters(x, gostParams.PublicKeyParamSet);
+			}
             else
             {
                 throw new SecurityUtilityException("algorithm identifier in key not recognised");