TlsImplUtilities
Useful utility methods.
using Org.BouncyCastle.Utilities;
using System;
namespace Org.BouncyCastle.Tls.Crypto.Impl
{
public abstract class TlsImplUtilities
{
public static bool IsSsl(TlsCryptoParameters cryptoParams)
{
return cryptoParams.ServerVersion.IsSsl;
}
public static bool IsTlsV10(ProtocolVersion version)
{
return ProtocolVersion.TLSv10.IsEqualOrEarlierVersionOf(version.GetEquivalentTlsVersion());
}
public static bool IsTlsV10(TlsCryptoParameters cryptoParams)
{
return IsTlsV10(cryptoParams.ServerVersion);
}
public static bool IsTlsV11(ProtocolVersion version)
{
return ProtocolVersion.TLSv11.IsEqualOrEarlierVersionOf(version.GetEquivalentTlsVersion());
}
public static bool IsTlsV11(TlsCryptoParameters cryptoParams)
{
return IsTlsV11(cryptoParams.ServerVersion);
}
public static bool IsTlsV12(ProtocolVersion version)
{
return ProtocolVersion.TLSv12.IsEqualOrEarlierVersionOf(version.GetEquivalentTlsVersion());
}
public static bool IsTlsV12(TlsCryptoParameters cryptoParams)
{
return IsTlsV12(cryptoParams.ServerVersion);
}
public static bool IsTlsV13(ProtocolVersion version)
{
return ProtocolVersion.TLSv13.IsEqualOrEarlierVersionOf(version.GetEquivalentTlsVersion());
}
public static bool IsTlsV13(TlsCryptoParameters cryptoParams)
{
return IsTlsV13(cryptoParams.ServerVersion);
}
public static byte[] CalculateKeyBlock(TlsCryptoParameters cryptoParams, int length)
{
SecurityParameters securityParameters = cryptoParams.SecurityParameters;
TlsSecret masterSecret = securityParameters.MasterSecret;
int prfAlgorithm = securityParameters.PrfAlgorithm;
byte[] seed = Arrays.Concatenate(securityParameters.ServerRandom, securityParameters.ClientRandom);
return masterSecret.DeriveUsingPrf(prfAlgorithm, "key expansion", seed, length).Extract();
}
public unsafe static void CalculateKeyBlock(TlsCryptoParameters cryptoParams, Span<byte> keyBlock)
{
SecurityParameters securityParameters = cryptoParams.SecurityParameters;
TlsSecret masterSecret = securityParameters.MasterSecret;
int prfAlgorithm = securityParameters.PrfAlgorithm;
Span<byte> span = securityParameters.ClientRandom;
Span<byte> span2 = securityParameters.ServerRandom;
int num = span2.Length + span.Length;
Span<byte> span3 = new Span<byte>(stackalloc byte[(int)(uint)num], num);
span2.CopyTo(span3);
num = span2.Length;
span.CopyTo(span3.Slice(num, span3.Length - num));
masterSecret.DeriveUsingPrf(prfAlgorithm, "key expansion", span3, keyBlock.Length).ExtractTo(keyBlock);
}
}
}