using System;
using System.Collections;
using System.IO;
using Org.BouncyCastle.Asn1;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Utilities;
namespace Org.BouncyCastle.Tls
{
public abstract class TlsExtensionsUtilities
{
public static IDictionary EnsureExtensionsInitialised(IDictionary extensions)
{
return extensions == null ? Platform.CreateHashtable() : extensions;
}
/// (Int32 -> byte[])
/// an of .
///
public static void AddAlpnExtensionClient(IDictionary extensions, IList protocolNameList)
{
extensions[ExtensionType.application_layer_protocol_negotiation] = CreateAlpnExtensionClient(protocolNameList);
}
///
public static void AddAlpnExtensionServer(IDictionary extensions, ProtocolName protocolName)
{
extensions[ExtensionType.application_layer_protocol_negotiation] = CreateAlpnExtensionServer(protocolName);
}
///
public static void AddCertificateAuthoritiesExtension(IDictionary extensions, IList authorities)
{
extensions[ExtensionType.certificate_authorities] = CreateCertificateAuthoritiesExtension(authorities);
}
///
public static void AddClientCertificateTypeExtensionClient(IDictionary extensions, short[] certificateTypes)
{
extensions[ExtensionType.client_certificate_type] = CreateCertificateTypeExtensionClient(certificateTypes);
}
///
public static void AddClientCertificateTypeExtensionServer(IDictionary extensions, short certificateType)
{
extensions[ExtensionType.client_certificate_type] = CreateCertificateTypeExtensionServer(certificateType);
}
public static void AddClientCertificateUrlExtension(IDictionary extensions)
{
extensions[ExtensionType.client_certificate_url] = CreateClientCertificateUrlExtension();
}
///
public static void AddCookieExtension(IDictionary extensions, byte[] cookie)
{
extensions[ExtensionType.cookie] = CreateCookieExtension(cookie);
}
public static void AddEarlyDataIndication(IDictionary extensions)
{
extensions[ExtensionType.early_data] = CreateEarlyDataIndication();
}
///
public static void AddEarlyDataMaxSize(IDictionary extensions, long maxSize)
{
extensions[ExtensionType.early_data] = CreateEarlyDataMaxSize(maxSize);
}
public static void AddEmptyExtensionData(IDictionary extensions, Int32 extType)
{
extensions[extType] = CreateEmptyExtensionData();
}
public static void AddEncryptThenMacExtension(IDictionary extensions)
{
extensions[ExtensionType.encrypt_then_mac] = CreateEncryptThenMacExtension();
}
public static void AddExtendedMasterSecretExtension(IDictionary extensions)
{
extensions[ExtensionType.extended_master_secret] = CreateExtendedMasterSecretExtension();
}
///
public static void AddHeartbeatExtension(IDictionary extensions, HeartbeatExtension heartbeatExtension)
{
extensions[ExtensionType.heartbeat] = CreateHeartbeatExtension(heartbeatExtension);
}
///
public static void AddKeyShareClientHello(IDictionary extensions, IList clientShares)
{
extensions[ExtensionType.key_share] = CreateKeyShareClientHello(clientShares);
}
///
public static void AddKeyShareHelloRetryRequest(IDictionary extensions, int namedGroup)
{
extensions[ExtensionType.key_share] = CreateKeyShareHelloRetryRequest(namedGroup);
}
///
public static void AddKeyShareServerHello(IDictionary extensions, KeyShareEntry serverShare)
{
extensions[ExtensionType.key_share] = CreateKeyShareServerHello(serverShare);
}
///
public static void AddMaxFragmentLengthExtension(IDictionary extensions, short maxFragmentLength)
{
extensions[ExtensionType.max_fragment_length] = CreateMaxFragmentLengthExtension(maxFragmentLength);
}
///
public static void AddOidFiltersExtension(IDictionary extensions, IDictionary filters)
{
extensions[ExtensionType.oid_filters] = CreateOidFiltersExtension(filters);
}
///
public static void AddPaddingExtension(IDictionary extensions, int dataLength)
{
extensions[ExtensionType.padding] = CreatePaddingExtension(dataLength);
}
public static void AddPostHandshakeAuthExtension(IDictionary extensions)
{
extensions[ExtensionType.post_handshake_auth] = CreatePostHandshakeAuthExtension();
}
///
public static void AddPreSharedKeyClientHello(IDictionary extensions, OfferedPsks offeredPsks)
{
extensions[ExtensionType.pre_shared_key] = CreatePreSharedKeyClientHello(offeredPsks);
}
///
public static void AddPreSharedKeyServerHello(IDictionary extensions, int selectedIdentity)
{
extensions[ExtensionType.pre_shared_key] = CreatePreSharedKeyServerHello(selectedIdentity);
}
///
public static void AddPskKeyExchangeModesExtension(IDictionary extensions, short[] modes)
{
extensions[ExtensionType.psk_key_exchange_modes] = CreatePskKeyExchangeModesExtension(modes);
}
///
public static void AddRecordSizeLimitExtension(IDictionary extensions, int recordSizeLimit)
{
extensions[ExtensionType.record_size_limit] = CreateRecordSizeLimitExtension(recordSizeLimit);
}
///
public static void AddServerCertificateTypeExtensionClient(IDictionary extensions, short[] certificateTypes)
{
extensions[ExtensionType.server_certificate_type] = CreateCertificateTypeExtensionClient(certificateTypes);
}
///
public static void AddServerCertificateTypeExtensionServer(IDictionary extensions, short certificateType)
{
extensions[ExtensionType.server_certificate_type] = CreateCertificateTypeExtensionServer(certificateType);
}
///
public static void AddServerNameExtensionClient(IDictionary extensions, IList serverNameList)
{
extensions[ExtensionType.server_name] = CreateServerNameExtensionClient(serverNameList);
}
///
public static void AddServerNameExtensionServer(IDictionary extensions)
{
extensions[ExtensionType.server_name] = CreateServerNameExtensionServer();
}
///
public static void AddSignatureAlgorithmsExtension(IDictionary extensions, IList supportedSignatureAlgorithms)
{
extensions[ExtensionType.signature_algorithms] = CreateSignatureAlgorithmsExtension(supportedSignatureAlgorithms);
}
///
public static void AddSignatureAlgorithmsCertExtension(IDictionary extensions, IList supportedSignatureAlgorithms)
{
extensions[ExtensionType.signature_algorithms_cert] = CreateSignatureAlgorithmsCertExtension(supportedSignatureAlgorithms);
}
///
public static void AddStatusRequestExtension(IDictionary extensions, CertificateStatusRequest statusRequest)
{
extensions[ExtensionType.status_request] = CreateStatusRequestExtension(statusRequest);
}
///
public static void AddStatusRequestV2Extension(IDictionary extensions, IList statusRequestV2)
{
extensions[ExtensionType.status_request_v2] = CreateStatusRequestV2Extension(statusRequestV2);
}
///
public static void AddSupportedGroupsExtension(IDictionary extensions, IList namedGroups)
{
extensions[ExtensionType.supported_groups] = CreateSupportedGroupsExtension(namedGroups);
}
///
public static void AddSupportedPointFormatsExtension(IDictionary extensions, short[] ecPointFormats)
{
extensions[ExtensionType.ec_point_formats] = CreateSupportedPointFormatsExtension(ecPointFormats);
}
///
public static void AddSupportedVersionsExtensionClient(IDictionary extensions, ProtocolVersion[] versions)
{
extensions[ExtensionType.supported_versions] = CreateSupportedVersionsExtensionClient(versions);
}
///
public static void AddSupportedVersionsExtensionServer(IDictionary extensions, ProtocolVersion selectedVersion)
{
extensions[ExtensionType.supported_versions] = CreateSupportedVersionsExtensionServer(selectedVersion);
}
public static void AddTruncatedHmacExtension(IDictionary extensions)
{
extensions[ExtensionType.truncated_hmac] = CreateTruncatedHmacExtension();
}
///
public static void AddTrustedCAKeysExtensionClient(IDictionary extensions, IList trustedAuthoritiesList)
{
extensions[ExtensionType.trusted_ca_keys] = CreateTrustedCAKeysExtensionClient(trustedAuthoritiesList);
}
public static void AddTrustedCAKeysExtensionServer(IDictionary extensions)
{
extensions[ExtensionType.trusted_ca_keys] = CreateTrustedCAKeysExtensionServer();
}
/// an of .
///
public static IList GetAlpnExtensionClient(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.application_layer_protocol_negotiation);
return extensionData == null ? null : ReadAlpnExtensionClient(extensionData);
}
///
public static ProtocolName GetAlpnExtensionServer(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.application_layer_protocol_negotiation);
return extensionData == null ? null : ReadAlpnExtensionServer(extensionData);
}
///
public static IList GetCertificateAuthoritiesExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.certificate_authorities);
return extensionData == null ? null : ReadCertificateAuthoritiesExtension(extensionData);
}
///
public static short[] GetClientCertificateTypeExtensionClient(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.client_certificate_type);
return extensionData == null ? null : ReadCertificateTypeExtensionClient(extensionData);
}
///
public static short GetClientCertificateTypeExtensionServer(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.client_certificate_type);
return extensionData == null ? (short)-1 : ReadCertificateTypeExtensionServer(extensionData);
}
///
public static byte[] GetCookieExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.cookie);
return extensionData == null ? null : ReadCookieExtension(extensionData);
}
///
public static long GetEarlyDataMaxSize(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.early_data);
return extensionData == null ? -1L : ReadEarlyDataMaxSize(extensionData);
}
///
public static HeartbeatExtension GetHeartbeatExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.heartbeat);
return extensionData == null ? null : ReadHeartbeatExtension(extensionData);
}
///
public static IList GetKeyShareClientHello(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.key_share);
return extensionData == null ? null : ReadKeyShareClientHello(extensionData);
}
///
public static int GetKeyShareHelloRetryRequest(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.key_share);
return extensionData == null ? -1 : ReadKeyShareHelloRetryRequest(extensionData);
}
///
public static KeyShareEntry GetKeyShareServerHello(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.key_share);
return extensionData == null ? null : ReadKeyShareServerHello(extensionData);
}
///
public static short GetMaxFragmentLengthExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.max_fragment_length);
return extensionData == null ? (short)-1 : ReadMaxFragmentLengthExtension(extensionData);
}
///
public static IDictionary GetOidFiltersExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.oid_filters);
return extensionData == null ? null : ReadOidFiltersExtension(extensionData);
}
///
public static int GetPaddingExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.padding);
return extensionData == null ? -1 : ReadPaddingExtension(extensionData);
}
///
public static OfferedPsks GetPreSharedKeyClientHello(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.pre_shared_key);
return extensionData == null ? null : ReadPreSharedKeyClientHello(extensionData);
}
///
public static int GetPreSharedKeyServerHello(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.pre_shared_key);
return extensionData == null ? -1 : ReadPreSharedKeyServerHello(extensionData);
}
///
public static short[] GetPskKeyExchangeModesExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.psk_key_exchange_modes);
return extensionData == null ? null : ReadPskKeyExchangeModesExtension(extensionData);
}
///
public static int GetRecordSizeLimitExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.record_size_limit);
return extensionData == null ? -1 : ReadRecordSizeLimitExtension(extensionData);
}
///
public static short[] GetServerCertificateTypeExtensionClient(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.server_certificate_type);
return extensionData == null ? null : ReadCertificateTypeExtensionClient(extensionData);
}
///
public static short GetServerCertificateTypeExtensionServer(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.server_certificate_type);
return extensionData == null ? (short)-1 : ReadCertificateTypeExtensionServer(extensionData);
}
///
public static IList GetServerNameExtensionClient(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.server_name);
return extensionData == null ? null : ReadServerNameExtensionClient(extensionData);
}
///
public static IList GetSignatureAlgorithmsExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.signature_algorithms);
return extensionData == null ? null : ReadSignatureAlgorithmsExtension(extensionData);
}
///
public static IList GetSignatureAlgorithmsCertExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.signature_algorithms_cert);
return extensionData == null ? null : ReadSignatureAlgorithmsCertExtension(extensionData);
}
///
public static CertificateStatusRequest GetStatusRequestExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.status_request);
return extensionData == null ? null : ReadStatusRequestExtension(extensionData);
}
///
public static IList GetStatusRequestV2Extension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.status_request_v2);
return extensionData == null ? null : ReadStatusRequestV2Extension(extensionData);
}
///
public static int[] GetSupportedGroupsExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.supported_groups);
return extensionData == null ? null : ReadSupportedGroupsExtension(extensionData);
}
///
public static short[] GetSupportedPointFormatsExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.ec_point_formats);
return extensionData == null ? null : ReadSupportedPointFormatsExtension(extensionData);
}
///
public static ProtocolVersion[] GetSupportedVersionsExtensionClient(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.supported_versions);
return extensionData == null ? null : ReadSupportedVersionsExtensionClient(extensionData);
}
///
public static ProtocolVersion GetSupportedVersionsExtensionServer(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.supported_versions);
return extensionData == null ? null : ReadSupportedVersionsExtensionServer(extensionData);
}
///
public static IList GetTrustedCAKeysExtensionClient(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.trusted_ca_keys);
return extensionData == null ? null : ReadTrustedCAKeysExtensionClient(extensionData);
}
///
public static bool HasClientCertificateUrlExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.client_certificate_url);
return extensionData == null ? false : ReadClientCertificateUrlExtension(extensionData);
}
///
public static bool HasEarlyDataIndication(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.early_data);
return extensionData == null ? false : ReadEarlyDataIndication(extensionData);
}
///
public static bool HasEncryptThenMacExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.encrypt_then_mac);
return extensionData == null ? false : ReadEncryptThenMacExtension(extensionData);
}
///
public static bool HasExtendedMasterSecretExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.extended_master_secret);
return extensionData == null ? false : ReadExtendedMasterSecretExtension(extensionData);
}
///
public static bool HasServerNameExtensionServer(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.server_name);
return extensionData == null ? false : ReadServerNameExtensionServer(extensionData);
}
///
public static bool HasPostHandshakeAuthExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.post_handshake_auth);
return extensionData == null ? false : ReadPostHandshakeAuthExtension(extensionData);
}
///
public static bool HasTruncatedHmacExtension(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.truncated_hmac);
return extensionData == null ? false : ReadTruncatedHmacExtension(extensionData);
}
///
public static bool HasTrustedCAKeysExtensionServer(IDictionary extensions)
{
byte[] extensionData = TlsUtilities.GetExtensionData(extensions, ExtensionType.trusted_ca_keys);
return extensionData == null ? false : ReadTrustedCAKeysExtensionServer(extensionData);
}
/// an of .
///
public static byte[] CreateAlpnExtensionClient(IList protocolNameList)
{
if (protocolNameList == null || protocolNameList.Count < 1)
throw new TlsFatalAlert(AlertDescription.internal_error);
MemoryStream buf = new MemoryStream();
// Placeholder for length
TlsUtilities.WriteUint16(0, buf);
foreach (ProtocolName protocolName in protocolNameList)
{
protocolName.Encode(buf);
}
return PatchOpaque16(buf);
}
///
public static byte[] CreateAlpnExtensionServer(ProtocolName protocolName)
{
IList protocol_name_list = Platform.CreateArrayList();
protocol_name_list.Add(protocolName);
return CreateAlpnExtensionClient(protocol_name_list);
}
///
public static byte[] CreateCertificateAuthoritiesExtension(IList authorities)
{
if (null == authorities || authorities.Count < 1)
throw new TlsFatalAlert(AlertDescription.internal_error);
MemoryStream buf = new MemoryStream();
// Placeholder for length
TlsUtilities.WriteUint16(0, buf);
foreach (X509Name authority in authorities)
{
byte[] derEncoding = authority.GetEncoded(Asn1Encodable.Der);
TlsUtilities.WriteOpaque16(derEncoding, buf);
}
return PatchOpaque16(buf);
}
///
public static byte[] CreateCertificateTypeExtensionClient(short[] certificateTypes)
{
if (TlsUtilities.IsNullOrEmpty(certificateTypes) || certificateTypes.Length > 255)
throw new TlsFatalAlert(AlertDescription.internal_error);
return TlsUtilities.EncodeUint8ArrayWithUint8Length(certificateTypes);
}
///
public static byte[] CreateCertificateTypeExtensionServer(short certificateType)
{
return TlsUtilities.EncodeUint8(certificateType);
}
public static byte[] CreateClientCertificateUrlExtension()
{
return CreateEmptyExtensionData();
}
///
public static byte[] CreateCookieExtension(byte[] cookie)
{
if (TlsUtilities.IsNullOrEmpty(cookie) || cookie.Length >= (1 << 16))
throw new TlsFatalAlert(AlertDescription.internal_error);
return TlsUtilities.EncodeOpaque16(cookie);
}
public static byte[] CreateEarlyDataIndication()
{
return CreateEmptyExtensionData();
}
///
public static byte[] CreateEarlyDataMaxSize(long maxSize)
{
return TlsUtilities.EncodeUint32(maxSize);
}
public static byte[] CreateEmptyExtensionData()
{
return TlsUtilities.EmptyBytes;
}
public static byte[] CreateEncryptThenMacExtension()
{
return CreateEmptyExtensionData();
}
public static byte[] CreateExtendedMasterSecretExtension()
{
return CreateEmptyExtensionData();
}
///
public static byte[] CreateHeartbeatExtension(HeartbeatExtension heartbeatExtension)
{
if (heartbeatExtension == null)
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
MemoryStream buf = new MemoryStream();
heartbeatExtension.Encode(buf);
return buf.ToArray();
}
///
public static byte[] CreateKeyShareClientHello(IList clientShares)
{
if (clientShares == null || clientShares.Count < 1)
return TlsUtilities.EncodeUint16(0);
MemoryStream buf = new MemoryStream();
// Placeholder for length
TlsUtilities.WriteUint16(0, buf);
foreach (KeyShareEntry clientShare in clientShares)
{
clientShare.Encode(buf);
}
return PatchOpaque16(buf);
}
///
public static byte[] CreateKeyShareHelloRetryRequest(int namedGroup)
{
return TlsUtilities.EncodeUint16(namedGroup);
}
///
public static byte[] CreateKeyShareServerHello(KeyShareEntry serverShare)
{
if (serverShare == null)
{
throw new TlsFatalAlert(AlertDescription.internal_error);
}
MemoryStream buf = new MemoryStream();
serverShare.Encode(buf);
return buf.ToArray();
}
///
public static byte[] CreateMaxFragmentLengthExtension(short maxFragmentLength)
{
return TlsUtilities.EncodeUint8(maxFragmentLength);
}
///
public static byte[] CreateOidFiltersExtension(IDictionary filters)
{
MemoryStream buf = new MemoryStream();
// Placeholder for length
TlsUtilities.WriteUint16(0, buf);
if (null != filters)
{
foreach (DerObjectIdentifier certificateExtensionOid in filters.Keys)
{
byte[] certificateExtensionValues = (byte[])filters[certificateExtensionOid];
if (null == certificateExtensionOid || null == certificateExtensionValues)
throw new TlsFatalAlert(AlertDescription.internal_error);
byte[] derEncoding = certificateExtensionOid.GetEncoded(Asn1Encodable.Der);
TlsUtilities.WriteOpaque8(derEncoding, buf);
TlsUtilities.WriteOpaque16(certificateExtensionValues, buf);
}
}
return PatchOpaque16(buf);
}
///
public static byte[] CreatePaddingExtension(int dataLength)
{
TlsUtilities.CheckUint16(dataLength);
return new byte[dataLength];
}
public static byte[] CreatePostHandshakeAuthExtension()
{
return CreateEmptyExtensionData();
}
///
public static byte[] CreatePreSharedKeyClientHello(OfferedPsks offeredPsks)
{
if (offeredPsks == null)
throw new TlsFatalAlert(AlertDescription.internal_error);
MemoryStream buf = new MemoryStream();
offeredPsks.Encode(buf);
return buf.ToArray();
}
///
public static byte[] CreatePreSharedKeyServerHello(int selectedIdentity)
{
return TlsUtilities.EncodeUint16(selectedIdentity);
}
///
public static byte[] CreatePskKeyExchangeModesExtension(short[] modes)
{
if (TlsUtilities.IsNullOrEmpty(modes) || modes.Length > 255)
throw new TlsFatalAlert(AlertDescription.internal_error);
return TlsUtilities.EncodeUint8ArrayWithUint8Length(modes);
}
///
public static byte[] CreateRecordSizeLimitExtension(int recordSizeLimit)
{
if (recordSizeLimit < 64)
throw new TlsFatalAlert(AlertDescription.internal_error);
return TlsUtilities.EncodeUint16(recordSizeLimit);
}
///
public static byte[] CreateServerNameExtensionClient(IList serverNameList)
{
if (serverNameList == null)
throw new TlsFatalAlert(AlertDescription.internal_error);
MemoryStream buf = new MemoryStream();
new ServerNameList(serverNameList).Encode(buf);
return buf.ToArray();
}
public static byte[] CreateServerNameExtensionServer()
{
return CreateEmptyExtensionData();
}
///
public static byte[] CreateSignatureAlgorithmsExtension(IList supportedSignatureAlgorithms)
{
MemoryStream buf = new MemoryStream();
TlsUtilities.EncodeSupportedSignatureAlgorithms(supportedSignatureAlgorithms, buf);
return buf.ToArray();
}
///
public static byte[] CreateSignatureAlgorithmsCertExtension(IList supportedSignatureAlgorithms)
{
return CreateSignatureAlgorithmsExtension(supportedSignatureAlgorithms);
}
///
public static byte[] CreateStatusRequestExtension(CertificateStatusRequest statusRequest)
{
if (statusRequest == null)
throw new TlsFatalAlert(AlertDescription.internal_error);
MemoryStream buf = new MemoryStream();
statusRequest.Encode(buf);
return buf.ToArray();
}
///
public static byte[] CreateStatusRequestV2Extension(IList statusRequestV2)
{
if (statusRequestV2 == null || statusRequestV2.Count < 1)
throw new TlsFatalAlert(AlertDescription.internal_error);
MemoryStream buf = new MemoryStream();
// Placeholder for length
TlsUtilities.WriteUint16(0, buf);
foreach (CertificateStatusRequestItemV2 entry in statusRequestV2)
{
entry.Encode(buf);
}
return PatchOpaque16(buf);
}
///
public static byte[] CreateSupportedGroupsExtension(IList namedGroups)
{
if (namedGroups == null || namedGroups.Count < 1)
throw new TlsFatalAlert(AlertDescription.internal_error);
int count = namedGroups.Count;
int[] values = new int[count];
for (int i = 0; i < count; ++i)
{
values[i] = (Int32)namedGroups[i];
}
return TlsUtilities.EncodeUint16ArrayWithUint16Length(values);
}
///
public static byte[] CreateSupportedPointFormatsExtension(short[] ecPointFormats)
{
if (ecPointFormats == null || !Arrays.Contains(ecPointFormats, ECPointFormat.uncompressed))
{
/*
* RFC 4492 5.1. If the Supported Point Formats Extension is indeed sent, it MUST
* contain the value 0 (uncompressed) as one of the items in the list of point formats.
*/
// NOTE: We add it at the start (highest preference)
ecPointFormats = Arrays.Prepend(ecPointFormats, ECPointFormat.uncompressed);
}
return TlsUtilities.EncodeUint8ArrayWithUint8Length(ecPointFormats);
}
///
public static byte[] CreateSupportedVersionsExtensionClient(ProtocolVersion[] versions)
{
if (TlsUtilities.IsNullOrEmpty(versions) || versions.Length > 127)
throw new TlsFatalAlert(AlertDescription.internal_error);
int count = versions.Length;
byte[] data = new byte[1 + count * 2];
TlsUtilities.WriteUint8(count * 2, data, 0);
for (int i = 0; i < count; ++i)
{
TlsUtilities.WriteVersion((ProtocolVersion)versions[i], data, 1 + i * 2);
}
return data;
}
///
public static byte[] CreateSupportedVersionsExtensionServer(ProtocolVersion selectedVersion)
{
return TlsUtilities.EncodeVersion(selectedVersion);
}
public static byte[] CreateTruncatedHmacExtension()
{
return CreateEmptyExtensionData();
}
///
public static byte[] CreateTrustedCAKeysExtensionClient(IList trustedAuthoritiesList)
{
MemoryStream buf = new MemoryStream();
// Placeholder for length
TlsUtilities.WriteUint16(0, buf);
if (trustedAuthoritiesList != null)
{
foreach (TrustedAuthority entry in trustedAuthoritiesList)
{
entry.Encode(buf);
}
}
return PatchOpaque16(buf);
}
public static byte[] CreateTrustedCAKeysExtensionServer()
{
return CreateEmptyExtensionData();
}
///
private static bool ReadEmptyExtensionData(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (extensionData.Length != 0)
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
return true;
}
/// an of .
///
public static IList ReadAlpnExtensionClient(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
MemoryStream buf = new MemoryStream(extensionData);
int length = TlsUtilities.ReadUint16(buf);
if (length != (extensionData.Length - 2))
throw new TlsFatalAlert(AlertDescription.decode_error);
IList protocol_name_list = Platform.CreateArrayList();
while (buf.Position < buf.Length)
{
ProtocolName protocolName = ProtocolName.Parse(buf);
protocol_name_list.Add(protocolName);
}
return protocol_name_list;
}
///
public static ProtocolName ReadAlpnExtensionServer(byte[] extensionData)
{
IList protocol_name_list = ReadAlpnExtensionClient(extensionData);
if (protocol_name_list.Count != 1)
throw new TlsFatalAlert(AlertDescription.decode_error);
return (ProtocolName)protocol_name_list[0];
}
///
public static IList ReadCertificateAuthoritiesExtension(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (extensionData.Length < 5)
throw new TlsFatalAlert(AlertDescription.decode_error);
MemoryStream buf = new MemoryStream(extensionData);
int length = TlsUtilities.ReadUint16(buf);
if (length != (extensionData.Length - 2))
throw new TlsFatalAlert(AlertDescription.decode_error);
IList authorities = Platform.CreateArrayList();
while (buf.Position < buf.Length)
{
byte[] derEncoding = TlsUtilities.ReadOpaque16(buf, 1);
Asn1Object asn1 = TlsUtilities.ReadAsn1Object(derEncoding);
X509Name ca = X509Name.GetInstance(asn1);
TlsUtilities.RequireDerEncoding(ca, derEncoding);
authorities.Add(ca);
}
return authorities;
}
///
public static short[] ReadCertificateTypeExtensionClient(byte[] extensionData)
{
short[] certificateTypes = TlsUtilities.DecodeUint8ArrayWithUint8Length(extensionData);
if (certificateTypes.Length < 1)
throw new TlsFatalAlert(AlertDescription.decode_error);
return certificateTypes;
}
///
public static short ReadCertificateTypeExtensionServer(byte[] extensionData)
{
return TlsUtilities.DecodeUint8(extensionData);
}
///
public static bool ReadClientCertificateUrlExtension(byte[] extensionData)
{
return ReadEmptyExtensionData(extensionData);
}
///
public static byte[] ReadCookieExtension(byte[] extensionData)
{
return TlsUtilities.DecodeOpaque16(extensionData, 1);
}
///
public static bool ReadEarlyDataIndication(byte[] extensionData)
{
return ReadEmptyExtensionData(extensionData);
}
///
public static long ReadEarlyDataMaxSize(byte[] extensionData)
{
return TlsUtilities.DecodeUint32(extensionData);
}
///
public static bool ReadEncryptThenMacExtension(byte[] extensionData)
{
return ReadEmptyExtensionData(extensionData);
}
///
public static bool ReadExtendedMasterSecretExtension(byte[] extensionData)
{
return ReadEmptyExtensionData(extensionData);
}
///
public static HeartbeatExtension ReadHeartbeatExtension(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
MemoryStream buf = new MemoryStream(extensionData, false);
HeartbeatExtension heartbeatExtension = HeartbeatExtension.Parse(buf);
TlsProtocol.AssertEmpty(buf);
return heartbeatExtension;
}
///
public static IList ReadKeyShareClientHello(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
/*
* TODO[tls13] Clients MUST NOT offer multiple KeyShareEntry values for the same group.
* Clients MUST NOT offer any KeyShareEntry values for groups not listed in the client's
* "supported_groups" extension. Servers MAY check for violations of these rules and abort
* the handshake with an "illegal_parameter" alert if one is violated.
*/
MemoryStream buf = new MemoryStream(extensionData, false);
int length = TlsUtilities.ReadUint16(buf);
if (length != (extensionData.Length - 2))
throw new TlsFatalAlert(AlertDescription.decode_error);
IList clientShares = Platform.CreateArrayList();
while (buf.Position < buf.Length)
{
KeyShareEntry clientShare = KeyShareEntry.Parse(buf);
clientShares.Add(clientShare);
}
return clientShares;
}
///
public static int ReadKeyShareHelloRetryRequest(byte[] extensionData)
{
return TlsUtilities.DecodeUint16(extensionData);
}
///
public static KeyShareEntry ReadKeyShareServerHello(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
MemoryStream buf = new MemoryStream(extensionData, false);
KeyShareEntry serverShare = KeyShareEntry.Parse(buf);
TlsProtocol.AssertEmpty(buf);
return serverShare;
}
///
public static short ReadMaxFragmentLengthExtension(byte[] extensionData)
{
return TlsUtilities.DecodeUint8(extensionData);
}
///
public static IDictionary ReadOidFiltersExtension(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (extensionData.Length < 2)
throw new TlsFatalAlert(AlertDescription.decode_error);
MemoryStream buf = new MemoryStream(extensionData, false);
int length = TlsUtilities.ReadUint16(buf);
if (length != (extensionData.Length - 2))
throw new TlsFatalAlert(AlertDescription.decode_error);
IDictionary filters = Platform.CreateHashtable();
while (buf.Position < buf.Length)
{
byte[] derEncoding = TlsUtilities.ReadOpaque8(buf, 1);
Asn1Object asn1 = TlsUtilities.ReadAsn1Object(derEncoding);
DerObjectIdentifier certificateExtensionOid = DerObjectIdentifier.GetInstance(asn1);
TlsUtilities.RequireDerEncoding(certificateExtensionOid, derEncoding);
if (filters.Contains(certificateExtensionOid))
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
byte[] certificateExtensionValues = TlsUtilities.ReadOpaque16(buf);
filters[certificateExtensionOid] = certificateExtensionValues;
}
return filters;
}
///
public static int ReadPaddingExtension(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (!Arrays.AreAllZeroes(extensionData, 0, extensionData.Length))
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
return extensionData.Length;
}
///
public static bool ReadPostHandshakeAuthExtension(byte[] extensionData)
{
return ReadEmptyExtensionData(extensionData);
}
///
public static OfferedPsks ReadPreSharedKeyClientHello(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
MemoryStream buf = new MemoryStream(extensionData, false);
OfferedPsks offeredPsks = OfferedPsks.Parse(buf);
TlsProtocol.AssertEmpty(buf);
return offeredPsks;
}
///
public static int ReadPreSharedKeyServerHello(byte[] extensionData)
{
return TlsUtilities.DecodeUint16(extensionData);
}
///
public static short[] ReadPskKeyExchangeModesExtension(byte[] extensionData)
{
short[] modes = TlsUtilities.DecodeUint8ArrayWithUint8Length(extensionData);
if (modes.Length < 1)
throw new TlsFatalAlert(AlertDescription.decode_error);
return modes;
}
///
public static int ReadRecordSizeLimitExtension(byte[] extensionData)
{
int recordSizeLimit = TlsUtilities.DecodeUint16(extensionData);
if (recordSizeLimit < 64)
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
return recordSizeLimit;
}
///
public static IList ReadServerNameExtensionClient(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
MemoryStream buf = new MemoryStream(extensionData, false);
ServerNameList serverNameList = ServerNameList.Parse(buf);
TlsProtocol.AssertEmpty(buf);
return serverNameList.ServerNames;
}
///
public static bool ReadServerNameExtensionServer(byte[] extensionData)
{
return ReadEmptyExtensionData(extensionData);
}
///
public static IList ReadSignatureAlgorithmsExtension(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
MemoryStream buf = new MemoryStream(extensionData, false);
IList supported_signature_algorithms = TlsUtilities.ParseSupportedSignatureAlgorithms(buf);
TlsProtocol.AssertEmpty(buf);
return supported_signature_algorithms;
}
///
public static IList ReadSignatureAlgorithmsCertExtension(byte[] extensionData)
{
return ReadSignatureAlgorithmsExtension(extensionData);
}
///
public static CertificateStatusRequest ReadStatusRequestExtension(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
MemoryStream buf = new MemoryStream(extensionData, false);
CertificateStatusRequest statusRequest = CertificateStatusRequest.Parse(buf);
TlsProtocol.AssertEmpty(buf);
return statusRequest;
}
///
public static IList ReadStatusRequestV2Extension(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (extensionData.Length < 3)
throw new TlsFatalAlert(AlertDescription.decode_error);
MemoryStream buf = new MemoryStream(extensionData, false);
int length = TlsUtilities.ReadUint16(buf);
if (length != (extensionData.Length - 2))
throw new TlsFatalAlert(AlertDescription.decode_error);
IList statusRequestV2 = Platform.CreateArrayList();
while (buf.Position < buf.Length)
{
CertificateStatusRequestItemV2 entry = CertificateStatusRequestItemV2.Parse(buf);
statusRequestV2.Add(entry);
}
return statusRequestV2;
}
///
public static int[] ReadSupportedGroupsExtension(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
MemoryStream buf = new MemoryStream(extensionData, false);
int length = TlsUtilities.ReadUint16(buf);
if (length < 2 || (length & 1) != 0)
throw new TlsFatalAlert(AlertDescription.decode_error);
int[] namedGroups = TlsUtilities.ReadUint16Array(length / 2, buf);
TlsProtocol.AssertEmpty(buf);
return namedGroups;
}
///
public static short[] ReadSupportedPointFormatsExtension(byte[] extensionData)
{
short[] ecPointFormats = TlsUtilities.DecodeUint8ArrayWithUint8Length(extensionData);
if (!Arrays.Contains(ecPointFormats, ECPointFormat.uncompressed))
{
/*
* RFC 4492 5.1. If the Supported Point Formats Extension is indeed sent, it MUST
* contain the value 0 (uncompressed) as one of the items in the list of point formats.
*/
throw new TlsFatalAlert(AlertDescription.illegal_parameter);
}
return ecPointFormats;
}
///
public static ProtocolVersion[] ReadSupportedVersionsExtensionClient(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (extensionData.Length < 3 || extensionData.Length > 255 || (extensionData.Length & 1) == 0)
throw new TlsFatalAlert(AlertDescription.decode_error);
int length = TlsUtilities.ReadUint8(extensionData, 0);
if (length != (extensionData.Length - 1))
throw new TlsFatalAlert(AlertDescription.decode_error);
int count = length / 2;
ProtocolVersion[] versions = new ProtocolVersion[count];
for (int i = 0; i < count; ++i)
{
versions[i] = TlsUtilities.ReadVersion(extensionData, 1 + i * 2);
}
return versions;
}
///
public static ProtocolVersion ReadSupportedVersionsExtensionServer(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (extensionData.Length != 2)
throw new TlsFatalAlert(AlertDescription.decode_error);
return TlsUtilities.ReadVersion(extensionData, 0);
}
///
public static bool ReadTruncatedHmacExtension(byte[] extensionData)
{
return ReadEmptyExtensionData(extensionData);
}
///
public static IList ReadTrustedCAKeysExtensionClient(byte[] extensionData)
{
if (extensionData == null)
throw new ArgumentNullException("extensionData");
if (extensionData.Length < 2)
throw new TlsFatalAlert(AlertDescription.decode_error);
MemoryStream buf = new MemoryStream(extensionData, false);
int length = TlsUtilities.ReadUint16(buf);
if (length != (extensionData.Length - 2))
throw new TlsFatalAlert(AlertDescription.decode_error);
IList trusted_authorities_list = Platform.CreateArrayList();
while (buf.Position < buf.Length)
{
TrustedAuthority entry = TrustedAuthority.Parse(buf);
trusted_authorities_list.Add(entry);
}
return trusted_authorities_list;
}
///
public static bool ReadTrustedCAKeysExtensionServer(byte[] extensionData)
{
return ReadEmptyExtensionData(extensionData);
}
///
private static byte[] PatchOpaque16(MemoryStream buf)
{
int length = (int)buf.Length - 2;
TlsUtilities.CheckUint16(length);
byte[] extensionData = buf.ToArray();
TlsUtilities.WriteUint16(length, extensionData, 0);
return extensionData;
}
}
}