@@ -24,6 +24,12 @@ public static string GetServerName(byte[] clientHello)
24
24
private static string GetSniFromSslPlainText ( ReadOnlySpan < byte > sslPlainText )
25
25
{
26
26
// 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;
27
33
const int ContentTypeOffset = 0 ;
28
34
const int ProtocolVersionOffset = ContentTypeOffset + sizeof ( ContentType ) ;
29
35
const int LengthOffset = ProtocolVersionOffset + ProtocolVersionSize ;
@@ -51,6 +57,15 @@ private static string GetSniFromSslPlainText(ReadOnlySpan<byte> sslPlainText)
51
57
private static string GetSniFromSslHandshake ( ReadOnlySpan < byte > sslHandshake )
52
58
{
53
59
// 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;
54
69
const int HandshakeTypeOffset = 0 ;
55
70
const int ClientHelloLengthOffset = HandshakeTypeOffset + sizeof ( HandshakeType ) ;
56
71
const int ClientHelloOffset = ClientHelloLengthOffset + UInt24Size ;
@@ -75,7 +90,14 @@ private static string GetSniFromClientHello(ReadOnlySpan<byte> clientHello)
75
90
{
76
91
// Basic structure: https://tools.ietf.org/html/rfc6101#section-5.6.1.2
77
92
// 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;
79
101
ReadOnlySpan < byte > p = SkipBytes ( clientHello , ProtocolVersionSize + RandomSize ) ;
80
102
81
103
// Skip SessionID (max size 32 => size fits in 1 byte)
@@ -130,6 +152,10 @@ private static string GetSniFromClientHello(ReadOnlySpan<byte> clientHello)
130
152
private static string GetSniFromExtension ( ReadOnlySpan < byte > extension , out ReadOnlySpan < byte > remainingBytes , out bool invalid )
131
153
{
132
154
// 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;
133
159
const int ExtensionDataOffset = sizeof ( ExtensionType ) ;
134
160
135
161
if ( extension . Length < ExtensionDataOffset )
@@ -156,6 +182,10 @@ private static string GetSniFromExtension(ReadOnlySpan<byte> extension, out Read
156
182
private static string GetSniFromServerNameList ( ReadOnlySpan < byte > serverNameListExtension , out ReadOnlySpan < byte > remainingBytes , out bool invalid )
157
183
{
158
184
// 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)
159
189
const int ServerNameListOffset = sizeof ( ushort ) ;
160
190
161
191
if ( serverNameListExtension . Length < ServerNameListOffset )
@@ -184,6 +214,13 @@ private static string GetSniFromServerNameList(ReadOnlySpan<byte> serverNameList
184
214
private static string GetSniFromServerName ( ReadOnlySpan < byte > serverName , out bool invalid )
185
215
{
186
216
// 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)
187
224
const int ServerNameLengthOffset = 0 ;
188
225
const int NameTypeOffset = ServerNameLengthOffset + sizeof ( ushort ) ;
189
226
const int HostNameStructOffset = NameTypeOffset + sizeof ( NameType ) ;
@@ -210,6 +247,7 @@ private static string GetSniFromServerName(ReadOnlySpan<byte> serverName, out bo
210
247
private static string GetSniFromHostNameStruct ( ReadOnlySpan < byte > hostNameStruct , out bool invalid )
211
248
{
212
249
// 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)
213
251
const int HostNameLengthOffset = 0 ;
214
252
const int HostNameOffset = HostNameLengthOffset + sizeof ( ushort ) ;
215
253
0 commit comments