Skip to content

Commit 6265bb1

Browse files
committed
Expose TLS details for QUIC connection
1 parent 438dabb commit 6265bb1

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

src/libraries/System.Net.Quic/ref/System.Net.Quic.cs

+9
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@ public QuicClientConnectionOptions() { }
2323
public sealed partial class QuicConnection : System.IAsyncDisposable
2424
{
2525
internal QuicConnection() { }
26+
public System.Security.Authentication.CipherAlgorithmType CipherAlgorithm { get { throw null; } }
27+
public int CipherStrength { get { throw null; } }
28+
public System.Security.Authentication.HashAlgorithmType HashAlgorithm { get { throw null; } }
29+
public int HashStrength { get { throw null; } }
2630
public static bool IsSupported { get { throw null; } }
31+
public System.Security.Authentication.ExchangeAlgorithmType KeyExchangeAlgorithm { get { throw null; } }
32+
public int KeyExchangeStrength { get { throw null; } }
2733
public System.Net.IPEndPoint LocalEndPoint { get { throw null; } }
2834
public System.Net.Security.SslApplicationProtocol NegotiatedApplicationProtocol { get { throw null; } }
35+
[System.CLSCompliantAttribute(false)]
36+
public System.Net.Security.TlsCipherSuite NegotiatedCipherSuite { get { throw null; } }
2937
public System.Security.Cryptography.X509Certificates.X509Certificate? RemoteCertificate { get { throw null; } }
3038
public System.Net.IPEndPoint RemoteEndPoint { get { throw null; } }
39+
public System.Security.Authentication.SslProtocols SslProtocol { get { throw null; } }
3140
public string TargetHostName { get { throw null; } }
3241
public System.Threading.Tasks.ValueTask<System.Net.Quic.QuicStream> AcceptInboundStreamAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
3342
public System.Threading.Tasks.ValueTask CloseAsync(long errorCode, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }

src/libraries/System.Net.Quic/src/System/Net/Quic/QuicConnection.cs

+80
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@
77
using System.Runtime.CompilerServices;
88
using System.Runtime.ExceptionServices;
99
using System.Runtime.InteropServices;
10+
using System.Security.Authentication;
1011
using System.Security.Cryptography.X509Certificates;
1112
using System.Threading;
1213
using System.Threading.Channels;
1314
using System.Threading.Tasks;
1415
using Microsoft.Quic;
1516
using static Microsoft.Quic.MsQuic;
17+
using static Microsoft.Quic.QUIC_CIPHER_ALGORITHM;
18+
using static Microsoft.Quic.QUIC_CIPHER_SUITE;
19+
using static Microsoft.Quic.QUIC_HASH_ALGORITHM;
20+
using static Microsoft.Quic.QUIC_KEY_EXCHANGE_ALGORITHM;
21+
using static Microsoft.Quic.QUIC_TLS_PROTOCOL_VERSION;
22+
1623
using CONNECTED_DATA = Microsoft.Quic.QUIC_CONNECTION_EVENT._Anonymous_e__Union._CONNECTED_e__Struct;
1724
using LOCAL_ADDRESS_CHANGED_DATA = Microsoft.Quic.QUIC_CONNECTION_EVENT._Anonymous_e__Union._LOCAL_ADDRESS_CHANGED_e__Struct;
1825
using PEER_ADDRESS_CHANGED_DATA = Microsoft.Quic.QUIC_CONNECTION_EVENT._Anonymous_e__Union._PEER_ADDRESS_CHANGED_e__Struct;
@@ -223,6 +230,40 @@ public X509Certificate? RemoteCertificate
223230
}
224231
}
225232

