From 20ed63bdf95801371d276eab7d91775adbc451d3 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Fri, 17 Nov 2023 10:39:02 +0700 Subject: Refactoring in Tls --- crypto/src/tls/ByteQueue.cs | 2 +- crypto/src/tls/crypto/impl/TlsSuiteHmac.cs | 4 +++- crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs | 8 +++++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/crypto/src/tls/ByteQueue.cs b/crypto/src/tls/ByteQueue.cs index a92f79baf..6d29a2d3e 100644 --- a/crypto/src/tls/ByteQueue.cs +++ b/crypto/src/tls/ByteQueue.cs @@ -164,7 +164,7 @@ namespace Org.BouncyCastle.Tls if ((m_available - skip) < buffer.Length) throw new InvalidOperationException("Not enough data to read"); - buffer.CopyFrom(m_databuf.AsSpan(m_skipped + skip)); + m_databuf.AsSpan(m_skipped + skip, buffer.Length).CopyTo(buffer); } #endif diff --git a/crypto/src/tls/crypto/impl/TlsSuiteHmac.cs b/crypto/src/tls/crypto/impl/TlsSuiteHmac.cs index f008fc759..3e4b6f0f2 100644 --- a/crypto/src/tls/crypto/impl/TlsSuiteHmac.cs +++ b/crypto/src/tls/crypto/impl/TlsSuiteHmac.cs @@ -66,7 +66,9 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl int msgLen) { #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - return CalculateMac(seqNo, type, Spans.FromNullableReadOnly(connectionID), msg.AsSpan(msgOff, msgLen)); + var connIDSpan = connectionID == null ? Span.Empty : connectionID.AsSpan(); + + return CalculateMac(seqNo, type, connIDSpan, msg.AsSpan(msgOff, msgLen)); #else ProtocolVersion serverVersion = m_cryptoParams.ServerVersion; diff --git a/crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs b/crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs index 39df32ed8..0686d8ece 100644 --- a/crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs +++ b/crypto/src/tls/crypto/impl/bc/BcTlsCrypto.cs @@ -156,8 +156,14 @@ namespace Org.BouncyCastle.Tls.Crypto.Impl.BC public override TlsNonceGenerator CreateNonceGenerator(byte[] additionalSeedMaterial) { + // TODO[api] Require non-null additionalSeedMaterial + //if (additionalSeedMaterial == null) + // throw new ArgumentNullException(nameof(additionalSeedMaterial)); + #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - return CreateNonceGenerator(Spans.FromNullableReadOnly(additionalSeedMaterial)); + var seed = additionalSeedMaterial == null ? Span.Empty : additionalSeedMaterial.AsSpan(); + + return CreateNonceGenerator(seed); #else int cryptoHashAlgorithm = CryptoHashAlgorithm.sha256; IDigest digest = CreateDigest(cryptoHashAlgorithm); -- cgit 1.4.1