Skip to content

Commit

Permalink
Generate SslConnectionInfo.Unix.cs with T4 template (#84690)
Browse files Browse the repository at this point in the history
* Generate SslConnectionInfo.Unix.cs with T4 template

* Try fitting into ushort
  • Loading branch information
am11 authored Apr 17, 2023
1 parent 0099ffa commit f92b9ef
Show file tree
Hide file tree
Showing 7 changed files with 500 additions and 3,526 deletions.
16 changes: 7 additions & 9 deletions src/libraries/System.Net.Security/src/System.Net.Security.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,32 +122,32 @@
<None Include="System\Net\Security\TlsCipherSuiteNameParser.ttinclude" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != '' and '$(TargetPlatformIdentifier)' != 'windows'">
<Compile Include="System\Net\Security\TlsCipherSuiteData.Lookup.cs">
<Compile Include="System\Net\Security\SslConnectionInfo.Unix.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>TlsCipherSuiteData.Lookup.tt</DependentUpon>
<DependentUpon>SslConnectionInfo.Unix.tt</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
<None Include="System\Net\Security\TlsCipherSuiteData.Lookup.cs">
<None Include="System\Net\Security\SslConnectionInfo.Unix.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>TlsCipherSuiteData.Lookup.tt</DependentUpon>
<DependentUpon>SslConnectionInfo.Unix.tt</DependentUpon>
</None>
</ItemGroup>
<ItemGroup Condition="'$(AllowTlsCipherSuiteGeneration)' == 'true'">
<None Include="System\Net\Security\TlsCipherSuite.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>TlsCipherSuite.cs</LastGenOutput>
</None>
<None Include="System\Net\Security\TlsCipherSuiteData.Lookup.tt">
<None Include="System\Net\Security\SslConnectionInfo.Unix.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>TlsCipherSuiteData.Lookup.cs</LastGenOutput>
<LastGenOutput>SslConnectionInfo.Unix.cs</LastGenOutput>
</None>
</ItemGroup>
<ItemGroup Condition="'$(AllowTlsCipherSuiteGeneration)' != 'true'">
<None Include="System\Net\Security\TlsCipherSuite.tt" />
<None Include="System\Net\Security\TlsCipherSuiteData.Lookup.tt" />
<None Include="System\Net\Security\SslConnectionInfo.Unix.tt" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'windows'">
<Compile Include="System\Net\CertificateValidationPal.Windows.cs" />
Expand Down Expand Up @@ -285,8 +285,6 @@
Link="Common\System\Net\Security\Unix\SecChannelBindings.cs" />
<Compile Include="System\Net\Security\Pal.Managed\EndpointChannelBindingToken.cs" />
<Compile Include="System\Net\Security\Pal.Managed\SafeChannelBindingHandle.cs" />
<Compile Include="System\Net\Security\SslConnectionInfo.Unix.cs" />
<Compile Include="System\Net\Security\TlsCipherSuiteData.cs" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' != '' and '$(TargetPlatformIdentifier)' != 'windows' and '$(UseManagedNtlm)' != 'true'">
<Compile Include="$(CommonPath)Microsoft\Win32\SafeHandles\GssSafeHandles.cs"
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ import namespace="System" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Net.Primitives" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
<#@ include file="TlsCipherSuiteNameParser.ttinclude" #><#@
include file="TlsCipherSuite.cs" #>
<# Array tlsEnumValues = typeof(TlsCipherSuite).GetEnumValues(); #>
<# Array exchangeEnumValues = typeof(ExchangeAlgorithmTypeIndex).GetEnumValues(); #>
<# Array dataCipherAlgs = typeof(CipherAlgorithmTypeIndex).GetEnumValues(); #>
<# Array dataHashAlgs = typeof(HashAlgorithmTypeIndex).GetEnumValues(); #>


using System.Diagnostics;
using System.Security.Authentication;

namespace System.Net.Security
{
internal partial struct SslConnectionInfo
{
private void MapCipherSuite(TlsCipherSuite cipherSuite)
{
TlsCipherSuite = cipherSuite;
KeyExchKeySize = 0;
ReadOnlySpan<int> keyExchangeAlgs =
new[] { <#
foreach (ExchangeAlgorithmTypeIndex val in exchangeEnumValues)
{
#>(int)ExchangeAlgorithmType.<#= val #>, <#
}
#>};
ReadOnlySpan<int> dataCipherAlgs =
new[] { <#
foreach (CipherAlgorithmTypeIndex val in dataCipherAlgs)
{
#>(int)CipherAlgorithmType.<#= val #>, <#
}
#>};
<#
ReadOnlySpan<int> strengths = new[] { 0, 40, 56, 128, 168, 256 };
#>
ReadOnlySpan<int> dataKeySizes =
new[] { <#= string.Join(", ", strengths.ToArray()) #> };
ReadOnlySpan<int> dataHashAlgs =
new[] { <#
foreach (HashAlgorithmTypeIndex val in dataHashAlgs)
{
#>(int)HashAlgorithmType.<#= val #>, <#
}
#>};
ReadOnlySpan<int> dataHashKeySizes =
new[] { 0, 128, 160, 256, 384, 512 };

int data = GetPackedData(cipherSuite);
Debug.Assert(data != 0, $"No mapping found for cipherSuite {cipherSuite}");

KeyExchangeAlg = keyExchangeAlgs[(data >> 12) & 0xF];
DataCipherAlg = dataCipherAlgs[(data >> 8) & 0xF];
DataKeySize = dataKeySizes[(data >> 4) & 0xF];
DataHashAlg = dataHashAlgs[data & 0xF];
DataHashKeySize = dataHashKeySizes[data & 0xF];

static int GetPackedData(TlsCipherSuite cipherSuite)
{
switch (cipherSuite)
{
<#
foreach (TlsCipherSuite val in tlsEnumValues)
{
TlsCipherSuiteData data = new CipherSuiteNameData(val.ToString()).Data;
int exchangeAlgorithmType = (int)Enum.Parse<ExchangeAlgorithmTypeIndex>(EnumHelpers.ToFrameworkName(data.KeyExchangeAlgorithm));
int cipherAlgorithmType = (int)Enum.Parse<CipherAlgorithmTypeIndex>(EnumHelpers.ToFrameworkName(data.CipherAlgorithm));
int cipherAlgorithmStrength = (int)strengths.IndexOf(data.CipherAlgorithmStrength);
int hashAlgorithmType = (int)Enum.Parse<HashAlgorithmTypeIndex>(EnumHelpers.ToFrameworkName(data.MACAlgorithm));

if (cipherAlgorithmStrength == -1)
throw new Exception($"Value '{data.CipherAlgorithmStrength}' is not found in 'strengths' array.");
#>
case TlsCipherSuite.<#= val #>: return <#= exchangeAlgorithmType #> << 12 | <#= cipherAlgorithmType #> << 8 | <#= cipherAlgorithmStrength #> << 4 | <#= hashAlgorithmType #>;
<#
}
#>
default: return 0;
}
}
}
}
}
Loading

0 comments on commit f92b9ef

Please sign in to comment.