File: SspiSession.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (273 lines) | stat: -rwxr-xr-x 8,339 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
using System;
using System.IO;
using System.Net;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using Mono.Security;
using Mono.Security.Protocol.Ntlm;

namespace System.ServiceModel.Security
{
	internal abstract class SspiSession
	{
		internal static readonly byte [] NtlmSSP = new byte [] {
			0x4E, 0x54, 0x4C, 0x4D, 0x53, 0x53, 0x50, 0x00};

		public long Challenge, Context, ClientOSVersion, ServerOSVersion;
		public string ServerName, DomainName, DnsHostName, DnsDomainName;

		public bool Verify (byte [] expected, byte [] actual, int offset, int length)
		{
			if (expected.Length != length)
				return false;
			for (int i = 0; i < length; i++)
				if (expected [i] != actual [i + offset])
					return false;
			return true;
		}

		public SspiSecurityBufferStruct ReadSecurityBuffer (BinaryReader reader)
		{
			return new SspiSecurityBufferStruct (
				reader.ReadInt16 (),
				reader.ReadInt16 (),
				reader.ReadInt32 ());
		}
	}

	internal struct SspiSecurityBufferStruct
	{
		public SspiSecurityBufferStruct (short length, short allocatedSpace, int offset)
		{
			Length = length;
			AllocatedSpace = allocatedSpace;
			Offset = offset;
		}

		public readonly short Length;
		public readonly short AllocatedSpace;
		public readonly int Offset;
	}

	internal class SspiClientSession : SspiSession
	{
		Type2Message type2;
		Type3Message type3;

		// Class(60) {
		//   OID(spnego),
		//   Class(A0) {
		//     Class(30) {
		//       Class(A0) {
		//         Class(30) { OID,OID,OID} },
		//       Class(A2) { OctetStream } } } }
		public byte [] ProcessSpnegoInitialContextTokenRequest ()
		{
			Type1Message type1 = new Type1Message (NtlmVersion.Version3);
			type1.Flags = unchecked ((NtlmFlags) 0xE21882B7);
			type1.Domain = "WORKGROUP"; // FIXME: remove it

			ASN1 asn = new ASN1 (0x60);
			ASN1 asn2 = new ASN1 (0xA0);
			ASN1 asn21 = new ASN1 (0x30);
			ASN1 asn211 = new ASN1 (0xA0);
			ASN1 asn2111 = new ASN1 (0x30);
			asn211.Add (asn2111);
			asn2111.Add (ASN1Convert.FromOid (Constants.OidNtlmSsp));
			asn2111.Add (ASN1Convert.FromOid (Constants.OidKerberos5));
			asn2111.Add (ASN1Convert.FromOid (Constants.OidMIT));
			ASN1 asn212 = new ASN1 (0xA2);
			ASN1 asn2121 = new ASN1 (0x4);
			asn2121.Value = type1.GetBytes ();
			asn212.Add (asn2121);
			asn21.Add (asn211);
			asn21.Add (asn212);
			asn2.Add (asn21);
			asn.Add (ASN1Convert.FromOid (Constants.OidSpnego));
			asn.Add (asn2);
			return asn.GetBytes ();
		}

		// Example buffer:
		// A18181 307F A003
		//   0A0101
		//   A10C 060A2B06010401823702020A
		//   A26A 0468 NTLM
		//   NTLM = 4E544C4D53535000 0200000004000400 3800000035829AE2
		//    0D1A7FF0F171F339 0000000000000000 2C002C003C000000
		//    0501280A0000000F 5000430002000400 5000430001000400
		//    5000430004000400 5000430003000400 5000430006000400
		//    0100000000000000
		public void ProcessSpnegoInitialContextTokenResponse (byte [] raw)
		{
			ASN1 asn1 = new ASN1 (raw);
			// FIXME: check OIDs and structure
			ProcessMessageType2 (asn1 [0] [2] [0].Value);
		}

		// Class { Class { Class { OctetStream } } }
		public byte [] ProcessSpnegoProcessContextToken (string user, string pass)
		{
			ASN1 asn = new ASN1 (0xA1);
			ASN1 asn2 = new ASN1 (0x30);
			ASN1 asn3 = new ASN1 (0xA2);
			asn3.Add (new ASN1 (0x04, ProcessMessageType3 (user, pass)));
			asn2.Add (asn3);
			asn.Add (asn2);
			return asn.GetBytes ();
		}

		public byte [] ProcessMessageType1 ()
		{
			Type1Message type1 = new Type1Message (NtlmVersion.Version3);
			type1.Flags = unchecked ((NtlmFlags) 0xE21882B7);
			return type1.GetBytes ();
		}

		string TargetName;

		public void ProcessMessageType2 (byte [] raw)
		{
			type2 = new Type2Message (raw);
		}

		public byte [] ProcessMessageType3 (string user, string password)
		{
			TargetName = Environment.MachineName;
			ServerName = Environment.MachineName;
			// FIXME
			DomainName = ServerName;// IPGlobalProperties.GetIPGlobalProperties ().DomainName;
			DnsHostName = Dns.GetHostName ();
			DnsDomainName = DnsHostName; // FIXME

			type3 = new Type3Message (NtlmVersion.Version3);
			type3.Flags = (NtlmFlags) (unchecked ((int) 0xE2188235));
			type3.Domain = DomainName;
			type3.Host = DnsHostName;
			type3.Challenge = type2.Nonce;
			type3.Username = user;
			type3.Password = password;

			return type3.GetBytes ();
		}
	}

