Skip to content

Commit dfdc5c2

Browse files
committed
CSHARP-1557: Get GssapiAuthenticator and helper classes to compile against .NET Core.
1 parent 66c392a commit dfdc5c2

22 files changed

+94
-54
lines changed

src/MongoDB.Driver.Core.Dotnet/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"NETStandard.Library": "1.6.0",
77
"System.Collections.Specialized": "4.0.1",
88
"System.Diagnostics.TraceSource": "4.0.0",
9+
"System.Net.NameResolution": "4.0.0",
910
"System.Net.Security": "4.0.0",
1011
"System.Security.Cryptography.Algorithms": "4.2.0",
1112
"System.Security.Cryptography.X509Certificates": "4.1.0",

src/MongoDB.Driver.Core/Core/Authentication/GssapiAuthenticator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if NET45
1716
using System;
1817
using System.Collections.Generic;
1918
using System.Linq;
@@ -205,7 +204,11 @@ public ISaslStep Initialize(IConnection connection, ConnectionDescription descri
205204

206205
if (_canonicalizeHostName)
207206
{
207+
#if NETSTANDARD1_6
208+
var entry = Dns.GetHostEntryAsync(hostName).GetAwaiter().GetResult();
209+
#else
208210
var entry = Dns.GetHostEntry(hostName);
211+
#endif
209212
if (entry != null)
210213
{
211214
hostName = entry.HostName;
@@ -397,4 +400,3 @@ public ISaslStep Transition(SaslConversation conversation, byte[] bytesReceivedF
397400
}
398401
}
399402
}
400-
#endif

src/MongoDB.Driver.Core/Core/Authentication/Sspi/AuthIdentity.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if NET45
1716
using System;
1817
using System.Runtime.InteropServices;
1918
using System.Security;
@@ -53,7 +52,11 @@ public AuthIdentity(string username, SecureString password)
5352

5453
if (password != null && password.Length > 0)
5554
{
55+
#if NETSTANDARD1_6
56+
Password = SecureStringMarshal.SecureStringToGlobalAllocUnicode(password);
57+
#else
5658
Password = Marshal.SecureStringToGlobalAllocUnicode(password);
59+
#endif
5760
PasswordLength = password.Length;
5861
}
5962

@@ -86,4 +89,3 @@ private void Dispose(bool disposing)
8689
}
8790
}
8891
}
89-
#endif

src/MongoDB.Driver.Core/Core/Authentication/Sspi/AuthIdentityFlag.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if NET45
1716
namespace MongoDB.Driver.Core.Authentication.Sspi
1817
{
1918
/// <summary>
@@ -31,4 +30,3 @@ internal enum AuthIdentityFlag
3130
Unicode = 0x2
3231
}
3332
}
34-
#endif

src/MongoDB.Driver.Core/Core/Authentication/Sspi/DataRepresentation.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if NET45
1716
namespace MongoDB.Driver.Core.Authentication.Sspi
1817
{
1918
/// <summary>
@@ -35,4 +34,3 @@ internal enum DataRepresentation
3534
Native = 16
3635
}
3736
}
38-
#endif

src/MongoDB.Driver.Core/Core/Authentication/Sspi/EncryptQualityOfProtection.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if NET45
1716
namespace MongoDB.Driver.Core.Authentication.Sspi
1817
{
1918
/// <summary>
@@ -31,4 +30,3 @@ internal enum EncryptQualityOfProtection : uint
3130
WrapNoEncrypt = 0x80000001
3231
}
3332
}
34-
#endif

src/MongoDB.Driver.Core/Core/Authentication/Sspi/NativeMethods.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if NET45
1716
using System;
17+
#if NET45
1818
using System.Runtime.ConstrainedExecution;
19+
#endif
1920
using System.Runtime.InteropServices;
2021

