Skip to content

Commit

Permalink
TLS 1.3 more work
Browse files Browse the repository at this point in the history
  • Loading branch information
Drawaes committed Apr 8, 2017
1 parent 380a93a commit fd8c378
Show file tree
Hide file tree
Showing 50 changed files with 422 additions and 436 deletions.
2 changes: 1 addition & 1 deletion .EditorConfig
Expand Up @@ -26,7 +26,7 @@ indent_size = 2
# .NET Code Style Settings
# See https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
# REVIEW: Should these be errors? warnings? suggestions?
[*.cs,*.vb]
[{*.cs,*.vb}]
dotnet_sort_system_directives_first = true

# Don't use 'this.'/'Me.' prefix for anything
Expand Down
17 changes: 9 additions & 8 deletions Leto.sln
@@ -1,10 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26228.9
VisualStudioVersion = 15.0.26228.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6C9F168D-895E-4FB3-A7C7-61EB03C7B97D}"
ProjectSection(SolutionItems) = preProject
.EditorConfig = .EditorConfig
common.props = common.props
NuGet.Config = NuGet.Config
README.md = README.md
Expand Down Expand Up @@ -34,19 +35,19 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{837E39A4
build\dependencies.props = build\dependencies.props
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Leto", "src\Leto\Leto.csproj", "{FC7CAA09-281A-4DB0-8787-0438DBF9AFEB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leto", "src\Leto\Leto.csproj", "{FC7CAA09-281A-4DB0-8787-0438DBF9AFEB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Leto.OpenSslFacts", "test\Leto.OpenSslFacts\Leto.OpenSslFacts.csproj", "{72685434-D105-4004-B023-4E428AADDA26}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leto.OpenSslFacts", "test\Leto.OpenSslFacts\Leto.OpenSslFacts.csproj", "{72685434-D105-4004-B023-4E428AADDA26}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Leto.OpenSsl11", "src\Leto.OpenSsl11\Leto.OpenSsl11.csproj", "{D5367558-339F-4F66-9885-3A0C7EBB94B6}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leto.OpenSsl11", "src\Leto.OpenSsl11\Leto.OpenSsl11.csproj", "{D5367558-339F-4F66-9885-3A0C7EBB94B6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Leto.Windows", "src\Leto.Windows\Leto.Windows.csproj", "{DC923514-D2A9-4E80-A020-8C58406E7A00}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leto.Windows", "src\Leto.Windows\Leto.Windows.csproj", "{DC923514-D2A9-4E80-A020-8C58406E7A00}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Leto.WindowsFacts", "test\Leto.WindowsFacts\Leto.WindowsFacts.csproj", "{0E1D900F-49D9-4330-9FF2-EBEEFFF0A4F3}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leto.WindowsFacts", "test\Leto.WindowsFacts\Leto.WindowsFacts.csproj", "{0E1D900F-49D9-4330-9FF2-EBEEFFF0A4F3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Leto.ProtocolFacts", "test\Leto.ProtocolFacts\Leto.ProtocolFacts.csproj", "{0DC7CEA6-8721-47F8-A0A1-7C85FB819E74}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Leto.ProtocolFacts", "test\Leto.ProtocolFacts\Leto.ProtocolFacts.csproj", "{0DC7CEA6-8721-47F8-A0A1-7C85FB819E74}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SocketServer", "samples\SocketServer\SocketServer.csproj", "{4E69773F-D420-493B-899B-18EF1767BE14}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SocketServer", "samples\SocketServer\SocketServer.csproj", "{4E69773F-D420-493B-899B-18EF1767BE14}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
3 changes: 2 additions & 1 deletion samples/SocketServer/SocketServer.csproj
Expand Up @@ -7,7 +7,8 @@

<ItemGroup>
<PackageReference Include="Microsoft.DotNet.PlatformAbstractions" Version="1.1.1" />
<PackageReference Include="System.IO.Pipelines.Networking.Sockets" Version="0.1.0-e170401-1" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
<PackageReference Include="System.IO.Pipelines.Networking.Sockets" Version="0.1.0-e170406-2" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 1 addition & 4 deletions src/Leto.OpenSsl11/ExceptionHelper.cs
Expand Up @@ -8,9 +8,6 @@ internal static class ExceptionHelper
{
[MethodImpl(MethodImplOptions.NoInlining)]
[DebuggerStepThrough]
internal static void ThrowException(Exception ex)
{
throw ex;
}
internal static void ThrowException(Exception ex) => throw ex;
}
}
10 changes: 5 additions & 5 deletions src/Leto.OpenSsl11/Leto.OpenSsl11.csproj
Expand Up @@ -7,11 +7,11 @@
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Binary" Version="0.1.0-e170401-1" />
<PackageReference Include="System.Buffers.Experimental" Version="0.1.0-e170401-1" />
<PackageReference Include="System.Buffers.Primitives" Version="0.1.0-e170401-1" />
<PackageReference Include="System.IO.Pipelines" Version="0.1.0-e170401-1" />
<PackageReference Include="System.IO.Pipelines.Extensions" Version="0.1.0-e170401-1" />
<PackageReference Include="System.Binary" Version="0.1.0-e170406-2" />
<PackageReference Include="System.Buffers.Experimental" Version="0.1.0-e170406-2" />
<PackageReference Include="System.Buffers.Primitives" Version="0.1.0-e170406-2" />
<PackageReference Include="System.IO.Pipelines" Version="0.1.0-e170406-2" />
<PackageReference Include="System.IO.Pipelines.Extensions" Version="0.1.0-e170406-2" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.3.0" />
<PackageReference Include="System.Threading" Version="4.3.0" />
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
Expand Down
30 changes: 6 additions & 24 deletions src/Leto.OpenSsl11/OpenSslBulkCipherKey.cs
Expand Up @@ -29,25 +29,10 @@ internal OpenSslBulkCipherKey(EVP_BulkCipher_Type type, Buffer<byte> keyStore, i
public Buffer<byte> IV => _iv;
public int TagSize => _tagSize;

public void Init(KeyMode mode)
{
EVP_CipherInit_ex(_ctx, _type, _key.Span, _iv.Span, mode);
}

public int Update(Span<byte> input, Span<byte> output)
{
return EVP_CipherUpdate(_ctx, output, input);
}

public int Update(Span<byte> inputAndOutput)
{
return EVP_CipherUpdate(_ctx, inputAndOutput, inputAndOutput);
}

public void AddAdditionalInfo(AdditionalInfo addInfo)
{
EVP_CipherUpdate(_ctx, addInfo);
}
public void Init(KeyMode mode) => EVP_CipherInit_ex(_ctx, _type, _key.Span, _iv.Span, mode);
public int Update(Span<byte> input, Span<byte> output) => EVP_CipherUpdate(_ctx, output, input);
public int Update(Span<byte> inputAndOutput) => EVP_CipherUpdate(_ctx, inputAndOutput, inputAndOutput);
public void AddAdditionalInfo(AdditionalInfo addInfo) => EVP_CipherUpdate(_ctx, addInfo);

public void ReadTag(Span<byte> span)
{
Expand All @@ -64,16 +49,13 @@ public void WriteTag(ReadOnlySpan<byte> tagSpan)
EVP_CIPHER_CTX_SetTag(_ctx, tagSpan);
EVP_CipherFinal_ex(_ctx);
}

public void Dispose()
{
_ctx.Free();
GC.SuppressFinalize(this);
}

~OpenSslBulkCipherKey()
{
Dispose();
}
~OpenSslBulkCipherKey() => Dispose();
}
}
11 changes: 2 additions & 9 deletions src/Leto.OpenSsl11/OpenSslKeyExchangeProvider.cs
@@ -1,8 +1,5 @@
using Leto.Certificates;
using Leto.KeyExchanges;
using Leto.KeyExchanges;
using System;
using System.Collections.Generic;
using System.Text;

namespace Leto.OpenSsl11
{
Expand Down Expand Up @@ -100,10 +97,6 @@ public void Dispose()
//No resources currently to clean up
}

public IKeyExchange GetKeyExchange(Span<byte> keyshare)
{
throw new NotImplementedException();
}

public IKeyExchange GetKeyExchange(Span<byte> keyshare) => throw new NotImplementedException();
}
}
7 changes: 2 additions & 5 deletions src/Leto.OpenSsl11/OpenSslSecurePipeListener.cs
Expand Up @@ -14,12 +14,9 @@ public sealed class OpenSslSecurePipeListener : SecurePipeListener
{
private ICryptoProvider _cryptoProvider;
private CertificateList _certificateList = new CertificateList();

public OpenSslSecurePipeListener(ICertificate certificate, PipeFactory pipeFactory = null)
:base(certificate, pipeFactory)
{
_cryptoProvider = new OpenSslCryptoProvider();
}
: base(certificate, pipeFactory) => _cryptoProvider = new OpenSslCryptoProvider();

public override ICryptoProvider CryptoProvider => _cryptoProvider;
public override ISessionProvider SessionProvider => null;
Expand Down
5 changes: 1 addition & 4 deletions src/Leto.Windows/ExceptionHelper.cs
Expand Up @@ -8,9 +8,6 @@ internal static class ExceptionHelper
{
[MethodImpl(MethodImplOptions.NoInlining)]
[DebuggerStepThrough]
internal static void ThrowException(Exception ex)
{
throw ex;
}
internal static void ThrowException(Exception ex) => throw ex;
}
}
10 changes: 5 additions & 5 deletions src/Leto.Windows/Leto.Windows.csproj
Expand Up @@ -6,11 +6,11 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Binary" Version="0.1.0-e170401-1" />
<PackageReference Include="System.Buffers.Experimental" Version="0.1.0-e170401-1" />
<PackageReference Include="System.Buffers.Primitives" Version="0.1.0-e170401-1" />
<PackageReference Include="System.IO.Pipelines" Version="0.1.0-e170401-1" />
<PackageReference Include="System.IO.Pipelines.Extensions" Version="0.1.0-e170401-1" />
<PackageReference Include="System.Binary" Version="0.1.0-e170406-2" />
<PackageReference Include="System.Buffers.Experimental" Version="0.1.0-e170406-2" />
<PackageReference Include="System.Buffers.Primitives" Version="0.1.0-e170406-2" />
<PackageReference Include="System.IO.Pipelines" Version="0.1.0-e170406-2" />
<PackageReference Include="System.IO.Pipelines.Extensions" Version="0.1.0-e170406-2" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.3.0" />
<PackageReference Include="System.Threading" Version="4.3.0" />
<PackageReference Include="System.ValueTuple" Version="4.3.0" />
Expand Down
7 changes: 2 additions & 5 deletions src/Leto.Windows/WindowsBulkCipherKey.cs
Expand Up @@ -55,7 +55,7 @@ public unsafe void Init(KeyMode mode)
_keyMode = mode;
fixed (void* empty = _empty)
{
Unsafe.CopyBlock(TempIVPointer, empty,(uint) _empty.Length);
Unsafe.CopyBlock(TempIVPointer, empty, (uint)_empty.Length);
}
_context = new BCRYPT_AUTHENTICATED_CIPHER_MODE_INFO()
{
Expand Down Expand Up @@ -117,10 +117,7 @@ public unsafe int Update(Span<byte> inputAndOutput)
return (int)totalWritten;
}

public unsafe void WriteTag(ReadOnlySpan<byte> tagSpan)
{
BCryptDecryptSetTag(_keyHandle, tagSpan, _context, TempIVPointer);
}
public unsafe void WriteTag(ReadOnlySpan<byte> tagSpan) => BCryptDecryptSetTag(_keyHandle, tagSpan, _context, TempIVPointer);

public void Dispose()
{
Expand Down
5 changes: 1 addition & 4 deletions src/Leto.Windows/WindowsHash.cs
Expand Up @@ -33,10 +33,7 @@ public int FinishHash(Span<byte> output)
return _size;
}

public void HashData(ReadOnlySpan<byte> data)
{
BCryptHashData(_hashHandle, data);
}
public void HashData(ReadOnlySpan<byte> data) => BCryptHashData(_hashHandle, data);

public int InterimHash(Span<byte> output)
{
Expand Down
18 changes: 13 additions & 5 deletions src/Leto.Windows/WindowsKeyExchangeProvider.cs
Expand Up @@ -108,12 +108,20 @@ public void Dispose()

public IKeyExchange GetKeyExchange(Span<byte> keyshare)
{
throw new NotImplementedException();
while(keyshare.Length > 0)
{
var namedGroup = BufferExtensions.ReadBigEndian<NamedGroup>(ref keyshare);
var key = BufferExtensions.ReadVector16(ref keyshare);
var instance = GetKeyExchange(namedGroup);
if(instance != null)
{
instance.SetPeerKey(key, null, Certificates.SignatureScheme.none);
return instance;
}
}
return null;
}

~WindowsKeyExchangeProvider()
{
Dispose();
}
~WindowsKeyExchangeProvider() => Dispose();
}
}
31 changes: 7 additions & 24 deletions src/Leto/Alerts/AlertException.cs
Expand Up @@ -7,7 +7,7 @@ namespace Leto.Alerts
public class AlertException : Exception
{
public AlertException(AlertLevel alertLevel, AlertDescription description, string message)
:base(message)
: base(message)
{
Level = alertLevel;
Description = description;
Expand All @@ -21,34 +21,17 @@ public AlertException(AlertLevel alertLevel, AlertDescription description, strin

[MethodImpl(MethodImplOptions.NoInlining)]
[DebuggerHidden()]
public static void ThrowAlert(AlertLevel alertLevel, AlertDescription description, string message)
{
public static void ThrowAlert(AlertLevel alertLevel, AlertDescription description, string message) =>
throw new AlertException(alertLevel, description, message);
}

public static void ThrowFailedHandshake(string message)
{
public static void ThrowFailedHandshake(string message) =>
ThrowAlert(AlertLevel.Fatal, AlertDescription.handshake_failure, message);
}

public static void ThrowUnexpectedMessage(RecordLayer.RecordType recordType)
{
public static void ThrowUnexpectedMessage(RecordLayer.RecordType recordType) =>
ThrowAlert(AlertLevel.Fatal, AlertDescription.unexpected_message, $"Unexpected message of type {recordType}");
}

public static void ThrowUnexpectedMessage(Handshake.HandshakeType handshakeType)
{
public static void ThrowUnexpectedMessage(Handshake.HandshakeType handshakeType) =>
ThrowAlert(AlertLevel.Fatal, AlertDescription.unexpected_message, $"Unexpected message of type {handshakeType}");
}

public static void ThrowInvalidLength(int expected, int actual)
{
public static void ThrowInvalidLength(int expected, int actual) =>
ThrowAlert(AlertLevel.Fatal, AlertDescription.decode_error, $"Invalid vector length expected {expected} actual {actual}");
}

public static void ThrowDecode(string message)
{
public static void ThrowDecode(string message) =>
ThrowAlert(AlertLevel.Fatal, AlertDescription.decode_error, message);
}
}
}
6 changes: 1 addition & 5 deletions src/Leto/Alerts/AlertLevel.cs
@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Leto.Alerts
namespace Leto.Alerts
{
public enum AlertLevel : byte
{
Expand Down
1 change: 0 additions & 1 deletion src/Leto/BulkCiphers/AdditionalInfo.cs
@@ -1,5 +1,4 @@
using Leto.RecordLayer;
using System.Runtime;
using System.Runtime.InteropServices;
using static Leto.BufferExtensions;

Expand Down
14 changes: 4 additions & 10 deletions src/Leto/Certificates/CertificateList.cs
Expand Up @@ -8,10 +8,7 @@ public class CertificateList
{
private List<ICertificate> _certificates = new List<ICertificate>();

public void AddCertificate(ICertificate certificate)
{
_certificates.Add(certificate);
}
public void AddCertificate(ICertificate certificate) => _certificates.Add(certificate);

public ICertificate GetCertificate(string host, CertificateType certificateType)
{
Expand All @@ -34,18 +31,15 @@ public ICertificate GetCertificate(string host, CertificateType certificateType)
return null;
}

public ICertificate GetCertificate(string host, SignatureScheme type)
{
return _certificates[0];
}

public ICertificate GetCertificate(string host, SignatureScheme type) => _certificates[0];

public (ICertificate, SignatureScheme) GetCertificate(Span<byte> buffer)
{
buffer = ReadVector16(ref buffer);
while(buffer.Length > 0)
{
var scheme = ReadBigEndian<SignatureScheme>(ref buffer);
for(int i = 0; i < _certificates.Count;i++)
for(var i = 0; i < _certificates.Count;i++)
{
if (_certificates[i].SupportsScheme(scheme))
{
Expand Down
4 changes: 2 additions & 2 deletions src/Leto/Certificates/ManagedCertificate.cs
Expand Up @@ -25,7 +25,7 @@ public ManagedCertificate(X509Certificate2 certificate, X509Certificate2Collecti
else
{
_certificateChain = new byte[chain.Count][];
for (int i = 0; i < _certificateChain.Length; i++)
for (var i = 0; i < _certificateChain.Length; i++)
{
_certificateChain[i] = chain[i].RawData;
}
Expand Down Expand Up @@ -72,7 +72,7 @@ public int Decrypt(SignatureScheme scheme, Span<byte> encryptedData, Span<byte>
{
if (_certificateType == CertificateType.rsa)
{
RSAEncryptionPadding padding = RSAEncryptionPadding.Pkcs1;
var padding = RSAEncryptionPadding.Pkcs1;
var result = _rsaPrivateKey.Decrypt(encryptedData.ToArray(), padding);
result.CopyTo(output);
return result.Length;
Expand Down
9 changes: 3 additions & 6 deletions src/Leto/CipherSuites/CipherSuiteProvider.cs
Expand Up @@ -8,16 +8,13 @@ public class CipherSuiteProvider
{
private CipherSuite[] _cipherSuites;

public CipherSuiteProvider(CipherSuite[] cipherSuites)
{
_cipherSuites = cipherSuites;
}
public CipherSuiteProvider(CipherSuite[] cipherSuites) => _cipherSuites = cipherSuites;

public CipherSuite GetCipherSuite(ushort cipherSuite)
{
for(int i = 0; i < _cipherSuites.Length; i++)
for (var i = 0; i < _cipherSuites.Length; i++)
{
if(_cipherSuites[i].Code == cipherSuite)
if (_cipherSuites[i].Code == cipherSuite)
{
return _cipherSuites[i];
}
Expand Down

0 comments on commit fd8c378

Please sign in to comment.