	internal class SspiServerSession : SspiSession
	{
		public string TargetName;
		public long SuppliedDomain, SuppliedWorkstation;
		Type1Message type1;
		Type2Message type2;
		Type3Message type3;

		// Example buffer:
		// 6069 0606 2B0601050502 A05F 305D A024 3022
		//	  060A 2B06010401823702020A
		//	  0609 2A864882F712010202
		//	  0609 2A864886F712010202
		// A235 0433 NTLM
		// NTLM = 4E544C4D53535000 01000000 B7B218E2 090009002A000000
		//  0200020028000000 0501280A0000000F 5043 574F524B47524F5550
		public void ProcessSpnegoInitialContextTokenRequest (byte [] raw)
		{
			ASN1 asn1 = new ASN1 (raw);
			// FIXME: check OIDs
			ProcessMessageType1 (asn1 [1] [0] [1] [0].Value);
		}

		// Class {
		//   Class {
		//     Class { Enum },
		//     Class { OID(NTLMSSP) },
		//     Class { OctetStream } } }
		public byte [] ProcessSpnegoInitialContextTokenResponse ()
		{
			ASN1 top = new ASN1 (0xA1);
			ASN1 asn = new ASN1 (0x30);
			ASN1 asn1 = new ASN1 (0xA0);
			// FIXME: what is this enum?
			asn1.Add (new ASN1 (0x0A, new byte [] {1})); // Enum whatever
			ASN1 asn2 = new ASN1 (0xA1);
			asn2.Add (ASN1Convert.FromOid (Constants.OidNtlmSsp));
			ASN1 asn3 = new ASN1 (0xA2);
			asn3.Add (new ASN1 (0x04, ProcessMessageType2 ()));
			asn.Add (asn1);
			asn.Add (asn2);
			asn.Add (asn3);
			top.Add (asn);
			return top.GetBytes ();
		}

		// Example buffer:
		// A181A7
		//   3081A4
		//     A281A1
		//       04819E
		// 4E544C4D53535000 03000000 
		// 180018005E000000 1800180076000000 0400040048000000
		// 0E000E004C000000 040004005A000000 100010008E000000
		// 358218E2 0501280A0000000F
		// 50004300 6100740073007500730068006900 50004300
		// [8 bytes LM] [16 bytes of 0s]
		// [24 bytes of NTLM]
		// C94EE2ADE7E32244 BD60D3B33609C167
		public void ProcessSpnegoProcessContextToken (byte [] raw)
		{
			ASN1 asn1 = new ASN1 (raw);
			// FIXME: check structure
			ProcessMessageType3 (asn1 [0] [0] [0].Value);
		}

		public void ProcessMessageType1 (byte [] raw)
		{
			type1 = new Type1Message (raw, NtlmVersion.Version3);
		}

		public byte [] ProcessMessageType2 ()
		{
			byte [] bytes = new byte [8];
			RandomNumberGenerator.Create ().GetNonZeroBytes (bytes);
			Challenge = bytes [0] << 24 + bytes [1] << 16 + bytes [2] << 8 + bytes [3];
			Context = 0; // FIXME
			ServerOSVersion = 0x0F00000A28010500; // FIXME
			TargetName = Environment.MachineName;
			ServerName = Environment.MachineName;
			// FIXME
			DomainName = ServerName;// IPGlobalProperties.GetIPGlobalProperties ().DomainName;
			DnsHostName = Dns.GetHostName ();
			DnsDomainName = DnsHostName; // FIXME

			type2 = new Type2Message (NtlmVersion.Version3);
			type2.Flags = (NtlmFlags) (unchecked ((int) 0xE21882B7));
			type2.TargetName = TargetName;
			type2.Target.ServerName = ServerName;
			type2.Target.DomainName = DomainName;
			type2.Target.DnsHostName = DnsHostName;
			type2.Target.DnsDomainName = DnsDomainName;
			return type2.GetBytes ();
		}

		public void ProcessMessageType3 (byte [] raw)
		{
			/*
			MemoryStream ms = new MemoryStream (raw);
			if (!Verify (NtlmSSP, raw, 0, 8))
				throw new SecurityNegotiationException ("Expected NTLM SSPI header not found");
			BinaryReader reader = new BinaryReader (ms);
			reader.ReadInt64 (); // skip 8 bytes
			if (reader.ReadInt32 () != 3)
				throw new SecurityNegotiationException ("SSPI type 3 message is expected");
			SspiSecurityBufferStruct lmResInfo = ReadSecurityBuffer (reader);
			SspiSecurityBufferStruct ntlmResInfo = ReadSecurityBuffer (reader);
			SspiSecurityBufferStruct targetNameInfo = ReadSecurityBuffer (reader);
			SspiSecurityBufferStruct userNameInfo = ReadSecurityBuffer (reader);
			SspiSecurityBufferStruct wsNameInfo = ReadSecurityBuffer (reader);
			SspiSecurityBufferStruct sessionKeyInfo = ReadSecurityBuffer (reader);
			int flags = reader.ReadInt32 ();
			ServerOSVersion = reader.ReadInt64 ();
			*/
			type3 = new Type3Message (raw, NtlmVersion.Version3);
		}
	}
}