2122
namespace MongoDB.Driver.Core.Authentication.Sspi
@@ -186,8 +187,10 @@ public static extern uint AcquireCredentialsHandle(
186187
/// <remarks>
187188
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa375354(v=vs.85).aspx
188189
/// </remarks>
189-
[DllImport("security.dll", CharSet = CharSet.Auto, SetLastError = false)]
190+
[DllImport("security.dll", CharSet = CharSet.Unicode, SetLastError = false)]
191+
#if NET45
190192
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
193+
#endif
191194
public static extern uint DeleteSecurityContext(ref SspiHandle context);
192195

193196
/// <summary>
@@ -201,7 +204,7 @@ public static extern uint AcquireCredentialsHandle(
201204
/// <remarks>
202205
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa375211(v=vs.85).aspx
203206
/// </remarks>
204-
[DllImport("security.dll", CharSet = CharSet.Auto, SetLastError = false)]
207+
[DllImport("security.dll", CharSet = CharSet.Unicode, SetLastError = false)]
205208
public static extern uint DecryptMessage(ref SspiHandle context,
206209
ref SecurityBufferDescriptor pMessage,
207210
uint sequenceNumber,
@@ -218,7 +221,7 @@ public static extern uint DecryptMessage(ref SspiHandle context,
218221
/// <remarks>
219222
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa375378(v=vs.85).aspx
220223
/// </remarks>
221-
[DllImport("security.dll", CharSet = CharSet.Auto, SetLastError = false)]
224+
[DllImport("security.dll", CharSet = CharSet.Unicode, SetLastError = false)]
222225
public static extern uint EncryptMessage(ref SspiHandle context,
223226
EncryptQualityOfProtection quality,
224227
ref SecurityBufferDescriptor pMessage,
@@ -233,7 +236,7 @@ public static extern uint EncryptMessage(ref SspiHandle context,
233236
/// <remarks>
234237
/// http://msdn.microsoft.com/en-us/library/aa375397%28v=VS.85%29.aspx
235238
/// </remarks>
236-
[DllImport("security.dll", CharSet = CharSet.Auto, SetLastError = false)]
239+
[DllImport("security.dll", CharSet = CharSet.Unicode, SetLastError = false)]
237240
public static extern uint EnumerateSecurityPackages(ref uint numPackages, ref IntPtr securityPackageInfoArray);
238241

239242
/// <summary>
@@ -244,8 +247,10 @@ public static extern uint EncryptMessage(ref SspiHandle context,
244247
/// <remarks>
245248
/// http://msdn.microsoft.com/en-us/library/aa375416(v=vs.85).aspx
246249
/// </remarks>
247-
[DllImport("security.dll", CharSet = CharSet.None)]
250+
[DllImport("security.dll")]
251+
#if NET45
248252
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
253+
#endif
249254
public static extern uint FreeContextBuffer(IntPtr contextBuffer);
250255

251256
/// <summary>
@@ -256,8 +261,10 @@ public static extern uint EncryptMessage(ref SspiHandle context,
256261
/// <remarks>
257262
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa375417(v=vs.85).aspx
258263
/// </remarks>
259-
[DllImport("security.dll", CharSet = CharSet.None)]
264+
[DllImport("security.dll")]
265+
#if NET45
260266
[ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
267+
#endif
261268
public static extern int FreeCredentialsHandle(ref SspiHandle sspiHandle);
262269

263270
/// <summary>
@@ -338,11 +345,10 @@ public static extern uint InitializeSecurityContext(
338345
/// <remarks>
339346
/// http://msdn.microsoft.com/en-us/library/windows/desktop/aa379326(v=vs.85).aspx
340347
/// </remarks>
341-
[DllImport("security.dll", CharSet = CharSet.Auto, SetLastError = false)]
348+
[DllImport("security.dll", CharSet = CharSet.Unicode, SetLastError = false)]
342349
public static extern uint QueryContextAttributes(
343350
ref SspiHandle inContextHandle,
344351
QueryContextAttributes attribute,
345352
out SecurityPackageContextSizes sizes);
346353
}
347354
}
348-
#endif

src/MongoDB.Driver.Core/Core/Authentication/Sspi/QueryContextAttribute.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if NET45
1716
namespace MongoDB.Driver.Core.Authentication.Sspi
1817
{
1918
/// <summary>
@@ -31,4 +30,3 @@ internal enum QueryContextAttributes
3130
Sizes = 0x0
3231
}
3332
}
34-
#endif

src/MongoDB.Driver.Core/Core/Authentication/Sspi/SecurityBuffer.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if NET45
1716
using System;
1817
using System.Diagnostics.CodeAnalysis;
1918
using System.Runtime.InteropServices;
@@ -79,4 +78,3 @@ public void Free()
7978
}
8079
}
8180
}
82-
#endif

src/MongoDB.Driver.Core/Core/Authentication/Sspi/SecurityBufferDescriptor.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* limitations under the License.
1414
*/
1515

16-
#if NET45
1716
using System;
1817
using System.Diagnostics.CodeAnalysis;
1918
using System.Runtime.InteropServices;
@@ -65,18 +64,34 @@ public SecurityBufferDescriptor(SecurityBuffer[] buffers)
6564
NumBuffers = buffers.Length;
6665

6766
//Allocate memory for SecBuffer Array....
67+
#if NETSTANDARD1_6
68+
BufferPtr = Marshal.AllocHGlobal(Marshal.SizeOf< SecurityBuffer>() * NumBuffers);
69+
#else
6870
BufferPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(SecurityBuffer)) * NumBuffers);
71+
#endif
6972

7073
for (int i = 0; i < buffers.Length; i++)
7174
{
7275
var currentBuffer = buffers[i];
76+
#if NETSTANDARD1_6
77+
var currentOffset = i * Marshal.SizeOf<SecurityBuffer>();
78+
#else
7379
var currentOffset = i * Marshal.SizeOf(typeof(SecurityBuffer));
80+
#endif
7481
Marshal.WriteInt32(BufferPtr, currentOffset, currentBuffer.Count);
7582

83+
#if NETSTANDARD1_6
84+
var length = currentOffset + Marshal.SizeOf<int>();
85+
#else
7686
var length = currentOffset + Marshal.SizeOf(typeof(int));
87+
#endif
7788
Marshal.WriteInt32(BufferPtr, length, (int)currentBuffer.BufferType);
7889

90+
#if NETSTANDARD1_6
91+
length = currentOffset + Marshal.SizeOf<int>() + Marshal.SizeOf<int>();
92+
#else
7993
length = currentOffset + Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(int));
94+
#endif
8095
Marshal.WriteIntPtr(BufferPtr, length, currentBuffer.Token);
8196
}
8297
}
@@ -88,7 +103,11 @@ public void Free()
88103
{
89104
if (NumBuffers == 1)
90105
{
106+
#if NETSTANDARD1_6
107+
var buffer = Marshal.PtrToStructure<SecurityBuffer>(BufferPtr);
108+
#else
91109
var buffer = (SecurityBuffer)Marshal.PtrToStructure(BufferPtr, typeof(SecurityBuffer));
110+
#endif
92111
buffer.Free();
93112
}
94113
else
@@ -97,8 +116,13 @@ public void Free()
97116
// The 1st buffer is going to be empty. We can skip it.
98117
for (int i = 1; i < NumBuffers; i++)
99118
{
119+
#if NETSTANDARD1_6
120+
var currentOffset = i * Marshal.SizeOf<SecurityBuffer>();
121+
var totalLength = currentOffset + Marshal.SizeOf<int>() + Marshal.SizeOf<int>();
122+
#else
100123
var currentOffset = i * Marshal.SizeOf(typeof(SecurityBuffer));
101124
var totalLength = currentOffset + Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(int));
125+
#endif
102126
var buffer = Marshal.ReadIntPtr(BufferPtr, totalLength);
103127
Marshal.FreeHGlobal(buffer);
104128
}
@@ -125,7 +149,11 @@ public byte[] ToByteArray()
125149

