@@ -26,121 +26,7 @@ internal class SNIProxy
26
26
private static readonly SNIProxy s_singleton = new SNIProxy ( ) ;
27
27
28
28
internal static SNIProxy Instance => s_singleton ;
29
- #if ! NET8_0_OR_GREATER
30
- /// <summary>
31
- /// Generate SSPI context
32
- /// </summary>
33
- /// <param name="sspiClientContextStatus">SSPI client context status</param>
34
- /// <param name="receivedBuff">Receive buffer</param>
35
- /// <param name="sendWriter">Writer for send buffer</param>
36
- /// <param name="serverName">Service Principal Name buffer</param>
37
- /// <returns>SNI error code</returns>
38
- internal static void GenSspiClientContext ( SspiClientContextStatus sspiClientContextStatus , ReadOnlySpan < byte > receivedBuff , IBufferWriter < byte > sendWriter , byte [ ] [ ] serverName )
39
- {
40
- // TODO: this should use ReadOnlyMemory all the way through
41
- byte [ ] array = null ;
42
-
43
- if ( ! receivedBuff . IsEmpty )
44
- {
45
- array = new byte [ receivedBuff . Length ] ;
46
- receivedBuff . CopyTo ( array ) ;
47
- }
48
-
49
- GenSspiClientContext ( sspiClientContextStatus , array , sendWriter , serverName ) ;
50
- }
51
-
52
- private static void GenSspiClientContext ( SspiClientContextStatus sspiClientContextStatus , byte [ ] receivedBuff , IBufferWriter < byte > sendWriter , byte [ ] [ ] serverName )
53
- {
54
- SafeDeleteContext securityContext = sspiClientContextStatus . SecurityContext ;
55
- ContextFlagsPal contextFlags = sspiClientContextStatus . ContextFlags ;
56
- SafeFreeCredentials credentialsHandle = sspiClientContextStatus . CredentialsHandle ;
57
-
58
- string securityPackage = NegotiationInfoClass . Negotiate ;
59
-
60
- if ( securityContext == null )
61
- {
62
- credentialsHandle = NegotiateStreamPal . AcquireDefaultCredential ( securityPackage , false ) ;
63
- }
64
-
65
- SecurityBuffer [ ] inSecurityBufferArray ;
66
- if ( receivedBuff != null )
67
- {
68
- inSecurityBufferArray = new SecurityBuffer [ ] { new SecurityBuffer ( receivedBuff , SecurityBufferType . SECBUFFER_TOKEN ) } ;
69
- }
70
- else
71
- {
72
- inSecurityBufferArray = Array . Empty < SecurityBuffer > ( ) ;
73
- }
74
-
75
- int tokenSize = NegotiateStreamPal . QueryMaxTokenSize ( securityPackage ) ;
76
-
77
- SecurityBuffer outSecurityBuffer = new SecurityBuffer ( tokenSize , SecurityBufferType . SECBUFFER_TOKEN ) ;
78
29
79
- ContextFlagsPal requestedContextFlags = ContextFlagsPal . Connection
80
- | ContextFlagsPal . Confidentiality
81
- | ContextFlagsPal . Delegate
82
- | ContextFlagsPal . MutualAuth ;
83
-
84
- string [ ] serverSPNs = new string [ serverName . Length ] ;
85
- for ( int i = 0 ; i < serverName . Length ; i ++ )
86
- {
87
- serverSPNs [ i ] = Encoding . Unicode . GetString ( serverName [ i ] ) ;
88
- }
89
- SecurityStatusPal statusCode = NegotiateStreamPal . InitializeSecurityContext (
90
- credentialsHandle ,
91
- ref securityContext ,
92
- serverSPNs ,
93
- requestedContextFlags ,
94
- inSecurityBufferArray ,
95
- outSecurityBuffer ,
96
- ref contextFlags ) ;
97
-
98
- if ( statusCode . ErrorCode == SecurityStatusPalErrorCode . CompleteNeeded ||
99
- statusCode . ErrorCode == SecurityStatusPalErrorCode . CompAndContinue )
100
- {
101
- inSecurityBufferArray = new SecurityBuffer [ ] { outSecurityBuffer } ;
102
- statusCode = NegotiateStreamPal . CompleteAuthToken ( ref securityContext , inSecurityBufferArray ) ;
103
- outSecurityBuffer . token = null ;
104
- }
105
-
106
- if ( outSecurityBuffer . token is { } token )
107
- {
108
- sendWriter . Write ( token ) ;
109
- }
110
-
111
- sspiClientContextStatus . SecurityContext = securityContext ;
112
- sspiClientContextStatus . ContextFlags = contextFlags ;
113
- sspiClientContextStatus . CredentialsHandle = credentialsHandle ;
114
-
115
- if ( IsErrorStatus ( statusCode . ErrorCode ) )
116
- {
117
- // Could not access Kerberos Ticket.
118
- //
119
- // SecurityStatusPalErrorCode.InternalError only occurs in Unix and always comes with a GssApiException,
120
- // so we don't need to check for a GssApiException here.
121
- if ( statusCode . ErrorCode == SecurityStatusPalErrorCode . InternalError )
122
- {
123
- throw new InvalidOperationException ( SQLMessage . KerberosTicketMissingError ( ) + Environment . NewLine + statusCode ) ;
124
- }
125
- else
126
- {
127
- throw new InvalidOperationException ( SQLMessage . SSPIGenerateError ( ) + Environment . NewLine + statusCode ) ;
128
- }
129
- }
130
- }
131
-
132
- private static bool IsErrorStatus ( SecurityStatusPalErrorCode errorCode )
133
- {
134
- return errorCode != SecurityStatusPalErrorCode . NotSet &&
135
- errorCode != SecurityStatusPalErrorCode . OK &&
136
- errorCode != SecurityStatusPalErrorCode . ContinueNeeded &&
137
- errorCode != SecurityStatusPalErrorCode . CompleteNeeded &&
138
- errorCode != SecurityStatusPalErrorCode . CompAndContinue &&
139
- errorCode != SecurityStatusPalErrorCode . ContextExpired &&
140
- errorCode != SecurityStatusPalErrorCode . CredentialsNeeded &&
141
- errorCode != SecurityStatusPalErrorCode . Renegotiate ;
142
- }
143
- #endif
144
30
/// <summary>
145
31
/// Create a SNI connection handle
146
32
/// </summary>
0 commit comments