From 24214b3eed3cc1b85ea228e88c42a29f14955f3a Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Tue, 18 Jul 2023 11:16:50 +0700 Subject: Refactoring around PrivateKeyInfo --- crypto/src/security/PrivateKeyFactory.cs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'crypto/src/security') diff --git a/crypto/src/security/PrivateKeyFactory.cs b/crypto/src/security/PrivateKeyFactory.cs index d6fa87943..205a604a8 100644 --- a/crypto/src/security/PrivateKeyFactory.cs +++ b/crypto/src/security/PrivateKeyFactory.cs @@ -148,10 +148,11 @@ namespace Org.BouncyCastle.Security gostParams.DigestParamSet, gostParams.EncryptionParamSet); - Asn1OctetString privEnc = keyInfo.PrivateKeyData; - if (privEnc.GetOctets().Length == 32 || privEnc.GetOctets().Length == 64) + int privateKeyLength = keyInfo.PrivateKeyLength; + + if (privateKeyLength == 32 || privateKeyLength == 64) { - d = new BigInteger(1, privEnc.GetOctets(), bigEndian: false); + d = new BigInteger(1, keyInfo.PrivateKey.GetOctets(), bigEndian: false); } else { @@ -241,10 +242,18 @@ namespace Org.BouncyCastle.Security else if (algOid.Equals(EdECObjectIdentifiers.id_X25519) || algOid.Equals(CryptlibObjectIdentifiers.curvey25519)) { + // Java 11 bug: exact length of X25519/X448 secret used in Java 11 + if (X25519PrivateKeyParameters.KeySize == keyInfo.PrivateKeyLength) + return new X25519PrivateKeyParameters(keyInfo.PrivateKey.GetOctets()); + return new X25519PrivateKeyParameters(GetRawKey(keyInfo)); } else if (algOid.Equals(EdECObjectIdentifiers.id_X448)) { + // Java 11 bug: exact length of X25519/X448 secret used in Java 11 + if (X448PrivateKeyParameters.KeySize == keyInfo.PrivateKeyLength) + return new X448PrivateKeyParameters(keyInfo.PrivateKey.GetOctets()); + return new X448PrivateKeyParameters(GetRawKey(keyInfo)); } else if (algOid.Equals(EdECObjectIdentifiers.id_Ed25519) @@ -277,10 +286,11 @@ namespace Org.BouncyCastle.Security gostParams.DigestParamSet, gostParams.EncryptionParamSet); - Asn1OctetString privEnc = keyInfo.PrivateKeyData; - if (privEnc.GetOctets().Length == 32 || privEnc.GetOctets().Length == 64) + int privateKeyLength = keyInfo.PrivateKeyLength; + + if (privateKeyLength == 32 || privateKeyLength == 64) { - d = new BigInteger(1, privEnc.GetOctets(), bigEndian: false); + d = new BigInteger(1, keyInfo.PrivateKey.GetOctets(), bigEndian: false); } else { -- cgit 1.4.1