126150
if (NumBuffers == 1)
127151
{
152+
#if NETSTANDARD1_6
153+
var buffer = Marshal.PtrToStructure< SecurityBuffer>(BufferPtr);
154+
#else
128155
var buffer = (SecurityBuffer)Marshal.PtrToStructure(BufferPtr, typeof(SecurityBuffer));
156+
#endif
129157

130158
if (buffer.Count > 0)
131159
{
@@ -139,17 +167,29 @@ public byte[] ToByteArray()
139167

140168
for (int i = 0; i < NumBuffers; i++)
141169
{
170+
#if NETSTANDARD1_6
171+
var currentOffset = i * Marshal.SizeOf<SecurityBuffer>();
172+
#else
142173
var currentOffset = i * Marshal.SizeOf(typeof(SecurityBuffer));
174+
#endif
143175
bytesToAllocate += Marshal.ReadInt32(BufferPtr, currentOffset);
144176
}
145177

146178
bytes = new byte[bytesToAllocate];
147179

148180
for (int i = 0, bufferIndex = 0; i < NumBuffers; i++)
149181
{
182+
#if NETSTANDARD1_6
183+
var currentOffset = i * Marshal.SizeOf<SecurityBuffer>();
184+
#else
150185
var currentOffset = i * Marshal.SizeOf(typeof(SecurityBuffer));
186+
#endif
151187
var bytesToCopy = Marshal.ReadInt32(BufferPtr, currentOffset);
188+
#if NETSTANDARD1_6
189+
var length = currentOffset + Marshal.SizeOf<int>() + Marshal.SizeOf<int>();
190+
#else
152191
var length = currentOffset + Marshal.SizeOf(typeof(int)) + Marshal.SizeOf(typeof(int));
192+
#endif
153193
IntPtr SecBufferpvBuffer = Marshal.ReadIntPtr(BufferPtr, length);
154194
Marshal.Copy(SecBufferpvBuffer, bytes, bufferIndex, bytesToCopy);
155195
bufferIndex += bytesToCopy;
@@ -160,4 +200,3 @@ public byte[] ToByteArray()
160200
}
161201
}
162202
}
163-
#endif

0 commit comments

Comments
 (0)