Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 40f8447

Browse files
committed
add structures descriptions
1 parent 3dda001 commit 40f8447

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

src/System.Net.Security/src/System/Net/Security/SniHelper.cs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ public static string GetServerName(byte[] clientHello)
2424
private static string GetSniFromSslPlainText(ReadOnlySpan<byte> sslPlainText)
2525
{
2626
// https://tools.ietf.org/html/rfc6101#section-5.2.1
27+
// struct {
28+
// ContentType type; // enum with max value 255
29+
// ProtocolVersion version; // 2x uint8
30+
// uint16 length;
31+
// opaque fragment[SSLPlaintext.length];
32+
// } SSLPlaintext;
2733
const int ContentTypeOffset = 0;
2834
const int ProtocolVersionOffset = ContentTypeOffset + sizeof(ContentType);
2935
const int LengthOffset = ProtocolVersionOffset + ProtocolVersionSize;
@@ -51,6 +57,15 @@ private static string GetSniFromSslPlainText(ReadOnlySpan<byte> sslPlainText)
5157
private static string GetSniFromSslHandshake(ReadOnlySpan<byte> sslHandshake)
5258
{
5359
// https://tools.ietf.org/html/rfc6101#section-5.6
60+
// struct {
61+
// HandshakeType msg_type; /* handshake type */
62+
// uint24 length; /* bytes in message */
63+
// select (HandshakeType) {
64+
// ...
65+
// case client_hello: ClientHello;
66+
// ...
67+
// } body;
68+
// } Handshake;
5469
const int HandshakeTypeOffset = 0;
5570
const int ClientHelloLengthOffset = HandshakeTypeOffset + sizeof(HandshakeType);
5671
const int ClientHelloOffset = ClientHelloLengthOffset + UInt24Size;
@@ -75,7 +90,14 @@ private static string GetSniFromClientHello(ReadOnlySpan<byte> clientHello)
7590
{
7691
// Basic structure: https://tools.ietf.org/html/rfc6101#section-5.6.1.2
7792
// Extended structure: https://tools.ietf.org/html/rfc3546#section-2.1
78-
93+
// struct {
94+
// ProtocolVersion client_version; // 2x uint8
95+
// Random random; // 32 bytes
96+
// SessionID session_id; // opaque type
97+
// CipherSuite cipher_suites<2..2^16-1>; // opaque type
98+
// CompressionMethod compression_methods<1..2^8-1>; // opaque type
99+
// Extension client_hello_extension_list<0..2^16-1>;
100+
// } ClientHello;
79101
ReadOnlySpan<byte> p = SkipBytes(clientHello, ProtocolVersionSize + RandomSize);
80102

81103
// Skip SessionID (max size 32 => size fits in 1 byte)
@@ -130,6 +152,10 @@ private static string GetSniFromClientHello(ReadOnlySpan<byte> clientHello)
130152
private static string GetSniFromExtension(ReadOnlySpan<byte> extension, out ReadOnlySpan<byte> remainingBytes, out bool invalid)
131153
{
132154
// https://tools.ietf.org/html/rfc3546#section-2.3
155+
// struct {
156+
// ExtensionType extension_type;
157+
// opaque extension_data<0..2^16-1>;
158+
// } Extension;
133159
const int ExtensionDataOffset = sizeof(ExtensionType);
134160

135161
if (extension.Length < ExtensionDataOffset)
@@ -156,6 +182,10 @@ private static string GetSniFromExtension(ReadOnlySpan<byte> extension, out Read
156182
private static string GetSniFromServerNameList(ReadOnlySpan<byte> serverNameListExtension, out ReadOnlySpan<byte> remainingBytes, out bool invalid)
157183
{
158184
// https://tools.ietf.org/html/rfc3546#section-3.1
185+
// struct {
186+
// ServerName server_name_list<1..2^16-1>
187+
// } ServerNameList;
188+
// ServerNameList is an opaque type (length of sufficient size for max data length is prepended)
159189
const int ServerNameListOffset = sizeof(ushort);
160190

161191
if (serverNameListExtension.Length < ServerNameListOffset)
@@ -184,6 +214,13 @@ private static string GetSniFromServerNameList(ReadOnlySpan<byte> serverNameList
184214
private static string GetSniFromServerName(ReadOnlySpan<byte> serverName, out bool invalid)
185215
{
186216
// https://tools.ietf.org/html/rfc3546#section-3.1
217+
// struct {
218+
// NameType name_type;
219+
// select (name_type) {
220+
// case host_name: HostName;
221+
// } name;
222+
// } ServerName;
223+
// ServerName is an opaque type (length of sufficient size for max data length is prepended)
187224
const int ServerNameLengthOffset = 0;
188225
const int NameTypeOffset = ServerNameLengthOffset + sizeof(ushort);
189226
const int HostNameStructOffset = NameTypeOffset + sizeof(NameType);
@@ -210,6 +247,7 @@ private static string GetSniFromServerName(ReadOnlySpan<byte> serverName, out bo
210247
private static string GetSniFromHostNameStruct(ReadOnlySpan<byte> hostNameStruct, out bool invalid)
211248
{
212249
// https://tools.ietf.org/html/rfc3546#section-3.1
250+
// HostName is an opaque type (length of sufficient size for max data length is prepended)
213251
const int HostNameLengthOffset = 0;
214252
const int HostNameOffset = HostNameLengthOffset + sizeof(ushort);
215253

0 commit comments

Comments
 (0)