diff options
author | David Hook <dgh@cryptoworkshop.com> | 2020-04-25 13:11:33 +1000 |
---|---|---|
committer | David Hook <dgh@cryptoworkshop.com> | 2020-04-25 13:11:33 +1000 |
commit | fac7ac9bfe82e7c4eedf5a7b26bed58e49fa988b (patch) | |
tree | 229da03984ecbf55afbd25707ccee29e2523557b | |
parent | fixed typo (diff) | |
download | BouncyCastle.NET-ed25519-fac7ac9bfe82e7c4eedf5a7b26bed58e49fa988b.tar.xz |
github #237 - gost 2012 parsing
-rw-r--r-- | crypto/src/asn1/pkcs/PrivateKeyInfo.cs | 5 | ||||
-rw-r--r-- | crypto/src/security/PrivateKeyFactory.cs | 20 | ||||
-rw-r--r-- | crypto/test/src/security/test/TestEncodings.cs | 13 |
3 files changed, 33 insertions, 5 deletions
diff --git a/crypto/src/asn1/pkcs/PrivateKeyInfo.cs b/crypto/src/asn1/pkcs/PrivateKeyInfo.cs index ba9ef6a53..c5c7c3a2f 100644 --- a/crypto/src/asn1/pkcs/PrivateKeyInfo.cs +++ b/crypto/src/asn1/pkcs/PrivateKeyInfo.cs @@ -167,6 +167,11 @@ namespace Org.BouncyCastle.Asn1.Pkcs get { return privateKeyAlgorithm; } } + public virtual Asn1OctetString PrivateKeyData + { + get { return privateKey; } + } + public virtual Asn1Object ParsePrivateKey() { return Asn1Object.FromByteArray(privateKey.GetOctets()); diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs index b72ece365..4c7a91ecf 100644 --- a/crypto/src/security/PrivateKeyFactory.cs +++ b/crypto/src/security/PrivateKeyFactory.cs @@ -206,15 +206,25 @@ namespace Org.BouncyCastle.Security gostParams.PublicKeyParamSet, gostParams.DigestParamSet, gostParams.EncryptionParamSet); - Asn1Encodable privKey = keyInfo.ParsePrivateKey(); - if (privKey is DerInteger) + + Asn1OctetString privEnc = keyInfo.PrivateKeyData; + if (privEnc.GetOctets().Length == 32 || privEnc.GetOctets().Length == 64) { - d = DerInteger.GetInstance(privKey).PositiveValue; + byte[] dVal = Arrays.Reverse(privEnc.GetOctets()); + d = new BigInteger(1, dVal); } else { - byte[] dVal = Arrays.Reverse(Asn1OctetString.GetInstance(privKey).GetOctets()); - d = new BigInteger(1, dVal); + Asn1Encodable privKey = keyInfo.ParsePrivateKey(); + if (privKey is DerInteger) + { + d = DerInteger.GetInstance(privKey).PositiveValue; + } + else + { + byte[] dVal = Arrays.Reverse(Asn1OctetString.GetInstance(privKey).GetOctets()); + d = new BigInteger(1, dVal); + } } } else diff --git a/crypto/test/src/security/test/TestEncodings.cs b/crypto/test/src/security/test/TestEncodings.cs index a70d5b5b7..91e1375dd 100644 --- a/crypto/test/src/security/test/TestEncodings.cs +++ b/crypto/test/src/security/test/TestEncodings.cs @@ -179,5 +179,18 @@ namespace Org.BouncyCastle.Security.Tests Assert.IsTrue(true, "DSATest worked."); } + + [Test] + public void TestGost2012() + { + byte[] data = Base64.Decode( + "MIGiAgEAMCEGCCqFAwcBAQECMBUGCSqFAwcBAgECAQYIKoUDBwEBAgMEQIXnWrZ6" + + "ajvbCU6x9jK49PgQqCP00T/lW3laXCXueMF8X4Q1y3N9zfOJT2s/IgyPJVrUhgtO" + + "1Akp+Roh8bCPPlqgODA2BggqhQMCCQMIATEqBCi72ZvrBVW6mFL/bQeXeMTf8Jh8" + + "p/diI7Cg8ig4mXg3tsIUf4vBi61b"); + + var keyInfo = PrivateKeyInfo.GetInstance(data); + var akp = Org.BouncyCastle.Security.PrivateKeyFactory.CreateKey(keyInfo); + } } } |