From ea97e77307f5e2b6f119250a1542ccea24446fc7 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 4 Sep 2017 17:30:13 +0700 Subject: Support INTEGER encoding when reading GOST private keys - see https://github.com/bcgit/bc-csharp/pull/86 --- .../cryptopro/GOST3410PublicKeyAlgParameters.cs | 11 ++--------- crypto/src/security/PrivateKeyFactory.cs | 23 ++++++++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-) (limited to 'crypto') 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"); -- cgit 1.4.1