sspi 0.21.1

A Rust implementation of the Security Support Provider Interface (SSPI) API
Documentation
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
using AADJoin.Creds;
using AADJoin.Messages;
using AADJoin.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace AADJoin
{
    internal class AzureAD {
        /// <summary>
        /// Checks if the account associated with provided username exists and active
        /// </summary>
        /// <param name="username">Account username</param>
        /// <returns></returns>
        public static async Task CheckUsername(string username)
        {
            Console.WriteLine("Start {0} username checking...", username);
            var url = String.Format("https://login.microsoftonline.com/common/userrealm/{0}?api-version=1.0", username);

            var httpClient = new HttpClient();

            var response = await httpClient.GetAsync(url);
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine("Error. Status code: {0}. Body:", response.StatusCode);
                Console.WriteLine(content);
                Environment.Exit(1);
            }

            Console.WriteLine(content);
            Console.WriteLine("User exist and active");
        }

        /// <summary>
        /// Performs basic authorization in the Azure AD using account username and password.
        /// It's just account authorization and not related to any tenant.
        /// </summary>
        /// <param name="clientId">client id</param>
        /// <param name="user">Account credentials</param>
        /// <returns>AuthResponse object</returns>
        public static async Task<AuthResponse> AzureAdAuthorize(string clientId, UserCreds user)
        {
            Console.WriteLine("Start Azure AD authorization...");

            var url = "https://login.microsoftonline.com//common/oauth2/token";

            var httpClient = new HttpClient();

            var form = new Dictionary<string, string>();
            form.Add("grant_type", "password");
            form.Add("password", user.Password);
            form.Add("client_id", clientId);
            form.Add("username", user.Username);
            form.Add("resource", "https://graph.windows.net");
            form.Add("scope", "openid");

            var response = await httpClient.PostAsync(url, new FormUrlEncodedContent(form));
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine(string.Format("Error. Status code: {0}. Body:", response.StatusCode));
                Console.WriteLine(content);
                Environment.Exit(1);
            }

            Console.WriteLine(content);
            var authResponse = JsonSerializer.Deserialize<AuthResponse>(content);
            Console.WriteLine("Azure AD authorization succeeded!");

            return authResponse;
        }

        /// <summary>
        /// Performs user authorization in the tenant using previously obtained refresh token.
        /// </summary>
        /// <param name="clientId">client id</param>
        /// <param name="authResponse">AuthResponse object returned from the AzureADAuthorize method</param>
        /// <returns>TenantAuthResponse object</returns>
        public static async Task<TenantAuthResponse> AzureTenantAuthorize(string clientId, AuthResponse authResponse)
        {
            Console.WriteLine("Start Azure Tenant authorization...");

            var url = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", authResponse.GetTenantId());

            var httpClient = new HttpClient();

            var form = new Dictionary<string, string>();
            form.Add("scope", "openid");
            form.Add("grant_type", "refresh_token");

            // https://github.com/Gerenios/AADInternals/blob/master/AccessToken.ps1#L1864
            // 01cb2876-7ebd-4aa4-9cc9-d28bd4d359a9 means urn:ms-drs:enterpriseregistration.windows.net
            // More UUIDs: https://www.rickvanrousselt.com/blog/azure-default-service-principals-reference-table/
            form.Add("resource", "01cb2876-7ebd-4aa4-9cc9-d28bd4d359a9");

            form.Add("client_id", clientId);
            form.Add("refresh_token", authResponse.refresh_token);

            var response = await httpClient.PostAsync(url, new FormUrlEncodedContent(form));
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine(string.Format("Error. Status code: {0}. Body:", response.StatusCode));
                Console.WriteLine(content);
                Environment.Exit(1);
            }

            Console.WriteLine(content);
            var tenantAuthResponse = TenantAuthResponse.FromString(content);
            Console.WriteLine("Azure Tenant authorization succeeded!");

            return tenantAuthResponse;
        }

        /// <summary>
        /// Joins new device to the Azure AD. Automatically creates new device key/certificate, and id. The device id is
        /// a randomly generated UUID. The private key password is the device id.
        /// Saves them in corresponding files:
        /// * `{device_id}.key` - the device private key; {device_id} is a password from the key;
        /// * `{random_id}.csr` - the device certificate request that has been used to obtain the device certificate;
        /// * `{device_id}.cer` - the device certificate;
        /// </summary>
        /// <param name="domain">Azure AD domain</param>
        /// <param name="tenantAuthResponse">TenantAuthResponse object returned from the AzureTenantAuthorize method</param>
        /// <returns>Device creds object</returns>
        public static async Task<DeviceCreds> JoinDevice(string domain, TenantAuthResponse tenantAuthResponse)
        {
            Console.WriteLine("Start device joining...");

            var url = "https://enterpriseregistration.windows.net/EnrollmentServer/device/?api-version=1.0";

            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tenantAuthResponse.access_token);

            var deviceId = Guid.NewGuid();
            // minimal key size = 2048
            var deviceKey = new RSACng(2048);

            var joinDeviceRequest = new JoinDeviceRequest(domain, deviceKey, deviceId);

            var data = new StringContent(joinDeviceRequest.ToString());
            data.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            var response = await httpClient.PostAsync(url, data);
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine(string.Format("Error. Status code: {0}. Body:", response.StatusCode));
                Console.WriteLine(content);
                Environment.Exit(1);
            }

            Console.WriteLine(content);
            var joinDeviceResponse = JoinDeviceResponse.FromString(content);

            var certificate = new X509Certificate(Convert.FromBase64String(joinDeviceResponse.Certificate.RawBody));
            var device = new DeviceCreds(deviceKey, certificate);

            Console.WriteLine("The device transport key password: {0}", device.Id);
            Console.WriteLine(
                "The device transport key (PKCS8) has been written into the file: {0}",
                CryptoUtils.RsaToPkcs8File(deviceKey, device.Id, device.Id)
            );

            Console.WriteLine(
                "The device certificate (.pfx) has been written into the file: {0}",
                CryptoUtils.PemCrtToPfxFile(joinDeviceResponse.Certificate.RawBody, deviceKey, device.Id, device.Id)
            );

            Console.WriteLine(
                "The device certificate (.cer) has been written into the file: {0}",
                CryptoUtils.PemCrtToFile(joinDeviceResponse.Certificate.RawBody, device.Id)
            );

            Console.WriteLine("Device joining succeeded!");

            return device;
        }

        /// <summary>
        /// Requests nonce from the Azure AD tenant.
        /// </summary>
        /// <param name="tenantId">Tenant UUID</param>
        /// <returns>Base64 encoded nonce</returns>
        private static async Task<string> RequestNonce(string tenantId)
        {
            Console.WriteLine("Start obtaining nonce...");

            var httpClient = new HttpClient();

            var url = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", tenantId);

            var nonceForm = new Dictionary<string, string>();
            nonceForm.Add("grant_type", "srv_challenge");

            var nonceResponse = await httpClient.PostAsync(url, new FormUrlEncodedContent(nonceForm));
            var nonceContent = await nonceResponse.Content.ReadAsStringAsync();

            if (nonceResponse.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine(string.Format("Error. Status code: {0}. Body:", nonceResponse.StatusCode));
                Console.WriteLine(nonceContent);
                Environment.Exit(1);
            }

            Console.WriteLine(nonceContent);

            var document = JsonDocument.Parse(nonceContent);
            var nonce = document.RootElement.GetProperty("Nonce");

            return nonce.ToString();
        }

        /// <summary>
        /// Obtains refresh and access token with the TGT.
        /// </summary>
        /// <param name="user">Account credentials</param>
        /// <param name="tenantId">Tenant id</param>
        /// <param name="nonce">Previously obtained nonce</param>
        /// <param name="device">Device creds (private key and certificate)</param>
        /// <returns>JSON response from AzureAD as string</returns>
        private static async Task<string> TokenWithTgt(
            UserCreds user,
            string tenantId,
            string nonce,
            DeviceCreds device
        )
        {
            Console.WriteLine("Start TokenWithTGT...");

            var httpClient = new HttpClient();

            var url = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", tenantId);

            var certForm = new Dictionary<string, string>();
            certForm.Add("request", JwtUtils.GenerateFRequestJwt(
                nonce,
                device,
                user
            ));
            certForm.Add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
            certForm.Add("client_info", "1");
            certForm.Add("tgt", "true");
            certForm.Add("windows_api_version", "2.2");

            var response = await httpClient.PostAsync(url, new FormUrlEncodedContent(certForm));
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine(string.Format("Error. Status code: {0}. Body:", response.StatusCode));
                Console.WriteLine(content);

                Environment.Exit(1);
            }

            Console.WriteLine(content);
            return content;
        }

        /// <summary>
        /// Checks if the provided domain exists and active
        /// </summary>
        /// <param name="domain">AzureAD domain</param>
        /// <returns></returns>
        private static async Task CheckDomain(string domain)
        {
            var url = String.Format("https://login.microsoftonline.com/common/UserRealm/?user={0}&api-version=1.0&checkForMicrosoftAccount=false&fallback_domain={1}", domain, domain);

            var httpClient = new HttpClient();

            var response = await httpClient.GetAsync(url);
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine("Error. Status code: {0}. Body:", response.StatusCode);
                Console.WriteLine(content);
                Environment.Exit(1);
            }

            Console.WriteLine(content);
            Console.WriteLine("Domain exist and active");
        }

        /// <summary>
        /// Obtains client P2P certificates ans saves them into the corresponding files.
        /// </summary>
        /// <param name="user">Account credentials</param>
        /// <param name="tenantId">Tenant id</param>
        /// <param name="device">Device credentials (private key and certificate)</param>
        /// <returns></returns>
        public static async Task ObtainClientP2PCertificate(UserCreds user, string tenantId, DeviceCreds device)
        {
            Console.WriteLine("Start obtaining client P2P certificates...");

            await CheckDomain(user.Domain);

            var nonce = await RequestNonce(tenantId);
            Console.WriteLine("First nonce: {0}", nonce);

            var response = await TokenWithTgt(user, tenantId, nonce, device);

            var responseJson = JsonDocument.Parse(response);
            var refreshToken = responseJson.RootElement.GetProperty("refresh_token").ToString();
            var sessionKeyJwe = responseJson.RootElement.GetProperty("session_key_jwe").ToString();
            var sessionKey = CryptoUtils.DecryptSessionKeyFromJwe(sessionKeyJwe, device.Key);

            nonce = await RequestNonce(tenantId);
            Console.WriteLine("Second nonce: {0}", nonce);

            // randomly generated nonce
            // we can hard code it for our tool
            byte[] context = { 25, 152, 185, 126, 55, 118, 199, 221, 254, 108, 255, 202, 88, 128, 76, 218, 200, 157, 211, 63, 242, 37, 152, 198 };
            var contextBase64 = "GZi5fjd2x93+bP/KWIBM2sid0z/yJZjG";

            var signingKey = CryptoUtils.DeriveSigningKey(sessionKey, context);

            var certificateJwe = await RdpCertificate(
                nonce,
                contextBase64,
                tenantId,
                refreshToken,
                signingKey,
                user,
                device
            );

            var certificates = CryptoUtils.DecryptCeritficate(sessionKey, CryptoUtils.ExtractContextFromJwe(certificateJwe), certificateJwe);
            var id = string.Format("{0}_client_auth", device.Id);

            Console.WriteLine(
                "The client P2P certificate (.cer) has been written into the file: {0}",
                CryptoUtils.PemCrtToFile(certificates[0], id + "_p2p")
            );

            Console.WriteLine(
                "The client P2P CA certificate (.cer) has been written into the file: {0}",
                CryptoUtils.PemCrtToFile(certificates[1], id + "_p2p_ca")
            );

            Console.WriteLine("Finished obtaining client P2P certificates!");
        }

        private static async Task<string> RdpCertificate(
            string nonce,
            string context,
            string tenantId,
            string refreshToken,
            byte[] signingKey,
            UserCreds user,
            DeviceCreds device
        )
        {
            Console.WriteLine("Start RDP certificate obtaining...");

            var httpClient = new HttpClient();

            var url = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", tenantId);

            var certForm = new Dictionary<string, string>();
            certForm.Add("request", JwtUtils.GenerateRdpCertificateRequestJwt(
                nonce,
                context,
                user.Username,
                refreshToken,
                signingKey,
                device.Key
            ));
            certForm.Add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
            certForm.Add("windows_api_version", "2.2");

            var response = await httpClient.PostAsync(url, new FormUrlEncodedContent(certForm));
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine(string.Format("Error. Status code: {0}. Body:", response.StatusCode));
                Console.WriteLine(content);

                Environment.Exit(1);
            }

            Console.WriteLine(content);
            return content;
        }

        /// <summary>
        /// Obtains P2P certificate for server autherization using device key and certificate. Saves certificates in the 
        /// corresponding files:
        /// * `{device_id}_server_auth_p2p.cer`
        /// * `{device_id}_server_auth_p2p_ca.cer`
        /// </summary>
        /// <param name="domain">AzureAD domain</param>
        /// <param name="tenantId">Tenant id</param>
        /// <param name="device">Device credentials</param>
        /// <returns></returns>
        public static async Task ObtainServerP2PCertificate(string domain, string tenantId, DeviceCreds device)
        {
            Console.WriteLine("Start obtaining server P2P certificates...");

            var url = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", tenantId);

            var nonce = await RequestNonce(tenantId);

            var certForm = new Dictionary<string, string>();
            certForm.Add("request", JwtUtils.GenerateP2PRequestJwt(
                nonce,
                device,
                string.Format("tenjo.{0}", domain)
            ));
            certForm.Add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
            certForm.Add("windows_api_version", "2.0");

            var httpClient = new HttpClient();

            var response = await httpClient.PostAsync(url, new FormUrlEncodedContent(certForm));
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine(string.Format("Error. Status code: {0}. Body:", response.StatusCode));
                Console.WriteLine(content);

                Environment.Exit(1);
            }

            Console.WriteLine(content);
            var p2pCerificatesResponse = P2PCertificatesResponse.FromString(content);
            var id = string.Format("{0}_server_auth", device.Id);

            Console.WriteLine(
                "The server P2P certificate (.cer) has been written into the file: {0}",
                CryptoUtils.PemCrtToFile(p2pCerificatesResponse.x5c, id + "_p2p")
            );

            Console.WriteLine(
                "The server P2P CA certificate (.cer) has been written into the file: {0}",
                CryptoUtils.PemCrtToFile(p2pCerificatesResponse.x5c_ca, id + "_p2p_ca")
            );

            Console.WriteLine("Finished obtaining server P2P certificates!");
        }

        /// <summary>
        /// Obtains P2P certificates.
        /// </summary>
        /// <param name="deviceId">Device id</param>
        /// <param name="domain">Azure AD domain</param>
        /// <param name="tenantId">Azure AD tenant id</param>
        /// <param name="device">Device credentials (private key and certificate)</param>
        /// <returns></returns>
        public static async Task ObtainP2PCertificates(string deviceId, string domain, string tenantId, DeviceCreds device)
        {
            Console.WriteLine("Start obtaining device P2P certificates...");

            var httpClient = new HttpClient();

            var url = string.Format("https://login.microsoftonline.com/{0}/oauth2/token", tenantId);

            var nonceForm = new Dictionary<string, string>();
            nonceForm.Add("grant_type", "srv_challenge");

            var nonceResponse = await httpClient.PostAsync(url, new FormUrlEncodedContent(nonceForm));
            var nonceContent = await nonceResponse.Content.ReadAsStringAsync();

            if (nonceResponse.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine(string.Format("Error. Status code: {0}. Body:", nonceResponse.StatusCode));
                Console.WriteLine(nonceContent);
                Environment.Exit(1);
            }

            Console.WriteLine(nonceContent);

            var document = JsonDocument.Parse(nonceContent);
            var nonce = document.RootElement.GetProperty("Nonce");

            var certForm = new Dictionary<string, string>();
            certForm.Add("request", JwtUtils.GenerateP2PRequestJwt(
                nonce.ToString(),
                device,
                string.Format("mypc.{0}", domain)
            ));
            certForm.Add("grant_type", "urn:ietf:params:oauth:grant-type:jwt-bearer");
            certForm.Add("windows_api_version", "2.0");

            var response = await httpClient.PostAsync(url, new FormUrlEncodedContent(certForm));
            var content = await response.Content.ReadAsStringAsync();

            if (response.StatusCode != HttpStatusCode.OK)
            {
                Console.WriteLine(string.Format("Error. Status code: {0}. Body:", response.StatusCode));
                Console.WriteLine(content);

                Environment.Exit(1);
            }

            Console.WriteLine(content);
            var p2pCerificatesResponse = P2PCertificatesResponse.FromString(content);

            Console.WriteLine(
                "The device P2P certificate (.cer) has been written into the file: {0}",
                CryptoUtils.PemCrtToFile(p2pCerificatesResponse.x5c, deviceId + "_p2p")
            );

            Console.WriteLine(
                "The device P2P CA certificate (.cer) has been written into the file: {0}",
                CryptoUtils.PemCrtToFile(p2pCerificatesResponse.x5c_ca, deviceId + "_p2p_ca")
            );

            Console.WriteLine("Finished obtaining device P2P certificates!");
        }
    }
}