4
4
5
5
using System ;
6
6
using System . Buffers ;
7
+ using System . Collections . Generic ;
7
8
using System . Diagnostics ;
8
9
using System . IO ;
9
10
using System . Net ;
@@ -32,25 +33,25 @@ internal class SNIProxy
32
33
/// </summary>
33
34
/// <param name="sspiClientContextStatus">SSPI client context status</param>
34
35
/// <param name="receivedBuff">Receive buffer</param>
35
- /// <param name="serverName ">Service Principal Name buffer</param>
36
+ /// <param name="serverNames ">Service Principal Name buffer</param>
36
37
/// <returns>Memory for response</returns>
37
- internal static IMemoryOwner < byte > GenSspiClientContext ( SspiClientContextStatus sspiClientContextStatus , ReadOnlyMemory < byte > receivedBuff , byte [ ] [ ] serverName )
38
+ internal static IMemoryOwner < byte > GenSspiClientContext ( SspiClientContextStatus sspiClientContextStatus , ReadOnlyMemory < byte > receivedBuff , IReadOnlyList < string > serverNames )
38
39
{
39
40
// TODO: this should use ReadOnlyMemory all the way through
40
41
var array = ArrayPool < byte > . Shared . Rent ( receivedBuff . Length ) ;
41
42
42
43
try
43
44
{
44
45
receivedBuff . CopyTo ( array ) ;
45
- return GenSspiClientContext ( sspiClientContextStatus , array , receivedBuff . Length , serverName ) ;
46
+ return GenSspiClientContext ( sspiClientContextStatus , array , receivedBuff . Length , serverNames ) ;
46
47
}
47
48
finally
48
49
{
49
50
ArrayPool < byte > . Shared . Return ( array ) ;
50
51
}
51
52
}
52
53
53
- private static IMemoryOwner < byte > GenSspiClientContext ( SspiClientContextStatus sspiClientContextStatus , byte [ ] receivedBuff , int receivedBuffLength , byte [ ] [ ] serverName )
54
+ private static IMemoryOwner < byte > GenSspiClientContext ( SspiClientContextStatus sspiClientContextStatus , byte [ ] receivedBuff , int receivedBuffLength , IReadOnlyList < string > serverSPNs )
54
55
{
55
56
SafeDeleteContext securityContext = sspiClientContextStatus . SecurityContext ;
56
57
ContextFlagsPal contextFlags = sspiClientContextStatus . ContextFlags ;
@@ -66,7 +67,7 @@ private static IMemoryOwner<byte> GenSspiClientContext(SspiClientContextStatus s
66
67
SecurityBuffer [ ] inSecurityBufferArray ;
67
68
if ( receivedBuff != null )
68
69
{
69
- inSecurityBufferArray = new SecurityBuffer [ ] { new SecurityBuffer ( receivedBuff , SecurityBufferType . SECBUFFER_TOKEN ) } ;
70
+ inSecurityBufferArray = new SecurityBuffer [ ] { new SecurityBuffer ( receivedBuff , 0 , receivedBuffLength , SecurityBufferType . SECBUFFER_TOKEN ) } ;
70
71
}
71
72
else
72
73
{
@@ -82,11 +83,6 @@ private static IMemoryOwner<byte> GenSspiClientContext(SspiClientContextStatus s
82
83
| ContextFlagsPal . Delegate
83
84
| ContextFlagsPal . MutualAuth ;
84
85
85
- string [ ] serverSPNs = new string [ serverName . Length ] ;
86
- for ( int i = 0 ; i < serverName . Length ; i ++ )
87
- {
88
- serverSPNs [ i ] = Encoding . Unicode . GetString ( serverName [ i ] ) ;
89
- }
90
86
SecurityStatusPal statusCode = NegotiateStreamPal . InitializeSecurityContext (
91
87
credentialsHandle ,
92
88
ref securityContext ,
@@ -162,7 +158,7 @@ internal static SNIHandle CreateConnectionHandle(
162
158
string fullServerName ,
163
159
TimeoutTimer timeout ,
164
160
out byte [ ] instanceName ,
165
- ref byte [ ] [ ] spnBuffer ,
161
+ ref string [ ] spnBuffer ,
166
162
string serverSPN ,
167
163
bool flushCache ,
168
164
bool async ,
@@ -226,12 +222,12 @@ internal static SNIHandle CreateConnectionHandle(
226
222
return sniHandle ;
227
223
}
228
224
229
- private static byte [ ] [ ] GetSqlServerSPNs ( DataSource dataSource , string serverSPN )
225
+ private static string [ ] GetSqlServerSPNs ( DataSource dataSource , string serverSPN )
230
226
{
231
227
Debug . Assert ( ! string . IsNullOrWhiteSpace ( dataSource . ServerName ) ) ;
232
228
if ( ! string . IsNullOrWhiteSpace ( serverSPN ) )
233
229
{
234
- return new byte [ 1 ] [ ] { Encoding . Unicode . GetBytes ( serverSPN ) } ;
230
+ return new [ ] { serverSPN } ;
235
231
}
236
232
237
233
string hostName = dataSource . ServerName ;
@@ -249,7 +245,7 @@ private static byte[][] GetSqlServerSPNs(DataSource dataSource, string serverSPN
249
245
return GetSqlServerSPNs ( hostName , postfix , dataSource . _connectionProtocol ) ;
250
246
}
251
247
252
- private static byte [ ] [ ] GetSqlServerSPNs ( string hostNameOrAddress , string portOrInstanceName , DataSource . Protocol protocol )
248
+ private static string [ ] GetSqlServerSPNs ( string hostNameOrAddress , string portOrInstanceName , DataSource . Protocol protocol )
253
249
{
254
250
Debug . Assert ( ! string . IsNullOrWhiteSpace ( hostNameOrAddress ) ) ;
255
251
IPHostEntry hostEntry = null ;
@@ -280,12 +276,12 @@ private static byte[][] GetSqlServerSPNs(string hostNameOrAddress, string portOr
280
276
string serverSpnWithDefaultPort = serverSpn + $ ":{ DefaultSqlServerPort } ";
281
277
// Set both SPNs with and without Port as Port is optional for default instance
282
278
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "SNIProxy.GetSqlServerSPN | Info | ServerSPNs {0} and {1}" , serverSpn , serverSpnWithDefaultPort ) ;
283
- return new byte [ ] [ ] { Encoding . Unicode . GetBytes ( serverSpn ) , Encoding . Unicode . GetBytes ( serverSpnWithDefaultPort ) } ;
279
+ return new [ ] { serverSpn , serverSpnWithDefaultPort } ;
284
280
}
285
281
// else Named Pipes do not need to valid port
286
282
287
283
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "SNIProxy.GetSqlServerSPN | Info | ServerSPN {0}" , serverSpn ) ;
288
- return new byte [ ] [ ] { Encoding . Unicode . GetBytes ( serverSpn ) } ;
284
+ return new [ ] { serverSpn } ;
289
285
}
290
286
291
287
/// <summary>
0 commit comments