233+
/// <summary>
234+
/// Gets the <see cref="SslProtocols" /> corresponding to the TLS version used during the connection handshake.
235+
/// </summary>
236+
public SslProtocols SslProtocol { get; private set; }
237+
238+
/// <summary>
239+
/// Gets the cipher suite which was negotiated for this connection.
240+
/// </summary>
241+
[CLSCompliant(false)]
242+
public TlsCipherSuite NegotiatedCipherSuite { get; private set; }
243+
244+
/// <summary>
245+
/// Gets a value that identifies the bulk encryption algorithm used by this connection.
246+
/// </summary>
247+
public CipherAlgorithmType CipherAlgorithm { get; private set; }
248+
249+
/// <summary>
250+
/// Gets a value that identifies the strength of the cipher algorithm used by this connection.
251+
/// </summary>
252+
public int CipherStrength { get; private set; }
253+
254+
//
255+
// defined in SslStream but we don't need it as QUIC uses AEAD which don't use the hash algorithm
256+
// from the cipher suite
257+
//
258+
public HashAlgorithmType HashAlgorithm { get; private set; }
259+
public int HashStrength { get; private set; }
260+
261+
//
262+
// based on SslConnectionInfo.Unix.cs it would return 0/None in all cases
263+
//
264+
public ExchangeAlgorithmType KeyExchangeAlgorithm { get; private set; }
265+
public int KeyExchangeStrength { get; private set; }
266+
226267
/// <summary>
227268
/// Final, negotiated application protocol.
228269
/// </summary>
@@ -514,6 +555,45 @@ private unsafe int HandleEventConnected(ref CONNECTED_DATA data)
514555
_tlsSecret?.WriteSecret();
515556
#endif
516557

558+
QUIC_HANDSHAKE_INFO handshakeInfo = MsQuicHelpers.GetMsQuicParameter<QUIC_HANDSHAKE_INFO>(_handle, QUIC_PARAM_TLS_HANDSHAKE_INFO);
559+
560+
SslProtocol = handshakeInfo.TlsProtocolVersion switch
561+
{
562+
QUIC_TLS_PROTOCOL_VERSION.TLS_1_3 => SslProtocols.Tls13,
563+
_ => SslProtocols.None,
564+
};
565+
566+
CipherAlgorithm = handshakeInfo.CipherAlgorithm switch
567+
{
568+
QUIC_CIPHER_ALGORITHM.AES_128 => CipherAlgorithmType.Aes128,
569+
QUIC_CIPHER_ALGORITHM.AES_256 => CipherAlgorithmType.Aes256,
570+
QUIC_CIPHER_ALGORITHM.CHACHA20 => CipherAlgorithmType.None, // TODO: CipherAlgorithmType.ChaCha20,
571+
_ => CipherAlgorithmType.None,
572+
};
573+
CipherStrength = handshakeInfo.CipherStrength;
574+
575+
HashAlgorithm = handshakeInfo.Hash switch
576+
{
577+
QUIC_HASH_ALGORITHM.SHA_256 => HashAlgorithmType.Sha256,
578+
QUIC_HASH_ALGORITHM.SHA_384 => HashAlgorithmType.Sha384,
579+
_ => HashAlgorithmType.None,
580+
};
581+
HashStrength = handshakeInfo.HashStrength;
582+
583+
KeyExchangeAlgorithm = handshakeInfo.KeyExchangeAlgorithm switch
584+
{
585+
_ => ExchangeAlgorithmType.None,
586+
};
587+
KeyExchangeStrength = handshakeInfo.KeyExchangeStrength;
588+
589+
NegotiatedCipherSuite = handshakeInfo.CipherSuite switch
590+
{
591+
QUIC_CIPHER_SUITE.TLS_AES_128_GCM_SHA256 => TlsCipherSuite.TLS_AES_128_GCM_SHA256,
592+
QUIC_CIPHER_SUITE.TLS_AES_256_GCM_SHA384 => TlsCipherSuite.TLS_AES_256_GCM_SHA384,
593+
QUIC_CIPHER_SUITE.TLS_CHACHA20_POLY1305_SHA256 => TlsCipherSuite.TLS_CHACHA20_POLY1305_SHA256,
594+
_ => TlsCipherSuite.TLS_AES_128_GCM_SHA256,
595+
};
596+
517597
if (NetEventSource.Log.IsEnabled())
518598
{
519599
NetEventSource.Info(this, $"{this} Connection connected {LocalEndPoint} -> {RemoteEndPoint} for {_negotiatedApplicationProtocol} protocol");

0 commit comments

Comments
 (0)